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 #include "includes.h" 26 RCSID("$OpenBSD: auth.c,v 1.45 2002/09/20 18:41:29 stevesk Exp $"); 27 RCSID("$FreeBSD: src/crypto/openssh/auth.c,v 1.3.2.7 2003/02/03 17:31:06 des Exp $"); 28 RCSID("$DragonFly: src/crypto/openssh/Attic/auth.c,v 1.2 2003/06/17 04:24:36 dillon Exp $"); 29 30 #ifdef HAVE_LOGIN_H 31 #include <login.h> 32 #endif 33 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) 34 #include <shadow.h> 35 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ 36 37 #ifdef HAVE_LIBGEN_H 38 #include <libgen.h> 39 #endif 40 41 #include "xmalloc.h" 42 #include "match.h" 43 #include "groupaccess.h" 44 #include "log.h" 45 #include "servconf.h" 46 #include "auth.h" 47 #include "auth-options.h" 48 #include "canohost.h" 49 #include "buffer.h" 50 #include "bufaux.h" 51 #include "uidswap.h" 52 #include "tildexpand.h" 53 #include "misc.h" 54 #include "bufaux.h" 55 #include "packet.h" 56 57 /* import */ 58 extern ServerOptions options; 59 60 /* Debugging messages */ 61 Buffer auth_debug; 62 int auth_debug_init; 63 64 /* 65 * Check if the user is allowed to log in via ssh. If user is listed 66 * in DenyUsers or one of user's groups is listed in DenyGroups, false 67 * will be returned. If AllowUsers isn't empty and user isn't listed 68 * there, or if AllowGroups isn't empty and one of user's groups isn't 69 * listed there, false will be returned. 70 * If the user's shell is not executable, false will be returned. 71 * Otherwise true is returned. 72 */ 73 int 74 allowed_user(struct passwd * pw) 75 { 76 struct stat st; 77 const char *hostname = NULL, *ipaddr = NULL; 78 char *shell; 79 int i; 80 #ifdef WITH_AIXAUTHENTICATE 81 char *loginmsg; 82 #endif /* WITH_AIXAUTHENTICATE */ 83 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ 84 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) 85 struct spwd *spw; 86 87 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 88 if (!pw || !pw->pw_name) 89 return 0; 90 91 #define DAY (24L * 60 * 60) /* 1 day in seconds */ 92 spw = getspnam(pw->pw_name); 93 if (spw != NULL) { 94 time_t today = time(NULL) / DAY; 95 debug3("allowed_user: today %d sp_expire %d sp_lstchg %d" 96 " sp_max %d", (int)today, (int)spw->sp_expire, 97 (int)spw->sp_lstchg, (int)spw->sp_max); 98 99 /* 100 * We assume account and password expiration occurs the 101 * day after the day specified. 102 */ 103 if (spw->sp_expire != -1 && today > spw->sp_expire) { 104 log("Account %.100s has expired", pw->pw_name); 105 return 0; 106 } 107 108 if (spw->sp_lstchg == 0) { 109 log("User %.100s password has expired (root forced)", 110 pw->pw_name); 111 return 0; 112 } 113 114 if (spw->sp_max != -1 && 115 today > spw->sp_lstchg + spw->sp_max) { 116 log("User %.100s password has expired (password aged)", 117 pw->pw_name); 118 return 0; 119 } 120 } 121 #else 122 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 123 if (!pw || !pw->pw_name) 124 return 0; 125 #endif 126 127 /* 128 * Get the shell from the password data. An empty shell field is 129 * legal, and means /bin/sh. 130 */ 131 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 132 133 /* deny if shell does not exists or is not executable */ 134 if (stat(shell, &st) != 0) { 135 log("User %.100s not allowed because shell %.100s does not exist", 136 pw->pw_name, shell); 137 return 0; 138 } 139 if (S_ISREG(st.st_mode) == 0 || 140 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 141 log("User %.100s not allowed because shell %.100s is not executable", 142 pw->pw_name, shell); 143 return 0; 144 } 145 146 if (options.num_deny_users > 0 || options.num_allow_users > 0) { 147 hostname = get_canonical_hostname(options.verify_reverse_mapping); 148 ipaddr = get_remote_ipaddr(); 149 } 150 151 /* Return false if user is listed in DenyUsers */ 152 if (options.num_deny_users > 0) { 153 for (i = 0; i < options.num_deny_users; i++) 154 if (match_user(pw->pw_name, hostname, ipaddr, 155 options.deny_users[i])) { 156 log("User %.100s not allowed because listed in DenyUsers", 157 pw->pw_name); 158 return 0; 159 } 160 } 161 /* Return false if AllowUsers isn't empty and user isn't listed there */ 162 if (options.num_allow_users > 0) { 163 for (i = 0; i < options.num_allow_users; i++) 164 if (match_user(pw->pw_name, hostname, ipaddr, 165 options.allow_users[i])) 166 break; 167 /* i < options.num_allow_users iff we break for loop */ 168 if (i >= options.num_allow_users) { 169 log("User %.100s not allowed because not listed in AllowUsers", 170 pw->pw_name); 171 return 0; 172 } 173 } 174 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 175 /* Get the user's group access list (primary and supplementary) */ 176 if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 177 log("User %.100s not allowed because not in any group", 178 pw->pw_name); 179 return 0; 180 } 181 182 /* Return false if one of user's groups is listed in DenyGroups */ 183 if (options.num_deny_groups > 0) 184 if (ga_match(options.deny_groups, 185 options.num_deny_groups)) { 186 ga_free(); 187 log("User %.100s not allowed because a group is listed in DenyGroups", 188 pw->pw_name); 189 return 0; 190 } 191 /* 192 * Return false if AllowGroups isn't empty and one of user's groups 193 * isn't listed there 194 */ 195 if (options.num_allow_groups > 0) 196 if (!ga_match(options.allow_groups, 197 options.num_allow_groups)) { 198 ga_free(); 199 log("User %.100s not allowed because none of user's groups are listed in AllowGroups", 200 pw->pw_name); 201 return 0; 202 } 203 ga_free(); 204 } 205 206 #ifdef WITH_AIXAUTHENTICATE 207 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { 208 if (loginmsg && *loginmsg) { 209 /* Remove embedded newlines (if any) */ 210 char *p; 211 for (p = loginmsg; *p; p++) { 212 if (*p == '\n') 213 *p = ' '; 214 } 215 /* Remove trailing newline */ 216 *--p = '\0'; 217 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); 218 } 219 return 0; 220 } 221 #endif /* WITH_AIXAUTHENTICATE */ 222 223 /* We found no reason not to let this user try to log on... */ 224 return 1; 225 } 226 227 Authctxt * 228 authctxt_new(void) 229 { 230 Authctxt *authctxt = xmalloc(sizeof(*authctxt)); 231 memset(authctxt, 0, sizeof(*authctxt)); 232 return authctxt; 233 } 234 235 void 236 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) 237 { 238 void (*authlog) (const char *fmt,...) = verbose; 239 char *authmsg; 240 241 /* Raise logging level */ 242 if (authenticated == 1 || 243 !authctxt->valid || 244 authctxt->failures >= AUTH_FAIL_LOG || 245 strcmp(method, "password") == 0) 246 authlog = log; 247 248 if (authctxt->postponed) 249 authmsg = "Postponed"; 250 else 251 authmsg = authenticated ? "Accepted" : "Failed"; 252 253 authlog("%s %s for %s%.100s from %.200s port %d%s", 254 authmsg, 255 method, 256 authctxt->valid ? "" : "illegal user ", 257 authctxt->user, 258 get_remote_ipaddr(), 259 get_remote_port(), 260 info); 261 262 #ifdef WITH_AIXAUTHENTICATE 263 if (authenticated == 0 && strcmp(method, "password") == 0) 264 loginfailed(authctxt->user, 265 get_canonical_hostname(options.verify_reverse_mapping), 266 "ssh"); 267 #endif /* WITH_AIXAUTHENTICATE */ 268 269 } 270 271 /* 272 * Check whether root logins are disallowed. 273 */ 274 int 275 auth_root_allowed(char *method) 276 { 277 switch (options.permit_root_login) { 278 case PERMIT_YES: 279 return 1; 280 break; 281 case PERMIT_NO_PASSWD: 282 if (strcmp(method, "password") != 0) 283 return 1; 284 break; 285 case PERMIT_FORCED_ONLY: 286 if (forced_command) { 287 log("Root login accepted for forced command."); 288 return 1; 289 } 290 break; 291 } 292 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 293 return 0; 294 } 295 296 297 /* 298 * Given a template and a passwd structure, build a filename 299 * by substituting % tokenised options. Currently, %% becomes '%', 300 * %h becomes the home directory and %u the username. 301 * 302 * This returns a buffer allocated by xmalloc. 303 */ 304 char * 305 expand_filename(const char *filename, struct passwd *pw) 306 { 307 Buffer buffer; 308 char *file; 309 const char *cp; 310 311 /* 312 * Build the filename string in the buffer by making the appropriate 313 * substitutions to the given file name. 314 */ 315 buffer_init(&buffer); 316 for (cp = filename; *cp; cp++) { 317 if (cp[0] == '%' && cp[1] == '%') { 318 buffer_append(&buffer, "%", 1); 319 cp++; 320 continue; 321 } 322 if (cp[0] == '%' && cp[1] == 'h') { 323 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir)); 324 cp++; 325 continue; 326 } 327 if (cp[0] == '%' && cp[1] == 'u') { 328 buffer_append(&buffer, pw->pw_name, 329 strlen(pw->pw_name)); 330 cp++; 331 continue; 332 } 333 buffer_append(&buffer, cp, 1); 334 } 335 buffer_append(&buffer, "\0", 1); 336 337 /* 338 * Ensure that filename starts anchored. If not, be backward 339 * compatible and prepend the '%h/' 340 */ 341 file = xmalloc(MAXPATHLEN); 342 cp = buffer_ptr(&buffer); 343 if (*cp != '/') 344 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp); 345 else 346 strlcpy(file, cp, MAXPATHLEN); 347 348 buffer_free(&buffer); 349 return file; 350 } 351 352 char * 353 authorized_keys_file(struct passwd *pw) 354 { 355 return expand_filename(options.authorized_keys_file, pw); 356 } 357 358 char * 359 authorized_keys_file2(struct passwd *pw) 360 { 361 return expand_filename(options.authorized_keys_file2, pw); 362 } 363 364 /* return ok if key exists in sysfile or userfile */ 365 HostStatus 366 check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 367 const char *sysfile, const char *userfile) 368 { 369 Key *found; 370 char *user_hostfile; 371 struct stat st; 372 HostStatus host_status; 373 374 /* Check if we know the host and its host key. */ 375 found = key_new(key->type); 376 host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 377 378 if (host_status != HOST_OK && userfile != NULL) { 379 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 380 if (options.strict_modes && 381 (stat(user_hostfile, &st) == 0) && 382 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 383 (st.st_mode & 022) != 0)) { 384 log("Authentication refused for %.100s: " 385 "bad owner or modes for %.200s", 386 pw->pw_name, user_hostfile); 387 } else { 388 temporarily_use_uid(pw); 389 host_status = check_host_in_hostfile(user_hostfile, 390 host, key, found, NULL); 391 restore_uid(); 392 } 393 xfree(user_hostfile); 394 } 395 key_free(found); 396 397 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 398 "ok" : "not found", host); 399 return host_status; 400 } 401 402 403 /* 404 * Check a given file for security. This is defined as all components 405 * of the path to the file must be owned by either the owner of 406 * of the file or root and no directories must be group or world writable. 407 * 408 * XXX Should any specific check be done for sym links ? 409 * 410 * Takes an open file descriptor, the file name, a uid and and 411 * error buffer plus max size as arguments. 412 * 413 * Returns 0 on success and -1 on failure 414 */ 415 int 416 secure_filename(FILE *f, const char *file, struct passwd *pw, 417 char *err, size_t errlen) 418 { 419 uid_t uid = pw->pw_uid; 420 char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 421 char *cp; 422 struct stat st; 423 424 if (realpath(file, buf) == NULL) { 425 snprintf(err, errlen, "realpath %s failed: %s", file, 426 strerror(errno)); 427 return -1; 428 } 429 if (realpath(pw->pw_dir, homedir) == NULL) { 430 snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir, 431 strerror(errno)); 432 return -1; 433 } 434 435 /* check the open file to avoid races */ 436 if (fstat(fileno(f), &st) < 0 || 437 (st.st_uid != 0 && st.st_uid != uid) || 438 (st.st_mode & 022) != 0) { 439 snprintf(err, errlen, "bad ownership or modes for file %s", 440 buf); 441 return -1; 442 } 443 444 /* for each component of the canonical path, walking upwards */ 445 for (;;) { 446 if ((cp = dirname(buf)) == NULL) { 447 snprintf(err, errlen, "dirname() failed"); 448 return -1; 449 } 450 strlcpy(buf, cp, sizeof(buf)); 451 452 debug3("secure_filename: checking '%s'", buf); 453 if (stat(buf, &st) < 0 || 454 (st.st_uid != 0 && st.st_uid != uid) || 455 (st.st_mode & 022) != 0) { 456 snprintf(err, errlen, 457 "bad ownership or modes for directory %s", buf); 458 return -1; 459 } 460 461 /* If are passed the homedir then we can stop */ 462 if (strcmp(homedir, buf) == 0) { 463 debug3("secure_filename: terminating check at '%s'", 464 buf); 465 break; 466 } 467 /* 468 * dirname should always complete with a "/" path, 469 * but we can be paranoid and check for "." too 470 */ 471 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 472 break; 473 } 474 return 0; 475 } 476 477 struct passwd * 478 getpwnamallow(const char *user) 479 { 480 #ifdef HAVE_LOGIN_CAP 481 extern login_cap_t *lc; 482 #ifdef BSD_AUTH 483 auth_session_t *as; 484 #endif 485 #endif 486 struct passwd *pw; 487 488 pw = getpwnam(user); 489 if (pw == NULL) { 490 log("Illegal user %.100s from %.100s", 491 user, get_remote_ipaddr()); 492 return (NULL); 493 } 494 if (!allowed_user(pw)) 495 return (NULL); 496 #ifdef HAVE_LOGIN_CAP 497 if ((lc = login_getpwclass(pw)) == NULL) { 498 debug("unable to get login class: %s", user); 499 return (NULL); 500 } 501 #ifdef BSD_AUTH 502 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 503 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 504 debug("Approval failure for %s", user); 505 pw = NULL; 506 } 507 if (as != NULL) 508 auth_close(as); 509 #endif 510 #endif 511 if (pw != NULL) 512 return (pwcopy(pw)); 513 return (NULL); 514 } 515 516 void 517 auth_debug_add(const char *fmt,...) 518 { 519 char buf[1024]; 520 va_list args; 521 522 if (!auth_debug_init) 523 return; 524 525 va_start(args, fmt); 526 vsnprintf(buf, sizeof(buf), fmt, args); 527 va_end(args); 528 buffer_put_cstring(&auth_debug, buf); 529 } 530 531 void 532 auth_debug_send(void) 533 { 534 char *msg; 535 536 if (!auth_debug_init) 537 return; 538 while (buffer_len(&auth_debug)) { 539 msg = buffer_get_string(&auth_debug, NULL); 540 packet_send_debug("%s", msg); 541 xfree(msg); 542 } 543 } 544 545 void 546 auth_debug_reset(void) 547 { 548 if (auth_debug_init) 549 buffer_clear(&auth_debug); 550 else { 551 buffer_init(&auth_debug); 552 auth_debug_init = 1; 553 } 554 } 555