1Apache::AuthDBI(3)    User Contributed Perl Documentation   Apache::AuthDBI(3)
2
3
4

NAME

6       Apache::AuthDBI - Authentication and Authorization via Perl's DBI
7

SYNOPSIS

9        # Configuration in httpd.conf or startup.pl:
10
11        PerlModule Apache::AuthDBI
12
13        # Authentication and Authorization in .htaccess:
14
15        AuthName DBI
16        AuthType Basic
17
18        PerlAuthenHandler Apache::AuthDBI::authen
19        PerlAuthzHandler  Apache::AuthDBI::authz
20
21        PerlSetVar Auth_DBI_data_source   dbi:driver:dsn
22        PerlSetVar Auth_DBI_username      db_username
23        PerlSetVar Auth_DBI_password      db_password
24        #DBI->connect($data_source, $username, $password)
25
26        PerlSetVar Auth_DBI_pwd_table     users
27        PerlSetVar Auth_DBI_uid_field     username
28        PerlSetVar Auth_DBI_pwd_field     password
29        # authentication: SELECT pwd_field FROM pwd_table WHERE uid_field=$user
30        PerlSetVar Auth_DBI_grp_field     groupname
31        # authorization: SELECT grp_field FROM pwd_table WHERE uid_field=$user
32
33        require valid-user
34        require user   user_1  user_2 ...
35        require group group_1 group_2 ...
36
37       The AuthType is limited to Basic. You may use one or more valid require
38       lines.  For a single require line with the requirement 'valid-user' or
39       with the requirements 'user user_1 user_2 ...' it is sufficient to use
40       only the authentication handler.
41

DESCRIPTION

43       This module allows authentication and authorization against a database
44       using Perl's DBI. For supported DBI drivers see:
45
46        http://dbi.perl.org/
47
48       Authentication:
49
50       For the given username the password is looked up in the cache. If the
51       cache is not configured or if the user is not found in the cache, or if
52       the given password does not match the cached password, it is requested
53       from the database.
54
55       If the username does not exist and the authoritative directive is set
56       to 'on', the request is rejected. If the authoritative directive is set
57       to 'off', the control is passed on to next module in line.
58
59       If the password from the database for the given username is empty and
60       the nopasswd directive is set to 'off', the request is rejected. If the
61       nopasswd directive is set to 'on', any password is accepted.
62
63       Finally the passwords (multiple passwords per userid are allowed) are
64       retrieved from the database. The result is put into the environment
65       variable REMOTE_PASSWORDS. Then it is compared to the password given.
66       If the encrypted directive is set to 'on', the given password is
67       encrypted using perl's crypt() function before comparison. If the
68       encrypted directive is set to 'off' the plain-text passwords are com‐
69       pared.
70
71       If this comparison fails the request is rejected, otherwise the request
72       is accepted and the password is put into the environment variable
73       REMOTE_PASSWORD.
74
75       The SQL-select used for retrieving the passwords is as follows:
76
77        SELECT pwd_field FROM pwd_table WHERE uid_field = user
78
79       If a pwd_whereclause exists, it is appended to the SQL-select.
80
81       This module supports in addition a simple kind of logging mechanism.
82       Whenever the handler is called and a log_string is configured, the
83       log_field will be updated with the log_string. As log_string - depend‐
84       ing upon the database - macros like TODAY can be used.
85
86       The SQL-select used for the logging mechanism is as follows:
87
88        UPDATE pwd_table SET log_field = log_string WHERE uid_field = user
89
90       Authorization:
91
92       When the authorization handler is called, the authentication has
93       already been done. This means, that the given username/password has
94       been validated.
95
96       The handler analyzes and processes the requirements line by line. The
97       request is accepted if the first requirement is fulfilled.
98
99       In case of 'valid-user' the request is accepted.
100
101       In case of one or more user-names, they are compared with the given
102       user-name until the first match.
103
104       In case of one or more group-names, all groups of the given username
105       are looked up in the cache. If the cache is not configured or if the
106       user is not found in the cache, or if the requested group does not
107       match the cached group, the groups are requested from the database. A
108       comma separated list of all these groups is put into the environment
109       variable REMOTE_GROUPS. Then these groups are compared with the
110       required groups until the first match.
111
112       If there is no match and the authoritative directive is set to 'on' the
113       request is rejected.
114
115       In case the authorization succeeds, the environment variable
116       REMOTE_GROUP is set to the group name, which can be used by user
117       scripts without accessing the database again.
118
119       The SQL-select used for retrieving the groups is as follows (depending
120       upon the existence of a grp_table):
121
122        SELECT grp_field FROM pwd_table WHERE uid_field = user
123        SELECT grp_field FROM grp_table WHERE uid_field = user
124
125       This way the group-information can either be held in the main users ta‐
126       ble, or in an extra table, if there is an m:n relationship between
127       users and groups.  From all selected groups a comma-separated list is
128       build, which is compared with the required groups. If you don't like
129       normalized group records you can put such a comma-separated list of
130       groups (no spaces) into the grp_field instead of single groups.
131
132       If a grp_whereclause exists, it is appended to the SQL-select.
133
134       Cache:
135
136       The module maintains an optional cash for all passwords/groups. See the
137       method setCacheTime(n) on how to enable the cache. Every server has
138       it's own cache. Optionally the cache can be put into a shared memory
139       segment, so that it can be shared among all servers. See the CONFIGURA‐
140       TION section on how to enable the usage of shared memory.
141
142       In order to prevent the cache from growing indefinitely a CleanupHan‐
143       dler can be initialized, which skips through the cache and deletes all
144       outdated entries.  This can be done once per request after sending the
145       response, hence without slowing down response time to the client. The
146       minimum time between two successive runs of the CleanupHandler is con‐
147       figurable (see the CONFIGURATION section). The default is 0, which runs
148       the CleanupHandler after every request.
149

