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