| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD module mod_sftp_sql</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center> |
| 11 | <h2><b>ProFTPD module <code>mod_sftp_sql</code></b></h2> |
| 12 | </center> |
| 13 | <hr><br> |
| 14 | |
| 15 | <p> |
| 16 | The <a href="../contrib/mod_sftp.html"><code>mod_sftp</code></a> module for |
| 17 | ProFTPD can support different storage formats for its user- and host-based |
| 18 | authorized keys. By default, the <code>mod_sftp</code> module supports storing |
| 19 | authorized keys in flat files. This <code>mod_sftp_sql</code> module allows |
| 20 | for authorized SSH keys to be stored in SQL tables. |
| 21 | |
| 22 | <p> |
| 23 | This module is contained in the <code>mod_sftp_sql.c</code> file for |
| 24 | ProFTPD 1.3.<i>x</i>, and is not compiled by default. Installation |
| 25 | instructions are discussed <a href="#Installation">here</a>. Examples |
| 26 | of how to use the <code>mod_sftp_sql</code> module are available |
| 27 | <a href="#Usage">here</a>. |
| 28 | |
| 29 | <p> |
| 30 | The most current version of <code>mod_sftp_sql</code> is distributed with the |
| 31 | ProFTPD source code. |
| 32 | |
| 33 | <p> |
| 34 | This product includes software developed by the OpenSSL Project for use in the |
| 35 | OpenSSL Toolkit (<a href="http://www.openssl.org/">http://www.openssl.org/</a>). |
| 36 | This product includes cryptographic software written by Eric Young ([email protected]). |
| 37 | |
| 38 | <h2>Author</h2> |
| 39 | <p> |
| 40 | Please contact TJ Saunders <tj <i>at</i> castaglia.org> with any |
| 41 | questions, concerns, or suggestions regarding this module. |
| 42 | |
| 43 | <p> |
| 44 | <hr> |
| 45 | <h2><a name="Installation">Installation</a></h2> |
| 46 | To build <code>mod_sftp_sql</code>, follow the usual steps for using |
| 47 | third-party modules in ProFTPD, making sure to include the |
| 48 | <code>mod_sftp</code> and <code>mod_sql</code> modules, which |
| 49 | <code>mod_sftp_sql</code> requires. For example, if you use MySQL as your |
| 50 | SQL database, then you might use: |
| 51 | <pre> |
| 52 | $ ./configure --with-modules=mod_sql:mod_sql_mysql:mod_sftp:mod_sftp_sql ... |
| 53 | $ make |
| 54 | $ make install |
| 55 | </pre> |
| 56 | |
| 57 | <p> |
| 58 | <hr><br> |
| 59 | <h2><a name="Usage">Usage</a></h2> |
| 60 | |
| 61 | <p> |
| 62 | The <code>mod_sftp_sql</code> module works by using <code>mod_sql</code>'s |
| 63 | <code>SQLNamedQuery</code> ability to define a SQL <code>SELECT</code> |
| 64 | statement which returns the requested key. Thus the <code>mod_sftp_sql</code> |
| 65 | module has no configuration directives of its own. |
| 66 | |
| 67 | <p> |
| 68 | To help demonstrate, see the example configuration below: |
| 69 | <pre> |
| 70 | <IfModule mod_sql.c> |
| 71 | # Other mod_sql configuration here |
| 72 | |
| 73 | # Define a SELECT statement to retrieve users' authorized SSH keys |
| 74 | SQLNamedQuery get-user-authorized-keys SELECT "key FROM sftpuserkeys WHERE name='%U'" |
| 75 | |
| 76 | # Define a SELECT statement to retrieve hosts' authorized SSH keys |
| 77 | SQLNamedQuery get-host-authorized-keys SELECT "key FROM sftphostkeys WHERE host='%{0}'" |
| 78 | </IfModule> |
| 79 | |
| 80 | <IfModule mod_sftp.c> |
| 81 | SFTPEngine on |
| 82 | SFTPLog /path/to/sftp.log |
| 83 | |
| 84 | # Host keys, for server host authentication |
| 85 | SFTPHostKey /etc/ssh_host_dsa_key |
| 86 | SFTPHostKey /etc/ssh_host_rsa_key |
| 87 | |
| 88 | <IfModule mod_sftp_sql.c> |
| 89 | # Instead of using a file-based key store, we tell mod_sftp to use |
| 90 | # the SQL-based key store provided by mod_sftp_sql |
| 91 | SFTPAuthorizedUserKeys sql:/get-user-authorized-keys |
| 92 | SFTPAuthorizedHostKeys sql:/get-host-authorized-keys |
| 93 | </IfModule> |
| 94 | </IfModule> |
| 95 | </pre> |
| 96 | |
| 97 | <p> |
| 98 | What should the schema be, for the table which holds these authorized keys? |
| 99 | The <b>required</b> columns are one for the key (as a single base64-encoded |
| 100 | string) and one for the name of the entity owning that key, <i>e.g.</i> the |
| 101 | user name or FQDN (or IP address) of the host. These columns can be added to |
| 102 | existing tables you might have, or be part of a new table. |
| 103 | |
| 104 | <p> |
| 105 | For example, using SQLite, you could do: |
| 106 | <pre> |
| 107 | # sqlite3 sftp.db |
| 108 | sqlite> CREATE TABLE sftpuserkeys ( |
| 109 | sqlite> name TEXT NOT NULL, |
| 110 | sqlite> key BLOB NOT NULL |
| 111 | sqlite> ); |
| 112 | sqlite> CREATE INDEX sftpuserkeys_idx ON sftpuserkeys (name); |
| 113 | |
| 114 | sqlite> CREATE TABLE sftphostkeys ( |
| 115 | sqlite> host TEXT NOT NULL, |
| 116 | sqlite> key BLOB NOT NULL |
| 117 | sqlite> ); |
| 118 | sqlite> CREATE INDEX sftphostkeys_idx ON sftphostkeys (host); |
| 119 | </pre> |
| 120 | and then configure <code>mod_sql</code> to use that <code>sftp.db</code> |
| 121 | database file. The indices are a very good idea, especially if you have many |
| 122 | rows and/or users. And for good data hygiene, adding a foreign key constraint |
| 123 | on the <code>sftpuserkeys.name</code> column to your normal users table is |
| 124 | recommended. |
| 125 | |
| 126 | <p> |
| 127 | An example MySQL schema looks like: |
| 128 | <pre> |
| 129 | CREATE TABLE sftpuserkeys ( |
| 130 | user VARCHAR(256) NOT NULL, |
| 131 | user_key VARCHAR(8192) NOT NULL |
| 132 | ); |
| 133 | CREATE INDEX sftpuserkeys_idx ON sftpuserkeys (user); |
| 134 | |
| 135 | CREATE TABLE sftphostkeys ( |
| 136 | host VARCHAR(256) NOT NULL, |
| 137 | host_key VARCHAR(8192) NOT NULL |
| 138 | ); |
| 139 | CREATE INDEX sftphostkeys_idx ON sftphostkeys (host); |
| 140 | </pre> |
| 141 | |
| 142 | <p> |
| 143 | Which leads to the next question: how can I transfer existing authorized |
| 144 | SSH keys from their current flat files into the SQL tables? First, you need |
| 145 | to make sure that the key is in the RFC4716 format, using: |
| 146 | <pre> |
| 147 | # ssh-keygen -e -f /path/to/key.pub |
| 148 | </pre> |
| 149 | Then simply add the output data to your SQL table (<i>e.g.</i> to the |
| 150 | <code>sftpuserkeys.key</code> column in the above example schema). |
| 151 | |
| 152 | <p> |
| 153 | Other databases (<i>e.g.</i> MySQL, Postgres, Oracle, <i>etc</i>) have |
| 154 | bulk data loading tools which can also be used to load a CSV file containing |
| 155 | keys into your SQL tables, for use via <code>mod_sftp_sql</code>. |
| 156 | |
| 157 | <p> |
| 158 | <b>Note</b> that the newlines which are part of the RFC 4716 formatted key |
| 159 | data <b>are important</b>. Use of the wrong data type in your SQL schema |
| 160 | could lead to unexpected parsing issues, which will be logged as: |
| 161 | <pre> |
| 162 | mod_sftp_sql/0.4[16284]: error base64-decoding raw key data from database |
| 163 | </pre> |
| 164 | |
| 165 | <p> |
| 166 | <b>Logging</b><br> |
| 167 | The <code>mod_sftp_sql</code> module supports different forms of logging. For |
| 168 | debugging purposes, the module also uses <a href="../howto/Tracing.html">trace logging</a>, via the module-specific log channels: |
| 169 | <ul> |
| 170 | <li>sftp.sql |
| 171 | </ul> |
| 172 | Thus for trace logging, to aid in debugging, you would use the following in |
| 173 | your <code>proftpd.conf</code>: |
| 174 | <pre> |
| 175 | TraceLog /path/to/sftp-trace.log |
| 176 | Trace scp:20 sftp:20 sftp.sql:20 ssh2:20 |
| 177 | </pre> |
| 178 | This trace logging can generate large files; it is intended for debugging |
| 179 | use only, and should be removed from any production configuration. |
| 180 | |
| 181 | <p> |
| 182 | <hr> |
| 183 | <font size=2><b><i> |
| 184 | © Copyright 2009-2023 TJ Saunders<br> |
| 185 | All Rights Reserved<br> |
| 186 | </i></b></font> |
| 187 | <hr> |
| 188 | |
| 189 | </body> |
| 190 | </html> |