Annotation of src/usr.bin/ssh/auth.c, Revision 1.99
1.99 ! dtucker 1: /* $OpenBSD: auth.c,v 1.98 2012/12/02 20:34:09 djm Exp $ */
1.1 markus 2: /*
1.19 deraadt 3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.9 deraadt 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.
1.1 markus 24: */
25:
1.62 stevesk 26: #include <sys/types.h>
27: #include <sys/stat.h>
1.73 stevesk 28: #include <sys/param.h>
1.22 markus 29:
1.70 stevesk 30: #include <errno.h>
1.79 dtucker 31: #include <fcntl.h>
1.22 markus 32: #include <libgen.h>
1.77 djm 33: #include <login_cap.h>
1.61 stevesk 34: #include <paths.h>
1.68 stevesk 35: #include <pwd.h>
1.69 stevesk 36: #include <stdarg.h>
1.74 stevesk 37: #include <stdio.h>
1.72 stevesk 38: #include <string.h>
1.80 djm 39: #include <unistd.h>
1.1 markus 40:
41: #include "xmalloc.h"
1.13 markus 42: #include "match.h"
1.14 markus 43: #include "groupaccess.h"
44: #include "log.h"
1.75 deraadt 45: #include "buffer.h"
1.1 markus 46: #include "servconf.h"
1.75 deraadt 47: #include "key.h"
48: #include "hostfile.h"
1.2 markus 49: #include "auth.h"
1.13 markus 50: #include "auth-options.h"
1.14 markus 51: #include "canohost.h"
1.24 markus 52: #include "uidswap.h"
1.40 markus 53: #include "misc.h"
1.42 markus 54: #include "packet.h"
1.75 deraadt 55: #ifdef GSSAPI
56: #include "ssh-gss.h"
57: #endif
1.85 djm 58: #include "authfile.h"
1.67 dtucker 59: #include "monitor_wrap.h"
1.2 markus 60:
1.1 markus 61: /* import */
62: extern ServerOptions options;
1.67 dtucker 63: extern int use_privsep;
1.1 markus 64:
1.42 markus 65: /* Debugging messages */
66: Buffer auth_debug;
67: int auth_debug_init;
68:
1.1 markus 69: /*
1.12 markus 70: * Check if the user is allowed to log in via ssh. If user is listed
71: * in DenyUsers or one of user's groups is listed in DenyGroups, false
72: * will be returned. If AllowUsers isn't empty and user isn't listed
73: * there, or if AllowGroups isn't empty and one of user's groups isn't
74: * listed there, false will be returned.
1.1 markus 75: * If the user's shell is not executable, false will be returned.
1.4 markus 76: * Otherwise true is returned.
1.1 markus 77: */
1.5 markus 78: int
1.1 markus 79: allowed_user(struct passwd * pw)
80: {
81: struct stat st;
1.35 markus 82: const char *hostname = NULL, *ipaddr = NULL;
1.60 djm 83: u_int i;
1.1 markus 84:
85: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 markus 86: if (!pw || !pw->pw_name)
1.1 markus 87: return 0;
88:
1.7 deraadt 89: /*
1.84 djm 90: * Deny if shell does not exist or is not executable unless we
91: * are chrooting.
1.7 deraadt 92: */
1.84 djm 93: if (options.chroot_directory == NULL ||
94: strcasecmp(options.chroot_directory, "none") == 0) {
95: char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
96: _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
97:
98: if (stat(shell, &st) != 0) {
99: logit("User %.100s not allowed because shell %.100s "
100: "does not exist", pw->pw_name, shell);
101: xfree(shell);
102: return 0;
103: }
104: if (S_ISREG(st.st_mode) == 0 ||
105: (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
106: logit("User %.100s not allowed because shell %.100s "
107: "is not executable", pw->pw_name, shell);
108: xfree(shell);
109: return 0;
110: }
1.83 djm 111: xfree(shell);
1.34 stevesk 112: }
1.1 markus 113:
1.58 dtucker 114: if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
115: options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.48 markus 116: hostname = get_canonical_hostname(options.use_dns);
1.35 markus 117: ipaddr = get_remote_ipaddr();
118: }
119:
1.1 markus 120: /* Return false if user is listed in DenyUsers */
121: if (options.num_deny_users > 0) {
122: for (i = 0; i < options.num_deny_users; i++)
1.39 markus 123: if (match_user(pw->pw_name, hostname, ipaddr,
1.34 stevesk 124: options.deny_users[i])) {
1.57 dtucker 125: logit("User %.100s from %.100s not allowed "
126: "because listed in DenyUsers",
127: pw->pw_name, hostname);
1.1 markus 128: return 0;
1.34 stevesk 129: }
1.1 markus 130: }
131: /* Return false if AllowUsers isn't empty and user isn't listed there */
132: if (options.num_allow_users > 0) {
133: for (i = 0; i < options.num_allow_users; i++)
1.39 markus 134: if (match_user(pw->pw_name, hostname, ipaddr,
1.26 markus 135: options.allow_users[i]))
1.1 markus 136: break;
137: /* i < options.num_allow_users iff we break for loop */
1.34 stevesk 138: if (i >= options.num_allow_users) {
1.57 dtucker 139: logit("User %.100s from %.100s not allowed because "
140: "not listed in AllowUsers", pw->pw_name, hostname);
1.1 markus 141: return 0;
1.34 stevesk 142: }
1.1 markus 143: }
144: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 145: /* Get the user's group access list (primary and supplementary) */
1.34 stevesk 146: if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57 dtucker 147: logit("User %.100s from %.100s not allowed because "
148: "not in any group", pw->pw_name, hostname);
1.1 markus 149: return 0;
1.34 stevesk 150: }
1.1 markus 151:
1.12 markus 152: /* Return false if one of user's groups is listed in DenyGroups */
153: if (options.num_deny_groups > 0)
154: if (ga_match(options.deny_groups,
155: options.num_deny_groups)) {
156: ga_free();
1.57 dtucker 157: logit("User %.100s from %.100s not allowed "
158: "because a group is listed in DenyGroups",
159: pw->pw_name, hostname);
1.1 markus 160: return 0;
1.12 markus 161: }
1.1 markus 162: /*
1.12 markus 163: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 164: * isn't listed there
165: */
1.12 markus 166: if (options.num_allow_groups > 0)
167: if (!ga_match(options.allow_groups,
168: options.num_allow_groups)) {
169: ga_free();
1.57 dtucker 170: logit("User %.100s from %.100s not allowed "
171: "because none of user's groups are listed "
172: "in AllowGroups", pw->pw_name, hostname);
1.1 markus 173: return 0;
1.12 markus 174: }
175: ga_free();
1.1 markus 176: }
177: /* We found no reason not to let this user try to log on... */
178: return 1;
1.13 markus 179: }
180:
181: void
1.98 djm 182: auth_log(Authctxt *authctxt, int authenticated, int partial,
183: const char *method, const char *submethod, const char *info)
1.13 markus 184: {
185: void (*authlog) (const char *fmt,...) = verbose;
186: char *authmsg;
1.67 dtucker 187:
188: if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
189: return;
1.13 markus 190:
191: /* Raise logging level */
192: if (authenticated == 1 ||
193: !authctxt->valid ||
1.54 dtucker 194: authctxt->failures >= options.max_authtries / 2 ||
1.13 markus 195: strcmp(method, "password") == 0)
1.47 itojun 196: authlog = logit;
1.13 markus 197:
198: if (authctxt->postponed)
199: authmsg = "Postponed";
1.98 djm 200: else if (partial)
201: authmsg = "Partial";
1.13 markus 202: else
203: authmsg = authenticated ? "Accepted" : "Failed";
204:
1.98 djm 205: authlog("%s %s%s%s for %s%.100s from %.200s port %d%s",
1.13 markus 206: authmsg,
207: method,
1.98 djm 208: submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56 markus 209: authctxt->valid ? "" : "invalid user ",
1.29 markus 210: authctxt->user,
1.13 markus 211: get_remote_ipaddr(),
212: get_remote_port(),
213: info);
214: }
215:
216: /*
1.17 markus 217: * Check whether root logins are disallowed.
1.13 markus 218: */
219: int
1.98 djm 220: auth_root_allowed(const char *method)
1.13 markus 221: {
1.17 markus 222: switch (options.permit_root_login) {
223: case PERMIT_YES:
1.13 markus 224: return 1;
1.17 markus 225: case PERMIT_NO_PASSWD:
226: if (strcmp(method, "password") != 0)
227: return 1;
228: break;
229: case PERMIT_FORCED_ONLY:
230: if (forced_command) {
1.47 itojun 231: logit("Root login accepted for forced command.");
1.17 markus 232: return 1;
233: }
234: break;
1.13 markus 235: }
1.47 itojun 236: logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22 markus 237: return 0;
238: }
239:
240:
241: /*
242: * Given a template and a passwd structure, build a filename
243: * by substituting % tokenised options. Currently, %% becomes '%',
244: * %h becomes the home directory and %u the username.
245: *
246: * This returns a buffer allocated by xmalloc.
247: */
1.93 djm 248: char *
1.59 djm 249: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22 markus 250: {
1.65 djm 251: char *file, ret[MAXPATHLEN];
252: int i;
1.22 markus 253:
1.59 djm 254: file = percent_expand(filename, "h", pw->pw_dir,
255: "u", pw->pw_name, (char *)NULL);
1.22 markus 256:
257: /*
258: * Ensure that filename starts anchored. If not, be backward
259: * compatible and prepend the '%h/'
260: */
1.59 djm 261: if (*file == '/')
262: return (file);
263:
1.65 djm 264: i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
265: if (i < 0 || (size_t)i >= sizeof(ret))
1.59 djm 266: fatal("expand_authorized_keys: path too long");
267: xfree(file);
1.65 djm 268: return (xstrdup(ret));
1.22 markus 269: }
1.24 markus 270:
1.87 djm 271: char *
272: authorized_principals_file(struct passwd *pw)
273: {
1.95 djm 274: if (options.authorized_principals_file == NULL ||
275: strcasecmp(options.authorized_principals_file, "none") == 0)
1.87 djm 276: return NULL;
277: return expand_authorized_keys(options.authorized_principals_file, pw);
278: }
279:
1.24 markus 280: /* return ok if key exists in sysfile or userfile */
281: HostStatus
282: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
283: const char *sysfile, const char *userfile)
284: {
285: char *user_hostfile;
286: struct stat st;
1.30 stevesk 287: HostStatus host_status;
1.91 djm 288: struct hostkeys *hostkeys;
289: const struct hostkey_entry *found;
1.24 markus 290:
1.91 djm 291: hostkeys = init_hostkeys();
292: load_hostkeys(hostkeys, host, sysfile);
293: if (userfile != NULL) {
1.24 markus 294: user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
295: if (options.strict_modes &&
296: (stat(user_hostfile, &st) == 0) &&
297: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31 deraadt 298: (st.st_mode & 022) != 0)) {
1.47 itojun 299: logit("Authentication refused for %.100s: "
1.24 markus 300: "bad owner or modes for %.200s",
301: pw->pw_name, user_hostfile);
1.88 djm 302: auth_debug_add("Ignored %.200s: bad ownership or modes",
303: user_hostfile);
1.24 markus 304: } else {
305: temporarily_use_uid(pw);
1.91 djm 306: load_hostkeys(hostkeys, host, user_hostfile);
1.24 markus 307: restore_uid();
308: }
309: xfree(user_hostfile);
310: }
1.91 djm 311: host_status = check_key_in_hostkeys(hostkeys, key, &found);
312: if (host_status == HOST_REVOKED)
313: error("WARNING: revoked key for %s attempted authentication",
314: found->host);
315: else if (host_status == HOST_OK)
316: debug("%s: key for %s found at %s:%ld", __func__,
317: found->host, found->file, found->line);
318: else
319: debug("%s: key for host %s not found", __func__, host);
320:
321: free_hostkeys(hostkeys);
1.24 markus 322:
323: return host_status;
324: }
325:
1.22 markus 326: /*
1.97 djm 327: * Check a given path for security. This is defined as all components
1.44 stevesk 328: * of the path to the file must be owned by either the owner of
1.23 markus 329: * of the file or root and no directories must be group or world writable.
1.22 markus 330: *
331: * XXX Should any specific check be done for sym links ?
332: *
1.97 djm 333: * Takes an the file name, its stat information (preferably from fstat() to
334: * avoid races), the uid of the expected owner, their home directory and an
1.22 markus 335: * error buffer plus max size as arguments.
336: *
337: * Returns 0 on success and -1 on failure
338: */
1.97 djm 339: int
340: auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
341: uid_t uid, char *err, size_t errlen)
1.22 markus 342: {
1.28 markus 343: char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22 markus 344: char *cp;
1.46 markus 345: int comparehome = 0;
1.22 markus 346: struct stat st;
347:
1.97 djm 348: if (realpath(name, buf) == NULL) {
349: snprintf(err, errlen, "realpath %s failed: %s", name,
1.22 markus 350: strerror(errno));
351: return -1;
352: }
1.97 djm 353: if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL)
1.46 markus 354: comparehome = 1;
1.22 markus 355:
1.97 djm 356: if (!S_ISREG(stp->st_mode)) {
357: snprintf(err, errlen, "%s is not a regular file", buf);
358: return -1;
359: }
360: if ((stp->st_uid != 0 && stp->st_uid != uid) ||
361: (stp->st_mode & 022) != 0) {
1.22 markus 362: snprintf(err, errlen, "bad ownership or modes for file %s",
363: buf);
364: return -1;
365: }
366:
367: /* for each component of the canonical path, walking upwards */
368: for (;;) {
369: if ((cp = dirname(buf)) == NULL) {
370: snprintf(err, errlen, "dirname() failed");
371: return -1;
372: }
373: strlcpy(buf, cp, sizeof(buf));
1.25 provos 374:
1.22 markus 375: if (stat(buf, &st) < 0 ||
376: (st.st_uid != 0 && st.st_uid != uid) ||
377: (st.st_mode & 022) != 0) {
1.31 deraadt 378: snprintf(err, errlen,
1.22 markus 379: "bad ownership or modes for directory %s", buf);
380: return -1;
381: }
382:
1.82 dtucker 383: /* If are past the homedir then we can stop */
1.94 djm 384: if (comparehome && strcmp(homedir, buf) == 0)
1.27 markus 385: break;
1.94 djm 386:
1.22 markus 387: /*
388: * dirname should always complete with a "/" path,
389: * but we can be paranoid and check for "." too
390: */
391: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
392: break;
393: }
1.17 markus 394: return 0;
1.97 djm 395: }
396:
397: /*
398: * Version of secure_path() that accepts an open file descriptor to
399: * avoid races.
400: *
401: * Returns 0 on success and -1 on failure
402: */
403: static int
404: secure_filename(FILE *f, const char *file, struct passwd *pw,
405: char *err, size_t errlen)
406: {
407: struct stat st;
408:
409: /* check the open file to avoid races */
410: if (fstat(fileno(f), &st) < 0) {
411: snprintf(err, errlen, "cannot stat file %s: %s",
1.99 ! dtucker 412: file, strerror(errno));
1.97 djm 413: return -1;
414: }
415: return auth_secure_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen);
1.79 dtucker 416: }
417:
1.87 djm 418: static FILE *
419: auth_openfile(const char *file, struct passwd *pw, int strict_modes,
420: int log_missing, char *file_type)
1.79 dtucker 421: {
422: char line[1024];
423: struct stat st;
424: int fd;
425: FILE *f;
426:
1.81 dtucker 427: if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
1.87 djm 428: if (log_missing || errno != ENOENT)
429: debug("Could not open %s '%s': %s", file_type, file,
1.81 dtucker 430: strerror(errno));
1.79 dtucker 431: return NULL;
1.81 dtucker 432: }
1.79 dtucker 433:
434: if (fstat(fd, &st) < 0) {
435: close(fd);
436: return NULL;
437: }
438: if (!S_ISREG(st.st_mode)) {
1.87 djm 439: logit("User %s %s %s is not a regular file",
440: pw->pw_name, file_type, file);
1.79 dtucker 441: close(fd);
442: return NULL;
443: }
444: unset_nonblock(fd);
445: if ((f = fdopen(fd, "r")) == NULL) {
446: close(fd);
447: return NULL;
448: }
1.90 djm 449: if (strict_modes &&
1.79 dtucker 450: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
451: fclose(f);
452: logit("Authentication refused: %s", line);
1.88 djm 453: auth_debug_add("Ignored %s: %s", file_type, line);
1.79 dtucker 454: return NULL;
455: }
456:
457: return f;
1.87 djm 458: }
459:
460:
461: FILE *
462: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
463: {
464: return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
465: }
466:
467: FILE *
468: auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
469: {
470: return auth_openfile(file, pw, strict_modes, 0,
471: "authorized principals");
1.37 provos 472: }
473:
474: struct passwd *
475: getpwnamallow(const char *user)
476: {
1.38 provos 477: extern login_cap_t *lc;
478: auth_session_t *as;
1.37 provos 479: struct passwd *pw;
1.96 dtucker 480: struct connection_info *ci = get_connection_info(1, options.use_dns);
1.71 dtucker 481:
1.96 dtucker 482: ci->user = user;
483: parse_server_match_config(&options, ci);
1.37 provos 484:
485: pw = getpwnam(user);
1.45 stevesk 486: if (pw == NULL) {
1.55 markus 487: logit("Invalid user %.100s from %.100s",
1.45 stevesk 488: user, get_remote_ipaddr());
489: return (NULL);
490: }
491: if (!allowed_user(pw))
1.38 provos 492: return (NULL);
493: if ((lc = login_getclass(pw->pw_class)) == NULL) {
494: debug("unable to get login class: %s", user);
495: return (NULL);
496: }
497: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43 millert 498: auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38 provos 499: debug("Approval failure for %s", user);
1.37 provos 500: pw = NULL;
1.38 provos 501: }
502: if (as != NULL)
503: auth_close(as);
1.41 markus 504: if (pw != NULL)
505: return (pwcopy(pw));
506: return (NULL);
1.85 djm 507: }
508:
509: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
510: int
511: auth_key_is_revoked(Key *key)
512: {
513: char *key_fp;
514:
515: if (options.revoked_keys_file == NULL)
516: return 0;
517:
518: switch (key_in_file(key, options.revoked_keys_file, 0)) {
519: case 0:
520: /* key not revoked */
521: return 0;
522: case -1:
523: /* Error opening revoked_keys_file: refuse all keys */
524: error("Revoked keys file is unreadable: refusing public key "
525: "authentication");
526: return 1;
527: case 1:
528: /* Key revoked */
529: key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1.86 djm 530: error("WARNING: authentication attempt with a revoked "
531: "%s key %s ", key_type(key), key_fp);
1.85 djm 532: xfree(key_fp);
533: return 1;
534: }
535: fatal("key_in_file returned junk");
1.42 markus 536: }
537:
538: void
539: auth_debug_add(const char *fmt,...)
540: {
541: char buf[1024];
542: va_list args;
543:
544: if (!auth_debug_init)
545: return;
546:
547: va_start(args, fmt);
548: vsnprintf(buf, sizeof(buf), fmt, args);
549: va_end(args);
550: buffer_put_cstring(&auth_debug, buf);
551: }
552:
553: void
554: auth_debug_send(void)
555: {
556: char *msg;
557:
558: if (!auth_debug_init)
559: return;
560: while (buffer_len(&auth_debug)) {
561: msg = buffer_get_string(&auth_debug, NULL);
562: packet_send_debug("%s", msg);
563: xfree(msg);
564: }
565: }
566:
567: void
568: auth_debug_reset(void)
569: {
570: if (auth_debug_init)
571: buffer_clear(&auth_debug);
572: else {
573: buffer_init(&auth_debug);
574: auth_debug_init = 1;
575: }
1.49 markus 576: }
577:
578: struct passwd *
579: fakepw(void)
580: {
581: static struct passwd fake;
582:
583: memset(&fake, 0, sizeof(fake));
584: fake.pw_name = "NOUSER";
585: fake.pw_passwd =
1.51 djm 586: "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49 markus 587: fake.pw_gecos = "NOUSER";
1.53 deraadt 588: fake.pw_uid = (uid_t)-1;
589: fake.pw_gid = (gid_t)-1;
1.49 markus 590: fake.pw_class = "";
591: fake.pw_dir = "/nonexist";
592: fake.pw_shell = "/nonexist";
593:
594: return (&fake);
1.1 markus 595: }