Annotation of src/usr.bin/ssh/auth.c, Revision 1.83
1.83 ! djm 1: /* $OpenBSD: auth.c,v 1.82 2010/01/13 00:19:04 dtucker 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.67 dtucker 58: #include "monitor_wrap.h"
1.2 markus 59:
1.1 markus 60: /* import */
61: extern ServerOptions options;
1.67 dtucker 62: extern int use_privsep;
1.1 markus 63:
1.42 markus 64: /* Debugging messages */
65: Buffer auth_debug;
66: int auth_debug_init;
67:
1.1 markus 68: /*
1.12 markus 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.
1.1 markus 74: * If the user's shell is not executable, false will be returned.
1.4 markus 75: * Otherwise true is returned.
1.1 markus 76: */
1.5 markus 77: int
1.1 markus 78: allowed_user(struct passwd * pw)
79: {
80: struct stat st;
1.35 markus 81: const char *hostname = NULL, *ipaddr = NULL;
1.83 ! djm 82: char *shell, *tmp, *chroot_path;
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: /*
90: * Get the shell from the password data. An empty shell field is
91: * legal, and means /bin/sh.
92: */
1.83 ! djm 93: shell = xstrdup((pw->pw_shell[0] == '\0') ?
! 94: _PATH_BSHELL : pw->pw_shell);
! 95:
! 96: /*
! 97: * Amend shell if chroot is requested.
! 98: */
! 99: if (options.chroot_directory != NULL &&
! 100: strcasecmp(options.chroot_directory, "none") != 0) {
! 101: tmp = tilde_expand_filename(options.chroot_directory,
! 102: pw->pw_uid);
! 103: chroot_path = percent_expand(tmp, "h", pw->pw_dir,
! 104: "u", pw->pw_name, (char *)NULL);
! 105: xfree(tmp);
! 106: xasprintf(&tmp, "%s/%s", chroot_path, shell);
! 107: xfree(shell);
! 108: shell = tmp;
! 109: free(chroot_path);
! 110: }
1.7 deraadt 111:
1.1 markus 112: /* deny if shell does not exists or is not executable */
1.34 stevesk 113: if (stat(shell, &st) != 0) {
1.47 itojun 114: logit("User %.100s not allowed because shell %.100s does not exist",
1.34 stevesk 115: pw->pw_name, shell);
1.83 ! djm 116: xfree(shell);
1.1 markus 117: return 0;
1.34 stevesk 118: }
1.36 itojun 119: if (S_ISREG(st.st_mode) == 0 ||
120: (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
1.47 itojun 121: logit("User %.100s not allowed because shell %.100s is not executable",
1.34 stevesk 122: pw->pw_name, shell);
1.83 ! djm 123: xfree(shell);
1.1 markus 124: return 0;
1.34 stevesk 125: }
1.83 ! djm 126: xfree(shell);
1.1 markus 127:
1.58 dtucker 128: if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
129: options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.48 markus 130: hostname = get_canonical_hostname(options.use_dns);
1.35 markus 131: ipaddr = get_remote_ipaddr();
132: }
133:
1.1 markus 134: /* Return false if user is listed in DenyUsers */
135: if (options.num_deny_users > 0) {
136: for (i = 0; i < options.num_deny_users; i++)
1.39 markus 137: if (match_user(pw->pw_name, hostname, ipaddr,
1.34 stevesk 138: options.deny_users[i])) {
1.57 dtucker 139: logit("User %.100s from %.100s not allowed "
140: "because listed in DenyUsers",
141: pw->pw_name, hostname);
1.1 markus 142: return 0;
1.34 stevesk 143: }
1.1 markus 144: }
145: /* Return false if AllowUsers isn't empty and user isn't listed there */
146: if (options.num_allow_users > 0) {
147: for (i = 0; i < options.num_allow_users; i++)
1.39 markus 148: if (match_user(pw->pw_name, hostname, ipaddr,
1.26 markus 149: options.allow_users[i]))
1.1 markus 150: break;
151: /* i < options.num_allow_users iff we break for loop */
1.34 stevesk 152: if (i >= options.num_allow_users) {
1.57 dtucker 153: logit("User %.100s from %.100s not allowed because "
154: "not listed in AllowUsers", pw->pw_name, hostname);
1.1 markus 155: return 0;
1.34 stevesk 156: }
1.1 markus 157: }
158: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 159: /* Get the user's group access list (primary and supplementary) */
1.34 stevesk 160: if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57 dtucker 161: logit("User %.100s from %.100s not allowed because "
162: "not in any group", pw->pw_name, hostname);
1.1 markus 163: return 0;
1.34 stevesk 164: }
1.1 markus 165:
1.12 markus 166: /* Return false if one of user's groups is listed in DenyGroups */
167: if (options.num_deny_groups > 0)
168: if (ga_match(options.deny_groups,
169: options.num_deny_groups)) {
170: ga_free();
1.57 dtucker 171: logit("User %.100s from %.100s not allowed "
172: "because a group is listed in DenyGroups",
173: pw->pw_name, hostname);
1.1 markus 174: return 0;
1.12 markus 175: }
1.1 markus 176: /*
1.12 markus 177: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 178: * isn't listed there
179: */
1.12 markus 180: if (options.num_allow_groups > 0)
181: if (!ga_match(options.allow_groups,
182: options.num_allow_groups)) {
183: ga_free();
1.57 dtucker 184: logit("User %.100s from %.100s not allowed "
185: "because none of user's groups are listed "
186: "in AllowGroups", pw->pw_name, hostname);
1.1 markus 187: return 0;
1.12 markus 188: }
189: ga_free();
1.1 markus 190: }
191: /* We found no reason not to let this user try to log on... */
192: return 1;
1.13 markus 193: }
194:
195: void
196: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
197: {
198: void (*authlog) (const char *fmt,...) = verbose;
199: char *authmsg;
1.67 dtucker 200:
201: if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
202: return;
1.13 markus 203:
204: /* Raise logging level */
205: if (authenticated == 1 ||
206: !authctxt->valid ||
1.54 dtucker 207: authctxt->failures >= options.max_authtries / 2 ||
1.13 markus 208: strcmp(method, "password") == 0)
1.47 itojun 209: authlog = logit;
1.13 markus 210:
211: if (authctxt->postponed)
212: authmsg = "Postponed";
213: else
214: authmsg = authenticated ? "Accepted" : "Failed";
215:
216: authlog("%s %s for %s%.100s from %.200s port %d%s",
217: authmsg,
218: method,
1.56 markus 219: authctxt->valid ? "" : "invalid user ",
1.29 markus 220: authctxt->user,
1.13 markus 221: get_remote_ipaddr(),
222: get_remote_port(),
223: info);
224: }
225:
226: /*
1.17 markus 227: * Check whether root logins are disallowed.
1.13 markus 228: */
229: int
1.17 markus 230: auth_root_allowed(char *method)
1.13 markus 231: {
1.17 markus 232: switch (options.permit_root_login) {
233: case PERMIT_YES:
1.13 markus 234: return 1;
1.17 markus 235: case PERMIT_NO_PASSWD:
236: if (strcmp(method, "password") != 0)
237: return 1;
238: break;
239: case PERMIT_FORCED_ONLY:
240: if (forced_command) {
1.47 itojun 241: logit("Root login accepted for forced command.");
1.17 markus 242: return 1;
243: }
244: break;
1.13 markus 245: }
1.47 itojun 246: logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22 markus 247: return 0;
248: }
249:
250:
251: /*
252: * Given a template and a passwd structure, build a filename
253: * by substituting % tokenised options. Currently, %% becomes '%',
254: * %h becomes the home directory and %u the username.
255: *
256: * This returns a buffer allocated by xmalloc.
257: */
1.59 djm 258: static char *
259: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22 markus 260: {
1.65 djm 261: char *file, ret[MAXPATHLEN];
262: int i;
1.22 markus 263:
1.59 djm 264: file = percent_expand(filename, "h", pw->pw_dir,
265: "u", pw->pw_name, (char *)NULL);
1.22 markus 266:
267: /*
268: * Ensure that filename starts anchored. If not, be backward
269: * compatible and prepend the '%h/'
270: */
1.59 djm 271: if (*file == '/')
272: return (file);
273:
1.65 djm 274: i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
275: if (i < 0 || (size_t)i >= sizeof(ret))
1.59 djm 276: fatal("expand_authorized_keys: path too long");
277: xfree(file);
1.65 djm 278: return (xstrdup(ret));
1.22 markus 279: }
280:
281: char *
282: authorized_keys_file(struct passwd *pw)
283: {
1.59 djm 284: return expand_authorized_keys(options.authorized_keys_file, pw);
1.22 markus 285: }
286:
287: char *
288: authorized_keys_file2(struct passwd *pw)
289: {
1.59 djm 290: return expand_authorized_keys(options.authorized_keys_file2, pw);
1.22 markus 291: }
1.24 markus 292:
293: /* return ok if key exists in sysfile or userfile */
294: HostStatus
295: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
296: const char *sysfile, const char *userfile)
297: {
298: Key *found;
299: char *user_hostfile;
300: struct stat st;
1.30 stevesk 301: HostStatus host_status;
1.24 markus 302:
303: /* Check if we know the host and its host key. */
304: found = key_new(key->type);
305: host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
306:
307: if (host_status != HOST_OK && userfile != NULL) {
308: user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
309: if (options.strict_modes &&
310: (stat(user_hostfile, &st) == 0) &&
311: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31 deraadt 312: (st.st_mode & 022) != 0)) {
1.47 itojun 313: logit("Authentication refused for %.100s: "
1.24 markus 314: "bad owner or modes for %.200s",
315: pw->pw_name, user_hostfile);
316: } else {
317: temporarily_use_uid(pw);
318: host_status = check_host_in_hostfile(user_hostfile,
319: host, key, found, NULL);
320: restore_uid();
321: }
322: xfree(user_hostfile);
323: }
324: key_free(found);
325:
326: debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
327: "ok" : "not found", host);
328: return host_status;
329: }
330:
1.22 markus 331:
332: /*
333: * Check a given file for security. This is defined as all components
1.44 stevesk 334: * of the path to the file must be owned by either the owner of
1.23 markus 335: * of the file or root and no directories must be group or world writable.
1.22 markus 336: *
337: * XXX Should any specific check be done for sym links ?
338: *
339: * Takes an open file descriptor, the file name, a uid and and
340: * error buffer plus max size as arguments.
341: *
342: * Returns 0 on success and -1 on failure
343: */
1.79 dtucker 344: static int
1.25 provos 345: secure_filename(FILE *f, const char *file, struct passwd *pw,
346: char *err, size_t errlen)
1.22 markus 347: {
1.25 provos 348: uid_t uid = pw->pw_uid;
1.28 markus 349: char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22 markus 350: char *cp;
1.46 markus 351: int comparehome = 0;
1.22 markus 352: struct stat st;
353:
354: if (realpath(file, buf) == NULL) {
355: snprintf(err, errlen, "realpath %s failed: %s", file,
356: strerror(errno));
357: return -1;
358: }
1.46 markus 359: if (realpath(pw->pw_dir, homedir) != NULL)
360: comparehome = 1;
1.22 markus 361:
362: /* check the open file to avoid races */
363: if (fstat(fileno(f), &st) < 0 ||
364: (st.st_uid != 0 && st.st_uid != uid) ||
365: (st.st_mode & 022) != 0) {
366: snprintf(err, errlen, "bad ownership or modes for file %s",
367: buf);
368: return -1;
369: }
370:
371: /* for each component of the canonical path, walking upwards */
372: for (;;) {
373: if ((cp = dirname(buf)) == NULL) {
374: snprintf(err, errlen, "dirname() failed");
375: return -1;
376: }
377: strlcpy(buf, cp, sizeof(buf));
1.25 provos 378:
1.22 markus 379: debug3("secure_filename: checking '%s'", buf);
380: if (stat(buf, &st) < 0 ||
381: (st.st_uid != 0 && st.st_uid != uid) ||
382: (st.st_mode & 022) != 0) {
1.31 deraadt 383: snprintf(err, errlen,
1.22 markus 384: "bad ownership or modes for directory %s", buf);
385: return -1;
386: }
387:
1.82 dtucker 388: /* If are past the homedir then we can stop */
1.46 markus 389: if (comparehome && strcmp(homedir, buf) == 0) {
1.27 markus 390: debug3("secure_filename: terminating check at '%s'",
391: buf);
392: break;
393: }
1.22 markus 394: /*
395: * dirname should always complete with a "/" path,
396: * but we can be paranoid and check for "." too
397: */
398: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
399: break;
400: }
1.17 markus 401: return 0;
1.79 dtucker 402: }
403:
404: FILE *
405: auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
406: {
407: char line[1024];
408: struct stat st;
409: int fd;
410: FILE *f;
411:
412: /*
413: * Open the file containing the authorized keys
414: * Fail quietly if file does not exist
415: */
1.81 dtucker 416: if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
417: if (errno != ENOENT)
418: debug("Could not open keyfile '%s': %s", file,
419: strerror(errno));
1.79 dtucker 420: return NULL;
1.81 dtucker 421: }
1.79 dtucker 422:
423: if (fstat(fd, &st) < 0) {
424: close(fd);
425: return NULL;
426: }
427: if (!S_ISREG(st.st_mode)) {
428: logit("User %s authorized keys %s is not a regular file",
429: pw->pw_name, file);
430: close(fd);
431: return NULL;
432: }
433: unset_nonblock(fd);
434: if ((f = fdopen(fd, "r")) == NULL) {
435: close(fd);
436: return NULL;
437: }
438: if (options.strict_modes &&
439: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
440: fclose(f);
441: logit("Authentication refused: %s", line);
442: return NULL;
443: }
444:
445: return f;
1.37 provos 446: }
447:
448: struct passwd *
449: getpwnamallow(const char *user)
450: {
1.38 provos 451: extern login_cap_t *lc;
452: auth_session_t *as;
1.37 provos 453: struct passwd *pw;
1.71 dtucker 454:
455: parse_server_match_config(&options, user,
456: get_canonical_hostname(options.use_dns), get_remote_ipaddr());
1.37 provos 457:
458: pw = getpwnam(user);
1.45 stevesk 459: if (pw == NULL) {
1.55 markus 460: logit("Invalid user %.100s from %.100s",
1.45 stevesk 461: user, get_remote_ipaddr());
462: return (NULL);
463: }
464: if (!allowed_user(pw))
1.38 provos 465: return (NULL);
466: if ((lc = login_getclass(pw->pw_class)) == NULL) {
467: debug("unable to get login class: %s", user);
468: return (NULL);
469: }
470: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43 millert 471: auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38 provos 472: debug("Approval failure for %s", user);
1.37 provos 473: pw = NULL;
1.38 provos 474: }
475: if (as != NULL)
476: auth_close(as);
1.41 markus 477: if (pw != NULL)
478: return (pwcopy(pw));
479: return (NULL);
1.42 markus 480: }
481:
482: void
483: auth_debug_add(const char *fmt,...)
484: {
485: char buf[1024];
486: va_list args;
487:
488: if (!auth_debug_init)
489: return;
490:
491: va_start(args, fmt);
492: vsnprintf(buf, sizeof(buf), fmt, args);
493: va_end(args);
494: buffer_put_cstring(&auth_debug, buf);
495: }
496:
497: void
498: auth_debug_send(void)
499: {
500: char *msg;
501:
502: if (!auth_debug_init)
503: return;
504: while (buffer_len(&auth_debug)) {
505: msg = buffer_get_string(&auth_debug, NULL);
506: packet_send_debug("%s", msg);
507: xfree(msg);
508: }
509: }
510:
511: void
512: auth_debug_reset(void)
513: {
514: if (auth_debug_init)
515: buffer_clear(&auth_debug);
516: else {
517: buffer_init(&auth_debug);
518: auth_debug_init = 1;
519: }
1.49 markus 520: }
521:
522: struct passwd *
523: fakepw(void)
524: {
525: static struct passwd fake;
526:
527: memset(&fake, 0, sizeof(fake));
528: fake.pw_name = "NOUSER";
529: fake.pw_passwd =
1.51 djm 530: "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49 markus 531: fake.pw_gecos = "NOUSER";
1.53 deraadt 532: fake.pw_uid = (uid_t)-1;
533: fake.pw_gid = (gid_t)-1;
1.49 markus 534: fake.pw_class = "";
535: fake.pw_dir = "/nonexist";
536: fake.pw_shell = "/nonexist";
537:
538: return (&fake);
1.1 markus 539: }