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