Debian Codegraph @

doc/howto/Rewrite.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>ProFTPD: Using mod_rewrite</title>
5 </head>
6
7 <body bgcolor=white>
8
9 <hr>
10 <center><h2><b><i>ProFTPD: Using the <code>mod_rewrite</code> Module</i></b></h2></center>
11 <hr>
12
13 <p>
14 The <a href="../contrib/mod_rewrite.html"><code>mod_rewrite</code></a> module for <code>proftpd</code> is a powerful tool
15 for rewriting FTP commands received from clients. It has been used to
16 automatically append (or remove) domain names from logins, to translate
17 Windows paths (using backslashes) to Unix paths (using slashes), to handle
18 case-insensitive files, <i>etc</i>. One of the great things about
19 <code>mod_rewrite</code> is that <b>any</b> modification made to the commands
20 is transparent to the client; that is, FTP clients are completely unaware
21 that their commands are being changed on-the-fly.
22
23 <p>
24 The following is a collection of examples of how <code>mod_rewrite</code>
25 has been used. If you use <code>mod_rewrite</code> and would like to contribute
26 your recipe/configuration, please let us know!
27
28 <p>
29 Since much of <code>mod_rewrite</code>'s power is based on regular expressions
30 and pattern matching, I highly recommend that you read through this
31 introduction to POSIX regular expressions, and use the <a href="http://www.castaglia.org/proftpd/doc/contrib/regex.html"><code>regex</code></a> tool for
32 testing out your regexes against paths/strings:
33 <pre>
34 <a href="http://www.castaglia.org/proftpd/doc/contrib/regexp.html">http://www.castaglia.org/proftpd/doc/contrib/regexp.html</a>
35 </pre>
36
37 <p>
38 <b>Case Sensitivity</b><br>
39 The following example configuration shows how to configure
40 <code>mod_rewrite</code> so that all files uploaded to the FTP server will have
41 all-uppercase filenames:
42 <pre>
43 &lt;IfModule mod_rewrite.c&gt;
44 RewriteEngine on
45
46 # Have a log for double-checking any errors
47 RewriteLog /var/log/ftpd/rewrite.log
48
49 # Define a map that uses the internal "toupper" function
50 RewriteMap uppercase int:toupper
51
52 # Make the file names used by STOR be in all uppercase
53 RewriteCondition %m STOR
54
55 # Apply the map to the command parameters
56 RewriteRule ^(.*) ${uppercase:$1}
57 &lt;/IfModule&gt;
58 </pre>
59
60 <p>
61 What if you wanted to make the filename always be uppercase for uploaded
62 files, but <b>not</b> any directories in the path leading up the file name?
63 Using the above, if you did:
64 <pre>
65 ftp&gt; cd /upload
66 ftp&gt; put file1.txt
67 </pre>
68 The file would appear as "/upload/FILE1.TXT". <i>But</i> if you did:
69 <pre>
70 ftp&gt; put /upload/file1.txt
71 </pre>
72 the file would appear as "/UPLOAD/FILE1.TXT", which may not be what you want.
73 To handle this, you need to change the "^(.*)" pattern in the
74 above <code>RewriteRule</code> directive. The "^(.*)" regular expression
75 matches the entire parameter string. Instead, you might try this pattern:
76 <pre>
77 RewriteRule (.*/)?(.*)$ ${uppercase:$2}
78 </pre>
79 which tries to isolate into match group 2 (<i>i.e.</i> <code>$2</code>) the
80 part of the argument string which is not followed by any slashes.
81
82 <p>
83 Somewhat similar is the situation where the admin found, for case-sensitivity
84 reasons, that it was easier to rewrite <b>all</b> FTP commands (except
85 <code>PASS</code>, since passwords are case-sensitive) to be lowercase:
86 <pre>
87 &lt;IfModule mod_rewrite.c&gt;
88 RewriteEngine on
89
90 # Define a map that uses the internal "tolower" function
91 RewriteMap lowercase int:tolower
92
93 # Rewrite all commands except PASS
94 RewriteCondition %m !PASS
95
96 RewriteRule ^(.*) ${lowercase:$1}
97 &lt;/IfModule&gt;
98 </pre>
99 This means an FTP client can refer to "/DiR/Dir2/FiLe" when on the server the
100 file is actually "/dir/dir2/file"; it works for uploads, too. (This works
101 especially well for Windows clients.)
102
103 <p>
104 <b>Trimming Whitespace</b><br>
105 Some FTP clients can properly handle files whose names start with spaces,
106 but other FTP clients cannot. If your users use whatever FTP clients they
107 wish, you need may need to deal with this situation on the server side of
108 things. The <code>mod_rewrite</code> module can help with this, by
109 automatically trimming leading/trailing whitespace from commands.
110 <pre>
111 &lt;IfModule mod_rewrite.c&gt;
112 RewriteEngine on
113
114 # Trim whitespace for commands dealing with files
115 RewriteCondition %m LIST|MLST|NLST|RETR|STAT|STOR
116
117 # Only the portion of the command that matches our rule is used.
118 # This works by saying "match all non-space characters".
119 RewriteRule [^[:space:]]+ $0
120 &lt;/IfModule&gt;
121 </pre>
122
123 <p>
124 <b>Changing the Filenames</b><br>
125 One user had the following problem: Files uploaded via a web browser had their
126 filenames changed by the browser. Specifically, the web browser changed
127 any spaces in the filenames to "%20" (URL encoding for a space character).
128 Fortunately, the user was able to use <code>mod_rewrite</code> to undo the
129 change or, as shown below, to change that "%20" to an underscore:
130 <pre>
131 &lt;IfModule mod_rewrite.c&gt;
132 RewriteEngine on
133
134 # Define a map that uses the internal "replaceall" function
135 RewriteMap replace int:replaceall
136
137 # We only want to use this rule on STOR commands
138 RewriteCondition %m STOR
139
140 # Apply the map to the command parameters. Use '!' as the delimiter,
141 # not '/', as the path sent might contain slashes
142 RewriteRule ^(.*) "${replace:!$1!%20!_}"
143 &lt;/IfModule&gt;
144 </pre>
145
146 <p>
147 Another site wanted to "tag" each uploaded file name with the current process
148 ID (PID), to ensure some sort of file name uniqueness. Enter
149 <code>mod_rewrite</code>!
150 <pre>
151 &lt;IfModule mod_rewrite.c&gt;
152 RewriteEngine on
153
154 RewriteCondition %m STOR
155 RewriteRule (.*) $1.%P
156 &lt;/IfModule&gt;
157 </pre>
158 This appends the PID of the current session process to any uploaded filename.
159 For more variables like <code>%P</code>, see the <a href="../contrib/mod_rewrite.html#RewriteCondition"><code>RewriteCondition</code></a> and <a href="../contrib/mod_rewrite.html#RewriteRule"><code>RewriteRule</code></a> descriptions.
160
161 <p>
162 <b>Replacing Backslashes With Slashes</b><br>
163 Some sites have FTP clients which seem to send <code>CWD</code> and
164 <code>RETR</code>/<code>STOR</code> commands which use Windows-style
165 backslashes, <i>e.g.</i> "path\to\file". And ideally, these sites would like
166 to work seamlessly with such clients, without having to get the clients to
167 change. Can <code>mod_rewrite</code> be used to change those backslashes
168 into more Unix-friendly regular slashes? Absolutely.
169
170 The following <code>mod_rewrite</code> configuration should do the trick:
171 <pre>
172 &lt;IfModule mod_rewrite.c&gt;
173 RewriteEngine on
174
175 # Use the replaceall internal RewriteMap
176 RewriteMap replace int:replaceall
177
178 # Every RewriteRule needs a condition. Here, we want to apply our
179 # rule to every command.
180 RewriteCondition %m .*
181 RewriteRule (.*) "${replace:!$1!\\\\!/}"
182 &lt;/IfModule&gt;
183 </pre>
184 Yes, you will need the four consecutive backslashes there, in order to make it
185 past ProFTPD's config file parser (which thinks backslashes are escape
186 sequences) as well as the regular expression compiler.
187
188 <p>
189 <b>Modifying User Names</b><br>
190 Is there a way that I can transparently change the login name that the FTP
191 client sends, from one set of known login names to the new set of names
192 that should be used by the FTP server? But of course! For this example,
193 let us assume that you have a text file which maps the old login names to
194 the new login names. Using <code>mod_rewrite</code>'s <code>RewriteMap</code>
195 directive and that text file, this becomes simple:
196 <pre>
197 &lt;IfModule mod_rewrite.c&gt;
198 RewriteEngine on
199
200 # Tell mod_rewrite where to find the "usermap" text file
201 RewriteMap usermap txt:/path/to/usermap.txt
202
203 # For USER commands, use the "usermap" file to translate the login names
204 RewriteCondition %m USER
205 RewriteRule (.*) ${usermap:$1}
206 &lt;/IfModule&gt;
207 </pre>
208
209 <p>
210 Rather than having a fixed map of old-to-new login names, what if you wanted
211 to always append the same prefix (or suffix) to every login name? For
212 example, what if you wanted every login name on your FTP server to look
213 like "[email protected]", but the clients were sending simply "user". This
214 solution does not need <code>RewriteMap</code>; instead, you simply use:
215 <pre>
216 &lt;IfModule mod_rewrite.c&gt;
217 RewriteEngine on
218
219 RewriteCondition %m USER
220 RewriteRule (.*) [email protected]
221 &lt;/IfModule&gt;
222 </pre>
223 And if instead you wanted to use a fixed prefix, rather than a suffix, the
224 only difference would be in the <code>RewriteRule</code> directive, <i>e.g.</i>:
225 <pre>
226 &lt;IfModule mod_rewrite.c&gt;
227 RewriteEngine on
228
229 RewriteCondition %m USER
230 RewriteRule (.*) PREFIX$1
231 &lt;/IfModule&gt;
232 </pre>
233
234 <p>
235 Another interesting use case is where your clients might send the login
236 name in a variety of constructions, <i>e.g.</i>:
237 <ul>
238 <li><i>user</i>
239 <li><i>user</i>@domain.com
240 <li>prefix#<i>user</i>@domain.com
241 </ul>
242 but you want the FTP server only to use the "<i>user</i>" parts. How can
243 you configure <code>mod_rewrite</code> to strip off any potential prefix
244 and suffix? Regular expressions can be tricky, but using the <code>regex</code>
245 tool mentioned above, I worked out the following configuration that does
246 the trick:
247 <pre>
248 &lt;IfModule mod_rewrite.c&gt;
249 RewriteEngine on
250
251 RewriteCondition %m USER
252 RewriteRule ^(.*#)?([0-9A-Za-z]+)(@)? $2
253 &lt;/IfModule&gt;
254 </pre>
255
256 <p>
257 And if you simply wanted to have all user names be in lowercase, despite what
258 the FTP clients send, it's merely a matter of:
259 <pre>
260 &lt;IfModule mod_rewrite.c&gt;
261 RewriteEngine on
262
263 RewriteMap lowercase int:tolower
264 RewriteCondition %m USER
265 RewriteRule (.*) ${lowercase:$1}
266 &lt;/IfModule&gt;
267 </pre>
268
269 <p>
270 <b>Handling Clients' Bad PORT Commands</b><br>
271 A ProFTPD admin encountered a case where one of their customers refused to
272 use anything but the standard command-line FTP client that comes with Windows.
273 That FTP client does <b>not</b> support passive data transfers; it <i>always</i>
274 uses the <code>PORT</code> command to do active data transfers. However,
275 one issue with the <code>PORT</code> command is that the parameter contains
276 an IP address. In this situation, the FTP client was behind a NAT, and the
277 client was sending the <i>internal</i> LAN address in its <code>PORT</code>
278 command. Could <code>mod_rewrite</code> be used to solve the problem, and
279 allow that bad FTP client to use active data transfers despite its' sending
280 of an unusable (to the FTP server) IP address? Yes!
281
282 <p>
283 The solution was to use <code>mod_rewrite</code> to rewrite the address in
284 the sent <code>PORT</code> command, replacing the internal LAN address with
285 the IP address of the client that <code>proftpd</code> saw. Below is the
286 configuration used to make this work:
287 <pre>
288 # This is necessary, to keep proftpd from complaining about mismatched
289 # addresses in this situation
290 AllowForeignAddress on
291
292 &lt;IfModule mod_rewrite.c&gt;
293 RewriteEngine on
294
295 RewriteMap replace int:replaceall
296
297 # Substitute in the IP address of the client, regardless of the address
298 # the client tells us to use in the PORT command
299 RewriteCondition %m ^PORT$
300 RewriteRule ([0-9]+,[0-9]+,[0-9]+,[0-9]+)(.*) ${replace:/$1/$1/%a$2}
301
302 # Replace the periods in the client address with commas, as per RFC959
303 # requirements
304 RewriteCondition %m ^PORT$
305 RewriteRule (.*) ${replace:/$1/./,}
306 &lt;/IfModule&gt;
307 </pre>
308
309 <p>
310 <b><code>SITE</code> Commands</b><br>
311 The <code>mod_rewrite</code> module can also handle <i>some</i>
312 <code>SITE</code> commands, specifically:
313 <ul>
314 <li><code>SITE CHGRP</code>
315 <li><code>SITE CHMOD</code>
316 </ul>
317 These being supported by the <code>mod_site</code> module, which is part of
318 the normal <code>proftpd</code> build.
319
320 <p>
321 One site needed to make sure that any backslashes (<i>e.g.</i> used by Windows
322 clients) were translated to slashes, including in these <code>SITE</code>
323 commands. As of ProFTPD 1.3.2 (see <a href="http://bugs.proftpd.org/show_bug.cgi?id=2915">Bug #2915</a>), this can be accomplished using the following:
324 <pre>
325 &lt;IfModule mod_rewrite.c&gt;
326 RewriteEngine on
327 RewriteMap replace int:replaceall
328 RewriteCondition %m "^SITE CHMOD$" [NC]
329 RewriteRule "^(.*) +(.*)$" "$1 ${replace:!$2!\\\\!/}"'
330 &lt;/IfModule&gt;
331 </pre>
332 Notice how, for <code>SITE CHGRP</code> and <code>SITE CHMOD</code> commands,
333 the <code>%m</code> parameter in the <code>RewriteCondition</code> <b>must</b>
334 match the string "SITE CHGRP" or "SITE CHMOD", not just "SITE". This is
335 <i>important</i> -- and it only works for the
336 <code>SITE CHGRP</code>/<code>SITE CHMOD</code> commands. The use of the
337 "[NC]" modifier helps to catch those cases where the client might send
338 "SITE chmod", for instance.
339
340 <p>
341 <b>Redirecting FTP Requests</b><br>
342 One user wanted to know if <code>mod_rewrite</code> could be used to
343 redirect a request, just like one might do using Apache's
344 <code>mod_rewrite</code>, something like:
345 <pre>
346 RewriteRule /(.*) ftp://newname.domain.com/$1
347 </pre>
348 The above <code>RewriteRule</code> would work, but it would <b>not</b> actually
349 redirect the FTP client to the URL. FTP unfortuntely does <i>not</i> support
350 redirection of requests to other servers, at the protocol level, unlike HTTP.
351
352 <p>
353 However, it <i>is</i> possible to redirect a request to some other directory
354 on the same machine. For example, if you wanted to have any file uploaded
355 by a client go into the "/Incoming/" directory, no matter where the client
356 wanted to upload the file, you could use:
357 <pre>
358 &lt;IfModule mod_rewrite.c&gt;
359 RewriteEngine on
360 RewriteCondition %m STOR
361 RewriteRule (.*/)?(.*) /Incoming/$2
362 &lt;/IfModule&gt;
363 </pre>
364
365 <p>
366 <b>URL Encoded Characters</b><br>
367 On very rare occasions, you may find yourself dealing with URL-encoded
368 characters in your FTP command parameters. If you have worked with web servers
369 and URLs, you will accustomed to seeing sequences like "%20" in URLs; these are
370 URL encoded characters (as per <a href="http://www.faqs.org/rfcs/rfc2369.html">RFC2369</a>). Unescaping these URL-encoded sequences is exactly what the
371 "unescape" <code>RewriteMap</code> builtin function handles.
372
373 <p>
374 <b>Handling Non-ASCII Characters</b><br>
375 If you need to handle non-ASCII characters in your <code>mod_rewrite</code>
376 rules, then you may need to generate your configuration using a scripting
377 language, rather than using your editor. For example, my editor does not
378 handle non-ASCII characters well; it displays them as <code>?</code>.
379
380 <p>
381 Here's an example, using Perl, to replace "&auml;" with "ae" in uploaded file
382 names. Note that "&auml;" in hex notation is <code>0xE4</code>:
383 <pre>
384 my $rewrite_rule = 'RewriteRule (.*) ${replace:/$1/' . chr(0xE4) . '/ae}';
385
386 my $config = '/path/to/proftpd.conf';
387 if (open(my $fh, "&gt; $config")) {
388 print $fh EOR;
389
390 &lt;IfModule mod_rewrite.c&gt;
391 RewriteEngine on
392 RewriteLog /path/to/rewrite.log
393
394 RewriteCondition %m ^STOR$
395 $rewrite_rule
396 &lt;/IfModule&gt;
397 EOR
398 }
399 </pre>
400
401 <p>
402 <b>Time-Related Content</b><br>
403 What if you find yourself wanting to serve different files based on the
404 time of day, day of the month, <i>etc</i>? Or what if you wanted to
405 put an automatic timestamp on the names of files being uploaded? Starting
406 with <code>proftpd-1.3.5rc1</code>, these things are now possible using
407 <code>mod_rewrite</code>; see the time-related variables in the
408 <a href="../contrib/mod_rewrite.html#RewriteCondition"><code>RewriteCondition</code></a> documentation.
409
410 <p>
411 To demonstrate the concept of time-related content, let's assume that you
412 have a two different files, one for "daytime" and one for "nighttime".
413 Depending on when a client connects and requests this file, you can have
414 <code>mod_rewrite</code> transparently point the client to the correct file.
415 Let's show how this might work:
416 <pre>
417 &lt;IfModule mod_rewrite.c&gt;
418 RewriteEngine on
419 RewriteLog /path/to/rewrite.log
420
421 # For requests of index.txt during the day, rewrite the command to
422 # be for index.txt.day
423 RewriteCondition %m RETR
424 RewriteCondition %f index.txt$
425 RewriteCondition %{TIME_HOUR}%{TIME_MIN} &gt;0700
426 RewriteCondition %{TIME_HOUR}%{TIME_MIN} &lt;1900
427 RewriteRule ^(.*) $1.day
428
429 # For requests of index.txt during the night, rewrite the command to
430 # be for index.txt.night
431 RewriteCondition %m RETR
432 RewriteCondition %f index.txt$
433 RewriteCondition %{TIME_HOUR}%{TIME_MIN} &lt;0700
434 RewriteCondition %{TIME_HOUR}%{TIME_MIN} &gt;1900
435 RewriteRule ^(.*) $1.night
436
437 &lt;/IfModule&gt;
438 </pre>
439
440 <p>
441 Another use case involving time is the case where you might want to
442 automatically timestamp every file being uploaded. To do this, you can
443 use <code>mod_rewrite</code> like so:
444 <pre>
445 &lt;IfModule mod_rewrite.c&gt;
446 RewriteEngine on
447 RewriteLog /path/to/rewrite.log
448
449 # Automatically timestamp all uploaded files with ".DD-MM-YYYY".
450 RewriteCondition %m STOR
451 RewriteRule (.*) $1.%{TIME_DAY}-%{TIME_MON}-%{TIME_YEAR}
452 &lt;/IfModule&gt;
453 </pre>
454
455 <p>
456 Or maybe you have a special file that should only be available for a month,
457 and then be inaccessible? To do this, you would first give the special file
458 a name that includes a timestamp, <i>e.g.</i> "special.bin-01-2013". Then
459 have the following <code>mod_rewrite</code> configuration:
460 <pre>
461 &lt;IfModule mod_rewrite.c&gt;
462 RewriteEngine on
463 RewriteLog /path/to/rewrite.log
464
465 RewriteCondition %m RETR
466 RewriteCondition %f special.bin$
467 RewriteRule (.*) $1-%{TIME_MON}-%{TIME_YEAR}
468 &lt;/IfModule&gt;
469 </pre>
470 Now, if the "special.bin" file is requested during January 2013, the RETR
471 request will be rewritten and will match the name of the file on disk; the
472 file is accessible, and the download succeeds. If the same file is requested
473 any other time than during January 2013, then the
474 <code>mod_rewrite</code>-rewritten path will not match the name of the file
475 on disk, and the download will fail.
476
477 <p>
478 <hr>
479 <font size=2><b><i>
480 &copy; Copyright 2020-2023 The ProFTPD Project<br>
481 All Rights Reserved<br>
482 </i></b></font>
483 <hr>
484
485 </body>
486 </html>