mod_auth_pgsql-2.0.3/0000755000000000000000000000000010357242272013224 5ustar rootrootmod_auth_pgsql-2.0.3/mod_auth_pgsql.html0000644000000000000000000007304410357242200017117 0ustar rootroot Module mod_auth_pgsql PostgreSQL authentication module fro Apache web server

Apache2 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.
	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.
	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 2.0.3 (2006 01 05) of mod_auth_pgsql require Apache >= 2.0.40 and PostreSQL >=  7.x 
If you need to authenticate with Apache 1.3.x please use the old version (0.9.12), this one will not work.


Home Page http://www.giuseppetanzilli.it/mod_auth_pgsql2/

Old module for Apache 1.3 :  http://www.giuseppetanzilli.it/mod_auth_pgsql/


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

Directives


Auth_PG_host

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

Specifies the host on which the postmaster is running. It is optional, if not specified unix socket will be used.
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.
Please remember to use field of type varchar, not char for the password.

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 or BASE64
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:

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:

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





Download 





Compilation & Installation Notes

From the INSTALL file:
PXS DSO Install:
- untar mod_auth_pgsql
cd /usr/local/src
tar zxf mod_auth_pgsql-2.0.x.tar.gz
cd mod_auth_pgsql-2.0.x
- make & install as DSO

/usr/local/apache2/bin/apxs -i -a -c -I /usr/local/pgsql/include -L /usr/local/pgsql/lib -lpq mod_auth_pgsql.c



STATIC Install


- untar mod_auth_pgsql
cd /usr/local/src
tar zxf mod_auth_pgsql-2.0.x.tar.gz

- untar apache source and run ./configure from the apache source dir
cd /usr/local/src
tar zxf httpd-2.0.x.tar.gz

cd httpd-2.0.x
./configure --with-module=aaa:../mod_auth_pgsql-2.0.x/mod_auth_pgsql.c

- edit config_vars.mk
vi build/config_vars.mk
add "-I/usr/local/pgsql/include" to the end of EXTRA_INCLUDES
add "-lpq -L/usr/local/pgsql/lib" to the end of EXTRA_LIBS

NOTE: they have to be the last in the line, otherwise we get conflicts between pgsql & apache includes
NOTE2: there is a way to do it in a better way (without vi ) ?

- make & install
make
make install





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
    2.0.0       2003-01-30 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
                   - ported to Apache 2.0
                   - fixes from Gary Benson <gbenson@redhat.com>
                   - fixes from Guenter  Knauf <info@gknw.de>
                   - added debug feature, compile time option, do not use it on production systems
                   - added Auth_PG_grp_user_field
                   - renamed Auth_PG_gid_field to Auth_PG_grp_group_field
                   - added BASE64 support, thanks to Norikatsu Shigemura <n-shigemura@ensure-tech.co.jp>
                   - now we reuse database connection,  2x speedup !!  
     2.0.1       2003-02-21 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
                   - fix bug introduced with connection reuse
    2.0.3       2006-01-05 (Giuseppe Tanzilli <info@giuseppetanzilli.it>)
                    - Security fix from iDefense Security Advisory [IDEF1245]
                   - many bug fix





