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