| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD module mod_rewrite</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center> |
| 11 | <h2><b>ProFTPD module <code>mod_rewrite</code></b></h2> |
| 12 | </center> |
| 13 | <hr><br> |
| 14 | |
| 15 | <p> |
| 16 | This module is contained in the <code>mod_rewrite.c</code> file for ProFTPD |
| 17 | 1.3.<i>x</i>, and is not compiled by default. Installation instructions are |
| 18 | discussed <a href="#Installation">here</a>. |
| 19 | |
| 20 | <p> |
| 21 | The most current version of <code>mod_rewrite</code> is distributed with the |
| 22 | ProFTPD source code. |
| 23 | |
| 24 | <p> |
| 25 | A discussion of the <a href="#Usage">usage</a> of this module follows. |
| 26 | |
| 27 | <h2>Author</h2> |
| 28 | <p> |
| 29 | Please contact TJ Saunders <tj <i>at</i> castaglia.org> with any |
| 30 | questions, concerns, or suggestions regarding this module. |
| 31 | |
| 32 | <h2>Directives</h2> |
| 33 | <ul> |
| 34 | <li><a href="#RewriteCondition">RewriteCondition</a> |
| 35 | <li><a href="#RewriteEngine">RewriteEngine</a> |
| 36 | <li><a href="#RewriteLock">RewriteLock</a> |
| 37 | <li><a href="#RewriteLog">RewriteLog</a> |
| 38 | <li><a href="#RewriteMap">RewriteMap</a> |
| 39 | <li><a href="#RewriteMaxReplace">RewriteMaxReplace</a> |
| 40 | <li><a href="#RewriteRule">RewriteRule</a> |
| 41 | </ul> |
| 42 | |
| 43 | <hr> |
| 44 | <h3><a name="RewriteCondition">RewriteCondition</a></h3> |
| 45 | <strong>Syntax:</strong> RewriteCondition <em>condition pattern [flags]</em><br> |
| 46 | <strong>Default:</strong> None<br> |
| 47 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code>, <code><Anonymous></code>, <code><Directory></code><br> |
| 48 | <strong>Module:</strong> mod_rewrite<br> |
| 49 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 50 | |
| 51 | <p> |
| 52 | The <code>RewriteCondition</code> directive defines a rule condition. Precede a |
| 53 | <a href="#RewriteRule"><code>RewriteRule</code></a> directive with one or more |
| 54 | <code>RewriteCondition</code> directives. The following rewriting rule is only |
| 55 | used if its pattern matches the current state of the FTP command <em>and</em> |
| 56 | if these additional conditions apply too. |
| 57 | |
| 58 | <p> |
| 59 | <em>Condition</em> is a string which can contain the following expanded |
| 60 | constructs in addition to plain text: |
| 61 | <ul> |
| 62 | <li><b><code>RewriteRule</code> backreferences</b><br> |
| 63 | These are backreferences of the form: |
| 64 | <pre> |
| 65 | <b><code>$N</code></b> |
| 66 | </pre> |
| 67 | (0 <= <em>N</em> <= 9) which provide access to the grouped parts |
| 68 | (parentheses!) of the pattern from the corresponding |
| 69 | <code>RewriteRule</code> directive (the one following the current bunch of |
| 70 | <code>RewriteCondition</code> directives). Note that <em>$0</em> will |
| 71 | refer back to the entire original string being matched. |
| 72 | |
| 73 | <p> |
| 74 | Use an extra <code>$</code> character to escape a sequence which |
| 75 | <i>looks</i> like a <code>RewriteRule</code> backreference, <i>e.g.</i> |
| 76 | <em>$$N</em>. |
| 77 | </li> |
| 78 | |
| 79 | <p> |
| 80 | <li><b><code>RewriteCondition</code> backreferences</b><br> |
| 81 | These are backreferences of the form: |
| 82 | <pre> |
| 83 | <b><code>%N</code></b> |
| 84 | </pre> |
| 85 | (0 <= <em>N</em> <= 9) which provide access to the grouped parts |
| 86 | (parentheses!) of the pattern from the previous |
| 87 | <code>RewriteCondition</code> attached to this <code>RewriteRule</code>. |
| 88 | |
| 89 | <p> |
| 90 | Use an extra <code>%</code> character to escape a sequence which |
| 91 | <i>looks</i> like a <code>RewriteCondition</code> backreference, <i>e.g.</i> |
| 92 | <em>%%N</em>. |
| 93 | </li> |
| 94 | |
| 95 | <p> |
| 96 | <li><b><code>RewriteMap</code> expansions:</b><br> |
| 97 | These are expansions of the form: |
| 98 | <pre> |
| 99 | <b><code>${map-name:lookup-key|default-value}</code></b> |
| 100 | </pre> |
| 101 | See the documentation for <a href="#RewriteMap"><code>RewriteMap</code></a> |
| 102 | for more details. |
| 103 | </li> |
| 104 | |
| 105 | <p> |
| 106 | <li><b>Variable substitutions:</b><br> |
| 107 | These are substitutions of the form: |
| 108 | <p> |
| 109 | <table border=0 summary="Rewrite Variables"> |
| 110 | <tr> |
| 111 | <td><b>%a</b></td> |
| 112 | <td>Client IP address</td> |
| 113 | </tr> |
| 114 | |
| 115 | <tr> |
| 116 | <td><b>%c</b></td> |
| 117 | <td>Name of Class for current session</td> |
| 118 | </tr> |
| 119 | |
| 120 | <tr> |
| 121 | <td><b>%f</b></td> |
| 122 | <td>Filename</td> |
| 123 | </tr> |
| 124 | |
| 125 | <tr> |
| 126 | <td><b>%F</b></td> |
| 127 | <td>Transfer path, as seen by the client (only useful for upload/download commands)</td> |
| 128 | </tr> |
| 129 | |
| 130 | <tr> |
| 131 | <td><b>%g</b></td> |
| 132 | <td>Primary group of authenticated user</td> |
| 133 | </tr> |
| 134 | |
| 135 | <tr> |
| 136 | <td><b>%G</b></td> |
| 137 | <td>Supplemental groups of authenticated user</td> |
| 138 | </tr> |
| 139 | |
| 140 | <tr> |
| 141 | <td><b>%h</b></td> |
| 142 | <td>Client DNS name</td> |
| 143 | </tr> |
| 144 | |
| 145 | <tr> |
| 146 | <td><b>%m</b></td> |
| 147 | <td>FTP command</td> |
| 148 | </tr> |
| 149 | |
| 150 | <tr> |
| 151 | <td><b>%p</b></td> |
| 152 | <td>Port of server handling the session</td> |
| 153 | </tr> |
| 154 | |
| 155 | <tr> |
| 156 | <td><b>%u</b></td> |
| 157 | <td>Name of authenticated user</td> |
| 158 | </tr> |
| 159 | |
| 160 | <tr> |
| 161 | <td><b>%U</b></td> |
| 162 | <td>Name of user sent by client via <code>USER</code></td> |
| 163 | </tr> |
| 164 | |
| 165 | <tr> |
| 166 | <td><b>%v</b></td> |
| 167 | <td><code>ServerName</code> of server handling the session</td> |
| 168 | </tr> |
| 169 | |
| 170 | <tr> |
| 171 | <td><b>%{TIME_YEAR}</b></td> |
| 172 | <td>Current local year in <em>YYYY</em> format</td> |
| 173 | </tr> |
| 174 | |
| 175 | <tr> |
| 176 | <td><b>%{TIME_MON}</b></td> |
| 177 | <td>Current local month in <em>MM</em> format</td> |
| 178 | </tr> |
| 179 | |
| 180 | <tr> |
| 181 | <td><b>%{TIME_DAY}</b></td> |
| 182 | <td>Current local day in <em>DD</em> format</td> |
| 183 | </tr> |
| 184 | |
| 185 | <tr> |
| 186 | <td><b>%{TIME_WDAY}</b></td> |
| 187 | <td>Current local day of week (Sunday is 0)</td> |
| 188 | </tr> |
| 189 | |
| 190 | <tr> |
| 191 | <td><b>%{TIME_HOUR}</b></td> |
| 192 | <td>Current local hour in <em>HH</em> format</td> |
| 193 | </tr> |
| 194 | |
| 195 | <tr> |
| 196 | <td><b>%{TIME_MIN}</b></td> |
| 197 | <td>Current local minute in <em>mm</em> format</td> |
| 198 | </tr> |
| 199 | |
| 200 | <tr> |
| 201 | <td><b>%{TIME_SEC}</b></td> |
| 202 | <td>Current local second in <em>ss</em> format</td> |
| 203 | </tr> |
| 204 | |
| 205 | <tr> |
| 206 | <td><b>%{TIME}</b></td> |
| 207 | <td>Current local time in <em>YYYYMMDDHHmmss</em> format</td> |
| 208 | </tr> |
| 209 | </table> |
| 210 | </li> |
| 211 | |
| 212 | <p> |
| 213 | <li><b>Environment Variable substitutions:</b><br> |
| 214 | These are substitutions of the form: |
| 215 | <pre> |
| 216 | <b><code>%{ENV:<i>variable-name</i>}</code></b> |
| 217 | </pre> |
| 218 | If the <em>variable-name</em> environment variable is not present, |
| 219 | it will be substituted with the empty string. |
| 220 | </li> |
| 221 | </ul> |
| 222 | |
| 223 | <p> |
| 224 | <em>Pattern</em> is the condition pattern, <i>i.e.</i>, a regular expression |
| 225 | which is applied to the current instance of the <em>condition</em>, |
| 226 | <i>i.e.</i>, <em>condition</em> is evaluated and then matched against |
| 227 | <em>pattern</em>. You can prefix the <em>pattern</em> string with a '!' |
| 228 | character (exclamation mark) to specify a non-matching pattern. |
| 229 | |
| 230 | <p> |
| 231 | The <em>pattern</em> can also be one of these special variants: |
| 232 | <ul> |
| 233 | <li><code><</code><em>pattern</em> (is lexically lower)<br> |
| 234 | Treats the <em>pattern</em> as a plain string and compares it lexically |
| 235 | to <em>condition</em>. True if <em>condition</em> is lexically lower |
| 236 | than <em>pattern</em> |
| 237 | </li> |
| 238 | |
| 239 | <p> |
| 240 | <li><code>></code><em>pattern</em> (is lexically greater)<br> |
| 241 | Treats the <em>pattern</em> as a plain string and compares it lexically |
| 242 | to <em>condition</em>. True if <em>condition</em> is lexically greater |
| 243 | than <em>pattern</em> |
| 244 | </li> |
| 245 | |
| 246 | <p> |
| 247 | <li><code>=</code><em>pattern</em> (is lexically equal)<br> |
| 248 | Treats the <em>pattern</em> as a plain string and compares it lexically |
| 249 | to <em>condition</em>. True if <em>condition</em> is lexically equal to |
| 250 | <em>pattern</em>, <i>i.e.</i> the two strings are exactly equal |
| 251 | (character by character). If <em>pattern</em> is just "" (two |
| 252 | quotation marks) this compares <em>condition</em> to the empty string |
| 253 | </li> |
| 254 | |
| 255 | <p> |
| 256 | <li><code>-d</code> (is directory)<br> |
| 257 | Treats the <em>condition</em> as a pathname and tests if it exists and |
| 258 | is a directory |
| 259 | </li> |
| 260 | |
| 261 | <p> |
| 262 | <li><code>-f</code> (is regular file)<br> |
| 263 | Treats the <em>condition</em> as a pathname and tests if it exists and |
| 264 | is a regular file |
| 265 | </li> |
| 266 | |
| 267 | <p> |
| 268 | <li><code>-s</code> (is regular file with size)<br> |
| 269 | Treats the <em>condition</em> as a pathname and tests if it exists and |
| 270 | is a regular file with size greater than zero |
| 271 | </li> |
| 272 | |
| 273 | <p> |
| 274 | <li><code>-l</code> (is symbolic link)<br> |
| 275 | Treats the <em>condition</em> as a pathname and tests if it exists and |
| 276 | is a symbolic link |
| 277 | </li> |
| 278 | </ul> |
| 279 | Notice: All of these variants can also be prefixed by an exclamation mark |
| 280 | ('!') to negate their meaning. |
| 281 | |
| 282 | <p> |
| 283 | <em>Flags</em>, if present, modify how this <code>RewriteCondition</code> is |
| 284 | evaluated. Supported flags are: |
| 285 | <ul> |
| 286 | <li><b>nocase|NC</b> (<b>n</b>o <b>c</b>ase)<br> |
| 287 | This makes the <em>pattern</em> case-insensitive, <i>i.e.</i> there is |
| 288 | no difference between 'A-Z' and 'a-z' when <em>pattern</em> is matched |
| 289 | against the <em>condition</em> |
| 290 | </li> |
| 291 | |
| 292 | <p> |
| 293 | <li><b>ornext|OR</b> (<b>or</b> next condition)<br> |
| 294 | Use this to combine rule conditions with a logical <code>OR</code> |
| 295 | instead of the implicit <code>AND</code>. Typical example: |
| 296 | <pre> |
| 297 | RewriteCondition %h ^host1.* [OR] |
| 298 | RewriteCondition %h ^host2.* [OR] |
| 299 | RewriteCondition %h ^host3.* |
| 300 | RewriteRule <i>...some special stuff for any of these hosts...</i> |
| 301 | </pre> |
| 302 | Without this flag you would have to write the condition/rule combination |
| 303 | three times. |
| 304 | </li> |
| 305 | </ul> |
| 306 | |
| 307 | <p> |
| 308 | <hr> |
| 309 | <h3><a name="RewriteEngine">RewriteEngine</a></h3> |
| 310 | <strong>Syntax:</strong> RewriteEngine <em>on|off</em><br> |
| 311 | <strong>Default:</strong> off<br> |
| 312 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br> |
| 313 | <strong>Module:</strong> mod_rewrite<br> |
| 314 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 315 | |
| 316 | <p> |
| 317 | The <code>RewriteEngine</code> directive enables or disables the module's |
| 318 | runtime rewriting engine. If it is set to <em>off</em> this module does no |
| 319 | parsing or rewriting at all. Use this directive to disable the module instead |
| 320 | of commenting out all <code>mod_rewrite</code> directives. |
| 321 | |
| 322 | <p> |
| 323 | <hr> |
| 324 | <h3><a name="RewriteLock">RewriteLock</a></h3> |
| 325 | <strong>Syntax:</strong> RewriteLock <em>file</em><br> |
| 326 | <strong>Default:</strong> None<br> |
| 327 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br> |
| 328 | <strong>Module:</strong> mod_rewrite<br> |
| 329 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 330 | |
| 331 | <p> |
| 332 | The <code>RewriteLock</code> directive sets the filename for a synchronization |
| 333 | lockfile which <code>mod_rewrite</code> needs to communicate with |
| 334 | <code>RewriteMap</code>s of type <i>fifo</i>. Set <em>file</em> to a local |
| 335 | absolute path (not on a NFS-mounted device) when you want to use a rewriting |
| 336 | FIFO. It is not required for other types of rewriting maps. |
| 337 | |
| 338 | <p> |
| 339 | <hr> |
| 340 | <h3><a name="RewriteLog">RewriteLog</a></h3> |
| 341 | <strong>Syntax:</strong> RewriteLog <em>file|"none"</em><br> |
| 342 | <strong>Default:</strong> None<br> |
| 343 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br> |
| 344 | <strong>Module:</strong> mod_rewrite<br> |
| 345 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 346 | |
| 347 | <p> |
| 348 | The <code>RewriteLog</code> directive is used to specify a log file for |
| 349 | <code>mod_rewrite</code> reporting and debugging, and can be done a per-server |
| 350 | basis. The <em>file</em> parameter must be the full path to the file to use for |
| 351 | logging. Note that this path must <b>not</b> be to a world-writeable |
| 352 | directory and, unless <code>AllowLogSymlinks</code> is explicitly set to |
| 353 | <em>on</em> (generally a bad idea), the path must <b>not</b> be a symbolic |
| 354 | link. In general, this directive should <i>only be used</i> for debugging |
| 355 | your <code>mod_rewrite</code> configuration, and should be removed once |
| 356 | debugging is completed; <b>do not use this directive in a production |
| 357 | configuration</b>. |
| 358 | |
| 359 | <p> |
| 360 | If <em>file</em> is "none", no logging will be done at all; this |
| 361 | setting can be used to override a <code>RewriteLog</code> setting inherited from |
| 362 | a <code><Global></code> context. |
| 363 | |
| 364 | <p> |
| 365 | <hr> |
| 366 | <h3><a name="RewriteMap">RewriteMap</a></h3> |
| 367 | <strong>Syntax:</strong> RewriteMap <em>map-name map-type:map-source</em><br> |
| 368 | <strong>Default:</strong> None<br> |
| 369 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br> |
| 370 | <strong>Module:</strong> mod_rewrite<br> |
| 371 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 372 | |
| 373 | <p> |
| 374 | The <code>RewriteMap</code> directive defines a <em>rewriting map</em> which |
| 375 | can be used inside rule substitution strings by the mapping-functions to |
| 376 | insert/substitute fields through a key lookup. The source of this lookup can |
| 377 | be of various types. |
| 378 | |
| 379 | <p> |
| 380 | The <em>map-name</em> is the name of the map and will be used to specify a |
| 381 | mapping-function for the substitution strings of a rewriting rule via one of |
| 382 | the following constructs: |
| 383 | |
| 384 | <blockquote><strong> |
| 385 | <code>${</code> <em>map-name</em> <code>:</code> |
| 386 | <em>lookup-key</em> <code>}</code><br> |
| 387 | <code>${</code> <em>map-name</em> <code>:</code> |
| 388 | <em>lookup-key</em> <code>|</code> <em>default-value</em><code>}</code> |
| 389 | </strong></blockquote> |
| 390 | |
| 391 | When such a construct occurs the map <em>map-name</em> is consulted and the |
| 392 | key <em>lookup-key</em> is resolved. If the key is found, the map-function |
| 393 | construct is substituted by <em>subst-value</em>. If the key is not found |
| 394 | then it is substituted by <em>default-value</em> or by the empty string if no |
| 395 | <em>default-value</em> was specified. |
| 396 | |
| 397 | <p> |
| 398 | The following combinations for <em>map-type</em> and <em>map-src</em> can be |
| 399 | used: |
| 400 | |
| 401 | <ul> |
| 402 | <li><strong>Standard Plain Text</strong><br> |
| 403 | <em>map-type</em>: <code>txt</code>, <em>map-src</em>: Unix filesystem |
| 404 | path to valid regular file. |
| 405 | |
| 406 | <p> |
| 407 | This is the standard rewriting map feature where the <em>map-src</em> is |
| 408 | a plain ASCII file containing either blank lines, comment lines (starting |
| 409 | with a '#' character) or pairs like the following - one per line. |
| 410 | |
| 411 | <blockquote> |
| 412 | <strong><em>matching-key</em> <em>subst-value</em></strong> |
| 413 | </blockquote> |
| 414 | |
| 415 | <p> |
| 416 | Example: |
| 417 | <pre> |
| 418 | # -------------------------------------------- |
| 419 | # usermap.txt -- map for rewriting user names |
| 420 | # -------------------------------------------- |
| 421 | |
| 422 | Dave.Admin dave # The Uber-admin |
| 423 | root anonymous # no one should be logging in as root anyway |
| 424 | </pre> |
| 425 | And, to configure this map to be used: |
| 426 | <pre> |
| 427 | RewriteMap real-to-user txt:/path/to/file/usermap.txt |
| 428 | </pre> |
| 429 | </li> |
| 430 | |
| 431 | <li><strong>FIFO/Named Pipe</strong><br> |
| 432 | <em>map-type</em>: <code>fifo</code>, <em>map-src</em>: Unix filesystem |
| 433 | path to valid FIFO. |
| 434 | |
| 435 | <p> |
| 436 | For this rewriting map, <em>map-src</em> is a FIFO (<i>a.k.a.</i> named |
| 437 | pipe). To create it, you can use the <code>mkfifo(1)</code> command. |
| 438 | |
| 439 | An external program that opens the FIFO for reading and writing <b>must</b> |
| 440 | be started <i>before</i> <code>proftpd</code> is started. This program |
| 441 | can communicate with the rewriting engine via the FIFO. For each mapping |
| 442 | lookup, it can read the key to lookup as a newline-terminated string from |
| 443 | the FIFO. It then has to write back to the FIFO the looked-up value as a |
| 444 | newline-terminated string, or just simply newline character (denoting an |
| 445 | empty string) if there is no corresponding value for the given key). |
| 446 | |
| 447 | <p> |
| 448 | An example program which will implement a 1:1 mapping (<i>i.e.</i>, |
| 449 | key == value) could be: |
| 450 | <pre> |
| 451 | #!/usr/bin/perl |
| 452 | use strict; |
| 453 | |
| 454 | use File::Basename qw(basename); |
| 455 | use Getopt::Long; |
| 456 | use IO::Handle; |
| 457 | use IO::Select; |
| 458 | |
| 459 | my $default_delay = 0.5; |
| 460 | my $program = basename($0); |
| 461 | my %opts = (); |
| 462 | |
| 463 | GetOptions(\%opts, 'delay=f', 'fifo=s', 'help', 'verbose'); |
| 464 | |
| 465 | usage() if $opts{'help'}; |
| 466 | |
| 467 | my $delay = $opts{'delay'} ? $opts{'delay'} : $default_delay; |
| 468 | |
| 469 | die "$program: missing required --fifo parameter\n" unless $opts{'fifo'}; |
| 470 | my $fifo = $opts{'fifo'}; |
| 471 | |
| 472 | my $verbose = $opts{'verbose'} ? 1 : 0; |
| 473 | |
| 474 | open(my $fifo_fh, "+> $fifo") or die "$program: unable to open $fifo: $!\n"; |
| 475 | |
| 476 | # Instantiate a Select object for knowing when to read from and write to |
| 477 | # the FIFO. |
| 478 | my $sel = IO::Select->new(); |
| 479 | |
| 480 | while (1) { |
| 481 | |
| 482 | # Blocking select() for reading. |
| 483 | $sel->add($fifo_fh); |
| 484 | |
| 485 | print STDERR "$program: selecting for reading\n" if $verbose; |
| 486 | my ($rfh) = $sel->can_read(); |
| 487 | |
| 488 | my $key = <$rfh>; |
| 489 | print STDERR "$program: read '$key'\n" if $verbose; |
| 490 | |
| 491 | # Lookup a value for the given key. |
| 492 | my $value = lookup_value($key); |
| 493 | |
| 494 | # Clear the Select object's filehandles. |
| 495 | $sel->remove(); |
| 496 | |
| 497 | print $fifo_fh "$value\n" if $verbose; |
| 498 | $fifo_fh->flush(); |
| 499 | |
| 500 | print STDERR "$program: wrote '$value'\n" if $verbose; |
| 501 | |
| 502 | # Wait for the buffer's byte to be cleared before reading again. |
| 503 | wait_fifo($fifo_fh); |
| 504 | } |
| 505 | |
| 506 | close($fifo_fh); |
| 507 | print STDOUT "$program: done\n" if $verbose; |
| 508 | |
| 509 | exit 0; |
| 510 | |
| 511 | # -------------------------------------------------------------------------- |
| 512 | sub lookup_value { |
| 513 | my ($key) = @_; |
| 514 | |
| 515 | # NOTE: do something to obtain a value for the given key here. |
| 516 | chomp(my $value = $key); |
| 517 | |
| 518 | return $value; |
| 519 | } |
| 520 | |
| 521 | # -------------------------------------------------------------------------- |
| 522 | sub usage { |
| 523 | print STDOUT <<END_OF_USAGE; |
| 524 | |
| 525 | usage: $program [options] |
| 526 | |
| 527 | --delay Configure the buffer check delay. |
| 528 | The default is $default_delay seconds. |
| 529 | |
| 530 | --fifo Configure the path to the FIFO. Required. |
| 531 | |
| 532 | --help Displays this message. |
| 533 | |
| 534 | --verbose Enables verbose output while $program runs. |
| 535 | |
| 536 | END_OF_USAGE |
| 537 | |
| 538 | exit 0; |
| 539 | } |
| 540 | |
| 541 | # -------------------------------------------------------------------------- |
| 542 | sub wait_fifo { |
| 543 | my ($fh) = @_; |
| 544 | |
| 545 | # Now we get tricky. Use ioctl(2) to poll the number of bytes to |
| 546 | # be read from the FIFO filehandle. When the number drops to zero, |
| 547 | # it means that the data we just wrote has been read from the buffer |
| 548 | # by some other process, so we can go back to the top of this loop. |
| 549 | # Otherwise, if this program loops faster than the reader/writer on |
| 550 | # the other end of the FIFO, we'd end up reading the data we just |
| 551 | # wrote. Quite annoying, actually. |
| 552 | # |
| 553 | # Note: this value must be manually extracted from the system header files |
| 554 | # using the following program: |
| 555 | # |
| 556 | # -------- fionread.c ------------------- |
| 557 | # #include <sys/ioctl.h> |
| 558 | # |
| 559 | # int main(int argc, char *argv[]) { |
| 560 | # printf("%#08x\n", FIONREAD); |
| 561 | # return 0; |
| 562 | # } |
| 563 | # --------------------------------------- |
| 564 | # |
| 565 | # > cc -o fionread fionread.c |
| 566 | # > ./fionread |
| 567 | |
| 568 | my $FIONREAD = 0x00541b; |
| 569 | |
| 570 | my $size = pack('L', 0); |
| 571 | ioctl($fh, $FIONREAD, $size) or die "$program: unable to use ioctl: $!\n"; |
| 572 | $size = unpack('L', $size); |
| 573 | |
| 574 | while ($size != 0) { |
| 575 | print STDERR "$program: waiting for buffer to be read\n" if $verbose; |
| 576 | select(undef, undef, undef, $delay); |
| 577 | |
| 578 | $size = pack('L', 0); |
| 579 | ioctl($fh, $FIONREAD, $size) or die "$program: unable to use ioctl: $!\n"; |
| 580 | $size = unpack('L', $size); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | </pre> |
| 585 | To make use of this example script, simply implement your lookup code |
| 586 | in the <code>lookup_value()</code> subroutine. Be very careful with |
| 587 | such scripts, though: |
| 588 | |
| 589 | <p> |
| 590 | <ol> |
| 591 | <li>"Keep it simple, stupid" (KISS), because if this program |
| 592 | hangs it will hang <code>proftpd</code> when the rule occurs. |
| 593 | Well, keep it as simple as possible...<br> |
| 594 | <br> |
| 595 | |
| 596 | <li>Avoid one common mistake: avoid buffered I/O if possible. This can |
| 597 | cause a deadloop. If necessary, be sure to flush the filehandle |
| 598 | before reading, and after writing.<br> |
| 599 | <br> |
| 600 | |
| 601 | <li>Use the <code>RewriteLock</code> directive to define a lockfile |
| 602 | <code>mod_rewrite</code> can use to synchronize the communication to |
| 603 | the FIFO program. By default no such synchronization takes place.<br> |
| 604 | <br> |
| 605 | </ol> |
| 606 | </li> |
| 607 | |
| 608 | <li><strong>Internal Function</strong><br> |
| 609 | <em>map-type</em>: <code>int</code>, <em>map-src</em>: Internal |
| 610 | <code>mod_rewrite</code> function. |
| 611 | |
| 612 | <p> |
| 613 | Here the <em>map-src</em> is a <code>mod_rewrite</code> built-in function. |
| 614 | Currently you cannot create your own, but the following functions already |
| 615 | exist: |
| 616 | <ul> |
| 617 | <p> |
| 618 | <li><strong>idnatrans</strong>:<br> |
| 619 | Translates text in the lookup up key into <a href="https://en.wikipedia.org/wiki/Internationalized_domain_name">IDNA</a> text. |
| 620 | </li> |
| 621 | |
| 622 | <p> |
| 623 | <li><strong>replaceall</strong>:<br> |
| 624 | Replace all occurrences, in the looked up key, of one string with |
| 625 | another string |
| 626 | </li> |
| 627 | |
| 628 | <p> |
| 629 | <li><strong>toupper</strong>:<br> |
| 630 | Converts the looked up key to all upper case. |
| 631 | </li> |
| 632 | |
| 633 | <p> |
| 634 | <li><strong>tolower</strong>:<br> |
| 635 | Converts the looked up key to all lower case. |
| 636 | </li> |
| 637 | |
| 638 | <p> |
| 639 | <li><strong>unescape</strong>:<br> |
| 640 | Translates hex encodings (<i>i.e.</i> "URL encoding"; see <a href="http://www.faqs.org/rfcs/rfc2396.html">RFC2396</a>, Section 2.4.1), in the looked up |
| 641 | key back to special characters. |
| 642 | </li> |
| 643 | |
| 644 | <p> |
| 645 | <li><strong>utf8trans</strong>:<br> |
| 646 | Translates UTF-8 encodings in the lookup up key into Latin-1 |
| 647 | characters. |
| 648 | </li> |
| 649 | </ul> |
| 650 | </li> |
| 651 | </ul> |
| 652 | |
| 653 | The <code>RewriteMap</code> directive can occur more than once. For each |
| 654 | mapping-function use one <code>RewriteMap</code> directive to declare its |
| 655 | rewriting map name. |
| 656 | |
| 657 | <p> |
| 658 | <strong>Note:</strong> For plain text files the looked-up keys are cached |
| 659 | in-core until the <code>mtime</code> of the text map file changes or the |
| 660 | server does a restart. This way you can have map-functions in rules which are |
| 661 | used for <strong>every</strong> request. This is no problem, because the |
| 662 | parsing of the text files only happens once! |
| 663 | |
| 664 | <p> |
| 665 | <hr> |
| 666 | <h3><a name="RewriteMaxReplace">RewriteMaxReplace</a></h3> |
| 667 | <strong>Syntax:</strong> RewriteMaxReplace <em>count</em><br> |
| 668 | <strong>Default:</strong> 8<br> |
| 669 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code><br> |
| 670 | <strong>Module:</strong> mod_rewrite<br> |
| 671 | <strong>Compatibility:</strong> 1.3.5rc1 and later |
| 672 | |
| 673 | <p> |
| 674 | The <code>RewriteMaxReplace</code> directive is used to increase the number |
| 675 | of replacements/substitutions that <code>mod_rewrite</code> will perform, |
| 676 | when rewriting commands. By default, <code>mod_rewrite</code> will only |
| 677 | replace up to 8 occurrences of a pattern in the input string; if there are more |
| 678 | than 8 replacement occurrences, then the input string will be unchanged. |
| 679 | If your input strings happen to have more than 8 occurrences to be replaced, |
| 680 | you can use the <code>RewriteMaxReplace</code> to increase that limit. |
| 681 | |
| 682 | <p> |
| 683 | For example, to increase the limit to 32 occurrences to be replaced, use: |
| 684 | <pre> |
| 685 | RewriteMaxReplace 32 |
| 686 | </pre> |
| 687 | |
| 688 | <p> |
| 689 | <hr> |
| 690 | <h3><a name="RewriteRule">RewriteRule</a></h3> |
| 691 | <strong>Syntax:</strong> RewriteRule <em>pattern substitution [flags]</em><br> |
| 692 | <strong>Default:</strong> None<br> |
| 693 | <strong>Context:</strong> server config, <code><VirtualHost></code>, <code><Global></code>, <code><Anonymous></code>, <code><Directory></code><br> |
| 694 | <strong>Module:</strong> mod_rewrite<br> |
| 695 | <strong>Compatibility:</strong> 1.2.6rc1 and later |
| 696 | |
| 697 | <p> |
| 698 | The <code>RewriteRule</code> directive is the real rewriting workhorse. The |
| 699 | configuration directive can occur more than once. Each directive defines a |
| 700 | single rewriting rule. The order of definition of these rules is important, |
| 701 | because this order is used when applying the rules at run-time. |
| 702 | |
| 703 | <p> |
| 704 | <b>Note</b> that each <code>RewriteRule</code> <b>must</b> be preceded by |
| 705 | one (or more) <code>RewriteCondition</code> directive. |
| 706 | |
| 707 | <p> |
| 708 | <em>Pattern</em> can be POSIX regular expression which gets applied to the |
| 709 | current FTP command argument(s). |
| 710 | |
| 711 | <p> |
| 712 | Some hints about the syntax of regular expressions: |
| 713 | <ul> |
| 714 | <li><b>Text:</b> |
| 715 | <pre> |
| 716 | . <em>Any single character</em> |
| 717 | [chars] <em>Character class: one of <code>chars</code></em> |
| 718 | [^chars] <em>Character class: none of <code>chars</code></em> |
| 719 | text1|text2 <em>Alternative: <code>text1</code> or <code>text2</code></em> |
| 720 | </pre> |
| 721 | </li> |
| 722 | |
| 723 | <p> |
| 724 | <li><b>Quantifiers:</b> |
| 725 | <pre> |
| 726 | ? <em>0 or 1 of the preceding text</em> |
| 727 | * <em>0 or N of the preceding text (N > 0)</em> |
| 728 | + <em>1 or N of the preceding text (N > 1)</em> |
| 729 | </pre> |
| 730 | </li> |
| 731 | |
| 732 | <p> |
| 733 | <li><b>Grouping:</b> |
| 734 | <pre> |
| 735 | (text) <em>Grouping of text |
| 736 | (either to set the borders of an alternative or |
| 737 | for making backreferences where the Nth group can |
| 738 | be used on the RHS of a <code>RewriteRule</code> with $N)</em> |
| 739 | </pre> |
| 740 | </li> |
| 741 | |
| 742 | <p> |
| 743 | <li><b>Anchors:</b> |
| 744 | <pre> |
| 745 | ^ <em>Start of line anchor</em> |
| 746 | $ <em>End of line anchor</em> |
| 747 | </pre> |
| 748 | </li> |
| 749 | |
| 750 | <p> |
| 751 | <li><b>Escaping:</b> |
| 752 | <pre> |
| 753 | \char <em>Escape that particular char |
| 754 | (for instance to specify the chars ".[]()" etc.)</em> |
| 755 | </pre> |
| 756 | </li> |
| 757 | </ul> |
| 758 | |
| 759 | <p> |
| 760 | For more information about regular expressions have a look at your local |
| 761 | <code>regex(3)</code> manpage. If you are interested in more detailed |
| 762 | information about regular expressions and their variants (POSIX regex, Perl |
| 763 | regex, <i>etc.</i>) have a look at the following dedicated book on this topic: |
| 764 | <p> |
| 765 | <blockquote> |
| 766 | Mastering Regular Expressions<br> |
| 767 | Jeffrey E.F. Friedl<br> |
| 768 | Nutshell Handbook Series<br> |
| 769 | O'Reilly & Associates, Inc. 1997<br> |
| 770 | ISBN 1-56592-257-3<br> |
| 771 | </blockquote> |
| 772 | |
| 773 | <p> |
| 774 | Additionally in <code>mod_rewrite</code> the NOT character ('!') is a possible |
| 775 | pattern prefix. This gives you the ability to negate a pattern; to say, for |
| 776 | instance: "if the current argument(s) does NOT match this pattern". |
| 777 | This can be used for exceptional cases, where it is easier to match the |
| 778 | negative pattern, or as a last default rule. |
| 779 | |
| 780 | <p> |
| 781 | <b>Notice</b>: When using the NOT character to negate a pattern you cannot |
| 782 | have grouped wildcard parts in the pattern. This is impossible because when |
| 783 | the pattern does NOT match, there are no contents for the groups. In |
| 784 | consequence, if negated patterns are used, you cannot use $N in the |
| 785 | substitution string. |
| 786 | |
| 787 | <p> |
| 788 | Substitution of a rewriting rule is the string which is substituted for |
| 789 | (or replaces) the original argument(s) for which <em>pattern</em> matched. |
| 790 | Beside plain text you can use: |
| 791 | <ol> |
| 792 | <li><code>$N</code> backreferences to the <code>RewriteRule</code> pattern |
| 793 | <li><code>%N</code> backreferences to the last matched |
| 794 | <code>RewriteCondition</code> pattern |
| 795 | <li>variables as in <code>RewriteCondition</code> test strings |
| 796 | <li>map function calls (<code>${map-name:lookup-key|default-value}</code>) |
| 797 | <li>environment variable substitutions (<code>%{ENV:<i>variable-name</i>}</code>) |
| 798 | </ol> |
| 799 | Backreferences are $<b>N</b> (<b>N</b>=0..9) identifiers which will be replaced |
| 800 | by the contents of the <b>N</b>th group of the matched <em>pattern</em>. The |
| 801 | variables are the same as for the <em>condition</em> of a |
| 802 | <a href="#RewriteCondition"><code>RewriteCondition</code></a> directive, with |
| 803 | two additions: |
| 804 | <pre> |
| 805 | <b>%P</b> process ID |
| 806 | <b>%t</b> Unix time since the epoch, in seconds |
| 807 | </pre> |
| 808 | The map functions come from the |
| 809 | <a href="#RewriteMap"><code>RewriteMap</code></a> directive and are explained |
| 810 | there. These four types of variables are expanded in the order of the above |
| 811 | list. |
| 812 | |
| 813 | <p> |
| 814 | All of the rewriting rules are applied to <em>substitution</em>. The command |
| 815 | argument(s) is completely replaced by the <em>substitution</em>. |
| 816 | |
| 817 | <p> |
| 818 | <em>Flags</em>, if present, modify how this <code>RewriteRule</code> is |
| 819 | evaluated. Supported flags are: |
| 820 | <ul> |
| 821 | <li><b>nocase|NC</b> (<b>n</b>o <b>c</b>ase)<br> |
| 822 | This makes the <em>pattern</em> case-insensitive, <i>i.e.</i> there is |
| 823 | no difference between 'A-Z' and 'a-z' when <em>pattern</em> is matched |
| 824 | against the command arguments |
| 825 | </li> |
| 826 | </ul> |
| 827 | |
| 828 | <p> |
| 829 | <hr> |
| 830 | <h2><a name="Usage">Usage</a></h2> |
| 831 | The <code>mod_rewrite</code>'s regular expressions are POSIX extended regular |
| 832 | expressions, <b>not</b> Perl regular expressions. This can catch the |
| 833 | unsuspecting admin unawares, especially if they are used to Perl. |
| 834 | |
| 835 | <p> |
| 836 | When processing a <code>RewriteRule</code>, the <code>mod_rewrite</code> |
| 837 | engine will first execute the <code>RewriteRule</code>'s regular expression |
| 838 | against the command parameters. If that expression fails, the |
| 839 | <code>RewriteRule</code> is skipped. Any <code>RewriteCondition</code>s that |
| 840 | are attached to the rule are processed only if the rule's expression matches |
| 841 | first. |
| 842 | |
| 843 | <p> |
| 844 | One of the consequences is that the rewritten path may run afoul of any |
| 845 | configured <code>AllowFilter</code>, <code>DenyFilter</code>, |
| 846 | <code>PathAllowFilter</code>, or <code>PathDenyFilter</code> directives, |
| 847 | causing unexpected or unwanted transfers. Please keep this in mind when |
| 848 | configuring <code>RewriteRule</code>s. |
| 849 | |
| 850 | <p> |
| 851 | Some metas are not available at certain times (e.g. %U/%u for USER/PASS |
| 852 | commands, etc)... |
| 853 | |
| 854 | <p> |
| 855 | <b>Examples</b><br> |
| 856 | The following example configuration shows how to configure |
| 857 | <code>mod_rewrite</code> so that all files uploaded to the server will have |
| 858 | all-uppercase filenames: |
| 859 | <pre> |
| 860 | <IfModule mod_rewrite.c> |
| 861 | RewriteEngine on |
| 862 | |
| 863 | # Have a log for double-checking any errors |
| 864 | RewriteLog /var/log/ftpd/rewrite.log |
| 865 | |
| 866 | # Define a map that uses the internal "toupper" function |
| 867 | RewriteMap uppercase int:toupper |
| 868 | |
| 869 | # Make the file names used by STOR be in all uppercase |
| 870 | RewriteCondition %m STOR |
| 871 | |
| 872 | # Apply the map to the command parameters |
| 873 | RewriteRule ^(.*) ${uppercase:$1} |
| 874 | |
| 875 | </IfModule> |
| 876 | </pre> |
| 877 | This example shows how to convert all spaces in uploaded file names to |
| 878 | underscores: |
| 879 | <pre> |
| 880 | <IfModule mod_rewrite.c> |
| 881 | RewriteEngine on |
| 882 | RewriteLog /var/log/ftpd/rewrite.log |
| 883 | |
| 884 | # Define a map that uses the internal "replaceall" function |
| 885 | RewriteMap replace int:replaceall |
| 886 | |
| 887 | # We only want to use this rule on STOR commands |
| 888 | RewriteCondition %m STOR |
| 889 | |
| 890 | # Apply the map to the command parameters |
| 891 | RewriteRule ^(.*) "${replace:/$1/ /_}" |
| 892 | |
| 893 | </IfModule> |
| 894 | </pre> |
| 895 | |
| 896 | <p> |
| 897 | <hr> |
| 898 | <h2><a name="Installation">Installation</a></h2> |
| 899 | To install <code>mod_rewrite</code>, follow the usual steps for using |
| 900 | third-party modules in ProFTPD: |
| 901 | <pre> |
| 902 | $ ./configure --with-modules=mod_rewrite |
| 903 | </pre> |
| 904 | To build <code>mod_rewrite</code> as a DSO module: |
| 905 | <pre> |
| 906 | $ ./configure --enable-dso --with-shared=mod_rewrite |
| 907 | </pre> |
| 908 | Then follow the usual steps: |
| 909 | <pre> |
| 910 | $ make |
| 911 | $ make install |
| 912 | </pre> |
| 913 | |
| 914 | <p> |
| 915 | Alternatively, if your <code>proftpd</code> was compiled with DSO support, you |
| 916 | can use the <code>prxs</code> tool to build <code>mod_rewrite</code> as a shared |
| 917 | module: |
| 918 | <pre> |
| 919 | $ prxs -c -i -d mod_rewrite.c |
| 920 | </pre> |
| 921 | |
| 922 | <p> |
| 923 | <b>Logging</b><br> |
| 924 | The <code>mod_rewrite</code> module supports <a href="../howto/Tracing.html">trace logging</a>, via the module-specific log channels: |
| 925 | <ul> |
| 926 | <li>rewrite |
| 927 | </ul> |
| 928 | Thus for trace logging, to aid in debugging, you would use the following in |
| 929 | your <code>proftpd.conf</code>: |
| 930 | <pre> |
| 931 | TraceLog /path/to/ftpd/trace.log |
| 932 | Trace rewrite:20 |
| 933 | </pre> |
| 934 | This trace logging can generate large files; it is intended for debugging use |
| 935 | only, and should be removed from any production configuration. |
| 936 | |
| 937 | <p> |
| 938 | <hr> |
| 939 | <font size=2><b><i> |
| 940 | © Copyright 2000-2023 TJ Saunders<br> |
| 941 | All Rights Reserved<br> |
| 942 | </i></b></font> |
| 943 | <hr> |
| 944 | |
| 945 | </body> |
| 946 | </html> |