xref: /freebsd/crypto/openssh/auth2.c (revision 0fdf8fae8b569bf9fff3b5171e669dcd7cf9c79e)
1*0fdf8faeSEd Maste /* $OpenBSD: auth2.c,v 1.169 2024/05/17 00:30:23 djm Exp $ */
2a04a10f8SKris Kennaway /*
3a04a10f8SKris Kennaway  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4a04a10f8SKris Kennaway  *
5a04a10f8SKris Kennaway  * Redistribution and use in source and binary forms, with or without
6a04a10f8SKris Kennaway  * modification, are permitted provided that the following conditions
7a04a10f8SKris Kennaway  * are met:
8a04a10f8SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
9a04a10f8SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
10a04a10f8SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
11a04a10f8SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
12a04a10f8SKris Kennaway  *    documentation and/or other materials provided with the distribution.
13a04a10f8SKris Kennaway  *
14a04a10f8SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15a04a10f8SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16a04a10f8SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17a04a10f8SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18a04a10f8SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19a04a10f8SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20a04a10f8SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21a04a10f8SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22a04a10f8SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23a04a10f8SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24a04a10f8SKris Kennaway  */
25c2d3a559SKris Kennaway 
26a04a10f8SKris Kennaway #include "includes.h"
27a04a10f8SKris Kennaway 
28333ee039SDag-Erling Smørgrav #include <sys/types.h>
29d4af9e69SDag-Erling Smørgrav #include <sys/stat.h>
30d4af9e69SDag-Erling Smørgrav #include <sys/uio.h>
31333ee039SDag-Erling Smørgrav 
32d4af9e69SDag-Erling Smørgrav #include <fcntl.h>
334f52dfbbSDag-Erling Smørgrav #include <limits.h>
34333ee039SDag-Erling Smørgrav #include <pwd.h>
35333ee039SDag-Erling Smørgrav #include <stdarg.h>
36333ee039SDag-Erling Smørgrav #include <string.h>
37d4af9e69SDag-Erling Smørgrav #include <unistd.h>
3819261079SEd Maste #include <time.h>
39333ee039SDag-Erling Smørgrav 
4019261079SEd Maste #include "stdlib.h"
41d4af9e69SDag-Erling Smørgrav #include "atomicio.h"
427aee6ffeSDag-Erling Smørgrav #include "xmalloc.h"
43333ee039SDag-Erling Smørgrav #include "ssh2.h"
44a04a10f8SKris Kennaway #include "packet.h"
45ca3176e7SBrian Feldman #include "log.h"
46190cef3dSDag-Erling Smørgrav #include "sshbuf.h"
47a0ee8cc6SDag-Erling Smørgrav #include "misc.h"
48a04a10f8SKris Kennaway #include "servconf.h"
49190cef3dSDag-Erling Smørgrav #include "sshkey.h"
50333ee039SDag-Erling Smørgrav #include "hostfile.h"
51a04a10f8SKris Kennaway #include "auth.h"
52a04a10f8SKris Kennaway #include "dispatch.h"
53ca3176e7SBrian Feldman #include "pathnames.h"
54190cef3dSDag-Erling Smørgrav #include "ssherr.h"
55b2af61ecSKurt Lidl #include "blacklist_client.h"
56cf2b5f3bSDag-Erling Smørgrav #ifdef GSSAPI
57cf2b5f3bSDag-Erling Smørgrav #include "ssh-gss.h"
58cf2b5f3bSDag-Erling Smørgrav #endif
59333ee039SDag-Erling Smørgrav #include "monitor_wrap.h"
60190cef3dSDag-Erling Smørgrav #include "digest.h"
61069ac184SEd Maste #include "kex.h"
62cf2b5f3bSDag-Erling Smørgrav 
63a04a10f8SKris Kennaway /* import */
64a04a10f8SKris Kennaway extern ServerOptions options;
65190cef3dSDag-Erling Smørgrav extern struct sshbuf *loginmsg;
66a04a10f8SKris Kennaway 
6780628bacSDag-Erling Smørgrav /* methods */
6880628bacSDag-Erling Smørgrav 
6980628bacSDag-Erling Smørgrav extern Authmethod method_none;
7080628bacSDag-Erling Smørgrav extern Authmethod method_pubkey;
7180628bacSDag-Erling Smørgrav extern Authmethod method_passwd;
7280628bacSDag-Erling Smørgrav extern Authmethod method_kbdint;
7380628bacSDag-Erling Smørgrav extern Authmethod method_hostbased;
74cf2b5f3bSDag-Erling Smørgrav #ifdef GSSAPI
75cf2b5f3bSDag-Erling Smørgrav extern Authmethod method_gssapi;
76cf2b5f3bSDag-Erling Smørgrav #endif
7780628bacSDag-Erling Smørgrav 
7880628bacSDag-Erling Smørgrav Authmethod *authmethods[] = {
7980628bacSDag-Erling Smørgrav 	&method_none,
8080628bacSDag-Erling Smørgrav 	&method_pubkey,
81cf2b5f3bSDag-Erling Smørgrav #ifdef GSSAPI
82cf2b5f3bSDag-Erling Smørgrav 	&method_gssapi,
83cf2b5f3bSDag-Erling Smørgrav #endif
8480628bacSDag-Erling Smørgrav 	&method_passwd,
8580628bacSDag-Erling Smørgrav 	&method_kbdint,
8680628bacSDag-Erling Smørgrav 	&method_hostbased,
8780628bacSDag-Erling Smørgrav 	NULL
8809958426SBrian Feldman };
8909958426SBrian Feldman 
90a04a10f8SKris Kennaway /* protocol */
91a04a10f8SKris Kennaway 
924f52dfbbSDag-Erling Smørgrav static int input_service_request(int, u_int32_t, struct ssh *);
934f52dfbbSDag-Erling Smørgrav static int input_userauth_request(int, u_int32_t, struct ssh *);
94a04a10f8SKris Kennaway 
95a04a10f8SKris Kennaway /* helper */
961323ec57SEd Maste static Authmethod *authmethod_byname(const char *);
976888a9beSDag-Erling Smørgrav static Authmethod *authmethod_lookup(Authctxt *, const char *);
986888a9beSDag-Erling Smørgrav static char *authmethods_get(Authctxt *authctxt);
99e4a9863fSDag-Erling Smørgrav 
100e4a9863fSDag-Erling Smørgrav #define MATCH_NONE	0	/* method or submethod mismatch */
101e4a9863fSDag-Erling Smørgrav #define MATCH_METHOD	1	/* method matches (no submethod specified) */
102e4a9863fSDag-Erling Smørgrav #define MATCH_BOTH	2	/* method and submethod match */
103e4a9863fSDag-Erling Smørgrav #define MATCH_PARTIAL	3	/* method matches, submethod can't be checked */
104e4a9863fSDag-Erling Smørgrav static int list_starts_with(const char *, const char *, const char *);
105d4af9e69SDag-Erling Smørgrav 
106d4af9e69SDag-Erling Smørgrav char *
auth2_read_banner(void)107d4af9e69SDag-Erling Smørgrav auth2_read_banner(void)
108d4af9e69SDag-Erling Smørgrav {
109d4af9e69SDag-Erling Smørgrav 	struct stat st;
110d4af9e69SDag-Erling Smørgrav 	char *banner = NULL;
111d4af9e69SDag-Erling Smørgrav 	size_t len, n;
112d4af9e69SDag-Erling Smørgrav 	int fd;
113d4af9e69SDag-Erling Smørgrav 
114d4af9e69SDag-Erling Smørgrav 	if ((fd = open(options.banner, O_RDONLY)) == -1)
115d4af9e69SDag-Erling Smørgrav 		return (NULL);
116d4af9e69SDag-Erling Smørgrav 	if (fstat(fd, &st) == -1) {
117d4af9e69SDag-Erling Smørgrav 		close(fd);
118d4af9e69SDag-Erling Smørgrav 		return (NULL);
119d4af9e69SDag-Erling Smørgrav 	}
120462c32cbSDag-Erling Smørgrav 	if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
121d4af9e69SDag-Erling Smørgrav 		close(fd);
122d4af9e69SDag-Erling Smørgrav 		return (NULL);
123d4af9e69SDag-Erling Smørgrav 	}
124d4af9e69SDag-Erling Smørgrav 
125d4af9e69SDag-Erling Smørgrav 	len = (size_t)st.st_size;		/* truncate */
126d4af9e69SDag-Erling Smørgrav 	banner = xmalloc(len + 1);
127d4af9e69SDag-Erling Smørgrav 	n = atomicio(read, fd, banner, len);
128d4af9e69SDag-Erling Smørgrav 	close(fd);
129d4af9e69SDag-Erling Smørgrav 
130d4af9e69SDag-Erling Smørgrav 	if (n != len) {
131e4a9863fSDag-Erling Smørgrav 		free(banner);
132d4af9e69SDag-Erling Smørgrav 		return (NULL);
133d4af9e69SDag-Erling Smørgrav 	}
134d4af9e69SDag-Erling Smørgrav 	banner[n] = '\0';
135d4af9e69SDag-Erling Smørgrav 
136d4af9e69SDag-Erling Smørgrav 	return (banner);
137d4af9e69SDag-Erling Smørgrav }
138d4af9e69SDag-Erling Smørgrav 
13919261079SEd Maste static void
userauth_send_banner(struct ssh * ssh,const char * msg)14019261079SEd Maste userauth_send_banner(struct ssh *ssh, const char *msg)
141d4af9e69SDag-Erling Smørgrav {
14219261079SEd Maste 	int r;
14319261079SEd Maste 
14419261079SEd Maste 	if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
14519261079SEd Maste 	    (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
14619261079SEd Maste 	    (r = sshpkt_put_cstring(ssh, "")) != 0 ||	/* language, unused */
14719261079SEd Maste 	    (r = sshpkt_send(ssh)) != 0)
14819261079SEd Maste 		fatal_fr(r, "send packet");
149d4af9e69SDag-Erling Smørgrav 	debug("%s: sent", __func__);
150d4af9e69SDag-Erling Smørgrav }
151d4af9e69SDag-Erling Smørgrav 
152d4af9e69SDag-Erling Smørgrav static void
userauth_banner(struct ssh * ssh)15319261079SEd Maste userauth_banner(struct ssh *ssh)
154d4af9e69SDag-Erling Smørgrav {
155d4af9e69SDag-Erling Smørgrav 	char *banner = NULL;
156d4af9e69SDag-Erling Smørgrav 
15747dd1d1bSDag-Erling Smørgrav 	if (options.banner == NULL)
158d4af9e69SDag-Erling Smørgrav 		return;
159d4af9e69SDag-Erling Smørgrav 
160*0fdf8faeSEd Maste 	if ((banner = mm_auth2_read_banner()) == NULL)
161d4af9e69SDag-Erling Smørgrav 		goto done;
16219261079SEd Maste 	userauth_send_banner(ssh, banner);
163d4af9e69SDag-Erling Smørgrav 
164d4af9e69SDag-Erling Smørgrav done:
165e4a9863fSDag-Erling Smørgrav 	free(banner);
166d4af9e69SDag-Erling Smørgrav }
167a04a10f8SKris Kennaway 
168a04a10f8SKris Kennaway /*
16909958426SBrian Feldman  * loop until authctxt->success == TRUE
170a04a10f8SKris Kennaway  */
1711ec0d754SDag-Erling Smørgrav void
do_authentication2(struct ssh * ssh)17219261079SEd Maste do_authentication2(struct ssh *ssh)
173a04a10f8SKris Kennaway {
17419261079SEd Maste 	Authctxt *authctxt = ssh->authctxt;
17519261079SEd Maste 
1764f52dfbbSDag-Erling Smørgrav 	ssh_dispatch_init(ssh, &dispatch_protocol_error);
177069ac184SEd Maste 	if (ssh->kex->ext_info_c)
178069ac184SEd Maste 		ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_input_ext_info);
1794f52dfbbSDag-Erling Smørgrav 	ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1804f52dfbbSDag-Erling Smørgrav 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
1814f52dfbbSDag-Erling Smørgrav 	ssh->authctxt = NULL;
182a04a10f8SKris Kennaway }
183a04a10f8SKris Kennaway 
184bc5531deSDag-Erling Smørgrav static int
input_service_request(int type,u_int32_t seq,struct ssh * ssh)1854f52dfbbSDag-Erling Smørgrav input_service_request(int type, u_int32_t seq, struct ssh *ssh)
186a04a10f8SKris Kennaway {
1874f52dfbbSDag-Erling Smørgrav 	Authctxt *authctxt = ssh->authctxt;
18819261079SEd Maste 	char *service = NULL;
18919261079SEd Maste 	int r, acceptit = 0;
19019261079SEd Maste 
19119261079SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
19219261079SEd Maste 	    (r = sshpkt_get_end(ssh)) != 0)
19319261079SEd Maste 		goto out;
194a04a10f8SKris Kennaway 
19509958426SBrian Feldman 	if (authctxt == NULL)
19609958426SBrian Feldman 		fatal("input_service_request: no authctxt");
19709958426SBrian Feldman 
198a04a10f8SKris Kennaway 	if (strcmp(service, "ssh-userauth") == 0) {
19909958426SBrian Feldman 		if (!authctxt->success) {
200f388f5efSDag-Erling Smørgrav 			acceptit = 1;
201a04a10f8SKris Kennaway 			/* now we can handle user-auth requests */
20219261079SEd Maste 			ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
20319261079SEd Maste 			    &input_userauth_request);
204a04a10f8SKris Kennaway 		}
205a04a10f8SKris Kennaway 	}
206a04a10f8SKris Kennaway 	/* XXX all other service requests are denied */
207a04a10f8SKris Kennaway 
208f388f5efSDag-Erling Smørgrav 	if (acceptit) {
20919261079SEd Maste 		if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
21019261079SEd Maste 		    (r = sshpkt_put_cstring(ssh, service)) != 0 ||
21119261079SEd Maste 		    (r = sshpkt_send(ssh)) != 0 ||
21219261079SEd Maste 		    (r = ssh_packet_write_wait(ssh)) != 0)
21319261079SEd Maste 			goto out;
214a04a10f8SKris Kennaway 	} else {
215a04a10f8SKris Kennaway 		debug("bad service request %s", service);
21619261079SEd Maste 		ssh_packet_disconnect(ssh, "bad service request %s", service);
217a04a10f8SKris Kennaway 	}
218069ac184SEd Maste 	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &dispatch_protocol_error);
21919261079SEd Maste 	r = 0;
22019261079SEd Maste  out:
221e4a9863fSDag-Erling Smørgrav 	free(service);
22219261079SEd Maste 	return r;
223a04a10f8SKris Kennaway }
224a04a10f8SKris Kennaway 
225190cef3dSDag-Erling Smørgrav #define MIN_FAIL_DELAY_SECONDS 0.005
226edf85781SEd Maste #define MAX_FAIL_DELAY_SECONDS 5.0
227190cef3dSDag-Erling Smørgrav static double
user_specific_delay(const char * user)228190cef3dSDag-Erling Smørgrav user_specific_delay(const char *user)
229190cef3dSDag-Erling Smørgrav {
230190cef3dSDag-Erling Smørgrav 	char b[512];
231190cef3dSDag-Erling Smørgrav 	size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
232190cef3dSDag-Erling Smørgrav 	u_char *hash = xmalloc(len);
233190cef3dSDag-Erling Smørgrav 	double delay;
234190cef3dSDag-Erling Smørgrav 
235190cef3dSDag-Erling Smørgrav 	(void)snprintf(b, sizeof b, "%llu%s",
236190cef3dSDag-Erling Smørgrav 	    (unsigned long long)options.timing_secret, user);
237190cef3dSDag-Erling Smørgrav 	if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
23819261079SEd Maste 		fatal_f("ssh_digest_memory");
239190cef3dSDag-Erling Smørgrav 	/* 0-4.2 ms of delay */
240190cef3dSDag-Erling Smørgrav 	delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
241190cef3dSDag-Erling Smørgrav 	freezero(hash, len);
24219261079SEd Maste 	debug3_f("user specific delay %0.3lfms", delay/1000);
243190cef3dSDag-Erling Smørgrav 	return MIN_FAIL_DELAY_SECONDS + delay;
244190cef3dSDag-Erling Smørgrav }
245190cef3dSDag-Erling Smørgrav 
246190cef3dSDag-Erling Smørgrav static void
ensure_minimum_time_since(double start,double seconds)247190cef3dSDag-Erling Smørgrav ensure_minimum_time_since(double start, double seconds)
248190cef3dSDag-Erling Smørgrav {
249190cef3dSDag-Erling Smørgrav 	struct timespec ts;
250190cef3dSDag-Erling Smørgrav 	double elapsed = monotime_double() - start, req = seconds, remain;
251190cef3dSDag-Erling Smørgrav 
252edf85781SEd Maste 	if (elapsed > MAX_FAIL_DELAY_SECONDS) {
253edf85781SEd Maste 		debug3_f("elapsed %0.3lfms exceeded the max delay "
254edf85781SEd Maste 		    "requested %0.3lfms)", elapsed*1000, req*1000);
255edf85781SEd Maste 		return;
256edf85781SEd Maste 	}
257edf85781SEd Maste 
258190cef3dSDag-Erling Smørgrav 	/* if we've already passed the requested time, scale up */
259190cef3dSDag-Erling Smørgrav 	while ((remain = seconds - elapsed) < 0.0)
260190cef3dSDag-Erling Smørgrav 		seconds *= 2;
261190cef3dSDag-Erling Smørgrav 
262190cef3dSDag-Erling Smørgrav 	ts.tv_sec = remain;
263190cef3dSDag-Erling Smørgrav 	ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
26419261079SEd Maste 	debug3_f("elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
26519261079SEd Maste 	    elapsed*1000, remain*1000, req*1000);
266190cef3dSDag-Erling Smørgrav 	nanosleep(&ts, NULL);
267190cef3dSDag-Erling Smørgrav }
268190cef3dSDag-Erling Smørgrav 
269bc5531deSDag-Erling Smørgrav static int
input_userauth_request(int type,u_int32_t seq,struct ssh * ssh)2704f52dfbbSDag-Erling Smørgrav input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
271a04a10f8SKris Kennaway {
2724f52dfbbSDag-Erling Smørgrav 	Authctxt *authctxt = ssh->authctxt;
27309958426SBrian Feldman 	Authmethod *m = NULL;
27419261079SEd Maste 	char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
27519261079SEd Maste 	int r, authenticated = 0;
276190cef3dSDag-Erling Smørgrav 	double tstart = monotime_double();
277d93a896eSDag-Erling Smørgrav 
27809958426SBrian Feldman 	if (authctxt == NULL)
27909958426SBrian Feldman 		fatal("input_userauth_request: no authctxt");
280a04a10f8SKris Kennaway 
28119261079SEd Maste 	if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
28219261079SEd Maste 	    (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
28319261079SEd Maste 	    (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
28419261079SEd Maste 		goto out;
285a04a10f8SKris Kennaway 	debug("userauth-request for user %s service %s method %s", user, service, method);
286ca3176e7SBrian Feldman 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
287a04a10f8SKris Kennaway 
288ca3176e7SBrian Feldman 	if ((style = strchr(user, ':')) != NULL)
289ca3176e7SBrian Feldman 		*style++ = 0;
290ca3176e7SBrian Feldman 
2911323ec57SEd Maste 	if (authctxt->attempt >= 1024)
2921323ec57SEd Maste 		auth_maxtries_exceeded(ssh);
293ca3176e7SBrian Feldman 	if (authctxt->attempt++ == 0) {
29409958426SBrian Feldman 		/* setup auth context */
295*0fdf8faeSEd Maste 		authctxt->pw = mm_getpwnamallow(ssh, user);
2965962c0e9SDag-Erling Smørgrav 		authctxt->user = xstrdup(user);
29780628bacSDag-Erling Smørgrav 		if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
29809958426SBrian Feldman 			authctxt->valid = 1;
29919261079SEd Maste 			debug2_f("setting up authctxt for %s", user);
30009958426SBrian Feldman 		} else {
3011323ec57SEd Maste 			authctxt->valid = 0;
302d93a896eSDag-Erling Smørgrav 			/* Invalid user, fake password information */
303cf2b5f3bSDag-Erling Smørgrav 			authctxt->pw = fakepw();
304aa49c926SDag-Erling Smørgrav #ifdef SSH_AUDIT_EVENTS
305*0fdf8faeSEd Maste 			mm_audit_event(ssh, SSH_INVALID_USER);
306aa49c926SDag-Erling Smørgrav #endif
307a04a10f8SKris Kennaway 		}
308b74df5b2SDag-Erling Smørgrav #ifdef USE_PAM
309b74df5b2SDag-Erling Smørgrav 		if (options.use_pam)
310*0fdf8faeSEd Maste 			mm_start_pam(ssh);
311b74df5b2SDag-Erling Smørgrav #endif
312d93a896eSDag-Erling Smørgrav 		ssh_packet_set_log_preamble(ssh, "%suser %s",
313d93a896eSDag-Erling Smørgrav 		    authctxt->valid ? "authenticating " : "invalid ", user);
314*0fdf8faeSEd Maste 		setproctitle("%s [net]", authctxt->valid ? user : "unknown");
31509958426SBrian Feldman 		authctxt->service = xstrdup(service);
316af12a3e7SDag-Erling Smørgrav 		authctxt->style = style ? xstrdup(style) : NULL;
31780628bacSDag-Erling Smørgrav 		mm_inform_authserv(service, style);
31819261079SEd Maste 		userauth_banner(ssh);
319069ac184SEd Maste 		if ((r = kex_server_update_ext_info(ssh)) != 0)
320069ac184SEd Maste 			fatal_fr(r, "kex_server_update_ext_info failed");
3216888a9beSDag-Erling Smørgrav 		if (auth2_setup_methods_lists(authctxt) != 0)
32219261079SEd Maste 			ssh_packet_disconnect(ssh,
32319261079SEd Maste 			    "no authentication methods enabled");
324af12a3e7SDag-Erling Smørgrav 	} else if (strcmp(user, authctxt->user) != 0 ||
32509958426SBrian Feldman 	    strcmp(service, authctxt->service) != 0) {
32619261079SEd Maste 		ssh_packet_disconnect(ssh, "Change of username or service "
32719261079SEd Maste 		    "not allowed: (%s,%s) -> (%s,%s)",
328af12a3e7SDag-Erling Smørgrav 		    authctxt->user, authctxt->service, user, service);
329a04a10f8SKris Kennaway 	}
330ca3176e7SBrian Feldman 	/* reset state */
3314f52dfbbSDag-Erling Smørgrav 	auth2_challenge_stop(ssh);
332cf2b5f3bSDag-Erling Smørgrav 
333cf2b5f3bSDag-Erling Smørgrav #ifdef GSSAPI
334cce7d346SDag-Erling Smørgrav 	/* XXX move to auth2_gssapi_stop() */
3354f52dfbbSDag-Erling Smørgrav 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
3364f52dfbbSDag-Erling Smørgrav 	ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
337cf2b5f3bSDag-Erling Smørgrav #endif
338cf2b5f3bSDag-Erling Smørgrav 
3394f52dfbbSDag-Erling Smørgrav 	auth2_authctxt_reset_info(authctxt);
340ca3176e7SBrian Feldman 	authctxt->postponed = 0;
341e146993eSDag-Erling Smørgrav 	authctxt->server_caused_failure = 0;
34203e72be8SBrian Feldman 
343ca3176e7SBrian Feldman 	/* try to authenticate user */
3446888a9beSDag-Erling Smørgrav 	m = authmethod_lookup(authctxt, method);
345d4af9e69SDag-Erling Smørgrav 	if (m != NULL && authctxt->failures < options.max_authtries) {
34609958426SBrian Feldman 		debug2("input_userauth_request: try method %s", method);
3471323ec57SEd Maste 		authenticated =	m->userauth(ssh, method);
34809958426SBrian Feldman 	}
349edf85781SEd Maste 	if (!authctxt->authenticated && strcmp(method, "none") != 0)
350190cef3dSDag-Erling Smørgrav 		ensure_minimum_time_since(tstart,
351190cef3dSDag-Erling Smørgrav 		    user_specific_delay(authctxt->user));
3524f52dfbbSDag-Erling Smørgrav 	userauth_finish(ssh, authenticated, method, NULL);
35319261079SEd Maste 	r = 0;
35419261079SEd Maste  out:
355e4a9863fSDag-Erling Smørgrav 	free(service);
356e4a9863fSDag-Erling Smørgrav 	free(user);
357e4a9863fSDag-Erling Smørgrav 	free(method);
35819261079SEd Maste 	return r;
35909958426SBrian Feldman }
36009958426SBrian Feldman 
361ca3176e7SBrian Feldman void
userauth_finish(struct ssh * ssh,int authenticated,const char * packet_method,const char * submethod)3621323ec57SEd Maste userauth_finish(struct ssh *ssh, int authenticated, const char *packet_method,
3636888a9beSDag-Erling Smørgrav     const char *submethod)
364ca3176e7SBrian Feldman {
3654f52dfbbSDag-Erling Smørgrav 	Authctxt *authctxt = ssh->authctxt;
3661323ec57SEd Maste 	Authmethod *m = NULL;
3671323ec57SEd Maste 	const char *method = packet_method;
368af12a3e7SDag-Erling Smørgrav 	char *methods;
36919261079SEd Maste 	int r, partial = 0;
370af12a3e7SDag-Erling Smørgrav 
3711323ec57SEd Maste 	if (authenticated) {
3721323ec57SEd Maste 		if (!authctxt->valid) {
373ca3176e7SBrian Feldman 			fatal("INTERNAL ERROR: authenticated invalid user %s",
374ca3176e7SBrian Feldman 			    authctxt->user);
3751323ec57SEd Maste 		}
3761323ec57SEd Maste 		if (authctxt->postponed)
3776888a9beSDag-Erling Smørgrav 			fatal("INTERNAL ERROR: authenticated and postponed");
3781323ec57SEd Maste 		/* prefer primary authmethod name to possible synonym */
3791323ec57SEd Maste 		if ((m = authmethod_byname(method)) == NULL)
3801323ec57SEd Maste 			fatal("INTERNAL ERROR: bad method %s", method);
381*0fdf8faeSEd Maste 		method = m->cfg->name;
3821323ec57SEd Maste 	}
383ca3176e7SBrian Feldman 
384ca3176e7SBrian Feldman 	/* Special handling for root */
385e73e9afaSDag-Erling Smørgrav 	if (authenticated && authctxt->pw->pw_uid == 0 &&
38647dd1d1bSDag-Erling Smørgrav 	    !auth_root_allowed(ssh, method)) {
387ca3176e7SBrian Feldman 		authenticated = 0;
388aa49c926SDag-Erling Smørgrav #ifdef SSH_AUDIT_EVENTS
389*0fdf8faeSEd Maste 		mm_audit_event(ssh, SSH_LOGIN_ROOT_DENIED);
390aa49c926SDag-Erling Smørgrav #endif
391aa49c926SDag-Erling Smørgrav 	}
392ca3176e7SBrian Feldman 
3936888a9beSDag-Erling Smørgrav 	if (authenticated && options.num_auth_methods != 0) {
394e4a9863fSDag-Erling Smørgrav 		if (!auth2_update_methods_lists(authctxt, method, submethod)) {
3956888a9beSDag-Erling Smørgrav 			authenticated = 0;
3966888a9beSDag-Erling Smørgrav 			partial = 1;
3976888a9beSDag-Erling Smørgrav 		}
3986888a9beSDag-Erling Smørgrav 	}
3996888a9beSDag-Erling Smørgrav 
4006888a9beSDag-Erling Smørgrav 	/* Log before sending the reply */
40119261079SEd Maste 	auth_log(ssh, authenticated, partial, method, submethod);
4026888a9beSDag-Erling Smørgrav 
4034f52dfbbSDag-Erling Smørgrav 	/* Update information exposed to session */
4044f52dfbbSDag-Erling Smørgrav 	if (authenticated || partial)
4054f52dfbbSDag-Erling Smørgrav 		auth2_update_session_info(authctxt, method, submethod);
4064f52dfbbSDag-Erling Smørgrav 
4076888a9beSDag-Erling Smørgrav 	if (authctxt->postponed)
4086888a9beSDag-Erling Smørgrav 		return;
4096888a9beSDag-Erling Smørgrav 
410989dd127SDag-Erling Smørgrav #ifdef USE_PAM
411aa49c926SDag-Erling Smørgrav 	if (options.use_pam && authenticated) {
412*0fdf8faeSEd Maste 		int r, success = mm_do_pam_account();
413190cef3dSDag-Erling Smørgrav 
41419261079SEd Maste 		/* If PAM returned a message, send it to the user. */
415190cef3dSDag-Erling Smørgrav 		if (sshbuf_len(loginmsg) > 0) {
416190cef3dSDag-Erling Smørgrav 			if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
417190cef3dSDag-Erling Smørgrav 				fatal("%s: buffer error: %s",
418190cef3dSDag-Erling Smørgrav 				    __func__, ssh_err(r));
41919261079SEd Maste 			userauth_send_banner(ssh, sshbuf_ptr(loginmsg));
42019261079SEd Maste 			if ((r = ssh_packet_write_wait(ssh)) != 0) {
42119261079SEd Maste 				sshpkt_fatal(ssh, r,
42219261079SEd Maste 				    "%s: send PAM banner", __func__);
423aa49c926SDag-Erling Smørgrav 			}
42419261079SEd Maste 		}
42519261079SEd Maste 		if (!success) {
426aa49c926SDag-Erling Smørgrav 			fatal("Access denied for user %s by PAM account "
427aa49c926SDag-Erling Smørgrav 			    "configuration", authctxt->user);
428aa49c926SDag-Erling Smørgrav 		}
429aa49c926SDag-Erling Smørgrav 	}
430cf2b5f3bSDag-Erling Smørgrav #endif
431989dd127SDag-Erling Smørgrav 
432af12a3e7SDag-Erling Smørgrav 	if (authenticated == 1) {
433af12a3e7SDag-Erling Smørgrav 		/* turn off userauth */
43419261079SEd Maste 		ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
43519261079SEd Maste 		    &dispatch_protocol_ignore);
43619261079SEd Maste 		if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
43719261079SEd Maste 		    (r = sshpkt_send(ssh)) != 0 ||
43819261079SEd Maste 		    (r = ssh_packet_write_wait(ssh)) != 0)
43919261079SEd Maste 			fatal_fr(r, "send success packet");
440af12a3e7SDag-Erling Smørgrav 		/* now we can break out */
441af12a3e7SDag-Erling Smørgrav 		authctxt->success = 1;
442d93a896eSDag-Erling Smørgrav 		ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
443af12a3e7SDag-Erling Smørgrav 	} else {
444d4af9e69SDag-Erling Smørgrav 		/* Allow initial try of "none" auth without failure penalty */
445bc5531deSDag-Erling Smørgrav 		if (!partial && !authctxt->server_caused_failure &&
446342b8b88SKurt Lidl 		    (authctxt->attempt > 1 || strcmp(method, "none") != 0)) {
447d4af9e69SDag-Erling Smørgrav 			authctxt->failures++;
4480f9bafdfSEd Maste 			BLACKLIST_NOTIFY(ssh, BLACKLIST_AUTH_FAIL, "ssh");
449342b8b88SKurt Lidl 		}
450d4af9e69SDag-Erling Smørgrav 		if (authctxt->failures >= options.max_authtries) {
451aa49c926SDag-Erling Smørgrav #ifdef SSH_AUDIT_EVENTS
452*0fdf8faeSEd Maste 			mm_audit_event(ssh, SSH_LOGIN_EXCEED_MAXTRIES);
453aa49c926SDag-Erling Smørgrav #endif
45419261079SEd Maste 			auth_maxtries_exceeded(ssh);
455aa49c926SDag-Erling Smørgrav 		}
4566888a9beSDag-Erling Smørgrav 		methods = authmethods_get(authctxt);
45719261079SEd Maste 		debug3_f("failure partial=%d next methods=\"%s\"",
4586888a9beSDag-Erling Smørgrav 		    partial, methods);
45919261079SEd Maste 		if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
46019261079SEd Maste 		    (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
46119261079SEd Maste 		    (r = sshpkt_put_u8(ssh, partial)) != 0 ||
46219261079SEd Maste 		    (r = sshpkt_send(ssh)) != 0 ||
46319261079SEd Maste 		    (r = ssh_packet_write_wait(ssh)) != 0)
46419261079SEd Maste 			fatal_fr(r, "send failure packet");
465e4a9863fSDag-Erling Smørgrav 		free(methods);
466af12a3e7SDag-Erling Smørgrav 	}
467ca3176e7SBrian Feldman }
46809958426SBrian Feldman 
4696888a9beSDag-Erling Smørgrav /*
4706888a9beSDag-Erling Smørgrav  * Checks whether method is allowed by at least one AuthenticationMethods
4716888a9beSDag-Erling Smørgrav  * methods list. Returns 1 if allowed, or no methods lists configured.
4726888a9beSDag-Erling Smørgrav  * 0 otherwise.
4736888a9beSDag-Erling Smørgrav  */
474e4a9863fSDag-Erling Smørgrav int
auth2_method_allowed(Authctxt * authctxt,const char * method,const char * submethod)475e4a9863fSDag-Erling Smørgrav auth2_method_allowed(Authctxt *authctxt, const char *method,
476e4a9863fSDag-Erling Smørgrav     const char *submethod)
4776888a9beSDag-Erling Smørgrav {
4786888a9beSDag-Erling Smørgrav 	u_int i;
4796888a9beSDag-Erling Smørgrav 
4806888a9beSDag-Erling Smørgrav 	/*
4816888a9beSDag-Erling Smørgrav 	 * NB. authctxt->num_auth_methods might be zero as a result of
4826888a9beSDag-Erling Smørgrav 	 * auth2_setup_methods_lists(), so check the configuration.
4836888a9beSDag-Erling Smørgrav 	 */
4846888a9beSDag-Erling Smørgrav 	if (options.num_auth_methods == 0)
4856888a9beSDag-Erling Smørgrav 		return 1;
4866888a9beSDag-Erling Smørgrav 	for (i = 0; i < authctxt->num_auth_methods; i++) {
487e4a9863fSDag-Erling Smørgrav 		if (list_starts_with(authctxt->auth_methods[i], method,
488e4a9863fSDag-Erling Smørgrav 		    submethod) != MATCH_NONE)
4896888a9beSDag-Erling Smørgrav 			return 1;
4906888a9beSDag-Erling Smørgrav 	}
4916888a9beSDag-Erling Smørgrav 	return 0;
4926888a9beSDag-Erling Smørgrav }
4936888a9beSDag-Erling Smørgrav 
494af12a3e7SDag-Erling Smørgrav static char *
authmethods_get(Authctxt * authctxt)4956888a9beSDag-Erling Smørgrav authmethods_get(Authctxt *authctxt)
496a04a10f8SKris Kennaway {
497190cef3dSDag-Erling Smørgrav 	struct sshbuf *b;
49809958426SBrian Feldman 	char *list;
499190cef3dSDag-Erling Smørgrav 	int i, r;
500a04a10f8SKris Kennaway 
501190cef3dSDag-Erling Smørgrav 	if ((b = sshbuf_new()) == NULL)
50219261079SEd Maste 		fatal_f("sshbuf_new failed");
50380628bacSDag-Erling Smørgrav 	for (i = 0; authmethods[i] != NULL; i++) {
504*0fdf8faeSEd Maste 		if (strcmp(authmethods[i]->cfg->name, "none") == 0)
50509958426SBrian Feldman 			continue;
506*0fdf8faeSEd Maste 		if (authmethods[i]->cfg->enabled == NULL ||
507*0fdf8faeSEd Maste 		    *(authmethods[i]->cfg->enabled) == 0)
5086888a9beSDag-Erling Smørgrav 			continue;
509*0fdf8faeSEd Maste 		if (!auth2_method_allowed(authctxt, authmethods[i]->cfg->name,
510e4a9863fSDag-Erling Smørgrav 		    NULL))
5116888a9beSDag-Erling Smørgrav 			continue;
512190cef3dSDag-Erling Smørgrav 		if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
513*0fdf8faeSEd Maste 		    authmethods[i]->cfg->name)) != 0)
51419261079SEd Maste 			fatal_fr(r, "buffer error");
51509958426SBrian Feldman 	}
516190cef3dSDag-Erling Smørgrav 	if ((list = sshbuf_dup_string(b)) == NULL)
51719261079SEd Maste 		fatal_f("sshbuf_dup_string failed");
518190cef3dSDag-Erling Smørgrav 	sshbuf_free(b);
51909958426SBrian Feldman 	return list;
52009958426SBrian Feldman }
52109958426SBrian Feldman 
522af12a3e7SDag-Erling Smørgrav static Authmethod *
authmethod_byname(const char * name)5231323ec57SEd Maste authmethod_byname(const char *name)
52409958426SBrian Feldman {
52580628bacSDag-Erling Smørgrav 	int i;
52680628bacSDag-Erling Smørgrav 
5271323ec57SEd Maste 	if (name == NULL)
5281323ec57SEd Maste 		fatal_f("NULL authentication method name");
5291323ec57SEd Maste 	for (i = 0; authmethods[i] != NULL; i++) {
530*0fdf8faeSEd Maste 		if (strcmp(name, authmethods[i]->cfg->name) == 0 ||
531*0fdf8faeSEd Maste 		    (authmethods[i]->cfg->synonym != NULL &&
532*0fdf8faeSEd Maste 		    strcmp(name, authmethods[i]->cfg->synonym) == 0))
53380628bacSDag-Erling Smørgrav 			return authmethods[i];
5341323ec57SEd Maste 	}
5351323ec57SEd Maste 	debug_f("unrecognized authentication method name: %s", name);
536a04a10f8SKris Kennaway 	return NULL;
537a04a10f8SKris Kennaway }
538d4af9e69SDag-Erling Smørgrav 
5391323ec57SEd Maste static Authmethod *
authmethod_lookup(Authctxt * authctxt,const char * name)5401323ec57SEd Maste authmethod_lookup(Authctxt *authctxt, const char *name)
5411323ec57SEd Maste {
5421323ec57SEd Maste 	Authmethod *method;
5431323ec57SEd Maste 
5441323ec57SEd Maste 	if ((method = authmethod_byname(name)) == NULL)
5451323ec57SEd Maste 		return NULL;
5461323ec57SEd Maste 
547*0fdf8faeSEd Maste 	if (method->cfg->enabled == NULL || *(method->cfg->enabled) == 0) {
5481323ec57SEd Maste 		debug3_f("method %s not enabled", name);
5491323ec57SEd Maste 		return NULL;
5501323ec57SEd Maste 	}
551*0fdf8faeSEd Maste 	if (!auth2_method_allowed(authctxt, method->cfg->name, NULL)) {
5521323ec57SEd Maste 		debug3_f("method %s not allowed "
5531323ec57SEd Maste 		    "by AuthenticationMethods", name);
5541323ec57SEd Maste 		return NULL;
5551323ec57SEd Maste 	}
5561323ec57SEd Maste 	return method;
5571323ec57SEd Maste }
5581323ec57SEd Maste 
5596888a9beSDag-Erling Smørgrav /*
5606888a9beSDag-Erling Smørgrav  * Prune the AuthenticationMethods supplied in the configuration, removing
5616888a9beSDag-Erling Smørgrav  * any methods lists that include disabled methods. Note that this might
5626888a9beSDag-Erling Smørgrav  * leave authctxt->num_auth_methods == 0, even when multiple required auth
5636888a9beSDag-Erling Smørgrav  * has been requested. For this reason, all tests for whether multiple is
5646888a9beSDag-Erling Smørgrav  * enabled should consult options.num_auth_methods directly.
5656888a9beSDag-Erling Smørgrav  */
5666888a9beSDag-Erling Smørgrav int
auth2_setup_methods_lists(Authctxt * authctxt)5676888a9beSDag-Erling Smørgrav auth2_setup_methods_lists(Authctxt *authctxt)
5686888a9beSDag-Erling Smørgrav {
5696888a9beSDag-Erling Smørgrav 	u_int i;
5706888a9beSDag-Erling Smørgrav 
57119261079SEd Maste 	/* First, normalise away the "any" pseudo-method */
57219261079SEd Maste 	if (options.num_auth_methods == 1 &&
57319261079SEd Maste 	    strcmp(options.auth_methods[0], "any") == 0) {
57419261079SEd Maste 		free(options.auth_methods[0]);
57519261079SEd Maste 		options.auth_methods[0] = NULL;
57619261079SEd Maste 		options.num_auth_methods = 0;
57719261079SEd Maste 	}
57819261079SEd Maste 
5796888a9beSDag-Erling Smørgrav 	if (options.num_auth_methods == 0)
5806888a9beSDag-Erling Smørgrav 		return 0;
58119261079SEd Maste 	debug3_f("checking methods");
5826888a9beSDag-Erling Smørgrav 	authctxt->auth_methods = xcalloc(options.num_auth_methods,
5836888a9beSDag-Erling Smørgrav 	    sizeof(*authctxt->auth_methods));
5846888a9beSDag-Erling Smørgrav 	authctxt->num_auth_methods = 0;
5856888a9beSDag-Erling Smørgrav 	for (i = 0; i < options.num_auth_methods; i++) {
5866888a9beSDag-Erling Smørgrav 		if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
5876888a9beSDag-Erling Smørgrav 			logit("Authentication methods list \"%s\" contains "
5886888a9beSDag-Erling Smørgrav 			    "disabled method, skipping",
5896888a9beSDag-Erling Smørgrav 			    options.auth_methods[i]);
5906888a9beSDag-Erling Smørgrav 			continue;
5916888a9beSDag-Erling Smørgrav 		}
5926888a9beSDag-Erling Smørgrav 		debug("authentication methods list %d: %s",
5936888a9beSDag-Erling Smørgrav 		    authctxt->num_auth_methods, options.auth_methods[i]);
5946888a9beSDag-Erling Smørgrav 		authctxt->auth_methods[authctxt->num_auth_methods++] =
5956888a9beSDag-Erling Smørgrav 		    xstrdup(options.auth_methods[i]);
5966888a9beSDag-Erling Smørgrav 	}
5976888a9beSDag-Erling Smørgrav 	if (authctxt->num_auth_methods == 0) {
5986888a9beSDag-Erling Smørgrav 		error("No AuthenticationMethods left after eliminating "
5996888a9beSDag-Erling Smørgrav 		    "disabled methods");
6006888a9beSDag-Erling Smørgrav 		return -1;
6016888a9beSDag-Erling Smørgrav 	}
6026888a9beSDag-Erling Smørgrav 	return 0;
6036888a9beSDag-Erling Smørgrav }
6046888a9beSDag-Erling Smørgrav 
6056888a9beSDag-Erling Smørgrav static int
list_starts_with(const char * methods,const char * method,const char * submethod)606e4a9863fSDag-Erling Smørgrav list_starts_with(const char *methods, const char *method,
607e4a9863fSDag-Erling Smørgrav     const char *submethod)
6086888a9beSDag-Erling Smørgrav {
6096888a9beSDag-Erling Smørgrav 	size_t l = strlen(method);
610e4a9863fSDag-Erling Smørgrav 	int match;
611e4a9863fSDag-Erling Smørgrav 	const char *p;
6126888a9beSDag-Erling Smørgrav 
6136888a9beSDag-Erling Smørgrav 	if (strncmp(methods, method, l) != 0)
614e4a9863fSDag-Erling Smørgrav 		return MATCH_NONE;
615e4a9863fSDag-Erling Smørgrav 	p = methods + l;
616e4a9863fSDag-Erling Smørgrav 	match = MATCH_METHOD;
617e4a9863fSDag-Erling Smørgrav 	if (*p == ':') {
618e4a9863fSDag-Erling Smørgrav 		if (!submethod)
619e4a9863fSDag-Erling Smørgrav 			return MATCH_PARTIAL;
620e4a9863fSDag-Erling Smørgrav 		l = strlen(submethod);
621e4a9863fSDag-Erling Smørgrav 		p += 1;
622e4a9863fSDag-Erling Smørgrav 		if (strncmp(submethod, p, l))
623e4a9863fSDag-Erling Smørgrav 			return MATCH_NONE;
624e4a9863fSDag-Erling Smørgrav 		p += l;
625e4a9863fSDag-Erling Smørgrav 		match = MATCH_BOTH;
626e4a9863fSDag-Erling Smørgrav 	}
627e4a9863fSDag-Erling Smørgrav 	if (*p != ',' && *p != '\0')
628e4a9863fSDag-Erling Smørgrav 		return MATCH_NONE;
629e4a9863fSDag-Erling Smørgrav 	return match;
6306888a9beSDag-Erling Smørgrav }
6316888a9beSDag-Erling Smørgrav 
6326888a9beSDag-Erling Smørgrav /*
6336888a9beSDag-Erling Smørgrav  * Remove method from the start of a comma-separated list of methods.
6346888a9beSDag-Erling Smørgrav  * Returns 0 if the list of methods did not start with that method or 1
6356888a9beSDag-Erling Smørgrav  * if it did.
6366888a9beSDag-Erling Smørgrav  */
6376888a9beSDag-Erling Smørgrav static int
remove_method(char ** methods,const char * method,const char * submethod)638e4a9863fSDag-Erling Smørgrav remove_method(char **methods, const char *method, const char *submethod)
6396888a9beSDag-Erling Smørgrav {
640e4a9863fSDag-Erling Smørgrav 	char *omethods = *methods, *p;
6416888a9beSDag-Erling Smørgrav 	size_t l = strlen(method);
642e4a9863fSDag-Erling Smørgrav 	int match;
6436888a9beSDag-Erling Smørgrav 
644e4a9863fSDag-Erling Smørgrav 	match = list_starts_with(omethods, method, submethod);
645e4a9863fSDag-Erling Smørgrav 	if (match != MATCH_METHOD && match != MATCH_BOTH)
6466888a9beSDag-Erling Smørgrav 		return 0;
647e4a9863fSDag-Erling Smørgrav 	p = omethods + l;
648e4a9863fSDag-Erling Smørgrav 	if (submethod && match == MATCH_BOTH)
649e4a9863fSDag-Erling Smørgrav 		p += 1 + strlen(submethod); /* include colon */
650e4a9863fSDag-Erling Smørgrav 	if (*p == ',')
651e4a9863fSDag-Erling Smørgrav 		p++;
652e4a9863fSDag-Erling Smørgrav 	*methods = xstrdup(p);
6536888a9beSDag-Erling Smørgrav 	free(omethods);
6546888a9beSDag-Erling Smørgrav 	return 1;
6556888a9beSDag-Erling Smørgrav }
6566888a9beSDag-Erling Smørgrav 
6576888a9beSDag-Erling Smørgrav /*
6586888a9beSDag-Erling Smørgrav  * Called after successful authentication. Will remove the successful method
6596888a9beSDag-Erling Smørgrav  * from the start of each list in which it occurs. If it was the last method
6606888a9beSDag-Erling Smørgrav  * in any list, then authentication is deemed successful.
6616888a9beSDag-Erling Smørgrav  * Returns 1 if the method completed any authentication list or 0 otherwise.
6626888a9beSDag-Erling Smørgrav  */
6636888a9beSDag-Erling Smørgrav int
auth2_update_methods_lists(Authctxt * authctxt,const char * method,const char * submethod)664e4a9863fSDag-Erling Smørgrav auth2_update_methods_lists(Authctxt *authctxt, const char *method,
665e4a9863fSDag-Erling Smørgrav     const char *submethod)
6666888a9beSDag-Erling Smørgrav {
6676888a9beSDag-Erling Smørgrav 	u_int i, found = 0;
6686888a9beSDag-Erling Smørgrav 
66919261079SEd Maste 	debug3_f("updating methods list after \"%s\"", method);
6706888a9beSDag-Erling Smørgrav 	for (i = 0; i < authctxt->num_auth_methods; i++) {
671e4a9863fSDag-Erling Smørgrav 		if (!remove_method(&(authctxt->auth_methods[i]), method,
672e4a9863fSDag-Erling Smørgrav 		    submethod))
6736888a9beSDag-Erling Smørgrav 			continue;
6746888a9beSDag-Erling Smørgrav 		found = 1;
6756888a9beSDag-Erling Smørgrav 		if (*authctxt->auth_methods[i] == '\0') {
6766888a9beSDag-Erling Smørgrav 			debug2("authentication methods list %d complete", i);
6776888a9beSDag-Erling Smørgrav 			return 1;
6786888a9beSDag-Erling Smørgrav 		}
6796888a9beSDag-Erling Smørgrav 		debug3("authentication methods list %d remaining: \"%s\"",
6806888a9beSDag-Erling Smørgrav 		    i, authctxt->auth_methods[i]);
6816888a9beSDag-Erling Smørgrav 	}
6826888a9beSDag-Erling Smørgrav 	/* This should not happen, but would be bad if it did */
6836888a9beSDag-Erling Smørgrav 	if (!found)
68419261079SEd Maste 		fatal_f("method not in AuthenticationMethods");
6856888a9beSDag-Erling Smørgrav 	return 0;
6866888a9beSDag-Erling Smørgrav }
6876888a9beSDag-Erling Smørgrav 
6884f52dfbbSDag-Erling Smørgrav /* Reset method-specific information */
auth2_authctxt_reset_info(Authctxt * authctxt)6894f52dfbbSDag-Erling Smørgrav void auth2_authctxt_reset_info(Authctxt *authctxt)
6904f52dfbbSDag-Erling Smørgrav {
6914f52dfbbSDag-Erling Smørgrav 	sshkey_free(authctxt->auth_method_key);
6924f52dfbbSDag-Erling Smørgrav 	free(authctxt->auth_method_info);
6934f52dfbbSDag-Erling Smørgrav 	authctxt->auth_method_key = NULL;
6944f52dfbbSDag-Erling Smørgrav 	authctxt->auth_method_info = NULL;
6954f52dfbbSDag-Erling Smørgrav }
6964f52dfbbSDag-Erling Smørgrav 
6974f52dfbbSDag-Erling Smørgrav /* Record auth method-specific information for logs */
6984f52dfbbSDag-Erling Smørgrav void
auth2_record_info(Authctxt * authctxt,const char * fmt,...)6994f52dfbbSDag-Erling Smørgrav auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
7004f52dfbbSDag-Erling Smørgrav {
7014f52dfbbSDag-Erling Smørgrav 	va_list ap;
7024f52dfbbSDag-Erling Smørgrav 	int i;
7034f52dfbbSDag-Erling Smørgrav 
7044f52dfbbSDag-Erling Smørgrav 	free(authctxt->auth_method_info);
7054f52dfbbSDag-Erling Smørgrav 	authctxt->auth_method_info = NULL;
7064f52dfbbSDag-Erling Smørgrav 
7074f52dfbbSDag-Erling Smørgrav 	va_start(ap, fmt);
7084f52dfbbSDag-Erling Smørgrav 	i = vasprintf(&authctxt->auth_method_info, fmt, ap);
7094f52dfbbSDag-Erling Smørgrav 	va_end(ap);
7104f52dfbbSDag-Erling Smørgrav 
71119261079SEd Maste 	if (i == -1)
71219261079SEd Maste 		fatal_f("vasprintf failed");
7134f52dfbbSDag-Erling Smørgrav }
7144f52dfbbSDag-Erling Smørgrav 
7154f52dfbbSDag-Erling Smørgrav /*
7164f52dfbbSDag-Erling Smørgrav  * Records a public key used in authentication. This is used for logging
7174f52dfbbSDag-Erling Smørgrav  * and to ensure that the same key is not subsequently accepted again for
7184f52dfbbSDag-Erling Smørgrav  * multiple authentication.
7194f52dfbbSDag-Erling Smørgrav  */
7204f52dfbbSDag-Erling Smørgrav void
auth2_record_key(Authctxt * authctxt,int authenticated,const struct sshkey * key)7214f52dfbbSDag-Erling Smørgrav auth2_record_key(Authctxt *authctxt, int authenticated,
7224f52dfbbSDag-Erling Smørgrav     const struct sshkey *key)
7234f52dfbbSDag-Erling Smørgrav {
7244f52dfbbSDag-Erling Smørgrav 	struct sshkey **tmp, *dup;
7254f52dfbbSDag-Erling Smørgrav 	int r;
7264f52dfbbSDag-Erling Smørgrav 
7272a01feabSEd Maste 	if ((r = sshkey_from_private(key, &dup)) != 0)
72819261079SEd Maste 		fatal_fr(r, "copy key");
7294f52dfbbSDag-Erling Smørgrav 	sshkey_free(authctxt->auth_method_key);
7304f52dfbbSDag-Erling Smørgrav 	authctxt->auth_method_key = dup;
7314f52dfbbSDag-Erling Smørgrav 
7324f52dfbbSDag-Erling Smørgrav 	if (!authenticated)
7334f52dfbbSDag-Erling Smørgrav 		return;
7344f52dfbbSDag-Erling Smørgrav 
7354f52dfbbSDag-Erling Smørgrav 	/* If authenticated, make sure we don't accept this key again */
7362a01feabSEd Maste 	if ((r = sshkey_from_private(key, &dup)) != 0)
73719261079SEd Maste 		fatal_fr(r, "copy key");
7384f52dfbbSDag-Erling Smørgrav 	if (authctxt->nprev_keys >= INT_MAX ||
7394f52dfbbSDag-Erling Smørgrav 	    (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
7404f52dfbbSDag-Erling Smørgrav 	    authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
74119261079SEd Maste 		fatal_f("reallocarray failed");
7424f52dfbbSDag-Erling Smørgrav 	authctxt->prev_keys = tmp;
7434f52dfbbSDag-Erling Smørgrav 	authctxt->prev_keys[authctxt->nprev_keys] = dup;
7444f52dfbbSDag-Erling Smørgrav 	authctxt->nprev_keys++;
7454f52dfbbSDag-Erling Smørgrav 
7464f52dfbbSDag-Erling Smørgrav }
7474f52dfbbSDag-Erling Smørgrav 
7484f52dfbbSDag-Erling Smørgrav /* Checks whether a key has already been previously used for authentication */
7494f52dfbbSDag-Erling Smørgrav int
auth2_key_already_used(Authctxt * authctxt,const struct sshkey * key)7504f52dfbbSDag-Erling Smørgrav auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
7514f52dfbbSDag-Erling Smørgrav {
7524f52dfbbSDag-Erling Smørgrav 	u_int i;
7534f52dfbbSDag-Erling Smørgrav 	char *fp;
7544f52dfbbSDag-Erling Smørgrav 
7554f52dfbbSDag-Erling Smørgrav 	for (i = 0; i < authctxt->nprev_keys; i++) {
7564f52dfbbSDag-Erling Smørgrav 		if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
7574f52dfbbSDag-Erling Smørgrav 			fp = sshkey_fingerprint(authctxt->prev_keys[i],
7584f52dfbbSDag-Erling Smørgrav 			    options.fingerprint_hash, SSH_FP_DEFAULT);
75919261079SEd Maste 			debug3_f("key already used: %s %s",
7604f52dfbbSDag-Erling Smørgrav 			    sshkey_type(authctxt->prev_keys[i]),
7614f52dfbbSDag-Erling Smørgrav 			    fp == NULL ? "UNKNOWN" : fp);
7624f52dfbbSDag-Erling Smørgrav 			free(fp);
7634f52dfbbSDag-Erling Smørgrav 			return 1;
7644f52dfbbSDag-Erling Smørgrav 		}
7654f52dfbbSDag-Erling Smørgrav 	}
7664f52dfbbSDag-Erling Smørgrav 	return 0;
7674f52dfbbSDag-Erling Smørgrav }
7684f52dfbbSDag-Erling Smørgrav 
7694f52dfbbSDag-Erling Smørgrav /*
7704f52dfbbSDag-Erling Smørgrav  * Updates authctxt->session_info with details of authentication. Should be
7714f52dfbbSDag-Erling Smørgrav  * whenever an authentication method succeeds.
7724f52dfbbSDag-Erling Smørgrav  */
7734f52dfbbSDag-Erling Smørgrav void
auth2_update_session_info(Authctxt * authctxt,const char * method,const char * submethod)7744f52dfbbSDag-Erling Smørgrav auth2_update_session_info(Authctxt *authctxt, const char *method,
7754f52dfbbSDag-Erling Smørgrav     const char *submethod)
7764f52dfbbSDag-Erling Smørgrav {
7774f52dfbbSDag-Erling Smørgrav 	int r;
7784f52dfbbSDag-Erling Smørgrav 
7794f52dfbbSDag-Erling Smørgrav 	if (authctxt->session_info == NULL) {
7804f52dfbbSDag-Erling Smørgrav 		if ((authctxt->session_info = sshbuf_new()) == NULL)
78119261079SEd Maste 			fatal_f("sshbuf_new");
7824f52dfbbSDag-Erling Smørgrav 	}
7834f52dfbbSDag-Erling Smørgrav 
7844f52dfbbSDag-Erling Smørgrav 	/* Append method[/submethod] */
7854f52dfbbSDag-Erling Smørgrav 	if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
7864f52dfbbSDag-Erling Smørgrav 	    method, submethod == NULL ? "" : "/",
7874f52dfbbSDag-Erling Smørgrav 	    submethod == NULL ? "" : submethod)) != 0)
78819261079SEd Maste 		fatal_fr(r, "append method");
7894f52dfbbSDag-Erling Smørgrav 
7904f52dfbbSDag-Erling Smørgrav 	/* Append key if present */
7914f52dfbbSDag-Erling Smørgrav 	if (authctxt->auth_method_key != NULL) {
7924f52dfbbSDag-Erling Smørgrav 		if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
7934f52dfbbSDag-Erling Smørgrav 		    (r = sshkey_format_text(authctxt->auth_method_key,
7944f52dfbbSDag-Erling Smørgrav 		    authctxt->session_info)) != 0)
79519261079SEd Maste 			fatal_fr(r, "append key");
7964f52dfbbSDag-Erling Smørgrav 	}
7974f52dfbbSDag-Erling Smørgrav 
7984f52dfbbSDag-Erling Smørgrav 	if (authctxt->auth_method_info != NULL) {
7994f52dfbbSDag-Erling Smørgrav 		/* Ensure no ambiguity here */
8004f52dfbbSDag-Erling Smørgrav 		if (strchr(authctxt->auth_method_info, '\n') != NULL)
80119261079SEd Maste 			fatal_f("auth_method_info contains \\n");
8024f52dfbbSDag-Erling Smørgrav 		if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
8034f52dfbbSDag-Erling Smørgrav 		    (r = sshbuf_putf(authctxt->session_info, "%s",
8044f52dfbbSDag-Erling Smørgrav 		    authctxt->auth_method_info)) != 0) {
80519261079SEd Maste 			fatal_fr(r, "append method info");
8064f52dfbbSDag-Erling Smørgrav 		}
8074f52dfbbSDag-Erling Smørgrav 	}
8084f52dfbbSDag-Erling Smørgrav 	if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
80919261079SEd Maste 		fatal_fr(r, "append");
8104f52dfbbSDag-Erling Smørgrav }
8116888a9beSDag-Erling Smørgrav 
812