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