File: mod_auth_pgsql.c

package info (click to toggle)
apache 1.3.9-14.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,816 kB
  • ctags: 416
  • sloc: ansic: 3,803; perl: 722; sh: 508; makefile: 411
file content (642 lines) | stat: -rw-r--r-- 19,599 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642

/* ====================================================================
 * Copyright (c) 1996 Vidya Media Ventures, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of this source code or a derived source code must
 *    retain the above copyright notice, this list of conditions and the
 *    following disclaimer. 
 *
 * 2. Redistributions of this module or a derived module in binary form
 *    must reproduce the above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY VIDYA MEDIA VENTURES, INC. ``AS IS'' AND 
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL VIDYA MEDIA VENTURES, INC.
 * OR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This software is a contribution to and makes use of the Apache HTTP
 * server which is written, maintained and copywritten by the Apache Group.
 * See http://www.apache.org/ for more information.
 *
 * This software makes use of libpq which an interface to the PostgreSQL
 * database.  PostgreSQL is copyright (c) 1994 by the Regents of the
 * University of California.  As of this writing, more information on
 * PostgreSQL can be found at http://www.postgresSQL.org/
 *
 */

/*
 * 
 * PostgreSQL authentication
 *
 * Version 0.6 (July 1998)
 *
 * Needs libpq-fe.h and libpq.a
 *
 * Outline:
 *
 * One database, and one (or two) tables.  One table holds the username and
 * the encryped password.  The other table holds the username and the names
 * of the group to which the user belongs.  It is possible to have username,
 * groupname and password in the same table.
 *
 * 
 * Directives:
 *
 * Auth_PGhost   	Hostname of the machine running
 *		   	the postmaster. The effective uid
 *		   	of the server should be allowed
 *		   	access. 
 *
 * Auth_PGport	        Port that the postmaster is listening to.
 *
 * Auth_PGoptions	Options to set on the database connection(optional)
 *
 * Auth_PGdatabase	Name of the database in which the following
 *			table(s) are
 *			
 * Auth_PGpwd_table	Contains at least the fields with the
 *			username and the (encrypted) password
 *
 * Auth_PGgrp_table	Contains at least the fields with the
 *			username and the groupname. A user which
 *			is in multiple groups has therefore
 *			multiple entries
 *
 * Auth_PGuid_field	Name of the field containing the username
 * Auth_PGpwd_field     Fieldname for the passwords
 * Auth_PGgid_field	Fieldname for the groupname
 *
 * Auth_PG_nopasswd	<on|off>   [defaults to off]
 *			skip password comparison if passwd field is
 *			empty.
 *
 * Auth_PG_authoratative <on|off>  [defaults to on]
 *			When turned off, errors in authentication will be
 *			allowed to fall through to possibly be covered by
 *			other authorization modules or parent directory
 *			authorization schemes.  Exercise caution when deciding
 *			to turn this off!
 *
 * Auth_PG_authorative <on|off>  [defaults to on]
 *			Same as Auth_PG_Authoratative.  This is here for
 *			backwards compatability.
 *
 * Auth_PG_encrypted	<on|off>   [defaults to on]
 *			Controls weather this module expects passwords in the database
 *			to be encrypted or not.  When turned off, you can use 
 *			unencrypted passwords in your database.  Exercise caution
 *			when deciding to turn this off!
 *
 * Auth_PGpwd_whereclause  
 *                      Add this sql fragement to the end of the
 *			SQL select statement on the password table.
 *                      It would be going onto the end of a where 
 *			clause so it should start with a conjunctive
 *                      like 'and' or 'or'.
 *
 * Auth_PGgrp_whereclause  
 *                      Add this sql fragement to the end of the
 *			SQL select statement on the group table.
 *                      It would be going onto the end of a where 
 *			clause so it should start with a conjunctive
 *                      like 'and' or 'or'.
 *
 *
 * Adam Sussman (asussman@vidya.com) Feb, 1996
 *
 * see http://www.postgreSQL.org/
 *
 *
 * Version 0.0  (Feb 1996) First release (adaptation from mod_auth_msql.c v0.5)
 *			   mod_auth_msql.c was written by Dirk.vanGulik@jrc.it; 
 *                 	   [http://ewse.ceo.org; http://me-www.jrc.it/~dirkx]
 *	   0.1  (Mar 1996) Correct PGgid_field command
 *	   0.2  (Mar 1996) Added use and Auth_PGgrp_whereclause
 *	   0.3  (May 1996) Some sundry patches to fix grp_whereclause and some compilation
 *			   issues.  Thanks to Randy Terbrush.
 *	   0.4  (Jun 1996) Got rid of pg_set_string_slot, use the stock function in http_config.c
 *			   Made Auth_PGoptions take an actual string argument.
 *			   Fixed some logical sillyness in check_auth
 *			   Made command error strings more reasonable.
 *			   Try to weed out possibility of function names conflicting 
 *			     with other 3rd party modules.
 *			   Added Auth_PG_authorative directive
 *			   Added Auth_PG_enrypted directive
 *	   0.5  (Dec 1996) Some cosmetic changes to make apache 1.2 happy.
 *			   Added correctly spelled Auth_PG_Authoratative command.
 *	   0.6	(13 July 1998) Renamed to mod_auth_pgsql 
 *				Some changes just to make it compile under apache 1.3
 *				tested on apache 1.3 postgreSQL 6.4
 *				Giuseppe Tanzilli g.tanzilli@eurolink.it
 *	   0.7  (11 November 1998)	
 *				Some little changes just to make it compile in 
 *				Apache 1.3.3 & PostgreSQL 6.4
 *				the Where clause commands now need double quote
 *	   0.7.1 (02 March 1999)
 *			   (Matthias Eckermann, eckerman@lrz.uni-muenchen.de)
 *			   added 'AUTH_PGSQL_VERSION' & 'pg_auth_init_handler'
 */

