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