xref: /onnv-gate/usr/src/cmd/ssh/sshd/auth.c (revision 3984:10d407109a7a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate /*
25*3984Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: auth.c,v 1.45 2002/09/20 18:41:29 stevesk Exp $");
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #ifdef HAVE_LOGIN_H
350Sstevel@tonic-gate #include <login.h>
360Sstevel@tonic-gate #endif
370Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
380Sstevel@tonic-gate #include <shadow.h>
390Sstevel@tonic-gate #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef HAVE_LIBGEN_H
420Sstevel@tonic-gate #include <libgen.h>
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "xmalloc.h"
460Sstevel@tonic-gate #include "match.h"
470Sstevel@tonic-gate #include "groupaccess.h"
480Sstevel@tonic-gate #include "log.h"
490Sstevel@tonic-gate #include "servconf.h"
500Sstevel@tonic-gate #include "auth.h"
510Sstevel@tonic-gate #include "auth-options.h"
520Sstevel@tonic-gate #include "canohost.h"
530Sstevel@tonic-gate #include "buffer.h"
540Sstevel@tonic-gate #include "bufaux.h"
550Sstevel@tonic-gate #include "uidswap.h"
560Sstevel@tonic-gate #include "tildexpand.h"
570Sstevel@tonic-gate #include "misc.h"
580Sstevel@tonic-gate #include "bufaux.h"
590Sstevel@tonic-gate #include "packet.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef HAVE_BSM
620Sstevel@tonic-gate #include "bsmaudit.h"
630Sstevel@tonic-gate #include <bsm/adt.h>
640Sstevel@tonic-gate #endif /* HAVE_BSM */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* import */
670Sstevel@tonic-gate extern ServerOptions options;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* Debugging messages */
700Sstevel@tonic-gate Buffer auth_debug;
710Sstevel@tonic-gate int auth_debug_init;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Check if the user is allowed to log in via ssh. If user is listed
750Sstevel@tonic-gate  * in DenyUsers or one of user's groups is listed in DenyGroups, false
760Sstevel@tonic-gate  * will be returned. If AllowUsers isn't empty and user isn't listed
770Sstevel@tonic-gate  * there, or if AllowGroups isn't empty and one of user's groups isn't
780Sstevel@tonic-gate  * listed there, false will be returned.
790Sstevel@tonic-gate  * If the user's shell is not executable, false will be returned.
800Sstevel@tonic-gate  * Otherwise true is returned.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate int
830Sstevel@tonic-gate allowed_user(struct passwd * pw)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	struct stat st;
860Sstevel@tonic-gate 	const char *hostname = NULL, *ipaddr = NULL;
870Sstevel@tonic-gate 	char *shell;
880Sstevel@tonic-gate 	int i;
890Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
900Sstevel@tonic-gate 	char *loginmsg;
910Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
920Sstevel@tonic-gate #if !defined(USE_PAM) && defined(HAVE_SHADOW_H) && \
930Sstevel@tonic-gate 	!defined(DISABLE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
940Sstevel@tonic-gate 	struct spwd *spw;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
970Sstevel@tonic-gate 	if (!pw || !pw->pw_name)
980Sstevel@tonic-gate 		return 0;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	DAY		(24L * 60 * 60) /* 1 day in seconds */
1010Sstevel@tonic-gate 	spw = getspnam(pw->pw_name);
1020Sstevel@tonic-gate 	if (spw != NULL) {
1030Sstevel@tonic-gate 		time_t today = time(NULL) / DAY;
1040Sstevel@tonic-gate 		debug3("allowed_user: today %d sp_expire %d sp_lstchg %d"
1050Sstevel@tonic-gate 		    " sp_max %d", (int)today, (int)spw->sp_expire,
1060Sstevel@tonic-gate 		    (int)spw->sp_lstchg, (int)spw->sp_max);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		/*
1090Sstevel@tonic-gate 		 * We assume account and password expiration occurs the
1100Sstevel@tonic-gate 		 * day after the day specified.
1110Sstevel@tonic-gate 		 */
1120Sstevel@tonic-gate 		if (spw->sp_expire != -1 && today > spw->sp_expire) {
1130Sstevel@tonic-gate 			log("Account %.100s has expired", pw->pw_name);
1140Sstevel@tonic-gate 			return 0;
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		if (spw->sp_lstchg == 0) {
1180Sstevel@tonic-gate 			log("User %.100s password has expired (root forced)",
1190Sstevel@tonic-gate 			    pw->pw_name);
1200Sstevel@tonic-gate 			return 0;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		if (spw->sp_max != -1 &&
1240Sstevel@tonic-gate 		    today > spw->sp_lstchg + spw->sp_max) {
1250Sstevel@tonic-gate 			log("User %.100s password has expired (password aged)",
1260Sstevel@tonic-gate 			    pw->pw_name);
1270Sstevel@tonic-gate 			return 0;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate #else
1310Sstevel@tonic-gate 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
1320Sstevel@tonic-gate 	if (!pw || !pw->pw_name)
1330Sstevel@tonic-gate 		return 0;
1340Sstevel@tonic-gate #endif
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Get the shell from the password data.  An empty shell field is
1380Sstevel@tonic-gate 	 * legal, and means /bin/sh.
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* deny if shell does not exists or is not executable */
1430Sstevel@tonic-gate 	if (stat(shell, &st) != 0) {
1440Sstevel@tonic-gate 		log("User %.100s not allowed because shell %.100s does not exist",
1450Sstevel@tonic-gate 		    pw->pw_name, shell);
1460Sstevel@tonic-gate 		return 0;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	if (S_ISREG(st.st_mode) == 0 ||
1490Sstevel@tonic-gate 	    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
1500Sstevel@tonic-gate 		log("User %.100s not allowed because shell %.100s is not executable",
1510Sstevel@tonic-gate 		    pw->pw_name, shell);
1520Sstevel@tonic-gate 		return 0;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (options.num_deny_users > 0 || options.num_allow_users > 0) {
1560Sstevel@tonic-gate 		hostname = get_canonical_hostname(options.verify_reverse_mapping);
1570Sstevel@tonic-gate 		ipaddr = get_remote_ipaddr();
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* Return false if user is listed in DenyUsers */
1610Sstevel@tonic-gate 	if (options.num_deny_users > 0) {
1620Sstevel@tonic-gate 		for (i = 0; i < options.num_deny_users; i++)
1630Sstevel@tonic-gate 			if (match_user(pw->pw_name, hostname, ipaddr,
1640Sstevel@tonic-gate 			    options.deny_users[i])) {
1650Sstevel@tonic-gate 				log("User %.100s not allowed because listed in DenyUsers",
1660Sstevel@tonic-gate 				    pw->pw_name);
1670Sstevel@tonic-gate 				return 0;
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	/* Return false if AllowUsers isn't empty and user isn't listed there */
1710Sstevel@tonic-gate 	if (options.num_allow_users > 0) {
1720Sstevel@tonic-gate 		for (i = 0; i < options.num_allow_users; i++)
1730Sstevel@tonic-gate 			if (match_user(pw->pw_name, hostname, ipaddr,
1740Sstevel@tonic-gate 			    options.allow_users[i]))
1750Sstevel@tonic-gate 				break;
1760Sstevel@tonic-gate 		/* i < options.num_allow_users iff we break for loop */
1770Sstevel@tonic-gate 		if (i >= options.num_allow_users) {
1780Sstevel@tonic-gate 			log("User %.100s not allowed because not listed in AllowUsers",
1790Sstevel@tonic-gate 			    pw->pw_name);
1800Sstevel@tonic-gate 			return 0;
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1840Sstevel@tonic-gate 		/* Get the user's group access list (primary and supplementary) */
1850Sstevel@tonic-gate 		if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1860Sstevel@tonic-gate 			log("User %.100s not allowed because not in any group",
1870Sstevel@tonic-gate 			    pw->pw_name);
1880Sstevel@tonic-gate 			return 0;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		/* Return false if one of user's groups is listed in DenyGroups */
1920Sstevel@tonic-gate 		if (options.num_deny_groups > 0)
1930Sstevel@tonic-gate 			if (ga_match(options.deny_groups,
1940Sstevel@tonic-gate 			    options.num_deny_groups)) {
1950Sstevel@tonic-gate 				ga_free();
1960Sstevel@tonic-gate 				log("User %.100s not allowed because a group is listed in DenyGroups",
1970Sstevel@tonic-gate 				    pw->pw_name);
1980Sstevel@tonic-gate 				return 0;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Return false if AllowGroups isn't empty and one of user's groups
2020Sstevel@tonic-gate 		 * isn't listed there
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		if (options.num_allow_groups > 0)
2050Sstevel@tonic-gate 			if (!ga_match(options.allow_groups,
2060Sstevel@tonic-gate 			    options.num_allow_groups)) {
2070Sstevel@tonic-gate 				ga_free();
2080Sstevel@tonic-gate 				log("User %.100s not allowed because none of user's groups are listed in AllowGroups",
2090Sstevel@tonic-gate 				    pw->pw_name);
2100Sstevel@tonic-gate 				return 0;
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		ga_free();
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
2160Sstevel@tonic-gate 	if (loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &loginmsg) != 0) {
2170Sstevel@tonic-gate 		if (loginmsg && *loginmsg) {
2180Sstevel@tonic-gate 			/* Remove embedded newlines (if any) */
2190Sstevel@tonic-gate 			char *p;
2200Sstevel@tonic-gate 			for (p = loginmsg; *p; p++) {
2210Sstevel@tonic-gate 				if (*p == '\n')
2220Sstevel@tonic-gate 					*p = ' ';
2230Sstevel@tonic-gate 			}
2240Sstevel@tonic-gate 			/* Remove trailing newline */
2250Sstevel@tonic-gate 			*--p = '\0';
2260Sstevel@tonic-gate 			log("Login restricted for %s: %.100s", pw->pw_name, loginmsg);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		return 0;
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* We found no reason not to let this user try to log on... */
2330Sstevel@tonic-gate 	return 1;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate Authctxt *
2370Sstevel@tonic-gate authctxt_new(void)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	Authctxt *authctxt = xmalloc(sizeof(*authctxt));
2400Sstevel@tonic-gate 	memset(authctxt, 0, sizeof(*authctxt));
2410Sstevel@tonic-gate 	return authctxt;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate void
2450Sstevel@tonic-gate auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	void (*authlog) (const char *fmt,...) = verbose;
2480Sstevel@tonic-gate 	char *authmsg, *user_str;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (authctxt == NULL)
2510Sstevel@tonic-gate 		fatal("%s: INTERNAL ERROR", __func__);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* Raise logging level */
2540Sstevel@tonic-gate 	if (authenticated == 1 || !authctxt->valid)
2550Sstevel@tonic-gate 		authlog = log;
2560Sstevel@tonic-gate 	else if (authctxt->failures >= AUTH_FAIL_LOG ||
2570Sstevel@tonic-gate 	    authctxt->attempt >= options.max_auth_tries_log ||
2580Sstevel@tonic-gate 	    authctxt->init_attempt >= options.max_init_auth_tries_log)
2590Sstevel@tonic-gate 		authlog = notice;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (authctxt->method) {
2620Sstevel@tonic-gate 		authmsg = "Failed";
2630Sstevel@tonic-gate 		if (authctxt->method->postponed)
2640Sstevel@tonic-gate 			authmsg = "Postponed"; /* shouldn't happen */
2650Sstevel@tonic-gate 		if (authctxt->method->abandoned)
2660Sstevel@tonic-gate 			authmsg = "Abandoned";
2670Sstevel@tonic-gate 		if (authctxt->method->authenticated) {
2680Sstevel@tonic-gate 			if (userauth_check_partial_failure(authctxt))
2690Sstevel@tonic-gate 				authmsg = "Partially accepted";
2700Sstevel@tonic-gate 			else
2710Sstevel@tonic-gate 				authmsg = "Accepted";
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		else
2740Sstevel@tonic-gate 			authmsg = "Failed";
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	else {
2770Sstevel@tonic-gate 		authmsg = authenticated ? "Accepted" : "Failed";
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (authctxt->user == NULL || *authctxt->user == '\0')
2810Sstevel@tonic-gate 		user_str = "<implicit>";
2820Sstevel@tonic-gate 	else if (!authctxt->valid)
2830Sstevel@tonic-gate 		user_str =  "<invalid username>";
2840Sstevel@tonic-gate 	else
2850Sstevel@tonic-gate 		user_str =  authctxt->user;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	authlog("%s %s for %s from %.200s port %d%s",
2880Sstevel@tonic-gate 	    authmsg,
2890Sstevel@tonic-gate 	    (method != NULL) ? method : "<unknown authentication method>",
2900Sstevel@tonic-gate 	    user_str,
2910Sstevel@tonic-gate 	    get_remote_ipaddr(),
2920Sstevel@tonic-gate 	    get_remote_port(),
2930Sstevel@tonic-gate 	    info);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate #ifdef WITH_AIXAUTHENTICATE
2960Sstevel@tonic-gate 	if (authenticated == 0 && strcmp(method, "password") == 0)
2970Sstevel@tonic-gate 	    loginfailed(authctxt->user,
2980Sstevel@tonic-gate 		get_canonical_hostname(options.verify_reverse_mapping),
2990Sstevel@tonic-gate 		"ssh");
3000Sstevel@tonic-gate #endif /* WITH_AIXAUTHENTICATE */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate #ifdef HAVE_BSM
3050Sstevel@tonic-gate void
3060Sstevel@tonic-gate audit_failed_login_cleanup(void *ctxt)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	Authctxt *authctxt = (Authctxt *)ctxt;
3090Sstevel@tonic-gate 	adt_session_data_t *ah;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (authctxt == NULL)
3120Sstevel@tonic-gate 		/* Internal error */
3130Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, PAM_ABORT);
3140Sstevel@tonic-gate 	else if (authctxt->pam == NULL && authctxt->pam_retval != PAM_SUCCESS)
3150Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, authctxt->pam_retval);
3160Sstevel@tonic-gate 	else if (authctxt->pam == NULL && authctxt->pam_retval == PAM_SUCCESS)
3170Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, PAM_PERM_DENIED);
3180Sstevel@tonic-gate 	else if (!authctxt->valid)
3190Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN);
3200Sstevel@tonic-gate 	else
3210Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt,
3220Sstevel@tonic-gate 			    PAM_PERM_DENIED));
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate #endif /* HAVE_BSM */
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * Check whether root logins are disallowed.
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate int
3300Sstevel@tonic-gate auth_root_allowed(char *method)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	switch (options.permit_root_login) {
3330Sstevel@tonic-gate 	case PERMIT_YES:
3340Sstevel@tonic-gate 		return 1;
3350Sstevel@tonic-gate 		break;
3360Sstevel@tonic-gate 	case PERMIT_NO_PASSWD:
3370Sstevel@tonic-gate 		if (strcmp(method, "password") != 0 &&
3380Sstevel@tonic-gate 		    strcmp(method, "keyboard-interactive") != 0)
3390Sstevel@tonic-gate 			return 1;
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 	case PERMIT_FORCED_ONLY:
3420Sstevel@tonic-gate 		if (forced_command) {
3430Sstevel@tonic-gate 			log("Root login accepted for forced command.");
3440Sstevel@tonic-gate 			return 1;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 		break;
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate 	log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
3490Sstevel@tonic-gate 	return 0;
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * Given a template and a passwd structure, build a filename
3550Sstevel@tonic-gate  * by substituting % tokenised options. Currently, %% becomes '%',
3560Sstevel@tonic-gate  * %h becomes the home directory and %u the username.
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  * This returns a buffer allocated by xmalloc.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate char *
3610Sstevel@tonic-gate expand_filename(const char *filename, struct passwd *pw)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	Buffer buffer;
3640Sstevel@tonic-gate 	char *file;
3650Sstevel@tonic-gate 	const char *cp;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (pw == 0)
3680Sstevel@tonic-gate 		return NULL; /* shouldn't happen */
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * Build the filename string in the buffer by making the appropriate
3710Sstevel@tonic-gate 	 * substitutions to the given file name.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	buffer_init(&buffer);
3740Sstevel@tonic-gate 	for (cp = filename; *cp; cp++) {
3750Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == '%') {
3760Sstevel@tonic-gate 			buffer_append(&buffer, "%", 1);
3770Sstevel@tonic-gate 			cp++;
3780Sstevel@tonic-gate 			continue;
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == 'h') {
3810Sstevel@tonic-gate 			buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
3820Sstevel@tonic-gate 			cp++;
3830Sstevel@tonic-gate 			continue;
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == 'u') {
3860Sstevel@tonic-gate 			buffer_append(&buffer, pw->pw_name,
3870Sstevel@tonic-gate 			    strlen(pw->pw_name));
3880Sstevel@tonic-gate 			cp++;
3890Sstevel@tonic-gate 			continue;
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 		buffer_append(&buffer, cp, 1);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 	buffer_append(&buffer, "\0", 1);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Ensure that filename starts anchored. If not, be backward
3970Sstevel@tonic-gate 	 * compatible and prepend the '%h/'
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	file = xmalloc(MAXPATHLEN);
4000Sstevel@tonic-gate 	cp = buffer_ptr(&buffer);
4010Sstevel@tonic-gate 	if (*cp != '/')
4020Sstevel@tonic-gate 		snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
4030Sstevel@tonic-gate 	else
4040Sstevel@tonic-gate 		strlcpy(file, cp, MAXPATHLEN);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	buffer_free(&buffer);
4070Sstevel@tonic-gate 	return file;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate char *
4110Sstevel@tonic-gate authorized_keys_file(struct passwd *pw)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	return expand_filename(options.authorized_keys_file, pw);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate char *
4170Sstevel@tonic-gate authorized_keys_file2(struct passwd *pw)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	return expand_filename(options.authorized_keys_file2, pw);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /* return ok if key exists in sysfile or userfile */
4230Sstevel@tonic-gate HostStatus
4240Sstevel@tonic-gate check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
4250Sstevel@tonic-gate     const char *sysfile, const char *userfile)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	Key *found;
4280Sstevel@tonic-gate 	char *user_hostfile;
4290Sstevel@tonic-gate 	struct stat st;
4300Sstevel@tonic-gate 	HostStatus host_status;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/* Check if we know the host and its host key. */
4330Sstevel@tonic-gate 	found = key_new(key->type);
4340Sstevel@tonic-gate 	host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (host_status != HOST_OK && userfile != NULL) {
4370Sstevel@tonic-gate 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
4380Sstevel@tonic-gate 		if (options.strict_modes &&
4390Sstevel@tonic-gate 		    (stat(user_hostfile, &st) == 0) &&
4400Sstevel@tonic-gate 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
4410Sstevel@tonic-gate 		    (st.st_mode & 022) != 0)) {
4420Sstevel@tonic-gate 			log("Authentication refused for %.100s: "
4430Sstevel@tonic-gate 			    "bad owner or modes for %.200s",
4440Sstevel@tonic-gate 			    pw->pw_name, user_hostfile);
4450Sstevel@tonic-gate 		} else {
4460Sstevel@tonic-gate 			temporarily_use_uid(pw);
4470Sstevel@tonic-gate 			host_status = check_host_in_hostfile(user_hostfile,
4480Sstevel@tonic-gate 			    host, key, found, NULL);
4490Sstevel@tonic-gate 			restore_uid();
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		xfree(user_hostfile);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	key_free(found);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
4560Sstevel@tonic-gate 	    "ok" : "not found", host);
4570Sstevel@tonic-gate 	return host_status;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate  * Check a given file for security. This is defined as all components
4630Sstevel@tonic-gate  * of the path to the file must be owned by either the owner of
4640Sstevel@tonic-gate  * of the file or root and no directories must be group or world writable.
4650Sstevel@tonic-gate  *
4660Sstevel@tonic-gate  * XXX Should any specific check be done for sym links ?
4670Sstevel@tonic-gate  *
4680Sstevel@tonic-gate  * Takes an open file descriptor, the file name, a uid and and
4690Sstevel@tonic-gate  * error buffer plus max size as arguments.
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
4720Sstevel@tonic-gate  */
4730Sstevel@tonic-gate int
4740Sstevel@tonic-gate secure_filename(FILE *f, const char *file, struct passwd *pw,
4750Sstevel@tonic-gate     char *err, size_t errlen)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	uid_t uid;
4780Sstevel@tonic-gate 	char buf[MAXPATHLEN], homedir[MAXPATHLEN];
4790Sstevel@tonic-gate 	char *cp;
480*3984Sjp161948 	int comparehome = 0;
4810Sstevel@tonic-gate 	struct stat st;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if (pw == NULL)
4840Sstevel@tonic-gate 		return 0;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	uid = pw->pw_uid;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (realpath(file, buf) == NULL) {
4890Sstevel@tonic-gate 		snprintf(err, errlen, "realpath %s failed: %s", file,
4900Sstevel@tonic-gate 		    strerror(errno));
4910Sstevel@tonic-gate 		return -1;
4920Sstevel@tonic-gate 	}
493*3984Sjp161948 
494*3984Sjp161948 	/*
495*3984Sjp161948 	 * A user is not required to have all the files that are subject to
496*3984Sjp161948 	 * the strict mode checking in his/her home directory. If the
497*3984Sjp161948 	 * directory is not present at the moment, which might be the case if
498*3984Sjp161948 	 * the directory is not mounted until the user is authenticated, do
499*3984Sjp161948 	 * not perform the home directory check below.
500*3984Sjp161948 	 */
501*3984Sjp161948 	if (realpath(pw->pw_dir, homedir) != NULL)
502*3984Sjp161948 		comparehome = 1;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/* check the open file to avoid races */
5050Sstevel@tonic-gate 	if (fstat(fileno(f), &st) < 0 ||
5060Sstevel@tonic-gate 	    (st.st_uid != 0 && st.st_uid != uid) ||
5070Sstevel@tonic-gate 	    (st.st_mode & 022) != 0) {
5080Sstevel@tonic-gate 		snprintf(err, errlen, "bad ownership or modes for file %s",
5090Sstevel@tonic-gate 		    buf);
5100Sstevel@tonic-gate 		return -1;
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/* for each component of the canonical path, walking upwards */
5140Sstevel@tonic-gate 	for (;;) {
5150Sstevel@tonic-gate 		if ((cp = dirname(buf)) == NULL) {
5160Sstevel@tonic-gate 			snprintf(err, errlen, "dirname() failed");
5170Sstevel@tonic-gate 			return -1;
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		strlcpy(buf, cp, sizeof(buf));
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		debug3("secure_filename: checking '%s'", buf);
5220Sstevel@tonic-gate 		if (stat(buf, &st) < 0 ||
5230Sstevel@tonic-gate 		    (st.st_uid != 0 && st.st_uid != uid) ||
5240Sstevel@tonic-gate 		    (st.st_mode & 022) != 0) {
5250Sstevel@tonic-gate 			snprintf(err, errlen,
5260Sstevel@tonic-gate 			    "bad ownership or modes for directory %s", buf);
5270Sstevel@tonic-gate 			return -1;
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 
530*3984Sjp161948 		/* If we passed the homedir then we can stop. */
531*3984Sjp161948 		if (comparehome && strcmp(homedir, buf) == 0) {
5320Sstevel@tonic-gate 			debug3("secure_filename: terminating check at '%s'",
5330Sstevel@tonic-gate 			    buf);
5340Sstevel@tonic-gate 			break;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 		/*
5370Sstevel@tonic-gate 		 * dirname should always complete with a "/" path,
5380Sstevel@tonic-gate 		 * but we can be paranoid and check for "." too
5390Sstevel@tonic-gate 		 */
5400Sstevel@tonic-gate 		if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
5410Sstevel@tonic-gate 			break;
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 	return 0;
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate struct passwd *
5470Sstevel@tonic-gate getpwnamallow(const char *user)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
5500Sstevel@tonic-gate 	extern login_cap_t *lc;
5510Sstevel@tonic-gate #ifdef BSD_AUTH
5520Sstevel@tonic-gate 	auth_session_t *as;
5530Sstevel@tonic-gate #endif
5540Sstevel@tonic-gate #endif
5550Sstevel@tonic-gate 	struct passwd *pw;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (user == NULL || *user == '\0')
5580Sstevel@tonic-gate 		return (NULL); /* implicit user, will be set later */
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	pw = getpwnam(user);
5610Sstevel@tonic-gate 	if (pw == NULL) {
5620Sstevel@tonic-gate 		log("Illegal user %.100s from %.100s",
5630Sstevel@tonic-gate 		    user, get_remote_ipaddr());
5640Sstevel@tonic-gate 		return (NULL);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 	if (!allowed_user(pw))
5670Sstevel@tonic-gate 		return (NULL);
5680Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
5690Sstevel@tonic-gate 	if ((lc = login_getclass(pw->pw_class)) == NULL) {
5700Sstevel@tonic-gate 		debug("unable to get login class: %s", user);
5710Sstevel@tonic-gate 		return (NULL);
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate #ifdef BSD_AUTH
5740Sstevel@tonic-gate 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
5750Sstevel@tonic-gate 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
5760Sstevel@tonic-gate 		debug("Approval failure for %s", user);
5770Sstevel@tonic-gate 		pw = NULL;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 	if (as != NULL)
5800Sstevel@tonic-gate 		auth_close(as);
5810Sstevel@tonic-gate #endif
5820Sstevel@tonic-gate #endif
5830Sstevel@tonic-gate 	if (pw != NULL)
5840Sstevel@tonic-gate 		return (pwcopy(pw));
5850Sstevel@tonic-gate 	return (NULL);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate void
5890Sstevel@tonic-gate auth_debug_add(const char *fmt,...)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	char buf[1024];
5920Sstevel@tonic-gate 	va_list args;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (!auth_debug_init)
5950Sstevel@tonic-gate 		return;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	va_start(args, fmt);
5980Sstevel@tonic-gate 	vsnprintf(buf, sizeof(buf), fmt, args);
5990Sstevel@tonic-gate 	va_end(args);
6000Sstevel@tonic-gate 	buffer_put_cstring(&auth_debug, buf);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate void
6040Sstevel@tonic-gate auth_debug_send(void)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	char *msg;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (!auth_debug_init)
6090Sstevel@tonic-gate 		return;
6100Sstevel@tonic-gate 	while (buffer_len(&auth_debug)) {
6110Sstevel@tonic-gate 		msg = buffer_get_string(&auth_debug, NULL);
6120Sstevel@tonic-gate 		packet_send_debug("%s", msg);
6130Sstevel@tonic-gate 		xfree(msg);
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate void
6180Sstevel@tonic-gate auth_debug_reset(void)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	if (auth_debug_init)
6210Sstevel@tonic-gate 		buffer_clear(&auth_debug);
6220Sstevel@tonic-gate 	else {
6230Sstevel@tonic-gate 		buffer_init(&auth_debug);
6240Sstevel@tonic-gate 		auth_debug_init = 1;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate }
627