blob: e4f867541ef562bed4c67353517285c6cc1683f7 [file] [log] [blame]
Damien Millerb38eff82000-04-01 11:09:21 +10001/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +00002 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +11003 *
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.
Damien Millerb38eff82000-04-01 11:09:21 +100023 */
24
25#include "includes.h"
Ben Lindstrom60567ff2001-06-05 20:27:53 +000026RCSID("$OpenBSD: auth.c,v 1.23 2001/05/24 11:12:42 markus Exp $");
Damien Millerb38eff82000-04-01 11:09:21 +100027
Damien Millerd2c208a2000-05-17 22:00:02 +100028#ifdef HAVE_LOGIN_H
29#include <login.h>
30#endif
Damien Miller1f335fb2000-06-26 11:31:33 +100031#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
32#include <shadow.h>
33#endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
Damien Millerb38eff82000-04-01 11:09:21 +100034
Ben Lindstrom68c3ce12001-06-10 17:24:51 +000035#ifdef HAVE_LIBGEN_H
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +000036#include <libgen.h>
Ben Lindstrom68c3ce12001-06-10 17:24:51 +000037#endif
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +000038
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "xmalloc.h"
40#include "match.h"
41#include "groupaccess.h"
42#include "log.h"
43#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100044#include "auth.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000045#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "canohost.h"
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +000047#include "buffer.h"
48#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100049
Damien Millerb38eff82000-04-01 11:09:21 +100050/* import */
51extern ServerOptions options;
Damien Millerb38eff82000-04-01 11:09:21 +100052
53/*
Kevin Steves7b61cfa2001-01-14 19:11:00 +000054 * Check if the user is allowed to log in via ssh. If user is listed
55 * in DenyUsers or one of user's groups is listed in DenyGroups, false
56 * will be returned. If AllowUsers isn't empty and user isn't listed
57 * there, or if AllowGroups isn't empty and one of user's groups isn't
58 * listed there, false will be returned.
Damien Millerb38eff82000-04-01 11:09:21 +100059 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100060 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100061 */
Damien Millereba71ba2000-04-29 23:57:08 +100062int
Damien Millerb38eff82000-04-01 11:09:21 +100063allowed_user(struct passwd * pw)
64{
65 struct stat st;
Damien Millere7cf07c2001-03-20 09:15:57 +110066 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100067 int i;
68#ifdef WITH_AIXAUTHENTICATE
69 char *loginmsg;
70#endif /* WITH_AIXAUTHENTICATE */
Kevin Stevesa58e0af2000-10-29 14:38:55 +000071#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
Damien Miller62dd94b2000-09-23 14:26:32 +110072 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Kevin Steves7b61cfa2001-01-14 19:11:00 +000073 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100074
75 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000076 if (!pw || !pw->pw_name)
Damien Millerb38eff82000-04-01 11:09:21 +100077 return 0;
78
Damien Miller1f335fb2000-06-26 11:31:33 +100079 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100080 if (spw != NULL) {
81 int days = time(NULL) / 86400;
Damien Miller1f335fb2000-06-26 11:31:33 +100082
Damien Millerc7088432000-07-02 18:44:54 +100083 /* Check account expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +110084 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
Damien Millerc7088432000-07-02 18:44:54 +100085 return 0;
86
87 /* Check password expiry */
Kevin Stevesef4eea92001-02-05 12:42:17 +000088 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
Damien Miller606f8802000-09-16 15:39:56 +110089 (days > (spw->sp_lstchg + spw->sp_max)))
Damien Millerc7088432000-07-02 18:44:54 +100090 return 0;
91 }
Damien Miller1f335fb2000-06-26 11:31:33 +100092#else
93 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000094 if (!pw || !pw->pw_name)
Damien Miller1f335fb2000-06-26 11:31:33 +100095 return 0;
96#endif
97
Damien Milleref7df542000-05-19 00:03:23 +100098 /*
99 * Get the shell from the password data. An empty shell field is
100 * legal, and means /bin/sh.
101 */
102 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
103
104 /* deny if shell does not exists or is not executable */
105 if (stat(shell, &st) != 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000106 return 0;
107 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
108 return 0;
109
110 /* Return false if user is listed in DenyUsers */
111 if (options.num_deny_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000112 for (i = 0; i < options.num_deny_users; i++)
113 if (match_pattern(pw->pw_name, options.deny_users[i]))
114 return 0;
115 }
116 /* Return false if AllowUsers isn't empty and user isn't listed there */
117 if (options.num_allow_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000118 for (i = 0; i < options.num_allow_users; i++)
119 if (match_pattern(pw->pw_name, options.allow_users[i]))
120 break;
121 /* i < options.num_allow_users iff we break for loop */
122 if (i >= options.num_allow_users)
123 return 0;
124 }
Damien Millerb38eff82000-04-01 11:09:21 +1000125 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000126 /* Get the user's group access list (primary and supplementary) */
127 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000128 return 0;
129
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000130 /* Return false if one of user's groups is listed in DenyGroups */
131 if (options.num_deny_groups > 0)
132 if (ga_match(options.deny_groups,
133 options.num_deny_groups)) {
134 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000135 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000136 }
Damien Millerb38eff82000-04-01 11:09:21 +1000137 /*
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000138 * Return false if AllowGroups isn't empty and one of user's groups
Damien Millerb38eff82000-04-01 11:09:21 +1000139 * isn't listed there
140 */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000141 if (options.num_allow_groups > 0)
142 if (!ga_match(options.allow_groups,
143 options.num_allow_groups)) {
144 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000145 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000146 }
147 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000148 }
149
150#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000151 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000152 if (loginmsg && *loginmsg) {
153 /* Remove embedded newlines (if any) */
154 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000155 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000156 if (*p == '\n')
157 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000158 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000159 /* Remove trailing newline */
160 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000161 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000162 }
Damien Millerb38eff82000-04-01 11:09:21 +1000163 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000164 }
Damien Millerb38eff82000-04-01 11:09:21 +1000165#endif /* WITH_AIXAUTHENTICATE */
166
167 /* We found no reason not to let this user try to log on... */
168 return 1;
169}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000170
171Authctxt *
172authctxt_new(void)
173{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000174 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
175 memset(authctxt, 0, sizeof(*authctxt));
176 return authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000177}
178
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000179void
180auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
181{
182 void (*authlog) (const char *fmt,...) = verbose;
183 char *authmsg;
184
185 /* Raise logging level */
186 if (authenticated == 1 ||
187 !authctxt->valid ||
188 authctxt->failures >= AUTH_FAIL_LOG ||
189 strcmp(method, "password") == 0)
190 authlog = log;
191
192 if (authctxt->postponed)
193 authmsg = "Postponed";
194 else
195 authmsg = authenticated ? "Accepted" : "Failed";
196
197 authlog("%s %s for %s%.100s from %.200s port %d%s",
198 authmsg,
199 method,
200 authctxt->valid ? "" : "illegal user ",
201 authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
202 get_remote_ipaddr(),
203 get_remote_port(),
204 info);
205}
206
207/*
Ben Lindstromd8a90212001-02-15 03:08:27 +0000208 * Check whether root logins are disallowed.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000209 */
210int
Ben Lindstromd8a90212001-02-15 03:08:27 +0000211auth_root_allowed(char *method)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000212{
Ben Lindstromd8a90212001-02-15 03:08:27 +0000213 switch (options.permit_root_login) {
214 case PERMIT_YES:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000215 return 1;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000216 break;
217 case PERMIT_NO_PASSWD:
218 if (strcmp(method, "password") != 0)
219 return 1;
220 break;
221 case PERMIT_FORCED_ONLY:
222 if (forced_command) {
223 log("Root login accepted for forced command.");
224 return 1;
225 }
226 break;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000227 }
Ben Lindstromd8a90212001-02-15 03:08:27 +0000228 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
229 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000230}
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000231
232
233/*
234 * Given a template and a passwd structure, build a filename
235 * by substituting % tokenised options. Currently, %% becomes '%',
236 * %h becomes the home directory and %u the username.
237 *
238 * This returns a buffer allocated by xmalloc.
239 */
240char *
241expand_filename(const char *filename, struct passwd *pw)
242{
243 Buffer buffer;
244 char *file;
245 const char *cp;
246
247 /*
248 * Build the filename string in the buffer by making the appropriate
249 * substitutions to the given file name.
250 */
251 buffer_init(&buffer);
252 for (cp = filename; *cp; cp++) {
253 if (cp[0] == '%' && cp[1] == '%') {
254 buffer_append(&buffer, "%", 1);
255 cp++;
256 continue;
257 }
258 if (cp[0] == '%' && cp[1] == 'h') {
259 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
260 cp++;
261 continue;
262 }
263 if (cp[0] == '%' && cp[1] == 'u') {
264 buffer_append(&buffer, pw->pw_name,
265 strlen(pw->pw_name));
266 cp++;
267 continue;
268 }
269 buffer_append(&buffer, cp, 1);
270 }
271 buffer_append(&buffer, "\0", 1);
272
273 /*
274 * Ensure that filename starts anchored. If not, be backward
275 * compatible and prepend the '%h/'
276 */
277 file = xmalloc(MAXPATHLEN);
278 cp = buffer_ptr(&buffer);
279 if (*cp != '/')
280 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
281 else
282 strlcpy(file, cp, MAXPATHLEN);
283
284 buffer_free(&buffer);
285 return file;
286}
287
288char *
289authorized_keys_file(struct passwd *pw)
290{
291 return expand_filename(options.authorized_keys_file, pw);
292}
293
294char *
295authorized_keys_file2(struct passwd *pw)
296{
297 return expand_filename(options.authorized_keys_file2, pw);
298}
299
300/*
301 * Check a given file for security. This is defined as all components
302 * of the path to the file must either be owned by either the owner of
Ben Lindstrom60567ff2001-06-05 20:27:53 +0000303 * of the file or root and no directories must be group or world writable.
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000304 *
305 * XXX Should any specific check be done for sym links ?
306 *
307 * Takes an open file descriptor, the file name, a uid and and
308 * error buffer plus max size as arguments.
309 *
310 * Returns 0 on success and -1 on failure
311 */
312int
313secure_filename(FILE *f, const char *file, uid_t uid, char *err, size_t errlen)
314{
315 char buf[MAXPATHLEN];
316 char *cp;
317 struct stat st;
318
319 if (realpath(file, buf) == NULL) {
320 snprintf(err, errlen, "realpath %s failed: %s", file,
321 strerror(errno));
322 return -1;
323 }
324
325 /* check the open file to avoid races */
326 if (fstat(fileno(f), &st) < 0 ||
327 (st.st_uid != 0 && st.st_uid != uid) ||
328 (st.st_mode & 022) != 0) {
329 snprintf(err, errlen, "bad ownership or modes for file %s",
330 buf);
331 return -1;
332 }
333
334 /* for each component of the canonical path, walking upwards */
335 for (;;) {
336 if ((cp = dirname(buf)) == NULL) {
337 snprintf(err, errlen, "dirname() failed");
338 return -1;
339 }
340 strlcpy(buf, cp, sizeof(buf));
341
342 debug3("secure_filename: checking '%s'", buf);
343 if (stat(buf, &st) < 0 ||
344 (st.st_uid != 0 && st.st_uid != uid) ||
345 (st.st_mode & 022) != 0) {
346 snprintf(err, errlen,
347 "bad ownership or modes for directory %s", buf);
348 return -1;
349 }
350
351 /*
352 * dirname should always complete with a "/" path,
353 * but we can be paranoid and check for "." too
354 */
355 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
356 break;
357 }
358 return 0;
359}