| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD: Configuring a <Directory></title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center><h2><b>ProFTPD: Configuring a <code><Directory></code></b></h2></center> |
| 11 | <hr> |
| 12 | |
| 13 | <p> |
| 14 | Use of the <code><Directory></code> configuration directive is, in |
| 15 | general, straightforward. However, there are a few caveats of which to |
| 16 | be aware. |
| 17 | |
| 18 | <p> |
| 19 | First, it is not necessary to nest <code><Directory></code> sections, |
| 20 | like: |
| 21 | <pre> |
| 22 | <Directory /path/to/dir> |
| 23 | <Directory /path/to/dir/subdir> |
| 24 | ... |
| 25 | </Directory> |
| 26 | </Directory> |
| 27 | </pre> |
| 28 | The daemon will not let one do this, in fact. The daemon will determine |
| 29 | automatically the relations of <code><Directory></code> paths, depending |
| 30 | on the path given and surrounding configuration section. |
| 31 | |
| 32 | <p> |
| 33 | Always use the normal, absolute path for a <code><Directory></code> |
| 34 | section, regardless of whether that directory will eventually be accessed |
| 35 | during a session which has been <code>chroot</code>'d, as for |
| 36 | <code><Anonymous></code> or <code>DefaultRoot</code>ed sessions. |
| 37 | There are two allowed exceptions to the rule of using absolute paths: |
| 38 | if the path has a <code>~</code> prefix, or if the |
| 39 | <code><Directory></code> occurs within a <code><Anonymous></code> |
| 40 | section. In the latter case, the path may be relative (<i>i.e.</i> does |
| 41 | not need to start with a <code>/</code>), in which case the path will be |
| 42 | relative to the directory to which anonymous sessions are restricted. |
| 43 | |
| 44 | <p> |
| 45 | If the name of the directory contains spaces, you should enclose the entire |
| 46 | directory name in quotations, <i>e.g.</i>: |
| 47 | <pre> |
| 48 | <Directory "/path/to/My Directory"> |
| 49 | </pre> |
| 50 | |
| 51 | <p> |
| 52 | Any configuration directives in a <code><Directory></code> section |
| 53 | will apply to that directory <i>and to all of the contents of that directory |
| 54 | recursively</i>. Thus if you use: |
| 55 | <pre> |
| 56 | <Directory /path/to/dir> |
| 57 | Umask 022 |
| 58 | </Directory> |
| 59 | </pre> |
| 60 | Then that <code>Umask</code> value will be used within the |
| 61 | "/path/to/dir/subdir/" directory as well. |
| 62 | |
| 63 | <p> |
| 64 | As noted in the documentation, use of a <code>/*</code> suffix on a path |
| 65 | will change the effect of a <code><Directory></code> section |
| 66 | slightly. For example: |
| 67 | <pre> |
| 68 | <Directory /path/to/dir> |
| 69 | </pre> |
| 70 | applies the section's configuration directives to the <code>dir</code> |
| 71 | directory and its contents, while: |
| 72 | <pre> |
| 73 | <Directory /path/to/dir/*> |
| 74 | </pre> |
| 75 | applies the section's configuration directives only to the <i>contents</i> |
| 76 | of <code>dir</code>, not to the directory itself. This is a small |
| 77 | distinction, but it can often cause misconfigurations. In general, unless |
| 78 | you know what you're doing, it's best not to use a <code>/*</code> suffix. |
| 79 | |
| 80 | <p> |
| 81 | Also, a <code>*</code> within a path, such as: |
| 82 | <pre> |
| 83 | <Directory /path/to/*/dir> |
| 84 | </pre> |
| 85 | will only match that single directory level, and will not match multiple |
| 86 | directory levels. This means that the above <code><Directory></code> |
| 87 | will match: |
| 88 | <pre> |
| 89 | /path/to/<b>a/</b>dir |
| 90 | /path/to/<b>b/</b>dir |
| 91 | </pre> |
| 92 | because <code>*</code> will match the <code>a/</code> and <code>b/</code>, |
| 93 | as they are on the same level in the path as <code>*</code>. However, the |
| 94 | following paths <b>will not</b> match: |
| 95 | <pre> |
| 96 | /path/to/<b>some/other/</b>dir |
| 97 | /path/to/<b>some/other/level/</b>dir |
| 98 | </pre> |
| 99 | since <code>*</code> does not expand to <code>some/other/</code> or |
| 100 | <code>/some/other/level/</code>; they cover multiple levels. |
| 101 | |
| 102 | <p> |
| 103 | There is another case about which the administrator should know: for the |
| 104 | purposes of handling the <code>APPE</code>, <code>RETR</code>, |
| 105 | <code>RNTO</code>, <code>STOR</code>, and <code>STOU</code> FTP commands, the |
| 106 | daemon will match a <code><Directory></code> path with the filename |
| 107 | appended. As above, in most cases this will not matter much. However, |
| 108 | consider the case where the administrator specifically wants to use the |
| 109 | trailing <code>/*</code>, as when she wants a particular |
| 110 | <code><Limit></code> to apply to all subdirectories of a given directory, |
| 111 | but not to that directory itself. For example, the administrators wishes to |
| 112 | block anonymous uploads everywhere except for subdirectories of |
| 113 | <code>upload/</code>: |
| 114 | <pre> |
| 115 | <Anonymous ~ftp> |
| 116 | User ftp |
| 117 | Group ftp |
| 118 | |
| 119 | UserAlias anonymous ftp |
| 120 | |
| 121 | <Limit WRITE> |
| 122 | DenyAll |
| 123 | </Limit> |
| 124 | |
| 125 | <Directory upload/*> |
| 126 | <Limit STOR> |
| 127 | AllowAll |
| 128 | </Limit> |
| 129 | </Directory> |
| 130 | </Anonymous> |
| 131 | </pre> |
| 132 | This configuration <i>looks</i> like it should work, allowing files to be |
| 133 | uploaded only to subdirectories of <code>upload/</code>, but not to the |
| 134 | <code>upload/</code> directory itself. As described above, though, the |
| 135 | daemon will append the filename being uploaded via <code>STOR</code> to the |
| 136 | path used when looking up <code><Directory></code>, meaning that |
| 137 | <code>upload/<i>filename</i></code> will match <code>upload/*</code>, and |
| 138 | allow files to be uploaded into <code>upload/</code>. In this particular case, |
| 139 | then, what is wanted is to use this <code><Directory></code> pattern: |
| 140 | <pre> |
| 141 | <Directory upload/*/*> |
| 142 | <Limit STOR> |
| 143 | AllowAll |
| 144 | </Limit> |
| 145 | </Directory> |
| 146 | </pre> |
| 147 | which will achieve the desired effect of allowing uploads only in |
| 148 | subdirectories of the given directory, <code>upload/</code>. |
| 149 | |
| 150 | <p> |
| 151 | Also, it is good to keep in mind the <a href="ftpaccess.html">similarity</a> |
| 152 | between a <code><Directory></code> section and a <code>.ftpaccess</code> |
| 153 | file. In some cases, using <code>.ftpaccess</code> files might be more |
| 154 | convenient. The |
| 155 | <a href="../modules/mod_core.html#AllowOverride"><code>AllowOverride</code></a> |
| 156 | configuration directive (which first appeared in the 1.2.7rc1 release) provides |
| 157 | fine-grained control over when <code>.ftpaccess</code> files will be honored. |
| 158 | |
| 159 | <p> |
| 160 | The fact that <code><Directory></code> sections can be used to |
| 161 | refer to specific <i>files</i>, in addition to directories, is not obvious. |
| 162 | However, there are some cases where it can be useful to use this feature. |
| 163 | One proftpd user used this feature in the following way: the |
| 164 | <a href="../modules/mod_ls.html#DirFakeMode"><code>DirFakeMode</code></a> |
| 165 | directive was used to make all files look read-only (mostly so that FTP |
| 166 | mirroring tools would create a read-only mirror of the site). However, a |
| 167 | particular file on the site needed have execute permissions, |
| 168 | even in the FTP mirrored site. A <code><Directory></code> section |
| 169 | was used just for this one file, <i>e.g.</i>: |
| 170 | <pre> |
| 171 | # Make all files look read-only to clients, regardless of the actual |
| 172 | # permissions on the filesystem |
| 173 | DirFakeMode 0444 |
| 174 | |
| 175 | <Anonymous /var/ftpd> |
| 176 | |
| 177 | # However, for this script, we need it to look like it is executable, too |
| 178 | <Directory /var/ftpd/bin/script> |
| 179 | DirFakeMode 0555 |
| 180 | </Directory> |
| 181 | |
| 182 | </Anonymous> |
| 183 | </pre> |
| 184 | |
| 185 | <p><a name="FAQ"></a> |
| 186 | <b>Frequently Asked Questions</b><br> |
| 187 | |
| 188 | <p><a name="MultipleDirectoriesSamePath"> |
| 189 | <font color=red>Question</font>: What happens if I configure two <code><Directory></code> sections for the exact same path?<br> |
| 190 | <font color=blue>Answer</font>: If you use explicit paths, then the config |
| 191 | parser will choke on the duplicate <code><Directory></code> sections. |
| 192 | For example, if you tried: |
| 193 | <pre> |
| 194 | <Directory /path/to/dir> |
| 195 | <Limit ALL> |
| 196 | DenyAll |
| 197 | </Limit> |
| 198 | </Directory> |
| 199 | |
| 200 | <Directory /path/to/dir> |
| 201 | <Limit ALL> |
| 202 | AllowAll |
| 203 | </Limit> |
| 204 | |
| 205 | <Limit WRITE> |
| 206 | DenyAll |
| 207 | </Limit> |
| 208 | </Directory> |
| 209 | </pre> |
| 210 | When starting <code>proftpd</code>, you would see something like: |
| 211 | <pre> |
| 212 | - Fatal: <Directory>: <Directory> section already configured for '/path/to/dir' on line 39 of '/etc/ftpd/proftpd.conf' |
| 213 | </pre> |
| 214 | |
| 215 | <p> |
| 216 | But what if you have the two <code><Directory></code> sections, but |
| 217 | one of the sections uses a wildcard character which would still match the |
| 218 | same path? For example: |
| 219 | <pre> |
| 220 | <Directory /path/to/dir> |
| 221 | <Limit ALL> |
| 222 | DenyAll |
| 223 | </Limit> |
| 224 | </Directory> |
| 225 | |
| 226 | <Directory /path/*/dir> |
| 227 | <Limit ALL> |
| 228 | AllowAll |
| 229 | </Limit> |
| 230 | |
| 231 | <Limit WRITE> |
| 232 | DenyAll |
| 233 | </Limit> |
| 234 | </Directory> |
| 235 | </pre> |
| 236 | This time, the config parser would not choke; <code>proftpd</code> would start |
| 237 | up normally. When it came time to look up the <code><Directory></code> |
| 238 | section to use, <i>e.g.</i> for uploading to "/path/to/dir/test.txt", |
| 239 | the matching <code><Directory></code> section <i>which appears later in |
| 240 | the config file</i> wins. In the above example, the upload to |
| 241 | "/path/to/dir/test.txt" would be denied (because the wildcard-using |
| 242 | <code><Directory></code> section appears later, and it has a |
| 243 | <code><Limit WRITE></code> section denying writes). |
| 244 | |
| 245 | <p> |
| 246 | However, if you simply reversed the order of the above |
| 247 | <code><Directory></code> sections and tried to upload to |
| 248 | "/path/to/subdir/test.txt", <i>e.g.</i>: |
| 249 | <pre> |
| 250 | <Directory /path/*/dir> |
| 251 | <Limit ALL> |
| 252 | AllowAll |
| 253 | </Limit> |
| 254 | |
| 255 | <Limit WRITE> |
| 256 | DenyAll |
| 257 | </Limit> |
| 258 | </Directory> |
| 259 | |
| 260 | <Directory /path/to/dir> |
| 261 | <Limit ALL> |
| 262 | DenyAll |
| 263 | </Limit> |
| 264 | </Directory> |
| 265 | </pre> |
| 266 | the upload would succeed, since the non-wildcard-using |
| 267 | <code><Directory></code> section appeared later in the config. |
| 268 | |
| 269 | <p><a name="PreventDirectoryRename"></a> |
| 270 | <font color=red>Question</font>: How can I prevent a specific directory from |
| 271 | being renamed? I am currently trying: |
| 272 | <pre> |
| 273 | <Directory /dir/*> |
| 274 | <Limit CWD XCWD RNFR RNTO> |
| 275 | AllowAll |
| 276 | </Limit> |
| 277 | |
| 278 | <Limit ALL> |
| 279 | DenyAll |
| 280 | </Limit> |
| 281 | </Directory> |
| 282 | |
| 283 | <Directory /dir/subdir> |
| 284 | <Limit WRITE> |
| 285 | DenyAll |
| 286 | </Limit> |
| 287 | </Directory> |
| 288 | </pre> |
| 289 | to keep "/dir/subdir" from being renamed, but it doesn't work!<br> |
| 290 | <font color=blue>Answer</font>: The trick is to block the <code>RNFR</code> |
| 291 | command within the <code><Directory></code> section for that |
| 292 | specific directory, <i>i.e.</i>: |
| 293 | <pre> |
| 294 | <Directory /dir/subdir> |
| 295 | <Limit RNFR WRITE> |
| 296 | DenyAll |
| 297 | </Limit> |
| 298 | </Directory> |
| 299 | </pre> |
| 300 | |
| 301 | <p> |
| 302 | The reason the original config did not work as expected is that |
| 303 | <code>proftpd</code>, when handling the <code>RNTO</code> command (<i>e.g.</i> "<code>RNTO subdir2</code>"), would <b>not</b> match the |
| 304 | <code><Directory /dir/subdir></code> section for the path "/dir/subdir2", |
| 305 | but instead matches the <code><Directory /dir/*></code> section. |
| 306 | |
| 307 | <p> |
| 308 | Renaming of files via FTP is done by first sending the <code>RNFR</code> |
| 309 | command (for the old filename), then sending <code>RNTO</code> (with the |
| 310 | new filename). By placing <code>RNFR</code> in the |
| 311 | <code><Directory /dir/subdir></code> section's <code><Limit></code> |
| 312 | list, we make sure that the <code><i>RNFR</i></code> <i>does</i> match the |
| 313 | <code><Directory /dir/subdir></code> section, and is thus denied. |
| 314 | |
| 315 | <p> |
| 316 | <hr> |
| 317 | <font size=2><b><i> |
| 318 | © Copyright 2017 The ProFTPD Project<br> |
| 319 | All Rights Reserved<br> |
| 320 | </i></b></font> |
| 321 | <hr> |
| 322 | |
| 323 | </body> |
| 324 | </html> |