]> andersk Git - openssh.git/blame - auth.c
- (djm) Fix my fix of the fix for the Bug #442 for PAM case. Spotted by
[openssh.git] / auth.c
CommitLineData
20d0f77f 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"
f6f02456 26RCSID("$OpenBSD: auth.c,v 1.46 2002/11/04 10:07:53 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
b4d02860 35#ifdef HAVE_LIBGEN_H
c8445989 36#include <libgen.h>
b4d02860 37#endif
c8445989 38
42f11eb2 39#include "xmalloc.h"
40#include "match.h"
41#include "groupaccess.h"
42#include "log.h"
43#include "servconf.h"
e78a59f5 44#include "auth.h"
59c97189 45#include "auth-options.h"
42f11eb2 46#include "canohost.h"
c8445989 47#include "buffer.h"
48#include "bufaux.h"
d0c8ca5c 49#include "uidswap.h"
50#include "tildexpand.h"
5f1f36b5 51#include "misc.h"
01dafcb5 52#include "bufaux.h"
53#include "packet.h"
e78a59f5 54
7368a6c8 55/* import */
56extern ServerOptions options;
7368a6c8 57
01dafcb5 58/* Debugging messages */
59Buffer auth_debug;
60int auth_debug_init;
61
7368a6c8 62/*
c6a69271 63 * 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.
7368a6c8 68 * If the user's shell is not executable, false will be returned.
6ae2364d 69 * Otherwise true is returned.
7368a6c8 70 */
a306f2dd 71int
7368a6c8 72allowed_user(struct passwd * pw)
73{
74 struct stat st;
e2ef2342 75 const char *hostname = NULL, *ipaddr = NULL, *passwd;
614dee3a 76 char *shell;
7368a6c8 77 int i;
78#ifdef WITH_AIXAUTHENTICATE
79 char *loginmsg;
80#endif /* WITH_AIXAUTHENTICATE */
b57c8e20 81#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
c6a69271 82 struct spwd *spw;
e2ef2342 83#endif
7368a6c8 84
85 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 86 if (!pw || !pw->pw_name)
7368a6c8 87 return 0;
88
b57c8e20 89 /* Grab the password for locked account checking */
90#if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
4cb5ffa0 91 spw = getspnam(pw->pw_name);
b57c8e20 92 if (!spw)
93 return 0;
e2ef2342 94 passwd = spw->sp_pwdp;
4cb5ffa0 95#else
e2ef2342 96 passwd = pw->pw_passwd;
4cb5ffa0 97#endif
93a9a35a 98
e2ef2342 99 /* check for locked account */
100 if (strcmp(passwd, "*LK*") == 0 || passwd[0] == '!') {
101 log("User %.100s not allowed because account is locked",
102 pw->pw_name);
103 return 0;
104 }
4cb5ffa0 105
b57c8e20 106#if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
107 !defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
108#define DAY (24L * 60 * 60) /* 1 day in seconds */
109 time_t today = time(NULL) / DAY;
110 debug3("allowed_user: today %d sp_expire %d sp_lstchg %d"
111 " sp_max %d", (int)today, (int)spw->sp_expire,
112 (int)spw->sp_lstchg, (int)spw->sp_max);
113
114 /*
115 * We assume account and password expiration occurs the
116 * day after the day specified.
117 */
118 if (spw->sp_expire != -1 && today > spw->sp_expire) {
119 log("Account %.100s has expired", pw->pw_name);
120 return 0;
121 }
122
123 if (spw->sp_lstchg == 0) {
124 log("User %.100s password has expired (root forced)",
125 pw->pw_name);
126 return 0;
127 }
128
129 if (spw->sp_max != -1 &&
130 today > spw->sp_lstchg + spw->sp_max) {
131 log("User %.100s password has expired (password aged)",
132 pw->pw_name);
133 return 0;
134 }
135#endif
136
301e9b01 137 /*
138 * Get the shell from the password data. An empty shell field is
139 * legal, and means /bin/sh.
140 */
141 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
142
143 /* deny if shell does not exists or is not executable */
b334badd 144 if (stat(shell, &st) != 0) {
145 log("User %.100s not allowed because shell %.100s does not exist",
146 pw->pw_name, shell);
7368a6c8 147 return 0;
b334badd 148 }
c390a3c8 149 if (S_ISREG(st.st_mode) == 0 ||
150 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
b334badd 151 log("User %.100s not allowed because shell %.100s is not executable",
152 pw->pw_name, shell);
7368a6c8 153 return 0;
b334badd 154 }
7368a6c8 155
6805fc56 156 if (options.num_deny_users > 0 || options.num_allow_users > 0) {
157 hostname = get_canonical_hostname(options.verify_reverse_mapping);
158 ipaddr = get_remote_ipaddr();
159 }
160
7368a6c8 161 /* Return false if user is listed in DenyUsers */
162 if (options.num_deny_users > 0) {
7368a6c8 163 for (i = 0; i < options.num_deny_users; i++)
762715ce 164 if (match_user(pw->pw_name, hostname, ipaddr,
b334badd 165 options.deny_users[i])) {
762715ce 166 log("User %.100s not allowed because listed in DenyUsers",
167 pw->pw_name);
7368a6c8 168 return 0;
b334badd 169 }
7368a6c8 170 }
171 /* Return false if AllowUsers isn't empty and user isn't listed there */
172 if (options.num_allow_users > 0) {
7368a6c8 173 for (i = 0; i < options.num_allow_users; i++)
762715ce 174 if (match_user(pw->pw_name, hostname, ipaddr,
80f8f24f 175 options.allow_users[i]))
7368a6c8 176 break;
177 /* i < options.num_allow_users iff we break for loop */
b334badd 178 if (i >= options.num_allow_users) {
179 log("User %.100s not allowed because not listed in AllowUsers",
180 pw->pw_name);
7368a6c8 181 return 0;
b334badd 182 }
7368a6c8 183 }
7368a6c8 184 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
c6a69271 185 /* Get the user's group access list (primary and supplementary) */
b334badd 186 if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
187 log("User %.100s not allowed because not in any group",
188 pw->pw_name);
7368a6c8 189 return 0;
b334badd 190 }
7368a6c8 191
c6a69271 192 /* Return false if one of user's groups is listed in DenyGroups */
193 if (options.num_deny_groups > 0)
194 if (ga_match(options.deny_groups,
195 options.num_deny_groups)) {
196 ga_free();
b334badd 197 log("User %.100s not allowed because a group is listed in DenyGroups",
198 pw->pw_name);
7368a6c8 199 return 0;
c6a69271 200 }
7368a6c8 201 /*
c6a69271 202 * Return false if AllowGroups isn't empty and one of user's groups
7368a6c8 203 * isn't listed there
204 */
c6a69271 205 if (options.num_allow_groups > 0)
206 if (!ga_match(options.allow_groups,
207 options.num_allow_groups)) {
208 ga_free();
b334badd 209 log("User %.100s not allowed because none of user's groups are listed in AllowGroups",
210 pw->pw_name);
7368a6c8 211 return 0;
c6a69271 212 }
213 ga_free();
7368a6c8 214 }
215
216#ifdef WITH_AIXAUTHENTICATE
f3e87063 217 /*
218 * Don't check loginrestrictions() for root account (use
219 * PermitRootLogin to control logins via ssh), or if running as
220 * non-root user (since loginrestrictions will always fail).
221 */
222 if ( (pw->pw_uid != 0) && (geteuid() == 0) &&
223 loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
99f4fb69 224 int loginrestrict_errno = errno;
225
c1ef8333 226 if (loginmsg && *loginmsg) {
227 /* Remove embedded newlines (if any) */
228 char *p;
5daf7064 229 for (p = loginmsg; *p; p++) {
c1ef8333 230 if (*p == '\n')
231 *p = ' ';
5daf7064 232 }
c1ef8333 233 /* Remove trailing newline */
234 *--p = '\0';
99f4fb69 235 log("Login restricted for %s: %.100s", pw->pw_name,
236 loginmsg);
c1ef8333 237 }
99f4fb69 238 /* Don't fail if /etc/nologin set */
239 if (!(loginrestrict_errno == EPERM &&
240 stat(_PATH_NOLOGIN, &st) == 0))
241 return 0;
c1ef8333 242 }
7368a6c8 243#endif /* WITH_AIXAUTHENTICATE */
244
245 /* We found no reason not to let this user try to log on... */
246 return 1;
247}
59c97189 248
249Authctxt *
250authctxt_new(void)
251{
2b87da3b 252 Authctxt *authctxt = xmalloc(sizeof(*authctxt));
253 memset(authctxt, 0, sizeof(*authctxt));
254 return authctxt;
59c97189 255}
256
59c97189 257void
258auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
259{
260 void (*authlog) (const char *fmt,...) = verbose;
261 char *authmsg;
262
263 /* Raise logging level */
264 if (authenticated == 1 ||
265 !authctxt->valid ||
266 authctxt->failures >= AUTH_FAIL_LOG ||
267 strcmp(method, "password") == 0)
268 authlog = log;
269
270 if (authctxt->postponed)
271 authmsg = "Postponed";
272 else
273 authmsg = authenticated ? "Accepted" : "Failed";
274
275 authlog("%s %s for %s%.100s from %.200s port %d%s",
276 authmsg,
277 method,
278 authctxt->valid ? "" : "illegal user ",
fad3754c 279 authctxt->user,
59c97189 280 get_remote_ipaddr(),
281 get_remote_port(),
282 info);
56fd97d7 283
284#ifdef WITH_AIXAUTHENTICATE
285 if (authenticated == 0 && strcmp(method, "password") == 0)
286 loginfailed(authctxt->user,
287 get_canonical_hostname(options.verify_reverse_mapping),
288 "ssh");
289#endif /* WITH_AIXAUTHENTICATE */
290
59c97189 291}
292
293/*
15853e93 294 * Check whether root logins are disallowed.
59c97189 295 */
296int
15853e93 297auth_root_allowed(char *method)
59c97189 298{
15853e93 299 switch (options.permit_root_login) {
300 case PERMIT_YES:
59c97189 301 return 1;
15853e93 302 break;
303 case PERMIT_NO_PASSWD:
304 if (strcmp(method, "password") != 0)
305 return 1;
306 break;
307 case PERMIT_FORCED_ONLY:
308 if (forced_command) {
309 log("Root login accepted for forced command.");
310 return 1;
311 }
312 break;
59c97189 313 }
15853e93 314 log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
315 return 0;
59c97189 316}
c8445989 317
318
319/*
320 * Given a template and a passwd structure, build a filename
321 * by substituting % tokenised options. Currently, %% becomes '%',
322 * %h becomes the home directory and %u the username.
323 *
324 * This returns a buffer allocated by xmalloc.
325 */
326char *
327expand_filename(const char *filename, struct passwd *pw)
328{
329 Buffer buffer;
330 char *file;
331 const char *cp;
332
333 /*
334 * Build the filename string in the buffer by making the appropriate
335 * substitutions to the given file name.
336 */
337 buffer_init(&buffer);
338 for (cp = filename; *cp; cp++) {
339 if (cp[0] == '%' && cp[1] == '%') {
340 buffer_append(&buffer, "%", 1);
341 cp++;
342 continue;
343 }
344 if (cp[0] == '%' && cp[1] == 'h') {
345 buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
346 cp++;
347 continue;
348 }
349 if (cp[0] == '%' && cp[1] == 'u') {
350 buffer_append(&buffer, pw->pw_name,
184eed6a 351 strlen(pw->pw_name));
c8445989 352 cp++;
353 continue;
354 }
355 buffer_append(&buffer, cp, 1);
356 }
357 buffer_append(&buffer, "\0", 1);
358
359 /*
360 * Ensure that filename starts anchored. If not, be backward
361 * compatible and prepend the '%h/'
362 */
363 file = xmalloc(MAXPATHLEN);
364 cp = buffer_ptr(&buffer);
365 if (*cp != '/')
366 snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
367 else
368 strlcpy(file, cp, MAXPATHLEN);
369
370 buffer_free(&buffer);
371 return file;
372}
373
374char *
375authorized_keys_file(struct passwd *pw)
376{
377 return expand_filename(options.authorized_keys_file, pw);
378}
379
380char *
381authorized_keys_file2(struct passwd *pw)
382{
383 return expand_filename(options.authorized_keys_file2, pw);
384}
385
d0c8ca5c 386/* return ok if key exists in sysfile or userfile */
387HostStatus
388check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
389 const char *sysfile, const char *userfile)
390{
391 Key *found;
392 char *user_hostfile;
393 struct stat st;
17a3011c 394 HostStatus host_status;
d0c8ca5c 395
396 /* Check if we know the host and its host key. */
397 found = key_new(key->type);
398 host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
399
400 if (host_status != HOST_OK && userfile != NULL) {
401 user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
402 if (options.strict_modes &&
403 (stat(user_hostfile, &st) == 0) &&
404 ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
184eed6a 405 (st.st_mode & 022) != 0)) {
d0c8ca5c 406 log("Authentication refused for %.100s: "
407 "bad owner or modes for %.200s",
408 pw->pw_name, user_hostfile);
409 } else {
410 temporarily_use_uid(pw);
411 host_status = check_host_in_hostfile(user_hostfile,
412 host, key, found, NULL);
413 restore_uid();
414 }
415 xfree(user_hostfile);
416 }
417 key_free(found);
418
419 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
420 "ok" : "not found", host);
421 return host_status;
422}
423
424
c8445989 425/*
426 * Check a given file for security. This is defined as all components
d6097023 427 * of the path to the file must be owned by either the owner of
1ddf764b 428 * of the file or root and no directories must be group or world writable.
c8445989 429 *
430 * XXX Should any specific check be done for sym links ?
431 *
432 * Takes an open file descriptor, the file name, a uid and and
433 * error buffer plus max size as arguments.
434 *
435 * Returns 0 on success and -1 on failure
436 */
437int
6978866a 438secure_filename(FILE *f, const char *file, struct passwd *pw,
439 char *err, size_t errlen)
c8445989 440{
6978866a 441 uid_t uid = pw->pw_uid;
76fbdd47 442 char buf[MAXPATHLEN], homedir[MAXPATHLEN];
c8445989 443 char *cp;
f6f02456 444 int comparehome = 0;
c8445989 445 struct stat st;
446
447 if (realpath(file, buf) == NULL) {
448 snprintf(err, errlen, "realpath %s failed: %s", file,
449 strerror(errno));
450 return -1;
451 }
f6f02456 452 if (realpath(pw->pw_dir, homedir) != NULL)
453 comparehome = 1;
c8445989 454
455 /* check the open file to avoid races */
456 if (fstat(fileno(f), &st) < 0 ||
457 (st.st_uid != 0 && st.st_uid != uid) ||
458 (st.st_mode & 022) != 0) {
459 snprintf(err, errlen, "bad ownership or modes for file %s",
460 buf);
461 return -1;
462 }
463
464 /* for each component of the canonical path, walking upwards */
465 for (;;) {
466 if ((cp = dirname(buf)) == NULL) {
467 snprintf(err, errlen, "dirname() failed");
468 return -1;
469 }
470 strlcpy(buf, cp, sizeof(buf));
471
472 debug3("secure_filename: checking '%s'", buf);
473 if (stat(buf, &st) < 0 ||
474 (st.st_uid != 0 && st.st_uid != uid) ||
475 (st.st_mode & 022) != 0) {
184eed6a 476 snprintf(err, errlen,
c8445989 477 "bad ownership or modes for directory %s", buf);
478 return -1;
479 }
480
afd501f9 481 /* If are passed the homedir then we can stop */
f6f02456 482 if (comparehome && strcmp(homedir, buf) == 0) {
afd501f9 483 debug3("secure_filename: terminating check at '%s'",
484 buf);
485 break;
486 }
c8445989 487 /*
488 * dirname should always complete with a "/" path,
489 * but we can be paranoid and check for "." too
490 */
491 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
492 break;
493 }
494 return 0;
495}
443fa1cd 496
497struct passwd *
498getpwnamallow(const char *user)
499{
1836f69f 500#ifdef HAVE_LOGIN_CAP
501 extern login_cap_t *lc;
502#ifdef BSD_AUTH
503 auth_session_t *as;
504#endif
505#endif
443fa1cd 506 struct passwd *pw;
507
508 pw = getpwnam(user);
c2802d92 509 if (pw == NULL) {
510 log("Illegal user %.100s from %.100s",
511 user, get_remote_ipaddr());
819d3f09 512#ifdef WITH_AIXAUTHENTICATE
513 loginfailed(user,
514 get_canonical_hostname(options.verify_reverse_mapping),
515 "ssh");
516#endif
c2802d92 517 return (NULL);
518 }
519 if (!allowed_user(pw))
1836f69f 520 return (NULL);
521#ifdef HAVE_LOGIN_CAP
522 if ((lc = login_getclass(pw->pw_class)) == NULL) {
523 debug("unable to get login class: %s", user);
524 return (NULL);
525 }
526#ifdef BSD_AUTH
527 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
65721f9c 528 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1836f69f 529 debug("Approval failure for %s", user);
443fa1cd 530 pw = NULL;
1836f69f 531 }
532 if (as != NULL)
533 auth_close(as);
534#endif
535#endif
06bea668 536 if (pw != NULL)
537 return (pwcopy(pw));
538 return (NULL);
443fa1cd 539}
01dafcb5 540
541void
542auth_debug_add(const char *fmt,...)
543{
544 char buf[1024];
545 va_list args;
546
547 if (!auth_debug_init)
548 return;
549
550 va_start(args, fmt);
551 vsnprintf(buf, sizeof(buf), fmt, args);
552 va_end(args);
553 buffer_put_cstring(&auth_debug, buf);
554}
555
556void
557auth_debug_send(void)
558{
559 char *msg;
560
561 if (!auth_debug_init)
562 return;
563 while (buffer_len(&auth_debug)) {
564 msg = buffer_get_string(&auth_debug, NULL);
565 packet_send_debug("%s", msg);
566 xfree(msg);
567 }
568}
569
570void
571auth_debug_reset(void)
572{
573 if (auth_debug_init)
574 buffer_clear(&auth_debug);
575 else {
576 buffer_init(&auth_debug);
577 auth_debug_init = 1;
578 }
579}
This page took 0.966543 seconds and 5 git commands to generate.