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