1 /* $NetBSD: pam_unix.c,v 1.5 2005/02/26 02:57:32 thorpej Exp $ */ 2 3 /*- 4 * Copyright 1998 Juniper Networks, Inc. 5 * All rights reserved. 6 * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 7 * All rights reserved. 8 * 9 * Portions of this software was developed for the FreeBSD Project by 10 * ThinkSec AS and NAI Labs, the Security Research Division of Network 11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 12 * ("CBOSS"), as part of the DARPA CHATS research program. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. The name of the author may not be used to endorse or promote 23 * products derived from this software without specific prior written 24 * permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifdef __FreeBSD__ 41 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.49 2004/02/10 10:13:21 des Exp $"); 42 #else 43 __RCSID("$NetBSD: pam_unix.c,v 1.5 2005/02/26 02:57:32 thorpej Exp $"); 44 #endif 45 46 47 #include <sys/types.h> 48 49 #include <ctype.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <pwd.h> 53 #include <grp.h> 54 #include <limits.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <stdio.h> 58 #include <login_cap.h> 59 #include <time.h> 60 #include <tzfile.h> 61 #include <unistd.h> 62 63 #include <util.h> 64 65 #ifdef YP 66 #include <rpc/rpc.h> 67 #include <rpcsvc/ypclnt.h> 68 #include <rpcsvc/yppasswd.h> 69 #endif 70 71 #define PAM_SM_AUTH 72 #define PAM_SM_ACCOUNT 73 #define PAM_SM_PASSWORD 74 75 #include <security/pam_appl.h> 76 #include <security/pam_modules.h> 77 #include <security/pam_mod_misc.h> 78 79 /* 80 * authentication management 81 */ 82 PAM_EXTERN int 83 /*ARGSUSED*/ 84 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 85 int argc __unused, const char *argv[] __unused) 86 { 87 login_cap_t *lc; 88 struct passwd *pwd; 89 int retval; 90 const char *pass, *user, *realpw; 91 92 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) { 93 pwd = getpwnam(getlogin()); 94 } else { 95 retval = pam_get_user(pamh, &user, NULL); 96 if (retval != PAM_SUCCESS) 97 return (retval); 98 PAM_LOG("Got user: %s", user); 99 pwd = getpwnam(user); 100 } 101 102 if (pwd != NULL) { 103 PAM_LOG("Doing real authentication"); 104 realpw = pwd->pw_passwd; 105 if (realpw[0] == '\0') { 106 if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) && 107 openpam_get_option(pamh, PAM_OPT_NULLOK)) 108 return (PAM_SUCCESS); 109 realpw = "*"; 110 } 111 lc = login_getpwclass(pwd); 112 } else { 113 PAM_LOG("Doing dummy authentication"); 114 realpw = "*"; 115 lc = login_getclass(NULL); 116 } 117 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, NULL); 118 login_close(lc); 119 if (retval != PAM_SUCCESS) 120 return (retval); 121 PAM_LOG("Got password"); 122 if (strcmp(crypt(pass, realpw), realpw) == 0) 123 return (PAM_SUCCESS); 124 125 PAM_VERBOSE_ERROR("UNIX authentication refused"); 126 return (PAM_AUTH_ERR); 127 } 128 129 PAM_EXTERN int 130 /*ARGSUSED*/ 131 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, 132 int argc __unused, const char *argv[] __unused) 133 { 134 135 return (PAM_SUCCESS); 136 } 137 138 /* 139 * account management 140 */ 141 PAM_EXTERN int 142 /*ARGSUSED*/ 143 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused, 144 int argc __unused, const char *argv[] __unused) 145 { 146 struct passwd *pwd; 147 struct timeval now; 148 login_cap_t *lc; 149 time_t warntime; 150 int retval; 151 const char *user; 152 153 retval = pam_get_user(pamh, &user, NULL); 154 if (retval != PAM_SUCCESS) 155 return (retval); 156 157 if (user == NULL || (pwd = getpwnam(user)) == NULL) 158 return (PAM_SERVICE_ERR); 159 160 PAM_LOG("Got user: %s", user); 161 162 if (*pwd->pw_passwd == '\0' && 163 (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) 164 return (PAM_NEW_AUTHTOK_REQD); 165 166 lc = login_getpwclass(pwd); 167 if (lc == NULL) { 168 PAM_LOG("Unable to get login class for user %s", user); 169 return (PAM_SERVICE_ERR); 170 } 171 172 PAM_LOG("Got login_cap"); 173 174 if (pwd->pw_change || pwd->pw_expire) 175 (void) gettimeofday(&now, NULL); 176 177 warntime = (time_t)login_getcaptime(lc, "password-warn", 178 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY), 179 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY)); 180 181 /* 182 * Check pw_expire before pw_change - no point in letting the 183 * user change the password on an expired account. 184 */ 185 186 if (pwd->pw_expire) { 187 if (now.tv_sec >= pwd->pw_expire) { 188 login_close(lc); 189 return (PAM_ACCT_EXPIRED); 190 } else if (pwd->pw_expire - now.tv_sec < warntime && 191 (flags & PAM_SILENT) == 0) { 192 pam_error(pamh, "Warning: your account expires on %s", 193 ctime(&pwd->pw_expire)); 194 } 195 } 196 197 if (pwd->pw_change) { 198 /* XXX How to handle _PASSWORD_CHGNOW? --thorpej */ 199 if (now.tv_sec >= pwd->pw_change) { 200 login_close(lc); 201 return (PAM_NEW_AUTHTOK_REQD); 202 } else if (pwd->pw_change - now.tv_sec < warntime && 203 (flags & PAM_SILENT) == 0) { 204 pam_error(pamh, "Warning: your password expires on %s", 205 ctime(&pwd->pw_change)); 206 } 207 } 208 209 login_close(lc); 210 211 return (PAM_SUCCESS); 212 } 213 214 #ifdef YP 215 /* 216 * yp_check_user: 217 * 218 * Helper function; check that a user exists in the NIS 219 * password map. 220 */ 221 static int 222 yp_check_user(const char *domain, const char *user) 223 { 224 char *val; 225 int reason, vallen; 226 227 val = NULL; 228 reason = yp_match(domain, "passwd.byname", user, (int)strlen(user), 229 &val, &vallen); 230 if (reason != 0) { 231 if (val != NULL) 232 free(val); 233 return (0); 234 } 235 free(val); 236 return (1); 237 } 238 239 static int 240 /*ARGSUSED*/ 241 yp_set_password(pam_handle_t *pamh, struct passwd *opwd, 242 struct passwd *pwd, const char *old_pass, const char *domain) 243 { 244 char *master; 245 int r, rpcport, status; 246 struct yppasswd yppwd; 247 CLIENT *client; 248 uid_t uid; 249 int retval = PAM_SERVICE_ERR; 250 struct timeval tv; 251 252 /* 253 * Find the master for the passwd map; it should be running 254 * rpc.yppasswdd. 255 */ 256 if ((r = yp_master(domain, "passwd.byname", &master)) != 0) { 257 pam_error(pamh, "Can't find master NIS server. Reason: %s", 258 yperr_string(r)); 259 return (PAM_SERVICE_ERR); 260 } 261 262 /* 263 * Ask the portmapper for the port of rpc.yppasswdd. 264 */ 265 if ((rpcport = getrpcport(master, YPPASSWDPROG, 266 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) { 267 pam_error(pamh, 268 "Master NIS server not runing yppasswd daemon.\n\t" 269 "Can't change NIS password."); 270 return (PAM_SERVICE_ERR); 271 } 272 273 /* 274 * Be sure the port is privileged. 275 */ 276 if (rpcport >= IPPORT_RESERVED) { 277 pam_error(pamh, "yppasswd daemon is on an invalid port."); 278 return (PAM_SERVICE_ERR); 279 } 280 281 uid = getuid(); 282 if (uid != 0 && uid != pwd->pw_uid) { 283 pam_error(pamh, "You may only change your own password: %s", 284 strerror(EACCES)); 285 return (PAM_SERVICE_ERR); 286 } 287 288 /* 289 * Fill in the yppasswd structure for yppasswdd. 290 */ 291 memset(&yppwd, 0, sizeof(yppwd)); 292 yppwd.oldpass = strdup(old_pass); 293 if ((yppwd.newpw.pw_passwd = strdup(pwd->pw_passwd)) == NULL) 294 goto malloc_failure; 295 if ((yppwd.newpw.pw_name = strdup(pwd->pw_name)) == NULL) 296 goto malloc_failure; 297 yppwd.newpw.pw_uid = pwd->pw_uid; 298 yppwd.newpw.pw_gid = pwd->pw_gid; 299 if ((yppwd.newpw.pw_gecos = strdup(pwd->pw_gecos)) == NULL) 300 goto malloc_failure; 301 if ((yppwd.newpw.pw_dir = strdup(pwd->pw_dir)) == NULL) 302 goto malloc_failure; 303 if ((yppwd.newpw.pw_shell = strdup(pwd->pw_shell)) == NULL) 304 goto malloc_failure; 305 306 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp"); 307 if (client == NULL) { 308 pam_error(pamh, "Can't contact yppasswdd on %s: Reason: %s", 309 master, yperr_string(YPERR_YPBIND)); 310 goto out; 311 } 312 313 client->cl_auth = authunix_create_default(); 314 tv.tv_sec = 2; 315 tv.tv_usec = 0; 316 r = clnt_call(client, YPPASSWDPROC_UPDATE, 317 xdr_yppasswd, &yppwd, xdr_int, &status, tv); 318 if (r) 319 pam_error(pamh, "RPC to yppasswdd failed."); 320 else if (status) 321 pam_error(pamh, "Couldn't change NIS password."); 322 else { 323 pam_info(pamh, "The NIS password has been changed on %s, " 324 "the master NIS passwd server.", master); 325 retval = PAM_SUCCESS; 326 } 327 328 out: 329 if (yppwd.oldpass != NULL) 330 free(yppwd.oldpass); 331 if (yppwd.newpw.pw_passwd != NULL) 332 free(yppwd.newpw.pw_passwd); 333 if (yppwd.newpw.pw_name != NULL) 334 free(yppwd.newpw.pw_name); 335 if (yppwd.newpw.pw_gecos != NULL) 336 free(yppwd.newpw.pw_gecos); 337 if (yppwd.newpw.pw_dir != NULL) 338 free(yppwd.newpw.pw_dir); 339 if (yppwd.newpw.pw_shell != NULL) 340 free(yppwd.newpw.pw_shell); 341 return (retval); 342 343 malloc_failure: 344 pam_error(pamh, "memory allocation failure"); 345 goto out; 346 } 347 #endif /* YP */ 348 349 static int 350 local_set_password(pam_handle_t *pamh, struct passwd *opwd, 351 struct passwd *pwd) 352 { 353 char errbuf[200]; 354 int tfd, pfd; 355 356 pw_init(); 357 tfd = pw_lock(0); 358 if (tfd < 0) { 359 pam_error(pamh, "The password file is busy, waiting..."); 360 tfd = pw_lock(10); 361 if (tfd < 0) { 362 pam_error(pamh, "The password file is still busy, " 363 "try again later."); 364 return (PAM_SERVICE_ERR); 365 } 366 } 367 368 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); 369 if (pfd < 0) { 370 pam_error(pamh, "%s: %s", _PATH_MASTERPASSWD, strerror(errno)); 371 pw_abort(); 372 return (PAM_SERVICE_ERR); 373 } 374 375 if (pw_copyx(pfd, tfd, pwd, opwd, errbuf, sizeof(errbuf)) == 0) { 376 pam_error(pamh, "Unable to update password entry: %s", 377 errbuf); 378 pw_abort(); 379 return (PAM_SERVICE_ERR); 380 } 381 382 if (pw_mkdb(pwd->pw_name, opwd->pw_change == pwd->pw_change) < 0) { 383 pam_error(pamh, "Unable to rebuild local password database."); 384 pw_abort(); 385 return (PAM_SERVICE_ERR); 386 } 387 388 return (PAM_SUCCESS); 389 } 390 391 /* 392 * password management 393 * 394 * standard Unix and NIS password changing 395 */ 396 PAM_EXTERN int 397 /*ARGSUSED*/ 398 pam_sm_chauthtok(pam_handle_t *pamh, int flags, 399 int argc __unused, const char *argv[] __unused) 400 { 401 struct passwd *pwd, old_pwd; 402 login_cap_t *lc; 403 const char *user, *passwd_db, *new_pass, *old_pass, *p; 404 int retval, tries, min_pw_len = 0, pw_expiry = 0; 405 char salt[_PASSWORD_LEN+1]; 406 #ifdef YP 407 char *domain; 408 int r; 409 #endif 410 411 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) 412 pwd = getpwnam(getlogin()); 413 else { 414 retval = pam_get_user(pamh, &user, NULL); 415 if (retval != PAM_SUCCESS) 416 return (retval); 417 pwd = getpwnam(user); 418 } 419 420 if (pwd == NULL) 421 return (PAM_AUTHTOK_RECOVERY_ERR); 422 423 old_pwd = *pwd; 424 425 PAM_LOG("Got user: %s", user); 426 427 /* 428 * Determine which password type we're going to change, and 429 * remember it. 430 * 431 * NOTE: domain does not need to be freed; its storage is 432 * allocated statically in libc. 433 */ 434 passwd_db = openpam_get_option(pamh, "passwd_db"); 435 if (passwd_db == NULL) { 436 #ifdef YP 437 /* Prefer YP, if configured. */ 438 if (_yp_check(NULL)) { 439 /* If _yp_check() succeeded, then this must. */ 440 if ((r = yp_get_default_domain(&domain)) != 0) { 441 pam_error(pamh, 442 "Unable to get NIS domain, reason: %s", 443 yperr_string(r)); 444 return (PAM_SERVICE_ERR); 445 } 446 if (yp_check_user(domain, user)) 447 passwd_db = "nis"; 448 } 449 #endif 450 /* Otherwise we always use local files. */ 451 if (passwd_db == NULL) { 452 /* XXX Any validation to do here? */ 453 passwd_db = "files"; 454 } 455 456 if (passwd_db == NULL) { 457 pam_error(pamh, "Unable to determine Unix password DB"); 458 return (PAM_SERVICE_ERR); 459 } else if ((retval = openpam_set_option(pamh, "passwd_db", 460 passwd_db)) != PAM_SUCCESS) { 461 return (retval); 462 } 463 } else { 464 /* Check to see if the specified password DB is usable. */ 465 #ifdef YP 466 if (strcmp(passwd_db, "nis") == 0) { 467 if (_yp_check(NULL) == 0) { 468 pam_error(pamh, "NIS not in use."); 469 return (PAM_SERVICE_ERR); 470 } 471 if ((r = yp_get_default_domain(&domain)) != 0) { 472 pam_error(pamh, 473 "Unable to get NIS domain, reason: %s", 474 yperr_string(r)); 475 return (PAM_SERVICE_ERR); 476 } 477 if (yp_check_user(domain, user) == 0) { 478 pam_error(pamh, 479 "User %s does not exist in NIS.", user); 480 return (PAM_USER_UNKNOWN); 481 } 482 goto known_passwd_db; 483 } 484 #endif 485 if (strcmp(passwd_db, "files") == 0) { 486 /* XXX Any validation to do here? */ 487 goto known_passwd_db; 488 } 489 pam_error(pamh, "Unknown Unix password DB: %s", passwd_db); 490 return (PAM_SERVICE_ERR); 491 } 492 known_passwd_db: 493 494 if (flags & PAM_PRELIM_CHECK) { 495 PAM_LOG("PRELIM round"); 496 497 if (strcmp(passwd_db, "files") == 0) { 498 if (getuid() == 0) { 499 /* Root doesn't need the old password. */ 500 return (pam_set_item(pamh, PAM_OLDAUTHTOK, "")); 501 } 502 } 503 504 if (pwd->pw_passwd[0] == '\0') { 505 /* 506 * No password case. 507 * XXX Are we giviing too much away by not prompting 508 * XXX for a password? 509 * XXX Check PAM_DISALLOW_NULL_AUTHTOK 510 */ 511 return (pam_set_item(pamh, PAM_OLDAUTHTOK, "")); 512 } else { 513 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, 514 &old_pass, NULL); 515 if (retval != PAM_SUCCESS) 516 return (retval); 517 if (strcmp(crypt(old_pass, pwd->pw_passwd), 518 pwd->pw_passwd) != 0) 519 return (PAM_PERM_DENIED); 520 return (PAM_SUCCESS); 521 } 522 } 523 524 if (flags & PAM_UPDATE_AUTHTOK) { 525 char option[LINE_MAX], *key, *opt; 526 527 PAM_LOG("UPDATE round"); 528 529 if ((lc = login_getclass(pwd->pw_class)) != NULL) { 530 min_pw_len = (int) login_getcapnum(lc, 531 "minpasswordlen", (quad_t)0, (quad_t)0); 532 pw_expiry = (int) login_getcapnum(lc, 533 "passwordtime", (quad_t)0, (quad_t)0); 534 login_close(lc); 535 } 536 537 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &old_pass, NULL); 538 if (retval != PAM_SUCCESS) 539 return (retval); 540 541 /* Get the new password. */ 542 for (tries = 0;;) { 543 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &new_pass, 544 NULL); 545 if (retval == PAM_TRY_AGAIN) { 546 pam_error(pamh, 547 "Mismatch; try again, EOF to quit."); 548 continue; 549 } 550 if (retval != PAM_SUCCESS) { 551 PAM_VERBOSE_ERROR("Unable to get new password"); 552 return (retval); 553 } 554 /* Successfully got new password. */ 555 if (new_pass[0] == '\0') { 556 pam_info(pamh, "Password unchanged."); 557 return (PAM_SUCCESS); 558 } 559 if (min_pw_len > 0 && strlen(new_pass) < min_pw_len) { 560 pam_error(pamh, "Password is too short."); 561 continue; 562 } 563 if (strlen(new_pass) <= 5 && ++tries < 2) { 564 pam_error(pamh, 565 "Please enter a longer password."); 566 continue; 567 } 568 for (p = new_pass; *p && islower((unsigned char)*p); ++p); 569 if (!*p && ++tries < 2) { 570 pam_error(pamh, 571 "Please don't use an all-lower case " 572 "password.\nUnusual capitalization, " 573 "control characters or digits are " 574 "suggested."); 575 continue; 576 } 577 /* Password is OK. */ 578 break; 579 } 580 pw_getpwconf(option, sizeof(option), pwd, 581 #ifdef YP 582 strcmp(passwd_db, "nis") == 0 ? "ypcipher" : 583 #endif 584 "localcipher"); 585 opt = option; 586 key = strsep(&opt, ","); 587 588 if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) { 589 pam_error(pamh, "Couldn't generate salt."); 590 return (PAM_SERVICE_ERR); 591 } 592 593 pwd->pw_passwd = crypt(new_pass, salt); 594 pwd->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0; 595 596 retval = PAM_SERVICE_ERR; 597 if (strcmp(passwd_db, "files") == 0) 598 retval = local_set_password(pamh, &old_pwd, pwd); 599 #ifdef YP 600 if (strcmp(passwd_db, "nis") == 0) 601 retval = yp_set_password(pamh, &old_pwd, pwd, old_pass, 602 domain); 603 #endif 604 return (retval); 605 } 606 607 PAM_LOG("Illegal flags argument"); 608 return (PAM_ABORT); 609 } 610 611 PAM_MODULE_ENTRY("pam_unix"); 612