2 * Copyright 1998 Juniper Networks, Inc.
4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 * products derived from this software without specific prior written
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libpam/modules/pam_radius/pam_radius.c,v 1.23 2005/06/13 21:18:52 des Exp $
37 * $DragonFly: src/lib/pam_module/pam_radius/pam_radius.c,v 1.1 2005/07/12 23:13:26 joerg Exp $
40 #include <sys/param.h>
41 #include <sys/socket.h>
52 #include <security/pam_appl.h>
53 #include <security/pam_modules.h>
54 #include <security/pam_mod_misc.h>
56 #define PAM_OPT_CONF "conf"
57 #define PAM_OPT_TEMPLATE_USER "template_user"
58 #define PAM_OPT_NAS_ID "nas_id"
59 #define PAM_OPT_NAS_IPADDR "nas_ipaddr"
61 #define MAX_CHALLENGE_MSGS 10
62 #define PASSWORD_PROMPT "RADIUS Password:"
64 static int build_access_request(struct rad_handle *, const char *,
65 const char *, const char *, const char *, const void *,
67 static int do_accept(pam_handle_t *, struct rad_handle *);
68 static int do_challenge(pam_handle_t *, struct rad_handle *,
72 * Construct an access request, but don't send it. Returns 0 on success,
76 build_access_request(struct rad_handle *radh, const char *user,
77 const char *pass, const char *nas_id, const char *nas_ipaddr,
78 const void *state, size_t state_len)
81 char host[MAXHOSTNAMELEN];
82 struct sockaddr_in *haddr;
83 struct addrinfo hints;
86 if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
87 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
91 (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) {
92 if (gethostname(host, sizeof host) != -1) {
95 if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)
100 rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
102 rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
104 rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
105 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
108 if (nas_ipaddr != NULL) {
109 memset(&hints, 0, sizeof(hints));
110 hints.ai_family = AF_INET;
111 if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 &&
112 res != NULL && res->ai_family == AF_INET) {
113 haddr = (struct sockaddr_in *)res->ai_addr;
114 error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS,
118 syslog(LOG_CRIT, "rad_put_addr: %s",
124 if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
126 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
129 if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
130 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
137 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
144 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
145 if (attrtype == RAD_USER_NAME) {
146 s = rad_cvt_string(attrval, attrlen);
149 "rad_cvt_string: out of memory");
152 pam_set_item(pamh, PAM_USER, s);
156 if (attrtype == -1) {
157 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
164 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user)
172 struct pam_message msgs[MAX_CHALLENGE_MSGS];
173 const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
174 struct pam_response *resp;
177 const struct pam_conv *conv;
182 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
190 case RAD_REPLY_MESSAGE:
191 if (num_msgs >= MAX_CHALLENGE_MSGS) {
193 "Too many RADIUS challenge messages");
194 return (PAM_SERVICE_ERR);
196 msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
197 if (msgs[num_msgs].msg == NULL) {
199 "rad_cvt_string: out of memory");
200 return (PAM_SERVICE_ERR);
202 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
203 msg_ptrs[num_msgs] = &msgs[num_msgs];
208 if (attrtype == -1) {
209 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
210 return (PAM_SERVICE_ERR);
213 msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
214 if (msgs[num_msgs].msg == NULL) {
215 syslog(LOG_CRIT, "Out of memory");
216 return (PAM_SERVICE_ERR);
218 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
219 msg_ptrs[num_msgs] = &msgs[num_msgs];
222 msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
223 if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
224 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
227 conv = (const struct pam_conv *)item;
228 if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
229 conv->appdata_ptr)) != PAM_SUCCESS)
231 if (build_access_request(radh, user, resp[num_msgs-1].resp, NULL,
232 NULL, state, statelen) == -1)
233 return (PAM_SERVICE_ERR);
234 memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
235 free(resp[num_msgs-1].resp);
238 free(msgs[--num_msgs].msg);
239 return (PAM_SUCCESS);
243 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
244 int argc __unused, const char *argv[] __unused)
246 struct rad_handle *radh;
247 const char *user, *pass;
249 const char *conf_file, *template_user, *nas_id, *nas_ipaddr;
253 conf_file = openpam_get_option(pamh, PAM_OPT_CONF);
254 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER);
255 nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID);
256 nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR);
258 retval = pam_get_user(pamh, &user, NULL);
259 if (retval != PAM_SUCCESS)
262 PAM_LOG("Got user: %s", user);
264 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
265 if (retval != PAM_SUCCESS)
268 PAM_LOG("Got password");
272 syslog(LOG_CRIT, "rad_open failed");
273 return (PAM_SERVICE_ERR);
276 PAM_LOG("Radius opened");
278 if (rad_config(radh, conf_file) == -1) {
279 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
281 return (PAM_SERVICE_ERR);
284 PAM_LOG("Radius config file read");
286 if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, NULL,
289 return (PAM_SERVICE_ERR);
292 PAM_LOG("Radius build access done");
295 switch (rad_send_request(radh)) {
297 case RAD_ACCESS_ACCEPT:
298 e = do_accept(pamh, radh);
301 return (PAM_SERVICE_ERR);
302 if (template_user != NULL) {
304 PAM_LOG("Trying template user: %s",
308 * If the given user name doesn't exist in
309 * the local password database, change it
310 * to the value given in the "template_user"
313 retval = pam_get_item(pamh, PAM_USER, &tmpuser);
314 if (retval != PAM_SUCCESS)
316 if (getpwnam(tmpuser) == NULL) {
317 pam_set_item(pamh, PAM_USER,
319 PAM_LOG("Using template user");
323 return (PAM_SUCCESS);
325 case RAD_ACCESS_REJECT:
327 PAM_VERBOSE_ERROR("Radius rejection");
328 return (PAM_AUTH_ERR);
330 case RAD_ACCESS_CHALLENGE:
331 retval = do_challenge(pamh, radh, user);
332 if (retval != PAM_SUCCESS) {
339 syslog(LOG_CRIT, "rad_send_request: %s",
342 PAM_VERBOSE_ERROR("Radius failure");
343 return (PAM_AUTHINFO_UNAVAIL);
347 "rad_send_request: unexpected return value");
349 PAM_VERBOSE_ERROR("Radius error");
350 return (PAM_SERVICE_ERR);
356 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
357 int argc __unused, const char *argv[] __unused)
360 return (PAM_SUCCESS);
363 PAM_MODULE_ENTRY("pam_radius");