|
From: Thomas L. S. <tsh...@io...> - 2010-07-27 04:40:21
|
At 01:46 PM 7/26/2010, Ken S. wrote: >I found that the mod_rewrite modules is going to fix my problem as >there is already an example on how to do this with filenames. >However, if I combine the technique to apply to directories, it fails >for me. > >The problem I am trying to fix is that I have a user who insists on >uploading files and directories with spaces in them, but I also have a >script that watches the xfer_log for uploads and if there is a space >in the directory or file name, then proftpd replaces that with an >underscore in the log entry thus breaking the script. > >The last example on this page >(http://www.proftpd.org/docs/contrib/mod_rewrite.html) shows how to >remove whitespace from the file name. Here is a snippet: > > # We only want to use this rule on STOR commands > RewriteCondition %m STOR > > # Apply the map to the command parameters > RewriteRule ^(.*) "${replace:/$1/ /_}" > >I thought I would modify it by adding a block for the MKD command. >That works for a single test (the directory has a space but not a >file) but not if each have a space. Then looking at the debug log I >saw that it was doing a CWD and realized that if the filename still >contained the space but the directory had been created with an >underscore that the CWD would fail, so I added that as well. Here is >what my block from proftpd.conf looks like now: > ><IfModule mod_rewrite.c> > RewriteEngine on > RewriteLog /var/log/ftpd_rewrite.log > > # Define a map that uses the internal "replaceall" function > RewriteMap replace int:replaceall > > # We only want to use this rule on STOR commands > #RewriteCondition %m STOR [OR] > #RewriteCondition %m MKD [OR] > #RewriteCondition %m CWD > # Apply the map to the command parameters > #RewriteRule ^(.*) "${replace:/$1/ /_}" > > RewriteCondition %m STOR > RewriteRule ^(.*) "${replace:/$1/ /_}" > RewriteCondition %m MKD > RewriteRule ^(.*) "${replace:/$1/ /_}" > RewriteCondition %m CWD > RewriteRule ^(.*) "${replace:/$1/ /_}" ></IfModule> > >I tried it with the [OR] block but couldn't get it to work that way either. > >Does anyone see anything that I am missing? Thanks for any >suggestions you can offer. There is another end to play with here, and you mentioned it above. The program that monitors the xferlog can attempt to "not care" whether spaces have been replaced by underscores, and yet correctly identify the actual filename. I do this, and it works for me. (I don't think anyone has ever noticed a failing case in the last 10+ years (!) ) The problem is xferlog'ing replaces embedded spaces with underscores, so that this one logged data item stays one single 'column' in the space-separated 'record'. We can then have an ambiguity - is the underscore there because the filepath had a space, or did it originally have the underscore? I grab xferlog records and check the filepath item. If there isn't an underscore I just pass it along for processing. If there are one or more underscores then I consider the xferlog filepath a "matching mask". I replace each underscore in the filepath with a regex match clause that will match _either_ an underscore or a space. In my case I use Perl and I do # Create a filename match mask by replacing all '_' chars # with a character class matching either '_' or space, # after escaping every other character. my $matchmask = quotemeta $upldfilename; $matchmask =~ s/_/[ _]/g; The "[ _]" will, for that position, match either a space or an underscore - we make both acceptable. Later after having grabbed a list of all filenames in the upload directory I just use the match mask to hopefully find the one correct filename. my @matchedfiles = grep { m/^$matchmask$/ } @allfiles; if( @matchedfiles == 1 ) { If more than one matched we'd still have the ambiguity. We could try to match sizes, date/times, etc. but it seems in practice that the problem doesn't come up. But if we come up with just one, we've found the 'real' filename and we're done. Now looking at my code and re-reading your description I'm not sure I correctly handle directory names with spaces. It seems I don't do the stepwise refinement, starting from the highest level initial directory and matching each path part against the list of filenames/dirnames in that directory. I'll have to test that maybe tomorrow. But the principle should work for you, of not worrying about rewrite (and messing with the users' minds), but rather equipping the xferlog monitoring program to handle ambiguities and "just work" in spite of them. >-ken >-- >Have a nice day ... unless you've made other plans. > >------------------------------------------------------------------------------ >The Palm PDK Hot Apps Program offers developers who use the >Plug-In Development Kit to bring their C/C++ apps to Palm for a share >of $1 Million in cash or HP Products. Visit us here for more details: >http://ad.doubleclick.net/clk;226879339;13503038;l? >http://clk.atdmt.com/CRS/go/247765532/direct/01/ >_______________________________________________ >ProFTPD Users List <pro...@pr...> >Unsubscribe problems? >http://www.proftpd.org/list-unsub.html |