Annotation of src/usr.bin/ssh/auth.c, Revision 1.81
1.81 ! dtucker 1: /* $OpenBSD: auth.c,v 1.80 2008/11/04 07:58: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.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.21 markus 82: char *shell;
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: */
93: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
94:
1.1 markus 95: /* deny if shell does not exists or is not executable */
1.34 stevesk 96: if (stat(shell, &st) != 0) {
1.47 itojun 97: logit("User %.100s not allowed because shell %.100s does not exist",
1.34 stevesk 98: pw->pw_name, shell);
1.1 markus 99: return 0;
1.34 stevesk 100: }
1.36 itojun 101: if (S_ISREG(st.st_mode) == 0 ||
102: (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
1.47 itojun 103: logit("User %.100s not allowed because shell %.100s is not executable",
1.34 stevesk 104: pw->pw_name, shell);
1.1 markus 105: return 0;
1.34 stevesk 106: }
1.1 markus 107:
1.58 dtucker 108: if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
109: options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.48 markus 110: hostname = get_canonical_hostname(options.use_dns);
1.35 markus 111: ipaddr = get_remote_ipaddr();
112: }
113:
1.1 markus 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++)
1.39 markus 117: if (match_user(pw->pw_name, hostname, ipaddr,
1.34 stevesk 118: options.deny_users[i])) {
1.57 dtucker 119: logit("User %.100s from %.100s not allowed "
120: "because listed in DenyUsers",
121: pw->pw_name, hostname);
1.1 markus 122: return 0;
1.34 stevesk 123: }
1.1 markus 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++)
1.39 markus 128: if (match_user(pw->pw_name, hostname, ipaddr,
1.26 markus 129: options.allow_users[i]))
1.1 markus 130: break;
131: /* i < options.num_allow_users iff we break for loop */
1.34 stevesk 132: if (i >= options.num_allow_users) {
1.57 dtucker 133: logit("User %.100s from %.100s not allowed because "
134: "not listed in AllowUsers", pw->pw_name, hostname);
1.1 markus 135: return 0;
1.34 stevesk 136: }
1.1 markus 137: }
138: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 139: /* Get the user's group access list (primary and supplementary) */
1.34 stevesk 140: if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57 dtucker 141: logit("User %.100s from %.100s not allowed because "
142: "not in any group", pw->pw_name, hostname);
1.1 markus 143: return 0;
1.34 stevesk 144: }
1.1 markus 145:
1.12 markus 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();
1.57 dtucker 151: logit("User %.100s from %.100s not allowed "
152: "because a group is listed in DenyGroups",
153: pw->pw_name, hostname);
1.1 markus 154: return 0;
1.12 markus 155: }
1.1 markus 156: /*
1.12 markus 157: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 158: * isn't listed there
159: */
1.12 markus 160: if (options.num_allow_groups > 0)
161: if (!ga_match(options.allow_groups,
162: options.num_allow_groups)) {
163: ga_free();
1.57 dtucker 164: logit("User %.100s from %.100s not allowed "
165: "because none of user's groups are listed "
166: "in AllowGroups", pw->pw_name, hostname);
1.1 markus 167: return 0;
1.12 markus 168: }
169: ga_free();
1.1 markus 170: }
171: /* We found no reason not to let this user try to log on... */
172: return 1;
1.13 markus 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;
1.67 dtucker 180:
181: if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
182: return;
1.13 markus 183:
184: /* Raise logging level */
185: if (authenticated == 1 ||
186: !authctxt->valid ||
1.54 dtucker 187: authctxt->failures >= options.max_authtries / 2 ||
1.13 markus 188: strcmp(method, "password") == 0)
1.47 itojun 189: authlog = logit;
1.13 markus 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,
1.56 markus 199: authctxt->valid ? "" : "invalid user ",
1.29 markus 200: authctxt->user,
1.13 markus 201: get_remote_ipaddr(),
202: get_remote_port(),
203: info);
204: }
205:
206: /*
1.17 markus 207: * Check whether root logins are disallowed.
1.13 markus 208: */
209: int
1.17 markus 210: auth_root_allowed(char *method)
1.13 markus 211: {
1.17 markus 212: switch (options.permit_root_login) {
213: case PERMIT_YES:
1.13 markus 214: return 1;
1.17 markus 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) {
1.47 itojun 221: logit("Root login accepted for forced command.");
1.17 markus 222: return 1;
223: }
224: break;
1.13 markus 225: }
1.47 itojun 226: logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22 markus 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: */
1.59 djm 238: static char *
239: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22 markus 240: {
1.65 djm 241: char *file, ret[MAXPATHLEN];
242: int i;
1.22 markus 243:
1.59 djm 244: file = percent_expand(filename, "h", pw->pw_dir,
245: "u", pw->pw_name, (char *)NULL);
1.22 markus 246:
247: /*
248: * Ensure that filename starts anchored. If not, be backward
249: * compatible and prepend the '%h/'
250: */
1.59 djm 251: if (*file == '/')
252: return (file);
253:
1.65 djm 254: i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
255: if (i < 0 || (size_t)i >= sizeof(ret))
1.59 djm 256: fatal("expand_authorized_keys: path too long");
257: xfree(file);
1.65 djm 258: return (xstrdup(ret));
1.22 markus 259: }
260:
261: char *
262: authorized_keys_file(struct passwd *pw)
263: {
1.59 djm 264: return expand_authorized_keys(options.authorized_keys_file, pw);
1.22 markus 265: }
266:
267: char *
268: authorized_keys_file2(struct passwd *pw)
269: {
1.59 djm 270: return expand_authorized_keys(options.authorized_keys_file2, pw);
1.22 markus 271: }
1.24 markus 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;
1.30 stevesk 281: HostStatus host_status;
1.24 markus 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) ||
1.31 deraadt 292: (st.st_mode & 022) != 0)) {
1.47 itojun 293: logit("Authentication refused for %.100s: "
1.24 markus 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:
1.22 markus 311:
312: /*
313: * Check a given file for security. This is defined as all components
1.44 stevesk 314: * of the path to the file must be owned by either the owner of
1.23 markus 315: * of the file or root and no directories must be group or world writable.
1.22 markus 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: */
1.79 dtucker 324: static int
1.25 provos 325: secure_filename(FILE *f, const char *file, struct passwd *pw,
326: char *err, size_t errlen)
1.22 markus 327: {
1.25 provos 328: uid_t uid = pw->pw_uid;
1.28 markus 329: char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22 markus 330: char *cp;
1.46 markus 331: int comparehome = 0;
1.22 markus 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: }
1.46 markus 339: if (realpath(pw->pw_dir, homedir) != NULL)
340: comparehome = 1;
1.22 markus 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));
1.25 provos 358:
1.22 markus 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) {
1.31 deraadt 363: snprintf(err, errlen,
1.22 markus 364: "bad ownership or modes for directory %s", buf);
365: return -1;
366: }
367:
1.27 markus 368: /* If are passed the homedir then we can stop */
1.46 markus 369: if (comparehome && strcmp(homedir, buf) == 0) {
1.27 markus 370: debug3("secure_filename: terminating check at '%s'",
371: buf);
372: break;
373: }
1.22 markus 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: }
1.17 markus 381: return 0;
1.79 dtucker 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: */
1.81 ! dtucker 396: if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
! 397: if (errno != ENOENT)
! 398: debug("Could not open keyfile '%s': %s", file,
! 399: strerror(errno));
1.79 dtucker 400: return NULL;
1.81 ! dtucker 401: }
1.79 dtucker 402:
403: if (fstat(fd, &st) < 0) {
404: close(fd);
405: return NULL;
406: }
407: if (!S_ISREG(st.st_mode)) {
408: logit("User %s authorized keys %s is not a regular file",
409: pw->pw_name, file);
410: close(fd);
411: return NULL;
412: }
413: unset_nonblock(fd);
414: if ((f = fdopen(fd, "r")) == NULL) {
415: close(fd);
416: return NULL;
417: }
418: if (options.strict_modes &&
419: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
420: fclose(f);
421: logit("Authentication refused: %s", line);
422: return NULL;
423: }
424:
425: return f;
1.37 provos 426: }
427:
428: struct passwd *
429: getpwnamallow(const char *user)
430: {
1.38 provos 431: extern login_cap_t *lc;
432: auth_session_t *as;
1.37 provos 433: struct passwd *pw;
1.71 dtucker 434:
435: parse_server_match_config(&options, user,
436: get_canonical_hostname(options.use_dns), get_remote_ipaddr());
1.37 provos 437:
438: pw = getpwnam(user);
1.45 stevesk 439: if (pw == NULL) {
1.55 markus 440: logit("Invalid user %.100s from %.100s",
1.45 stevesk 441: user, get_remote_ipaddr());
442: return (NULL);
443: }
444: if (!allowed_user(pw))
1.38 provos 445: return (NULL);
446: if ((lc = login_getclass(pw->pw_class)) == NULL) {
447: debug("unable to get login class: %s", user);
448: return (NULL);
449: }
450: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43 millert 451: auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38 provos 452: debug("Approval failure for %s", user);
1.37 provos 453: pw = NULL;
1.38 provos 454: }
455: if (as != NULL)
456: auth_close(as);
1.41 markus 457: if (pw != NULL)
458: return (pwcopy(pw));
459: return (NULL);
1.42 markus 460: }
461:
462: void
463: auth_debug_add(const char *fmt,...)
464: {
465: char buf[1024];
466: va_list args;
467:
468: if (!auth_debug_init)
469: return;
470:
471: va_start(args, fmt);
472: vsnprintf(buf, sizeof(buf), fmt, args);
473: va_end(args);
474: buffer_put_cstring(&auth_debug, buf);
475: }
476:
477: void
478: auth_debug_send(void)
479: {
480: char *msg;
481:
482: if (!auth_debug_init)
483: return;
484: while (buffer_len(&auth_debug)) {
485: msg = buffer_get_string(&auth_debug, NULL);
486: packet_send_debug("%s", msg);
487: xfree(msg);
488: }
489: }
490:
491: void
492: auth_debug_reset(void)
493: {
494: if (auth_debug_init)
495: buffer_clear(&auth_debug);
496: else {
497: buffer_init(&auth_debug);
498: auth_debug_init = 1;
499: }
1.49 markus 500: }
501:
502: struct passwd *
503: fakepw(void)
504: {
505: static struct passwd fake;
506:
507: memset(&fake, 0, sizeof(fake));
508: fake.pw_name = "NOUSER";
509: fake.pw_passwd =
1.51 djm 510: "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49 markus 511: fake.pw_gecos = "NOUSER";
1.53 deraadt 512: fake.pw_uid = (uid_t)-1;
513: fake.pw_gid = (gid_t)-1;
1.49 markus 514: fake.pw_class = "";
515: fake.pw_dir = "/nonexist";
516: fake.pw_shell = "/nonexist";
517:
518: return (&fake);
1.1 markus 519: }