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