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