xref: /netbsd-src/lib/libpam/modules/pam_radius/pam_radius.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: pam_radius.c,v 1.2 2004/12/12 08:18:46 christos Exp $	*/
2 
3 /*-
4  * Copyright 1998 Juniper Networks, Inc.
5  * All rights reserved.
6  * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
7  * All rights reserved.
8  *
9  * Portions of this software were developed for the FreeBSD Project by
10  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12  * ("CBOSS"), as part of the DARPA CHATS research program.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. The name of the author may not be used to endorse or promote
23  *    products derived from this software without specific prior written
24  *    permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_radius/pam_radius.c,v 1.22 2004/06/25 12:32:45 kan Exp $");
42 #else
43 __RCSID("$NetBSD: pam_radius.c,v 1.2 2004/12/12 08:18:46 christos Exp $");
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netdb.h>
50 #include <pwd.h>
51 #include <radlib.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 
57 #define PAM_SM_AUTH
58 
59 #include <security/pam_appl.h>
60 #include <security/pam_modules.h>
61 #include <security/pam_mod_misc.h>
62 
63 #define PAM_OPT_CONF		"conf"
64 #define PAM_OPT_TEMPLATE_USER	"template_user"
65 #define PAM_OPT_NAS_ID		"nas_id"
66 #define PAM_OPT_NAS_IPADDR	"nas_ipaddr"
67 
68 #define	MAX_CHALLENGE_MSGS	10
69 #define	PASSWORD_PROMPT		"RADIUS Password:"
70 
71 static int	 build_access_request(struct rad_handle *, const char *,
72 		    const char *, const char *, const char *, const void *,
73 		    size_t);
74 static int	 do_accept(pam_handle_t *, struct rad_handle *);
75 static int	 do_challenge(pam_handle_t *, struct rad_handle *,
76 		    const char *);
77 
78 /*
79  * Construct an access request, but don't send it.  Returns 0 on success,
80  * -1 on failure.
81  */
82 static int
83 build_access_request(struct rad_handle *radh, const char *user,
84     const char *pass, const char *nas_id, const char *nas_ipaddr,
85     const void *state, size_t state_len)
86 {
87 	int error;
88 	char host[MAXHOSTNAMELEN];
89 	struct sockaddr_in *haddr;
90 	struct addrinfo hints;
91 	struct addrinfo *res;
92 
93 	if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
94 		syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
95 		return (-1);
96 	}
97 	if (nas_id == NULL ||
98 	    (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
99 		if (gethostname(host, sizeof host) != -1) {
100 			if (nas_id == NULL)
101 				nas_id = host;
102 			if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
103 				nas_ipaddr = host;
104 		}
105 	}
106 	if ((user != NULL &&
107 	    rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
108 	    (pass != NULL &&
109 	    rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
110 	    (nas_id != NULL &&
111 	    rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
112 		syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
113 		return (-1);
114 	}
115 	if (nas_ipaddr != NULL) {
116 		memset(&hints, 0, sizeof(hints));
117 		hints.ai_family = PF_INET;
118 		if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
119 		    res != NULL) {
120 			(struct sockaddr *)haddr = res->ai_addr;
121 			error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
122 			    haddr->sin_addr);
123 			freeaddrinfo(res);
124 			if (error == -1) {
125 				syslog(LOG_CRIT, "rad_put_addr: %s",
126 				    rad_strerror(radh));
127 				return (-1);
128 			}
129 		}
130 	}
131 	if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
132 	    state_len) == -1) {
133 		syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
134 		return (-1);
135 	}
136 	if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
137 		syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
138 		return (-1);
139 	}
140 	return (0);
141 }
142 
143 static int
144 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
145 {
146 	int attrtype;
147 	const void *attrval;
148 	size_t attrlen;
149 	char *s;
150 
151 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
152 		if (attrtype == RAD_USER_NAME) {
153 			s = rad_cvt_string(attrval, attrlen);
154 			if (s == NULL) {
155 				syslog(LOG_CRIT,
156 				    "rad_cvt_string: out of memory");
157 				return (-1);
158 			}
159 			pam_set_item(pamh, PAM_USER, s);
160 			free(s);
161 		}
162 	}
163 	if (attrtype == -1) {
164 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
165 		return (-1);
166 	}
167 	return (0);
168 }
169 
170 static int
171 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user)
172 {
173 	int retval;
174 	int attrtype;
175 	const void *attrval;
176 	size_t attrlen;
177 	const void *state;
178 	size_t statelen;
179 	struct pam_message msgs[MAX_CHALLENGE_MSGS];
180 	const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
181 	struct pam_response *resp;
182 	int num_msgs;
183 	const void *item;
184 	const struct pam_conv *conv;
185 
186 	state = NULL;
187 	statelen = 0;
188 	num_msgs = 0;
189 	while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
190 		switch (attrtype) {
191 
192 		case RAD_STATE:
193 			state = attrval;
194 			statelen = attrlen;
195 			break;
196 
197 		case RAD_REPLY_MESSAGE:
198 			if (num_msgs >= MAX_CHALLENGE_MSGS) {
199 				syslog(LOG_CRIT,
200 				    "Too many RADIUS challenge messages");
201 				return (PAM_SERVICE_ERR);
202 			}
203 			msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
204 			if (msgs[num_msgs].msg == NULL) {
205 				syslog(LOG_CRIT,
206 				    "rad_cvt_string: out of memory");
207 				return (PAM_SERVICE_ERR);
208 			}
209 			msgs[num_msgs].msg_style = PAM_TEXT_INFO;
210 			msg_ptrs[num_msgs] = &msgs[num_msgs];
211 			num_msgs++;
212 			break;
213 		}
214 	}
215 	if (attrtype == -1) {
216 		syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
217 		return (PAM_SERVICE_ERR);
218 	}
219 	if (num_msgs == 0) {
220 		msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
221 		if (msgs[num_msgs].msg == NULL) {
222 			syslog(LOG_CRIT, "Out of memory");
223 			return (PAM_SERVICE_ERR);
224 		}
225 		msgs[num_msgs].msg_style = PAM_TEXT_INFO;
226 		msg_ptrs[num_msgs] = &msgs[num_msgs];
227 		num_msgs++;
228 	}
229 	msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
230 	if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
231 		syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
232 		return (retval);
233 	}
234 	conv = (const struct pam_conv *)item;
235 	if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
236 	    conv->appdata_ptr)) != PAM_SUCCESS)
237 		return (retval);
238 	if (build_access_request(radh, user, resp[num_msgs-1].resp, NULL,
239 	    NULL, state, statelen) == -1)
240 		return (PAM_SERVICE_ERR);
241 	memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
242 	free(resp[num_msgs-1].resp);
243 	free(resp);
244 	while (num_msgs > 0)
245 		free(msgs[--num_msgs].msg);
246 	return (PAM_SUCCESS);
247 }
248 
249 PAM_EXTERN int
250 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
251     int argc __unused, const char *argv[] __unused)
252 {
253 	struct rad_handle *radh;
254 	const char *user, *pass;
255 	const void *tmpuser;
256 	const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
257 	int retval;
258 	int e;
259 
260 	conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
261 	template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
262 	nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
263 	nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
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, NULL,
294 	    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);
339 			if (retval != PAM_SUCCESS) {
340 				rad_close(radh);
341 				return (retval);
342 			}
343 			break;
344 
345 		case -1:
346 			syslog(LOG_CRIT, "rad_send_request: %s",
347 			    rad_strerror(radh));
348 			rad_close(radh);
349 			PAM_VERBOSE_ERROR("Radius failure");
350 			return (PAM_AUTHINFO_UNAVAIL);
351 
352 		default:
353 			syslog(LOG_CRIT,
354 			    "rad_send_request: unexpected return value");
355 			rad_close(radh);
356 			PAM_VERBOSE_ERROR("Radius error");
357 			return (PAM_SERVICE_ERR);
358 		}
359 	}
360 }
361 
362 PAM_EXTERN int
363 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
364     int argc __unused, const char *argv[] __unused)
365 {
366 
367 	return (PAM_SUCCESS);
368 }
369 
370 PAM_MODULE_ENTRY("pam_radius");
371