Merge from vendor branch OPENPAM:
[dragonfly.git] / crypto / openssh-4 / monitor_wrap.c
1 /*
2  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3  * Copyright 2002 Markus Friedl <markus@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28 RCSID("$OpenBSD: monitor_wrap.c,v 1.39 2004/07/17 05:31:41 dtucker Exp $");
29
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
32
33 #include "ssh.h"
34 #include "dh.h"
35 #include "kex.h"
36 #include "auth.h"
37 #include "auth-options.h"
38 #include "buffer.h"
39 #include "bufaux.h"
40 #include "packet.h"
41 #include "mac.h"
42 #include "log.h"
43 #ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
44 #undef TARGET_OS_MAC
45 #include "zlib.h"
46 #define TARGET_OS_MAC 1
47 #else
48 #include "zlib.h"
49 #endif
50 #include "monitor.h"
51 #include "monitor_wrap.h"
52 #include "xmalloc.h"
53 #include "atomicio.h"
54 #include "monitor_fdpass.h"
55 #include "getput.h"
56 #include "servconf.h"
57
58 #include "auth.h"
59 #include "channels.h"
60 #include "session.h"
61
62 #ifdef GSSAPI
63 #include "ssh-gss.h"
64 #endif
65
66 /* Imports */
67 extern int compat20;
68 extern Newkeys *newkeys[];
69 extern z_stream incoming_stream;
70 extern z_stream outgoing_stream;
71 extern struct monitor *pmonitor;
72 extern Buffer input, output;
73 extern Buffer loginmsg;
74 extern ServerOptions options;
75 extern Buffer loginmsg;
76
77 int
78 mm_is_monitor(void)
79 {
80         /*
81          * m_pid is only set in the privileged part, and
82          * points to the unprivileged child.
83          */
84         return (pmonitor && pmonitor->m_pid > 0);
85 }
86
87 void
88 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
89 {
90         u_int mlen = buffer_len(m);
91         u_char buf[5];
92
93         debug3("%s entering: type %d", __func__, type);
94
95         PUT_32BIT(buf, mlen + 1);
96         buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
97         if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
98                 fatal("%s: write", __func__);
99         if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
100                 fatal("%s: write", __func__);
101 }
102
103 void
104 mm_request_receive(int sock, Buffer *m)
105 {
106         u_char buf[4];
107         u_int msg_len;
108         ssize_t res;
109
110         debug3("%s entering", __func__);
111
112         res = atomicio(read, sock, buf, sizeof(buf));
113         if (res != sizeof(buf)) {
114                 if (res == 0)
115                         cleanup_exit(255);
116                 fatal("%s: read: %ld", __func__, (long)res);
117         }
118         msg_len = GET_32BIT(buf);
119         if (msg_len > 256 * 1024)
120                 fatal("%s: read: bad msg_len %d", __func__, msg_len);
121         buffer_clear(m);
122         buffer_append_space(m, msg_len);
123         res = atomicio(read, sock, buffer_ptr(m), msg_len);
124         if (res != msg_len)
125                 fatal("%s: read: %ld != msg_len", __func__, (long)res);
126 }
127
128 void
129 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
130 {
131         u_char rtype;
132
133         debug3("%s entering: type %d", __func__, type);
134
135         mm_request_receive(sock, m);
136         rtype = buffer_get_char(m);
137         if (rtype != type)
138                 fatal("%s: read: rtype %d != type %d", __func__,
139                     rtype, type);
140 }
141
142 DH *
143 mm_choose_dh(int min, int nbits, int max)
144 {
145         BIGNUM *p, *g;
146         int success = 0;
147         Buffer m;
148
149         buffer_init(&m);
150         buffer_put_int(&m, min);
151         buffer_put_int(&m, nbits);
152         buffer_put_int(&m, max);
153
154         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
155
156         debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
157         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
158
159         success = buffer_get_char(&m);
160         if (success == 0)
161                 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
162
163         if ((p = BN_new()) == NULL)
164                 fatal("%s: BN_new failed", __func__);
165         if ((g = BN_new()) == NULL)
166                 fatal("%s: BN_new failed", __func__);
167         buffer_get_bignum2(&m, p);
168         buffer_get_bignum2(&m, g);
169
170         debug3("%s: remaining %d", __func__, buffer_len(&m));
171         buffer_free(&m);
172
173         return (dh_new_group(g, p));
174 }
175
176 int
177 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
178 {
179         Kex *kex = *pmonitor->m_pkex;
180         Buffer m;
181
182         debug3("%s entering", __func__);
183
184         buffer_init(&m);
185         buffer_put_int(&m, kex->host_key_index(key));
186         buffer_put_string(&m, data, datalen);
187
188         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
189
190         debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
191         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
192         *sigp  = buffer_get_string(&m, lenp);
193         buffer_free(&m);
194
195         return (0);
196 }
197
198 struct passwd *
199 mm_getpwnamallow(const char *username)
200 {
201         Buffer m;
202         struct passwd *pw;
203         u_int pwlen;
204
205         debug3("%s entering", __func__);
206
207         buffer_init(&m);
208         buffer_put_cstring(&m, username);
209
210         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
211
212         debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
213         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
214
215         if (buffer_get_char(&m) == 0) {
216                 buffer_free(&m);
217                 return (NULL);
218         }
219         pw = buffer_get_string(&m, &pwlen);
220         if (pwlen != sizeof(struct passwd))
221                 fatal("%s: struct passwd size mismatch", __func__);
222         pw->pw_name = buffer_get_string(&m, NULL);
223         pw->pw_passwd = buffer_get_string(&m, NULL);
224         pw->pw_gecos = buffer_get_string(&m, NULL);
225 #ifdef HAVE_PW_CLASS_IN_PASSWD
226         pw->pw_class = buffer_get_string(&m, NULL);
227 #endif
228         pw->pw_dir = buffer_get_string(&m, NULL);
229         pw->pw_shell = buffer_get_string(&m, NULL);
230         buffer_free(&m);
231
232         return (pw);
233 }
234
235 char *
236 mm_auth2_read_banner(void)
237 {
238         Buffer m;
239         char *banner;
240
241         debug3("%s entering", __func__);
242
243         buffer_init(&m);
244         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
245         buffer_clear(&m);
246
247         mm_request_receive_expect(pmonitor->m_recvfd,
248             MONITOR_ANS_AUTH2_READ_BANNER, &m);
249         banner = buffer_get_string(&m, NULL);
250         buffer_free(&m);
251
252         /* treat empty banner as missing banner */
253         if (strlen(banner) == 0) {
254                 xfree(banner);
255                 banner = NULL;
256         }
257         return (banner);
258 }
259
260 /* Inform the privileged process about service and style */
261
262 void
263 mm_inform_authserv(char *service, char *style)
264 {
265         Buffer m;
266
267         debug3("%s entering", __func__);
268
269         buffer_init(&m);
270         buffer_put_cstring(&m, service);
271         buffer_put_cstring(&m, style ? style : "");
272
273         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
274
275         buffer_free(&m);
276 }
277
278 /* Do the password authentication */
279 int
280 mm_auth_password(Authctxt *authctxt, char *password)
281 {
282         Buffer m;
283         int authenticated = 0;
284
285         debug3("%s entering", __func__);
286
287         buffer_init(&m);
288         buffer_put_cstring(&m, password);
289         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
290
291         debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
292         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
293
294         authenticated = buffer_get_int(&m);
295
296         buffer_free(&m);
297
298         debug3("%s: user %sauthenticated",
299             __func__, authenticated ? "" : "not ");
300         return (authenticated);
301 }
302
303 int
304 mm_user_key_allowed(struct passwd *pw, Key *key)
305 {
306         return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
307 }
308
309 int
310 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
311     Key *key)
312 {
313         return (mm_key_allowed(MM_HOSTKEY, user, host, key));
314 }
315
316 int
317 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
318     char *host, Key *key)
319 {
320         int ret;
321
322         key->type = KEY_RSA; /* XXX hack for key_to_blob */
323         ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
324         key->type = KEY_RSA1;
325         return (ret);
326 }
327
328 static void
329 mm_send_debug(Buffer *m)
330 {
331         char *msg;
332
333         while (buffer_len(m)) {
334                 msg = buffer_get_string(m, NULL);
335                 debug3("%s: Sending debug: %s", __func__, msg);
336                 packet_send_debug("%s", msg);
337                 xfree(msg);
338         }
339 }
340
341 int
342 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
343 {
344         Buffer m;
345         u_char *blob;
346         u_int len;
347         int allowed = 0, have_forced = 0;
348
349         debug3("%s entering", __func__);
350
351         /* Convert the key to a blob and the pass it over */
352         if (!key_to_blob(key, &blob, &len))
353                 return (0);
354
355         buffer_init(&m);
356         buffer_put_int(&m, type);
357         buffer_put_cstring(&m, user ? user : "");
358         buffer_put_cstring(&m, host ? host : "");
359         buffer_put_string(&m, blob, len);
360         xfree(blob);
361
362         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
363
364         debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
365         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
366
367         allowed = buffer_get_int(&m);
368
369         /* fake forced command */
370         auth_clear_options();
371         have_forced = buffer_get_int(&m);
372         forced_command = have_forced ? xstrdup("true") : NULL;
373
374         /* Send potential debug messages */
375         mm_send_debug(&m);
376
377         buffer_free(&m);
378
379         return (allowed);
380 }
381
382 /*
383  * This key verify needs to send the key type along, because the
384  * privileged parent makes the decision if the key is allowed
385  * for authentication.
386  */
387
388 int
389 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
390 {
391         Buffer m;
392         u_char *blob;
393         u_int len;
394         int verified = 0;
395
396         debug3("%s entering", __func__);
397
398         /* Convert the key to a blob and the pass it over */
399         if (!key_to_blob(key, &blob, &len))
400                 return (0);
401
402         buffer_init(&m);
403         buffer_put_string(&m, blob, len);
404         buffer_put_string(&m, sig, siglen);
405         buffer_put_string(&m, data, datalen);
406         xfree(blob);
407
408         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
409
410         debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
411         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
412
413         verified = buffer_get_int(&m);
414
415         buffer_free(&m);
416
417         return (verified);
418 }
419
420 /* Export key state after authentication */
421 Newkeys *
422 mm_newkeys_from_blob(u_char *blob, int blen)
423 {
424         Buffer b;
425         u_int len;
426         Newkeys *newkey = NULL;
427         Enc *enc;
428         Mac *mac;
429         Comp *comp;
430
431         debug3("%s: %p(%d)", __func__, blob, blen);
432 #ifdef DEBUG_PK
433         dump_base64(stderr, blob, blen);
434 #endif
435         buffer_init(&b);
436         buffer_append(&b, blob, blen);
437
438         newkey = xmalloc(sizeof(*newkey));
439         enc = &newkey->enc;
440         mac = &newkey->mac;
441         comp = &newkey->comp;
442
443         /* Enc structure */
444         enc->name = buffer_get_string(&b, NULL);
445         buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
446         enc->enabled = buffer_get_int(&b);
447         enc->block_size = buffer_get_int(&b);
448         enc->key = buffer_get_string(&b, &enc->key_len);
449         enc->iv = buffer_get_string(&b, &len);
450         if (len != enc->block_size)
451                 fatal("%s: bad ivlen: expected %u != %u", __func__,
452                     enc->block_size, len);
453
454         if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
455                 fatal("%s: bad cipher name %s or pointer %p", __func__,
456                     enc->name, enc->cipher);
457
458         /* Mac structure */
459         mac->name = buffer_get_string(&b, NULL);
460         if (mac->name == NULL || mac_init(mac, mac->name) == -1)
461                 fatal("%s: can not init mac %s", __func__, mac->name);
462         mac->enabled = buffer_get_int(&b);
463         mac->key = buffer_get_string(&b, &len);
464         if (len > mac->key_len)
465                 fatal("%s: bad mac key length: %u > %d", __func__, len,
466                     mac->key_len);
467         mac->key_len = len;
468
469         /* Comp structure */
470         comp->type = buffer_get_int(&b);
471         comp->enabled = buffer_get_int(&b);
472         comp->name = buffer_get_string(&b, NULL);
473
474         len = buffer_len(&b);
475         if (len != 0)
476                 error("newkeys_from_blob: remaining bytes in blob %u", len);
477         buffer_free(&b);
478         return (newkey);
479 }
480
481 int
482 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
483 {
484         Buffer b;
485         int len;
486         Enc *enc;
487         Mac *mac;
488         Comp *comp;
489         Newkeys *newkey = newkeys[mode];
490
491         debug3("%s: converting %p", __func__, newkey);
492
493         if (newkey == NULL) {
494                 error("%s: newkey == NULL", __func__);
495                 return 0;
496         }
497         enc = &newkey->enc;
498         mac = &newkey->mac;
499         comp = &newkey->comp;
500
501         buffer_init(&b);
502         /* Enc structure */
503         buffer_put_cstring(&b, enc->name);
504         /* The cipher struct is constant and shared, you export pointer */
505         buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
506         buffer_put_int(&b, enc->enabled);
507         buffer_put_int(&b, enc->block_size);
508         buffer_put_string(&b, enc->key, enc->key_len);
509         packet_get_keyiv(mode, enc->iv, enc->block_size);
510         buffer_put_string(&b, enc->iv, enc->block_size);
511
512         /* Mac structure */
513         buffer_put_cstring(&b, mac->name);
514         buffer_put_int(&b, mac->enabled);
515         buffer_put_string(&b, mac->key, mac->key_len);
516
517         /* Comp structure */
518         buffer_put_int(&b, comp->type);
519         buffer_put_int(&b, comp->enabled);
520         buffer_put_cstring(&b, comp->name);
521
522         len = buffer_len(&b);
523         if (lenp != NULL)
524                 *lenp = len;
525         if (blobp != NULL) {
526                 *blobp = xmalloc(len);
527                 memcpy(*blobp, buffer_ptr(&b), len);
528         }
529         memset(buffer_ptr(&b), 0, len);
530         buffer_free(&b);
531         return len;
532 }
533
534 static void
535 mm_send_kex(Buffer *m, Kex *kex)
536 {
537         buffer_put_string(m, kex->session_id, kex->session_id_len);
538         buffer_put_int(m, kex->we_need);
539         buffer_put_int(m, kex->hostkey_type);
540         buffer_put_int(m, kex->kex_type);
541         buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
542         buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
543         buffer_put_int(m, kex->flags);
544         buffer_put_cstring(m, kex->client_version_string);
545         buffer_put_cstring(m, kex->server_version_string);
546 }
547
548 void
549 mm_send_keystate(struct monitor *monitor)
550 {
551         Buffer m;
552         u_char *blob, *p;
553         u_int bloblen, plen;
554         u_int32_t seqnr, packets;
555         u_int64_t blocks;
556
557         buffer_init(&m);
558
559         if (!compat20) {
560                 u_char iv[24];
561                 u_char *key;
562                 u_int ivlen, keylen;
563
564                 buffer_put_int(&m, packet_get_protocol_flags());
565
566                 buffer_put_int(&m, packet_get_ssh1_cipher());
567
568                 debug3("%s: Sending ssh1 KEY+IV", __func__);
569                 keylen = packet_get_encryption_key(NULL);
570                 key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
571                 keylen = packet_get_encryption_key(key);
572                 buffer_put_string(&m, key, keylen);
573                 memset(key, 0, keylen);
574                 xfree(key);
575
576                 ivlen = packet_get_keyiv_len(MODE_OUT);
577                 packet_get_keyiv(MODE_OUT, iv, ivlen);
578                 buffer_put_string(&m, iv, ivlen);
579                 ivlen = packet_get_keyiv_len(MODE_OUT);
580                 packet_get_keyiv(MODE_IN, iv, ivlen);
581                 buffer_put_string(&m, iv, ivlen);
582                 goto skip;
583         } else {
584                 /* Kex for rekeying */
585                 mm_send_kex(&m, *monitor->m_pkex);
586         }
587
588         debug3("%s: Sending new keys: %p %p",
589             __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
590
591         /* Keys from Kex */
592         if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
593                 fatal("%s: conversion of newkeys failed", __func__);
594
595         buffer_put_string(&m, blob, bloblen);
596         xfree(blob);
597
598         if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
599                 fatal("%s: conversion of newkeys failed", __func__);
600
601         buffer_put_string(&m, blob, bloblen);
602         xfree(blob);
603
604         packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
605         buffer_put_int(&m, seqnr);
606         buffer_put_int64(&m, blocks);
607         buffer_put_int(&m, packets);
608         packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
609         buffer_put_int(&m, seqnr);
610         buffer_put_int64(&m, blocks);
611         buffer_put_int(&m, packets);
612
613         debug3("%s: New keys have been sent", __func__);
614  skip:
615         /* More key context */
616         plen = packet_get_keycontext(MODE_OUT, NULL);
617         p = xmalloc(plen+1);
618         packet_get_keycontext(MODE_OUT, p);
619         buffer_put_string(&m, p, plen);
620         xfree(p);
621
622         plen = packet_get_keycontext(MODE_IN, NULL);
623         p = xmalloc(plen+1);
624         packet_get_keycontext(MODE_IN, p);
625         buffer_put_string(&m, p, plen);
626         xfree(p);
627
628         /* Compression state */
629         debug3("%s: Sending compression state", __func__);
630         buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
631         buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
632
633         /* Network I/O buffers */
634         buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
635         buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
636
637         mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
638         debug3("%s: Finished sending state", __func__);
639
640         buffer_free(&m);
641 }
642
643 int
644 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
645 {
646         Buffer m;
647         char *p, *msg;
648         int success = 0;
649
650         buffer_init(&m);
651         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
652
653         debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
654         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
655
656         success = buffer_get_int(&m);
657         if (success == 0) {
658                 debug3("%s: pty alloc failed", __func__);
659                 buffer_free(&m);
660                 return (0);
661         }
662         p = buffer_get_string(&m, NULL);
663         msg = buffer_get_string(&m, NULL);
664         buffer_free(&m);
665
666         strlcpy(namebuf, p, namebuflen); /* Possible truncation */
667         xfree(p);
668
669         buffer_append(&loginmsg, msg, strlen(msg));
670         xfree(msg);
671
672         *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
673         *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
674
675         /* Success */
676         return (1);
677 }
678
679 void
680 mm_session_pty_cleanup2(Session *s)
681 {
682         Buffer m;
683
684         if (s->ttyfd == -1)
685                 return;
686         buffer_init(&m);
687         buffer_put_cstring(&m, s->tty);
688         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
689         buffer_free(&m);
690
691         /* closed dup'ed master */
692         if (close(s->ptymaster) < 0)
693                 error("close(s->ptymaster): %s", strerror(errno));
694
695         /* unlink pty from session */
696         s->ttyfd = -1;
697 }
698
699 #ifdef USE_PAM
700 void
701 mm_start_pam(Authctxt *authctxt)
702 {
703         Buffer m;
704
705         debug3("%s entering", __func__);
706         if (!options.use_pam)
707                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
708
709         buffer_init(&m);
710         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
711
712         buffer_free(&m);
713 }
714
715 u_int
716 mm_do_pam_account(void)
717 {
718         Buffer m;
719         u_int ret;
720         char *msg;
721
722         debug3("%s entering", __func__);
723         if (!options.use_pam)
724                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
725
726         buffer_init(&m);
727         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
728
729         mm_request_receive_expect(pmonitor->m_recvfd,
730             MONITOR_ANS_PAM_ACCOUNT, &m);
731         ret = buffer_get_int(&m);
732         msg = buffer_get_string(&m, NULL);
733         buffer_append(&loginmsg, msg, strlen(msg));
734         xfree(msg);
735
736         buffer_free(&m);
737
738         debug3("%s returning %d", __func__, ret);
739
740         return (ret);
741 }
742
743 void *
744 mm_sshpam_init_ctx(Authctxt *authctxt)
745 {
746         Buffer m;
747         int success;
748
749         debug3("%s", __func__);
750         buffer_init(&m);
751         buffer_put_cstring(&m, authctxt->user);
752         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
753         debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
754         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
755         success = buffer_get_int(&m);
756         if (success == 0) {
757                 debug3("%s: pam_init_ctx failed", __func__);
758                 buffer_free(&m);
759                 return (NULL);
760         }
761         buffer_free(&m);
762         return (authctxt);
763 }
764
765 int
766 mm_sshpam_query(void *ctx, char **name, char **info,
767     u_int *num, char ***prompts, u_int **echo_on)
768 {
769         Buffer m;
770         int i, ret;
771
772         debug3("%s", __func__);
773         buffer_init(&m);
774         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
775         debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
776         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
777         ret = buffer_get_int(&m);
778         debug3("%s: pam_query returned %d", __func__, ret);
779         *name = buffer_get_string(&m, NULL);
780         *info = buffer_get_string(&m, NULL);
781         *num = buffer_get_int(&m);
782         *prompts = xmalloc((*num + 1) * sizeof(char *));
783         *echo_on = xmalloc((*num + 1) * sizeof(u_int));
784         for (i = 0; i < *num; ++i) {
785                 (*prompts)[i] = buffer_get_string(&m, NULL);
786                 (*echo_on)[i] = buffer_get_int(&m);
787         }
788         buffer_free(&m);
789         return (ret);
790 }
791
792 int
793 mm_sshpam_respond(void *ctx, u_int num, char **resp)
794 {
795         Buffer m;
796         int i, ret;
797
798         debug3("%s", __func__);
799         buffer_init(&m);
800         buffer_put_int(&m, num);
801         for (i = 0; i < num; ++i)
802                 buffer_put_cstring(&m, resp[i]);
803         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
804         debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
805         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
806         ret = buffer_get_int(&m);
807         debug3("%s: pam_respond returned %d", __func__, ret);
808         buffer_free(&m);
809         return (ret);
810 }
811
812 void
813 mm_sshpam_free_ctx(void *ctxtp)
814 {
815         Buffer m;
816
817         debug3("%s", __func__);
818         buffer_init(&m);
819         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
820         debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
821         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
822         buffer_free(&m);
823 }
824 #endif /* USE_PAM */
825
826 /* Request process termination */
827
828 void
829 mm_terminate(void)
830 {
831         Buffer m;
832
833         buffer_init(&m);
834         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
835         buffer_free(&m);
836 }
837
838 int
839 mm_ssh1_session_key(BIGNUM *num)
840 {
841         int rsafail;
842         Buffer m;
843
844         buffer_init(&m);
845         buffer_put_bignum2(&m, num);
846         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
847
848         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
849
850         rsafail = buffer_get_int(&m);
851         buffer_get_bignum2(&m, num);
852
853         buffer_free(&m);
854
855         return (rsafail);
856 }
857
858 static void
859 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
860     char ***prompts, u_int **echo_on)
861 {
862         *name = xstrdup("");
863         *infotxt = xstrdup("");
864         *numprompts = 1;
865         *prompts = xmalloc(*numprompts * sizeof(char *));
866         *echo_on = xmalloc(*numprompts * sizeof(u_int));
867         (*echo_on)[0] = 0;
868 }
869
870 int
871 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
872    u_int *numprompts, char ***prompts, u_int **echo_on)
873 {
874         Buffer m;
875         u_int success;
876         char *challenge;
877
878         debug3("%s: entering", __func__);
879
880         buffer_init(&m);
881         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
882
883         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
884             &m);
885         success = buffer_get_int(&m);
886         if (success == 0) {
887                 debug3("%s: no challenge", __func__);
888                 buffer_free(&m);
889                 return (-1);
890         }
891
892         /* Get the challenge, and format the response */
893         challenge  = buffer_get_string(&m, NULL);
894         buffer_free(&m);
895
896         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
897         (*prompts)[0] = challenge;
898
899         debug3("%s: received challenge: %s", __func__, challenge);
900
901         return (0);
902 }
903
904 int
905 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
906 {
907         Buffer m;
908         int authok;
909
910         debug3("%s: entering", __func__);
911         if (numresponses != 1)
912                 return (-1);
913
914         buffer_init(&m);
915         buffer_put_cstring(&m, responses[0]);
916         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
917
918         mm_request_receive_expect(pmonitor->m_recvfd,
919             MONITOR_ANS_BSDAUTHRESPOND, &m);
920
921         authok = buffer_get_int(&m);
922         buffer_free(&m);
923
924         return ((authok == 0) ? -1 : 0);
925 }
926
927 #ifdef SKEY
928 int
929 mm_skey_query(void *ctx, char **name, char **infotxt,
930    u_int *numprompts, char ***prompts, u_int **echo_on)
931 {
932         Buffer m;
933         int len;
934         u_int success;
935         char *p, *challenge;
936
937         debug3("%s: entering", __func__);
938
939         buffer_init(&m);
940         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
941
942         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
943             &m);
944         success = buffer_get_int(&m);
945         if (success == 0) {
946                 debug3("%s: no challenge", __func__);
947                 buffer_free(&m);
948                 return (-1);
949         }
950
951         /* Get the challenge, and format the response */
952         challenge  = buffer_get_string(&m, NULL);
953         buffer_free(&m);
954
955         debug3("%s: received challenge: %s", __func__, challenge);
956
957         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
958
959         len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
960         p = xmalloc(len);
961         strlcpy(p, challenge, len);
962         strlcat(p, SKEY_PROMPT, len);
963         (*prompts)[0] = p;
964         xfree(challenge);
965
966         return (0);
967 }
968
969 int
970 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
971 {
972         Buffer m;
973         int authok;
974
975         debug3("%s: entering", __func__);
976         if (numresponses != 1)
977                 return (-1);
978
979         buffer_init(&m);
980         buffer_put_cstring(&m, responses[0]);
981         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
982
983         mm_request_receive_expect(pmonitor->m_recvfd,
984             MONITOR_ANS_SKEYRESPOND, &m);
985
986         authok = buffer_get_int(&m);
987         buffer_free(&m);
988
989         return ((authok == 0) ? -1 : 0);
990 }
991 #endif /* SKEY */
992
993 void
994 mm_ssh1_session_id(u_char session_id[16])
995 {
996         Buffer m;
997         int i;
998
999         debug3("%s entering", __func__);
1000
1001         buffer_init(&m);
1002         for (i = 0; i < 16; i++)
1003                 buffer_put_char(&m, session_id[i]);
1004
1005         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1006         buffer_free(&m);
1007 }
1008
1009 int
1010 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1011 {
1012         Buffer m;
1013         Key *key;
1014         u_char *blob;
1015         u_int blen;
1016         int allowed = 0, have_forced = 0;
1017
1018         debug3("%s entering", __func__);
1019
1020         buffer_init(&m);
1021         buffer_put_bignum2(&m, client_n);
1022
1023         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1024         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1025
1026         allowed = buffer_get_int(&m);
1027
1028         /* fake forced command */
1029         auth_clear_options();
1030         have_forced = buffer_get_int(&m);
1031         forced_command = have_forced ? xstrdup("true") : NULL;
1032
1033         if (allowed && rkey != NULL) {
1034                 blob = buffer_get_string(&m, &blen);
1035                 if ((key = key_from_blob(blob, blen)) == NULL)
1036                         fatal("%s: key_from_blob failed", __func__);
1037                 *rkey = key;
1038                 xfree(blob);
1039         }
1040         mm_send_debug(&m);
1041         buffer_free(&m);
1042
1043         return (allowed);
1044 }
1045
1046 BIGNUM *
1047 mm_auth_rsa_generate_challenge(Key *key)
1048 {
1049         Buffer m;
1050         BIGNUM *challenge;
1051         u_char *blob;
1052         u_int blen;
1053
1054         debug3("%s entering", __func__);
1055
1056         if ((challenge = BN_new()) == NULL)
1057                 fatal("%s: BN_new failed", __func__);
1058
1059         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1060         if (key_to_blob(key, &blob, &blen) == 0)
1061                 fatal("%s: key_to_blob failed", __func__);
1062         key->type = KEY_RSA1;
1063
1064         buffer_init(&m);
1065         buffer_put_string(&m, blob, blen);
1066         xfree(blob);
1067
1068         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1069         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1070
1071         buffer_get_bignum2(&m, challenge);
1072         buffer_free(&m);
1073
1074         return (challenge);
1075 }
1076
1077 int
1078 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1079 {
1080         Buffer m;
1081         u_char *blob;
1082         u_int blen;
1083         int success = 0;
1084
1085         debug3("%s entering", __func__);
1086
1087         key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
1088         if (key_to_blob(key, &blob, &blen) == 0)
1089                 fatal("%s: key_to_blob failed", __func__);
1090         key->type = KEY_RSA1;
1091
1092         buffer_init(&m);
1093         buffer_put_string(&m, blob, blen);
1094         buffer_put_string(&m, response, 16);
1095         xfree(blob);
1096
1097         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1098         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1099
1100         success = buffer_get_int(&m);
1101         buffer_free(&m);
1102
1103         return (success);
1104 }
1105
1106 #ifdef SSH_AUDIT_EVENTS
1107 void
1108 mm_audit_event(ssh_audit_event_t event)
1109 {
1110         Buffer m;
1111
1112         debug3("%s entering", __func__);
1113
1114         buffer_init(&m);
1115         buffer_put_int(&m, event);
1116
1117         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1118         buffer_free(&m);
1119 }
1120
1121 void
1122 mm_audit_run_command(const char *command)
1123 {
1124         Buffer m;
1125
1126         debug3("%s entering command %s", __func__, command);
1127
1128         buffer_init(&m);
1129         buffer_put_cstring(&m, command);
1130
1131         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1132         buffer_free(&m);
1133 }
1134 #endif /* SSH_AUDIT_EVENTS */
1135
1136 #ifdef GSSAPI
1137 OM_uint32
1138 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1139 {
1140         Buffer m;
1141         OM_uint32 major;
1142
1143         /* Client doesn't get to see the context */
1144         *ctx = NULL;
1145
1146         buffer_init(&m);
1147         buffer_put_string(&m, goid->elements, goid->length);
1148
1149         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1150         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1151
1152         major = buffer_get_int(&m);
1153
1154         buffer_free(&m);
1155         return (major);
1156 }
1157
1158 OM_uint32
1159 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1160     gss_buffer_desc *out, OM_uint32 *flags)
1161 {
1162         Buffer m;
1163         OM_uint32 major;
1164         u_int len;
1165
1166         buffer_init(&m);
1167         buffer_put_string(&m, in->value, in->length);
1168
1169         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1170         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1171
1172         major = buffer_get_int(&m);
1173         out->value = buffer_get_string(&m, &len);
1174         out->length = len;
1175         if (flags)
1176                 *flags = buffer_get_int(&m);
1177
1178         buffer_free(&m);
1179
1180         return (major);
1181 }
1182
1183 OM_uint32
1184 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1185 {
1186         Buffer m;
1187         OM_uint32 major;
1188
1189         buffer_init(&m);
1190         buffer_put_string(&m, gssbuf->value, gssbuf->length);
1191         buffer_put_string(&m, gssmic->value, gssmic->length);
1192
1193         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1194         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1195             &m);
1196
1197         major = buffer_get_int(&m);
1198         buffer_free(&m);
1199         return(major);
1200 }
1201
1202 int
1203 mm_ssh_gssapi_userok(char *user)
1204 {
1205         Buffer m;
1206         int authenticated = 0;
1207
1208         buffer_init(&m);
1209
1210         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1211         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1212                                   &m);
1213
1214         authenticated = buffer_get_int(&m);
1215
1216         buffer_free(&m);
1217         debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1218         return (authenticated);
1219 }
1220 #endif /* GSSAPI */