1 /* $OpenBSD: auth.c,v 1.80 2008/11/04 07:58:09 djm Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/param.h> 29 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <libgen.h> 33 #include <login_cap.h> 34 #include <paths.h> 35 #include <pwd.h> 36 #include <stdarg.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #include "xmalloc.h" 42 #include "match.h" 43 #include "groupaccess.h" 44 #include "log.h" 45 #include "buffer.h" 46 #include "servconf.h" 47 #include "key.h" 48 #include "hostfile.h" 49 #include "auth.h" 50 #include "auth-options.h" 51 #include "canohost.h" 52 #include "uidswap.h" 53 #include "misc.h" 54 #include "packet.h" 55 #ifdef GSSAPI 56 #include "ssh-gss.h" 57 #endif 58 #include "monitor_wrap.h" 59 60 /* import */ 61 extern ServerOptions options; 62 extern int use_privsep; 63 64 /* Debugging messages */ 65 Buffer auth_debug; 66 int auth_debug_init; 67 68 /* 69 * Check if the user is allowed to log in via ssh. If user is listed 70 * in DenyUsers or one of user's groups is listed in DenyGroups, false 71 * will be returned. If AllowUsers isn't empty and user isn't listed 72 * there, or if AllowGroups isn't empty and one of user's groups isn't 73 * listed there, false will be returned. 74 * If the user's shell is not executable, false will be returned. 75 * Otherwise true is returned. 76 */ 77 int 78 allowed_user(struct passwd * pw) 79 { 80 struct stat st; 81 const char *hostname = NULL, *ipaddr = NULL; 82 char *shell; 83 u_int i; 84 85 /* Shouldn't be called if pw is NULL, but better safe than sorry... */ 86 if (!pw || !pw->pw_name) 87 return 0; 88 89 /* 90 * Get the shell from the password data. An empty shell field is 91 * legal, and means /bin/sh. 92 */ 93 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; 94 95 /* deny if shell does not exists or is not executable */ 96 if (stat(shell, &st) != 0) { 97 logit("User %.100s not allowed because shell %.100s does not exist", 98 pw->pw_name, shell); 99 return 0; 100 } 101 if (S_ISREG(st.st_mode) == 0 || 102 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) { 103 logit("User %.100s not allowed because shell %.100s is not executable", 104 pw->pw_name, shell); 105 return 0; 106 } 107 108 if (options.num_deny_users > 0 || options.num_allow_users > 0 || 109 options.num_deny_groups > 0 || options.num_allow_groups > 0) { 110 hostname = get_canonical_hostname(options.use_dns); 111 ipaddr = get_remote_ipaddr(); 112 } 113 114 /* Return false if user is listed in DenyUsers */ 115 if (options.num_deny_users > 0) { 116 for (i = 0; i < options.num_deny_users; i++) 117 if (match_user(pw->pw_name, hostname, ipaddr, 118 options.deny_users[i])) { 119 logit("User %.100s from %.100s not allowed " 120 "because listed in DenyUsers", 121 pw->pw_name, hostname); 122 return 0; 123 } 124 } 125 /* Return false if AllowUsers isn't empty and user isn't listed there */ 126 if (options.num_allow_users > 0) { 127 for (i = 0; i < options.num_allow_users; i++) 128 if (match_user(pw->pw_name, hostname, ipaddr, 129 options.allow_users[i])) 130 break; 131 /* i < options.num_allow_users iff we break for loop */ 132 if (i >= options.num_allow_users) { 133 logit("User %.100s from %.100s not allowed because " 134 "not listed in AllowUsers", pw->pw_name, hostname); 135 return 0; 136 } 137 } 138 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { 139 /* Get the user's group access list (primary and supplementary) */ 140 if (ga_init(pw->pw_name, pw->pw_gid) == 0) { 141 logit("User %.100s from %.100s not allowed because " 142 "not in any group", pw->pw_name, hostname); 143 return 0; 144 } 145 146 /* Return false if one of user's groups is listed in DenyGroups */ 147 if (options.num_deny_groups > 0) 148 if (ga_match(options.deny_groups, 149 options.num_deny_groups)) { 150 ga_free(); 151 logit("User %.100s from %.100s not allowed " 152 "because a group is listed in DenyGroups", 153 pw->pw_name, hostname); 154 return 0; 155 } 156 /* 157 * Return false if AllowGroups isn't empty and one of user's groups 158 * isn't listed there 159 */ 160 if (options.num_allow_groups > 0) 161 if (!ga_match(options.allow_groups, 162 options.num_allow_groups)) { 163 ga_free(); 164 logit("User %.100s from %.100s not allowed " 165 "because none of user's groups are listed " 166 "in AllowGroups", pw->pw_name, hostname); 167 return 0; 168 } 169 ga_free(); 170 } 171 /* We found no reason not to let this user try to log on... */ 172 return 1; 173 } 174 175 void 176 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) 177 { 178 void (*authlog) (const char *fmt,...) = verbose; 179 char *authmsg; 180 181 if (use_privsep && !mm_is_monitor() && !authctxt->postponed) 182 return; 183 184 /* Raise logging level */ 185 if (authenticated == 1 || 186 !authctxt->valid || 187 authctxt->failures >= options.max_authtries / 2 || 188 strcmp(method, "password") == 0) 189 authlog = logit; 190 191 if (authctxt->postponed) 192 authmsg = "Postponed"; 193 else 194 authmsg = authenticated ? "Accepted" : "Failed"; 195 196 authlog("%s %s for %s%.100s from %.200s port %d%s", 197 authmsg, 198 method, 199 authctxt->valid ? "" : "invalid user ", 200 authctxt->user, 201 get_remote_ipaddr(), 202 get_remote_port(), 203 info); 204 } 205 206 /* 207 * Check whether root logins are disallowed. 208 */ 209 int 210 auth_root_allowed(char *method) 211 { 212 switch (options.permit_root_login) { 213 case PERMIT_YES: 214 return 1; 215 case PERMIT_NO_PASSWD: 216 if (strcmp(method, "password") != 0) 217 return 1; 218 break; 219 case PERMIT_FORCED_ONLY: 220 if (forced_command) { 221 logit("Root login accepted for forced command."); 222 return 1; 223 } 224 break; 225 } 226 logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); 227 return 0; 228 } 229 230 231 /* 232 * Given a template and a passwd structure, build a filename 233 * by substituting % tokenised options. Currently, %% becomes '%', 234 * %h becomes the home directory and %u the username. 235 * 236 * This returns a buffer allocated by xmalloc. 237 */ 238 static char * 239 expand_authorized_keys(const char *filename, struct passwd *pw) 240 { 241 char *file, ret[MAXPATHLEN]; 242 int i; 243 244 file = percent_expand(filename, "h", pw->pw_dir, 245 "u", pw->pw_name, (char *)NULL); 246 247 /* 248 * Ensure that filename starts anchored. If not, be backward 249 * compatible and prepend the '%h/' 250 */ 251 if (*file == '/') 252 return (file); 253 254 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file); 255 if (i < 0 || (size_t)i >= sizeof(ret)) 256 fatal("expand_authorized_keys: path too long"); 257 xfree(file); 258 return (xstrdup(ret)); 259 } 260 261 char * 262 authorized_keys_file(struct passwd *pw) 263 { 264 return expand_authorized_keys(options.authorized_keys_file, pw); 265 } 266 267 char * 268 authorized_keys_file2(struct passwd *pw) 269 { 270 return expand_authorized_keys(options.authorized_keys_file2, pw); 271 } 272 273 /* return ok if key exists in sysfile or userfile */ 274 HostStatus 275 check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host, 276 const char *sysfile, const char *userfile) 277 { 278 Key *found; 279 char *user_hostfile; 280 struct stat st; 281 HostStatus host_status; 282 283 /* Check if we know the host and its host key. */ 284 found = key_new(key->type); 285 host_status = check_host_in_hostfile(sysfile, host, key, found, NULL); 286 287 if (host_status != HOST_OK && userfile != NULL) { 288 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid); 289 if (options.strict_modes && 290 (stat(user_hostfile, &st) == 0) && 291 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) || 292 (st.st_mode & 022) != 0)) { 293 logit("Authentication refused for %.100s: " 294 "bad owner or modes for %.200s", 295 pw->pw_name, user_hostfile); 296 } else { 297 temporarily_use_uid(pw); 298 host_status = check_host_in_hostfile(user_hostfile, 299 host, key, found, NULL); 300 restore_uid(); 301 } 302 xfree(user_hostfile); 303 } 304 key_free(found); 305 306 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ? 307 "ok" : "not found", host); 308 return host_status; 309 } 310 311 312 /* 313 * Check a given file for security. This is defined as all components 314 * of the path to the file must be owned by either the owner of 315 * of the file or root and no directories must be group or world writable. 316 * 317 * XXX Should any specific check be done for sym links ? 318 * 319 * Takes an open file descriptor, the file name, a uid and and 320 * error buffer plus max size as arguments. 321 * 322 * Returns 0 on success and -1 on failure 323 */ 324 static int 325 secure_filename(FILE *f, const char *file, struct passwd *pw, 326 char *err, size_t errlen) 327 { 328 uid_t uid = pw->pw_uid; 329 char buf[MAXPATHLEN], homedir[MAXPATHLEN]; 330 char *cp; 331 int comparehome = 0; 332 struct stat st; 333 334 if (realpath(file, buf) == NULL) { 335 snprintf(err, errlen, "realpath %s failed: %s", file, 336 strerror(errno)); 337 return -1; 338 } 339 if (realpath(pw->pw_dir, homedir) != NULL) 340 comparehome = 1; 341 342 /* check the open file to avoid races */ 343 if (fstat(fileno(f), &st) < 0 || 344 (st.st_uid != 0 && st.st_uid != uid) || 345 (st.st_mode & 022) != 0) { 346 snprintf(err, errlen, "bad ownership or modes for file %s", 347 buf); 348 return -1; 349 } 350 351 /* for each component of the canonical path, walking upwards */ 352 for (;;) { 353 if ((cp = dirname(buf)) == NULL) { 354 snprintf(err, errlen, "dirname() failed"); 355 return -1; 356 } 357 strlcpy(buf, cp, sizeof(buf)); 358 359 debug3("secure_filename: checking '%s'", buf); 360 if (stat(buf, &st) < 0 || 361 (st.st_uid != 0 && st.st_uid != uid) || 362 (st.st_mode & 022) != 0) { 363 snprintf(err, errlen, 364 "bad ownership or modes for directory %s", buf); 365 return -1; 366 } 367 368 /* If are passed the homedir then we can stop */ 369 if (comparehome && strcmp(homedir, buf) == 0) { 370 debug3("secure_filename: terminating check at '%s'", 371 buf); 372 break; 373 } 374 /* 375 * dirname should always complete with a "/" path, 376 * but we can be paranoid and check for "." too 377 */ 378 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0)) 379 break; 380 } 381 return 0; 382 } 383 384 FILE * 385 auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes) 386 { 387 char line[1024]; 388 struct stat st; 389 int fd; 390 FILE *f; 391 392 /* 393 * Open the file containing the authorized keys 394 * Fail quietly if file does not exist 395 */ 396 if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) 397 return NULL; 398 399 if (fstat(fd, &st) < 0) { 400 close(fd); 401 return NULL; 402 } 403 if (!S_ISREG(st.st_mode)) { 404 logit("User %s authorized keys %s is not a regular file", 405 pw->pw_name, file); 406 close(fd); 407 return NULL; 408 } 409 unset_nonblock(fd); 410 if ((f = fdopen(fd, "r")) == NULL) { 411 close(fd); 412 return NULL; 413 } 414 if (options.strict_modes && 415 secure_filename(f, file, pw, line, sizeof(line)) != 0) { 416 fclose(f); 417 logit("Authentication refused: %s", line); 418 return NULL; 419 } 420 421 return f; 422 } 423 424 struct passwd * 425 getpwnamallow(const char *user) 426 { 427 extern login_cap_t *lc; 428 auth_session_t *as; 429 struct passwd *pw; 430 431 parse_server_match_config(&options, user, 432 get_canonical_hostname(options.use_dns), get_remote_ipaddr()); 433 434 pw = getpwnam(user); 435 if (pw == NULL) { 436 logit("Invalid user %.100s from %.100s", 437 user, get_remote_ipaddr()); 438 return (NULL); 439 } 440 if (!allowed_user(pw)) 441 return (NULL); 442 if ((lc = login_getclass(pw->pw_class)) == NULL) { 443 debug("unable to get login class: %s", user); 444 return (NULL); 445 } 446 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 || 447 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) { 448 debug("Approval failure for %s", user); 449 pw = NULL; 450 } 451 if (as != NULL) 452 auth_close(as); 453 if (pw != NULL) 454 return (pwcopy(pw)); 455 return (NULL); 456 } 457 458 void 459 auth_debug_add(const char *fmt,...) 460 { 461 char buf[1024]; 462 va_list args; 463 464 if (!auth_debug_init) 465 return; 466 467 va_start(args, fmt); 468 vsnprintf(buf, sizeof(buf), fmt, args); 469 va_end(args); 470 buffer_put_cstring(&auth_debug, buf); 471 } 472 473 void 474 auth_debug_send(void) 475 { 476 char *msg; 477 478 if (!auth_debug_init) 479 return; 480 while (buffer_len(&auth_debug)) { 481 msg = buffer_get_string(&auth_debug, NULL); 482 packet_send_debug("%s", msg); 483 xfree(msg); 484 } 485 } 486 487 void 488 auth_debug_reset(void) 489 { 490 if (auth_debug_init) 491 buffer_clear(&auth_debug); 492 else { 493 buffer_init(&auth_debug); 494 auth_debug_init = 1; 495 } 496 } 497 498 struct passwd * 499 fakepw(void) 500 { 501 static struct passwd fake; 502 503 memset(&fake, 0, sizeof(fake)); 504 fake.pw_name = "NOUSER"; 505 fake.pw_passwd = 506 "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK"; 507 fake.pw_gecos = "NOUSER"; 508 fake.pw_uid = (uid_t)-1; 509 fake.pw_gid = (gid_t)-1; 510 fake.pw_class = ""; 511 fake.pw_dir = "/nonexist"; 512 fake.pw_shell = "/nonexist"; 513 514 return (&fake); 515 } 516