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