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