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/param.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 #define PAM_OPT_NO_REPLYMSG "no_reply_message" 62 63 #define MAX_CHALLENGE_MSGS 10 64 #define PASSWORD_PROMPT "RADIUS Password:" 65 66 static int build_access_request(struct rad_handle *, const char *, 67 const char *, const char *, const char *, const char *, 68 const void *, size_t); 69 static int do_accept(pam_handle_t *, struct rad_handle *); 70 static int do_challenge(pam_handle_t *, struct rad_handle *, 71 const char *, const char *, const char *, const char *); 72 73 /* 74 * Construct an access request, but don't send it. Returns 0 on success, 75 * -1 on failure. 76 */ 77 static int 78 build_access_request(struct rad_handle *radh, const char *user, 79 const char *pass, const char *nas_id, const char *nas_ipaddr, 80 const char *rhost, const void *state, size_t state_len) 81 { 82 int error; 83 char host[MAXHOSTNAMELEN]; 84 struct sockaddr_in *haddr; 85 struct addrinfo hints; 86 struct addrinfo *res; 87 88 if (rad_create_request(radh, RAD_ACCESS_REQUEST) == -1) { 89 syslog(LOG_CRIT, "rad_create_request: %s", rad_strerror(radh)); 90 return (-1); 91 } 92 if (nas_id == NULL || 93 (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0)) { 94 if (gethostname(host, sizeof host) != -1) { 95 if (nas_id == NULL) 96 nas_id = host; 97 if (nas_ipaddr != NULL && strlen(nas_ipaddr) == 0) 98 nas_ipaddr = host; 99 } 100 } 101 if ((user != NULL && 102 rad_put_string(radh, RAD_USER_NAME, user) == -1) || 103 (pass != NULL && 104 rad_put_string(radh, RAD_USER_PASSWORD, pass) == -1) || 105 (nas_id != NULL && 106 rad_put_string(radh, RAD_NAS_IDENTIFIER, nas_id) == -1)) { 107 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh)); 108 return (-1); 109 } 110 if (nas_ipaddr != NULL) { 111 memset(&hints, 0, sizeof(hints)); 112 hints.ai_family = AF_INET; 113 if (getaddrinfo(nas_ipaddr, NULL, &hints, &res) == 0 && 114 res != NULL && res->ai_family == AF_INET) { 115 haddr = (struct sockaddr_in *)res->ai_addr; 116 error = rad_put_addr(radh, RAD_NAS_IP_ADDRESS, 117 haddr->sin_addr); 118 freeaddrinfo(res); 119 if (error == -1) { 120 syslog(LOG_CRIT, "rad_put_addr: %s", 121 rad_strerror(radh)); 122 return (-1); 123 } 124 } 125 } 126 if (rhost != NULL && 127 rad_put_string(radh, RAD_CALLING_STATION_ID, rhost) == -1) { 128 syslog(LOG_CRIT, "rad_put_string: %s", rad_strerror(radh)); 129 return (-1); 130 } 131 if (state != NULL && 132 rad_put_attr(radh, RAD_STATE, state, state_len) == -1) { 133 syslog(LOG_CRIT, "rad_put_attr: %s", rad_strerror(radh)); 134 return (-1); 135 } 136 if (rad_put_int(radh, RAD_SERVICE_TYPE, RAD_AUTHENTICATE_ONLY) == -1) { 137 syslog(LOG_CRIT, "rad_put_int: %s", rad_strerror(radh)); 138 return (-1); 139 } 140 return (0); 141 } 142 143 static int 144 do_accept(pam_handle_t *pamh, struct rad_handle *radh) 145 { 146 int attrtype; 147 const void *attrval; 148 size_t attrlen; 149 char *s; 150 151 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) { 152 switch (attrtype) { 153 case RAD_USER_NAME: 154 if ((s = rad_cvt_string(attrval, attrlen)) == NULL) 155 goto enomem; 156 pam_set_item(pamh, PAM_USER, s); 157 free(s); 158 break; 159 case RAD_REPLY_MESSAGE: 160 if ((s = rad_cvt_string(attrval, attrlen)) == NULL) 161 goto enomem; 162 if (!openpam_get_option(pamh, PAM_OPT_NO_REPLYMSG)) 163 pam_info(pamh, "%s", s); 164 free(s); 165 break; 166 default: 167 PAM_LOG("%s(): ignoring RADIUS attribute %d", 168 __func__, attrtype); 169 } 170 } 171 if (attrtype == -1) { 172 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh)); 173 return (-1); 174 } 175 return (0); 176 enomem: 177 syslog(LOG_CRIT, "%s(): out of memory", __func__); 178 return (-1); 179 } 180 181 static int 182 do_reject(pam_handle_t *pamh, struct rad_handle *radh) 183 { 184 int attrtype; 185 const void *attrval; 186 size_t attrlen; 187 char *s; 188 189 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) { 190 switch (attrtype) { 191 case RAD_REPLY_MESSAGE: 192 if ((s = rad_cvt_string(attrval, attrlen)) == NULL) 193 goto enomem; 194 if (!openpam_get_option(pamh, PAM_OPT_NO_REPLYMSG)) 195 pam_error(pamh, "%s", s); 196 free(s); 197 break; 198 default: 199 PAM_LOG("%s(): ignoring RADIUS attribute %d", 200 __func__, attrtype); 201 } 202 } 203 if (attrtype < 0) { 204 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh)); 205 return (-1); 206 } 207 return (0); 208 enomem: 209 syslog(LOG_CRIT, "%s(): out of memory", __func__); 210 return (-1); 211 } 212 213 static int 214 do_challenge(pam_handle_t *pamh, struct rad_handle *radh, const char *user, 215 const char *nas_id, const char *nas_ipaddr, const char *rhost) 216 { 217 int retval; 218 int attrtype; 219 const void *attrval; 220 size_t attrlen; 221 const void *state; 222 size_t statelen; 223 struct pam_message msgs[MAX_CHALLENGE_MSGS]; 224 const struct pam_message *msg_ptrs[MAX_CHALLENGE_MSGS]; 225 struct pam_response *resp; 226 int num_msgs; 227 const void *item; 228 const struct pam_conv *conv; 229 230 state = NULL; 231 statelen = 0; 232 num_msgs = 0; 233 while ((attrtype = rad_get_attr(radh, &attrval, &attrlen)) > 0) { 234 switch (attrtype) { 235 236 case RAD_STATE: 237 state = attrval; 238 statelen = attrlen; 239 break; 240 241 case RAD_REPLY_MESSAGE: 242 if (num_msgs >= MAX_CHALLENGE_MSGS) { 243 syslog(LOG_CRIT, 244 "Too many RADIUS challenge messages"); 245 return (PAM_SERVICE_ERR); 246 } 247 msgs[num_msgs].msg = rad_cvt_string(attrval, attrlen); 248 if (msgs[num_msgs].msg == NULL) { 249 syslog(LOG_CRIT, 250 "rad_cvt_string: out of memory"); 251 return (PAM_SERVICE_ERR); 252 } 253 msgs[num_msgs].msg_style = PAM_TEXT_INFO; 254 msg_ptrs[num_msgs] = &msgs[num_msgs]; 255 num_msgs++; 256 break; 257 } 258 } 259 if (attrtype == -1) { 260 syslog(LOG_CRIT, "rad_get_attr: %s", rad_strerror(radh)); 261 return (PAM_SERVICE_ERR); 262 } 263 if (num_msgs == 0) { 264 msgs[num_msgs].msg = strdup("(null RADIUS challenge): "); 265 if (msgs[num_msgs].msg == NULL) { 266 syslog(LOG_CRIT, "Out of memory"); 267 return (PAM_SERVICE_ERR); 268 } 269 msgs[num_msgs].msg_style = PAM_TEXT_INFO; 270 msg_ptrs[num_msgs] = &msgs[num_msgs]; 271 num_msgs++; 272 } 273 msgs[num_msgs-1].msg_style = PAM_PROMPT_ECHO_ON; 274 if ((retval = pam_get_item(pamh, PAM_CONV, &item)) != PAM_SUCCESS) { 275 syslog(LOG_CRIT, "do_challenge: cannot get PAM_CONV"); 276 return (retval); 277 } 278 conv = (const struct pam_conv *)item; 279 if ((retval = conv->conv(num_msgs, msg_ptrs, &resp, 280 conv->appdata_ptr)) != PAM_SUCCESS) 281 return (retval); 282 if (build_access_request(radh, user, resp[num_msgs-1].resp, nas_id, 283 nas_ipaddr, rhost, state, statelen) == -1) 284 return (PAM_SERVICE_ERR); 285 memset(resp[num_msgs-1].resp, 0, strlen(resp[num_msgs-1].resp)); 286 free(resp[num_msgs-1].resp); 287 free(resp); 288 while (num_msgs > 0) 289 free(msgs[--num_msgs].msg); 290 return (PAM_SUCCESS); 291 } 292 293 PAM_EXTERN int 294 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 295 int argc __unused, const char *argv[] __unused) 296 { 297 struct rad_handle *radh; 298 const char *user, *pass; 299 const void *rhost, *tmpuser; 300 const char *conf_file, *template_user, *nas_id, *nas_ipaddr; 301 int retval; 302 int e; 303 304 conf_file = openpam_get_option(pamh, PAM_OPT_CONF); 305 template_user = openpam_get_option(pamh, PAM_OPT_TEMPLATE_USER); 306 nas_id = openpam_get_option(pamh, PAM_OPT_NAS_ID); 307 nas_ipaddr = openpam_get_option(pamh, PAM_OPT_NAS_IPADDR); 308 pam_get_item(pamh, PAM_RHOST, &rhost); 309 310 retval = pam_get_user(pamh, &user, NULL); 311 if (retval != PAM_SUCCESS) 312 return (retval); 313 314 PAM_LOG("Got user: %s", user); 315 316 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT); 317 if (retval != PAM_SUCCESS) 318 return (retval); 319 320 PAM_LOG("Got password"); 321 322 radh = rad_open(); 323 if (radh == NULL) { 324 syslog(LOG_CRIT, "rad_open failed"); 325 return (PAM_SERVICE_ERR); 326 } 327 328 PAM_LOG("Radius opened"); 329 330 if (rad_config(radh, conf_file) == -1) { 331 syslog(LOG_ALERT, "rad_config: %s", rad_strerror(radh)); 332 rad_close(radh); 333 return (PAM_SERVICE_ERR); 334 } 335 336 PAM_LOG("Radius config file read"); 337 338 if (build_access_request(radh, user, pass, nas_id, nas_ipaddr, rhost, 339 NULL, 0) == -1) { 340 rad_close(radh); 341 return (PAM_SERVICE_ERR); 342 } 343 344 PAM_LOG("Radius build access done"); 345 346 for (;;) { 347 switch (rad_send_request(radh)) { 348 349 case RAD_ACCESS_ACCEPT: 350 e = do_accept(pamh, radh); 351 rad_close(radh); 352 if (e == -1) 353 return (PAM_SERVICE_ERR); 354 if (template_user != NULL) { 355 356 PAM_LOG("Trying template user: %s", 357 template_user); 358 359 /* 360 * If the given user name doesn't exist in 361 * the local password database, change it 362 * to the value given in the "template_user" 363 * option. 364 */ 365 retval = pam_get_item(pamh, PAM_USER, &tmpuser); 366 if (retval != PAM_SUCCESS) 367 return (retval); 368 if (getpwnam(tmpuser) == NULL) { 369 pam_set_item(pamh, PAM_USER, 370 template_user); 371 PAM_LOG("Using template user"); 372 } 373 374 } 375 return (PAM_SUCCESS); 376 377 case RAD_ACCESS_REJECT: 378 retval = do_reject(pamh, radh); 379 rad_close(radh); 380 PAM_VERBOSE_ERROR("Radius rejection"); 381 return (PAM_AUTH_ERR); 382 383 case RAD_ACCESS_CHALLENGE: 384 retval = do_challenge(pamh, radh, user, nas_id, 385 nas_ipaddr, rhost); 386 if (retval != PAM_SUCCESS) { 387 rad_close(radh); 388 return (retval); 389 } 390 break; 391 392 case -1: 393 syslog(LOG_CRIT, "rad_send_request: %s", 394 rad_strerror(radh)); 395 rad_close(radh); 396 PAM_VERBOSE_ERROR("Radius failure"); 397 return (PAM_AUTHINFO_UNAVAIL); 398 399 default: 400 syslog(LOG_CRIT, 401 "rad_send_request: unexpected return value"); 402 rad_close(radh); 403 PAM_VERBOSE_ERROR("Radius error"); 404 return (PAM_SERVICE_ERR); 405 } 406 } 407 } 408 409 PAM_EXTERN int 410 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, 411 int argc __unused, const char *argv[] __unused) 412 { 413 414 return (PAM_SUCCESS); 415 } 416 417 PAM_MODULE_ENTRY("pam_radius"); 418