]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
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. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
26 | RCSID("$OpenBSD: auth.c,v 1.19 2001/03/02 18:54:31 deraadt Exp $"); | |
27 | ||
28 | #ifdef HAVE_LOGIN_H | |
29 | #include <login.h> | |
30 | #endif | |
31 | #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) | |
32 | #include <shadow.h> | |
33 | #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */ | |
34 | ||
35 | #include "xmalloc.h" | |
36 | #include "match.h" | |
37 | #include "groupaccess.h" | |
38 | #include "log.h" | |
39 | #include "servconf.h" | |
40 | #include "auth.h" | |
41 | #include "auth-options.h" | |
42 | #include "canohost.h" | |
43 | ||
44 | /* import */ | |
45 | extern ServerOptions options; | |
46 | ||
47 | /* | |
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. | |
55 | */ | |
56 | int | |
57 | allowed_user(struct passwd * pw) | |
58 | { | |
59 | struct stat st; | |
60 | char *shell; | |
61 | int i; | |
62 | #ifdef WITH_AIXAUTHENTICATE | |
63 | char *loginmsg; | |
64 | #endif /* WITH_AIXAUTHENTICATE */ | |
65 | #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \ | |
66 | !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE) | |
67 | struct spwd *spw; | |
68 | ||
69 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ | |
70 | if (!pw || !pw->pw_name) | |
71 | return 0; | |
72 | ||
73 | spw = getspnam(pw->pw_name); | |
74 | if (spw != NULL) { | |
75 | int days = time(NULL) / 86400; | |
76 | ||
77 | /* Check account expiry */ | |
78 | if ((spw->sp_expire >= 0) && (days > spw->sp_expire)) | |
79 | return 0; | |
80 | ||
81 | /* Check password expiry */ | |
82 | if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) && | |
83 | (days > (spw->sp_lstchg + spw->sp_max))) | |
84 | return 0; | |
85 | } | |
86 | #else | |
87 | /* Shouldn't be called if pw is NULL, but better safe than sorry... */ | |
88 | if (!pw || !pw->pw_name) | |
89 | return 0; | |
90 | #endif | |
91 | ||
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 | ||
98 | /* deny if shell does not exists or is not executable */ | |
99 | if (stat(shell, &st) != 0) | |
100 | return 0; | |
101 | if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)))) | |
102 | return 0; | |
103 | ||
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])) | |
108 | return 0; | |
109 | } | |
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])) | |
114 | break; | |
115 | /* i < options.num_allow_users iff we break for loop */ | |
116 | if (i >= options.num_allow_users) | |
117 | return 0; | |
118 | } | |
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) | |
122 | return 0; | |
123 | ||
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)) { | |
128 | ga_free(); | |
129 | return 0; | |
130 | } | |
131 | /* | |
132 | * Return false if AllowGroups isn't empty and one of user's groups | |
133 | * isn't listed there | |
134 | */ | |
135 | if (options.num_allow_groups > 0) | |
136 | if (!ga_match(options.allow_groups, | |
137 | options.num_allow_groups)) { | |
138 | ga_free(); | |
139 | return 0; | |
140 | } | |
141 | ga_free(); | |
142 | } | |
143 | ||
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) */ | |
148 | char *p; | |
149 | for (p = loginmsg; *p; p++) { | |
150 | if (*p == '\n') | |
151 | *p = ' '; | |
152 | } | |
153 | /* Remove trailing newline */ | |
154 | *--p = '\0'; | |
155 | log("Login restricted for %s: %.100s", pw->pw_name, loginmsg); | |
156 | } | |
157 | return 0; | |
158 | } | |
159 | #endif /* WITH_AIXAUTHENTICATE */ | |
160 | ||
161 | /* We found no reason not to let this user try to log on... */ | |
162 | return 1; | |
163 | } | |
164 | ||
165 | Authctxt * | |
166 | authctxt_new(void) | |
167 | { | |
168 | Authctxt *authctxt = xmalloc(sizeof(*authctxt)); | |
169 | memset(authctxt, 0, sizeof(*authctxt)); | |
170 | return authctxt; | |
171 | } | |
172 | ||
173 | void | |
174 | auth_log(Authctxt *authctxt, int authenticated, char *method, char *info) | |
175 | { | |
176 | void (*authlog) (const char *fmt,...) = verbose; | |
177 | char *authmsg; | |
178 | ||
179 | /* Raise logging level */ | |
180 | if (authenticated == 1 || | |
181 | !authctxt->valid || | |
182 | authctxt->failures >= AUTH_FAIL_LOG || | |
183 | strcmp(method, "password") == 0) | |
184 | authlog = log; | |
185 | ||
186 | if (authctxt->postponed) | |
187 | authmsg = "Postponed"; | |
188 | else | |
189 | authmsg = authenticated ? "Accepted" : "Failed"; | |
190 | ||
191 | authlog("%s %s for %s%.100s from %.200s port %d%s", | |
192 | authmsg, | |
193 | method, | |
194 | authctxt->valid ? "" : "illegal user ", | |
195 | authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user, | |
196 | get_remote_ipaddr(), | |
197 | get_remote_port(), | |
198 | info); | |
199 | } | |
200 | ||
201 | /* | |
202 | * Check whether root logins are disallowed. | |
203 | */ | |
204 | int | |
205 | auth_root_allowed(char *method) | |
206 | { | |
207 | switch (options.permit_root_login) { | |
208 | case PERMIT_YES: | |
209 | return 1; | |
210 | break; | |
211 | case PERMIT_NO_PASSWD: | |
212 | if (strcmp(method, "password") != 0) | |
213 | return 1; | |
214 | break; | |
215 | case PERMIT_FORCED_ONLY: | |
216 | if (forced_command) { | |
217 | log("Root login accepted for forced command."); | |
218 | return 1; | |
219 | } | |
220 | break; | |
221 | } | |
222 | log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr()); | |
223 | return 0; | |
224 | } |