blob: c5e3fb988edfd186225b76eb56dc2019a35f356c [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
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"
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000028RCSID("$OpenBSD: monitor_wrap.c,v 1.7 2002/05/15 15:47:49 mouring Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
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 "buffer.h"
38#include "bufaux.h"
39#include "packet.h"
40#include "mac.h"
41#include "log.h"
42#include "zlib.h"
43#include "monitor.h"
44#include "monitor_wrap.h"
45#include "xmalloc.h"
46#include "atomicio.h"
47#include "monitor_fdpass.h"
48#include "getput.h"
49
50#include "auth.h"
51#include "channels.h"
52#include "session.h"
53
54/* Imports */
55extern int compat20;
56extern Newkeys *newkeys[];
57extern z_stream incoming_stream;
58extern z_stream outgoing_stream;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000059extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060extern Buffer input, output;
61
62void
63mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
64{
65 u_char buf[5];
66 u_int mlen = buffer_len(m);
67
68 debug3("%s entering: type %d", __FUNCTION__, type);
69
70 PUT_32BIT(buf, mlen + 1);
71 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
72 if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
73 fatal("%s: write", __FUNCTION__);
74 if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen)
75 fatal("%s: write", __FUNCTION__);
76}
77
78void
79mm_request_receive(int socket, Buffer *m)
80{
81 u_char buf[4];
82 ssize_t res;
83 u_int msg_len;
84
85 debug3("%s entering", __FUNCTION__);
86
87 res = atomicio(read, socket, buf, sizeof(buf));
88 if (res != sizeof(buf)) {
89 if (res == 0)
90 fatal_cleanup();
Ben Lindstromc8615472002-03-26 03:20:45 +000091 fatal("%s: read: %ld", __FUNCTION__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092 }
93 msg_len = GET_32BIT(buf);
94 if (msg_len > 256 * 1024)
95 fatal("%s: read: bad msg_len %d", __FUNCTION__, msg_len);
96 buffer_clear(m);
97 buffer_append_space(m, msg_len);
98 res = atomicio(read, socket, buffer_ptr(m), msg_len);
99 if (res != msg_len)
Ben Lindstromc8615472002-03-26 03:20:45 +0000100 fatal("%s: read: %ld != msg_len", __FUNCTION__, (long)res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101}
102
103void
104mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
105{
106 u_char rtype;
107
108 debug3("%s entering: type %d", __FUNCTION__, type);
109
110 mm_request_receive(socket, m);
111 rtype = buffer_get_char(m);
112 if (rtype != type)
113 fatal("%s: read: rtype %d != type %d", __FUNCTION__,
114 rtype, type);
115}
116
117DH *
118mm_choose_dh(int min, int nbits, int max)
119{
120 BIGNUM *p, *g;
121 int success = 0;
122 Buffer m;
123
124 buffer_init(&m);
125 buffer_put_int(&m, min);
126 buffer_put_int(&m, nbits);
127 buffer_put_int(&m, max);
128
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000129 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130
131 debug3("%s: waiting for MONITOR_ANS_MODULI", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000132 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133
134 success = buffer_get_char(&m);
135 if (success == 0)
136 fatal("%s: MONITOR_ANS_MODULI failed", __FUNCTION__);
137
138 if ((p = BN_new()) == NULL)
139 fatal("%s: BN_new failed", __FUNCTION__);
140 if ((g = BN_new()) == NULL)
141 fatal("%s: BN_new failed", __FUNCTION__);
142 buffer_get_bignum2(&m, p);
143 buffer_get_bignum2(&m, g);
144
145 debug3("%s: remaining %d", __FUNCTION__, buffer_len(&m));
146 buffer_free(&m);
147
148 return (dh_new_group(g, p));
149}
150
151int
152mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
153{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000154 Kex *kex = *pmonitor->m_pkex;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000155 Buffer m;
156
157 debug3("%s entering", __FUNCTION__);
158
159 buffer_init(&m);
160 buffer_put_int(&m, kex->host_key_index(key));
161 buffer_put_string(&m, data, datalen);
162
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000163 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164
165 debug3("%s: waiting for MONITOR_ANS_SIGN", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000166 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167 *sigp = buffer_get_string(&m, lenp);
168 buffer_free(&m);
169
170 return (0);
171}
172
173struct passwd *
174mm_getpwnamallow(const char *login)
175{
176 Buffer m;
177 struct passwd *pw;
178 u_int pwlen;
179
180 debug3("%s entering", __FUNCTION__);
181
182 buffer_init(&m);
183 buffer_put_cstring(&m, login);
184
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000185 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186
187 debug3("%s: waiting for MONITOR_ANS_PWNAM", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000188 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189
190 if (buffer_get_char(&m) == 0) {
191 buffer_free(&m);
192 return (NULL);
193 }
194 pw = buffer_get_string(&m, &pwlen);
195 if (pwlen != sizeof(struct passwd))
196 fatal("%s: struct passwd size mismatch", __FUNCTION__);
197 pw->pw_name = buffer_get_string(&m, NULL);
198 pw->pw_passwd = buffer_get_string(&m, NULL);
199 pw->pw_gecos = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000200#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201 pw->pw_class = buffer_get_string(&m, NULL);
Kevin Steves7e147602002-03-22 18:07:17 +0000202#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 pw->pw_dir = buffer_get_string(&m, NULL);
204 pw->pw_shell = buffer_get_string(&m, NULL);
205 buffer_free(&m);
206
207 return (pw);
208}
209
Damien Miller5ad9fd92002-05-13 11:07:41 +1000210char* mm_auth2_read_banner(void)
211{
212 Buffer m;
213 char *banner;
214
215 debug3("%s entering", __FUNCTION__);
216
217 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000218 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000219 buffer_clear(&m);
220
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000221 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000222 banner = buffer_get_string(&m, NULL);
223 buffer_free(&m);
224
225 return (banner);
226}
227
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228/* Inform the privileged process about service and style */
229
230void
231mm_inform_authserv(char *service, char *style)
232{
233 Buffer m;
234
235 debug3("%s entering", __FUNCTION__);
236
237 buffer_init(&m);
238 buffer_put_cstring(&m, service);
239 buffer_put_cstring(&m, style ? style : "");
240
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000241 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242
243 buffer_free(&m);
244}
245
246/* Do the password authentication */
247int
248mm_auth_password(Authctxt *authctxt, char *password)
249{
250 Buffer m;
251 int authenticated = 0;
252
253 debug3("%s entering", __FUNCTION__);
254
255 buffer_init(&m);
256 buffer_put_cstring(&m, password);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000257 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258
259 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000260 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261
262 authenticated = buffer_get_int(&m);
263
264 buffer_free(&m);
265
266 debug3("%s: user %sauthenticated",
267 __FUNCTION__, authenticated ? "" : "not ");
268 return (authenticated);
269}
270
271int
272mm_user_key_allowed(struct passwd *pw, Key *key)
273{
274 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
275}
276
277int
278mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
279 Key *key)
280{
281 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
282}
283
284int
285mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
286 char *host, Key *key)
287{
288 int ret;
289
290 key->type = KEY_RSA; /* XXX hack for key_to_blob */
291 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
292 key->type = KEY_RSA1;
293 return (ret);
294}
295
296static void
297mm_send_debug(Buffer *m)
298{
299 char *msg;
300
301 while (buffer_len(m)) {
302 msg = buffer_get_string(m, NULL);
303 debug3("%s: Sending debug: %s", __FUNCTION__, msg);
304 packet_send_debug("%s", msg);
305 xfree(msg);
306 }
307}
308
309int
310mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
311{
312 Buffer m;
313 u_char *blob;
314 u_int len;
315 int allowed = 0;
316
317 debug3("%s entering", __FUNCTION__);
318
319 /* Convert the key to a blob and the pass it over */
320 if (!key_to_blob(key, &blob, &len))
321 return (0);
322
323 buffer_init(&m);
324 buffer_put_int(&m, type);
325 buffer_put_cstring(&m, user ? user : "");
326 buffer_put_cstring(&m, host ? host : "");
327 buffer_put_string(&m, blob, len);
328 xfree(blob);
329
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000330 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000331
332 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000333 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334
335 allowed = buffer_get_int(&m);
336
337 /* Send potential debug messages */
338 mm_send_debug(&m);
339
340 buffer_free(&m);
341
342 return (allowed);
343}
344
345/*
346 * This key verify needs to send the key type along, because the
347 * privileged parent makes the decision if the key is allowed
348 * for authentication.
349 */
350
351int
352mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
353{
354 Buffer m;
355 u_char *blob;
356 u_int len;
357 int verified = 0;
358
359 debug3("%s entering", __FUNCTION__);
360
361 /* Convert the key to a blob and the pass it over */
362 if (!key_to_blob(key, &blob, &len))
363 return (0);
364
365 buffer_init(&m);
366 buffer_put_string(&m, blob, len);
367 buffer_put_string(&m, sig, siglen);
368 buffer_put_string(&m, data, datalen);
369 xfree(blob);
370
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000374 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375
376 verified = buffer_get_int(&m);
377
378 buffer_free(&m);
379
380 return (verified);
381}
382
383/* Export key state after authentication */
384Newkeys *
385mm_newkeys_from_blob(u_char *blob, int blen)
386{
387 Buffer b;
388 u_int len;
389 Newkeys *newkey = NULL;
390 Enc *enc;
391 Mac *mac;
392 Comp *comp;
393
394 debug3("%s: %p(%d)", __FUNCTION__, blob, blen);
395#ifdef DEBUG_PK
396 dump_base64(stderr, blob, blen);
397#endif
398 buffer_init(&b);
399 buffer_append(&b, blob, blen);
400
401 newkey = xmalloc(sizeof(*newkey));
402 enc = &newkey->enc;
403 mac = &newkey->mac;
404 comp = &newkey->comp;
405
406 /* Enc structure */
407 enc->name = buffer_get_string(&b, NULL);
408 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
409 enc->enabled = buffer_get_int(&b);
410 enc->block_size = buffer_get_int(&b);
411 enc->key = buffer_get_string(&b, &enc->key_len);
412 enc->iv = buffer_get_string(&b, &len);
413 if (len != enc->block_size)
414 fatal("%s: bad ivlen: expected %d != %d", __FUNCTION__,
415 enc->block_size, len);
416
417 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
418 fatal("%s: bad cipher name %s or pointer %p", __FUNCTION__,
419 enc->name, enc->cipher);
420
421 /* Mac structure */
422 mac->name = buffer_get_string(&b, NULL);
423 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
424 fatal("%s: can not init mac %s", __FUNCTION__, mac->name);
425 mac->enabled = buffer_get_int(&b);
426 mac->key = buffer_get_string(&b, &len);
427 if (len > mac->key_len)
428 fatal("%s: bad mac key lenght: %d > %d", __FUNCTION__, len,
429 mac->key_len);
430 mac->key_len = len;
431
432 /* Comp structure */
433 comp->type = buffer_get_int(&b);
434 comp->enabled = buffer_get_int(&b);
435 comp->name = buffer_get_string(&b, NULL);
436
437 len = buffer_len(&b);
438 if (len != 0)
439 error("newkeys_from_blob: remaining bytes in blob %d", len);
440 buffer_free(&b);
441 return (newkey);
442}
443
444int
445mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
446{
447 Buffer b;
448 int len;
449 u_char *buf;
450 Enc *enc;
451 Mac *mac;
452 Comp *comp;
453 Newkeys *newkey = newkeys[mode];
454
455 debug3("%s: converting %p", __FUNCTION__, newkey);
456
457 if (newkey == NULL) {
458 error("%s: newkey == NULL", __FUNCTION__);
459 return 0;
460 }
461 enc = &newkey->enc;
462 mac = &newkey->mac;
463 comp = &newkey->comp;
464
465 buffer_init(&b);
466 /* Enc structure */
467 buffer_put_cstring(&b, enc->name);
468 /* The cipher struct is constant and shared, you export pointer */
469 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
470 buffer_put_int(&b, enc->enabled);
471 buffer_put_int(&b, enc->block_size);
472 buffer_put_string(&b, enc->key, enc->key_len);
473 packet_get_keyiv(mode, enc->iv, enc->block_size);
474 buffer_put_string(&b, enc->iv, enc->block_size);
475
476 /* Mac structure */
477 buffer_put_cstring(&b, mac->name);
478 buffer_put_int(&b, mac->enabled);
479 buffer_put_string(&b, mac->key, mac->key_len);
480
481 /* Comp structure */
482 buffer_put_int(&b, comp->type);
483 buffer_put_int(&b, comp->enabled);
484 buffer_put_cstring(&b, comp->name);
485
486 len = buffer_len(&b);
487 buf = xmalloc(len);
488 memcpy(buf, buffer_ptr(&b), len);
489 memset(buffer_ptr(&b), 0, len);
490 buffer_free(&b);
491 if (lenp != NULL)
492 *lenp = len;
493 if (blobp != NULL)
494 *blobp = buf;
495 return len;
496}
497
498static void
499mm_send_kex(Buffer *m, Kex *kex)
500{
501 buffer_put_string(m, kex->session_id, kex->session_id_len);
502 buffer_put_int(m, kex->we_need);
503 buffer_put_int(m, kex->hostkey_type);
504 buffer_put_int(m, kex->kex_type);
505 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
506 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
507 buffer_put_int(m, kex->flags);
508 buffer_put_cstring(m, kex->client_version_string);
509 buffer_put_cstring(m, kex->server_version_string);
510}
511
512void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000513mm_send_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514{
515 Buffer m;
516 u_char *blob, *p;
517 u_int bloblen, plen;
518
519 buffer_init(&m);
520
521 if (!compat20) {
522 u_char iv[24];
523 int ivlen;
524
525 buffer_put_int(&m, packet_get_protocol_flags());
526
527 buffer_put_int(&m, packet_get_ssh1_cipher());
528
529 debug3("%s: Sending ssh1 IV", __FUNCTION__);
530 ivlen = packet_get_keyiv_len(MODE_OUT);
531 packet_get_keyiv(MODE_OUT, iv, ivlen);
532 buffer_put_string(&m, iv, ivlen);
533 ivlen = packet_get_keyiv_len(MODE_OUT);
534 packet_get_keyiv(MODE_IN, iv, ivlen);
535 buffer_put_string(&m, iv, ivlen);
536 goto skip;
537 } else {
538 /* Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000539 mm_send_kex(&m, *pmonitor->m_pkex);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540 }
541
542 debug3("%s: Sending new keys: %p %p",
543 __FUNCTION__, newkeys[MODE_OUT], newkeys[MODE_IN]);
544
545 /* Keys from Kex */
546 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
547 fatal("%s: conversion of newkeys failed", __FUNCTION__);
548
549 buffer_put_string(&m, blob, bloblen);
550 xfree(blob);
551
552 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
553 fatal("%s: conversion of newkeys failed", __FUNCTION__);
554
555 buffer_put_string(&m, blob, bloblen);
556 xfree(blob);
557
558 buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
559 buffer_put_int(&m, packet_get_seqnr(MODE_IN));
560
561 debug3("%s: New keys have been sent", __FUNCTION__);
562 skip:
563 /* More key context */
564 plen = packet_get_keycontext(MODE_OUT, NULL);
565 p = xmalloc(plen+1);
566 packet_get_keycontext(MODE_OUT, p);
567 buffer_put_string(&m, p, plen);
568 xfree(p);
569
570 plen = packet_get_keycontext(MODE_IN, NULL);
571 p = xmalloc(plen+1);
572 packet_get_keycontext(MODE_IN, p);
573 buffer_put_string(&m, p, plen);
574 xfree(p);
575
576 /* Compression state */
577 debug3("%s: Sending compression state", __FUNCTION__);
578 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
579 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
580
581 /* Network I/O buffers */
582 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
583 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
584
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000585 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 debug3("%s: Finished sending state", __FUNCTION__);
587
588 buffer_free(&m);
589}
590
591int
592mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
593{
594 Buffer m;
595 u_char *p;
596 int success = 0;
597
598 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000599 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600
601 debug3("%s: waiting for MONITOR_ANS_PTY", __FUNCTION__);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000602 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603
604 success = buffer_get_int(&m);
605 if (success == 0) {
606 debug3("%s: pty alloc failed", __FUNCTION__);
607 buffer_free(&m);
608 return (0);
609 }
610 p = buffer_get_string(&m, NULL);
611 buffer_free(&m);
612
613 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
614 xfree(p);
615
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000616 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
617 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
619 /* Success */
620 return (1);
621}
622
623void
624mm_session_pty_cleanup2(void *session)
625{
626 Session *s = session;
627 Buffer m;
628
629 if (s->ttyfd == -1)
630 return;
631 buffer_init(&m);
632 buffer_put_cstring(&m, s->tty);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000633 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 buffer_free(&m);
635
636 /* closed dup'ed master */
637 if (close(s->ptymaster) < 0)
638 error("close(s->ptymaster): %s", strerror(errno));
639
640 /* unlink pty from session */
641 s->ttyfd = -1;
642}
643
Damien Miller79418552002-04-23 20:28:48 +1000644#ifdef USE_PAM
645void
646mm_start_pam(char *user)
647{
648 Buffer m;
649
650 debug3("%s entering", __FUNCTION__);
651
652 buffer_init(&m);
653 buffer_put_cstring(&m, user);
654
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000655 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
Damien Miller79418552002-04-23 20:28:48 +1000656
657 buffer_free(&m);
658}
659#endif /* USE_PAM */
660
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661/* Request process termination */
662
663void
664mm_terminate(void)
665{
666 Buffer m;
667
668 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000669 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670 buffer_free(&m);
671}
672
673int
674mm_ssh1_session_key(BIGNUM *num)
675{
676 int rsafail;
677 Buffer m;
678
679 buffer_init(&m);
680 buffer_put_bignum2(&m, num);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000681 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000683 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685 rsafail = buffer_get_int(&m);
686 buffer_get_bignum2(&m, num);
687
688 buffer_free(&m);
689
690 return (rsafail);
691}
692
693static void
694mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
695 char ***prompts, u_int **echo_on)
696{
697 *name = xstrdup("");
698 *infotxt = xstrdup("");
699 *numprompts = 1;
700 *prompts = xmalloc(*numprompts * sizeof(char*));
701 *echo_on = xmalloc(*numprompts * sizeof(u_int));
702 (*echo_on)[0] = 0;
703}
704
705int
706mm_bsdauth_query(void *ctx, char **name, char **infotxt,
707 u_int *numprompts, char ***prompts, u_int **echo_on)
708{
709 Buffer m;
710 int res;
711 char *challenge;
712
713 debug3("%s: entering", __FUNCTION__);
714
715 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000716 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000718 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719 &m);
720 res = buffer_get_int(&m);
721 if (res == -1) {
722 debug3("%s: no challenge", __FUNCTION__);
723 buffer_free(&m);
724 return (-1);
725 }
726
727 /* Get the challenge, and format the response */
728 challenge = buffer_get_string(&m, NULL);
729 buffer_free(&m);
730
731 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
732 (*prompts)[0] = challenge;
733
734 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
735
736 return (0);
737}
738
739int
740mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
741{
742 Buffer m;
743 int authok;
744
745 debug3("%s: entering", __FUNCTION__);
746 if (numresponses != 1)
747 return (-1);
748
749 buffer_init(&m);
750 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000751 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000753 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754 MONITOR_ANS_BSDAUTHRESPOND, &m);
755
756 authok = buffer_get_int(&m);
757 buffer_free(&m);
758
759 return ((authok == 0) ? -1 : 0);
760}
761
762int
763mm_skey_query(void *ctx, char **name, char **infotxt,
764 u_int *numprompts, char ***prompts, u_int **echo_on)
765{
766 Buffer m;
767 int len, res;
768 char *p, *challenge;
769
770 debug3("%s: entering", __FUNCTION__);
771
772 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000773 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000775 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776 &m);
777 res = buffer_get_int(&m);
778 if (res == -1) {
779 debug3("%s: no challenge", __FUNCTION__);
780 buffer_free(&m);
781 return (-1);
782 }
783
784 /* Get the challenge, and format the response */
785 challenge = buffer_get_string(&m, NULL);
786 buffer_free(&m);
787
788 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
789
790 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
791
792 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
793 p = xmalloc(len);
794 strlcpy(p, challenge, len);
795 strlcat(p, SKEY_PROMPT, len);
796 (*prompts)[0] = p;
797 xfree(challenge);
798
799 return (0);
800}
801
802int
803mm_skey_respond(void *ctx, u_int numresponses, char **responses)
804{
805 Buffer m;
806 int authok;
807
808 debug3("%s: entering", __FUNCTION__);
809 if (numresponses != 1)
810 return (-1);
811
812 buffer_init(&m);
813 buffer_put_cstring(&m, responses[0]);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000814 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000816 mm_request_receive_expect(pmonitor->m_recvfd,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817 MONITOR_ANS_SKEYRESPOND, &m);
818
819 authok = buffer_get_int(&m);
820 buffer_free(&m);
821
822 return ((authok == 0) ? -1 : 0);
823}
824
825void
826mm_ssh1_session_id(u_char session_id[16])
827{
828 Buffer m;
829 int i;
830
831 debug3("%s entering", __FUNCTION__);
832
833 buffer_init(&m);
834 for (i = 0; i < 16; i++)
835 buffer_put_char(&m, session_id[i]);
836
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000837 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838 buffer_free(&m);
839}
840
841int
842mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
843{
844 Buffer m;
845 Key *key;
846 u_char *blob;
847 u_int blen;
848 int allowed = 0;
849
850 debug3("%s entering", __FUNCTION__);
851
852 buffer_init(&m);
853 buffer_put_bignum2(&m, client_n);
854
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000855 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
856 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857
858 allowed = buffer_get_int(&m);
859
860 if (allowed && rkey != NULL) {
861 blob = buffer_get_string(&m, &blen);
862 if ((key = key_from_blob(blob, blen)) == NULL)
863 fatal("%s: key_from_blob failed", __FUNCTION__);
864 *rkey = key;
865 xfree(blob);
866 }
867 mm_send_debug(&m);
868 buffer_free(&m);
869
870 return (allowed);
871}
872
873BIGNUM *
874mm_auth_rsa_generate_challenge(Key *key)
875{
876 Buffer m;
877 BIGNUM *challenge;
878 u_char *blob;
879 u_int blen;
880
881 debug3("%s entering", __FUNCTION__);
882
883 if ((challenge = BN_new()) == NULL)
884 fatal("%s: BN_new failed", __FUNCTION__);
885
886 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
887 if (key_to_blob(key, &blob, &blen) == 0)
888 fatal("%s: key_to_blob failed", __FUNCTION__);
889 key->type = KEY_RSA1;
890
891 buffer_init(&m);
892 buffer_put_string(&m, blob, blen);
893 xfree(blob);
894
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000895 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
896 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
898 buffer_get_bignum2(&m, challenge);
899 buffer_free(&m);
900
901 return (challenge);
902}
903
904int
905mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
906{
907 Buffer m;
908 u_char *blob;
909 u_int blen;
910 int success = 0;
911
912 debug3("%s entering", __FUNCTION__);
913
914 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
915 if (key_to_blob(key, &blob, &blen) == 0)
916 fatal("%s: key_to_blob failed", __FUNCTION__);
917 key->type = KEY_RSA1;
918
919 buffer_init(&m);
920 buffer_put_string(&m, blob, blen);
921 buffer_put_string(&m, response, 16);
922 xfree(blob);
923
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000924 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
925 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 success = buffer_get_int(&m);
928 buffer_free(&m);
929
930 return (success);
931}