xref: /openbsd/usr.bin/ssh/auth.c (revision 5bfd970d)
1 /* $OpenBSD: auth.c,v 1.162 2024/09/15 01:18:26 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/wait.h>
30 
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <login_cap.h>
35 #include <paths.h>
36 #include <pwd.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <time.h>
44 
45 #include "xmalloc.h"
46 #include "match.h"
47 #include "groupaccess.h"
48 #include "log.h"
49 #include "sshbuf.h"
50 #include "misc.h"
51 #include "servconf.h"
52 #include "sshkey.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "canohost.h"
57 #include "uidswap.h"
58 #include "packet.h"
59 #ifdef GSSAPI
60 #include "ssh-gss.h"
61 #endif
62 #include "authfile.h"
63 #include "monitor_wrap.h"
64 #include "ssherr.h"
65 #include "channels.h"
66 
67 /* import */
68 extern ServerOptions options;
69 extern struct include_list includes;
70 extern struct sshauthopt *auth_opts;
71 
72 /* Debugging messages */
73 static struct sshbuf *auth_debug;
74 
75 /*
76  * Check if the user is allowed to log in via ssh. If user is listed
77  * in DenyUsers or one of user's groups is listed in DenyGroups, false
78  * will be returned. If AllowUsers isn't empty and user isn't listed
79  * there, or if AllowGroups isn't empty and one of user's groups isn't
80  * listed there, false will be returned.
81  * If the user's shell is not executable, false will be returned.
82  * Otherwise true is returned.
83  */
84 int
allowed_user(struct ssh * ssh,struct passwd * pw)85 allowed_user(struct ssh *ssh, struct passwd * pw)
86 {
87 	struct stat st;
88 	const char *hostname = NULL, *ipaddr = NULL;
89 	int r;
90 	u_int i;
91 
92 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
93 	if (!pw || !pw->pw_name)
94 		return 0;
95 
96 	/*
97 	 * Deny if shell does not exist or is not executable unless we
98 	 * are chrooting.
99 	 */
100 	if (options.chroot_directory == NULL ||
101 	    strcasecmp(options.chroot_directory, "none") == 0) {
102 		char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
103 		    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
104 
105 		if (stat(shell, &st) == -1) {
106 			logit("User %.100s not allowed because shell %.100s "
107 			    "does not exist", pw->pw_name, shell);
108 			free(shell);
109 			return 0;
110 		}
111 		if (S_ISREG(st.st_mode) == 0 ||
112 		    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
113 			logit("User %.100s not allowed because shell %.100s "
114 			    "is not executable", pw->pw_name, shell);
115 			free(shell);
116 			return 0;
117 		}
118 		free(shell);
119 	}
120 
121 	if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
122 	    options.num_deny_groups > 0 || options.num_allow_groups > 0) {
123 		hostname = auth_get_canonical_hostname(ssh, options.use_dns);
124 		ipaddr = ssh_remote_ipaddr(ssh);
125 	}
126 
127 	/* Return false if user is listed in DenyUsers */
128 	if (options.num_deny_users > 0) {
129 		for (i = 0; i < options.num_deny_users; i++) {
130 			r = match_user(pw->pw_name, hostname, ipaddr,
131 			    options.deny_users[i]);
132 			if (r < 0) {
133 				fatal("Invalid DenyUsers pattern \"%.100s\"",
134 				    options.deny_users[i]);
135 			} else if (r != 0) {
136 				logit("User %.100s from %.100s not allowed "
137 				    "because listed in DenyUsers",
138 				    pw->pw_name, hostname);
139 				return 0;
140 			}
141 		}
142 	}
143 	/* Return false if AllowUsers isn't empty and user isn't listed there */
144 	if (options.num_allow_users > 0) {
145 		for (i = 0; i < options.num_allow_users; i++) {
146 			r = match_user(pw->pw_name, hostname, ipaddr,
147 			    options.allow_users[i]);
148 			if (r < 0) {
149 				fatal("Invalid AllowUsers pattern \"%.100s\"",
150 				    options.allow_users[i]);
151 			} else if (r == 1)
152 				break;
153 		}
154 		/* i < options.num_allow_users iff we break for loop */
155 		if (i >= options.num_allow_users) {
156 			logit("User %.100s from %.100s not allowed because "
157 			    "not listed in AllowUsers", pw->pw_name, hostname);
158 			return 0;
159 		}
160 	}
161 	if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
162 		/* Get the user's group access list (primary and supplementary) */
163 		if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
164 			logit("User %.100s from %.100s not allowed because "
165 			    "not in any group", pw->pw_name, hostname);
166 			return 0;
167 		}
168 
169 		/* Return false if one of user's groups is listed in DenyGroups */
170 		if (options.num_deny_groups > 0)
171 			if (ga_match(options.deny_groups,
172 			    options.num_deny_groups)) {
173 				ga_free();
174 				logit("User %.100s from %.100s not allowed "
175 				    "because a group is listed in DenyGroups",
176 				    pw->pw_name, hostname);
177 				return 0;
178 			}
179 		/*
180 		 * Return false if AllowGroups isn't empty and one of user's groups
181 		 * isn't listed there
182 		 */
183 		if (options.num_allow_groups > 0)
184 			if (!ga_match(options.allow_groups,
185 			    options.num_allow_groups)) {
186 				ga_free();
187 				logit("User %.100s from %.100s not allowed "
188 				    "because none of user's groups are listed "
189 				    "in AllowGroups", pw->pw_name, hostname);
190 				return 0;
191 			}
192 		ga_free();
193 	}
194 	/* We found no reason not to let this user try to log on... */
195 	return 1;
196 }
197 
198 /*
199  * Formats any key left in authctxt->auth_method_key for inclusion in
200  * auth_log()'s message. Also includes authxtct->auth_method_info if present.
201  */
202 static char *
format_method_key(Authctxt * authctxt)203 format_method_key(Authctxt *authctxt)
204 {
205 	const struct sshkey *key = authctxt->auth_method_key;
206 	const char *methinfo = authctxt->auth_method_info;
207 	char *fp, *cafp, *ret = NULL;
208 
209 	if (key == NULL)
210 		return NULL;
211 
212 	if (sshkey_is_cert(key)) {
213 		fp = sshkey_fingerprint(key,
214 		    options.fingerprint_hash, SSH_FP_DEFAULT);
215 		cafp = sshkey_fingerprint(key->cert->signature_key,
216 		    options.fingerprint_hash, SSH_FP_DEFAULT);
217 		xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
218 		    sshkey_type(key), fp == NULL ? "(null)" : fp,
219 		    key->cert->key_id,
220 		    (unsigned long long)key->cert->serial,
221 		    sshkey_type(key->cert->signature_key),
222 		    cafp == NULL ? "(null)" : cafp,
223 		    methinfo == NULL ? "" : ", ",
224 		    methinfo == NULL ? "" : methinfo);
225 		free(fp);
226 		free(cafp);
227 	} else {
228 		fp = sshkey_fingerprint(key, options.fingerprint_hash,
229 		    SSH_FP_DEFAULT);
230 		xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
231 		    fp == NULL ? "(null)" : fp,
232 		    methinfo == NULL ? "" : ", ",
233 		    methinfo == NULL ? "" : methinfo);
234 		free(fp);
235 	}
236 	return ret;
237 }
238 
239 void
auth_log(struct ssh * ssh,int authenticated,int partial,const char * method,const char * submethod)240 auth_log(struct ssh *ssh, int authenticated, int partial,
241     const char *method, const char *submethod)
242 {
243 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
244 	int level = SYSLOG_LEVEL_VERBOSE;
245 	const char *authmsg;
246 	char *extra = NULL;
247 
248 	if (!mm_is_monitor() && !authctxt->postponed)
249 		return;
250 
251 	/* Raise logging level */
252 	if (authenticated == 1 ||
253 	    !authctxt->valid ||
254 	    authctxt->failures >= options.max_authtries / 2 ||
255 	    strcmp(method, "password") == 0)
256 		level = SYSLOG_LEVEL_INFO;
257 
258 	if (authctxt->postponed)
259 		authmsg = "Postponed";
260 	else if (partial)
261 		authmsg = "Partial";
262 	else
263 		authmsg = authenticated ? "Accepted" : "Failed";
264 
265 	if ((extra = format_method_key(authctxt)) == NULL) {
266 		if (authctxt->auth_method_info != NULL)
267 			extra = xstrdup(authctxt->auth_method_info);
268 	}
269 
270 	do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
271 	    authmsg,
272 	    method,
273 	    submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
274 	    authctxt->valid ? "" : "invalid user ",
275 	    authctxt->user,
276 	    ssh_remote_ipaddr(ssh),
277 	    ssh_remote_port(ssh),
278 	    extra != NULL ? ": " : "",
279 	    extra != NULL ? extra : "");
280 
281 	free(extra);
282 }
283 
284 void
auth_maxtries_exceeded(struct ssh * ssh)285 auth_maxtries_exceeded(struct ssh *ssh)
286 {
287 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
288 
289 	error("maximum authentication attempts exceeded for "
290 	    "%s%.100s from %.200s port %d ssh2",
291 	    authctxt->valid ? "" : "invalid user ",
292 	    authctxt->user,
293 	    ssh_remote_ipaddr(ssh),
294 	    ssh_remote_port(ssh));
295 	ssh_packet_disconnect(ssh, "Too many authentication failures");
296 	/* NOTREACHED */
297 }
298 
299 /*
300  * Check whether root logins are disallowed.
301  */
302 int
auth_root_allowed(struct ssh * ssh,const char * method)303 auth_root_allowed(struct ssh *ssh, const char *method)
304 {
305 	switch (options.permit_root_login) {
306 	case PERMIT_YES:
307 		return 1;
308 	case PERMIT_NO_PASSWD:
309 		if (strcmp(method, "publickey") == 0 ||
310 		    strcmp(method, "hostbased") == 0 ||
311 		    strcmp(method, "gssapi-with-mic") == 0)
312 			return 1;
313 		break;
314 	case PERMIT_FORCED_ONLY:
315 		if (auth_opts->force_command != NULL) {
316 			logit("Root login accepted for forced command.");
317 			return 1;
318 		}
319 		break;
320 	}
321 	logit("ROOT LOGIN REFUSED FROM %.200s port %d",
322 	    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
323 	return 0;
324 }
325 
326 
327 /*
328  * Given a template and a passwd structure, build a filename
329  * by substituting % tokenised options. Currently, %% becomes '%',
330  * %h becomes the home directory and %u the username.
331  *
332  * This returns a buffer allocated by xmalloc.
333  */
334 char *
expand_authorized_keys(const char * filename,struct passwd * pw)335 expand_authorized_keys(const char *filename, struct passwd *pw)
336 {
337 	char *file, uidstr[32], ret[PATH_MAX];
338 	int i;
339 
340 	snprintf(uidstr, sizeof(uidstr), "%llu",
341 	    (unsigned long long)pw->pw_uid);
342 	file = percent_expand(filename, "h", pw->pw_dir,
343 	    "u", pw->pw_name, "U", uidstr, (char *)NULL);
344 
345 	/*
346 	 * Ensure that filename starts anchored. If not, be backward
347 	 * compatible and prepend the '%h/'
348 	 */
349 	if (path_absolute(file))
350 		return (file);
351 
352 	i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
353 	if (i < 0 || (size_t)i >= sizeof(ret))
354 		fatal("expand_authorized_keys: path too long");
355 	free(file);
356 	return (xstrdup(ret));
357 }
358 
359 char *
authorized_principals_file(struct passwd * pw)360 authorized_principals_file(struct passwd *pw)
361 {
362 	if (options.authorized_principals_file == NULL)
363 		return NULL;
364 	return expand_authorized_keys(options.authorized_principals_file, pw);
365 }
366 
367 /* return ok if key exists in sysfile or userfile */
368 HostStatus
check_key_in_hostfiles(struct passwd * pw,struct sshkey * key,const char * host,const char * sysfile,const char * userfile)369 check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
370     const char *sysfile, const char *userfile)
371 {
372 	char *user_hostfile;
373 	struct stat st;
374 	HostStatus host_status;
375 	struct hostkeys *hostkeys;
376 	const struct hostkey_entry *found;
377 
378 	hostkeys = init_hostkeys();
379 	load_hostkeys(hostkeys, host, sysfile, 0);
380 	if (userfile != NULL) {
381 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
382 		if (options.strict_modes &&
383 		    (stat(user_hostfile, &st) == 0) &&
384 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
385 		    (st.st_mode & 022) != 0)) {
386 			logit("Authentication refused for %.100s: "
387 			    "bad owner or modes for %.200s",
388 			    pw->pw_name, user_hostfile);
389 			auth_debug_add("Ignored %.200s: bad ownership or modes",
390 			    user_hostfile);
391 		} else {
392 			temporarily_use_uid(pw);
393 			load_hostkeys(hostkeys, host, user_hostfile, 0);
394 			restore_uid();
395 		}
396 		free(user_hostfile);
397 	}
398 	host_status = check_key_in_hostkeys(hostkeys, key, &found);
399 	if (host_status == HOST_REVOKED)
400 		error("WARNING: revoked key for %s attempted authentication",
401 		    host);
402 	else if (host_status == HOST_OK)
403 		debug_f("key for %s found at %s:%ld",
404 		    found->host, found->file, found->line);
405 	else
406 		debug_f("key for host %s not found", host);
407 
408 	free_hostkeys(hostkeys);
409 
410 	return host_status;
411 }
412 
413 struct passwd *
getpwnamallow(struct ssh * ssh,const char * user)414 getpwnamallow(struct ssh *ssh, const char *user)
415 {
416 	extern login_cap_t *lc;
417 	auth_session_t *as;
418 	struct passwd *pw;
419 	struct connection_info *ci;
420 	u_int i;
421 
422 	ci = server_get_connection_info(ssh, 1, options.use_dns);
423 	ci->user = user;
424 	ci->user_invalid = getpwnam(user) == NULL;
425 	parse_server_match_config(&options, &includes, ci);
426 	log_change_level(options.log_level);
427 	log_verbose_reset();
428 	for (i = 0; i < options.num_log_verbose; i++)
429 		log_verbose_add(options.log_verbose[i]);
430 	server_process_permitopen(ssh);
431 
432 	pw = getpwnam(user);
433 	if (pw == NULL) {
434 		logit("Invalid user %.100s from %.100s port %d",
435 		    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
436 		return (NULL);
437 	}
438 	if (!allowed_user(ssh, pw))
439 		return (NULL);
440 	if ((lc = login_getclass(pw->pw_class)) == NULL) {
441 		debug("unable to get login class: %s", user);
442 		return (NULL);
443 	}
444 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
445 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
446 		debug("Approval failure for %s", user);
447 		pw = NULL;
448 	}
449 	if (as != NULL)
450 		auth_close(as);
451 	if (pw != NULL)
452 		return (pwcopy(pw));
453 	return (NULL);
454 }
455 
456 /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
457 int
auth_key_is_revoked(struct sshkey * key)458 auth_key_is_revoked(struct sshkey *key)
459 {
460 	char *fp = NULL;
461 	int r;
462 
463 	if (options.revoked_keys_file == NULL)
464 		return 0;
465 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
466 	    SSH_FP_DEFAULT)) == NULL) {
467 		r = SSH_ERR_ALLOC_FAIL;
468 		error_fr(r, "fingerprint key");
469 		goto out;
470 	}
471 
472 	r = sshkey_check_revoked(key, options.revoked_keys_file);
473 	switch (r) {
474 	case 0:
475 		break; /* not revoked */
476 	case SSH_ERR_KEY_REVOKED:
477 		error("Authentication key %s %s revoked by file %s",
478 		    sshkey_type(key), fp, options.revoked_keys_file);
479 		goto out;
480 	default:
481 		error_r(r, "Error checking authentication key %s %s in "
482 		    "revoked keys file %s", sshkey_type(key), fp,
483 		    options.revoked_keys_file);
484 		goto out;
485 	}
486 
487 	/* Success */
488 	r = 0;
489 
490  out:
491 	free(fp);
492 	return r == 0 ? 0 : 1;
493 }
494 
495 void
auth_debug_add(const char * fmt,...)496 auth_debug_add(const char *fmt,...)
497 {
498 	char buf[1024];
499 	va_list args;
500 	int r;
501 
502 	va_start(args, fmt);
503 	vsnprintf(buf, sizeof(buf), fmt, args);
504 	va_end(args);
505 	debug3("%s", buf);
506 	if (auth_debug != NULL)
507 		if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
508 			fatal_fr(r, "sshbuf_put_cstring");
509 }
510 
511 void
auth_debug_send(struct ssh * ssh)512 auth_debug_send(struct ssh *ssh)
513 {
514 	char *msg;
515 	int r;
516 
517 	if (auth_debug == NULL)
518 		return;
519 	while (sshbuf_len(auth_debug) != 0) {
520 		if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
521 			fatal_fr(r, "sshbuf_get_cstring");
522 		ssh_packet_send_debug(ssh, "%s", msg);
523 		free(msg);
524 	}
525 }
526 
527 void
auth_debug_reset(void)528 auth_debug_reset(void)
529 {
530 	if (auth_debug != NULL)
531 		sshbuf_reset(auth_debug);
532 	else if ((auth_debug = sshbuf_new()) == NULL)
533 		fatal_f("sshbuf_new failed");
534 }
535 
536 struct passwd *
fakepw(void)537 fakepw(void)
538 {
539 	static int done = 0;
540 	static struct passwd fake;
541 	const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
542 	    "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
543 	char *cp;
544 
545 	if (done)
546 		return (&fake);
547 
548 	memset(&fake, 0, sizeof(fake));
549 	fake.pw_name = "NOUSER";
550 	fake.pw_passwd = xstrdup("$2a$10$"
551 	    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
552 	for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
553 		*cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
554 	fake.pw_gecos = "NOUSER";
555 	fake.pw_uid = (uid_t)-1;
556 	fake.pw_gid = (gid_t)-1;
557 	fake.pw_class = "";
558 	fake.pw_dir = "/nonexist";
559 	fake.pw_shell = "/nonexist";
560 	done = 1;
561 
562 	return (&fake);
563 }
564 
565 /*
566  * Return the canonical name of the host in the other side of the current
567  * connection.  The host name is cached, so it is efficient to call this
568  * several times.
569  */
570 
571 const char *
auth_get_canonical_hostname(struct ssh * ssh,int use_dns)572 auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
573 {
574 	static char *dnsname;
575 
576 	if (!use_dns)
577 		return ssh_remote_ipaddr(ssh);
578 	if (dnsname != NULL)
579 		return dnsname;
580 	dnsname = ssh_remote_hostname(ssh);
581 	return dnsname;
582 }
583 
584 /* These functions link key/cert options to the auth framework */
585 
586 /* Log sshauthopt options locally and (optionally) for remote transmission */
587 void
auth_log_authopts(const char * loc,const struct sshauthopt * opts,int do_remote)588 auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
589 {
590 	int do_env = options.permit_user_env && opts->nenv > 0;
591 	int do_permitopen = opts->npermitopen > 0 &&
592 	    (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
593 	int do_permitlisten = opts->npermitlisten > 0 &&
594 	    (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
595 	size_t i;
596 	char msg[1024], buf[64];
597 
598 	snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
599 	/* Try to keep this alphabetically sorted */
600 	snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
601 	    opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
602 	    opts->force_command == NULL ? "" : " command",
603 	    do_env ?  " environment" : "",
604 	    opts->valid_before == 0 ? "" : "expires",
605 	    opts->no_require_user_presence ? " no-touch-required" : "",
606 	    do_permitopen ?  " permitopen" : "",
607 	    do_permitlisten ?  " permitlisten" : "",
608 	    opts->permit_port_forwarding_flag ? " port-forwarding" : "",
609 	    opts->cert_principals == NULL ? "" : " principals",
610 	    opts->permit_pty_flag ? " pty" : "",
611 	    opts->require_verify ? " uv" : "",
612 	    opts->force_tun_device == -1 ? "" : " tun=",
613 	    opts->force_tun_device == -1 ? "" : buf,
614 	    opts->permit_user_rc ? " user-rc" : "",
615 	    opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
616 
617 	debug("%s: %s", loc, msg);
618 	if (do_remote)
619 		auth_debug_add("%s: %s", loc, msg);
620 
621 	if (options.permit_user_env) {
622 		for (i = 0; i < opts->nenv; i++) {
623 			debug("%s: environment: %s", loc, opts->env[i]);
624 			if (do_remote) {
625 				auth_debug_add("%s: environment: %s",
626 				    loc, opts->env[i]);
627 			}
628 		}
629 	}
630 
631 	/* Go into a little more details for the local logs. */
632 	if (opts->valid_before != 0) {
633 		format_absolute_time(opts->valid_before, buf, sizeof(buf));
634 		debug("%s: expires at %s", loc, buf);
635 	}
636 	if (opts->cert_principals != NULL) {
637 		debug("%s: authorized principals: \"%s\"",
638 		    loc, opts->cert_principals);
639 	}
640 	if (opts->force_command != NULL)
641 		debug("%s: forced command: \"%s\"", loc, opts->force_command);
642 	if (do_permitopen) {
643 		for (i = 0; i < opts->npermitopen; i++) {
644 			debug("%s: permitted open: %s",
645 			    loc, opts->permitopen[i]);
646 		}
647 	}
648 	if (do_permitlisten) {
649 		for (i = 0; i < opts->npermitlisten; i++) {
650 			debug("%s: permitted listen: %s",
651 			    loc, opts->permitlisten[i]);
652 		}
653 	}
654 }
655 
656 /* Activate a new set of key/cert options; merging with what is there. */
657 int
auth_activate_options(struct ssh * ssh,struct sshauthopt * opts)658 auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
659 {
660 	struct sshauthopt *old = auth_opts;
661 	const char *emsg = NULL;
662 
663 	debug_f("setting new authentication options");
664 	if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
665 		error("Inconsistent authentication options: %s", emsg);
666 		return -1;
667 	}
668 	return 0;
669 }
670 
671 /* Disable forwarding, etc for the session */
672 void
auth_restrict_session(struct ssh * ssh)673 auth_restrict_session(struct ssh *ssh)
674 {
675 	struct sshauthopt *restricted;
676 
677 	debug_f("restricting session");
678 
679 	/* A blank sshauthopt defaults to permitting nothing */
680 	if ((restricted = sshauthopt_new()) == NULL)
681 		fatal_f("sshauthopt_new failed");
682 	restricted->permit_pty_flag = 1;
683 	restricted->restricted = 1;
684 
685 	if (auth_activate_options(ssh, restricted) != 0)
686 		fatal_f("failed to restrict session");
687 	sshauthopt_free(restricted);
688 }
689