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