LIST OF TOKENS

151* Auth_DBI_data_source (Authentication and Authorization)
152The data_source value has the syntax 'dbi:driver:dsn'. This parameter is
153passed to the database driver for processing during connect. The data_source
154parameter (as well as the username and the password parameters) may be a tilde
155('~') separated list of several data_sources. All of these triples will be
156used until a successful connect is made. This way several backup-servers can
157be configured. if you want to use the environment variable DBI_DSN instead of
158a data_source, do not specify this parameter at all.
159
160* Auth_DBI_username (Authentication and Authorization)
161The username argument is passed to the database driver for processing during
162connect. This parameter may be a tilde ('~') separated list.  See the
163data_source parameter above for the usage of a list.
164
165* Auth_DBI_password (Authentication and Authorization)
166The password argument is passed to the database driver for processing during
167connect. This parameter may be a tilde ('~')  separated list.  See the
168data_source parameter above for the usage of a list.
169
170* Auth_DBI_pwd_table (Authentication and Authorization)
171Contains at least the fields with the username and the (possibly encrypted)
172password. The username should be unique.
173
174* Auth_DBI_uid_field (Authentication and Authorization)
175Field name containing the username in the Auth_DBI_pwd_table.
176
177* Auth_DBI_pwd_field (Authentication only)
178Field name containing the password in the Auth_DBI_pwd_table.
179
180* Auth_DBI_pwd_whereclause (Authentication only)
181Use this option for specifying more constraints to the SQL-select.
182
183* Auth_DBI_grp_table (Authorization only)
184Contains at least the fields with the username and the groupname.
185
186* Auth_DBI_grp_field (Authorization only)
187Field-name containing the groupname in the Auth_DBI_grp_table.
188
189* Auth_DBI_grp_whereclause (Authorization only)
190Use this option for specifying more constraints to the SQL-select.
191
192* Auth_DBI_log_field (Authentication only)
193Field name containing the log string in the Auth_DBI_pwd_table.
194
195* Auth_DBI_log_string (Authentication only)
196String to update the Auth_DBI_log_field in the Auth_DBI_pwd_table. Depending
197upon the database this can be a macro like 'TODAY'.
198
199* Auth_DBI_authoritative  < on / off> (Authentication and Authorization)
200Default is 'on'. When set 'on', there is no fall-through to other authentica‐
201tion methods if the authentication check fails. When this directive is set to
202'off', control is passed on to any other authentication modules. Be sure you
203know what you are doing when you decide to switch it off.
204
205* Auth_DBI_nopasswd  < on / off > (Authentication only)
206Default is 'off'. When set 'on' the password comparison is skipped if the
207password retrieved from the database is empty, i.e. allow any password.  This
208is 'off' by default to ensure that an empty Auth_DBI_pwd_field does not allow
209people to log in with a random password. Be sure you know what you are doing
210when you decide to switch it on.
211
212* Auth_DBI_encrypted  < on / off > (Authentication only)
213Default is 'on'. When set to 'on', the password retrieved from the database is
214assumed to be crypted. Hence the incoming password will be crypted before com‐
215parison. When this directive is set to 'off', the comparison is done directly
216with the plain-text entered password.
217
218* Auth_DBI_encryption_method < sha1hex/md5hex/crypt > (Authentication only)
219Default is blank. When set to one or more encryption method, the password
220retrieved from the database is assumed to be crypted. Hence the incoming pass‐
221word will be crypted before comparison.  The method supports falling back so
222specifying 'sha1hex/md5hex' would allow for a site that is upgrading to sha1
223to support both methods.  sha1 is the recommended method.
224
225* Auth_DBI_encryption_salt < password / userid > (Authentication only)
226When crypting the given password AuthDBI uses per default the password
227selected from the database as salt. Setting this parameter to 'userid', the
228module uses the userid as salt.
229
230* Auth_DBI_uidcasesensitive  < on / off > (Authentication and Authorization)
231Default is 'on'. When set 'off', the entered userid is converted to lower
232case.  Also the userid in the password select-statement is converted to lower
233case.
234
235* Auth_DBI_pwdcasesensitive  < on / off > (Authentication only)
236Default is 'on'. When set 'off', the entered password is converted to lower
237case.
238
239* Auth_DBI_placeholder < on / off > (Authentication and Authorization)
240Default is 'off'.  When set 'on', the select statement is prepared using a
241placeholder for the username.  This may result in improved performance for
242databases supporting this method.
243

