xref: /openbsd/usr.bin/ssh/monitor.c (revision 856b6ee8)
1 /* $OpenBSD: monitor.c,v 1.246 2024/10/14 01:57:50 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2002 Markus Friedl <markus@openbsd.org>
5  * 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 <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/socket.h>
31 #include <sys/tree.h>
32 #include <sys/queue.h>
33 
34 #ifdef WITH_OPENSSL
35 #include <openssl/dh.h>
36 #endif
37 
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <paths.h>
42 #include <poll.h>
43 #include <pwd.h>
44 #include <signal.h>
45 #include <stdarg.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "atomicio.h"
53 #include "xmalloc.h"
54 #include "ssh.h"
55 #include "sshkey.h"
56 #include "sshbuf.h"
57 #include "hostfile.h"
58 #include "auth.h"
59 #include "cipher.h"
60 #include "kex.h"
61 #include "dh.h"
62 #include "packet.h"
63 #include "auth-options.h"
64 #include "sshpty.h"
65 #include "channels.h"
66 #include "session.h"
67 #include "sshlogin.h"
68 #include "canohost.h"
69 #include "log.h"
70 #include "misc.h"
71 #include "servconf.h"
72 #include "monitor.h"
73 #ifdef GSSAPI
74 #include "ssh-gss.h"
75 #endif
76 #include "monitor_wrap.h"
77 #include "monitor_fdpass.h"
78 #include "compat.h"
79 #include "ssh2.h"
80 #include "authfd.h"
81 #include "match.h"
82 #include "ssherr.h"
83 #include "sk-api.h"
84 #include "srclimit.h"
85 
86 #ifdef GSSAPI
87 static Gssctxt *gsscontext = NULL;
88 #endif
89 
90 /* Imports */
91 extern ServerOptions options;
92 extern u_int utmp_len;
93 extern struct sshbuf *cfg;
94 extern struct sshbuf *loginmsg;
95 extern struct include_list includes;
96 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
97 
98 /* State exported from the child */
99 static struct sshbuf *child_state;
100 
101 /* Functions on the monitor that answer unprivileged requests */
102 
103 int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
104 int mm_answer_sign(struct ssh *, int, struct sshbuf *);
105 int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
106 int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
107 int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
108 int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
109 int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
110 int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
111 int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
112 int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
113 int mm_answer_pty(struct ssh *, int, struct sshbuf *);
114 int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
115 int mm_answer_term(struct ssh *, int, struct sshbuf *);
116 int mm_answer_state(struct ssh *, int, struct sshbuf *);
117 
118 #ifdef GSSAPI
119 int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
120 int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
121 int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
122 int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
123 #endif
124 
125 static Authctxt *authctxt;
126 
127 /* local state for key verify */
128 static u_char *key_blob = NULL;
129 static size_t key_bloblen = 0;
130 static u_int key_blobtype = MM_NOKEY;
131 static struct sshauthopt *key_opts = NULL;
132 static char *hostbased_cuser = NULL;
133 static char *hostbased_chost = NULL;
134 static char *auth_method = "unknown";
135 static char *auth_submethod = NULL;
136 static u_int session_id2_len = 0;
137 static u_char *session_id2 = NULL;
138 static pid_t monitor_child_pid;
139 int auth_attempted = 0;
140 
141 struct mon_table {
142 	enum monitor_reqtype type;
143 	int flags;
144 	int (*f)(struct ssh *, int, struct sshbuf *);
145 };
146 
147 #define MON_ISAUTH	0x0004	/* Required for Authentication */
148 #define MON_AUTHDECIDE	0x0008	/* Decides Authentication */
149 #define MON_ONCE	0x0010	/* Disable after calling */
150 #define MON_ALOG	0x0020	/* Log auth attempt without authenticating */
151 
152 #define MON_AUTH	(MON_ISAUTH|MON_AUTHDECIDE)
153 
154 #define MON_PERMIT	0x1000	/* Request is permitted */
155 
156 static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
157     struct mon_table **);
158 static int monitor_read_log(struct monitor *);
159 
160 struct mon_table mon_dispatch_proto20[] = {
161     {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state},
162 #ifdef WITH_OPENSSL
163     {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
164 #endif
165     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
166     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
167     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
168     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
169     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
170     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
171     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
172     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
173     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
174 #ifdef GSSAPI
175     {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
176     {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
177     {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
178     {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
179 #endif
180     {0, 0, NULL}
181 };
182 
183 struct mon_table mon_dispatch_postauth20[] = {
184     {MONITOR_REQ_STATE, MON_ONCE, mm_answer_state},
185 #ifdef WITH_OPENSSL
186     {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
187 #endif
188     {MONITOR_REQ_SIGN, 0, mm_answer_sign},
189     {MONITOR_REQ_PTY, 0, mm_answer_pty},
190     {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
191     {MONITOR_REQ_TERM, 0, mm_answer_term},
192     {0, 0, NULL}
193 };
194 
195 struct mon_table *mon_dispatch;
196 
197 /* Specifies if a certain message is allowed at the moment */
198 static void
monitor_permit(struct mon_table * ent,enum monitor_reqtype type,int permit)199 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
200 {
201 	while (ent->f != NULL) {
202 		if (ent->type == type) {
203 			ent->flags &= ~MON_PERMIT;
204 			ent->flags |= permit ? MON_PERMIT : 0;
205 			return;
206 		}
207 		ent++;
208 	}
209 }
210 
211 static void
monitor_permit_authentications(int permit)212 monitor_permit_authentications(int permit)
213 {
214 	struct mon_table *ent = mon_dispatch;
215 
216 	while (ent->f != NULL) {
217 		if (ent->flags & MON_AUTH) {
218 			ent->flags &= ~MON_PERMIT;
219 			ent->flags |= permit ? MON_PERMIT : 0;
220 		}
221 		ent++;
222 	}
223 }
224 
225 void
monitor_child_preauth(struct ssh * ssh,struct monitor * pmonitor)226 monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
227 {
228 	struct mon_table *ent;
229 	int authenticated = 0, partial = 0;
230 
231 	debug3("preauth child monitor started");
232 
233 	if (pmonitor->m_recvfd >= 0)
234 		close(pmonitor->m_recvfd);
235 	if (pmonitor->m_log_sendfd >= 0)
236 		close(pmonitor->m_log_sendfd);
237 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
238 
239 	authctxt = (Authctxt *)ssh->authctxt;
240 	memset(authctxt, 0, sizeof(*authctxt));
241 	ssh->authctxt = authctxt;
242 
243 	mon_dispatch = mon_dispatch_proto20;
244 	/* Permit requests for state, moduli and signatures */
245 	monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1);
246 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
247 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
248 
249 	/* The first few requests do not require asynchronous access */
250 	while (!authenticated) {
251 		partial = 0;
252 		auth_method = "unknown";
253 		auth_submethod = NULL;
254 		auth2_authctxt_reset_info(authctxt);
255 
256 		authenticated = (monitor_read(ssh, pmonitor,
257 		    mon_dispatch, &ent) == 1);
258 
259 		/* Record that auth was attempted to set exit status later */
260 		if ((ent->flags & MON_AUTH) != 0)
261 			auth_attempted = 1;
262 
263 		/* Special handling for multiple required authentications */
264 		if (options.num_auth_methods != 0) {
265 			if (authenticated &&
266 			    !auth2_update_methods_lists(authctxt,
267 			    auth_method, auth_submethod)) {
268 				debug3_f("method %s: partial", auth_method);
269 				authenticated = 0;
270 				partial = 1;
271 			}
272 		}
273 
274 		if (authenticated) {
275 			if (!(ent->flags & MON_AUTHDECIDE))
276 				fatal_f("unexpected authentication from %d",
277 				    ent->type);
278 			if (authctxt->pw->pw_uid == 0 &&
279 			    !auth_root_allowed(ssh, auth_method))
280 				authenticated = 0;
281 		}
282 		if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
283 			auth_log(ssh, authenticated, partial,
284 			    auth_method, auth_submethod);
285 			if (!partial && !authenticated)
286 				authctxt->failures++;
287 			if (authenticated || partial) {
288 				auth2_update_session_info(authctxt,
289 				    auth_method, auth_submethod);
290 			}
291 		}
292 		if (authctxt->failures > options.max_authtries) {
293 			/* Shouldn't happen */
294 			fatal_f("privsep child made too many authentication "
295 			    "attempts");
296 		}
297 	}
298 
299 	if (!authctxt->valid)
300 		fatal_f("authenticated invalid user");
301 	if (strcmp(auth_method, "unknown") == 0)
302 		fatal_f("authentication method name unknown");
303 
304 	debug_f("user %s authenticated by privileged process", authctxt->user);
305 	auth_attempted = 0;
306 	ssh->authctxt = NULL;
307 	ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
308 
309 	mm_get_keystate(ssh, pmonitor);
310 
311 	/* Drain any buffered messages from the child */
312 	while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
313 		;
314 
315 	if (pmonitor->m_recvfd >= 0)
316 		close(pmonitor->m_recvfd);
317 	if (pmonitor->m_log_sendfd >= 0)
318 		close(pmonitor->m_log_sendfd);
319 	pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
320 }
321 
322 static void
monitor_set_child_handler(pid_t pid)323 monitor_set_child_handler(pid_t pid)
324 {
325 	monitor_child_pid = pid;
326 }
327 
328 static void
monitor_child_handler(int sig)329 monitor_child_handler(int sig)
330 {
331 	kill(monitor_child_pid, sig);
332 }
333 
334 void
monitor_child_postauth(struct ssh * ssh,struct monitor * pmonitor)335 monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
336 {
337 	close(pmonitor->m_recvfd);
338 	pmonitor->m_recvfd = -1;
339 
340 	monitor_set_child_handler(pmonitor->m_pid);
341 	ssh_signal(SIGHUP, &monitor_child_handler);
342 	ssh_signal(SIGTERM, &monitor_child_handler);
343 	ssh_signal(SIGINT, &monitor_child_handler);
344 
345 	mon_dispatch = mon_dispatch_postauth20;
346 
347 	/* Permit requests for moduli and signatures */
348 	monitor_permit(mon_dispatch, MONITOR_REQ_STATE, 1);
349 	monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
350 	monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
351 	monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
352 
353 	if (auth_opts->permit_pty_flag) {
354 		monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
355 		monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
356 	}
357 
358 	for (;;)
359 		monitor_read(ssh, pmonitor, mon_dispatch, NULL);
360 }
361 
362 static int
monitor_read_log(struct monitor * pmonitor)363 monitor_read_log(struct monitor *pmonitor)
364 {
365 	struct sshbuf *logmsg;
366 	u_int len, level, forced;
367 	char *msg;
368 	u_char *p;
369 	int r;
370 
371 	if ((logmsg = sshbuf_new()) == NULL)
372 		fatal_f("sshbuf_new");
373 
374 	/* Read length */
375 	if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
376 		fatal_fr(r, "reserve len");
377 	if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
378 		if (errno == EPIPE) {
379 			sshbuf_free(logmsg);
380 			debug_f("child log fd closed");
381 			close(pmonitor->m_log_recvfd);
382 			pmonitor->m_log_recvfd = -1;
383 			return -1;
384 		}
385 		fatal_f("log fd read: %s", strerror(errno));
386 	}
387 	if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
388 		fatal_fr(r, "parse len");
389 	if (len <= 4 || len > 8192)
390 		fatal_f("invalid log message length %u", len);
391 
392 	/* Read severity, message */
393 	sshbuf_reset(logmsg);
394 	if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
395 		fatal_fr(r, "reserve msg");
396 	if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
397 		fatal_f("log fd read: %s", strerror(errno));
398 	if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
399 	    (r = sshbuf_get_u32(logmsg, &forced)) != 0 ||
400 	    (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
401 		fatal_fr(r, "parse");
402 
403 	/* Log it */
404 	if (log_level_name(level) == NULL)
405 		fatal_f("invalid log level %u (corrupted message?)", level);
406 	sshlogdirect(level, forced, "%s [%s]", msg,
407 	    mon_dispatch == mon_dispatch_postauth20 ? "postauth" : "preauth");
408 
409 	sshbuf_free(logmsg);
410 	free(msg);
411 
412 	return 0;
413 }
414 
415 static int
monitor_read(struct ssh * ssh,struct monitor * pmonitor,struct mon_table * ent,struct mon_table ** pent)416 monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
417     struct mon_table **pent)
418 {
419 	struct sshbuf *m;
420 	int r, ret;
421 	u_char type;
422 	struct pollfd pfd[2];
423 
424 	for (;;) {
425 		memset(&pfd, 0, sizeof(pfd));
426 		pfd[0].fd = pmonitor->m_sendfd;
427 		pfd[0].events = POLLIN;
428 		pfd[1].fd = pmonitor->m_log_recvfd;
429 		pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
430 		if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
431 			if (errno == EINTR || errno == EAGAIN)
432 				continue;
433 			fatal_f("poll: %s", strerror(errno));
434 		}
435 		if (pfd[1].revents) {
436 			/*
437 			 * Drain all log messages before processing next
438 			 * monitor request.
439 			 */
440 			monitor_read_log(pmonitor);
441 			continue;
442 		}
443 		if (pfd[0].revents)
444 			break;  /* Continues below */
445 	}
446 
447 	if ((m = sshbuf_new()) == NULL)
448 		fatal_f("sshbuf_new");
449 
450 	mm_request_receive(pmonitor->m_sendfd, m);
451 	if ((r = sshbuf_get_u8(m, &type)) != 0)
452 		fatal_fr(r, "parse type");
453 
454 	debug3_f("checking request %d", type);
455 
456 	while (ent->f != NULL) {
457 		if (ent->type == type)
458 			break;
459 		ent++;
460 	}
461 
462 	if (ent->f != NULL) {
463 		if (!(ent->flags & MON_PERMIT))
464 			fatal_f("unpermitted request %d", type);
465 		ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
466 		sshbuf_free(m);
467 
468 		/* The child may use this request only once, disable it */
469 		if (ent->flags & MON_ONCE) {
470 			debug2_f("%d used once, disabling now", type);
471 			ent->flags &= ~MON_PERMIT;
472 		}
473 
474 		if (pent != NULL)
475 			*pent = ent;
476 
477 		return ret;
478 	}
479 
480 	fatal_f("unsupported request: %d", type);
481 
482 	/* NOTREACHED */
483 	return (-1);
484 }
485 
486 /* allowed key state */
487 static int
monitor_allowed_key(const u_char * blob,u_int bloblen)488 monitor_allowed_key(const u_char *blob, u_int bloblen)
489 {
490 	/* make sure key is allowed */
491 	if (key_blob == NULL || key_bloblen != bloblen ||
492 	    timingsafe_bcmp(key_blob, blob, key_bloblen))
493 		return (0);
494 	return (1);
495 }
496 
497 static void
monitor_reset_key_state(void)498 monitor_reset_key_state(void)
499 {
500 	/* reset state */
501 	free(key_blob);
502 	free(hostbased_cuser);
503 	free(hostbased_chost);
504 	sshauthopt_free(key_opts);
505 	key_blob = NULL;
506 	key_bloblen = 0;
507 	key_blobtype = MM_NOKEY;
508 	key_opts = NULL;
509 	hostbased_cuser = NULL;
510 	hostbased_chost = NULL;
511 }
512 
513 int
mm_answer_state(struct ssh * ssh,int sock,struct sshbuf * m)514 mm_answer_state(struct ssh *ssh, int sock, struct sshbuf *m)
515 {
516 	struct sshbuf *inc = NULL, *hostkeys = NULL;
517 	struct sshbuf *opts = NULL, *confdata = NULL;
518 	struct include_item *item = NULL;
519 	int postauth;
520 	int r;
521 
522 	sshbuf_reset(m);
523 
524 	debug_f("config len %zu", sshbuf_len(cfg));
525 
526 	if ((m = sshbuf_new()) == NULL ||
527 	    (inc = sshbuf_new()) == NULL ||
528 	    (opts = sshbuf_new()) == NULL ||
529 	    (confdata = sshbuf_new()) == NULL)
530 		fatal_f("sshbuf_new failed");
531 
532 	/* XXX unneccessary? */
533 	/* pack includes into a string */
534 	TAILQ_FOREACH(item, &includes, entry) {
535 		if ((r = sshbuf_put_cstring(inc, item->selector)) != 0 ||
536 		    (r = sshbuf_put_cstring(inc, item->filename)) != 0 ||
537 		    (r = sshbuf_put_stringb(inc, item->contents)) != 0)
538 			fatal_fr(r, "compose includes");
539 	}
540 
541 	hostkeys = pack_hostkeys();
542 
543 	/*
544 	 * Protocol from monitor to unpriv privsep process:
545 	 *	string	configuration
546 	 *	uint64	timing_secret	XXX move delays to monitor and remove
547 	 *	string	host_keys[] {
548 	 *		string public_key
549 	 *		string certificate
550 	 *	}
551 	 *	string  server_banner
552 	 *	string  client_banner
553 	 *	string	included_files[] {
554 	 *		string	selector
555 	 *		string	filename
556 	 *		string	contents
557 	 *	}
558 	 *	string	configuration_data (postauth)
559 	 *	string  keystate (postauth)
560 	 *	string  authenticated_user (postauth)
561 	 *	string  session_info (postauth)
562 	 *	string  authopts (postauth)
563 	 */
564 	if ((r = sshbuf_put_stringb(m, cfg)) != 0 ||
565 	    (r = sshbuf_put_u64(m, options.timing_secret)) != 0 ||
566 	    (r = sshbuf_put_stringb(m, hostkeys)) != 0 ||
567 	    (r = sshbuf_put_stringb(m, ssh->kex->server_version)) != 0 ||
568 	    (r = sshbuf_put_stringb(m, ssh->kex->client_version)) != 0 ||
569 	    (r = sshbuf_put_stringb(m, inc)) != 0)
570 		fatal_fr(r, "compose config");
571 
572 	postauth = (authctxt && authctxt->pw && authctxt->authenticated);
573 	if (postauth) {
574 		/* XXX shouldn't be reachable */
575 		fatal_f("internal error: called in postauth");
576 	}
577 
578 	sshbuf_free(inc);
579 	sshbuf_free(opts);
580 	sshbuf_free(confdata);
581 
582 	mm_request_send(sock, MONITOR_ANS_STATE, m);
583 
584 	debug3_f("done");
585 
586 	return (0);
587 }
588 
589 #ifdef WITH_OPENSSL
590 int
mm_answer_moduli(struct ssh * ssh,int sock,struct sshbuf * m)591 mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
592 {
593 	DH *dh;
594 	const BIGNUM *dh_p, *dh_g;
595 	int r;
596 	u_int min, want, max;
597 
598 	if ((r = sshbuf_get_u32(m, &min)) != 0 ||
599 	    (r = sshbuf_get_u32(m, &want)) != 0 ||
600 	    (r = sshbuf_get_u32(m, &max)) != 0)
601 		fatal_fr(r, "parse");
602 
603 	debug3_f("got parameters: %d %d %d", min, want, max);
604 	/* We need to check here, too, in case the child got corrupted */
605 	if (max < min || want < min || max < want)
606 		fatal_f("bad parameters: %d %d %d", min, want, max);
607 
608 	sshbuf_reset(m);
609 
610 	dh = choose_dh(min, want, max);
611 	if (dh == NULL) {
612 		if ((r = sshbuf_put_u8(m, 0)) != 0)
613 			fatal_fr(r, "assemble empty");
614 		return (0);
615 	} else {
616 		/* Send first bignum */
617 		DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
618 		if ((r = sshbuf_put_u8(m, 1)) != 0 ||
619 		    (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
620 		    (r = sshbuf_put_bignum2(m, dh_g)) != 0)
621 			fatal_fr(r, "assemble");
622 
623 		DH_free(dh);
624 	}
625 	mm_request_send(sock, MONITOR_ANS_MODULI, m);
626 	return (0);
627 }
628 #endif
629 
630 int
mm_answer_sign(struct ssh * ssh,int sock,struct sshbuf * m)631 mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
632 {
633 	extern int auth_sock;			/* XXX move to state struct? */
634 	struct sshkey *pubkey, *key;
635 	struct sshbuf *sigbuf = NULL;
636 	u_char *p = NULL, *signature = NULL;
637 	char *alg = NULL;
638 	size_t datlen, siglen;
639 	int r, is_proof = 0, keyid;
640 	u_int compat;
641 	const char proof_req[] = "hostkeys-prove-00@openssh.com";
642 
643 	debug3_f("entering");
644 
645 	if ((r = sshkey_froms(m, &pubkey)) != 0 ||
646 	    (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
647 	    (r = sshbuf_get_cstring(m, &alg, NULL)) != 0 ||
648 	    (r = sshbuf_get_u32(m, &compat)) != 0)
649 		fatal_fr(r, "parse");
650 
651 	if ((keyid = get_hostkey_index(pubkey, 1, ssh)) == -1)
652 		fatal_f("unknown hostkey");
653 	debug_f("hostkey %s index %d", sshkey_ssh_name(pubkey), keyid);
654 	sshkey_free(pubkey);
655 
656 	/*
657 	 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
658 	 * SHA384 (48 bytes) and SHA512 (64 bytes).
659 	 *
660 	 * Otherwise, verify the signature request is for a hostkey
661 	 * proof.
662 	 *
663 	 * XXX perform similar check for KEX signature requests too?
664 	 * it's not trivial, since what is signed is the hash, rather
665 	 * than the full kex structure...
666 	 */
667 	if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
668 		/*
669 		 * Construct expected hostkey proof and compare it to what
670 		 * the client sent us.
671 		 */
672 		if (session_id2_len == 0) /* hostkeys is never first */
673 			fatal_f("bad data length: %zu", datlen);
674 		if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
675 			fatal_f("no hostkey for index %d", keyid);
676 		if ((sigbuf = sshbuf_new()) == NULL)
677 			fatal_f("sshbuf_new");
678 		if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
679 		    (r = sshbuf_put_string(sigbuf, session_id2,
680 		    session_id2_len)) != 0 ||
681 		    (r = sshkey_puts(key, sigbuf)) != 0)
682 			fatal_fr(r, "assemble private key proof");
683 		if (datlen != sshbuf_len(sigbuf) ||
684 		    memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
685 			fatal_f("bad data length: %zu, hostkey proof len %zu",
686 			    datlen, sshbuf_len(sigbuf));
687 		sshbuf_free(sigbuf);
688 		is_proof = 1;
689 	}
690 
691 	/* save session id, it will be passed on the first call */
692 	if (session_id2_len == 0) {
693 		session_id2_len = datlen;
694 		session_id2 = xmalloc(session_id2_len);
695 		memcpy(session_id2, p, session_id2_len);
696 	}
697 
698 	if ((key = get_hostkey_by_index(keyid)) != NULL) {
699 		if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
700 		    options.sk_provider, NULL, compat)) != 0)
701 			fatal_fr(r, "sign");
702 	} else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
703 	    auth_sock > 0) {
704 		if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
705 		    p, datlen, alg, compat)) != 0)
706 			fatal_fr(r, "agent sign");
707 	} else
708 		fatal_f("no hostkey from index %d", keyid);
709 
710 	debug3_f("%s %s signature len=%zu", alg,
711 	    is_proof ? "hostkey proof" : "KEX", siglen);
712 
713 	sshbuf_reset(m);
714 	if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
715 		fatal_fr(r, "assemble");
716 
717 	free(alg);
718 	free(p);
719 	free(signature);
720 
721 	mm_request_send(sock, MONITOR_ANS_SIGN, m);
722 
723 	/* Turn on permissions for getpwnam */
724 	monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
725 
726 	return (0);
727 }
728 
729 #define PUTPW(b, id) \
730 	do { \
731 		if ((r = sshbuf_put_string(b, \
732 		    &pwent->id, sizeof(pwent->id))) != 0) \
733 			fatal_fr(r, "assemble %s", #id); \
734 	} while (0)
735 
736 void
mm_encode_server_options(struct sshbuf * m)737 mm_encode_server_options(struct sshbuf *m)
738 {
739 	int r;
740 	u_int i;
741 
742 	/* XXX this leaks raw pointers to the unpriv child processes */
743 	if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
744 		fatal_fr(r, "assemble options");
745 
746 #define M_CP_STROPT(x) do { \
747 		if (options.x != NULL && \
748 		    (r = sshbuf_put_cstring(m, options.x)) != 0) \
749 			fatal_fr(r, "assemble %s", #x); \
750 	} while (0)
751 #define M_CP_STRARRAYOPT(x, nx) do { \
752 		for (i = 0; i < options.nx; i++) { \
753 			if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
754 				fatal_fr(r, "assemble %s", #x); \
755 		} \
756 	} while (0)
757 	/* See comment in servconf.h */
758 	COPY_MATCH_STRING_OPTS();
759 #undef M_CP_STROPT
760 #undef M_CP_STRARRAYOPT
761 }
762 
763 /* Retrieves the password entry and also checks if the user is permitted */
764 int
mm_answer_pwnamallow(struct ssh * ssh,int sock,struct sshbuf * m)765 mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
766 {
767 	struct passwd *pwent;
768 	int r, allowed = 0;
769 
770 	debug3_f("entering");
771 
772 	if (authctxt->attempt++ != 0)
773 		fatal_f("multiple attempts for getpwnam");
774 
775 	if ((r = sshbuf_get_cstring(m, &authctxt->user, NULL)) != 0)
776 		fatal_fr(r, "parse");
777 
778 	pwent = getpwnamallow(ssh, authctxt->user);
779 
780 	setproctitle("%s [priv]", pwent ? authctxt->user : "unknown");
781 
782 	sshbuf_reset(m);
783 
784 	if (pwent == NULL) {
785 		if ((r = sshbuf_put_u8(m, 0)) != 0)
786 			fatal_fr(r, "assemble fakepw");
787 		authctxt->pw = fakepw();
788 		goto out;
789 	}
790 
791 	allowed = 1;
792 	authctxt->pw = pwent;
793 	authctxt->valid = 1;
794 
795 	/* XXX send fake class/dir/shell, etc. */
796 	if ((r = sshbuf_put_u8(m, 1)) != 0)
797 		fatal_fr(r, "assemble ok");
798 	PUTPW(m, pw_uid);
799 	PUTPW(m, pw_gid);
800 	PUTPW(m, pw_change);
801 	PUTPW(m, pw_expire);
802 	if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
803 	    (r = sshbuf_put_cstring(m, "*")) != 0 ||
804 	    (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
805 	    (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
806 	    (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
807 	    (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
808 		fatal_fr(r, "assemble pw");
809 
810  out:
811 	ssh_packet_set_log_preamble(ssh, "%suser %s",
812 	    authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
813 
814 	if (options.refuse_connection) {
815 		logit("administratively prohibited connection for "
816 		    "%s%s from %.128s port %d",
817 		    authctxt->valid ? "" : "invalid user ",
818 		    authctxt->user, ssh_remote_ipaddr(ssh),
819 		    ssh_remote_port(ssh));
820 		cleanup_exit(EXIT_CONFIG_REFUSED);
821 	}
822 
823 	/* Send active options to unpriv */
824 	mm_encode_server_options(m);
825 
826 	/* Create valid auth method lists */
827 	if (auth2_setup_methods_lists(authctxt) != 0) {
828 		/*
829 		 * The monitor will continue long enough to let the child
830 		 * run to its packet_disconnect(), but it must not allow any
831 		 * authentication to succeed.
832 		 */
833 		debug_f("no valid authentication method lists");
834 	}
835 
836 	debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed);
837 	mm_request_send(sock, MONITOR_ANS_PWNAM, m);
838 
839 	/* Allow service/style information on the auth context */
840 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
841 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
842 
843 	return (0);
844 }
845 
mm_answer_auth2_read_banner(struct ssh * ssh,int sock,struct sshbuf * m)846 int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
847 {
848 	char *banner;
849 	int r;
850 
851 	sshbuf_reset(m);
852 	banner = auth2_read_banner();
853 	if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
854 		fatal_fr(r, "assemble");
855 	mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
856 	free(banner);
857 
858 	return (0);
859 }
860 
861 int
mm_answer_authserv(struct ssh * ssh,int sock,struct sshbuf * m)862 mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
863 {
864 	int r;
865 
866 	monitor_permit_authentications(1);
867 
868 	if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
869 	    (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
870 		fatal_fr(r, "parse");
871 	debug3_f("service=%s, style=%s", authctxt->service, authctxt->style);
872 
873 	if (strlen(authctxt->style) == 0) {
874 		free(authctxt->style);
875 		authctxt->style = NULL;
876 	}
877 
878 	return (0);
879 }
880 
881 int
mm_answer_authpassword(struct ssh * ssh,int sock,struct sshbuf * m)882 mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
883 {
884 	static int call_count;
885 	char *passwd;
886 	int r, authenticated;
887 	size_t plen;
888 
889 	if (!options.password_authentication)
890 		fatal_f("password authentication not enabled");
891 	if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
892 		fatal_fr(r, "parse");
893 	/* Only authenticate if the context is valid */
894 	authenticated = options.password_authentication &&
895 	    auth_password(ssh, passwd);
896 	freezero(passwd, plen);
897 
898 	sshbuf_reset(m);
899 	if ((r = sshbuf_put_u32(m, authenticated)) != 0)
900 		fatal_fr(r, "assemble");
901 
902 	debug3_f("sending result %d", authenticated);
903 	mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
904 
905 	call_count++;
906 	if (plen == 0 && call_count == 1)
907 		auth_method = "none";
908 	else
909 		auth_method = "password";
910 
911 	/* Causes monitor loop to terminate if authenticated */
912 	return (authenticated);
913 }
914 
915 int
mm_answer_bsdauthquery(struct ssh * ssh,int sock,struct sshbuf * m)916 mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
917 {
918 	char *name, *infotxt;
919 	u_int numprompts, *echo_on, success;
920 	char **prompts;
921 	int r;
922 
923 	if (!options.kbd_interactive_authentication)
924 		fatal_f("kbd-int authentication not enabled");
925 	success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
926 	    &prompts, &echo_on) < 0 ? 0 : 1;
927 
928 	sshbuf_reset(m);
929 	if ((r = sshbuf_put_u32(m, success)) != 0)
930 		fatal_fr(r, "assemble");
931 	if (success) {
932 		if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
933 			fatal_fr(r, "assemble prompt");
934 	}
935 
936 	debug3_f("sending challenge success: %u", success);
937 	mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
938 
939 	if (success) {
940 		free(name);
941 		free(infotxt);
942 		free(prompts);
943 		free(echo_on);
944 	}
945 
946 	return (0);
947 }
948 
949 int
mm_answer_bsdauthrespond(struct ssh * ssh,int sock,struct sshbuf * m)950 mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
951 {
952 	char *response;
953 	int r, authok;
954 
955 	if (!options.kbd_interactive_authentication)
956 		fatal_f("kbd-int authentication not enabled");
957 	if (authctxt->as == NULL)
958 		fatal_f("no bsd auth session");
959 
960 	if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
961 		fatal_fr(r, "parse");
962 	authok = options.kbd_interactive_authentication &&
963 	    auth_userresponse(authctxt->as, response, 0);
964 	authctxt->as = NULL;
965 	debug3_f("<%s> = <%d>", response, authok);
966 	free(response);
967 
968 	sshbuf_reset(m);
969 	if ((r = sshbuf_put_u32(m, authok)) != 0)
970 		fatal_fr(r, "assemble");
971 
972 	debug3_f("sending authenticated: %d", authok);
973 	mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
974 
975 	auth_method = "keyboard-interactive";
976 	auth_submethod = "bsdauth";
977 
978 	return (authok != 0);
979 }
980 
981 /*
982  * Check that the key type appears in the supplied pattern list, ignoring
983  * mismatches in the signature algorithm. (Signature algorithm checks are
984  * performed in the unprivileged authentication code).
985  * Returns 1 on success, 0 otherwise.
986  */
987 static int
key_base_type_match(const char * method,const struct sshkey * key,const char * list)988 key_base_type_match(const char *method, const struct sshkey *key,
989     const char *list)
990 {
991 	char *s, *l, *ol = xstrdup(list);
992 	int found = 0;
993 
994 	l = ol;
995 	for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
996 		if (sshkey_type_from_name(s) == key->type) {
997 			found = 1;
998 			break;
999 		}
1000 	}
1001 	if (!found) {
1002 		error("%s key type %s is not in permitted list %s", method,
1003 		    sshkey_ssh_name(key), list);
1004 	}
1005 
1006 	free(ol);
1007 	return found;
1008 }
1009 
1010 int
mm_answer_keyallowed(struct ssh * ssh,int sock,struct sshbuf * m)1011 mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
1012 {
1013 	struct sshkey *key = NULL;
1014 	char *cuser, *chost;
1015 	u_int pubkey_auth_attempt;
1016 	u_int type = 0;
1017 	int r, allowed = 0;
1018 	struct sshauthopt *opts = NULL;
1019 
1020 	debug3_f("entering");
1021 	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
1022 	    (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
1023 	    (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
1024 	    (r = sshkey_froms(m, &key)) != 0 ||
1025 	    (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
1026 		fatal_fr(r, "parse");
1027 
1028 	if (key != NULL && authctxt->valid) {
1029 		switch (type) {
1030 		case MM_USERKEY:
1031 			auth_method = "publickey";
1032 			if (!options.pubkey_authentication)
1033 				break;
1034 			if (auth2_key_already_used(authctxt, key))
1035 				break;
1036 			if (!key_base_type_match(auth_method, key,
1037 			    options.pubkey_accepted_algos))
1038 				break;
1039 			allowed = user_key_allowed(ssh, authctxt->pw, key,
1040 			    pubkey_auth_attempt, &opts);
1041 			break;
1042 		case MM_HOSTKEY:
1043 			auth_method = "hostbased";
1044 			if (!options.hostbased_authentication)
1045 				break;
1046 			if (auth2_key_already_used(authctxt, key))
1047 				break;
1048 			if (!key_base_type_match(auth_method, key,
1049 			    options.hostbased_accepted_algos))
1050 				break;
1051 			allowed = hostbased_key_allowed(ssh, authctxt->pw,
1052 			    cuser, chost, key);
1053 			auth2_record_info(authctxt,
1054 			    "client user \"%.100s\", client host \"%.100s\"",
1055 			    cuser, chost);
1056 			break;
1057 		default:
1058 			fatal_f("unknown key type %u", type);
1059 			break;
1060 		}
1061 	}
1062 
1063 	debug3_f("%s authentication%s: %s key is %s", auth_method,
1064 	    pubkey_auth_attempt ? "" : " test",
1065 	    (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
1066 	    allowed ? "allowed" : "not allowed");
1067 
1068 	auth2_record_key(authctxt, 0, key);
1069 
1070 	/* clear temporarily storage (used by verify) */
1071 	monitor_reset_key_state();
1072 
1073 	if (allowed) {
1074 		/* Save temporarily for comparison in verify */
1075 		if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
1076 			fatal_fr(r, "sshkey_to_blob");
1077 		key_blobtype = type;
1078 		key_opts = opts;
1079 		hostbased_cuser = cuser;
1080 		hostbased_chost = chost;
1081 	} else {
1082 		/* Log failed attempt */
1083 		auth_log(ssh, 0, 0, auth_method, NULL);
1084 		free(cuser);
1085 		free(chost);
1086 	}
1087 	sshkey_free(key);
1088 
1089 	sshbuf_reset(m);
1090 	if ((r = sshbuf_put_u32(m, allowed)) != 0)
1091 		fatal_fr(r, "assemble");
1092 	if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
1093 		fatal_fr(r, "sshauthopt_serialise");
1094 	mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1095 
1096 	if (!allowed)
1097 		sshauthopt_free(opts);
1098 
1099 	return (0);
1100 }
1101 
1102 static int
monitor_valid_userblob(struct ssh * ssh,const u_char * data,u_int datalen)1103 monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
1104 {
1105 	struct sshbuf *b;
1106 	struct sshkey *hostkey = NULL;
1107 	const u_char *p;
1108 	char *userstyle, *cp;
1109 	size_t len;
1110 	u_char type;
1111 	int hostbound = 0, r, fail = 0;
1112 
1113 	if ((b = sshbuf_from(data, datalen)) == NULL)
1114 		fatal_f("sshbuf_from");
1115 
1116 	if (ssh->compat & SSH_OLD_SESSIONID) {
1117 		p = sshbuf_ptr(b);
1118 		len = sshbuf_len(b);
1119 		if ((session_id2 == NULL) ||
1120 		    (len < session_id2_len) ||
1121 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1122 			fail++;
1123 		if ((r = sshbuf_consume(b, session_id2_len)) != 0)
1124 			fatal_fr(r, "consume");
1125 	} else {
1126 		if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1127 			fatal_fr(r, "parse sessionid");
1128 		if ((session_id2 == NULL) ||
1129 		    (len != session_id2_len) ||
1130 		    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1131 			fail++;
1132 	}
1133 	if ((r = sshbuf_get_u8(b, &type)) != 0)
1134 		fatal_fr(r, "parse type");
1135 	if (type != SSH2_MSG_USERAUTH_REQUEST)
1136 		fail++;
1137 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1138 		fatal_fr(r, "parse userstyle");
1139 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
1140 	    authctxt->style ? ":" : "",
1141 	    authctxt->style ? authctxt->style : "");
1142 	if (strcmp(userstyle, cp) != 0) {
1143 		logit("wrong user name passed to monitor: "
1144 		    "expected %s != %.100s", userstyle, cp);
1145 		fail++;
1146 	}
1147 	free(userstyle);
1148 	free(cp);
1149 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* service */
1150 	    (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1151 		fatal_fr(r, "parse method");
1152 	if (strcmp("publickey", cp) != 0) {
1153 		if (strcmp("publickey-hostbound-v00@openssh.com", cp) == 0)
1154 			hostbound = 1;
1155 		else
1156 			fail++;
1157 	}
1158 	free(cp);
1159 	if ((r = sshbuf_get_u8(b, &type)) != 0)
1160 		fatal_fr(r, "parse pktype");
1161 	if (type == 0)
1162 		fail++;
1163 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* pkalg */
1164 	    (r = sshbuf_skip_string(b)) != 0 ||	/* pkblob */
1165 	    (hostbound && (r = sshkey_froms(b, &hostkey)) != 0))
1166 		fatal_fr(r, "parse pk");
1167 	if (sshbuf_len(b) != 0)
1168 		fail++;
1169 	sshbuf_free(b);
1170 	if (hostkey != NULL) {
1171 		/*
1172 		 * Ensure this is actually one of our hostkeys; unfortunately
1173 		 * can't check ssh->kex->initial_hostkey directly at this point
1174 		 * as packet state has not yet been exported to monitor.
1175 		 */
1176 		if (get_hostkey_index(hostkey, 1, ssh) == -1)
1177 			fatal_f("hostbound hostkey does not match");
1178 		sshkey_free(hostkey);
1179 	}
1180 	return (fail == 0);
1181 }
1182 
1183 static int
monitor_valid_hostbasedblob(const u_char * data,u_int datalen,const char * cuser,const char * chost)1184 monitor_valid_hostbasedblob(const u_char *data, u_int datalen,
1185     const char *cuser, const char *chost)
1186 {
1187 	struct sshbuf *b;
1188 	const u_char *p;
1189 	char *cp, *userstyle;
1190 	size_t len;
1191 	int r, fail = 0;
1192 	u_char type;
1193 
1194 	if ((b = sshbuf_from(data, datalen)) == NULL)
1195 		fatal_f("sshbuf_new");
1196 	if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
1197 		fatal_fr(r, "parse sessionid");
1198 
1199 	if ((session_id2 == NULL) ||
1200 	    (len != session_id2_len) ||
1201 	    (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1202 		fail++;
1203 
1204 	if ((r = sshbuf_get_u8(b, &type)) != 0)
1205 		fatal_fr(r, "parse type");
1206 	if (type != SSH2_MSG_USERAUTH_REQUEST)
1207 		fail++;
1208 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1209 		fatal_fr(r, "parse userstyle");
1210 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
1211 	    authctxt->style ? ":" : "",
1212 	    authctxt->style ? authctxt->style : "");
1213 	if (strcmp(userstyle, cp) != 0) {
1214 		logit("wrong user name passed to monitor: "
1215 		    "expected %s != %.100s", userstyle, cp);
1216 		fail++;
1217 	}
1218 	free(userstyle);
1219 	free(cp);
1220 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* service */
1221 	    (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1222 		fatal_fr(r, "parse method");
1223 	if (strcmp(cp, "hostbased") != 0)
1224 		fail++;
1225 	free(cp);
1226 	if ((r = sshbuf_skip_string(b)) != 0 ||	/* pkalg */
1227 	    (r = sshbuf_skip_string(b)) != 0)	/* pkblob */
1228 		fatal_fr(r, "parse pk");
1229 
1230 	/* verify client host, strip trailing dot if necessary */
1231 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1232 		fatal_fr(r, "parse host");
1233 	if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
1234 		cp[len - 1] = '\0';
1235 	if (strcmp(cp, chost) != 0)
1236 		fail++;
1237 	free(cp);
1238 
1239 	/* verify client user */
1240 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
1241 		fatal_fr(r, "parse ruser");
1242 	if (strcmp(cp, cuser) != 0)
1243 		fail++;
1244 	free(cp);
1245 
1246 	if (sshbuf_len(b) != 0)
1247 		fail++;
1248 	sshbuf_free(b);
1249 	return (fail == 0);
1250 }
1251 
1252 int
mm_answer_keyverify(struct ssh * ssh,int sock,struct sshbuf * m)1253 mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
1254 {
1255 	struct sshkey *key;
1256 	const u_char *signature, *data, *blob;
1257 	char *sigalg = NULL, *fp = NULL;
1258 	size_t signaturelen, datalen, bloblen;
1259 	int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
1260 	int encoded_ret;
1261 	struct sshkey_sig_details *sig_details = NULL;
1262 
1263 	if ((r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
1264 	    (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
1265 	    (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
1266 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
1267 		fatal_fr(r, "parse");
1268 
1269 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1270 	  !monitor_allowed_key(blob, bloblen))
1271 		fatal_f("bad key, not previously allowed");
1272 
1273 	/* Empty signature algorithm means NULL. */
1274 	if (*sigalg == '\0') {
1275 		free(sigalg);
1276 		sigalg = NULL;
1277 	}
1278 
1279 	/* XXX use sshkey_froms here; need to change key_blob, etc. */
1280 	if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1281 		fatal_fr(r, "parse key");
1282 
1283 	switch (key_blobtype) {
1284 	case MM_USERKEY:
1285 		valid_data = monitor_valid_userblob(ssh, data, datalen);
1286 		auth_method = "publickey";
1287 		break;
1288 	case MM_HOSTKEY:
1289 		valid_data = monitor_valid_hostbasedblob(data, datalen,
1290 		    hostbased_cuser, hostbased_chost);
1291 		auth_method = "hostbased";
1292 		break;
1293 	default:
1294 		valid_data = 0;
1295 		break;
1296 	}
1297 	if (!valid_data)
1298 		fatal_f("bad %s signature data blob",
1299 		    key_blobtype == MM_USERKEY ? "userkey" :
1300 		    (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
1301 
1302 	if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
1303 	    SSH_FP_DEFAULT)) == NULL)
1304 		fatal_f("sshkey_fingerprint failed");
1305 
1306 	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1307 	    sigalg, ssh->compat, &sig_details);
1308 	debug3_f("%s %s signature using %s %s%s%s", auth_method,
1309 	    sshkey_type(key), sigalg == NULL ? "default" : sigalg,
1310 	    (ret == 0) ? "verified" : "unverified",
1311 	    (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
1312 
1313 	if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
1314 		req_presence = (options.pubkey_auth_options &
1315 		    PUBKEYAUTH_TOUCH_REQUIRED) ||
1316 		    !key_opts->no_require_user_presence;
1317 		if (req_presence &&
1318 		    (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
1319 			error("public key %s %s signature for %s%s from %.128s "
1320 			    "port %d rejected: user presence "
1321 			    "(authenticator touch) requirement not met ",
1322 			    sshkey_type(key), fp,
1323 			    authctxt->valid ? "" : "invalid user ",
1324 			    authctxt->user, ssh_remote_ipaddr(ssh),
1325 			    ssh_remote_port(ssh));
1326 			ret = SSH_ERR_SIGNATURE_INVALID;
1327 		}
1328 		req_verify = (options.pubkey_auth_options &
1329 		    PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
1330 		if (req_verify &&
1331 		    (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
1332 			error("public key %s %s signature for %s%s from %.128s "
1333 			    "port %d rejected: user verification requirement "
1334 			    "not met ", sshkey_type(key), fp,
1335 			    authctxt->valid ? "" : "invalid user ",
1336 			    authctxt->user, ssh_remote_ipaddr(ssh),
1337 			    ssh_remote_port(ssh));
1338 			ret = SSH_ERR_SIGNATURE_INVALID;
1339 		}
1340 	}
1341 	auth2_record_key(authctxt, ret == 0, key);
1342 
1343 	if (key_blobtype == MM_USERKEY && ret == 0)
1344 		auth_activate_options(ssh, key_opts);
1345 	monitor_reset_key_state();
1346 
1347 	sshbuf_reset(m);
1348 
1349 	/* encode ret != 0 as positive integer, since we're sending u32 */
1350 	encoded_ret = (ret != 0);
1351 	if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
1352 	    (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
1353 		fatal_fr(r, "assemble");
1354 	if (sig_details != NULL) {
1355 		if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
1356 		    (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
1357 			fatal_fr(r, "assemble sk");
1358 	}
1359 	sshkey_sig_details_free(sig_details);
1360 	mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1361 
1362 	free(sigalg);
1363 	free(fp);
1364 	sshkey_free(key);
1365 
1366 	return ret == 0;
1367 }
1368 
1369 static void
mm_record_login(struct ssh * ssh,Session * s,struct passwd * pw)1370 mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
1371 {
1372 	socklen_t fromlen;
1373 	struct sockaddr_storage from;
1374 
1375 	/*
1376 	 * Get IP address of client. If the connection is not a socket, let
1377 	 * the address be 0.0.0.0.
1378 	 */
1379 	memset(&from, 0, sizeof(from));
1380 	fromlen = sizeof(from);
1381 	if (ssh_packet_connection_is_on_socket(ssh)) {
1382 		if (getpeername(ssh_packet_get_connection_in(ssh),
1383 		    (struct sockaddr *)&from, &fromlen) == -1) {
1384 			debug("getpeername: %.100s", strerror(errno));
1385 			cleanup_exit(255);
1386 		}
1387 	}
1388 	/* Record that there was a login on that tty from the remote host. */
1389 	record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1390 	    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1391 	    (struct sockaddr *)&from, fromlen);
1392 }
1393 
1394 static void
mm_session_close(Session * s)1395 mm_session_close(Session *s)
1396 {
1397 	debug3_f("session %d pid %ld", s->self, (long)s->pid);
1398 	if (s->ttyfd != -1) {
1399 		debug3_f("tty %s ptyfd %d", s->tty, s->ptyfd);
1400 		session_pty_cleanup2(s);
1401 	}
1402 	session_unused(s->self);
1403 }
1404 
1405 int
mm_answer_pty(struct ssh * ssh,int sock,struct sshbuf * m)1406 mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
1407 {
1408 	extern struct monitor *pmonitor;
1409 	Session *s;
1410 	int r, res, fd0;
1411 
1412 	debug3_f("entering");
1413 
1414 	sshbuf_reset(m);
1415 	s = session_new();
1416 	if (s == NULL)
1417 		goto error;
1418 	s->authctxt = authctxt;
1419 	s->pw = authctxt->pw;
1420 	s->pid = pmonitor->m_pid;
1421 	res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1422 	if (res == 0)
1423 		goto error;
1424 	pty_setowner(authctxt->pw, s->tty);
1425 
1426 	if ((r = sshbuf_put_u32(m, 1)) != 0 ||
1427 	    (r = sshbuf_put_cstring(m, s->tty)) != 0)
1428 		fatal_fr(r, "assemble");
1429 
1430 	/* We need to trick ttyslot */
1431 	if (dup2(s->ttyfd, 0) == -1)
1432 		fatal_f("dup2");
1433 
1434 	mm_record_login(ssh, s, authctxt->pw);
1435 
1436 	/* Now we can close the file descriptor again */
1437 	close(0);
1438 
1439 	/* send messages generated by record_login */
1440 	if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
1441 		fatal_fr(r, "assemble loginmsg");
1442 	sshbuf_reset(loginmsg);
1443 
1444 	mm_request_send(sock, MONITOR_ANS_PTY, m);
1445 
1446 	if (mm_send_fd(sock, s->ptyfd) == -1 ||
1447 	    mm_send_fd(sock, s->ttyfd) == -1)
1448 		fatal_f("send fds failed");
1449 
1450 	/* make sure nothing uses fd 0 */
1451 	if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
1452 		fatal_f("open(/dev/null): %s", strerror(errno));
1453 	if (fd0 != 0)
1454 		error_f("fd0 %d != 0", fd0);
1455 
1456 	/* slave side of pty is not needed */
1457 	close(s->ttyfd);
1458 	s->ttyfd = s->ptyfd;
1459 	/* no need to dup() because nobody closes ptyfd */
1460 	s->ptymaster = s->ptyfd;
1461 
1462 	debug3_f("tty %s ptyfd %d", s->tty, s->ttyfd);
1463 
1464 	return (0);
1465 
1466  error:
1467 	if (s != NULL)
1468 		mm_session_close(s);
1469 	if ((r = sshbuf_put_u32(m, 0)) != 0)
1470 		fatal_fr(r, "assemble 0");
1471 	mm_request_send(sock, MONITOR_ANS_PTY, m);
1472 	return (0);
1473 }
1474 
1475 int
mm_answer_pty_cleanup(struct ssh * ssh,int sock,struct sshbuf * m)1476 mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
1477 {
1478 	Session *s;
1479 	char *tty;
1480 	int r;
1481 
1482 	debug3_f("entering");
1483 
1484 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
1485 		fatal_fr(r, "parse tty");
1486 	if ((s = session_by_tty(tty)) != NULL)
1487 		mm_session_close(s);
1488 	sshbuf_reset(m);
1489 	free(tty);
1490 	return (0);
1491 }
1492 
1493 int
mm_answer_term(struct ssh * ssh,int sock,struct sshbuf * req)1494 mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
1495 {
1496 	extern struct monitor *pmonitor;
1497 	int res, status;
1498 
1499 	debug3_f("tearing down sessions");
1500 
1501 	/* The child is terminating */
1502 	session_destroy_all(ssh, &mm_session_close);
1503 
1504 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1505 		if (errno != EINTR)
1506 			exit(1);
1507 
1508 	res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1509 
1510 	/* Terminate process */
1511 	exit(res);
1512 }
1513 
1514 void
monitor_clear_keystate(struct ssh * ssh,struct monitor * pmonitor)1515 monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
1516 {
1517 	ssh_clear_newkeys(ssh, MODE_IN);
1518 	ssh_clear_newkeys(ssh, MODE_OUT);
1519 	sshbuf_free(child_state);
1520 	child_state = NULL;
1521 }
1522 
1523 void
monitor_apply_keystate(struct ssh * ssh,struct monitor * pmonitor)1524 monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
1525 {
1526 	struct kex *kex;
1527 	int r;
1528 
1529 	debug3_f("packet_set_state");
1530 	if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1531 		fatal_fr(r, "packet_set_state");
1532 	sshbuf_free(child_state);
1533 	child_state = NULL;
1534 	if ((kex = ssh->kex) == NULL)
1535 		fatal_f("internal error: ssh->kex == NULL");
1536 	if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
1537 		fatal_f("incorrect session id length %zu (expected %u)",
1538 		    sshbuf_len(ssh->kex->session_id), session_id2_len);
1539 	}
1540 	if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
1541 	    session_id2_len) != 0)
1542 		fatal_f("session ID mismatch");
1543 	/* XXX set callbacks */
1544 #ifdef WITH_OPENSSL
1545 	kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
1546 	kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
1547 	kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
1548 	kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
1549 	kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
1550 	kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1551 	kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1552 	kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
1553 #endif
1554 	kex->kex[KEX_C25519_SHA256] = kex_gen_server;
1555 	kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
1556 	kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server;
1557 	kex->load_host_public_key=&get_hostkey_public_by_type;
1558 	kex->load_host_private_key=&get_hostkey_private_by_type;
1559 	kex->host_key_index=&get_hostkey_index;
1560 	kex->sign = sshd_hostkey_sign;
1561 }
1562 
1563 /* This function requires careful sanity checking */
1564 
1565 void
mm_get_keystate(struct ssh * ssh,struct monitor * pmonitor)1566 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
1567 {
1568 	debug3_f("Waiting for new keys");
1569 
1570 	if ((child_state = sshbuf_new()) == NULL)
1571 		fatal_f("sshbuf_new failed");
1572 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1573 	    child_state);
1574 	debug3_f("GOT new keys");
1575 }
1576 
1577 
1578 /* XXX */
1579 
1580 #define FD_CLOSEONEXEC(x) do { \
1581 	if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1582 		fatal("fcntl(%d, F_SETFD)", x); \
1583 } while (0)
1584 
1585 static void
monitor_openfds(struct monitor * mon,int do_logfds)1586 monitor_openfds(struct monitor *mon, int do_logfds)
1587 {
1588 	int pair[2];
1589 #ifdef SO_ZEROIZE
1590 	int on = 1;
1591 #endif
1592 
1593 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1594 		fatal_f("socketpair: %s", strerror(errno));
1595 #ifdef SO_ZEROIZE
1596 	if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1597 		error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1598 	if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
1599 		error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1600 #endif
1601 	FD_CLOSEONEXEC(pair[0]);
1602 	FD_CLOSEONEXEC(pair[1]);
1603 	mon->m_recvfd = pair[0];
1604 	mon->m_sendfd = pair[1];
1605 
1606 	if (do_logfds) {
1607 		if (pipe(pair) == -1)
1608 			fatal_f("pipe: %s", strerror(errno));
1609 		FD_CLOSEONEXEC(pair[0]);
1610 		FD_CLOSEONEXEC(pair[1]);
1611 		mon->m_log_recvfd = pair[0];
1612 		mon->m_log_sendfd = pair[1];
1613 	} else
1614 		mon->m_log_recvfd = mon->m_log_sendfd = -1;
1615 }
1616 
1617 struct monitor *
monitor_init(void)1618 monitor_init(void)
1619 {
1620 	struct monitor *mon;
1621 
1622 	mon = xcalloc(1, sizeof(*mon));
1623 	monitor_openfds(mon, 1);
1624 
1625 	return mon;
1626 }
1627 
1628 void
monitor_reinit(struct monitor * mon)1629 monitor_reinit(struct monitor *mon)
1630 {
1631 	monitor_openfds(mon, 0);
1632 }
1633 
1634 #ifdef GSSAPI
1635 int
mm_answer_gss_setup_ctx(struct ssh * ssh,int sock,struct sshbuf * m)1636 mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1637 {
1638 	gss_OID_desc goid;
1639 	OM_uint32 major;
1640 	size_t len;
1641 	u_char *p;
1642 	int r;
1643 
1644 	if (!options.gss_authentication)
1645 		fatal_f("GSSAPI authentication not enabled");
1646 
1647 	if ((r = sshbuf_get_string(m, &p, &len)) != 0)
1648 		fatal_fr(r, "parse");
1649 	goid.elements = p;
1650 	goid.length = len;
1651 
1652 	major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1653 
1654 	free(goid.elements);
1655 
1656 	sshbuf_reset(m);
1657 	if ((r = sshbuf_put_u32(m, major)) != 0)
1658 		fatal_fr(r, "assemble");
1659 
1660 	mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1661 
1662 	/* Now we have a context, enable the step */
1663 	monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1664 
1665 	return (0);
1666 }
1667 
1668 int
mm_answer_gss_accept_ctx(struct ssh * ssh,int sock,struct sshbuf * m)1669 mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
1670 {
1671 	gss_buffer_desc in;
1672 	gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1673 	OM_uint32 major, minor;
1674 	OM_uint32 flags = 0; /* GSI needs this */
1675 	int r;
1676 
1677 	if (!options.gss_authentication)
1678 		fatal_f("GSSAPI authentication not enabled");
1679 
1680 	if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
1681 		fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1682 	major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1683 	free(in.value);
1684 
1685 	sshbuf_reset(m);
1686 	if ((r = sshbuf_put_u32(m, major)) != 0 ||
1687 	    (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
1688 	    (r = sshbuf_put_u32(m, flags)) != 0)
1689 		fatal_fr(r, "assemble");
1690 	mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1691 
1692 	gss_release_buffer(&minor, &out);
1693 
1694 	if (major == GSS_S_COMPLETE) {
1695 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1696 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1697 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1698 	}
1699 	return (0);
1700 }
1701 
1702 int
mm_answer_gss_checkmic(struct ssh * ssh,int sock,struct sshbuf * m)1703 mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
1704 {
1705 	gss_buffer_desc gssbuf, mic;
1706 	OM_uint32 ret;
1707 	int r;
1708 
1709 	if (!options.gss_authentication)
1710 		fatal_f("GSSAPI authentication not enabled");
1711 
1712 	if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
1713 	    (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
1714 		fatal_fr(r, "ssh_gssapi_get_buffer_desc");
1715 
1716 	ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1717 
1718 	free(gssbuf.value);
1719 	free(mic.value);
1720 
1721 	sshbuf_reset(m);
1722 	if ((r = sshbuf_put_u32(m, ret)) != 0)
1723 		fatal_fr(r, "assemble");
1724 
1725 	mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1726 
1727 	if (!GSS_ERROR(ret))
1728 		monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1729 
1730 	return (0);
1731 }
1732 
1733 int
mm_answer_gss_userok(struct ssh * ssh,int sock,struct sshbuf * m)1734 mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
1735 {
1736 	int r, authenticated;
1737 	const char *displayname;
1738 
1739 	if (!options.gss_authentication)
1740 		fatal_f("GSSAPI authentication not enabled");
1741 
1742 	authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1743 
1744 	sshbuf_reset(m);
1745 	if ((r = sshbuf_put_u32(m, authenticated)) != 0)
1746 		fatal_fr(r, "assemble");
1747 
1748 	debug3_f("sending result %d", authenticated);
1749 	mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1750 
1751 	auth_method = "gssapi-with-mic";
1752 
1753 	if ((displayname = ssh_gssapi_displayname()) != NULL)
1754 		auth2_record_info(authctxt, "%s", displayname);
1755 
1756 	/* Monitor loop will terminate if authenticated */
1757 	return (authenticated);
1758 }
1759 #endif /* GSSAPI */
1760 
1761