Module mod_auth_pgsql PostgreSQL authentication module fro Apache web server

# Apache Module mod_auth_pgsql

## PostgreSQL Authentication

This module allows user authentication (and can log authethication requests) against information stored in a PostgreSQL database. PostgreSQL is a public domain SQL database.

- Authentication

    	One database, and one (or two) tables.  One table holds the username and the encryped (or plain) 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.

- Access Logging

    	Every authentication access is logged in the same database of the authentication table, but in different table.
    	User name and date of the request are logged.
    	As option, it can log password, ip address, request line.

This page documents version 0.9.12 (2002 01 11) of mod_auth_pgsql require Apache >= 1.3.9 (tested up to 1.3.20) and PostreSQL >= 7.x

Module Directives | Download | Compilation & Installation Notes | Example | Technical Notes | Changelog

## Directives

- Auth_PG_host
- Auth_PG_port
- Auth_PG_options
- Auth_PG_database
- Auth_PG_user
- Auth_PG_pwd
- Auth_PG_pwd_table
- Auth_PG_grp_table
- Auth_PG_uid_field
- Auth_PG_pwd_field
- Auth_PG_gid_field
- Auth_PG_nopasswd
- Auth_PG_authoritative
- Auth_PG_lowercase_uid
- Auth_PG_uppercase_uid
- Auth_PG_pwd_ignore_case
- Auth_PG_encrypted
- Auth_PG_hash_type
- Auth_PG_pwd_whereclause
- Auth_PG_grp_whereclause
- Auth_PG_cache_passwords
- Auth_PG_log_table
- Auth_PG_log_uname_field
- Auth_PG_log_date_field
- Auth_PG_log_uri_field
- Auth_PG_log_addrs_field
- Auth_PG_log_pwd_field

## Auth_PG_host

Syntax: Auth_PG_host hostname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the host on which the postmaster is running. The effective uid of the server should be allowed access, otherwise a trusted user or user with password must be specified.

## Auth_PG_port

Syntax: Auth_PG_port port number
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the TCP/IP port number at which the postmaster can be found.

## Auth_PG_options

Syntax: Auth_PG_options option string
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies an option string to be passed to the postgreSQL backend process. Refer to the PostgreSQL user manual for a description of the available options.

## Auth_PG_database

Syntax: Auth_PG_database database name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the name of the database that stores the authentication information.

## Auth_PG_user

Syntax: Auth_PG_user username
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the database username who access the PostgreSQL, should have read access on all authetication tables, and write access on all the log tables (if used).
Needed if the user who make the quey is differrent from the user runnig apache, or if the posmater is on a different server and you must autheticate with password

## Auth_PG_pwd

Syntax: Auth_PG_pwd password
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the user password for the user who access the PostgreSQL.
Needed if the user specified with Auth_PG_user is not trusted.

## Auth_PG_pwd_table

Syntax: Auth_PG_pwd_table relation name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Gives the name of the relation which contains the username and password information.
See Auth_PG_encrypted, by default the password is encrypted.

## Auth_PG_grp_table

Syntax: Auth_PG_grp_table relation name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Gives the name of the relation which contains the username and group information. This can be the same table specified with Auth_PG_pwd_table. This directive is only necessary if you want to authenticate by user groups. A user within multiple groups has therefore multiple entries.

## Auth_PG_uid_field

Syntax: Auth_PG_uid_field attribute name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the attribute name of the field containing the user name in the Auth_PG_pwd_table relation.

## Auth_PG_pwd_field

Syntax: Auth_PG_pwd_field attribute name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the attribute name of the field containing the encrypted (see Auth_PG_encrypted) password in the Auth_PGpwd_table relation.

## Auth_PG_gid_field

Syntax: Auth_PG_gid_field attribute name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Specifies the attribute name of the field containing the group name in the Auth_PG_grp_table relation. This directive is only necessary if you want to authenticate by user groups.

## Auth_PG_nopasswd

