1 /*- 2 * Copyright 1998 Juniper Networks, Inc. 3 * All rights reserved. 4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * Portions of this software was 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/socket.h> 42 #include <sys/time.h> 43 #include <netinet/in.h> 44 #include <arpa/inet.h> 45 46 #include <login_cap.h> 47 #include <netdb.h> 48 #include <pwd.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <stdio.h> 52 #include <syslog.h> 53 #include <unistd.h> 54 55 #include <libutil.h> 56 57 #ifdef YP 58 #include <ypclnt.h> 59 #endif 60 61 #define PAM_SM_AUTH 62 #define PAM_SM_ACCOUNT 63 #define PAM_SM_PASSWORD 64 65 #include <security/pam_appl.h> 66 #include <security/pam_modules.h> 67 #include <security/pam_mod_misc.h> 68 69 #define PASSWORD_HASH "md5" 70 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */ 71 #define SALTSIZE 32 72 73 #define LOCKED_PREFIX "*LOCKED*" 74 #define LOCKED_PREFIX_LEN (sizeof(LOCKED_PREFIX) - 1) 75 76 static void makesalt(char []); 77 78 static char password_hash[] = PASSWORD_HASH; 79 80 #define PAM_OPT_LOCAL_PASS "local_pass" 81 #define PAM_OPT_NIS_PASS "nis_pass" 82 83 char *tempname = NULL; 84 85 /* 86 * authentication management 87 */ 88 PAM_EXTERN int 89 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 90 int argc __unused, const char *argv[] __unused) 91 { 92 login_cap_t *lc; 93 struct passwd *pwd; 94 int retval; 95 const char *pass, *user, *realpw, *prompt; 96 97 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) { 98 pwd = getpwnam(getlogin()); 99 } else { 100 retval = pam_get_user(pamh, &user, NULL); 101 if (retval != PAM_SUCCESS) 102 return (retval); 103 pwd = getpwnam(user); 104 } 105 106 PAM_LOG("Got user: %s", user); 107 108 if (pwd != NULL) { 109 PAM_LOG("Doing real authentication"); 110 realpw = pwd->pw_passwd; 111 if (realpw[0] == '\0') { 112 if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) && 113 openpam_get_option(pamh, PAM_OPT_NULLOK)) 114 return (PAM_SUCCESS); 115 realpw = "*"; 116 } 117 lc = login_getpwclass(pwd); 118 } else { 119 PAM_LOG("Doing dummy authentication"); 120 realpw = "*"; 121 lc = login_getclass(NULL); 122 } 123 prompt = login_getcapstr(lc, "passwd_prompt", NULL, NULL); 124 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, prompt); 125 login_close(lc); 126 if (retval != PAM_SUCCESS) 127 return (retval); 128 PAM_LOG("Got password"); 129 if (strcmp(crypt(pass, realpw), realpw) == 0) 130 return (PAM_SUCCESS); 131 132 PAM_VERBOSE_ERROR("UNIX authentication refused"); 133 return (PAM_AUTH_ERR); 134 } 135 136 PAM_EXTERN int 137 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, 138 int argc __unused, const char *argv[] __unused) 139 { 140 141 return (PAM_SUCCESS); 142 } 143 144 /* 145 * account management 146 */ 147 PAM_EXTERN int 148 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused, 149 int argc __unused, const char *argv[] __unused) 150 { 151 struct addrinfo hints, *res; 152 struct passwd *pwd; 153 struct timeval tp; 154 login_cap_t *lc; 155 time_t warntime; 156 int retval; 157 const char *user; 158 const void *rhost, *tty; 159 char rhostip[MAXHOSTNAMELEN] = ""; 160 161 retval = pam_get_user(pamh, &user, NULL); 162 if (retval != PAM_SUCCESS) 163 return (retval); 164 165 if (user == NULL || (pwd = getpwnam(user)) == NULL) 166 return (PAM_SERVICE_ERR); 167 168 PAM_LOG("Got user: %s", user); 169 170 retval = pam_get_item(pamh, PAM_RHOST, &rhost); 171 if (retval != PAM_SUCCESS) 172 return (retval); 173 174 retval = pam_get_item(pamh, PAM_TTY, &tty); 175 if (retval != PAM_SUCCESS) 176 return (retval); 177 178 if (*pwd->pw_passwd == '\0' && 179 (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) 180 return (PAM_NEW_AUTHTOK_REQD); 181 182 if (strncmp(pwd->pw_passwd, LOCKED_PREFIX, LOCKED_PREFIX_LEN) == 0) 183 return (PAM_AUTH_ERR); 184 185 lc = login_getpwclass(pwd); 186 if (lc == NULL) { 187 PAM_LOG("Unable to get login class for user %s", user); 188 return (PAM_SERVICE_ERR); 189 } 190 191 PAM_LOG("Got login_cap"); 192 193 if (pwd->pw_change || pwd->pw_expire) 194 gettimeofday(&tp, NULL); 195 196 /* 197 * Check pw_expire before pw_change - no point in letting the 198 * user change the password on an expired account. 199 */ 200 201 if (pwd->pw_expire) { 202 warntime = login_getcaptime(lc, "warnexpire", 203 DEFAULT_WARN, DEFAULT_WARN); 204 if (tp.tv_sec >= pwd->pw_expire) { 205 login_close(lc); 206 return (PAM_ACCT_EXPIRED); 207 } else if (pwd->pw_expire - tp.tv_sec < warntime && 208 (flags & PAM_SILENT) == 0) { 209 pam_error(pamh, "Warning: your account expires on %s", 210 ctime(&pwd->pw_expire)); 211 } 212 } 213 214 retval = PAM_SUCCESS; 215 if (pwd->pw_change) { 216 warntime = login_getcaptime(lc, "warnpassword", 217 DEFAULT_WARN, DEFAULT_WARN); 218 if (tp.tv_sec >= pwd->pw_change) { 219 retval = PAM_NEW_AUTHTOK_REQD; 220 } else if (pwd->pw_change - tp.tv_sec < warntime && 221 (flags & PAM_SILENT) == 0) { 222 pam_error(pamh, "Warning: your password expires on %s", 223 ctime(&pwd->pw_change)); 224 } 225 } 226 227 /* 228 * From here on, we must leave retval untouched (unless we 229 * know we're going to fail), because we need to remember 230 * whether we're supposed to return PAM_SUCCESS or 231 * PAM_NEW_AUTHTOK_REQD. 232 */ 233 234 if (rhost && *(const char *)rhost != '\0') { 235 memset(&hints, 0, sizeof(hints)); 236 hints.ai_family = AF_UNSPEC; 237 if (getaddrinfo(rhost, NULL, &hints, &res) == 0) { 238 getnameinfo(res->ai_addr, res->ai_addrlen, 239 rhostip, sizeof(rhostip), NULL, 0, 240 NI_NUMERICHOST); 241 } 242 if (res != NULL) 243 freeaddrinfo(res); 244 } 245 246 /* 247 * Check host / tty / time-of-day restrictions 248 */ 249 250 if (!auth_hostok(lc, rhost, rhostip) || 251 !auth_ttyok(lc, tty) || 252 !auth_timeok(lc, time(NULL))) 253 retval = PAM_AUTH_ERR; 254 255 login_close(lc); 256 257 return (retval); 258 } 259 260 /* 261 * password management 262 * 263 * standard Unix and NIS password changing 264 */ 265 PAM_EXTERN int 266 pam_sm_chauthtok(pam_handle_t *pamh, int flags, 267 int argc __unused, const char *argv[] __unused) 268 { 269 #ifdef YP 270 struct ypclnt *ypclnt; 271 const void *yp_domain, *yp_server; 272 #endif 273 char salt[SALTSIZE + 1]; 274 login_cap_t *lc; 275 struct passwd *pwd, *old_pwd; 276 const char *user, *old_pass, *new_pass; 277 char *encrypted; 278 time_t passwordtime; 279 int pfd, tfd, retval; 280 281 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) 282 pwd = getpwnam(getlogin()); 283 else { 284 retval = pam_get_user(pamh, &user, NULL); 285 if (retval != PAM_SUCCESS) 286 return (retval); 287 pwd = getpwnam(user); 288 } 289 290 if (pwd == NULL) 291 return (PAM_AUTHTOK_RECOVERY_ERR); 292 293 PAM_LOG("Got user: %s", user); 294 295 if (flags & PAM_PRELIM_CHECK) { 296 297 PAM_LOG("PRELIM round"); 298 299 if (getuid() == 0 && 300 (pwd->pw_fields & _PWF_SOURCE) == _PWF_FILES) 301 /* root doesn't need the old password */ 302 return (pam_set_item(pamh, PAM_OLDAUTHTOK, "")); 303 #ifdef YP 304 if (getuid() == 0 && 305 (pwd->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 306 307 yp_domain = yp_server = NULL; 308 (void)pam_get_data(pamh, "yp_domain", &yp_domain); 309 (void)pam_get_data(pamh, "yp_server", &yp_server); 310 311 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_server); 312 if (ypclnt == NULL) 313 return (PAM_BUF_ERR); 314 315 if (ypclnt_connect(ypclnt) == -1) { 316 ypclnt_free(ypclnt); 317 return (PAM_SERVICE_ERR); 318 } 319 320 retval = ypclnt_havepasswdd(ypclnt); 321 ypclnt_free(ypclnt); 322 if (retval == 1) 323 return (pam_set_item(pamh, PAM_OLDAUTHTOK, "")); 324 else if (retval == -1) 325 return (PAM_SERVICE_ERR); 326 } 327 #endif 328 if (pwd->pw_passwd[0] == '\0' 329 && openpam_get_option(pamh, PAM_OPT_NULLOK)) { 330 /* 331 * No password case. XXX Are we giving too much away 332 * by not prompting for a password? 333 * XXX check PAM_DISALLOW_NULL_AUTHTOK 334 */ 335 old_pass = ""; 336 } else { 337 retval = pam_get_authtok(pamh, 338 PAM_OLDAUTHTOK, &old_pass, NULL); 339 if (retval != PAM_SUCCESS) 340 return (retval); 341 } 342 PAM_LOG("Got old password"); 343 /* always encrypt first */ 344 encrypted = crypt(old_pass, pwd->pw_passwd); 345 if (old_pass[0] == '\0' && 346 !openpam_get_option(pamh, PAM_OPT_NULLOK)) 347 return (PAM_PERM_DENIED); 348 if (strcmp(encrypted, pwd->pw_passwd) != 0) 349 return (PAM_PERM_DENIED); 350 } 351 else if (flags & PAM_UPDATE_AUTHTOK) { 352 PAM_LOG("UPDATE round"); 353 354 retval = pam_get_authtok(pamh, 355 PAM_OLDAUTHTOK, &old_pass, NULL); 356 if (retval != PAM_SUCCESS) 357 return (retval); 358 PAM_LOG("Got old password"); 359 360 /* get new password */ 361 for (;;) { 362 retval = pam_get_authtok(pamh, 363 PAM_AUTHTOK, &new_pass, NULL); 364 if (retval != PAM_TRY_AGAIN) 365 break; 366 pam_error(pamh, "Mismatch; try again, EOF to quit."); 367 } 368 PAM_LOG("Got new password"); 369 if (retval != PAM_SUCCESS) { 370 PAM_VERBOSE_ERROR("Unable to get new password"); 371 return (retval); 372 } 373 374 if (getuid() != 0 && new_pass[0] == '\0' && 375 !openpam_get_option(pamh, PAM_OPT_NULLOK)) 376 return (PAM_PERM_DENIED); 377 378 if ((old_pwd = pw_dup(pwd)) == NULL) 379 return (PAM_BUF_ERR); 380 381 lc = login_getclass(pwd->pw_class); 382 if (login_setcryptfmt(lc, password_hash, NULL) == NULL) 383 openpam_log(PAM_LOG_ERROR, 384 "can't set password cipher, relying on default"); 385 386 /* set password expiry date */ 387 pwd->pw_change = 0; 388 passwordtime = login_getcaptime(lc, "passwordtime", 0, 0); 389 if (passwordtime > 0) 390 pwd->pw_change = time(NULL) + passwordtime; 391 392 login_close(lc); 393 makesalt(salt); 394 pwd->pw_passwd = crypt(new_pass, salt); 395 #ifdef YP 396 switch (old_pwd->pw_fields & _PWF_SOURCE) { 397 case _PWF_FILES: 398 #endif 399 retval = PAM_SERVICE_ERR; 400 if (pw_init(NULL, NULL)) 401 openpam_log(PAM_LOG_ERROR, "pw_init() failed"); 402 else if ((pfd = pw_lock()) == -1) 403 openpam_log(PAM_LOG_ERROR, "pw_lock() failed"); 404 else if ((tfd = pw_tmp(-1)) == -1) 405 openpam_log(PAM_LOG_ERROR, "pw_tmp() failed"); 406 else if (pw_copy(pfd, tfd, pwd, old_pwd) == -1) 407 openpam_log(PAM_LOG_ERROR, "pw_copy() failed"); 408 else if (pw_mkdb(pwd->pw_name) == -1) 409 openpam_log(PAM_LOG_ERROR, "pw_mkdb() failed"); 410 else 411 retval = PAM_SUCCESS; 412 pw_fini(); 413 #ifdef YP 414 break; 415 case _PWF_NIS: 416 yp_domain = yp_server = NULL; 417 (void)pam_get_data(pamh, "yp_domain", &yp_domain); 418 (void)pam_get_data(pamh, "yp_server", &yp_server); 419 ypclnt = ypclnt_new(yp_domain, 420 "passwd.byname", yp_server); 421 if (ypclnt == NULL) { 422 retval = PAM_BUF_ERR; 423 } else if (ypclnt_connect(ypclnt) == -1 || 424 ypclnt_passwd(ypclnt, pwd, old_pass) == -1) { 425 openpam_log(PAM_LOG_ERROR, "%s", ypclnt->error); 426 retval = PAM_SERVICE_ERR; 427 } else { 428 retval = PAM_SUCCESS; 429 } 430 ypclnt_free(ypclnt); 431 break; 432 default: 433 openpam_log(PAM_LOG_ERROR, "unsupported source 0x%x", 434 pwd->pw_fields & _PWF_SOURCE); 435 retval = PAM_SERVICE_ERR; 436 } 437 #endif 438 free(old_pwd); 439 } 440 else { 441 /* Very bad juju */ 442 retval = PAM_ABORT; 443 PAM_LOG("Illegal 'flags'"); 444 } 445 446 return (retval); 447 } 448 449 /* Mostly stolen from passwd(1)'s local_passwd.c - markm */ 450 451 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ 452 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 453 454 static void 455 to64(char *s, long v, int n) 456 { 457 while (--n >= 0) { 458 *s++ = itoa64[v&0x3f]; 459 v >>= 6; 460 } 461 } 462 463 /* Salt suitable for traditional DES and MD5 */ 464 void 465 makesalt(char salt[SALTSIZE]) 466 { 467 int i; 468 469 /* These are not really random numbers, they are just 470 * numbers that change to thwart construction of a 471 * dictionary. This is exposed to the public. 472 */ 473 for (i = 0; i < SALTSIZE; i += 4) 474 to64(&salt[i], arc4random(), 4); 475 salt[SALTSIZE] = '\0'; 476 } 477 478 PAM_MODULE_ENTRY("pam_unix"); 479