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