#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include <libpq-fe.h>
#undef palloc

#define	AUTH_PGSQL_VERSION	"0.7.1"

/* Apache 1.2 changed some prototypes.  This is for backwards compatability */

#if (MODULE_MAGIC_NUMBER >= 19960725)
#define IS_APACHE_12         1
#define _const const
#else
#define _const
#endif

/* Apache 1.3 changed some function names  */
#if (MODULE_MAGIC_NUMBER >= 19980520)
#define IS_APACHE_13         1
#define _const const
#define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)   PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
#endif

/* Apache 1.3.1 */ 
#if (MODULE_MAGIC_NUMBER >= 19980713)
#include"ap_compat.h"
#else
#include"compat.h"
#endif



#ifndef MAX_STRING_LEN
#define MAX_STRING_LEN 8000
#endif

typedef struct  {

    char *auth_pg_host;
    char *auth_pg_database;
    char *auth_pg_port;
    char *auth_pg_options;

    char *auth_pg_pwd_table;
    char *auth_pg_grp_table;

    char *auth_pg_pwd_field;
    char *auth_pg_uname_field;
    char *auth_pg_grp_field;

    int auth_pg_nopasswd;
    int auth_pg_authorative;
    int auth_pg_encrypted;

    char *auth_pg_pwd_whereclause;
    char *auth_pg_grp_whereclause;

} pg_auth_config_rec;

void *create_pg_auth_dir_config (pool *p, char *d)
{
   pg_auth_config_rec		*new_rec;

   new_rec =  pcalloc (p, sizeof(pg_auth_config_rec));
   if (new_rec == NULL) return NULL;

   /* sane defaults */
   new_rec->auth_pg_nopasswd = 0;
   new_rec->auth_pg_authorative = 1;
   new_rec->auth_pg_encrypted = 1;

   return new_rec;
}

_const
char *pg_set_passwd_flag (cmd_parms *cmd, pg_auth_config_rec *sec, int arg) {
    sec->auth_pg_nopasswd=arg;
    return NULL;
}

_const
char* pg_set_encrypted_flag (cmd_parms *cmd, pg_auth_config_rec *sec, int arg) {
    sec->auth_pg_encrypted=arg;
    return NULL;
}

_const
char* pg_set_authorative_flag (cmd_parms *cmd, pg_auth_config_rec *sec, int arg) {
    sec->auth_pg_authorative=arg;
    return NULL;
}

command_rec pg_auth_cmds[] = {
{ "Auth_PGhost", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_host),
    OR_AUTHCFG, TAKE1, "the host name of the postgreSQL server." },

{ "Auth_PGdatabase", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_database),
    OR_AUTHCFG, TAKE1, "the name of the database that contains authorization information. " },

