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