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