1 /* $NetBSD: monitor_wrap.c,v 1.18 2017/10/07 19:39:19 christos Exp $ */ 2 /* $OpenBSD: monitor_wrap.c,v 1.94 2017/10/02 19:33:20 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.18 2017/10/07 19:39:19 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 z_stream incoming_stream; 86 extern z_stream outgoing_stream; 87 extern struct monitor *pmonitor; 88 extern Buffer loginmsg; 89 extern ServerOptions options; 90 91 void 92 mm_log_handler(LogLevel level, const char *msg, void *ctx) 93 { 94 Buffer log_msg; 95 struct monitor *mon = (struct monitor *)ctx; 96 97 if (mon->m_log_sendfd == -1) 98 fatal("%s: no log channel", __func__); 99 100 buffer_init(&log_msg); 101 /* 102 * Placeholder for packet length. Will be filled in with the actual 103 * packet length once the packet has been constucted. This saves 104 * fragile math. 105 */ 106 buffer_put_int(&log_msg, 0); 107 108 buffer_put_int(&log_msg, level); 109 buffer_put_cstring(&log_msg, msg); 110 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4); 111 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg), 112 buffer_len(&log_msg)) != buffer_len(&log_msg)) 113 fatal("%s: write: %s", __func__, strerror(errno)); 114 buffer_free(&log_msg); 115 } 116 117 int 118 mm_is_monitor(void) 119 { 120 /* 121 * m_pid is only set in the privileged part, and 122 * points to the unprivileged child. 123 */ 124 return (pmonitor && pmonitor->m_pid > 0); 125 } 126 127 void 128 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m) 129 { 130 u_int mlen = buffer_len(m); 131 u_char buf[5]; 132 133 debug3("%s entering: type %d", __func__, type); 134 135 put_u32(buf, mlen + 1); 136 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ 137 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) 138 fatal("%s: write: %s", __func__, strerror(errno)); 139 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) 140 fatal("%s: write: %s", __func__, strerror(errno)); 141 } 142 143 void 144 mm_request_receive(int sock, Buffer *m) 145 { 146 u_char buf[4]; 147 u_int msg_len; 148 149 debug3("%s entering", __func__); 150 151 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { 152 if (errno == EPIPE) 153 cleanup_exit(255); 154 fatal("%s: read: %s", __func__, strerror(errno)); 155 } 156 msg_len = get_u32(buf); 157 if (msg_len > 256 * 1024) 158 fatal("%s: read: bad msg_len %d", __func__, msg_len); 159 buffer_clear(m); 160 buffer_append_space(m, msg_len); 161 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len) 162 fatal("%s: read: %s", __func__, strerror(errno)); 163 } 164 165 void 166 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m) 167 { 168 u_char rtype; 169 170 debug3("%s entering: type %d", __func__, type); 171 172 mm_request_receive(sock, m); 173 rtype = buffer_get_char(m); 174 if (rtype != type) 175 fatal("%s: read: rtype %d != type %d", __func__, 176 rtype, type); 177 } 178 179 #ifdef WITH_OPENSSL 180 DH * 181 mm_choose_dh(int min, int nbits, int max) 182 { 183 BIGNUM *p, *g; 184 int success = 0; 185 Buffer m; 186 187 buffer_init(&m); 188 buffer_put_int(&m, min); 189 buffer_put_int(&m, nbits); 190 buffer_put_int(&m, max); 191 192 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); 193 194 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); 195 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); 196 197 success = buffer_get_char(&m); 198 if (success == 0) 199 fatal("%s: MONITOR_ANS_MODULI failed", __func__); 200 201 if ((p = BN_new()) == NULL) 202 fatal("%s: BN_new failed", __func__); 203 if ((g = BN_new()) == NULL) 204 fatal("%s: BN_new failed", __func__); 205 buffer_get_bignum2(&m, p); 206 buffer_get_bignum2(&m, g); 207 208 debug3("%s: remaining %d", __func__, buffer_len(&m)); 209 buffer_free(&m); 210 211 return (dh_new_group(g, p)); 212 } 213 #endif 214 215 int 216 mm_key_sign(struct sshkey *key, u_char **sigp, u_int *lenp, 217 const u_char *data, u_int datalen, const char *hostkey_alg) 218 { 219 struct kex *kex = *pmonitor->m_pkex; 220 Buffer m; 221 222 debug3("%s entering", __func__); 223 224 buffer_init(&m); 225 buffer_put_int(&m, kex->host_key_index(key, 0, active_state)); 226 buffer_put_string(&m, data, datalen); 227 buffer_put_cstring(&m, hostkey_alg); 228 229 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); 230 231 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); 232 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); 233 *sigp = buffer_get_string(&m, lenp); 234 buffer_free(&m); 235 236 return (0); 237 } 238 239 struct passwd * 240 mm_getpwnamallow(const char *username) 241 { 242 struct ssh *ssh = active_state; /* XXX */ 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 #define M_CP_STRARRAYOPT_ALLOC(x, nx) do { \ 287 newopts->x = newopts->nx == 0 ? \ 288 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \ 289 M_CP_STRARRAYOPT(x, nx); \ 290 } while (0) 291 /* See comment in servconf.h */ 292 COPY_MATCH_STRING_OPTS(); 293 #undef M_CP_STROPT 294 #undef M_CP_STRARRAYOPT 295 #undef M_CP_STRARRAYOPT_ALLOC 296 297 copy_set_server_options(&options, newopts, 1); 298 log_change_level(options.log_level); 299 process_permitopen(ssh, &options); 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, const 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, struct sshkey *key, 377 int pubkey_auth_attempt) 378 { 379 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key, 380 pubkey_auth_attempt)); 381 } 382 383 int 384 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host, 385 struct sshkey *key) 386 { 387 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0)); 388 } 389 390 int 391 mm_key_allowed(enum mm_keytype type, const char *user, const char *host, 392 struct sshkey *key, int pubkey_auth_attempt) 393 { 394 Buffer m; 395 u_char *blob; 396 u_int len; 397 int allowed = 0, have_forced = 0; 398 399 debug3("%s entering", __func__); 400 401 /* Convert the key to a blob and the pass it over */ 402 if (!key_to_blob(key, &blob, &len)) 403 return (0); 404 405 buffer_init(&m); 406 buffer_put_int(&m, type); 407 buffer_put_cstring(&m, user ? user : ""); 408 buffer_put_cstring(&m, host ? host : ""); 409 buffer_put_string(&m, blob, len); 410 buffer_put_int(&m, pubkey_auth_attempt); 411 free(blob); 412 413 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); 414 415 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); 416 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); 417 418 allowed = buffer_get_int(&m); 419 420 /* fake forced command */ 421 auth_clear_options(); 422 have_forced = buffer_get_int(&m); 423 forced_command = have_forced ? xstrdup("true") : NULL; 424 425 buffer_free(&m); 426 427 return (allowed); 428 } 429 430 /* 431 * This key verify needs to send the key type along, because the 432 * privileged parent makes the decision if the key is allowed 433 * for authentication. 434 */ 435 436 int 437 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen, 438 const u_char *data, size_t datalen, u_int compat) 439 { 440 Buffer m; 441 u_char *blob; 442 u_int len; 443 u_int encoded_ret = 0; 444 445 debug3("%s entering", __func__); 446 447 /* Convert the key to a blob and the pass it over */ 448 if (!key_to_blob(key, &blob, &len)) 449 return (0); 450 451 buffer_init(&m); 452 buffer_put_string(&m, blob, len); 453 buffer_put_string(&m, sig, siglen); 454 buffer_put_string(&m, data, datalen); 455 free(blob); 456 457 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); 458 459 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); 460 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); 461 462 encoded_ret = buffer_get_int(&m); 463 464 buffer_free(&m); 465 466 if (encoded_ret != 0) 467 return SSH_ERR_SIGNATURE_INVALID; 468 return 0; 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 #if defined(BSD_AUTH) || defined(SKEY) 704 static void 705 mm_chall_setup(char **name, char **infotxt, u_int *numprompts, 706 char ***prompts, u_int **echo_on) 707 { 708 *name = xstrdup(""); 709 *infotxt = xstrdup(""); 710 *numprompts = 1; 711 *prompts = xcalloc(*numprompts, sizeof(char *)); 712 *echo_on = xcalloc(*numprompts, sizeof(u_int)); 713 (*echo_on)[0] = 0; 714 } 715 716 #ifdef BSD_AUTH 717 int 718 mm_bsdauth_query(void *ctx, char **name, char **infotxt, 719 u_int *numprompts, char ***prompts, u_int **echo_on) 720 { 721 Buffer m; 722 u_int success; 723 char *challenge; 724 725 debug3("%s: entering", __func__); 726 727 buffer_init(&m); 728 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); 729 730 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, 731 &m); 732 success = buffer_get_int(&m); 733 if (success == 0) { 734 debug3("%s: no challenge", __func__); 735 buffer_free(&m); 736 return (-1); 737 } 738 739 /* Get the challenge, and format the response */ 740 challenge = buffer_get_string(&m, NULL); 741 buffer_free(&m); 742 743 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 744 (*prompts)[0] = challenge; 745 746 debug3("%s: received challenge: %s", __func__, challenge); 747 748 return (0); 749 } 750 751 int 752 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) 753 { 754 Buffer m; 755 int authok; 756 757 debug3("%s: entering", __func__); 758 if (numresponses != 1) 759 return (-1); 760 761 buffer_init(&m); 762 buffer_put_cstring(&m, responses[0]); 763 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); 764 765 mm_request_receive_expect(pmonitor->m_recvfd, 766 MONITOR_ANS_BSDAUTHRESPOND, &m); 767 768 authok = buffer_get_int(&m); 769 buffer_free(&m); 770 771 return ((authok == 0) ? -1 : 0); 772 } 773 #endif 774 775 #ifdef SKEY 776 int 777 mm_skey_query(void *ctx, char **name, char **infotxt, 778 u_int *numprompts, char ***prompts, u_int **echo_on) 779 { 780 Buffer m; 781 u_int success; 782 char *challenge; 783 784 debug3("%s: entering", __func__); 785 786 buffer_init(&m); 787 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); 788 789 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, 790 &m); 791 success = buffer_get_int(&m); 792 if (success == 0) { 793 debug3("%s: no challenge", __func__); 794 buffer_free(&m); 795 return (-1); 796 } 797 798 /* Get the challenge, and format the response */ 799 challenge = buffer_get_string(&m, NULL); 800 buffer_free(&m); 801 802 debug3("%s: received challenge: %s", __func__, challenge); 803 804 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 805 806 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT); 807 free(challenge); 808 809 return (0); 810 } 811 812 int 813 mm_skey_respond(void *ctx, u_int numresponses, char **responses) 814 { 815 Buffer m; 816 int authok; 817 818 debug3("%s: entering", __func__); 819 if (numresponses != 1) 820 return (-1); 821 822 buffer_init(&m); 823 buffer_put_cstring(&m, responses[0]); 824 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); 825 826 mm_request_receive_expect(pmonitor->m_recvfd, 827 MONITOR_ANS_SKEYRESPOND, &m); 828 829 authok = buffer_get_int(&m); 830 buffer_free(&m); 831 832 return ((authok == 0) ? -1 : 0); 833 } 834 #endif /* SKEY */ 835 #endif /* BSDAUTH || SKEY */ 836 837 #ifdef GSSAPI 838 OM_uint32 839 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) 840 { 841 Buffer m; 842 OM_uint32 major; 843 844 /* Client doesn't get to see the context */ 845 *ctx = NULL; 846 847 buffer_init(&m); 848 buffer_put_string(&m, goid->elements, goid->length); 849 850 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m); 851 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m); 852 853 major = buffer_get_int(&m); 854 855 buffer_free(&m); 856 return (major); 857 } 858 859 OM_uint32 860 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, 861 gss_buffer_desc *out, OM_uint32 *flags) 862 { 863 Buffer m; 864 OM_uint32 major; 865 u_int len; 866 867 buffer_init(&m); 868 buffer_put_string(&m, in->value, in->length); 869 870 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m); 871 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m); 872 873 major = buffer_get_int(&m); 874 out->value = buffer_get_string(&m, &len); 875 out->length = len; 876 if (flags) 877 *flags = buffer_get_int(&m); 878 879 buffer_free(&m); 880 881 return (major); 882 } 883 884 OM_uint32 885 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) 886 { 887 Buffer m; 888 OM_uint32 major; 889 890 buffer_init(&m); 891 buffer_put_string(&m, gssbuf->value, gssbuf->length); 892 buffer_put_string(&m, gssmic->value, gssmic->length); 893 894 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m); 895 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC, 896 &m); 897 898 major = buffer_get_int(&m); 899 buffer_free(&m); 900 return(major); 901 } 902 903 int 904 mm_ssh_gssapi_userok(char *user) 905 { 906 Buffer m; 907 int authenticated = 0; 908 909 buffer_init(&m); 910 911 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m); 912 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK, 913 &m); 914 915 authenticated = buffer_get_int(&m); 916 917 buffer_free(&m); 918 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); 919 return (authenticated); 920 } 921 #endif /* GSSAPI */ 922 923 #ifdef KRB5 924 int 925 mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp) 926 { 927 krb5_data *tkt, *reply; 928 Buffer m; 929 int success; 930 931 debug3("%s entering", __func__); 932 tkt = (krb5_data *) argp; 933 reply = (krb5_data *) resp; 934 935 buffer_init(&m); 936 buffer_put_string(&m, tkt->data, tkt->length); 937 938 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m); 939 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m); 940 941 success = buffer_get_int(&m); 942 if (success) { 943 u_int len; 944 945 *userp = buffer_get_string(&m, NULL); 946 reply->data = buffer_get_string(&m, &len); 947 reply->length = len; 948 } else { 949 memset(reply, 0, sizeof(*reply)); 950 *userp = NULL; 951 } 952 953 buffer_free(&m); 954 return (success); 955 } 956 #endif 957