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