home *** CD-ROM | disk | FTP | other *** search
-
- /* ====================================================================
- * Copyright (c) 1995 The Apache Group. 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 source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions 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.
- *
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * 4. The names "Apache Server" and "Apache Group" must not be used to
- * endorse or promote products derived from this software without
- * prior written permission.
- *
- * 5. Redistributions of any form whatsoever must retain the following
- * acknowledgment:
- * "This product includes software developed by the Apache Group
- * for use in the Apache HTTP server project (http://www.apache.org/)."
- *
- * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 THE APACHE GROUP OR
- * IT'S CONTRIBUTORS 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 consists of voluntary contributions made by many
- * individuals on behalf of the Apache Group and was originally based
- * on public domain software written at the National Center for
- * Supercomputing Applications, University of Illinois, Urbana-Champaign.
- * For more information on the Apache Group and the Apache HTTP server
- * project, please see <http://www.apache.org/>.
- *
- */
-
-
- /*
- *
- * Postgres95 authentication
- *
- * Version 0.3 (March 1996)
- *
- * Adapted from mod_auth_msql.c version 0.5 (Feb 1996)
- *
- * mod_auth_msql.c was written by Dirk.vanGulik@jrc.it;
- * http://ewse.ceo.org; http://me-www.jrc.it/~dirkx
- *
- *
- * Needs libpq-fe.h and libpq.a
- *
- * Outline:
- *
- * One database, and one (or two) tables.
- * one table holds the username (preferably as
- * a primary key) 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>
- * skip password comparison if passwd field is
- * empty.
- *
- * 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://epoch.cs.berkeley.edu:8000/postgres95/
- *
- *
- * Version 0.0 (Feb 1996) First release (adaptation from mod_auth_msql.c v0.5)
- * 0.1 (Mar 1996) Correct PGgid_field command
- * 0.2 (Mar 1996) Added Auth_PGpwd_whereclause and Auth_PGgrp_whereclause
- *
- */
-
- #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
-
- 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;
-
- char *auth_pg_pwd_whereclause;
- char *auth_pg_grp_whereclause;
-
- } pg_auth_config_rec;
-
- void *create_pg_auth_dir_config (pool *p, char *d)
- {
- return pcalloc (p, sizeof(pg_auth_config_rec));
-
- }
-
- char *set_passwd_flag (cmd_parms *cmd, pg_auth_config_rec *sec, int arg) {
- sec->auth_pg_nopasswd=arg;
- return NULL;
- }
-
- char *pg_set_string_slot (cmd_parms *cmd, char *struct_ptr, char *arg)
- {
-
- int offset = (int)cmd->info;
- *(char **)(struct_ptr + offset) = pstrdup (cmd->pool, arg);
- /* do we want to check anything ? */
- return NULL;
- }
-
- command_rec pg_auth_cmds[] = {
- { "Auth_PGhost", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_host),
- OR_AUTHCFG, TAKE1, "The Host must be set to something" },
-
- { "Auth_PGdatabase", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_database),
- OR_AUTHCFG, TAKE1, "The Database field must be set to something. " },
-
- { "Auth_PGport", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_port),
- OR_AUTHCFG, TAKE1, "You must specify the postmaster port. " },
-
- { "Auth_PGoptions", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_options),
- OR_AUTHCFG, TAKE1, "You must give an option string. " },
-
- { "Auth_PGpwd_table", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_table),
- OR_AUTHCFG, TAKE1, "You must give a password table name" },
-
- { "Auth_PGgrp_table", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_table),
- OR_AUTHCFG, TAKE1, "If you want to use groups, you must give a group table name" },
-
- { "Auth_PGpwd_field", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_field),
- OR_AUTHCFG, TAKE1, "The Password-field name must be set to something" },
-
- { "Auth_PGuid_field", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_uname_field),
- OR_AUTHCFG, TAKE1, "The UserID field name must be set to something" },
-
- { "Auth_PGgid_field", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_field),
- OR_AUTHCFG, TAKE1,
- "GID field name must be set to something if you want to use groups" },
-
- { "Auth_PG_nopasswd", set_passwd_flag, NULL, OR_AUTHCFG, FLAG,
- "Limited to 'on' or 'off'" },
-
- { "Auth_PGgrp_whereclause", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_grp_whereclause),
- OR_AUTHCFG, RAW_ARGS,
- "You must give an SQL fragement that can be attached to the end of a where clause" },
-
- { "Auth_PGpwd_whereclause", pg_set_string_slot,
- (void*)XtOffsetOf(pg_auth_config_rec, auth_pg_pwd_whereclause),
- OR_AUTHCFG, RAW_ARGS,
- "You must give an SQL fragement that can be attached to the end of a where clause" },
-
- { NULL }
- };
-
- module pg95_auth_module;
-
- char pg_errstr[MAX_STRING_LEN];
- /* global errno to be able to handle config/sql
- * failures separately
- */
-
- /* get the password for uname=user, and copy it
- * into r. Assume that user is a string and stored
- * as such in the pg95 database
- */
- 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, "%s", PQerrorMessage(pg_conn));
- return NULL;
- }
-
- pg_result = PQexec(pg_conn, query);
-
- if (pg_result == NULL)
- {
- sprintf(pg_errstr, "%s", PQerrorMessage(pg_conn));
- 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, "%s", PQerrorMessage(pg_conn));
- 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, "%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];
-
- 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);
-
- return do_pg_query(r,query,sec);
- }
-
- 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", 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,
- &pg95_auth_module);
- conn_rec *c = r->connection;
- char *sent_pw, *real_pw, *colon_pw;
- int res;
-
- pg_errstr[0]='\0';
-
- if ((res = get_basic_auth_pw (r, &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 (dbm) guy in line.
- */
- if (
- (!sec->auth_pg_pwd_table) &&
- (!sec->auth_pg_pwd_field)
- ) return DECLINED;
-
- if(!(real_pw = get_pg_pw(r, c->user, sec ))) {
- if ( pg_errstr[0] ) {
- res = SERVER_ERROR;
- } else {
- sprintf(pg_errstr,"PG: Password for user %s not found", c->user);
- note_basic_auth_failure (r);
- res = AUTH_REQUIRED;
- };
- 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;
- };
-
- /* anyone know where the prototype for crypt is? */
- if(strcmp(real_pw,(char *)crypt(sent_pw,real_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,
- &pg95_auth_module);
- char *user = r->connection->user;
- int m = r->method_number;
-
-
- array_header *reqs_arr = requires (r);
- require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL;
-
- register int x,res;
- char *t, *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) 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 (!strcmp(w,"group")) {
- /* look up the membership for each of the groups in the table */
- while(t[0]) {
- if (get_pg_grp(r,getword(r->pool, &t, ' '),user,sec)) {
- return OK;
- };
- };
- if (pg_errstr[0]) {
- res = SERVER_ERROR;
- } else {
- sprintf(pg_errstr,"user %s not in right groups (%s) ",user,w);
- note_basic_auth_failure(r);
- res = AUTH_REQUIRED;
- };
- log_reason (pg_errstr, r->filename, r);
- return res;
- }
- }
-
- return DECLINED;
- }
-
-
- module pg95_auth_module = {
- STANDARD_MODULE_STUFF,
- NULL, /* 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 */
- };
-