pam_radius.c revision 1.2.2.3 1 /* $NetBSD: pam_radius.c,v 1.2.2.3 2007/01/05 14:14:53 tron 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.2.3 2007/01/05 14:14:53 tron 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 haddr = (struct sockaddr_in *)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 struct passwd *pwd, pwres;
257 char pwbuf[1024];
258 const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
259 int retval;
260 int e;
261
262 conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
263 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
264 nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
265 nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
266
267 retval = pam_get_user(pamh, &user, NULL);
268 if (retval != PAM_SUCCESS)
269 return (retval);
270
271 PAM_LOG("Got user: %s", user);
272
273 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
274 if (retval != PAM_SUCCESS)
275 return (retval);
276
277 PAM_LOG("Got password");
278
279 radh = rad_open();
280 if (radh == NULL) {
281 syslog(LOG_CRIT, "rad_open failed");
282 return (PAM_SERVICE_ERR);
283 }
284
285 PAM_LOG("Radius opened");
286
287 if (rad_config(radh, conf_file) == -1) {
288 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
289 rad_close(radh);
290 return (PAM_SERVICE_ERR);
291 }
292
293 PAM_LOG("Radius config file read");
294
295 if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, NULL,
296 0) == -1) {
297 rad_close(radh);
298 return (PAM_SERVICE_ERR);
299 }
300
301 PAM_LOG("Radius build access done");
302
303 for (;;) {
304 switch (rad_send_request(radh)) {
305
306 case RAD_ACCESS_ACCEPT:
307 e = do_accept(pamh, radh);
308 rad_close(radh);
309 if (e == -1)
310 return (PAM_SERVICE_ERR);
311 if (template_user != NULL) {
312
313 PAM_LOG("Trying template user: %s",
314 template_user);
315
316 /*
317 * If the given user name doesn't exist in
318 * the local password database, change it
319 * to the value given in the "template_user"
320 * option.
321 */
322 retval = pam_get_item(pamh, PAM_USER, &tmpuser);
323 if (retval != PAM_SUCCESS)
324 return (retval);
325 if (getpwnam_r(tmpuser, &pwres, pwbuf,
326 sizeof(pwbuf), &pwd) != 0 ||
327 pwd == NULL) {
328 pam_set_item(pamh, PAM_USER,
329 template_user);
330 PAM_LOG("Using template user");
331 }
332
333 }
334 return (PAM_SUCCESS);
335
336 case RAD_ACCESS_REJECT:
337 rad_close(radh);
338 PAM_VERBOSE_ERROR("Radius rejection");
339 return (PAM_AUTH_ERR);
340
341 case RAD_ACCESS_CHALLENGE:
342 retval = do_challenge(pamh, radh, user);
343 if (retval != PAM_SUCCESS) {
344 rad_close(radh);
345 return (retval);
346 }
347 break;
348
349 case -1:
350 syslog(LOG_CRIT, "rad_send_request: %s",
351 rad_strerror(radh));
352 rad_close(radh);
353 PAM_VERBOSE_ERROR("Radius failure");
354 return (PAM_AUTHINFO_UNAVAIL);
355
356 default:
357 syslog(LOG_CRIT,
358 "rad_send_request: unexpected return value");
359 rad_close(radh);
360 PAM_VERBOSE_ERROR("Radius error");
361 return (PAM_SERVICE_ERR);
362 }
363 }
364 }
365
366 PAM_EXTERN int
367 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
368 int argc __unused, const char *argv[] __unused)
369 {
370
371 return (PAM_SUCCESS);
372 }
373
374 PAM_MODULE_ENTRY("pam_radius");
375