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