Annotation of src/usr.bin/ssh/auth.c, Revision 1.117
1.117 ! djm 1: /* $OpenBSD: auth.c,v 1.116 2016/08/13 17:47:41 markus 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.114 djm 28: #include <sys/socket.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.109 deraadt 40: #include <limits.h>
1.114 djm 41: #include <netdb.h>
1.1 markus 42:
43: #include "xmalloc.h"
1.13 markus 44: #include "match.h"
1.14 markus 45: #include "groupaccess.h"
46: #include "log.h"
1.75 deraadt 47: #include "buffer.h"
1.106 millert 48: #include "misc.h"
1.1 markus 49: #include "servconf.h"
1.75 deraadt 50: #include "key.h"
51: #include "hostfile.h"
1.2 markus 52: #include "auth.h"
1.13 markus 53: #include "auth-options.h"
1.14 markus 54: #include "canohost.h"
1.24 markus 55: #include "uidswap.h"
1.42 markus 56: #include "packet.h"
1.75 deraadt 57: #ifdef GSSAPI
58: #include "ssh-gss.h"
59: #endif
1.85 djm 60: #include "authfile.h"
1.67 dtucker 61: #include "monitor_wrap.h"
1.107 djm 62: #include "authfile.h"
63: #include "ssherr.h"
1.103 djm 64: #include "compat.h"
1.2 markus 65:
1.1 markus 66: /* import */
67: extern ServerOptions options;
1.67 dtucker 68: extern int use_privsep;
1.1 markus 69:
1.42 markus 70: /* Debugging messages */
71: Buffer auth_debug;
72: int auth_debug_init;
73:
1.1 markus 74: /*
1.12 markus 75: * Check if the user is allowed to log in via ssh. If user is listed
76: * in DenyUsers or one of user's groups is listed in DenyGroups, false
77: * will be returned. If AllowUsers isn't empty and user isn't listed
78: * there, or if AllowGroups isn't empty and one of user's groups isn't
79: * listed there, false will be returned.
1.1 markus 80: * If the user's shell is not executable, false will be returned.
1.4 markus 81: * Otherwise true is returned.
1.1 markus 82: */
1.5 markus 83: int
1.1 markus 84: allowed_user(struct passwd * pw)
85: {
1.114 djm 86: struct ssh *ssh = active_state; /* XXX */
1.1 markus 87: struct stat st;
1.35 markus 88: const char *hostname = NULL, *ipaddr = NULL;
1.117 ! djm 89: int r;
1.60 djm 90: u_int i;
1.1 markus 91:
92: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 markus 93: if (!pw || !pw->pw_name)
1.1 markus 94: return 0;
95:
1.7 deraadt 96: /*
1.84 djm 97: * Deny if shell does not exist or is not executable unless we
98: * are chrooting.
1.7 deraadt 99: */
1.84 djm 100: if (options.chroot_directory == NULL ||
101: strcasecmp(options.chroot_directory, "none") == 0) {
102: char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
103: _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
104:
105: if (stat(shell, &st) != 0) {
106: logit("User %.100s not allowed because shell %.100s "
107: "does not exist", pw->pw_name, shell);
1.102 djm 108: free(shell);
1.84 djm 109: return 0;
110: }
111: if (S_ISREG(st.st_mode) == 0 ||
112: (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
113: logit("User %.100s not allowed because shell %.100s "
114: "is not executable", pw->pw_name, shell);
1.102 djm 115: free(shell);
1.84 djm 116: return 0;
117: }
1.102 djm 118: free(shell);
1.34 stevesk 119: }
1.1 markus 120:
1.58 dtucker 121: if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
122: options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.114 djm 123: hostname = auth_get_canonical_hostname(ssh, options.use_dns);
124: ipaddr = ssh_remote_ipaddr(ssh);
1.35 markus 125: }
126:
1.1 markus 127: /* Return false if user is listed in DenyUsers */
128: if (options.num_deny_users > 0) {
129: for (i = 0; i < options.num_deny_users; i++)
1.117 ! djm 130: r = match_user(pw->pw_name, hostname, ipaddr,
! 131: options.deny_users[i]);
! 132: if (r < 0) {
! 133: fatal("Invalid DenyUsers pattern \"%.100s\"",
! 134: options.deny_users[i]);
! 135: } else if (r != 1) {
1.57 dtucker 136: logit("User %.100s from %.100s not allowed "
137: "because listed in DenyUsers",
138: pw->pw_name, hostname);
1.1 markus 139: return 0;
1.34 stevesk 140: }
1.1 markus 141: }
142: /* Return false if AllowUsers isn't empty and user isn't listed there */
143: if (options.num_allow_users > 0) {
1.117 ! djm 144: for (i = 0; i < options.num_allow_users; i++) {
! 145: r = match_user(pw->pw_name, hostname, ipaddr,
! 146: options.allow_users[i]);
! 147: if (r < 0) {
! 148: fatal("Invalid AllowUsers pattern \"%.100s\"",
! 149: options.allow_users[i]);
! 150: } else if (r == 1)
1.1 markus 151: break;
1.117 ! djm 152: }
1.1 markus 153: /* i < options.num_allow_users iff we break for loop */
1.34 stevesk 154: if (i >= options.num_allow_users) {
1.57 dtucker 155: logit("User %.100s from %.100s not allowed because "
156: "not listed in AllowUsers", pw->pw_name, hostname);
1.1 markus 157: return 0;
1.34 stevesk 158: }
1.1 markus 159: }
160: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 161: /* Get the user's group access list (primary and supplementary) */
1.34 stevesk 162: if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57 dtucker 163: logit("User %.100s from %.100s not allowed because "
164: "not in any group", pw->pw_name, hostname);
1.1 markus 165: return 0;
1.34 stevesk 166: }
1.1 markus 167:
1.12 markus 168: /* Return false if one of user's groups is listed in DenyGroups */
169: if (options.num_deny_groups > 0)
170: if (ga_match(options.deny_groups,
171: options.num_deny_groups)) {
172: ga_free();
1.57 dtucker 173: logit("User %.100s from %.100s not allowed "
174: "because a group is listed in DenyGroups",
175: pw->pw_name, hostname);
1.1 markus 176: return 0;
1.12 markus 177: }
1.1 markus 178: /*
1.12 markus 179: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 180: * isn't listed there
181: */
1.12 markus 182: if (options.num_allow_groups > 0)
183: if (!ga_match(options.allow_groups,
184: options.num_allow_groups)) {
185: ga_free();
1.57 dtucker 186: logit("User %.100s from %.100s not allowed "
187: "because none of user's groups are listed "
188: "in AllowGroups", pw->pw_name, hostname);
1.1 markus 189: return 0;
1.12 markus 190: }
191: ga_free();
1.1 markus 192: }
193: /* We found no reason not to let this user try to log on... */
194: return 1;
1.13 markus 195: }
196:
197: void
1.103 djm 198: auth_info(Authctxt *authctxt, const char *fmt, ...)
199: {
200: va_list ap;
201: int i;
202:
203: free(authctxt->info);
204: authctxt->info = NULL;
205:
206: va_start(ap, fmt);
207: i = vasprintf(&authctxt->info, fmt, ap);
208: va_end(ap);
209:
210: if (i < 0 || authctxt->info == NULL)
211: fatal("vasprintf failed");
212: }
213:
214: void
1.98 djm 215: auth_log(Authctxt *authctxt, int authenticated, int partial,
1.103 djm 216: const char *method, const char *submethod)
1.13 markus 217: {
1.114 djm 218: struct ssh *ssh = active_state; /* XXX */
1.13 markus 219: void (*authlog) (const char *fmt,...) = verbose;
220: char *authmsg;
1.67 dtucker 221:
222: if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
223: return;
1.13 markus 224:
225: /* Raise logging level */
226: if (authenticated == 1 ||
227: !authctxt->valid ||
1.54 dtucker 228: authctxt->failures >= options.max_authtries / 2 ||
1.13 markus 229: strcmp(method, "password") == 0)
1.47 itojun 230: authlog = logit;
1.13 markus 231:
232: if (authctxt->postponed)
233: authmsg = "Postponed";
1.98 djm 234: else if (partial)
235: authmsg = "Partial";
1.13 markus 236: else
237: authmsg = authenticated ? "Accepted" : "Failed";
238:
1.116 markus 239: authlog("%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
1.13 markus 240: authmsg,
241: method,
1.98 djm 242: submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
1.56 markus 243: authctxt->valid ? "" : "invalid user ",
1.29 markus 244: authctxt->user,
1.114 djm 245: ssh_remote_ipaddr(ssh),
246: ssh_remote_port(ssh),
1.103 djm 247: authctxt->info != NULL ? ": " : "",
248: authctxt->info != NULL ? authctxt->info : "");
249: free(authctxt->info);
250: authctxt->info = NULL;
1.105 djm 251: }
252:
253: void
254: auth_maxtries_exceeded(Authctxt *authctxt)
255: {
1.114 djm 256: struct ssh *ssh = active_state; /* XXX */
257:
1.110 djm 258: error("maximum authentication attempts exceeded for "
1.116 markus 259: "%s%.100s from %.200s port %d ssh2",
1.105 djm 260: authctxt->valid ? "" : "invalid user ",
261: authctxt->user,
1.114 djm 262: ssh_remote_ipaddr(ssh),
1.116 markus 263: ssh_remote_port(ssh));
1.110 djm 264: packet_disconnect("Too many authentication failures");
1.105 djm 265: /* NOTREACHED */
1.13 markus 266: }
267:
268: /*
1.17 markus 269: * Check whether root logins are disallowed.
1.13 markus 270: */
271: int
1.98 djm 272: auth_root_allowed(const char *method)
1.13 markus 273: {
1.114 djm 274: struct ssh *ssh = active_state; /* XXX */
275:
1.17 markus 276: switch (options.permit_root_login) {
277: case PERMIT_YES:
1.13 markus 278: return 1;
1.17 markus 279: case PERMIT_NO_PASSWD:
1.112 deraadt 280: if (strcmp(method, "publickey") == 0 ||
281: strcmp(method, "hostbased") == 0 ||
1.113 djm 282: strcmp(method, "gssapi-with-mic") == 0)
1.17 markus 283: return 1;
284: break;
285: case PERMIT_FORCED_ONLY:
286: if (forced_command) {
1.47 itojun 287: logit("Root login accepted for forced command.");
1.17 markus 288: return 1;
289: }
290: break;
1.13 markus 291: }
1.114 djm 292: logit("ROOT LOGIN REFUSED FROM %.200s port %d",
293: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.22 markus 294: return 0;
295: }
296:
297:
298: /*
299: * Given a template and a passwd structure, build a filename
300: * by substituting % tokenised options. Currently, %% becomes '%',
301: * %h becomes the home directory and %u the username.
302: *
303: * This returns a buffer allocated by xmalloc.
304: */
1.93 djm 305: char *
1.59 djm 306: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22 markus 307: {
1.109 deraadt 308: char *file, ret[PATH_MAX];
1.65 djm 309: int i;
1.22 markus 310:
1.59 djm 311: file = percent_expand(filename, "h", pw->pw_dir,
312: "u", pw->pw_name, (char *)NULL);
1.22 markus 313:
314: /*
315: * Ensure that filename starts anchored. If not, be backward
316: * compatible and prepend the '%h/'
317: */
1.59 djm 318: if (*file == '/')
319: return (file);
320:
1.65 djm 321: i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
322: if (i < 0 || (size_t)i >= sizeof(ret))
1.59 djm 323: fatal("expand_authorized_keys: path too long");
1.102 djm 324: free(file);
1.65 djm 325: return (xstrdup(ret));
1.22 markus 326: }
1.24 markus 327:
1.87 djm 328: char *
329: authorized_principals_file(struct passwd *pw)
330: {
1.111 djm 331: if (options.authorized_principals_file == NULL)
1.87 djm 332: return NULL;
333: return expand_authorized_keys(options.authorized_principals_file, pw);
334: }
335:
1.24 markus 336: /* return ok if key exists in sysfile or userfile */
337: HostStatus
338: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
339: const char *sysfile, const char *userfile)
340: {
341: char *user_hostfile;
342: struct stat st;
1.30 stevesk 343: HostStatus host_status;
1.91 djm 344: struct hostkeys *hostkeys;
345: const struct hostkey_entry *found;
1.24 markus 346:
1.91 djm 347: hostkeys = init_hostkeys();
348: load_hostkeys(hostkeys, host, sysfile);
349: if (userfile != NULL) {
1.24 markus 350: user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
351: if (options.strict_modes &&
352: (stat(user_hostfile, &st) == 0) &&
353: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31 deraadt 354: (st.st_mode & 022) != 0)) {
1.47 itojun 355: logit("Authentication refused for %.100s: "
1.24 markus 356: "bad owner or modes for %.200s",
357: pw->pw_name, user_hostfile);
1.88 djm 358: auth_debug_add("Ignored %.200s: bad ownership or modes",
359: user_hostfile);
1.24 markus 360: } else {
361: temporarily_use_uid(pw);
1.91 djm 362: load_hostkeys(hostkeys, host, user_hostfile);
1.24 markus 363: restore_uid();
364: }
1.102 djm 365: free(user_hostfile);
1.24 markus 366: }
1.91 djm 367: host_status = check_key_in_hostkeys(hostkeys, key, &found);
368: if (host_status == HOST_REVOKED)
369: error("WARNING: revoked key for %s attempted authentication",
370: found->host);
371: else if (host_status == HOST_OK)
372: debug("%s: key for %s found at %s:%ld", __func__,
373: found->host, found->file, found->line);
374: else
375: debug("%s: key for host %s not found", __func__, host);
376:
377: free_hostkeys(hostkeys);
1.24 markus 378:
379: return host_status;
380: }
381:
1.22 markus 382: /*
1.97 djm 383: * Check a given path for security. This is defined as all components
1.44 stevesk 384: * of the path to the file must be owned by either the owner of
1.23 markus 385: * of the file or root and no directories must be group or world writable.
1.22 markus 386: *
387: * XXX Should any specific check be done for sym links ?
388: *
1.101 dtucker 389: * Takes a file name, its stat information (preferably from fstat() to
1.97 djm 390: * avoid races), the uid of the expected owner, their home directory and an
1.22 markus 391: * error buffer plus max size as arguments.
392: *
393: * Returns 0 on success and -1 on failure
394: */
1.97 djm 395: int
396: auth_secure_path(const char *name, struct stat *stp, const char *pw_dir,
397: uid_t uid, char *err, size_t errlen)
1.22 markus 398: {
1.109 deraadt 399: char buf[PATH_MAX], homedir[PATH_MAX];
1.22 markus 400: char *cp;
1.46 markus 401: int comparehome = 0;
1.22 markus 402: struct stat st;
403:
1.97 djm 404: if (realpath(name, buf) == NULL) {
405: snprintf(err, errlen, "realpath %s failed: %s", name,
1.22 markus 406: strerror(errno));
407: return -1;
408: }
1.97 djm 409: if (pw_dir != NULL && realpath(pw_dir, homedir) != NULL)
1.46 markus 410: comparehome = 1;
1.22 markus 411:
1.97 djm 412: if (!S_ISREG(stp->st_mode)) {
413: snprintf(err, errlen, "%s is not a regular file", buf);
414: return -1;
415: }
416: if ((stp->st_uid != 0 && stp->st_uid != uid) ||
417: (stp->st_mode & 022) != 0) {
1.22 markus 418: snprintf(err, errlen, "bad ownership or modes for file %s",
419: buf);
420: return -1;
421: }
422:
423: /* for each component of the canonical path, walking upwards */
424: for (;;) {
425: if ((cp = dirname(buf)) == NULL) {
426: snprintf(err, errlen, "dirname() failed");
427: return -1;
428: }
429: strlcpy(buf, cp, sizeof(buf));
1.25 provos 430:
1.22 markus 431: if (stat(buf, &st) < 0 ||
432: (st.st_uid != 0 && st.st_uid != uid) ||
433: (st.st_mode & 022) != 0) {
1.31 deraadt 434: snprintf(err, errlen,
1.22 markus 435: "bad ownership or modes for directory %s", buf);
436: return -1;
437: }
438:
1.82 dtucker 439: /* If are past the homedir then we can stop */
1.94 djm 440: if (comparehome && strcmp(homedir, buf) == 0)
1.27 markus 441: break;
1.94 djm 442:
1.22 markus 443: /*
444: * dirname should always complete with a "/" path,
445: * but we can be paranoid and check for "." too
446: */
447: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
448: break;
449: }
1.17 markus 450: return 0;
1.97 djm 451: }
452:
453: /*
454: * Version of secure_path() that accepts an open file descriptor to
455: * avoid races.
456: *
457: * Returns 0 on success and -1 on failure
458: */
459: static int
460: secure_filename(FILE *f, const char *file, struct passwd *pw,
461: char *err, size_t errlen)
462: {
463: struct stat st;
464:
465: /* check the open file to avoid races */
466: if (fstat(fileno(f), &st) < 0) {
467: snprintf(err, errlen, "cannot stat file %s: %s",
1.99 dtucker 468: file, strerror(errno));
1.97 djm 469: return -1;
470: }
471: return auth_secure_path(file, &st, pw->pw_dir, pw->pw_uid, err, errlen);
1.79 dtucker 472: }
473:
1.87 djm 474: static FILE *
475: auth_openfile(const char *file, struct passwd *pw, int strict_modes,
476: int log_missing, char *file_type)
1.79 dtucker 477: {
478: char line[1024];
479: struct stat st;
480: int fd;
481: FILE *f;
482:
1.81 dtucker 483: if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
1.87 djm 484: if (log_missing || errno != ENOENT)
485: debug("Could not open %s '%s': %s", file_type, file,
1.81 dtucker 486: strerror(errno));
1.79 dtucker 487: return NULL;
1.81 dtucker 488: }
1.79 dtucker 489:
490: if (fstat(fd, &st) < 0) {
491: close(fd);
492: return NULL;
493: }
494: if (!S_ISREG(st.st_mode)) {
1.87 djm 495: logit("User %s %s %s is not a regular file",
496: pw->pw_name, file_type, file);
1.79 dtucker 497: close(fd);
498: return NULL;
499: }
500: unset_nonblock(fd);
501: if ((f = fdopen(fd, "r")) == NULL) {
502: close(fd);
503: return NULL;
504: }
1.90 djm 505: if (strict_modes &&
1.79 dtucker 506: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
507: fclose(f);
508: logit("Authentication refused: %s", line);
1.88 djm 509: auth_debug_add("Ignored %s: %s", file_type, line);
1.79 dtucker 510: return NULL;
511: }
512:
513: return f;
1.87 djm 514: }
515:
516:
517: FILE *
518: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
519: {
520: return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
521: }
522:
523: FILE *
524: auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
525: {
526: return auth_openfile(file, pw, strict_modes, 0,
527: "authorized principals");
1.37 provos 528: }
529:
530: struct passwd *
531: getpwnamallow(const char *user)
532: {
1.114 djm 533: struct ssh *ssh = active_state; /* XXX */
1.38 provos 534: extern login_cap_t *lc;
535: auth_session_t *as;
1.37 provos 536: struct passwd *pw;
1.96 dtucker 537: struct connection_info *ci = get_connection_info(1, options.use_dns);
1.71 dtucker 538:
1.96 dtucker 539: ci->user = user;
540: parse_server_match_config(&options, ci);
1.37 provos 541:
542: pw = getpwnam(user);
1.45 stevesk 543: if (pw == NULL) {
1.114 djm 544: logit("Invalid user %.100s from %.100s port %d",
545: user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.45 stevesk 546: return (NULL);
547: }
548: if (!allowed_user(pw))
1.38 provos 549: return (NULL);
550: if ((lc = login_getclass(pw->pw_class)) == NULL) {
551: debug("unable to get login class: %s", user);
552: return (NULL);
553: }
554: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43 millert 555: auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38 provos 556: debug("Approval failure for %s", user);
1.37 provos 557: pw = NULL;
1.38 provos 558: }
559: if (as != NULL)
560: auth_close(as);
1.41 markus 561: if (pw != NULL)
562: return (pwcopy(pw));
563: return (NULL);
1.85 djm 564: }
565:
566: /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
567: int
568: auth_key_is_revoked(Key *key)
569: {
1.107 djm 570: char *fp = NULL;
571: int r;
1.85 djm 572:
573: if (options.revoked_keys_file == NULL)
574: return 0;
1.108 djm 575: if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
576: SSH_FP_DEFAULT)) == NULL) {
1.107 djm 577: r = SSH_ERR_ALLOC_FAIL;
578: error("%s: fingerprint key: %s", __func__, ssh_err(r));
579: goto out;
580: }
581:
582: r = sshkey_check_revoked(key, options.revoked_keys_file);
583: switch (r) {
1.100 djm 584: case 0:
1.107 djm 585: break; /* not revoked */
586: case SSH_ERR_KEY_REVOKED:
587: error("Authentication key %s %s revoked by file %s",
588: sshkey_type(key), fp, options.revoked_keys_file);
589: goto out;
1.100 djm 590: default:
1.107 djm 591: error("Error checking authentication key %s %s in "
592: "revoked keys file %s: %s", sshkey_type(key), fp,
593: options.revoked_keys_file, ssh_err(r));
594: goto out;
1.100 djm 595: }
1.107 djm 596:
597: /* Success */
598: r = 0;
599:
600: out:
601: free(fp);
602: return r == 0 ? 0 : 1;
1.42 markus 603: }
604:
605: void
606: auth_debug_add(const char *fmt,...)
607: {
608: char buf[1024];
609: va_list args;
610:
611: if (!auth_debug_init)
612: return;
613:
614: va_start(args, fmt);
615: vsnprintf(buf, sizeof(buf), fmt, args);
616: va_end(args);
617: buffer_put_cstring(&auth_debug, buf);
618: }
619:
620: void
621: auth_debug_send(void)
622: {
623: char *msg;
624:
625: if (!auth_debug_init)
626: return;
627: while (buffer_len(&auth_debug)) {
628: msg = buffer_get_string(&auth_debug, NULL);
629: packet_send_debug("%s", msg);
1.102 djm 630: free(msg);
1.42 markus 631: }
632: }
633:
634: void
635: auth_debug_reset(void)
636: {
637: if (auth_debug_init)
638: buffer_clear(&auth_debug);
639: else {
640: buffer_init(&auth_debug);
641: auth_debug_init = 1;
642: }
1.49 markus 643: }
644:
645: struct passwd *
646: fakepw(void)
647: {
648: static struct passwd fake;
649:
650: memset(&fake, 0, sizeof(fake));
651: fake.pw_name = "NOUSER";
652: fake.pw_passwd =
1.51 djm 653: "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49 markus 654: fake.pw_gecos = "NOUSER";
1.53 deraadt 655: fake.pw_uid = (uid_t)-1;
656: fake.pw_gid = (gid_t)-1;
1.49 markus 657: fake.pw_class = "";
658: fake.pw_dir = "/nonexist";
659: fake.pw_shell = "/nonexist";
660:
661: return (&fake);
1.114 djm 662: }
663:
664: /*
665: * Returns the remote DNS hostname as a string. The returned string must not
666: * be freed. NB. this will usually trigger a DNS query the first time it is
667: * called.
668: * This function does additional checks on the hostname to mitigate some
669: * attacks on legacy rhosts-style authentication.
670: * XXX is RhostsRSAAuthentication vulnerable to these?
671: * XXX Can we remove these checks? (or if not, remove RhostsRSAAuthentication?)
672: */
673:
674: static char *
675: remote_hostname(struct ssh *ssh)
676: {
677: struct sockaddr_storage from;
678: socklen_t fromlen;
679: struct addrinfo hints, *ai, *aitop;
680: char name[NI_MAXHOST], ntop2[NI_MAXHOST];
681: const char *ntop = ssh_remote_ipaddr(ssh);
682:
683: /* Get IP address of client. */
684: fromlen = sizeof(from);
685: memset(&from, 0, sizeof(from));
686: if (getpeername(ssh_packet_get_connection_in(ssh),
687: (struct sockaddr *)&from, &fromlen) < 0) {
688: debug("getpeername failed: %.100s", strerror(errno));
689: return strdup(ntop);
690: }
691:
692: debug3("Trying to reverse map address %.100s.", ntop);
693: /* Map the IP address to a host name. */
694: if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
695: NULL, 0, NI_NAMEREQD) != 0) {
696: /* Host name not found. Use ip address. */
697: return strdup(ntop);
698: }
699:
700: /*
701: * if reverse lookup result looks like a numeric hostname,
702: * someone is trying to trick us by PTR record like following:
703: * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
704: */
705: memset(&hints, 0, sizeof(hints));
706: hints.ai_socktype = SOCK_DGRAM; /*dummy*/
707: hints.ai_flags = AI_NUMERICHOST;
708: if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
709: logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
710: name, ntop);
711: freeaddrinfo(ai);
712: return strdup(ntop);
713: }
714:
715: /* Names are stored in lowercase. */
716: lowercase(name);
717:
718: /*
719: * Map it back to an IP address and check that the given
720: * address actually is an address of this host. This is
721: * necessary because anyone with access to a name server can
722: * define arbitrary names for an IP address. Mapping from
723: * name to IP address can be trusted better (but can still be
724: * fooled if the intruder has access to the name server of
725: * the domain).
726: */
727: memset(&hints, 0, sizeof(hints));
728: hints.ai_family = from.ss_family;
729: hints.ai_socktype = SOCK_STREAM;
730: if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
731: logit("reverse mapping checking getaddrinfo for %.700s "
1.115 dtucker 732: "[%s] failed.", name, ntop);
1.114 djm 733: return strdup(ntop);
734: }
735: /* Look for the address from the list of addresses. */
736: for (ai = aitop; ai; ai = ai->ai_next) {
737: if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
738: sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
739: (strcmp(ntop, ntop2) == 0))
740: break;
741: }
742: freeaddrinfo(aitop);
743: /* If we reached the end of the list, the address was not there. */
744: if (ai == NULL) {
745: /* Address not found for the host name. */
746: logit("Address %.100s maps to %.600s, but this does not "
1.115 dtucker 747: "map back to the address.", ntop, name);
1.114 djm 748: return strdup(ntop);
749: }
750: return strdup(name);
751: }
752:
753: /*
754: * Return the canonical name of the host in the other side of the current
755: * connection. The host name is cached, so it is efficient to call this
756: * several times.
757: */
758:
759: const char *
760: auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
761: {
762: static char *dnsname;
763:
764: if (!use_dns)
765: return ssh_remote_ipaddr(ssh);
766: else if (dnsname != NULL)
767: return dnsname;
768: else {
769: dnsname = remote_hostname(ssh);
770: return dnsname;
771: }
1.1 markus 772: }