Import OpenSSH-5.6p1.
[dragonfly.git] / crypto / openssh / monitor_wrap.c
1 /* $OpenBSD: monitor_wrap.c,v 1.69 2010/03/07 11:57:13 dtucker 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/uio.h>
32
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include <openssl/bn.h>
42 #include <openssl/dh.h>
43 #include <openssl/evp.h>
44
45 #include "openbsd-compat/sys-queue.h"
46 #include "xmalloc.h"
47 #include "ssh.h"
48 #include "dh.h"
49 #include "buffer.h"
50 #include "key.h"
51 #include "cipher.h"
52 #include "kex.h"
53 #include "hostfile.h"
54 #include "auth.h"
55 #include "auth-options.h"
56 #include "packet.h"
57 #include "mac.h"
58 #include "log.h"
59 #ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
60 #undef TARGET_OS_MAC
61 #include "zlib.h"
62 #define TARGET_OS_MAC 1
63 #else
64 #include "zlib.h"
65 #endif
66 #include "monitor.h"
67 #ifdef GSSAPI
68 #include "ssh-gss.h"
69 #endif
70 #include "monitor_wrap.h"
71 #include "atomicio.h"
72 #include "monitor_fdpass.h"
73 #include "misc.h"
74 #include "schnorr.h"
75 #include "jpake.h"
76
77 #include "channels.h"
78 #include "session.h"
79 #include "servconf.h"
80 #include "roaming.h"
81
82 /* Imports */
83 extern int compat20;
84 extern z_stream incoming_stream;
85 extern z_stream outgoing_stream;
86 extern struct monitor *pmonitor;
87 extern Buffer loginmsg;
88 extern ServerOptions options;
89
90 int
91 mm_is_monitor(void)
92 {
93         /*
94          * m_pid is only set in the privileged part, and
95          * points to the unprivileged child.
96          */
97         return (pmonitor && pmonitor->m_pid > 0);
98 }
99
100 void
101 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
102 {
103         u_int mlen = buffer_len(m);
104         u_char buf[5];
105
106         debug3("%s entering: type %d", __func__, type);
107
108         put_u32(buf, mlen + 1);
109         buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
110         if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
111                 fatal("%s: write: %s", __func__, strerror(errno));
112         if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
113                 fatal("%s: write: %s", __func__, strerror(errno));
114 }
115
116 void
117 mm_request_receive(int sock, Buffer *m)
118 {
119         u_char buf[4];
120         u_int msg_len;
121
122         debug3("%s entering", __func__);
123
124         if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
125                 if (errno == EPIPE)
126                         cleanup_exit(255);
127                 fatal("%s: read: %s", __func__, strerror(errno));
128         }
129         msg_len = get_u32(buf);
130         if (msg_len > 256 * 1024)
131                 fatal("%s: read: bad msg_len %d", __func__, msg_len);
132         buffer_clear(m);
133         buffer_append_space(m, msg_len);
134         if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
135                 fatal("%s: read: %s", __func__, strerror(errno));
136 }
137
138 void
139 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
140 {
141         u_char rtype;
142
143         debug3("%s entering: type %d", __func__, type);
144
145         mm_request_receive(sock, m);
146         rtype = buffer_get_char(m);
147         if (rtype != type)
148                 fatal("%s: read: rtype %d != type %d", __func__,
149                     rtype, type);
150 }
151
152 DH *
153 mm_choose_dh(int min, int nbits, int max)
154 {
155         BIGNUM *p, *g;
156         int success = 0;
157         Buffer m;
158
159         buffer_init(&m);
160         buffer_put_int(&m, min);
161         buffer_put_int(&m, nbits);
162         buffer_put_int(&m, max);
163
164         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
165
166         debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
167         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
168
169         success = buffer_get_char(&m);
170         if (success == 0)
171                 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
172
173         if ((p = BN_new()) == NULL)
174                 fatal("%s: BN_new failed", __func__);
175         if ((g = BN_new()) == NULL)
176                 fatal("%s: BN_new failed", __func__);
177         buffer_get_bignum2(&m, p);
178         buffer_get_bignum2(&m, g);
179
180         debug3("%s: remaining %d", __func__, buffer_len(&m));
181         buffer_free(&m);
182
183         return (dh_new_group(g, p));
184 }
185
186 int
187 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
188 {
189         Kex *kex = *pmonitor->m_pkex;
190         Buffer m;
191
192         debug3("%s entering", __func__);
193
194         buffer_init(&m);
195         buffer_put_int(&m, kex->host_key_index(key));
196         buffer_put_string(&m, data, datalen);
197
198         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
199
200         debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
201         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
202         *sigp  = buffer_get_string(&m, lenp);
203         buffer_free(&m);
204
205         return (0);
206 }
207
208 struct passwd *
209 mm_getpwnamallow(const char *username)
210 {
211         Buffer m;
212         struct passwd *pw;
213         u_int len;
214         ServerOptions *newopts;
215
216         debug3("%s entering", __func__);
217
218         buffer_init(&m);
219         buffer_put_cstring(&m, username);
220
221         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
222
223         debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
224         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
225
226         if (buffer_get_char(&m) == 0) {
227                 pw = NULL;
228                 goto out;
229         }
230         pw = buffer_get_string(&m, &len);
231         if (len != sizeof(struct passwd))
232                 fatal("%s: struct passwd size mismatch", __func__);
233         pw->pw_name = buffer_get_string(&m, NULL);
234         pw->pw_passwd = buffer_get_string(&m, NULL);
235         pw->pw_gecos = buffer_get_string(&m, NULL);
236 #ifdef HAVE_PW_CLASS_IN_PASSWD
237         pw->pw_class = buffer_get_string(&m, NULL);
238 #endif
239         pw->pw_dir = buffer_get_string(&m, NULL);
240         pw->pw_shell = buffer_get_string(&m, NULL);
241
242 out:
243         /* copy options block as a Match directive may have changed some */
244         newopts = buffer_get_string(&m, &len);
245         if (len != sizeof(*newopts))
246                 fatal("%s: option block size mismatch", __func__);
247         if (newopts->banner != NULL)
248                 newopts->banner = buffer_get_string(&m, NULL);
249         copy_set_server_options(&options, newopts, 1);
250         xfree(newopts);
251
252         buffer_free(&m);
253
254         return (pw);
255 }
256
257 char *
258 mm_auth2_read_banner(void)
259 {
260         Buffer m;
261         char *banner;
262
263         debug3("%s entering", __func__);
264
265         buffer_init(&m);
266         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
267         buffer_clear(&m);
268
269         mm_request_receive_expect(pmonitor->m_recvfd,
270             MONITOR_ANS_AUTH2_READ_BANNER, &m);
271         banner = buffer_get_string(&m, NULL);
272         buffer_free(&m);
273
274         /* treat empty banner as missing banner */
275         if (strlen(banner) == 0) {
276                 xfree(banner);
277                 banner = NULL;
278         }
279         return (banner);
280 }
281
282 /* Inform the privileged process about service and style */
283
284 void
285 mm_inform_authserv(char *service, char *style)
286 {
287         Buffer m;
288
289         debug3("%s entering", __func__);
290
291         buffer_init(&m);
292         buffer_put_cstring(&m, service);
293         buffer_put_cstring(&m, style ? style : "");
294
295         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
296
297         buffer_free(&m);
298 }
299
300 /* Do the password authentication */
301 int
302 mm_auth_password(Authctxt *authctxt, char *password)
303 {
304         Buffer m;
305         int authenticated = 0;
306
307         debug3("%s entering", __func__);
308
309         buffer_init(&m);
310         buffer_put_cstring(&m, password);
311         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
312
313         debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
314         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
315
316         authenticated = buffer_get_int(&m);
317
318         buffer_free(&m);
319
320         debug3("%s: user %sauthenticated",
321             __func__, authenticated ? "" : "not ");
322         return (authenticated);
323 }
324
325 int
326 mm_user_key_allowed(struct passwd *pw, Key *key)
327 {
328         return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
329 }
330
331 int
332 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
333     Key *key)
334 {
335         return (mm_key_allowed(MM_HOSTKEY, user, host, key));
336 }
337
338 int
339 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
340     char *host, Key *key)
341 {
342         int ret;
343
344         key->type = KEY_RSA; /* XXX hack for key_to_blob */
345         ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
346         key->type = KEY_RSA1;
347         return (ret);
348 }
349
350 int
351 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
352 {
353         Buffer m;
354         u_char *blob;
355         u_int len;
356         int allowed = 0, have_forced = 0;
357
358         debug3("%s entering", __func__);
359
360         /* Convert the key to a blob and the pass it over */
361         if (!key_to_blob(key, &blob, &len))
362                 return (0);
363
364         buffer_init(&m);
365         buffer_put_int(&m, type);
366         buffer_put_cstring(&m, user ? user : "");
367         buffer_put_cstring(&m, host ? host : "");
368         buffer_put_string(&m, blob, len);
369         xfree(blob);
370
371         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
372
373         debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
374         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
375
376         allowed = buffer_get_int(&m);
377
378         /* fake forced command */
379         auth_clear_options();
380         have_forced = buffer_get_int(&m);
381         forced_command = have_forced ? xstrdup("true") : NULL;
382
383         buffer_free(&m);
384
385         return (allowed);
386 }
387
388 /*
389  * This key verify needs to send the key type along, because the
390  * privileged parent makes the decision if the key is allowed
391  * for authentication.
392  */
393
394 int
395 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
396 {
397         Buffer m;
398         u_char *blob;
399         u_int len;
400         int verified = 0;
401
402         debug3("%s entering", __func__);
403
404         /* Convert the key to a blob and the pass it over */
405         if (!key_to_blob(key, &blob, &len))
406                 return (0);
407
408         buffer_init(&m);
409         buffer_put_string(&m, blob, len);
410         buffer_put_string(&m, sig, siglen);
411         buffer_put_string(&m, data, datalen);
412         xfree(blob);
413
414         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
415
416         debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
417         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
418
419         verified = buffer_get_int(&m);
420
421         buffer_free(&m);
422
423         return (verified);
424 }
425
426 /* Export key state after authentication */
427 Newkeys *
428 mm_newkeys_from_blob(u_char *blob, int blen)
429 {
430         Buffer b;
431         u_int len;
432         Newkeys *newkey = NULL;
433         Enc *enc;
434         Mac *mac;
435         Comp *comp;
436
437         debug3("%s: %p(%d)", __func__, blob, blen);
438 #ifdef DEBUG_PK
439         dump_base64(stderr, blob, blen);
440 #endif
441         buffer_init(&b);
442         buffer_append(&b, blob, blen);
443
444         newkey = xmalloc(sizeof(*newkey));
445         enc = &newkey->enc;
446         mac = &newkey->mac;
447         comp = &newkey->comp;
448
449         /* Enc structure */
450         enc->name = buffer_get_string(&b, NULL);
451         buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
452         enc->enabled = buffer_get_int(&b);
453         enc->block_size = buffer_get_int(&b);
454         enc->key = buffer_get_string(&b, &enc->key_len);
455         enc->iv = buffer_get_string(&b, &len);
456         if (len != enc->block_size)
457                 fatal("%s: bad ivlen: expected %u != %u", __func__,
458                     enc->block_size, len);
459
460         if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
461                 fatal("%s: bad cipher name %s or pointer %p", __func__,
462                     enc->name, enc->cipher);
463
464         /* Mac structure */
465         mac->name = buffer_get_string(&b, NULL);
466         if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
467                 fatal("%s: can not setup mac %s", __func__, mac->name);
468         mac->enabled = buffer_get_int(&b);
469         mac->key = buffer_get_string(&b, &len);
470         if (len > mac->key_len)
471                 fatal("%s: bad mac key length: %u > %d", __func__, len,
472                     mac->key_len);
473         mac->key_len = len;
474
475         /* Comp structure */
476         comp->type = buffer_get_int(&b);
477         comp->enabled = buffer_get_int(&b);
478         comp->name = buffer_get_string(&b, NULL);
479
480         len = buffer_len(&b);
481         if (len != 0)
482                 error("newkeys_from_blob: remaining bytes in blob %u", len);
483         buffer_free(&b);
484         return (newkey);
485 }
486
487 int
488 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
489 {
490         Buffer b;
491         int len;
492         Enc *enc;
493         Mac *mac;
494         Comp *comp;
495         Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
496
497         debug3("%s: converting %p", __func__, newkey);
498
499         if (newkey == NULL) {
500                 error("%s: newkey == NULL", __func__);
501                 return 0;
502         }
503         enc = &newkey->enc;
504         mac = &newkey->mac;
505         comp = &newkey->comp;
506
507         buffer_init(&b);
508         /* Enc structure */
509         buffer_put_cstring(&b, enc->name);
510         /* The cipher struct is constant and shared, you export pointer */
511         buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
512         buffer_put_int(&b, enc->enabled);
513         buffer_put_int(&b, enc->block_size);
514         buffer_put_string(&b, enc->key, enc->key_len);
515         packet_get_keyiv(mode, enc->iv, enc->block_size);
516         buffer_put_string(&b, enc->iv, enc->block_size);
517
518         /* Mac structure */
519         buffer_put_cstring(&b, mac->name);
520         buffer_put_int(&b, mac->enabled);
521         buffer_put_string(&b, mac->key, mac->key_len);
522
523         /* Comp structure */
524         buffer_put_int(&b, comp->type);
525         buffer_put_int(&b, comp->enabled);
526         buffer_put_cstring(&b, comp->name);
527
528         len = buffer_len(&b);
529         if (lenp != NULL)
530                 *lenp = len;
531         if (blobp != NULL) {
532                 *blobp = xmalloc(len);
533                 memcpy(*blobp, buffer_ptr(&b), len);
534         }
535         memset(buffer_ptr(&b), 0, len);
536         buffer_free(&b);
537         return len;
538 }
539
540 static void
541 mm_send_kex(Buffer *m, Kex *kex)
542 {
543         buffer_put_string(m, kex->session_id, kex->session_id_len);
544         buffer_put_int(m, kex->we_need);
545         buffer_put_int(m, kex->hostkey_type);
546         buffer_put_int(m, kex->kex_type);
547         buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
548         buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
549         buffer_put_int(m, kex->flags);
550         buffer_put_cstring(m, kex->client_version_string);
551         buffer_put_cstring(m, kex->server_version_string);
552 }
553
554 void
555 mm_send_keystate(struct monitor *monitor)
556 {
557         Buffer m, *input, *output;
558         u_char *blob, *p;
559         u_int bloblen, plen;
560         u_int32_t seqnr, packets;
561         u_int64_t blocks, bytes;
562
563         buffer_init(&m);
564
565         if (!compat20) {
566                 u_char iv[24];
567                 u_char *key;
568                 u_int ivlen, keylen;
569
570                 buffer_put_int(&m, packet_get_protocol_flags());
571
572                 buffer_put_int(&m, packet_get_ssh1_cipher());
573
574                 debug3("%s: Sending ssh1 KEY+IV", __func__);
575                 keylen = packet_get_encryption_key(NULL);
576                 key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
577                 keylen = packet_get_encryption_key(key);
578                 buffer_put_string(&m, key, keylen);
579                 memset(key, 0, keylen);
580                 xfree(key);
581
582                 ivlen = packet_get_keyiv_len(MODE_OUT);
583                 packet_get_keyiv(MODE_OUT, iv, ivlen);
584                 buffer_put_string(&m, iv, ivlen);
585                 ivlen = packet_get_keyiv_len(MODE_OUT);
586                 packet_get_keyiv(MODE_IN, iv, ivlen);
587                 buffer_put_string(&m, iv, ivlen);
588                 goto skip;
589         } else {
590                 /* Kex for rekeying */
591                 mm_send_kex(&m, *monitor->m_pkex);
592         }
593
594         debug3("%s: Sending new keys: %p %p",
595             __func__, packet_get_newkeys(MODE_OUT),
596             packet_get_newkeys(MODE_IN));
597
598         /* Keys from Kex */
599         if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
600                 fatal("%s: conversion of newkeys failed", __func__);
601
602         buffer_put_string(&m, blob, bloblen);
603         xfree(blob);
604
605         if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
606                 fatal("%s: conversion of newkeys failed", __func__);
607
608         buffer_put_string(&m, blob, bloblen);
609         xfree(blob);
610
611         packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
612         buffer_put_int(&m, seqnr);
613         buffer_put_int64(&m, blocks);
614         buffer_put_int(&m, packets);
615         buffer_put_int64(&m, bytes);
616         packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
617         buffer_put_int(&m, seqnr);
618         buffer_put_int64(&m, blocks);
619         buffer_put_int(&m, packets);
620         buffer_put_int64(&m, bytes);
621
622         debug3("%s: New keys have been sent", __func__);
623  skip:
624         /* More key context */
625         plen = packet_get_keycontext(MODE_OUT, NULL);
626         p = xmalloc(plen+1);
627         packet_get_keycontext(MODE_OUT, p);
628         buffer_put_string(&m, p, plen);
629         xfree(p);
630
631         plen = packet_get_keycontext(MODE_IN, NULL);
632         p = xmalloc(plen+1);
633         packet_get_keycontext(MODE_IN, p);
634         buffer_put_string(&m, p, plen);
635         xfree(p);
636
637         /* Compression state */
638         debug3("%s: Sending compression state", __func__);
639         buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
640         buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
641
642         /* Network I/O buffers */
643         input = (Buffer *)packet_get_input();
644         output = (Buffer *)packet_get_output();
645         buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
646         buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
647
648         /* Roaming */
649         if (compat20) {
650                 buffer_put_int64(&m, get_sent_bytes());
651                 buffer_put_int64(&m, get_recv_bytes());
652         }
653
654         mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
655         debug3("%s: Finished sending state", __func__);
656
657         buffer_free(&m);
658 }
659
660 int
661 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
662 {
663         Buffer m;
664         char *p, *msg;
665         int success = 0, tmp1 = -1, tmp2 = -1;
666
667         /* Kludge: ensure there are fds free to receive the pty/tty */
668         if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
669             (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
670                 error("%s: cannot allocate fds for pty", __func__);
671                 if (tmp1 > 0)
672                         close(tmp1);
673                 if (tmp2 > 0)
674                         close(tmp2);
675                 return 0;
676         }
677         close(tmp1);
678         close(tmp2);
679
680         buffer_init(&m);
681         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
682
683         debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
684         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
685
686         success = buffer_get_int(&m);
687         if (success == 0) {
688                 debug3("%s: pty alloc failed", __func__);
689                 buffer_free(&m);
690                 return (0);
691         }
692         p = buffer_get_string(&m, NULL);
693         msg = buffer_get_string(&m, NULL);
694         buffer_free(&m);
695
696         strlcpy(namebuf, p, namebuflen); /* Possible truncation */
697         xfree(p);
698
699         buffer_append(&loginmsg, msg, strlen(msg));
700         xfree(msg);
701
702         if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
703             (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
704                 fatal("%s: receive fds failed", __func__);
705
706         /* Success */
707         return (1);
708 }
709
710 void
711 mm_session_pty_cleanup2(Session *s)
712 {
713         Buffer m;
714
715         if (s->ttyfd == -1)
716                 return;
717         buffer_init(&m);
718         buffer_put_cstring(&m, s->tty);
719         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
720         buffer_free(&m);
721
722         /* closed dup'ed master */
723         if (s->ptymaster != -1 && close(s->ptymaster) < 0)
724                 error("close(s->ptymaster/%d): %s",
725                     s->ptymaster, strerror(errno));
726
727         /* unlink pty from session */
728         s->ttyfd = -1;
729 }
730
731 #ifdef USE_PAM
732 void
733 mm_start_pam(Authctxt *authctxt)
734 {
735         Buffer m;
736
737         debug3("%s entering", __func__);
738         if (!options.use_pam)
739                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
740
741         buffer_init(&m);
742         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
743
744         buffer_free(&m);
745 }
746
747 u_int
748 mm_do_pam_account(void)
749 {
750         Buffer m;
751         u_int ret;
752         char *msg;
753
754         debug3("%s entering", __func__);
755         if (!options.use_pam)
756                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
757
758         buffer_init(&m);
759         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
760
761         mm_request_receive_expect(pmonitor->m_recvfd,
762             MONITOR_ANS_PAM_ACCOUNT, &m);
763         ret = buffer_get_int(&m);
764         msg = buffer_get_string(&m, NULL);
765         buffer_append(&loginmsg, msg, strlen(msg));
766         xfree(msg);
767
768         buffer_free(&m);
769
770         debug3("%s returning %d", __func__, ret);
771
772         return (ret);
773 }
774
775 void *
776 mm_sshpam_init_ctx(Authctxt *authctxt)
777 {
778         Buffer m;
779         int success;
780
781         debug3("%s", __func__);
782         buffer_init(&m);
783         buffer_put_cstring(&m, authctxt->user);
784         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
785         debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
786         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
787         success = buffer_get_int(&m);
788         if (success == 0) {
789                 debug3("%s: pam_init_ctx failed", __func__);
790                 buffer_free(&m);
791                 return (NULL);
792         }
793         buffer_free(&m);
794         return (authctxt);
795 }
796
797 int
798 mm_sshpam_query(void *ctx, char **name, char **info,
799     u_int *num, char ***prompts, u_int **echo_on)
800 {
801         Buffer m;
802         u_int i;
803         int ret;
804
805         debug3("%s", __func__);
806         buffer_init(&m);
807         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
808         debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
809         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
810         ret = buffer_get_int(&m);
811         debug3("%s: pam_query returned %d", __func__, ret);
812         *name = buffer_get_string(&m, NULL);
813         *info = buffer_get_string(&m, NULL);
814         *num = buffer_get_int(&m);
815         if (*num > PAM_MAX_NUM_MSG)
816                 fatal("%s: recieved %u PAM messages, expected <= %u",
817                     __func__, *num, PAM_MAX_NUM_MSG);
818         *prompts = xcalloc((*num + 1), sizeof(char *));
819         *echo_on = xcalloc((*num + 1), sizeof(u_int));
820         for (i = 0; i < *num; ++i) {
821                 (*prompts)[i] = buffer_get_string(&m, NULL);
822                 (*echo_on)[i] = buffer_get_int(&m);
823         }
824         buffer_free(&m);
825         return (ret);
826 }
827
828 int
829 mm_sshpam_respond(void *ctx, u_int num, char **resp)
830 {
831         Buffer m;
832         u_int i;
833         int ret;
834
835         debug3("%s", __func__);
836         buffer_init(&m);
837         buffer_put_int(&m, num);
838         for (i = 0; i < num; ++i)
839                 buffer_put_cstring(&m, resp[i]);
840         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
841         debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
842         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
843         ret = buffer_get_int(&m);
844         debug3("%s: pam_respond returned %d", __func__, ret);
845         buffer_free(&m);
846         return (ret);
847 }
848
849 void
850 mm_sshpam_free_ctx(void *ctxtp)
851 {
852         Buffer m;
853
854         debug3("%s", __func__);
855         buffer_init(&m);
856         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
857         debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
858         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
859         buffer_free(&m);
860 }
861 #endif /* USE_PAM */
862
863 /* Request process termination */
864
865 void
866 mm_terminate(void)
867 {
868         Buffer m;
869
870         buffer_init(&m);
871         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
872         buffer_free(&m);
873 }
874
875 int
876 mm_ssh1_session_key(BIGNUM *num)
877 {
878         int rsafail;
879         Buffer m;
880
881         buffer_init(&m);
882         buffer_put_bignum2(&m, num);
883         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
884
885         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
886
887         rsafail = buffer_get_int(&m);
888         buffer_get_bignum2(&m, num);
889
890         buffer_free(&m);
891
892         return (rsafail);
893 }
894
895 static void
896 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
897     char ***prompts, u_int **echo_on)
898 {
899         *name = xstrdup("");
900         *infotxt = xstrdup("");
901         *numprompts = 1;
902         *prompts = xcalloc(*numprompts, sizeof(char *));
903         *echo_on = xcalloc(*numprompts, sizeof(u_int));
904         (*echo_on)[0] = 0;
905 }
906
907 int
908 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
909    u_int *numprompts, char ***prompts, u_int **echo_on)
910 {
911         Buffer m;
912         u_int success;
913         char *challenge;
914
915         debug3("%s: entering", __func__);
916
917         buffer_init(&m);
918         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
919
920         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
921             &m);
922         success = buffer_get_int(&m);
923         if (success == 0) {
924                 debug3("%s: no challenge", __func__);
925                 buffer_free(&m);
926                 return (-1);
927         }
928
929         /* Get the challenge, and format the response */
930         challenge  = buffer_get_string(&m, NULL);
931         buffer_free(&m);
932
933         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
934         (*prompts)[0] = challenge;
935
936         debug3("%s: received challenge: %s", __func__, challenge);
937
938         return (0);
939 }
940
941 int
942 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
943 {
944         Buffer m;
945         int authok;
946
947         debug3("%s: entering", __func__);
948         if (numresponses != 1)
949                 return (-1);
950
951         buffer_init(&m);
952         buffer_put_cstring(&m, responses[0]);
953         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
954
955         mm_request_receive_expect(pmonitor->m_recvfd,
956             MONITOR_ANS_BSDAUTHRESPOND, &m);
957
958         authok = buffer_get_int(&m);
959         buffer_free(&m);
960
961         return ((authok == 0) ? -1 : 0);
962 }
963
964 #ifdef SKEY
965 int
966 mm_skey_query(void *ctx, char **name, char **infotxt,
967    u_int *numprompts, char ***prompts, u_int **echo_on)
968 {
969         Buffer m;
970         u_int success;
971         char *challenge;
972
973         debug3("%s: entering", __func__);
974
975         buffer_init(&m);
976         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
977
978         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
979             &m);
980         success = buffer_get_int(&m);
981         if (success == 0) {
982                 debug3("%s: no challenge", __func__);
983                 buffer_free(&m);
984                 return (-1);
985         }
986
987         /* Get the challenge, and format the response */
988         challenge  = buffer_get_string(&m, NULL);
989         buffer_free(&m);
990
991         debug3("%s: received challenge: %s", __func__, challenge);
992
993         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
994
995         xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
996         xfree(challenge);
997
998         return (0);
999 }
1000
1001 int
1002 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1003 {
1004         Buffer m;
1005         int authok;
1006
1007         debug3("%s: entering", __func__);
1008         if (numresponses != 1)
1009                 return (-1);
1010
1011         buffer_init(&m);
1012         buffer_put_cstring(&m, responses[0]);
1013         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1014
1015         mm_request_receive_expect(pmonitor->m_recvfd,
1016             MONITOR_ANS_SKEYRESPOND, &m);
1017
1018         authok = buffer_get_int(&m);
1019         buffer_free(&m);
1020
1021         return ((authok == 0) ? -1 : 0);
1022 }
1023 #endif /* SKEY */
1024
1025 void
1026 mm_ssh1_session_id(u_char session_id[16])
1027 {
1028         Buffer m;
1029         int i;
1030
1031         debug3("%s entering", __func__);
1032
1033         buffer_init(&m);
1034         for (i = 0; i < 16; i++)
1035                 buffer_put_char(&m, session_id[i]);
1036
1037         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1038         buffer_free(&m);
1039 }
1040
1041 int
1042 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1043 {
1044         Buffer m;
1045         Key *key;
1046         u_char *blob;
1047         u_int blen;
1048         int allowed = 0, have_forced = 0;
1049
1050         debug3("%s entering", __func__);
1051
1052         buffer_init(&m);
1053         buffer_put_bignum2(&m, client_n);
1054
1055         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1056         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1057
1058         allowed = buffer_get_int(&m);
1059
1060         /* fake forced command */
1061         auth_clear_options();
1062         have_forced = buffer_get_int(&m);
1063         forced_command = have_forced ? xstrdup("true") : NULL;
1064
1065         if (allowed && rkey != NULL) {
1066                 blob = buffer_get_string(&m, &blen);
1067                 if ((key = key_from_blob(blob, blen)) == NULL)
1068                         fatal("%s: key_from_blob failed", __func__);
1069                 *rkey = key;
1070                 xfree(blob);
1071         }
1072         buffer_free(&m);
1073
1074         return (allowed);
1075 }
1076
1077 BIGNUM *
1078 mm_auth_rsa_generate_challenge(Key *key)
1079 {
1080         Buffer m;
1081         BIGNUM *challenge;
1082         u_char *blob;
1083         u_int blen;
1084
1085         debug3("%s entering", __func__);
1086
1087         if ((challenge = BN_new()) == NULL)
1088                 fatal("%s: BN_new failed", __func__);
1089
1090         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1091         if (key_to_blob(key, &blob, &blen) == 0)
1092                 fatal("%s: key_to_blob failed", __func__);
1093         key->type = KEY_RSA1;
1094
1095         buffer_init(&m);
1096         buffer_put_string(&m, blob, blen);
1097         xfree(blob);
1098
1099         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1100         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1101
1102         buffer_get_bignum2(&m, challenge);
1103         buffer_free(&m);
1104
1105         return (challenge);
1106 }
1107
1108 int
1109 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1110 {
1111         Buffer m;
1112         u_char *blob;
1113         u_int blen;
1114         int success = 0;
1115
1116         debug3("%s entering", __func__);
1117
1118         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1119         if (key_to_blob(key, &blob, &blen) == 0)
1120                 fatal("%s: key_to_blob failed", __func__);
1121         key->type = KEY_RSA1;
1122
1123         buffer_init(&m);
1124         buffer_put_string(&m, blob, blen);
1125         buffer_put_string(&m, response, 16);
1126         xfree(blob);
1127
1128         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1129         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1130
1131         success = buffer_get_int(&m);
1132         buffer_free(&m);
1133
1134         return (success);
1135 }
1136
1137 #ifdef SSH_AUDIT_EVENTS
1138 void
1139 mm_audit_event(ssh_audit_event_t event)
1140 {
1141         Buffer m;
1142
1143         debug3("%s entering", __func__);
1144
1145         buffer_init(&m);
1146         buffer_put_int(&m, event);
1147
1148         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1149         buffer_free(&m);
1150 }
1151
1152 void
1153 mm_audit_run_command(const char *command)
1154 {
1155         Buffer m;
1156
1157         debug3("%s entering command %s", __func__, command);
1158
1159         buffer_init(&m);
1160         buffer_put_cstring(&m, command);
1161
1162         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1163         buffer_free(&m);
1164 }
1165 #endif /* SSH_AUDIT_EVENTS */
1166
1167 #ifdef GSSAPI
1168 OM_uint32
1169 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1170 {
1171         Buffer m;
1172         OM_uint32 major;
1173
1174         /* Client doesn't get to see the context */
1175         *ctx = NULL;
1176
1177         buffer_init(&m);
1178         buffer_put_string(&m, goid->elements, goid->length);
1179
1180         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1181         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1182
1183         major = buffer_get_int(&m);
1184
1185         buffer_free(&m);
1186         return (major);
1187 }
1188
1189 OM_uint32
1190 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1191     gss_buffer_desc *out, OM_uint32 *flags)
1192 {
1193         Buffer m;
1194         OM_uint32 major;
1195         u_int len;
1196
1197         buffer_init(&m);
1198         buffer_put_string(&m, in->value, in->length);
1199
1200         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1201         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1202
1203         major = buffer_get_int(&m);
1204         out->value = buffer_get_string(&m, &len);
1205         out->length = len;
1206         if (flags)
1207                 *flags = buffer_get_int(&m);
1208
1209         buffer_free(&m);
1210
1211         return (major);
1212 }
1213
1214 OM_uint32
1215 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1216 {
1217         Buffer m;
1218         OM_uint32 major;
1219
1220         buffer_init(&m);
1221         buffer_put_string(&m, gssbuf->value, gssbuf->length);
1222         buffer_put_string(&m, gssmic->value, gssmic->length);
1223
1224         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1225         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1226             &m);
1227
1228         major = buffer_get_int(&m);
1229         buffer_free(&m);
1230         return(major);
1231 }
1232
1233 int
1234 mm_ssh_gssapi_userok(char *user)
1235 {
1236         Buffer m;
1237         int authenticated = 0;
1238
1239         buffer_init(&m);
1240
1241         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1242         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1243                                   &m);
1244
1245         authenticated = buffer_get_int(&m);
1246
1247         buffer_free(&m);
1248         debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1249         return (authenticated);
1250 }
1251 #endif /* GSSAPI */
1252
1253 #ifdef JPAKE
1254 void
1255 mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
1256     char **hash_scheme, char **salt)
1257 {
1258         Buffer m;
1259
1260         debug3("%s entering", __func__);
1261
1262         buffer_init(&m);
1263         mm_request_send(pmonitor->m_recvfd,
1264             MONITOR_REQ_JPAKE_GET_PWDATA, &m);
1265
1266         debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
1267         mm_request_receive_expect(pmonitor->m_recvfd,
1268             MONITOR_ANS_JPAKE_GET_PWDATA, &m);
1269
1270         *hash_scheme = buffer_get_string(&m, NULL);
1271         *salt = buffer_get_string(&m, NULL);
1272
1273         buffer_free(&m);
1274 }
1275
1276 void
1277 mm_jpake_step1(struct modp_group *grp,
1278     u_char **id, u_int *id_len,
1279     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
1280     u_char **priv1_proof, u_int *priv1_proof_len,
1281     u_char **priv2_proof, u_int *priv2_proof_len)
1282 {
1283         Buffer m;
1284
1285         debug3("%s entering", __func__);
1286
1287         buffer_init(&m);
1288         mm_request_send(pmonitor->m_recvfd,
1289             MONITOR_REQ_JPAKE_STEP1, &m);
1290
1291         debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
1292         mm_request_receive_expect(pmonitor->m_recvfd,
1293             MONITOR_ANS_JPAKE_STEP1, &m);
1294
1295         if ((*priv1 = BN_new()) == NULL ||
1296             (*priv2 = BN_new()) == NULL ||
1297             (*g_priv1 = BN_new()) == NULL ||
1298             (*g_priv2 = BN_new()) == NULL)
1299                 fatal("%s: BN_new", __func__);
1300
1301         *id = buffer_get_string(&m, id_len);
1302         /* priv1 and priv2 are, well, private */
1303         buffer_get_bignum2(&m, *g_priv1);
1304         buffer_get_bignum2(&m, *g_priv2);
1305         *priv1_proof = buffer_get_string(&m, priv1_proof_len);
1306         *priv2_proof = buffer_get_string(&m, priv2_proof_len);
1307
1308         buffer_free(&m);
1309 }
1310
1311 void
1312 mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1313     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
1314     const u_char *theirid, u_int theirid_len,
1315     const u_char *myid, u_int myid_len,
1316     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
1317     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
1318     BIGNUM **newpub,
1319     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
1320 {
1321         Buffer m;
1322
1323         debug3("%s entering", __func__);
1324
1325         buffer_init(&m);
1326         /* monitor already has all bignums except theirpub1, theirpub2 */
1327         buffer_put_bignum2(&m, theirpub1);
1328         buffer_put_bignum2(&m, theirpub2);
1329         /* monitor already knows our id */
1330         buffer_put_string(&m, theirid, theirid_len);
1331         buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
1332         buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
1333
1334         mm_request_send(pmonitor->m_recvfd,
1335             MONITOR_REQ_JPAKE_STEP2, &m);
1336
1337         debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
1338         mm_request_receive_expect(pmonitor->m_recvfd,
1339             MONITOR_ANS_JPAKE_STEP2, &m);
1340
1341         if ((*newpub = BN_new()) == NULL)
1342                 fatal("%s: BN_new", __func__);
1343
1344         buffer_get_bignum2(&m, *newpub);
1345         *newpub_exponent_proof = buffer_get_string(&m,
1346             newpub_exponent_proof_len);
1347
1348         buffer_free(&m);
1349 }
1350
1351 void
1352 mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1353     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
1354     BIGNUM *theirpub1, BIGNUM *theirpub2,
1355     const u_char *my_id, u_int my_id_len,
1356     const u_char *their_id, u_int their_id_len,
1357     const u_char *sess_id, u_int sess_id_len,
1358     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
1359     BIGNUM **k,
1360     u_char **confirm_hash, u_int *confirm_hash_len)
1361 {
1362         Buffer m;
1363
1364         debug3("%s entering", __func__);
1365
1366         buffer_init(&m);
1367         /* monitor already has all bignums except step2_val */
1368         buffer_put_bignum2(&m, step2_val);
1369         /* monitor already knows all the ids */
1370         buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
1371
1372         mm_request_send(pmonitor->m_recvfd,
1373             MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
1374
1375         debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
1376         mm_request_receive_expect(pmonitor->m_recvfd,
1377             MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
1378
1379         /* 'k' is sensitive and stays in the monitor */
1380         *confirm_hash = buffer_get_string(&m, confirm_hash_len);
1381
1382         buffer_free(&m);
1383 }
1384
1385 int
1386 mm_jpake_check_confirm(const BIGNUM *k,
1387     const u_char *peer_id, u_int peer_id_len,
1388     const u_char *sess_id, u_int sess_id_len,
1389     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
1390 {
1391         Buffer m;
1392         int success = 0;
1393
1394         debug3("%s entering", __func__);
1395
1396         buffer_init(&m);
1397         /* k is dummy in slave, ignored */
1398         /* monitor knows all the ids */
1399         buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
1400         mm_request_send(pmonitor->m_recvfd,
1401             MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
1402
1403         debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
1404         mm_request_receive_expect(pmonitor->m_recvfd,
1405             MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
1406
1407         success = buffer_get_int(&m);
1408         buffer_free(&m);
1409
1410         debug3("%s: success = %d", __func__, success);
1411         return success;
1412 }
1413 #endif /* JPAKE */