1 /* $OpenBSD: monitor.c,v 1.188 2018/11/16 02:43:56 djm Exp $ */ 2 /* 3 * Copyright 2002 Niels Provos <provos@citi.umich.edu> 4 * Copyright 2002 Markus Friedl <markus@openbsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/wait.h> 30 #include <sys/socket.h> 31 #include <sys/tree.h> 32 #include <sys/queue.h> 33 34 #ifdef WITH_OPENSSL 35 #include <openssl/dh.h> 36 #endif 37 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <limits.h> 41 #include <paths.h> 42 #include <poll.h> 43 #include <pwd.h> 44 #include <signal.h> 45 #include <stdarg.h> 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "atomicio.h" 53 #include "xmalloc.h" 54 #include "ssh.h" 55 #include "sshkey.h" 56 #include "sshbuf.h" 57 #include "hostfile.h" 58 #include "auth.h" 59 #include "cipher.h" 60 #include "kex.h" 61 #include "dh.h" 62 #include "packet.h" 63 #include "auth-options.h" 64 #include "sshpty.h" 65 #include "channels.h" 66 #include "session.h" 67 #include "sshlogin.h" 68 #include "canohost.h" 69 #include "log.h" 70 #include "misc.h" 71 #include "servconf.h" 72 #include "monitor.h" 73 #ifdef GSSAPI 74 #include "ssh-gss.h" 75 #endif 76 #include "monitor_wrap.h" 77 #include "monitor_fdpass.h" 78 #include "compat.h" 79 #include "ssh2.h" 80 #include "authfd.h" 81 #include "match.h" 82 #include "ssherr.h" 83 84 #ifdef GSSAPI 85 static Gssctxt *gsscontext = NULL; 86 #endif 87 88 /* Imports */ 89 extern ServerOptions options; 90 extern u_int utmp_len; 91 extern u_char session_id[]; 92 extern struct sshbuf *loginmsg; 93 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */ 94 95 /* State exported from the child */ 96 static struct sshbuf *child_state; 97 98 /* Functions on the monitor that answer unprivileged requests */ 99 100 int mm_answer_moduli(int, struct sshbuf *); 101 int mm_answer_sign(int, struct sshbuf *); 102 int mm_answer_pwnamallow(int, struct sshbuf *); 103 int mm_answer_auth2_read_banner(int, struct sshbuf *); 104 int mm_answer_authserv(int, struct sshbuf *); 105 int mm_answer_authpassword(int, struct sshbuf *); 106 int mm_answer_bsdauthquery(int, struct sshbuf *); 107 int mm_answer_bsdauthrespond(int, struct sshbuf *); 108 int mm_answer_skeyquery(int, struct sshbuf *); 109 int mm_answer_skeyrespond(int, struct sshbuf *); 110 int mm_answer_keyallowed(int, struct sshbuf *); 111 int mm_answer_keyverify(int, struct sshbuf *); 112 int mm_answer_pty(int, struct sshbuf *); 113 int mm_answer_pty_cleanup(int, struct sshbuf *); 114 int mm_answer_term(int, struct sshbuf *); 115 int mm_answer_rsa_keyallowed(int, struct sshbuf *); 116 int mm_answer_rsa_challenge(int, struct sshbuf *); 117 int mm_answer_rsa_response(int, struct sshbuf *); 118 int mm_answer_sesskey(int, struct sshbuf *); 119 int mm_answer_sessid(int, struct sshbuf *); 120 121 #ifdef GSSAPI 122 int mm_answer_gss_setup_ctx(int, struct sshbuf *); 123 int mm_answer_gss_accept_ctx(int, struct sshbuf *); 124 int mm_answer_gss_userok(int, struct sshbuf *); 125 int mm_answer_gss_checkmic(int, struct sshbuf *); 126 #endif 127 128 static int monitor_read_log(struct monitor *); 129 130 static Authctxt *authctxt; 131 132 /* local state for key verify */ 133 static u_char *key_blob = NULL; 134 static size_t key_bloblen = 0; 135 static int key_blobtype = MM_NOKEY; 136 static struct sshauthopt *key_opts = NULL; 137 static char *hostbased_cuser = NULL; 138 static char *hostbased_chost = NULL; 139 static char *auth_method = "unknown"; 140 static char *auth_submethod = NULL; 141 static u_int session_id2_len = 0; 142 static u_char *session_id2 = NULL; 143 static pid_t monitor_child_pid; 144 145 struct mon_table { 146 enum monitor_reqtype type; 147 int flags; 148 int (*f)(int, struct sshbuf *); 149 }; 150 151 #define MON_ISAUTH 0x0004 /* Required for Authentication */ 152 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ 153 #define MON_ONCE 0x0010 /* Disable after calling */ 154 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ 155 156 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) 157 158 #define MON_PERMIT 0x1000 /* Request is permitted */ 159 160 struct mon_table mon_dispatch_proto20[] = { 161 #ifdef WITH_OPENSSL 162 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, 163 #endif 164 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, 165 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, 166 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, 167 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, 168 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 169 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 170 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, 171 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, 172 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, 173 #ifdef GSSAPI 174 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, 175 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx}, 176 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok}, 177 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic}, 178 #endif 179 {0, 0, NULL} 180 }; 181 182 struct mon_table mon_dispatch_postauth20[] = { 183 #ifdef WITH_OPENSSL 184 {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, 185 #endif 186 {MONITOR_REQ_SIGN, 0, mm_answer_sign}, 187 {MONITOR_REQ_PTY, 0, mm_answer_pty}, 188 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, 189 {MONITOR_REQ_TERM, 0, mm_answer_term}, 190 {0, 0, NULL} 191 }; 192 193 struct mon_table *mon_dispatch; 194 195 /* Specifies if a certain message is allowed at the moment */ 196 static void 197 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) 198 { 199 while (ent->f != NULL) { 200 if (ent->type == type) { 201 ent->flags &= ~MON_PERMIT; 202 ent->flags |= permit ? MON_PERMIT : 0; 203 return; 204 } 205 ent++; 206 } 207 } 208 209 static void 210 monitor_permit_authentications(int permit) 211 { 212 struct mon_table *ent = mon_dispatch; 213 214 while (ent->f != NULL) { 215 if (ent->flags & MON_AUTH) { 216 ent->flags &= ~MON_PERMIT; 217 ent->flags |= permit ? MON_PERMIT : 0; 218 } 219 ent++; 220 } 221 } 222 223 void 224 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) 225 { 226 struct ssh *ssh = active_state; /* XXX */ 227 struct mon_table *ent; 228 int authenticated = 0, partial = 0; 229 230 debug3("preauth child monitor started"); 231 232 if (pmonitor->m_recvfd >= 0) 233 close(pmonitor->m_recvfd); 234 if (pmonitor->m_log_sendfd >= 0) 235 close(pmonitor->m_log_sendfd); 236 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; 237 238 authctxt = _authctxt; 239 memset(authctxt, 0, sizeof(*authctxt)); 240 ssh->authctxt = authctxt; 241 242 mon_dispatch = mon_dispatch_proto20; 243 /* Permit requests for moduli and signatures */ 244 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 245 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 246 247 /* The first few requests do not require asynchronous access */ 248 while (!authenticated) { 249 partial = 0; 250 auth_method = "unknown"; 251 auth_submethod = NULL; 252 auth2_authctxt_reset_info(authctxt); 253 254 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); 255 256 /* Special handling for multiple required authentications */ 257 if (options.num_auth_methods != 0) { 258 if (authenticated && 259 !auth2_update_methods_lists(authctxt, 260 auth_method, auth_submethod)) { 261 debug3("%s: method %s: partial", __func__, 262 auth_method); 263 authenticated = 0; 264 partial = 1; 265 } 266 } 267 268 if (authenticated) { 269 if (!(ent->flags & MON_AUTHDECIDE)) 270 fatal("%s: unexpected authentication from %d", 271 __func__, ent->type); 272 if (authctxt->pw->pw_uid == 0 && 273 !auth_root_allowed(ssh, auth_method)) 274 authenticated = 0; 275 } 276 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { 277 auth_log(authctxt, authenticated, partial, 278 auth_method, auth_submethod); 279 if (!partial && !authenticated) 280 authctxt->failures++; 281 if (authenticated || partial) { 282 auth2_update_session_info(authctxt, 283 auth_method, auth_submethod); 284 } 285 } 286 } 287 288 if (!authctxt->valid) 289 fatal("%s: authenticated invalid user", __func__); 290 if (strcmp(auth_method, "unknown") == 0) 291 fatal("%s: authentication method name unknown", __func__); 292 293 debug("%s: %s has been authenticated by privileged process", 294 __func__, authctxt->user); 295 ssh->authctxt = NULL; 296 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user); 297 298 mm_get_keystate(pmonitor); 299 300 /* Drain any buffered messages from the child */ 301 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) 302 ; 303 304 if (pmonitor->m_recvfd >= 0) 305 close(pmonitor->m_recvfd); 306 if (pmonitor->m_log_sendfd >= 0) 307 close(pmonitor->m_log_sendfd); 308 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; 309 } 310 311 static void 312 monitor_set_child_handler(pid_t pid) 313 { 314 monitor_child_pid = pid; 315 } 316 317 static void 318 monitor_child_handler(int sig) 319 { 320 kill(monitor_child_pid, sig); 321 } 322 323 void 324 monitor_child_postauth(struct monitor *pmonitor) 325 { 326 close(pmonitor->m_recvfd); 327 pmonitor->m_recvfd = -1; 328 329 monitor_set_child_handler(pmonitor->m_pid); 330 signal(SIGHUP, &monitor_child_handler); 331 signal(SIGTERM, &monitor_child_handler); 332 signal(SIGINT, &monitor_child_handler); 333 334 mon_dispatch = mon_dispatch_postauth20; 335 336 /* Permit requests for moduli and signatures */ 337 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 338 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 339 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 340 341 if (auth_opts->permit_pty_flag) { 342 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); 343 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); 344 } 345 346 for (;;) 347 monitor_read(pmonitor, mon_dispatch, NULL); 348 } 349 350 static int 351 monitor_read_log(struct monitor *pmonitor) 352 { 353 struct sshbuf *logmsg; 354 u_int len, level; 355 char *msg; 356 u_char *p; 357 int r; 358 359 if ((logmsg = sshbuf_new()) == NULL) 360 fatal("%s: sshbuf_new", __func__); 361 362 /* Read length */ 363 if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0) 364 fatal("%s: reserve: %s", __func__, ssh_err(r)); 365 if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) { 366 if (errno == EPIPE) { 367 sshbuf_free(logmsg); 368 debug("%s: child log fd closed", __func__); 369 close(pmonitor->m_log_recvfd); 370 pmonitor->m_log_recvfd = -1; 371 return -1; 372 } 373 fatal("%s: log fd read: %s", __func__, strerror(errno)); 374 } 375 if ((r = sshbuf_get_u32(logmsg, &len)) != 0) 376 fatal("%s: get len: %s", __func__, ssh_err(r)); 377 if (len <= 4 || len > 8192) 378 fatal("%s: invalid log message length %u", __func__, len); 379 380 /* Read severity, message */ 381 sshbuf_reset(logmsg); 382 if ((r = sshbuf_reserve(logmsg, len, &p)) != 0) 383 fatal("%s: reserve: %s", __func__, ssh_err(r)); 384 if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len) 385 fatal("%s: log fd read: %s", __func__, strerror(errno)); 386 if ((r = sshbuf_get_u32(logmsg, &level)) != 0 || 387 (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0) 388 fatal("%s: decode: %s", __func__, ssh_err(r)); 389 390 /* Log it */ 391 if (log_level_name(level) == NULL) 392 fatal("%s: invalid log level %u (corrupted message?)", 393 __func__, level); 394 do_log2(level, "%s [preauth]", msg); 395 396 sshbuf_free(logmsg); 397 free(msg); 398 399 return 0; 400 } 401 402 int 403 monitor_read(struct monitor *pmonitor, struct mon_table *ent, 404 struct mon_table **pent) 405 { 406 struct sshbuf *m; 407 int r, ret; 408 u_char type; 409 struct pollfd pfd[2]; 410 411 for (;;) { 412 memset(&pfd, 0, sizeof(pfd)); 413 pfd[0].fd = pmonitor->m_sendfd; 414 pfd[0].events = POLLIN; 415 pfd[1].fd = pmonitor->m_log_recvfd; 416 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; 417 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { 418 if (errno == EINTR || errno == EAGAIN) 419 continue; 420 fatal("%s: poll: %s", __func__, strerror(errno)); 421 } 422 if (pfd[1].revents) { 423 /* 424 * Drain all log messages before processing next 425 * monitor request. 426 */ 427 monitor_read_log(pmonitor); 428 continue; 429 } 430 if (pfd[0].revents) 431 break; /* Continues below */ 432 } 433 434 if ((m = sshbuf_new()) == NULL) 435 fatal("%s: sshbuf_new", __func__); 436 437 mm_request_receive(pmonitor->m_sendfd, m); 438 if ((r = sshbuf_get_u8(m, &type)) != 0) 439 fatal("%s: decode: %s", __func__, ssh_err(r)); 440 441 debug3("%s: checking request %d", __func__, type); 442 443 while (ent->f != NULL) { 444 if (ent->type == type) 445 break; 446 ent++; 447 } 448 449 if (ent->f != NULL) { 450 if (!(ent->flags & MON_PERMIT)) 451 fatal("%s: unpermitted request %d", __func__, 452 type); 453 ret = (*ent->f)(pmonitor->m_sendfd, m); 454 sshbuf_free(m); 455 456 /* The child may use this request only once, disable it */ 457 if (ent->flags & MON_ONCE) { 458 debug2("%s: %d used once, disabling now", __func__, 459 type); 460 ent->flags &= ~MON_PERMIT; 461 } 462 463 if (pent != NULL) 464 *pent = ent; 465 466 return ret; 467 } 468 469 fatal("%s: unsupported request: %d", __func__, type); 470 471 /* NOTREACHED */ 472 return (-1); 473 } 474 475 /* allowed key state */ 476 static int 477 monitor_allowed_key(u_char *blob, u_int bloblen) 478 { 479 /* make sure key is allowed */ 480 if (key_blob == NULL || key_bloblen != bloblen || 481 timingsafe_bcmp(key_blob, blob, key_bloblen)) 482 return (0); 483 return (1); 484 } 485 486 static void 487 monitor_reset_key_state(void) 488 { 489 /* reset state */ 490 free(key_blob); 491 free(hostbased_cuser); 492 free(hostbased_chost); 493 sshauthopt_free(key_opts); 494 key_blob = NULL; 495 key_bloblen = 0; 496 key_blobtype = MM_NOKEY; 497 key_opts = NULL; 498 hostbased_cuser = NULL; 499 hostbased_chost = NULL; 500 } 501 502 #ifdef WITH_OPENSSL 503 int 504 mm_answer_moduli(int sock, struct sshbuf *m) 505 { 506 DH *dh; 507 const BIGNUM *dh_p, *dh_g; 508 int r; 509 u_int min, want, max; 510 511 if ((r = sshbuf_get_u32(m, &min)) != 0 || 512 (r = sshbuf_get_u32(m, &want)) != 0 || 513 (r = sshbuf_get_u32(m, &max)) != 0) 514 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 515 516 debug3("%s: got parameters: %d %d %d", 517 __func__, min, want, max); 518 /* We need to check here, too, in case the child got corrupted */ 519 if (max < min || want < min || max < want) 520 fatal("%s: bad parameters: %d %d %d", 521 __func__, min, want, max); 522 523 sshbuf_reset(m); 524 525 dh = choose_dh(min, want, max); 526 if (dh == NULL) { 527 if ((r = sshbuf_put_u8(m, 0)) != 0) 528 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 529 return (0); 530 } else { 531 /* Send first bignum */ 532 DH_get0_pqg(dh, &dh_p, NULL, &dh_g); 533 if ((r = sshbuf_put_u8(m, 1)) != 0 || 534 (r = sshbuf_put_bignum2(m, dh_p)) != 0 || 535 (r = sshbuf_put_bignum2(m, dh_g)) != 0) 536 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 537 538 DH_free(dh); 539 } 540 mm_request_send(sock, MONITOR_ANS_MODULI, m); 541 return (0); 542 } 543 #endif 544 545 int 546 mm_answer_sign(int sock, struct sshbuf *m) 547 { 548 struct ssh *ssh = active_state; /* XXX */ 549 extern int auth_sock; /* XXX move to state struct? */ 550 struct sshkey *key; 551 struct sshbuf *sigbuf = NULL; 552 u_char *p = NULL, *signature = NULL; 553 char *alg = NULL; 554 size_t datlen, siglen, alglen; 555 int r, is_proof = 0; 556 u_int keyid, compat; 557 const char proof_req[] = "hostkeys-prove-00@openssh.com"; 558 559 debug3("%s", __func__); 560 561 if ((r = sshbuf_get_u32(m, &keyid)) != 0 || 562 (r = sshbuf_get_string(m, &p, &datlen)) != 0 || 563 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 || 564 (r = sshbuf_get_u32(m, &compat)) != 0) 565 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 566 if (keyid > INT_MAX) 567 fatal("%s: invalid key ID", __func__); 568 569 /* 570 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), 571 * SHA384 (48 bytes) and SHA512 (64 bytes). 572 * 573 * Otherwise, verify the signature request is for a hostkey 574 * proof. 575 * 576 * XXX perform similar check for KEX signature requests too? 577 * it's not trivial, since what is signed is the hash, rather 578 * than the full kex structure... 579 */ 580 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) { 581 /* 582 * Construct expected hostkey proof and compare it to what 583 * the client sent us. 584 */ 585 if (session_id2_len == 0) /* hostkeys is never first */ 586 fatal("%s: bad data length: %zu", __func__, datlen); 587 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL) 588 fatal("%s: no hostkey for index %d", __func__, keyid); 589 if ((sigbuf = sshbuf_new()) == NULL) 590 fatal("%s: sshbuf_new", __func__); 591 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 || 592 (r = sshbuf_put_string(sigbuf, session_id2, 593 session_id2_len)) != 0 || 594 (r = sshkey_puts(key, sigbuf)) != 0) 595 fatal("%s: couldn't prepare private key " 596 "proof buffer: %s", __func__, ssh_err(r)); 597 if (datlen != sshbuf_len(sigbuf) || 598 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0) 599 fatal("%s: bad data length: %zu, hostkey proof len %zu", 600 __func__, datlen, sshbuf_len(sigbuf)); 601 sshbuf_free(sigbuf); 602 is_proof = 1; 603 } 604 605 /* save session id, it will be passed on the first call */ 606 if (session_id2_len == 0) { 607 session_id2_len = datlen; 608 session_id2 = xmalloc(session_id2_len); 609 memcpy(session_id2, p, session_id2_len); 610 } 611 612 if ((key = get_hostkey_by_index(keyid)) != NULL) { 613 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg, 614 compat)) != 0) 615 fatal("%s: sshkey_sign failed: %s", 616 __func__, ssh_err(r)); 617 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL && 618 auth_sock > 0) { 619 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen, 620 p, datlen, alg, compat)) != 0) { 621 fatal("%s: ssh_agent_sign failed: %s", 622 __func__, ssh_err(r)); 623 } 624 } else 625 fatal("%s: no hostkey from index %d", __func__, keyid); 626 627 debug3("%s: %s signature %p(%zu)", __func__, 628 is_proof ? "KEX" : "hostkey proof", signature, siglen); 629 630 sshbuf_reset(m); 631 if ((r = sshbuf_put_string(m, signature, siglen)) != 0) 632 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 633 634 free(alg); 635 free(p); 636 free(signature); 637 638 mm_request_send(sock, MONITOR_ANS_SIGN, m); 639 640 /* Turn on permissions for getpwnam */ 641 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); 642 643 return (0); 644 } 645 646 /* Retrieves the password entry and also checks if the user is permitted */ 647 648 int 649 mm_answer_pwnamallow(int sock, struct sshbuf *m) 650 { 651 struct ssh *ssh = active_state; /* XXX */ 652 char *username; 653 struct passwd *pwent; 654 int r, allowed = 0; 655 u_int i; 656 657 debug3("%s", __func__); 658 659 if (authctxt->attempt++ != 0) 660 fatal("%s: multiple attempts for getpwnam", __func__); 661 662 if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0) 663 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 664 665 pwent = getpwnamallow(username); 666 667 authctxt->user = xstrdup(username); 668 setproctitle("%s [priv]", pwent ? username : "unknown"); 669 free(username); 670 671 sshbuf_reset(m); 672 673 if (pwent == NULL) { 674 if ((r = sshbuf_put_u8(m, 0)) != 0) 675 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 676 authctxt->pw = fakepw(); 677 goto out; 678 } 679 680 allowed = 1; 681 authctxt->pw = pwent; 682 authctxt->valid = 1; 683 684 /* XXX don't sent pwent to unpriv; send fake class/dir/shell too */ 685 if ((r = sshbuf_put_u8(m, 1)) != 0 || 686 (r = sshbuf_put_string(m, pwent, sizeof(*pwent))) != 0 || 687 (r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 || 688 (r = sshbuf_put_cstring(m, "*")) != 0 || 689 (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 || 690 (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 || 691 (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 || 692 (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0) 693 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 694 695 out: 696 ssh_packet_set_log_preamble(ssh, "%suser %s", 697 authctxt->valid ? "authenticating" : "invalid ", authctxt->user); 698 if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0) 699 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 700 701 #define M_CP_STROPT(x) do { \ 702 if (options.x != NULL) { \ 703 if ((r = sshbuf_put_cstring(m, options.x)) != 0) \ 704 fatal("%s: buffer error: %s", \ 705 __func__, ssh_err(r)); \ 706 } \ 707 } while (0) 708 #define M_CP_STRARRAYOPT(x, nx) do { \ 709 for (i = 0; i < options.nx; i++) { \ 710 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \ 711 fatal("%s: buffer error: %s", \ 712 __func__, ssh_err(r)); \ 713 } \ 714 } while (0) 715 /* See comment in servconf.h */ 716 COPY_MATCH_STRING_OPTS(); 717 #undef M_CP_STROPT 718 #undef M_CP_STRARRAYOPT 719 720 /* Create valid auth method lists */ 721 if (auth2_setup_methods_lists(authctxt) != 0) { 722 /* 723 * The monitor will continue long enough to let the child 724 * run to it's packet_disconnect(), but it must not allow any 725 * authentication to succeed. 726 */ 727 debug("%s: no valid authentication method lists", __func__); 728 } 729 730 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); 731 mm_request_send(sock, MONITOR_ANS_PWNAM, m); 732 733 /* Allow service/style information on the auth context */ 734 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); 735 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); 736 737 return (0); 738 } 739 740 int mm_answer_auth2_read_banner(int sock, struct sshbuf *m) 741 { 742 char *banner; 743 int r; 744 745 sshbuf_reset(m); 746 banner = auth2_read_banner(); 747 if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0) 748 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 749 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); 750 free(banner); 751 752 return (0); 753 } 754 755 int 756 mm_answer_authserv(int sock, struct sshbuf *m) 757 { 758 int r; 759 760 monitor_permit_authentications(1); 761 762 if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 || 763 (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0) 764 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 765 debug3("%s: service=%s, style=%s", 766 __func__, authctxt->service, authctxt->style); 767 768 if (strlen(authctxt->style) == 0) { 769 free(authctxt->style); 770 authctxt->style = NULL; 771 } 772 773 return (0); 774 } 775 776 int 777 mm_answer_authpassword(int sock, struct sshbuf *m) 778 { 779 struct ssh *ssh = active_state; /* XXX */ 780 static int call_count; 781 char *passwd; 782 int r, authenticated; 783 size_t plen; 784 785 if (!options.password_authentication) 786 fatal("%s: password authentication not enabled", __func__); 787 if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0) 788 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 789 /* Only authenticate if the context is valid */ 790 authenticated = options.password_authentication && 791 auth_password(ssh, passwd); 792 explicit_bzero(passwd, plen); 793 free(passwd); 794 795 sshbuf_reset(m); 796 if ((r = sshbuf_put_u32(m, authenticated)) != 0) 797 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 798 799 debug3("%s: sending result %d", __func__, authenticated); 800 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); 801 802 call_count++; 803 if (plen == 0 && call_count == 1) 804 auth_method = "none"; 805 else 806 auth_method = "password"; 807 808 /* Causes monitor loop to terminate if authenticated */ 809 return (authenticated); 810 } 811 812 int 813 mm_answer_bsdauthquery(int sock, struct sshbuf *m) 814 { 815 char *name, *infotxt; 816 u_int numprompts, *echo_on, success; 817 char **prompts; 818 int r; 819 820 if (!options.kbd_interactive_authentication) 821 fatal("%s: kbd-int authentication not enabled", __func__); 822 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, 823 &prompts, &echo_on) < 0 ? 0 : 1; 824 825 sshbuf_reset(m); 826 if ((r = sshbuf_put_u32(m, success)) != 0) 827 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 828 if (success) { 829 if ((r = sshbuf_put_cstring(m, prompts[0])) != 0) 830 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 831 } 832 833 debug3("%s: sending challenge success: %u", __func__, success); 834 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); 835 836 if (success) { 837 free(name); 838 free(infotxt); 839 free(prompts); 840 free(echo_on); 841 } 842 843 return (0); 844 } 845 846 int 847 mm_answer_bsdauthrespond(int sock, struct sshbuf *m) 848 { 849 char *response; 850 int r, authok; 851 852 if (!options.kbd_interactive_authentication) 853 fatal("%s: kbd-int authentication not enabled", __func__); 854 if (authctxt->as == NULL) 855 fatal("%s: no bsd auth session", __func__); 856 857 if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0) 858 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 859 authok = options.challenge_response_authentication && 860 auth_userresponse(authctxt->as, response, 0); 861 authctxt->as = NULL; 862 debug3("%s: <%s> = <%d>", __func__, response, authok); 863 free(response); 864 865 sshbuf_reset(m); 866 if ((r = sshbuf_put_u32(m, authok)) != 0) 867 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 868 869 debug3("%s: sending authenticated: %d", __func__, authok); 870 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); 871 872 auth_method = "keyboard-interactive"; 873 auth_submethod = "bsdauth"; 874 875 return (authok != 0); 876 } 877 878 /* 879 * Check that the key type appears in the supplied pattern list, ignoring 880 * mismatches in the signature algorithm. (Signature algorithm checks are 881 * performed in the unprivileged authentication code). 882 * Returns 1 on success, 0 otherwise. 883 */ 884 static int 885 key_base_type_match(const char *method, const struct sshkey *key, 886 const char *list) 887 { 888 char *s, *l, *ol = xstrdup(list); 889 int found = 0; 890 891 l = ol; 892 for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) { 893 if (sshkey_type_from_name(s) == key->type) { 894 found = 1; 895 break; 896 } 897 } 898 if (!found) { 899 error("%s key type %s is not in permitted list %s", method, 900 sshkey_ssh_name(key), list); 901 } 902 903 free(ol); 904 return found; 905 } 906 907 int 908 mm_answer_keyallowed(int sock, struct sshbuf *m) 909 { 910 struct ssh *ssh = active_state; /* XXX */ 911 struct sshkey *key = NULL; 912 char *cuser, *chost; 913 u_int pubkey_auth_attempt; 914 enum mm_keytype type = 0; 915 int r, allowed = 0; 916 struct sshauthopt *opts = NULL; 917 918 debug3("%s entering", __func__); 919 if ((r = sshbuf_get_u32(m, &type)) != 0 || 920 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 || 921 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 || 922 (r = sshkey_froms(m, &key)) != 0 || 923 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0) 924 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 925 926 debug3("%s: key_from_blob: %p", __func__, key); 927 928 if (key != NULL && authctxt->valid) { 929 /* These should not make it past the privsep child */ 930 if (sshkey_type_plain(key->type) == KEY_RSA && 931 (datafellows & SSH_BUG_RSASIGMD5) != 0) 932 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); 933 934 switch (type) { 935 case MM_USERKEY: 936 auth_method = "publickey"; 937 if (!options.pubkey_authentication) 938 break; 939 if (auth2_key_already_used(authctxt, key)) 940 break; 941 if (!key_base_type_match(auth_method, key, 942 options.pubkey_key_types)) 943 break; 944 allowed = user_key_allowed(ssh, authctxt->pw, key, 945 pubkey_auth_attempt, &opts); 946 break; 947 case MM_HOSTKEY: 948 auth_method = "hostbased"; 949 if (!options.hostbased_authentication) 950 break; 951 if (auth2_key_already_used(authctxt, key)) 952 break; 953 if (!key_base_type_match(auth_method, key, 954 options.hostbased_key_types)) 955 break; 956 allowed = hostbased_key_allowed(authctxt->pw, 957 cuser, chost, key); 958 auth2_record_info(authctxt, 959 "client user \"%.100s\", client host \"%.100s\"", 960 cuser, chost); 961 break; 962 default: 963 fatal("%s: unknown key type %d", __func__, type); 964 break; 965 } 966 } 967 968 debug3("%s: %s authentication%s: %s key is %s", __func__, 969 auth_method, pubkey_auth_attempt ? "" : " test", 970 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key), 971 allowed ? "allowed" : "not allowed"); 972 973 auth2_record_key(authctxt, 0, key); 974 975 /* clear temporarily storage (used by verify) */ 976 monitor_reset_key_state(); 977 978 if (allowed) { 979 /* Save temporarily for comparison in verify */ 980 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0) 981 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 982 key_blobtype = type; 983 key_opts = opts; 984 hostbased_cuser = cuser; 985 hostbased_chost = chost; 986 } else { 987 /* Log failed attempt */ 988 auth_log(authctxt, 0, 0, auth_method, NULL); 989 free(cuser); 990 free(chost); 991 } 992 sshkey_free(key); 993 994 sshbuf_reset(m); 995 if ((r = sshbuf_put_u32(m, allowed)) != 0) 996 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 997 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0) 998 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r)); 999 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 1000 1001 if (!allowed) 1002 sshauthopt_free(opts); 1003 1004 return (0); 1005 } 1006 1007 static int 1008 monitor_valid_userblob(u_char *data, u_int datalen) 1009 { 1010 struct sshbuf *b; 1011 const u_char *p; 1012 char *userstyle, *cp; 1013 size_t len; 1014 u_char type; 1015 int r, fail = 0; 1016 1017 if ((b = sshbuf_new()) == NULL) 1018 fatal("%s: sshbuf_new", __func__); 1019 if ((r = sshbuf_put(b, data, datalen)) != 0) 1020 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1021 1022 if (datafellows & SSH_OLD_SESSIONID) { 1023 p = sshbuf_ptr(b); 1024 len = sshbuf_len(b); 1025 if ((session_id2 == NULL) || 1026 (len < session_id2_len) || 1027 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1028 fail++; 1029 if ((r = sshbuf_consume(b, session_id2_len)) != 0) 1030 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1031 } else { 1032 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1033 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1034 if ((session_id2 == NULL) || 1035 (len != session_id2_len) || 1036 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1037 fail++; 1038 } 1039 if ((r = sshbuf_get_u8(b, &type)) != 0) 1040 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1041 if (type != SSH2_MSG_USERAUTH_REQUEST) 1042 fail++; 1043 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1044 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1045 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1046 authctxt->style ? ":" : "", 1047 authctxt->style ? authctxt->style : ""); 1048 if (strcmp(userstyle, cp) != 0) { 1049 logit("wrong user name passed to monitor: " 1050 "expected %s != %.100s", userstyle, cp); 1051 fail++; 1052 } 1053 free(userstyle); 1054 free(cp); 1055 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1056 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1057 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1058 if (strcmp("publickey", cp) != 0) 1059 fail++; 1060 free(cp); 1061 if ((r = sshbuf_get_u8(b, &type)) != 0) 1062 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1063 if (type == 0) 1064 fail++; 1065 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1066 (r = sshbuf_skip_string(b)) != 0) /* pkblob */ 1067 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1068 if (sshbuf_len(b) != 0) 1069 fail++; 1070 sshbuf_free(b); 1071 return (fail == 0); 1072 } 1073 1074 static int 1075 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1076 char *chost) 1077 { 1078 struct sshbuf *b; 1079 const u_char *p; 1080 char *cp, *userstyle; 1081 size_t len; 1082 int r, fail = 0; 1083 u_char type; 1084 1085 if ((b = sshbuf_new()) == NULL) 1086 fatal("%s: sshbuf_new", __func__); 1087 if ((r = sshbuf_put(b, data, datalen)) != 0 || 1088 (r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1089 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1090 1091 if ((session_id2 == NULL) || 1092 (len != session_id2_len) || 1093 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1094 fail++; 1095 1096 if ((r = sshbuf_get_u8(b, &type)) != 0) 1097 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1098 if (type != SSH2_MSG_USERAUTH_REQUEST) 1099 fail++; 1100 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1101 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1102 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1103 authctxt->style ? ":" : "", 1104 authctxt->style ? authctxt->style : ""); 1105 if (strcmp(userstyle, cp) != 0) { 1106 logit("wrong user name passed to monitor: " 1107 "expected %s != %.100s", userstyle, cp); 1108 fail++; 1109 } 1110 free(userstyle); 1111 free(cp); 1112 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1113 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1114 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1115 if (strcmp(cp, "hostbased") != 0) 1116 fail++; 1117 free(cp); 1118 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1119 (r = sshbuf_skip_string(b)) != 0) /* pkblob */ 1120 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1121 1122 /* verify client host, strip trailing dot if necessary */ 1123 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1124 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1125 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.') 1126 cp[len - 1] = '\0'; 1127 if (strcmp(cp, chost) != 0) 1128 fail++; 1129 free(cp); 1130 1131 /* verify client user */ 1132 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1133 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1134 if (strcmp(cp, cuser) != 0) 1135 fail++; 1136 free(cp); 1137 1138 if (sshbuf_len(b) != 0) 1139 fail++; 1140 sshbuf_free(b); 1141 return (fail == 0); 1142 } 1143 1144 int 1145 mm_answer_keyverify(int sock, struct sshbuf *m) 1146 { 1147 struct ssh *ssh = active_state; /* XXX */ 1148 struct sshkey *key; 1149 u_char *signature, *data, *blob; 1150 char *sigalg; 1151 size_t signaturelen, datalen, bloblen; 1152 int r, ret, valid_data = 0, encoded_ret; 1153 1154 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 || 1155 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 || 1156 (r = sshbuf_get_string(m, &data, &datalen)) != 0 || 1157 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0) 1158 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1159 1160 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1161 !monitor_allowed_key(blob, bloblen)) 1162 fatal("%s: bad key, not previously allowed", __func__); 1163 1164 /* Empty signature algorithm means NULL. */ 1165 if (*sigalg == '\0') { 1166 free(sigalg); 1167 sigalg = NULL; 1168 } 1169 1170 /* XXX use sshkey_froms here; need to change key_blob, etc. */ 1171 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0) 1172 fatal("%s: bad public key blob: %s", __func__, ssh_err(r)); 1173 1174 switch (key_blobtype) { 1175 case MM_USERKEY: 1176 valid_data = monitor_valid_userblob(data, datalen); 1177 auth_method = "publickey"; 1178 break; 1179 case MM_HOSTKEY: 1180 valid_data = monitor_valid_hostbasedblob(data, datalen, 1181 hostbased_cuser, hostbased_chost); 1182 auth_method = "hostbased"; 1183 break; 1184 default: 1185 valid_data = 0; 1186 break; 1187 } 1188 if (!valid_data) 1189 fatal("%s: bad signature data blob", __func__); 1190 1191 ret = sshkey_verify(key, signature, signaturelen, data, datalen, 1192 sigalg, active_state->compat); 1193 debug3("%s: %s %p signature %s", __func__, auth_method, key, 1194 (ret == 0) ? "verified" : "unverified"); 1195 auth2_record_key(authctxt, ret == 0, key); 1196 1197 free(blob); 1198 free(signature); 1199 free(data); 1200 free(sigalg); 1201 1202 if (key_blobtype == MM_USERKEY) 1203 auth_activate_options(ssh, key_opts); 1204 monitor_reset_key_state(); 1205 1206 sshkey_free(key); 1207 sshbuf_reset(m); 1208 1209 /* encode ret != 0 as positive integer, since we're sending u32 */ 1210 encoded_ret = (ret != 0); 1211 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0) 1212 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1213 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1214 1215 return ret == 0; 1216 } 1217 1218 static void 1219 mm_record_login(Session *s, struct passwd *pw) 1220 { 1221 struct ssh *ssh = active_state; /* XXX */ 1222 socklen_t fromlen; 1223 struct sockaddr_storage from; 1224 1225 /* 1226 * Get IP address of client. If the connection is not a socket, let 1227 * the address be 0.0.0.0. 1228 */ 1229 memset(&from, 0, sizeof(from)); 1230 fromlen = sizeof(from); 1231 if (packet_connection_is_on_socket()) { 1232 if (getpeername(packet_get_connection_in(), 1233 (struct sockaddr *)&from, &fromlen) < 0) { 1234 debug("getpeername: %.100s", strerror(errno)); 1235 cleanup_exit(255); 1236 } 1237 } 1238 /* Record that there was a login on that tty from the remote host. */ 1239 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1240 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns), 1241 (struct sockaddr *)&from, fromlen); 1242 } 1243 1244 static void 1245 mm_session_close(Session *s) 1246 { 1247 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1248 if (s->ttyfd != -1) { 1249 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1250 session_pty_cleanup2(s); 1251 } 1252 session_unused(s->self); 1253 } 1254 1255 int 1256 mm_answer_pty(int sock, struct sshbuf *m) 1257 { 1258 extern struct monitor *pmonitor; 1259 Session *s; 1260 int r, res, fd0; 1261 1262 debug3("%s entering", __func__); 1263 1264 sshbuf_reset(m); 1265 s = session_new(); 1266 if (s == NULL) 1267 goto error; 1268 s->authctxt = authctxt; 1269 s->pw = authctxt->pw; 1270 s->pid = pmonitor->m_pid; 1271 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1272 if (res == 0) 1273 goto error; 1274 pty_setowner(authctxt->pw, s->tty); 1275 1276 if ((r = sshbuf_put_u32(m, 1)) != 0 || 1277 (r = sshbuf_put_cstring(m, s->tty)) != 0) 1278 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1279 1280 /* We need to trick ttyslot */ 1281 if (dup2(s->ttyfd, 0) == -1) 1282 fatal("%s: dup2", __func__); 1283 1284 mm_record_login(s, authctxt->pw); 1285 1286 /* Now we can close the file descriptor again */ 1287 close(0); 1288 1289 /* send messages generated by record_login */ 1290 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0) 1291 fatal("%s: put login message: %s", __func__, ssh_err(r)); 1292 sshbuf_reset(loginmsg); 1293 1294 mm_request_send(sock, MONITOR_ANS_PTY, m); 1295 1296 if (mm_send_fd(sock, s->ptyfd) == -1 || 1297 mm_send_fd(sock, s->ttyfd) == -1) 1298 fatal("%s: send fds failed", __func__); 1299 1300 /* make sure nothing uses fd 0 */ 1301 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1302 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1303 if (fd0 != 0) 1304 error("%s: fd0 %d != 0", __func__, fd0); 1305 1306 /* slave is not needed */ 1307 close(s->ttyfd); 1308 s->ttyfd = s->ptyfd; 1309 /* no need to dup() because nobody closes ptyfd */ 1310 s->ptymaster = s->ptyfd; 1311 1312 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1313 1314 return (0); 1315 1316 error: 1317 if (s != NULL) 1318 mm_session_close(s); 1319 if ((r = sshbuf_put_u32(m, 0)) != 0) 1320 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1321 mm_request_send(sock, MONITOR_ANS_PTY, m); 1322 return (0); 1323 } 1324 1325 int 1326 mm_answer_pty_cleanup(int sock, struct sshbuf *m) 1327 { 1328 Session *s; 1329 char *tty; 1330 int r; 1331 1332 debug3("%s entering", __func__); 1333 1334 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0) 1335 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1336 if ((s = session_by_tty(tty)) != NULL) 1337 mm_session_close(s); 1338 sshbuf_reset(m); 1339 free(tty); 1340 return (0); 1341 } 1342 1343 int 1344 mm_answer_term(int sock, struct sshbuf *req) 1345 { 1346 struct ssh *ssh = active_state; /* XXX */ 1347 extern struct monitor *pmonitor; 1348 int res, status; 1349 1350 debug3("%s: tearing down sessions", __func__); 1351 1352 /* The child is terminating */ 1353 session_destroy_all(ssh, &mm_session_close); 1354 1355 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1356 if (errno != EINTR) 1357 exit(1); 1358 1359 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1360 1361 /* Terminate process */ 1362 exit(res); 1363 } 1364 1365 void 1366 monitor_clear_keystate(struct monitor *pmonitor) 1367 { 1368 struct ssh *ssh = active_state; /* XXX */ 1369 1370 ssh_clear_newkeys(ssh, MODE_IN); 1371 ssh_clear_newkeys(ssh, MODE_OUT); 1372 sshbuf_free(child_state); 1373 child_state = NULL; 1374 } 1375 1376 void 1377 monitor_apply_keystate(struct monitor *pmonitor) 1378 { 1379 struct ssh *ssh = active_state; /* XXX */ 1380 struct kex *kex; 1381 int r; 1382 1383 debug3("%s: packet_set_state", __func__); 1384 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1385 fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); 1386 sshbuf_free(child_state); 1387 child_state = NULL; 1388 1389 if ((kex = ssh->kex) != NULL) { 1390 /* XXX set callbacks */ 1391 #ifdef WITH_OPENSSL 1392 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1393 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 1394 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server; 1395 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server; 1396 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server; 1397 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1398 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1399 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 1400 #endif 1401 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 1402 kex->load_host_public_key=&get_hostkey_public_by_type; 1403 kex->load_host_private_key=&get_hostkey_private_by_type; 1404 kex->host_key_index=&get_hostkey_index; 1405 kex->sign = sshd_hostkey_sign; 1406 } 1407 } 1408 1409 /* This function requries careful sanity checking */ 1410 1411 void 1412 mm_get_keystate(struct monitor *pmonitor) 1413 { 1414 debug3("%s: Waiting for new keys", __func__); 1415 1416 if ((child_state = sshbuf_new()) == NULL) 1417 fatal("%s: sshbuf_new failed", __func__); 1418 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1419 child_state); 1420 debug3("%s: GOT new keys", __func__); 1421 } 1422 1423 1424 /* XXX */ 1425 1426 #define FD_CLOSEONEXEC(x) do { \ 1427 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ 1428 fatal("fcntl(%d, F_SETFD)", x); \ 1429 } while (0) 1430 1431 static void 1432 monitor_openfds(struct monitor *mon, int do_logfds) 1433 { 1434 int pair[2]; 1435 #ifdef SO_ZEROIZE 1436 int on = 1; 1437 #endif 1438 1439 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1440 fatal("%s: socketpair: %s", __func__, strerror(errno)); 1441 #ifdef SO_ZEROIZE 1442 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1443 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno)); 1444 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1445 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno)); 1446 #endif 1447 FD_CLOSEONEXEC(pair[0]); 1448 FD_CLOSEONEXEC(pair[1]); 1449 mon->m_recvfd = pair[0]; 1450 mon->m_sendfd = pair[1]; 1451 1452 if (do_logfds) { 1453 if (pipe(pair) == -1) 1454 fatal("%s: pipe: %s", __func__, strerror(errno)); 1455 FD_CLOSEONEXEC(pair[0]); 1456 FD_CLOSEONEXEC(pair[1]); 1457 mon->m_log_recvfd = pair[0]; 1458 mon->m_log_sendfd = pair[1]; 1459 } else 1460 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1461 } 1462 1463 #define MM_MEMSIZE 65536 1464 1465 struct monitor * 1466 monitor_init(void) 1467 { 1468 struct monitor *mon; 1469 1470 mon = xcalloc(1, sizeof(*mon)); 1471 monitor_openfds(mon, 1); 1472 1473 return mon; 1474 } 1475 1476 void 1477 monitor_reinit(struct monitor *mon) 1478 { 1479 monitor_openfds(mon, 0); 1480 } 1481 1482 #ifdef GSSAPI 1483 int 1484 mm_answer_gss_setup_ctx(int sock, struct sshbuf *m) 1485 { 1486 gss_OID_desc goid; 1487 OM_uint32 major; 1488 size_t len; 1489 u_char *p; 1490 int r; 1491 1492 if (!options.gss_authentication) 1493 fatal("%s: GSSAPI authentication not enabled", __func__); 1494 1495 if ((r = sshbuf_get_string(m, &p, &len)) != 0) 1496 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1497 goid.elements = p; 1498 goid.length = len; 1499 1500 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1501 1502 free(goid.elements); 1503 1504 sshbuf_reset(m); 1505 if ((r = sshbuf_put_u32(m, major)) != 0) 1506 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1507 1508 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1509 1510 /* Now we have a context, enable the step */ 1511 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1512 1513 return (0); 1514 } 1515 1516 int 1517 mm_answer_gss_accept_ctx(int sock, struct sshbuf *m) 1518 { 1519 gss_buffer_desc in; 1520 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 1521 OM_uint32 major, minor; 1522 OM_uint32 flags = 0; /* GSI needs this */ 1523 int r; 1524 1525 if (!options.gss_authentication) 1526 fatal("%s: GSSAPI authentication not enabled", __func__); 1527 1528 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0) 1529 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1530 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 1531 free(in.value); 1532 1533 sshbuf_reset(m); 1534 if ((r = sshbuf_put_u32(m, major)) != 0 || 1535 (r = sshbuf_put_string(m, out.value, out.length)) != 0 || 1536 (r = sshbuf_put_u32(m, flags)) != 0) 1537 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1538 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 1539 1540 gss_release_buffer(&minor, &out); 1541 1542 if (major == GSS_S_COMPLETE) { 1543 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 1544 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1545 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 1546 } 1547 return (0); 1548 } 1549 1550 int 1551 mm_answer_gss_checkmic(int sock, struct sshbuf *m) 1552 { 1553 gss_buffer_desc gssbuf, mic; 1554 OM_uint32 ret; 1555 int r; 1556 1557 if (!options.gss_authentication) 1558 fatal("%s: GSSAPI authentication not enabled", __func__); 1559 1560 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 || 1561 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0) 1562 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1563 1564 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 1565 1566 free(gssbuf.value); 1567 free(mic.value); 1568 1569 sshbuf_reset(m); 1570 if ((r = sshbuf_put_u32(m, ret)) != 0) 1571 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1572 1573 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 1574 1575 if (!GSS_ERROR(ret)) 1576 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1577 1578 return (0); 1579 } 1580 1581 int 1582 mm_answer_gss_userok(int sock, struct sshbuf *m) 1583 { 1584 int r, authenticated; 1585 const char *displayname; 1586 1587 if (!options.gss_authentication) 1588 fatal("%s: GSSAPI authentication not enabled", __func__); 1589 1590 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 1591 1592 sshbuf_reset(m); 1593 if ((r = sshbuf_put_u32(m, authenticated)) != 0) 1594 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1595 1596 debug3("%s: sending result %d", __func__, authenticated); 1597 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 1598 1599 auth_method = "gssapi-with-mic"; 1600 1601 if ((displayname = ssh_gssapi_displayname()) != NULL) 1602 auth2_record_info(authctxt, "%s", displayname); 1603 1604 /* Monitor loop will terminate if authenticated */ 1605 return (authenticated); 1606 } 1607 #endif /* GSSAPI */ 1608 1609