Debian Codegraph @

doc/howto/SQL.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>ProFTPD: SQL and mod_sql</title>
5 </head>
6
7 <body bgcolor=white>
8
9 <hr>
10 <center><h2><b>ProFTPD: SQL and <code>mod_sql</code></b></h2></center>
11 <hr>
12
13 <p>
14 <b>Compiling with <code>mod_sql</code></b><br>
15 To compile <code>proftpd</code> with the <code>mod_sql</code> SQL module, you
16 will need to have the libraries and header files of a SQL database installed;
17 the <code>mysql.h</code> and <code>libmysqlclient.a</code> files for MySQL,
18 the <code>libpq-fe.h</code> and <code>libpq.a</code> files for Postgres.
19
20 <p>
21 <code>mod_sql</code> is the module that provides a generic interface between
22 the <code>proftpd</code> daemon and the underlying SQL database(s);
23 <code>mod_sql</code> relies on backend modules to handle the database-specific
24 operations. For MySQL database, the backend module is
25 <code>mod_sql_mysql</code>; <code>mod_sql_postgres</code> is to be used when
26 dealing with Postgres databases. Then, to build a <code>proftpd</code>
27 daemon to use <code>mod_sql</code>, use the <code>--with-modules</code> option
28 of the <code>configure</code> script, specifying both <code>mod_sql</code> and
29 the backed module, <i>e.g.</i>:
30 <pre>
31 $ ./configure --with-modules=mod_sql:mod_sql_mysql
32 </pre>
33 Sometimes the necessary header and library files for building in SQL support
34 are in non-standard locations; the <code>configure</code> script needs to be
35 told about these non-standard locations so that it can find the necessary files,
36 and build the daemon properly. The <code>--with-includes</code> option (for
37 header file locations) and <code>--with-libraries</code> option (for library
38 file locations) are used for informing <code>configure</code> of such things.
39 For example, if you had installed MySQL using a prefix of
40 <code>/usr/local/mysql</code>, so that the path the header file was:
41 <pre>
42 /usr/local/mysql/include/mysql/mysql.h
43 </pre>
44 and the path the library was:
45 <pre>
46 /usr/local/mysql/lib/mysql/libmysqlclient.a
47 </pre>
48 then, the above <code>configure</code> line would be changed to look like:
49 <pre>
50 $ ./configure \
51 --with-modules=mod_sql:mod_sql_mysql \
52 --with-includes=/usr/local/mysql/include/mysql \
53 --with-libraries=/usr/local/mysql/lib/mysql
54 </pre>
55 The same options can be used similarly for specifying Postgres file locations.
56 (<b>NOTE: Postgres 7.2 or higher should be used; earlier versions of Postgres
57 lacked a string-escaping function that allowed for an SQL injection
58 vulnerability. Use the <code>mod_sql_postgres</code> from proftpd 1.2.9rc1
59 or later in conjunction with a newer Postgres library to fix the bug.</b>)
60
61 <p>
62 <code>mod_sql</code> is capable of using OpenSSL for different ways of
63 encrypting passwords stored in database tables. The <code>configure</code>
64 options for building an OpenSSL-capable <code>mod_sql</code> might look
65 something like this:
66 <pre>
67 $ CFLAGS=-DHAVE_OPENSSL LIBS=-lcrypto ./configure \
68 --with-modules=mod_sql:mod_sql_mysql \
69 --with-includes=/usr/local/mysql/include/mysql:/usr/local/openssl/include \
70 --with-libraries=/usr/local/mysql/lib/mysql:/usr/local/openssl/lib
71 </pre>
72 Note that this example assumes that you have installed OpenSSL using a prefix
73 of <code>/usr/local/openssl</code>.
74
75 <p>
76 <b>Configuring <code>mod_sql</code></b><br>
77 Now that a <code>proftpd</code> daemon has been compiled for SQL support,
78 you can begin the task of configuring it so that <code>mod_sql</code> can
79 access your SQL database tables. At a very minimum, <code>mod_sql</code>
80 assumes that it has access to a table with two columns, one for user names, the
81 other for passwords. For more complete functionality, tables providing
82 full user and group information are needed. The full information that can
83 be provided is described below.
84
85 <p>
86 <b>User Information Table</b><sub>1</sub>
87 <table border=1 summary="User Information Table">
88 <tr>
89 <td><i>Column</i></td>
90 <td><i>Type</i></td>
91 <td><i>Required?</i></td>
92 <td><i>Duplicates?</i></td>
93 <td><i>Null?</i></td>
94 <td><i>Purpose</i></td>
95 </tr>
96
97 <tr>
98 <td>userid</td>
99 <td>text</td>
100 <td>yes</td>
101 <td>no</td>
102 <td>no</td>
103 <td>user's login</td>
104 </tr>
105
106 <tr>
107 <td>passwd</td>
108 <td>text</td>
109 <td>yes</td>
110 <td>yes</td>
111 <td>no</td>
112 <td>user's password</td>
113 </tr>
114
115 <tr>
116 <td>uid</td>
117 <td>number</td>
118 <td>yes</td>
119 <td>no<sub>2</sub></td>
120 <td>yes</td>
121 <td>user's UID</td>
122 </tr>
123
124 <tr>
125 <td>gid</td>
126 <td>number</td>
127 <td>no</td>
128 <td>yes</td>
129 <td>yes</td>
130 <td>user's GID</td>
131 </tr>
132
133 <tr>
134 <td>home<sub>3</sub></td>
135 <td>text</td>
136 <td>no</td>
137 <td>yes</td>
138 <td>yes</td>
139 <td>user's home</td>
140 </tr>
141
142 <tr>
143 <td>shell<sub>4</sub></td>
144 <td>text</td>
145 <td>no</td>
146 <td>yes</td>
147 <td>yes</td>
148 <td>user's shell</td>
149 </tr>
150 </table>
151
152 <p>
153 <i>Notes</i>:
154 <ol>
155 <li>The user table <b>MUST</b> exist.
156
157 <p>
158 <li>Nothing in <code>proftpd</code> or <code>mod_sql</code> prevents you
159 from using duplicate UIDs, from given multiple users the same UID. Hower,
160 this is should be done <i>only if you are certain you know what you are
161 doing</i>.
162
163 <p>
164 <li>See the <a href="../contrib/mod_sql.html#SQLDefaultHomedir"><code>SQLDefaultHomedir</code></a> and <a href="../contrib/mod_sql.html#SQLUserInfo"><code>SQLUserInfo</code></a> configuration directives.
165
166 <p>
167 <li>See the <a href="../modules/mod_auth.html#RequireValidShell"><code>RequireValidShell</code></a> configuration directive.
168 </ol>
169
170 <p>
171 <b>Group Information Table</b><sub>1</sub>
172 <table border=1 summary="Group Information Table">
173 <tr>
174 <td><i>Column</i></td>
175 <td><i>Type</i></td>
176 <td><i>Required?</i></td>
177 <td><i>Null?</i></td>
178 <td><i>Purpose</i></td>
179 </tr>
180
181 <tr>
182 <td>groupname</td>
183 <td>text</td>
184 <td>yes</td>
185 <td>no</td>
186 <td>group's name</td>
187 </tr>
188
189 <tr>
190 <td>gid</td>
191 <td>number</td>
192 <td>yes</td>
193 <td>no</td>
194 <td>group's GID</td>
195 </tr>
196
197 <tr>
198 <td>members<sub>2</sub></td>
199 <td>text</td>
200 <td>yes</td>
201 <td>yes</td>
202 <td>group's members</td>
203 </tr>
204 </table>
205
206 <p>
207 <i>Notes</i>:
208 <ol>
209 <li><code>mod_sql</code> will normally concatenate all matching group rows;
210 you can have multiple rows for each group with only one member per group,
211 or have a single row with multiple groups, or a mixing of the two. However,
212 if you use the <em>fast</em> option for <em>groupset</em> of the
213 <a href="../contrib/mod_sql.html#SQLAuthenticate"><code>SQLAuthenticate</code></a> directive, you may <i>not</i> have multiple rows per group.
214
215 <p>
216 <li>Multiple members per group are formatted as comma-separated names
217 (no contained whitespace) in the text for this column.
218 </ol>
219
220 <p>
221 The two SQL statements below should work for any ANSI SQL compliant databases,
222 and are known to work for MySQL and PostgreSQL. They both fully specify the
223 tables as described above, with reasonable defaults for field length and data
224 type. More stringent definitions are suggested: if you plan on keeping home
225 directory or shell information in the database, those fields could be defined
226 as <code>NOT NULL</code>, or even <code>UNIQUE</code> for home directory.
227 Similarly, if you plan on being able to use the <em>groupsetfast</em> argument
228 to the <code>SQLAuthenticate</code> directive, you should create both the
229 <em>groupname</em> and <em>gid</em> fields as <code>UNIQUE</code>.
230
231 <p>
232 To create a user table:
233 <pre>
234 CREATE TABLE `users` (
235 userid VARCHAR(30) NOT NULL UNIQUE,
236 passwd VARCHAR(80) NOT NULL,
237 uid INTEGER UNIQUE,
238 gid INTEGER,
239 homedir VARCHAR(255),
240 shell VARCHAR(255)
241 );
242
243 CREATE INDEX users_userid_idx ON users (userid);
244 </pre>
245 (<i>Note</i>: if you plan to reuse the same UID for multiple users, then you
246 will need to remove the <code>UNIQUE</code> from the <code>uid</code> column
247 description). To create a group table:
248 <pre>
249 CREATE TABLE `groups` (
250 groupname VARCHAR(30) NOT NULL,
251 gid INTEGER NOT NULL,
252 members VARCHAR(255)
253 );
254
255 CREATE INDEX groups_gid_idx ON groups (gid);
256 </pre>
257
258 <p>
259 The key configuration directives for <code>mod_sql</code> are:
260 <ul>
261 <li><a href="../contrib/mod_sql.html#SQLConnectInfo"><code>SQLConnectInfo</code></a> for setting the information for connecting to the database server
262 <li><a href="../contrib/mod_sql.html#SQLAuthenticate"><code>SQLAuthenticate</code></a> for controlling how the module will perform its authentication lookups
263 <li><a href="../contrib/mod_sql.html"><code>SQLAuthTypes</code></a> for defining which authentication methods to use
264 <li><a href="../contrib/mod_sql.html#SQLUserInfo"><code>SQLUserInfo</code></a> for modifying the names of the columns using which the module will lookup values from the user table
265 <li><a href="../contrib/mod_sql.html#SQLGroupInfo"><code>SQLGroupInfo</code></a> for modifying the names of the columns using which the module will lookup values from the group table
266 </ul>
267
268 <p><a name="Recipes">
269 <b>Recipes</b><br>
270
271 <p>
272 <i>Using SQLNamedConnectInfo</i><br>
273 What is <code>SQLNamedConnectInfo</code>, and how can I use it? The
274 <a href="../contrib/mod_sql.html#SQLNamedConnectInfo"><code>SQLNamedConnectInfo</code></a> directive appeared in proftpd-1.3.4rc2 (see <a href="http://bugs.proftpd.org/show_bug.cgi?id=3262">Bug#3262</a>); it allows logging to
275 SQL tables in a database separate from <i>e.g.</i> your user database. Using
276 <a href="../contrib/mod_sql.html#SQLNamedQuery"><code>SQLNamedQuery</code></a>
277 and <a href="../contrib/mod_sql.html#SQLLog"><code>SQLLog</code></a>, you tell
278 <code>mod_sql</code> to log information to your database. And now, with
279 <code>SQLNamedConnectInfo</code>, you can tell <code>mod_sql</code> to log
280 information to <em>multiple different</em> databases.
281
282 <p>
283 The following illustrates how <code>SQLNamedConnectInfo</code> can be used.
284 We have two databases, a "userdb" containing the tables with our user
285 data, and a "logdb" database containing various logging tables. First, in the
286 <code>users</code> table schema in the <code>userdb</code> database, let's
287 include a <code>users.last_accessed</code> column, indicating when that
288 user last logged in successfully:
289 <pre>
290 CREATE TABLE users (
291 userid VARCHAR(30) NOT NULL UNIQUE,
292 passwd VARCHAR(80) NOT NULL,
293 uid INTEGER UNIQUE,
294 gid INTEGER,
295 homedir VARCHAR(255),
296 shell VARCHAR(255),
297 last_accessed DATETIME
298 );
299 </pre>
300 In the <code>logdb</code> database, let's define a table that we will use for
301 separately logging all successful logins:
302 <pre>
303 CREATE TABLE login_history (
304 user VARCHAR NOT NULL,
305 client_ip VARCHAR NOT NULL,
306 server_ip VARCHAR NOT NULL,
307 protocol VARCHAR NOT NULL,
308 when DATETIME
309 );
310 </pre>
311
312 <p>
313 With these tables defined in their databases, we can now configure
314 <code>mod_sql</code> to use the <code>users</code> table for authenticating
315 users. In addition, when a user logs in successfully, update the <code>users.last_accessed</code> column <b>and</b> add a row to the
316 <code>login_history</code> table in the separate logging database:
317 <pre>
318 &lt;IfModule mod_sql.c&gt;
319 AuthOrder mod_sql.c
320
321 # We need our "default" connection to the <b>userdb</b> database
322 SQLConnectInfo userdb@dbhost:3306 user pass
323
324 # Now that we have a default connection, we can create another connection, named "logdb" and using the "mysql" backend, to the <b>logdb</b> database
325 SQLNamedConnectInfo <b><i>logdb</i></b> mysql logdb@dbhost:3306 user pass
326
327 # Point mod_sql at our users/groups tables
328 SQLUserInfo users ...
329 SQLGroupInfo groups ...
330
331 # Update the users.last_accessed column on successful login in the <b>userdb</b>
332 SQLNamedQuery last_accessed UPDATE "last_accessed = NOW() WHERE userid='%u'" users
333 SQLLog PASS last_accessed
334
335 # Add a row to the login_history table on successful login in the <b>logdb</b>
336 SQLNamedQuery log_sess FREEFORM "INSERT INTO login_history (user, client_ip, server_ip, protocol, when) VALUES ('%u', '%a', '%V', '%{protocol}', NOW())" <b><i>logdb</i></b>
337
338 SQLLog PASS log_sess IGNORE_ERRORS
339 &lt;/IfModule&gt;
340 </pre>
341 Notice how the "log_sess" <code>SQLNamedQuery</code> has "logdb" at the end of the directive, after the SQL to be run? That tells <code>mod_sql</code> that,
342 when running that <code>SQLNamedQuery</code>, use the connection named "logdb".
343
344 <p>
345 Another interesting point in the example above is the use of the
346 <em>IGNORE_ERRORS</em> modifier, when we call the "log_sess" <code>SQLNamedQuery</code>. Why is that used? Here, it means that if the logdb database is not
347 available, or if there is any other problem when inserting the row into the
348 <code>login_history</code> table, that <code>mod_sql</code> should ignore
349 that error and keep processing the connection.
350
351 <p>
352 <font color=red>Question</font>: Why does the <code>SQLNamedConnectInfo</code>
353 documentation say that "<code>SQLNamedConnectInfo</code> directives will only
354 be honored if a <code>SQLConnectInfo</code> directive is configured"?<br>
355 <font color=blue>Answer</font>: The <code>mod_sql</code> module requires
356 a <code>SQLConnectInfo</code> directive in order to define the "default"
357 named connection. This will be the database connection that
358 <code>mod_sql</code> uses for queries, <i>unless explicitly told</i> to use
359 some other named connection. And if there is no <code>SQLConnectInfo</code>
360 directive present, then <code>mod_sql</code> won't even bother to look for
361 <code>SQLNamedConnectInfo</code> directives, since the "default" connection
362 must be present in order to use any other named connection.
363
364 <p><a name="SQLPrimaryKeys">
365 <i>Using SQLUserPrimaryKey and SQLGroupPrimaryKey</i><br>
366 What is a primary key in <code>mod_sql</code>, and how can I use it? To
367 answer this question, let's look at a common database layout for a
368 <code>proftpd</code> site. You have a <code>ftp_users</code> table
369 containing the user information (<i>i.e.</i> username/password, UID, GID,
370 <i>etc</i>). You have a <code>ftp_sessions</code> logging table that
371 records all client sessions, keyed to the user that logged in. And you have
372 another logging table, <code>ftp_transfers</code>, that tracks the files
373 that are transferred in and out by clients; the entries in this table, too,
374 are keyed to the logged-in user.
375
376 <p>
377 Good schema design principles suggest that which this schema, the
378 <code>ftp_sessions</code> and <code>ftp_transfers</code> tables should have
379 foreign key constraints on columns in the <code>ftp_users</code> table, to
380 enforce the fact that entries in the <code>ftp_sessions</code> and
381 <code>ftp_transfers</code> tables <b>must</b> be related to a row in the
382 <code>ftp_users</code> table. And for efficient database storage, you want
383 the foreign key constraint to use a small column. But there's a problem.
384 The obvious choice of primary key for an <code>ftp_user</code> row would
385 be the UID, a numeric value. <b>But</b> you reuse UIDs for your users;
386 this means that a single UID does <b>not</b> uniquely identify a user.
387 In fact, the only unique identifier you have in <code>ftp_users</code> is
388 the actual username, a string. And some of your usernames are quite large.
389
390 <p>
391 To demonstrate this more clearly, let's look at the <code>mod_sql</code>
392 configuration that would be used to add rows to the <code>ftp_sessions</code>
393 table:
394 <pre>
395 # Insert a row on successful login (i.e. successful PASS)
396 SQLLog PASS start-session IGNORE_ERRORS
397
398 SQLNamedQuery start-session FREEFORM "INSERT INTO ftp_sessions (userid, session_id, ...) VALUES ('%u', '%{env:UNIQUE_ID}', ...)"
399 </pre>
400 Instead of <code>userid</code> (which is a <code>VARCHAR(255)</code>), you
401 would prefer to use the <code>ftp_users.id</code> column value, which you
402 added to your <code>ftp_users</code> table specifically to use as a primary
403 key. How to do you tell <code>mod_sql</code> to use that as the primary key
404 for the logged-in user?
405
406 <p>
407 The
408 <a href="../contrib/mod_sql.html#SQLUserPrimaryKey"><code>SQLUserPrimaryKey</code></a> and <a href="../contrib/mod_sql.html#SQLGroupPrimaryKey"><code>SQLGroupPrimaryKey</code></a> directives appeared in proftpd-1.3.5rc3 (see <a href="http://bugs.proftpd.org/show_bug.cgi?id=3864">Bug#3864</a>) for just this reason;
409 they configure the columns that <code>mod_sql</code> to retrieve/use as
410 primary keys. When these directives were present, whenever
411 <code>mod_sql</code> does a successful lookup of a user <b>by name</b> (or
412 of a group <b>by name</b>), <code>mod_sql</code> will then run another query,
413 <i>i.e.</i>:
414 <pre>
415 SELECT <i>column-name</i> FROM <i>user/group-table</i> WHERE name = <i>user/group-name</i>
416 </pre>
417 And to support the case where the tables/columns are customised, these new
418 directives would of course support custom queries:
419 <pre>
420 SQLUserPrimaryKey custom:/<i>named-query</i>
421 SQLGroupPrimaryKey custom:/<i>named-query</i>
422 </pre>
423 So that you could do:
424 <pre>
425 SQLNamedQuery get-user-key SELECT "id FROM ftp_users WHERE userid = '%U'"
426 SQLUserPrimaryKey custom:/get-user-key
427
428 SQLNamedQuery get-group-key SELECT "id FROM ftp_groups WHERE groupname = '%g'"
429 SQLGroupPrimaryKey custom:/get-group-key
430 </pre>
431
432 <p>
433 Now that <code>mod_sql</code> can look up the value for the primary key for
434 the user/group, we need to then be able to <i>use</i> that value in other
435 queries. For this, you would use the "notes" <code>SQLLog</code> variable:
436 <pre>
437 %{note:sql.user-primary-key}
438 %{note:sql.group-primary-key}
439 </pre>
440
441 <p>
442 Putting this altogether, the above <code>mod_sql</code> configuration for
443 adding rows to the <code>ftp_sessions</code> table, using
444 <code>ftp_users.id</code> as the primary key, becomes:
445 <pre>
446 # Tell mod_sql to use the ftp_users table for user data
447 SQLUserInfo ftp_users ...
448
449 # Use ftp_users.id as the user primary key
450 SQLUserPrimaryKey id
451
452 # Insert a row on successful login (i.e. successful PASS)
453 SQLLog PASS start-session IGNORE_ERRORS
454
455 SQLNamedQuery start-session FREEFORM "INSERT INTO ftp_sessions (userid, session_id, ...) VALUES (%{note:sql.user-primary-key}, '%{env:UNIQUE_ID}', ...)"
456 </pre>
457
458 <p>
459 <font color=red>Question</font>: Why use new configuration directives, rather
460 than changing the existing <code>SQLUserInfo</code>/<code>SQLGroupInfo</code>
461 directives to include a primary key column?<br>
462 <font color=blue>Answer</font>: New directives were added for this functionality
463 for two reasons: <i>1)</i> not every site needs/uses primary keys and
464 foreign key constraints in their schema, and <i>2)</i> we wanted to make
465 sure that existing configurations/schema did not break and behave unexpectedly
466 (<i>i.e.</i> to preserve backward compatibility).
467
468 <p><a name="FAQ">
469 <b>Frequently Asked Questions</b><br>
470 Whenever questions arise about problems using <code>mod_sql</code>, the first
471 place to look will be the server debugging output and in a
472 <a href="../contrib/mod_sql.html#SQLLogFile"><code>SQLLogFile</code></a>.
473 <code>mod_sql</code> is <i>very</i> verbose with its debugging information,
474 and you can see everything it is doing. However, there are times when there
475 is too much information in the debugging output, and you are unsure of how to
476 remedy the problem. These are some of the frequent questions.
477
478 <p><a name="SQLAuthoritative"></a>
479 <font color=red>Question</font>: Why is <code>proftpd</code> only looking in
480 my SQL tables when a user logs in?<br>
481 <font color=blue>Answer</font>: You probably configured <code>mod_sql</code>
482 to be &quot;authoritative&quot; in your <code>SQLAuthenticate</code> setting by
483 using the <em>*</em> option. Conversely, if you actually want
484 <code>proftpd</code> to only authenticate SQL-defined users, the <em>*</em>
485 &quot;authoritative&quot; suffix is what you would want to use.
486
487 <p><a name="SQLDefaultID"></a>
488 <font color=red>Question</font>: Why does my SQL user not use the UID/GID I
489 configured for her in my SQL table?<br>
490 <font color=blue>Answer</font>: More than likely, you gave this user a UID
491 that is below the default <a href="../contrib/mod_sql.html#SQLMinUserUID"><code>SQLMinUserUID</code></a> (999), or a GID that is
492 below the default <a href="../contrib/mod_sql.html#SQLMinUserGID"><code>SQLMinUserGID</code></a> (999). Use the
493 <code>SQLMinUserUID</code>, <code>SQLMinUserGID</code>, and/or
494 <a href="../contrib/mod_sql.html#SQLMinID"><code>SQLMinID</code></a>
495 configuration directives to set these limits lower as needed.
496
497 <p>
498 This problem also presents itself in terms of queries using UID/GID of 65533,
499 <i>e.g.</i> when you see queries like this against your database:
500 <pre>
501 SELECT groupname FROM ftp_groups WHERE (gid = 65533) LIMIT 1
502 </pre>
503 If the UID/GID retrieved from the database is below the minimum UID/GID,
504 then <code>mod_sql</code> will use the default UID/GID value, which is 65533.
505 These defaults can be changed using <a href="../contrib/mod_sql.html#SQLDefaultUID"><code>SQLDefaultUID</code></a> and <a href="../contrib/mod_sql.html#SQLDefaultGID"><code>SQLDefaultGID</code></a>.
506
507 <p><a name="SQLShell"></a>
508 <font color=red>Question</font>: Do I have to configure a real shell for my
509 SQL-defined users?<br>
510 <font color=blue>Answer</font>: No. The <code>proftpd</code> daemon only checks
511 the shell for a user in order to provide compatibility with other FTP daemons,
512 which do the same check; <code>proftpd</code> itself does not spawn the shell.
513 See the <a href="../modules/mod_auth.html#RequireValidShell"><code>RequireValidShell</code></a> configuration directive for turning this check off.
514
515 <p><a name="SQLLogQUIT"></a>
516 <font color=red>Question</font>: How come my <code>SQLLog QUIT</code> is not
517 run if the session aborts or dies?<br>
518 <font color=blue>Answer</font>: Not all FTP clients are polite and issue the
519 <code>QUIT</code> before the session ends. Perhaps their session is timed out,
520 or dropped due to network problems. Use <code>EXIT</code> as the FTP command
521 in your <code>SQLLog</code> directive, rather than <code>QUIT</code>, as
522 mentioned in the <code>SQLLog</code> documentation.
523
524 <p><a name="SQLFaster"></a>
525 <font color=red>Question</font>: How can I make <code>mod_sql</code> go
526 faster?<br>
527 <font color=blue>Answer</font>: There are a couple of things you might try.
528 First, if using a version of <code>mod_sql</code> from ProFTPD-1.2.7rc1 or
529 later, make use of the <code>SQLNegativeCache</code> configuration directive.
530
531 <p>
532 Other forms of this question are &quot;Why does <code>mod_sql</code> iterate
533 through every user in the database?&quot;, or &quot;Why is <code>mod_sql</code>
534 so slow during logins?&quot; Here's the reason: <code>mod_sql</code> is
535 designed to handle all authentication functions that the daemon throws at it.
536 This includes the functions that iterate through all users
537 (<code>setpwent()</code>, <code>getpwent()</code>, <code>endpwent()</code>) and
538 the functions that iterate through all groups (<code>setgrent()</code>,
539 <code>getgrent()</code>, <code>endgrent()</code>).
540
541 <p>
542 When you see <code>mod_sql</code> iterating through all groups or users, it is
543 doing so because it has been asked to do so by the daemon. Since there is no
544 good way to keep an open query around without adding more to the various
545 backend modules than we already have, <code>mod_sql</code> pre-caches all
546 users when <code>setpwent()</code> is called, and pre-caches all groups when
547 <code>setgrent()</code> is called. This allows the <code>getpwent()</code> and
548 <code>getgrent()</code> calls to be simple, at the cost of more time during
549 login.
550
551 <p>
552 In simple situations, these functions are never called. When you start
553 limiting access to directories, files, or various FTP commands based on user or
554 group, that is when the daemon needs to iterate through the users and groups to
555 check permissions. A basic FTP server, including virtual and anonymous
556 servers, will never make the (potentially, very) expensive user iteration
557 calls, but may iterate through all groups.
558
559 <p>
560 The <code>SQLAuthenticate</code> directive provides a method to tune
561 <code>mod_sql</code>; by default, <code>mod_sql</code> will handle the various
562 *<code>pwent()</code> and *<code>grent()</code> calls. When
563 <code>SQLAuthenticate</code> is told not to handle <em>userset</em> or
564 <em>groupset</em>, <code>mod_sql</code> simply passes the request on to
565 whatever authentication handlers exist in the system. Keep in mind that
566 using <code>SQLAuthenticate</code> in this way means that the
567 <code>proftpd</code> daemon is <b>not</b> using the same information to
568 authenticate the user as it is to control the user's actions during their
569 session.
570
571 <p>
572 For those of you who have used <code>mod_sql</code> in the past, these lookups
573 should probably be set to <em>off</em>. Versions of <code>mod_sql</code> prior
574 to 3.2.0 (or thereabouts) did not handle the
575 <code>{set|get|end}{pw|gr}ent</code> functions at all, so by setting these
576 lookups to <em>off</em>, you lose no functionality. Those of you new to
577 <code>mod_sql</code> should to consider your needs: is the cost of iterating
578 through every user stored in the database worth the ability to limit access
579 based on users/groups from the database? If not, you will need to re-evaluate
580 the way you are using the database, and where you should be storing your
581 user/group information.
582
583 <p><a name="SQLOpenSSLSQLAuthType"></a>
584 <font color=red>Question</font>: When I use an <code>SQLAuthTypes</code> that
585 includes &quot;OpenSSL&quot;, what do the values in the database need to
586 look like?<br>
587 <font color=blue>Answer</font>: The value that <code>mod_sql</code> expects
588 is the base64-encoded value of the digested password string. To get a list
589 of the message digest algorithms supported by your OpenSSL installation, you
590 can execute the following command:
591 <pre>
592 $ openssl list-message-digest-commands
593 </pre>
594 To generate the string to put into the SQL tables, using MD5 as the digest
595 algorithm and &quot;password&quot; as the password:
596 <pre>
597 $ /bin/echo &quot;{md5}&quot;`/bin/echo -n &quot;password&quot; | openssl dgst -binary -md5 | openssl enc -base64`
598 </pre>
599 The &quot;{md5}&quot; prefix is necessary, so that <code>mod_sql</code> knows
600 what digest algorithm was used.
601
602 <p>
603 Here's a quick and dirty example of generating database-ready strings using
604 every digest algorithm supported by the installed OpenSSL:
605 <pre>
606 $ for c in `openssl list-message-digest-commands`; do
607 /bin/echo "{$c}"`/bin/echo -n "password" | openssl dgst -binary -$c | openssl enc -base64`
608 done
609 </pre>
610 which generates:
611 <pre>
612 {md2}8DiBqIxuORNfDsxg79YJuQ==
613 {md4}ip0JPxT4cB3xdzKyuxgsdA==
614 {md5}X03MO1qnZdYdgyfeuILPmQ==
615 {mdc2}HA0SCu32vhW+exItsGK4lQ==
616 {rmd160}LAjo9YhHUKe5n28vNC/GONsl/zE=
617 {sha}gAclaL6zshAjJesgP20P+S9c744=
618 {sha1}W6ph5Mm5Pz8GgiULbPgzG37mj9g=
619 </pre>
620
621 <p>
622 <b>Note</b>: the <code>{digest}</code> prefix syntax is <b>only</b> needed for
623 passwords that will be handled as OpenSSL-style passwords. This prefix
624 is <b>not</b> needed for any of the other <code>SQLAuthType</code> types.
625
626 <p>
627 There are a lot of PHP applications which might want to generate passwords
628 suitable for use with <code>mod_sql</code>. For example, to generate
629 a base64-encoded MD5 password with PHP, which can then be read by
630 <code>mod_sql</code>, use the following PHP code:
631 <pre>
632 // $password contains the cleartext password before
633 $password = "{md5}".base64_encode(pack("H*", md5($password)));
634
635 // $password now contains the encrypted, encoded password
636 </pre>
637 Or if you're using Python, you might try the following:
638 <pre>
639 password = "{md5}" + base64.b64encode(md5.new("password").digest())
640 </pre>
641
642 <p>
643 If this prefixed format is not sufficient for your needs, you can also
644 use the <a href="../contrib/mod_sql_passwd.html"><code>mod_sql_passwd</code></a>
645 module, which knows how to handle "raw" MD5, SHA1, and other encoding schemes.
646
647 <p><a name="SQLBackendSQLAuthType"></a>
648 <font color=red>Question</font>: I've upgraded to MySQL 5.7, and now I am unable
649 to login using my MySQL users. The <code>SQLLogFile</code> shows something like
650 this:
651 <pre>
652 mod_sql/4.3[9097]: checking password using SQLAuthType 'Backend'
653 mod_sql/4.3[9097]: entering mysql cmd_checkauth
654 mod_sql/4.3[9097]: MySQL client library used MySQL SHA256 password format, and Backend SQLAuthType cannot succeed; consider using MD5/SHA1/SHA256 SQLAuthType using mod_sql_passwd
655 mod_sql/4.3[9097]: MySQL server used MySQL 4.1 password format for PASSWORD() value
656 mod_sql/4.3[9097]: password mismatch
657 </pre>
658 and my <code>mod_sql</code> configuration has not changed; it uses:
659 <pre>
660 SQLAuthTypes Backend
661 </pre>
662 Is this a bug?<br>
663 <p>
664 <font color=blue>Answer</font>: The short answer is that no, it is not a bug.
665 But it <b>is</b> a regression caused by changes in the MySQL API, and the only
666 fix is to change the password values stored in your MySQL tables.
667
668 <p>
669 First, the background. The "Backend" <code>SQLAuthType</code> is <b>only</b>
670 implemented by <code>mod_sql_mysql</code>, for MySQL databases; Postgres,
671 SQLite, <i>et al</i> do not have an equivalent. And in MySQL, the "Backend"
672 <code>SQLAuthType</code> is there specifically for the use case where an admin
673 uses MySQL's <code>PASSWORD()</code> function to generate the hashed value
674 stored in the <code>users</code> table for ProFTPD users. The way that the
675 "Backend" <code>SQLAuthType</code> is implemented is that
676 <code>mod_sql_mysql</code> gets the hashed value stored in the row from the
677 MySQL server, and then <code>mod_sql_mysql</code> uses a function in the
678 <code>libmysqlclient</code> library to try to generate the same hash (using the
679 password from the client); it then compares the two hashes to see if they match.
680
681 <p>
682 MySQL has had <a href="http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_password">issues</a> with its <code>PASSWORD()</code>
683 generated format over the years. They initially generated a 16 byte string,
684 but this was deemed insecure in MySQL 4.1, and changed to a 41 byte string.
685 But even that 41 byte string was found to be vulnerable to <i>e.g.</i>
686 <a href="https://en.wikipedia.org/wiki/Rainbow_table">rainbow table</a> attacks,
687 and thus the format was changed again to be much longer, to use SHA256,
688 <b>and</b> (importantly for us) to <i>always include a randomly generated
689 salt</i>. (It is this salt which mitigates the rainbow table attack scenario.)
690 This sequence of events means that a MySQL database table used by
691 <code>mod_sql_mysql</code> might contain a <code>PASSWORD()</code>-generated
692 hashed value that is in the pre-4.1 format (16 bytes), the 4.1 format
693 (41 bytes), and/or the sha256 format.
694
695 <p>
696 For the pre-4.1 and 4.1 hashed value formats, the MySQL <code>PASSWORD()</code>
697 function and the <code>libmysqlclient</code> library functions used by
698 <code>mod_sql_mysql</code> would generate the <b>same hashed value</b> for the
699 <b>same password</b>, and thus worked as expected. <em>But</em> for sha256
700 hashed value formats, <code>PASSWORD()</code> and the <code>libmysqlcient</code>
701 library functions will <b><i>not</i></b> generate the same hashed value for
702 the same password. Why not? Each time those functions are called, they
703 <i>internally</i> generate and use a random salt value; there is no way to
704 explicitly <i>provide</i> the salt value to the function. This means that the
705 sha256 hashed value <b><i>will</i></b> be different, each time. If
706 <code>PASSWORD()</code> is used to generate an sha256 formatted hashed value in
707 the database table using password "test", <b>and</b> if the
708 <code>libmysqlclient</code> library function is called to generate an sha256
709 formatted hashed using password "test", those functions will generate
710 <b><i>different</i></b> hashed values. Calling those functions again will
711 generate different hashes, each and every time; this means that they
712 <b>cannot</b> be compared/matched.
713
714 <p>
715 If sha256 hashed values are different each time, how can MySQL use them to
716 authenticate? The process used for authenticating the client
717 (the database client, <code>mod_sql_mysql</code>, not the FTP client connecting
718 to ProFTPD) is all internal to MySQL, and is slightly different; it assumes
719 that the client being authenticated is defined in the <code>mysql.users</code>
720 system table, and <b>only</b> there. That internal support does <b>not</b>
721 extend to any other table which might have used the <code>PASSWORD()</code>
722 function for its columns. (This is why that <code>PASSWORD()</code> function is
723 <a href="http://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_password">slated for deprecation</a>, as of MySQL 5.7, and later removal.)
724 This means that you can use sha256 formatted hashed values for authenticating
725 <i><code>mod_sql_mysql</code></i>, but <b>not</b> for authenticating the
726 ProFTPD users stored in the MySQL database tables.
727
728 <p>
729 Why is it only recently (as of late 2016) that users are encountering this
730 issue? MySQL 5.5 (the default MySQL version for Debian/Ubuntu for a while) did
731 not support the sha256 formatted hashed values, only the pre-4.1 and 4.1
732 formatted hashed values. So no issue with the "Backend"
733 <code>SQLAuthType</code> there. The introduction/use of sha256 formatted
734 hashed values started in MySQL 5.6.
735
736 <p>
737 But ProFTPD users using MySQL 5.6 did not encounter this problem, either. Why
738 not? Turns out that the <code>libmysqlclient</code> library in 5.6 had a
739 couple of <em>different</em> functions for making hashed values (of different
740 formats). The <code>mod_sql_mysql</code> module would try all of those
741 different functions, for backward compatibility. And it worked, as one of the
742 older <code>libmysqlclient</code> library functions (the
743 <code>make_scrambled_password()</code> function, specifically) <em>would</em>
744 generate the 4.1 formatted hash value, which would match the values stored
745 for ProFTPD users in the <code>users</code> table successfully.
746
747 <p>
748 However, in MySQL 5.7, those older <code>libmysqlclient</code> library
749 functions were <b>removed</b> (they had been deprecated for a while anyway),
750 so now, the <b>only</b> <code>libmysqlclient</code> library function for
751 generating hashed values is the sha256 one. And that, as mentioned above,
752 uses an internally-generated random salt each time, and its output is not
753 consistent/repeatable. And that means that starting with MySQL 5.7, the
754 "Backend" <code>SQLAuthType</code> will no longer work. As mentioned by the
755 log message in the <code>SQLLogFile</code>.
756
757 <p>
758 The alternatives are to use <em>different</em> <code>SQLAuthTypes</code>;
759 this <b>will</b> require that you change the <code>users.passwd</code> value
760 for your ProFTPD users. First, make sure that the <a href="../contrib/mod_sql_passwd.html"><code>mod_sql_passwd</code></a> module is compiled/loaded into your
761 ProFTPD. If you are building from source:
762 <pre>
763 $ ./configure --enable-dso --with-shared=mod_sql:mod_sql_mysql:mod_sql_passwd ...
764 $ make
765 $ make install
766 </pre>
767 And in your <code>proftpd.conf</code>:
768 <pre>
769 # Make sure that mod_sql_passwd is loaded, if available
770 LoadModule mod_sql_passwd.c
771
772 &lt;IfModule mod_sql.c&gt;
773 SQLBackend mysql
774
775 &lt;IfModule !mod_sql_passwd.c&gt;
776 # If mod_sql_passwd is not available, then consider using the OpenSSL
777 # SQLAuthType
778 SQLAuthTypes OpenSSL Backend
779 &lt;/IfModule&gt;
780
781 &lt;IfModule mod_sql_passwd.c&gt;
782 # If mod_sql_passwd is available, try other SQLAuthTypes, too
783 SQLPasswordEngine on
784
785 # MySQL uses lowercase hex-encoded strings for MD5() et al
786 SQLPasswordEncoding hex
787
788 SQLAuthTypes SHA256 SHA1 MD5 Backend
789 &lt;/IfModule&gt;
790 &lt;/IfModule&gt;
791 </pre>
792 When populating the <code>users.passwd</code> field for your users, you can
793 use MD5 passwords:
794 <pre>
795 mysql&gt; UPDATE users SET passwd = MD5('password') WHERE userid = 'myuser';
796 </pre>
797 or SHA1 passwords:
798 <pre>
799 mysql&gt; UPDATE users SET passwd = SHA1('password') WHERE userid = 'myuser';
800 </pre>
801 or SHA256 passwords:
802 <pre>
803 mysql&gt; UPDATE users SET passwd = SHA2('password', 256) WHERE userid = 'myuser';
804 </pre>
805 And the <code>SQLAuthTypes</code> configuration will try <b>all</b> of those
806 types. The "Plaintext" and "Empty" <code>SQLAuthTypes</code> should <b>never
807 be used</b> <em>except</em> for debugging/development; they are too insecure
808 for production systems.
809
810 <p><a name="SQLSSLUnableToUseRandomDevice"></a>
811 <font color=red>Question</font>: I see the following SSL-related error message
812 in my <code>SQLLogFile</code> recently; what happened?
813 <pre>
814 mod_sql/4.3[30]: unrecoverable backend error
815 mod_sql/4.3[30]: error: '2026'
816 mod_sql/4.3[30]: message: 'SSL connection error: unable to use random device'
817 </pre>
818 <font color=blue>Answer</font>: This mostly likely occurs for MySQL connections,
819 but can occur for <i>any</i> database connection that uses TLS. And
820 specifically, it can happen when the <code>DefaultRoot</code> directive is
821 used to <code>chroot</code> the sessions.
822
823 <p>
824 Newer database client libraries, such as for MySQL, now automatically try
825 to use SSL/TLS for connecting to the database server whenever possible. When
826 a session is <code>chrooted</code>, however, any SSL/TLS reconnects or
827 renegotiations can fail with the above "unable to use random device" error.
828 Why? The SSL/TLS library in use, such as OpenSSL, needs a source of randomness
829 for its cryptographic needs; it usually uses a device such as
830 <code>/dev/random</code> or <code>/dev/urandom</code>, provided by most Unix
831 platforms. However, the <code>chroot</code> means that those devices are
832 no longer accessible from within the chrooted filesystem.
833
834 <p>
835 To prevent this, you can disable use of SSL/TLS (not preferable), or use
836 something like
837 <a href="../contrib/mod_sql.html#SQLKeepAlive"><code>SQLKeepAlive</code></a> to
838 help ensure that your database connections stay alive, and thus avoid
839 reconnects.
840
841 <p><a name="SQLScoreboardError"></a>
842 <font color=red>Question</font>: Why do I see &quot;error deleting scoreboard entry: Invalid argument&quot;?<br>
843 <font color=blue>Answer</font>: This log message almost always denotes use
844 of <code>mod_sql</code>, and a problem in the <code>mod_sql</code>
845 configuration. To debug the problem, define an <a href="../contrib/mod_sql.html#SQLLogFile"><code>SQLLogFile</code></a>
846 (making sure the path is <b>not</b> in a world-writable directory), to which
847 <code>mod_sql</code> will write debugging information.
848
849 <p><a name="SQLZlibError"></a>
850 <font color=red>Question</font>: I am trying to compile <code>proftpd</code> with MySQL support, and get the following error when I run <code>make</code>. Why?<br>
851 <pre>
852 /usr/bin/ld: cannot find -lz
853 collect2: ld returned 1 exit status
854 make: *** [proftpd] Error 1
855 </pre>
856 <font color=blue>Answer</font>: This error means that the linker cannot find
857 the <code>libz</code> library. For many systems, this library is provided by
858 the <code>zlib</code> package; you may need to install this zlib package.
859 The <code>libz</code> library is required by the MySQL client libraries.
860
861 <p><a name="SQLLoaderError"></a>
862 <font color=red>Question</font>: I was able to compile <code>proftpd</code> with <code>mod_sql</code> without problems, but now when I try to start <code>proftpd</code>, I get the following error. Why?<br>
863 <pre>
864 proftpd: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
865 </pre>
866 <font color=blue>Answer</font>: Errors about "loading shared libraries",
867 especially when they happen when starting <code>proftpd</code>, come from
868 the dynamic/shared library loader. The linker was able to find that library
869 at compile time, but not at run time. If you were to look for the specified
870 library, you would see it. So why can't the shared library loader find it?
871
872 <p>
873 Updating the directories searched automatically by the shared library loader
874 is a platform-specific task. On Linux, for example, you will need to
875 edit your <code>/etc/ld.so.conf</code> file to include the directory containing
876 the SQL client libraries; then run <code>/sbin/ldconfig</code> in order
877 update the shared library loader cache file. Once the shared library loader
878 has been updated, <code>proftpd</code> should start without problems.
879
880 <p><a name="SQLUsersetfast"></a>
881 <font color=red>Question</font>: What is the difference between the
882 <em>userset</em> and <em>usersetfast</em> <code>SQLAuthenticate</code> options,
883 and when would I use them?<br>
884 <font color=blue>Answer</font>: It depends, of course.
885
886 <p>
887 First, the use of these options is <i>only</i> relevant when the session
888 is chrooted, either via the <code>DefaultRoot</code> directive or because
889 it is an <code>&lt;Anonymous&gt;</code> login. (Why? When chrooting a
890 session, <code>proftpd</code> calls the <code>setpwent()</code> function in
891 order to "reset" or "rewind" the user info database; this ensures that
892 all of the info in the user info database is accessible in the chrooted
893 session.)
894
895 <p>
896 The <em>userset</em> <code>SQLAuthenticate</code> option says that the
897 info for <i>all</i> users should be looked up and cached when
898 <code>setpwent()</code> is called. Specifically, the list of names for every
899 user in the database (and <i>just</i> the names!) is looked up. For each
900 user name, a SELECT query (using the <code>SQLUserInfo</code> configuration)
901 is used to retrieve all of the details for that user.
902
903 <p>
904 The <em>usersetfast</em> <code>SQLAuthenticate</code> option goes even further,
905 Rather than having a SELECT per user name, <em>usersetfast</em> says to lookup
906 <i>all the info for all the users</i> when <code>setpwent()</code> is called,
907 in a single SELECT query (again, based on the <code>SQLUserInfo</code>
908 configuration).
909
910 <p>
911 Knowing the above, the choice of when to use <em>userset</em> or
912 <em>usersetfast</em> becomes one of efficiency. Some efficiency (in terms
913 of ID-to-name lookups during the session) can be gained by using
914 <em>userset</em>, since the info for all users is cached (using a SELECT query
915 per user). Even more efficiency is gained by using <em>usersetfast</em> to
916 lookup the info for all users using a single SELECT query. In both cases, the
917 price you pay for the efficiency is increased memory usage in the session
918 process, of course; the info for <b>all</b> users will be held in memory in
919 the process for every session.
920
921 <p>
922 The above holds true for the <em>groupset</em> and <em>groupsetfast</em>
923 <code>SQLAuthenticate</code> options as well.
924
925 <p><a name="SQLEncryptedDBConn"></a>
926 <font color=red>Question</font>: How do I configure <code>mod_sql</code> so
927 that it will use encrypted connections (<i>e.g.</i> SSL/TLS) to the
928 backend database server?<br>
929 <font color=blue>Answer</font>: This sort of configuration depends on the
930 backend database server you are using.
931
932 <p>
933 If you are using MySQL, then you can configure this in the "[client]" section
934 of your <code>my.cnf</code> configuration file.
935
936 <p>
937 If you are using Postgres, then this will happen automatically, by default,
938 as long as your <code>libpq</code> Postgres client library has been
939 compiled with SSL support.
940
941 <p>
942 <hr>
943 <font size=2><b><i>
944 &copy; Copyright 2017-2023 The ProFTPD Project<br>
945 All Rights Reserved<br>
946 </i></b></font>
947 <hr>
948
949 </body>
950 </html>