]> andersk Git - openssh.git/blame - auth.c
Should have removed this. It's part of nchan.c now.
[openssh.git] / auth.c
CommitLineData
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"
1ddf764b 26RCSID("$OpenBSD: auth.c,v 1.23 2001/05/24 11:12:42 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
c8445989 35#include <libgen.h>
36
42f11eb2 37#include "xmalloc.h"
38#include "match.h"
39#include "groupaccess.h"
40#include "log.h"
41#include "servconf.h"
e78a59f5 42#include "auth.h"
59c97189 43#include "auth-options.h"
42f11eb2 44#include "canohost.h"
c8445989 45#include "buffer.h"
46#include "bufaux.h"
e78a59f5 47
7368a6c8 48/* import */
49extern ServerOptions options;
7368a6c8 50
51/*
c6a69271 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.
7368a6c8 57 * If the user's shell is not executable, false will be returned.
6ae2364d 58 * Otherwise true is returned.
7368a6c8 59 */
a306f2dd 60int
7368a6c8 61allowed_user(struct passwd * pw)
62{
63 struct stat st;
614dee3a 64 char *shell;
7368a6c8 65 int i;
66#ifdef WITH_AIXAUTHENTICATE
67 char *loginmsg;
68#endif /* WITH_AIXAUTHENTICATE */
75b90ced 69#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
37c1c46d 70 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
c6a69271 71 struct spwd *spw;
7368a6c8 72
73 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 74 if (!pw || !pw->pw_name)
7368a6c8 75 return 0;
76
4cb5ffa0 77 spw = getspnam(pw->pw_name);
27494968 78 if (spw != NULL) {
79 int days = time(NULL) / 86400;
4cb5ffa0 80
27494968 81 /* Check account expiry */
37c1c46d 82 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
27494968 83 return 0;
84
85 /* Check password expiry */
2b87da3b 86 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
7f8f5e00 87 (days > (spw->sp_lstchg + spw->sp_max)))
27494968 88 return 0;
89 }
4cb5ffa0 90#else
91 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 92 if (!pw || !pw->pw_name)
4cb5ffa0 93 return 0;
94#endif
95
301e9b01 96 /*
97 * Get the shell from the password data. An empty shell field is
98 * legal, and means /bin/sh.
99 */
100 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
101
102 /* deny if shell does not exists or is not executable */
103 if (stat(shell, &st) != 0)
7368a6c8 104 return 0;
105 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
106 return 0;
107
108 /* Return false if user is listed in DenyUsers */
109 if (options.num_deny_users > 0) {
7368a6c8 110 for (i = 0; i < options.num_deny_users; i++)
111 if (match_pattern(pw->pw_name, options.deny_users[i]))
112 return 0;
113 }
114 /* Return false if AllowUsers isn't empty and user isn't listed there */
115 if (options.num_allow_users > 0) {
7368a6c8 116 for (i = 0; i < options.num_allow_users; i++)
117 if (match_pattern(pw->pw_name, options.allow_users[i]))
118 break;
119 /* i < options.num_allow_users iff we break for loop */
120 if (i >= options.num_allow_users)
121 return 0;
122 }
7368a6c8 123 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
c6a69271 124 /* Get the user's group access list (primary and supplementary) */
125 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
7368a6c8 126 return 0;
127
c6a69271 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)) {
132 ga_free();
7368a6c8 133 return 0;
c6a69271 134 }
7368a6c8 135 /*
c6a69271 136 * Return false if AllowGroups isn't empty and one of user's groups
7368a6c8 137 * isn't listed there
138 */
c6a69271 139 if (options.num_allow_groups > 0)
140 if (!ga_match(options.allow_groups,
141 options.num_allow_groups)) {
142 ga_free();
7368a6c8 143 return 0;
c6a69271 144 }
145 ga_free();
7368a6c8 146 }
147
148#ifdef WITH_AIXAUTHENTICATE
5daf7064 149 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
c1ef8333 150 if (loginmsg && *loginmsg) {
151 /* Remove embedded newlines (if any) */
152 char *p;
5daf7064 153 for (p = loginmsg; *p; p++) {
c1ef8333 154 if (*p == '\n')
155 *p = ' ';
5daf7064 156 }
c1ef8333 157 /* Remove trailing newline */
158 *--p = '\0';
5daf7064 159 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
c1ef8333 160 }
7368a6c8 161 return 0;
c1ef8333 162 }
7368a6c8 163#endif /* WITH_AIXAUTHENTICATE */
164
165 /* We found no reason not to let this user try to log on... */
166 return 1;
167}
59c97189 168
169Authctxt *
170authctxt_new(void)
171{
2b87da3b 172 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
173 memset(authctxt, 0, sizeof(*authctxt));
174 return authctxt;
59c97189 175}
176
59c97189 177void
178auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
179{
180 void (*authlog) (const char *fmt,...) = verbose;
181 char *authmsg;
182
183 /* Raise logging level */
184 if (authenticated == 1 ||
185 !authctxt->valid ||
186 authctxt->failures >= AUTH_FAIL_LOG ||
187 strcmp(method, "password") == 0)
188 authlog = log;
189
190 if (authctxt->postponed)
191 authmsg = "Postponed";
192 else
193 authmsg = authenticated ? "Accepted" : "Failed";
194
195 authlog("%s %s for %s%.100s from %.200s port %d%s",
196 authmsg,
197 method,
198 authctxt->valid ? "" : "illegal user ",
199 authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
200 get_remote_ipaddr(),
201 get_remote_port(),
202 info);
203}
204
205/*
15853e93 206 * Check whether root logins are disallowed.
59c97189 207 */
208int
15853e93 209auth_root_allowed(char *method)
59c97189 210{
15853e93 211 switch (options.permit_root_login) {
212 case PERMIT_YES:
59c97189 213 return 1;
15853e93 214 break;
215 case PERMIT_NO_PASSWD:
216 if (strcmp(method, "password") != 0)
217 return 1;
218 break;
219 case PERMIT_FORCED_ONLY:
220 if (forced_command) {
221 log("Root login accepted for forced command.");
222 return 1;
223 }
224 break;
59c97189 225 }
15853e93 226 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
227 return 0;
59c97189 228}
c8445989 229
230
231/*
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.
235 *
236 * This returns a buffer allocated by xmalloc.
237 */
238char *
239expand_filename(const char *filename, struct passwd *pw)
240{
241 Buffer buffer;
242 char *file;
243 const char *cp;
244
245 /*
246 * Build the filename string in the buffer by making the appropriate
247 * substitutions to the given file name.
248 */
249 buffer_init(&buffer);
250 for (cp = filename; *cp; cp++) {
251 if (cp[0] == '%' && cp[1] == '%') {
252 buffer_append(&buffer, "%", 1);
253 cp++;
254 continue;
255 }
256 if (cp[0] == '%' && cp[1] == 'h') {
257 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
258 cp++;
259 continue;
260 }
261 if (cp[0] == '%' && cp[1] == 'u') {
262 buffer_append(&buffer, pw->pw_name,
263 strlen(pw->pw_name));
264 cp++;
265 continue;
266 }
267 buffer_append(&buffer, cp, 1);
268 }
269 buffer_append(&buffer, "\0", 1);
270
271 /*
272 * Ensure that filename starts anchored. If not, be backward
273 * compatible and prepend the '%h/'
274 */
275 file = xmalloc(MAXPATHLEN);
276 cp = buffer_ptr(&buffer);
277 if (*cp != '/')
278 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
279 else
280 strlcpy(file, cp, MAXPATHLEN);
281
282 buffer_free(&buffer);
283 return file;
284}
285
286char *
287authorized_keys_file(struct passwd *pw)
288{
289 return expand_filename(options.authorized_keys_file, pw);
290}
291
292char *
293authorized_keys_file2(struct passwd *pw)
294{
295 return expand_filename(options.authorized_keys_file2, pw);
296}
297
298/*
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
1ddf764b 301 * of the file or root and no directories must be group or world writable.
c8445989 302 *
303 * XXX Should any specific check be done for sym links ?
304 *
305 * Takes an open file descriptor, the file name, a uid and and
306 * error buffer plus max size as arguments.
307 *
308 * Returns 0 on success and -1 on failure
309 */
310int
311secure_filename(FILE *f, const char *file, uid_t uid, char *err, size_t errlen)
312{
313 char buf[MAXPATHLEN];
314 char *cp;
315 struct stat st;
316
317 if (realpath(file, buf) == NULL) {
318 snprintf(err, errlen, "realpath %s failed: %s", file,
319 strerror(errno));
320 return -1;
321 }
322
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",
328 buf);
329 return -1;
330 }
331
332 /* for each component of the canonical path, walking upwards */
333 for (;;) {
334 if ((cp = dirname(buf)) == NULL) {
335 snprintf(err, errlen, "dirname() failed");
336 return -1;
337 }
338 strlcpy(buf, cp, sizeof(buf));
339
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);
346 return -1;
347 }
348
349 /*
350 * dirname should always complete with a "/" path,
351 * but we can be paranoid and check for "." too
352 */
353 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
354 break;
355 }
356 return 0;
357}
This page took 0.131809 seconds and 5 git commands to generate.