1 /* $OpenBSD: auth.c,v 1.75 2006/08/03 03:34:41 deraadt Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
30 #include <sys/param.h>
52 #include "groupaccess.h"
59 #include "auth-options.h"
68 #include "monitor_wrap.h"
71 extern ServerOptions options;
72 extern int use_privsep;
73 extern Buffer loginmsg;
75 /* Debugging messages */
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.
85 * If the user's shell is not executable, false will be returned.
86 * Otherwise true is returned.
89 allowed_user(struct passwd * pw)
92 const char *hostname = NULL, *ipaddr = NULL, *passwd = NULL;
96 struct spwd *spw = NULL;
99 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
100 if (!pw || !pw->pw_name)
104 if (!options.use_pam)
105 spw = getspnam(pw->pw_name);
106 #ifdef HAS_SHADOW_EXPIRE
107 if (!options.use_pam && spw != NULL && auth_shadow_acctexpired(spw))
109 #endif /* HAS_SHADOW_EXPIRE */
110 #endif /* USE_SHADOW */
112 /* grab passwd field for locked account check */
115 #if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
116 passwd = get_iaf_password(pw);
118 passwd = spw->sp_pwdp;
119 #endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
121 passwd = pw->pw_passwd;
124 /* check for locked account */
125 if (!options.use_pam && passwd && *passwd) {
128 #ifdef LOCKED_PASSWD_STRING
129 if (strcmp(passwd, LOCKED_PASSWD_STRING) == 0)
132 #ifdef LOCKED_PASSWD_PREFIX
133 if (strncmp(passwd, LOCKED_PASSWD_PREFIX,
134 strlen(LOCKED_PASSWD_PREFIX)) == 0)
137 #ifdef LOCKED_PASSWD_SUBSTR
138 if (strstr(passwd, LOCKED_PASSWD_SUBSTR))
141 #if defined(HAVE_LIBIAF) && !defined(BROKEN_LIBIAF)
143 #endif /* HAVE_LIBIAF && !BROKEN_LIBIAF */
145 logit("User %.100s not allowed because account is locked",
152 * Get the shell from the password data. An empty shell field is
153 * legal, and means /bin/sh.
155 shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
157 /* deny if shell does not exists or is not executable */
158 if (stat(shell, &st) != 0) {
159 logit("User %.100s not allowed because shell %.100s does not exist",
163 if (S_ISREG(st.st_mode) == 0 ||
164 (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
165 logit("User %.100s not allowed because shell %.100s is not executable",
170 if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
171 options.num_deny_groups > 0 || options.num_allow_groups > 0) {
172 hostname = get_canonical_hostname(options.use_dns);
173 ipaddr = get_remote_ipaddr();
176 /* Return false if user is listed in DenyUsers */
177 if (options.num_deny_users > 0) {
178 for (i = 0; i < options.num_deny_users; i++)
179 if (match_user(pw->pw_name, hostname, ipaddr,
180 options.deny_users[i])) {
181 logit("User %.100s from %.100s not allowed "
182 "because listed in DenyUsers",
183 pw->pw_name, hostname);
187 /* Return false if AllowUsers isn't empty and user isn't listed there */
188 if (options.num_allow_users > 0) {
189 for (i = 0; i < options.num_allow_users; i++)
190 if (match_user(pw->pw_name, hostname, ipaddr,
191 options.allow_users[i]))
193 /* i < options.num_allow_users iff we break for loop */
194 if (i >= options.num_allow_users) {
195 logit("User %.100s from %.100s not allowed because "
196 "not listed in AllowUsers", pw->pw_name, hostname);
200 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
201 /* Get the user's group access list (primary and supplementary) */
202 if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
203 logit("User %.100s from %.100s not allowed because "
204 "not in any group", pw->pw_name, hostname);
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)) {
213 logit("User %.100s from %.100s not allowed "
214 "because a group is listed in DenyGroups",
215 pw->pw_name, hostname);
219 * Return false if AllowGroups isn't empty and one of user's groups
222 if (options.num_allow_groups > 0)
223 if (!ga_match(options.allow_groups,
224 options.num_allow_groups)) {
226 logit("User %.100s from %.100s not allowed "
227 "because none of user's groups are listed "
228 "in AllowGroups", pw->pw_name, hostname);
234 #ifdef CUSTOM_SYS_AUTH_ALLOWED_USER
235 if (!sys_auth_allowed_user(pw, &loginmsg))
239 /* We found no reason not to let this user try to log on... */
244 auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
246 void (*authlog) (const char *fmt,...) = verbose;
249 if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
252 /* Raise logging level */
253 if (authenticated == 1 ||
255 authctxt->failures >= options.max_authtries / 2 ||
256 strcmp(method, "password") == 0)
259 if (authctxt->postponed)
260 authmsg = "Postponed";
262 authmsg = authenticated ? "Accepted" : "Failed";
264 authlog("%s %s for %s%.100s from %.200s port %d%s",
267 authctxt->valid ? "" : "invalid user ",
273 #ifdef CUSTOM_FAILED_LOGIN
274 if (authenticated == 0 && !authctxt->postponed &&
275 (strcmp(method, "password") == 0 ||
276 strncmp(method, "keyboard-interactive", 20) == 0 ||
277 strcmp(method, "challenge-response") == 0))
278 record_failed_login(authctxt->user,
279 get_canonical_hostname(options.use_dns), "ssh");
281 #ifdef SSH_AUDIT_EVENTS
282 if (authenticated == 0 && !authctxt->postponed)
283 audit_event(audit_classify_auth(method));
288 * Check whether root logins are disallowed.
291 auth_root_allowed(char *method)
293 switch (options.permit_root_login) {
296 case PERMIT_NO_PASSWD:
297 if (strcmp(method, "password") != 0)
300 case PERMIT_FORCED_ONLY:
301 if (forced_command) {
302 logit("Root login accepted for forced command.");
307 logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
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.
317 * This returns a buffer allocated by xmalloc.
320 expand_authorized_keys(const char *filename, struct passwd *pw)
322 char *file, ret[MAXPATHLEN];
325 file = percent_expand(filename, "h", pw->pw_dir,
326 "u", pw->pw_name, (char *)NULL);
329 * Ensure that filename starts anchored. If not, be backward
330 * compatible and prepend the '%h/'
335 i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
336 if (i < 0 || (size_t)i >= sizeof(ret))
337 fatal("expand_authorized_keys: path too long");
339 return (xstrdup(ret));
343 authorized_keys_file(struct passwd *pw)
345 return expand_authorized_keys(options.authorized_keys_file, pw);
349 authorized_keys_file2(struct passwd *pw)
351 return expand_authorized_keys(options.authorized_keys_file2, pw);
354 /* return ok if key exists in sysfile or userfile */
356 check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
357 const char *sysfile, const char *userfile)
362 HostStatus host_status;
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);
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) ||
373 (st.st_mode & 022) != 0)) {
374 logit("Authentication refused for %.100s: "
375 "bad owner or modes for %.200s",
376 pw->pw_name, user_hostfile);
378 temporarily_use_uid(pw);
379 host_status = check_host_in_hostfile(user_hostfile,
380 host, key, found, NULL);
383 xfree(user_hostfile);
387 debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
388 "ok" : "not found", host);
394 * Check a given file for security. This is defined as all components
395 * of the path to the file must be owned by either the owner of
396 * of the file or root and no directories must be group or world writable.
398 * XXX Should any specific check be done for sym links ?
400 * Takes an open file descriptor, the file name, a uid and and
401 * error buffer plus max size as arguments.
403 * Returns 0 on success and -1 on failure
406 secure_filename(FILE *f, const char *file, struct passwd *pw,
407 char *err, size_t errlen)
409 uid_t uid = pw->pw_uid;
410 char buf[MAXPATHLEN], homedir[MAXPATHLEN];
415 if (realpath(file, buf) == NULL) {
416 snprintf(err, errlen, "realpath %s failed: %s", file,
420 if (realpath(pw->pw_dir, homedir) != NULL)
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",
432 /* for each component of the canonical path, walking upwards */
434 if ((cp = dirname(buf)) == NULL) {
435 snprintf(err, errlen, "dirname() failed");
438 strlcpy(buf, cp, sizeof(buf));
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) {
444 snprintf(err, errlen,
445 "bad ownership or modes for directory %s", buf);
449 /* If are passed the homedir then we can stop */
450 if (comparehome && strcmp(homedir, buf) == 0) {
451 debug3("secure_filename: terminating check at '%s'",
456 * dirname should always complete with a "/" path,
457 * but we can be paranoid and check for "." too
459 if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
466 getpwnamallow(const char *user)
468 #ifdef HAVE_LOGIN_CAP
469 extern login_cap_t *lc;
476 parse_server_match_config(&options, user,
477 get_canonical_hostname(options.use_dns), get_remote_ipaddr());
481 logit("Invalid user %.100s from %.100s",
482 user, get_remote_ipaddr());
483 #ifdef CUSTOM_FAILED_LOGIN
484 record_failed_login(user,
485 get_canonical_hostname(options.use_dns), "ssh");
487 #ifdef SSH_AUDIT_EVENTS
488 audit_event(SSH_INVALID_USER);
489 #endif /* SSH_AUDIT_EVENTS */
492 if (!allowed_user(pw))
494 #ifdef HAVE_LOGIN_CAP
495 if ((lc = login_getclass(pw->pw_class)) == NULL) {
496 debug("unable to get login class: %s", user);
500 if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
501 auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
502 debug("Approval failure for %s", user);
515 auth_debug_add(const char *fmt,...)
520 if (!auth_debug_init)
524 vsnprintf(buf, sizeof(buf), fmt, args);
526 buffer_put_cstring(&auth_debug, buf);
530 auth_debug_send(void)
534 if (!auth_debug_init)
536 while (buffer_len(&auth_debug)) {
537 msg = buffer_get_string(&auth_debug, NULL);
538 packet_send_debug("%s", msg);
544 auth_debug_reset(void)
547 buffer_clear(&auth_debug);
549 buffer_init(&auth_debug);
557 static struct passwd fake;
559 memset(&fake, 0, sizeof(fake));
560 fake.pw_name = "NOUSER";
562 "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
563 fake.pw_gecos = "NOUSER";
564 fake.pw_uid = (uid_t)-1;
565 fake.pw_gid = (gid_t)-1;
566 #ifdef HAVE_PW_CLASS_IN_PASSWD
569 fake.pw_dir = "/nonexist";
570 fake.pw_shell = "/nonexist";