1 /*- 2 * Copyright 1998 Juniper Networks, Inc. 3 * All rights reserved. 4 * Copyright (c) 2001-2003 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 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. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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 22 * permission. 23 * 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 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <netdb.h> 44 #include <pwd.h> 45 #include <radlib.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <syslog.h> 49 #include <unistd.h> 50 51 #define PAM_SM_AUTH 52 53 #include <security/pam_appl.h> 54 #include <security/pam_modules.h> 55 #include <security/pam_mod_misc.h> 56 57 #define PAM_OPT_CONF "conf" 58 #define PAM_OPT_TEMPLATE_USER "template_user" 59 #define PAM_OPT_NAS_ID "nas_id" 60 #define PAM_OPT_NAS_IPADDR "nas_ipaddr" 61 62 #define MAX_CHALLENGE_MSGS 10 63 #define PASSWORD_PROMPT "RADIUS Password:" 64 65 static int build_access_request(struct rad_handle *, const char *, 66 const char *, const char *, const char *, const void *, 67 size_t); 68 static int do_accept(pam_handle_t *, struct rad_handle *); 69 static int do_challenge(pam_handle_t *, struct rad_handle *, 70 const char *, const char *, const char *); 71 72 /* 73 * Construct an access request, but don't send it. Returns 0 on success, 74 * -1 on failure. 75 */ 76 static int 77 build_access_request(struct rad_handle *radh, const char *user, 78 const char *pass, const char *nas_id, const char *nas_ipaddr, 79 const void *state, size_t state_len) 80 { 81 int error; 82 char host[MAXHOSTNAMELEN]; 83 struct sockaddr_in *haddr; 84 struct addrinfo hints; 85 struct addrinfo *res; 86 87 if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) { 88 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh)); 89 return (-1); 90 } 91 if (nas_id == NULL || 92 (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) { 93 if (gethostname(host, sizeof host) != -1) { 94 if (nas_id == NULL) 95 nas_id = host; 96 if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0) 97 nas_ipaddr = host; 98 } 99 } 100 if ((user != NULL && 101 rad_put_string(radh, RAD_USER_NAME, user) == -1) || 102 (pass != NULL && 103 rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) || 104 (nas_id != NULL && 105 rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) { 106 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh)); 107 return (-1); 108 } 109 if (nas_ipaddr != NULL) { 110 memset(&hints, 0, sizeof(hints)); 111 hints.ai_family = AF_INET; 112 if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 && 113 res != NULL && res->ai_family == AF_INET) { 114 haddr = (struct sockaddr_in *)res->ai_addr; 115 error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS, 116 haddr->sin_addr); 117 freeaddrinfo(res); 118 if (error == -1) { 119 syslog(LOG_CRIT, "rad_put_addr: %s", 120 rad_strerror(radh)); 121 return (-1); 122 } 123 } 124 } 125 if (state != NULL && rad_put_attr(radh, RAD_STATE, state, 126 state_len) == -1) { 127 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh)); 128 return (-1); 129 } 130 if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) { 131 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh)); 132 return (-1); 133 } 134 return (0); 135 } 136 137 static int 138 do_accept(pam_handle_t *pamh, struct rad_handle *radh) 139 { 140 int attrtype; 141 const void *attrval; 142 size_t attrlen; 143 char *s; 144 145 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) { 146 if (attrtype == RAD_USER_NAME) { 147 s = rad_cvt_string(attrval, attrlen); 148 if (s == NULL) { 149 syslog(LOG_CRIT, 150 "rad_cvt_string: out of memory"); 151 return (-1); 152 } 153 pam_set_item(pamh, PAM_USER, s); 154 free(s); 155 } 156 } 157 if (attrtype == -1) { 158 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh)); 159 return (-1); 160 } 161 return (0); 162 } 163 164 static int 165 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user, 166 const char *nas_id, const char *nas_ipaddr) 167 { 168 int retval; 169 int attrtype; 170 const void *attrval; 171 size_t attrlen; 172 const void *state; 173 size_t statelen; 174 struct pam_message msgs[MAX_CHALLENGE_MSGS]; 175 const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS]; 176 struct pam_response *resp; 177 int num_msgs; 178 const void *item; 179 const struct pam_conv *conv; 180 181 state = NULL; 182 statelen = 0; 183 num_msgs = 0; 184 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) { 185 switch (attrtype) { 186 187 case RAD_STATE: 188 state = attrval; 189 statelen = attrlen; 190 break; 191 192 case RAD_REPLY_MESSAGE: 193 if (num_msgs >= MAX_CHALLENGE_MSGS) { 194 syslog(LOG_CRIT, 195 "Too many RADIUS challenge messages"); 196 return (PAM_SERVICE_ERR); 197 } 198 msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen); 199 if (msgs[num_msgs].msg == NULL) { 200 syslog(LOG_CRIT, 201 "rad_cvt_string: out of memory"); 202 return (PAM_SERVICE_ERR); 203 } 204 msgs[num_msgs].msg_style = PAM_TEXT_INFO; 205 msg_ptrs[num_msgs] = &msgs[num_msgs]; 206 num_msgs++; 207 break; 208 } 209 } 210 if (attrtype == -1) { 211 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh)); 212 return (PAM_SERVICE_ERR); 213 } 214 if (num_msgs == 0) { 215 msgs[num_msgs].msg = strdup("(null RADIUS challenge): "); 216 if (msgs[num_msgs].msg == NULL) { 217 syslog(LOG_CRIT, "Out of memory"); 218 return (PAM_SERVICE_ERR); 219 } 220 msgs[num_msgs].msg_style = PAM_TEXT_INFO; 221 msg_ptrs[num_msgs] = &msgs[num_msgs]; 222 num_msgs++; 223 } 224 msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON; 225 if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) { 226 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV"); 227 return (retval); 228 } 229 conv = (const struct pam_conv *)item; 230 if ((retval = conv->conv(num_msgs, msg_ptrs, &resp, 231 conv->appdata_ptr)) != PAM_SUCCESS) 232 return (retval); 233 if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id, 234 nas_ipaddr, state, statelen) == -1) 235 return (PAM_SERVICE_ERR); 236 memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp)); 237 free(resp[num_msgs-1].resp); 238 free(resp); 239 while (num_msgs > 0) 240 free(msgs[--num_msgs].msg); 241 return (PAM_SUCCESS); 242 } 243 244 PAM_EXTERN int 245 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 246 int argc __unused, const char *argv[] __unused) 247 { 248 struct rad_handle *radh; 249 const char *user, *pass; 250 const void *tmpuser; 251 const char *conf_file, *template_user, *nas_id, *nas_ipaddr; 252 int retval; 253 int e; 254 255 conf_file = openpam_get_option(pamh, PAM_OPT_CONF); 256 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER); 257 nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID); 258 nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR); 259 260 retval = pam_get_user(pamh, &user, NULL); 261 if (retval != PAM_SUCCESS) 262 return (retval); 263 264 PAM_LOG("Got user: %s", user); 265 266 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT); 267 if (retval != PAM_SUCCESS) 268 return (retval); 269 270 PAM_LOG("Got password"); 271 272 radh = rad_open(); 273 if (radh == NULL) { 274 syslog(LOG_CRIT, "rad_open failed"); 275 return (PAM_SERVICE_ERR); 276 } 277 278 PAM_LOG("Radius opened"); 279 280 if (rad_config(radh, conf_file) == -1) { 281 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh)); 282 rad_close(radh); 283 return (PAM_SERVICE_ERR); 284 } 285 286 PAM_LOG("Radius config file read"); 287 288 if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, NULL, 289 0) == -1) { 290 rad_close(radh); 291 return (PAM_SERVICE_ERR); 292 } 293 294 PAM_LOG("Radius build access done"); 295 296 for (;;) { 297 switch (rad_send_request(radh)) { 298 299 case RAD_ACCESS_ACCEPT: 300 e = do_accept(pamh, radh); 301 rad_close(radh); 302 if (e == -1) 303 return (PAM_SERVICE_ERR); 304 if (template_user != NULL) { 305 306 PAM_LOG("Trying template user: %s", 307 template_user); 308 309 /* 310 * If the given user name doesn't exist in 311 * the local password database, change it 312 * to the value given in the "template_user" 313 * option. 314 */ 315 retval = pam_get_item(pamh, PAM_USER, &tmpuser); 316 if (retval != PAM_SUCCESS) 317 return (retval); 318 if (getpwnam(tmpuser) == NULL) { 319 pam_set_item(pamh, PAM_USER, 320 template_user); 321 PAM_LOG("Using template user"); 322 } 323 324 } 325 return (PAM_SUCCESS); 326 327 case RAD_ACCESS_REJECT: 328 rad_close(radh); 329 PAM_VERBOSE_ERROR("Radius rejection"); 330 return (PAM_AUTH_ERR); 331 332 case RAD_ACCESS_CHALLENGE: 333 retval = do_challenge(pamh, radh, user, nas_id, 334 nas_ipaddr); 335 if (retval != PAM_SUCCESS) { 336 rad_close(radh); 337 return (retval); 338 } 339 break; 340 341 case -1: 342 syslog(LOG_CRIT, "rad_send_request: %s", 343 rad_strerror(radh)); 344 rad_close(radh); 345 PAM_VERBOSE_ERROR("Radius failure"); 346 return (PAM_AUTHINFO_UNAVAIL); 347 348 default: 349 syslog(LOG_CRIT, 350 "rad_send_request: unexpected return value"); 351 rad_close(radh); 352 PAM_VERBOSE_ERROR("Radius error"); 353 return (PAM_SERVICE_ERR); 354 } 355 } 356 } 357 358 PAM_EXTERN int 359 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, 360 int argc __unused, const char *argv[] __unused) 361 { 362 363 return (PAM_SUCCESS); 364 } 365 366 PAM_MODULE_ENTRY("pam_radius"); 367