xref: /onnv-gate/usr/src/cmd/ssh/sshd/auth.c (revision 11251:ff36d9f84a57)
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 /*
2511044SHuie-Ying.Lee@Sun.COM  * Copyright 2009 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 #ifdef HAVE_LOGIN_H
330Sstevel@tonic-gate #include <login.h>
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate #if defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW)
360Sstevel@tonic-gate #include <shadow.h>
370Sstevel@tonic-gate #endif /* defined(HAVE_SHADOW_H) && !defined(DISABLE_SHADOW) */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifdef HAVE_LIBGEN_H
400Sstevel@tonic-gate #include <libgen.h>
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "xmalloc.h"
440Sstevel@tonic-gate #include "match.h"
450Sstevel@tonic-gate #include "groupaccess.h"
460Sstevel@tonic-gate #include "log.h"
4711044SHuie-Ying.Lee@Sun.COM #include "buffer.h"
480Sstevel@tonic-gate #include "servconf.h"
490Sstevel@tonic-gate #include "auth.h"
500Sstevel@tonic-gate #include "auth-options.h"
510Sstevel@tonic-gate #include "canohost.h"
520Sstevel@tonic-gate #include "bufaux.h"
530Sstevel@tonic-gate #include "uidswap.h"
540Sstevel@tonic-gate #include "tildexpand.h"
550Sstevel@tonic-gate #include "misc.h"
560Sstevel@tonic-gate #include "bufaux.h"
570Sstevel@tonic-gate #include "packet.h"
58*11251SErik.Trauschke@Sun.COM #include "channels.h"
59*11251SErik.Trauschke@Sun.COM #include "session.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
allowed_user(struct passwd * pw)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 *
authctxt_new(void)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
auth_log(Authctxt * authctxt,int authenticated,char * method,char * info)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
audit_failed_login_cleanup(void * ctxt)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 
3118206SBrent.Paulson@Sun.COM 	/*
3128206SBrent.Paulson@Sun.COM 	 * This table lists the different variable combinations evaluated and
3138206SBrent.Paulson@Sun.COM 	 * what the resulting PAM return value is.  As the table shows
3148206SBrent.Paulson@Sun.COM 	 * authctxt and authctxt->valid need to be checked before either of
3158206SBrent.Paulson@Sun.COM 	 * the authctxt->pam* variables.
3168206SBrent.Paulson@Sun.COM 	 *
3178206SBrent.Paulson@Sun.COM 	 *           authctxt->                     authctxt->
3188206SBrent.Paulson@Sun.COM 	 * authctxt    valid      authctxt->pam       pam_retval   PAM rval
3198206SBrent.Paulson@Sun.COM 	 * --------  ----------   -------------     ------------   --------
3208206SBrent.Paulson@Sun.COM 	 *   NULL      ANY             ANY              ANY        PAM_ABORT
3218206SBrent.Paulson@Sun.COM 	 *    OK      zero (0)         ANY              ANY     PAM_USER_UNKNOWN
3228206SBrent.Paulson@Sun.COM 	 *    OK       one (1)         NULL         PAM_SUCCESS  PAM_PERM_DENIED
3238206SBrent.Paulson@Sun.COM 	 *    OK       one (1)         NULL        !PAM_SUCCESS   authctxt->
3248206SBrent.Paulson@Sun.COM 	 *                                                          pam_retval
3258206SBrent.Paulson@Sun.COM 	 *    OK       one (1)         VALID            ANY       authctxt->
3268206SBrent.Paulson@Sun.COM 	 *                                                        pam_retval (+)
3278206SBrent.Paulson@Sun.COM 	 * (+) If not set then default to PAM_PERM_DENIED
3288206SBrent.Paulson@Sun.COM 	 */
3298206SBrent.Paulson@Sun.COM 
3308206SBrent.Paulson@Sun.COM 	if (authctxt == NULL) {
3310Sstevel@tonic-gate 		/* Internal error */
3328064SBrent.Paulson@Sun.COM 		audit_sshd_login_failure(&ah, PAM_ABORT, NULL);
3338206SBrent.Paulson@Sun.COM 		return;
3348206SBrent.Paulson@Sun.COM 	}
3358206SBrent.Paulson@Sun.COM 
3368206SBrent.Paulson@Sun.COM 	if (authctxt->valid == 0) {
3378064SBrent.Paulson@Sun.COM 		audit_sshd_login_failure(&ah, PAM_USER_UNKNOWN, NULL);
3388206SBrent.Paulson@Sun.COM 	} else if (authctxt->pam == NULL) {
3398206SBrent.Paulson@Sun.COM 		if (authctxt->pam_retval == PAM_SUCCESS) {
3408206SBrent.Paulson@Sun.COM 			audit_sshd_login_failure(&ah, PAM_PERM_DENIED,
3418206SBrent.Paulson@Sun.COM 			    authctxt->user);
3428206SBrent.Paulson@Sun.COM 		} else {
3438206SBrent.Paulson@Sun.COM 			audit_sshd_login_failure(&ah, authctxt->pam_retval,
3448206SBrent.Paulson@Sun.COM 			    authctxt->user);
3458206SBrent.Paulson@Sun.COM 		}
3468206SBrent.Paulson@Sun.COM 	} else {
3470Sstevel@tonic-gate 		audit_sshd_login_failure(&ah, AUTHPAM_ERROR(authctxt,
3488064SBrent.Paulson@Sun.COM 		    PAM_PERM_DENIED), authctxt->user);
3498206SBrent.Paulson@Sun.COM 	}
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate #endif /* HAVE_BSM */
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * Check whether root logins are disallowed.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate int
auth_root_allowed(char * method)3570Sstevel@tonic-gate auth_root_allowed(char *method)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	switch (options.permit_root_login) {
3600Sstevel@tonic-gate 	case PERMIT_YES:
3610Sstevel@tonic-gate 		return 1;
3620Sstevel@tonic-gate 		break;
3630Sstevel@tonic-gate 	case PERMIT_NO_PASSWD:
3640Sstevel@tonic-gate 		if (strcmp(method, "password") != 0 &&
3650Sstevel@tonic-gate 		    strcmp(method, "keyboard-interactive") != 0)
3660Sstevel@tonic-gate 			return 1;
3670Sstevel@tonic-gate 		break;
3680Sstevel@tonic-gate 	case PERMIT_FORCED_ONLY:
3690Sstevel@tonic-gate 		if (forced_command) {
3700Sstevel@tonic-gate 			log("Root login accepted for forced command.");
3710Sstevel@tonic-gate 			return 1;
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 		break;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 	log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
3760Sstevel@tonic-gate 	return 0;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * Given a template and a passwd structure, build a filename
3820Sstevel@tonic-gate  * by substituting % tokenised options. Currently, %% becomes '%',
3830Sstevel@tonic-gate  * %h becomes the home directory and %u the username.
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  * This returns a buffer allocated by xmalloc.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate char *
expand_filename(const char * filename,struct passwd * pw)3880Sstevel@tonic-gate expand_filename(const char *filename, struct passwd *pw)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	Buffer buffer;
3910Sstevel@tonic-gate 	char *file;
3920Sstevel@tonic-gate 	const char *cp;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if (pw == 0)
3950Sstevel@tonic-gate 		return NULL; /* shouldn't happen */
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * Build the filename string in the buffer by making the appropriate
3980Sstevel@tonic-gate 	 * substitutions to the given file name.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	buffer_init(&buffer);
4010Sstevel@tonic-gate 	for (cp = filename; *cp; cp++) {
4020Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == '%') {
4030Sstevel@tonic-gate 			buffer_append(&buffer, "%", 1);
4040Sstevel@tonic-gate 			cp++;
4050Sstevel@tonic-gate 			continue;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == 'h') {
4080Sstevel@tonic-gate 			buffer_append(&buffer, pw->pw_dir, strlen(pw->pw_dir));
4090Sstevel@tonic-gate 			cp++;
4100Sstevel@tonic-gate 			continue;
4110Sstevel@tonic-gate 		}
4120Sstevel@tonic-gate 		if (cp[0] == '%' && cp[1] == 'u') {
4130Sstevel@tonic-gate 			buffer_append(&buffer, pw->pw_name,
4140Sstevel@tonic-gate 			    strlen(pw->pw_name));
4150Sstevel@tonic-gate 			cp++;
4160Sstevel@tonic-gate 			continue;
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 		buffer_append(&buffer, cp, 1);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	buffer_append(&buffer, "\0", 1);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * Ensure that filename starts anchored. If not, be backward
4240Sstevel@tonic-gate 	 * compatible and prepend the '%h/'
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	file = xmalloc(MAXPATHLEN);
4270Sstevel@tonic-gate 	cp = buffer_ptr(&buffer);
4280Sstevel@tonic-gate 	if (*cp != '/')
4290Sstevel@tonic-gate 		snprintf(file, MAXPATHLEN, "%s/%s", pw->pw_dir, cp);
4300Sstevel@tonic-gate 	else
4310Sstevel@tonic-gate 		strlcpy(file, cp, MAXPATHLEN);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	buffer_free(&buffer);
4340Sstevel@tonic-gate 	return file;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate char *
authorized_keys_file(struct passwd * pw)4380Sstevel@tonic-gate authorized_keys_file(struct passwd *pw)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	return expand_filename(options.authorized_keys_file, pw);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate char *
authorized_keys_file2(struct passwd * pw)4440Sstevel@tonic-gate authorized_keys_file2(struct passwd *pw)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	return expand_filename(options.authorized_keys_file2, pw);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /* return ok if key exists in sysfile or userfile */
4500Sstevel@tonic-gate HostStatus
check_key_in_hostfiles(struct passwd * pw,Key * key,const char * host,const char * sysfile,const char * userfile)4510Sstevel@tonic-gate check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
4520Sstevel@tonic-gate     const char *sysfile, const char *userfile)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	Key *found;
4550Sstevel@tonic-gate 	char *user_hostfile;
4560Sstevel@tonic-gate 	struct stat st;
4570Sstevel@tonic-gate 	HostStatus host_status;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/* Check if we know the host and its host key. */
4600Sstevel@tonic-gate 	found = key_new(key->type);
4610Sstevel@tonic-gate 	host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if (host_status != HOST_OK && userfile != NULL) {
4640Sstevel@tonic-gate 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
4650Sstevel@tonic-gate 		if (options.strict_modes &&
4660Sstevel@tonic-gate 		    (stat(user_hostfile, &st) == 0) &&
4670Sstevel@tonic-gate 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
4680Sstevel@tonic-gate 		    (st.st_mode & 022) != 0)) {
4690Sstevel@tonic-gate 			log("Authentication refused for %.100s: "
4700Sstevel@tonic-gate 			    "bad owner or modes for %.200s",
4710Sstevel@tonic-gate 			    pw->pw_name, user_hostfile);
4720Sstevel@tonic-gate 		} else {
4730Sstevel@tonic-gate 			temporarily_use_uid(pw);
4740Sstevel@tonic-gate 			host_status = check_host_in_hostfile(user_hostfile,
4750Sstevel@tonic-gate 			    host, key, found, NULL);
4760Sstevel@tonic-gate 			restore_uid();
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 		xfree(user_hostfile);
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 	key_free(found);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
4830Sstevel@tonic-gate 	    "ok" : "not found", host);
4840Sstevel@tonic-gate 	return host_status;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Check a given file for security. This is defined as all components
4900Sstevel@tonic-gate  * of the path to the file must be owned by either the owner of
4910Sstevel@tonic-gate  * of the file or root and no directories must be group or world writable.
4920Sstevel@tonic-gate  *
4930Sstevel@tonic-gate  * XXX Should any specific check be done for sym links ?
4940Sstevel@tonic-gate  *
4950Sstevel@tonic-gate  * Takes an open file descriptor, the file name, a uid and and
4960Sstevel@tonic-gate  * error buffer plus max size as arguments.
4970Sstevel@tonic-gate  *
4980Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate int
secure_filename(FILE * f,const char * file,struct passwd * pw,char * err,size_t errlen)5010Sstevel@tonic-gate secure_filename(FILE *f, const char *file, struct passwd *pw,
5020Sstevel@tonic-gate     char *err, size_t errlen)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	uid_t uid;
5050Sstevel@tonic-gate 	char buf[MAXPATHLEN], homedir[MAXPATHLEN];
5060Sstevel@tonic-gate 	char *cp;
5073984Sjp161948 	int comparehome = 0;
5080Sstevel@tonic-gate 	struct stat st;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (pw == NULL)
5110Sstevel@tonic-gate 		return 0;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	uid = pw->pw_uid;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (realpath(file, buf) == NULL) {
5160Sstevel@tonic-gate 		snprintf(err, errlen, "realpath %s failed: %s", file,
5170Sstevel@tonic-gate 		    strerror(errno));
5180Sstevel@tonic-gate 		return -1;
5190Sstevel@tonic-gate 	}
5203984Sjp161948 
5213984Sjp161948 	/*
5223984Sjp161948 	 * A user is not required to have all the files that are subject to
5233984Sjp161948 	 * the strict mode checking in his/her home directory. If the
5243984Sjp161948 	 * directory is not present at the moment, which might be the case if
5253984Sjp161948 	 * the directory is not mounted until the user is authenticated, do
5263984Sjp161948 	 * not perform the home directory check below.
5273984Sjp161948 	 */
5283984Sjp161948 	if (realpath(pw->pw_dir, homedir) != NULL)
5293984Sjp161948 		comparehome = 1;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/* check the open file to avoid races */
5320Sstevel@tonic-gate 	if (fstat(fileno(f), &st) < 0 ||
5330Sstevel@tonic-gate 	    (st.st_uid != 0 && st.st_uid != uid) ||
5340Sstevel@tonic-gate 	    (st.st_mode & 022) != 0) {
5350Sstevel@tonic-gate 		snprintf(err, errlen, "bad ownership or modes for file %s",
5360Sstevel@tonic-gate 		    buf);
5370Sstevel@tonic-gate 		return -1;
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* for each component of the canonical path, walking upwards */
5410Sstevel@tonic-gate 	for (;;) {
5420Sstevel@tonic-gate 		if ((cp = dirname(buf)) == NULL) {
5430Sstevel@tonic-gate 			snprintf(err, errlen, "dirname() failed");
5440Sstevel@tonic-gate 			return -1;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 		strlcpy(buf, cp, sizeof(buf));
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		debug3("secure_filename: checking '%s'", buf);
5490Sstevel@tonic-gate 		if (stat(buf, &st) < 0 ||
5500Sstevel@tonic-gate 		    (st.st_uid != 0 && st.st_uid != uid) ||
5510Sstevel@tonic-gate 		    (st.st_mode & 022) != 0) {
5520Sstevel@tonic-gate 			snprintf(err, errlen,
5530Sstevel@tonic-gate 			    "bad ownership or modes for directory %s", buf);
5540Sstevel@tonic-gate 			return -1;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 
5573984Sjp161948 		/* If we passed the homedir then we can stop. */
5583984Sjp161948 		if (comparehome && strcmp(homedir, buf) == 0) {
5590Sstevel@tonic-gate 			debug3("secure_filename: terminating check at '%s'",
5600Sstevel@tonic-gate 			    buf);
5610Sstevel@tonic-gate 			break;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * dirname should always complete with a "/" path,
5650Sstevel@tonic-gate 		 * but we can be paranoid and check for "." too
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 		if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
5680Sstevel@tonic-gate 			break;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	return 0;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate struct passwd *
getpwnamallow(const char * user)5740Sstevel@tonic-gate getpwnamallow(const char *user)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
5770Sstevel@tonic-gate 	extern login_cap_t *lc;
5780Sstevel@tonic-gate #ifdef BSD_AUTH
5790Sstevel@tonic-gate 	auth_session_t *as;
5800Sstevel@tonic-gate #endif
5810Sstevel@tonic-gate #endif
5820Sstevel@tonic-gate 	struct passwd *pw;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	if (user == NULL || *user == '\0')
5850Sstevel@tonic-gate 		return (NULL); /* implicit user, will be set later */
5860Sstevel@tonic-gate 
58711044SHuie-Ying.Lee@Sun.COM 	parse_server_match_config(&options, user,
58811044SHuie-Ying.Lee@Sun.COM 	    get_canonical_hostname(options.verify_reverse_mapping), get_remote_ipaddr());
58911044SHuie-Ying.Lee@Sun.COM 
5900Sstevel@tonic-gate 	pw = getpwnam(user);
5910Sstevel@tonic-gate 	if (pw == NULL) {
5920Sstevel@tonic-gate 		log("Illegal user %.100s from %.100s",
5930Sstevel@tonic-gate 		    user, get_remote_ipaddr());
5940Sstevel@tonic-gate 		return (NULL);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	if (!allowed_user(pw))
5970Sstevel@tonic-gate 		return (NULL);
5980Sstevel@tonic-gate #ifdef HAVE_LOGIN_CAP
5990Sstevel@tonic-gate 	if ((lc = login_getclass(pw->pw_class)) == NULL) {
6000Sstevel@tonic-gate 		debug("unable to get login class: %s", user);
6010Sstevel@tonic-gate 		return (NULL);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate #ifdef BSD_AUTH
6040Sstevel@tonic-gate 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
6050Sstevel@tonic-gate 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
6060Sstevel@tonic-gate 		debug("Approval failure for %s", user);
6070Sstevel@tonic-gate 		pw = NULL;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	if (as != NULL)
6100Sstevel@tonic-gate 		auth_close(as);
6110Sstevel@tonic-gate #endif
6120Sstevel@tonic-gate #endif
6130Sstevel@tonic-gate 	if (pw != NULL)
6140Sstevel@tonic-gate 		return (pwcopy(pw));
6150Sstevel@tonic-gate 	return (NULL);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
618*11251SErik.Trauschke@Sun.COM 
619*11251SErik.Trauschke@Sun.COM /*
620*11251SErik.Trauschke@Sun.COM  * The fatal_cleanup method to kill the hook. Since hook has been put into
621*11251SErik.Trauschke@Sun.COM  * new process group all descendants will be killed as well.
622*11251SErik.Trauschke@Sun.COM  */
623*11251SErik.Trauschke@Sun.COM static void
kill_hook(void * arg)624*11251SErik.Trauschke@Sun.COM kill_hook(void *arg)
625*11251SErik.Trauschke@Sun.COM {
626*11251SErik.Trauschke@Sun.COM 	pid_t pid;
627*11251SErik.Trauschke@Sun.COM 
628*11251SErik.Trauschke@Sun.COM         pid = *(pid_t*)arg;
629*11251SErik.Trauschke@Sun.COM 	debug("killing hook and all it's children, process group: %ld", pid);
630*11251SErik.Trauschke@Sun.COM 	xfree(arg);
631*11251SErik.Trauschke@Sun.COM 	(void)killpg(pid, SIGTERM);
632*11251SErik.Trauschke@Sun.COM }
633*11251SErik.Trauschke@Sun.COM 
634*11251SErik.Trauschke@Sun.COM /*
635*11251SErik.Trauschke@Sun.COM  * Runs the PreUserauthHook.
636*11251SErik.Trauschke@Sun.COM  * Returns -1 on execution error or the exit code of the hook if execution is
637*11251SErik.Trauschke@Sun.COM  * successful.
638*11251SErik.Trauschke@Sun.COM  */
639*11251SErik.Trauschke@Sun.COM int
run_auth_hook(const char * path,const char * user,const char * method)640*11251SErik.Trauschke@Sun.COM run_auth_hook(const char *path, const char *user, const char *method)
641*11251SErik.Trauschke@Sun.COM {
642*11251SErik.Trauschke@Sun.COM 	struct stat st;
643*11251SErik.Trauschke@Sun.COM 	int i, status, ret = 1;
644*11251SErik.Trauschke@Sun.COM 	u_int envsize, argsize;
645*11251SErik.Trauschke@Sun.COM 	char buf[256];
646*11251SErik.Trauschke@Sun.COM 	char **env, **args;
647*11251SErik.Trauschke@Sun.COM         pid_t pid, *ppid;
648*11251SErik.Trauschke@Sun.COM 
649*11251SErik.Trauschke@Sun.COM 	if (path == NULL || user == NULL || method == NULL) {
650*11251SErik.Trauschke@Sun.COM 		return (-1);
651*11251SErik.Trauschke@Sun.COM 	}
652*11251SErik.Trauschke@Sun.COM 
653*11251SErik.Trauschke@Sun.COM 	/* Initialize the environment/arguments for the hook. */
654*11251SErik.Trauschke@Sun.COM 	envsize = 4; /* 3 env vars + EndOfList marker */
655*11251SErik.Trauschke@Sun.COM 	argsize = 4; /* 2 args + exe name + EndOfList marker */
656*11251SErik.Trauschke@Sun.COM 	env = xmalloc(envsize * sizeof (char *));
657*11251SErik.Trauschke@Sun.COM 	args = xmalloc(argsize * sizeof (char *));
658*11251SErik.Trauschke@Sun.COM 	env[0] = NULL;
659*11251SErik.Trauschke@Sun.COM 
660*11251SErik.Trauschke@Sun.COM 	/* we use the SSH env handling scheme */
661*11251SErik.Trauschke@Sun.COM 	child_set_env_silent(&env, &envsize, "PATH", "/usr/bin:/bin");
662*11251SErik.Trauschke@Sun.COM 	child_set_env_silent(&env, &envsize, "IFS", " \t\n");
663*11251SErik.Trauschke@Sun.COM 
664*11251SErik.Trauschke@Sun.COM 	(void) snprintf(buf, sizeof (buf), "%.50s %d %.50s %d",
665*11251SErik.Trauschke@Sun.COM 	    get_remote_ipaddr(), get_remote_port(),
666*11251SErik.Trauschke@Sun.COM 	    get_local_ipaddr(packet_get_connection_in()), get_local_port());
667*11251SErik.Trauschke@Sun.COM 	child_set_env_silent(&env, &envsize, "SSH_CONNECTION", buf);
668*11251SErik.Trauschke@Sun.COM 
669*11251SErik.Trauschke@Sun.COM 	args[0] = xstrdup(path);
670*11251SErik.Trauschke@Sun.COM 	args[1] = xstrdup(method);
671*11251SErik.Trauschke@Sun.COM 	args[2] = xstrdup(user);
672*11251SErik.Trauschke@Sun.COM 	args[3] = NULL;
673*11251SErik.Trauschke@Sun.COM 
674*11251SErik.Trauschke@Sun.COM 	/*
675*11251SErik.Trauschke@Sun.COM 	 * sanity checks
676*11251SErik.Trauschke@Sun.COM 	 * note: the checks do not make sure that the file checked is actually
677*11251SErik.Trauschke@Sun.COM 	 * the same which is executed. However, in this case it shouldn't be a
678*11251SErik.Trauschke@Sun.COM 	 * major issue since the hook is rather static and the worst case would
679*11251SErik.Trauschke@Sun.COM 	 * be an uncorrect message in the log or a hook is run even though the
680*11251SErik.Trauschke@Sun.COM 	 * permissions are not right.
681*11251SErik.Trauschke@Sun.COM 	 */
682*11251SErik.Trauschke@Sun.COM 
683*11251SErik.Trauschke@Sun.COM 	/* check if script does exist */
684*11251SErik.Trauschke@Sun.COM 	if (stat(path, &st) < 0) {
685*11251SErik.Trauschke@Sun.COM 		log("Error executing PreUserauthHook \"%s\": %s", path,
686*11251SErik.Trauschke@Sun.COM 		    strerror(errno));
687*11251SErik.Trauschke@Sun.COM 		goto cleanup;
688*11251SErik.Trauschke@Sun.COM 	}
689*11251SErik.Trauschke@Sun.COM 
690*11251SErik.Trauschke@Sun.COM 	/* Check correct permissions for script (uid of SSHD, mode 500) */
691*11251SErik.Trauschke@Sun.COM 	if (st.st_uid != getuid() || ((st.st_mode & 0777) != 0500)) {
692*11251SErik.Trauschke@Sun.COM 		log("PreUserauthHook has invalid permissions (should be 500, is"
693*11251SErik.Trauschke@Sun.COM 		    " %o) or ownership (should be %d, is %d)",
694*11251SErik.Trauschke@Sun.COM 		(uint) st.st_mode & 0777, getuid(), st.st_uid);
695*11251SErik.Trauschke@Sun.COM 		goto cleanup;
696*11251SErik.Trauschke@Sun.COM 	}
697*11251SErik.Trauschke@Sun.COM 
698*11251SErik.Trauschke@Sun.COM 	if ((pid = fork()) == 0) {
699*11251SErik.Trauschke@Sun.COM 		/*
700*11251SErik.Trauschke@Sun.COM 		 * We put the hook and all its (possible) descendants into
701*11251SErik.Trauschke@Sun.COM 		 * a new process group so that in case of a hanging hook
702*11251SErik.Trauschke@Sun.COM 		 * we can wipe out the whole "family".
703*11251SErik.Trauschke@Sun.COM 		 */
704*11251SErik.Trauschke@Sun.COM             	if (setpgid(0, 0) != 0) {
705*11251SErik.Trauschke@Sun.COM 			log("setpgid: %s", strerror(errno));
706*11251SErik.Trauschke@Sun.COM 			_exit(255);
707*11251SErik.Trauschke@Sun.COM                 }
708*11251SErik.Trauschke@Sun.COM 		(void) execve(path, args, env);
709*11251SErik.Trauschke@Sun.COM 		/* child is gone so we shouldn't get here */
710*11251SErik.Trauschke@Sun.COM 		log("Error executing PreUserauthHook \"%s\": %s", path,
711*11251SErik.Trauschke@Sun.COM 		    strerror(errno));
712*11251SErik.Trauschke@Sun.COM 		_exit(255);
713*11251SErik.Trauschke@Sun.COM 	} else if (pid == -1) {
714*11251SErik.Trauschke@Sun.COM 		log("Error executing PreUserauthHook \"%s\": %s", path,
715*11251SErik.Trauschke@Sun.COM 		    strerror(errno));
716*11251SErik.Trauschke@Sun.COM 		goto cleanup;
717*11251SErik.Trauschke@Sun.COM 	}
718*11251SErik.Trauschke@Sun.COM 
719*11251SErik.Trauschke@Sun.COM 	/* make preparations to kill hook if it is hanging */
720*11251SErik.Trauschke@Sun.COM 	ppid = xmalloc(sizeof (pid_t));
721*11251SErik.Trauschke@Sun.COM 	*ppid = pid;
722*11251SErik.Trauschke@Sun.COM 	fatal_add_cleanup((void (*)(void *))kill_hook, (void *) ppid);
723*11251SErik.Trauschke@Sun.COM 
724*11251SErik.Trauschke@Sun.COM 	if (waitpid(pid, &status, 0) == -1) {
725*11251SErik.Trauschke@Sun.COM 		log("Error executing PreUserauthHook \"%s\": %s", path,
726*11251SErik.Trauschke@Sun.COM 		    strerror(errno));
727*11251SErik.Trauschke@Sun.COM 		goto cleanup;
728*11251SErik.Trauschke@Sun.COM 	}
729*11251SErik.Trauschke@Sun.COM 
730*11251SErik.Trauschke@Sun.COM 	ret = WEXITSTATUS(status);
731*11251SErik.Trauschke@Sun.COM 
732*11251SErik.Trauschke@Sun.COM 	if (ret == 255) {
733*11251SErik.Trauschke@Sun.COM 		ret = -1; /* execve() failed, error msg already logged */
734*11251SErik.Trauschke@Sun.COM 	} else if (ret != 0) {
735*11251SErik.Trauschke@Sun.COM 		log("PreUserauthHook \"%s\" failed with exit code %d",
736*11251SErik.Trauschke@Sun.COM 		    path, ret);
737*11251SErik.Trauschke@Sun.COM 	} else {
738*11251SErik.Trauschke@Sun.COM 		debug("PreUserauthHook \"%s\" finished successfully", path);
739*11251SErik.Trauschke@Sun.COM 	}
740*11251SErik.Trauschke@Sun.COM 
741*11251SErik.Trauschke@Sun.COM cleanup:
742*11251SErik.Trauschke@Sun.COM 	for (i = 0; args[i] != NULL; i++) {
743*11251SErik.Trauschke@Sun.COM 		xfree(args[i]);
744*11251SErik.Trauschke@Sun.COM 	}
745*11251SErik.Trauschke@Sun.COM 	for (i = 0; env[i] != NULL; i++) {
746*11251SErik.Trauschke@Sun.COM 		xfree(env[i]);
747*11251SErik.Trauschke@Sun.COM 	}
748*11251SErik.Trauschke@Sun.COM 	xfree(args);
749*11251SErik.Trauschke@Sun.COM 	xfree(env);
750*11251SErik.Trauschke@Sun.COM 
751*11251SErik.Trauschke@Sun.COM 	fatal_remove_cleanup((void (*)(void *))kill_hook, (void *) ppid);
752*11251SErik.Trauschke@Sun.COM 
753*11251SErik.Trauschke@Sun.COM 	return (ret);
754*11251SErik.Trauschke@Sun.COM }
755*11251SErik.Trauschke@Sun.COM 
7560Sstevel@tonic-gate void
auth_debug_add(const char * fmt,...)7570Sstevel@tonic-gate auth_debug_add(const char *fmt,...)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	char buf[1024];
7600Sstevel@tonic-gate 	va_list args;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (!auth_debug_init)
7630Sstevel@tonic-gate 		return;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	va_start(args, fmt);
7660Sstevel@tonic-gate 	vsnprintf(buf, sizeof(buf), fmt, args);
7670Sstevel@tonic-gate 	va_end(args);
7680Sstevel@tonic-gate 	buffer_put_cstring(&auth_debug, buf);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate void
auth_debug_send(void)7720Sstevel@tonic-gate auth_debug_send(void)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	char *msg;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	if (!auth_debug_init)
7770Sstevel@tonic-gate 		return;
7780Sstevel@tonic-gate 	while (buffer_len(&auth_debug)) {
7790Sstevel@tonic-gate 		msg = buffer_get_string(&auth_debug, NULL);
7800Sstevel@tonic-gate 		packet_send_debug("%s", msg);
7810Sstevel@tonic-gate 		xfree(msg);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate void
auth_debug_reset(void)7860Sstevel@tonic-gate auth_debug_reset(void)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	if (auth_debug_init)
7890Sstevel@tonic-gate 		buffer_clear(&auth_debug);
7900Sstevel@tonic-gate 	else {
7910Sstevel@tonic-gate 		buffer_init(&auth_debug);
7920Sstevel@tonic-gate 		auth_debug_init = 1;
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate }
795