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