1 /* $NetBSD: monitor.c,v 1.18 2016/03/11 01:55:00 christos Exp $ */ 2 /* $OpenBSD: monitor.c,v 1.157 2016/02/15 23:32:37 djm Exp $ */ 3 4 /* 5 * Copyright 2002 Niels Provos <provos@citi.umich.edu> 6 * Copyright 2002 Markus Friedl <markus@openbsd.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "includes.h" 31 __RCSID("$NetBSD: monitor.c,v 1.18 2016/03/11 01:55:00 christos Exp $"); 32 #include <sys/types.h> 33 #include <sys/wait.h> 34 #include <sys/socket.h> 35 #include <sys/tree.h> 36 #include <sys/queue.h> 37 38 #ifdef WITH_OPENSSL 39 #include <openssl/dh.h> 40 #endif 41 42 #include <errno.h> 43 #include <fcntl.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 uint32_t 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 682 /* 683 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), 684 * SHA384 (48 bytes) and SHA512 (64 bytes). 685 * 686 * Otherwise, verify the signature request is for a hostkey 687 * proof. 688 * 689 * XXX perform similar check for KEX signature requests too? 690 * it's not trivial, since what is signed is the hash, rather 691 * than the full kex structure... 692 */ 693 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { 694 /* 695 * Construct expected hostkey proof and compare it to what 696 * the client sent us. 697 */ 698 if (session_id2_len == 0) /* hostkeys is never first */ 699 fatal("%s: bad data length: %zu", __func__, datlen); 700 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) 701 fatal("%s: no hostkey for index %d", __func__, keyid); 702 if ((sigbuf = sshbuf_new()) == NULL) 703 fatal("%s: sshbuf_new", __func__); 704 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || 705 (r = sshbuf_put_string(sigbuf, session_id2, 706 session_id2_len)) != 0 || 707 (r = sshkey_puts(key, sigbuf)) != 0) 708 fatal("%s: couldn't prepare private key " 709 "proof buffer: %s", __func__, ssh_err(r)); 710 if (datlen != sshbuf_len(sigbuf) || 711 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) 712 fatal("%s: bad data length: %zu, hostkey proof len %zu", 713 __func__, datlen, sshbuf_len(sigbuf)); 714 sshbuf_free(sigbuf); 715 is_proof = 1; 716 } 717 718 /* save session id, it will be passed on the first call */ 719 if (session_id2_len == 0) { 720 session_id2_len = datlen; 721 session_id2 = xmalloc(session_id2_len); 722 memcpy(session_id2, p, session_id2_len); 723 } 724 725 if ((key = get_hostkey_by_index(keyid)) != NULL) { 726 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg, 727 datafellows)) != 0) 728 fatal("%s: sshkey_sign failed: %s", 729 __func__, ssh_err(r)); 730 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && 731 auth_sock > 0) { 732 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, 733 p, datlen, alg, datafellows)) != 0) { 734 fatal("%s: ssh_agent_sign failed: %s", 735 __func__, ssh_err(r)); 736 } 737 } else 738 fatal("%s: no hostkey from index %d", __func__, keyid); 739 740 debug3("%s: %s signature %p(%zu)", __func__, 741 is_proof ? "KEX" : "hostkey proof", signature, siglen); 742 743 sshbuf_reset(m); 744 if ((r = sshbuf_put_string(m, signature, siglen)) != 0) 745 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 746 747 free(alg); 748 free(p); 749 free(signature); 750 751 mm_request_send(sock, MONITOR_ANS_SIGN, m); 752 753 /* Turn on permissions for getpwnam */ 754 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 755 756 return (0); 757 } 758 759 /* Retrieves the password entry and also checks if the user is permitted */ 760 761 int 762 mm_answer_pwnamallow(int sock, Buffer *m) 763 { 764 char *username; 765 struct passwd *pwent; 766 int allowed = 0; 767 u_int i; 768 769 debug3("%s", __func__); 770 771 if (authctxt->attempt++ != 0) 772 fatal("%s: multiple attempts for getpwnam", __func__); 773 774 username = buffer_get_string(m, NULL); 775 776 pwent = getpwnamallow(username); 777 778 authctxt->user = xstrdup(username); 779 setproctitle("%s [priv]", pwent ? username : "unknown"); 780 free(username); 781 782 buffer_clear(m); 783 784 if (pwent == NULL) { 785 buffer_put_char(m, 0); 786 authctxt->pw = fakepw(); 787 goto out; 788 } 789 790 allowed = 1; 791 authctxt->pw = pwent; 792 authctxt->valid = 1; 793 794 buffer_put_char(m, 1); 795 buffer_put_string(m, pwent, sizeof(struct passwd)); 796 buffer_put_cstring(m, pwent->pw_name); 797 buffer_put_cstring(m, "*"); 798 buffer_put_cstring(m, pwent->pw_gecos); 799 buffer_put_cstring(m, pwent->pw_class); 800 buffer_put_cstring(m, pwent->pw_dir); 801 buffer_put_cstring(m, pwent->pw_shell); 802 803 out: 804 buffer_put_string(m, &options, sizeof(options)); 805 806 #define M_CP_STROPT(x) do { \ 807 if (options.x != NULL) \ 808 buffer_put_cstring(m, options.x); \ 809 } while (0) 810 #define M_CP_STRARRAYOPT(x, nx) do { \ 811 for (i = 0; i < options.nx; i++) \ 812 buffer_put_cstring(m, options.x[i]); \ 813 } while (0) 814 /* See comment in servconf.h */ 815 COPY_MATCH_STRING_OPTS(); 816 #undef M_CP_STROPT 817 #undef M_CP_STRARRAYOPT 818 819 /* Create valid auth method lists */ 820 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) { 821 /* 822 * The monitor will continue long enough to let the child 823 * run to it's packet_disconnect(), but it must not allow any 824 * authentication to succeed. 825 */ 826 debug("%s: no valid authentication method lists", __func__); 827 } 828 829 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); 830 mm_request_send(sock, MONITOR_ANS_PWNAM, m); 831 832 /* For SSHv1 allow authentication now */ 833 if (!compat20) 834 monitor_permit_authentications(1); 835 else { 836 /* Allow service/style information on the auth context */ 837 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); 838 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); 839 } 840 841 #ifdef USE_PAM 842 if (options.use_pam) 843 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); 844 #endif 845 846 return (0); 847 } 848 849 int mm_answer_auth2_read_banner(int sock, Buffer *m) 850 { 851 char *banner; 852 853 buffer_clear(m); 854 banner = auth2_read_banner(); 855 buffer_put_cstring(m, banner != NULL ? banner : ""); 856 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); 857 free(banner); 858 859 return (0); 860 } 861 862 int 863 mm_answer_authserv(int sock, Buffer *m) 864 { 865 monitor_permit_authentications(1); 866 867 authctxt->service = buffer_get_string(m, NULL); 868 authctxt->style = buffer_get_string(m, NULL); 869 debug3("%s: service=%s, style=%s", 870 __func__, authctxt->service, authctxt->style); 871 872 if (strlen(authctxt->style) == 0) { 873 free(authctxt->style); 874 authctxt->style = NULL; 875 } 876 877 return (0); 878 } 879 880 int 881 mm_answer_authpassword(int sock, Buffer *m) 882 { 883 static int call_count; 884 char *passwd; 885 int authenticated; 886 u_int plen; 887 888 passwd = buffer_get_string(m, &plen); 889 /* Only authenticate if the context is valid */ 890 authenticated = options.password_authentication && 891 auth_password(authctxt, passwd); 892 explicit_bzero(passwd, strlen(passwd)); 893 free(passwd); 894 895 buffer_clear(m); 896 buffer_put_int(m, authenticated); 897 898 debug3("%s: sending result %d", __func__, authenticated); 899 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); 900 901 call_count++; 902 if (plen == 0 && call_count == 1) 903 auth_method = "none"; 904 else 905 auth_method = "password"; 906 907 /* Causes monitor loop to terminate if authenticated */ 908 return (authenticated); 909 } 910 911 #ifdef BSD_AUTH 912 int 913 mm_answer_bsdauthquery(int sock, Buffer *m) 914 { 915 char *name, *infotxt; 916 u_int numprompts; 917 u_int *echo_on; 918 char **prompts; 919 u_int success; 920 921 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 922 &prompts, &echo_on) < 0 ? 0 : 1; 923 924 buffer_clear(m); 925 buffer_put_int(m, success); 926 if (success) 927 buffer_put_cstring(m, prompts[0]); 928 929 debug3("%s: sending challenge success: %u", __func__, success); 930 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); 931 932 if (success) { 933 free(name); 934 free(infotxt); 935 free(prompts); 936 free(echo_on); 937 } 938 939 return (0); 940 } 941 942 int 943 mm_answer_bsdauthrespond(int sock, Buffer *m) 944 { 945 char *response; 946 int authok; 947 948 if (authctxt->as == NULL) 949 fatal("%s: no bsd auth session", __func__); 950 951 response = buffer_get_string(m, NULL); 952 authok = options.challenge_response_authentication && 953 auth_userresponse(authctxt->as, response, 0); 954 authctxt->as = NULL; 955 debug3("%s: <%s> = <%d>", __func__, response, authok); 956 free(response); 957 958 buffer_clear(m); 959 buffer_put_int(m, authok); 960 961 debug3("%s: sending authenticated: %d", __func__, authok); 962 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); 963 964 if (compat20) { 965 auth_method = "keyboard-interactive"; 966 auth_submethod = "bsdauth"; 967 } else 968 auth_method = "bsdauth"; 969 970 return (authok != 0); 971 } 972 #endif 973 974 #ifdef SKEY 975 int 976 mm_answer_skeyquery(int sock, Buffer *m) 977 { 978 struct skey skey; 979 char challenge[1024]; 980 u_int success; 981 982 success = skeychallenge(&skey, authctxt->user, challenge, 983 sizeof(challenge)) < 0 ? 0 : 1; 984 985 buffer_clear(m); 986 buffer_put_int(m, success); 987 if (success) 988 buffer_put_cstring(m, challenge); 989 990 debug3("%s: sending challenge success: %u", __func__, success); 991 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m); 992 993 return (0); 994 } 995 996 int 997 mm_answer_skeyrespond(int sock, Buffer *m) 998 { 999 char *response; 1000 int authok; 1001 1002 response = buffer_get_string(m, NULL); 1003 1004 authok = (options.challenge_response_authentication && 1005 authctxt->valid && 1006 skey_haskey(authctxt->pw->pw_name) == 0 && 1007 skey_passcheck(authctxt->pw->pw_name, response) != -1); 1008 1009 free(response); 1010 1011 buffer_clear(m); 1012 buffer_put_int(m, authok); 1013 1014 debug3("%s: sending authenticated: %d", __func__, authok); 1015 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m); 1016 1017 auth_method = "skey"; 1018 1019 return (authok != 0); 1020 } 1021 #endif 1022 1023 #ifdef USE_PAM 1024 int 1025 mm_answer_pam_start(int sock, Buffer *m) 1026 { 1027 if (!options.use_pam) 1028 fatal("UsePAM not set, but ended up in %s anyway", __func__); 1029 1030 start_pam(authctxt); 1031 1032 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); 1033 1034 return (0); 1035 } 1036 1037 int 1038 mm_answer_pam_account(int sock, Buffer *m) 1039 { 1040 u_int ret; 1041 1042 if (!options.use_pam) 1043 fatal("UsePAM not set, but ended up in %s anyway", __func__); 1044 1045 ret = do_pam_account(); 1046 1047 buffer_put_int(m, ret); 1048 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); 1049 1050 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); 1051 1052 return (ret); 1053 } 1054 1055 static void *sshpam_ctxt, *sshpam_authok; 1056 extern KbdintDevice sshpam_device; 1057 1058 int 1059 mm_answer_pam_init_ctx(int sock, Buffer *m) 1060 { 1061 debug3("%s", __func__); 1062 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); 1063 sshpam_authok = NULL; 1064 buffer_clear(m); 1065 if (sshpam_ctxt != NULL) { 1066 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); 1067 buffer_put_int(m, 1); 1068 } else { 1069 buffer_put_int(m, 0); 1070 } 1071 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); 1072 return (0); 1073 } 1074 1075 int 1076 mm_answer_pam_query(int sock, Buffer *m) 1077 { 1078 char *name, *info, **prompts; 1079 u_int i, num, *echo_on; 1080 int ret; 1081 1082 debug3("%s", __func__); 1083 sshpam_authok = NULL; 1084 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); 1085 if (ret == 0 && num == 0) 1086 sshpam_authok = sshpam_ctxt; 1087 if (num > 1 || name == NULL || info == NULL) 1088 ret = -1; 1089 buffer_clear(m); 1090 buffer_put_int(m, ret); 1091 buffer_put_cstring(m, name); 1092 free(name); 1093 buffer_put_cstring(m, info); 1094 free(info); 1095 buffer_put_int(m, num); 1096 for (i = 0; i < num; ++i) { 1097 buffer_put_cstring(m, prompts[i]); 1098 free(prompts[i]); 1099 buffer_put_int(m, echo_on[i]); 1100 } 1101 if (prompts != NULL) 1102 free(prompts); 1103 if (echo_on != NULL) 1104 free(echo_on); 1105 auth_method = "keyboard-interactive/pam"; 1106 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); 1107 return (0); 1108 } 1109 1110 int 1111 mm_answer_pam_respond(int sock, Buffer *m) 1112 { 1113 char **resp; 1114 u_int i, num; 1115 int ret; 1116 1117 debug3("%s", __func__); 1118 sshpam_authok = NULL; 1119 num = buffer_get_int(m); 1120 if (num > 0) { 1121 resp = xmalloc(num * sizeof(char *)); 1122 for (i = 0; i < num; ++i) 1123 resp[i] = buffer_get_string(m, NULL); 1124 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); 1125 for (i = 0; i < num; ++i) 1126 free(resp[i]); 1127 free(resp); 1128 } else { 1129 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); 1130 } 1131 buffer_clear(m); 1132 buffer_put_int(m, ret); 1133 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); 1134 auth_method = "keyboard-interactive/pam"; 1135 if (ret == 0) 1136 sshpam_authok = sshpam_ctxt; 1137 return (0); 1138 } 1139 1140 int 1141 mm_answer_pam_free_ctx(int sock, Buffer *m) 1142 { 1143 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt; 1144 1145 debug3("%s", __func__); 1146 (sshpam_device.free_ctx)(sshpam_ctxt); 1147 sshpam_ctxt = sshpam_authok = NULL; 1148 buffer_clear(m); 1149 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); 1150 auth_method = "keyboard-interactive/pam"; 1151 return r; 1152 } 1153 #endif 1154 1155 int 1156 mm_answer_keyallowed(int sock, Buffer *m) 1157 { 1158 Key *key; 1159 char *cuser, *chost; 1160 u_char *blob; 1161 u_int bloblen, pubkey_auth_attempt; 1162 enum mm_keytype type = 0; 1163 int allowed = 0; 1164 1165 debug3("%s entering", __func__); 1166 1167 type = buffer_get_int(m); 1168 cuser = buffer_get_string(m, NULL); 1169 chost = buffer_get_string(m, NULL); 1170 blob = buffer_get_string(m, &bloblen); 1171 pubkey_auth_attempt = buffer_get_int(m); 1172 1173 key = key_from_blob(blob, bloblen); 1174 1175 if ((compat20 && type == MM_RSAHOSTKEY) || 1176 (!compat20 && type != MM_RSAHOSTKEY)) 1177 fatal("%s: key type and protocol mismatch", __func__); 1178 1179 debug3("%s: key_from_blob: %p", __func__, key); 1180 1181 if (key != NULL && authctxt->valid) { 1182 /* These should not make it past the privsep child */ 1183 if (key_type_plain(key->type) == KEY_RSA && 1184 (datafellows & SSH_BUG_RSASIGMD5) != 0) 1185 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); 1186 1187 switch (type) { 1188 case MM_USERKEY: 1189 allowed = options.pubkey_authentication && 1190 !auth2_userkey_already_used(authctxt, key) && 1191 match_pattern_list(sshkey_ssh_name(key), 1192 options.pubkey_key_types, 0) == 1 && 1193 user_key_allowed(authctxt->pw, key, 1194 pubkey_auth_attempt); 1195 pubkey_auth_info(authctxt, key, NULL); 1196 auth_method = "publickey"; 1197 if (options.pubkey_authentication && 1198 (!pubkey_auth_attempt || allowed != 1)) 1199 auth_clear_options(); 1200 break; 1201 case MM_HOSTKEY: 1202 allowed = options.hostbased_authentication && 1203 match_pattern_list(sshkey_ssh_name(key), 1204 options.hostbased_key_types, 0) == 1 && 1205 hostbased_key_allowed(authctxt->pw, 1206 cuser, chost, key); 1207 pubkey_auth_info(authctxt, key, 1208 "client user \"%.100s\", client host \"%.100s\"", 1209 cuser, chost); 1210 auth_method = "hostbased"; 1211 break; 1212 #ifdef WITH_SSH1 1213 case MM_RSAHOSTKEY: 1214 key->type = KEY_RSA1; /* XXX */ 1215 allowed = options.rhosts_rsa_authentication && 1216 auth_rhosts_rsa_key_allowed(authctxt->pw, 1217 cuser, chost, key); 1218 if (options.rhosts_rsa_authentication && allowed != 1) 1219 auth_clear_options(); 1220 auth_method = "rsa"; 1221 break; 1222 #endif 1223 default: 1224 fatal("%s: unknown key type %d", __func__, type); 1225 break; 1226 } 1227 } 1228 debug3("%s: key %p is %s", 1229 __func__, key, allowed ? "allowed" : "not allowed"); 1230 1231 if (key != NULL) 1232 key_free(key); 1233 1234 /* clear temporarily storage (used by verify) */ 1235 monitor_reset_key_state(); 1236 1237 if (allowed) { 1238 /* Save temporarily for comparison in verify */ 1239 key_blob = blob; 1240 key_bloblen = bloblen; 1241 key_blobtype = type; 1242 hostbased_cuser = cuser; 1243 hostbased_chost = chost; 1244 } else { 1245 /* Log failed attempt */ 1246 auth_log(authctxt, 0, 0, auth_method, NULL); 1247 free(blob); 1248 free(cuser); 1249 free(chost); 1250 } 1251 1252 buffer_clear(m); 1253 buffer_put_int(m, allowed); 1254 buffer_put_int(m, forced_command != NULL); 1255 1256 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 1257 1258 if (type == MM_RSAHOSTKEY) 1259 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 1260 1261 return (0); 1262 } 1263 1264 static int 1265 monitor_valid_userblob(u_char *data, u_int datalen) 1266 { 1267 Buffer b; 1268 char *p, *userstyle; 1269 u_int len; 1270 int fail = 0; 1271 1272 buffer_init(&b); 1273 buffer_append(&b, data, datalen); 1274 1275 if (datafellows & SSH_OLD_SESSIONID) { 1276 p = (char *)buffer_ptr(&b); 1277 len = buffer_len(&b); 1278 if ((session_id2 == NULL) || 1279 (len < session_id2_len) || 1280 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1281 fail++; 1282 buffer_consume(&b, session_id2_len); 1283 } else { 1284 p = buffer_get_string(&b, &len); 1285 if ((session_id2 == NULL) || 1286 (len != session_id2_len) || 1287 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1288 fail++; 1289 free(p); 1290 } 1291 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1292 fail++; 1293 p = buffer_get_cstring(&b, NULL); 1294 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1295 authctxt->style ? ":" : "", 1296 authctxt->style ? authctxt->style : ""); 1297 if (strcmp(userstyle, p) != 0) { 1298 logit("wrong user name passed to monitor: expected %s != %.100s", 1299 userstyle, p); 1300 fail++; 1301 } 1302 free(userstyle); 1303 free(p); 1304 buffer_skip_string(&b); 1305 if (datafellows & SSH_BUG_PKAUTH) { 1306 if (!buffer_get_char(&b)) 1307 fail++; 1308 } else { 1309 p = buffer_get_cstring(&b, NULL); 1310 if (strcmp("publickey", p) != 0) 1311 fail++; 1312 free(p); 1313 if (!buffer_get_char(&b)) 1314 fail++; 1315 buffer_skip_string(&b); 1316 } 1317 buffer_skip_string(&b); 1318 if (buffer_len(&b) != 0) 1319 fail++; 1320 buffer_free(&b); 1321 return (fail == 0); 1322 } 1323 1324 static int 1325 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1326 char *chost) 1327 { 1328 Buffer b; 1329 char *p, *userstyle; 1330 u_int len; 1331 int fail = 0; 1332 1333 buffer_init(&b); 1334 buffer_append(&b, data, datalen); 1335 1336 p = buffer_get_string(&b, &len); 1337 if ((session_id2 == NULL) || 1338 (len != session_id2_len) || 1339 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1340 fail++; 1341 free(p); 1342 1343 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 1344 fail++; 1345 p = buffer_get_cstring(&b, NULL); 1346 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1347 authctxt->style ? ":" : "", 1348 authctxt->style ? authctxt->style : ""); 1349 if (strcmp(userstyle, p) != 0) { 1350 logit("wrong user name passed to monitor: expected %s != %.100s", 1351 userstyle, p); 1352 fail++; 1353 } 1354 free(userstyle); 1355 free(p); 1356 buffer_skip_string(&b); /* service */ 1357 p = buffer_get_cstring(&b, NULL); 1358 if (strcmp(p, "hostbased") != 0) 1359 fail++; 1360 free(p); 1361 buffer_skip_string(&b); /* pkalg */ 1362 buffer_skip_string(&b); /* pkblob */ 1363 1364 /* verify client host, strip trailing dot if necessary */ 1365 p = buffer_get_string(&b, NULL); 1366 if (((len = strlen(p)) > 0) && p[len - 1] == '.') 1367 p[len - 1] = '\0'; 1368 if (strcmp(p, chost) != 0) 1369 fail++; 1370 free(p); 1371 1372 /* verify client user */ 1373 p = buffer_get_string(&b, NULL); 1374 if (strcmp(p, cuser) != 0) 1375 fail++; 1376 free(p); 1377 1378 if (buffer_len(&b) != 0) 1379 fail++; 1380 buffer_free(&b); 1381 return (fail == 0); 1382 } 1383 1384 int 1385 mm_answer_keyverify(int sock, Buffer *m) 1386 { 1387 Key *key; 1388 u_char *signature, *data, *blob; 1389 u_int signaturelen, datalen, bloblen; 1390 int verified = 0; 1391 int valid_data = 0; 1392 1393 blob = buffer_get_string(m, &bloblen); 1394 signature = buffer_get_string(m, &signaturelen); 1395 data = buffer_get_string(m, &datalen); 1396 1397 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1398 !monitor_allowed_key(blob, bloblen)) 1399 fatal("%s: bad key, not previously allowed", __func__); 1400 1401 key = key_from_blob(blob, bloblen); 1402 if (key == NULL) 1403 fatal("%s: bad public key blob", __func__); 1404 1405 switch (key_blobtype) { 1406 case MM_USERKEY: 1407 valid_data = monitor_valid_userblob(data, datalen); 1408 break; 1409 case MM_HOSTKEY: 1410 valid_data = monitor_valid_hostbasedblob(data, datalen, 1411 hostbased_cuser, hostbased_chost); 1412 break; 1413 default: 1414 valid_data = 0; 1415 break; 1416 } 1417 if (!valid_data) 1418 fatal("%s: bad signature data blob", __func__); 1419 1420 verified = key_verify(key, signature, signaturelen, data, datalen); 1421 debug3("%s: key %p signature %s", 1422 __func__, key, (verified == 1) ? "verified" : "unverified"); 1423 1424 /* If auth was successful then record key to ensure it isn't reused */ 1425 if (verified == 1 && key_blobtype == MM_USERKEY) 1426 auth2_record_userkey(authctxt, key); 1427 else 1428 key_free(key); 1429 1430 free(blob); 1431 free(signature); 1432 free(data); 1433 1434 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; 1435 1436 monitor_reset_key_state(); 1437 1438 buffer_clear(m); 1439 buffer_put_int(m, verified); 1440 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1441 1442 return (verified == 1); 1443 } 1444 1445 static void 1446 mm_record_login(Session *s, struct passwd *pw) 1447 { 1448 socklen_t fromlen; 1449 struct sockaddr_storage from; 1450 1451 if (options.use_login) 1452 return; 1453 1454 /* 1455 * Get IP address of client. If the connection is not a socket, let 1456 * the address be 0.0.0.0. 1457 */ 1458 memset(&from, 0, sizeof(from)); 1459 fromlen = sizeof(from); 1460 if (packet_connection_is_on_socket()) { 1461 if (getpeername(packet_get_connection_in(), 1462 (struct sockaddr *)&from, &fromlen) < 0) { 1463 debug("getpeername: %.100s", strerror(errno)); 1464 cleanup_exit(255); 1465 } 1466 } 1467 /* Record that there was a login on that tty from the remote host. */ 1468 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1469 get_remote_name_or_ip(utmp_len, options.use_dns), 1470 (struct sockaddr *)&from, fromlen); 1471 } 1472 1473 static void 1474 mm_session_close(Session *s) 1475 { 1476 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1477 if (s->ttyfd != -1) { 1478 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1479 session_pty_cleanup2(s); 1480 } 1481 session_unused(s->self); 1482 } 1483 1484 int 1485 mm_answer_pty(int sock, Buffer *m) 1486 { 1487 extern struct monitor *pmonitor; 1488 Session *s; 1489 int res, fd0; 1490 1491 debug3("%s entering", __func__); 1492 1493 buffer_clear(m); 1494 s = session_new(); 1495 if (s == NULL) 1496 goto error; 1497 s->authctxt = authctxt; 1498 s->pw = authctxt->pw; 1499 s->pid = pmonitor->m_pid; 1500 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1501 if (res == 0) 1502 goto error; 1503 pty_setowner(authctxt->pw, s->tty); 1504 1505 buffer_put_int(m, 1); 1506 buffer_put_cstring(m, s->tty); 1507 1508 /* We need to trick ttyslot */ 1509 if (dup2(s->ttyfd, 0) == -1) 1510 fatal("%s: dup2", __func__); 1511 1512 mm_record_login(s, authctxt->pw); 1513 1514 /* Now we can close the file descriptor again */ 1515 close(0); 1516 1517 /* send messages generated by record_login */ 1518 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); 1519 buffer_clear(&loginmsg); 1520 1521 mm_request_send(sock, MONITOR_ANS_PTY, m); 1522 1523 if (mm_send_fd(sock, s->ptyfd) == -1 || 1524 mm_send_fd(sock, s->ttyfd) == -1) 1525 fatal("%s: send fds failed", __func__); 1526 1527 /* make sure nothing uses fd 0 */ 1528 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1529 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1530 if (fd0 != 0) 1531 error("%s: fd0 %d != 0", __func__, fd0); 1532 1533 /* slave is not needed */ 1534 close(s->ttyfd); 1535 s->ttyfd = s->ptyfd; 1536 /* no need to dup() because nobody closes ptyfd */ 1537 s->ptymaster = s->ptyfd; 1538 1539 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1540 1541 return (0); 1542 1543 error: 1544 if (s != NULL) 1545 mm_session_close(s); 1546 buffer_put_int(m, 0); 1547 mm_request_send(sock, MONITOR_ANS_PTY, m); 1548 return (0); 1549 } 1550 1551 int 1552 mm_answer_pty_cleanup(int sock, Buffer *m) 1553 { 1554 Session *s; 1555 char *tty; 1556 1557 debug3("%s entering", __func__); 1558 1559 tty = buffer_get_string(m, NULL); 1560 if ((s = session_by_tty(tty)) != NULL) 1561 mm_session_close(s); 1562 buffer_clear(m); 1563 free(tty); 1564 return (0); 1565 } 1566 1567 #ifdef WITH_SSH1 1568 int 1569 mm_answer_sesskey(int sock, Buffer *m) 1570 { 1571 BIGNUM *p; 1572 int rsafail; 1573 1574 /* Turn off permissions */ 1575 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0); 1576 1577 if ((p = BN_new()) == NULL) 1578 fatal("%s: BN_new", __func__); 1579 1580 buffer_get_bignum2(m, p); 1581 1582 rsafail = ssh1_session_key(p); 1583 1584 buffer_clear(m); 1585 buffer_put_int(m, rsafail); 1586 buffer_put_bignum2(m, p); 1587 1588 BN_clear_free(p); 1589 1590 mm_request_send(sock, MONITOR_ANS_SESSKEY, m); 1591 1592 /* Turn on permissions for sessid passing */ 1593 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); 1594 1595 return (0); 1596 } 1597 1598 int 1599 mm_answer_sessid(int sock, Buffer *m) 1600 { 1601 int i; 1602 1603 debug3("%s entering", __func__); 1604 1605 if (buffer_len(m) != 16) 1606 fatal("%s: bad ssh1 session id", __func__); 1607 for (i = 0; i < 16; i++) 1608 session_id[i] = buffer_get_char(m); 1609 1610 /* Turn on permissions for getpwnam */ 1611 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 1612 1613 return (0); 1614 } 1615 1616 int 1617 mm_answer_rsa_keyallowed(int sock, Buffer *m) 1618 { 1619 BIGNUM *client_n; 1620 Key *key = NULL; 1621 u_char *blob = NULL; 1622 u_int blen = 0; 1623 int allowed = 0; 1624 1625 debug3("%s entering", __func__); 1626 1627 auth_method = "rsa"; 1628 if (options.rsa_authentication && authctxt->valid) { 1629 if ((client_n = BN_new()) == NULL) 1630 fatal("%s: BN_new", __func__); 1631 buffer_get_bignum2(m, client_n); 1632 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); 1633 BN_clear_free(client_n); 1634 } 1635 buffer_clear(m); 1636 buffer_put_int(m, allowed); 1637 buffer_put_int(m, forced_command != NULL); 1638 1639 /* clear temporarily storage (used by generate challenge) */ 1640 monitor_reset_key_state(); 1641 1642 if (allowed && key != NULL) { 1643 key->type = KEY_RSA; /* cheat for key_to_blob */ 1644 if (key_to_blob(key, &blob, &blen) == 0) 1645 fatal("%s: key_to_blob failed", __func__); 1646 buffer_put_string(m, blob, blen); 1647 1648 /* Save temporarily for comparison in verify */ 1649 key_blob = blob; 1650 key_bloblen = blen; 1651 key_blobtype = MM_RSAUSERKEY; 1652 } 1653 if (key != NULL) 1654 key_free(key); 1655 1656 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m); 1657 1658 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); 1659 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); 1660 return (0); 1661 } 1662 1663 int 1664 mm_answer_rsa_challenge(int sock, Buffer *m) 1665 { 1666 Key *key = NULL; 1667 u_char *blob; 1668 u_int blen; 1669 1670 debug3("%s entering", __func__); 1671 1672 if (!authctxt->valid) 1673 fatal("%s: authctxt not valid", __func__); 1674 blob = buffer_get_string(m, &blen); 1675 if (!monitor_allowed_key(blob, blen)) 1676 fatal("%s: bad key, not previously allowed", __func__); 1677 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1678 fatal("%s: key type mismatch", __func__); 1679 if ((key = key_from_blob(blob, blen)) == NULL) 1680 fatal("%s: received bad key", __func__); 1681 if (key->type != KEY_RSA) 1682 fatal("%s: received bad key type %d", __func__, key->type); 1683 key->type = KEY_RSA1; 1684 if (ssh1_challenge) 1685 BN_clear_free(ssh1_challenge); 1686 ssh1_challenge = auth_rsa_generate_challenge(key); 1687 1688 buffer_clear(m); 1689 buffer_put_bignum2(m, ssh1_challenge); 1690 1691 debug3("%s sending reply", __func__); 1692 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m); 1693 1694 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); 1695 1696 free(blob); 1697 key_free(key); 1698 return (0); 1699 } 1700 1701 int 1702 mm_answer_rsa_response(int sock, Buffer *m) 1703 { 1704 Key *key = NULL; 1705 u_char *blob, *response; 1706 u_int blen, len; 1707 int success; 1708 1709 debug3("%s entering", __func__); 1710 1711 if (!authctxt->valid) 1712 fatal("%s: authctxt not valid", __func__); 1713 if (ssh1_challenge == NULL) 1714 fatal("%s: no ssh1_challenge", __func__); 1715 1716 blob = buffer_get_string(m, &blen); 1717 if (!monitor_allowed_key(blob, blen)) 1718 fatal("%s: bad key, not previously allowed", __func__); 1719 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) 1720 fatal("%s: key type mismatch: %d", __func__, key_blobtype); 1721 if ((key = key_from_blob(blob, blen)) == NULL) 1722 fatal("%s: received bad key", __func__); 1723 response = buffer_get_string(m, &len); 1724 if (len != 16) 1725 fatal("%s: received bad response to challenge", __func__); 1726 success = auth_rsa_verify_response(key, ssh1_challenge, response); 1727 1728 free(blob); 1729 key_free(key); 1730 free(response); 1731 1732 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; 1733 1734 /* reset state */ 1735 BN_clear_free(ssh1_challenge); 1736 ssh1_challenge = NULL; 1737 monitor_reset_key_state(); 1738 1739 buffer_clear(m); 1740 buffer_put_int(m, success); 1741 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m); 1742 1743 return (success); 1744 } 1745 #endif 1746 1747 #ifdef KRB4 1748 int 1749 mm_answer_krb4(int socket, Buffer *m) 1750 { 1751 KTEXT_ST auth, reply; 1752 char *client, *p; 1753 int success; 1754 u_int alen; 1755 1756 reply.length = auth.length = 0; 1757 1758 p = buffer_get_string(m, &alen); 1759 if (alen >= MAX_KTXT_LEN) 1760 fatal("%s: auth too large", __func__); 1761 memcpy(auth.dat, p, alen); 1762 auth.length = alen; 1763 memset(p, 0, alen); 1764 free(p); 1765 1766 success = options.kerberos_authentication && 1767 authctxt->valid && 1768 auth_krb4(authctxt, &auth, &client, &reply); 1769 1770 memset(auth.dat, 0, alen); 1771 buffer_clear(m); 1772 buffer_put_int(m, success); 1773 1774 if (success) { 1775 buffer_put_cstring(m, client); 1776 buffer_put_string(m, reply.dat, reply.length); 1777 if (client) 1778 free(client); 1779 if (reply.length) 1780 memset(reply.dat, 0, reply.length); 1781 } 1782 1783 debug3("%s: sending result %d", __func__, success); 1784 mm_request_send(socket, MONITOR_ANS_KRB4, m); 1785 1786 auth_method = "kerberos"; 1787 1788 /* Causes monitor loop to terminate if authenticated */ 1789 return (success); 1790 } 1791 #endif 1792 1793 #ifdef KRB5 1794 int 1795 mm_answer_krb5(int xsocket, Buffer *m) 1796 { 1797 krb5_data tkt, reply; 1798 char *client_user; 1799 u_int len; 1800 int success; 1801 1802 /* use temporary var to avoid size issues on 64bit arch */ 1803 tkt.data = buffer_get_string(m, &len); 1804 tkt.length = len; 1805 1806 success = options.kerberos_authentication && 1807 authctxt->valid && 1808 auth_krb5(authctxt, &tkt, &client_user, &reply); 1809 1810 if (tkt.length) 1811 free(tkt.data); 1812 1813 buffer_clear(m); 1814 buffer_put_int(m, success); 1815 1816 if (success) { 1817 buffer_put_cstring(m, client_user); 1818 buffer_put_string(m, reply.data, reply.length); 1819 if (client_user) 1820 free(client_user); 1821 if (reply.length) 1822 free(reply.data); 1823 } 1824 mm_request_send(xsocket, MONITOR_ANS_KRB5, m); 1825 1826 auth_method = "kerberos"; 1827 1828 return success; 1829 } 1830 #endif 1831 1832 int 1833 mm_answer_term(int sock, Buffer *req) 1834 { 1835 extern struct monitor *pmonitor; 1836 int res, status; 1837 1838 debug3("%s: tearing down sessions", __func__); 1839 1840 /* The child is terminating */ 1841 session_destroy_all(&mm_session_close); 1842 1843 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1844 if (errno != EINTR) 1845 exit(1); 1846 1847 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1848 1849 /* Terminate process */ 1850 exit(res); 1851 } 1852 1853 void 1854 monitor_apply_keystate(struct monitor *pmonitor) 1855 { 1856 struct ssh *ssh = active_state; /* XXX */ 1857 struct kex *kex; 1858 int r; 1859 1860 debug3("%s: packet_set_state", __func__); 1861 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1862 fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); 1863 sshbuf_free(child_state); 1864 child_state = NULL; 1865 1866 if ((kex = ssh->kex) != NULL) { 1867 /* XXX set callbacks */ 1868 #ifdef WITH_OPENSSL 1869 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1870 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 1871 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1872 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1873 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 1874 #endif 1875 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 1876 kex->load_host_public_key=&get_hostkey_public_by_type; 1877 kex->load_host_private_key=&get_hostkey_private_by_type; 1878 kex->host_key_index=&get_hostkey_index; 1879 kex->sign = sshd_hostkey_sign; 1880 } 1881 1882 /* Update with new address */ 1883 if (options.compression) { 1884 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib, 1885 (ssh_packet_comp_alloc_func *)mm_zalloc, 1886 (ssh_packet_comp_free_func *)mm_zfree); 1887 } 1888 } 1889 1890 /* This function requries careful sanity checking */ 1891 1892 void 1893 mm_get_keystate(struct monitor *pmonitor) 1894 { 1895 debug3("%s: Waiting for new keys", __func__); 1896 1897 if ((child_state = sshbuf_new()) == NULL) 1898 fatal("%s: sshbuf_new failed", __func__); 1899 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1900 child_state); 1901 debug3("%s: GOT new keys", __func__); 1902 } 1903 1904 1905 /* XXX */ 1906 1907 #define FD_CLOSEONEXEC(x) do { \ 1908 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ 1909 fatal("fcntl(%d, F_SETFD)", x); \ 1910 } while (0) 1911 1912 static void 1913 monitor_openfds(struct monitor *mon, int do_logfds) 1914 { 1915 int pair[2]; 1916 1917 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1918 fatal("%s: socketpair: %s", __func__, strerror(errno)); 1919 FD_CLOSEONEXEC(pair[0]); 1920 FD_CLOSEONEXEC(pair[1]); 1921 mon->m_recvfd = pair[0]; 1922 mon->m_sendfd = pair[1]; 1923 1924 if (do_logfds) { 1925 if (pipe(pair) == -1) 1926 fatal("%s: pipe: %s", __func__, strerror(errno)); 1927 FD_CLOSEONEXEC(pair[0]); 1928 FD_CLOSEONEXEC(pair[1]); 1929 mon->m_log_recvfd = pair[0]; 1930 mon->m_log_sendfd = pair[1]; 1931 } else 1932 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1933 } 1934 1935 #define MM_MEMSIZE 65536 1936 1937 struct monitor * 1938 monitor_init(void) 1939 { 1940 struct ssh *ssh = active_state; /* XXX */ 1941 struct monitor *mon; 1942 1943 mon = xcalloc(1, sizeof(*mon)); 1944 1945 monitor_openfds(mon, 1); 1946 1947 /* Used to share zlib space across processes */ 1948 if (options.compression) { 1949 mon->m_zback = mm_create(NULL, MM_MEMSIZE); 1950 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); 1951 1952 /* Compression needs to share state across borders */ 1953 ssh_packet_set_compress_hooks(ssh, mon->m_zlib, 1954 (ssh_packet_comp_alloc_func *)mm_zalloc, 1955 (ssh_packet_comp_free_func *)mm_zfree); 1956 } 1957 1958 return mon; 1959 } 1960 1961 void 1962 monitor_reinit(struct monitor *mon) 1963 { 1964 monitor_openfds(mon, 0); 1965 } 1966 1967 #ifdef GSSAPI 1968 int 1969 mm_answer_gss_setup_ctx(int sock, Buffer *m) 1970 { 1971 gss_OID_desc goid; 1972 OM_uint32 major; 1973 u_int len; 1974 1975 goid.elements = buffer_get_string(m, &len); 1976 goid.length = len; 1977 1978 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1979 1980 free(goid.elements); 1981 1982 buffer_clear(m); 1983 buffer_put_int(m, major); 1984 1985 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1986 1987 /* Now we have a context, enable the step */ 1988 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1989 1990 return (0); 1991 } 1992 1993 int 1994 mm_answer_gss_accept_ctx(int sock, Buffer *m) 1995 { 1996 gss_buffer_desc in; 1997 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 1998 OM_uint32 major, minor; 1999 OM_uint32 flags = 0; /* GSI needs this */ 2000 u_int len; 2001 2002 in.value = buffer_get_string(m, &len); 2003 in.length = len; 2004 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 2005 free(in.value); 2006 2007 buffer_clear(m); 2008 buffer_put_int(m, major); 2009 buffer_put_string(m, out.value, out.length); 2010 buffer_put_int(m, flags); 2011 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 2012 2013 gss_release_buffer(&minor, &out); 2014 2015 if (major == GSS_S_COMPLETE) { 2016 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 2017 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2018 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 2019 } 2020 return (0); 2021 } 2022 2023 int 2024 mm_answer_gss_checkmic(int sock, Buffer *m) 2025 { 2026 gss_buffer_desc gssbuf, mic; 2027 OM_uint32 ret; 2028 u_int len; 2029 2030 gssbuf.value = buffer_get_string(m, &len); 2031 gssbuf.length = len; 2032 mic.value = buffer_get_string(m, &len); 2033 mic.length = len; 2034 2035 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 2036 2037 free(gssbuf.value); 2038 free(mic.value); 2039 2040 buffer_clear(m); 2041 buffer_put_int(m, ret); 2042 2043 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 2044 2045 if (!GSS_ERROR(ret)) 2046 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 2047 2048 return (0); 2049 } 2050 2051 int 2052 mm_answer_gss_userok(int sock, Buffer *m) 2053 { 2054 int authenticated; 2055 2056 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 2057 2058 buffer_clear(m); 2059 buffer_put_int(m, authenticated); 2060 2061 debug3("%s: sending result %d", __func__, authenticated); 2062 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 2063 2064 auth_method = "gssapi-with-mic"; 2065 2066 /* Monitor loop will terminate if authenticated */ 2067 return (authenticated); 2068 } 2069 #endif /* GSSAPI */ 2070 2071