pam_radius.c revision 90229
1/*-
2 * Copyright 1998 Juniper Networks, Inc.
3 * All rights reserved.
4 * Copyright (c) 2001 Networks Associates Technologies, 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: head/lib/libpam/modules/pam_radius/pam_radius.c 90229 2002-02-05 06:08:26Z des $");
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#define PAM_SM_ACCOUNT
50#define PAM_SM_SESSION
51#define PAM_SM_PASSWORD
52
53#include <security/pam_appl.h>
54#include <security/pam_modules.h>
55#include <security/pam_mod_misc.h>
56
57enum { PAM_OPT_CONF=PAM_OPT_STD_MAX, PAM_OPT_TEMPLATE_USER };
58
59static 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
68static int	 build_access_request(struct rad_handle *, const char *,
69		    const char *, const void *, size_t);
70static int	 do_accept(pam_handle_t *, struct rad_handle *);
71static 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 */
78static int
79build_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
109static int
110do_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
136static int
137do_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
215PAM_EXTERN int
216pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, int argc, const char **argv)
217{
218	struct options options;
219	struct rad_handle *radh;
220	const char *user, *tmpuser, *pass;
221	char *conf_file, *template_user;
222	int retval;
223	int e;
224
225	pam_std_option(&options, other_options, argc, argv);
226
227	PAM_LOG("Options processed");
228
229	conf_file = NULL;
230	pam_test_option(&options, PAM_OPT_CONF, &conf_file);
231	template_user = NULL;
232	pam_test_option(&options, PAM_OPT_TEMPLATE_USER, &template_user);
233
234	retval = pam_get_user(pamh, &user, NULL);
235	if (retval != PAM_SUCCESS)
236		PAM_RETURN(retval);
237
238	PAM_LOG("Got user: %s", user);
239
240	retval = pam_get_pass(pamh, &pass, PASSWORD_PROMPT, &options);
241	if (retval != PAM_SUCCESS)
242		PAM_RETURN(retval);
243
244	PAM_LOG("Got password");
245
246	radh = rad_open();
247	if (radh == NULL) {
248		syslog(LOG_CRIT, "rad_open failed");
249		PAM_RETURN(PAM_SERVICE_ERR);
250	}
251
252	PAM_LOG("Radius opened");
253
254	if (rad_config(radh, conf_file) == -1) {
255		syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
256		rad_close(radh);
257		PAM_RETURN(PAM_SERVICE_ERR);
258	}
259
260	PAM_LOG("Radius config file read");
261
262	if (build_access_request(radh, user, pass, NULL, 0) == -1) {
263		rad_close(radh);
264		PAM_RETURN(PAM_SERVICE_ERR);
265	}
266
267	PAM_LOG("Radius build access done");
268
269	for (;;) {
270		switch (rad_send_request(radh)) {
271
272		case RAD_ACCESS_ACCEPT:
273			e = do_accept(pamh, radh);
274			rad_close(radh);
275			if (e == -1)
276				PAM_RETURN(PAM_SERVICE_ERR);
277			if (template_user != NULL) {
278
279				PAM_LOG("Trying template user: %s",
280				    template_user);
281
282				/*
283				 * If the given user name doesn't exist in
284				 * the local password database, change it
285				 * to the value given in the "template_user"
286				 * option.
287				 */
288				retval = pam_get_item(pamh, PAM_USER, &tmpuser);
289				if (retval != PAM_SUCCESS)
290					PAM_RETURN(retval);
291				if (getpwnam(tmpuser) == NULL) {
292					pam_set_item(pamh, PAM_USER,
293					    template_user);
294					PAM_LOG("Using template user");
295				}
296
297			}
298			PAM_RETURN(PAM_SUCCESS);
299
300		case RAD_ACCESS_REJECT:
301			rad_close(radh);
302			PAM_VERBOSE_ERROR("Radius rejection");
303			PAM_RETURN(PAM_AUTH_ERR);
304
305		case RAD_ACCESS_CHALLENGE:
306			retval = do_challenge(pamh, radh, user);
307			if (retval != PAM_SUCCESS) {
308				rad_close(radh);
309				PAM_RETURN(retval);
310			}
311			break;
312
313		case -1:
314			syslog(LOG_CRIT, "rad_send_request: %s",
315			    rad_strerror(radh));
316			rad_close(radh);
317			PAM_VERBOSE_ERROR("Radius failure");
318			PAM_RETURN(PAM_AUTHINFO_UNAVAIL);
319
320		default:
321			syslog(LOG_CRIT,
322			    "rad_send_request: unexpected return value");
323			rad_close(radh);
324			PAM_VERBOSE_ERROR("Radius error");
325			PAM_RETURN(PAM_SERVICE_ERR);
326		}
327	}
328}
329
330PAM_EXTERN int
331pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
332{
333	struct options options;
334
335	pam_std_option(&options, NULL, argc, argv);
336
337	PAM_LOG("Options processed");
338
339	PAM_RETURN(PAM_SUCCESS);
340}
341
342PAM_EXTERN int
343pam_sm_acct_mgmt(pam_handle_t *pamh __unused, int flags __unused, int argc ,const char **argv)
344{
345	struct options options;
346
347	pam_std_option(&options, NULL, argc, argv);
348
349	PAM_LOG("Options processed");
350
351	PAM_RETURN(PAM_IGNORE);
352}
353
354PAM_EXTERN int
355pam_sm_chauthtok(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
356{
357	struct options options;
358
359	pam_std_option(&options, NULL, argc, argv);
360
361	PAM_LOG("Options processed");
362
363	PAM_RETURN(PAM_IGNORE);
364}
365
366PAM_EXTERN int
367pam_sm_open_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
368{
369	struct options options;
370
371	pam_std_option(&options, NULL, argc, argv);
372
373	PAM_LOG("Options processed");
374
375	PAM_RETURN(PAM_IGNORE);
376}
377
378PAM_EXTERN int
379pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
380{
381	struct options options;
382
383	pam_std_option(&options, NULL, argc, argv);
384
385	PAM_LOG("Options processed");
386
387	PAM_RETURN(PAM_IGNORE);
388}
389
390PAM_MODULE_ENTRY("pam_radius");
391