{ "Auth_PGport", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_port),
    OR_AUTHCFG, TAKE1, "the port the postmaster is running on. " },

{ "Auth_PGoptions", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_options),
    OR_AUTHCFG, RAW_ARGS, "an options string to be sent to the postgres backed process. " },

{ "Auth_PGpwd_table", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_table),
    OR_AUTHCFG, TAKE1, "the name of the table containing username/password tuples." },

{ "Auth_PGgrp_table", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_table),
    OR_AUTHCFG, TAKE1, "the name of the table containing username/group tuples." },

{ "Auth_PGpwd_field", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_field),
    OR_AUTHCFG, TAKE1, "the name of the password field." },

{ "Auth_PGuid_field", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_uname_field),
    OR_AUTHCFG, TAKE1, "the name of the user-id field." },

{ "Auth_PGgid_field", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_field),
    OR_AUTHCFG, TAKE1, 
	"the name of the group-name field." },

{ "Auth_PG_nopasswd", pg_set_passwd_flag, NULL, OR_AUTHCFG, FLAG, 
	"'on' or 'off'" },

{ "Auth_PG_encrypted", pg_set_encrypted_flag, NULL, OR_AUTHCFG, FLAG, 
	"'on' or 'off'" },

{ "Auth_PG_authorative", pg_set_authorative_flag, NULL, OR_AUTHCFG, FLAG, 
	"'on' or 'off'" },

{ "Auth_PG_authoratative", pg_set_authorative_flag, NULL, OR_AUTHCFG, FLAG, 
	"'on' or 'off'" },

{ "Auth_PGgrp_whereclause", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_whereclause),
    OR_AUTHCFG, TAKE1,
	"an SQL fragement that can be attached to the end of a where clause." },

{ "Auth_PGpwd_whereclause", set_string_slot,
    (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_whereclause),
    OR_AUTHCFG, TAKE1,
	"an SQL fragement that can be attached to the end of a where clause." },

{ NULL }
};

module pgsql_auth_module;

char pg_errstr[MAX_STRING_LEN];
		 /* global errno to be able to handle config/sql 
		 * failures separately
		 */

/* Do a query and return the (0,0) value.  The query is assumed to be
 * a select.
 */
char *do_pg_query(request_rec *r, char *query, pg_auth_config_rec *sec) {

    PGconn		*pg_conn;
	PGresult	*pg_result;

	char		*val;
 	char 		*result=NULL;

	pg_errstr[0]='\0';


	pg_conn = PQsetdb(sec->auth_pg_host, sec->auth_pg_port, sec->auth_pg_options,
			 NULL, sec->auth_pg_database);

	if (PQstatus(pg_conn) != CONNECTION_OK) {
		sprintf(pg_errstr, "PGSQL 1: %s", PQerrorMessage(pg_conn));
		return NULL;
	}

	pg_result = PQexec(pg_conn, query);

	if (pg_result == NULL)
	{
		sprintf(pg_errstr, "PGSQL 2: %s -- Query: %s ", PQerrorMessage(pg_conn), query);
		PQfinish(pg_conn);
		return NULL;
	}

	if (PQresultStatus(pg_result) == PGRES_EMPTY_QUERY) {
		PQclear(pg_result);
		PQfinish(pg_conn);
		return NULL;
	}

	if (PQresultStatus(pg_result) != PGRES_TUPLES_OK) {
		sprintf(pg_errstr, "PGSQL 3: %s -- Query: %s", PQerrorMessage(pg_conn), query);
		PQclear(pg_result);
		PQfinish(pg_conn);
		return NULL;
	}

	if (PQntuples(pg_result) == 1) {
		val = PQgetvalue(pg_result, 0, 0);
		if (val == NULL) {
			sprintf(pg_errstr, "PGSQL 4: %s", PQerrorMessage(pg_conn));
			PQclear(pg_result);
			PQfinish(pg_conn);
			return NULL;
		}

		if (!(result = (char*)palloc(r->pool, strlen(val)+1))) {
			sprintf (pg_errstr, "Could not get memory for Postgres query.");
			PQclear(pg_result);
			PQfinish(pg_conn);
			return NULL;
		}

		strcpy(result, val);
	}

	/* ignore errors here ! */
	PQclear(pg_result);
	PQfinish(pg_conn);
	return result;
}
	
