| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>ProFTPD module mod_quotatab_sql</title> |
| 5 | </head> |
| 6 | |
| 7 | <body bgcolor=white> |
| 8 | |
| 9 | <hr> |
| 10 | <center> |
| 11 | <h2><b>ProFTPD module <code>mod_quotatab_sql</code></b></h2> |
| 12 | </center> |
| 13 | <hr><br> |
| 14 | |
| 15 | This <code>mod_quotatab</code> submodule is contained in the |
| 16 | <code>mod_quotatab_sql.c</code>, and is found in the <code>contrib/</code> |
| 17 | area of the ProFTPD source distribution. See the <code>mod_quotatab</code> |
| 18 | <a href="mod_quotatab.html#Installation">installation</a> instructions. |
| 19 | |
| 20 | <p> |
| 21 | This submodule provides the SQL database "driver" for storing quota |
| 22 | table information in SQL tables. |
| 23 | |
| 24 | <h2>Author</h2> |
| 25 | <p> |
| 26 | Please contact TJ Saunders <tj <i>at</i> castaglia.org> with any |
| 27 | questions, concerns, or suggestions regarding this module. |
| 28 | |
| 29 | <h2>Thanks</h2> |
| 30 | <p> |
| 31 | <i>2002-04-01</i>: Thanks to Tomasz Konefal <tomk <i>at</i> compt.com> for his great feedback and support in developing this module. |
| 32 | |
| 33 | <p> |
| 34 | <i>2002-04-03</i>: Thanks to Noah <sitz <i>at</i> onastick.net> for suggesting the better form of the UPDATE query. |
| 35 | |
| 36 | <p> |
| 37 | <i>2004-01-20</i>: Thanks to Alex Ursu <alex.ursu <i>at</i> wapda.com> for supplying SQL Server FREEFORM query syntax. |
| 38 | |
| 39 | <p> |
| 40 | <hr><h2><a name="SQLTables">SQL Quota Tables</a></h2> |
| 41 | The <code>mod_quotatab_sql</code> module supports the "sql" string |
| 42 | for the <em>source-type</em> parameter of the |
| 43 | <a href="mod_quotatab.html#QuotaLimitTable"><code>QuotaLimitTable</code></a> |
| 44 | and |
| 45 | <a href="mod_quotatab.html#QuotaTallyTable"><code>QuotaTallyTable</code></a> |
| 46 | configuration directives. If the "sql" <em>source-type</em> is used, |
| 47 | then the <em>source-info</em> parameter must be as described below. Note |
| 48 | that support for SQL-based quota tables <b>requires</b> the use of |
| 49 | <code>mod_sql</code>. |
| 50 | |
| 51 | <p> |
| 52 | <code>mod_quotatab_sql</code> requires five SQL-related configuration |
| 53 | directives: <code>SQLConnectInfo</code>, and four <code>SQLNamedQuery</code> |
| 54 | directives. The <code>SQLConnectInfo</code> is needed to allow the module to |
| 55 | connect to the SQL database. The other four directives define the SQL queries |
| 56 | that will be used by <code>mod_quotatab</code> for its storage/retrieval |
| 57 | operations involving quota data. |
| 58 | |
| 59 | <p> |
| 60 | When using SQL tables and <code>mod_sql</code>, the quota tables should appear |
| 61 | in the database named by the <code>SQLConnectInfo</code> directive, which |
| 62 | will probably also contain the authentication information tables. This way |
| 63 | your <code>proftpd</code>-specific tables are kept together. Also, it is |
| 64 | probably better to use a connection policy of PERSESSION, otherwise there will |
| 65 | be more overhead associated with each new connection made to the database for |
| 66 | each transaction; <code>mod_quotatab</code> makes a lot of reads and writes to |
| 67 | its tables, each of which will require <code>mod_quotatab_sql</code> |
| 68 | to use a database connection. |
| 69 | |
| 70 | <p> |
| 71 | One <code>SQLNamedQuery</code> is needed to <code>SELECT</code> quota |
| 72 | information from the limit table. The defined SQL statement for this |
| 73 | <b>must</b> return ten values, in the following order: |
| 74 | <ul> |
| 75 | <li>name |
| 76 | <li>quota type |
| 77 | <li>quotas per session |
| 78 | <li>quota limit type |
| 79 | <li>upload limit in bytes |
| 80 | <li>download limit in bytes |
| 81 | <li>transfer limit in bytes |
| 82 | <li>upload limit in files |
| 83 | <li>download limit in files |
| 84 | <li>transfer limit in files |
| 85 | </ul> |
| 86 | A similar <code>SQLNamedQuery</code> is needed to <code>SELECT</code> quota |
| 87 | information from the tally table; the SQL statement defined for this |
| 88 | <b>must</b> return eight values, in the following order: |
| 89 | <ul> |
| 90 | <li>name |
| 91 | <li>quota type |
| 92 | <li>upload tally in bytes |
| 93 | <li>download tally in bytes |
| 94 | <li>transfer tally in bytes |
| 95 | <li>upload tally in files |
| 96 | <li>download tally in files |
| 97 | <li>transfer tally in files |
| 98 | </ul> |
| 99 | The remaining two <code>SQLNamedQuery</code> directives are used to define the |
| 100 | necessary <code>UPDATE</code> and <code>INSERT</code> operations on the |
| 101 | tally table. Failure to define any of the four required |
| 102 | <code>SQLNamedQuery</code> directives will cause an error during |
| 103 | <code>mod_quotatab_sql</code>'s operation. |
| 104 | |
| 105 | <p> |
| 106 | For SQL tables, the format for the <code>QuotaLimitTable</code> directive |
| 107 | is: |
| 108 | <pre> |
| 109 | QuotaLimitTable sql:/<i>SELECT-SQLNamedQuery</i> |
| 110 | </pre> |
| 111 | And, for the <code>QuotaTallyTable</code> directive: |
| 112 | <pre> |
| 113 | QuotaTallyTable sql:/<i>SELECT-SQLNamedQuery</i>/<i>UPDATE-SQLNamedQuery</i>/<i>INSERT-SQLNamedQuery</i> |
| 114 | </pre> |
| 115 | |
| 116 | <p> |
| 117 | Also note that SQL-based tally tables have an issue with proper synchronization |
| 118 | of updates, especially when multiple sessions involving the same tally |
| 119 | are ocurring. In order to prevent the tally table from becoming out of |
| 120 | sync, you are <b>strongly</b> encouraged to define a <a href="../contrib/mod_quotatab.html#QuotaLock"><code>QuotaLock</code></a> |
| 121 | file. |
| 122 | |
| 123 | <p> |
| 124 | <b>SQL Quota Tables Example</b><br> |
| 125 | Here are example <code>SQLNamedQuery</code> directives to help demonstrate |
| 126 | how the <code>mod_sql</code> hooks are used by <code>mod_quotatab</code>. |
| 127 | These example SQL statements assume the existence of two tables: a |
| 128 | <code>quotalimits</code> table that defines limit records, and a |
| 129 | <code>quotatallies</code> table that holds the current tally records. Note |
| 130 | that these queries would appear all on a single line in your |
| 131 | <code>proftpd.conf</code>. |
| 132 | |
| 133 | <pre> |
| 134 | SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, \ |
| 135 | bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits \ |
| 136 | WHERE name = '%{0}' AND quota_type = '%{1}'" |
| 137 | </pre> |
| 138 | This defines the SQL statement to return the required limit column values, in |
| 139 | the necessary order. The <code>%{0}</code> and <code>%{1}</code> meta |
| 140 | sequences will be substituted with the name being looked up (<i>e.g.</i> user |
| 141 | name, primary group name, class name (if in effect), or the empty string) and |
| 142 | the corresponding quota type (<i>e.g.</i> "user", "group", |
| 143 | "class", or "all"), respectively. |
| 144 | |
| 145 | <pre> |
| 146 | SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, \ |
| 147 | bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies \ |
| 148 | WHERE name = '%{0}' AND quota_type = '%{1}'" |
| 149 | </pre> |
| 150 | Very similar to the above example, the SQL statement defined here is for |
| 151 | the tally table, rather than the limit table. The meta sequences will |
| 152 | be substituted with the same values as above. |
| 153 | |
| 154 | <pre> |
| 155 | SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, \ |
| 156 | bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, \ |
| 157 | files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, \ |
| 158 | files_xfer_used = files_xfer_used + %{5} \ |
| 159 | WHERE name = '%{6}' AND quota_type = '%{7}'" quotatallies |
| 160 | </pre> |
| 161 | This SQL statement is used to update the tally table with the current tally |
| 162 | values for the session. The meta sequences will be substituted with the |
| 163 | "deltas", the difference in the known versus current tallies. |
| 164 | The reason for forming the UPDATE query this way is to provide as atomic |
| 165 | an update operation as possible; this query will update the current values |
| 166 | in the table at the time of the update, in order that any race condition |
| 167 | between a SELECT and an UPDATE query on that information be reduced to |
| 168 | as small a window as possible. |
| 169 | |
| 170 | <pre> |
| 171 | SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies |
| 172 | </pre> |
| 173 | This last SQL statement is used to create a new tally record, in the |
| 174 | event that a limit record is found for the current session but no matching |
| 175 | tally record is found. The meta sequences will be substituted with: |
| 176 | name, quota type, upload tally in bytes, download tally in bytes, transfer |
| 177 | tally in bytes, upload tally in files, download tally in files, and transfer |
| 178 | tally in files, in that order. These values are initialized to zero for |
| 179 | newly-created tally records by default. |
| 180 | |
| 181 | <p> |
| 182 | <b>Note</b>: SQL Server uses a slightly different INSERT syntax. This means |
| 183 | that if you are using <code>mod_quotatab_sql</code> in conjunction with the |
| 184 | <code>mod_sql_tds</code> module to speak to a SQL Server, your INSERT |
| 185 | query may need to look like: |
| 186 | <pre> |
| 187 | SQLNamedQuery insert-quota-tally FREEFORM \ |
| 188 | "INSERT INTO quotatallies VALUES ('%{0}','%{1}','%{2}','%{3}','%{4}','%{5}','%{6}','%{7}')" |
| 189 | </pre> |
| 190 | |
| 191 | <p> |
| 192 | Now, using the above defined queries, the table configuration directives would |
| 193 | be: |
| 194 | <pre> |
| 195 | QuotaLock /var/run/ftpd/tally.lock |
| 196 | QuotaLimitTable sql:/get-quota-limit |
| 197 | QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally |
| 198 | </pre> |
| 199 | |
| 200 | <p> |
| 201 | <b>Example Schema</b><br> |
| 202 | Here are some example table schema for SQL-based quota tables: |
| 203 | <ul> |
| 204 | <li><b><i>Limit table</i></b><br> |
| 205 | <i>MySQL example</i>: |
| 206 | <pre> |
| 207 | CREATE TABLE quotalimits ( |
| 208 | name VARCHAR(30), |
| 209 | quota_type ENUM("user", "group", "class", "all") NOT NULL, |
| 210 | per_session ENUM("false", "true") NOT NULL, |
| 211 | limit_type ENUM("soft", "hard") NOT NULL, |
| 212 | bytes_in_avail FLOAT NOT NULL, |
| 213 | bytes_out_avail FLOAT NOT NULL, |
| 214 | bytes_xfer_avail FLOAT NOT NULL, |
| 215 | files_in_avail INT UNSIGNED NOT NULL, |
| 216 | files_out_avail INT UNSIGNED NOT NULL, |
| 217 | files_xfer_avail INT UNSIGNED NOT NULL |
| 218 | ); |
| 219 | </pre> |
| 220 | <i>Postgres example</i>: |
| 221 | <pre> |
| 222 | CREATE TABLE quotalimits ( |
| 223 | name VARCHAR(32) NOT NULL, |
| 224 | quota_type VARCHAR(8) NOT NULL |
| 225 | CHECK (quota_type IN ('user', 'group', 'class', 'all')), |
| 226 | per_session BOOLEAN NOT NULL, |
| 227 | limit_type VARCHAR(4) NOT NULL |
| 228 | CHECK (limit_type IN ('soft', 'hard')), |
| 229 | bytes_in_avail FLOAT NOT NULL, |
| 230 | bytes_out_avail FLOAT NOT NULL, |
| 231 | bytes_xfer_avail FLOAT NOT NULL, |
| 232 | files_in_avail INT8 NOT NULL, |
| 233 | files_out_avail INT8 NOT NULL, |
| 234 | files_xfer_avail INT8 NOT NULL |
| 235 | ); |
| 236 | </pre> |
| 237 | </li> |
| 238 | |
| 239 | <li><b><i>Tally table</i></b><br> |
| 240 | <i>MySQL example</i>: |
| 241 | <pre> |
| 242 | CREATE TABLE quotatallies ( |
| 243 | name VARCHAR(30) NOT NULL, |
| 244 | quota_type ENUM("user", "group", "class", "all") NOT NULL, |
| 245 | bytes_in_used FLOAT NOT NULL, |
| 246 | bytes_out_used FLOAT NOT NULL, |
| 247 | bytes_xfer_used FLOAT NOT NULL, |
| 248 | files_in_used INT UNSIGNED NOT NULL, |
| 249 | files_out_used INT UNSIGNED NOT NULL, |
| 250 | files_xfer_used INT UNSIGNED NOT NULL |
| 251 | ); |
| 252 | </pre> |
| 253 | <i>Postgres example</i>: |
| 254 | <pre> |
| 255 | CREATE TABLE quotatallies ( |
| 256 | name VARCHAR(32) NOT NULL, |
| 257 | quota_type VARCHAR(8) NOT NULL |
| 258 | CHECK (quota_type IN ('user','group','class','all')), |
| 259 | bytes_in_used FLOAT NOT NULL, |
| 260 | bytes_out_used FLOAT NOT NULL, |
| 261 | bytes_xfer_used FLOAT NOT NULL, |
| 262 | files_in_used INT8 NOT NULL, |
| 263 | files_out_used INT8 NOT NULL, |
| 264 | files_xfer_used INT8 NOT NULL |
| 265 | ); |
| 266 | </pre> |
| 267 | </li> |
| 268 | </ul> |
| 269 | |
| 270 | <p> |
| 271 | <code>mod_quotatab_sql</code> treats any number <b>zero or less</b> for a |
| 272 | <b>bytes limit</b> as "unlimited". Similarly, it treats a value of |
| 273 | <b>zero</b> for any of the <b>files limit</b>s as "unlimited". |
| 274 | "Unlimited" values are ignored, in that any limit that is |
| 275 | "unlimited" is not used in any of <code>mod_quotatab</code>'s |
| 276 | calculations. NULL values should be avoided whenever possible. |
| 277 | |
| 278 | <p> |
| 279 | <hr> |
| 280 | <font size=2><b><i> |
| 281 | © Copyright 2000-2017 TJ Saunders<br> |
| 282 | All Rights Reserved<br> |
| 283 | </i></b></font> |
| 284 | <hr> |
| 285 | |
| 286 | </body> |
| 287 | </html> |
| 288 |