Syntax: Auth_PG_nopasswd on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option is off by default. Turning it on will cause a user to be validated when their password field is empty. The password entered will be ignored. Exercise caution when turning this on.

## Auth_PG_authoritative

Syntax: Auth_PG_authoritative on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option is on by default. Turning it off will cause low level errors such a user not being found or a simple configuration error to fall through to other authentication directives which may be defined for this area. For example, if a parent directory has another authorization scheme and a user name is not found for the PostgreSQL scheme, the parent directory scheme will be given the chance to try and authenticate the user. Exercise caution when turning this option off. It can be a security risk. Can be used to use two authentication schemes for the same dir.

## Auth_PG_lowercase_uid

Syntax: Auth_PG_lowercase_uid on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option is off by default. Controls whether this module converts user UIDs to lowercase before looking them up. When turned on this does not affect the case of the original user ID should this module decline to authenticate and a lower level is called.

## Auth_PG_uppercase_uid

Syntax: Auth_PG_uppercase_uid on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option is off by default. Controls whether this module converts user UIDs to uppercase before looking them up. When turned on this does not affect the case of the original user ID should this module decline to authenticate and a lower level is called.

## Auth_PG_pwd_ignore_case

Syntax: Auth_PG_pwd_ignore_case on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option is off by default. Controls whether this module ignore the upper/lowercase of password from user, when looking up clear text password from db.

## Auth_PG_encrypted

Syntax: Auth_PG_encrypted on or off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

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_PG_hash_type

Syntax: Auth_PG_hash_type CRYPT or MD5
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

Set the encryption type for the password stored in the database.
Defaults to CRYPT.

## Auth_PG_pwd_whereclause

Syntax: Auth_PG_pwd_whereclause SQL fragment
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option allows you to exercise greater control over the SQL code used to retrieve the user name and password from the database. You can use this to search for the username using more attributes in the table than the pwd_field.

The basic SQL statement used to retrieve a user's password for checking looks like this:

select <pwd_field> from <pwd_table> where <uid_field> ='<remote_user> '

The pwd_whereclause will be added to the end of this statement and must fit logically. The where clause must be double quoted, with initial space .

Example:

          Auth_PG_pwd_whereclause " and access_level > 100 "

## Auth_PG_grp_whereclause

Syntax: Auth_PG_grp_whereclause SQL fragment
Context: directory, .htaccess
Override: AuthConfig
Status: Extension

This option allows you to exercise greater control over the SQL code used to retrieve the group name and corresponding user from the database. You can use this to search for the group name using more attributes in the table than the gid_field.

The basic SQL statement used to retrieve a group name and user name for checking looks like this:

select <uid_field> from <grp_table> where <gid_field> ='<required group> '

The gid_whereclause will be added to the end of this statement and must fit logically. The where clause must be double quoted.

## Auth_PG_cache_passwords

Syntax: Auth_PG_cache_passwords on | off
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Enable password caching, default is off.
The cache table is local to the directory, will be cleaned when full, default size is 50.

## Auth_PG_log_table

Syntax: Auth_PG_log_table table name
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the table where logging information will go, the user need write access to this table.
Required for logging.

## Auth_PG_log_uname_field

Syntax: Auth_PG_log_uname_field fieldname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the fieldname where the username will be saved.
Required for logging.

## Auth_PG_log_date_field

Syntax: Auth_PG_log_date_field fieldname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the fieldname where the date will be saved.
Required for logging.

## Auth_PG_log_uri_field

Syntax: Auth_PG_uri_field fieldname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the fieldname where the request string will be saved.
Optional for logging.

## Auth_PG_log_addrs_field

Syntax: Auth_PG_addrs_field fieldname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the fieldname where the IP address will be saved.
Optional for logging.

## Auth_PG_log_pwd_field

Syntax: Auth_PG_pwd_field fieldname
Context: directory, .htaccess
Override: AuthConfig
Status: Extension
Specifies the fileld name where the password used to authenticate will be saved.
Note: the password will be saved in clear text
Optional for logging.

## Example

