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