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