Annotation of src/usr.bin/ssh/auth.c, Revision 1.33
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.33 ! stevesk 26: RCSID("$OpenBSD: auth.c,v 1.32 2002/01/29 14:32:03 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.2 markus 42:
1.1 markus 43: /* import */
44: extern ServerOptions options;
45:
46: /*
1.12 markus 47: * Check if the user is allowed to log in via ssh. If user is listed
48: * in DenyUsers or one of user's groups is listed in DenyGroups, false
49: * will be returned. If AllowUsers isn't empty and user isn't listed
50: * there, or if AllowGroups isn't empty and one of user's groups isn't
51: * listed there, false will be returned.
1.1 markus 52: * If the user's shell is not executable, false will be returned.
1.4 markus 53: * Otherwise true is returned.
1.1 markus 54: */
1.5 markus 55: int
1.1 markus 56: allowed_user(struct passwd * pw)
57: {
58: struct stat st;
1.21 markus 59: char *shell;
1.1 markus 60: int i;
61:
62: /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12 markus 63: if (!pw || !pw->pw_name)
1.1 markus 64: return 0;
65:
1.7 deraadt 66: /*
67: * Get the shell from the password data. An empty shell field is
68: * legal, and means /bin/sh.
69: */
70: shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
71:
1.1 markus 72: /* deny if shell does not exists or is not executable */
1.7 deraadt 73: if (stat(shell, &st) != 0)
1.1 markus 74: return 0;
75: if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
76: return 0;
77:
78: /* Return false if user is listed in DenyUsers */
79: if (options.num_deny_users > 0) {
80: for (i = 0; i < options.num_deny_users; i++)
1.33 ! stevesk 81: if (match_user(pw->pw_name, options.verify_reverse_mapping,
1.26 markus 82: options.deny_users[i]))
1.1 markus 83: return 0;
84: }
85: /* Return false if AllowUsers isn't empty and user isn't listed there */
86: if (options.num_allow_users > 0) {
87: for (i = 0; i < options.num_allow_users; i++)
1.33 ! stevesk 88: if (match_user(pw->pw_name, options.verify_reverse_mapping,
1.26 markus 89: options.allow_users[i]))
1.1 markus 90: break;
91: /* i < options.num_allow_users iff we break for loop */
92: if (i >= options.num_allow_users)
93: return 0;
94: }
95: if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12 markus 96: /* Get the user's group access list (primary and supplementary) */
97: if (ga_init(pw->pw_name, pw->pw_gid) == 0)
1.1 markus 98: return 0;
99:
1.12 markus 100: /* Return false if one of user's groups is listed in DenyGroups */
101: if (options.num_deny_groups > 0)
102: if (ga_match(options.deny_groups,
103: options.num_deny_groups)) {
104: ga_free();
1.1 markus 105: return 0;
1.12 markus 106: }
1.1 markus 107: /*
1.12 markus 108: * Return false if AllowGroups isn't empty and one of user's groups
1.1 markus 109: * isn't listed there
110: */
1.12 markus 111: if (options.num_allow_groups > 0)
112: if (!ga_match(options.allow_groups,
113: options.num_allow_groups)) {
114: ga_free();
1.1 markus 115: return 0;
1.12 markus 116: }
117: ga_free();
1.1 markus 118: }
119: /* We found no reason not to let this user try to log on... */
120: return 1;
1.13 markus 121: }
122:
123: Authctxt *
124: authctxt_new(void)
125: {
1.16 stevesk 126: Authctxt *authctxt = xmalloc(sizeof(*authctxt));
127: memset(authctxt, 0, sizeof(*authctxt));
128: return authctxt;
1.13 markus 129: }
130:
131: void
132: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
133: {
134: void (*authlog) (const char *fmt,...) = verbose;
135: char *authmsg;
136:
137: /* Raise logging level */
138: if (authenticated == 1 ||
139: !authctxt->valid ||
140: authctxt->failures >= AUTH_FAIL_LOG ||
141: strcmp(method, "password") == 0)
142: authlog = log;
143:
144: if (authctxt->postponed)
145: authmsg = "Postponed";
146: else
147: authmsg = authenticated ? "Accepted" : "Failed";
148:
149: authlog("%s %s for %s%.100s from %.200s port %d%s",
150: authmsg,
151: method,
152: authctxt->valid ? "" : "illegal user ",
1.29 markus 153: authctxt->user,
1.13 markus 154: get_remote_ipaddr(),
155: get_remote_port(),
156: info);
157: }
158:
159: /*
1.17 markus 160: * Check whether root logins are disallowed.
1.13 markus 161: */
162: int
1.17 markus 163: auth_root_allowed(char *method)
1.13 markus 164: {
1.17 markus 165: switch (options.permit_root_login) {
166: case PERMIT_YES:
1.13 markus 167: return 1;
1.17 markus 168: break;
169: case PERMIT_NO_PASSWD:
170: if (strcmp(method, "password") != 0)
171: return 1;
172: break;
173: case PERMIT_FORCED_ONLY:
174: if (forced_command) {
175: log("Root login accepted for forced command.");
176: return 1;
177: }
178: break;
1.13 markus 179: }
1.17 markus 180: log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22 markus 181: return 0;
182: }
183:
184:
185: /*
186: * Given a template and a passwd structure, build a filename
187: * by substituting % tokenised options. Currently, %% becomes '%',
188: * %h becomes the home directory and %u the username.
189: *
190: * This returns a buffer allocated by xmalloc.
191: */
192: char *
193: expand_filename(const char *filename, struct passwd *pw)
194: {
195: Buffer buffer;
196: char *file;
197: const char *cp;
198:
199: /*
200: * Build the filename string in the buffer by making the appropriate
201: * substitutions to the given file name.
202: */
203: buffer_init(&buffer);
204: for (cp = filename; *cp; cp++) {
205: if (cp[0] == '%' && cp[1] == '%') {
206: buffer_append(&buffer, "%", 1);
207: cp++;
208: continue;
209: }
210: if (cp[0] == '%' && cp[1] == 'h') {
211: buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
212: cp++;
213: continue;
214: }
215: if (cp[0] == '%' && cp[1] == 'u') {
216: buffer_append(&buffer, pw->pw_name,
1.31 deraadt 217: strlen(pw->pw_name));
1.22 markus 218: cp++;
219: continue;
220: }
221: buffer_append(&buffer, cp, 1);
222: }
223: buffer_append(&buffer, "\0", 1);
224:
225: /*
226: * Ensure that filename starts anchored. If not, be backward
227: * compatible and prepend the '%h/'
228: */
229: file = xmalloc(MAXPATHLEN);
230: cp = buffer_ptr(&buffer);
231: if (*cp != '/')
232: snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
233: else
234: strlcpy(file, cp, MAXPATHLEN);
235:
236: buffer_free(&buffer);
237: return file;
238: }
239:
240: char *
241: authorized_keys_file(struct passwd *pw)
242: {
243: return expand_filename(options.authorized_keys_file, pw);
244: }
245:
246: char *
247: authorized_keys_file2(struct passwd *pw)
248: {
249: return expand_filename(options.authorized_keys_file2, pw);
250: }
1.24 markus 251:
252: /* return ok if key exists in sysfile or userfile */
253: HostStatus
254: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
255: const char *sysfile, const char *userfile)
256: {
257: Key *found;
258: char *user_hostfile;
259: struct stat st;
1.30 stevesk 260: HostStatus host_status;
1.24 markus 261:
262: /* Check if we know the host and its host key. */
263: found = key_new(key->type);
264: host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
265:
266: if (host_status != HOST_OK && userfile != NULL) {
267: user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
268: if (options.strict_modes &&
269: (stat(user_hostfile, &st) == 0) &&
270: ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31 deraadt 271: (st.st_mode & 022) != 0)) {
1.24 markus 272: log("Authentication refused for %.100s: "
273: "bad owner or modes for %.200s",
274: pw->pw_name, user_hostfile);
275: } else {
276: temporarily_use_uid(pw);
277: host_status = check_host_in_hostfile(user_hostfile,
278: host, key, found, NULL);
279: restore_uid();
280: }
281: xfree(user_hostfile);
282: }
283: key_free(found);
284:
285: debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
286: "ok" : "not found", host);
287: return host_status;
288: }
289:
1.22 markus 290:
291: /*
292: * Check a given file for security. This is defined as all components
293: * of the path to the file must either be owned by either the owner of
1.23 markus 294: * of the file or root and no directories must be group or world writable.
1.22 markus 295: *
296: * XXX Should any specific check be done for sym links ?
297: *
298: * Takes an open file descriptor, the file name, a uid and and
299: * error buffer plus max size as arguments.
300: *
301: * Returns 0 on success and -1 on failure
302: */
303: int
1.25 provos 304: secure_filename(FILE *f, const char *file, struct passwd *pw,
305: char *err, size_t errlen)
1.22 markus 306: {
1.25 provos 307: uid_t uid = pw->pw_uid;
1.28 markus 308: char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22 markus 309: char *cp;
310: struct stat st;
311:
312: if (realpath(file, buf) == NULL) {
313: snprintf(err, errlen, "realpath %s failed: %s", file,
314: strerror(errno));
315: return -1;
316: }
1.28 markus 317: if (realpath(pw->pw_dir, homedir) == NULL) {
318: snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir,
319: strerror(errno));
320: return -1;
321: }
1.22 markus 322:
323: /* check the open file to avoid races */
324: if (fstat(fileno(f), &st) < 0 ||
325: (st.st_uid != 0 && st.st_uid != uid) ||
326: (st.st_mode & 022) != 0) {
327: snprintf(err, errlen, "bad ownership or modes for file %s",
328: buf);
329: return -1;
330: }
331:
332: /* for each component of the canonical path, walking upwards */
333: for (;;) {
334: if ((cp = dirname(buf)) == NULL) {
335: snprintf(err, errlen, "dirname() failed");
336: return -1;
337: }
338: strlcpy(buf, cp, sizeof(buf));
1.25 provos 339:
1.22 markus 340: debug3("secure_filename: checking '%s'", buf);
341: if (stat(buf, &st) < 0 ||
342: (st.st_uid != 0 && st.st_uid != uid) ||
343: (st.st_mode & 022) != 0) {
1.31 deraadt 344: snprintf(err, errlen,
1.22 markus 345: "bad ownership or modes for directory %s", buf);
346: return -1;
347: }
348:
1.27 markus 349: /* If are passed the homedir then we can stop */
1.28 markus 350: if (strcmp(homedir, buf) == 0) {
1.27 markus 351: debug3("secure_filename: terminating check at '%s'",
352: buf);
353: break;
354: }
1.22 markus 355: /*
356: * dirname should always complete with a "/" path,
357: * but we can be paranoid and check for "." too
358: */
359: if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
360: break;
361: }
1.17 markus 362: return 0;
1.1 markus 363: }