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