Here is an example .htaccess file you might use to enable PostgreSQL authentication:

    PostgreSQL trusted user:

    	AuthName "My PostgreSQL Authenticator"
    	AuthType basic

    	Auth_PG_host localhost
    	Auth_PG_port 5432
    	Auth_PG_user postgres
    	Auth_PG_database www
    	Auth_PG_pwd_table valid_users
    	Auth_PG_uid_field user
    	Auth_PG_pwd_field password

    	<LIMIT GET POST>
    		require valid-user
    	</LIMIT>

    PostgreSQL trusted user, select only users with access_level > 100:

    	AuthName "My PostgreSQL Authenticator"
    	AuthType basic

    	Auth_PG_host localhost
    	Auth_PG_port 5432
    	Auth_PG_user postgres
    	Auth_PG_database www
    	Auth_PG_pwd_table valid_users
    	Auth_PG_uid_field user
    	Auth_PG_pwd_field password
            Auth_PG_pwd_whereclause " and access_level > 100 "

    	<LIMIT GET POST>
    		require valid-user
    	</LIMIT>

    PostgreSQL user with password authentication, with logging:

    	AuthName "My PostgreSQL Authenticator"
    	AuthType basic

    	Auth_PG_host someserver.somenet
    	Auth_PG_port 5432
    	Auth_PG_user postgres
    	Auth_PG_pwd xxxxxxx
    	Auth_PG_database www
    	Auth_PG_pwd_table valid_users
    	Auth_PG_uid_field user
    	Auth_PG_pwd_field password
    	Auth_PG_log_table access_log
    	Auth_PG_log_uname_field login
    	Auth_PG_log_date_field date
    	Auth_PG_log_uri_field 	request
    	Auth_PG_log_addrs_field  ip_address

    	<LIMIT GET POST>
    		require valid-user
    	</LIMIT>

## Technical Notes

- If the SQL statement used to retrieve the user or group name returns more than one tuple, it is considered an error. If this is likely to happen, use Auth_PG_pwd_whereclause and Auth_PG_grp_whereclause to create a query that will only return one tuple.

## Download

- Latest release:
  http://www.giuseppetanzilli.it /mod_auth_pgsql/dist/
- Homepage and Documentation:
  http://www.giuseppetanzilli.it/mod_auth_pgsql

## Compilation & Installation Notes

From the INSTALL file:

    APACI STATIC Install
    	- untar apache source and run ./configure from the apache source dir
    		cd /usr/local/src
    		tar zxf apache_1.3.X.tar.gz
    		cd apache_1.3.X
    		./configure

    	- untar mod_auth_pgsql  and configure from the module dir:
    		cd /usr/local/src
    		tar zxf mod_auth_pgsql-X.Y.Z.tar.gz
    		cd mod_auth_pgsql-X.Y.Z
    		./configure --with-apache=/usr/local/src/apache_1.3.X --with-pgsql=/usr/local/pgsql

    		   NOTE: 	--with-apache point to apache source tree
    		   			--with-pgsql specify where pgsql was installed
    	- make & install
    		make
    		make install

    	- run the apache configure with  "--activate-module=src/modules/auth_pgsql/mod_auth_pgsql.c"
    		cd /usr/local/src/apache_1.3.X
    		./configure --activate-module=src/modules/auth_pgsql/mod_auth_pgsql.c
    	- run the apache make & install
    		make
    		make install

    APXS DSO Install:
    	- untar apache source and run ./configure and install
    		cd /usr/local/src
    		tar zxf apache_1.3.X.tar.gz
    		cd apache_1.3.X
    		./configure -enable-module=so
    		make
    		make install

    	- untar mod_auth_pgsql  and configure from the module dir:
    		cd /usr/local/src
    		tar zxf mod_auth_pgsql-X.Y.Z.tar.gz
    		cd mod_auth_pgsql-X.Y.Z
    		./configure --with-apxs=/usr/local/apache/bin/apxs --with-pgsql=/usr/local/pgsql
    			NOTE: --with-apxs is the apache installation tree (not source)

    	- edit /usr/local/apache/bin/apxs and change these two rows:
    		FROM:
    			my $CFG_LD_SHLIB      = q();          # substituted via Makefile.tmpl
    			my $CFG_LDFLAGS_SHLIB = q(); # substituted via Makefile.tmpl
    		TO:
    			my $CFG_LD_SHLIB      = q(ld);          # substituted via Makefile.tmpl
    			my $CFG_LDFLAGS_SHLIB = q(-G); # substituted via Makefile.tmpl

    	- compile the module
    		/usr/local/apache/bin/apxs \
            	-I/usr/local/pgsql/include \
            	-L/usr/local/pgsql/lib -lpq \
            	-o mod_auth_pgsql.so -c mod_auth_pgsql.c auth_pgsql_shared_stub.c
    	- install the module
    		/usr/local/apache/bin/apxs -i -a -n auth_pgsql mod_auth_pgsql.so

