xref: /freebsd/crypto/openssh/auth.c (revision 80628bacb0b4bc1daaef4e038e755602c972bede)
1a04a10f8SKris Kennaway /*
2a04a10f8SKris Kennaway  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3e8aafc91SKris Kennaway  *
4c2d3a559SKris Kennaway  * Redistribution and use in source and binary forms, with or without
5c2d3a559SKris Kennaway  * modification, are permitted provided that the following conditions
6c2d3a559SKris Kennaway  * are met:
7c2d3a559SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
8c2d3a559SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
9c2d3a559SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
10c2d3a559SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
11c2d3a559SKris Kennaway  *    documentation and/or other materials provided with the distribution.
12c2d3a559SKris Kennaway  *
13c2d3a559SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14c2d3a559SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15c2d3a559SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16c2d3a559SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17c2d3a559SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18c2d3a559SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19c2d3a559SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20c2d3a559SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21c2d3a559SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22c2d3a559SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23a04a10f8SKris Kennaway  */
24a04a10f8SKris Kennaway 
25a04a10f8SKris Kennaway #include "includes.h"
2680628bacSDag-Erling Smørgrav RCSID("$OpenBSD: auth.c,v 1.43 2002/05/17 14:27:55 millert Exp $");
27c2d3a559SKris Kennaway RCSID("$FreeBSD$");
28a04a10f8SKris Kennaway 
29af12a3e7SDag-Erling Smørgrav #include <libgen.h>
30af12a3e7SDag-Erling Smørgrav 
31a04a10f8SKris Kennaway #include "xmalloc.h"
32a04a10f8SKris Kennaway #include "match.h"
33ca3176e7SBrian Feldman #include "groupaccess.h"
34ca3176e7SBrian Feldman #include "log.h"
35ca3176e7SBrian Feldman #include "servconf.h"
36a04a10f8SKris Kennaway #include "auth.h"
37ca3176e7SBrian Feldman #include "auth-options.h"
38ca3176e7SBrian Feldman #include "canohost.h"
39af12a3e7SDag-Erling Smørgrav #include "buffer.h"
40af12a3e7SDag-Erling Smørgrav #include "bufaux.h"
41af12a3e7SDag-Erling Smørgrav #include "uidswap.h"
42af12a3e7SDag-Erling Smørgrav #include "tildexpand.h"
4380628bacSDag-Erling Smørgrav #include "misc.h"
4480628bacSDag-Erling Smørgrav #include "bufaux.h"
4580628bacSDag-Erling Smørgrav #include "packet.h"
46a04a10f8SKris Kennaway 
47a04a10f8SKris Kennaway /* import */
48a04a10f8SKris Kennaway extern ServerOptions options;
49a04a10f8SKris Kennaway 
5080628bacSDag-Erling Smørgrav /* Debugging messages */
5180628bacSDag-Erling Smørgrav Buffer auth_debug;
5280628bacSDag-Erling Smørgrav int auth_debug_init;
5380628bacSDag-Erling Smørgrav 
54a04a10f8SKris Kennaway /*
55ca3176e7SBrian Feldman  * Check if the user is allowed to log in via ssh. If user is listed
56ca3176e7SBrian Feldman  * in DenyUsers or one of user's groups is listed in DenyGroups, false
57ca3176e7SBrian Feldman  * will be returned. If AllowUsers isn't empty and user isn't listed
58ca3176e7SBrian Feldman  * there, or if AllowGroups isn't empty and one of user's groups isn't
59ca3176e7SBrian Feldman  * listed there, false will be returned.
60a04a10f8SKris Kennaway  * If the user's shell is not executable, false will be returned.
61a04a10f8SKris Kennaway  * Otherwise true is returned.
62a04a10f8SKris Kennaway  */
63a04a10f8SKris Kennaway int
64a04a10f8SKris Kennaway allowed_user(struct passwd * pw)
65a04a10f8SKris Kennaway {
66a04a10f8SKris Kennaway 	struct stat st;
67af12a3e7SDag-Erling Smørgrav 	const char *hostname = NULL, *ipaddr = NULL;
68c322fe35SKris Kennaway 	char *shell;
69a04a10f8SKris Kennaway 	int i;
70a04a10f8SKris Kennaway 
71a04a10f8SKris Kennaway 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
72ca3176e7SBrian Feldman 	if (!pw || !pw->pw_name)
73a04a10f8SKris Kennaway 		return 0;
74a04a10f8SKris Kennaway 
75c322fe35SKris Kennaway 	/*
76c322fe35SKris Kennaway 	 * Get the shell from the password data.  An empty shell field is
77c322fe35SKris Kennaway 	 * legal, and means /bin/sh.
78c322fe35SKris Kennaway 	 */
79c322fe35SKris Kennaway 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
80c322fe35SKris Kennaway 
81a04a10f8SKris Kennaway 	/* deny if shell does not exists or is not executable */
82af12a3e7SDag-Erling Smørgrav 	if (stat(shell, &st) != 0) {
83af12a3e7SDag-Erling Smørgrav 		log("User %.100s not allowed because shell %.100s does not exist",
84af12a3e7SDag-Erling Smørgrav 		    pw->pw_name, shell);
85a04a10f8SKris Kennaway 		return 0;
86af12a3e7SDag-Erling Smørgrav 	}
8780628bacSDag-Erling Smørgrav 	if (S_ISREG(st.st_mode) == 0 ||
8880628bacSDag-Erling Smørgrav 	    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
89af12a3e7SDag-Erling Smørgrav 		log("User %.100s not allowed because shell %.100s is not executable",
90af12a3e7SDag-Erling Smørgrav 		    pw->pw_name, shell);
91a04a10f8SKris Kennaway 		return 0;
92af12a3e7SDag-Erling Smørgrav 	}
93af12a3e7SDag-Erling Smørgrav 
94af12a3e7SDag-Erling Smørgrav 	if (options.num_deny_users > 0 || options.num_allow_users > 0) {
95af12a3e7SDag-Erling Smørgrav 		hostname = get_canonical_hostname(options.verify_reverse_mapping);
96af12a3e7SDag-Erling Smørgrav 		ipaddr = get_remote_ipaddr();
97af12a3e7SDag-Erling Smørgrav 	}
98a04a10f8SKris Kennaway 
99a04a10f8SKris Kennaway 	/* Return false if user is listed in DenyUsers */
100a04a10f8SKris Kennaway 	if (options.num_deny_users > 0) {
101a04a10f8SKris Kennaway 		for (i = 0; i < options.num_deny_users; i++)
102af12a3e7SDag-Erling Smørgrav 			if (match_user(pw->pw_name, hostname, ipaddr,
103af12a3e7SDag-Erling Smørgrav 			    options.deny_users[i])) {
104af12a3e7SDag-Erling Smørgrav 				log("User %.100s not allowed because listed in DenyUsers",
105af12a3e7SDag-Erling Smørgrav 				    pw->pw_name);
106a04a10f8SKris Kennaway 				return 0;
107a04a10f8SKris Kennaway 			}
108af12a3e7SDag-Erling Smørgrav 	}
109a04a10f8SKris Kennaway 	/* Return false if AllowUsers isn't empty and user isn't listed there */
110a04a10f8SKris Kennaway 	if (options.num_allow_users > 0) {
111a04a10f8SKris Kennaway 		for (i = 0; i < options.num_allow_users; i++)
112af12a3e7SDag-Erling Smørgrav 			if (match_user(pw->pw_name, hostname, ipaddr,
113af12a3e7SDag-Erling Smørgrav 			    options.allow_users[i]))
114a04a10f8SKris Kennaway 				break;
115a04a10f8SKris Kennaway 		/* i < options.num_allow_users iff we break for loop */
116af12a3e7SDag-Erling Smørgrav 		if (i >= options.num_allow_users) {
117af12a3e7SDag-Erling Smørgrav 			log("User %.100s not allowed because not listed in AllowUsers",
118af12a3e7SDag-Erling Smørgrav 			    pw->pw_name);
119a04a10f8SKris Kennaway 			return 0;
120a04a10f8SKris Kennaway 		}
121af12a3e7SDag-Erling Smørgrav 	}
122a04a10f8SKris Kennaway 	if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
123ca3176e7SBrian Feldman 		/* Get the user's group access list (primary and supplementary) */
124af12a3e7SDag-Erling Smørgrav 		if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
125af12a3e7SDag-Erling Smørgrav 			log("User %.100s not allowed because not in any group",
126af12a3e7SDag-Erling Smørgrav 			    pw->pw_name);
127a04a10f8SKris Kennaway 			return 0;
128af12a3e7SDag-Erling Smørgrav 		}
129a04a10f8SKris Kennaway 
130ca3176e7SBrian Feldman 		/* Return false if one of user's groups is listed in DenyGroups */
131ca3176e7SBrian Feldman 		if (options.num_deny_groups > 0)
132ca3176e7SBrian Feldman 			if (ga_match(options.deny_groups,
133ca3176e7SBrian Feldman 			    options.num_deny_groups)) {
134ca3176e7SBrian Feldman 				ga_free();
135af12a3e7SDag-Erling Smørgrav 				log("User %.100s not allowed because a group is listed in DenyGroups",
136af12a3e7SDag-Erling Smørgrav 				    pw->pw_name);
137a04a10f8SKris Kennaway 				return 0;
138a04a10f8SKris Kennaway 			}
139a04a10f8SKris Kennaway 		/*
140ca3176e7SBrian Feldman 		 * Return false if AllowGroups isn't empty and one of user's groups
141a04a10f8SKris Kennaway 		 * isn't listed there
142a04a10f8SKris Kennaway 		 */
143ca3176e7SBrian Feldman 		if (options.num_allow_groups > 0)
144ca3176e7SBrian Feldman 			if (!ga_match(options.allow_groups,
145ca3176e7SBrian Feldman 			    options.num_allow_groups)) {
146ca3176e7SBrian Feldman 				ga_free();
147af12a3e7SDag-Erling Smørgrav 				log("User %.100s not allowed because none of user's groups are listed in AllowGroups",
148af12a3e7SDag-Erling Smørgrav 				    pw->pw_name);
149a04a10f8SKris Kennaway 				return 0;
150a04a10f8SKris Kennaway 			}
151ca3176e7SBrian Feldman 		ga_free();
152a04a10f8SKris Kennaway 	}
153a04a10f8SKris Kennaway 	/* We found no reason not to let this user try to log on... */
154a04a10f8SKris Kennaway 	return 1;
155a04a10f8SKris Kennaway }
156ca3176e7SBrian Feldman 
157ca3176e7SBrian Feldman Authctxt *
158ca3176e7SBrian Feldman authctxt_new(void)
159ca3176e7SBrian Feldman {
160ca3176e7SBrian Feldman 	Authctxt *authctxt = xmalloc(sizeof(*authctxt));
161ca3176e7SBrian Feldman 	memset(authctxt, 0, sizeof(*authctxt));
162ca3176e7SBrian Feldman 	return authctxt;
163ca3176e7SBrian Feldman }
164ca3176e7SBrian Feldman 
165ca3176e7SBrian Feldman void
166ca3176e7SBrian Feldman auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
167ca3176e7SBrian Feldman {
168ca3176e7SBrian Feldman 	void (*authlog) (const char *fmt,...) = verbose;
169ca3176e7SBrian Feldman 	char *authmsg;
170ca3176e7SBrian Feldman 
171ca3176e7SBrian Feldman 	/* Raise logging level */
172ca3176e7SBrian Feldman 	if (authenticated == 1 ||
173ca3176e7SBrian Feldman 	    !authctxt->valid ||
174ca3176e7SBrian Feldman 	    authctxt->failures >= AUTH_FAIL_LOG ||
175ca3176e7SBrian Feldman 	    strcmp(method, "password") == 0)
176ca3176e7SBrian Feldman 		authlog = log;
177ca3176e7SBrian Feldman 
178ca3176e7SBrian Feldman 	if (authctxt->postponed)
179ca3176e7SBrian Feldman 		authmsg = "Postponed";
180ca3176e7SBrian Feldman 	else
181ca3176e7SBrian Feldman 		authmsg = authenticated ? "Accepted" : "Failed";
182ca3176e7SBrian Feldman 
183ca3176e7SBrian Feldman 	authlog("%s %s for %s%.100s from %.200s port %d%s",
184ca3176e7SBrian Feldman 	    authmsg,
185ca3176e7SBrian Feldman 	    method,
186ca3176e7SBrian Feldman 	    authctxt->valid ? "" : "illegal user ",
187af12a3e7SDag-Erling Smørgrav 	    authctxt->user,
188ca3176e7SBrian Feldman 	    get_remote_ipaddr(),
189ca3176e7SBrian Feldman 	    get_remote_port(),
190ca3176e7SBrian Feldman 	    info);
191ca3176e7SBrian Feldman }
192ca3176e7SBrian Feldman 
193ca3176e7SBrian Feldman /*
194ca3176e7SBrian Feldman  * Check whether root logins are disallowed.
195ca3176e7SBrian Feldman  */
196ca3176e7SBrian Feldman int
197ca3176e7SBrian Feldman auth_root_allowed(char *method)
198ca3176e7SBrian Feldman {
199ca3176e7SBrian Feldman 	switch (options.permit_root_login) {
200ca3176e7SBrian Feldman 	case PERMIT_YES:
201ca3176e7SBrian Feldman 		return 1;
202ca3176e7SBrian Feldman 		break;
203ca3176e7SBrian Feldman 	case PERMIT_NO_PASSWD:
204ca3176e7SBrian Feldman 		if (strcmp(method, "password") != 0)
205ca3176e7SBrian Feldman 			return 1;
206ca3176e7SBrian Feldman 		break;
207ca3176e7SBrian Feldman 	case PERMIT_FORCED_ONLY:
208ca3176e7SBrian Feldman 		if (forced_command) {
209ca3176e7SBrian Feldman 			log("Root login accepted for forced command.");
210ca3176e7SBrian Feldman 			return 1;
211ca3176e7SBrian Feldman 		}
212ca3176e7SBrian Feldman 		break;
213ca3176e7SBrian Feldman 	}
214ca3176e7SBrian Feldman 	log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
215ca3176e7SBrian Feldman 	return 0;
216ca3176e7SBrian Feldman }
217af12a3e7SDag-Erling Smørgrav 
218af12a3e7SDag-Erling Smørgrav 
219af12a3e7SDag-Erling Smørgrav /*
220af12a3e7SDag-Erling Smørgrav  * Given a template and a passwd structure, build a filename
221af12a3e7SDag-Erling Smørgrav  * by substituting % tokenised options. Currently, %% becomes '%',
222af12a3e7SDag-Erling Smørgrav  * %h becomes the home directory and %u the username.
223af12a3e7SDag-Erling Smørgrav  *
224af12a3e7SDag-Erling Smørgrav  * This returns a buffer allocated by xmalloc.
225af12a3e7SDag-Erling Smørgrav  */
226af12a3e7SDag-Erling Smørgrav char *
227af12a3e7SDag-Erling Smørgrav expand_filename(const char *filename, struct passwd *pw)
228af12a3e7SDag-Erling Smørgrav {
229af12a3e7SDag-Erling Smørgrav 	Buffer buffer;
230af12a3e7SDag-Erling Smørgrav 	char *file;
231af12a3e7SDag-Erling Smørgrav 	const char *cp;
232af12a3e7SDag-Erling Smørgrav 
233af12a3e7SDag-Erling Smørgrav 	/*
234af12a3e7SDag-Erling Smørgrav 	 * Build the filename string in the buffer by making the appropriate
235af12a3e7SDag-Erling Smørgrav 	 * substitutions to the given file name.
236af12a3e7SDag-Erling Smørgrav 	 */
237af12a3e7SDag-Erling Smørgrav 	buffer_init(&buffer);
238af12a3e7SDag-Erling Smørgrav 	for (cp = filename; *cp; cp++) {
239af12a3e7SDag-Erling Smørgrav 		if (cp[0] == '%' && cp[1] == '%') {
240af12a3e7SDag-Erling Smørgrav 			buffer_append(&buffer, "%", 1);
241af12a3e7SDag-Erling Smørgrav 			cp++;
242af12a3e7SDag-Erling Smørgrav 			continue;
243af12a3e7SDag-Erling Smørgrav 		}
244af12a3e7SDag-Erling Smørgrav 		if (cp[0] == '%' && cp[1] == 'h') {
245af12a3e7SDag-Erling Smørgrav 			buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
246af12a3e7SDag-Erling Smørgrav 			cp++;
247af12a3e7SDag-Erling Smørgrav 			continue;
248af12a3e7SDag-Erling Smørgrav 		}
249af12a3e7SDag-Erling Smørgrav 		if (cp[0] == '%' && cp[1] == 'u') {
250af12a3e7SDag-Erling Smørgrav 			buffer_append(&buffer, pw->pw_name,
251af12a3e7SDag-Erling Smørgrav 			    strlen(pw->pw_name));
252af12a3e7SDag-Erling Smørgrav 			cp++;
253af12a3e7SDag-Erling Smørgrav 			continue;
254af12a3e7SDag-Erling Smørgrav 		}
255af12a3e7SDag-Erling Smørgrav 		buffer_append(&buffer, cp, 1);
256af12a3e7SDag-Erling Smørgrav 	}
257af12a3e7SDag-Erling Smørgrav 	buffer_append(&buffer, "\0", 1);
258af12a3e7SDag-Erling Smørgrav 
259af12a3e7SDag-Erling Smørgrav 	/*
260af12a3e7SDag-Erling Smørgrav 	 * Ensure that filename starts anchored. If not, be backward
261af12a3e7SDag-Erling Smørgrav 	 * compatible and prepend the '%h/'
262af12a3e7SDag-Erling Smørgrav 	 */
263af12a3e7SDag-Erling Smørgrav 	file = xmalloc(MAXPATHLEN);
264af12a3e7SDag-Erling Smørgrav 	cp = buffer_ptr(&buffer);
265af12a3e7SDag-Erling Smørgrav 	if (*cp != '/')
266af12a3e7SDag-Erling Smørgrav 		snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
267af12a3e7SDag-Erling Smørgrav 	else
268af12a3e7SDag-Erling Smørgrav 		strlcpy(file, cp, MAXPATHLEN);
269af12a3e7SDag-Erling Smørgrav 
270af12a3e7SDag-Erling Smørgrav 	buffer_free(&buffer);
271af12a3e7SDag-Erling Smørgrav 	return file;
272af12a3e7SDag-Erling Smørgrav }
273af12a3e7SDag-Erling Smørgrav 
274af12a3e7SDag-Erling Smørgrav char *
275af12a3e7SDag-Erling Smørgrav authorized_keys_file(struct passwd *pw)
276af12a3e7SDag-Erling Smørgrav {
277af12a3e7SDag-Erling Smørgrav 	return expand_filename(options.authorized_keys_file, pw);
278af12a3e7SDag-Erling Smørgrav }
279af12a3e7SDag-Erling Smørgrav 
280af12a3e7SDag-Erling Smørgrav char *
281af12a3e7SDag-Erling Smørgrav authorized_keys_file2(struct passwd *pw)
282af12a3e7SDag-Erling Smørgrav {
283af12a3e7SDag-Erling Smørgrav 	return expand_filename(options.authorized_keys_file2, pw);
284af12a3e7SDag-Erling Smørgrav }
285af12a3e7SDag-Erling Smørgrav 
286af12a3e7SDag-Erling Smørgrav /* return ok if key exists in sysfile or userfile */
287af12a3e7SDag-Erling Smørgrav HostStatus
288af12a3e7SDag-Erling Smørgrav check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
289af12a3e7SDag-Erling Smørgrav     const char *sysfile, const char *userfile)
290af12a3e7SDag-Erling Smørgrav {
291af12a3e7SDag-Erling Smørgrav 	Key *found;
292af12a3e7SDag-Erling Smørgrav 	char *user_hostfile;
293af12a3e7SDag-Erling Smørgrav 	struct stat st;
294af12a3e7SDag-Erling Smørgrav 	HostStatus host_status;
295af12a3e7SDag-Erling Smørgrav 
296af12a3e7SDag-Erling Smørgrav 	/* Check if we know the host and its host key. */
297af12a3e7SDag-Erling Smørgrav 	found = key_new(key->type);
298af12a3e7SDag-Erling Smørgrav 	host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
299af12a3e7SDag-Erling Smørgrav 
300af12a3e7SDag-Erling Smørgrav 	if (host_status != HOST_OK && userfile != NULL) {
301af12a3e7SDag-Erling Smørgrav 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
302af12a3e7SDag-Erling Smørgrav 		if (options.strict_modes &&
303af12a3e7SDag-Erling Smørgrav 		    (stat(user_hostfile, &st) == 0) &&
304af12a3e7SDag-Erling Smørgrav 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
305af12a3e7SDag-Erling Smørgrav 		    (st.st_mode & 022) != 0)) {
306af12a3e7SDag-Erling Smørgrav 			log("Authentication refused for %.100s: "
307af12a3e7SDag-Erling Smørgrav 			    "bad owner or modes for %.200s",
308af12a3e7SDag-Erling Smørgrav 			    pw->pw_name, user_hostfile);
309af12a3e7SDag-Erling Smørgrav 		} else {
310af12a3e7SDag-Erling Smørgrav 			temporarily_use_uid(pw);
311af12a3e7SDag-Erling Smørgrav 			host_status = check_host_in_hostfile(user_hostfile,
312af12a3e7SDag-Erling Smørgrav 			    host, key, found, NULL);
313af12a3e7SDag-Erling Smørgrav 			restore_uid();
314af12a3e7SDag-Erling Smørgrav 		}
315af12a3e7SDag-Erling Smørgrav 		xfree(user_hostfile);
316af12a3e7SDag-Erling Smørgrav 	}
317af12a3e7SDag-Erling Smørgrav 	key_free(found);
318af12a3e7SDag-Erling Smørgrav 
319af12a3e7SDag-Erling Smørgrav 	debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
320af12a3e7SDag-Erling Smørgrav 	    "ok" : "not found", host);
321af12a3e7SDag-Erling Smørgrav 	return host_status;
322af12a3e7SDag-Erling Smørgrav }
323af12a3e7SDag-Erling Smørgrav 
324af12a3e7SDag-Erling Smørgrav 
325af12a3e7SDag-Erling Smørgrav /*
326af12a3e7SDag-Erling Smørgrav  * Check a given file for security. This is defined as all components
327af12a3e7SDag-Erling Smørgrav  * of the path to the file must either be owned by either the owner of
328af12a3e7SDag-Erling Smørgrav  * of the file or root and no directories must be group or world writable.
329af12a3e7SDag-Erling Smørgrav  *
330af12a3e7SDag-Erling Smørgrav  * XXX Should any specific check be done for sym links ?
331af12a3e7SDag-Erling Smørgrav  *
332af12a3e7SDag-Erling Smørgrav  * Takes an open file descriptor, the file name, a uid and and
333af12a3e7SDag-Erling Smørgrav  * error buffer plus max size as arguments.
334af12a3e7SDag-Erling Smørgrav  *
335af12a3e7SDag-Erling Smørgrav  * Returns 0 on success and -1 on failure
336af12a3e7SDag-Erling Smørgrav  */
337af12a3e7SDag-Erling Smørgrav int
338af12a3e7SDag-Erling Smørgrav secure_filename(FILE *f, const char *file, struct passwd *pw,
339af12a3e7SDag-Erling Smørgrav     char *err, size_t errlen)
340af12a3e7SDag-Erling Smørgrav {
341af12a3e7SDag-Erling Smørgrav 	uid_t uid = pw->pw_uid;
342af12a3e7SDag-Erling Smørgrav 	char buf[MAXPATHLEN], homedir[MAXPATHLEN];
343af12a3e7SDag-Erling Smørgrav 	char *cp;
344af12a3e7SDag-Erling Smørgrav 	struct stat st;
345af12a3e7SDag-Erling Smørgrav 
346af12a3e7SDag-Erling Smørgrav 	if (realpath(file, buf) == NULL) {
347af12a3e7SDag-Erling Smørgrav 		snprintf(err, errlen, "realpath %s failed: %s", file,
348af12a3e7SDag-Erling Smørgrav 		    strerror(errno));
349af12a3e7SDag-Erling Smørgrav 		return -1;
350af12a3e7SDag-Erling Smørgrav 	}
351af12a3e7SDag-Erling Smørgrav 	if (realpath(pw->pw_dir, homedir) == NULL) {
352af12a3e7SDag-Erling Smørgrav 		snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir,
353af12a3e7SDag-Erling Smørgrav 		    strerror(errno));
354af12a3e7SDag-Erling Smørgrav 		return -1;
355af12a3e7SDag-Erling Smørgrav 	}
356af12a3e7SDag-Erling Smørgrav 
357af12a3e7SDag-Erling Smørgrav 	/* check the open file to avoid races */
358af12a3e7SDag-Erling Smørgrav 	if (fstat(fileno(f), &st) < 0 ||
359af12a3e7SDag-Erling Smørgrav 	    (st.st_uid != 0 && st.st_uid != uid) ||
360af12a3e7SDag-Erling Smørgrav 	    (st.st_mode & 022) != 0) {
361af12a3e7SDag-Erling Smørgrav 		snprintf(err, errlen, "bad ownership or modes for file %s",
362af12a3e7SDag-Erling Smørgrav 		    buf);
363af12a3e7SDag-Erling Smørgrav 		return -1;
364af12a3e7SDag-Erling Smørgrav 	}
365af12a3e7SDag-Erling Smørgrav 
366af12a3e7SDag-Erling Smørgrav 	/* for each component of the canonical path, walking upwards */
367af12a3e7SDag-Erling Smørgrav 	for (;;) {
368af12a3e7SDag-Erling Smørgrav 		if ((cp = dirname(buf)) == NULL) {
369af12a3e7SDag-Erling Smørgrav 			snprintf(err, errlen, "dirname() failed");
370af12a3e7SDag-Erling Smørgrav 			return -1;
371af12a3e7SDag-Erling Smørgrav 		}
372af12a3e7SDag-Erling Smørgrav 		strlcpy(buf, cp, sizeof(buf));
373af12a3e7SDag-Erling Smørgrav 
374af12a3e7SDag-Erling Smørgrav 		debug3("secure_filename: checking '%s'", buf);
375af12a3e7SDag-Erling Smørgrav 		if (stat(buf, &st) < 0 ||
376af12a3e7SDag-Erling Smørgrav 		    (st.st_uid != 0 && st.st_uid != uid) ||
377af12a3e7SDag-Erling Smørgrav 		    (st.st_mode & 022) != 0) {
378af12a3e7SDag-Erling Smørgrav 			snprintf(err, errlen,
379af12a3e7SDag-Erling Smørgrav 			    "bad ownership or modes for directory %s", buf);
380af12a3e7SDag-Erling Smørgrav 			return -1;
381af12a3e7SDag-Erling Smørgrav 		}
382af12a3e7SDag-Erling Smørgrav 
383af12a3e7SDag-Erling Smørgrav 		/* If are passed the homedir then we can stop */
384af12a3e7SDag-Erling Smørgrav 		if (strcmp(homedir, buf) == 0) {
385af12a3e7SDag-Erling Smørgrav 			debug3("secure_filename: terminating check at '%s'",
386af12a3e7SDag-Erling Smørgrav 			    buf);
387af12a3e7SDag-Erling Smørgrav 			break;
388af12a3e7SDag-Erling Smørgrav 		}
389af12a3e7SDag-Erling Smørgrav 		/*
390af12a3e7SDag-Erling Smørgrav 		 * dirname should always complete with a "/" path,
391af12a3e7SDag-Erling Smørgrav 		 * but we can be paranoid and check for "." too
392af12a3e7SDag-Erling Smørgrav 		 */
393af12a3e7SDag-Erling Smørgrav 		if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
394af12a3e7SDag-Erling Smørgrav 			break;
395af12a3e7SDag-Erling Smørgrav 	}
396af12a3e7SDag-Erling Smørgrav 	return 0;
397af12a3e7SDag-Erling Smørgrav }
39880628bacSDag-Erling Smørgrav 
39980628bacSDag-Erling Smørgrav struct passwd *
40080628bacSDag-Erling Smørgrav getpwnamallow(const char *user)
40180628bacSDag-Erling Smørgrav {
40280628bacSDag-Erling Smørgrav #ifdef HAVE_LOGIN_CAP
40380628bacSDag-Erling Smørgrav 	extern login_cap_t *lc;
40480628bacSDag-Erling Smørgrav #ifdef BSD_AUTH
40580628bacSDag-Erling Smørgrav 	auth_session_t *as;
40680628bacSDag-Erling Smørgrav #endif
40780628bacSDag-Erling Smørgrav #endif
40880628bacSDag-Erling Smørgrav 	struct passwd *pw;
40980628bacSDag-Erling Smørgrav 
41080628bacSDag-Erling Smørgrav 	pw = getpwnam(user);
41180628bacSDag-Erling Smørgrav 	if (pw == NULL || !allowed_user(pw))
41280628bacSDag-Erling Smørgrav 		return (NULL);
41380628bacSDag-Erling Smørgrav #ifdef HAVE_LOGIN_CAP
41480628bacSDag-Erling Smørgrav 	if ((lc = login_getclass(pw->pw_class)) == NULL) {
41580628bacSDag-Erling Smørgrav 		debug("unable to get login class: %s", user);
41680628bacSDag-Erling Smørgrav 		return (NULL);
41780628bacSDag-Erling Smørgrav 	}
41880628bacSDag-Erling Smørgrav #ifdef BSD_AUTH
41980628bacSDag-Erling Smørgrav 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
42080628bacSDag-Erling Smørgrav 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
42180628bacSDag-Erling Smørgrav 		debug("Approval failure for %s", user);
42280628bacSDag-Erling Smørgrav 		pw = NULL;
42380628bacSDag-Erling Smørgrav 	}
42480628bacSDag-Erling Smørgrav 	if (as != NULL)
42580628bacSDag-Erling Smørgrav 		auth_close(as);
42680628bacSDag-Erling Smørgrav #endif
42780628bacSDag-Erling Smørgrav #endif
42880628bacSDag-Erling Smørgrav 	if (pw != NULL)
42980628bacSDag-Erling Smørgrav 		return (pwcopy(pw));
43080628bacSDag-Erling Smørgrav 	return (NULL);
43180628bacSDag-Erling Smørgrav }
43280628bacSDag-Erling Smørgrav 
43380628bacSDag-Erling Smørgrav void
43480628bacSDag-Erling Smørgrav auth_debug_add(const char *fmt,...)
43580628bacSDag-Erling Smørgrav {
43680628bacSDag-Erling Smørgrav 	char buf[1024];
43780628bacSDag-Erling Smørgrav 	va_list args;
43880628bacSDag-Erling Smørgrav 
43980628bacSDag-Erling Smørgrav 	if (!auth_debug_init)
44080628bacSDag-Erling Smørgrav 		return;
44180628bacSDag-Erling Smørgrav 
44280628bacSDag-Erling Smørgrav 	va_start(args, fmt);
44380628bacSDag-Erling Smørgrav 	vsnprintf(buf, sizeof(buf), fmt, args);
44480628bacSDag-Erling Smørgrav 	va_end(args);
44580628bacSDag-Erling Smørgrav 	buffer_put_cstring(&auth_debug, buf);
44680628bacSDag-Erling Smørgrav }
44780628bacSDag-Erling Smørgrav 
44880628bacSDag-Erling Smørgrav void
44980628bacSDag-Erling Smørgrav auth_debug_send(void)
45080628bacSDag-Erling Smørgrav {
45180628bacSDag-Erling Smørgrav 	char *msg;
45280628bacSDag-Erling Smørgrav 
45380628bacSDag-Erling Smørgrav 	if (!auth_debug_init)
45480628bacSDag-Erling Smørgrav 		return;
45580628bacSDag-Erling Smørgrav 	while (buffer_len(&auth_debug)) {
45680628bacSDag-Erling Smørgrav 		msg = buffer_get_string(&auth_debug, NULL);
45780628bacSDag-Erling Smørgrav 		packet_send_debug("%s", msg);
45880628bacSDag-Erling Smørgrav 		xfree(msg);
45980628bacSDag-Erling Smørgrav 	}
46080628bacSDag-Erling Smørgrav }
46180628bacSDag-Erling Smørgrav 
46280628bacSDag-Erling Smørgrav void
46380628bacSDag-Erling Smørgrav auth_debug_reset(void)
46480628bacSDag-Erling Smørgrav {
46580628bacSDag-Erling Smørgrav 	if (auth_debug_init)
46680628bacSDag-Erling Smørgrav 		buffer_clear(&auth_debug);
46780628bacSDag-Erling Smørgrav 	else {
46880628bacSDag-Erling Smørgrav 		buffer_init(&auth_debug);
46980628bacSDag-Erling Smørgrav 		auth_debug_init = 1;
47080628bacSDag-Erling Smørgrav 	}
47180628bacSDag-Erling Smørgrav }
472