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