blob: 066b50d6ba1dcc132182cd8d57c166988242ffab [file] [log] [blame]
Damien Miller116e6df2002-05-22 15:06:28 +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"
Damien Miller13e35a02002-05-22 14:04:11 +100026RCSID("$OpenBSD: auth.c,v 1.43 2002/05/17 14:27:55 millert 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"
Ben Lindstrom83647ce2001-06-25 04:30:16 +000049#include "uidswap.h"
50#include "tildexpand.h"
Ben Lindstrom7ebb6352002-03-22 03:04:08 +000051#include "misc.h"
Ben Lindstroma574cda2002-05-15 16:16:14 +000052#include "bufaux.h"
53#include "packet.h"
Damien Millerefb4afe2000-04-12 18:45:05 +100054
Damien Millerb38eff82000-04-01 11:09:21 +100055/* import */
56extern ServerOptions options;
Damien Millerb38eff82000-04-01 11:09:21 +100057
Ben Lindstroma574cda2002-05-15 16:16:14 +000058/* Debugging messages */
59Buffer auth_debug;
60int auth_debug_init;
61
Damien Millerb38eff82000-04-01 11:09:21 +100062/*
Kevin Steves7b61cfa2001-01-14 19:11:00 +000063 * Check if the user is allowed to log in via ssh. If user is listed
64 * in DenyUsers or one of user's groups is listed in DenyGroups, false
65 * will be returned. If AllowUsers isn't empty and user isn't listed
66 * there, or if AllowGroups isn't empty and one of user's groups isn't
67 * listed there, false will be returned.
Damien Millerb38eff82000-04-01 11:09:21 +100068 * If the user's shell is not executable, false will be returned.
Damien Miller4af51302000-04-16 11:18:38 +100069 * Otherwise true is returned.
Damien Millerb38eff82000-04-01 11:09:21 +100070 */
Damien Millereba71ba2000-04-29 23:57:08 +100071int
Damien Millerb38eff82000-04-01 11:09:21 +100072allowed_user(struct passwd * pw)
73{
74 struct stat st;
Ben Lindstrom3fb5d002002-03-05 01:42:42 +000075 const char *hostname = NULL, *ipaddr = NULL;
Damien Millere7cf07c2001-03-20 09:15:57 +110076 char *shell;
Damien Millerb38eff82000-04-01 11:09:21 +100077 int i;
78#ifdef WITH_AIXAUTHENTICATE
79 char *loginmsg;
80#endif /* WITH_AIXAUTHENTICATE */
Kevin Stevesa58e0af2000-10-29 14:38:55 +000081#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
Damien Miller62dd94b2000-09-23 14:26:32 +110082 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
Kevin Steves7b61cfa2001-01-14 19:11:00 +000083 struct spwd *spw;
Damien Millerb38eff82000-04-01 11:09:21 +100084
85 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +000086 if (!pw || !pw->pw_name)
Damien Millerb38eff82000-04-01 11:09:21 +100087 return 0;
88
Kevin Stevesf98fb722002-05-10 15:48:52 +000089#define DAY (24L * 60 * 60) /* 1 day in seconds */
Damien Miller1f335fb2000-06-26 11:31:33 +100090 spw = getspnam(pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +100091 if (spw != NULL) {
Kevin Stevesf98fb722002-05-10 15:48:52 +000092 time_t today = time(NULL) / DAY;
93 debug3("allowed_user: today %d sp_expire %d sp_lstchg %d"
94 " sp_max %d", (int)today, (int)spw->sp_expire,
95 (int)spw->sp_lstchg, (int)spw->sp_max);
Damien Miller1f335fb2000-06-26 11:31:33 +100096
Kevin Stevesf98fb722002-05-10 15:48:52 +000097 /*
98 * We assume account and password expiration occurs the
99 * day after the day specified.
100 */
101 if (spw->sp_expire != -1 && today > spw->sp_expire) {
102 log("Account %.100s has expired", pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +1000103 return 0;
Kevin Stevesf98fb722002-05-10 15:48:52 +0000104 }
Damien Millerc7088432000-07-02 18:44:54 +1000105
Kevin Stevesf98fb722002-05-10 15:48:52 +0000106 if (spw->sp_lstchg == 0) {
107 log("User %.100s password has expired (root forced)",
108 pw->pw_name);
Damien Millerc7088432000-07-02 18:44:54 +1000109 return 0;
Kevin Stevesf98fb722002-05-10 15:48:52 +0000110 }
111
112 if (spw->sp_max != -1 &&
113 today > spw->sp_lstchg + spw->sp_max) {
114 log("User %.100s password has expired (password aged)",
115 pw->pw_name);
116 return 0;
117 }
Damien Millerc7088432000-07-02 18:44:54 +1000118 }
Damien Miller1f335fb2000-06-26 11:31:33 +1000119#else
120 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000121 if (!pw || !pw->pw_name)
Damien Miller1f335fb2000-06-26 11:31:33 +1000122 return 0;
123#endif
124
Damien Milleref7df542000-05-19 00:03:23 +1000125 /*
126 * Get the shell from the password data. An empty shell field is
127 * legal, and means /bin/sh.
128 */
129 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
130
131 /* deny if shell does not exists or is not executable */
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000132 if (stat(shell, &st) != 0) {
133 log("User %.100s not allowed because shell %.100s does not exist",
134 pw->pw_name, shell);
Damien Millerb38eff82000-04-01 11:09:21 +1000135 return 0;
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000136 }
Ben Lindstromb61e6df2002-03-22 01:15:33 +0000137 if (S_ISREG(st.st_mode) == 0 ||
138 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000139 log("User %.100s not allowed because shell %.100s is not executable",
140 pw->pw_name, shell);
Damien Millerb38eff82000-04-01 11:09:21 +1000141 return 0;
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000142 }
Damien Millerb38eff82000-04-01 11:09:21 +1000143
Ben Lindstrom3fb5d002002-03-05 01:42:42 +0000144 if (options.num_deny_users > 0 || options.num_allow_users > 0) {
145 hostname = get_canonical_hostname(options.verify_reverse_mapping);
146 ipaddr = get_remote_ipaddr();
147 }
148
Damien Millerb38eff82000-04-01 11:09:21 +1000149 /* Return false if user is listed in DenyUsers */
150 if (options.num_deny_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000151 for (i = 0; i < options.num_deny_users; i++)
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000152 if (match_user(pw->pw_name, hostname, ipaddr,
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000153 options.deny_users[i])) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000154 log("User %.100s not allowed because listed in DenyUsers",
155 pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000156 return 0;
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000157 }
Damien Millerb38eff82000-04-01 11:09:21 +1000158 }
159 /* Return false if AllowUsers isn't empty and user isn't listed there */
160 if (options.num_allow_users > 0) {
Damien Millerb38eff82000-04-01 11:09:21 +1000161 for (i = 0; i < options.num_allow_users; i++)
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000162 if (match_user(pw->pw_name, hostname, ipaddr,
Ben Lindstrom60260022001-07-04 04:56:44 +0000163 options.allow_users[i]))
Damien Millerb38eff82000-04-01 11:09:21 +1000164 break;
165 /* i < options.num_allow_users iff we break for loop */
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000166 if (i >= options.num_allow_users) {
167 log("User %.100s not allowed because not listed in AllowUsers",
168 pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000169 return 0;
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000170 }
Damien Millerb38eff82000-04-01 11:09:21 +1000171 }
Damien Millerb38eff82000-04-01 11:09:21 +1000172 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000173 /* Get the user's group access list (primary and supplementary) */
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000174 if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
175 log("User %.100s not allowed because not in any group",
176 pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000177 return 0;
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000178 }
Damien Millerb38eff82000-04-01 11:09:21 +1000179
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000180 /* Return false if one of user's groups is listed in DenyGroups */
181 if (options.num_deny_groups > 0)
182 if (ga_match(options.deny_groups,
183 options.num_deny_groups)) {
184 ga_free();
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000185 log("User %.100s not allowed because a group is listed in DenyGroups",
186 pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000187 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000188 }
Damien Millerb38eff82000-04-01 11:09:21 +1000189 /*
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000190 * Return false if AllowGroups isn't empty and one of user's groups
Damien Millerb38eff82000-04-01 11:09:21 +1000191 * isn't listed there
192 */
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000193 if (options.num_allow_groups > 0)
194 if (!ga_match(options.allow_groups,
195 options.num_allow_groups)) {
196 ga_free();
Ben Lindstrom6ef9ec62002-03-05 01:40:37 +0000197 log("User %.100s not allowed because none of user's groups are listed in AllowGroups",
198 pw->pw_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000199 return 0;
Kevin Steves7b61cfa2001-01-14 19:11:00 +0000200 }
201 ga_free();
Damien Millerb38eff82000-04-01 11:09:21 +1000202 }
203
204#ifdef WITH_AIXAUTHENTICATE
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000205 if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000206 if (loginmsg && *loginmsg) {
207 /* Remove embedded newlines (if any) */
208 char *p;
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000209 for (p = loginmsg; *p; p++) {
Damien Millerd2c208a2000-05-17 22:00:02 +1000210 if (*p == '\n')
211 *p = ' ';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000212 }
Damien Millerd2c208a2000-05-17 22:00:02 +1000213 /* Remove trailing newline */
214 *--p = '\0';
Damien Miller2f6a0ad2000-05-31 11:20:11 +1000215 log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
Damien Millerd2c208a2000-05-17 22:00:02 +1000216 }
Damien Millerb38eff82000-04-01 11:09:21 +1000217 return 0;
Damien Millerd2c208a2000-05-17 22:00:02 +1000218 }
Damien Millerb38eff82000-04-01 11:09:21 +1000219#endif /* WITH_AIXAUTHENTICATE */
220
221 /* We found no reason not to let this user try to log on... */
222 return 1;
223}
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000224
225Authctxt *
226authctxt_new(void)
227{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000228 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
229 memset(authctxt, 0, sizeof(*authctxt));
230 return authctxt;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000231}
232
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000233void
234auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
235{
236 void (*authlog) (const char *fmt,...) = verbose;
237 char *authmsg;
238
239 /* Raise logging level */
240 if (authenticated == 1 ||
241 !authctxt->valid ||
242 authctxt->failures >= AUTH_FAIL_LOG ||
243 strcmp(method, "password") == 0)
244 authlog = log;
245
246 if (authctxt->postponed)
247 authmsg = "Postponed";
248 else
249 authmsg = authenticated ? "Accepted" : "Failed";
250
251 authlog("%s %s for %s%.100s from %.200s port %d%s",
252 authmsg,
253 method,
254 authctxt->valid ? "" : "illegal user ",
Damien Millerf6552072001-11-12 11:06:06 +1100255 authctxt->user,
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000256 get_remote_ipaddr(),
257 get_remote_port(),
258 info);
259}
260
261/*
Ben Lindstromd8a90212001-02-15 03:08:27 +0000262 * Check whether root logins are disallowed.
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000263 */
264int
Ben Lindstromd8a90212001-02-15 03:08:27 +0000265auth_root_allowed(char *method)
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000266{
Ben Lindstromd8a90212001-02-15 03:08:27 +0000267 switch (options.permit_root_login) {
268 case PERMIT_YES:
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000269 return 1;
Ben Lindstromd8a90212001-02-15 03:08:27 +0000270 break;
271 case PERMIT_NO_PASSWD:
272 if (strcmp(method, "password") != 0)
273 return 1;
274 break;
275 case PERMIT_FORCED_ONLY:
276 if (forced_command) {
277 log("Root login accepted for forced command.");
278 return 1;
279 }
280 break;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000281 }
Ben Lindstromd8a90212001-02-15 03:08:27 +0000282 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
283 return 0;
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000284}
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000285
286
287/*
288 * Given a template and a passwd structure, build a filename
289 * by substituting % tokenised options. Currently, %% becomes '%',
290 * %h becomes the home directory and %u the username.
291 *
292 * This returns a buffer allocated by xmalloc.
293 */
294char *
295expand_filename(const char *filename, struct passwd *pw)
296{
297 Buffer buffer;
298 char *file;
299 const char *cp;
300
301 /*
302 * Build the filename string in the buffer by making the appropriate
303 * substitutions to the given file name.
304 */
305 buffer_init(&buffer);
306 for (cp = filename; *cp; cp++) {
307 if (cp[0] == '%' && cp[1] == '%') {
308 buffer_append(&buffer, "%", 1);
309 cp++;
310 continue;
311 }
312 if (cp[0] == '%' && cp[1] == 'h') {
313 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
314 cp++;
315 continue;
316 }
317 if (cp[0] == '%' && cp[1] == 'u') {
318 buffer_append(&buffer, pw->pw_name,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100319 strlen(pw->pw_name));
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000320 cp++;
321 continue;
322 }
323 buffer_append(&buffer, cp, 1);
324 }
325 buffer_append(&buffer, "\0", 1);
326
327 /*
328 * Ensure that filename starts anchored. If not, be backward
329 * compatible and prepend the '%h/'
330 */
331 file = xmalloc(MAXPATHLEN);
332 cp = buffer_ptr(&buffer);
333 if (*cp != '/')
334 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
335 else
336 strlcpy(file, cp, MAXPATHLEN);
337
338 buffer_free(&buffer);
339 return file;
340}
341
342char *
343authorized_keys_file(struct passwd *pw)
344{
345 return expand_filename(options.authorized_keys_file, pw);
346}
347
348char *
349authorized_keys_file2(struct passwd *pw)
350{
351 return expand_filename(options.authorized_keys_file2, pw);
352}
353
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000354/* return ok if key exists in sysfile or userfile */
355HostStatus
356check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
357 const char *sysfile, const char *userfile)
358{
359 Key *found;
360 char *user_hostfile;
361 struct stat st;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000362 HostStatus host_status;
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000363
364 /* Check if we know the host and its host key. */
365 found = key_new(key->type);
366 host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
367
368 if (host_status != HOST_OK && userfile != NULL) {
369 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
370 if (options.strict_modes &&
371 (stat(user_hostfile, &st) == 0) &&
372 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
Damien Miller9f0f5c62001-12-21 14:45:46 +1100373 (st.st_mode & 022) != 0)) {
Ben Lindstrom83647ce2001-06-25 04:30:16 +0000374 log("Authentication refused for %.100s: "
375 "bad owner or modes for %.200s",
376 pw->pw_name, user_hostfile);
377 } else {
378 temporarily_use_uid(pw);
379 host_status = check_host_in_hostfile(user_hostfile,
380 host, key, found, NULL);
381 restore_uid();
382 }
383 xfree(user_hostfile);
384 }
385 key_free(found);
386
387 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
388 "ok" : "not found", host);
389 return host_status;
390}
391
392
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000393/*
394 * Check a given file for security. This is defined as all components
395 * of the path to the file must either be owned by either the owner of
Ben Lindstrom60567ff2001-06-05 20:27:53 +0000396 * of the file or root and no directories must be group or world writable.
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000397 *
398 * XXX Should any specific check be done for sym links ?
399 *
400 * Takes an open file descriptor, the file name, a uid and and
401 * error buffer plus max size as arguments.
402 *
403 * Returns 0 on success and -1 on failure
404 */
405int
Ben Lindstrom248c0782001-07-04 03:40:39 +0000406secure_filename(FILE *f, const char *file, struct passwd *pw,
407 char *err, size_t errlen)
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000408{
Ben Lindstrom248c0782001-07-04 03:40:39 +0000409 uid_t uid = pw->pw_uid;
Ben Lindstromc3e49e72001-10-03 17:55:26 +0000410 char buf[MAXPATHLEN], homedir[MAXPATHLEN];
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000411 char *cp;
412 struct stat st;
413
414 if (realpath(file, buf) == NULL) {
415 snprintf(err, errlen, "realpath %s failed: %s", file,
416 strerror(errno));
417 return -1;
418 }
Ben Lindstromc3e49e72001-10-03 17:55:26 +0000419 if (realpath(pw->pw_dir, homedir) == NULL) {
420 snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir,
421 strerror(errno));
422 return -1;
423 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000424
425 /* check the open file to avoid races */
426 if (fstat(fileno(f), &st) < 0 ||
427 (st.st_uid != 0 && st.st_uid != uid) ||
428 (st.st_mode & 022) != 0) {
429 snprintf(err, errlen, "bad ownership or modes for file %s",
430 buf);
431 return -1;
432 }
433
434 /* for each component of the canonical path, walking upwards */
435 for (;;) {
436 if ((cp = dirname(buf)) == NULL) {
437 snprintf(err, errlen, "dirname() failed");
438 return -1;
439 }
440 strlcpy(buf, cp, sizeof(buf));
441
442 debug3("secure_filename: checking '%s'", buf);
443 if (stat(buf, &st) < 0 ||
444 (st.st_uid != 0 && st.st_uid != uid) ||
445 (st.st_mode & 022) != 0) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100446 snprintf(err, errlen,
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000447 "bad ownership or modes for directory %s", buf);
448 return -1;
449 }
450
Damien Miller0ae6e002001-07-14 12:21:34 +1000451 /* If are passed the homedir then we can stop */
Ben Lindstromc3e49e72001-10-03 17:55:26 +0000452 if (strcmp(homedir, buf) == 0) {
Damien Miller0ae6e002001-07-14 12:21:34 +1000453 debug3("secure_filename: terminating check at '%s'",
454 buf);
455 break;
456 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000457 /*
458 * dirname should always complete with a "/" path,
459 * but we can be paranoid and check for "." too
460 */
461 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
462 break;
463 }
464 return 0;
465}
Ben Lindstrom2ae18f42002-03-22 01:24:38 +0000466
467struct passwd *
468getpwnamallow(const char *user)
469{
Ben Lindstromb481e132002-03-22 01:35:47 +0000470#ifdef HAVE_LOGIN_CAP
471 extern login_cap_t *lc;
472#ifdef BSD_AUTH
473 auth_session_t *as;
474#endif
475#endif
Ben Lindstrom2ae18f42002-03-22 01:24:38 +0000476 struct passwd *pw;
477
478 pw = getpwnam(user);
Ben Lindstromb481e132002-03-22 01:35:47 +0000479 if (pw == NULL || !allowed_user(pw))
480 return (NULL);
481#ifdef HAVE_LOGIN_CAP
482 if ((lc = login_getclass(pw->pw_class)) == NULL) {
483 debug("unable to get login class: %s", user);
484 return (NULL);
485 }
486#ifdef BSD_AUTH
487 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
Damien Miller13e35a02002-05-22 14:04:11 +1000488 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
Ben Lindstromb481e132002-03-22 01:35:47 +0000489 debug("Approval failure for %s", user);
Ben Lindstrom2ae18f42002-03-22 01:24:38 +0000490 pw = NULL;
Ben Lindstromb481e132002-03-22 01:35:47 +0000491 }
492 if (as != NULL)
493 auth_close(as);
494#endif
495#endif
Ben Lindstromf34e4eb2002-03-22 03:08:30 +0000496 if (pw != NULL)
497 return (pwcopy(pw));
498 return (NULL);
Ben Lindstrom2ae18f42002-03-22 01:24:38 +0000499}
Ben Lindstroma574cda2002-05-15 16:16:14 +0000500
501void
502auth_debug_add(const char *fmt,...)
503{
504 char buf[1024];
505 va_list args;
506
507 if (!auth_debug_init)
508 return;
509
510 va_start(args, fmt);
511 vsnprintf(buf, sizeof(buf), fmt, args);
512 va_end(args);
513 buffer_put_cstring(&auth_debug, buf);
514}
515
516void
517auth_debug_send(void)
518{
519 char *msg;
520
521 if (!auth_debug_init)
522 return;
523 while (buffer_len(&auth_debug)) {
524 msg = buffer_get_string(&auth_debug, NULL);
525 packet_send_debug("%s", msg);
526 xfree(msg);
527 }
528}
529
530void
531auth_debug_reset(void)
532{
533 if (auth_debug_init)
534 buffer_clear(&auth_debug);
535 else {
536 buffer_init(&auth_debug);
537 auth_debug_init = 1;
538 }
539}