1 /* $OpenBSD: monitor_wrap.c,v 1.88 2016/03/07 19:02:43 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 #include "auth-pam.h" 64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ 65 #undef TARGET_OS_MAC 66 #include "zlib.h" 67 #define TARGET_OS_MAC 1 68 #else 69 #include "zlib.h" 70 #endif 71 #include "monitor.h" 72 #ifdef GSSAPI 73 #include "ssh-gss.h" 74 #endif 75 #include "monitor_wrap.h" 76 #include "atomicio.h" 77 #include "monitor_fdpass.h" 78 #include "misc.h" 79 #include "uuencode.h" 80 81 #include "channels.h" 82 #include "session.h" 83 #include "servconf.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, const char *hostkey_alg) 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 buffer_put_cstring(&m, hostkey_alg); 232 233 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); 234 235 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); 236 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); 237 *sigp = buffer_get_string(&m, lenp); 238 buffer_free(&m); 239 240 return (0); 241 } 242 243 struct passwd * 244 mm_getpwnamallow(const char *username) 245 { 246 Buffer m; 247 struct passwd *pw; 248 u_int len, i; 249 ServerOptions *newopts; 250 251 debug3("%s entering", __func__); 252 253 buffer_init(&m); 254 buffer_put_cstring(&m, username); 255 256 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); 257 258 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); 259 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); 260 261 if (buffer_get_char(&m) == 0) { 262 pw = NULL; 263 goto out; 264 } 265 pw = buffer_get_string(&m, &len); 266 if (len != sizeof(struct passwd)) 267 fatal("%s: struct passwd size mismatch", __func__); 268 pw->pw_name = buffer_get_string(&m, NULL); 269 pw->pw_passwd = buffer_get_string(&m, NULL); 270 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS 271 pw->pw_gecos = buffer_get_string(&m, NULL); 272 #endif 273 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS 274 pw->pw_class = buffer_get_string(&m, NULL); 275 #endif 276 pw->pw_dir = buffer_get_string(&m, NULL); 277 pw->pw_shell = buffer_get_string(&m, NULL); 278 279 out: 280 /* copy options block as a Match directive may have changed some */ 281 newopts = buffer_get_string(&m, &len); 282 if (len != sizeof(*newopts)) 283 fatal("%s: option block size mismatch", __func__); 284 285 #define M_CP_STROPT(x) do { \ 286 if (newopts->x != NULL) \ 287 newopts->x = buffer_get_string(&m, NULL); \ 288 } while (0) 289 #define M_CP_STRARRAYOPT(x, nx) do { \ 290 for (i = 0; i < newopts->nx; i++) \ 291 newopts->x[i] = buffer_get_string(&m, NULL); \ 292 } while (0) 293 /* See comment in servconf.h */ 294 COPY_MATCH_STRING_OPTS(); 295 #undef M_CP_STROPT 296 #undef M_CP_STRARRAYOPT 297 298 copy_set_server_options(&options, newopts, 1); 299 free(newopts); 300 301 buffer_free(&m); 302 303 return (pw); 304 } 305 306 char * 307 mm_auth2_read_banner(void) 308 { 309 Buffer m; 310 char *banner; 311 312 debug3("%s entering", __func__); 313 314 buffer_init(&m); 315 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); 316 buffer_clear(&m); 317 318 mm_request_receive_expect(pmonitor->m_recvfd, 319 MONITOR_ANS_AUTH2_READ_BANNER, &m); 320 banner = buffer_get_string(&m, NULL); 321 buffer_free(&m); 322 323 /* treat empty banner as missing banner */ 324 if (strlen(banner) == 0) { 325 free(banner); 326 banner = NULL; 327 } 328 return (banner); 329 } 330 331 /* Inform the privileged process about service and style */ 332 333 void 334 mm_inform_authserv(char *service, char *style) 335 { 336 Buffer m; 337 338 debug3("%s entering", __func__); 339 340 buffer_init(&m); 341 buffer_put_cstring(&m, service); 342 buffer_put_cstring(&m, style ? style : ""); 343 344 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); 345 346 buffer_free(&m); 347 } 348 349 /* Do the password authentication */ 350 int 351 mm_auth_password(Authctxt *authctxt, char *password) 352 { 353 Buffer m; 354 int authenticated = 0; 355 356 debug3("%s entering", __func__); 357 358 buffer_init(&m); 359 buffer_put_cstring(&m, password); 360 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); 361 362 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); 363 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); 364 365 authenticated = buffer_get_int(&m); 366 #ifdef USE_PAM 367 sshpam_set_maxtries_reached(buffer_get_int(&m)); 368 #endif 369 370 buffer_free(&m); 371 372 debug3("%s: user %sauthenticated", 373 __func__, authenticated ? "" : "not "); 374 return (authenticated); 375 } 376 377 int 378 mm_user_key_allowed(struct passwd *pw, Key *key, int pubkey_auth_attempt) 379 { 380 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key, 381 pubkey_auth_attempt)); 382 } 383 384 int 385 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host, 386 Key *key) 387 { 388 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0)); 389 } 390 391 int 392 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, const char *user, 393 const char *host, Key *key) 394 { 395 int ret; 396 397 key->type = KEY_RSA; /* XXX hack for key_to_blob */ 398 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key, 0); 399 key->type = KEY_RSA1; 400 return (ret); 401 } 402 403 int 404 mm_key_allowed(enum mm_keytype type, const char *user, const char *host, 405 Key *key, int pubkey_auth_attempt) 406 { 407 Buffer m; 408 u_char *blob; 409 u_int len; 410 int allowed = 0, have_forced = 0; 411 412 debug3("%s entering", __func__); 413 414 /* Convert the key to a blob and the pass it over */ 415 if (!key_to_blob(key, &blob, &len)) 416 return (0); 417 418 buffer_init(&m); 419 buffer_put_int(&m, type); 420 buffer_put_cstring(&m, user ? user : ""); 421 buffer_put_cstring(&m, host ? host : ""); 422 buffer_put_string(&m, blob, len); 423 buffer_put_int(&m, pubkey_auth_attempt); 424 free(blob); 425 426 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); 427 428 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); 429 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); 430 431 allowed = buffer_get_int(&m); 432 433 /* fake forced command */ 434 auth_clear_options(); 435 have_forced = buffer_get_int(&m); 436 forced_command = have_forced ? xstrdup("true") : NULL; 437 438 buffer_free(&m); 439 440 return (allowed); 441 } 442 443 /* 444 * This key verify needs to send the key type along, because the 445 * privileged parent makes the decision if the key is allowed 446 * for authentication. 447 */ 448 449 int 450 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) 451 { 452 Buffer m; 453 u_char *blob; 454 u_int len; 455 int verified = 0; 456 457 debug3("%s entering", __func__); 458 459 /* Convert the key to a blob and the pass it over */ 460 if (!key_to_blob(key, &blob, &len)) 461 return (0); 462 463 buffer_init(&m); 464 buffer_put_string(&m, blob, len); 465 buffer_put_string(&m, sig, siglen); 466 buffer_put_string(&m, data, datalen); 467 free(blob); 468 469 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); 470 471 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); 472 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); 473 474 verified = buffer_get_int(&m); 475 476 buffer_free(&m); 477 478 return (verified); 479 } 480 481 void 482 mm_send_keystate(struct monitor *monitor) 483 { 484 struct ssh *ssh = active_state; /* XXX */ 485 struct sshbuf *m; 486 int r; 487 488 if ((m = sshbuf_new()) == NULL) 489 fatal("%s: sshbuf_new failed", __func__); 490 if ((r = ssh_packet_get_state(ssh, m)) != 0) 491 fatal("%s: get_state failed: %s", 492 __func__, ssh_err(r)); 493 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m); 494 debug3("%s: Finished sending state", __func__); 495 sshbuf_free(m); 496 } 497 498 int 499 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) 500 { 501 Buffer m; 502 char *p, *msg; 503 int success = 0, tmp1 = -1, tmp2 = -1; 504 505 /* Kludge: ensure there are fds free to receive the pty/tty */ 506 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 || 507 (tmp2 = dup(pmonitor->m_recvfd)) == -1) { 508 error("%s: cannot allocate fds for pty", __func__); 509 if (tmp1 > 0) 510 close(tmp1); 511 if (tmp2 > 0) 512 close(tmp2); 513 return 0; 514 } 515 close(tmp1); 516 close(tmp2); 517 518 buffer_init(&m); 519 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); 520 521 debug3("%s: waiting for MONITOR_ANS_PTY", __func__); 522 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); 523 524 success = buffer_get_int(&m); 525 if (success == 0) { 526 debug3("%s: pty alloc failed", __func__); 527 buffer_free(&m); 528 return (0); 529 } 530 p = buffer_get_string(&m, NULL); 531 msg = buffer_get_string(&m, NULL); 532 buffer_free(&m); 533 534 strlcpy(namebuf, p, namebuflen); /* Possible truncation */ 535 free(p); 536 537 buffer_append(&loginmsg, msg, strlen(msg)); 538 free(msg); 539 540 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 || 541 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1) 542 fatal("%s: receive fds failed", __func__); 543 544 /* Success */ 545 return (1); 546 } 547 548 void 549 mm_session_pty_cleanup2(Session *s) 550 { 551 Buffer m; 552 553 if (s->ttyfd == -1) 554 return; 555 buffer_init(&m); 556 buffer_put_cstring(&m, s->tty); 557 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); 558 buffer_free(&m); 559 560 /* closed dup'ed master */ 561 if (s->ptymaster != -1 && close(s->ptymaster) < 0) 562 error("close(s->ptymaster/%d): %s", 563 s->ptymaster, strerror(errno)); 564 565 /* unlink pty from session */ 566 s->ttyfd = -1; 567 } 568 569 #ifdef USE_PAM 570 void 571 mm_start_pam(Authctxt *authctxt) 572 { 573 Buffer m; 574 575 debug3("%s entering", __func__); 576 if (!options.use_pam) 577 fatal("UsePAM=no, but ended up in %s anyway", __func__); 578 579 buffer_init(&m); 580 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); 581 582 buffer_free(&m); 583 } 584 585 u_int 586 mm_do_pam_account(void) 587 { 588 Buffer m; 589 u_int ret; 590 char *msg; 591 592 debug3("%s entering", __func__); 593 if (!options.use_pam) 594 fatal("UsePAM=no, but ended up in %s anyway", __func__); 595 596 buffer_init(&m); 597 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m); 598 599 mm_request_receive_expect(pmonitor->m_recvfd, 600 MONITOR_ANS_PAM_ACCOUNT, &m); 601 ret = buffer_get_int(&m); 602 msg = buffer_get_string(&m, NULL); 603 buffer_append(&loginmsg, msg, strlen(msg)); 604 free(msg); 605 606 buffer_free(&m); 607 608 debug3("%s returning %d", __func__, ret); 609 610 return (ret); 611 } 612 613 void * 614 mm_sshpam_init_ctx(Authctxt *authctxt) 615 { 616 Buffer m; 617 int success; 618 619 debug3("%s", __func__); 620 buffer_init(&m); 621 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m); 622 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__); 623 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m); 624 success = buffer_get_int(&m); 625 if (success == 0) { 626 debug3("%s: pam_init_ctx failed", __func__); 627 buffer_free(&m); 628 return (NULL); 629 } 630 buffer_free(&m); 631 return (authctxt); 632 } 633 634 int 635 mm_sshpam_query(void *ctx, char **name, char **info, 636 u_int *num, char ***prompts, u_int **echo_on) 637 { 638 Buffer m; 639 u_int i; 640 int ret; 641 642 debug3("%s", __func__); 643 buffer_init(&m); 644 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m); 645 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__); 646 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m); 647 ret = buffer_get_int(&m); 648 debug3("%s: pam_query returned %d", __func__, ret); 649 *name = buffer_get_string(&m, NULL); 650 *info = buffer_get_string(&m, NULL); 651 sshpam_set_maxtries_reached(buffer_get_int(&m)); 652 *num = buffer_get_int(&m); 653 if (*num > PAM_MAX_NUM_MSG) 654 fatal("%s: recieved %u PAM messages, expected <= %u", 655 __func__, *num, PAM_MAX_NUM_MSG); 656 *prompts = xcalloc((*num + 1), sizeof(char *)); 657 *echo_on = xcalloc((*num + 1), sizeof(u_int)); 658 for (i = 0; i < *num; ++i) { 659 (*prompts)[i] = buffer_get_string(&m, NULL); 660 (*echo_on)[i] = buffer_get_int(&m); 661 } 662 buffer_free(&m); 663 return (ret); 664 } 665 666 int 667 mm_sshpam_respond(void *ctx, u_int num, char **resp) 668 { 669 Buffer m; 670 u_int i; 671 int ret; 672 673 debug3("%s", __func__); 674 buffer_init(&m); 675 buffer_put_int(&m, num); 676 for (i = 0; i < num; ++i) 677 buffer_put_cstring(&m, resp[i]); 678 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m); 679 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__); 680 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m); 681 ret = buffer_get_int(&m); 682 debug3("%s: pam_respond returned %d", __func__, ret); 683 buffer_free(&m); 684 return (ret); 685 } 686 687 void 688 mm_sshpam_free_ctx(void *ctxtp) 689 { 690 Buffer m; 691 692 debug3("%s", __func__); 693 buffer_init(&m); 694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m); 695 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__); 696 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m); 697 buffer_free(&m); 698 } 699 #endif /* USE_PAM */ 700 701 /* Request process termination */ 702 703 void 704 mm_terminate(void) 705 { 706 Buffer m; 707 708 buffer_init(&m); 709 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); 710 buffer_free(&m); 711 } 712 713 #ifdef WITH_SSH1 714 int 715 mm_ssh1_session_key(BIGNUM *num) 716 { 717 int rsafail; 718 Buffer m; 719 720 buffer_init(&m); 721 buffer_put_bignum2(&m, num); 722 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); 723 724 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); 725 726 rsafail = buffer_get_int(&m); 727 buffer_get_bignum2(&m, num); 728 729 buffer_free(&m); 730 731 return (rsafail); 732 } 733 #endif 734 735 static void 736 mm_chall_setup(char **name, char **infotxt, u_int *numprompts, 737 char ***prompts, u_int **echo_on) 738 { 739 *name = xstrdup(""); 740 *infotxt = xstrdup(""); 741 *numprompts = 1; 742 *prompts = xcalloc(*numprompts, sizeof(char *)); 743 *echo_on = xcalloc(*numprompts, sizeof(u_int)); 744 (*echo_on)[0] = 0; 745 } 746 747 int 748 mm_bsdauth_query(void *ctx, char **name, char **infotxt, 749 u_int *numprompts, char ***prompts, u_int **echo_on) 750 { 751 Buffer m; 752 u_int success; 753 char *challenge; 754 755 debug3("%s: entering", __func__); 756 757 buffer_init(&m); 758 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); 759 760 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, 761 &m); 762 success = buffer_get_int(&m); 763 if (success == 0) { 764 debug3("%s: no challenge", __func__); 765 buffer_free(&m); 766 return (-1); 767 } 768 769 /* Get the challenge, and format the response */ 770 challenge = buffer_get_string(&m, NULL); 771 buffer_free(&m); 772 773 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 774 (*prompts)[0] = challenge; 775 776 debug3("%s: received challenge: %s", __func__, challenge); 777 778 return (0); 779 } 780 781 int 782 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) 783 { 784 Buffer m; 785 int authok; 786 787 debug3("%s: entering", __func__); 788 if (numresponses != 1) 789 return (-1); 790 791 buffer_init(&m); 792 buffer_put_cstring(&m, responses[0]); 793 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); 794 795 mm_request_receive_expect(pmonitor->m_recvfd, 796 MONITOR_ANS_BSDAUTHRESPOND, &m); 797 798 authok = buffer_get_int(&m); 799 buffer_free(&m); 800 801 return ((authok == 0) ? -1 : 0); 802 } 803 804 #ifdef SKEY 805 int 806 mm_skey_query(void *ctx, char **name, char **infotxt, 807 u_int *numprompts, char ***prompts, u_int **echo_on) 808 { 809 Buffer m; 810 u_int success; 811 char *challenge; 812 813 debug3("%s: entering", __func__); 814 815 buffer_init(&m); 816 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); 817 818 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, 819 &m); 820 success = buffer_get_int(&m); 821 if (success == 0) { 822 debug3("%s: no challenge", __func__); 823 buffer_free(&m); 824 return (-1); 825 } 826 827 /* Get the challenge, and format the response */ 828 challenge = buffer_get_string(&m, NULL); 829 buffer_free(&m); 830 831 debug3("%s: received challenge: %s", __func__, challenge); 832 833 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 834 835 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT); 836 free(challenge); 837 838 return (0); 839 } 840 841 int 842 mm_skey_respond(void *ctx, u_int numresponses, char **responses) 843 { 844 Buffer m; 845 int authok; 846 847 debug3("%s: entering", __func__); 848 if (numresponses != 1) 849 return (-1); 850 851 buffer_init(&m); 852 buffer_put_cstring(&m, responses[0]); 853 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); 854 855 mm_request_receive_expect(pmonitor->m_recvfd, 856 MONITOR_ANS_SKEYRESPOND, &m); 857 858 authok = buffer_get_int(&m); 859 buffer_free(&m); 860 861 return ((authok == 0) ? -1 : 0); 862 } 863 #endif /* SKEY */ 864 865 void 866 mm_ssh1_session_id(u_char session_id[16]) 867 { 868 Buffer m; 869 int i; 870 871 debug3("%s entering", __func__); 872 873 buffer_init(&m); 874 for (i = 0; i < 16; i++) 875 buffer_put_char(&m, session_id[i]); 876 877 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); 878 buffer_free(&m); 879 } 880 881 #ifdef WITH_SSH1 882 int 883 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) 884 { 885 Buffer m; 886 Key *key; 887 u_char *blob; 888 u_int blen; 889 int allowed = 0, have_forced = 0; 890 891 debug3("%s entering", __func__); 892 893 buffer_init(&m); 894 buffer_put_bignum2(&m, client_n); 895 896 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); 897 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); 898 899 allowed = buffer_get_int(&m); 900 901 /* fake forced command */ 902 auth_clear_options(); 903 have_forced = buffer_get_int(&m); 904 forced_command = have_forced ? xstrdup("true") : NULL; 905 906 if (allowed && rkey != NULL) { 907 blob = buffer_get_string(&m, &blen); 908 if ((key = key_from_blob(blob, blen)) == NULL) 909 fatal("%s: key_from_blob failed", __func__); 910 *rkey = key; 911 free(blob); 912 } 913 buffer_free(&m); 914 915 return (allowed); 916 } 917 918 BIGNUM * 919 mm_auth_rsa_generate_challenge(Key *key) 920 { 921 Buffer m; 922 BIGNUM *challenge; 923 u_char *blob; 924 u_int blen; 925 926 debug3("%s entering", __func__); 927 928 if ((challenge = BN_new()) == NULL) 929 fatal("%s: BN_new failed", __func__); 930 931 key->type = KEY_RSA; /* XXX cheat for key_to_blob */ 932 if (key_to_blob(key, &blob, &blen) == 0) 933 fatal("%s: key_to_blob failed", __func__); 934 key->type = KEY_RSA1; 935 936 buffer_init(&m); 937 buffer_put_string(&m, blob, blen); 938 free(blob); 939 940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); 941 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); 942 943 buffer_get_bignum2(&m, challenge); 944 buffer_free(&m); 945 946 return (challenge); 947 } 948 949 int 950 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) 951 { 952 Buffer m; 953 u_char *blob; 954 u_int blen; 955 int success = 0; 956 957 debug3("%s entering", __func__); 958 959 key->type = KEY_RSA; /* XXX cheat for key_to_blob */ 960 if (key_to_blob(key, &blob, &blen) == 0) 961 fatal("%s: key_to_blob failed", __func__); 962 key->type = KEY_RSA1; 963 964 buffer_init(&m); 965 buffer_put_string(&m, blob, blen); 966 buffer_put_string(&m, response, 16); 967 free(blob); 968 969 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); 970 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); 971 972 success = buffer_get_int(&m); 973 buffer_free(&m); 974 975 return (success); 976 } 977 #endif 978 979 #ifdef SSH_AUDIT_EVENTS 980 void 981 mm_audit_event(ssh_audit_event_t event) 982 { 983 Buffer m; 984 985 debug3("%s entering", __func__); 986 987 buffer_init(&m); 988 buffer_put_int(&m, event); 989 990 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m); 991 buffer_free(&m); 992 } 993 994 void 995 mm_audit_run_command(const char *command) 996 { 997 Buffer m; 998 999 debug3("%s entering command %s", __func__, command); 1000 1001 buffer_init(&m); 1002 buffer_put_cstring(&m, command); 1003 1004 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m); 1005 buffer_free(&m); 1006 } 1007 #endif /* SSH_AUDIT_EVENTS */ 1008 1009 #ifdef GSSAPI 1010 OM_uint32 1011 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) 1012 { 1013 Buffer m; 1014 OM_uint32 major; 1015 1016 /* Client doesn't get to see the context */ 1017 *ctx = NULL; 1018 1019 buffer_init(&m); 1020 buffer_put_string(&m, goid->elements, goid->length); 1021 1022 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m); 1023 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m); 1024 1025 major = buffer_get_int(&m); 1026 1027 buffer_free(&m); 1028 return (major); 1029 } 1030 1031 OM_uint32 1032 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, 1033 gss_buffer_desc *out, OM_uint32 *flags) 1034 { 1035 Buffer m; 1036 OM_uint32 major; 1037 u_int len; 1038 1039 buffer_init(&m); 1040 buffer_put_string(&m, in->value, in->length); 1041 1042 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m); 1043 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m); 1044 1045 major = buffer_get_int(&m); 1046 out->value = buffer_get_string(&m, &len); 1047 out->length = len; 1048 if (flags) 1049 *flags = buffer_get_int(&m); 1050 1051 buffer_free(&m); 1052 1053 return (major); 1054 } 1055 1056 OM_uint32 1057 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) 1058 { 1059 Buffer m; 1060 OM_uint32 major; 1061 1062 buffer_init(&m); 1063 buffer_put_string(&m, gssbuf->value, gssbuf->length); 1064 buffer_put_string(&m, gssmic->value, gssmic->length); 1065 1066 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m); 1067 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC, 1068 &m); 1069 1070 major = buffer_get_int(&m); 1071 buffer_free(&m); 1072 return(major); 1073 } 1074 1075 int 1076 mm_ssh_gssapi_userok(char *user) 1077 { 1078 Buffer m; 1079 int authenticated = 0; 1080 1081 buffer_init(&m); 1082 1083 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m); 1084 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK, 1085 &m); 1086 1087 authenticated = buffer_get_int(&m); 1088 1089 buffer_free(&m); 1090 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); 1091 return (authenticated); 1092 } 1093 #endif /* GSSAPI */ 1094 1095