]> andersk Git - openssh.git/blame - auth.c
- (djm) [auth-pam.c auth.c bufaux.h entropy.c openbsd-compat/port-tun.c]
[openssh.git] / auth.c
CommitLineData
31652869 1/* $OpenBSD: auth.c,v 1.75 2006/08/03 03:34:41 deraadt Exp $ */
20d0f77f 2/*
f3c7c613 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
bcbf86ec 4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7368a6c8 24 */
25
26#include "includes.h"
4095f623 27
28#include <sys/types.h>
29#include <sys/stat.h>
536c14e8 30#include <sys/param.h>
7368a6c8 31
028094f4 32#include <errno.h>
a75f5360 33#ifdef HAVE_PATHS_H
6f61e0ec 34# include <paths.h>
a75f5360 35#endif
b1842393 36#include <pwd.h>
c1ef8333 37#ifdef HAVE_LOGIN_H
38#include <login.h>
39#endif
f14ca4a4 40#ifdef USE_SHADOW
4cb5ffa0 41#include <shadow.h>
f14ca4a4 42#endif
b4d02860 43#ifdef HAVE_LIBGEN_H
c8445989 44#include <libgen.h>
b4d02860 45#endif
4c72fcfd 46#include <stdarg.h>
cf851879 47#include <stdio.h>
00146caa 48#include <string.h>
c8445989 49
42f11eb2 50#include "xmalloc.h"
51#include "match.h"
52#include "groupaccess.h"
53#include "log.h"
31652869 54#include "buffer.h"
42f11eb2 55#include "servconf.h"
31652869 56#include "key.h"
57#include "hostfile.h"
e78a59f5 58#include "auth.h"
59c97189 59#include "auth-options.h"
42f11eb2 60#include "canohost.h"
d0c8ca5c 61#include "uidswap.h"
5f1f36b5 62#include "misc.h"
01dafcb5 63#include "packet.h"
575e336f 64#include "loginrec.h"
31652869 65#ifdef GSSAPI
66#include "ssh-gss.h"
67#endif
c00e4d75 68#include "monitor_wrap.h"
e78a59f5 69
7368a6c8 70/* import */
71extern ServerOptions options;
c023a130 72extern int use_privsep;
bc7dfc06 73extern Buffer loginmsg;
7368a6c8 74
01dafcb5 75/* Debugging messages */
76Buffer auth_debug;
77int auth_debug_init;
78
7368a6c8 79/*
c6a69271 80 * Check if the user is allowed to log in via ssh. If user is listed
81 * in DenyUsers or one of user's groups is listed in DenyGroups, false
82 * will be returned. If AllowUsers isn't empty and user isn't listed
83 * there, or if AllowGroups isn't empty and one of user's groups isn't
84 * listed there, false will be returned.
7368a6c8 85 * If the user's shell is not executable, false will be returned.
6ae2364d 86 * Otherwise true is returned.
7368a6c8 87 */
a306f2dd 88int
7368a6c8 89allowed_user(struct passwd * pw)
90{
91 struct stat st;
da67ae18 92 const char *hostname = NULL, *ipaddr = NULL, *passwd = NULL;
614dee3a 93 char *shell;
2ceb8101 94 u_int i;
f14ca4a4 95#ifdef USE_SHADOW
3e6e3da0 96 struct spwd *spw = NULL;
e2ef2342 97#endif
7368a6c8 98
99 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
c6a69271 100 if (!pw || !pw->pw_name)
7368a6c8 101 return 0;
102
f14ca4a4 103#ifdef USE_SHADOW
3e6e3da0 104 if (!options.use_pam)
105 spw = getspnam(pw->pw_name);
106#ifdef HAS_SHADOW_EXPIRE
f14ca4a4 107 if (!options.use_pam && spw != NULL && auth_shadow_acctexpired(spw))
108 return 0;
3e6e3da0 109#endif /* HAS_SHADOW_EXPIRE */
f14ca4a4 110#endif /* USE_SHADOW */
3e6e3da0 111
aff51935 112 /* grab passwd field for locked account check */
f14ca4a4 113#ifdef USE_SHADOW
3e6e3da0 114 if (spw != NULL)
26d07095 115#if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
4c653d8e 116 passwd = get_iaf_password(pw);
117#else
3e6e3da0 118 passwd = spw->sp_pwdp;
26d07095 119#endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
3e6e3da0 120#else
121 passwd = pw->pw_passwd;
b57c8e20 122#endif
123
aff51935 124 /* check for locked account */
da67ae18 125 if (!options.use_pam && passwd && *passwd) {
3e6e3da0 126 int locked = 0;
127
128#ifdef LOCKED_PASSWD_STRING
129 if (strcmp(passwd, LOCKED_PASSWD_STRING) == 0)
130 locked = 1;
131#endif
132#ifdef LOCKED_PASSWD_PREFIX
133 if (strncmp(passwd, LOCKED_PASSWD_PREFIX,
134 strlen(LOCKED_PASSWD_PREFIX)) == 0)
135 locked = 1;
136#endif
137#ifdef LOCKED_PASSWD_SUBSTR
138 if (strstr(passwd, LOCKED_PASSWD_SUBSTR))
139 locked = 1;
140#endif
26d07095 141#if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
142 free(passwd);
143#endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
3e6e3da0 144 if (locked) {
145 logit("User %.100s not allowed because account is locked",
146 pw->pw_name);
147 return 0;
148 }
149 }
150
301e9b01 151 /*
152 * Get the shell from the password data. An empty shell field is
153 * legal, and means /bin/sh.
154 */
155 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
156
157 /* deny if shell does not exists or is not executable */
b334badd 158 if (stat(shell, &st) != 0) {
bbe88b6d 159 logit("User %.100s not allowed because shell %.100s does not exist",
b334badd 160 pw->pw_name, shell);
7368a6c8 161 return 0;
b334badd 162 }
c390a3c8 163 if (S_ISREG(st.st_mode) == 0 ||
164 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
bbe88b6d 165 logit("User %.100s not allowed because shell %.100s is not executable",
b334badd 166 pw->pw_name, shell);
7368a6c8 167 return 0;
b334badd 168 }
7368a6c8 169
2dcbac07 170 if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
171 options.num_deny_groups > 0 || options.num_allow_groups > 0) {
c5a7d788 172 hostname = get_canonical_hostname(options.use_dns);
6805fc56 173 ipaddr = get_remote_ipaddr();
174 }
175
7368a6c8 176 /* Return false if user is listed in DenyUsers */
177 if (options.num_deny_users > 0) {
7368a6c8 178 for (i = 0; i < options.num_deny_users; i++)
762715ce 179 if (match_user(pw->pw_name, hostname, ipaddr,
b334badd 180 options.deny_users[i])) {
31de8b2b 181 logit("User %.100s from %.100s not allowed "
182 "because listed in DenyUsers",
183 pw->pw_name, hostname);
7368a6c8 184 return 0;
b334badd 185 }
7368a6c8 186 }
187 /* Return false if AllowUsers isn't empty and user isn't listed there */
188 if (options.num_allow_users > 0) {
7368a6c8 189 for (i = 0; i < options.num_allow_users; i++)
762715ce 190 if (match_user(pw->pw_name, hostname, ipaddr,
80f8f24f 191 options.allow_users[i]))
7368a6c8 192 break;
193 /* i < options.num_allow_users iff we break for loop */
b334badd 194 if (i >= options.num_allow_users) {
31de8b2b 195 logit("User %.100s from %.100s not allowed because "
196 "not listed in AllowUsers", pw->pw_name, hostname);
7368a6c8 197 return 0;
b334badd 198 }
7368a6c8 199 }
7368a6c8 200 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
c6a69271 201 /* Get the user's group access list (primary and supplementary) */
b334badd 202 if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
31de8b2b 203 logit("User %.100s from %.100s not allowed because "
204 "not in any group", pw->pw_name, hostname);
7368a6c8 205 return 0;
b334badd 206 }
7368a6c8 207
c6a69271 208 /* Return false if one of user's groups is listed in DenyGroups */
209 if (options.num_deny_groups > 0)
210 if (ga_match(options.deny_groups,
211 options.num_deny_groups)) {
212 ga_free();
31de8b2b 213 logit("User %.100s from %.100s not allowed "
214 "because a group is listed in DenyGroups",
215 pw->pw_name, hostname);
7368a6c8 216 return 0;
c6a69271 217 }
7368a6c8 218 /*
c6a69271 219 * Return false if AllowGroups isn't empty and one of user's groups
7368a6c8 220 * isn't listed there
221 */
c6a69271 222 if (options.num_allow_groups > 0)
223 if (!ga_match(options.allow_groups,
224 options.num_allow_groups)) {
225 ga_free();
31de8b2b 226 logit("User %.100s from %.100s not allowed "
227 "because none of user's groups are listed "
228 "in AllowGroups", pw->pw_name, hostname);
7368a6c8 229 return 0;
c6a69271 230 }
231 ga_free();
7368a6c8 232 }
233
bc5c2025 234#ifdef CUSTOM_SYS_AUTH_ALLOWED_USER
5ccf88cb 235 if (!sys_auth_allowed_user(pw, &loginmsg))
bc5c2025 236 return 0;
237#endif
7368a6c8 238
239 /* We found no reason not to let this user try to log on... */
240 return 1;
241}
59c97189 242
59c97189 243void
244auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
245{
246 void (*authlog) (const char *fmt,...) = verbose;
247 char *authmsg;
248
c023a130 249 if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
250 return;
251
59c97189 252 /* Raise logging level */
253 if (authenticated == 1 ||
254 !authctxt->valid ||
af4bd935 255 authctxt->failures >= options.max_authtries / 2 ||
59c97189 256 strcmp(method, "password") == 0)
a3568201 257 authlog = logit;
59c97189 258
259 if (authctxt->postponed)
260 authmsg = "Postponed";
261 else
262 authmsg = authenticated ? "Accepted" : "Failed";
263
264 authlog("%s %s for %s%.100s from %.200s port %d%s",
265 authmsg,
266 method,
d77347cc 267 authctxt->valid ? "" : "invalid user ",
fad3754c 268 authctxt->user,
59c97189 269 get_remote_ipaddr(),
270 get_remote_port(),
271 info);
56fd97d7 272
73d9dad3 273#ifdef CUSTOM_FAILED_LOGIN
b6610e8f 274 if (authenticated == 0 && !authctxt->postponed &&
275 (strcmp(method, "password") == 0 ||
6af6e631 276 strncmp(method, "keyboard-interactive", 20) == 0 ||
277 strcmp(method, "challenge-response") == 0))
575e336f 278 record_failed_login(authctxt->user,
279 get_canonical_hostname(options.use_dns), "ssh");
73d9dad3 280#endif
6039eeef 281#ifdef SSH_AUDIT_EVENTS
2ae7f715 282 if (authenticated == 0 && !authctxt->postponed)
283 audit_event(audit_classify_auth(method));
c00e4d75 284#endif
59c97189 285}
286
287/*
15853e93 288 * Check whether root logins are disallowed.
59c97189 289 */
290int
15853e93 291auth_root_allowed(char *method)
59c97189 292{
15853e93 293 switch (options.permit_root_login) {
294 case PERMIT_YES:
59c97189 295 return 1;
15853e93 296 case PERMIT_NO_PASSWD:
297 if (strcmp(method, "password") != 0)
298 return 1;
299 break;
300 case PERMIT_FORCED_ONLY:
301 if (forced_command) {
bbe88b6d 302 logit("Root login accepted for forced command.");
15853e93 303 return 1;
304 }
305 break;
59c97189 306 }
bbe88b6d 307 logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
15853e93 308 return 0;
59c97189 309}
c8445989 310
311
312/*
313 * Given a template and a passwd structure, build a filename
314 * by substituting % tokenised options. Currently, %% becomes '%',
315 * %h becomes the home directory and %u the username.
316 *
317 * This returns a buffer allocated by xmalloc.
318 */
a980cbd7 319static char *
320expand_authorized_keys(const char *filename, struct passwd *pw)
c8445989 321{
52e3daed 322 char *file, ret[MAXPATHLEN];
323 int i;
c8445989 324
a980cbd7 325 file = percent_expand(filename, "h", pw->pw_dir,
326 "u", pw->pw_name, (char *)NULL);
c8445989 327
328 /*
329 * Ensure that filename starts anchored. If not, be backward
330 * compatible and prepend the '%h/'
331 */
a980cbd7 332 if (*file == '/')
333 return (file);
334
52e3daed 335 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
336 if (i < 0 || (size_t)i >= sizeof(ret))
a980cbd7 337 fatal("expand_authorized_keys: path too long");
a980cbd7 338 xfree(file);
52e3daed 339 return (xstrdup(ret));
c8445989 340}
341
342char *
343authorized_keys_file(struct passwd *pw)
344{
a980cbd7 345 return expand_authorized_keys(options.authorized_keys_file, pw);
c8445989 346}
347
348char *
349authorized_keys_file2(struct passwd *pw)
350{
a980cbd7 351 return expand_authorized_keys(options.authorized_keys_file2, pw);
c8445989 352}
353
d0c8ca5c 354/* 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;
17a3011c 362 HostStatus host_status;
d0c8ca5c 363
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) ||
184eed6a 373 (st.st_mode & 022) != 0)) {
bbe88b6d 374 logit("Authentication refused for %.100s: "
d0c8ca5c 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
c8445989 393/*
394 * Check a given file for security. This is defined as all components
d6097023 395 * of the path to the file must be owned by either the owner of
1ddf764b 396 * of the file or root and no directories must be group or world writable.
c8445989 397 *
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
6978866a 406secure_filename(FILE *f, const char *file, struct passwd *pw,
407 char *err, size_t errlen)
c8445989 408{
6978866a 409 uid_t uid = pw->pw_uid;
76fbdd47 410 char buf[MAXPATHLEN], homedir[MAXPATHLEN];
c8445989 411 char *cp;
f6f02456 412 int comparehome = 0;
c8445989 413 struct stat st;
414
415 if (realpath(file, buf) == NULL) {
416 snprintf(err, errlen, "realpath %s failed: %s", file,
417 strerror(errno));
418 return -1;
419 }
f6f02456 420 if (realpath(pw->pw_dir, homedir) != NULL)
421 comparehome = 1;
c8445989 422
423 /* check the open file to avoid races */
424 if (fstat(fileno(f), &st) < 0 ||
425 (st.st_uid != 0 && st.st_uid != uid) ||
426 (st.st_mode & 022) != 0) {
427 snprintf(err, errlen, "bad ownership or modes for file %s",
428 buf);
429 return -1;
430 }
431
432 /* for each component of the canonical path, walking upwards */
433 for (;;) {
434 if ((cp = dirname(buf)) == NULL) {
435 snprintf(err, errlen, "dirname() failed");
436 return -1;
437 }
438 strlcpy(buf, cp, sizeof(buf));
439
440 debug3("secure_filename: checking '%s'", buf);
441 if (stat(buf, &st) < 0 ||
442 (st.st_uid != 0 && st.st_uid != uid) ||
443 (st.st_mode & 022) != 0) {
184eed6a 444 snprintf(err, errlen,
c8445989 445 "bad ownership or modes for directory %s", buf);
446 return -1;
447 }
448
afd501f9 449 /* If are passed the homedir then we can stop */
f6f02456 450 if (comparehome && strcmp(homedir, buf) == 0) {
afd501f9 451 debug3("secure_filename: terminating check at '%s'",
452 buf);
453 break;
454 }
c8445989 455 /*
456 * dirname should always complete with a "/" path,
457 * but we can be paranoid and check for "." too
458 */
459 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
460 break;
461 }
462 return 0;
463}
443fa1cd 464
465struct passwd *
466getpwnamallow(const char *user)
467{
1836f69f 468#ifdef HAVE_LOGIN_CAP
469 extern login_cap_t *lc;
470#ifdef BSD_AUTH
471 auth_session_t *as;
472#endif
473#endif
443fa1cd 474 struct passwd *pw;
475
d231781a 476 parse_server_match_config(&options, user,
477 get_canonical_hostname(options.use_dns), get_remote_ipaddr());
478
443fa1cd 479 pw = getpwnam(user);
c2802d92 480 if (pw == NULL) {
227a6a97 481 logit("Invalid user %.100s from %.100s",
c2802d92 482 user, get_remote_ipaddr());
73d9dad3 483#ifdef CUSTOM_FAILED_LOGIN
575e336f 484 record_failed_login(user,
485 get_canonical_hostname(options.use_dns), "ssh");
819d3f09 486#endif
6039eeef 487#ifdef SSH_AUDIT_EVENTS
488 audit_event(SSH_INVALID_USER);
489#endif /* SSH_AUDIT_EVENTS */
c2802d92 490 return (NULL);
491 }
492 if (!allowed_user(pw))
1836f69f 493 return (NULL);
494#ifdef HAVE_LOGIN_CAP
495 if ((lc = login_getclass(pw->pw_class)) == NULL) {
496 debug("unable to get login class: %s", user);
497 return (NULL);
498 }
499#ifdef BSD_AUTH
500 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
65721f9c 501 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1836f69f 502 debug("Approval failure for %s", user);
443fa1cd 503 pw = NULL;
1836f69f 504 }
505 if (as != NULL)
506 auth_close(as);
507#endif
508#endif
06bea668 509 if (pw != NULL)
510 return (pwcopy(pw));
511 return (NULL);
443fa1cd 512}
01dafcb5 513
514void
515auth_debug_add(const char *fmt,...)
516{
517 char buf[1024];
518 va_list args;
519
520 if (!auth_debug_init)
521 return;
522
523 va_start(args, fmt);
524 vsnprintf(buf, sizeof(buf), fmt, args);
525 va_end(args);
526 buffer_put_cstring(&auth_debug, buf);
527}
528
529void
530auth_debug_send(void)
531{
532 char *msg;
533
534 if (!auth_debug_init)
535 return;
536 while (buffer_len(&auth_debug)) {
537 msg = buffer_get_string(&auth_debug, NULL);
538 packet_send_debug("%s", msg);
539 xfree(msg);
540 }
541}
542
543void
544auth_debug_reset(void)
545{
546 if (auth_debug_init)
547 buffer_clear(&auth_debug);
548 else {
549 buffer_init(&auth_debug);
550 auth_debug_init = 1;
551 }
552}
96d0bf74 553
554struct passwd *
555fakepw(void)
556{
557 static struct passwd fake;
558
559 memset(&fake, 0, sizeof(fake));
560 fake.pw_name = "NOUSER";
561 fake.pw_passwd =
b6453d99 562 "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
96d0bf74 563 fake.pw_gecos = "NOUSER";
f6be21a0 564 fake.pw_uid = (uid_t)-1;
565 fake.pw_gid = (gid_t)-1;
96d0bf74 566#ifdef HAVE_PW_CLASS_IN_PASSWD
567 fake.pw_class = "";
568#endif
569 fake.pw_dir = "/nonexist";
570 fake.pw_shell = "/nonexist";
571
572 return (&fake);
573}
This page took 0.312227 seconds and 5 git commands to generate.