2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth.c,v 1.22 2001/05/20 17:20:35 markus Exp $");
31 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
33 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
39 #include "groupaccess.h"
43 #include "auth-options.h"
49 extern ServerOptions options;
52 * Check if the user is allowed to log in via ssh. If user is listed
53 * in DenyUsers or one of user's groups is listed in DenyGroups, false
54 * will be returned. If AllowUsers isn't empty and user isn't listed
55 * there, or if AllowGroups isn't empty and one of user's groups isn't
56 * listed there, false will be returned.
57 * If the user's shell is not executable, false will be returned.
58 * Otherwise true is returned.
61 allowed_user(struct passwd * pw)
66 #ifdef WITH_AIXAUTHENTICATE
68 #endif /* WITH_AIXAUTHENTICATE */
69 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
70 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
73 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
74 if (!pw || !pw->pw_name)
77 spw = getspnam(pw->pw_name);
79 int days = time(NULL) / 86400;
81 /* Check account expiry */
82 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
85 /* Check password expiry */
86 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
87 (days > (spw->sp_lstchg + spw->sp_max)))
91 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
92 if (!pw || !pw->pw_name)
97 * Get the shell from the password data. An empty shell field is
98 * legal, and means /bin/sh.
100 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
102 /* deny if shell does not exists or is not executable */
103 if (stat(shell, &st) != 0)
105 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
108 /* Return false if user is listed in DenyUsers */
109 if (options.num_deny_users > 0) {
110 for (i = 0; i < options.num_deny_users; i++)
111 if (match_pattern(pw->pw_name, options.deny_users[i]))
114 /* Return false if AllowUsers isn't empty and user isn't listed there */
115 if (options.num_allow_users > 0) {
116 for (i = 0; i < options.num_allow_users; i++)
117 if (match_pattern(pw->pw_name, options.allow_users[i]))
119 /* i < options.num_allow_users iff we break for loop */
120 if (i >= options.num_allow_users)
123 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
124 /* Get the user's group access list (primary and supplementary) */
125 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
128 /* Return false if one of user's groups is listed in DenyGroups */
129 if (options.num_deny_groups > 0)
130 if (ga_match(options.deny_groups,
131 options.num_deny_groups)) {
136 * Return false if AllowGroups isn't empty and one of user's groups
139 if (options.num_allow_groups > 0)
140 if (!ga_match(options.allow_groups,
141 options.num_allow_groups)) {
148 #ifdef WITH_AIXAUTHENTICATE
149 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
150 if (loginmsg && *loginmsg) {
151 /* Remove embedded newlines (if any) */
153 for (p = loginmsg; *p; p++) {
157 /* Remove trailing newline */
159 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
163 #endif /* WITH_AIXAUTHENTICATE */
165 /* We found no reason not to let this user try to log on... */
172 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
173 memset(authctxt, 0, sizeof(*authctxt));
178 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
180 void (*authlog) (const char *fmt,...) = verbose;
183 /* Raise logging level */
184 if (authenticated == 1 ||
186 authctxt->failures >= AUTH_FAIL_LOG ||
187 strcmp(method, "password") == 0)
190 if (authctxt->postponed)
191 authmsg = "Postponed";
193 authmsg = authenticated ? "Accepted" : "Failed";
195 authlog("%s %s for %s%.100s from %.200s port %d%s",
198 authctxt->valid ? "" : "illegal user ",
199 authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
206 * Check whether root logins are disallowed.
209 auth_root_allowed(char *method)
211 switch (options.permit_root_login) {
215 case PERMIT_NO_PASSWD:
216 if (strcmp(method, "password") != 0)
219 case PERMIT_FORCED_ONLY:
220 if (forced_command) {
221 log("Root login accepted for forced command.");
226 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
232 * Given a template and a passwd structure, build a filename
233 * by substituting % tokenised options. Currently, %% becomes '%',
234 * %h becomes the home directory and %u the username.
236 * This returns a buffer allocated by xmalloc.
239 expand_filename(const char *filename, struct passwd *pw)
246 * Build the filename string in the buffer by making the appropriate
247 * substitutions to the given file name.
249 buffer_init(&buffer);
250 for (cp = filename; *cp; cp++) {
251 if (cp[0] == '%' && cp[1] == '%') {
252 buffer_append(&buffer, "%", 1);
256 if (cp[0] == '%' && cp[1] == 'h') {
257 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
261 if (cp[0] == '%' && cp[1] == 'u') {
262 buffer_append(&buffer, pw->pw_name,
263 strlen(pw->pw_name));
267 buffer_append(&buffer, cp, 1);
269 buffer_append(&buffer, "\0", 1);
272 * Ensure that filename starts anchored. If not, be backward
273 * compatible and prepend the '%h/'
275 file = xmalloc(MAXPATHLEN);
276 cp = buffer_ptr(&buffer);
278 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
280 strlcpy(file, cp, MAXPATHLEN);
282 buffer_free(&buffer);
287 authorized_keys_file(struct passwd *pw)
289 return expand_filename(options.authorized_keys_file, pw);
293 authorized_keys_file2(struct passwd *pw)
295 return expand_filename(options.authorized_keys_file2, pw);
299 * Check a given file for security. This is defined as all components
300 * of the path to the file must either be owned by either the owner of
301 * of the file or root and no directories must be world writable.
303 * XXX Should any specific check be done for sym links ?
305 * Takes an open file descriptor, the file name, a uid and and
306 * error buffer plus max size as arguments.
308 * Returns 0 on success and -1 on failure
311 secure_filename(FILE *f, const char *file, uid_t uid, char *err, size_t errlen)
313 char buf[MAXPATHLEN];
317 if (realpath(file, buf) == NULL) {
318 snprintf(err, errlen, "realpath %s failed: %s", file,
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",
332 /* for each component of the canonical path, walking upwards */
334 if ((cp = dirname(buf)) == NULL) {
335 snprintf(err, errlen, "dirname() failed");
338 strlcpy(buf, cp, sizeof(buf));
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) {
344 snprintf(err, errlen,
345 "bad ownership or modes for directory %s", buf);
350 * dirname should always complete with a "/" path,
351 * but we can be paranoid and check for "." too
353 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))