xref: /freebsd/lib/libpam/modules/pam_radius/pam_radius.c (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
1 /*-
2  * Copyright 1998 Juniper Networks, Inc.
3  * All rights reserved.
4  * Copyright (c) 2001-2003 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 <sys/socket.h>
42 #include <netdb.h>
43 #include <pwd.h>
44 #include <radlib.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <unistd.h>
49 
50 #define PAM_SM_AUTH
51 
52 #include <security/pam_appl.h>
53 #include <security/pam_modules.h>
54 #include <security/pam_mod_misc.h>
55 
56 #define PAM_OPT_CONF		"conf"
57 #define PAM_OPT_TEMPLATE_USER	"template_user"
58 #define PAM_OPT_NAS_ID		"nas_id"
59 #define PAM_OPT_NAS_IPADDR	"nas_ipaddr"
60 
61 #define	MAX_CHALLENGE_MSGS	10
62 #define	PASSWORD_PROMPT		"RADIUS Password:"
63 
64 static int	 build_access_request(struct rad_handle *, const char *,
65 		    const char *, const char *, const char *, const char *,
66 		    const void *, size_t);
67 static int	 do_accept(pam_handle_t *, struct rad_handle *);
68 static int	 do_challenge(pam_handle_t *, struct rad_handle *,
69 		    const char *, const char *, const char *, const char *);
70 
71 /*
72  * Construct an access request, but don't send it.  Returns 0 on success,
73  * -1 on failure.
74  */
75 static int
76 build_access_request(struct rad_handle *radh, const char *user,
77     const char *pass, const char *nas_id, const char *nas_ipaddr,
78     const char *rhost, const void *state, size_t state_len)
79 {
80 	int error;
81 	char host[MAXHOSTNAMELEN];
82 	struct sockaddr_in *haddr;
83 	struct addrinfo hints;
84 	struct addrinfo *res;
85 
86 	if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
87 		syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
88 		return (-1);
89 	}
90 	if (nas_id == NULL ||
91 	    (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
92 		if (gethostname(host, sizeof host) != -1) {
93 			if (nas_id == NULL)
94 				nas_id = host;
95 			if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
96 				nas_ipaddr = host;
97 		}
98 	}
99 	if ((user != NULL &&
100 	    rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
101 	    (pass != NULL &&
102 	    rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
103 	    (nas_id != NULL &&
104 	    rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
105 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
106 		return (-1);
107 	}
108 	if (nas_ipaddr != NULL) {
109 		memset(&hints, 0, sizeof(hints));
110 		hints.ai_family = AF_INET;
111 		if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
112 		    res != NULL && res->ai_family == AF_INET) {
113 			haddr = (struct sockaddr_in *)res->ai_addr;
114 			error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
115 			    haddr->sin_addr);
116 			freeaddrinfo(res);
117 			if (error == -1) {
118 				syslog(LOG_CRIT, "rad_put_addr: %s",
119 				    rad_strerror(radh));
120 				return (-1);
121 			}
122 		}
123 	}
124 	if (rhost != NULL &&
125 	    rad_put_string(radh, RAD_CALLING_STATION_ID, rhost) == -1) {
126 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
127 		return (-1);
128 	}
129 	if (state != NULL &&
130 	    rad_put_attr(radh, RAD_STATE, state, state_len) == -1) {
131 		syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
132 		return (-1);
133 	}
134 	if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
135 		syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
136 		return (-1);
137 	}
138 	return (0);
139 }
140 
141 static int
142 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
143 {
144 	int attrtype;
145 	const void *attrval;
146 	size_t attrlen;
147 	char *s;
148 
149 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
150 		if (attrtype == RAD_USER_NAME) {
151 			s = rad_cvt_string(attrval, attrlen);
152 			if (s == NULL) {
153 				syslog(LOG_CRIT,
154 				    "rad_cvt_string: out of memory");
155 				return (-1);
156 			}
157 			pam_set_item(pamh, PAM_USER, s);
158 			free(s);
159 		}
160 	}
161 	if (attrtype == -1) {
162 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
163 		return (-1);
164 	}
165 	return (0);
166 }
167 
168 static int
169 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user,
170     const char *nas_id, const char *nas_ipaddr, const char *rhost)
171 {
172 	int retval;
173 	int attrtype;
174 	const void *attrval;
175 	size_t attrlen;
176 	const void *state;
177 	size_t statelen;
178 	struct pam_message msgs[MAX_CHALLENGE_MSGS];
179 	const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
180 	struct pam_response *resp;
181 	int num_msgs;
182 	const void *item;
183 	const struct pam_conv *conv;
184 
185 	state = NULL;
186 	statelen = 0;
187 	num_msgs = 0;
188 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
189 		switch (attrtype) {
190 
191 		case RAD_STATE:
192 			state = attrval;
193 			statelen = attrlen;
194 			break;
195 
196 		case RAD_REPLY_MESSAGE:
197 			if (num_msgs >= MAX_CHALLENGE_MSGS) {
198 				syslog(LOG_CRIT,
199 				    "Too many RADIUS challenge messages");
200 				return (PAM_SERVICE_ERR);
201 			}
202 			msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
203 			if (msgs[num_msgs].msg == NULL) {
204 				syslog(LOG_CRIT,
205 				    "rad_cvt_string: out of memory");
206 				return (PAM_SERVICE_ERR);
207 			}
208 			msgs[num_msgs].msg_style = PAM_TEXT_INFO;
209 			msg_ptrs[num_msgs] = &msgs[num_msgs];
210 			num_msgs++;
211 			break;
212 		}
213 	}
214 	if (attrtype == -1) {
215 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
216 		return (PAM_SERVICE_ERR);
217 	}
218 	if (num_msgs == 0) {
219 		msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
220 		if (msgs[num_msgs].msg == NULL) {
221 			syslog(LOG_CRIT, "Out of memory");
222 			return (PAM_SERVICE_ERR);
223 		}
224 		msgs[num_msgs].msg_style = PAM_TEXT_INFO;
225 		msg_ptrs[num_msgs] = &msgs[num_msgs];
226 		num_msgs++;
227 	}
228 	msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
229 	if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
230 		syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
231 		return (retval);
232 	}
233 	conv = (const struct pam_conv *)item;
234 	if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
235 	    conv->appdata_ptr)) != PAM_SUCCESS)
236 		return (retval);
237 	if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id,
238 	    nas_ipaddr, rhost, state, statelen) == -1)
239 		return (PAM_SERVICE_ERR);
240 	memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
241 	free(resp[num_msgs-1].resp);
242 	free(resp);
243 	while (num_msgs > 0)
244 		free(msgs[--num_msgs].msg);
245 	return (PAM_SUCCESS);
246 }
247 
248 PAM_EXTERN int
249 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
250     int argc __unused, const char *argv[] __unused)
251 {
252 	struct rad_handle *radh;
253 	const char *user, *pass;
254 	const void *rhost, *tmpuser;
255 	const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
256 	int retval;
257 	int e;
258 
259 	conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
260 	template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
261 	nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
262 	nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
263 	pam_get_item(pamh, PAM_RHOST, &rhost);
264 
265 	retval = pam_get_user(pamh, &user, NULL);
266 	if (retval != PAM_SUCCESS)
267 		return (retval);
268 
269 	PAM_LOG("Got user: %s", user);
270 
271 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
272 	if (retval != PAM_SUCCESS)
273 		return (retval);
274 
275 	PAM_LOG("Got password");
276 
277 	radh = rad_open();
278 	if (radh == NULL) {
279 		syslog(LOG_CRIT, "rad_open failed");
280 		return (PAM_SERVICE_ERR);
281 	}
282 
283 	PAM_LOG("Radius opened");
284 
285 	if (rad_config(radh, conf_file) == -1) {
286 		syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
287 		rad_close(radh);
288 		return (PAM_SERVICE_ERR);
289 	}
290 
291 	PAM_LOG("Radius config file read");
292 
293 	if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, rhost,
294 	    NULL, 0) == -1) {
295 		rad_close(radh);
296 		return (PAM_SERVICE_ERR);
297 	}
298 
299 	PAM_LOG("Radius build access done");
300 
301 	for (;;) {
302 		switch (rad_send_request(radh)) {
303 
304 		case RAD_ACCESS_ACCEPT:
305 			e = do_accept(pamh, radh);
306 			rad_close(radh);
307 			if (e == -1)
308 				return (PAM_SERVICE_ERR);
309 			if (template_user != NULL) {
310 
311 				PAM_LOG("Trying template user: %s",
312 				    template_user);
313 
314 				/*
315 				 * If the given user name doesn't exist in
316 				 * the local password database, change it
317 				 * to the value given in the "template_user"
318 				 * option.
319 				 */
320 				retval = pam_get_item(pamh, PAM_USER, &tmpuser);
321 				if (retval != PAM_SUCCESS)
322 					return (retval);
323 				if (getpwnam(tmpuser) == NULL) {
324 					pam_set_item(pamh, PAM_USER,
325 					    template_user);
326 					PAM_LOG("Using template user");
327 				}
328 
329 			}
330 			return (PAM_SUCCESS);
331 
332 		case RAD_ACCESS_REJECT:
333 			rad_close(radh);
334 			PAM_VERBOSE_ERROR("Radius rejection");
335 			return (PAM_AUTH_ERR);
336 
337 		case RAD_ACCESS_CHALLENGE:
338 			retval = do_challenge(pamh, radh, user, nas_id,
339 			    nas_ipaddr, rhost);
340 			if (retval != PAM_SUCCESS) {
341 				rad_close(radh);
342 				return (retval);
343 			}
344 			break;
345 
346 		case -1:
347 			syslog(LOG_CRIT, "rad_send_request: %s",
348 			    rad_strerror(radh));
349 			rad_close(radh);
350 			PAM_VERBOSE_ERROR("Radius failure");
351 			return (PAM_AUTHINFO_UNAVAIL);
352 
353 		default:
354 			syslog(LOG_CRIT,
355 			    "rad_send_request: unexpected return value");
356 			rad_close(radh);
357 			PAM_VERBOSE_ERROR("Radius error");
358 			return (PAM_SERVICE_ERR);
359 		}
360 	}
361 }
362 
363 PAM_EXTERN int
364 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
365     int argc __unused, const char *argv[] __unused)
366 {
367 
368 	return (PAM_SUCCESS);
369 }
370 
371 PAM_MODULE_ENTRY("pam_radius");
372