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