| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Configuration Tricks</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b>ProFTPD: Configuration Tricks</b></h2></center> |
| 11 | <hr> |
| 12 | |
| 13 | <p> |
| 14 | Proficient users of <code>proftpd</code>, and site administrators who |
| 15 | require fancy configurations, usually make use of a handful of useful |
| 16 | tricks when it comes to configuring their FTP server. These tricks can |
| 17 | help to make your <code>proftpd.conf</code> smaller, clearer, and easier to |
| 18 | maintain. |
| 19 | |
| 20 | <p><a name="ConfigVariables"> |
| 21 | <b>Configuration File Variables</b><br> |
| 22 | One juicy tidbit of configuration knowledge is the little known |
| 23 | <code>%u</code> variable. It will be substituted, during the handling of |
| 24 | an FTP session, with the name of the user who logged in. |
| 25 | |
| 26 | <p> |
| 27 | The <code>%u</code> variable can be appear in the <code>proftpd.conf</code> |
| 28 | like so: |
| 29 | <pre> |
| 30 | DefaultRoot /home/%u |
| 31 | </pre> |
| 32 | Now, the above may not seem that useful, not much of an improvement over |
| 33 | using the tilde (<code>~</code>) symbol, for restricting users to their |
| 34 | home directories. But what if you wanted to restrict users to the |
| 35 | <code>public_ftp/</code> subdirectory, within their home directories? |
| 36 | Using the tilde character cannot achieve that. Instead, you might use: |
| 37 | <pre> |
| 38 | DefaultRoot /home/%u/public_ftp |
| 39 | </pre> |
| 40 | |
| 41 | <p> |
| 42 | This is also useful for setting up configurations for specific directories |
| 43 | in all users' home directories, <i>e.g.</i>: |
| 44 | <pre> |
| 45 | <Directory /home/%u/public_ftp> |
| 46 | ... |
| 47 | </Directory> |
| 48 | </pre> |
| 49 | |
| 50 | <p> |
| 51 | This <code>%u</code> variable can appear in the following configuration |
| 52 | directives: |
| 53 | <ul> |
| 54 | <li><code>DefaultChdir</code> |
| 55 | <li><code>DefaultRoot</code> |
| 56 | <li><code><Directory></code> |
| 57 | </ul> |
| 58 | |
| 59 | <p><a name="PerUserDirHash"> |
| 60 | <b>Simple Per-User Directory Hashing</b><br> |
| 61 | Some sites find that they might have large number of users (on the order of |
| 62 | hundreds to thousands), each with their own home directory. You might start |
| 63 | off using a directory layout such as <code>/home/<i>user</i>/</code> for each |
| 64 | user, but this quickly leads to such a large number of subdirectories in the |
| 65 | <code>/home/</code>. With that number of entries, simple commands like |
| 66 | <code>ls(1)</code> may never complete, or take minutes to hours. |
| 67 | |
| 68 | <p> |
| 69 | To reduce the number of entries within a given directory, such large sites |
| 70 | often look to use some form of <i>directory hashing</i>. The simplest form |
| 71 | is to change from <i>e.g.</i> <code>/home/<i>user</i>/</code> to <code>/home/<i>u</i>/<i>us</i>/<i>user</i>/</code>. For example, if you had user "bobsmith", |
| 72 | the home directory would be <code>/home/b/bo/bobsmith</code>. |
| 73 | |
| 74 | <p> |
| 75 | The question then is: can <code>proftpd</code> be configured to use a |
| 76 | directory hashing scheme like this, without having to change the home directory |
| 77 | attribute for those thousands of users (which might be stored in an LDAP |
| 78 | directory used by applications as well)? Answer: yes. The <code>%u</code> |
| 79 | variable above supports way to handle simple directory hashing. |
| 80 | |
| 81 | <p> |
| 82 | To use it, you would use something like the following in your |
| 83 | <code>proftpd.conf</code>: |
| 84 | <pre> |
| 85 | DefaultRoot /home/%u[0]/%u |
| 86 | </pre> |
| 87 | where the <code>%u</code> variable is the user name, and |
| 88 | <code>%u[<i>index</i>]</code> can be used to reference the letters in the user |
| 89 | name, at that <em>index</em> position. The above configuration would |
| 90 | use a single level of directory hashing, resulting in <i>e.g.</i> |
| 91 | <code>/home/b/bobsmith</code>. If your directory hashing gets more complex, |
| 92 | using more levels, you can still use this syntax: |
| 93 | <pre> |
| 94 | DefaultRoot /home/%u[0]/%u[0]%u[1]/%u[0]%u[1]%u[2]/%u |
| 95 | </pre> |
| 96 | which would result in <code>/home/b/bo/bob/bobsmith</code>. |
| 97 | |
| 98 | <p> |
| 99 | Of course, more <a href="http://michaelandrews.typepad.com/the_technical_times/2009/10/creating-a-hashed-directory-structure.html">complex hashing schemes</a> |
| 100 | can be used, but <code>proftpd</code> does not currently implement them. |
| 101 | |
| 102 | <p><a name="EnvironVariables"> |
| 103 | <b>Environment Variables</b><br> |
| 104 | Did you know that you can also substitute environment variables into your |
| 105 | <code>proftpd.conf</code> as well? It's true! ProFTPD started supporting |
| 106 | using environment variables in configuration files in the 1.2.10rc1 release. |
| 107 | To use the environment variable, simply use: |
| 108 | <pre> |
| 109 | %{env:<i>ENV_VAR_NAME</i>} |
| 110 | </pre> |
| 111 | Unlike the <code>%u</code> variable, you can use environment variables |
| 112 | anywhere within your <code>proftpd.conf</code> file. |
| 113 | |
| 114 | <p> |
| 115 | For example, you might use something like this if you wanted to control |
| 116 | the default IP address on which <code>proftpd</code> listens using |
| 117 | an environment variable: |
| 118 | <pre> |
| 119 | DefaultAddress %{env:PR_DEFAULT_ADDR} |
| 120 | </pre> |
| 121 | Then whatever value the <code>PR_DEFAULT_ADDR</code> environment variable |
| 122 | has, when starting <code>proftpd</code>, will be substituted in. Using: |
| 123 | <pre> |
| 124 | PR_DEFAULT_ADDR='127.0.0.1' /usr/local/sbin/proftpd ... |
| 125 | </pre> |
| 126 | would mean that, effectively, the configuration would be: |
| 127 | <pre> |
| 128 | DefaultAddress 127.0.0.1 |
| 129 | </pre> |
| 130 | "What happens if the environment variable is not set?" you ask. Good question. |
| 131 | Then the configuration file parser will substitute in an empty string, and |
| 132 | <code>proftpd</code> will probably fail to start, citing a configuration |
| 133 | error. |
| 134 | |
| 135 | <p><a name="ServerRoot"> |
| 136 | Have you ever wanted functionality like Apache's <code>ServerRoot</code>, where |
| 137 | you specify the root directory for all of your server-related files in one |
| 138 | place, and then all the other config files relative to that root, so that |
| 139 | relocating the entire server configuration is much easier? ProFTPD does not |
| 140 | have a <code>ServerRoot</code> directive, <b>but</b> using environment |
| 141 | variables, we can achieve the same thing! |
| 142 | |
| 143 | <p> |
| 144 | At the top of your <code>proftpd.conf</code>, use the |
| 145 | <a href="../modules/mod_core.html#SetEnv"><code>SetEnv</code></a> directive, |
| 146 | like so: |
| 147 | <pre> |
| 148 | # Must be set toward start/top of config |
| 149 | SetEnv SERVER_ROOT /path/to/proftpd/config/root |
| 150 | </pre> |
| 151 | Now, for the any other config file that should be relative to that location, |
| 152 | use that environment variable, <i>e.g.</i>: |
| 153 | <pre> |
| 154 | PidFile %{env:SERVER_ROOT}/var/run/proftpd.pid |
| 155 | SystemLog %{env:SERVER_ROOT}/log/proftpd.log |
| 156 | </pre> |
| 157 | If you want to tell <code>proftpd</code> that your config files are now all |
| 158 | located elsewhere (say for a custom installation), you only need to change |
| 159 | a single configuration line: the setting of your <code>SERVER_ROOT</code> |
| 160 | environment variable. |
| 161 | |
| 162 | <p><a name="ConditionalConfigFiles"> |
| 163 | <b>Conditional Configuration Files</b><br> |
| 164 | Another common request or desire to make large sections of your configuration |
| 165 | conditional. Maybe you only want some directives to apply to certain |
| 166 | users, or maybe you want to ship a <code>proftpd.conf</code> that can be |
| 167 | used by people with different modules loaded (<i>e.g.</i> for distributing |
| 168 | a common <code>proftpd.conf</code>). Or maybe you simply want some sections |
| 169 | of your configuration file to be used based on something else entirely. |
| 170 | |
| 171 | <p> |
| 172 | The most common conditional you will probably see in various |
| 173 | <code>proftpd.conf</code> files is: |
| 174 | <pre> |
| 175 | <IfModule <i>moduleName</i>> |
| 176 | ... |
| 177 | </IfModule> |
| 178 | </pre> |
| 179 | This is way of making the configuration directives within that section only |
| 180 | in effect if the specified module is present. It may not seem like much, |
| 181 | but this is quite useful. (I have a single <code>proftpd.conf</code> with |
| 182 | more than 20 such sections, for use while I develop new modules.) |
| 183 | |
| 184 | <p> |
| 185 | I have seen one clever sysadmin use the above conditional section in conjunction |
| 186 | with DSO modules in their <code>proftpd.conf</code> like so: |
| 187 | <pre> |
| 188 | <IfModule mod_dso.c> |
| 189 | # If mod_dso is present, we know we can dynamically load DSO modules |
| 190 | |
| 191 | <IfModule !mod_sql.c> |
| 192 | # If mod_sql is not yet loaded, load it! |
| 193 | LoadModule mod_sql.c |
| 194 | </IfModule> |
| 195 | |
| 196 | <IfModule !mod_sql_mysql.c> |
| 197 | # If mod_sql_mysql is not yet loaded, load it! |
| 198 | LoadModule mod_sql_mysql.c |
| 199 | </IfModule> |
| 200 | |
| 201 | </IfModule> |
| 202 | </pre> |
| 203 | You can see how, with the above, you can use provide the same |
| 204 | <code>proftpd.conf</code> to sites which use shared modules as well as those |
| 205 | which use static modules. |
| 206 | |
| 207 | <p> |
| 208 | Users who wish to have entire sections of configuration only apply to specific |
| 209 | users, or groups, or even <a href="Classes.html">classes</a> of clients really |
| 210 | should be aware of the |
| 211 | <a href="../contrib/mod_ifsession.html"><code>mod_ifsession</code></a> module, |
| 212 | and its very handy <code><IfUser></code>, <code><IfGroup></code>, |
| 213 | and <code><IfClass></code> sections. |
| 214 | |
| 215 | <p> |
| 216 | And, for those admins who want even more control over large sections of |
| 217 | their <code>proftpd.conf</code> on a conditional basis, there is: |
| 218 | <pre> |
| 219 | <IfDefine <i>value</i>> |
| 220 | ... |
| 221 | </IfDefine> |
| 222 | </pre> |
| 223 | The enclosed section of the configuration file will be parsed only if |
| 224 | <em>value</em> is defined. For multiple directives, this trick is better |
| 225 | than using multiple environment variables; the latter is better for single |
| 226 | parameters. |
| 227 | |
| 228 | <p> |
| 229 | "But how you define <em>value</em>?", you say. There are two ways of |
| 230 | defining the values that <code><IfDefine></code> looks for: using |
| 231 | the <code>-D</code> command-line option, or by using the <code>Define</code> |
| 232 | directive. |
| 233 | |
| 234 | <p> |
| 235 | Let us assume that your <code>proftpd.conf</code> contains a section like: |
| 236 | <pre> |
| 237 | <IfDefine USE_SQL> |
| 238 | LoadModule mod_sql.c |
| 239 | LoadModule mod_sql_mysql.c |
| 240 | </IfDefine> |
| 241 | </pre> |
| 242 | You can then make sure your <code>proftpd</code> loads those modules by |
| 243 | starting it using <code>-D</code>, <i>i.e.</i>: |
| 244 | <pre> |
| 245 | /usr/local/sbin/proftpd -DUSE_SQL ... |
| 246 | </pre> |
| 247 | Or, if later you decide that you don't want to use <code>-D</code> anymore, |
| 248 | you can simply add a <code>Define</code> to the <code>proftpd.conf</code>: |
| 249 | <pre> |
| 250 | Define USE_SQL |
| 251 | |
| 252 | ... |
| 253 | |
| 254 | <IfDefine USE_SQL> |
| 255 | LoadModule mod_sql.c |
| 256 | LoadModule mod_sql_mysql.c |
| 257 | </IfDefine> |
| 258 | </pre> |
| 259 | |
| 260 | <p> |
| 261 | If you are really ambitious, you can use devious combinations of |
| 262 | <code>Define</code>, environment variables, <code><IfModule></code> and |
| 263 | <code><IfDefine></code> sections in your configuration file to achieve |
| 264 | some terse but powerful files. |
| 265 | |
| 266 | <p> |
| 267 | As a demonstration, here is an example making use of environment variables, |
| 268 | <code>Define</code>, and <code><IfDefine></code>. First, set an |
| 269 | environment variable, <i>e.g.</i> (assuming Bourne shell syntax): |
| 270 | <pre> |
| 271 | $ USE_BANS=TRUE |
| 272 | $ export USE_BANS |
| 273 | </pre> |
| 274 | Now start proftpd using the -D command-line option to set a define based |
| 275 | on the value of that environment variable: |
| 276 | <pre> |
| 277 | $ ./proftpd -DUSE_BANS=$USE_BANS ... |
| 278 | </pre> |
| 279 | And in the <code>proftpd.conf</code>, you might have something like: |
| 280 | <pre> |
| 281 | <IfDefine USE_BANS=TRUE> |
| 282 | .. |
| 283 | </IfDefine> |
| 284 | </pre> |
| 285 | in which case, the directives within that conditional section would be |
| 286 | in effect when proftpd was started. On the other hand, to disable those |
| 287 | configuration directives before starting proftpd, all that you need to do |
| 288 | now is change the environment variable value: |
| 289 | <pre> |
| 290 | $ USE_BANS=FALSE |
| 291 | $ export USE_BANS |
| 292 | </pre> |
| 293 | That <code><IfDefine></code> section will no longer be in effect |
| 294 | when proftpd is started. |
| 295 | |
| 296 | <p><a name="MultipleDaemonsSameHost"> |
| 297 | <b>Multiple Daemons on Same Host</b><br> |
| 298 | What if you wanted to run multiple instances of <code>proftpd</code> on the |
| 299 | same host? This is actually a prudent idea, for running one production |
| 300 | <code>proftpd</code> while running a different, possibly newer |
| 301 | <code>proftpd</code> for testing, side-by-side. Is it possible? Of course! |
| 302 | |
| 303 | <p> |
| 304 | Different <code>proftpd</code> daemons can coexist on the same machine, |
| 305 | but they cannot all use the same configuration file. There is a small list |
| 306 | of directives that need to have different parameters for each |
| 307 | <code>proftpd</code> instance, <i>i.e.</i>: |
| 308 | <ul> |
| 309 | <li><code>PidFile</code> |
| 310 | <li><code>ScoreboardFile</code> |
| 311 | <li><code>ServerLog</code> |
| 312 | <li><code>SystemLog</code> |
| 313 | </ul> |
| 314 | |
| 315 | <p> |
| 316 | Failure to assign different <code>PidFile</code>s and |
| 317 | <code>ScoreboardFile</code>s for each <code>proftpd</code> will cause the |
| 318 | multiple instances to overwrite each other's files, and inevitably cause |
| 319 | problems. Keeping separate log files (<code>ServerLog</code>, |
| 320 | <code>SystemLog</code>, <i>etc</i>) for each daemon is simply a good idea. |
| 321 | |
| 322 | <p> |
| 323 | What about the <code>PassivePorts</code> directive? Do the different |
| 324 | <code>proftpd</code> instances each need their own range? No. |
| 325 | When a passive data transfer is requested, <code>proftpd</code> will choose a |
| 326 | random port from within a <code>PassivePorts</code> range, <i>but not before |
| 327 | then</i>. If the port happens to be in use, <code>proftpd</code> will try |
| 328 | another random port within the range, and so on, until the range is exhausted. |
| 329 | Thus multiple <code>proftpd</code> instances should be able to share the same |
| 330 | <code>PassivePorts</code> range without issue (assuming it is a decently wide |
| 331 | range). |
| 332 | |
| 333 | <p> |
| 334 | There is one final setting which <i>can</i> cause problems: <code>Port</code>. |
| 335 | An actual incident can help illustrate the issue: |
| 336 | <blockquote> |
| 337 | I tried to setup another instance of proftpd. I copied my existing config |
| 338 | file and changed the port information. My production FTP server runs on port |
| 339 | 1979. In the test config file I specified 1980. I started the testing |
| 340 | instance on the command line by executing the following command: |
| 341 | |
| 342 | ./proftpd -d 5 -n -c /etc/proftpd/proftpd.test.conf |
| 343 | |
| 344 | The testing instance started up without any problems. Unfortunately, when a |
| 345 | client connected it gave the error message that the server is already bound |
| 346 | to port 1979. This is very strange, as the client connected successfully to |
| 347 | port 1980 in the first instance. How did the test server get the port |
| 348 | information of the other production server? |
| 349 | </blockquote> |
| 350 | |
| 351 | <p> |
| 352 | In reality, the test server did not "get" the port information from the |
| 353 | production instance. Instead, this admin was encountering the "L-1" issue. |
| 354 | The FTP RFCs state that for <b>active</b> data transfers, the <i>source</i> |
| 355 | port of the TCP connection (the source port of the TCP connection from the |
| 356 | server <i>to</i> the client) must be <i>L-1</i>, where <i>L</i> is the control |
| 357 | port number. |
| 358 | |
| 359 | <p> |
| 360 | So if your control port (configured by the <code>Port</code> directive in |
| 361 | <code>proftpd.conf</code>) is 1980, then the source port that |
| 362 | <code>proftpd</code> has to use, for any active data transfers (<i>e.g.</i> |
| 363 | for clients which use the <code>PORT</code> or <code>EPRT</code> commands), is |
| 364 | 1979. If that port 1979 is already in use by another daemon (such as another |
| 365 | <code>proftpd</code> instance as its control port), you have a collision, |
| 366 | and will likely see the "Address already in use" error message. |
| 367 | |
| 368 | <p> |
| 369 | In the case, the test server should work if the following was used: |
| 370 | <pre> |
| 371 | Port 1981 |
| 372 | </pre> |
| 373 | <i>i.e.</i> a port number that is the existing <code>proftpd</code>'s port |
| 374 | number <i>plus</i> two (or more). |
| 375 | |
| 376 | <p> |
| 377 | Using the above configuration tricks, the same <code>proftpd.conf</code> |
| 378 | file <i>could</i> be used by both the production and the test daemons, |
| 379 | using something like: |
| 380 | <pre> |
| 381 | # These directives need to differ depending on whether the test server |
| 382 | # or the production server is reading this configuration. |
| 383 | <IfDefine TEST> |
| 384 | Port 2121 |
| 385 | PidFile /var/ftpd/proftpd.test.pid |
| 386 | ScoreboardFile /var/ftpd/proftpd.test.scoreboard |
| 387 | </IfDefine> |
| 388 | |
| 389 | <IfDefine !TEST> |
| 390 | Port 2123 |
| 391 | PidFile /var/ftpd/proftpd.pid |
| 392 | ScoreboardFile /var/ftpd/proftpd.scoreboard |
| 393 | </IfDefine> |
| 394 | </pre> |
| 395 | Then, starting <code>proftpd</code> with the proper command-line invocation |
| 396 | <i>e.g.</i>: |
| 397 | <pre> |
| 398 | /usr/local/sbin/proftpd -DTEST ... |
| 399 | </pre> |
| 400 | will use the test server configuration. Omitting the <code>-D</code> |
| 401 | option on the command-line will cause <code>proftpd</code> to use the |
| 402 | production configuration. |
| 403 | |
| 404 | <p><a name="TransferPriority"> |
| 405 | <b>Reducing FTP Traffic Impact on Host System</b><br> |
| 406 | Some sites run <code>proftpd</code> on a system which is used to host other |
| 407 | services as well (<i>e.g.</i> Apache, a file server, <i>etc</i>). These |
| 408 | sites might notice that when many FTP downloads or uploads are happening, |
| 409 | the other services start to be impacted: slower/stuttering web pages or |
| 410 | streaming video/music, for example. |
| 411 | |
| 412 | <p> |
| 413 | Wouldn't it be nice if you could somehow tell <code>proftpd</code> to use |
| 414 | a lower priority, <i>i.e.</i> in the manner of the <code>nice(1)</code> |
| 415 | command, for such data transfers? Fortunately, you can do exactly this using |
| 416 | the <a href="../modules/mod_xfer.html#TransferPriority"><code>TransferPriority</code></a> directive. For example, a good default configuration to use is: |
| 417 | <pre> |
| 418 | # Use a low process priority during all data transfers |
| 419 | TransferPriority APPE,RETR,STOR,STOU low |
| 420 | </pre> |
| 421 | One user reporting always having stuttering streaming video when FTP data |
| 422 | transfers were happening. After using the <code>TransferPriority</code> |
| 423 | directive, that user saw no more video stream stuttering while still |
| 424 | approaching full LAN network speed for the FTP data transfer. |
| 425 | |
| 426 | <p> |
| 427 | <hr> |
| 428 | <font size=2><b><i> |
| 429 | © Copyright 2017 The ProFTPD Project<br> |
| 430 | All Rights Reserved<br> |
| 431 | </i></b></font> |
| 432 | <hr> |
| 433 | |
| 434 | </body> |
| 435 | </html> |