xref: /freebsd/lib/libpam/modules/pam_radius/pam_radius.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
19a10bb17SJohn Polstra /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*5e53a4f9SPedro F. Giffuni  *
49a10bb17SJohn Polstra  * Copyright 1998 Juniper Networks, Inc.
59a10bb17SJohn Polstra  * All rights reserved.
64d6991c6SDag-Erling Smørgrav  * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
78d3978c1SDag-Erling Smørgrav  * All rights reserved.
88d3978c1SDag-Erling Smørgrav  *
98d3978c1SDag-Erling Smørgrav  * Portions of this software were developed for the FreeBSD Project by
108d3978c1SDag-Erling Smørgrav  * ThinkSec AS and NAI Labs, the Security Research Division of Network
118d3978c1SDag-Erling Smørgrav  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
128d3978c1SDag-Erling Smørgrav  * ("CBOSS"), as part of the DARPA CHATS research program.
139a10bb17SJohn Polstra  *
149a10bb17SJohn Polstra  * Redistribution and use in source and binary forms, with or without
159a10bb17SJohn Polstra  * modification, are permitted provided that the following conditions
169a10bb17SJohn Polstra  * are met:
179a10bb17SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
189a10bb17SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
199a10bb17SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
209a10bb17SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
219a10bb17SJohn Polstra  *    documentation and/or other materials provided with the distribution.
228d3978c1SDag-Erling Smørgrav  * 3. The name of the author may not be used to endorse or promote
238d3978c1SDag-Erling Smørgrav  *    products derived from this software without specific prior written
248d3978c1SDag-Erling Smørgrav  *    permission.
259a10bb17SJohn Polstra  *
269a10bb17SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
279a10bb17SJohn Polstra  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
289a10bb17SJohn Polstra  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
299a10bb17SJohn Polstra  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
309a10bb17SJohn Polstra  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
319a10bb17SJohn Polstra  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
329a10bb17SJohn Polstra  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
339a10bb17SJohn Polstra  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
349a10bb17SJohn Polstra  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
359a10bb17SJohn Polstra  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
369a10bb17SJohn Polstra  * SUCH DAMAGE.
379a10bb17SJohn Polstra  */
389a10bb17SJohn Polstra 
39ceaf33f5SMatthew Dillon #include <sys/cdefs.h>
40ceaf33f5SMatthew Dillon __FBSDID("$FreeBSD$");
41ceaf33f5SMatthew Dillon 
429a10bb17SJohn Polstra #include <sys/param.h>
43f142677bSMaxim Sobolev #include <sys/socket.h>
44f142677bSMaxim Sobolev #include <netdb.h>
459a10bb17SJohn Polstra #include <pwd.h>
469a10bb17SJohn Polstra #include <radlib.h>
479a10bb17SJohn Polstra #include <stdlib.h>
489a10bb17SJohn Polstra #include <string.h>
499a10bb17SJohn Polstra #include <syslog.h>
509a10bb17SJohn Polstra #include <unistd.h>
519a10bb17SJohn Polstra 
529a10bb17SJohn Polstra #define PAM_SM_AUTH
538d3978c1SDag-Erling Smørgrav 
548c66575dSDag-Erling Smørgrav #include <security/pam_appl.h>
559a10bb17SJohn Polstra #include <security/pam_modules.h>
568c66575dSDag-Erling Smørgrav #include <security/pam_mod_misc.h>
579a10bb17SJohn Polstra 
58545aa471SDag-Erling Smørgrav #define PAM_OPT_CONF		"conf"
59545aa471SDag-Erling Smørgrav #define PAM_OPT_TEMPLATE_USER	"template_user"
60545aa471SDag-Erling Smørgrav #define PAM_OPT_NAS_ID		"nas_id"
61f142677bSMaxim Sobolev #define PAM_OPT_NAS_IPADDR	"nas_ipaddr"
621642eb1aSMark Murray 
639a10bb17SJohn Polstra #define	MAX_CHALLENGE_MSGS	10
64111ccd25SDag-Erling Smørgrav #define	PASSWORD_PROMPT		"RADIUS Password:"
659a10bb17SJohn Polstra 
669a10bb17SJohn Polstra static int	 build_access_request(struct rad_handle *, const char *,
67f8b83996SDag-Erling Smørgrav 		    const char *, const char *, const char *, const char *,
68f8b83996SDag-Erling Smørgrav 		    const void *, size_t);
699a10bb17SJohn Polstra static int	 do_accept(pam_handle_t *, struct rad_handle *);
709a10bb17SJohn Polstra static int	 do_challenge(pam_handle_t *, struct rad_handle *,
71f8b83996SDag-Erling Smørgrav 		    const char *, const char *, const char *, const char *);
729a10bb17SJohn Polstra 
739a10bb17SJohn Polstra /*
749a10bb17SJohn Polstra  * Construct an access request, but don't send it.  Returns 0 on success,
759a10bb17SJohn Polstra  * -1 on failure.
769a10bb17SJohn Polstra  */
779a10bb17SJohn Polstra static int
789a10bb17SJohn Polstra build_access_request(struct rad_handle *radh, const char *user,
79f142677bSMaxim Sobolev     const char *pass, const char *nas_id, const char *nas_ipaddr,
80f8b83996SDag-Erling Smørgrav     const char *rhost, const void *state, size_t state_len)
819a10bb17SJohn Polstra {
82f142677bSMaxim Sobolev 	int error;
839a10bb17SJohn Polstra 	char host[MAXHOSTNAMELEN];
84f142677bSMaxim Sobolev 	struct sockaddr_in *haddr;
85f142677bSMaxim Sobolev 	struct addrinfo hints;
86f142677bSMaxim Sobolev 	struct addrinfo *res;
879a10bb17SJohn Polstra 
889a10bb17SJohn Polstra 	if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
899a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
9024fe7ba0SDag-Erling Smørgrav 		return (-1);
919a10bb17SJohn Polstra 	}
92f142677bSMaxim Sobolev 	if (nas_id == NULL ||
93f142677bSMaxim Sobolev 	    (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
94f142677bSMaxim Sobolev 		if (gethostname(host, sizeof host) != -1) {
95f142677bSMaxim Sobolev 			if (nas_id == NULL)
96a1d214e8SDag-Erling Smørgrav 				nas_id = host;
97f142677bSMaxim Sobolev 			if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
98f142677bSMaxim Sobolev 				nas_ipaddr = host;
99f142677bSMaxim Sobolev 		}
100f142677bSMaxim Sobolev 	}
1019a10bb17SJohn Polstra 	if ((user != NULL &&
1029a10bb17SJohn Polstra 	    rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
1039a10bb17SJohn Polstra 	    (pass != NULL &&
1049a10bb17SJohn Polstra 	    rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
105a1d214e8SDag-Erling Smørgrav 	    (nas_id != NULL &&
106a1d214e8SDag-Erling Smørgrav 	    rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
1079a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
10824fe7ba0SDag-Erling Smørgrav 		return (-1);
1099a10bb17SJohn Polstra 	}
110f142677bSMaxim Sobolev 	if (nas_ipaddr != NULL) {
111f142677bSMaxim Sobolev 		memset(&hints, 0, sizeof(hints));
11230d0a60aSDag-Erling Smørgrav 		hints.ai_family = AF_INET;
113f142677bSMaxim Sobolev 		if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
11430d0a60aSDag-Erling Smørgrav 		    res != NULL && res->ai_family == AF_INET) {
11530d0a60aSDag-Erling Smørgrav 			haddr = (struct sockaddr_in *)res->ai_addr;
116f142677bSMaxim Sobolev 			error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
117f142677bSMaxim Sobolev 			    haddr->sin_addr);
118f142677bSMaxim Sobolev 			freeaddrinfo(res);
119f142677bSMaxim Sobolev 			if (error == -1) {
120f142677bSMaxim Sobolev 				syslog(LOG_CRIT, "rad_put_addr: %s",
121f142677bSMaxim Sobolev 				    rad_strerror(radh));
122f142677bSMaxim Sobolev 				return (-1);
123f142677bSMaxim Sobolev 			}
124f142677bSMaxim Sobolev 		}
125f142677bSMaxim Sobolev 	}
126f8b83996SDag-Erling Smørgrav 	if (rhost != NULL &&
127f8b83996SDag-Erling Smørgrav 	    rad_put_string(radh, RAD_CALLING_STATION_ID, rhost) == -1) {
128f8b83996SDag-Erling Smørgrav 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
129f8b83996SDag-Erling Smørgrav 		return (-1);
130f8b83996SDag-Erling Smørgrav 	}
131f8b83996SDag-Erling Smørgrav 	if (state != NULL &&
132f8b83996SDag-Erling Smørgrav 	    rad_put_attr(radh, RAD_STATE, state, state_len) == -1) {
1339a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
13424fe7ba0SDag-Erling Smørgrav 		return (-1);
1359a10bb17SJohn Polstra 	}
1369a10bb17SJohn Polstra 	if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
1379a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
13824fe7ba0SDag-Erling Smørgrav 		return (-1);
1399a10bb17SJohn Polstra 	}
14024fe7ba0SDag-Erling Smørgrav 	return (0);
1419a10bb17SJohn Polstra }
1429a10bb17SJohn Polstra 
1439a10bb17SJohn Polstra static int
1449a10bb17SJohn Polstra do_accept(pam_handle_t *pamh, struct rad_handle *radh)
1459a10bb17SJohn Polstra {
1469a10bb17SJohn Polstra 	int attrtype;
1479a10bb17SJohn Polstra 	const void *attrval;
1489a10bb17SJohn Polstra 	size_t attrlen;
1499a10bb17SJohn Polstra 	char *s;
1509a10bb17SJohn Polstra 
1519a10bb17SJohn Polstra 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
1529a10bb17SJohn Polstra 		if (attrtype == RAD_USER_NAME) {
1539a10bb17SJohn Polstra 			s = rad_cvt_string(attrval, attrlen);
1549a10bb17SJohn Polstra 			if (s == NULL) {
1559a10bb17SJohn Polstra 				syslog(LOG_CRIT,
1569a10bb17SJohn Polstra 				    "rad_cvt_string: out of memory");
15724fe7ba0SDag-Erling Smørgrav 				return (-1);
1589a10bb17SJohn Polstra 			}
1599a10bb17SJohn Polstra 			pam_set_item(pamh, PAM_USER, s);
1609a10bb17SJohn Polstra 			free(s);
1619a10bb17SJohn Polstra 		}
1629a10bb17SJohn Polstra 	}
1639a10bb17SJohn Polstra 	if (attrtype == -1) {
1649a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
16524fe7ba0SDag-Erling Smørgrav 		return (-1);
1669a10bb17SJohn Polstra 	}
16724fe7ba0SDag-Erling Smørgrav 	return (0);
1689a10bb17SJohn Polstra }
1699a10bb17SJohn Polstra 
1709a10bb17SJohn Polstra static int
171d154a420SPawel Jakub Dawidek do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user,
172f8b83996SDag-Erling Smørgrav     const char *nas_id, const char *nas_ipaddr, const char *rhost)
1739a10bb17SJohn Polstra {
1749a10bb17SJohn Polstra 	int retval;
1759a10bb17SJohn Polstra 	int attrtype;
1769a10bb17SJohn Polstra 	const void *attrval;
1779a10bb17SJohn Polstra 	size_t attrlen;
1789a10bb17SJohn Polstra 	const void *state;
1799a10bb17SJohn Polstra 	size_t statelen;
1809a10bb17SJohn Polstra 	struct pam_message msgs[MAX_CHALLENGE_MSGS];
1819a10bb17SJohn Polstra 	const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
1829a10bb17SJohn Polstra 	struct pam_response *resp;
1839a10bb17SJohn Polstra 	int num_msgs;
1849a10bb17SJohn Polstra 	const void *item;
1859a10bb17SJohn Polstra 	const struct pam_conv *conv;
1869a10bb17SJohn Polstra 
1879a10bb17SJohn Polstra 	state = NULL;
1889a10bb17SJohn Polstra 	statelen = 0;
1899a10bb17SJohn Polstra 	num_msgs = 0;
1909a10bb17SJohn Polstra 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
1919a10bb17SJohn Polstra 		switch (attrtype) {
1929a10bb17SJohn Polstra 
1939a10bb17SJohn Polstra 		case RAD_STATE:
1949a10bb17SJohn Polstra 			state = attrval;
1959a10bb17SJohn Polstra 			statelen = attrlen;
1969a10bb17SJohn Polstra 			break;
1979a10bb17SJohn Polstra 
1989a10bb17SJohn Polstra 		case RAD_REPLY_MESSAGE:
1999a10bb17SJohn Polstra 			if (num_msgs >= MAX_CHALLENGE_MSGS) {
2009a10bb17SJohn Polstra 				syslog(LOG_CRIT,
2019a10bb17SJohn Polstra 				    "Too many RADIUS challenge messages");
20224fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
2039a10bb17SJohn Polstra 			}
2049a10bb17SJohn Polstra 			msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
2059a10bb17SJohn Polstra 			if (msgs[num_msgs].msg == NULL) {
2069a10bb17SJohn Polstra 				syslog(LOG_CRIT,
2079a10bb17SJohn Polstra 				    "rad_cvt_string: out of memory");
20824fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
2099a10bb17SJohn Polstra 			}
2109a10bb17SJohn Polstra 			msgs[num_msgs].msg_style = PAM_TEXT_INFO;
2119a10bb17SJohn Polstra 			msg_ptrs[num_msgs] = &msgs[num_msgs];
2129a10bb17SJohn Polstra 			num_msgs++;
2139a10bb17SJohn Polstra 			break;
2149a10bb17SJohn Polstra 		}
2159a10bb17SJohn Polstra 	}
2169a10bb17SJohn Polstra 	if (attrtype == -1) {
2179a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
21824fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2199a10bb17SJohn Polstra 	}
2209a10bb17SJohn Polstra 	if (num_msgs == 0) {
2219a10bb17SJohn Polstra 		msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
2229a10bb17SJohn Polstra 		if (msgs[num_msgs].msg == NULL) {
2239a10bb17SJohn Polstra 			syslog(LOG_CRIT, "Out of memory");
22424fe7ba0SDag-Erling Smørgrav 			return (PAM_SERVICE_ERR);
2259a10bb17SJohn Polstra 		}
2269a10bb17SJohn Polstra 		msgs[num_msgs].msg_style = PAM_TEXT_INFO;
2279a10bb17SJohn Polstra 		msg_ptrs[num_msgs] = &msgs[num_msgs];
2289a10bb17SJohn Polstra 		num_msgs++;
2299a10bb17SJohn Polstra 	}
2309a10bb17SJohn Polstra 	msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
2319a10bb17SJohn Polstra 	if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
2329a10bb17SJohn Polstra 		syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
23324fe7ba0SDag-Erling Smørgrav 		return (retval);
2349a10bb17SJohn Polstra 	}
2359a10bb17SJohn Polstra 	conv = (const struct pam_conv *)item;
2369a10bb17SJohn Polstra 	if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
2379a10bb17SJohn Polstra 	    conv->appdata_ptr)) != PAM_SUCCESS)
23824fe7ba0SDag-Erling Smørgrav 		return (retval);
239d154a420SPawel Jakub Dawidek 	if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id,
240f8b83996SDag-Erling Smørgrav 	    nas_ipaddr, rhost, state, statelen) == -1)
24124fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2429a10bb17SJohn Polstra 	memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
2439a10bb17SJohn Polstra 	free(resp[num_msgs-1].resp);
2449a10bb17SJohn Polstra 	free(resp);
2459a10bb17SJohn Polstra 	while (num_msgs > 0)
2463a256117SDag-Erling Smørgrav 		free(msgs[--num_msgs].msg);
24724fe7ba0SDag-Erling Smørgrav 	return (PAM_SUCCESS);
2489a10bb17SJohn Polstra }
2499a10bb17SJohn Polstra 
2509a10bb17SJohn Polstra PAM_EXTERN int
25124fe7ba0SDag-Erling Smørgrav pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
252545aa471SDag-Erling Smørgrav     int argc __unused, const char *argv[] __unused)
2539a10bb17SJohn Polstra {
2549a10bb17SJohn Polstra 	struct rad_handle *radh;
25591e93869SDag-Erling Smørgrav 	const char *user, *pass;
256f8b83996SDag-Erling Smørgrav 	const void *rhost, *tmpuser;
257f142677bSMaxim Sobolev 	const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
2589a10bb17SJohn Polstra 	int retval;
2599a10bb17SJohn Polstra 	int e;
2609a10bb17SJohn Polstra 
261545aa471SDag-Erling Smørgrav 	conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
262545aa471SDag-Erling Smørgrav 	template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
263545aa471SDag-Erling Smørgrav 	nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
264f142677bSMaxim Sobolev 	nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
265f8b83996SDag-Erling Smørgrav 	pam_get_item(pamh, PAM_RHOST, &rhost);
2661642eb1aSMark Murray 
2671642eb1aSMark Murray 	retval = pam_get_user(pamh, &user, NULL);
2681642eb1aSMark Murray 	if (retval != PAM_SUCCESS)
26924fe7ba0SDag-Erling Smørgrav 		return (retval);
2701642eb1aSMark Murray 
2711642eb1aSMark Murray 	PAM_LOG("Got user: %s", user);
2721642eb1aSMark Murray 
273111ccd25SDag-Erling Smørgrav 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
2741642eb1aSMark Murray 	if (retval != PAM_SUCCESS)
27524fe7ba0SDag-Erling Smørgrav 		return (retval);
2761642eb1aSMark Murray 
2771642eb1aSMark Murray 	PAM_LOG("Got password");
2781642eb1aSMark Murray 
2791642eb1aSMark Murray 	radh = rad_open();
2801642eb1aSMark Murray 	if (radh == NULL) {
2819a10bb17SJohn Polstra 		syslog(LOG_CRIT, "rad_open failed");
28224fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2839a10bb17SJohn Polstra 	}
2841642eb1aSMark Murray 
2851642eb1aSMark Murray 	PAM_LOG("Radius opened");
2861642eb1aSMark Murray 
2879a10bb17SJohn Polstra 	if (rad_config(radh, conf_file) == -1) {
2889a10bb17SJohn Polstra 		syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
2899a10bb17SJohn Polstra 		rad_close(radh);
29024fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2919a10bb17SJohn Polstra 	}
2921642eb1aSMark Murray 
2931642eb1aSMark Murray 	PAM_LOG("Radius config file read");
2941642eb1aSMark Murray 
295f8b83996SDag-Erling Smørgrav 	if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, rhost,
296f8b83996SDag-Erling Smørgrav 	    NULL, 0) == -1) {
2979a10bb17SJohn Polstra 		rad_close(radh);
29824fe7ba0SDag-Erling Smørgrav 		return (PAM_SERVICE_ERR);
2999a10bb17SJohn Polstra 	}
3001642eb1aSMark Murray 
3011642eb1aSMark Murray 	PAM_LOG("Radius build access done");
3021642eb1aSMark Murray 
3039a10bb17SJohn Polstra 	for (;;) {
3049a10bb17SJohn Polstra 		switch (rad_send_request(radh)) {
3059a10bb17SJohn Polstra 
3069a10bb17SJohn Polstra 		case RAD_ACCESS_ACCEPT:
3079a10bb17SJohn Polstra 			e = do_accept(pamh, radh);
3089a10bb17SJohn Polstra 			rad_close(radh);
3099a10bb17SJohn Polstra 			if (e == -1)
31024fe7ba0SDag-Erling Smørgrav 				return (PAM_SERVICE_ERR);
3119a10bb17SJohn Polstra 			if (template_user != NULL) {
3121642eb1aSMark Murray 
3131642eb1aSMark Murray 				PAM_LOG("Trying template user: %s",
3141642eb1aSMark Murray 				    template_user);
3159a10bb17SJohn Polstra 
3169a10bb17SJohn Polstra 				/*
3179a10bb17SJohn Polstra 				 * If the given user name doesn't exist in
3189a10bb17SJohn Polstra 				 * the local password database, change it
3199a10bb17SJohn Polstra 				 * to the value given in the "template_user"
3209a10bb17SJohn Polstra 				 * option.
3219a10bb17SJohn Polstra 				 */
32291e93869SDag-Erling Smørgrav 				retval = pam_get_item(pamh, PAM_USER, &tmpuser);
3239a10bb17SJohn Polstra 				if (retval != PAM_SUCCESS)
32424fe7ba0SDag-Erling Smørgrav 					return (retval);
3251642eb1aSMark Murray 				if (getpwnam(tmpuser) == NULL) {
3269a10bb17SJohn Polstra 					pam_set_item(pamh, PAM_USER,
3279a10bb17SJohn Polstra 					    template_user);
3281642eb1aSMark Murray 					PAM_LOG("Using template user");
3299a10bb17SJohn Polstra 				}
3301642eb1aSMark Murray 
3311642eb1aSMark Murray 			}
33224fe7ba0SDag-Erling Smørgrav 			return (PAM_SUCCESS);
3339a10bb17SJohn Polstra 
3349a10bb17SJohn Polstra 		case RAD_ACCESS_REJECT:
3359a10bb17SJohn Polstra 			rad_close(radh);
33665550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius rejection");
33724fe7ba0SDag-Erling Smørgrav 			return (PAM_AUTH_ERR);
3389a10bb17SJohn Polstra 
3399a10bb17SJohn Polstra 		case RAD_ACCESS_CHALLENGE:
340d154a420SPawel Jakub Dawidek 			retval = do_challenge(pamh, radh, user, nas_id,
341f8b83996SDag-Erling Smørgrav 			    nas_ipaddr, rhost);
3421642eb1aSMark Murray 			if (retval != PAM_SUCCESS) {
3439a10bb17SJohn Polstra 				rad_close(radh);
34424fe7ba0SDag-Erling Smørgrav 				return (retval);
3459a10bb17SJohn Polstra 			}
3469a10bb17SJohn Polstra 			break;
3479a10bb17SJohn Polstra 
3489a10bb17SJohn Polstra 		case -1:
3499a10bb17SJohn Polstra 			syslog(LOG_CRIT, "rad_send_request: %s",
3509a10bb17SJohn Polstra 			    rad_strerror(radh));
3519a10bb17SJohn Polstra 			rad_close(radh);
35265550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius failure");
35324fe7ba0SDag-Erling Smørgrav 			return (PAM_AUTHINFO_UNAVAIL);
3549a10bb17SJohn Polstra 
3559a10bb17SJohn Polstra 		default:
3569a10bb17SJohn Polstra 			syslog(LOG_CRIT,
3579a10bb17SJohn Polstra 			    "rad_send_request: unexpected return value");
3589a10bb17SJohn Polstra 			rad_close(radh);
35965550d9bSMark Murray 			PAM_VERBOSE_ERROR("Radius error");
36024fe7ba0SDag-Erling Smørgrav 			return (PAM_SERVICE_ERR);
3619a10bb17SJohn Polstra 		}
3629a10bb17SJohn Polstra 	}
3639a10bb17SJohn Polstra }
3649a10bb17SJohn Polstra 
3659a10bb17SJohn Polstra PAM_EXTERN int
36624fe7ba0SDag-Erling Smørgrav pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
36724fe7ba0SDag-Erling Smørgrav     int argc __unused, const char *argv[] __unused)
3689a10bb17SJohn Polstra {
36965550d9bSMark Murray 
37024fe7ba0SDag-Erling Smørgrav 	return (PAM_SUCCESS);
3718d3978c1SDag-Erling Smørgrav }
3728d3978c1SDag-Erling Smørgrav 
3739294327dSJohn Polstra PAM_MODULE_ENTRY("pam_radius");
374