1 /* $OpenBSD: monitor_wrap.c,v 1.68 2009/06/22 05:39:28 dtucker Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <sys/uio.h>
30 #include <sys/queue.h>
31
32 #include <errno.h>
33 #include <pwd.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <openssl/bn.h>
40 #include <openssl/dh.h>
41 #include <openssl/evp.h>
42
43 #include "xmalloc.h"
44 #include "ssh.h"
45 #include "dh.h"
46 #include "buffer.h"
47 #include "key.h"
48 #include "cipher.h"
49 #include "kex.h"
50 #include "hostfile.h"
51 #include "auth.h"
52 #include "auth-options.h"
53 #include "packet.h"
54 #include "mac.h"
55 #include "log.h"
56 #include <zlib.h>
57 #include "monitor.h"
58 #include "monitor_wrap.h"
59 #include "atomicio.h"
60 #include "monitor_fdpass.h"
61 #include "misc.h"
62
63 #include "channels.h"
64 #include "session.h"
65 #include "servconf.h"
66 #include "roaming.h"
67
68 __RCSID("$MirOS: src/usr.bin/ssh/monitor_wrap.c,v 1.14 2014/03/28 22:31:56 tg Exp $");
69
70 /* Imports */
71 extern int compat20;
72 extern z_stream incoming_stream;
73 extern z_stream outgoing_stream;
74 extern struct monitor *pmonitor;
75 extern Buffer loginmsg;
76 extern ServerOptions options;
77
78 int
mm_is_monitor(void)79 mm_is_monitor(void)
80 {
81 /*
82 * m_pid is only set in the privileged part, and
83 * points to the unprivileged child.
84 */
85 return (pmonitor && pmonitor->m_pid > 0);
86 }
87
88 void
mm_request_send(int sock,enum monitor_reqtype type,Buffer * m)89 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
90 {
91 u_int mlen = buffer_len(m);
92 u_char buf[5];
93
94 debug3("%s entering: type %d", __func__, type);
95
96 put_u32(buf, mlen + 1);
97 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
98 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
99 fatal("%s: write: %s", __func__, strerror(errno));
100 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
101 fatal("%s: write: %s", __func__, strerror(errno));
102 }
103
104 void
mm_request_receive(int sock,Buffer * m)105 mm_request_receive(int sock, Buffer *m)
106 {
107 u_char buf[4];
108 u_int msg_len;
109
110 debug3("%s entering", __func__);
111
112 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
113 if (errno == EPIPE)
114 cleanup_exit(255);
115 fatal("%s: read: %s", __func__, strerror(errno));
116 }
117 msg_len = get_u32(buf);
118 if (msg_len > 256 * 1024)
119 fatal("%s: read: bad msg_len %d", __func__, msg_len);
120 buffer_clear(m);
121 buffer_append_space(m, msg_len);
122 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
123 fatal("%s: read: %s", __func__, strerror(errno));
124 }
125
126 void
mm_request_receive_expect(int sock,enum monitor_reqtype type,Buffer * m)127 mm_request_receive_expect(int sock, 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(sock, 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 *
mm_choose_dh(int min,int nbits,int max)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
mm_key_sign(Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)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 *
mm_getpwnamallow(const char * username)197 mm_getpwnamallow(const char *username)
198 {
199 Buffer m;
200 struct passwd *pw;
201 u_int len;
202 ServerOptions *newopts;
203
204 debug3("%s entering", __func__);
205
206 buffer_init(&m);
207 buffer_put_cstring(&m, username);
208
209 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
210
211 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
212 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
213
214 if (buffer_get_char(&m) == 0) {
215 pw = NULL;
216 goto out;
217 }
218 pw = buffer_get_string(&m, &len);
219 if (len != sizeof(struct passwd))
220 fatal("%s: struct passwd size mismatch", __func__);
221 pw->pw_name = buffer_get_string(&m, NULL);
222 pw->pw_passwd = buffer_get_string(&m, NULL);
223 pw->pw_gecos = buffer_get_string(&m, NULL);
224 pw->pw_class = buffer_get_string(&m, NULL);
225 pw->pw_dir = buffer_get_string(&m, NULL);
226 pw->pw_shell = buffer_get_string(&m, NULL);
227
228 out:
229 /* copy options block as a Match directive may have changed some */
230 newopts = buffer_get_string(&m, &len);
231 if (len != sizeof(*newopts))
232 fatal("%s: option block size mismatch", __func__);
233 if (newopts->banner != NULL)
234 newopts->banner = buffer_get_string(&m, NULL);
235 copy_set_server_options(&options, newopts, 1);
236 xfree(newopts);
237
238 buffer_free(&m);
239
240 return (pw);
241 }
242
243 char *
mm_auth2_read_banner(void)244 mm_auth2_read_banner(void)
245 {
246 Buffer m;
247 char *banner;
248
249 debug3("%s entering", __func__);
250
251 buffer_init(&m);
252 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
253 buffer_clear(&m);
254
255 mm_request_receive_expect(pmonitor->m_recvfd,
256 MONITOR_ANS_AUTH2_READ_BANNER, &m);
257 banner = buffer_get_string(&m, NULL);
258 buffer_free(&m);
259
260 /* treat empty banner as missing banner */
261 if (strlen(banner) == 0) {
262 xfree(banner);
263 banner = NULL;
264 }
265 return (banner);
266 }
267
268 /* Inform the privileged process about service and style */
269
270 void
mm_inform_authserv(char * service,char * style)271 mm_inform_authserv(char *service, char *style)
272 {
273 Buffer m;
274
275 debug3("%s entering", __func__);
276
277 buffer_init(&m);
278 buffer_put_cstring(&m, service);
279 buffer_put_cstring(&m, style ? style : "");
280
281 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
282
283 buffer_free(&m);
284 }
285
286 /* Do the password authentication */
287 int
mm_auth_password(Authctxt * authctxt,char * password)288 mm_auth_password(Authctxt *authctxt, char *password)
289 {
290 Buffer m;
291 int authenticated = 0;
292
293 debug3("%s entering", __func__);
294
295 buffer_init(&m);
296 buffer_put_cstring(&m, password);
297 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
298
299 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
300 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
301
302 authenticated = buffer_get_int(&m);
303
304 buffer_free(&m);
305
306 debug3("%s: user %sauthenticated",
307 __func__, authenticated ? "" : "not ");
308 return (authenticated);
309 }
310
311 int
mm_user_key_allowed(struct passwd * pw,Key * key)312 mm_user_key_allowed(struct passwd *pw, Key *key)
313 {
314 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
315 }
316
317 int
mm_hostbased_key_allowed(struct passwd * pw,char * user,char * host,Key * key)318 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
319 Key *key)
320 {
321 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
322 }
323
324 int
mm_auth_rhosts_rsa_key_allowed(struct passwd * pw,char * user,char * host,Key * key)325 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
326 char *host, Key *key)
327 {
328 int ret;
329
330 key->type = KEY_RSA; /* XXX hack for key_to_blob */
331 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
332 key->type = KEY_RSA1;
333 return (ret);
334 }
335
336 static void
mm_send_debug(Buffer * m)337 mm_send_debug(Buffer *m)
338 {
339 char *msg;
340
341 while (buffer_len(m)) {
342 msg = buffer_get_string(m, NULL);
343 debug3("%s: Sending debug: %s", __func__, msg);
344 packet_send_debug("%s", msg);
345 xfree(msg);
346 }
347 }
348
349 int
mm_key_allowed(enum mm_keytype type,char * user,char * host,Key * key)350 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
351 {
352 Buffer m;
353 u_char *blob;
354 u_int len;
355 int allowed = 0, have_forced = 0;
356
357 debug3("%s entering", __func__);
358
359 /* Convert the key to a blob and the pass it over */
360 if (!key_to_blob(key, &blob, &len))
361 return (0);
362
363 buffer_init(&m);
364 buffer_put_int(&m, type);
365 buffer_put_cstring(&m, user ? user : "");
366 buffer_put_cstring(&m, host ? host : "");
367 buffer_put_string(&m, blob, len);
368 xfree(blob);
369
370 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
371
372 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
373 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
374
375 allowed = buffer_get_int(&m);
376
377 /* fake forced command */
378 auth_clear_options();
379 have_forced = buffer_get_int(&m);
380 forced_command = have_forced ? xstrdup("true") : NULL;
381
382 /* Send potential debug messages */
383 mm_send_debug(&m);
384
385 buffer_free(&m);
386
387 return (allowed);
388 }
389
390 /*
391 * This key verify needs to send the key type along, because the
392 * privileged parent makes the decision if the key is allowed
393 * for authentication.
394 */
395
396 int
mm_key_verify(Key * key,u_char * sig,u_int siglen,u_char * data,u_int datalen)397 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
398 {
399 Buffer m;
400 u_char *blob;
401 u_int len;
402 int verified = 0;
403
404 debug3("%s entering", __func__);
405
406 /* Convert the key to a blob and the pass it over */
407 if (!key_to_blob(key, &blob, &len))
408 return (0);
409
410 buffer_init(&m);
411 buffer_put_string(&m, blob, len);
412 buffer_put_string(&m, sig, siglen);
413 buffer_put_string(&m, data, datalen);
414 xfree(blob);
415
416 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
417
418 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
419 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
420
421 verified = buffer_get_int(&m);
422
423 buffer_free(&m);
424
425 return (verified);
426 }
427
428 /* Export key state after authentication */
429 Newkeys *
mm_newkeys_from_blob(u_char * blob,int blen)430 mm_newkeys_from_blob(u_char *blob, int blen)
431 {
432 Buffer b;
433 u_int len;
434 Newkeys *newkey = NULL;
435 Enc *enc;
436 Mac *mac;
437 Comp *comp;
438
439 debug3("%s: %p(%d)", __func__, blob, blen);
440 #ifdef DEBUG_PK
441 dump_base64(stderr, blob, blen);
442 #endif
443 buffer_init(&b);
444 buffer_append(&b, blob, blen);
445
446 newkey = xmalloc(sizeof(*newkey));
447 enc = &newkey->enc;
448 mac = &newkey->mac;
449 comp = &newkey->comp;
450
451 /* Enc structure */
452 enc->name = buffer_get_string(&b, NULL);
453 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
454 enc->enabled = buffer_get_int(&b);
455 enc->block_size = buffer_get_int(&b);
456 enc->key = buffer_get_string(&b, &enc->key_len);
457 enc->iv = buffer_get_string(&b, &len);
458 if (len != enc->block_size)
459 fatal("%s: bad ivlen: expected %u != %u", __func__,
460 enc->block_size, len);
461
462 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
463 fatal("%s: bad cipher name %s or pointer %p", __func__,
464 enc->name, enc->cipher);
465
466 /* Mac structure */
467 mac->name = buffer_get_string(&b, NULL);
468 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
469 fatal("%s: can not setup mac %s", __func__, mac->name);
470 mac->enabled = buffer_get_int(&b);
471 mac->key = buffer_get_string(&b, &len);
472 if (len > mac->key_len)
473 fatal("%s: bad mac key length: %u > %d", __func__, len,
474 mac->key_len);
475 mac->key_len = len;
476
477 /* Comp structure */
478 comp->type = buffer_get_int(&b);
479 comp->enabled = buffer_get_int(&b);
480 comp->name = buffer_get_string(&b, NULL);
481
482 len = buffer_len(&b);
483 if (len != 0)
484 error("newkeys_from_blob: remaining bytes in blob %u", len);
485 buffer_free(&b);
486 return (newkey);
487 }
488
489 int
mm_newkeys_to_blob(int mode,u_char ** blobp,u_int * lenp)490 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
491 {
492 Buffer b;
493 int len;
494 Enc *enc;
495 Mac *mac;
496 Comp *comp;
497 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
498
499 debug3("%s: converting %p", __func__, newkey);
500
501 if (newkey == NULL) {
502 error("%s: newkey == NULL", __func__);
503 return 0;
504 }
505 enc = &newkey->enc;
506 mac = &newkey->mac;
507 comp = &newkey->comp;
508
509 buffer_init(&b);
510 /* Enc structure */
511 buffer_put_cstring(&b, enc->name);
512 /* The cipher struct is constant and shared, you export pointer */
513 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
514 buffer_put_int(&b, enc->enabled);
515 buffer_put_int(&b, enc->block_size);
516 buffer_put_string(&b, enc->key, enc->key_len);
517 packet_get_keyiv(mode, enc->iv, enc->block_size);
518 buffer_put_string(&b, enc->iv, enc->block_size);
519
520 /* Mac structure */
521 buffer_put_cstring(&b, mac->name);
522 buffer_put_int(&b, mac->enabled);
523 buffer_put_string(&b, mac->key, mac->key_len);
524
525 /* Comp structure */
526 buffer_put_int(&b, comp->type);
527 buffer_put_int(&b, comp->enabled);
528 buffer_put_cstring(&b, comp->name);
529
530 len = buffer_len(&b);
531 if (lenp != NULL)
532 *lenp = len;
533 if (blobp != NULL) {
534 *blobp = xmalloc(len);
535 memcpy(*blobp, buffer_ptr(&b), len);
536 }
537 memset(buffer_ptr(&b), 0, len);
538 buffer_free(&b);
539 return len;
540 }
541
542 static void
mm_send_kex(Buffer * m,Kex * kex)543 mm_send_kex(Buffer *m, Kex *kex)
544 {
545 buffer_put_string(m, kex->session_id, kex->session_id_len);
546 buffer_put_int(m, kex->we_need);
547 buffer_put_int(m, kex->hostkey_type);
548 buffer_put_int(m, kex->kex_type);
549 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
550 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
551 buffer_put_int(m, kex->flags);
552 buffer_put_cstring(m, kex->client_version_string);
553 buffer_put_cstring(m, kex->server_version_string);
554 }
555
556 void
mm_send_keystate(struct monitor * monitor)557 mm_send_keystate(struct monitor *monitor)
558 {
559 Buffer m, *input, *output;
560 u_char *blob, *p;
561 u_int bloblen, plen;
562 u_int32_t seqnr, packets;
563 u_int64_t blocks, bytes;
564
565 buffer_init(&m);
566
567 if (!compat20) {
568 u_char iv[24];
569 u_char *key;
570 u_int ivlen, keylen;
571
572 buffer_put_int(&m, packet_get_protocol_flags());
573
574 buffer_put_int(&m, packet_get_ssh1_cipher());
575
576 debug3("%s: Sending ssh1 KEY+IV", __func__);
577 keylen = packet_get_encryption_key(NULL);
578 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
579 keylen = packet_get_encryption_key(key);
580 buffer_put_string(&m, key, keylen);
581 memset(key, 0, keylen);
582 xfree(key);
583
584 ivlen = packet_get_keyiv_len(MODE_OUT);
585 packet_get_keyiv(MODE_OUT, iv, ivlen);
586 buffer_put_string(&m, iv, ivlen);
587 ivlen = packet_get_keyiv_len(MODE_OUT);
588 packet_get_keyiv(MODE_IN, iv, ivlen);
589 buffer_put_string(&m, iv, ivlen);
590 goto skip;
591 } else {
592 /* Kex for rekeying */
593 mm_send_kex(&m, *monitor->m_pkex);
594 }
595
596 debug3("%s: Sending new keys: %p %p",
597 __func__, packet_get_newkeys(MODE_OUT),
598 packet_get_newkeys(MODE_IN));
599
600 /* Keys from Kex */
601 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
602 fatal("%s: conversion of newkeys failed", __func__);
603
604 buffer_put_string(&m, blob, bloblen);
605 xfree(blob);
606
607 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
608 fatal("%s: conversion of newkeys failed", __func__);
609
610 buffer_put_string(&m, blob, bloblen);
611 xfree(blob);
612
613 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
614 buffer_put_int(&m, seqnr);
615 buffer_put_int64(&m, blocks);
616 buffer_put_int(&m, packets);
617 buffer_put_int64(&m, bytes);
618 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
619 buffer_put_int(&m, seqnr);
620 buffer_put_int64(&m, blocks);
621 buffer_put_int(&m, packets);
622 buffer_put_int64(&m, bytes);
623
624 debug3("%s: New keys have been sent", __func__);
625 skip:
626 /* More key context */
627 plen = packet_get_keycontext(MODE_OUT, NULL);
628 p = xmalloc(plen+1);
629 packet_get_keycontext(MODE_OUT, p);
630 buffer_put_string(&m, p, plen);
631 xfree(p);
632
633 plen = packet_get_keycontext(MODE_IN, NULL);
634 p = xmalloc(plen+1);
635 packet_get_keycontext(MODE_IN, p);
636 buffer_put_string(&m, p, plen);
637 xfree(p);
638
639 /* Compression state */
640 debug3("%s: Sending compression state", __func__);
641 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
642 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
643
644 /* Network I/O buffers */
645 input = (Buffer *)packet_get_input();
646 output = (Buffer *)packet_get_output();
647 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
648 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
649
650 /* Roaming */
651 if (compat20) {
652 buffer_put_int64(&m, get_sent_bytes());
653 buffer_put_int64(&m, get_recv_bytes());
654 }
655
656 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
657 debug3("%s: Finished sending state", __func__);
658
659 buffer_free(&m);
660 }
661
662 int
mm_pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)663 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
664 {
665 Buffer m;
666 char *p, *msg;
667 int success = 0, tmp1 = -1, tmp2 = -1;
668
669 /* Kludge: ensure there are fds free to receive the pty/tty */
670 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
671 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
672 error("%s: cannot allocate fds for pty", __func__);
673 if (tmp1 > 0)
674 close(tmp1);
675 if (tmp2 > 0)
676 close(tmp2);
677 return 0;
678 }
679 close(tmp1);
680 close(tmp2);
681
682 buffer_init(&m);
683 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
684
685 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
686 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
687
688 success = buffer_get_int(&m);
689 if (success == 0) {
690 debug3("%s: pty alloc failed", __func__);
691 buffer_free(&m);
692 return (0);
693 }
694 p = buffer_get_string(&m, NULL);
695 msg = buffer_get_string(&m, NULL);
696 buffer_free(&m);
697
698 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
699 xfree(p);
700
701 buffer_append(&loginmsg, msg, strlen(msg));
702 xfree(msg);
703
704 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
705 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
706 fatal("%s: receive fds failed", __func__);
707
708 /* Success */
709 return (1);
710 }
711
712 void
mm_session_pty_cleanup2(Session * s)713 mm_session_pty_cleanup2(Session *s)
714 {
715 Buffer m;
716
717 if (s->ttyfd == -1)
718 return;
719 buffer_init(&m);
720 buffer_put_cstring(&m, s->tty);
721 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
722 buffer_free(&m);
723
724 /* closed dup'ed master */
725 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
726 error("close(s->ptymaster/%d): %s",
727 s->ptymaster, strerror(errno));
728
729 /* unlink pty from session */
730 s->ttyfd = -1;
731 }
732
733 /* Request process termination */
734
735 void
mm_terminate(void)736 mm_terminate(void)
737 {
738 Buffer m;
739
740 buffer_init(&m);
741 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
742 buffer_free(&m);
743 }
744
745 int
mm_ssh1_session_key(BIGNUM * num)746 mm_ssh1_session_key(BIGNUM *num)
747 {
748 int rsafail;
749 Buffer m;
750
751 buffer_init(&m);
752 buffer_put_bignum2(&m, num);
753 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
754
755 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
756
757 rsafail = buffer_get_int(&m);
758 buffer_get_bignum2(&m, num);
759
760 buffer_free(&m);
761
762 return (rsafail);
763 }
764
765 static void
mm_chall_setup(char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)766 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
767 char ***prompts, u_int **echo_on)
768 {
769 *name = xstrdup("");
770 *infotxt = xstrdup("");
771 *numprompts = 1;
772 *prompts = xcalloc(*numprompts, sizeof(char *));
773 *echo_on = xcalloc(*numprompts, sizeof(u_int));
774 (*echo_on)[0] = 0;
775 }
776
777 int
mm_bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)778 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
779 u_int *numprompts, char ***prompts, u_int **echo_on)
780 {
781 Buffer m;
782 u_int success;
783 char *challenge;
784
785 debug3("%s: entering", __func__);
786
787 buffer_init(&m);
788 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
789
790 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
791 &m);
792 success = buffer_get_int(&m);
793 if (success == 0) {
794 debug3("%s: no challenge", __func__);
795 buffer_free(&m);
796 return (-1);
797 }
798
799 /* Get the challenge, and format the response */
800 challenge = buffer_get_string(&m, NULL);
801 buffer_free(&m);
802
803 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
804 (*prompts)[0] = challenge;
805
806 debug3("%s: received challenge: %s", __func__, challenge);
807
808 return (0);
809 }
810
811 int
mm_bsdauth_respond(void * ctx,u_int numresponses,char ** responses)812 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
813 {
814 Buffer m;
815 int authok;
816
817 debug3("%s: entering", __func__);
818 if (numresponses != 1)
819 return (-1);
820
821 buffer_init(&m);
822 buffer_put_cstring(&m, responses[0]);
823 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
824
825 mm_request_receive_expect(pmonitor->m_recvfd,
826 MONITOR_ANS_BSDAUTHRESPOND, &m);
827
828 authok = buffer_get_int(&m);
829 buffer_free(&m);
830
831 return ((authok == 0) ? -1 : 0);
832 }
833
834
835 void
mm_ssh1_session_id(u_char session_id[16])836 mm_ssh1_session_id(u_char session_id[16])
837 {
838 Buffer m;
839 int i;
840
841 debug3("%s entering", __func__);
842
843 buffer_init(&m);
844 for (i = 0; i < 16; i++)
845 buffer_put_char(&m, session_id[i]);
846
847 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
848 buffer_free(&m);
849 }
850
851 int
mm_auth_rsa_key_allowed(struct passwd * pw,BIGNUM * client_n,Key ** rkey)852 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
853 {
854 Buffer m;
855 Key *key;
856 u_char *blob;
857 u_int blen;
858 int allowed = 0, have_forced = 0;
859
860 debug3("%s entering", __func__);
861
862 buffer_init(&m);
863 buffer_put_bignum2(&m, client_n);
864
865 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
866 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
867
868 allowed = buffer_get_int(&m);
869
870 /* fake forced command */
871 auth_clear_options();
872 have_forced = buffer_get_int(&m);
873 forced_command = have_forced ? xstrdup("true") : NULL;
874
875 if (allowed && rkey != NULL) {
876 blob = buffer_get_string(&m, &blen);
877 if ((key = key_from_blob(blob, blen)) == NULL)
878 fatal("%s: key_from_blob failed", __func__);
879 *rkey = key;
880 xfree(blob);
881 }
882 mm_send_debug(&m);
883 buffer_free(&m);
884
885 return (allowed);
886 }
887
888 BIGNUM *
mm_auth_rsa_generate_challenge(Key * key)889 mm_auth_rsa_generate_challenge(Key *key)
890 {
891 Buffer m;
892 BIGNUM *challenge;
893 u_char *blob;
894 u_int blen;
895
896 debug3("%s entering", __func__);
897
898 if ((challenge = BN_new()) == NULL)
899 fatal("%s: BN_new failed", __func__);
900
901 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
902 if (key_to_blob(key, &blob, &blen) == 0)
903 fatal("%s: key_to_blob failed", __func__);
904 key->type = KEY_RSA1;
905
906 buffer_init(&m);
907 buffer_put_string(&m, blob, blen);
908 xfree(blob);
909
910 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
911 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
912
913 buffer_get_bignum2(&m, challenge);
914 buffer_free(&m);
915
916 return (challenge);
917 }
918
919 int
mm_auth_rsa_verify_response(Key * key,BIGNUM * p,u_char response[16])920 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
921 {
922 Buffer m;
923 u_char *blob;
924 u_int blen;
925 int success = 0;
926
927 debug3("%s entering", __func__);
928
929 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
930 if (key_to_blob(key, &blob, &blen) == 0)
931 fatal("%s: key_to_blob failed", __func__);
932 key->type = KEY_RSA1;
933
934 buffer_init(&m);
935 buffer_put_string(&m, blob, blen);
936 buffer_put_string(&m, response, 16);
937 xfree(blob);
938
939 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
940 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
941
942 success = buffer_get_int(&m);
943 buffer_free(&m);
944
945 return (success);
946 }
947