Annotation of src/usr.bin/ssh/auth.c, Revision 1.40
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.40 ! markus 26: RCSID("$OpenBSD: auth.c,v 1.39 2002/03/19 10:49:35 markus 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"
41: #include "tildexpand.h"
1.40 ! markus 42: #include "misc.h"
1.2 markus 43:
1.1 markus 44: /* import */
45: extern ServerOptions options;
46:
47: /*
1.12 markus 48: * Check if the user is allowed to log in via ssh. If user is listed
49: * in DenyUsers or one of user's groups is listed in DenyGroups, false
50: * will be returned. If AllowUsers isn't empty and user isn't listed
51: * there, or if AllowGroups isn't empty and one of user's groups isn't
52: * listed there, false will be returned.
1.1 markus 53: * If the user's shell is not executable, false will be returned.
1.4 markus 54: * Otherwise true is returned.
1.1 markus 55: */
1.5 markus 56: int
1.1 markus 57: allowed_user(struct passwd * pw)
58: {
59: struct stat st;
1.35 markus 60: const char *hostname = NULL, *ipaddr = NULL;
1.21 markus 61: char *shell;
1.1 markus 62: int i;
63:
64: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 markus 65: if (!pw || !pw->pw_name)
1.1 markus 66: return 0;
67:
1.7 deraadt 68: /*
69: * Get the shell from the password data. An empty shell field is
70: * legal, and means /bin/sh.
71: */
72: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
73:
1.1 markus 74: /* deny if shell does not exists or is not executable */
1.34 stevesk 75: if (stat(shell, &st) != 0) {
76: log("User %.100s not allowed because shell %.100s does not exist",
77: pw->pw_name, shell);
1.1 markus 78: return 0;
1.34 stevesk 79: }
1.36 itojun 80: if (S_ISREG(st.st_mode) == 0 ||
81: (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
1.34 stevesk 82: log("User %.100s not allowed because shell %.100s is not executable",
83: pw->pw_name, shell);
1.1 markus 84: return 0;
1.34 stevesk 85: }
1.1 markus 86:
1.35 markus 87: if (options.num_deny_users > 0 || options.num_allow_users > 0) {
88: hostname = get_canonical_hostname(options.verify_reverse_mapping);
89: ipaddr = get_remote_ipaddr();
90: }
91:
1.1 markus 92: /* Return false if user is listed in DenyUsers */
93: if (options.num_deny_users > 0) {
94: for (i = 0; i < options.num_deny_users; i++)
1.39 markus 95: if (match_user(pw->pw_name, hostname, ipaddr,
1.34 stevesk 96: options.deny_users[i])) {
1.39 markus 97: log("User %.100s not allowed because listed in DenyUsers",
98: pw->pw_name);
1.1 markus 99: return 0;
1.34 stevesk 100: }
1.1 markus 101: }
102: /* Return false if AllowUsers isn't empty and user isn't listed there */
103: if (options.num_allow_users > 0) {
104: for (i = 0; i < options.num_allow_users; i++)
1.39 markus 105: if (match_user(pw->pw_name, hostname, ipaddr,
1.26 markus 106: options.allow_users[i]))
1.1 markus 107: break;
108: /* i < options.num_allow_users iff we break for loop */
1.34 stevesk 109: if (i >= options.num_allow_users) {
110: log("User %.100s not allowed because not listed in AllowUsers",
111: pw->pw_name);
1.1 markus 112: return 0;
1.34 stevesk 113: }
1.1 markus 114: }
115: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 116: /* Get the user's group access list (primary and supplementary) */
1.34 stevesk 117: if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
118: log("User %.100s not allowed because not in any group",
119: pw->pw_name);
1.1 markus 120: return 0;
1.34 stevesk 121: }
1.1 markus 122:
1.12 markus 123: /* Return false if one of user's groups is listed in DenyGroups */
124: if (options.num_deny_groups > 0)
125: if (ga_match(options.deny_groups,
126: options.num_deny_groups)) {
127: ga_free();
1.34 stevesk 128: log("User %.100s not allowed because a group is listed in DenyGroups",
129: pw->pw_name);
1.1 markus 130: return 0;
1.12 markus 131: }
1.1 markus 132: /*
1.12 markus 133: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 134: * isn't listed there
135: */
1.12 markus 136: if (options.num_allow_groups > 0)
137: if (!ga_match(options.allow_groups,
138: options.num_allow_groups)) {
139: ga_free();
1.34 stevesk 140: log("User %.100s not allowed because none of user's groups are listed in AllowGroups",
141: pw->pw_name);
1.1 markus 142: return 0;
1.12 markus 143: }
144: ga_free();
1.1 markus 145: }
146: /* We found no reason not to let this user try to log on... */
147: return 1;
1.13 markus 148: }
149:
150: Authctxt *
151: authctxt_new(void)
152: {
1.16 stevesk 153: Authctxt *authctxt = xmalloc(sizeof(*authctxt));
154: memset(authctxt, 0, sizeof(*authctxt));
155: return authctxt;
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 ||
167: authctxt->failures >= AUTH_FAIL_LOG ||
168: strcmp(method, "password") == 0)
169: authlog = log;
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,
179: authctxt->valid ? "" : "illegal 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) {
202: log("Root login accepted for forced command.");
203: return 1;
204: }
205: break;
1.13 markus 206: }
1.17 markus 207: log("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.24 markus 299: log("Authentication refused for %.100s: "
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
320: * of the path to the file must either 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;
337: struct stat st;
338:
339: if (realpath(file, buf) == NULL) {
340: snprintf(err, errlen, "realpath %s failed: %s", file,
341: strerror(errno));
342: return -1;
343: }
1.28 markus 344: if (realpath(pw->pw_dir, homedir) == NULL) {
345: snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir,
346: strerror(errno));
347: return -1;
348: }
1.22 markus 349:
350: /* check the open file to avoid races */
351: if (fstat(fileno(f), &st) < 0 ||
352: (st.st_uid != 0 && st.st_uid != uid) ||
353: (st.st_mode & 022) != 0) {
354: snprintf(err, errlen, "bad ownership or modes for file %s",
355: buf);
356: return -1;
357: }
358:
359: /* for each component of the canonical path, walking upwards */
360: for (;;) {
361: if ((cp = dirname(buf)) == NULL) {
362: snprintf(err, errlen, "dirname() failed");
363: return -1;
364: }
365: strlcpy(buf, cp, sizeof(buf));
1.25 provos 366:
1.22 markus 367: debug3("secure_filename: checking '%s'", buf);
368: if (stat(buf, &st) < 0 ||
369: (st.st_uid != 0 && st.st_uid != uid) ||
370: (st.st_mode & 022) != 0) {
1.31 deraadt 371: snprintf(err, errlen,
1.22 markus 372: "bad ownership or modes for directory %s", buf);
373: return -1;
374: }
375:
1.27 markus 376: /* If are passed the homedir then we can stop */
1.28 markus 377: if (strcmp(homedir, buf) == 0) {
1.27 markus 378: debug3("secure_filename: terminating check at '%s'",
379: buf);
380: break;
381: }
1.22 markus 382: /*
383: * dirname should always complete with a "/" path,
384: * but we can be paranoid and check for "." too
385: */
386: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
387: break;
388: }
1.17 markus 389: return 0;
1.37 provos 390: }
391:
392: struct passwd *
393: getpwnamallow(const char *user)
394: {
1.38 provos 395: #ifdef HAVE_LOGIN_CAP
396: extern login_cap_t *lc;
397: #ifdef BSD_AUTH
398: auth_session_t *as;
399: #endif
400: #endif
1.37 provos 401: struct passwd *pw;
402:
403: pw = getpwnam(user);
1.38 provos 404: if (pw == NULL || !allowed_user(pw))
405: return (NULL);
406: #ifdef HAVE_LOGIN_CAP
407: if ((lc = login_getclass(pw->pw_class)) == NULL) {
408: debug("unable to get login class: %s", user);
409: return (NULL);
410: }
411: #ifdef BSD_AUTH
412: if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
413: auth_approval(NULL, lc, pw->pw_name, "ssh") <= 0) {
414: debug("Approval failure for %s", user);
1.37 provos 415: pw = NULL;
1.38 provos 416: }
417: if (as != NULL)
418: auth_close(as);
419: #endif
420: #endif
1.40 ! markus 421: return (pwcopy(pw));
1.1 markus 422: }