char *get_pg_pw(request_rec *r, char *user, pg_auth_config_rec *sec) {
  	char 		query[MAX_STRING_LEN];
	char            *pass, *p;
	if (
	    (!sec->auth_pg_pwd_table) ||
	    (!sec->auth_pg_pwd_field) ||
	    (!sec->auth_pg_uname_field)
	   ) {
		sprintf(pg_errstr,
			"PG: Missing parameters for password lookup: %s%s%s",
			(sec->auth_pg_pwd_table ? "" : "Password table "),
			(sec->auth_pg_pwd_field ? "" : "Password field name "),
			(sec->auth_pg_uname_field ? "" : "UserID field name ")
			);
		return NULL;
		};

    	sprintf(query,"select %s from %s where %s='%s' ",
		sec->auth_pg_pwd_field,
		sec->auth_pg_pwd_table,
		sec->auth_pg_uname_field,
		user);

	if (sec->auth_pg_pwd_whereclause) {
		sprintf(query, "%s %s ", query, sec->auth_pg_pwd_whereclause);
	}
	pass = do_pg_query(r,query,sec);
	if (pass)
	        for (p = pass; *p; p++) if (*p == ' ') *p = 0;
	return pass;
}	   

char *get_pg_grp(request_rec *r, char *group,char *user, pg_auth_config_rec *sec) {
  	char 		query[MAX_STRING_LEN];

	if (
	    (!sec->auth_pg_grp_table) ||
	    (!sec->auth_pg_grp_field) ||
	    (!sec->auth_pg_uname_field)
	   ) {
		sprintf(pg_errstr,
			"PG: Missing parameters for password lookup: %s%s%s",
			(sec->auth_pg_grp_table ? "" : "Group table "),
			(sec->auth_pg_grp_field ? "" : "GroupID field name "),
			(sec->auth_pg_uname_field ? "" : "UserID field name ")
			);
		return NULL;
		};

    	sprintf(query,"select %s from %s where %s='%s' and %s='%s'",
		sec->auth_pg_grp_field,
		sec->auth_pg_grp_table,
		sec->auth_pg_uname_field,user,
		sec->auth_pg_grp_field,group
		);

	if (sec->auth_pg_grp_whereclause)
		sprintf(query, "%s %s ", query, sec->auth_pg_grp_whereclause);

	return do_pg_query(r,query,sec);
}	   


int pg_authenticate_basic_user (request_rec *r)
{
    pg_auth_config_rec *sec =
      (pg_auth_config_rec *)get_module_config (r->per_dir_config,
						&pgsql_auth_module);
    conn_rec *c = r->connection;
    char *sent_pw, *real_pw;
    int res;

    if ((res = get_basic_auth_pw (r, (const char **)&sent_pw)))
        return res;

    /* if *password* checking is configured in any way, i.e. then
     * handle it, if not decline and leave it to the next in line..  
     * We do not check on dbase, group, userid or host name, as it is
     * perfectly possible to only do group control and leave
     * user control to the next guy in line.
     */
    if (
    	(!sec->auth_pg_pwd_table) && 
    	(!sec->auth_pg_pwd_field) 
	 ) return DECLINED;

    pg_errstr[0]='\0';

    if(!(real_pw = get_pg_pw(r, c->user, sec ))) {
	if ( pg_errstr[0] ) {
		res = SERVER_ERROR;
		} else {
		if (sec->auth_pg_authorative) {
			/* force error and access denied */
	        	sprintf(pg_errstr,"mod_auth_pgsql: Password for user %s not found (PG-Authorative)", c->user);
			note_basic_auth_failure (r);
			res = AUTH_REQUIRED;
		} else {
			/* allow fall through to another module */
			return DECLINED;
		}
	}
	log_reason (pg_errstr, r->filename, r);
	return res;
    }    

    /* allow no password, if the flag is set and the password
     * is empty. But be sure to log this.
     */

    if ((sec->auth_pg_nopasswd) && (!strlen(real_pw))) {
        sprintf(pg_errstr,"PG: user %s: Empty password accepted",c->user);
	log_reason (pg_errstr, r->uri, r);
	return OK;
	};

    /* if the flag is off however, keep that kind of stuff at
     * an arms length.
     */
    if ((!strlen(real_pw)) || (!strlen(sent_pw))) {
        sprintf(pg_errstr,"PG: user %s: Empty Password(s) Rejected",c->user);
	log_reason (pg_errstr, r->uri, r);
	note_basic_auth_failure (r);
	return AUTH_REQUIRED;
	};

    if (sec->auth_pg_encrypted)
	sent_pw = (char*)crypt(sent_pw, real_pw);

    if(strcmp(real_pw,sent_pw)) {
        sprintf(pg_errstr,"PG user %s: password mismatch",c->user);
	log_reason (pg_errstr, r->uri, r);
	note_basic_auth_failure (r);
	return AUTH_REQUIRED;
    }
    return OK;
}
    
