xref: /openbsd-src/usr.bin/ssh/auth.c (revision 7c0ec4b8992567abb1e1536622dc789a9a39d9f1)
1 /* $OpenBSD: auth.c,v 1.161 2024/05/17 00:30:23 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
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 *
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
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
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
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 *
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 *
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
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 *
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 	parse_server_match_config(&options, &includes, ci);
425 	log_change_level(options.log_level);
426 	log_verbose_reset();
427 	for (i = 0; i < options.num_log_verbose; i++)
428 		log_verbose_add(options.log_verbose[i]);
429 	server_process_permitopen(ssh);
430 
431 	pw = getpwnam(user);
432 	if (pw == NULL) {
433 		logit("Invalid user %.100s from %.100s port %d",
434 		    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
435 		return (NULL);
436 	}
437 	if (!allowed_user(ssh, pw))
438 		return (NULL);
439 	if ((lc = login_getclass(pw->pw_class)) == NULL) {
440 		debug("unable to get login class: %s", user);
441 		return (NULL);
442 	}
443 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
444 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
445 		debug("Approval failure for %s", user);
446 		pw = NULL;
447 	}
448 	if (as != NULL)
449 		auth_close(as);
450 	if (pw != NULL)
451 		return (pwcopy(pw));
452 	return (NULL);
453 }
454 
455 /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
456 int
457 auth_key_is_revoked(struct sshkey *key)
458 {
459 	char *fp = NULL;
460 	int r;
461 
462 	if (options.revoked_keys_file == NULL)
463 		return 0;
464 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
465 	    SSH_FP_DEFAULT)) == NULL) {
466 		r = SSH_ERR_ALLOC_FAIL;
467 		error_fr(r, "fingerprint key");
468 		goto out;
469 	}
470 
471 	r = sshkey_check_revoked(key, options.revoked_keys_file);
472 	switch (r) {
473 	case 0:
474 		break; /* not revoked */
475 	case SSH_ERR_KEY_REVOKED:
476 		error("Authentication key %s %s revoked by file %s",
477 		    sshkey_type(key), fp, options.revoked_keys_file);
478 		goto out;
479 	default:
480 		error_r(r, "Error checking authentication key %s %s in "
481 		    "revoked keys file %s", sshkey_type(key), fp,
482 		    options.revoked_keys_file);
483 		goto out;
484 	}
485 
486 	/* Success */
487 	r = 0;
488 
489  out:
490 	free(fp);
491 	return r == 0 ? 0 : 1;
492 }
493 
494 void
495 auth_debug_add(const char *fmt,...)
496 {
497 	char buf[1024];
498 	va_list args;
499 	int r;
500 
501 	va_start(args, fmt);
502 	vsnprintf(buf, sizeof(buf), fmt, args);
503 	va_end(args);
504 	debug3("%s", buf);
505 	if (auth_debug != NULL)
506 		if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
507 			fatal_fr(r, "sshbuf_put_cstring");
508 }
509 
510 void
511 auth_debug_send(struct ssh *ssh)
512 {
513 	char *msg;
514 	int r;
515 
516 	if (auth_debug == NULL)
517 		return;
518 	while (sshbuf_len(auth_debug) != 0) {
519 		if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
520 			fatal_fr(r, "sshbuf_get_cstring");
521 		ssh_packet_send_debug(ssh, "%s", msg);
522 		free(msg);
523 	}
524 }
525 
526 void
527 auth_debug_reset(void)
528 {
529 	if (auth_debug != NULL)
530 		sshbuf_reset(auth_debug);
531 	else if ((auth_debug = sshbuf_new()) == NULL)
532 		fatal_f("sshbuf_new failed");
533 }
534 
535 struct passwd *
536 fakepw(void)
537 {
538 	static int done = 0;
539 	static struct passwd fake;
540 	const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
541 	    "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
542 	char *cp;
543 
544 	if (done)
545 		return (&fake);
546 
547 	memset(&fake, 0, sizeof(fake));
548 	fake.pw_name = "NOUSER";
549 	fake.pw_passwd = xstrdup("$2a$10$"
550 	    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
551 	for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
552 		*cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
553 	fake.pw_gecos = "NOUSER";
554 	fake.pw_uid = (uid_t)-1;
555 	fake.pw_gid = (gid_t)-1;
556 	fake.pw_class = "";
557 	fake.pw_dir = "/nonexist";
558 	fake.pw_shell = "/nonexist";
559 	done = 1;
560 
561 	return (&fake);
562 }
563 
564 /*
565  * Return the canonical name of the host in the other side of the current
566  * connection.  The host name is cached, so it is efficient to call this
567  * several times.
568  */
569 
570 const char *
571 auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
572 {
573 	static char *dnsname;
574 
575 	if (!use_dns)
576 		return ssh_remote_ipaddr(ssh);
577 	if (dnsname != NULL)
578 		return dnsname;
579 	dnsname = ssh_remote_hostname(ssh);
580 	return dnsname;
581 }
582 
583 /* These functions link key/cert options to the auth framework */
584 
585 /* Log sshauthopt options locally and (optionally) for remote transmission */
586 void
587 auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
588 {
589 	int do_env = options.permit_user_env && opts->nenv > 0;
590 	int do_permitopen = opts->npermitopen > 0 &&
591 	    (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
592 	int do_permitlisten = opts->npermitlisten > 0 &&
593 	    (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
594 	size_t i;
595 	char msg[1024], buf[64];
596 
597 	snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
598 	/* Try to keep this alphabetically sorted */
599 	snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
600 	    opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
601 	    opts->force_command == NULL ? "" : " command",
602 	    do_env ?  " environment" : "",
603 	    opts->valid_before == 0 ? "" : "expires",
604 	    opts->no_require_user_presence ? " no-touch-required" : "",
605 	    do_permitopen ?  " permitopen" : "",
606 	    do_permitlisten ?  " permitlisten" : "",
607 	    opts->permit_port_forwarding_flag ? " port-forwarding" : "",
608 	    opts->cert_principals == NULL ? "" : " principals",
609 	    opts->permit_pty_flag ? " pty" : "",
610 	    opts->require_verify ? " uv" : "",
611 	    opts->force_tun_device == -1 ? "" : " tun=",
612 	    opts->force_tun_device == -1 ? "" : buf,
613 	    opts->permit_user_rc ? " user-rc" : "",
614 	    opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
615 
616 	debug("%s: %s", loc, msg);
617 	if (do_remote)
618 		auth_debug_add("%s: %s", loc, msg);
619 
620 	if (options.permit_user_env) {
621 		for (i = 0; i < opts->nenv; i++) {
622 			debug("%s: environment: %s", loc, opts->env[i]);
623 			if (do_remote) {
624 				auth_debug_add("%s: environment: %s",
625 				    loc, opts->env[i]);
626 			}
627 		}
628 	}
629 
630 	/* Go into a little more details for the local logs. */
631 	if (opts->valid_before != 0) {
632 		format_absolute_time(opts->valid_before, buf, sizeof(buf));
633 		debug("%s: expires at %s", loc, buf);
634 	}
635 	if (opts->cert_principals != NULL) {
636 		debug("%s: authorized principals: \"%s\"",
637 		    loc, opts->cert_principals);
638 	}
639 	if (opts->force_command != NULL)
640 		debug("%s: forced command: \"%s\"", loc, opts->force_command);
641 	if (do_permitopen) {
642 		for (i = 0; i < opts->npermitopen; i++) {
643 			debug("%s: permitted open: %s",
644 			    loc, opts->permitopen[i]);
645 		}
646 	}
647 	if (do_permitlisten) {
648 		for (i = 0; i < opts->npermitlisten; i++) {
649 			debug("%s: permitted listen: %s",
650 			    loc, opts->permitlisten[i]);
651 		}
652 	}
653 }
654 
655 /* Activate a new set of key/cert options; merging with what is there. */
656 int
657 auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
658 {
659 	struct sshauthopt *old = auth_opts;
660 	const char *emsg = NULL;
661 
662 	debug_f("setting new authentication options");
663 	if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
664 		error("Inconsistent authentication options: %s", emsg);
665 		return -1;
666 	}
667 	return 0;
668 }
669 
670 /* Disable forwarding, etc for the session */
671 void
672 auth_restrict_session(struct ssh *ssh)
673 {
674 	struct sshauthopt *restricted;
675 
676 	debug_f("restricting session");
677 
678 	/* A blank sshauthopt defaults to permitting nothing */
679 	if ((restricted = sshauthopt_new()) == NULL)
680 		fatal_f("sshauthopt_new failed");
681 	restricted->permit_pty_flag = 1;
682 	restricted->restricted = 1;
683 
684 	if (auth_activate_options(ssh, restricted) != 0)
685 		fatal_f("failed to restrict session");
686 	sshauthopt_free(restricted);
687 }
688