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