/* Checking ID */
    
int pg_check_auth(request_rec *r) {
    pg_auth_config_rec *sec =
      (pg_auth_config_rec *)get_module_config (r->per_dir_config,
						&pgsql_auth_module);
    char *user = r->connection->user;
    int m = r->method_number;
    int group_result = DECLINED;

    array_header *reqs_arr = (array_header *) requires (r);
    require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL;

    register int x,res;
    const char *t;
    char *w;

    pg_errstr[0]='\0';

    /* if we cannot do it; leave it to some other guy 
     */
    if ((!sec->auth_pg_grp_table)&&(!sec->auth_pg_grp_field)) 
	return DECLINED;

    if (!reqs_arr) {
	if (sec->auth_pg_authorative)
	{
		/* force error and access denied */
	       	sprintf(pg_errstr,"mod_auth_pgsql: user %s denied, no access rules specified (PG-Authorative)", user);
		log_reason (pg_errstr, r->uri, r);
		note_basic_auth_failure (r);
		res = AUTH_REQUIRED;
	} else {
		return DECLINED;
	}
    }
    
    for(x=0; x < reqs_arr->nelts; x++) {
      
	if (! (reqs[x].method_mask & (1 << m))) continue;
	
        t = reqs[x].requirement;
        w = getword(r->pool, &t, ' ');

        if(!strcmp(w,"valid-user"))
            return OK;
	
        if(!strcmp(w,"user")) {
            while(t[0]) {
                w = getword_conf (r->pool, &t);
                if(!strcmp(user,w))
                    return OK;
            }
	    if (sec->auth_pg_authorative) {
		/* force error and access denied */
	       	sprintf(pg_errstr,"mod_auth_pgsql: user %s denied, no access rules specified (PG-Authorative)", user);
		log_reason (pg_errstr, r->uri, r);
		note_basic_auth_failure (r);
		return AUTH_REQUIRED;
	    }

        } else if (!strcmp(w,"group")) {
	   /* look up the membership for each of the groups in the table */
	   pg_errstr[0] = '\0';

           while(t[0]) {
                if (get_pg_grp(r,getword(r->pool, &t, ' '),user,sec)) {
			group_result = OK;
			};
       		};

	   if (pg_errstr[0]) {
		log_reason(pg_errstr, r->filename, r);
		return SERVER_ERROR;
	   } 

	   if (group_result == OK)
		return OK;

	   if (sec->auth_pg_authorative) {
           	sprintf(pg_errstr,"user %s not in right groups (PG-Authorative)", user);
		log_reason (pg_errstr, r->uri, r);
           	note_basic_auth_failure(r);
		return AUTH_REQUIRED;
		};
           }
        }
    
    return DECLINED;
}

void pg_auth_init_handler(server_rec *s, pool *p)
{
#if MODULE_MAGIC_NUMBER >= 19980527
    ap_add_version_component("AuthPostgreSQL/" AUTH_PGSQL_VERSION);
#endif
}

module pgsql_auth_module = {
   STANDARD_MODULE_STUFF,
   pg_auth_init_handler,	/* initializer */
   create_pg_auth_dir_config,	/* dir config creater */
   NULL,			/* dir merger --- default is to override */
   NULL,			/* server config */
   NULL,			/* merge server config */
   pg_auth_cmds,		/* command table */
   NULL,			/* handlers */
   NULL,			/* filename translation */
   pg_authenticate_basic_user,	/* check_user_id */
   pg_check_auth,		/* check auth */
   NULL,			/* check access */
   NULL,			/* type_checker */
   NULL,			/* pre-run fixups */
   NULL				/* logger */
};