[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

further modifications to mod_auth_pam.c



I have further modified the mod_auth_pam.c for Apache created by Ingo
Luetkebohle and modified by Michael Johnson. My modifications allow the
pam service name to be listed in the .htaccess file and also allow the
module to decline (and thus fall through to other authentication
methods) if there is no "AuthPamName" entry. The source is attached.

Here's a snippet from the comments:

 *
 * Further modified by Chris Dent <cdent@kiva.net> (cjd 19970506)
 * based on * study of mod_auth_msql to two helpful features:
 *
 *  In .htaccess or <Directory> you must set an AuthPamName that
 *  gives the service type to be used by the module in pam.conf.
 *
 *  Without the AuthPamName the module will DECLINE and fall through
 *  to other auth methods.
 *
 *  Having AuthPamName means you can have multiple authentication
 *  types by list multiple services in pam.conf (or pam.d).
 *
 *  In .htaccess or <Directory> do something similar to the following:
 *
 *  AuthPamName http
 *  AuthName Top Secret Insider Pages
 *  AuthType Basic
 *
 *  <LIMIT GET POST PUT>
 *  require valid-user
 *  </LIMIT>

If you are interested in trying it out, please do and let me know if
it works out. Also if you have any constructive comments I'd like to
hear them.

..........................
Chris Dent........SysAdmin
...........Kiva Networking
/* ====================================================================
 * 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
 * ITS 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/>.
 *
 */

/*
 * mod_auth_pam: 
 *  basic authentication against pluggable authentication module lib
 *
 *  For information about PAM, please refer to 
 *    http://parc.power.net/morgan/Linux-PAM/
 *
 * Author: Ingo Lütkebohle
 *  based upon mod_auth.c
 *
 * Modified for Red Hat Linux by Michael K. Johnson <johnsonm@redhat.com>
 *  further based upon mod_auth.c
 *  PAM does not manage group information, so I copied it in from mod_auth.c
 *  3/31/97
 *
 * usage information:
 *
 * 1. Configuration:
 *     Module pam_auth_module mod_auth_pam.o
 *     EXTRA_LIBS+= -lpam -ldl
 * 
 * 2. Add an entry for service type "http" to your PAM configuration
 * 
 * Further modified by Chris Dent <cdent@kiva.net> (cjd 19970506) based on
 * study of mod_auth_msql to two helpful features:
 *
 *  In .htaccess or <Directory> you must set an AuthPamName that
 *  gives the service type to be used by the module in pam.conf.
 * 
 *  Without the AuthPamName the module will DECLINE and fall through
 *  to other auth methods.
 * 
 *  Having AuthPamName means you can have multiple authentication
 *  types by list multiple services in pam.conf (or pam.d).
 *
 *  In .htaccess or <Directory> do something similar to the following:
 *
 *  AuthPamName http
 *  AuthName Top Secret Insider Pages
 *  AuthType Basic
 *
 *  <LIMIT GET POST PUT>
 *  require valid-user
 *  </LIMIT>
 */

#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"

#include <security/pam_appl.h>

typedef struct auth_config_struct {
    char *auth_grpfile;
    char *auth_pam_name;
} auth_config_rec;

void *pam_create_auth_dir_config (pool *p, char *d)
{
    return pcalloc (p, sizeof(auth_config_rec));
}

/* add a AuthPamName entry */
command_rec pam_auth_cmds[] = {
{ "AuthGroupFile", set_string_slot,
    (void*)XtOffsetOf(auth_config_rec,auth_grpfile), OR_AUTHCFG, TAKE1, NULL },
{ "AuthPamName", set_string_slot,
    (void*)XtOffsetOf(auth_config_rec,auth_pam_name),
     OR_AUTHCFG, TAKE1, "Name of the service in pam.conf" },
{ NULL }
};

module pam_auth_module;

/* PAM service name moved to pam_auth_cmds */
static const char *valid_user = "valid-user";

typedef struct {
  char *name, *pw;
} auth_pam_userinfo;

/* 
 * auth_pam_talker: supply authentication information to PAM when asked
 *
 * Assumptions:
 *   A password is asked for by requesting input without echoing
 *   A username is asked for by requesting input _with_ echoing
 *
 */ 
int auth_pam_talker(int num_msg, 
		    const struct pam_message **msg, 
		    struct pam_response **resp, 
		    void *appdata_ptr)
{
  unsigned short i = 0;
  auth_pam_userinfo *userinfo = (auth_pam_userinfo*)appdata_ptr;
  struct pam_response *response = 0;
  
  /* parameter sanity checking */
  if(!resp || !msg || !userinfo)
    return PAM_CONV_ERR;

  /* allocate memory to store response */
  response = malloc(num_msg * sizeof(struct pam_response));
  if(!response)
    return PAM_CONV_ERR;
  
  /* copy values */
  for(i = 0; i < num_msg; i++) {
    /* initialize to safe values */
    response[i].resp_retcode = 0;
    response[i].resp = 0;

    /* select response based on requested output style */
    switch(msg[i]->msg_style) {
    case PAM_PROMPT_ECHO_ON:
      /* on memory allocation failure, auth fails */
      response[i].resp = strdup(userinfo->name);
      break;
    case PAM_PROMPT_ECHO_OFF:
      response[i].resp = strdup(userinfo->pw);
      break;
    default:
      if(response)
	free(response);
      return PAM_CONV_ERR;
    }
  }
  /* everything okay, set PAM response values */
  *resp = response;
  return PAM_SUCCESS;
}

table *pam_groups_for_user (pool *p, char *user, char *grpfile) {
  FILE *f;
  table *grps = make_table (p, 32);
  pool *sp;
  char l[MAX_STRING_LEN];
  char *group_name, *ll, *w;

  if(!(f=pfopen(p, grpfile, "r")))
    return NULL;

  sp = make_sub_pool (p);
    
  while(!(cfg_getline(l,MAX_STRING_LEN,f))) {
    if((l[0] == '#') || (!l[0])) continue;
    ll = l;
    clear_pool (sp);
	
    group_name = getword(sp, &ll, ':');

    while(ll[0]) {
      w = getword_conf (sp, &ll);
      if(!strcmp(w,user)) {
	table_set (grps, group_name, "in");
	break;
      }
    }
  }
  pfclose(p, f);
  destroy_pool (sp);
  return grps;
}


/* 
 * These functions return 0 if client is OK, and proper error status
 * if not... either AUTH_REQUIRED, if we made a check, and it failed, or
 * SERVER_ERROR, if things are so totally confused that we couldn't
 * figure out how to tell if the client is authorized or not.
 *
 * If they return DECLINED, and all other modules also decline, that's
 * treated by the server core as a configuration error, logged and
 * reported as such.
 */

/* 
 * Determine user ID, and check if it really is that user
 */
int pam_auth_basic_user (request_rec *r)
{
  auth_config_rec *sec =
    (auth_config_rec *)get_module_config (r->per_dir_config, &pam_auth_module);
    int res = 0;
    /* mod_auth_pam specific */
    auth_pam_userinfo userinfo = { NULL, NULL };
    /* PAM specific  */
    struct pam_conv conv_info = { &auth_pam_talker, &userinfo};
    pam_handle_t *pamh  = NULL;

    /* read sent pw */
    if ((res = get_basic_auth_pw (r, &(userinfo.pw))))
      return res;

    /* decline if we don't have a pam_name 
       without it this module gets ignored (cjd 19970506) */
    if (!sec->auth_pam_name) return DECLINED;
    
    /* this is only set after get_basic_auth_pw was called */
    userinfo.name = r->connection->user;

    /* initialize pam */
    /* slip the pam servnice name in here (cjd 19970506) */
    if((res = pam_start(sec->auth_pam_name, 
			userinfo.name, 
			&conv_info, 
			&pamh)) != PAM_SUCCESS) {
      log_reason((char*)pam_strerror(res), r->uri, r);
      return DECLINED;
    }
    
    /* try to authenticate user, log error on failure */
    if((res = pam_authenticate(pamh, 0)) != PAM_SUCCESS) {
      log_reason((char*)pam_strerror(res), r->uri, r);
      note_basic_auth_failure(r);
      pam_end(pamh, PAM_SUCCESS);
      return AUTH_REQUIRED;
    }

    pam_end(pamh, PAM_SUCCESS);
    return OK;
}
    
/*
 * Look if that user (as authenticated above) is allowed here
 */
    
int pam_check_auth (request_rec *r) 
{
  auth_config_rec *sec =
    (auth_config_rec *)get_module_config (r->per_dir_config, &pam_auth_module);
  char *user = r->connection->user;
  register int i = 0;
  char method_restricted = 0, *line = 0, *word = 0;
  /* PDL */

  table *grpstatus;
  
  /* check for allowed users/group */
  array_header *reqs_arr = requires (r);
  require_line *reqs = 0;
  
  /* if any valid user suffices return success */
  if (!reqs_arr)
    return (OK);

  /* otherwise */
  reqs = (require_line*)reqs_arr->elts;

  if(sec->auth_grpfile)
    grpstatus = pam_groups_for_user (r->pool, user, sec->auth_grpfile);
  else
    grpstatus = NULL;

  /*  loop over requirement lines */
  for( i = 0; i < reqs_arr->nelts; i++) {
    /* if method of this requirement matches current method */
    if (reqs[i].method_mask & (1 << r->method_number)) {
      method_restricted = 1;

      line = reqs[i].requirement;
      word = getword(r->pool, &line, ' ');

      /* if any user is ok */
      if(!strcmp(word, valid_user))
	return OK;

      else if(!strcmp(word, "user")) {
        /* loop over line */
        while(*line) {
	  word = getword_conf(r->pool, &line);
	  /* if username matches remote username */
	  if(!strcmp(r->connection->user, word))
	    /* return success */
	    return OK;
        }
      }

      else if(!strcmp(word, "group")) {
        if(!grpstatus)
          return DECLINED;

        while(line[0]) {
          word = getword_conf(r->pool, &line);
          if(table_get (grpstatus, word))
            return OK;
        }
      }
    }
  }

  if (!method_restricted)
    return OK;
  
  note_basic_auth_failure (r);
  return AUTH_REQUIRED;
}

module pam_auth_module = {
   STANDARD_MODULE_STUFF,
   NULL,			/* initializer */
   pam_create_auth_dir_config,	/* dir config creater */
   NULL,			/* dir merger --- default is to override */
   NULL,			/* server config */
   NULL,			/* merge server config */
   pam_auth_cmds,		/* command table */
   NULL,			/* handlers */
   NULL,			/* filename translation */
   pam_auth_basic_user,   	/* check_user_id */
   pam_check_auth,		/* check auth */
   NULL,			/* check access */
   NULL,			/* type_checker */
   NULL,			/* fixups */
   NULL				/* logger */
};

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] []