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.19 2001/03/02 18:54:31 deraadt Exp $");
31 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
33 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
37 #include "groupaccess.h"
41 #include "auth-options.h"
45 extern ServerOptions options;
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.
53 * If the user's shell is not executable, false will be returned.
54 * Otherwise true is returned.
57 allowed_user(struct passwd * pw)
62 #ifdef WITH_AIXAUTHENTICATE
64 #endif /* WITH_AIXAUTHENTICATE */
65 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
66 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
69 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
70 if (!pw || !pw->pw_name)
73 spw = getspnam(pw->pw_name);
75 int days = time(NULL) / 86400;
77 /* Check account expiry */
78 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
81 /* Check password expiry */
82 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
83 (days > (spw->sp_lstchg + spw->sp_max)))
87 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
88 if (!pw || !pw->pw_name)
93 * Get the shell from the password data. An empty shell field is
94 * legal, and means /bin/sh.
96 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
98 /* deny if shell does not exists or is not executable */
99 if (stat(shell, &st) != 0)
101 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
104 /* Return false if user is listed in DenyUsers */
105 if (options.num_deny_users > 0) {
106 for (i = 0; i < options.num_deny_users; i++)
107 if (match_pattern(pw->pw_name, options.deny_users[i]))
110 /* Return false if AllowUsers isn't empty and user isn't listed there */
111 if (options.num_allow_users > 0) {
112 for (i = 0; i < options.num_allow_users; i++)
113 if (match_pattern(pw->pw_name, options.allow_users[i]))
115 /* i < options.num_allow_users iff we break for loop */
116 if (i >= options.num_allow_users)
119 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
120 /* Get the user's group access list (primary and supplementary) */
121 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
124 /* Return false if one of user's groups is listed in DenyGroups */
125 if (options.num_deny_groups > 0)
126 if (ga_match(options.deny_groups,
127 options.num_deny_groups)) {
132 * Return false if AllowGroups isn't empty and one of user's groups
135 if (options.num_allow_groups > 0)
136 if (!ga_match(options.allow_groups,
137 options.num_allow_groups)) {
144 #ifdef WITH_AIXAUTHENTICATE
145 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
146 if (loginmsg && *loginmsg) {
147 /* Remove embedded newlines (if any) */
149 for (p = loginmsg; *p; p++) {
153 /* Remove trailing newline */
155 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
159 #endif /* WITH_AIXAUTHENTICATE */
161 /* We found no reason not to let this user try to log on... */
168 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
169 memset(authctxt, 0, sizeof(*authctxt));
174 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
176 void (*authlog) (const char *fmt,...) = verbose;
179 /* Raise logging level */
180 if (authenticated == 1 ||
182 authctxt->failures >= AUTH_FAIL_LOG ||
183 strcmp(method, "password") == 0)
186 if (authctxt->postponed)
187 authmsg = "Postponed";
189 authmsg = authenticated ? "Accepted" : "Failed";
191 authlog("%s %s for %s%.100s from %.200s port %d%s",
194 authctxt->valid ? "" : "illegal user ",
195 authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
202 * Check whether root logins are disallowed.
205 auth_root_allowed(char *method)
207 switch (options.permit_root_login) {
211 case PERMIT_NO_PASSWD:
212 if (strcmp(method, "password") != 0)
215 case PERMIT_FORCED_ONLY:
216 if (forced_command) {
217 log("Root login accepted for forced command.");
222 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());