2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * As far as I am concerned, the code I have written for this software
6 * can be used freely for any purpose. Any derived versions of this
7 * software must be clearly marked as such, and if the derived work is
8 * incompatible with the protocol description in the RFC file, it must be
9 * called by a name other than "ssh" or "Secure Shell".
12 * Copyright (c) 2000 Markus Friedl. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 RCSID("$OpenBSD: auth.c,v 1.11 2000/10/11 20:27:23 markus Exp $");
52 #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
54 #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
62 extern ServerOptions options;
65 * Check if the user is allowed to log in via ssh. If user is listed in
66 * DenyUsers or user's primary group is listed in DenyGroups, false will
67 * be returned. If AllowUsers isn't empty and user isn't listed there, or
68 * if AllowGroups isn't empty and user isn't listed there, false will be
70 * If the user's shell is not executable, false will be returned.
71 * Otherwise true is returned.
74 allowed_user(struct passwd * pw)
80 #ifdef WITH_AIXAUTHENTICATE
82 #endif /* WITH_AIXAUTHENTICATE */
83 #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
84 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
87 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
91 spw = getspnam(pw->pw_name);
93 int days = time(NULL) / 86400;
95 /* Check account expiry */
96 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
99 /* Check password expiry */
100 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
101 (days > (spw->sp_lstchg + spw->sp_max)))
105 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
111 * Get the shell from the password data. An empty shell field is
112 * legal, and means /bin/sh.
114 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
116 /* deny if shell does not exists or is not executable */
117 if (stat(shell, &st) != 0)
119 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
122 /* Return false if user is listed in DenyUsers */
123 if (options.num_deny_users > 0) {
126 for (i = 0; i < options.num_deny_users; i++)
127 if (match_pattern(pw->pw_name, options.deny_users[i]))
130 /* Return false if AllowUsers isn't empty and user isn't listed there */
131 if (options.num_allow_users > 0) {
134 for (i = 0; i < options.num_allow_users; i++)
135 if (match_pattern(pw->pw_name, options.allow_users[i]))
137 /* i < options.num_allow_users iff we break for loop */
138 if (i >= options.num_allow_users)
141 /* Get the primary group name if we need it. Return false if it fails */
142 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
143 grp = getgrgid(pw->pw_gid);
147 /* Return false if user's group is listed in DenyGroups */
148 if (options.num_deny_groups > 0) {
151 for (i = 0; i < options.num_deny_groups; i++)
152 if (match_pattern(grp->gr_name, options.deny_groups[i]))
156 * Return false if AllowGroups isn't empty and user's group
159 if (options.num_allow_groups > 0) {
162 for (i = 0; i < options.num_allow_groups; i++)
163 if (match_pattern(grp->gr_name, options.allow_groups[i]))
165 /* i < options.num_allow_groups iff we break for
167 if (i >= options.num_allow_groups)
172 #ifdef WITH_AIXAUTHENTICATE
173 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
174 if (loginmsg && *loginmsg) {
175 /* Remove embedded newlines (if any) */
177 for (p = loginmsg; *p; p++) {
181 /* Remove trailing newline */
183 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
187 #endif /* WITH_AIXAUTHENTICATE */
189 /* We found no reason not to let this user try to log on... */