1/* $OpenBSD: monitor.c,v 1.150 2015/06/22 23:42:16 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/socket.h> 32#include "openbsd-compat/sys-tree.h" 33#include <sys/wait.h> 34 35#include <errno.h> 36#include <fcntl.h> 37#ifdef HAVE_PATHS_H 38#include <paths.h> 39#endif 40#include <pwd.h> 41#include <signal.h> 42#ifdef HAVE_STDINT_H 43#include <stdint.h> 44#endif 45#include <stdlib.h> 46#include <string.h> 47#include <stdarg.h> 48#include <stdio.h> 49#include <unistd.h> 50#ifdef HAVE_POLL_H 51#include <poll.h> 52#else 53# ifdef HAVE_SYS_POLL_H 54# include <sys/poll.h> 55# endif 56#endif 57 58#ifdef SKEY 59#include <skey.h> 60#endif 61 62#ifdef WITH_OPENSSL 63#include <openssl/dh.h> 64#endif 65 66#include "openbsd-compat/sys-queue.h" 67#include "atomicio.h" 68#include "xmalloc.h" 69#include "ssh.h" 70#include "key.h" 71#include "buffer.h" 72#include "hostfile.h" 73#include "auth.h" 74#include "cipher.h" 75#include "kex.h" 76#include "dh.h" 77#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ 78#undef TARGET_OS_MAC 79#include "zlib.h" 80#define TARGET_OS_MAC 1 81#else 82#include "zlib.h" 83#endif 84#include "packet.h" 85#include "auth-options.h" 86#include "sshpty.h" 87#include "channels.h" 88#include "session.h" 89#include "sshlogin.h" 90#include "canohost.h" 91#include "log.h" 92#include "misc.h" 93#include "servconf.h" 94#include "monitor.h" 95#include "monitor_mm.h" 96#ifdef GSSAPI 97#include "ssh-gss.h" 98#endif 99#include "monitor_wrap.h" 100#include "monitor_fdpass.h" 101#include "compat.h" 102#include "ssh2.h" 103#include "roaming.h" 104#include "authfd.h" 105#include "match.h" 106#include "ssherr.h" 107 108#ifdef GSSAPI 109static Gssctxt *gsscontext = NULL; 110#endif 111 112/* Imports */ 113extern ServerOptions options; 114extern u_int utmp_len; 115extern u_char session_id[]; 116extern Buffer auth_debug; 117extern int auth_debug_init; 118extern Buffer loginmsg; 119 120/* State exported from the child */ 121static struct sshbuf *child_state; 122 123/* Functions on the monitor that answer unprivileged requests */ 124 125int mm_answer_moduli(int, Buffer *); 126int mm_answer_sign(int, Buffer *); 127int mm_answer_pwnamallow(int, Buffer *); 128int mm_answer_auth2_read_banner(int, Buffer *); 129int mm_answer_authserv(int, Buffer *); 130int mm_answer_authpassword(int, Buffer *); 131int mm_answer_bsdauthquery(int, Buffer *); 132int mm_answer_bsdauthrespond(int, Buffer *); 133int mm_answer_skeyquery(int, Buffer *); 134int mm_answer_skeyrespond(int, Buffer *); 135int mm_answer_keyallowed(int, Buffer *); 136int mm_answer_keyverify(int, Buffer *); 137int mm_answer_pty(int, Buffer *); 138int mm_answer_pty_cleanup(int, Buffer *); 139int mm_answer_term(int, Buffer *); 140int mm_answer_rsa_keyallowed(int, Buffer *); 141int mm_answer_rsa_challenge(int, Buffer *); 142int mm_answer_rsa_response(int, Buffer *); 143int mm_answer_sesskey(int, Buffer *); 144int mm_answer_sessid(int, Buffer *); 145 146#ifdef USE_PAM 147int mm_answer_pam_start(int, Buffer *); 148int mm_answer_pam_account(int, Buffer *); 149int mm_answer_pam_init_ctx(int, Buffer *); 150int mm_answer_pam_query(int, Buffer *); 151int mm_answer_pam_respond(int, Buffer *); 152int mm_answer_pam_free_ctx(int, Buffer *); 153#endif 154 155#ifdef GSSAPI 156int mm_answer_gss_setup_ctx(int, Buffer *); 157int mm_answer_gss_accept_ctx(int, Buffer *); 158int mm_answer_gss_userok(int, Buffer *); 159int mm_answer_gss_checkmic(int, Buffer *); 160#endif 161 162#ifdef SSH_AUDIT_EVENTS 163int mm_answer_audit_event(int, Buffer *); 164int mm_answer_audit_command(int, Buffer *); 165#endif 166 167static int monitor_read_log(struct monitor *); 168 169static Authctxt *authctxt; 170 171#ifdef WITH_SSH1 172static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */ 173#endif 174 175/* local state for key verify */ 176static u_char *key_blob = NULL; 177static u_int key_bloblen = 0; 178static int key_blobtype = MM_NOKEY; 179static char *hostbased_cuser = NULL; 180static char *hostbased_chost = NULL; 181static char *auth_method = "unknown"; 182static char *auth_submethod = NULL; 183static u_int session_id2_len = 0; 184static u_char *session_id2 = NULL; 185static pid_t monitor_child_pid; 186 187struct mon_table { 188 enum monitor_reqtype type; 189 int flags; 190 int (*f)(int, Buffer *); 191}; 192 193#define MON_ISAUTH 0x0004 /* Required for Authentication */ 194#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ 195#define MON_ONCE 0x0010 /* Disable after calling */ 196#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ 197 198#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) 199 200#define MON_PERMIT 0x1000 /* Request is permitted */ 201 202struct mon_table mon_dispatch_proto20[] = { 203#ifdef WITH_OPENSSL 204 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, 205#endif 206 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, 207 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, 208 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, 209 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, 210 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 211#ifdef USE_PAM 212 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, 213 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, 214 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, 215 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, 216 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, 217 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, 218#endif 219#ifdef SSH_AUDIT_EVENTS 220 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 221#endif 222#ifdef BSD_AUTH 223 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 224 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, 225#endif 226#ifdef SKEY 227 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, 228 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, 229#endif 230 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, 231 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, 232#ifdef GSSAPI 233 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, 234 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx}, 235 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok}, 236 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic}, 237#endif 238 {0, 0, NULL} 239}; 240 241struct mon_table mon_dispatch_postauth20[] = { 242#ifdef WITH_OPENSSL 243 {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, 244#endif 245 {MONITOR_REQ_SIGN, 0, mm_answer_sign}, 246 {MONITOR_REQ_PTY, 0, mm_answer_pty}, 247 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, 248 {MONITOR_REQ_TERM, 0, mm_answer_term}, 249#ifdef SSH_AUDIT_EVENTS 250 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 251 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command}, 252#endif 253 {0, 0, NULL} 254}; 255 256struct mon_table mon_dispatch_proto15[] = { 257#ifdef WITH_SSH1 258 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, 259 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey}, 260 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid}, 261 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 262 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed}, 263 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed}, 264 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge}, 265 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response}, 266#ifdef BSD_AUTH 267 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 268 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, 269#endif 270#ifdef SKEY 271 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, 272 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, 273#endif 274#ifdef USE_PAM 275 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, 276 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, 277 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, 278 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, 279 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, 280 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, 281#endif 282#ifdef SSH_AUDIT_EVENTS 283 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 284#endif 285#endif /* WITH_SSH1 */ 286 {0, 0, NULL} 287}; 288 289struct mon_table mon_dispatch_postauth15[] = { 290#ifdef WITH_SSH1 291 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty}, 292 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup}, 293 {MONITOR_REQ_TERM, 0, mm_answer_term}, 294#ifdef SSH_AUDIT_EVENTS 295 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, 296 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command}, 297#endif 298#endif /* WITH_SSH1 */ 299 {0, 0, NULL} 300}; 301 302struct mon_table *mon_dispatch; 303 304/* Specifies if a certain message is allowed at the moment */ 305 306static void 307monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) 308{ 309 while (ent->f != NULL) { 310 if (ent->type == type) { 311 ent->flags &= ~MON_PERMIT; 312 ent->flags |= permit ? MON_PERMIT : 0; 313 return; 314 } 315 ent++; 316 } 317} 318 319static void 320monitor_permit_authentications(int permit) 321{ 322 struct mon_table *ent = mon_dispatch; 323 324 while (ent->f != NULL) { 325 if (ent->flags & MON_AUTH) { 326 ent->flags &= ~MON_PERMIT; 327 ent->flags |= permit ? MON_PERMIT : 0; 328 } 329 ent++; 330 } 331} 332 333void 334monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) 335{ 336 struct mon_table *ent; 337 int authenticated = 0, partial = 0; 338 339 debug3("preauth child monitor started"); 340 341 close(pmonitor->m_recvfd); 342 close(pmonitor->m_log_sendfd); 343 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; 344 345 authctxt = _authctxt; 346 memset(authctxt, 0, sizeof(*authctxt)); 347 348 authctxt->loginmsg = &loginmsg; 349 350 if (compat20) { 351 mon_dispatch = mon_dispatch_proto20; 352 353 /* Permit requests for moduli and signatures */ 354 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 355 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 356 } else { 357 mon_dispatch = mon_dispatch_proto15; 358 359 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); 360 } 361 362 /* The first few requests do not require asynchronous access */ 363 while (!authenticated) { 364 partial = 0; 365 auth_method = "unknown"; 366 auth_submethod = NULL; 367 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); 368 369 /* Special handling for multiple required authentications */ 370 if (options.num_auth_methods != 0) { 371 if (!compat20) 372 fatal("AuthenticationMethods is not supported" 373 "with SSH protocol 1"); 374 if (authenticated && 375 !auth2_update_methods_lists(authctxt, 376 auth_method, auth_submethod)) { 377 debug3("%s: method %s: partial", __func__, 378 auth_method); 379 authenticated = 0; 380 partial = 1; 381 } 382 } 383 384 if (authenticated) { 385 if (!(ent->flags & MON_AUTHDECIDE)) 386 fatal("%s: unexpected authentication from %d", 387 __func__, ent->type); 388 if (authctxt->pw->pw_uid == 0 && 389 !auth_root_allowed(auth_method)) 390 authenticated = 0; 391#ifdef USE_PAM 392 /* PAM needs to perform account checks after auth */ 393 if (options.use_pam && authenticated) { 394 Buffer m; 395 396 buffer_init(&m); 397 mm_request_receive_expect(pmonitor->m_sendfd, 398 MONITOR_REQ_PAM_ACCOUNT, &m); 399 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m); 400 buffer_free(&m); 401 } 402#endif 403 } 404 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { 405 auth_log(authctxt, authenticated, partial, 406 auth_method, auth_submethod); 407 if (!partial && !authenticated) 408 authctxt->failures++; 409 } 410 } 411 412 if (!authctxt->valid) 413 fatal("%s: authenticated invalid user", __func__); 414 if (strcmp(auth_method, "unknown") == 0) 415 fatal("%s: authentication method name unknown", __func__); 416 417 debug("%s: %s has been authenticated by privileged process", 418 __func__, authctxt->user); 419 420 mm_get_keystate(pmonitor); 421 422 /* Drain any buffered messages from the child */ 423 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) 424 ; 425 426 close(pmonitor->m_sendfd); 427 close(pmonitor->m_log_recvfd); 428 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; 429} 430 431static void 432monitor_set_child_handler(pid_t pid) 433{ 434 monitor_child_pid = pid; 435} 436 437static void 438monitor_child_handler(int sig) 439{ 440 kill(monitor_child_pid, sig); 441} 442 443void 444monitor_child_postauth(struct monitor *pmonitor) 445{ 446 close(pmonitor->m_recvfd); 447 pmonitor->m_recvfd = -1; 448 449 monitor_set_child_handler(pmonitor->m_pid); 450 signal(SIGHUP, &monitor_child_handler); 451 signal(SIGTERM, &monitor_child_handler); 452 signal(SIGINT, &monitor_child_handler); 453#ifdef SIGXFSZ 454 signal(SIGXFSZ, SIG_IGN); 455#endif 456 457 if (compat20) { 458 mon_dispatch = mon_dispatch_postauth20; 459 460 /* Permit requests for moduli and signatures */ 461 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 462 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 463 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 464 } else { 465 mon_dispatch = mon_dispatch_postauth15; 466 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 467 } 468 if (!no_pty_flag) { 469 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); 470 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); 471 } 472 473 for (;;) 474 monitor_read(pmonitor, mon_dispatch, NULL); 475} 476 477void 478monitor_sync(struct monitor *pmonitor) 479{ 480 if (options.compression) { 481 /* The member allocation is not visible, so sync it */ 482 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback); 483 } 484} 485 486/* Allocation functions for zlib */ 487static void * 488mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) 489{ 490 size_t len = (size_t) size * ncount; 491 void *address; 492 493 if (len == 0 || ncount > SIZE_MAX / size) 494 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); 495 496 address = mm_malloc(mm, len); 497 498 return (address); 499} 500 501static void 502mm_zfree(struct mm_master *mm, void *address) 503{ 504 mm_free(mm, address); 505} 506 507static int 508monitor_read_log(struct monitor *pmonitor) 509{ 510 Buffer logmsg; 511 u_int len, level; 512 char *msg; 513 514 buffer_init(&logmsg); 515 516 /* Read length */ 517 buffer_append_space(&logmsg, 4); 518 if (atomicio(read, pmonitor->m_log_recvfd, 519 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) { 520 if (errno == EPIPE) { 521 buffer_free(&logmsg); 522 debug("%s: child log fd closed", __func__); 523 close(pmonitor->m_log_recvfd); 524 pmonitor->m_log_recvfd = -1; 525 return -1; 526 } 527 fatal("%s: log fd read: %s", __func__, strerror(errno)); 528 } 529 len = buffer_get_int(&logmsg); 530 if (len <= 4 || len > 8192) 531 fatal("%s: invalid log message length %u", __func__, len); 532 533 /* Read severity, message */ 534 buffer_clear(&logmsg); 535 buffer_append_space(&logmsg, len); 536 if (atomicio(read, pmonitor->m_log_recvfd, 537 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) 538 fatal("%s: log fd read: %s", __func__, strerror(errno)); 539 540 /* Log it */ 541 level = buffer_get_int(&logmsg); 542 msg = buffer_get_string(&logmsg, NULL); 543 if (log_level_name(level) == NULL) 544 fatal("%s: invalid log level %u (corrupted message?)", 545 __func__, level); 546 do_log2(level, "%s [preauth]", msg); 547 548 buffer_free(&logmsg); 549 free(msg); 550 551 return 0; 552} 553 554int 555monitor_read(struct monitor *pmonitor, struct mon_table *ent, 556 struct mon_table **pent) 557{ 558 Buffer m; 559 int ret; 560 u_char type; 561 struct pollfd pfd[2]; 562 563 for (;;) { 564 memset(&pfd, 0, sizeof(pfd)); 565 pfd[0].fd = pmonitor->m_sendfd; 566 pfd[0].events = POLLIN; 567 pfd[1].fd = pmonitor->m_log_recvfd; 568 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; 569 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { 570 if (errno == EINTR || errno == EAGAIN) 571 continue; 572 fatal("%s: poll: %s", __func__, strerror(errno)); 573 } 574 if (pfd[1].revents) { 575 /* 576 * Drain all log messages before processing next 577 * monitor request. 578 */ 579 monitor_read_log(pmonitor); 580 continue; 581 } 582 if (pfd[0].revents) 583 break; /* Continues below */ 584 } 585 586 buffer_init(&m); 587 588 mm_request_receive(pmonitor->m_sendfd, &m); 589 type = buffer_get_char(&m); 590 591 debug3("%s: checking request %d", __func__, type); 592 593 while (ent->f != NULL) { 594 if (ent->type == type) 595 break; 596 ent++; 597 } 598 599 if (ent->f != NULL) { 600 if (!(ent->flags & MON_PERMIT)) 601 fatal("%s: unpermitted request %d", __func__, 602 type); 603 ret = (*ent->f)(pmonitor->m_sendfd, &m); 604 buffer_free(&m); 605 606 /* The child may use this request only once, disable it */ 607 if (ent->flags & MON_ONCE) { 608 debug2("%s: %d used once, disabling now", __func__, 609 type); 610 ent->flags &= ~MON_PERMIT; 611 } 612 613 if (pent != NULL) 614 *pent = ent; 615 616 return ret; 617 } 618 619 fatal("%s: unsupported request: %d", __func__, type); 620 621 /* NOTREACHED */ 622 return (-1); 623} 624 625/* allowed key state */ 626static int 627monitor_allowed_key(u_char *blob, u_int bloblen) 628{ 629 /* make sure key is allowed */ 630 if (key_blob == NULL || key_bloblen != bloblen || 631 timingsafe_bcmp(key_blob, blob, key_bloblen)) 632 return (0); 633 return (1); 634} 635 636static void 637monitor_reset_key_state(void) 638{ 639 /* reset state */ 640 free(key_blob); 641 free(hostbased_cuser); 642 free(hostbased_chost); 643 key_blob = NULL; 644 key_bloblen = 0; 645 key_blobtype = MM_NOKEY; 646 hostbased_cuser = NULL; 647 hostbased_chost = NULL; 648} 649 650#ifdef WITH_OPENSSL 651int 652mm_answer_moduli(int sock, Buffer *m) 653{ 654 DH *dh; 655 int min, want, max; 656 657 min = buffer_get_int(m); 658 want = buffer_get_int(m); 659 max = buffer_get_int(m); 660 661 debug3("%s: got parameters: %d %d %d", 662 __func__, min, want, max); 663 /* We need to check here, too, in case the child got corrupted */ 664 if (max < min || want < min || max < want) 665 fatal("%s: bad parameters: %d %d %d", 666 __func__, min, want, max); 667 668 buffer_clear(m); 669 670 dh = choose_dh(min, want, max); 671 if (dh == NULL) { 672 buffer_put_char(m, 0); 673 return (0); 674 } else { 675 /* Send first bignum */ 676 buffer_put_char(m, 1); 677 buffer_put_bignum2(m, dh->p); 678 buffer_put_bignum2(m, dh->g); 679 680 DH_free(dh); 681 } 682 mm_request_send(sock, MONITOR_ANS_MODULI, m); 683 return (0); 684} 685#endif 686 687int 688mm_answer_sign(int sock, Buffer *m) 689{ 690 struct ssh *ssh = active_state; /* XXX */ 691 extern int auth_sock; /* XXX move to state struct? */ 692 struct sshkey *key; 693 struct sshbuf *sigbuf; 694 u_char *p; 695 u_char *signature; 696 size_t datlen, siglen; 697 int r, keyid, is_proof = 0; 698 const char proof_req[] = "hostkeys-prove-00@openssh.com"; 699 700 debug3("%s", __func__); 701 702 if ((r = sshbuf_get_u32(m, &keyid)) != 0 || 703 (r = sshbuf_get_string(m, &p, &datlen)) != 0) 704 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 705 706 /* 707 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), 708 * SHA384 (48 bytes) and SHA512 (64 bytes). 709 * 710 * Otherwise, verify the signature request is for a hostkey 711 * proof. 712 * 713 * XXX perform similar check for KEX signature requests too? 714 * it's not trivial, since what is signed is the hash, rather 715 * than the full kex structure... 716 */ 717 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { 718 /* 719 * Construct expected hostkey proof and compare it to what 720 * the client sent us. 721 */ 722 if (session_id2_len == 0) /* hostkeys is never first */ 723 fatal("%s: bad data length: %zu", __func__, datlen); 724 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) 725 fatal("%s: no hostkey for index %d", __func__, keyid); 726 if ((sigbuf = sshbuf_new()) == NULL) 727 fatal("%s: sshbuf_new", __func__); 728 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || 729 (r = sshbuf_put_string(sigbuf, session_id2, 730 session_id2_len) != 0) || 731 (r = sshkey_puts(key, sigbuf)) != 0) 732 fatal("%s: couldn't prepare private key " 733 "proof buffer: %s", __func__, ssh_err(r)); 734 if (datlen != sshbuf_len(sigbuf) || 735 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) 736 fatal("%s: bad data length: %zu, hostkey proof len %zu", 737 __func__, datlen, sshbuf_len(sigbuf)); 738 sshbuf_free(sigbuf); 739 is_proof = 1; 740 } 741 742 /* save session id, it will be passed on the first call */ 743 if (session_id2_len == 0) { 744 session_id2_len = datlen; 745 session_id2 = xmalloc(session_id2_len); 746 memcpy(session_id2, p, session_id2_len); 747 } 748 749 if ((key = get_hostkey_by_index(keyid)) != NULL) { 750 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, 751 datafellows)) != 0) 752 fatal("%s: sshkey_sign failed: %s", 753 __func__, ssh_err(r)); 754 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && 755 auth_sock > 0) { 756 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, 757 p, datlen, datafellows)) != 0) { 758 fatal("%s: ssh_agent_sign failed: %s", 759 __func__, ssh_err(r)); 760 } 761 } else 762 fatal("%s: no hostkey from index %d", __func__, keyid); 763 764 debug3("%s: %s signature %p(%zu)", __func__, 765 is_proof ? "KEX" : "hostkey proof", signature, siglen); 766 767 sshbuf_reset(m); 768 if ((r = sshbuf_put_string(m, signature, siglen)) != 0) 769 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 770 771 free(p); 772 free(signature); 773 774 mm_request_send(sock, MONITOR_ANS_SIGN, m); 775 776 /* Turn on permissions for getpwnam */ 777 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 778 779 return (0); 780} 781 782/* Retrieves the password entry and also checks if the user is permitted */ 783 784int 785mm_answer_pwnamallow(int sock, Buffer *m) 786{ 787 char *username; 788 struct passwd *pwent; 789 int allowed = 0; 790 u_int i; 791 792 debug3("%s", __func__); 793 794 if (authctxt->attempt++ != 0) 795 fatal("%s: multiple attempts for getpwnam", __func__); 796 797 username = buffer_get_string(m, NULL); 798 799 pwent = getpwnamallow(username); 800 801 authctxt->user = xstrdup(username); 802 setproctitle("%s [priv]", pwent ? username : "unknown"); 803 free(username); 804 805 buffer_clear(m); 806 807 if (pwent == NULL) { 808 buffer_put_char(m, 0); 809 authctxt->pw = fakepw(); 810 goto out; 811 } 812 813 allowed = 1; 814 authctxt->pw = pwent; 815 authctxt->valid = 1; 816 817 buffer_put_char(m, 1); 818 buffer_put_string(m, pwent, sizeof(struct passwd)); 819 buffer_put_cstring(m, pwent->pw_name); 820 buffer_put_cstring(m, "*"); 821#ifdef HAVE_STRUCT_PASSWD_PW_GECOS 822 buffer_put_cstring(m, pwent->pw_gecos); 823#endif 824#ifdef HAVE_STRUCT_PASSWD_PW_CLASS 825 buffer_put_cstring(m, pwent->pw_class); 826#endif 827 buffer_put_cstring(m, pwent->pw_dir); 828 buffer_put_cstring(m, pwent->pw_shell); 829 830 out: 831 buffer_put_string(m, &options, sizeof(options)); 832 833#define M_CP_STROPT(x) do { \ 834 if (options.x != NULL) \ 835 buffer_put_cstring(m, options.x); \ 836 } while (0) 837#define M_CP_STRARRAYOPT(x, nx) do { \ 838 for (i = 0; i < options.nx; i++) \ 839 buffer_put_cstring(m, options.x[i]); \ 840 } while (0) 841 /* See comment in servconf.h */ 842 COPY_MATCH_STRING_OPTS(); 843#undef M_CP_STROPT 844#undef M_CP_STRARRAYOPT 845 846 /* Create valid auth method lists */ 847 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) { 848 /* 849 * The monitor will continue long enough to let the child 850 * run to it's packet_disconnect(), but it must not allow any 851 * authentication to succeed. 852 */ 853 debug("%s: no valid authentication method lists", __func__); 854 } 855 856 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); 857 mm_request_send(sock, MONITOR_ANS_PWNAM, m); 858 859 /* For SSHv1 allow authentication now */ 860 if (!compat20) 861 monitor_permit_authentications(1); 862 else { 863 /* Allow service/style information on the auth context */ 864 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); 865 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); 866 } 867#ifdef USE_PAM 868 if (options.use_pam) 869 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); 870#endif 871 872 return (0); 873} 874 875int mm_answer_auth2_read_banner(int sock, Buffer *m) 876{ 877 char *banner; 878 879 buffer_clear(m); 880 banner = auth2_read_banner(); 881 buffer_put_cstring(m, banner != NULL ? banner : ""); 882 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); 883 free(banner); 884 885 return (0); 886} 887 888int 889mm_answer_authserv(int sock, Buffer *m) 890{ 891 monitor_permit_authentications(1); 892 893 authctxt->service = buffer_get_string(m, NULL); 894 authctxt->style = buffer_get_string(m, NULL); 895 debug3("%s: service=%s, style=%s", 896 __func__, authctxt->service, authctxt->style); 897 898 if (strlen(authctxt->style) == 0) { 899 free(authctxt->style); 900 authctxt->style = NULL; 901 } 902 903 return (0); 904} 905 906int 907mm_answer_authpassword(int sock, Buffer *m) 908{ 909 static int call_count; 910 char *passwd; 911 int authenticated; 912 u_int plen; 913 914 passwd = buffer_get_string(m, &plen); 915#if !defined(ANDROID) 916 /* Only authenticate if the context is valid */ 917 authenticated = options.password_authentication && 918 auth_password(authctxt, passwd); 919#else 920 /* no password authentication in Android. */ 921 authenticated = 0; 922#endif 923 explicit_bzero(passwd, strlen(passwd)); 924 free(passwd); 925 926 buffer_clear(m); 927 buffer_put_int(m, authenticated); 928 929 debug3("%s: sending result %d", __func__, authenticated); 930 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); 931 932 call_count++; 933 if (plen == 0 && call_count == 1) 934 auth_method = "none"; 935 else 936 auth_method = "password"; 937 938 /* Causes monitor loop to terminate if authenticated */ 939 return (authenticated); 940} 941 942#ifdef BSD_AUTH 943int 944mm_answer_bsdauthquery(int sock, Buffer *m) 945{ 946 char *name, *infotxt; 947 u_int numprompts; 948 u_int *echo_on; 949 char **prompts; 950 u_int success; 951 952 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 953 &prompts, &echo_on) < 0 ? 0 : 1; 954 955 buffer_clear(m); 956 buffer_put_int(m, success); 957 if (success) 958 buffer_put_cstring(m, prompts[0]); 959 960 debug3("%s: sending challenge success: %u", __func__, success); 961 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); 962 963 if (success) { 964 free(name); 965 free(infotxt); 966 free(prompts); 967 free(echo_on); 968 } 969 970 return (0); 971} 972 973int 974mm_answer_bsdauthrespond(int sock, Buffer *m) 975{ 976 char *response; 977 int authok; 978 979 if (authctxt->as == 0) 980 fatal("%s: no bsd auth session", __func__); 981 982 response = buffer_get_string(m, NULL); 983 authok = options.challenge_response_authentication && 984 auth_userresponse(authctxt->as, response, 0); 985 authctxt->as = NULL; 986 debug3("%s: <%s> = <%d>", __func__, response, authok); 987 free(response); 988 989 buffer_clear(m); 990 buffer_put_int(m, authok); 991 992 debug3("%s: sending authenticated: %d", __func__, authok); 993 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); 994 995 if (compat20) { 996 auth_method = "keyboard-interactive"; 997 auth_submethod = "bsdauth"; 998 } else 999 auth_method = "bsdauth"; 1000 1001 return (authok != 0); 1002} 1003#endif 1004 1005#ifdef SKEY 1006int 1007mm_answer_skeyquery(int sock, Buffer *m) 1008{ 1009 struct skey skey; 1010 char challenge[1024]; 1011 u_int success; 1012 1013 success = _compat_skeychallenge(&skey, authctxt->user, challenge, 1014 sizeof(challenge)) < 0 ? 0 : 1; 1015 1016 buffer_clear(m); 1017 buffer_put_int(m, success); 1018 if (success) 1019 buffer_put_cstring(m, challenge); 1020 1021 debug3("%s: sending challenge success: %u", __func__, success); 1022 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m); 1023 1024 return (0); 1025} 1026 1027int 1028mm_answer_skeyrespond(int sock, Buffer *m) 1029{ 1030 char *response; 1031 int authok; 1032 1033 response = buffer_get_string(m, NULL); 1034 1035 authok = (options.challenge_response_authentication && 1036 authctxt->valid && 1037 skey_haskey(authctxt->pw->pw_name) == 0 && 1038 skey_passcheck(authctxt->pw->pw_name, response) != -1); 1039 1040 free(response); 1041 1042 buffer_clear(m); 1043 buffer_put_int(m, authok); 1044 1045 debug3("%s: sending authenticated: %d", __func__, authok); 1046 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m); 1047 1048 auth_method = "skey"; 1049 1050 return (authok != 0); 1051} 1052#endif 1053 1054#ifdef USE_PAM 1055int 1056mm_answer_pam_start(int sock, Buffer *m) 1057{ 1058 if (!options.use_pam) 1059 fatal("UsePAM not set, but ended up in %s anyway", __func__); 1060 1061 start_pam(authctxt); 1062 1063 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); 1064 1065 return (0); 1066} 1067 1068int 1069mm_answer_pam_account(int sock, Buffer *m) 1070{ 1071 u_int ret; 1072 1073 if (!options.use_pam) 1074 fatal("UsePAM not set, but ended up in %s anyway", __func__); 1075 1076 ret = do_pam_account(); 1077 1078 buffer_put_int(m, ret); 1079 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); 1080 1081 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); 1082 1083 return (ret); 1084} 1085 1086static void *sshpam_ctxt, *sshpam_authok; 1087extern KbdintDevice sshpam_device; 1088 1089int 1090mm_answer_pam_init_ctx(int sock, Buffer *m) 1091{ 1092 debug3("%s", __func__); 1093 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); 1094 sshpam_authok = NULL; 1095 buffer_clear(m); 1096 if (sshpam_ctxt != NULL) { 1097 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); 1098 buffer_put_int(m, 1); 1099 } else { 1100 buffer_put_int(m, 0); 1101 } 1102 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); 1103 return (0); 1104} 1105 1106int 1107mm_answer_pam_query(int sock, Buffer *m) 1108{ 1109 char *name = NULL, *info = NULL, **prompts = NULL; 1110 u_int i, num = 0, *echo_on = 0; 1111 int ret; 1112 1113 debug3("%s", __func__); 1114 sshpam_authok = NULL; 1115 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); 1116 if (ret == 0 && num == 0) 1117 sshpam_authok = sshpam_ctxt; 1118 if (num > 1 || name == NULL || info == NULL) 1119 ret = -1; 1120 buffer_clear(m); 1121 buffer_put_int(m, ret); 1122 buffer_put_cstring(m, name); 1123 free(name); 1124 buffer_put_cstring(m, info); 1125 free(info); 1126 buffer_put_int(m, num); 1127 for (i = 0; i < num; ++i) { 1128 buffer_put_cstring(m, prompts[i]); 1129 free(prompts[i]); 1130 buffer_put_int(m, echo_on[i]); 1131 } 1132 free(prompts); 1133 free(echo_on); 1134 auth_method = "keyboard-interactive"; 1135 auth_submethod = "pam"; 1136 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); 1137 return (0); 1138} 1139 1140int 1141mm_answer_pam_respond(int sock, Buffer *m) 1142{ 1143 char **resp; 1144 u_int i, num; 1145 int ret; 1146 1147 debug3("%s", __func__); 1148 sshpam_authok = NULL; 1149 num = buffer_get_int(m); 1150 if (num > 0) { 1151 resp = xcalloc(num, sizeof(char *)); 1152 for (i = 0; i < num; ++i) 1153 resp[i] = buffer_get_string(m, NULL); 1154 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); 1155 for (i = 0; i < num; ++i) 1156 free(resp[i]); 1157 free(resp); 1158 } else { 1159 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); 1160 } 1161 buffer_clear(m); 1162 buffer_put_int(m, ret); 1163 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); 1164 auth_method = "keyboard-interactive"; 1165 auth_submethod = "pam"; 1166 if (ret == 0) 1167 sshpam_authok = sshpam_ctxt; 1168 return (0); 1169} 1170 1171int 1172mm_answer_pam_free_ctx(int sock, Buffer *m) 1173{ 1174 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt; 1175 1176 debug3("%s", __func__); 1177 (sshpam_device.free_ctx)(sshpam_ctxt); 1178 sshpam_ctxt = sshpam_authok = NULL; 1179 buffer_clear(m); 1180 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); 1181 auth_method = "keyboard-interactive"; 1182 auth_submethod = "pam"; 1183 return r; 1184} 1185#endif 1186 1187int 1188mm_answer_keyallowed(int sock, Buffer *m) 1189{ 1190 Key *key; 1191 char *cuser, *chost; 1192 u_char *blob; 1193 u_int bloblen, pubkey_auth_attempt; 1194 enum mm_keytype type = 0; 1195 int allowed = 0; 1196 1197 debug3("%s entering", __func__); 1198 1199 type = buffer_get_int(m); 1200 cuser = buffer_get_string(m, NULL); 1201 chost = buffer_get_string(m, NULL); 1202 blob = buffer_get_string(m, &bloblen); 1203 pubkey_auth_attempt = buffer_get_int(m); 1204 1205 key = key_from_blob(blob, bloblen); 1206 1207 if ((compat20 && type == MM_RSAHOSTKEY) || 1208 (!compat20 && type != MM_RSAHOSTKEY)) 1209 fatal("%s: key type and protocol mismatch", __func__); 1210 1211 debug3("%s: key_from_blob: %p", __func__, key); 1212 1213 if (key != NULL && authctxt->valid) { 1214 /* These should not make it past the privsep child */ 1215 if (key_type_plain(key->type) == KEY_RSA && 1216 (datafellows & SSH_BUG_RSASIGMD5) != 0) 1217 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); 1218 1219 switch (type) { 1220 case MM_USERKEY: 1221 allowed = options.pubkey_authentication && 1222 !auth2_userkey_already_used(authctxt, key) && 1223 match_pattern_list(sshkey_ssh_name(key), 1224 options.pubkey_key_types, 0) == 1 && 1225 user_key_allowed(authctxt->pw, key, 1226 pubkey_auth_attempt); 1227 pubkey_auth_info(authctxt, key, NULL); 1228 auth_method = "publickey"; 1229 if (options.pubkey_authentication && 1230 (!pubkey_auth_attempt || allowed != 1)) 1231 auth_clear_options(); 1232 break; 1233 case MM_HOSTKEY: 1234 allowed = options.hostbased_authentication && 1235 match_pattern_list(sshkey_ssh_name(key), 1236 options.hostbased_key_types, 0) == 1 && 1237 hostbased_key_allowed(authctxt->pw, 1238 cuser, chost, key); 1239 pubkey_auth_info(authctxt, key, 1240 "client user \"%.100s\", client host \"%.100s\"", 1241 cuser, chost); 1242 auth_method = "hostbased"; 1243 break; 1244#ifdef WITH_SSH1 1245 case MM_RSAHOSTKEY: 1246 key->type = KEY_RSA1; /* XXX */ 1247 allowed = options.rhosts_rsa_authentication && 1248 auth_rhosts_rsa_key_allowed(authctxt->pw, 1249 cuser, chost, key); 1250 if (options.rhosts_rsa_authentication && allowed != 1) 1251 auth_clear_options(); 1252 auth_method = "rsa"; 1253 break; 1254#endif 1255 default: 1256 fatal("%s: unknown key type %d", __func__, type); 1257 break; 1258 } 1259 } 1260 if (key != NULL) 1261 key_free(key); 1262 1263 /* clear temporarily storage (used by verify) */ 1264 monitor_reset_key_state(); 1265 1266 if (allowed) { 1267 /* Save temporarily for comparison in verify */ 1268 key_blob = blob; 1269 key_bloblen = bloblen; 1270 key_blobtype = type; 1271 hostbased_cuser = cuser; 1272 hostbased_chost = chost; 1273 } else { 1274 /* Log failed attempt */ 1275 auth_log(authctxt, 0, 0, auth_method, NULL); 1276 free(blob); 1277 free(cuser); 1278 free(chost); 1279 } 1280 1281 debug3("%s: key %p is %s", 1282 __func__, key, allowed ? "allowed" : "not allowed"); 1283 1284 buffer_clear(m); 1285 buffer_put_int(m, allowed); 1286 buffer_put_int(m, forced_command != NULL); 1287 1288 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 1289 1290 if (type == MM_RSAHOSTKEY) 1291 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 1292 1293 return (0); 1294} 1295 1296static int 1297monitor_valid_userblob(u_char *data, u_int datalen) 1298{ 1299 Buffer b; 1300 char *p, *userstyle; 1301 u_int len; 1302 int fail = 0; 1303 1304 buffer_init(&b); 1305 buffer_append(&b, data, datalen); 1306 1307 if (datafellows & SSH_OLD_SESSIONID) { 1308 p = buffer_ptr(&b); 1309 len = buffer_len(&b); 1310 if ((session_id2 == NULL) || 1311 (len < session_id2_len) || 1312 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1313 fail++; 1314 buffer_consume(&b, session_id2_len); 1315 } else { 1316 p = buffer_get_string(&b, &len); 1317 if ((session_id2 == NULL) || 1318 (len != session_id2_len) || 1319 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1320 fail++; 1321 free(p); 1322 } 1323 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1324 fail++; 1325 p = buffer_get_cstring(&b, NULL); 1326 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1327 authctxt->style ? ":" : "", 1328 authctxt->style ? authctxt->style : ""); 1329 if (strcmp(userstyle, p) != 0) { 1330 logit("wrong user name passed to monitor: expected %s != %.100s", 1331 userstyle, p); 1332 fail++; 1333 } 1334 free(userstyle); 1335 free(p); 1336 buffer_skip_string(&b); 1337 if (datafellows & SSH_BUG_PKAUTH) { 1338 if (!buffer_get_char(&b)) 1339 fail++; 1340 } else { 1341 p = buffer_get_cstring(&b, NULL); 1342 if (strcmp("publickey", p) != 0) 1343 fail++; 1344 free(p); 1345 if (!buffer_get_char(&b)) 1346 fail++; 1347 buffer_skip_string(&b); 1348 } 1349 buffer_skip_string(&b); 1350 if (buffer_len(&b) != 0) 1351 fail++; 1352 buffer_free(&b); 1353 return (fail == 0); 1354} 1355 1356static int 1357monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1358 char *chost) 1359{ 1360 Buffer b; 1361 char *p, *userstyle; 1362 u_int len; 1363 int fail = 0; 1364 1365 buffer_init(&b); 1366 buffer_append(&b, data, datalen); 1367 1368 p = buffer_get_string(&b, &len); 1369 if ((session_id2 == NULL) || 1370 (len != session_id2_len) || 1371 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1372 fail++; 1373 free(p); 1374 1375 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1376 fail++; 1377 p = buffer_get_cstring(&b, NULL); 1378 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1379 authctxt->style ? ":" : "", 1380 authctxt->style ? authctxt->style : ""); 1381 if (strcmp(userstyle, p) != 0) { 1382 logit("wrong user name passed to monitor: expected %s != %.100s", 1383 userstyle, p); 1384 fail++; 1385 } 1386 free(userstyle); 1387 free(p); 1388 buffer_skip_string(&b); /* service */ 1389 p = buffer_get_cstring(&b, NULL); 1390 if (strcmp(p, "hostbased") != 0) 1391 fail++; 1392 free(p); 1393 buffer_skip_string(&b); /* pkalg */ 1394 buffer_skip_string(&b); /* pkblob */ 1395 1396 /* verify client host, strip trailing dot if necessary */ 1397 p = buffer_get_string(&b, NULL); 1398 if (((len = strlen(p)) > 0) && p[len - 1] == '.') 1399 p[len - 1] = '\0'; 1400 if (strcmp(p, chost) != 0) 1401 fail++; 1402 free(p); 1403 1404 /* verify client user */ 1405 p = buffer_get_string(&b, NULL); 1406 if (strcmp(p, cuser) != 0) 1407 fail++; 1408 free(p); 1409 1410 if (buffer_len(&b) != 0) 1411 fail++; 1412 buffer_free(&b); 1413 return (fail == 0); 1414} 1415 1416int 1417mm_answer_keyverify(int sock, Buffer *m) 1418{ 1419 Key *key; 1420 u_char *signature, *data, *blob; 1421 u_int signaturelen, datalen, bloblen; 1422 int verified = 0; 1423 int valid_data = 0; 1424 1425 blob = buffer_get_string(m, &bloblen); 1426 signature = buffer_get_string(m, &signaturelen); 1427 data = buffer_get_string(m, &datalen); 1428 1429 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1430 !monitor_allowed_key(blob, bloblen)) 1431 fatal("%s: bad key, not previously allowed", __func__); 1432 1433 key = key_from_blob(blob, bloblen); 1434 if (key == NULL) 1435 fatal("%s: bad public key blob", __func__); 1436 1437 switch (key_blobtype) { 1438 case MM_USERKEY: 1439 valid_data = monitor_valid_userblob(data, datalen); 1440 break; 1441 case MM_HOSTKEY: 1442 valid_data = monitor_valid_hostbasedblob(data, datalen, 1443 hostbased_cuser, hostbased_chost); 1444 break; 1445 default: 1446 valid_data = 0; 1447 break; 1448 } 1449 if (!valid_data) 1450 fatal("%s: bad signature data blob", __func__); 1451 1452 verified = key_verify(key, signature, signaturelen, data, datalen); 1453 debug3("%s: key %p signature %s", 1454 __func__, key, (verified == 1) ? "verified" : "unverified"); 1455 1456 /* If auth was successful then record key to ensure it isn't reused */ 1457 if (verified == 1) 1458 auth2_record_userkey(authctxt, key); 1459 else 1460 key_free(key); 1461 1462 free(blob); 1463 free(signature); 1464 free(data); 1465 1466 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; 1467 1468 monitor_reset_key_state(); 1469 1470 buffer_clear(m); 1471 buffer_put_int(m, verified); 1472 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1473 1474 return (verified == 1); 1475} 1476 1477static void 1478mm_record_login(Session *s, struct passwd *pw) 1479{ 1480 socklen_t fromlen; 1481 struct sockaddr_storage from; 1482 1483 if (options.use_login) 1484 return; 1485 1486 /* 1487 * Get IP address of client. If the connection is not a socket, let 1488 * the address be 0.0.0.0. 1489 */ 1490 memset(&from, 0, sizeof(from)); 1491 fromlen = sizeof(from); 1492 if (packet_connection_is_on_socket()) { 1493 if (getpeername(packet_get_connection_in(), 1494 (struct sockaddr *)&from, &fromlen) < 0) { 1495 debug("getpeername: %.100s", strerror(errno)); 1496 cleanup_exit(255); 1497 } 1498 } 1499 /* Record that there was a login on that tty from the remote host. */ 1500 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1501 get_remote_name_or_ip(utmp_len, options.use_dns), 1502 (struct sockaddr *)&from, fromlen); 1503} 1504 1505static void 1506mm_session_close(Session *s) 1507{ 1508 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1509 if (s->ttyfd != -1) { 1510 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1511 session_pty_cleanup2(s); 1512 } 1513 session_unused(s->self); 1514} 1515 1516int 1517mm_answer_pty(int sock, Buffer *m) 1518{ 1519 extern struct monitor *pmonitor; 1520 Session *s; 1521 int res, fd0; 1522 1523 debug3("%s entering", __func__); 1524 1525 buffer_clear(m); 1526 s = session_new(); 1527 if (s == NULL) 1528 goto error; 1529 s->authctxt = authctxt; 1530 s->pw = authctxt->pw; 1531 s->pid = pmonitor->m_pid; 1532 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1533 if (res == 0) 1534 goto error; 1535 pty_setowner(authctxt->pw, s->tty); 1536 1537 buffer_put_int(m, 1); 1538 buffer_put_cstring(m, s->tty); 1539 1540 /* We need to trick ttyslot */ 1541 if (dup2(s->ttyfd, 0) == -1) 1542 fatal("%s: dup2", __func__); 1543 1544 mm_record_login(s, authctxt->pw); 1545 1546 /* Now we can close the file descriptor again */ 1547 close(0); 1548 1549 /* send messages generated by record_login */ 1550 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); 1551 buffer_clear(&loginmsg); 1552 1553 mm_request_send(sock, MONITOR_ANS_PTY, m); 1554 1555 if (mm_send_fd(sock, s->ptyfd) == -1 || 1556 mm_send_fd(sock, s->ttyfd) == -1) 1557 fatal("%s: send fds failed", __func__); 1558 1559 /* make sure nothing uses fd 0 */ 1560 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1561 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1562 if (fd0 != 0) 1563 error("%s: fd0 %d != 0", __func__, fd0); 1564 1565 /* slave is not needed */ 1566 close(s->ttyfd); 1567 s->ttyfd = s->ptyfd; 1568 /* no need to dup() because nobody closes ptyfd */ 1569 s->ptymaster = s->ptyfd; 1570 1571 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1572 1573 return (0); 1574 1575 error: 1576 if (s != NULL) 1577 mm_session_close(s); 1578 buffer_put_int(m, 0); 1579 mm_request_send(sock, MONITOR_ANS_PTY, m); 1580 return (0); 1581} 1582 1583int 1584mm_answer_pty_cleanup(int sock, Buffer *m) 1585{ 1586 Session *s; 1587 char *tty; 1588 1589 debug3("%s entering", __func__); 1590 1591 tty = buffer_get_string(m, NULL); 1592 if ((s = session_by_tty(tty)) != NULL) 1593 mm_session_close(s); 1594 buffer_clear(m); 1595 free(tty); 1596 return (0); 1597} 1598 1599#ifdef WITH_SSH1 1600int 1601mm_answer_sesskey(int sock, Buffer *m) 1602{ 1603 BIGNUM *p; 1604 int rsafail; 1605 1606 /* Turn off permissions */ 1607 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0); 1608 1609 if ((p = BN_new()) == NULL) 1610 fatal("%s: BN_new", __func__); 1611 1612 buffer_get_bignum2(m, p); 1613 1614 rsafail = ssh1_session_key(p); 1615 1616 buffer_clear(m); 1617 buffer_put_int(m, rsafail); 1618 buffer_put_bignum2(m, p); 1619 1620 BN_clear_free(p); 1621 1622 mm_request_send(sock, MONITOR_ANS_SESSKEY, m); 1623 1624 /* Turn on permissions for sessid passing */ 1625 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); 1626 1627 return (0); 1628} 1629 1630int 1631mm_answer_sessid(int sock, Buffer *m) 1632{ 1633 int i; 1634 1635 debug3("%s entering", __func__); 1636 1637 if (buffer_len(m) != 16) 1638 fatal("%s: bad ssh1 session id", __func__); 1639 for (i = 0; i < 16; i++) 1640 session_id[i] = buffer_get_char(m); 1641 1642 /* Turn on permissions for getpwnam */ 1643 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 1644 1645 return (0); 1646} 1647 1648int 1649mm_answer_rsa_keyallowed(int sock, Buffer *m) 1650{ 1651 BIGNUM *client_n; 1652 Key *key = NULL; 1653 u_char *blob = NULL; 1654 u_int blen = 0; 1655 int allowed = 0; 1656 1657 debug3("%s entering", __func__); 1658 1659 auth_method = "rsa"; 1660 if (options.rsa_authentication && authctxt->valid) { 1661 if ((client_n = BN_new()) == NULL) 1662 fatal("%s: BN_new", __func__); 1663 buffer_get_bignum2(m, client_n); 1664 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); 1665 BN_clear_free(client_n); 1666 } 1667 buffer_clear(m); 1668 buffer_put_int(m, allowed); 1669 buffer_put_int(m, forced_command != NULL); 1670 1671 /* clear temporarily storage (used by generate challenge) */ 1672 monitor_reset_key_state(); 1673 1674 if (allowed && key != NULL) { 1675 key->type = KEY_RSA; /* cheat for key_to_blob */ 1676 if (key_to_blob(key, &blob, &blen) == 0) 1677 fatal("%s: key_to_blob failed", __func__); 1678 buffer_put_string(m, blob, blen); 1679 1680 /* Save temporarily for comparison in verify */ 1681 key_blob = blob; 1682 key_bloblen = blen; 1683 key_blobtype = MM_RSAUSERKEY; 1684 } 1685 if (key != NULL) 1686 key_free(key); 1687 1688 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m); 1689 1690 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 1691 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); 1692 return (0); 1693} 1694 1695int 1696mm_answer_rsa_challenge(int sock, Buffer *m) 1697{ 1698 Key *key = NULL; 1699 u_char *blob; 1700 u_int blen; 1701 1702 debug3("%s entering", __func__); 1703 1704 if (!authctxt->valid) 1705 fatal("%s: authctxt not valid", __func__); 1706 blob = buffer_get_string(m, &blen); 1707 if (!monitor_allowed_key(blob, blen)) 1708 fatal("%s: bad key, not previously allowed", __func__); 1709 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1710 fatal("%s: key type mismatch", __func__); 1711 if ((key = key_from_blob(blob, blen)) == NULL) 1712 fatal("%s: received bad key", __func__); 1713 if (key->type != KEY_RSA) 1714 fatal("%s: received bad key type %d", __func__, key->type); 1715 key->type = KEY_RSA1; 1716 if (ssh1_challenge) 1717 BN_clear_free(ssh1_challenge); 1718 ssh1_challenge = auth_rsa_generate_challenge(key); 1719 1720 buffer_clear(m); 1721 buffer_put_bignum2(m, ssh1_challenge); 1722 1723 debug3("%s sending reply", __func__); 1724 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m); 1725 1726 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); 1727 1728 free(blob); 1729 key_free(key); 1730 return (0); 1731} 1732 1733int 1734mm_answer_rsa_response(int sock, Buffer *m) 1735{ 1736 Key *key = NULL; 1737 u_char *blob, *response; 1738 u_int blen, len; 1739 int success; 1740 1741 debug3("%s entering", __func__); 1742 1743 if (!authctxt->valid) 1744 fatal("%s: authctxt not valid", __func__); 1745 if (ssh1_challenge == NULL) 1746 fatal("%s: no ssh1_challenge", __func__); 1747 1748 blob = buffer_get_string(m, &blen); 1749 if (!monitor_allowed_key(blob, blen)) 1750 fatal("%s: bad key, not previously allowed", __func__); 1751 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1752 fatal("%s: key type mismatch: %d", __func__, key_blobtype); 1753 if ((key = key_from_blob(blob, blen)) == NULL) 1754 fatal("%s: received bad key", __func__); 1755 response = buffer_get_string(m, &len); 1756 if (len != 16) 1757 fatal("%s: received bad response to challenge", __func__); 1758 success = auth_rsa_verify_response(key, ssh1_challenge, response); 1759 1760 free(blob); 1761 key_free(key); 1762 free(response); 1763 1764 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; 1765 1766 /* reset state */ 1767 BN_clear_free(ssh1_challenge); 1768 ssh1_challenge = NULL; 1769 monitor_reset_key_state(); 1770 1771 buffer_clear(m); 1772 buffer_put_int(m, success); 1773 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m); 1774 1775 return (success); 1776} 1777#endif 1778 1779int 1780mm_answer_term(int sock, Buffer *req) 1781{ 1782 extern struct monitor *pmonitor; 1783 int res, status; 1784 1785 debug3("%s: tearing down sessions", __func__); 1786 1787 /* The child is terminating */ 1788 session_destroy_all(&mm_session_close); 1789 1790#ifdef USE_PAM 1791 if (options.use_pam) 1792 sshpam_cleanup(); 1793#endif 1794 1795 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1796 if (errno != EINTR) 1797 exit(1); 1798 1799 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1800 1801 /* Terminate process */ 1802 exit(res); 1803} 1804 1805#ifdef SSH_AUDIT_EVENTS 1806/* Report that an audit event occurred */ 1807int 1808mm_answer_audit_event(int socket, Buffer *m) 1809{ 1810 ssh_audit_event_t event; 1811 1812 debug3("%s entering", __func__); 1813 1814 event = buffer_get_int(m); 1815 switch(event) { 1816 case SSH_AUTH_FAIL_PUBKEY: 1817 case SSH_AUTH_FAIL_HOSTBASED: 1818 case SSH_AUTH_FAIL_GSSAPI: 1819 case SSH_LOGIN_EXCEED_MAXTRIES: 1820 case SSH_LOGIN_ROOT_DENIED: 1821 case SSH_CONNECTION_CLOSE: 1822 case SSH_INVALID_USER: 1823 audit_event(event); 1824 break; 1825 default: 1826 fatal("Audit event type %d not permitted", event); 1827 } 1828 1829 return (0); 1830} 1831 1832int 1833mm_answer_audit_command(int socket, Buffer *m) 1834{ 1835 u_int len; 1836 char *cmd; 1837 1838 debug3("%s entering", __func__); 1839 cmd = buffer_get_string(m, &len); 1840 /* sanity check command, if so how? */ 1841 audit_run_command(cmd); 1842 free(cmd); 1843 return (0); 1844} 1845#endif /* SSH_AUDIT_EVENTS */ 1846 1847void 1848monitor_apply_keystate(struct monitor *pmonitor) 1849{ 1850 struct ssh *ssh = active_state; /* XXX */ 1851 struct kex *kex; 1852 int r; 1853 1854 debug3("%s: packet_set_state", __func__); 1855 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1856 fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); 1857 sshbuf_free(child_state); 1858 child_state = NULL; 1859 1860 if ((kex = ssh->kex) != 0) { 1861 /* XXX set callbacks */ 1862#ifdef WITH_OPENSSL 1863 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1864 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 1865 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1866 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1867# ifdef OPENSSL_HAS_ECC 1868 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 1869# endif 1870#endif /* WITH_OPENSSL */ 1871 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 1872 kex->load_host_public_key=&get_hostkey_public_by_type; 1873 kex->load_host_private_key=&get_hostkey_private_by_type; 1874 kex->host_key_index=&get_hostkey_index; 1875 kex->sign = sshd_hostkey_sign; 1876 } 1877 1878 /* Update with new address */ 1879 if (options.compression) { 1880 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib, 1881 (ssh_packet_comp_alloc_func *)mm_zalloc, 1882 (ssh_packet_comp_free_func *)mm_zfree); 1883 } 1884} 1885 1886/* This function requries careful sanity checking */ 1887 1888void 1889mm_get_keystate(struct monitor *pmonitor) 1890{ 1891 debug3("%s: Waiting for new keys", __func__); 1892 1893 if ((child_state = sshbuf_new()) == NULL) 1894 fatal("%s: sshbuf_new failed", __func__); 1895 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1896 child_state); 1897 debug3("%s: GOT new keys", __func__); 1898} 1899 1900 1901/* XXX */ 1902 1903#define FD_CLOSEONEXEC(x) do { \ 1904 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ 1905 fatal("fcntl(%d, F_SETFD)", x); \ 1906} while (0) 1907 1908static void 1909monitor_openfds(struct monitor *mon, int do_logfds) 1910{ 1911 int pair[2]; 1912 1913 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1914 fatal("%s: socketpair: %s", __func__, strerror(errno)); 1915 FD_CLOSEONEXEC(pair[0]); 1916 FD_CLOSEONEXEC(pair[1]); 1917 mon->m_recvfd = pair[0]; 1918 mon->m_sendfd = pair[1]; 1919 1920 if (do_logfds) { 1921 if (pipe(pair) == -1) 1922 fatal("%s: pipe: %s", __func__, strerror(errno)); 1923 FD_CLOSEONEXEC(pair[0]); 1924 FD_CLOSEONEXEC(pair[1]); 1925 mon->m_log_recvfd = pair[0]; 1926 mon->m_log_sendfd = pair[1]; 1927 } else 1928 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1929} 1930 1931#define MM_MEMSIZE 65536 1932 1933struct monitor * 1934monitor_init(void) 1935{ 1936 struct ssh *ssh = active_state; /* XXX */ 1937 struct monitor *mon; 1938 1939 mon = xcalloc(1, sizeof(*mon)); 1940 1941 monitor_openfds(mon, 1); 1942 1943 /* Used to share zlib space across processes */ 1944 if (options.compression) { 1945 mon->m_zback = mm_create(NULL, MM_MEMSIZE); 1946 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); 1947 1948 /* Compression needs to share state across borders */ 1949 ssh_packet_set_compress_hooks(ssh, mon->m_zlib, 1950 (ssh_packet_comp_alloc_func *)mm_zalloc, 1951 (ssh_packet_comp_free_func *)mm_zfree); 1952 } 1953 1954 return mon; 1955} 1956 1957void 1958monitor_reinit(struct monitor *mon) 1959{ 1960 monitor_openfds(mon, 0); 1961} 1962 1963#ifdef GSSAPI 1964int 1965mm_answer_gss_setup_ctx(int sock, Buffer *m) 1966{ 1967 gss_OID_desc goid; 1968 OM_uint32 major; 1969 u_int len; 1970 1971 goid.elements = buffer_get_string(m, &len); 1972 goid.length = len; 1973 1974 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1975 1976 free(goid.elements); 1977 1978 buffer_clear(m); 1979 buffer_put_int(m, major); 1980 1981 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1982 1983 /* Now we have a context, enable the step */ 1984 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1985 1986 return (0); 1987} 1988 1989int 1990mm_answer_gss_accept_ctx(int sock, Buffer *m) 1991{ 1992 gss_buffer_desc in; 1993 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 1994 OM_uint32 major, minor; 1995 OM_uint32 flags = 0; /* GSI needs this */ 1996 u_int len; 1997 1998 in.value = buffer_get_string(m, &len); 1999 in.length = len; 2000 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 2001 free(in.value); 2002 2003 buffer_clear(m); 2004 buffer_put_int(m, major); 2005 buffer_put_string(m, out.value, out.length); 2006 buffer_put_int(m, flags); 2007 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 2008 2009 gss_release_buffer(&minor, &out); 2010 2011 if (major == GSS_S_COMPLETE) { 2012 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 2013 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2014 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 2015 } 2016 return (0); 2017} 2018 2019int 2020mm_answer_gss_checkmic(int sock, Buffer *m) 2021{ 2022 gss_buffer_desc gssbuf, mic; 2023 OM_uint32 ret; 2024 u_int len; 2025 2026 gssbuf.value = buffer_get_string(m, &len); 2027 gssbuf.length = len; 2028 mic.value = buffer_get_string(m, &len); 2029 mic.length = len; 2030 2031 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 2032 2033 free(gssbuf.value); 2034 free(mic.value); 2035 2036 buffer_clear(m); 2037 buffer_put_int(m, ret); 2038 2039 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 2040 2041 if (!GSS_ERROR(ret)) 2042 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2043 2044 return (0); 2045} 2046 2047int 2048mm_answer_gss_userok(int sock, Buffer *m) 2049{ 2050 int authenticated; 2051 2052 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 2053 2054 buffer_clear(m); 2055 buffer_put_int(m, authenticated); 2056 2057 debug3("%s: sending result %d", __func__, authenticated); 2058 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 2059 2060 auth_method = "gssapi-with-mic"; 2061 2062 /* Monitor loop will terminate if authenticated */ 2063 return (authenticated); 2064} 2065#endif /* GSSAPI */ 2066 2067