1 /* 2 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 /* 25 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 #include "includes.h" 30 RCSID("$OpenBSD: auth.c,v 1.45 2002/09/20 18:41:29 stevesk Exp $"); 31 32 #ifdef HAVE_LOGIN_H 33 #include <login.h> 34 #endif 35 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 36 #include <shadow.h> 37 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ 38 39 #ifdef HAVE_LIBGEN_H 40 #include <libgen.h> 41 #endif 42 43 #include "xmalloc.h" 44 #include "match.h" 45 #include "groupaccess.h" 46 #include "log.h" 47 #include "servconf.h" 48 #include "auth.h" 49 #include "auth-options.h" 50 #include "canohost.h" 51 #include "buffer.h" 52 #include "bufaux.h" 53 #include "uidswap.h" 54 #include "tildexpand.h" 55 #include "misc.h" 56 #include "bufaux.h" 57 #include "packet.h" 58 59 #ifdef HAVE_BSM 60 #include "bsmaudit.h" 61 #include <bsm/adt.h> 62 #endif /* HAVE_BSM */ 63 64 /* import */ 65 extern ServerOptions options; 66 67 /* Debugging messages */ 68 Buffer auth_debug; 69 int auth_debug_init; 70 71 /* 72 * Check if the user is allowed to log in via ssh. If user is listed 73 * in DenyUsers or one of user's groups is listed in DenyGroups, false 74 * will be returned. If AllowUsers isn't empty and user isn't listed 75 * there, or if AllowGroups isn't empty and one of user's groups isn't 76 * listed there, false will be returned. 77 * If the user's shell is not executable, false will be returned. 78 * Otherwise true is returned. 79 */ 80 int 81 allowed_user(struct passwd * pw) 82 { 83 struct stat st; 84 const char *hostname = NULL, *ipaddr = NULL; 85 char *shell; 86 int i; 87 #ifdef WITH_AIXAUTHENTICATE 88 char *loginmsg; 89 #endif /* WITH_AIXAUTHENTICATE */ 90 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ 91 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 92 struct spwd *spw; 93 94 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 95 if (!pw || !pw->pw_name) 96 return 0; 97 98 #define DAY (24L * 60 * 60) /* 1 day in seconds */ 99 spw = getspnam(pw->pw_name); 100 if (spw != NULL) { 101 time_t today = time(NULL) / DAY; 102 debug3("allowed_user: today %d sp_expire %d sp_lstchg %d" 103 " sp_max %d", (int)today, (int)spw->sp_expire, 104 (int)spw->sp_lstchg, (int)spw->sp_max); 105 106 /* 107 * We assume account and password expiration occurs the 108 * day after the day specified. 109 */ 110 if (spw->sp_expire != -1 && today > spw->sp_expire) { 111 log("Account %.100s has expired", pw->pw_name); 112 return 0; 113 } 114 115 if (spw->sp_lstchg == 0) { 116 log("User %.100s password has expired (root forced)", 117 pw->pw_name); 118 return 0; 119 } 120 121 if (spw->sp_max != -1 && 122 today > spw->sp_lstchg + spw->sp_max) { 123 log("User %.100s password has expired (password aged)", 124 pw->pw_name); 125 return 0; 126 } 127 } 128 #else 129 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 130 if (!pw || !pw->pw_name) 131 return 0; 132 #endif 133 134 /* 135 * Get the shell from the password data. An empty shell field is 136 * legal, and means /bin/sh. 137 */ 138 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 139 140 /* deny if shell does not exists or is not executable */ 141 if (stat(shell, &st) != 0) { 142 log("User %.100s not allowed because shell %.100s does not exist", 143 pw->pw_name, shell); 144 return 0; 145 } 146 if (S_ISREG(st.st_mode) == 0 || 147 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 148 log("User %.100s not allowed because shell %.100s is not executable", 149 pw->pw_name, shell); 150 return 0; 151 } 152 153 if (options.num_deny_users > 0 || options.num_allow_users > 0) { 154 hostname = get_canonical_hostname(options.verify_reverse_mapping); 155 ipaddr = get_remote_ipaddr(); 156 } 157 158 /* Return false if user is listed in DenyUsers */ 159 if (options.num_deny_users > 0) { 160 for (i = 0; i < options.num_deny_users; i++) 161 if (match_user(pw->pw_name, hostname, ipaddr, 162 options.deny_users[i])) { 163 log("User %.100s not allowed because listed in DenyUsers", 164 pw->pw_name); 165 return 0; 166 } 167 } 168 /* Return false if AllowUsers isn't empty and user isn't listed there */ 169 if (options.num_allow_users > 0) { 170 for (i = 0; i < options.num_allow_users; i++) 171 if (match_user(pw->pw_name, hostname, ipaddr, 172 options.allow_users[i])) 173 break; 174 /* i < options.num_allow_users iff we break for loop */ 175 if (i >= options.num_allow_users) { 176 log("User %.100s not allowed because not listed in AllowUsers", 177 pw->pw_name); 178 return 0; 179 } 180 } 181 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 182 /* Get the user's group access list (primary and supplementary) */ 183 if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 184 log("User %.100s not allowed because not in any group", 185 pw->pw_name); 186 return 0; 187 } 188 189 /* Return false if one of user's groups is listed in DenyGroups */ 190 if (options.num_deny_groups > 0) 191 if (ga_match(options.deny_groups, 192 options.num_deny_groups)) { 193 ga_free(); 194 log("User %.100s not allowed because a group is listed in DenyGroups", 195 pw->pw_name); 196 return 0; 197 } 198 /* 199 * Return false if AllowGroups isn't empty and one of user's groups 200 * isn't listed there 201 */ 202 if (options.num_allow_groups > 0) 203 if (!ga_match(options.allow_groups, 204 options.num_allow_groups)) { 205 ga_free(); 206 log("User %.100s not allowed because none of user's groups are listed in AllowGroups", 207 pw->pw_name); 208 return 0; 209 } 210 ga_free(); 211 } 212 213 #ifdef WITH_AIXAUTHENTICATE 214 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { 215 if (loginmsg && *loginmsg) { 216 /* Remove embedded newlines (if any) */ 217 char *p; 218 for (p = loginmsg; *p; p++) { 219 if (*p == '\n') 220 *p = ' '; 221 } 222 /* Remove trailing newline */ 223 *--p = '\0'; 224 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); 225 } 226 return 0; 227 } 228 #endif /* WITH_AIXAUTHENTICATE */ 229 230 /* We found no reason not to let this user try to log on... */ 231 return 1; 232 } 233 234 Authctxt * 235 authctxt_new(void) 236 { 237 Authctxt *authctxt = xmalloc(sizeof(*authctxt)); 238 memset(authctxt, 0, sizeof(*authctxt)); 239 return authctxt; 240 } 241 242 void 243 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) 244 { 245 void (*authlog) (const char *fmt,...) = verbose; 246 char *authmsg, *user_str; 247 248 if (authctxt == NULL) 249 fatal("%s: INTERNAL ERROR", __func__); 250 251 /* Raise logging level */ 252 if (authenticated == 1 || !authctxt->valid) 253 authlog = log; 254 else if (authctxt->failures >= AUTH_FAIL_LOG || 255 authctxt->attempt >= options.max_auth_tries_log || 256 authctxt->init_attempt >= options.max_init_auth_tries_log) 257 authlog = notice; 258 259 if (authctxt->method) { 260 authmsg = "Failed"; 261 if (authctxt->method->postponed) 262 authmsg = "Postponed"; /* shouldn't happen */ 263 if (authctxt->method->abandoned) 264 authmsg = "Abandoned"; 265 if (authctxt->method->authenticated) { 266 if (userauth_check_partial_failure(authctxt)) 267 authmsg = "Partially accepted"; 268 else 269 authmsg = "Accepted"; 270 } 271 else 272 authmsg = "Failed"; 273 } 274 else { 275 authmsg = authenticated ? "Accepted" : "Failed"; 276 } 277 278 if (authctxt->user == NULL || *authctxt->user == '\0') 279 user_str = "<implicit>"; 280 else if (!authctxt->valid) 281 user_str = "<invalid username>"; 282 else 283 user_str = authctxt->user; 284 285 authlog("%s %s for %s from %.200s port %d%s", 286 authmsg, 287 (method != NULL) ? method : "<unknown authentication method>", 288 user_str, 289 get_remote_ipaddr(), 290 get_remote_port(), 291 info); 292 293 #ifdef WITH_AIXAUTHENTICATE 294 if (authenticated == 0 && strcmp(method, "password") == 0) 295 loginfailed(authctxt->user, 296 get_canonical_hostname(options.verify_reverse_mapping), 297 "ssh"); 298 #endif /* WITH_AIXAUTHENTICATE */ 299 300 } 301 302 #ifdef HAVE_BSM 303 void 304 audit_failed_login_cleanup(void *ctxt) 305 { 306 Authctxt *authctxt = (Authctxt *)ctxt; 307 adt_session_data_t *ah; 308 309 if (authctxt == NULL) 310 /* Internal error */ 311 audit_sshd_login_failure(&ah, PAM_ABORT, NULL); 312 else if (authctxt->pam == NULL && authctxt->pam_retval != PAM_SUCCESS) 313 audit_sshd_login_failure(&ah, authctxt->pam_retval, 314 authctxt->user); 315 else if (authctxt->pam == NULL && authctxt->pam_retval == PAM_SUCCESS) 316 audit_sshd_login_failure(&ah, PAM_PERM_DENIED, authctxt->user); 317 else if (!authctxt->valid) 318 audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN, NULL); 319 else 320 audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt, 321 PAM_PERM_DENIED), authctxt->user); 322 } 323 #endif /* HAVE_BSM */ 324 325 /* 326 * Check whether root logins are disallowed. 327 */ 328 int 329 auth_root_allowed(char *method) 330 { 331 switch (options.permit_root_login) { 332 case PERMIT_YES: 333 return 1; 334 break; 335 case PERMIT_NO_PASSWD: 336 if (strcmp(method, "password") != 0 && 337 strcmp(method, "keyboard-interactive") != 0) 338 return 1; 339 break; 340 case PERMIT_FORCED_ONLY: 341 if (forced_command) { 342 log("Root login accepted for forced command."); 343 return 1; 344 } 345 break; 346 } 347 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 348 return 0; 349 } 350 351 352 /* 353 * Given a template and a passwd structure, build a filename 354 * by substituting % tokenised options. Currently, %% becomes '%', 355 * %h becomes the home directory and %u the username. 356 * 357 * This returns a buffer allocated by xmalloc. 358 */ 359 char * 360 expand_filename(const char *filename, struct passwd *pw) 361 { 362 Buffer buffer; 363 char *file; 364 const char *cp; 365 366 if (pw == 0) 367 return NULL; /* shouldn't happen */ 368 /* 369 * Build the filename string in the buffer by making the appropriate 370 * substitutions to the given file name. 371 */ 372 buffer_init(&buffer); 373 for (cp = filename; *cp; cp++) { 374 if (cp[0] == '%' && cp[1] == '%') { 375 buffer_append(&buffer, "%", 1); 376 cp++; 377 continue; 378 } 379 if (cp[0] == '%' && cp[1] == 'h') { 380 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir)); 381 cp++; 382 continue; 383 } 384 if (cp[0] == '%' && cp[1] == 'u') { 385 buffer_append(&buffer, pw->pw_name, 386 strlen(pw->pw_name)); 387 cp++; 388 continue; 389 } 390 buffer_append(&buffer, cp, 1); 391 } 392 buffer_append(&buffer, "\0", 1); 393 394 /* 395 * Ensure that filename starts anchored. If not, be backward 396 * compatible and prepend the '%h/' 397 */ 398 file = xmalloc(MAXPATHLEN); 399 cp = buffer_ptr(&buffer); 400 if (*cp != '/') 401 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp); 402 else 403 strlcpy(file, cp, MAXPATHLEN); 404 405 buffer_free(&buffer); 406 return file; 407 } 408 409 char * 410 authorized_keys_file(struct passwd *pw) 411 { 412 return expand_filename(options.authorized_keys_file, pw); 413 } 414 415 char * 416 authorized_keys_file2(struct passwd *pw) 417 { 418 return expand_filename(options.authorized_keys_file2, pw); 419 } 420 421 /* return ok if key exists in sysfile or userfile */ 422 HostStatus 423 check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 424 const char *sysfile, const char *userfile) 425 { 426 Key *found; 427 char *user_hostfile; 428 struct stat st; 429 HostStatus host_status; 430 431 /* Check if we know the host and its host key. */ 432 found = key_new(key->type); 433 host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 434 435 if (host_status != HOST_OK && userfile != NULL) { 436 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 437 if (options.strict_modes && 438 (stat(user_hostfile, &st) == 0) && 439 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 440 (st.st_mode & 022) != 0)) { 441 log("Authentication refused for %.100s: " 442 "bad owner or modes for %.200s", 443 pw->pw_name, user_hostfile); 444 } else { 445 temporarily_use_uid(pw); 446 host_status = check_host_in_hostfile(user_hostfile, 447 host, key, found, NULL); 448 restore_uid(); 449 } 450 xfree(user_hostfile); 451 } 452 key_free(found); 453 454 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 455 "ok" : "not found", host); 456 return host_status; 457 } 458 459 460 /* 461 * Check a given file for security. This is defined as all components 462 * of the path to the file must be owned by either the owner of 463 * of the file or root and no directories must be group or world writable. 464 * 465 * XXX Should any specific check be done for sym links ? 466 * 467 * Takes an open file descriptor, the file name, a uid and and 468 * error buffer plus max size as arguments. 469 * 470 * Returns 0 on success and -1 on failure 471 */ 472 int 473 secure_filename(FILE *f, const char *file, struct passwd *pw, 474 char *err, size_t errlen) 475 { 476 uid_t uid; 477 char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 478 char *cp; 479 int comparehome = 0; 480 struct stat st; 481 482 if (pw == NULL) 483 return 0; 484 485 uid = pw->pw_uid; 486 487 if (realpath(file, buf) == NULL) { 488 snprintf(err, errlen, "realpath %s failed: %s", file, 489 strerror(errno)); 490 return -1; 491 } 492 493 /* 494 * A user is not required to have all the files that are subject to 495 * the strict mode checking in his/her home directory. If the 496 * directory is not present at the moment, which might be the case if 497 * the directory is not mounted until the user is authenticated, do 498 * not perform the home directory check below. 499 */ 500 if (realpath(pw->pw_dir, homedir) != NULL) 501 comparehome = 1; 502 503 /* check the open file to avoid races */ 504 if (fstat(fileno(f), &st) < 0 || 505 (st.st_uid != 0 && st.st_uid != uid) || 506 (st.st_mode & 022) != 0) { 507 snprintf(err, errlen, "bad ownership or modes for file %s", 508 buf); 509 return -1; 510 } 511 512 /* for each component of the canonical path, walking upwards */ 513 for (;;) { 514 if ((cp = dirname(buf)) == NULL) { 515 snprintf(err, errlen, "dirname() failed"); 516 return -1; 517 } 518 strlcpy(buf, cp, sizeof(buf)); 519 520 debug3("secure_filename: checking '%s'", buf); 521 if (stat(buf, &st) < 0 || 522 (st.st_uid != 0 && st.st_uid != uid) || 523 (st.st_mode & 022) != 0) { 524 snprintf(err, errlen, 525 "bad ownership or modes for directory %s", buf); 526 return -1; 527 } 528 529 /* If we passed the homedir then we can stop. */ 530 if (comparehome && strcmp(homedir, buf) == 0) { 531 debug3("secure_filename: terminating check at '%s'", 532 buf); 533 break; 534 } 535 /* 536 * dirname should always complete with a "/" path, 537 * but we can be paranoid and check for "." too 538 */ 539 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 540 break; 541 } 542 return 0; 543 } 544 545 struct passwd * 546 getpwnamallow(const char *user) 547 { 548 #ifdef HAVE_LOGIN_CAP 549 extern login_cap_t *lc; 550 #ifdef BSD_AUTH 551 auth_session_t *as; 552 #endif 553 #endif 554 struct passwd *pw; 555 556 if (user == NULL || *user == '\0') 557 return (NULL); /* implicit user, will be set later */ 558 559 pw = getpwnam(user); 560 if (pw == NULL) { 561 log("Illegal user %.100s from %.100s", 562 user, get_remote_ipaddr()); 563 return (NULL); 564 } 565 if (!allowed_user(pw)) 566 return (NULL); 567 #ifdef HAVE_LOGIN_CAP 568 if ((lc = login_getclass(pw->pw_class)) == NULL) { 569 debug("unable to get login class: %s", user); 570 return (NULL); 571 } 572 #ifdef BSD_AUTH 573 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 574 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 575 debug("Approval failure for %s", user); 576 pw = NULL; 577 } 578 if (as != NULL) 579 auth_close(as); 580 #endif 581 #endif 582 if (pw != NULL) 583 return (pwcopy(pw)); 584 return (NULL); 585 } 586 587 void 588 auth_debug_add(const char *fmt,...) 589 { 590 char buf[1024]; 591 va_list args; 592 593 if (!auth_debug_init) 594 return; 595 596 va_start(args, fmt); 597 vsnprintf(buf, sizeof(buf), fmt, args); 598 va_end(args); 599 buffer_put_cstring(&auth_debug, buf); 600 } 601 602 void 603 auth_debug_send(void) 604 { 605 char *msg; 606 607 if (!auth_debug_init) 608 return; 609 while (buffer_len(&auth_debug)) { 610 msg = buffer_get_string(&auth_debug, NULL); 611 packet_send_debug("%s", msg); 612 xfree(msg); 613 } 614 } 615 616 void 617 auth_debug_reset(void) 618 { 619 if (auth_debug_init) 620 buffer_clear(&auth_debug); 621 else { 622 buffer_init(&auth_debug); 623 auth_debug_init = 1; 624 } 625 } 626