2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 1998 Juniper Networks, Inc.
6 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
8 * Copyright (c) 2015-2018 The University of Oslo
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.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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
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
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/socket.h>
56 #include <security/pam_appl.h>
57 #include <security/pam_modules.h>
58 #include <security/pam_mod_misc.h>
60 #define PAM_OPT_CONF "conf"
61 #define PAM_OPT_TEMPLATE_USER "template_user"
62 #define PAM_OPT_NAS_ID "nas_id"
63 #define PAM_OPT_NAS_IPADDR "nas_ipaddr"
64 #define PAM_OPT_NO_REPLYMSG "no_reply_message"
66 #define MAX_CHALLENGE_MSGS 10
67 #define PASSWORD_PROMPT "RADIUS Password:"
69 static int build_access_request(struct rad_handle *, const char *,
70 const char *, const char *, const char *, const char *,
71 const void *, size_t);
72 static int do_accept(pam_handle_t *, struct rad_handle *);
73 static int do_challenge(pam_handle_t *, struct rad_handle *,
74 const char *, const char *, const char *, const char *);
77 * Construct an access request, but don't send it. Returns 0 on success,
81 build_access_request(struct rad_handle *radh, const char *user,
82 const char *pass, const char *nas_id, const char *nas_ipaddr,
83 const char *rhost, const void *state, size_t state_len)
86 char host[MAXHOSTNAMELEN];
87 struct sockaddr_in *haddr;
88 struct addrinfo hints;
91 if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
92 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
96 (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
97 if (gethostname(host, sizeof host) != -1) {
100 if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
105 rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
107 rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
109 rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
110 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
113 if (nas_ipaddr != NULL) {
114 memset(&hints, 0, sizeof(hints));
115 hints.ai_family = AF_INET;
116 if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
117 res != NULL && res->ai_family == AF_INET) {
118 haddr = (struct sockaddr_in *)res->ai_addr;
119 error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
123 syslog(LOG_CRIT, "rad_put_addr: %s",
130 rad_put_string(radh, RAD_CALLING_STATION_ID, rhost) == -1) {
131 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
135 rad_put_attr(radh, RAD_STATE, state, state_len) == -1) {
136 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
139 if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
140 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
147 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
154 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
157 if ((s = rad_cvt_string(attrval, attrlen)) == NULL)
159 pam_set_item(pamh, PAM_USER, s);
162 case RAD_REPLY_MESSAGE:
163 if ((s = rad_cvt_string(attrval, attrlen)) == NULL)
165 if (!openpam_get_option(pamh, PAM_OPT_NO_REPLYMSG))
166 pam_info(pamh, "%s", s);
170 PAM_LOG("%s(): ignoring RADIUS attribute %d",
174 if (attrtype == -1) {
175 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
180 syslog(LOG_CRIT, "%s(): out of memory", __func__);
185 do_reject(pam_handle_t *pamh, struct rad_handle *radh)
192 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
194 case RAD_REPLY_MESSAGE:
195 if ((s = rad_cvt_string(attrval, attrlen)) == NULL)
197 if (!openpam_get_option(pamh, PAM_OPT_NO_REPLYMSG))
198 pam_error(pamh, "%s", s);
202 PAM_LOG("%s(): ignoring RADIUS attribute %d",
207 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
212 syslog(LOG_CRIT, "%s(): out of memory", __func__);
217 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user,
218 const char *nas_id, const char *nas_ipaddr, const char *rhost)
226 struct pam_message msgs[MAX_CHALLENGE_MSGS];
227 const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
228 struct pam_response *resp;
231 const struct pam_conv *conv;
236 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
244 case RAD_REPLY_MESSAGE:
245 if (num_msgs >= MAX_CHALLENGE_MSGS) {
247 "Too many RADIUS challenge messages");
248 return (PAM_SERVICE_ERR);
250 msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
251 if (msgs[num_msgs].msg == NULL) {
253 "rad_cvt_string: out of memory");
254 return (PAM_SERVICE_ERR);
256 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
257 msg_ptrs[num_msgs] = &msgs[num_msgs];
262 if (attrtype == -1) {
263 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
264 return (PAM_SERVICE_ERR);
267 msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
268 if (msgs[num_msgs].msg == NULL) {
269 syslog(LOG_CRIT, "Out of memory");
270 return (PAM_SERVICE_ERR);
272 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
273 msg_ptrs[num_msgs] = &msgs[num_msgs];
276 msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
277 if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
278 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
281 conv = (const struct pam_conv *)item;
282 if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
283 conv->appdata_ptr)) != PAM_SUCCESS)
285 if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id,
286 nas_ipaddr, rhost, state, statelen) == -1)
287 return (PAM_SERVICE_ERR);
288 memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
289 free(resp[num_msgs-1].resp);
292 free(msgs[--num_msgs].msg);
293 return (PAM_SUCCESS);
297 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
298 int argc __unused, const char *argv[] __unused)
300 struct rad_handle *radh;
301 const char *user, *pass;
302 const void *rhost, *tmpuser;
303 const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
307 conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
308 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
309 nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
310 nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
311 pam_get_item(pamh, PAM_RHOST, &rhost);
313 retval = pam_get_user(pamh, &user, NULL);
314 if (retval != PAM_SUCCESS)
317 PAM_LOG("Got user: %s", user);
319 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
320 if (retval != PAM_SUCCESS)
323 PAM_LOG("Got password");
327 syslog(LOG_CRIT, "rad_open failed");
328 return (PAM_SERVICE_ERR);
331 PAM_LOG("Radius opened");
333 if (rad_config(radh, conf_file) == -1) {
334 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
336 return (PAM_SERVICE_ERR);
339 PAM_LOG("Radius config file read");
341 if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, rhost,
344 return (PAM_SERVICE_ERR);
347 PAM_LOG("Radius build access done");
350 switch (rad_send_request(radh)) {
352 case RAD_ACCESS_ACCEPT:
353 e = do_accept(pamh, radh);
356 return (PAM_SERVICE_ERR);
357 if (template_user != NULL) {
359 PAM_LOG("Trying template user: %s",
363 * If the given user name doesn't exist in
364 * the local password database, change it
365 * to the value given in the "template_user"
368 retval = pam_get_item(pamh, PAM_USER, &tmpuser);
369 if (retval != PAM_SUCCESS)
371 if (getpwnam(tmpuser) == NULL) {
372 pam_set_item(pamh, PAM_USER,
374 PAM_LOG("Using template user");
378 return (PAM_SUCCESS);
380 case RAD_ACCESS_REJECT:
381 retval = do_reject(pamh, radh);
383 PAM_VERBOSE_ERROR("Radius rejection");
384 return (PAM_AUTH_ERR);
386 case RAD_ACCESS_CHALLENGE:
387 retval = do_challenge(pamh, radh, user, nas_id,
389 if (retval != PAM_SUCCESS) {
396 syslog(LOG_CRIT, "rad_send_request: %s",
399 PAM_VERBOSE_ERROR("Radius failure");
400 return (PAM_AUTHINFO_UNAVAIL);
404 "rad_send_request: unexpected return value");
406 PAM_VERBOSE_ERROR("Radius error");
407 return (PAM_SERVICE_ERR);
413 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
414 int argc __unused, const char *argv[] __unused)
417 return (PAM_SUCCESS);
420 PAM_MODULE_ENTRY("pam_radius");