xref: /dflybsd-src/crypto/openssh/auth.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: auth.c,v 1.161 2024/05/17 00:30:23 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
418de8d7fSPeter Avalos  *
518de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
618de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
718de8d7fSPeter Avalos  * are met:
818de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
918de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1018de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1118de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1218de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
1318de8d7fSPeter Avalos  *
1418de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1518de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1618de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1718de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1818de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1918de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2018de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2118de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2218de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2318de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2418de8d7fSPeter Avalos  */
2518de8d7fSPeter Avalos 
2618de8d7fSPeter Avalos #include "includes.h"
2718de8d7fSPeter Avalos 
2818de8d7fSPeter Avalos #include <sys/types.h>
2918de8d7fSPeter Avalos #include <sys/stat.h>
30e9778795SPeter Avalos #include <sys/socket.h>
31664f4763Szrj #include <sys/wait.h>
3218de8d7fSPeter Avalos 
3318de8d7fSPeter Avalos #include <netinet/in.h>
3418de8d7fSPeter Avalos 
350cbfa66cSDaniel Fojt #include <stdlib.h>
3618de8d7fSPeter Avalos #include <errno.h>
3718de8d7fSPeter Avalos #include <fcntl.h>
3818de8d7fSPeter Avalos #ifdef HAVE_PATHS_H
3918de8d7fSPeter Avalos # include <paths.h>
4018de8d7fSPeter Avalos #endif
4118de8d7fSPeter Avalos #include <pwd.h>
4218de8d7fSPeter Avalos #ifdef HAVE_LOGIN_H
4318de8d7fSPeter Avalos #include <login.h>
4418de8d7fSPeter Avalos #endif
4518de8d7fSPeter Avalos #ifdef USE_SHADOW
4618de8d7fSPeter Avalos #include <shadow.h>
4718de8d7fSPeter Avalos #endif
4818de8d7fSPeter Avalos #include <stdarg.h>
4918de8d7fSPeter Avalos #include <stdio.h>
5018de8d7fSPeter Avalos #include <string.h>
5118de8d7fSPeter Avalos #include <unistd.h>
52e9778795SPeter Avalos #include <limits.h>
53e9778795SPeter Avalos #include <netdb.h>
54664f4763Szrj #include <time.h>
5518de8d7fSPeter Avalos 
5618de8d7fSPeter Avalos #include "xmalloc.h"
5718de8d7fSPeter Avalos #include "match.h"
5818de8d7fSPeter Avalos #include "groupaccess.h"
5918de8d7fSPeter Avalos #include "log.h"
60664f4763Szrj #include "sshbuf.h"
6136e94dc5SPeter Avalos #include "misc.h"
6218de8d7fSPeter Avalos #include "servconf.h"
63664f4763Szrj #include "sshkey.h"
6418de8d7fSPeter Avalos #include "hostfile.h"
6518de8d7fSPeter Avalos #include "auth.h"
6618de8d7fSPeter Avalos #include "auth-options.h"
6718de8d7fSPeter Avalos #include "canohost.h"
6818de8d7fSPeter Avalos #include "uidswap.h"
6918de8d7fSPeter Avalos #include "packet.h"
7018de8d7fSPeter Avalos #include "loginrec.h"
7118de8d7fSPeter Avalos #ifdef GSSAPI
7218de8d7fSPeter Avalos #include "ssh-gss.h"
7318de8d7fSPeter Avalos #endif
74856ea928SPeter Avalos #include "authfile.h"
7518de8d7fSPeter Avalos #include "monitor_wrap.h"
76e9778795SPeter Avalos #include "ssherr.h"
77664f4763Szrj #include "channels.h"
7818de8d7fSPeter Avalos 
7918de8d7fSPeter Avalos /* import */
8018de8d7fSPeter Avalos extern ServerOptions options;
810cbfa66cSDaniel Fojt extern struct include_list includes;
82664f4763Szrj extern struct sshbuf *loginmsg;
8318de8d7fSPeter Avalos extern struct passwd *privsep_pw;
84664f4763Szrj extern struct sshauthopt *auth_opts;
8518de8d7fSPeter Avalos 
8618de8d7fSPeter Avalos /* Debugging messages */
87664f4763Szrj static struct sshbuf *auth_debug;
8818de8d7fSPeter Avalos 
8918de8d7fSPeter Avalos /*
9018de8d7fSPeter Avalos  * Check if the user is allowed to log in via ssh. If user is listed
9118de8d7fSPeter Avalos  * in DenyUsers or one of user's groups is listed in DenyGroups, false
9218de8d7fSPeter Avalos  * will be returned. If AllowUsers isn't empty and user isn't listed
9318de8d7fSPeter Avalos  * there, or if AllowGroups isn't empty and one of user's groups isn't
9418de8d7fSPeter Avalos  * listed there, false will be returned.
9518de8d7fSPeter Avalos  * If the user's shell is not executable, false will be returned.
9618de8d7fSPeter Avalos  * Otherwise true is returned.
9718de8d7fSPeter Avalos  */
9818de8d7fSPeter Avalos int
allowed_user(struct ssh * ssh,struct passwd * pw)99664f4763Szrj allowed_user(struct ssh *ssh, struct passwd * pw)
10018de8d7fSPeter Avalos {
10118de8d7fSPeter Avalos 	struct stat st;
102ee116499SAntonio Huete Jimenez 	const char *hostname = NULL, *ipaddr = NULL;
10318de8d7fSPeter Avalos 	u_int i;
104ce74bacaSMatthew Dillon 	int r;
10518de8d7fSPeter Avalos 
10618de8d7fSPeter Avalos 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
10718de8d7fSPeter Avalos 	if (!pw || !pw->pw_name)
10818de8d7fSPeter Avalos 		return 0;
10918de8d7fSPeter Avalos 
110ee116499SAntonio Huete Jimenez 	if (!options.use_pam && platform_locked_account(pw)) {
11118de8d7fSPeter Avalos 		logit("User %.100s not allowed because account is locked",
11218de8d7fSPeter Avalos 		    pw->pw_name);
11318de8d7fSPeter Avalos 		return 0;
11418de8d7fSPeter Avalos 	}
11518de8d7fSPeter Avalos 
11618de8d7fSPeter Avalos 	/*
117856ea928SPeter Avalos 	 * Deny if shell does not exist or is not executable unless we
118856ea928SPeter Avalos 	 * are chrooting.
11918de8d7fSPeter Avalos 	 */
120856ea928SPeter Avalos 	if (options.chroot_directory == NULL ||
121856ea928SPeter Avalos 	    strcasecmp(options.chroot_directory, "none") == 0) {
122856ea928SPeter Avalos 		char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
123856ea928SPeter Avalos 		    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
12418de8d7fSPeter Avalos 
1250cbfa66cSDaniel Fojt 		if (stat(shell, &st) == -1) {
126856ea928SPeter Avalos 			logit("User %.100s not allowed because shell %.100s "
127856ea928SPeter Avalos 			    "does not exist", pw->pw_name, shell);
12836e94dc5SPeter Avalos 			free(shell);
12918de8d7fSPeter Avalos 			return 0;
13018de8d7fSPeter Avalos 		}
13118de8d7fSPeter Avalos 		if (S_ISREG(st.st_mode) == 0 ||
13218de8d7fSPeter Avalos 		    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
133856ea928SPeter Avalos 			logit("User %.100s not allowed because shell %.100s "
134856ea928SPeter Avalos 			    "is not executable", pw->pw_name, shell);
13536e94dc5SPeter Avalos 			free(shell);
13618de8d7fSPeter Avalos 			return 0;
13718de8d7fSPeter Avalos 		}
13836e94dc5SPeter Avalos 		free(shell);
139856ea928SPeter Avalos 	}
14018de8d7fSPeter Avalos 
14118de8d7fSPeter Avalos 	if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
14218de8d7fSPeter Avalos 	    options.num_deny_groups > 0 || options.num_allow_groups > 0) {
143e9778795SPeter Avalos 		hostname = auth_get_canonical_hostname(ssh, options.use_dns);
144e9778795SPeter Avalos 		ipaddr = ssh_remote_ipaddr(ssh);
14518de8d7fSPeter Avalos 	}
14618de8d7fSPeter Avalos 
14718de8d7fSPeter Avalos 	/* Return false if user is listed in DenyUsers */
14818de8d7fSPeter Avalos 	if (options.num_deny_users > 0) {
149ce74bacaSMatthew Dillon 		for (i = 0; i < options.num_deny_users; i++) {
150ce74bacaSMatthew Dillon 			r = match_user(pw->pw_name, hostname, ipaddr,
151ce74bacaSMatthew Dillon 			    options.deny_users[i]);
152ce74bacaSMatthew Dillon 			if (r < 0) {
153ce74bacaSMatthew Dillon 				fatal("Invalid DenyUsers pattern \"%.100s\"",
154ce74bacaSMatthew Dillon 				    options.deny_users[i]);
155ce74bacaSMatthew Dillon 			} else if (r != 0) {
15618de8d7fSPeter Avalos 				logit("User %.100s from %.100s not allowed "
15718de8d7fSPeter Avalos 				    "because listed in DenyUsers",
15818de8d7fSPeter Avalos 				    pw->pw_name, hostname);
15918de8d7fSPeter Avalos 				return 0;
16018de8d7fSPeter Avalos 			}
16118de8d7fSPeter Avalos 		}
162ce74bacaSMatthew Dillon 	}
16318de8d7fSPeter Avalos 	/* Return false if AllowUsers isn't empty and user isn't listed there */
16418de8d7fSPeter Avalos 	if (options.num_allow_users > 0) {
165ce74bacaSMatthew Dillon 		for (i = 0; i < options.num_allow_users; i++) {
166ce74bacaSMatthew Dillon 			r = match_user(pw->pw_name, hostname, ipaddr,
167ce74bacaSMatthew Dillon 			    options.allow_users[i]);
168ce74bacaSMatthew Dillon 			if (r < 0) {
169ce74bacaSMatthew Dillon 				fatal("Invalid AllowUsers pattern \"%.100s\"",
170ce74bacaSMatthew Dillon 				    options.allow_users[i]);
171ce74bacaSMatthew Dillon 			} else if (r == 1)
17218de8d7fSPeter Avalos 				break;
173ce74bacaSMatthew Dillon 		}
17418de8d7fSPeter Avalos 		/* i < options.num_allow_users iff we break for loop */
17518de8d7fSPeter Avalos 		if (i >= options.num_allow_users) {
17618de8d7fSPeter Avalos 			logit("User %.100s from %.100s not allowed because "
17718de8d7fSPeter Avalos 			    "not listed in AllowUsers", pw->pw_name, hostname);
17818de8d7fSPeter Avalos 			return 0;
17918de8d7fSPeter Avalos 		}
18018de8d7fSPeter Avalos 	}
18118de8d7fSPeter Avalos 	if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
18218de8d7fSPeter Avalos 		/* Get the user's group access list (primary and supplementary) */
18318de8d7fSPeter Avalos 		if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
18418de8d7fSPeter Avalos 			logit("User %.100s from %.100s not allowed because "
18518de8d7fSPeter Avalos 			    "not in any group", pw->pw_name, hostname);
18618de8d7fSPeter Avalos 			return 0;
18718de8d7fSPeter Avalos 		}
18818de8d7fSPeter Avalos 
18918de8d7fSPeter Avalos 		/* Return false if one of user's groups is listed in DenyGroups */
19018de8d7fSPeter Avalos 		if (options.num_deny_groups > 0)
19118de8d7fSPeter Avalos 			if (ga_match(options.deny_groups,
19218de8d7fSPeter Avalos 			    options.num_deny_groups)) {
19318de8d7fSPeter Avalos 				ga_free();
19418de8d7fSPeter Avalos 				logit("User %.100s from %.100s not allowed "
19518de8d7fSPeter Avalos 				    "because a group is listed in DenyGroups",
19618de8d7fSPeter Avalos 				    pw->pw_name, hostname);
19718de8d7fSPeter Avalos 				return 0;
19818de8d7fSPeter Avalos 			}
19918de8d7fSPeter Avalos 		/*
20018de8d7fSPeter Avalos 		 * Return false if AllowGroups isn't empty and one of user's groups
20118de8d7fSPeter Avalos 		 * isn't listed there
20218de8d7fSPeter Avalos 		 */
20318de8d7fSPeter Avalos 		if (options.num_allow_groups > 0)
20418de8d7fSPeter Avalos 			if (!ga_match(options.allow_groups,
20518de8d7fSPeter Avalos 			    options.num_allow_groups)) {
20618de8d7fSPeter Avalos 				ga_free();
20718de8d7fSPeter Avalos 				logit("User %.100s from %.100s not allowed "
20818de8d7fSPeter Avalos 				    "because none of user's groups are listed "
20918de8d7fSPeter Avalos 				    "in AllowGroups", pw->pw_name, hostname);
21018de8d7fSPeter Avalos 				return 0;
21118de8d7fSPeter Avalos 			}
21218de8d7fSPeter Avalos 		ga_free();
21318de8d7fSPeter Avalos 	}
21418de8d7fSPeter Avalos 
21518de8d7fSPeter Avalos #ifdef CUSTOM_SYS_AUTH_ALLOWED_USER
216664f4763Szrj 	if (!sys_auth_allowed_user(pw, loginmsg))
21718de8d7fSPeter Avalos 		return 0;
21818de8d7fSPeter Avalos #endif
21918de8d7fSPeter Avalos 
22018de8d7fSPeter Avalos 	/* We found no reason not to let this user try to log on... */
22118de8d7fSPeter Avalos 	return 1;
22218de8d7fSPeter Avalos }
22318de8d7fSPeter Avalos 
224ce74bacaSMatthew Dillon /*
225ce74bacaSMatthew Dillon  * Formats any key left in authctxt->auth_method_key for inclusion in
226ce74bacaSMatthew Dillon  * auth_log()'s message. Also includes authxtct->auth_method_info if present.
227ce74bacaSMatthew Dillon  */
228ce74bacaSMatthew Dillon static char *
format_method_key(Authctxt * authctxt)229ce74bacaSMatthew Dillon format_method_key(Authctxt *authctxt)
23036e94dc5SPeter Avalos {
231ce74bacaSMatthew Dillon 	const struct sshkey *key = authctxt->auth_method_key;
232ce74bacaSMatthew Dillon 	const char *methinfo = authctxt->auth_method_info;
233664f4763Szrj 	char *fp, *cafp, *ret = NULL;
23436e94dc5SPeter Avalos 
235ce74bacaSMatthew Dillon 	if (key == NULL)
236ce74bacaSMatthew Dillon 		return NULL;
23736e94dc5SPeter Avalos 
238664f4763Szrj 	if (sshkey_is_cert(key)) {
239664f4763Szrj 		fp = sshkey_fingerprint(key,
240ce74bacaSMatthew Dillon 		    options.fingerprint_hash, SSH_FP_DEFAULT);
241664f4763Szrj 		cafp = sshkey_fingerprint(key->cert->signature_key,
242664f4763Szrj 		    options.fingerprint_hash, SSH_FP_DEFAULT);
243664f4763Szrj 		xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
244664f4763Szrj 		    sshkey_type(key), fp == NULL ? "(null)" : fp,
245664f4763Szrj 		    key->cert->key_id,
246ce74bacaSMatthew Dillon 		    (unsigned long long)key->cert->serial,
247ce74bacaSMatthew Dillon 		    sshkey_type(key->cert->signature_key),
248664f4763Szrj 		    cafp == NULL ? "(null)" : cafp,
249ce74bacaSMatthew Dillon 		    methinfo == NULL ? "" : ", ",
250ce74bacaSMatthew Dillon 		    methinfo == NULL ? "" : methinfo);
251ce74bacaSMatthew Dillon 		free(fp);
252664f4763Szrj 		free(cafp);
253ce74bacaSMatthew Dillon 	} else {
254ce74bacaSMatthew Dillon 		fp = sshkey_fingerprint(key, options.fingerprint_hash,
255ce74bacaSMatthew Dillon 		    SSH_FP_DEFAULT);
256ce74bacaSMatthew Dillon 		xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
257ce74bacaSMatthew Dillon 		    fp == NULL ? "(null)" : fp,
258ce74bacaSMatthew Dillon 		    methinfo == NULL ? "" : ", ",
259ce74bacaSMatthew Dillon 		    methinfo == NULL ? "" : methinfo);
260ce74bacaSMatthew Dillon 		free(fp);
261ce74bacaSMatthew Dillon 	}
262ce74bacaSMatthew Dillon 	return ret;
26336e94dc5SPeter Avalos }
26436e94dc5SPeter Avalos 
26536e94dc5SPeter Avalos void
auth_log(struct ssh * ssh,int authenticated,int partial,const char * method,const char * submethod)266664f4763Szrj auth_log(struct ssh *ssh, int authenticated, int partial,
26736e94dc5SPeter Avalos     const char *method, const char *submethod)
26818de8d7fSPeter Avalos {
269664f4763Szrj 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
270664f4763Szrj 	int level = SYSLOG_LEVEL_VERBOSE;
271ce74bacaSMatthew Dillon 	const char *authmsg;
272ce74bacaSMatthew Dillon 	char *extra = NULL;
27318de8d7fSPeter Avalos 
274*ba1276acSMatthew Dillon 	if (!mm_is_monitor() && !authctxt->postponed)
27518de8d7fSPeter Avalos 		return;
27618de8d7fSPeter Avalos 
27718de8d7fSPeter Avalos 	/* Raise logging level */
27818de8d7fSPeter Avalos 	if (authenticated == 1 ||
27918de8d7fSPeter Avalos 	    !authctxt->valid ||
28018de8d7fSPeter Avalos 	    authctxt->failures >= options.max_authtries / 2 ||
28118de8d7fSPeter Avalos 	    strcmp(method, "password") == 0)
282664f4763Szrj 		level = SYSLOG_LEVEL_INFO;
28318de8d7fSPeter Avalos 
28418de8d7fSPeter Avalos 	if (authctxt->postponed)
28518de8d7fSPeter Avalos 		authmsg = "Postponed";
28636e94dc5SPeter Avalos 	else if (partial)
28736e94dc5SPeter Avalos 		authmsg = "Partial";
28818de8d7fSPeter Avalos 	else
28918de8d7fSPeter Avalos 		authmsg = authenticated ? "Accepted" : "Failed";
29018de8d7fSPeter Avalos 
291ce74bacaSMatthew Dillon 	if ((extra = format_method_key(authctxt)) == NULL) {
292ce74bacaSMatthew Dillon 		if (authctxt->auth_method_info != NULL)
293ce74bacaSMatthew Dillon 			extra = xstrdup(authctxt->auth_method_info);
294ce74bacaSMatthew Dillon 	}
295ce74bacaSMatthew Dillon 
296664f4763Szrj 	do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
29718de8d7fSPeter Avalos 	    authmsg,
29818de8d7fSPeter Avalos 	    method,
29936e94dc5SPeter Avalos 	    submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
30018de8d7fSPeter Avalos 	    authctxt->valid ? "" : "invalid user ",
30118de8d7fSPeter Avalos 	    authctxt->user,
302e9778795SPeter Avalos 	    ssh_remote_ipaddr(ssh),
303e9778795SPeter Avalos 	    ssh_remote_port(ssh),
304ce74bacaSMatthew Dillon 	    extra != NULL ? ": " : "",
305ce74bacaSMatthew Dillon 	    extra != NULL ? extra : "");
306ce74bacaSMatthew Dillon 
307ce74bacaSMatthew Dillon 	free(extra);
30818de8d7fSPeter Avalos 
30950a69bb5SSascha Wildner #if defined(CUSTOM_FAILED_LOGIN) || defined(SSH_AUDIT_EVENTS)
31050a69bb5SSascha Wildner 	if (authenticated == 0 && !(authctxt->postponed || partial)) {
31150a69bb5SSascha Wildner 		/* Log failed login attempt */
31218de8d7fSPeter Avalos # ifdef CUSTOM_FAILED_LOGIN
31350a69bb5SSascha Wildner 		if (strcmp(method, "password") == 0 ||
31418de8d7fSPeter Avalos 		    strncmp(method, "keyboard-interactive", 20) == 0 ||
31550a69bb5SSascha Wildner 		    strcmp(method, "challenge-response") == 0)
316664f4763Szrj 			record_failed_login(ssh, authctxt->user,
317e9778795SPeter Avalos 			    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
31850a69bb5SSascha Wildner # endif
31950a69bb5SSascha Wildner # ifdef SSH_AUDIT_EVENTS
32050a69bb5SSascha Wildner 		audit_event(ssh, audit_classify_auth(method));
32150a69bb5SSascha Wildner # endif
32250a69bb5SSascha Wildner 	}
32350a69bb5SSascha Wildner #endif
32450a69bb5SSascha Wildner #if defined(CUSTOM_FAILED_LOGIN) && defined(WITH_AIXAUTHENTICATE)
32518de8d7fSPeter Avalos 	if (authenticated)
32618de8d7fSPeter Avalos 		sys_auth_record_login(authctxt->user,
327e9778795SPeter Avalos 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh",
328664f4763Szrj 		    loginmsg);
32918de8d7fSPeter Avalos #endif
33018de8d7fSPeter Avalos }
33118de8d7fSPeter Avalos 
33236e94dc5SPeter Avalos void
auth_maxtries_exceeded(struct ssh * ssh)333664f4763Szrj auth_maxtries_exceeded(struct ssh *ssh)
33436e94dc5SPeter Avalos {
335664f4763Szrj 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
336e9778795SPeter Avalos 
337e9778795SPeter Avalos 	error("maximum authentication attempts exceeded for "
338ce74bacaSMatthew Dillon 	    "%s%.100s from %.200s port %d ssh2",
33936e94dc5SPeter Avalos 	    authctxt->valid ? "" : "invalid user ",
34036e94dc5SPeter Avalos 	    authctxt->user,
341e9778795SPeter Avalos 	    ssh_remote_ipaddr(ssh),
342ce74bacaSMatthew Dillon 	    ssh_remote_port(ssh));
343664f4763Szrj 	ssh_packet_disconnect(ssh, "Too many authentication failures");
34436e94dc5SPeter Avalos 	/* NOTREACHED */
34536e94dc5SPeter Avalos }
34636e94dc5SPeter Avalos 
34718de8d7fSPeter Avalos /*
34818de8d7fSPeter Avalos  * Check whether root logins are disallowed.
34918de8d7fSPeter Avalos  */
35018de8d7fSPeter Avalos int
auth_root_allowed(struct ssh * ssh,const char * method)351664f4763Szrj auth_root_allowed(struct ssh *ssh, const char *method)
35218de8d7fSPeter Avalos {
35318de8d7fSPeter Avalos 	switch (options.permit_root_login) {
35418de8d7fSPeter Avalos 	case PERMIT_YES:
35518de8d7fSPeter Avalos 		return 1;
35618de8d7fSPeter Avalos 	case PERMIT_NO_PASSWD:
357e9778795SPeter Avalos 		if (strcmp(method, "publickey") == 0 ||
358e9778795SPeter Avalos 		    strcmp(method, "hostbased") == 0 ||
359e9778795SPeter Avalos 		    strcmp(method, "gssapi-with-mic") == 0)
36018de8d7fSPeter Avalos 			return 1;
36118de8d7fSPeter Avalos 		break;
36218de8d7fSPeter Avalos 	case PERMIT_FORCED_ONLY:
363664f4763Szrj 		if (auth_opts->force_command != NULL) {
36418de8d7fSPeter Avalos 			logit("Root login accepted for forced command.");
36518de8d7fSPeter Avalos 			return 1;
36618de8d7fSPeter Avalos 		}
36718de8d7fSPeter Avalos 		break;
36818de8d7fSPeter Avalos 	}
369e9778795SPeter Avalos 	logit("ROOT LOGIN REFUSED FROM %.200s port %d",
370e9778795SPeter Avalos 	    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
37118de8d7fSPeter Avalos 	return 0;
37218de8d7fSPeter Avalos }
37318de8d7fSPeter Avalos 
37418de8d7fSPeter Avalos 
37518de8d7fSPeter Avalos /*
37618de8d7fSPeter Avalos  * Given a template and a passwd structure, build a filename
37718de8d7fSPeter Avalos  * by substituting % tokenised options. Currently, %% becomes '%',
37818de8d7fSPeter Avalos  * %h becomes the home directory and %u the username.
37918de8d7fSPeter Avalos  *
38018de8d7fSPeter Avalos  * This returns a buffer allocated by xmalloc.
38118de8d7fSPeter Avalos  */
3821c188a7fSPeter Avalos char *
expand_authorized_keys(const char * filename,struct passwd * pw)38318de8d7fSPeter Avalos expand_authorized_keys(const char *filename, struct passwd *pw)
38418de8d7fSPeter Avalos {
385664f4763Szrj 	char *file, uidstr[32], ret[PATH_MAX];
38618de8d7fSPeter Avalos 	int i;
38718de8d7fSPeter Avalos 
388664f4763Szrj 	snprintf(uidstr, sizeof(uidstr), "%llu",
389664f4763Szrj 	    (unsigned long long)pw->pw_uid);
39018de8d7fSPeter Avalos 	file = percent_expand(filename, "h", pw->pw_dir,
391664f4763Szrj 	    "u", pw->pw_name, "U", uidstr, (char *)NULL);
39218de8d7fSPeter Avalos 
39318de8d7fSPeter Avalos 	/*
39418de8d7fSPeter Avalos 	 * Ensure that filename starts anchored. If not, be backward
39518de8d7fSPeter Avalos 	 * compatible and prepend the '%h/'
39618de8d7fSPeter Avalos 	 */
397664f4763Szrj 	if (path_absolute(file))
39818de8d7fSPeter Avalos 		return (file);
39918de8d7fSPeter Avalos 
40018de8d7fSPeter Avalos 	i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
40118de8d7fSPeter Avalos 	if (i < 0 || (size_t)i >= sizeof(ret))
40218de8d7fSPeter Avalos 		fatal("expand_authorized_keys: path too long");
40336e94dc5SPeter Avalos 	free(file);
40418de8d7fSPeter Avalos 	return (xstrdup(ret));
40518de8d7fSPeter Avalos }
40618de8d7fSPeter Avalos 
40718de8d7fSPeter Avalos char *
authorized_principals_file(struct passwd * pw)408856ea928SPeter Avalos authorized_principals_file(struct passwd *pw)
409856ea928SPeter Avalos {
410e9778795SPeter Avalos 	if (options.authorized_principals_file == NULL)
411856ea928SPeter Avalos 		return NULL;
412856ea928SPeter Avalos 	return expand_authorized_keys(options.authorized_principals_file, pw);
413856ea928SPeter Avalos }
414856ea928SPeter Avalos 
41518de8d7fSPeter Avalos /* return ok if key exists in sysfile or userfile */
41618de8d7fSPeter Avalos HostStatus
check_key_in_hostfiles(struct passwd * pw,struct sshkey * key,const char * host,const char * sysfile,const char * userfile)417ce74bacaSMatthew Dillon check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
41818de8d7fSPeter Avalos     const char *sysfile, const char *userfile)
41918de8d7fSPeter Avalos {
42018de8d7fSPeter Avalos 	char *user_hostfile;
42118de8d7fSPeter Avalos 	struct stat st;
42218de8d7fSPeter Avalos 	HostStatus host_status;
4239f304aafSPeter Avalos 	struct hostkeys *hostkeys;
4249f304aafSPeter Avalos 	const struct hostkey_entry *found;
42518de8d7fSPeter Avalos 
4269f304aafSPeter Avalos 	hostkeys = init_hostkeys();
42750a69bb5SSascha Wildner 	load_hostkeys(hostkeys, host, sysfile, 0);
4289f304aafSPeter Avalos 	if (userfile != NULL) {
42918de8d7fSPeter Avalos 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
43018de8d7fSPeter Avalos 		if (options.strict_modes &&
43118de8d7fSPeter Avalos 		    (stat(user_hostfile, &st) == 0) &&
43218de8d7fSPeter Avalos 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
43318de8d7fSPeter Avalos 		    (st.st_mode & 022) != 0)) {
43418de8d7fSPeter Avalos 			logit("Authentication refused for %.100s: "
43518de8d7fSPeter Avalos 			    "bad owner or modes for %.200s",
43618de8d7fSPeter Avalos 			    pw->pw_name, user_hostfile);
437856ea928SPeter Avalos 			auth_debug_add("Ignored %.200s: bad ownership or modes",
438856ea928SPeter Avalos 			    user_hostfile);
43918de8d7fSPeter Avalos 		} else {
44018de8d7fSPeter Avalos 			temporarily_use_uid(pw);
44150a69bb5SSascha Wildner 			load_hostkeys(hostkeys, host, user_hostfile, 0);
44218de8d7fSPeter Avalos 			restore_uid();
44318de8d7fSPeter Avalos 		}
44436e94dc5SPeter Avalos 		free(user_hostfile);
44518de8d7fSPeter Avalos 	}
4469f304aafSPeter Avalos 	host_status = check_key_in_hostkeys(hostkeys, key, &found);
4479f304aafSPeter Avalos 	if (host_status == HOST_REVOKED)
4489f304aafSPeter Avalos 		error("WARNING: revoked key for %s attempted authentication",
4490cbfa66cSDaniel Fojt 		    host);
4509f304aafSPeter Avalos 	else if (host_status == HOST_OK)
45150a69bb5SSascha Wildner 		debug_f("key for %s found at %s:%ld",
4529f304aafSPeter Avalos 		    found->host, found->file, found->line);
4539f304aafSPeter Avalos 	else
45450a69bb5SSascha Wildner 		debug_f("key for host %s not found", host);
45518de8d7fSPeter Avalos 
4569f304aafSPeter Avalos 	free_hostkeys(hostkeys);
4579f304aafSPeter Avalos 
45818de8d7fSPeter Avalos 	return host_status;
45918de8d7fSPeter Avalos }
46018de8d7fSPeter Avalos 
46118de8d7fSPeter Avalos struct passwd *
getpwnamallow(struct ssh * ssh,const char * user)462664f4763Szrj getpwnamallow(struct ssh *ssh, const char *user)
46318de8d7fSPeter Avalos {
46418de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP
46518de8d7fSPeter Avalos 	extern login_cap_t *lc;
46618de8d7fSPeter Avalos #ifdef BSD_AUTH
46718de8d7fSPeter Avalos 	auth_session_t *as;
46818de8d7fSPeter Avalos #endif
46918de8d7fSPeter Avalos #endif
47018de8d7fSPeter Avalos 	struct passwd *pw;
471664f4763Szrj 	struct connection_info *ci;
47250a69bb5SSascha Wildner 	u_int i;
47318de8d7fSPeter Avalos 
474*ba1276acSMatthew Dillon 	ci = server_get_connection_info(ssh, 1, options.use_dns);
47599e85e0dSPeter Avalos 	ci->user = user;
4760cbfa66cSDaniel Fojt 	parse_server_match_config(&options, &includes, ci);
477ce74bacaSMatthew Dillon 	log_change_level(options.log_level);
47850a69bb5SSascha Wildner 	log_verbose_reset();
47950a69bb5SSascha Wildner 	for (i = 0; i < options.num_log_verbose; i++)
48050a69bb5SSascha Wildner 		log_verbose_add(options.log_verbose[i]);
481*ba1276acSMatthew Dillon 	server_process_permitopen(ssh);
48218de8d7fSPeter Avalos 
483856ea928SPeter Avalos #if defined(_AIX) && defined(HAVE_SETAUTHDB)
484856ea928SPeter Avalos 	aix_setauthdb(user);
485856ea928SPeter Avalos #endif
486856ea928SPeter Avalos 
48718de8d7fSPeter Avalos 	pw = getpwnam(user);
488856ea928SPeter Avalos 
489856ea928SPeter Avalos #if defined(_AIX) && defined(HAVE_SETAUTHDB)
490856ea928SPeter Avalos 	aix_restoreauthdb();
491856ea928SPeter Avalos #endif
49218de8d7fSPeter Avalos 	if (pw == NULL) {
493e9778795SPeter Avalos 		logit("Invalid user %.100s from %.100s port %d",
494e9778795SPeter Avalos 		    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
49518de8d7fSPeter Avalos #ifdef CUSTOM_FAILED_LOGIN
496664f4763Szrj 		record_failed_login(ssh, user,
497e9778795SPeter Avalos 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
49818de8d7fSPeter Avalos #endif
49918de8d7fSPeter Avalos #ifdef SSH_AUDIT_EVENTS
500664f4763Szrj 		audit_event(ssh, SSH_INVALID_USER);
50118de8d7fSPeter Avalos #endif /* SSH_AUDIT_EVENTS */
50218de8d7fSPeter Avalos 		return (NULL);
50318de8d7fSPeter Avalos 	}
504664f4763Szrj 	if (!allowed_user(ssh, pw))
50518de8d7fSPeter Avalos 		return (NULL);
50618de8d7fSPeter Avalos #ifdef HAVE_LOGIN_CAP
50750a69bb5SSascha Wildner 	if ((lc = login_getpwclass(pw)) == NULL) {
50818de8d7fSPeter Avalos 		debug("unable to get login class: %s", user);
50918de8d7fSPeter Avalos 		return (NULL);
51018de8d7fSPeter Avalos 	}
51118de8d7fSPeter Avalos #ifdef BSD_AUTH
51218de8d7fSPeter Avalos 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
51318de8d7fSPeter Avalos 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
51418de8d7fSPeter Avalos 		debug("Approval failure for %s", user);
51518de8d7fSPeter Avalos 		pw = NULL;
51618de8d7fSPeter Avalos 	}
51718de8d7fSPeter Avalos 	if (as != NULL)
51818de8d7fSPeter Avalos 		auth_close(as);
51918de8d7fSPeter Avalos #endif
52018de8d7fSPeter Avalos #endif
52118de8d7fSPeter Avalos 	if (pw != NULL)
52218de8d7fSPeter Avalos 		return (pwcopy(pw));
52318de8d7fSPeter Avalos 	return (NULL);
52418de8d7fSPeter Avalos }
52518de8d7fSPeter Avalos 
526856ea928SPeter Avalos /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
527856ea928SPeter Avalos int
auth_key_is_revoked(struct sshkey * key)528ce74bacaSMatthew Dillon auth_key_is_revoked(struct sshkey *key)
529856ea928SPeter Avalos {
530e9778795SPeter Avalos 	char *fp = NULL;
531e9778795SPeter Avalos 	int r;
532856ea928SPeter Avalos 
533856ea928SPeter Avalos 	if (options.revoked_keys_file == NULL)
534856ea928SPeter Avalos 		return 0;
535e9778795SPeter Avalos 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
536e9778795SPeter Avalos 	    SSH_FP_DEFAULT)) == NULL) {
537e9778795SPeter Avalos 		r = SSH_ERR_ALLOC_FAIL;
53850a69bb5SSascha Wildner 		error_fr(r, "fingerprint key");
539e9778795SPeter Avalos 		goto out;
540e9778795SPeter Avalos 	}
541e9778795SPeter Avalos 
542e9778795SPeter Avalos 	r = sshkey_check_revoked(key, options.revoked_keys_file);
543e9778795SPeter Avalos 	switch (r) {
54436e94dc5SPeter Avalos 	case 0:
545e9778795SPeter Avalos 		break; /* not revoked */
546e9778795SPeter Avalos 	case SSH_ERR_KEY_REVOKED:
547e9778795SPeter Avalos 		error("Authentication key %s %s revoked by file %s",
548e9778795SPeter Avalos 		    sshkey_type(key), fp, options.revoked_keys_file);
549e9778795SPeter Avalos 		goto out;
55036e94dc5SPeter Avalos 	default:
55150a69bb5SSascha Wildner 		error_r(r, "Error checking authentication key %s %s in "
55250a69bb5SSascha Wildner 		    "revoked keys file %s", sshkey_type(key), fp,
55350a69bb5SSascha Wildner 		    options.revoked_keys_file);
554e9778795SPeter Avalos 		goto out;
55536e94dc5SPeter Avalos 	}
556e9778795SPeter Avalos 
557e9778795SPeter Avalos 	/* Success */
558e9778795SPeter Avalos 	r = 0;
559e9778795SPeter Avalos 
560e9778795SPeter Avalos  out:
561e9778795SPeter Avalos 	free(fp);
562e9778795SPeter Avalos 	return r == 0 ? 0 : 1;
563856ea928SPeter Avalos }
564856ea928SPeter Avalos 
56518de8d7fSPeter Avalos void
auth_debug_add(const char * fmt,...)56618de8d7fSPeter Avalos auth_debug_add(const char *fmt,...)
56718de8d7fSPeter Avalos {
56818de8d7fSPeter Avalos 	char buf[1024];
56918de8d7fSPeter Avalos 	va_list args;
570664f4763Szrj 	int r;
57118de8d7fSPeter Avalos 
57218de8d7fSPeter Avalos 	va_start(args, fmt);
57318de8d7fSPeter Avalos 	vsnprintf(buf, sizeof(buf), fmt, args);
57418de8d7fSPeter Avalos 	va_end(args);
575*ba1276acSMatthew Dillon 	debug3("%s", buf);
576*ba1276acSMatthew Dillon 	if (auth_debug != NULL)
577664f4763Szrj 		if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
57850a69bb5SSascha Wildner 			fatal_fr(r, "sshbuf_put_cstring");
57918de8d7fSPeter Avalos }
58018de8d7fSPeter Avalos 
58118de8d7fSPeter Avalos void
auth_debug_send(struct ssh * ssh)582664f4763Szrj auth_debug_send(struct ssh *ssh)
58318de8d7fSPeter Avalos {
58418de8d7fSPeter Avalos 	char *msg;
585664f4763Szrj 	int r;
58618de8d7fSPeter Avalos 
587664f4763Szrj 	if (auth_debug == NULL)
58818de8d7fSPeter Avalos 		return;
589664f4763Szrj 	while (sshbuf_len(auth_debug) != 0) {
590664f4763Szrj 		if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
59150a69bb5SSascha Wildner 			fatal_fr(r, "sshbuf_get_cstring");
592664f4763Szrj 		ssh_packet_send_debug(ssh, "%s", msg);
59336e94dc5SPeter Avalos 		free(msg);
59418de8d7fSPeter Avalos 	}
59518de8d7fSPeter Avalos }
59618de8d7fSPeter Avalos 
59718de8d7fSPeter Avalos void
auth_debug_reset(void)59818de8d7fSPeter Avalos auth_debug_reset(void)
59918de8d7fSPeter Avalos {
600664f4763Szrj 	if (auth_debug != NULL)
601664f4763Szrj 		sshbuf_reset(auth_debug);
602664f4763Szrj 	else if ((auth_debug = sshbuf_new()) == NULL)
60350a69bb5SSascha Wildner 		fatal_f("sshbuf_new failed");
60418de8d7fSPeter Avalos }
60518de8d7fSPeter Avalos 
60618de8d7fSPeter Avalos struct passwd *
fakepw(void)60718de8d7fSPeter Avalos fakepw(void)
60818de8d7fSPeter Avalos {
609ee116499SAntonio Huete Jimenez 	static int done = 0;
61018de8d7fSPeter Avalos 	static struct passwd fake;
611ee116499SAntonio Huete Jimenez 	const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
612ee116499SAntonio Huete Jimenez 	    "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
613ee116499SAntonio Huete Jimenez 	char *cp;
614ee116499SAntonio Huete Jimenez 
615ee116499SAntonio Huete Jimenez 	if (done)
616ee116499SAntonio Huete Jimenez 		return (&fake);
61718de8d7fSPeter Avalos 
61818de8d7fSPeter Avalos 	memset(&fake, 0, sizeof(fake));
61918de8d7fSPeter Avalos 	fake.pw_name = "NOUSER";
620ee116499SAntonio Huete Jimenez 	fake.pw_passwd = xstrdup("$2a$10$"
621ee116499SAntonio Huete Jimenez 	    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
622ee116499SAntonio Huete Jimenez 	for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
623ee116499SAntonio Huete Jimenez 		*cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
62436e94dc5SPeter Avalos #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
62518de8d7fSPeter Avalos 	fake.pw_gecos = "NOUSER";
62636e94dc5SPeter Avalos #endif
62718de8d7fSPeter Avalos 	fake.pw_uid = privsep_pw == NULL ? (uid_t)-1 : privsep_pw->pw_uid;
62818de8d7fSPeter Avalos 	fake.pw_gid = privsep_pw == NULL ? (gid_t)-1 : privsep_pw->pw_gid;
62936e94dc5SPeter Avalos #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
63018de8d7fSPeter Avalos 	fake.pw_class = "";
63118de8d7fSPeter Avalos #endif
63218de8d7fSPeter Avalos 	fake.pw_dir = "/nonexist";
63318de8d7fSPeter Avalos 	fake.pw_shell = "/nonexist";
634ee116499SAntonio Huete Jimenez 	done = 1;
63518de8d7fSPeter Avalos 
63618de8d7fSPeter Avalos 	return (&fake);
63718de8d7fSPeter Avalos }
638e9778795SPeter Avalos 
639e9778795SPeter Avalos /*
640e9778795SPeter Avalos  * Return the canonical name of the host in the other side of the current
641e9778795SPeter Avalos  * connection.  The host name is cached, so it is efficient to call this
642e9778795SPeter Avalos  * several times.
643e9778795SPeter Avalos  */
644e9778795SPeter Avalos 
645e9778795SPeter Avalos const char *
auth_get_canonical_hostname(struct ssh * ssh,int use_dns)646e9778795SPeter Avalos auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
647e9778795SPeter Avalos {
648e9778795SPeter Avalos 	static char *dnsname;
649e9778795SPeter Avalos 
650e9778795SPeter Avalos 	if (!use_dns)
651e9778795SPeter Avalos 		return ssh_remote_ipaddr(ssh);
652*ba1276acSMatthew Dillon 	if (dnsname != NULL)
653e9778795SPeter Avalos 		return dnsname;
654*ba1276acSMatthew Dillon 	dnsname = ssh_remote_hostname(ssh);
655e9778795SPeter Avalos 	return dnsname;
656e9778795SPeter Avalos }
657664f4763Szrj 
658664f4763Szrj /* These functions link key/cert options to the auth framework */
659664f4763Szrj 
660664f4763Szrj /* Log sshauthopt options locally and (optionally) for remote transmission */
661664f4763Szrj void
auth_log_authopts(const char * loc,const struct sshauthopt * opts,int do_remote)662664f4763Szrj auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
663664f4763Szrj {
664664f4763Szrj 	int do_env = options.permit_user_env && opts->nenv > 0;
665664f4763Szrj 	int do_permitopen = opts->npermitopen > 0 &&
666664f4763Szrj 	    (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
667664f4763Szrj 	int do_permitlisten = opts->npermitlisten > 0 &&
668664f4763Szrj 	    (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
669664f4763Szrj 	size_t i;
670664f4763Szrj 	char msg[1024], buf[64];
671664f4763Szrj 
672664f4763Szrj 	snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
673664f4763Szrj 	/* Try to keep this alphabetically sorted */
67450a69bb5SSascha Wildner 	snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
675664f4763Szrj 	    opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
676664f4763Szrj 	    opts->force_command == NULL ? "" : " command",
677664f4763Szrj 	    do_env ?  " environment" : "",
678664f4763Szrj 	    opts->valid_before == 0 ? "" : "expires",
67950a69bb5SSascha Wildner 	    opts->no_require_user_presence ? " no-touch-required" : "",
680664f4763Szrj 	    do_permitopen ?  " permitopen" : "",
681664f4763Szrj 	    do_permitlisten ?  " permitlisten" : "",
682664f4763Szrj 	    opts->permit_port_forwarding_flag ? " port-forwarding" : "",
683664f4763Szrj 	    opts->cert_principals == NULL ? "" : " principals",
684664f4763Szrj 	    opts->permit_pty_flag ? " pty" : "",
68550a69bb5SSascha Wildner 	    opts->require_verify ? " uv" : "",
686664f4763Szrj 	    opts->force_tun_device == -1 ? "" : " tun=",
687664f4763Szrj 	    opts->force_tun_device == -1 ? "" : buf,
688664f4763Szrj 	    opts->permit_user_rc ? " user-rc" : "",
68950a69bb5SSascha Wildner 	    opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
690664f4763Szrj 
691664f4763Szrj 	debug("%s: %s", loc, msg);
692664f4763Szrj 	if (do_remote)
693664f4763Szrj 		auth_debug_add("%s: %s", loc, msg);
694664f4763Szrj 
695664f4763Szrj 	if (options.permit_user_env) {
696664f4763Szrj 		for (i = 0; i < opts->nenv; i++) {
697664f4763Szrj 			debug("%s: environment: %s", loc, opts->env[i]);
698664f4763Szrj 			if (do_remote) {
699664f4763Szrj 				auth_debug_add("%s: environment: %s",
700664f4763Szrj 				    loc, opts->env[i]);
701664f4763Szrj 			}
702664f4763Szrj 		}
703664f4763Szrj 	}
704664f4763Szrj 
705664f4763Szrj 	/* Go into a little more details for the local logs. */
706664f4763Szrj 	if (opts->valid_before != 0) {
707664f4763Szrj 		format_absolute_time(opts->valid_before, buf, sizeof(buf));
708664f4763Szrj 		debug("%s: expires at %s", loc, buf);
709664f4763Szrj 	}
710664f4763Szrj 	if (opts->cert_principals != NULL) {
711664f4763Szrj 		debug("%s: authorized principals: \"%s\"",
712664f4763Szrj 		    loc, opts->cert_principals);
713664f4763Szrj 	}
714664f4763Szrj 	if (opts->force_command != NULL)
715664f4763Szrj 		debug("%s: forced command: \"%s\"", loc, opts->force_command);
716664f4763Szrj 	if (do_permitopen) {
717664f4763Szrj 		for (i = 0; i < opts->npermitopen; i++) {
718664f4763Szrj 			debug("%s: permitted open: %s",
719664f4763Szrj 			    loc, opts->permitopen[i]);
720664f4763Szrj 		}
721664f4763Szrj 	}
722664f4763Szrj 	if (do_permitlisten) {
723664f4763Szrj 		for (i = 0; i < opts->npermitlisten; i++) {
724664f4763Szrj 			debug("%s: permitted listen: %s",
725664f4763Szrj 			    loc, opts->permitlisten[i]);
726664f4763Szrj 		}
727664f4763Szrj 	}
728664f4763Szrj }
729664f4763Szrj 
730664f4763Szrj /* Activate a new set of key/cert options; merging with what is there. */
731664f4763Szrj int
auth_activate_options(struct ssh * ssh,struct sshauthopt * opts)732664f4763Szrj auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
733664f4763Szrj {
734664f4763Szrj 	struct sshauthopt *old = auth_opts;
735664f4763Szrj 	const char *emsg = NULL;
736664f4763Szrj 
73750a69bb5SSascha Wildner 	debug_f("setting new authentication options");
738664f4763Szrj 	if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
739664f4763Szrj 		error("Inconsistent authentication options: %s", emsg);
740664f4763Szrj 		return -1;
741664f4763Szrj 	}
742664f4763Szrj 	return 0;
743664f4763Szrj }
744664f4763Szrj 
745664f4763Szrj /* Disable forwarding, etc for the session */
746664f4763Szrj void
auth_restrict_session(struct ssh * ssh)747664f4763Szrj auth_restrict_session(struct ssh *ssh)
748664f4763Szrj {
749664f4763Szrj 	struct sshauthopt *restricted;
750664f4763Szrj 
75150a69bb5SSascha Wildner 	debug_f("restricting session");
752664f4763Szrj 
753664f4763Szrj 	/* A blank sshauthopt defaults to permitting nothing */
754ee116499SAntonio Huete Jimenez 	if ((restricted = sshauthopt_new()) == NULL)
755ee116499SAntonio Huete Jimenez 		fatal_f("sshauthopt_new failed");
756664f4763Szrj 	restricted->permit_pty_flag = 1;
757664f4763Szrj 	restricted->restricted = 1;
758664f4763Szrj 
759664f4763Szrj 	if (auth_activate_options(ssh, restricted) != 0)
76050a69bb5SSascha Wildner 		fatal_f("failed to restrict session");
761664f4763Szrj 	sshauthopt_free(restricted);
762664f4763Szrj }
763