##
Changelog

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;
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
- now compile with Apache 1.3.3 & PostgreSQL 6.4
- now configuration tag in apache for where clause need double quote
- use the APACI configuration script
- APACI-Configuration with 1.3.4, additional-Server-Info-String
0.7.2 25 June 1999
- now compile with Apache 1.3.6 & PostgreSQL 6.5
0.8.0 8 August 1999
- now apache 1.3.1 and PostgreSQL 6.5 are needed
Added options to access authenticated database
Added options to log the authentication requests
0.9.0 1999-10-03 (Tollef Fog Heen <tollef@add.no>)
- tested on apache 1.3.9 and PostgreSQL 6.5.3
- added autoconf-script
- shared module support
0.9.1 2000-03-31 (Tollef Fog Heen <tollef@add.no>)
- fixed some minor bugs related to autoconf (we don't need expat)
0.9.2 2000-05-21 (Hayato UENOHARA <uenohara@ueda.info.waseda.ac.jp>)
- Original by Matthias Eckermann <eckerman@lrz.uni-muenchen.de>
- I only copied it from a lost branch version 0.7.1 (gone for some reason)
- So all credit goes to Matthias :-)
- added 'AUTH_PGSQL_VERSION' & 'pg_auth_init_handler'
0.9.5 2000-06-04 (Voloda <vladimir.kloz@dtg.cz>)
- added options Auth_PGuser, Auth_PGpwd
0.9.6 2001-08-27 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- ported configure & install to Apache 1.3.20 and PostgreSQL 7.1.x
- replaced sprintf with snprintf thanks to Erik Rossen <rossen@freesurf.ch>
- changed some directive names to make all them like Auth_PG_*
UPDATE YOUR CONFIG FILES
- Added Logging feature from 0.8 source
- Check input string from user, to deny sql string attack, see report:
http://cert.uni-stuttgart.de/advisories/apache_auth.php
- Updated html documentation
0.9.7 2001-09-02 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- fix Auth_PG_pwd_whereclause & Auth_PG_grp_whereclause
0.9.8 2001-09-03 ( Andrei Nigmatulin <anight@mail.ru>)
- Added Auth_PG_hash_type, now we support crypt and md5 password hash method
0.9.9 2001-09-25 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- fix security problem
- added password caching, default is off, please check it and report by mail
it will be useful for hi load sites
0.9.10 2001-10-11 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- fix MD5 auth failures in some cases
0.9.11 2001-11-20 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- security fix: string buffers Sebastian Krahmer <krahmer@suse.de> and Andreas Hasenack <andreas@conectiva.com.br>
- added Auth_PG_lowercase_uid option (Bernard Quatermass <bernard@quatermass.co.uk>)
- added Auth_PG_uppercase_uid option
- added --with-pgsql-lib=<dir> and --with-pgsql-include=<dir> to configure, to make happy debian users
0.9.12 2002-01-11 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
- small fix for Auth_PG_uppercase_uid/Auth_PG_lowercase_uid
- added Auth_PG_pwd_ignore_case option