Home | History | Annotate | Line # | Download | only in pam_radius
pam_radius.c revision 1.7.40.1
      1  1.7.40.1      yamt /*	$NetBSD: pam_radius.c,v 1.7.40.1 2014/05/22 11:36:58 yamt Exp $	*/
      2       1.2  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright 1998 Juniper Networks, Inc.
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
      7       1.1  christos  * All rights reserved.
      8       1.1  christos  *
      9       1.1  christos  * Portions of this software were developed for the FreeBSD Project by
     10       1.1  christos  * ThinkSec AS and NAI Labs, the Security Research Division of Network
     11       1.1  christos  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
     12       1.1  christos  * ("CBOSS"), as part of the DARPA CHATS research program.
     13       1.1  christos  *
     14       1.1  christos  * Redistribution and use in source and binary forms, with or without
     15       1.1  christos  * modification, are permitted provided that the following conditions
     16       1.1  christos  * are met:
     17       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     18       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     19       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     20       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     21       1.1  christos  *    documentation and/or other materials provided with the distribution.
     22       1.1  christos  * 3. The name of the author may not be used to endorse or promote
     23       1.1  christos  *    products derived from this software without specific prior written
     24       1.1  christos  *    permission.
     25       1.1  christos  *
     26       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     27       1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     30       1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1  christos  * SUCH DAMAGE.
     37       1.1  christos  */
     38       1.1  christos 
     39       1.1  christos #include <sys/cdefs.h>
     40       1.2  christos #ifdef __FreeBSD__
     41       1.1  christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_radius/pam_radius.c,v 1.22 2004/06/25 12:32:45 kan Exp $");
     42       1.2  christos #else
     43  1.7.40.1      yamt __RCSID("$NetBSD: pam_radius.c,v 1.7.40.1 2014/05/22 11:36:58 yamt Exp $");
     44       1.2  christos #endif
     45       1.1  christos 
     46       1.1  christos #include <sys/param.h>
     47       1.1  christos #include <sys/types.h>
     48       1.1  christos #include <sys/socket.h>
     49       1.1  christos #include <netdb.h>
     50       1.1  christos #include <pwd.h>
     51       1.1  christos #include <radlib.h>
     52       1.1  christos #include <stdlib.h>
     53       1.1  christos #include <string.h>
     54       1.1  christos #include <syslog.h>
     55       1.1  christos #include <unistd.h>
     56       1.6  christos #include <stdarg.h>
     57       1.1  christos 
     58       1.1  christos #define PAM_SM_AUTH
     59       1.1  christos 
     60       1.1  christos #include <security/pam_appl.h>
     61       1.1  christos #include <security/pam_modules.h>
     62       1.1  christos #include <security/pam_mod_misc.h>
     63       1.1  christos 
     64       1.1  christos #define PAM_OPT_CONF		"conf"
     65       1.1  christos #define PAM_OPT_TEMPLATE_USER	"template_user"
     66       1.1  christos #define PAM_OPT_NAS_ID		"nas_id"
     67       1.1  christos #define PAM_OPT_NAS_IPADDR	"nas_ipaddr"
     68       1.1  christos 
     69       1.1  christos #define	MAX_CHALLENGE_MSGS	10
     70       1.1  christos #define	PASSWORD_PROMPT		"RADIUS Password:"
     71       1.1  christos 
     72       1.1  christos static int	 build_access_request(struct rad_handle *, const char *,
     73       1.1  christos 		    const char *, const char *, const char *, const void *,
     74       1.1  christos 		    size_t);
     75       1.1  christos static int	 do_accept(pam_handle_t *, struct rad_handle *);
     76       1.1  christos static int	 do_challenge(pam_handle_t *, struct rad_handle *,
     77       1.1  christos 		    const char *);
     78       1.1  christos 
     79  1.7.40.1      yamt __printflike(2, 3)
     80       1.6  christos static void
     81       1.6  christos logit(int level, const char *fmt, ...)
     82       1.6  christos {
     83       1.6  christos 	va_list ap;
     84       1.7  christos 	struct syslog_data data = SYSLOG_DATA_INIT;
     85       1.6  christos 
     86       1.6  christos 	openlog_r("pam_radius", LOG_PID, LOG_AUTHPRIV, &data);
     87       1.6  christos 	va_start(ap, fmt);
     88       1.6  christos 	vsyslog_r(level, &data, fmt, ap);
     89       1.6  christos 	va_end(ap);
     90       1.6  christos 	closelog_r(&data);
     91       1.6  christos }
     92       1.6  christos 
     93       1.1  christos /*
     94       1.1  christos  * Construct an access request, but don't send it.  Returns 0 on success,
     95       1.1  christos  * -1 on failure.
     96       1.1  christos  */
     97       1.1  christos static int
     98       1.1  christos build_access_request(struct rad_handle *radh, const char *user,
     99       1.1  christos     const char *pass, const char *nas_id, const char *nas_ipaddr,
    100       1.1  christos     const void *state, size_t state_len)
    101       1.1  christos {
    102       1.1  christos 	int error;
    103       1.1  christos 	char host[MAXHOSTNAMELEN];
    104       1.1  christos 	struct sockaddr_in *haddr;
    105       1.1  christos 	struct addrinfo hints;
    106       1.1  christos 	struct addrinfo *res;
    107       1.1  christos 
    108       1.1  christos 	if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
    109       1.6  christos 		logit(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
    110       1.1  christos 		return (-1);
    111       1.1  christos 	}
    112       1.1  christos 	if (nas_id == NULL ||
    113       1.1  christos 	    (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
    114       1.1  christos 		if (gethostname(host, sizeof host) != -1) {
    115       1.1  christos 			if (nas_id == NULL)
    116       1.1  christos 				nas_id = host;
    117       1.1  christos 			if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
    118       1.1  christos 				nas_ipaddr = host;
    119       1.1  christos 		}
    120       1.1  christos 	}
    121       1.1  christos 	if ((user != NULL &&
    122       1.1  christos 	    rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
    123       1.1  christos 	    (pass != NULL &&
    124       1.1  christos 	    rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
    125       1.1  christos 	    (nas_id != NULL &&
    126       1.1  christos 	    rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
    127       1.6  christos 		logit(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
    128       1.1  christos 		return (-1);
    129       1.1  christos 	}
    130       1.1  christos 	if (nas_ipaddr != NULL) {
    131       1.1  christos 		memset(&hints, 0, sizeof(hints));
    132       1.1  christos 		hints.ai_family = PF_INET;
    133       1.1  christos 		if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
    134       1.1  christos 		    res != NULL) {
    135       1.5      matt 			haddr = (struct sockaddr_in *)res->ai_addr;
    136       1.1  christos 			error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
    137       1.1  christos 			    haddr->sin_addr);
    138       1.1  christos 			freeaddrinfo(res);
    139       1.1  christos 			if (error == -1) {
    140       1.6  christos 				logit(LOG_CRIT, "rad_put_addr: %s",
    141       1.1  christos 				    rad_strerror(radh));
    142       1.1  christos 				return (-1);
    143       1.1  christos 			}
    144       1.1  christos 		}
    145       1.1  christos 	}
    146       1.1  christos 	if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
    147       1.1  christos 	    state_len) == -1) {
    148       1.6  christos 		logit(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
    149       1.1  christos 		return (-1);
    150       1.1  christos 	}
    151       1.1  christos 	if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
    152       1.6  christos 		logit(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
    153       1.1  christos 		return (-1);
    154       1.1  christos 	}
    155       1.1  christos 	return (0);
    156       1.1  christos }
    157       1.1  christos 
    158       1.1  christos static int
    159       1.1  christos do_accept(pam_handle_t *pamh, struct rad_handle *radh)
    160       1.1  christos {
    161       1.1  christos 	int attrtype;
    162       1.1  christos 	const void *attrval;
    163       1.1  christos 	size_t attrlen;
    164       1.1  christos 	char *s;
    165       1.1  christos 
    166       1.1  christos 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
    167       1.1  christos 		if (attrtype == RAD_USER_NAME) {
    168       1.1  christos 			s = rad_cvt_string(attrval, attrlen);
    169       1.1  christos 			if (s == NULL) {
    170       1.6  christos 				logit(LOG_CRIT,
    171       1.1  christos 				    "rad_cvt_string: out of memory");
    172       1.1  christos 				return (-1);
    173       1.1  christos 			}
    174       1.1  christos 			pam_set_item(pamh, PAM_USER, s);
    175       1.1  christos 			free(s);
    176       1.1  christos 		}
    177       1.1  christos 	}
    178       1.1  christos 	if (attrtype == -1) {
    179       1.6  christos 		logit(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
    180       1.1  christos 		return (-1);
    181       1.1  christos 	}
    182       1.1  christos 	return (0);
    183       1.1  christos }
    184       1.1  christos 
    185       1.1  christos static int
    186       1.1  christos do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user)
    187       1.1  christos {
    188       1.1  christos 	int retval;
    189       1.1  christos 	int attrtype;
    190       1.1  christos 	const void *attrval;
    191       1.1  christos 	size_t attrlen;
    192       1.1  christos 	const void *state;
    193       1.1  christos 	size_t statelen;
    194       1.1  christos 	struct pam_message msgs[MAX_CHALLENGE_MSGS];
    195       1.1  christos 	const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
    196       1.1  christos 	struct pam_response *resp;
    197       1.1  christos 	int num_msgs;
    198       1.1  christos 	const void *item;
    199       1.1  christos 	const struct pam_conv *conv;
    200       1.1  christos 
    201       1.1  christos 	state = NULL;
    202       1.1  christos 	statelen = 0;
    203       1.1  christos 	num_msgs = 0;
    204       1.1  christos 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
    205       1.1  christos 		switch (attrtype) {
    206       1.1  christos 
    207       1.1  christos 		case RAD_STATE:
    208       1.1  christos 			state = attrval;
    209       1.1  christos 			statelen = attrlen;
    210       1.1  christos 			break;
    211       1.1  christos 
    212       1.1  christos 		case RAD_REPLY_MESSAGE:
    213       1.1  christos 			if (num_msgs >= MAX_CHALLENGE_MSGS) {
    214       1.6  christos 				logit(LOG_CRIT,
    215       1.1  christos 				    "Too many RADIUS challenge messages");
    216       1.1  christos 				return (PAM_SERVICE_ERR);
    217       1.1  christos 			}
    218       1.1  christos 			msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
    219       1.1  christos 			if (msgs[num_msgs].msg == NULL) {
    220       1.6  christos 				logit(LOG_CRIT,
    221       1.1  christos 				    "rad_cvt_string: out of memory");
    222       1.1  christos 				return (PAM_SERVICE_ERR);
    223       1.1  christos 			}
    224       1.1  christos 			msgs[num_msgs].msg_style = PAM_TEXT_INFO;
    225       1.1  christos 			msg_ptrs[num_msgs] = &msgs[num_msgs];
    226       1.1  christos 			num_msgs++;
    227       1.1  christos 			break;
    228       1.1  christos 		}
    229       1.1  christos 	}
    230       1.1  christos 	if (attrtype == -1) {
    231       1.6  christos 		logit(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
    232       1.1  christos 		return (PAM_SERVICE_ERR);
    233       1.1  christos 	}
    234       1.1  christos 	if (num_msgs == 0) {
    235       1.1  christos 		msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
    236       1.1  christos 		if (msgs[num_msgs].msg == NULL) {
    237       1.6  christos 			logit(LOG_CRIT, "Out of memory");
    238       1.1  christos 			return (PAM_SERVICE_ERR);
    239       1.1  christos 		}
    240       1.1  christos 		msgs[num_msgs].msg_style = PAM_TEXT_INFO;
    241       1.1  christos 		msg_ptrs[num_msgs] = &msgs[num_msgs];
    242       1.1  christos 		num_msgs++;
    243       1.1  christos 	}
    244       1.1  christos 	msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
    245       1.1  christos 	if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
    246       1.6  christos 		logit(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
    247       1.1  christos 		return (retval);
    248       1.1  christos 	}
    249       1.1  christos 	conv = (const struct pam_conv *)item;
    250       1.1  christos 	if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
    251       1.1  christos 	    conv->appdata_ptr)) != PAM_SUCCESS)
    252       1.1  christos 		return (retval);
    253       1.1  christos 	if (build_access_request(radh, user, resp[num_msgs-1].resp, NULL,
    254       1.1  christos 	    NULL, state, statelen) == -1)
    255       1.1  christos 		return (PAM_SERVICE_ERR);
    256       1.1  christos 	memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
    257       1.1  christos 	free(resp[num_msgs-1].resp);
    258       1.1  christos 	free(resp);
    259       1.1  christos 	while (num_msgs > 0)
    260       1.1  christos 		free(msgs[--num_msgs].msg);
    261       1.1  christos 	return (PAM_SUCCESS);
    262       1.1  christos }
    263       1.1  christos 
    264       1.1  christos PAM_EXTERN int
    265       1.1  christos pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
    266       1.1  christos     int argc __unused, const char *argv[] __unused)
    267       1.1  christos {
    268       1.1  christos 	struct rad_handle *radh;
    269       1.1  christos 	const char *user, *pass;
    270       1.1  christos 	const void *tmpuser;
    271       1.3   thorpej 	struct passwd *pwd, pwres;
    272       1.3   thorpej 	char pwbuf[1024];
    273       1.1  christos 	const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
    274       1.1  christos 	int retval;
    275       1.1  christos 	int e;
    276       1.1  christos 
    277       1.1  christos 	conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
    278       1.1  christos 	template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
    279       1.1  christos 	nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
    280       1.1  christos 	nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
    281       1.1  christos 
    282       1.1  christos 	retval = pam_get_user(pamh, &user, NULL);
    283       1.1  christos 	if (retval != PAM_SUCCESS)
    284       1.1  christos 		return (retval);
    285       1.1  christos 
    286       1.1  christos 	PAM_LOG("Got user: %s", user);
    287       1.1  christos 
    288       1.1  christos 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
    289       1.1  christos 	if (retval != PAM_SUCCESS)
    290       1.1  christos 		return (retval);
    291       1.1  christos 
    292       1.1  christos 	PAM_LOG("Got password");
    293       1.1  christos 
    294       1.1  christos 	radh = rad_open();
    295       1.1  christos 	if (radh == NULL) {
    296       1.6  christos 		logit(LOG_CRIT, "rad_open failed");
    297       1.1  christos 		return (PAM_SERVICE_ERR);
    298       1.1  christos 	}
    299       1.1  christos 
    300       1.1  christos 	PAM_LOG("Radius opened");
    301       1.1  christos 
    302       1.1  christos 	if (rad_config(radh, conf_file) == -1) {
    303       1.6  christos 		logit(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
    304       1.1  christos 		rad_close(radh);
    305       1.1  christos 		return (PAM_SERVICE_ERR);
    306       1.1  christos 	}
    307       1.1  christos 
    308       1.1  christos 	PAM_LOG("Radius config file read");
    309       1.1  christos 
    310       1.1  christos 	if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, NULL,
    311       1.1  christos 	    0) == -1) {
    312       1.1  christos 		rad_close(radh);
    313       1.1  christos 		return (PAM_SERVICE_ERR);
    314       1.1  christos 	}
    315       1.1  christos 
    316       1.1  christos 	PAM_LOG("Radius build access done");
    317       1.1  christos 
    318       1.1  christos 	for (;;) {
    319       1.1  christos 		switch (rad_send_request(radh)) {
    320       1.1  christos 
    321       1.1  christos 		case RAD_ACCESS_ACCEPT:
    322       1.1  christos 			e = do_accept(pamh, radh);
    323       1.1  christos 			rad_close(radh);
    324       1.1  christos 			if (e == -1)
    325       1.1  christos 				return (PAM_SERVICE_ERR);
    326       1.1  christos 			if (template_user != NULL) {
    327       1.1  christos 
    328       1.1  christos 				PAM_LOG("Trying template user: %s",
    329       1.1  christos 				    template_user);
    330       1.1  christos 
    331       1.1  christos 				/*
    332       1.1  christos 				 * If the given user name doesn't exist in
    333       1.1  christos 				 * the local password database, change it
    334       1.1  christos 				 * to the value given in the "template_user"
    335       1.1  christos 				 * option.
    336       1.1  christos 				 */
    337       1.1  christos 				retval = pam_get_item(pamh, PAM_USER, &tmpuser);
    338       1.1  christos 				if (retval != PAM_SUCCESS)
    339       1.1  christos 					return (retval);
    340       1.3   thorpej 				if (getpwnam_r(tmpuser, &pwres, pwbuf,
    341       1.4  christos 					       sizeof(pwbuf), &pwd) != 0 ||
    342       1.4  christos 				    pwd == NULL) {
    343       1.1  christos 					pam_set_item(pamh, PAM_USER,
    344       1.1  christos 					    template_user);
    345       1.1  christos 					PAM_LOG("Using template user");
    346       1.1  christos 				}
    347       1.1  christos 
    348       1.1  christos 			}
    349       1.1  christos 			return (PAM_SUCCESS);
    350       1.1  christos 
    351       1.1  christos 		case RAD_ACCESS_REJECT:
    352       1.1  christos 			rad_close(radh);
    353       1.1  christos 			PAM_VERBOSE_ERROR("Radius rejection");
    354       1.1  christos 			return (PAM_AUTH_ERR);
    355       1.1  christos 
    356       1.1  christos 		case RAD_ACCESS_CHALLENGE:
    357       1.1  christos 			retval = do_challenge(pamh, radh, user);
    358       1.1  christos 			if (retval != PAM_SUCCESS) {
    359       1.1  christos 				rad_close(radh);
    360       1.1  christos 				return (retval);
    361       1.1  christos 			}
    362       1.1  christos 			break;
    363       1.1  christos 
    364       1.1  christos 		case -1:
    365       1.6  christos 			logit(LOG_CRIT, "rad_send_request: %s",
    366       1.1  christos 			    rad_strerror(radh));
    367       1.1  christos 			rad_close(radh);
    368       1.1  christos 			PAM_VERBOSE_ERROR("Radius failure");
    369       1.1  christos 			return (PAM_AUTHINFO_UNAVAIL);
    370       1.1  christos 
    371       1.1  christos 		default:
    372       1.6  christos 			logit(LOG_CRIT,
    373       1.1  christos 			    "rad_send_request: unexpected return value");
    374       1.1  christos 			rad_close(radh);
    375       1.1  christos 			PAM_VERBOSE_ERROR("Radius error");
    376       1.1  christos 			return (PAM_SERVICE_ERR);
    377       1.1  christos 		}
    378       1.1  christos 	}
    379       1.1  christos }
    380       1.1  christos 
    381       1.1  christos PAM_EXTERN int
    382       1.1  christos pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
    383       1.1  christos     int argc __unused, const char *argv[] __unused)
    384       1.1  christos {
    385       1.1  christos 
    386       1.1  christos 	return (PAM_SUCCESS);
    387       1.1  christos }
    388       1.1  christos 
    389       1.1  christos PAM_MODULE_ENTRY("pam_radius");
    390