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