blob: 07e5b8f05329aaaefc5aa350e17066f784cf746e [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 Lindstrombfb3a0e2001-06-05 20:25:05 +000026RCSID("$OpenBSD: auth.c,v 1.22 2001/05/20 17:20:35 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 Lindstrombfb3a0e2001-06-05 20:25:05 +000035#include <libgen.h>
36
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "xmalloc.h"
38#include "match.h"
39#include "groupaccess.h"
40#include "log.h"
41#include "servconf.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100042#include "auth.h"
Ben Lindstromdb65e8f2001-01-19 04:26:52 +000043#include "auth-options.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "canohost.h"
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +000045#include "buffer.h"
46#include "bufaux.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100047
Damien Millerb38eff82000-04-01 11:09:21 +100048/* import */
49extern ServerOptions options;
Damien Millerb38eff82000-04-01 11:09:21 +100050
51/*
Kevin Steves7b61cfa2001-01-14 19:11:00 +000052 * 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.
Damien Millerb38eff82000-04-01 11:09:21 +100057 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100058 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100059 */
Damien Millereba71ba2000-04-29 23:57:08 +100060int
Damien Millerb38eff82000-04-01 11:09:21 +100061allowed_user(struct passwd * pw)
62{
63 struct stat st;
Damien Millere7cf07c2001-03-20 09:15:57 +110064 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100065 int i;
66#ifdef WITH_AIXAUTHENTICATE
67 char *loginmsg;
68#endif /* WITH_AIXAUTHENTICATE */
Kevin Stevesa58e0af2000-10-29 14:38:55 +000069#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
Damien Miller62dd94b2000-09-23 14:26:32 +110070 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Kevin Steves7b61cfa2001-01-14 19:11:00 +000071 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100072
73 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000074 if (!pw || !pw->pw_name)
Damien Millerb38eff82000-04-01 11:09:21 +100075 return 0;
76
Damien Miller1f335fb2000-06-26 11:31:33 +100077 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100078 if (spw != NULL) {
79 int days = time(NULL) / 86400;
Damien Miller1f335fb2000-06-26 11:31:33 +100080
Damien Millerc7088432000-07-02 18:44:54 +100081 /* Check account expiry */
Damien Miller62dd94b2000-09-23 14:26:32 +110082 if ((spw->sp_expire >= 0) && (days > spw->sp_expire))
Damien Millerc7088432000-07-02 18:44:54 +100083 return 0;
84
85 /* Check password expiry */
Kevin Stevesef4eea92001-02-05 12:42:17 +000086 if ((spw->sp_lstchg >= 0) && (spw->sp_max >= 0) &&
Damien Miller606f8802000-09-16 15:39:56 +110087 (days > (spw->sp_lstchg + spw->sp_max)))
Damien Millerc7088432000-07-02 18:44:54 +100088 return 0;
89 }
Damien Miller1f335fb2000-06-26 11:31:33 +100090#else
91 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000092 if (!pw || !pw->pw_name)
Damien Miller1f335fb2000-06-26 11:31:33 +100093 return 0;
94#endif
95
Damien Milleref7df542000-05-19 00:03:23 +100096 /*
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)
Damien Millerb38eff82000-04-01 11:09:21 +1000104 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) {
Damien Millerb38eff82000-04-01 11:09:21 +1000110 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) {
Damien Millerb38eff82000-04-01 11:09:21 +1000116 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 }
Damien Millerb38eff82000-04-01 11:09:21 +1000123 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000124 /* Get the user's group access list (primary and supplementary) */
125 if (ga_init(pw->pw_name, pw->pw_gid) == 0)
Damien Millerb38eff82000-04-01 11:09:21 +1000126 return 0;
127
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000128 /* 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();
Damien Millerb38eff82000-04-01 11:09:21 +1000133 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000134 }
Damien Millerb38eff82000-04-01 11:09:21 +1000135 /*
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000136 * Return false if AllowGroups isn't empty and one of user's groups
Damien Millerb38eff82000-04-01 11:09:21 +1000137 * isn't listed there
138 */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000139 if (options.num_allow_groups > 0)
140 if (!ga_match(options.allow_groups,
141 options.num_allow_groups)) {
142 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000143 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000144 }
145 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000146 }
147
148#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000149 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000150 if (loginmsg && *loginmsg) {
151 /* Remove embedded newlines (if any) */
152 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000153 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000154 if (*p == '\n')
155 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000156 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000157 /* Remove trailing newline */
158 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000159 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000160 }
Damien Millerb38eff82000-04-01 11:09:21 +1000161 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000162 }
Damien Millerb38eff82000-04-01 11:09:21 +1000163#endif /* WITH_AIXAUTHENTICATE */
164
165 /* We found no reason not to let this user try to log on... */
166 return 1;
167}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000168
169Authctxt *
170authctxt_new(void)
171{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000172 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
173 memset(authctxt, 0, sizeof(*authctxt));
174 return authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000175}
176
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000177void
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/*
Ben Lindstromd8a90212001-02-15 03:08:27 +0000206 * Check whether root logins are disallowed.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000207 */
208int
Ben Lindstromd8a90212001-02-15 03:08:27 +0000209auth_root_allowed(char *method)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000210{
Ben Lindstromd8a90212001-02-15 03:08:27 +0000211 switch (options.permit_root_login) {
212 case PERMIT_YES:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000213 return 1;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000214 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;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000225 }
Ben Lindstromd8a90212001-02-15 03:08:27 +0000226 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
227 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000228}
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000229
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
301 * of the file or root and no directories must be world writable.
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}