xref: /freebsd/lib/libpam/modules/pam_radius/pam_radius.c (revision d154a420f7ae6c5f40eaa8efda173c2de27b0737)
19a10bb17SJohn Polstra /*-
29a10bb17SJohn Polstra  * Copyright 1998 Juniper Networks, Inc.
39a10bb17SJohn Polstra  * All rights reserved.
44d6991c6SDag-Erling Smørgrav  * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
58d3978c1SDag-Erling Smørgrav  * All rights reserved.
68d3978c1SDag-Erling Smørgrav  *
78d3978c1SDag-Erling Smørgrav  * Portions of this software were developed for the FreeBSD Project by
88d3978c1SDag-Erling Smørgrav  * ThinkSec AS and NAI Labs, the Security Research Division of Network
98d3978c1SDag-Erling Smørgrav  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
108d3978c1SDag-Erling Smørgrav  * ("CBOSS"), as part of the DARPA CHATS research program.
119a10bb17SJohn Polstra  *
129a10bb17SJohn Polstra  * Redistribution and use in source and binary forms, with or without
139a10bb17SJohn Polstra  * modification, are permitted provided that the following conditions
149a10bb17SJohn Polstra  * are met:
159a10bb17SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
169a10bb17SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
179a10bb17SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
189a10bb17SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
199a10bb17SJohn Polstra  *    documentation and/or other materials provided with the distribution.
208d3978c1SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote
218d3978c1SDag-Erling Smørgrav  *    products derived from this software without specific prior written
228d3978c1SDag-Erling Smørgrav  *    permission.
239a10bb17SJohn Polstra  *
249a10bb17SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
259a10bb17SJohn Polstra  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269a10bb17SJohn Polstra  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279a10bb17SJohn Polstra  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
289a10bb17SJohn Polstra  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299a10bb17SJohn Polstra  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309a10bb17SJohn Polstra  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319a10bb17SJohn Polstra  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329a10bb17SJohn Polstra  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339a10bb17SJohn Polstra  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349a10bb17SJohn Polstra  * SUCH DAMAGE.
359a10bb17SJohn Polstra  */
369a10bb17SJohn Polstra 
37ceaf33f5SMatthew Dillon #include <sys/cdefs.h>
38ceaf33f5SMatthew Dillon __FBSDID("$FreeBSD$");
39ceaf33f5SMatthew Dillon 
409a10bb17SJohn Polstra #include <sys/param.h>
41f142677bSMaxim Sobolev #include <sys/types.h>
42f142677bSMaxim Sobolev #include <sys/socket.h>
43f142677bSMaxim Sobolev #include <netdb.h>
449a10bb17SJohn Polstra #include <pwd.h>
459a10bb17SJohn Polstra #include <radlib.h>
469a10bb17SJohn Polstra #include <stdlib.h>
479a10bb17SJohn Polstra #include <string.h>
489a10bb17SJohn Polstra #include <syslog.h>
499a10bb17SJohn Polstra #include <unistd.h>
509a10bb17SJohn Polstra 
519a10bb17SJohn Polstra #define PAM_SM_AUTH
528d3978c1SDag-Erling Smørgrav 
538c66575dSDag-Erling Smørgrav #include <security/pam_appl.h>
549a10bb17SJohn Polstra #include <security/pam_modules.h>
558c66575dSDag-Erling Smørgrav #include <security/pam_mod_misc.h>
569a10bb17SJohn Polstra 
57545aa471SDag-Erling Smørgrav #define PAM_OPT_CONF		"conf"
58545aa471SDag-Erling Smørgrav #define PAM_OPT_TEMPLATE_USER	"template_user"
59545aa471SDag-Erling Smørgrav #define PAM_OPT_NAS_ID		"nas_id"
60f142677bSMaxim Sobolev #define PAM_OPT_NAS_IPADDR	"nas_ipaddr"
611642eb1aSMark Murray 
629a10bb17SJohn Polstra #define	MAX_CHALLENGE_MSGS	10
63111ccd25SDag-Erling Smørgrav #define	PASSWORD_PROMPT		"RADIUS Password:"
649a10bb17SJohn Polstra 
659a10bb17SJohn Polstra static int	 build_access_request(struct rad_handle *, const char *,
66f142677bSMaxim Sobolev 		    const char *, const char *, const char *, const void *,
67f142677bSMaxim Sobolev 		    size_t);
689a10bb17SJohn Polstra static int	 do_accept(pam_handle_t *, struct rad_handle *);
699a10bb17SJohn Polstra static int	 do_challenge(pam_handle_t *, struct rad_handle *,
70d154a420SPawel Jakub Dawidek 		    const char *, const char *, const char *);
719a10bb17SJohn Polstra 
729a10bb17SJohn Polstra /*
739a10bb17SJohn Polstra  * Construct an access request, but don't send it.  Returns 0 on success,
749a10bb17SJohn Polstra  * -1 on failure.
759a10bb17SJohn Polstra  */
769a10bb17SJohn Polstra static int
779a10bb17SJohn Polstra build_access_request(struct rad_handle *radh, const char *user,
78f142677bSMaxim Sobolev     const char *pass, const char *nas_id, const char *nas_ipaddr,
79f142677bSMaxim Sobolev     const void *state, size_t state_len)
809a10bb17SJohn Polstra {
81f142677bSMaxim Sobolev 	int error;
829a10bb17SJohn Polstra 	char host[MAXHOSTNAMELEN];
83f142677bSMaxim Sobolev 	struct sockaddr_in *haddr;
84f142677bSMaxim Sobolev 	struct addrinfo hints;
85f142677bSMaxim Sobolev 	struct addrinfo *res;
869a10bb17SJohn Polstra 
879a10bb17SJohn Polstra 	if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
889a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
8924fe7ba0SDag-Erling Smørgrav 		return (-1);
909a10bb17SJohn Polstra 	}
91f142677bSMaxim Sobolev 	if (nas_id == NULL ||
92f142677bSMaxim Sobolev 	    (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
93f142677bSMaxim Sobolev 		if (gethostname(host, sizeof host) != -1) {
94f142677bSMaxim Sobolev 			if (nas_id == NULL)
95a1d214e8SDag-Erling Smørgrav 				nas_id = host;
96f142677bSMaxim Sobolev 			if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
97f142677bSMaxim Sobolev 				nas_ipaddr = host;
98f142677bSMaxim Sobolev 		}
99f142677bSMaxim Sobolev 	}
1009a10bb17SJohn Polstra 	if ((user != NULL &&
1019a10bb17SJohn Polstra 	    rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
1029a10bb17SJohn Polstra 	    (pass != NULL &&
1039a10bb17SJohn Polstra 	    rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
104a1d214e8SDag-Erling Smørgrav 	    (nas_id != NULL &&
105a1d214e8SDag-Erling Smørgrav 	    rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
1069a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
10724fe7ba0SDag-Erling Smørgrav 		return (-1);
1089a10bb17SJohn Polstra 	}
109f142677bSMaxim Sobolev 	if (nas_ipaddr != NULL) {
110f142677bSMaxim Sobolev 		memset(&hints, 0, sizeof(hints));
11130d0a60aSDag-Erling Smørgrav 		hints.ai_family = AF_INET;
112f142677bSMaxim Sobolev 		if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
11330d0a60aSDag-Erling Smørgrav 		    res != NULL && res->ai_family == AF_INET) {
11430d0a60aSDag-Erling Smørgrav 			haddr = (struct sockaddr_in *)res->ai_addr;
115f142677bSMaxim Sobolev 			error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
116f142677bSMaxim Sobolev 			    haddr->sin_addr);
117f142677bSMaxim Sobolev 			freeaddrinfo(res);
118f142677bSMaxim Sobolev 			if (error == -1) {
119f142677bSMaxim Sobolev 				syslog(LOG_CRIT, "rad_put_addr: %s",
120f142677bSMaxim Sobolev 				    rad_strerror(radh));
121f142677bSMaxim Sobolev 				return (-1);
122f142677bSMaxim Sobolev 			}
123f142677bSMaxim Sobolev 		}
124f142677bSMaxim Sobolev 	}
1259a10bb17SJohn Polstra 	if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
1269a10bb17SJohn Polstra 	    state_len) == -1) {
1279a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
12824fe7ba0SDag-Erling Smørgrav 		return (-1);
1299a10bb17SJohn Polstra 	}
1309a10bb17SJohn Polstra 	if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
1319a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
13224fe7ba0SDag-Erling Smørgrav 		return (-1);
1339a10bb17SJohn Polstra 	}
13424fe7ba0SDag-Erling Smørgrav 	return (0);
1359a10bb17SJohn Polstra }
1369a10bb17SJohn Polstra 
1379a10bb17SJohn Polstra static int
1389a10bb17SJohn Polstra do_accept(pam_handle_t *pamh, struct rad_handle *radh)
1399a10bb17SJohn Polstra {
1409a10bb17SJohn Polstra 	int attrtype;
1419a10bb17SJohn Polstra 	const void *attrval;
1429a10bb17SJohn Polstra 	size_t attrlen;
1439a10bb17SJohn Polstra 	char *s;
1449a10bb17SJohn Polstra 
1459a10bb17SJohn Polstra 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
1469a10bb17SJohn Polstra 		if (attrtype == RAD_USER_NAME) {
1479a10bb17SJohn Polstra 			s = rad_cvt_string(attrval, attrlen);
1489a10bb17SJohn Polstra 			if (s == NULL) {
1499a10bb17SJohn Polstra 				syslog(LOG_CRIT,
1509a10bb17SJohn Polstra 				    "rad_cvt_string: out of memory");
15124fe7ba0SDag-Erling Smørgrav 				return (-1);
1529a10bb17SJohn Polstra 			}
1539a10bb17SJohn Polstra 			pam_set_item(pamh, PAM_USER, s);
1549a10bb17SJohn Polstra 			free(s);
1559a10bb17SJohn Polstra 		}
1569a10bb17SJohn Polstra 	}
1579a10bb17SJohn Polstra 	if (attrtype == -1) {
1589a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
15924fe7ba0SDag-Erling Smørgrav 		return (-1);
1609a10bb17SJohn Polstra 	}
16124fe7ba0SDag-Erling Smørgrav 	return (0);
1629a10bb17SJohn Polstra }
1639a10bb17SJohn Polstra 
1649a10bb17SJohn Polstra static int
165d154a420SPawel Jakub Dawidek do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user,
166d154a420SPawel Jakub Dawidek     const char *nas_id, const char *nas_ipaddr)
1679a10bb17SJohn Polstra {
1689a10bb17SJohn Polstra 	int retval;
1699a10bb17SJohn Polstra 	int attrtype;
1709a10bb17SJohn Polstra 	const void *attrval;
1719a10bb17SJohn Polstra 	size_t attrlen;
1729a10bb17SJohn Polstra 	const void *state;
1739a10bb17SJohn Polstra 	size_t statelen;
1749a10bb17SJohn Polstra 	struct pam_message msgs[MAX_CHALLENGE_MSGS];
1759a10bb17SJohn Polstra 	const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
1769a10bb17SJohn Polstra 	struct pam_response *resp;
1779a10bb17SJohn Polstra 	int num_msgs;
1789a10bb17SJohn Polstra 	const void *item;
1799a10bb17SJohn Polstra 	const struct pam_conv *conv;
1809a10bb17SJohn Polstra 
1819a10bb17SJohn Polstra 	state = NULL;
1829a10bb17SJohn Polstra 	statelen = 0;
1839a10bb17SJohn Polstra 	num_msgs = 0;
1849a10bb17SJohn Polstra 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
1859a10bb17SJohn Polstra 		switch (attrtype) {
1869a10bb17SJohn Polstra 
1879a10bb17SJohn Polstra 		case RAD_STATE:
1889a10bb17SJohn Polstra 			state = attrval;
1899a10bb17SJohn Polstra 			statelen = attrlen;
1909a10bb17SJohn Polstra 			break;
1919a10bb17SJohn Polstra 
1929a10bb17SJohn Polstra 		case RAD_REPLY_MESSAGE:
1939a10bb17SJohn Polstra 			if (num_msgs >= MAX_CHALLENGE_MSGS) {
1949a10bb17SJohn Polstra 				syslog(LOG_CRIT,
1959a10bb17SJohn Polstra 				    "Too many RADIUS challenge messages");
19624fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
1979a10bb17SJohn Polstra 			}
1989a10bb17SJohn Polstra 			msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
1999a10bb17SJohn Polstra 			if (msgs[num_msgs].msg == NULL) {
2009a10bb17SJohn Polstra 				syslog(LOG_CRIT,
2019a10bb17SJohn Polstra 				    "rad_cvt_string: out of memory");
20224fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
2039a10bb17SJohn Polstra 			}
2049a10bb17SJohn Polstra 			msgs[num_msgs].msg_style = PAM_TEXT_INFO;
2059a10bb17SJohn Polstra 			msg_ptrs[num_msgs] = &msgs[num_msgs];
2069a10bb17SJohn Polstra 			num_msgs++;
2079a10bb17SJohn Polstra 			break;
2089a10bb17SJohn Polstra 		}
2099a10bb17SJohn Polstra 	}
2109a10bb17SJohn Polstra 	if (attrtype == -1) {
2119a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
21224fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2139a10bb17SJohn Polstra 	}
2149a10bb17SJohn Polstra 	if (num_msgs == 0) {
2159a10bb17SJohn Polstra 		msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
2169a10bb17SJohn Polstra 		if (msgs[num_msgs].msg == NULL) {
2179a10bb17SJohn Polstra 			syslog(LOG_CRIT, "Out of memory");
21824fe7ba0SDag-Erling Smørgrav 			return (PAM_SERVICE_ERR);
2199a10bb17SJohn Polstra 		}
2209a10bb17SJohn Polstra 		msgs[num_msgs].msg_style = PAM_TEXT_INFO;
2219a10bb17SJohn Polstra 		msg_ptrs[num_msgs] = &msgs[num_msgs];
2229a10bb17SJohn Polstra 		num_msgs++;
2239a10bb17SJohn Polstra 	}
2249a10bb17SJohn Polstra 	msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
2259a10bb17SJohn Polstra 	if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
2269a10bb17SJohn Polstra 		syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
22724fe7ba0SDag-Erling Smørgrav 		return (retval);
2289a10bb17SJohn Polstra 	}
2299a10bb17SJohn Polstra 	conv = (const struct pam_conv *)item;
2309a10bb17SJohn Polstra 	if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
2319a10bb17SJohn Polstra 	    conv->appdata_ptr)) != PAM_SUCCESS)
23224fe7ba0SDag-Erling Smørgrav 		return (retval);
233d154a420SPawel Jakub Dawidek 	if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id,
234d154a420SPawel Jakub Dawidek 	    nas_ipaddr, state, statelen) == -1)
23524fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2369a10bb17SJohn Polstra 	memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
2379a10bb17SJohn Polstra 	free(resp[num_msgs-1].resp);
2389a10bb17SJohn Polstra 	free(resp);
2399a10bb17SJohn Polstra 	while (num_msgs > 0)
2403a256117SDag-Erling Smørgrav 		free(msgs[--num_msgs].msg);
24124fe7ba0SDag-Erling Smørgrav 	return (PAM_SUCCESS);
2429a10bb17SJohn Polstra }
2439a10bb17SJohn Polstra 
2449a10bb17SJohn Polstra PAM_EXTERN int
24524fe7ba0SDag-Erling Smørgrav pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
246545aa471SDag-Erling Smørgrav     int argc __unused, const char *argv[] __unused)
2479a10bb17SJohn Polstra {
2489a10bb17SJohn Polstra 	struct rad_handle *radh;
24991e93869SDag-Erling Smørgrav 	const char *user, *pass;
25091e93869SDag-Erling Smørgrav 	const void *tmpuser;
251f142677bSMaxim Sobolev 	const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
2529a10bb17SJohn Polstra 	int retval;
2539a10bb17SJohn Polstra 	int e;
2549a10bb17SJohn Polstra 
255545aa471SDag-Erling Smørgrav 	conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
256545aa471SDag-Erling Smørgrav 	template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
257545aa471SDag-Erling Smørgrav 	nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
258f142677bSMaxim Sobolev 	nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
2591642eb1aSMark Murray 
2601642eb1aSMark Murray 	retval = pam_get_user(pamh, &user, NULL);
2611642eb1aSMark Murray 	if (retval != PAM_SUCCESS)
26224fe7ba0SDag-Erling Smørgrav 		return (retval);
2631642eb1aSMark Murray 
2641642eb1aSMark Murray 	PAM_LOG("Got user: %s", user);
2651642eb1aSMark Murray 
266111ccd25SDag-Erling Smørgrav 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
2671642eb1aSMark Murray 	if (retval != PAM_SUCCESS)
26824fe7ba0SDag-Erling Smørgrav 		return (retval);
2691642eb1aSMark Murray 
2701642eb1aSMark Murray 	PAM_LOG("Got password");
2711642eb1aSMark Murray 
2721642eb1aSMark Murray 	radh = rad_open();
2731642eb1aSMark Murray 	if (radh == NULL) {
2749a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_open failed");
27524fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2769a10bb17SJohn Polstra 	}
2771642eb1aSMark Murray 
2781642eb1aSMark Murray 	PAM_LOG("Radius opened");
2791642eb1aSMark Murray 
2809a10bb17SJohn Polstra 	if (rad_config(radh, conf_file) == -1) {
2819a10bb17SJohn Polstra 		syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
2829a10bb17SJohn Polstra 		rad_close(radh);
28324fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2849a10bb17SJohn Polstra 	}
2851642eb1aSMark Murray 
2861642eb1aSMark Murray 	PAM_LOG("Radius config file read");
2871642eb1aSMark Murray 
288f142677bSMaxim Sobolev 	if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, NULL,
289f142677bSMaxim Sobolev 	    0) == -1) {
2909a10bb17SJohn Polstra 		rad_close(radh);
29124fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2929a10bb17SJohn Polstra 	}
2931642eb1aSMark Murray 
2941642eb1aSMark Murray 	PAM_LOG("Radius build access done");
2951642eb1aSMark Murray 
2969a10bb17SJohn Polstra 	for (;;) {
2979a10bb17SJohn Polstra 		switch (rad_send_request(radh)) {
2989a10bb17SJohn Polstra 
2999a10bb17SJohn Polstra 		case RAD_ACCESS_ACCEPT:
3009a10bb17SJohn Polstra 			e = do_accept(pamh, radh);
3019a10bb17SJohn Polstra 			rad_close(radh);
3029a10bb17SJohn Polstra 			if (e == -1)
30324fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
3049a10bb17SJohn Polstra 			if (template_user != NULL) {
3051642eb1aSMark Murray 
3061642eb1aSMark Murray 				PAM_LOG("Trying template user: %s",
3071642eb1aSMark Murray 				    template_user);
3089a10bb17SJohn Polstra 
3099a10bb17SJohn Polstra 				/*
3109a10bb17SJohn Polstra 				 * If the given user name doesn't exist in
3119a10bb17SJohn Polstra 				 * the local password database, change it
3129a10bb17SJohn Polstra 				 * to the value given in the "template_user"
3139a10bb17SJohn Polstra 				 * option.
3149a10bb17SJohn Polstra 				 */
31591e93869SDag-Erling Smørgrav 				retval = pam_get_item(pamh, PAM_USER, &tmpuser);
3169a10bb17SJohn Polstra 				if (retval != PAM_SUCCESS)
31724fe7ba0SDag-Erling Smørgrav 					return (retval);
3181642eb1aSMark Murray 				if (getpwnam(tmpuser) == NULL) {
3199a10bb17SJohn Polstra 					pam_set_item(pamh, PAM_USER,
3209a10bb17SJohn Polstra 					    template_user);
3211642eb1aSMark Murray 					PAM_LOG("Using template user");
3229a10bb17SJohn Polstra 				}
3231642eb1aSMark Murray 
3241642eb1aSMark Murray 			}
32524fe7ba0SDag-Erling Smørgrav 			return (PAM_SUCCESS);
3269a10bb17SJohn Polstra 
3279a10bb17SJohn Polstra 		case RAD_ACCESS_REJECT:
3289a10bb17SJohn Polstra 			rad_close(radh);
32965550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius rejection");
33024fe7ba0SDag-Erling Smørgrav 			return (PAM_AUTH_ERR);
3319a10bb17SJohn Polstra 
3329a10bb17SJohn Polstra 		case RAD_ACCESS_CHALLENGE:
333d154a420SPawel Jakub Dawidek 			retval = do_challenge(pamh, radh, user, nas_id,
334d154a420SPawel Jakub Dawidek 			    nas_ipaddr);
3351642eb1aSMark Murray 			if (retval != PAM_SUCCESS) {
3369a10bb17SJohn Polstra 				rad_close(radh);
33724fe7ba0SDag-Erling Smørgrav 				return (retval);
3389a10bb17SJohn Polstra 			}
3399a10bb17SJohn Polstra 			break;
3409a10bb17SJohn Polstra 
3419a10bb17SJohn Polstra 		case -1:
3429a10bb17SJohn Polstra 			syslog(LOG_CRIT, "rad_send_request: %s",
3439a10bb17SJohn Polstra 			    rad_strerror(radh));
3449a10bb17SJohn Polstra 			rad_close(radh);
34565550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius failure");
34624fe7ba0SDag-Erling Smørgrav 			return (PAM_AUTHINFO_UNAVAIL);
3479a10bb17SJohn Polstra 
3489a10bb17SJohn Polstra 		default:
3499a10bb17SJohn Polstra 			syslog(LOG_CRIT,
3509a10bb17SJohn Polstra 			    "rad_send_request: unexpected return value");
3519a10bb17SJohn Polstra 			rad_close(radh);
35265550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius error");
35324fe7ba0SDag-Erling Smørgrav 			return (PAM_SERVICE_ERR);
3549a10bb17SJohn Polstra 		}
3559a10bb17SJohn Polstra 	}
3569a10bb17SJohn Polstra }
3579a10bb17SJohn Polstra 
3589a10bb17SJohn Polstra PAM_EXTERN int
35924fe7ba0SDag-Erling Smørgrav pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
36024fe7ba0SDag-Erling Smørgrav     int argc __unused, const char *argv[] __unused)
3619a10bb17SJohn Polstra {
36265550d9bSMark Murray 
36324fe7ba0SDag-Erling Smørgrav 	return (PAM_SUCCESS);
3648d3978c1SDag-Erling Smørgrav }
3658d3978c1SDag-Erling Smørgrav 
3669294327dSJohn Polstra PAM_MODULE_ENTRY("pam_radius");
367