xref: /freebsd/crypto/openssh/auth.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 /* $OpenBSD: auth.c,v 1.154 2022/02/23 11:17:10 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 "includes.h"
27 __RCSID("$FreeBSD$");
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <sys/wait.h>
33 
34 #include <netinet/in.h>
35 
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #ifdef HAVE_PATHS_H
40 # include <paths.h>
41 #endif
42 #include <pwd.h>
43 #ifdef HAVE_LOGIN_H
44 #include <login.h>
45 #endif
46 #ifdef USE_SHADOW
47 #include <shadow.h>
48 #endif
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <limits.h>
54 #include <netdb.h>
55 #include <time.h>
56 
57 #include "xmalloc.h"
58 #include "match.h"
59 #include "groupaccess.h"
60 #include "log.h"
61 #include "sshbuf.h"
62 #include "misc.h"
63 #include "servconf.h"
64 #include "sshkey.h"
65 #include "hostfile.h"
66 #include "auth.h"
67 #include "auth-options.h"
68 #include "canohost.h"
69 #include "uidswap.h"
70 #include "packet.h"
71 #include "loginrec.h"
72 #ifdef GSSAPI
73 #include "ssh-gss.h"
74 #endif
75 #include "authfile.h"
76 #include "monitor_wrap.h"
77 #include "ssherr.h"
78 #include "compat.h"
79 #include "channels.h"
80 #include "blacklist_client.h"
81 
82 /* import */
83 extern ServerOptions options;
84 extern struct include_list includes;
85 extern int use_privsep;
86 extern struct sshbuf *loginmsg;
87 extern struct passwd *privsep_pw;
88 extern struct sshauthopt *auth_opts;
89 
90 /* Debugging messages */
91 static struct sshbuf *auth_debug;
92 
93 /*
94  * Check if the user is allowed to log in via ssh. If user is listed
95  * in DenyUsers or one of user's groups is listed in DenyGroups, false
96  * will be returned. If AllowUsers isn't empty and user isn't listed
97  * there, or if AllowGroups isn't empty and one of user's groups isn't
98  * listed there, false will be returned.
99  * If the user's shell is not executable, false will be returned.
100  * Otherwise true is returned.
101  */
102 int
103 allowed_user(struct ssh *ssh, struct passwd * pw)
104 {
105 	struct stat st;
106 	const char *hostname = NULL, *ipaddr = NULL, *passwd = NULL;
107 	u_int i;
108 	int r;
109 #ifdef USE_SHADOW
110 	struct spwd *spw = NULL;
111 #endif
112 
113 	/* Shouldn't be called if pw is NULL, but better safe than sorry... */
114 	if (!pw || !pw->pw_name)
115 		return 0;
116 
117 #ifdef USE_SHADOW
118 	if (!options.use_pam)
119 		spw = getspnam(pw->pw_name);
120 #ifdef HAS_SHADOW_EXPIRE
121 	if (!options.use_pam && spw != NULL && auth_shadow_acctexpired(spw))
122 		return 0;
123 #endif /* HAS_SHADOW_EXPIRE */
124 #endif /* USE_SHADOW */
125 
126 	/* grab passwd field for locked account check */
127 	passwd = pw->pw_passwd;
128 #ifdef USE_SHADOW
129 	if (spw != NULL)
130 #ifdef USE_LIBIAF
131 		passwd = get_iaf_password(pw);
132 #else
133 		passwd = spw->sp_pwdp;
134 #endif /* USE_LIBIAF */
135 #endif
136 
137 	/* check for locked account */
138 	if (!options.use_pam && passwd && *passwd) {
139 		int locked = 0;
140 
141 #ifdef LOCKED_PASSWD_STRING
142 		if (strcmp(passwd, LOCKED_PASSWD_STRING) == 0)
143 			 locked = 1;
144 #endif
145 #ifdef LOCKED_PASSWD_PREFIX
146 		if (strncmp(passwd, LOCKED_PASSWD_PREFIX,
147 		    strlen(LOCKED_PASSWD_PREFIX)) == 0)
148 			 locked = 1;
149 #endif
150 #ifdef LOCKED_PASSWD_SUBSTR
151 		if (strstr(passwd, LOCKED_PASSWD_SUBSTR))
152 			locked = 1;
153 #endif
154 #ifdef USE_LIBIAF
155 		free((void *) passwd);
156 #endif /* USE_LIBIAF */
157 		if (locked) {
158 			logit("User %.100s not allowed because account is locked",
159 			    pw->pw_name);
160 			return 0;
161 		}
162 	}
163 
164 	/*
165 	 * Deny if shell does not exist or is not executable unless we
166 	 * are chrooting.
167 	 */
168 	if (options.chroot_directory == NULL ||
169 	    strcasecmp(options.chroot_directory, "none") == 0) {
170 		char *shell = xstrdup((pw->pw_shell[0] == '\0') ?
171 		    _PATH_BSHELL : pw->pw_shell); /* empty = /bin/sh */
172 
173 		if (stat(shell, &st) == -1) {
174 			logit("User %.100s not allowed because shell %.100s "
175 			    "does not exist", pw->pw_name, shell);
176 			free(shell);
177 			return 0;
178 		}
179 		if (S_ISREG(st.st_mode) == 0 ||
180 		    (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
181 			logit("User %.100s not allowed because shell %.100s "
182 			    "is not executable", pw->pw_name, shell);
183 			free(shell);
184 			return 0;
185 		}
186 		free(shell);
187 	}
188 
189 	if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
190 	    options.num_deny_groups > 0 || options.num_allow_groups > 0) {
191 		hostname = auth_get_canonical_hostname(ssh, options.use_dns);
192 		ipaddr = ssh_remote_ipaddr(ssh);
193 	}
194 
195 	/* Return false if user is listed in DenyUsers */
196 	if (options.num_deny_users > 0) {
197 		for (i = 0; i < options.num_deny_users; i++) {
198 			r = match_user(pw->pw_name, hostname, ipaddr,
199 			    options.deny_users[i]);
200 			if (r < 0) {
201 				fatal("Invalid DenyUsers pattern \"%.100s\"",
202 				    options.deny_users[i]);
203 			} else if (r != 0) {
204 				logit("User %.100s from %.100s not allowed "
205 				    "because listed in DenyUsers",
206 				    pw->pw_name, hostname);
207 				return 0;
208 			}
209 		}
210 	}
211 	/* Return false if AllowUsers isn't empty and user isn't listed there */
212 	if (options.num_allow_users > 0) {
213 		for (i = 0; i < options.num_allow_users; i++) {
214 			r = match_user(pw->pw_name, hostname, ipaddr,
215 			    options.allow_users[i]);
216 			if (r < 0) {
217 				fatal("Invalid AllowUsers pattern \"%.100s\"",
218 				    options.allow_users[i]);
219 			} else if (r == 1)
220 				break;
221 		}
222 		/* i < options.num_allow_users iff we break for loop */
223 		if (i >= options.num_allow_users) {
224 			logit("User %.100s from %.100s not allowed because "
225 			    "not listed in AllowUsers", pw->pw_name, hostname);
226 			return 0;
227 		}
228 	}
229 	if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
230 		/* Get the user's group access list (primary and supplementary) */
231 		if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
232 			logit("User %.100s from %.100s not allowed because "
233 			    "not in any group", pw->pw_name, hostname);
234 			return 0;
235 		}
236 
237 		/* Return false if one of user's groups is listed in DenyGroups */
238 		if (options.num_deny_groups > 0)
239 			if (ga_match(options.deny_groups,
240 			    options.num_deny_groups)) {
241 				ga_free();
242 				logit("User %.100s from %.100s not allowed "
243 				    "because a group is listed in DenyGroups",
244 				    pw->pw_name, hostname);
245 				return 0;
246 			}
247 		/*
248 		 * Return false if AllowGroups isn't empty and one of user's groups
249 		 * isn't listed there
250 		 */
251 		if (options.num_allow_groups > 0)
252 			if (!ga_match(options.allow_groups,
253 			    options.num_allow_groups)) {
254 				ga_free();
255 				logit("User %.100s from %.100s not allowed "
256 				    "because none of user's groups are listed "
257 				    "in AllowGroups", pw->pw_name, hostname);
258 				return 0;
259 			}
260 		ga_free();
261 	}
262 
263 #ifdef CUSTOM_SYS_AUTH_ALLOWED_USER
264 	if (!sys_auth_allowed_user(pw, loginmsg))
265 		return 0;
266 #endif
267 
268 	/* We found no reason not to let this user try to log on... */
269 	return 1;
270 }
271 
272 /*
273  * Formats any key left in authctxt->auth_method_key for inclusion in
274  * auth_log()'s message. Also includes authxtct->auth_method_info if present.
275  */
276 static char *
277 format_method_key(Authctxt *authctxt)
278 {
279 	const struct sshkey *key = authctxt->auth_method_key;
280 	const char *methinfo = authctxt->auth_method_info;
281 	char *fp, *cafp, *ret = NULL;
282 
283 	if (key == NULL)
284 		return NULL;
285 
286 	if (sshkey_is_cert(key)) {
287 		fp = sshkey_fingerprint(key,
288 		    options.fingerprint_hash, SSH_FP_DEFAULT);
289 		cafp = sshkey_fingerprint(key->cert->signature_key,
290 		    options.fingerprint_hash, SSH_FP_DEFAULT);
291 		xasprintf(&ret, "%s %s ID %s (serial %llu) CA %s %s%s%s",
292 		    sshkey_type(key), fp == NULL ? "(null)" : fp,
293 		    key->cert->key_id,
294 		    (unsigned long long)key->cert->serial,
295 		    sshkey_type(key->cert->signature_key),
296 		    cafp == NULL ? "(null)" : cafp,
297 		    methinfo == NULL ? "" : ", ",
298 		    methinfo == NULL ? "" : methinfo);
299 		free(fp);
300 		free(cafp);
301 	} else {
302 		fp = sshkey_fingerprint(key, options.fingerprint_hash,
303 		    SSH_FP_DEFAULT);
304 		xasprintf(&ret, "%s %s%s%s", sshkey_type(key),
305 		    fp == NULL ? "(null)" : fp,
306 		    methinfo == NULL ? "" : ", ",
307 		    methinfo == NULL ? "" : methinfo);
308 		free(fp);
309 	}
310 	return ret;
311 }
312 
313 void
314 auth_log(struct ssh *ssh, int authenticated, int partial,
315     const char *method, const char *submethod)
316 {
317 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
318 	int level = SYSLOG_LEVEL_VERBOSE;
319 	const char *authmsg;
320 	char *extra = NULL;
321 
322 	if (use_privsep && !mm_is_monitor() && !authctxt->postponed)
323 		return;
324 
325 	/* Raise logging level */
326 	if (authenticated == 1 ||
327 	    !authctxt->valid ||
328 	    authctxt->failures >= options.max_authtries / 2 ||
329 	    strcmp(method, "password") == 0)
330 		level = SYSLOG_LEVEL_INFO;
331 
332 	if (authctxt->postponed)
333 		authmsg = "Postponed";
334 	else if (partial)
335 		authmsg = "Partial";
336 	else {
337 		authmsg = authenticated ? "Accepted" : "Failed";
338 		if (authenticated)
339 			BLACKLIST_NOTIFY(ssh, BLACKLIST_AUTH_OK, "ssh");
340 	}
341 
342 	if ((extra = format_method_key(authctxt)) == NULL) {
343 		if (authctxt->auth_method_info != NULL)
344 			extra = xstrdup(authctxt->auth_method_info);
345 	}
346 
347 	do_log2(level, "%s %s%s%s for %s%.100s from %.200s port %d ssh2%s%s",
348 	    authmsg,
349 	    method,
350 	    submethod != NULL ? "/" : "", submethod == NULL ? "" : submethod,
351 	    authctxt->valid ? "" : "invalid user ",
352 	    authctxt->user,
353 	    ssh_remote_ipaddr(ssh),
354 	    ssh_remote_port(ssh),
355 	    extra != NULL ? ": " : "",
356 	    extra != NULL ? extra : "");
357 
358 	free(extra);
359 
360 #if defined(CUSTOM_FAILED_LOGIN) || defined(SSH_AUDIT_EVENTS)
361 	if (authenticated == 0 && !(authctxt->postponed || partial)) {
362 		/* Log failed login attempt */
363 # ifdef CUSTOM_FAILED_LOGIN
364 		if (strcmp(method, "password") == 0 ||
365 		    strncmp(method, "keyboard-interactive", 20) == 0 ||
366 		    strcmp(method, "challenge-response") == 0)
367 			record_failed_login(ssh, authctxt->user,
368 			    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
369 # endif
370 # ifdef SSH_AUDIT_EVENTS
371 		audit_event(ssh, audit_classify_auth(method));
372 # endif
373 	}
374 #endif
375 #if defined(CUSTOM_FAILED_LOGIN) && defined(WITH_AIXAUTHENTICATE)
376 	if (authenticated)
377 		sys_auth_record_login(authctxt->user,
378 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh",
379 		    loginmsg);
380 #endif
381 }
382 
383 void
384 auth_maxtries_exceeded(struct ssh *ssh)
385 {
386 	Authctxt *authctxt = (Authctxt *)ssh->authctxt;
387 
388 	error("maximum authentication attempts exceeded for "
389 	    "%s%.100s from %.200s port %d ssh2",
390 	    authctxt->valid ? "" : "invalid user ",
391 	    authctxt->user,
392 	    ssh_remote_ipaddr(ssh),
393 	    ssh_remote_port(ssh));
394 	ssh_packet_disconnect(ssh, "Too many authentication failures");
395 	/* NOTREACHED */
396 }
397 
398 /*
399  * Check whether root logins are disallowed.
400  */
401 int
402 auth_root_allowed(struct ssh *ssh, const char *method)
403 {
404 	switch (options.permit_root_login) {
405 	case PERMIT_YES:
406 		return 1;
407 	case PERMIT_NO_PASSWD:
408 		if (strcmp(method, "publickey") == 0 ||
409 		    strcmp(method, "hostbased") == 0 ||
410 		    strcmp(method, "gssapi-with-mic") == 0)
411 			return 1;
412 		break;
413 	case PERMIT_FORCED_ONLY:
414 		if (auth_opts->force_command != NULL) {
415 			logit("Root login accepted for forced command.");
416 			return 1;
417 		}
418 		break;
419 	}
420 	logit("ROOT LOGIN REFUSED FROM %.200s port %d",
421 	    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
422 	return 0;
423 }
424 
425 
426 /*
427  * Given a template and a passwd structure, build a filename
428  * by substituting % tokenised options. Currently, %% becomes '%',
429  * %h becomes the home directory and %u the username.
430  *
431  * This returns a buffer allocated by xmalloc.
432  */
433 char *
434 expand_authorized_keys(const char *filename, struct passwd *pw)
435 {
436 	char *file, uidstr[32], ret[PATH_MAX];
437 	int i;
438 
439 	snprintf(uidstr, sizeof(uidstr), "%llu",
440 	    (unsigned long long)pw->pw_uid);
441 	file = percent_expand(filename, "h", pw->pw_dir,
442 	    "u", pw->pw_name, "U", uidstr, (char *)NULL);
443 
444 	/*
445 	 * Ensure that filename starts anchored. If not, be backward
446 	 * compatible and prepend the '%h/'
447 	 */
448 	if (path_absolute(file))
449 		return (file);
450 
451 	i = snprintf(ret, sizeof(ret), "%s/%s", pw->pw_dir, file);
452 	if (i < 0 || (size_t)i >= sizeof(ret))
453 		fatal("expand_authorized_keys: path too long");
454 	free(file);
455 	return (xstrdup(ret));
456 }
457 
458 char *
459 authorized_principals_file(struct passwd *pw)
460 {
461 	if (options.authorized_principals_file == NULL)
462 		return NULL;
463 	return expand_authorized_keys(options.authorized_principals_file, pw);
464 }
465 
466 /* return ok if key exists in sysfile or userfile */
467 HostStatus
468 check_key_in_hostfiles(struct passwd *pw, struct sshkey *key, const char *host,
469     const char *sysfile, const char *userfile)
470 {
471 	char *user_hostfile;
472 	struct stat st;
473 	HostStatus host_status;
474 	struct hostkeys *hostkeys;
475 	const struct hostkey_entry *found;
476 
477 	hostkeys = init_hostkeys();
478 	load_hostkeys(hostkeys, host, sysfile, 0);
479 	if (userfile != NULL) {
480 		user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
481 		if (options.strict_modes &&
482 		    (stat(user_hostfile, &st) == 0) &&
483 		    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
484 		    (st.st_mode & 022) != 0)) {
485 			logit("Authentication refused for %.100s: "
486 			    "bad owner or modes for %.200s",
487 			    pw->pw_name, user_hostfile);
488 			auth_debug_add("Ignored %.200s: bad ownership or modes",
489 			    user_hostfile);
490 		} else {
491 			temporarily_use_uid(pw);
492 			load_hostkeys(hostkeys, host, user_hostfile, 0);
493 			restore_uid();
494 		}
495 		free(user_hostfile);
496 	}
497 	host_status = check_key_in_hostkeys(hostkeys, key, &found);
498 	if (host_status == HOST_REVOKED)
499 		error("WARNING: revoked key for %s attempted authentication",
500 		    host);
501 	else if (host_status == HOST_OK)
502 		debug_f("key for %s found at %s:%ld",
503 		    found->host, found->file, found->line);
504 	else
505 		debug_f("key for host %s not found", host);
506 
507 	free_hostkeys(hostkeys);
508 
509 	return host_status;
510 }
511 
512 static FILE *
513 auth_openfile(const char *file, struct passwd *pw, int strict_modes,
514     int log_missing, char *file_type)
515 {
516 	char line[1024];
517 	struct stat st;
518 	int fd;
519 	FILE *f;
520 
521 	if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
522 		if (log_missing || errno != ENOENT)
523 			debug("Could not open %s '%s': %s", file_type, file,
524 			    strerror(errno));
525 		return NULL;
526 	}
527 
528 	if (fstat(fd, &st) == -1) {
529 		close(fd);
530 		return NULL;
531 	}
532 	if (!S_ISREG(st.st_mode)) {
533 		logit("User %s %s %s is not a regular file",
534 		    pw->pw_name, file_type, file);
535 		close(fd);
536 		return NULL;
537 	}
538 	unset_nonblock(fd);
539 	if ((f = fdopen(fd, "r")) == NULL) {
540 		close(fd);
541 		return NULL;
542 	}
543 	if (strict_modes &&
544 	    safe_path_fd(fileno(f), file, pw, line, sizeof(line)) != 0) {
545 		fclose(f);
546 		logit("Authentication refused: %s", line);
547 		auth_debug_add("Ignored %s: %s", file_type, line);
548 		return NULL;
549 	}
550 
551 	return f;
552 }
553 
554 
555 FILE *
556 auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
557 {
558 	return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
559 }
560 
561 FILE *
562 auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
563 {
564 	return auth_openfile(file, pw, strict_modes, 0,
565 	    "authorized principals");
566 }
567 
568 struct passwd *
569 getpwnamallow(struct ssh *ssh, const char *user)
570 {
571 #ifdef HAVE_LOGIN_CAP
572 	extern login_cap_t *lc;
573 #ifdef HAVE_AUTH_HOSTOK
574 	const char *from_host, *from_ip;
575 #endif
576 #ifdef BSD_AUTH
577 	auth_session_t *as;
578 #endif
579 #endif
580 	struct passwd *pw;
581 	struct connection_info *ci;
582 	u_int i;
583 
584 	ci = get_connection_info(ssh, 1, options.use_dns);
585 	ci->user = user;
586 	parse_server_match_config(&options, &includes, ci);
587 	log_change_level(options.log_level);
588 	log_verbose_reset();
589 	for (i = 0; i < options.num_log_verbose; i++)
590 		log_verbose_add(options.log_verbose[i]);
591 	process_permitopen(ssh, &options);
592 
593 #if defined(_AIX) && defined(HAVE_SETAUTHDB)
594 	aix_setauthdb(user);
595 #endif
596 
597 	pw = getpwnam(user);
598 
599 #if defined(_AIX) && defined(HAVE_SETAUTHDB)
600 	aix_restoreauthdb();
601 #endif
602 	if (pw == NULL) {
603 		BLACKLIST_NOTIFY(ssh, BLACKLIST_BAD_USER, user);
604 		logit("Invalid user %.100s from %.100s port %d",
605 		    user, ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
606 #ifdef CUSTOM_FAILED_LOGIN
607 		record_failed_login(ssh, user,
608 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
609 #endif
610 #ifdef SSH_AUDIT_EVENTS
611 		audit_event(ssh, SSH_INVALID_USER);
612 #endif /* SSH_AUDIT_EVENTS */
613 		return (NULL);
614 	}
615 	if (!allowed_user(ssh, pw))
616 		return (NULL);
617 #ifdef HAVE_LOGIN_CAP
618 	if ((lc = login_getpwclass(pw)) == NULL) {
619 		debug("unable to get login class: %s", user);
620 		return (NULL);
621 	}
622 #ifdef HAVE_AUTH_HOSTOK
623 	from_host = auth_get_canonical_hostname(ssh, options.use_dns);
624 	from_ip = ssh_remote_ipaddr(ssh);
625 	if (!auth_hostok(lc, from_host, from_ip)) {
626 		debug("Denied connection for %.200s from %.200s [%.200s].",
627 		    pw->pw_name, from_host, from_ip);
628 		return (NULL);
629 	}
630 #endif /* HAVE_AUTH_HOSTOK */
631 #ifdef HAVE_AUTH_TIMEOK
632 	if (!auth_timeok(lc, time(NULL))) {
633 		debug("LOGIN %.200s REFUSED (TIME)", pw->pw_name);
634 		return (NULL);
635 	}
636 #endif /* HAVE_AUTH_TIMEOK */
637 #ifdef BSD_AUTH
638 	if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
639 	    auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
640 		debug("Approval failure for %s", user);
641 		pw = NULL;
642 	}
643 	if (as != NULL)
644 		auth_close(as);
645 #endif
646 #endif
647 	if (pw != NULL)
648 		return (pwcopy(pw));
649 	return (NULL);
650 }
651 
652 /* Returns 1 if key is revoked by revoked_keys_file, 0 otherwise */
653 int
654 auth_key_is_revoked(struct sshkey *key)
655 {
656 	char *fp = NULL;
657 	int r;
658 
659 	if (options.revoked_keys_file == NULL)
660 		return 0;
661 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
662 	    SSH_FP_DEFAULT)) == NULL) {
663 		r = SSH_ERR_ALLOC_FAIL;
664 		error_fr(r, "fingerprint key");
665 		goto out;
666 	}
667 
668 	r = sshkey_check_revoked(key, options.revoked_keys_file);
669 	switch (r) {
670 	case 0:
671 		break; /* not revoked */
672 	case SSH_ERR_KEY_REVOKED:
673 		error("Authentication key %s %s revoked by file %s",
674 		    sshkey_type(key), fp, options.revoked_keys_file);
675 		goto out;
676 	default:
677 		error_r(r, "Error checking authentication key %s %s in "
678 		    "revoked keys file %s", sshkey_type(key), fp,
679 		    options.revoked_keys_file);
680 		goto out;
681 	}
682 
683 	/* Success */
684 	r = 0;
685 
686  out:
687 	free(fp);
688 	return r == 0 ? 0 : 1;
689 }
690 
691 void
692 auth_debug_add(const char *fmt,...)
693 {
694 	char buf[1024];
695 	va_list args;
696 	int r;
697 
698 	if (auth_debug == NULL)
699 		return;
700 
701 	va_start(args, fmt);
702 	vsnprintf(buf, sizeof(buf), fmt, args);
703 	va_end(args);
704 	if ((r = sshbuf_put_cstring(auth_debug, buf)) != 0)
705 		fatal_fr(r, "sshbuf_put_cstring");
706 }
707 
708 void
709 auth_debug_send(struct ssh *ssh)
710 {
711 	char *msg;
712 	int r;
713 
714 	if (auth_debug == NULL)
715 		return;
716 	while (sshbuf_len(auth_debug) != 0) {
717 		if ((r = sshbuf_get_cstring(auth_debug, &msg, NULL)) != 0)
718 			fatal_fr(r, "sshbuf_get_cstring");
719 		ssh_packet_send_debug(ssh, "%s", msg);
720 		free(msg);
721 	}
722 }
723 
724 void
725 auth_debug_reset(void)
726 {
727 	if (auth_debug != NULL)
728 		sshbuf_reset(auth_debug);
729 	else if ((auth_debug = sshbuf_new()) == NULL)
730 		fatal_f("sshbuf_new failed");
731 }
732 
733 struct passwd *
734 fakepw(void)
735 {
736 	static int done = 0;
737 	static struct passwd fake;
738 	const char hashchars[] = "./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
739 	    "abcdefghijklmnopqrstuvwxyz0123456789"; /* from bcrypt.c */
740 	char *cp;
741 
742 	if (done)
743 		return (&fake);
744 
745 	memset(&fake, 0, sizeof(fake));
746 	fake.pw_name = "NOUSER";
747 	fake.pw_passwd = xstrdup("$2a$10$"
748 	    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
749 	for (cp = fake.pw_passwd + 7; *cp != '\0'; cp++)
750 		*cp = hashchars[arc4random_uniform(sizeof(hashchars) - 1)];
751 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
752 	fake.pw_gecos = "NOUSER";
753 #endif
754 	fake.pw_uid = privsep_pw == NULL ? (uid_t)-1 : privsep_pw->pw_uid;
755 	fake.pw_gid = privsep_pw == NULL ? (gid_t)-1 : privsep_pw->pw_gid;
756 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
757 	fake.pw_class = "";
758 #endif
759 	fake.pw_dir = "/nonexist";
760 	fake.pw_shell = "/nonexist";
761 	done = 1;
762 
763 	return (&fake);
764 }
765 
766 /*
767  * Returns the remote DNS hostname as a string. The returned string must not
768  * be freed. NB. this will usually trigger a DNS query the first time it is
769  * called.
770  * This function does additional checks on the hostname to mitigate some
771  * attacks on based on conflation of hostnames and IP addresses.
772  */
773 
774 static char *
775 remote_hostname(struct ssh *ssh)
776 {
777 	struct sockaddr_storage from;
778 	socklen_t fromlen;
779 	struct addrinfo hints, *ai, *aitop;
780 	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
781 	const char *ntop = ssh_remote_ipaddr(ssh);
782 
783 	/* Get IP address of client. */
784 	fromlen = sizeof(from);
785 	memset(&from, 0, sizeof(from));
786 	if (getpeername(ssh_packet_get_connection_in(ssh),
787 	    (struct sockaddr *)&from, &fromlen) == -1) {
788 		debug("getpeername failed: %.100s", strerror(errno));
789 		return xstrdup(ntop);
790 	}
791 
792 	ipv64_normalise_mapped(&from, &fromlen);
793 	if (from.ss_family == AF_INET6)
794 		fromlen = sizeof(struct sockaddr_in6);
795 
796 	debug3("Trying to reverse map address %.100s.", ntop);
797 	/* Map the IP address to a host name. */
798 	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
799 	    NULL, 0, NI_NAMEREQD) != 0) {
800 		/* Host name not found.  Use ip address. */
801 		return xstrdup(ntop);
802 	}
803 
804 	/*
805 	 * if reverse lookup result looks like a numeric hostname,
806 	 * someone is trying to trick us by PTR record like following:
807 	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
808 	 */
809 	memset(&hints, 0, sizeof(hints));
810 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
811 	hints.ai_flags = AI_NUMERICHOST;
812 	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
813 		logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
814 		    name, ntop);
815 		freeaddrinfo(ai);
816 		return xstrdup(ntop);
817 	}
818 
819 	/* Names are stored in lowercase. */
820 	lowercase(name);
821 
822 	/*
823 	 * Map it back to an IP address and check that the given
824 	 * address actually is an address of this host.  This is
825 	 * necessary because anyone with access to a name server can
826 	 * define arbitrary names for an IP address. Mapping from
827 	 * name to IP address can be trusted better (but can still be
828 	 * fooled if the intruder has access to the name server of
829 	 * the domain).
830 	 */
831 	memset(&hints, 0, sizeof(hints));
832 	hints.ai_family = from.ss_family;
833 	hints.ai_socktype = SOCK_STREAM;
834 	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
835 		logit("reverse mapping checking getaddrinfo for %.700s "
836 		    "[%s] failed.", name, ntop);
837 		return xstrdup(ntop);
838 	}
839 	/* Look for the address from the list of addresses. */
840 	for (ai = aitop; ai; ai = ai->ai_next) {
841 		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
842 		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
843 		    (strcmp(ntop, ntop2) == 0))
844 				break;
845 	}
846 	freeaddrinfo(aitop);
847 	/* If we reached the end of the list, the address was not there. */
848 	if (ai == NULL) {
849 		/* Address not found for the host name. */
850 		logit("Address %.100s maps to %.600s, but this does not "
851 		    "map back to the address.", ntop, name);
852 		return xstrdup(ntop);
853 	}
854 	return xstrdup(name);
855 }
856 
857 /*
858  * Return the canonical name of the host in the other side of the current
859  * connection.  The host name is cached, so it is efficient to call this
860  * several times.
861  */
862 
863 const char *
864 auth_get_canonical_hostname(struct ssh *ssh, int use_dns)
865 {
866 	static char *dnsname;
867 
868 	if (!use_dns)
869 		return ssh_remote_ipaddr(ssh);
870 	else if (dnsname != NULL)
871 		return dnsname;
872 	else {
873 		dnsname = remote_hostname(ssh);
874 		return dnsname;
875 	}
876 }
877 
878 /* These functions link key/cert options to the auth framework */
879 
880 /* Log sshauthopt options locally and (optionally) for remote transmission */
881 void
882 auth_log_authopts(const char *loc, const struct sshauthopt *opts, int do_remote)
883 {
884 	int do_env = options.permit_user_env && opts->nenv > 0;
885 	int do_permitopen = opts->npermitopen > 0 &&
886 	    (options.allow_tcp_forwarding & FORWARD_LOCAL) != 0;
887 	int do_permitlisten = opts->npermitlisten > 0 &&
888 	    (options.allow_tcp_forwarding & FORWARD_REMOTE) != 0;
889 	size_t i;
890 	char msg[1024], buf[64];
891 
892 	snprintf(buf, sizeof(buf), "%d", opts->force_tun_device);
893 	/* Try to keep this alphabetically sorted */
894 	snprintf(msg, sizeof(msg), "key options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
895 	    opts->permit_agent_forwarding_flag ? " agent-forwarding" : "",
896 	    opts->force_command == NULL ? "" : " command",
897 	    do_env ?  " environment" : "",
898 	    opts->valid_before == 0 ? "" : "expires",
899 	    opts->no_require_user_presence ? " no-touch-required" : "",
900 	    do_permitopen ?  " permitopen" : "",
901 	    do_permitlisten ?  " permitlisten" : "",
902 	    opts->permit_port_forwarding_flag ? " port-forwarding" : "",
903 	    opts->cert_principals == NULL ? "" : " principals",
904 	    opts->permit_pty_flag ? " pty" : "",
905 	    opts->require_verify ? " uv" : "",
906 	    opts->force_tun_device == -1 ? "" : " tun=",
907 	    opts->force_tun_device == -1 ? "" : buf,
908 	    opts->permit_user_rc ? " user-rc" : "",
909 	    opts->permit_x11_forwarding_flag ? " x11-forwarding" : "");
910 
911 	debug("%s: %s", loc, msg);
912 	if (do_remote)
913 		auth_debug_add("%s: %s", loc, msg);
914 
915 	if (options.permit_user_env) {
916 		for (i = 0; i < opts->nenv; i++) {
917 			debug("%s: environment: %s", loc, opts->env[i]);
918 			if (do_remote) {
919 				auth_debug_add("%s: environment: %s",
920 				    loc, opts->env[i]);
921 			}
922 		}
923 	}
924 
925 	/* Go into a little more details for the local logs. */
926 	if (opts->valid_before != 0) {
927 		format_absolute_time(opts->valid_before, buf, sizeof(buf));
928 		debug("%s: expires at %s", loc, buf);
929 	}
930 	if (opts->cert_principals != NULL) {
931 		debug("%s: authorized principals: \"%s\"",
932 		    loc, opts->cert_principals);
933 	}
934 	if (opts->force_command != NULL)
935 		debug("%s: forced command: \"%s\"", loc, opts->force_command);
936 	if (do_permitopen) {
937 		for (i = 0; i < opts->npermitopen; i++) {
938 			debug("%s: permitted open: %s",
939 			    loc, opts->permitopen[i]);
940 		}
941 	}
942 	if (do_permitlisten) {
943 		for (i = 0; i < opts->npermitlisten; i++) {
944 			debug("%s: permitted listen: %s",
945 			    loc, opts->permitlisten[i]);
946 		}
947 	}
948 }
949 
950 /* Activate a new set of key/cert options; merging with what is there. */
951 int
952 auth_activate_options(struct ssh *ssh, struct sshauthopt *opts)
953 {
954 	struct sshauthopt *old = auth_opts;
955 	const char *emsg = NULL;
956 
957 	debug_f("setting new authentication options");
958 	if ((auth_opts = sshauthopt_merge(old, opts, &emsg)) == NULL) {
959 		error("Inconsistent authentication options: %s", emsg);
960 		return -1;
961 	}
962 	return 0;
963 }
964 
965 /* Disable forwarding, etc for the session */
966 void
967 auth_restrict_session(struct ssh *ssh)
968 {
969 	struct sshauthopt *restricted;
970 
971 	debug_f("restricting session");
972 
973 	/* A blank sshauthopt defaults to permitting nothing */
974 	restricted = sshauthopt_new();
975 	restricted->permit_pty_flag = 1;
976 	restricted->restricted = 1;
977 
978 	if (auth_activate_options(ssh, restricted) != 0)
979 		fatal_f("failed to restrict session");
980 	sshauthopt_free(restricted);
981 }
982 
983 int
984 auth_authorise_keyopts(struct ssh *ssh, struct passwd *pw,
985     struct sshauthopt *opts, int allow_cert_authority, const char *loc)
986 {
987 	const char *remote_ip = ssh_remote_ipaddr(ssh);
988 	const char *remote_host = auth_get_canonical_hostname(ssh,
989 	    options.use_dns);
990 	time_t now = time(NULL);
991 	char buf[64];
992 
993 	/*
994 	 * Check keys/principals file expiry time.
995 	 * NB. validity interval in certificate is handled elsewhere.
996 	 */
997 	if (opts->valid_before && now > 0 &&
998 	    opts->valid_before < (uint64_t)now) {
999 		format_absolute_time(opts->valid_before, buf, sizeof(buf));
1000 		debug("%s: entry expired at %s", loc, buf);
1001 		auth_debug_add("%s: entry expired at %s", loc, buf);
1002 		return -1;
1003 	}
1004 	/* Consistency checks */
1005 	if (opts->cert_principals != NULL && !opts->cert_authority) {
1006 		debug("%s: principals on non-CA key", loc);
1007 		auth_debug_add("%s: principals on non-CA key", loc);
1008 		/* deny access */
1009 		return -1;
1010 	}
1011 	/* cert-authority flag isn't valid in authorized_principals files */
1012 	if (!allow_cert_authority && opts->cert_authority) {
1013 		debug("%s: cert-authority flag invalid here", loc);
1014 		auth_debug_add("%s: cert-authority flag invalid here", loc);
1015 		/* deny access */
1016 		return -1;
1017 	}
1018 
1019 	/* Perform from= checks */
1020 	if (opts->required_from_host_keys != NULL) {
1021 		switch (match_host_and_ip(remote_host, remote_ip,
1022 		    opts->required_from_host_keys )) {
1023 		case 1:
1024 			/* Host name matches. */
1025 			break;
1026 		case -1:
1027 		default:
1028 			debug("%s: invalid from criteria", loc);
1029 			auth_debug_add("%s: invalid from criteria", loc);
1030 			/* FALLTHROUGH */
1031 		case 0:
1032 			logit("%s: Authentication tried for %.100s with "
1033 			    "correct key but not from a permitted "
1034 			    "host (host=%.200s, ip=%.200s, required=%.200s).",
1035 			    loc, pw->pw_name, remote_host, remote_ip,
1036 			    opts->required_from_host_keys);
1037 			auth_debug_add("%s: Your host '%.200s' is not "
1038 			    "permitted to use this key for login.",
1039 			    loc, remote_host);
1040 			/* deny access */
1041 			return -1;
1042 		}
1043 	}
1044 	/* Check source-address restriction from certificate */
1045 	if (opts->required_from_host_cert != NULL) {
1046 		switch (addr_match_cidr_list(remote_ip,
1047 		    opts->required_from_host_cert)) {
1048 		case 1:
1049 			/* accepted */
1050 			break;
1051 		case -1:
1052 		default:
1053 			/* invalid */
1054 			error("%s: Certificate source-address invalid", loc);
1055 			/* FALLTHROUGH */
1056 		case 0:
1057 			logit("%s: Authentication tried for %.100s with valid "
1058 			    "certificate but not from a permitted source "
1059 			    "address (%.200s).", loc, pw->pw_name, remote_ip);
1060 			auth_debug_add("%s: Your address '%.200s' is not "
1061 			    "permitted to use this certificate for login.",
1062 			    loc, remote_ip);
1063 			return -1;
1064 		}
1065 	}
1066 	/*
1067 	 *
1068 	 * XXX this is spammy. We should report remotely only for keys
1069 	 *     that are successful in actual auth attempts, and not PK_OK
1070 	 *     tests.
1071 	 */
1072 	auth_log_authopts(loc, opts, 1);
1073 
1074 	return 0;
1075 }
1076