CONFIGURATION

245       The module should be loaded upon startup of the Apache daemon.  Add the
246       following line to your httpd.conf:
247
248        PerlModule Apache::AuthDBI
249
250       A common usage is to load the module in a startup file via the PerlRe‐
251       quire directive. See eg/startup.pl for an example.
252
253       There are three configurations which are server-specific and which can
254       be done in a startup file:
255
256        Apache::AuthDBI->setCacheTime(0);
257
258       This configures the lifetime in seconds for the entries in the cache.
259       Default is 0, which turns off the cache. When set to any value n > 0,
260       the passwords/groups of all users will be cached for at least n sec‐
261       onds. After finishing the request, a special handler skips through the
262       cache and deletes all outdated entries (entries, which are older than
263       the CacheTime).
264
265        Apache::AuthDBI->setCleanupTime(-1);
266
267       This configures the minimum time in seconds between two successive runs
268       of the CleanupHandler, which deletes all outdated entries from the
269       cache. The default is -1, which disables the CleanupHandler. Setting
270       the interval to 0 runs the CleanupHandler after every request. For a
271       heavily loaded server this should be set to a value, which reflects a
272       compromise between scanning a large cache possibly containing many out‐
273       dated entries and between running many times the CleanupHandler on a
274       cache containing only few entries.
275
276        Apache::AuthDBI->setProjID(1);
277
278       This configures the project ID used to create a semaphore ID for shared
279       memory.  It can be set to any integer 1 to 255 or it will default to a
280       value of 1.
281
282       NOTE: This must be set prior to calling initIPC.
283
284       If you are running multiple instances of Apache on the same server\
285       (for example, Apache1 and Apache2), you may not want (or be able) to
286       use shared memory between them.  In this case, use a different project
287       ID on each server.
288
289       If you are reading this because you suspect you have a permission issue
290       or a collision with a semaphore, use 'ipcs -s' to list semaphores and
291       look for the Semaphore ID from the apache error log.  If found, shut‐
292       down Apache (all of them) and use 'ipcrm sem <semaphore key>' to remove
293       the colliding (and hopefully unused) semaphore.
294
295       You may also want to remove any orphaned shared memory segments by
296       using 'ipcs -m' and removing the orphans with ipcrm shm <shared memory
297       id>.
298
299        Apache::AuthDBI->initIPC(50000);
300
301       This enables the usage of shared memory for the cache. Instead of every
302       server maintaining it's own cache, all servers have access to a common
303       cache. This should minimize the database load considerably for sites
304       running many servers.  The number indicates the size of the shared mem‐
305       ory segment in bytes. This size is fixed, there is no dynamic alloca‐
306       tion of more segments. As a rule of thumb multiply the estimated maxi‐
307       mum number of simultaneously cached users by 100 to get a rough esti‐
308       mate of the needed size. Values below 500 will be overwritten with the
309       default 50000.
310
311       To enable debugging the variable $Apache::AuthDBI::DEBUG must be set.
312       This can either be done in startup.pl or in the user script. Setting
313       the variable to 1, just reports about a cache miss. Setting the vari‐
314       able to 2 enables full debug output.
315

