]>
Commit | Line | Data |
---|---|---|
7368a6c8 | 1 | /* |
f3c7c613 | 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
bcbf86ec | 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. | |
7368a6c8 | 23 | */ |
24 | ||
25 | #include "includes.h" | |
5cc8d4ad | 26 | RCSID("$OpenBSD: auth.c,v 1.20 2001/03/17 17:27:59 markus Exp $"); |
7368a6c8 | 27 | |
c1ef8333 | 28 | #ifdef HAVE_LOGIN_H |
29 | #include <login.h> | |
30 | #endif | |
4cb5ffa0 | 31 | #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) |
32 | #include <shadow.h> | |
33 | #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ | |
7368a6c8 | 34 | |
42f11eb2 | 35 | #include "xmalloc.h" |
36 | #include "match.h" | |
37 | #include "groupaccess.h" | |
38 | #include "log.h" | |
39 | #include "servconf.h" | |
e78a59f5 | 40 | #include "auth.h" |
59c97189 | 41 | #include "auth-options.h" |
42f11eb2 | 42 | #include "canohost.h" |
e78a59f5 | 43 | |
7368a6c8 | 44 | /* import */ |
45 | extern ServerOptions options; | |
7368a6c8 | 46 | |
47 | /* | |
c6a69271 | 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. | |
7368a6c8 | 53 | * If the user's shell is not executable, false will be returned. |
6ae2364d | 54 | * Otherwise true is returned. |
7368a6c8 | 55 | */ |
a306f2dd | 56 | int |
7368a6c8 | 57 | allowed_user(struct passwd * pw) |
58 | { | |
59 | struct stat st; | |
5cc8d4ad | 60 | char *shell, *cp; |
7368a6c8 | 61 | int i; |
62 | #ifdef WITH_AIXAUTHENTICATE | |
63 | char *loginmsg; | |
64 | #endif /* WITH_AIXAUTHENTICATE */ | |
75b90ced | 65 | #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ |
37c1c46d | 66 | !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) |
c6a69271 | 67 | struct spwd *spw; |
7368a6c8 | 68 | |
69 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ | |
c6a69271 | 70 | if (!pw || !pw->pw_name) |
7368a6c8 | 71 | return 0; |
72 | ||
4cb5ffa0 | 73 | spw = getspnam(pw->pw_name); |
27494968 | 74 | if (spw != NULL) { |
75 | int days = time(NULL) / 86400; | |
4cb5ffa0 | 76 | |
27494968 | 77 | /* Check account expiry */ |
37c1c46d | 78 | if ((spw->sp_expire >= 0) && (days > spw->sp_expire)) |
27494968 | 79 | return 0; |
80 | ||
81 | /* Check password expiry */ | |
2b87da3b | 82 | if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) && |
7f8f5e00 | 83 | (days > (spw->sp_lstchg + spw->sp_max))) |
27494968 | 84 | return 0; |
85 | } | |
4cb5ffa0 | 86 | #else |
87 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ | |
c6a69271 | 88 | if (!pw || !pw->pw_name) |
4cb5ffa0 | 89 | return 0; |
90 | #endif | |
91 | ||
301e9b01 | 92 | /* |
93 | * Get the shell from the password data. An empty shell field is | |
94 | * legal, and means /bin/sh. | |
95 | */ | |
96 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; | |
97 | ||
5cc8d4ad | 98 | /* disallow anyone who does not have a standard shell */ |
99 | setusershell(); | |
100 | while ((cp = getusershell()) != NULL) | |
101 | if (strcmp(cp, shell) == 0) | |
102 | break; | |
103 | endusershell(); | |
104 | if (cp == NULL) | |
105 | return 0; | |
106 | ||
301e9b01 | 107 | /* deny if shell does not exists or is not executable */ |
108 | if (stat(shell, &st) != 0) | |
7368a6c8 | 109 | return 0; |
110 | if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)))) | |
111 | return 0; | |
112 | ||
113 | /* Return false if user is listed in DenyUsers */ | |
114 | if (options.num_deny_users > 0) { | |
7368a6c8 | 115 | for (i = 0; i < options.num_deny_users; i++) |
116 | if (match_pattern(pw->pw_name, options.deny_users[i])) | |
117 | return 0; | |
118 | } | |
119 | /* Return false if AllowUsers isn't empty and user isn't listed there */ | |
120 | if (options.num_allow_users > 0) { | |
7368a6c8 | 121 | for (i = 0; i < options.num_allow_users; i++) |
122 | if (match_pattern(pw->pw_name, options.allow_users[i])) | |
123 | break; | |
124 | /* i < options.num_allow_users iff we break for loop */ | |
125 | if (i >= options.num_allow_users) | |
126 | return 0; | |
127 | } | |
7368a6c8 | 128 | if (options.num_deny_groups > 0 || options.num_allow_groups > 0) { |
c6a69271 | 129 | /* Get the user's group access list (primary and supplementary) */ |
130 | if (ga_init(pw->pw_name, pw->pw_gid) == 0) | |
7368a6c8 | 131 | return 0; |
132 | ||
c6a69271 | 133 | /* Return false if one of user's groups is listed in DenyGroups */ |
134 | if (options.num_deny_groups > 0) | |
135 | if (ga_match(options.deny_groups, | |
136 | options.num_deny_groups)) { | |
137 | ga_free(); | |
7368a6c8 | 138 | return 0; |
c6a69271 | 139 | } |
7368a6c8 | 140 | /* |
c6a69271 | 141 | * Return false if AllowGroups isn't empty and one of user's groups |
7368a6c8 | 142 | * isn't listed there |
143 | */ | |
c6a69271 | 144 | if (options.num_allow_groups > 0) |
145 | if (!ga_match(options.allow_groups, | |
146 | options.num_allow_groups)) { | |
147 | ga_free(); | |
7368a6c8 | 148 | return 0; |
c6a69271 | 149 | } |
150 | ga_free(); | |
7368a6c8 | 151 | } |
152 | ||
153 | #ifdef WITH_AIXAUTHENTICATE | |
5daf7064 | 154 | if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) { |
c1ef8333 | 155 | if (loginmsg && *loginmsg) { |
156 | /* Remove embedded newlines (if any) */ | |
157 | char *p; | |
5daf7064 | 158 | for (p = loginmsg; *p; p++) { |
c1ef8333 | 159 | if (*p == '\n') |
160 | *p = ' '; | |
5daf7064 | 161 | } |
c1ef8333 | 162 | /* Remove trailing newline */ |
163 | *--p = '\0'; | |
5daf7064 | 164 | log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); |
c1ef8333 | 165 | } |
7368a6c8 | 166 | return 0; |
c1ef8333 | 167 | } |
7368a6c8 | 168 | #endif /* WITH_AIXAUTHENTICATE */ |
169 | ||
170 | /* We found no reason not to let this user try to log on... */ | |
171 | return 1; | |
172 | } | |
59c97189 | 173 | |
174 | Authctxt * | |
175 | authctxt_new(void) | |
176 | { | |
2b87da3b | 177 | Authctxt *authctxt = xmalloc(sizeof(*authctxt)); |
178 | memset(authctxt, 0, sizeof(*authctxt)); | |
179 | return authctxt; | |
59c97189 | 180 | } |
181 | ||
59c97189 | 182 | void |
183 | auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) | |
184 | { | |
185 | void (*authlog) (const char *fmt,...) = verbose; | |
186 | char *authmsg; | |
187 | ||
188 | /* Raise logging level */ | |
189 | if (authenticated == 1 || | |
190 | !authctxt->valid || | |
191 | authctxt->failures >= AUTH_FAIL_LOG || | |
192 | strcmp(method, "password") == 0) | |
193 | authlog = log; | |
194 | ||
195 | if (authctxt->postponed) | |
196 | authmsg = "Postponed"; | |
197 | else | |
198 | authmsg = authenticated ? "Accepted" : "Failed"; | |
199 | ||
200 | authlog("%s %s for %s%.100s from %.200s port %d%s", | |
201 | authmsg, | |
202 | method, | |
203 | authctxt->valid ? "" : "illegal user ", | |
204 | authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user, | |
205 | get_remote_ipaddr(), | |
206 | get_remote_port(), | |
207 | info); | |
208 | } | |
209 | ||
210 | /* | |
15853e93 | 211 | * Check whether root logins are disallowed. |
59c97189 | 212 | */ |
213 | int | |
15853e93 | 214 | auth_root_allowed(char *method) |
59c97189 | 215 | { |
15853e93 | 216 | switch (options.permit_root_login) { |
217 | case PERMIT_YES: | |
59c97189 | 218 | return 1; |
15853e93 | 219 | break; |
220 | case PERMIT_NO_PASSWD: | |
221 | if (strcmp(method, "password") != 0) | |
222 | return 1; | |
223 | break; | |
224 | case PERMIT_FORCED_ONLY: | |
225 | if (forced_command) { | |
226 | log("Root login accepted for forced command."); | |
227 | return 1; | |
228 | } | |
229 | break; | |
59c97189 | 230 | } |
15853e93 | 231 | log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); |
232 | return 0; | |
59c97189 | 233 | } |