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