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