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