Annotation of src/usr.bin/ssh/auth.c, Revision 1.63
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: break;
200: case PERMIT_NO_PASSWD:
201: if (strcmp(method, "password") != 0)
202: return 1;
203: break;
204: case PERMIT_FORCED_ONLY:
205: if (forced_command) {
1.47 itojun 206: logit("Root login accepted for forced command.");
1.17 markus 207: return 1;
208: }
209: break;
1.13 markus 210: }
1.47 itojun 211: logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22 markus 212: return 0;
213: }
214:
215:
216: /*
217: * Given a template and a passwd structure, build a filename
218: * by substituting % tokenised options. Currently, %% becomes '%',
219: * %h becomes the home directory and %u the username.
220: *
221: * This returns a buffer allocated by xmalloc.
222: */
1.59 djm 223: static char *
224: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22 markus 225: {
1.59 djm 226: char *file, *ret;
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:
238: ret = xmalloc(MAXPATHLEN);
239: if (strlcpy(ret, pw->pw_dir, MAXPATHLEN) >= MAXPATHLEN ||
240: strlcat(ret, "/", MAXPATHLEN) >= MAXPATHLEN ||
241: strlcat(ret, file, MAXPATHLEN) >= MAXPATHLEN)
242: fatal("expand_authorized_keys: path too long");
1.22 markus 243:
1.59 djm 244: xfree(file);
245: return (ret);
1.22 markus 246: }
247:
248: char *
249: authorized_keys_file(struct passwd *pw)
250: {
1.59 djm 251: return expand_authorized_keys(options.authorized_keys_file, pw);
1.22 markus 252: }
253:
254: char *
255: authorized_keys_file2(struct passwd *pw)
256: {
1.59 djm 257: return expand_authorized_keys(options.authorized_keys_file2, pw);
1.22 markus 258: }
1.24 markus 259:
260: /* return ok if key exists in sysfile or userfile */
261: HostStatus
262: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
263: const char *sysfile, const char *userfile)
264: {
265: Key *found;
266: char *user_hostfile;
267: struct stat st;
1.30 stevesk 268: HostStatus host_status;
1.24 markus 269:
270: /* Check if we know the host and its host key. */
271: found = key_new(key->type);
272: host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
273:
274: if (host_status != HOST_OK && userfile != NULL) {
275: user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
276: if (options.strict_modes &&
277: (stat(user_hostfile, &st) == 0) &&
278: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31 deraadt 279: (st.st_mode & 022) != 0)) {
1.47 itojun 280: logit("Authentication refused for %.100s: "
1.24 markus 281: "bad owner or modes for %.200s",
282: pw->pw_name, user_hostfile);
283: } else {
284: temporarily_use_uid(pw);
285: host_status = check_host_in_hostfile(user_hostfile,
286: host, key, found, NULL);
287: restore_uid();
288: }
289: xfree(user_hostfile);
290: }
291: key_free(found);
292:
293: debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
294: "ok" : "not found", host);
295: return host_status;
296: }
297:
1.22 markus 298:
299: /*
300: * Check a given file for security. This is defined as all components
1.44 stevesk 301: * of the path to the file must be owned by either the owner of
1.23 markus 302: * of the file or root and no directories must be group or world writable.
1.22 markus 303: *
304: * XXX Should any specific check be done for sym links ?
305: *
306: * Takes an open file descriptor, the file name, a uid and and
307: * error buffer plus max size as arguments.
308: *
309: * Returns 0 on success and -1 on failure
310: */
311: int
1.25 provos 312: secure_filename(FILE *f, const char *file, struct passwd *pw,
313: char *err, size_t errlen)
1.22 markus 314: {
1.25 provos 315: uid_t uid = pw->pw_uid;
1.28 markus 316: char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22 markus 317: char *cp;
1.46 markus 318: int comparehome = 0;
1.22 markus 319: struct stat st;
320:
321: if (realpath(file, buf) == NULL) {
322: snprintf(err, errlen, "realpath %s failed: %s", file,
323: strerror(errno));
324: return -1;
325: }
1.46 markus 326: if (realpath(pw->pw_dir, homedir) != NULL)
327: comparehome = 1;
1.22 markus 328:
329: /* check the open file to avoid races */
330: if (fstat(fileno(f), &st) < 0 ||
331: (st.st_uid != 0 && st.st_uid != uid) ||
332: (st.st_mode & 022) != 0) {
333: snprintf(err, errlen, "bad ownership or modes for file %s",
334: buf);
335: return -1;
336: }
337:
338: /* for each component of the canonical path, walking upwards */
339: for (;;) {
340: if ((cp = dirname(buf)) == NULL) {
341: snprintf(err, errlen, "dirname() failed");
342: return -1;
343: }
344: strlcpy(buf, cp, sizeof(buf));
1.25 provos 345:
1.22 markus 346: debug3("secure_filename: checking '%s'", buf);
347: if (stat(buf, &st) < 0 ||
348: (st.st_uid != 0 && st.st_uid != uid) ||
349: (st.st_mode & 022) != 0) {
1.31 deraadt 350: snprintf(err, errlen,
1.22 markus 351: "bad ownership or modes for directory %s", buf);
352: return -1;
353: }
354:
1.27 markus 355: /* If are passed the homedir then we can stop */
1.46 markus 356: if (comparehome && strcmp(homedir, buf) == 0) {
1.27 markus 357: debug3("secure_filename: terminating check at '%s'",
358: buf);
359: break;
360: }
1.22 markus 361: /*
362: * dirname should always complete with a "/" path,
363: * but we can be paranoid and check for "." too
364: */
365: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
366: break;
367: }
1.17 markus 368: return 0;
1.37 provos 369: }
370:
371: struct passwd *
372: getpwnamallow(const char *user)
373: {
1.38 provos 374: #ifdef HAVE_LOGIN_CAP
375: extern login_cap_t *lc;
376: #ifdef BSD_AUTH
377: auth_session_t *as;
378: #endif
379: #endif
1.37 provos 380: struct passwd *pw;
381:
382: pw = getpwnam(user);
1.45 stevesk 383: if (pw == NULL) {
1.55 markus 384: logit("Invalid user %.100s from %.100s",
1.45 stevesk 385: user, get_remote_ipaddr());
386: return (NULL);
387: }
388: if (!allowed_user(pw))
1.38 provos 389: return (NULL);
390: #ifdef HAVE_LOGIN_CAP
391: if ((lc = login_getclass(pw->pw_class)) == NULL) {
392: debug("unable to get login class: %s", user);
393: return (NULL);
394: }
395: #ifdef BSD_AUTH
396: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43 millert 397: auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38 provos 398: debug("Approval failure for %s", user);
1.37 provos 399: pw = NULL;
1.38 provos 400: }
401: if (as != NULL)
402: auth_close(as);
403: #endif
404: #endif
1.41 markus 405: if (pw != NULL)
406: return (pwcopy(pw));
407: return (NULL);
1.42 markus 408: }
409:
410: void
411: auth_debug_add(const char *fmt,...)
412: {
413: char buf[1024];
414: va_list args;
415:
416: if (!auth_debug_init)
417: return;
418:
419: va_start(args, fmt);
420: vsnprintf(buf, sizeof(buf), fmt, args);
421: va_end(args);
422: buffer_put_cstring(&auth_debug, buf);
423: }
424:
425: void
426: auth_debug_send(void)
427: {
428: char *msg;
429:
430: if (!auth_debug_init)
431: return;
432: while (buffer_len(&auth_debug)) {
433: msg = buffer_get_string(&auth_debug, NULL);
434: packet_send_debug("%s", msg);
435: xfree(msg);
436: }
437: }
438:
439: void
440: auth_debug_reset(void)
441: {
442: if (auth_debug_init)
443: buffer_clear(&auth_debug);
444: else {
445: buffer_init(&auth_debug);
446: auth_debug_init = 1;
447: }
1.49 markus 448: }
449:
450: struct passwd *
451: fakepw(void)
452: {
453: static struct passwd fake;
454:
455: memset(&fake, 0, sizeof(fake));
456: fake.pw_name = "NOUSER";
457: fake.pw_passwd =
1.51 djm 458: "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49 markus 459: fake.pw_gecos = "NOUSER";
1.53 deraadt 460: fake.pw_uid = (uid_t)-1;
461: fake.pw_gid = (gid_t)-1;
1.49 markus 462: fake.pw_class = "";
463: fake.pw_dir = "/nonexist";
464: fake.pw_shell = "/nonexist";
465:
466: return (&fake);
1.1 markus 467: }