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