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

Apache PAM-auth module



I also cc'ed this to Brian Behlendorf of the Apache Group because he
probably knows best where to forward it so that the right Apache people
see it. It would probably be best not to cc any replies to him as he's
busy enough. 

Because initially several people voiced interest in it and because I
didn't see Anthonys version, yet, this mail contains a preliminary version
of the PAM authentication module for Apache.

I tested it a little on our server (v 1.1.3) and so far it looks fine. 

Theres just one gotcha left: It doesn't do group authentication. This is
because I couldn't figure out how to get group information from PAM :( 
Any hints on this?

Please test out the module and tell me if you use it and how it works for
you. I'd be especially interested in reports on how it works on Apache
1.0.x and 1.2bx

---/dev/il
/* ====================================================================
 * 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
 *
 *
 * usage information:
 *
 * 1. Configuration:
 *     Module pam_auth_module mod_auth_pam.o
 *     EXTRA_LIBS+= -lpam
 * 
 * 2. Add an entry for service type "httpd" to your PAM configuration
 */

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

#include <security/pam_appl.h>

module pam_auth_module;

static const char 
  *pam_servicename = "httpd",
  *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;
}

/* 
 * 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)
{
    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;
    
    /* this is only set after get_basic_auth_pw was called */
    userinfo.name = r->connection->user;

    /* initialize pam */
    if((res = pam_start(pam_servicename, 
			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) 
{
  register int i = 0;
  char method_restricted = 0, *line = 0, *word = 0;
  /* PDL */
  
  /* 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;

  /*  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;

      /* 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;
      }
    }
  }

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

module pam_auth_module = {
   STANDARD_MODULE_STUFF,
   NULL,			/* initializer */
   NULL,	                /* dir config creater */
   NULL,			/* dir merger --- default is to override */
   NULL,			/* server config */
   NULL,			/* merge server config */
   NULL,			/* 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] []