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