mod_auth_pgsql-2.0.3/Makefile0000644000000000000000000000044610343515242014663 0ustar rootrootAPACHE2_HOME=/usr/local/apache2 PGSQL_LIB=/usr/local/pgsql/lib PGSQL_INCLUDE=/usr/local/pgsql/include shared: ${APACHE2_HOME}/bin/apxs -i -a -c -I ${PGSQL_INCLUDE} -L ${PGSQL_LIB} -lpq mod_auth_pgsql.c indent: indent -kr -ts4 mod_auth_pgsql.c clean: rm -rf .libs/ *.la *.o *.lo *.slo *~ mod_auth_pgsql-2.0.3/TODO0000644000000000000000000000044110343515242013706 0ustar rootrootTo be done: - test - add a group conf/require group example in the doc - investigate why the module will be reconfigured (per directory) at every request but only if we configure by .htaccess instead of Directory in httpd.cof - password caching with shared cache (?) - new documentation mod_auth_pgsql-2.0.3/INSTALL0000644000000000000000000000225110343515242014250 0ustar rootroot From mod_auth_pgsql version 2.0, apache >= 2.0.40 and PostgreSQL >= 7.1 are needed for apache 1.3.x use the mod_auth_pgsql 0.9.x tree APXS DSO Install: - untar mod_auth_pgsql cd /usr/local/src tar zxf mod_auth_pgsql-2.0.x.tar.gz cd mod_auth_pgsql-2.0.x - make & install as DSO /usr/local/apache2/bin/apxs -i -a -c -I /usr/local/pgsql/include -L /usr/local/pgsql/lib -lpq mod_auth_pgsql.c STATIC Install - untar mod_auth_pgsql cd /usr/local/src tar zxf mod_auth_pgsql-2.0.x.tar.gz - untar apache source and run ./configure from the apache source dir cd /usr/local/src tar zxf httpd-2.0.x.tar.gz cd httpd-2.0.x ./configure --with-module=aaa:../mod_auth_pgsql-2.0.x/mod_auth_pgsql.c - edit config_vars.mk vi build/config_vars.mk add "-I/usr/local/pgsql/include" to the end of EXTRA_INCLUDES add "-lpq -L/usr/local/pgsql/lib" to the end of EXTRA_LIBS NOTE: they have to be the last in the line, otherwise we get conflicts between pgsql & apache includes NOTE2: there is a way to do it in a better way (without vi ) ? - make & install make make install bye, Giuseppe Tanzilli mod_auth_pgsql-2.0.3/README0000644000000000000000000000124510343515242014101 0ustar rootroot For Install instructions read INSTALL file For Configuration information see the html documentation Changelog, see html file mod_auth_pgsql.html Please report bugs with platform and apache/postgresql release information HomePage http://www.giuseppetanzilli.it/mod_auth_pgsql2/ Old module for apache 1.3 HomePage http://www.giuseppetanzilli.it/mod_auth_pgsql/ Maintainer: Giuseppe Tanzilli info@giuseppetanzilli.it g.tanzilli@gruppocsf.com Original source: Adam Sussman Feb, 1996 Matthias Eckermann mod_auth_pgsql-2.0.3/mod_auth_pgsql.c0000644000000000000000000010254610357224761016411 0ustar rootroot/* ===================================================================== * 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 * * * Needs libpq-fe.h and libpq.a * * Outline: * * - 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. * * Module Directives: See html documentation * * Changelog: See html documentation * * see http://www.postgreSQL.org/ * * * * Homepage http://www.giuseppetanzilli.it/mod_auth_pgsql/ * * Latest sources http://www.giuseppetanzilli.it/mod_auth_pgsql/dist/ * * Maintainer: * Giuseppe Tanzilli * info@giuseppetanzilli.it * g.tanzilli@gruppocsf.com * * Original source: * Adam Sussman (asussman@vidya.com) Feb, 1996 * Matthias Eckermann * eckerman@lrz.uni-muenchen.de * */ #define AUTH_PGSQL_VERSION "2.0.3" #include "apr_hooks.h" #include "apr.h" #include "apr_lib.h" #include "apr_strings.h" #include "apr_buckets.h" #include "apr_md5.h" #include "apr_network_io.h" #include "apr_pools.h" #include "apr_uri.h" #include "apr_date.h" #include "apr_fnmatch.h" #define APR_WANT_STRFUNC #include "apr_want.h" #include "httpd.h" #include "http_config.h" #include "http_core.h" #include "http_log.h" #include "http_main.h" #include "http_protocol.h" #include "http_request.h" #include "util_script.h" #ifdef WIN32 #define crypt apr_password_validate #else #include #endif #include "libpq-fe.h" #if (MODULE_MAGIC_NUMBER_MAJOR < 20020628) #error Apache 2.0.40 required #endif /* ** uncomment the following line if you're having problem. The debug messages ** will be written to the server error log. ** WARNING: we log sensitive data, do not use on production systems */ /* #define DEBUG_AUTH_PGSQL 1 */ #ifndef MAX_STRING_LEN #define MAX_STRING_LEN 8192 #endif /* Cache table size */ #ifndef MAX_TABLE_LEN #define MAX_TABLE_LEN 50 #endif #define AUTH_PG_HASH_TYPE_CRYPT 1 #define AUTH_PG_HASH_TYPE_MD5 2 #define AUTH_PG_HASH_TYPE_BASE64 3 typedef struct { const char *dir; const char *auth_pg_host; const char *auth_pg_database; const char *auth_pg_port; const char *auth_pg_options; const char *auth_pg_user; const char *auth_pg_pwd; const char *auth_pg_pwd_table; const char *auth_pg_uname_field; const char *auth_pg_pwd_field; const char *auth_pg_grp_table; const char *auth_pg_grp_group_field; const char *auth_pg_grp_user_field; const char *auth_pg_pwd_whereclause; const char *auth_pg_grp_whereclause; int auth_pg_nopasswd; int auth_pg_authoritative; int auth_pg_lowercaseuid; int auth_pg_uppercaseuid; int auth_pg_pwdignorecase; int auth_pg_encrypted; int auth_pg_hash_type; int auth_pg_cache_passwords; const char *auth_pg_log_table; const char *auth_pg_log_addrs_field; const char *auth_pg_log_uname_field; const char *auth_pg_log_pwd_field; const char *auth_pg_log_date_field; const char *auth_pg_log_uri_field; apr_table_t *cache_pass_table; } pg_auth_config_rec; static apr_pool_t *auth_pgsql_pool = NULL; static apr_pool_t *auth_pgsql_pool_base64 = NULL; module AP_MODULE_DECLARE_DATA auth_pgsql_module; static int pg_log_auth_user(request_rec * r, pg_auth_config_rec * sec, char *user, char *sent_pw); static char *do_pg_query(request_rec * r, char *query, pg_auth_config_rec * sec); static void *create_pg_auth_dir_config(apr_pool_t * p, char *d) { pg_auth_config_rec *new_rec; #ifdef DEBUG_AUTH_PGSQL ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "[mod_auth_pgsql.c] - going to configure directory \"%s\" ", d); #endif /* DEBUG_AUTH_PGSQL */ new_rec = apr_pcalloc(p, sizeof(pg_auth_config_rec)); if (new_rec == NULL) return NULL; if (auth_pgsql_pool == NULL) apr_pool_create_ex(&auth_pgsql_pool, NULL, NULL, NULL); if (auth_pgsql_pool == NULL) return NULL; /* sane defaults */ if (d != NULL) new_rec->dir = apr_pstrdup(p, d); else new_rec->dir = NULL; new_rec->auth_pg_host = NULL; new_rec->auth_pg_database = NULL; new_rec->auth_pg_port = NULL; new_rec->auth_pg_options = NULL; new_rec->auth_pg_user = NULL; new_rec->auth_pg_pwd = NULL; new_rec->auth_pg_pwd_table = NULL; new_rec->auth_pg_uname_field = NULL; new_rec->auth_pg_pwd_field = NULL; new_rec->auth_pg_grp_table = NULL; new_rec->auth_pg_grp_user_field = NULL; new_rec->auth_pg_grp_group_field = NULL; new_rec->auth_pg_pwd_whereclause = NULL; new_rec->auth_pg_grp_whereclause = NULL; new_rec->auth_pg_nopasswd = 0; new_rec->auth_pg_authoritative = 1; new_rec->auth_pg_lowercaseuid = 0; new_rec->auth_pg_uppercaseuid = 0; new_rec->auth_pg_pwdignorecase = 0; new_rec->auth_pg_encrypted = 1; new_rec->auth_pg_hash_type = AUTH_PG_HASH_TYPE_CRYPT; new_rec->auth_pg_cache_passwords = 0; new_rec->auth_pg_log_table = NULL; new_rec->auth_pg_log_addrs_field = NULL; new_rec->auth_pg_log_uname_field = NULL; new_rec->auth_pg_log_pwd_field = NULL; new_rec->auth_pg_log_date_field = NULL; new_rec->auth_pg_log_uri_field = NULL; /* make a per directory cache table */ new_rec->cache_pass_table = apr_table_make(auth_pgsql_pool, MAX_TABLE_LEN); if (new_rec->cache_pass_table == NULL) return NULL; #ifdef DEBUG_AUTH_PGSQL ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "[mod_auth_pgsql.c] - configured directory \"%s\" ", d); #endif /* DEBUG_AUTH_PGSQL */ return new_rec; } static const char *pg_set_hash_type(cmd_parms * cmd, void *mconfig, const char *hash_type) { pg_auth_config_rec *sec = mconfig; if (!strcasecmp(hash_type, "MD5")) sec->auth_pg_hash_type = AUTH_PG_HASH_TYPE_MD5; else if (!strcasecmp(hash_type, "CRYPT")) sec->auth_pg_hash_type = AUTH_PG_HASH_TYPE_CRYPT; else if (!strcasecmp(hash_type, "BASE64")) sec->auth_pg_hash_type = AUTH_PG_HASH_TYPE_BASE64; else return apr_pstrcat(cmd->pool, "Invalid hash type for Auth_PG_hash_type: ", hash_type, NULL); return NULL; } static const char *pg_set_authoritative_flag(cmd_parms * cmd, pg_auth_config_rec * sec, const int arg) { sec->auth_pg_authoritative = arg; return NULL; } static const char *pg_set_lowercaseuid_flag(cmd_parms * cmd, void *sec, const int arg) { ((pg_auth_config_rec *) sec)->auth_pg_lowercaseuid = arg; ((pg_auth_config_rec *) sec)->auth_pg_uppercaseuid = 0; return NULL; } static const char *pg_set_uppercaseuid_flag(cmd_parms * cmd, void *sec, int arg) { ((pg_auth_config_rec *) sec)->auth_pg_uppercaseuid = arg; ((pg_auth_config_rec *) sec)->auth_pg_lowercaseuid = 0; return NULL; } static const command_rec pg_auth_cmds[] = { AP_INIT_TAKE1("Auth_PG_host", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_host), OR_AUTHCFG, "the host name of the postgreSQL server."), AP_INIT_TAKE1("Auth_PG_database", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_database), OR_AUTHCFG, "the name of the database that contains authorization information."), AP_INIT_TAKE1("Auth_PG_port", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_port), OR_AUTHCFG, "the port the postmaster is running on."), AP_INIT_TAKE1("Auth_PG_options", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_options), OR_AUTHCFG, "an options string to be sent to the postgres backed process."), AP_INIT_TAKE1("Auth_PG_user", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_user), OR_AUTHCFG, "user name connect as"), AP_INIT_TAKE1("Auth_PG_pwd", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_pwd), OR_AUTHCFG, "password to connect"), AP_INIT_TAKE1("Auth_PG_pwd_table", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_pwd_table), OR_AUTHCFG, "the name of the table containing username/password tuples."), AP_INIT_TAKE1("Auth_PG_pwd_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_pwd_field), OR_AUTHCFG, "the name of the password field."), AP_INIT_TAKE1("Auth_PG_uid_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_uname_field), OR_AUTHCFG, "the name of the user-id field."), AP_INIT_TAKE1("Auth_PG_grp_table", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_grp_table), OR_AUTHCFG, "the name of the table containing username/group tuples."), AP_INIT_TAKE1("Auth_PG_grp_group_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_grp_group_field), OR_AUTHCFG, "the name of the group-name field."), AP_INIT_TAKE1("Auth_PG_grp_user_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_grp_user_field), OR_AUTHCFG, "the name of the group-name field."), AP_INIT_FLAG("Auth_PG_nopasswd", ap_set_flag_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_nopasswd), OR_AUTHCFG, "'on' or 'off'"), AP_INIT_FLAG("Auth_PG_encrypted", ap_set_flag_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_encrypted), OR_AUTHCFG, "'on' or 'off'"), AP_INIT_TAKE1("Auth_PG_hash_type", pg_set_hash_type, NULL, OR_AUTHCFG, "password hash type (CRYPT|MD5|BASE64)."), AP_INIT_FLAG("Auth_PG_cache_passwords", ap_set_flag_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_cache_passwords), OR_AUTHCFG, "'on' or 'off'"), AP_INIT_FLAG("Auth_PG_authoritative", ap_set_flag_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_authoritative), OR_AUTHCFG, "'on' or 'off'"), AP_INIT_FLAG("Auth_PG_lowercase_uid", pg_set_lowercaseuid_flag, NULL, OR_AUTHCFG, "'on' or 'off'"), AP_INIT_FLAG("Auth_PG_uppercase_uid", pg_set_uppercaseuid_flag, NULL, OR_AUTHCFG, "'on' or 'off'"), AP_INIT_FLAG("Auth_PG_pwd_ignore_case", ap_set_flag_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_pwdignorecase), OR_AUTHCFG, "'on' or 'off'"), AP_INIT_TAKE1("Auth_PG_grp_whereclause", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_grp_whereclause), OR_AUTHCFG, "an SQL fragement that can be attached to the end of a where clause."), AP_INIT_TAKE1("Auth_PG_pwd_whereclause", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_pwd_whereclause), OR_AUTHCFG, "an SQL fragement that can be attached to the end of a where clause."), AP_INIT_TAKE1("Auth_PG_log_table", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_table), OR_AUTHCFG, "the name of the table containing log tuples."), AP_INIT_TAKE1("Auth_PG_log_addrs_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_addrs_field), OR_AUTHCFG, "the name of the field containing addrs in the log table (type char)."), AP_INIT_TAKE1("Auth_PG_log_uname_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_uname_field), OR_AUTHCFG, "the name of the field containing username in the log table (type char)."), AP_INIT_TAKE1("Auth_PG_log_pwd_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_pwd_field), OR_AUTHCFG, "the name of the field containing password in the log table (type char)."), AP_INIT_TAKE1("Auth_PG_log_date_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_date_field), OR_AUTHCFG, "the name of the field containing date in the log table (type char)."), AP_INIT_TAKE1("Auth_PG_log_uri_field", ap_set_string_slot, (void *) APR_OFFSETOF(pg_auth_config_rec, auth_pg_log_uri_field), OR_AUTHCFG, "the name of the field containing uri (Object fetched) in the log table (type char)."), {NULL} }; static char pg_errstr[MAX_STRING_LEN]; /* global errno to be able to handle config/sql * failures separately */ static char *auth_pg_md5(char *pw) { apr_md5_ctx_t ctx; unsigned char digest[APR_MD5_DIGESTSIZE]; static unsigned char md5hash[APR_MD5_DIGESTSIZE * 2 + 1]; int i; apr_md5(digest, (const unsigned char *) pw, strlen(pw)); for (i = 0; i < APR_MD5_DIGESTSIZE; i++) apr_snprintf((char *) &md5hash[i + i], 3, "%02x", digest[i]); md5hash[APR_MD5_DIGESTSIZE * 2] = '\0'; return (char *) md5hash; } static char *auth_pg_base64(char *pw) { if (auth_pgsql_pool_base64 == NULL) apr_pool_create_ex(&auth_pgsql_pool_base64, NULL, NULL, NULL); if (auth_pgsql_pool == NULL) return NULL; return ap_pbase64encode(auth_pgsql_pool, pw); } /* Got from POstgreSQL 7.2 */ /* --------------- * Escaping arbitrary strings to get valid SQL strings/identifiers. * * Replaces "\\" with "\\\\" and "'" with "''". * length is the length of the buffer pointed to by * from. The buffer at to must be at least 2*length + 1 characters * long. A terminating NUL character is written. * --------------- */ static size_t pg_check_string(char *to, const char *from, size_t length) { const char *source = from; char *target = to; unsigned int remaining = length; while (remaining > 0) { switch (*source) { case '\\': *target = '\\'; target++; *target = '\\'; /* target and remaining are updated below. */ break; case '\'': *target = '\''; target++; *target = '\''; /* target and remaining are updated below. */ break; default: *target = *source; /* target and remaining are updated below. */ } source++; target++; remaining--; } /* Write the terminating NUL character. */ *target = '\0'; return target - to; } /* 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) { PGresult *pg_result; PGconn *pg_conn; char *val; char *result = NULL; pg_errstr[0] = '\0'; #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - do_pg_query - going to connect database \"%s\" ", sec->auth_pg_database); #endif /* DEBUG_AUTH_PGSQL */ pg_conn = PQsetdbLogin(sec->auth_pg_host, sec->auth_pg_port, sec->auth_pg_options, NULL, sec->auth_pg_database, sec->auth_pg_user, sec->auth_pg_pwd); if (PQstatus(pg_conn) != CONNECTION_OK) { PQreset(pg_conn); apr_snprintf(pg_errstr, MAX_STRING_LEN, "mod_auth_pgsql database connection error resetting %s", PQerrorMessage(pg_conn)); if (PQstatus(pg_conn) != CONNECTION_OK) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "mod_auth_pgsql database connection error reset failed %s", PQerrorMessage(pg_conn)); PQfinish(pg_conn); return NULL; } } #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - do_pg_query - going to execute query \"%s\" ", query); #endif /* DEBUG_AUTH_PGSQL */ pg_result = PQexec(pg_conn, query); if (pg_result == NULL) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "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) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "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) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "PGSQL 4: %s", PQerrorMessage(pg_conn)); PQclear(pg_result); PQfinish(pg_conn); return NULL; } if (!(result = (char *) apr_pcalloc(r->pool, strlen(val) + 1))) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "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 *safe_user; int n; safe_user = apr_palloc(r->pool, 1 + 2 * strlen(user)); pg_check_string(safe_user, user, strlen(user)); #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - get_pg_pw - going to retrieve password for user \"%s\" from database", user); #endif /* DEBUG_AUTH_PGSQL */ if ((!sec->auth_pg_pwd_table) || (!sec->auth_pg_pwd_field) || (!sec->auth_pg_uname_field)) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "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; }; if (sec->auth_pg_lowercaseuid) { /* and force it to lowercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (isupper(safe_user[n])) { safe_user[n] = tolower(safe_user[n]); } n++; } } if (sec->auth_pg_uppercaseuid) { /* and force it to uppercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (islower(safe_user[n])) { safe_user[n] = toupper(safe_user[n]); } n++; } } n = apr_snprintf(query, MAX_STRING_LEN, "select %s from %s where %s='%s' %s", sec->auth_pg_pwd_field, sec->auth_pg_pwd_table, sec->auth_pg_uname_field, safe_user, sec->auth_pg_pwd_whereclause ? sec-> auth_pg_pwd_whereclause : ""); if (n < 0 || n > MAX_STRING_LEN) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "PG: Detected SQL-truncation attack. Auth aborted."); return NULL; } return do_pg_query(r, query, sec); } static char *get_pg_grp(request_rec * r, char *group, char *user, pg_auth_config_rec * sec) { char query[MAX_STRING_LEN]; char *safe_user; char *safe_group; int n; safe_user = apr_palloc(r->pool, 1 + 2 * strlen(user)); safe_group = apr_palloc(r->pool, 1 + 2 * strlen(group)); #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - get_pg_grp - going to retrieve group for user \"%s\" from database", user); #endif /* DEBUG_AUTH_PGSQL */ query[0] = '\0'; pg_check_string(safe_user, user, strlen(user)); pg_check_string(safe_group, group, strlen(group)); if ((!sec->auth_pg_grp_table) || (!sec->auth_pg_grp_group_field) || (!sec->auth_pg_grp_user_field)) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "PG: Missing parameters for password lookup: %s%s%s", (sec->auth_pg_grp_table ? "" : "Group table name"), (sec-> auth_pg_grp_group_field ? "" : "GroupID field name "), (sec-> auth_pg_grp_user_field ? "" : "Group table user field name ")); return NULL; }; if (sec->auth_pg_lowercaseuid) { /* and force it to lowercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (isupper(safe_user[n])) { safe_user[n] = tolower(safe_user[n]); } n++; } } if (sec->auth_pg_uppercaseuid) { /* and force it to uppercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (islower(safe_user[n])) { safe_user[n] = toupper(safe_user[n]); } n++; } } n = apr_snprintf(query, MAX_STRING_LEN, "select %s from %s where %s='%s' and %s='%s' %s", sec->auth_pg_grp_group_field, sec->auth_pg_grp_table, sec->auth_pg_grp_user_field, safe_user, sec->auth_pg_grp_group_field, safe_group, sec->auth_pg_grp_whereclause ? sec-> auth_pg_grp_whereclause : ""); if (n < 0 || n > MAX_STRING_LEN) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "PG: Detected SQL-truncation attack. Auth aborted."); return NULL; } return do_pg_query(r, query, sec); } /* Process authentication request from Apache*/ static int pg_authenticate_basic_user(request_rec * r) { pg_auth_config_rec *sec = (pg_auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_pgsql_module); char *val = NULL; char *sent_pw, *real_pw; int res; char *user; if ((res = ap_get_basic_auth_pw(r, (const char **) &sent_pw))) return res; user = r->user; #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - pg_authenticate_basic_user - going to auth user \"%s\" pass \"%s\" uri \"%s\"", user, sent_pw, r->unparsed_uri); #endif /* DEBUG_AUTH_PGSQL */ /* 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)) { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - missing configuration parameters"); return DECLINED; } pg_errstr[0] = '\0'; if (sec->auth_pg_cache_passwords && (!apr_is_empty_table(sec->cache_pass_table))) { val = (char *) apr_table_get(sec->cache_pass_table, user); if (val) real_pw = val; else real_pw = get_pg_pw(r, user, sec); } else real_pw = get_pg_pw(r, user, sec); if (!real_pw) { if (pg_errstr[0]) { res = HTTP_INTERNAL_SERVER_ERROR; } else { if (sec->auth_pg_authoritative) { /* force error and access denied */ apr_snprintf(pg_errstr, MAX_STRING_LEN, "mod_auth_pgsql: Password for user %s not found (PG-Authoritative)", user); ap_note_basic_auth_failure(r); res = HTTP_UNAUTHORIZED; } else { /* allow fall through to another module */ return DECLINED; } } ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); 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))) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "[mod_auth_pgsql.c] - Empty password accepted for user \"%s\"", user); ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); pg_log_auth_user(r, sec, user, sent_pw); return OK; }; /* if the flag is off however, keep that kind of stuff at * an arms length. */ if ((!strlen(real_pw)) || (!strlen(sent_pw))) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "[mod_auth_pgsql.c] - Empty password rejected for user \"%s\"", user); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; }; if (sec->auth_pg_encrypted) switch (sec->auth_pg_hash_type) { case AUTH_PG_HASH_TYPE_MD5: sent_pw = auth_pg_md5(sent_pw); break; case AUTH_PG_HASH_TYPE_CRYPT: sent_pw = (char *) crypt(sent_pw, real_pw); break; case AUTH_PG_HASH_TYPE_BASE64: sent_pw = auth_pg_base64(sent_pw); break; } if ((sec->auth_pg_hash_type == AUTH_PG_HASH_TYPE_MD5 || sec->auth_pg_pwdignorecase != 0) ? strcasecmp(real_pw, sent_pw) : strcmp(real_pw, sent_pw)) if ((sec->auth_pg_hash_type == AUTH_PG_HASH_TYPE_MD5 || sec->auth_pg_hash_type == AUTH_PG_HASH_TYPE_BASE64 || sec->auth_pg_pwdignorecase) ? strcasecmp(real_pw, sent_pw) : strcmp(real_pw, sent_pw)) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "PG user %s: password mismatch", user); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; } /* store password in the cache */ if (sec->auth_pg_cache_passwords && !val && sec->cache_pass_table) { if ((apr_table_elts(sec->cache_pass_table))->nelts >= MAX_TABLE_LEN) { apr_table_clear(sec->cache_pass_table); } apr_table_set(sec->cache_pass_table, user, real_pw); } pg_log_auth_user(r, sec, user, sent_pw); return OK; } /* Checking ID */ static int pg_check_auth(request_rec * r) { pg_auth_config_rec *sec = (pg_auth_config_rec *) ap_get_module_config(r->per_dir_config, &auth_pgsql_module); char *user = r->user; int m = r->method_number; int group_result = DECLINED; apr_array_header_t *reqs_arr = (apr_array_header_t *) ap_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'; #ifdef DEBUG_AUTH_PGSQL ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[mod_auth_pgsql.c] - pg_check_auth - going to check auth for user \"%s\" ", user); #endif /* DEBUG_AUTH_PGSQL */ /* if we cannot do it; leave it to some other guy */ if ((!sec->auth_pg_grp_table) && (!sec->auth_pg_grp_group_field) && (!sec->auth_pg_grp_user_field)) return DECLINED; if (!reqs_arr) { if (sec->auth_pg_authoritative) { /* force error and access denied */ apr_snprintf(pg_errstr, MAX_STRING_LEN, "mod_auth_pgsql: user %s denied, no access rules specified (PG-Authoritative)", user); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); ap_note_basic_auth_failure(r); res = HTTP_UNAUTHORIZED; } else { return DECLINED; } } for (x = 0; x < reqs_arr->nelts; x++) { if (!(reqs[x].method_mask & (1 << m))) continue; t = reqs[x].requirement; w = ap_getword(r->pool, &t, ' '); if (!strcmp(w, "valid-user")) return OK; if (!strcmp(w, "user")) { while (t[0]) { w = ap_getword_conf(r->pool, &t); if (!strcmp(user, w)) return OK; } if (sec->auth_pg_authoritative) { /* force error and access denied */ apr_snprintf(pg_errstr, MAX_STRING_LEN, "mod_auth_pgsql: user %s denied, no access rules specified (PG-Authoritative)", user); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; } } 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, ap_getword(r->pool, &t, ' '), user, sec)) { group_result = OK; }; }; if (pg_errstr[0]) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); return HTTP_INTERNAL_SERVER_ERROR; } if (group_result == OK) return OK; if (sec->auth_pg_authoritative) { apr_snprintf(pg_errstr, MAX_STRING_LEN, "[mod_auth_pgsql.c] - user %s not in right groups (PG-Authoritative)", user); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[mod_auth_pgsql.c] - ERROR - %s", pg_errstr); ap_note_basic_auth_failure(r); return HTTP_UNAUTHORIZED; }; } } return DECLINED; } /* Send the authentication to the log table */ int pg_log_auth_user(request_rec * r, pg_auth_config_rec * sec, char *user, char *sent_pw) { char sql[MAX_STRING_LEN]; char *s; int n; char fields[MAX_STRING_LEN]; char values[MAX_STRING_LEN]; char *safe_user; char *safe_pw; char *safe_req; char ts[MAX_STRING_LEN]; /* time in string format */ apr_time_exp_t t; /* time of request start */ apr_size_t retsize; safe_user = apr_palloc(r->pool, 1 + 2 * strlen(user)); safe_pw = apr_palloc(r->pool, 1 + 2 * strlen(sent_pw)); safe_req = apr_palloc(r->pool, 1 + 2 * strlen(r->the_request)); /* we do not want to process internal redirect */ if (!ap_is_initial_req(r)) return DECLINED; if ((!sec->auth_pg_log_table) || (!sec->auth_pg_log_uname_field) || (!sec->auth_pg_log_date_field)) { // At least table name, username and date field are specified // send error message and exit return DECLINED; } /* AUD: MAX_STRING_LEN probably isn't always correct */ pg_check_string(safe_user, user, strlen(user)); pg_check_string(safe_pw, sent_pw, strlen(sent_pw)); pg_check_string(safe_req, r->the_request, strlen(r->the_request)); if (sec->auth_pg_lowercaseuid) { /* and force it to lowercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (isupper(safe_user[n])) { safe_user[n] = tolower(safe_user[n]); } n++; } } if (sec->auth_pg_uppercaseuid) { /* and force it to uppercase */ n = 0; while (safe_user[n] && n < (MAX_STRING_LEN - 1)) { if (islower(safe_user[n])) { safe_user[n] = toupper(safe_user[n]); } n++; } } /* time field format */ apr_time_exp_lt(&t, r->request_time); apr_strftime(ts, &retsize, 100, "%Y-%m-%d %H:%M:%S", &t); /* SQL Statement, required fields: Username, Date */ apr_snprintf(fields, MAX_STRING_LEN, "%s,%s", sec->auth_pg_log_uname_field, sec->auth_pg_log_date_field); apr_snprintf(values, MAX_STRING_LEN, "'%s','%s'", safe_user, ts); /* Optional parameters */ if (sec->auth_pg_log_addrs_field) { /* IP Address field */ apr_snprintf(sql, MAX_STRING_LEN, ", %s", sec->auth_pg_log_addrs_field); strncat(fields, sql, MAX_STRING_LEN - strlen(fields) - 1); apr_snprintf(sql, MAX_STRING_LEN, ", '%s'", r->connection->remote_ip); strncat(values, sql, MAX_STRING_LEN - strlen(values) - 1); } if (sec->auth_pg_log_pwd_field) { /* Password field , clear WARNING */ apr_snprintf(sql, MAX_STRING_LEN, ", %s", sec->auth_pg_log_pwd_field); strncat(fields, sql, MAX_STRING_LEN - strlen(fields) - 1); apr_snprintf(sql, MAX_STRING_LEN, ", '%s'", safe_pw); strncat(values, sql, MAX_STRING_LEN - strlen(values) - 1); } if (sec->auth_pg_log_uri_field) { /* request string */ apr_snprintf(sql, MAX_STRING_LEN, ", %s", sec->auth_pg_log_uri_field); strncat(fields, sql, MAX_STRING_LEN - strlen(fields) - 1); apr_snprintf(sql, MAX_STRING_LEN, ", '%s'", safe_req); strncat(values, sql, MAX_STRING_LEN - strlen(values) - 1); } apr_snprintf(sql, MAX_STRING_LEN, "insert into %s (%s) values(%s) ; ", sec->auth_pg_log_table, fields, values); s = do_pg_query(r, sql, sec); return (0); } static int pg_auth_init_handler(apr_pool_t * p, apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s) { #ifdef DEBUG_AUTH_PGSQL ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "[mod_auth_pgsql.c] - pg_auth_init_handler - "); #endif /* DEBUG_AUTH_PGSQL */ ap_add_version_component(p, "mod_auth_pgsql/" AUTH_PGSQL_VERSION); return OK; } /* Init the module private memory pool, used for the per directory cache tables */ static void *pg_auth_server_config(apr_pool_t * p, server_rec * s) { #ifdef DEBUG_AUTH_PGSQL ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, p, "[mod_auth_pgsql.c] - pg_auth_server_config - "); #endif /* DEBUG_AUTH_PGSQL */ if (auth_pgsql_pool == NULL) apr_pool_create_ex(&auth_pgsql_pool, NULL, NULL, NULL); return OK; } static void register_hooks(apr_pool_t * p) { ap_hook_post_config(pg_auth_init_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_auth_checker(pg_check_auth, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_check_user_id(pg_authenticate_basic_user, NULL, NULL, APR_HOOK_MIDDLE); }; module AP_MODULE_DECLARE_DATA auth_pgsql_module = { STANDARD20_MODULE_STUFF, create_pg_auth_dir_config, /* dir config creater */ NULL, /* dir merger --- default is to override */ pg_auth_server_config, /* server config */ NULL, /* merge server config */ pg_auth_cmds, /* command table */ register_hooks /* Apache2 register hooks */ };