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