2 * Copyright 1998 Juniper Networks, Inc.
4 * Copyright (c) 2001,2002 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.2.6.2 2003/04/08 13:20:23 sobomax Exp $
37 * $DragonFly: src/lib/libpam/modules/pam_radius/Attic/pam_radius.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
40 #include <sys/param.h>
50 #include <security/pam_appl.h>
51 #include <security/pam_modules.h>
52 #include <security/pam_mod_misc.h>
55 PAM_OPT_CONF = PAM_OPT_STD_MAX,
56 PAM_OPT_TEMPLATE_USER,
60 static struct opttab other_options[] = {
61 { "conf", PAM_OPT_CONF },
62 { "template_user", PAM_OPT_TEMPLATE_USER },
63 { "nas_id", PAM_OPT_NAS_ID },
67 #define MAX_CHALLENGE_MSGS 10
68 #define PASSWORD_PROMPT "RADIUS Password:"
70 static int build_access_request(struct rad_handle *, const char *,
71 const char *, const char *, 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 *,
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 void *state, size_t state_len)
84 char host[MAXHOSTNAMELEN];
86 if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) {
87 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh));
90 if (nas_id == NULL && gethostname(host, sizeof host) != -1)
93 rad_put_string(radh, RAD_USER_NAME, user) == -1) ||
95 rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) ||
97 rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) {
98 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh));
101 if (state != NULL && rad_put_attr(radh, RAD_STATE, state,
103 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh));
106 if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) {
107 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh));
114 do_accept(pam_handle_t *pamh, struct rad_handle *radh)
121 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
122 if (attrtype == RAD_USER_NAME) {
123 s = rad_cvt_string(attrval, attrlen);
126 "rad_cvt_string: out of memory");
129 pam_set_item(pamh, PAM_USER, s);
133 if (attrtype == -1) {
134 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
141 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user)
149 struct pam_message msgs[MAX_CHALLENGE_MSGS];
150 const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS];
151 struct pam_response *resp;
154 const struct pam_conv *conv;
159 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) {
167 case RAD_REPLY_MESSAGE:
168 if (num_msgs >= MAX_CHALLENGE_MSGS) {
170 "Too many RADIUS challenge messages");
171 return (PAM_SERVICE_ERR);
173 msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen);
174 if (msgs[num_msgs].msg == NULL) {
176 "rad_cvt_string: out of memory");
177 return (PAM_SERVICE_ERR);
179 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
180 msg_ptrs[num_msgs] = &msgs[num_msgs];
185 if (attrtype == -1) {
186 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh));
187 return (PAM_SERVICE_ERR);
190 msgs[num_msgs].msg = strdup("(null RADIUS challenge): ");
191 if (msgs[num_msgs].msg == NULL) {
192 syslog(LOG_CRIT, "Out of memory");
193 return (PAM_SERVICE_ERR);
195 msgs[num_msgs].msg_style = PAM_TEXT_INFO;
196 msg_ptrs[num_msgs] = &msgs[num_msgs];
199 msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON;
200 if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) {
201 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV");
204 conv = (const struct pam_conv *)item;
205 if ((retval = conv->conv(num_msgs, msg_ptrs, &resp,
206 conv->appdata_ptr)) != PAM_SUCCESS)
208 if (build_access_request(radh, user, resp[num_msgs-1].resp, NULL,
209 state, statelen) == -1)
210 return (PAM_SERVICE_ERR);
211 memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp));
212 free(resp[num_msgs-1].resp);
215 free(msgs[--num_msgs].msg);
216 return (PAM_SUCCESS);
220 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
221 int argc, const char *argv[])
223 struct options options;
224 struct rad_handle *radh;
225 const char *user, *tmpuser, *pass;
226 char *conf_file, *template_user, *nas_id;
230 pam_std_option(&options, other_options, argc, argv);
232 PAM_LOG("Options processed");
235 pam_test_option(&options, PAM_OPT_CONF, &conf_file);
236 template_user = NULL;
237 pam_test_option(&options, PAM_OPT_TEMPLATE_USER, &template_user);
239 pam_test_option(&options, PAM_OPT_NAS_ID, &nas_id);
241 retval = pam_get_user(pamh, &user, NULL);
242 if (retval != PAM_SUCCESS)
245 PAM_LOG("Got user: %s", user);
247 retval = pam_get_pass(pamh, &pass, PASSWORD_PROMPT, &options);
248 if (retval != PAM_SUCCESS)
251 PAM_LOG("Got password");
255 syslog(LOG_CRIT, "rad_open failed");
256 return (PAM_SERVICE_ERR);
259 PAM_LOG("Radius opened");
261 if (rad_config(radh, conf_file) == -1) {
262 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh));
264 return (PAM_SERVICE_ERR);
267 PAM_LOG("Radius config file read");
269 if (build_access_request(radh, user, pass, nas_id, NULL, 0) == -1) {
271 return (PAM_SERVICE_ERR);
274 PAM_LOG("Radius build access done");
277 switch (rad_send_request(radh)) {
279 case RAD_ACCESS_ACCEPT:
280 e = do_accept(pamh, radh);
283 return (PAM_SERVICE_ERR);
284 if (template_user != NULL) {
286 PAM_LOG("Trying template user: %s",
290 * If the given user name doesn't exist in
291 * the local password database, change it
292 * to the value given in the "template_user"
295 retval = pam_get_item(pamh, PAM_USER,
296 (const void **)&tmpuser);
297 if (retval != PAM_SUCCESS)
299 if (getpwnam(tmpuser) == NULL) {
300 pam_set_item(pamh, PAM_USER,
302 PAM_LOG("Using template user");
306 return (PAM_SUCCESS);
308 case RAD_ACCESS_REJECT:
310 PAM_VERBOSE_ERROR("Radius rejection");
311 return (PAM_AUTH_ERR);
313 case RAD_ACCESS_CHALLENGE:
314 retval = do_challenge(pamh, radh, user);
315 if (retval != PAM_SUCCESS) {
322 syslog(LOG_CRIT, "rad_send_request: %s",
325 PAM_VERBOSE_ERROR("Radius failure");
326 return (PAM_AUTHINFO_UNAVAIL);
330 "rad_send_request: unexpected return value");
332 PAM_VERBOSE_ERROR("Radius error");
333 return (PAM_SERVICE_ERR);
339 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
340 int argc __unused, const char *argv[] __unused)
343 return (PAM_SUCCESS);
346 PAM_MODULE_ENTRY("pam_radius");