PREREQUISITES

317       MOD_PERL 2.0
318
319       Apache::DBI version 0.96 and should work under mod_perl 2.0 RC5 and
320       later with httpd 2.0.49 and later.
321
322       Apache::DBI versions less than 1.00 are NO longer supported.  Addition‐
323       ally, mod_perl versions less then 2.0.0 are NO longer supported.
324
325       MOD_PERL 1.0 Note that this module needs mod_perl-1.08 or higher,
326       apache_1.3.0 or higher and that mod_perl needs to be configured with
327       the appropriate call-back hooks:
328
329         PERL_AUTHEN=1 PERL_AUTHZ=1 PERL_CLEANUP=1 PERL_STACKED_HANDLERS=1
330
331       Apache::DBI v0.94 was the last version before dual mod_perl 2.x support
332       was begun.  It still recommened that you use the latest version of
333       Apache::DBI because Apache::DBI versions less than 1.00 are NO longer
334       supported.
335

SECURITY

337       In some cases it is more secure not to put the username and the pass‐
338       word in the .htaccess file. The following example shows a solution to
339       this problem:
340
341       httpd.conf:
342
343        <Perl>
344        my($uid,$pwd) = My::dbi_pwd_fetch();
345        $Location{'/foo/bar'}->{PerlSetVar} = [
346            [ Auth_DBI_username  => $uid ],
347            [ Auth_DBI_password  => $pwd ],
348        ];
349        </Perl>
350

SEE ALSO

352       Apache, mod_perl, DBI
353

AUTHORS

355* Apache::AuthDBI by Edmund Mergl; now maintained and supported by the modperl
356mailinglist, subscribe by sending mail to modperl-subscribe@perl.apache.org.
357* mod_perl by Doug MacEachern.
358* DBI by Tim Bunce <dbi-users-subscribe@perl.org>
359
361       The Apache::AuthDBI module is free software; you can redistribute it
362       and/or modify it under the same terms as Perl itself.
363
364
365
366perl v5.8.8                       2007-03-23                Apache::AuthDBI(3)
Impressum