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