1 /* $OpenBSD: monitor.c,v 1.187 2018/09/13 02:08:33 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 int 879 mm_answer_keyallowed(int sock, struct sshbuf *m) 880 { 881 struct ssh *ssh = active_state; /* XXX */ 882 struct sshkey *key = NULL; 883 char *cuser, *chost; 884 u_int pubkey_auth_attempt; 885 enum mm_keytype type = 0; 886 int r, allowed = 0; 887 struct sshauthopt *opts = NULL; 888 889 debug3("%s entering", __func__); 890 if ((r = sshbuf_get_u32(m, &type)) != 0 || 891 (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 || 892 (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 || 893 (r = sshkey_froms(m, &key)) != 0 || 894 (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0) 895 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 896 897 debug3("%s: key_from_blob: %p", __func__, key); 898 899 if (key != NULL && authctxt->valid) { 900 /* These should not make it past the privsep child */ 901 if (sshkey_type_plain(key->type) == KEY_RSA && 902 (datafellows & SSH_BUG_RSASIGMD5) != 0) 903 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); 904 905 switch (type) { 906 case MM_USERKEY: 907 auth_method = "publickey"; 908 if (!options.pubkey_authentication) 909 break; 910 if (auth2_key_already_used(authctxt, key)) 911 break; 912 if (match_pattern_list(sshkey_ssh_name(key), 913 options.pubkey_key_types, 0) != 1) 914 break; 915 allowed = user_key_allowed(ssh, authctxt->pw, key, 916 pubkey_auth_attempt, &opts); 917 break; 918 case MM_HOSTKEY: 919 auth_method = "hostbased"; 920 if (!options.hostbased_authentication) 921 break; 922 if (auth2_key_already_used(authctxt, key)) 923 break; 924 if (match_pattern_list(sshkey_ssh_name(key), 925 options.hostbased_key_types, 0) != 1) 926 break; 927 allowed = hostbased_key_allowed(authctxt->pw, 928 cuser, chost, key); 929 auth2_record_info(authctxt, 930 "client user \"%.100s\", client host \"%.100s\"", 931 cuser, chost); 932 break; 933 default: 934 fatal("%s: unknown key type %d", __func__, type); 935 break; 936 } 937 } 938 939 debug3("%s: %s authentication%s: %s key is %s", __func__, 940 auth_method, pubkey_auth_attempt ? "" : " test", 941 (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key), 942 allowed ? "allowed" : "not allowed"); 943 944 auth2_record_key(authctxt, 0, key); 945 946 /* clear temporarily storage (used by verify) */ 947 monitor_reset_key_state(); 948 949 if (allowed) { 950 /* Save temporarily for comparison in verify */ 951 if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0) 952 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 953 key_blobtype = type; 954 key_opts = opts; 955 hostbased_cuser = cuser; 956 hostbased_chost = chost; 957 } else { 958 /* Log failed attempt */ 959 auth_log(authctxt, 0, 0, auth_method, NULL); 960 free(cuser); 961 free(chost); 962 } 963 sshkey_free(key); 964 965 sshbuf_reset(m); 966 if ((r = sshbuf_put_u32(m, allowed)) != 0) 967 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 968 if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0) 969 fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r)); 970 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); 971 972 if (!allowed) 973 sshauthopt_free(opts); 974 975 return (0); 976 } 977 978 static int 979 monitor_valid_userblob(u_char *data, u_int datalen) 980 { 981 struct sshbuf *b; 982 const u_char *p; 983 char *userstyle, *cp; 984 size_t len; 985 u_char type; 986 int r, fail = 0; 987 988 if ((b = sshbuf_new()) == NULL) 989 fatal("%s: sshbuf_new", __func__); 990 if ((r = sshbuf_put(b, data, datalen)) != 0) 991 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 992 993 if (datafellows & SSH_OLD_SESSIONID) { 994 p = sshbuf_ptr(b); 995 len = sshbuf_len(b); 996 if ((session_id2 == NULL) || 997 (len < session_id2_len) || 998 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 999 fail++; 1000 if ((r = sshbuf_consume(b, session_id2_len)) != 0) 1001 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1002 } else { 1003 if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1004 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1005 if ((session_id2 == NULL) || 1006 (len != session_id2_len) || 1007 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1008 fail++; 1009 } 1010 if ((r = sshbuf_get_u8(b, &type)) != 0) 1011 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1012 if (type != SSH2_MSG_USERAUTH_REQUEST) 1013 fail++; 1014 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1015 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1016 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1017 authctxt->style ? ":" : "", 1018 authctxt->style ? authctxt->style : ""); 1019 if (strcmp(userstyle, cp) != 0) { 1020 logit("wrong user name passed to monitor: " 1021 "expected %s != %.100s", userstyle, cp); 1022 fail++; 1023 } 1024 free(userstyle); 1025 free(cp); 1026 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1027 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1028 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1029 if (strcmp("publickey", cp) != 0) 1030 fail++; 1031 free(cp); 1032 if ((r = sshbuf_get_u8(b, &type)) != 0) 1033 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1034 if (type == 0) 1035 fail++; 1036 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1037 (r = sshbuf_skip_string(b)) != 0) /* pkblob */ 1038 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1039 if (sshbuf_len(b) != 0) 1040 fail++; 1041 sshbuf_free(b); 1042 return (fail == 0); 1043 } 1044 1045 static int 1046 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, 1047 char *chost) 1048 { 1049 struct sshbuf *b; 1050 const u_char *p; 1051 char *cp, *userstyle; 1052 size_t len; 1053 int r, fail = 0; 1054 u_char type; 1055 1056 if ((b = sshbuf_new()) == NULL) 1057 fatal("%s: sshbuf_new", __func__); 1058 if ((r = sshbuf_put(b, data, datalen)) != 0 || 1059 (r = sshbuf_get_string_direct(b, &p, &len)) != 0) 1060 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1061 1062 if ((session_id2 == NULL) || 1063 (len != session_id2_len) || 1064 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) 1065 fail++; 1066 1067 if ((r = sshbuf_get_u8(b, &type)) != 0) 1068 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1069 if (type != SSH2_MSG_USERAUTH_REQUEST) 1070 fail++; 1071 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1072 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1073 xasprintf(&userstyle, "%s%s%s", authctxt->user, 1074 authctxt->style ? ":" : "", 1075 authctxt->style ? authctxt->style : ""); 1076 if (strcmp(userstyle, cp) != 0) { 1077 logit("wrong user name passed to monitor: " 1078 "expected %s != %.100s", userstyle, cp); 1079 fail++; 1080 } 1081 free(userstyle); 1082 free(cp); 1083 if ((r = sshbuf_skip_string(b)) != 0 || /* service */ 1084 (r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1085 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1086 if (strcmp(cp, "hostbased") != 0) 1087 fail++; 1088 free(cp); 1089 if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */ 1090 (r = sshbuf_skip_string(b)) != 0) /* pkblob */ 1091 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1092 1093 /* verify client host, strip trailing dot if necessary */ 1094 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1095 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1096 if (((len = strlen(cp)) > 0) && cp[len - 1] == '.') 1097 cp[len - 1] = '\0'; 1098 if (strcmp(cp, chost) != 0) 1099 fail++; 1100 free(cp); 1101 1102 /* verify client user */ 1103 if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0) 1104 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1105 if (strcmp(cp, cuser) != 0) 1106 fail++; 1107 free(cp); 1108 1109 if (sshbuf_len(b) != 0) 1110 fail++; 1111 sshbuf_free(b); 1112 return (fail == 0); 1113 } 1114 1115 int 1116 mm_answer_keyverify(int sock, struct sshbuf *m) 1117 { 1118 struct ssh *ssh = active_state; /* XXX */ 1119 struct sshkey *key; 1120 u_char *signature, *data, *blob; 1121 char *sigalg; 1122 size_t signaturelen, datalen, bloblen; 1123 int r, ret, valid_data = 0, encoded_ret; 1124 1125 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 || 1126 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 || 1127 (r = sshbuf_get_string(m, &data, &datalen)) != 0 || 1128 (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0) 1129 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1130 1131 if (hostbased_cuser == NULL || hostbased_chost == NULL || 1132 !monitor_allowed_key(blob, bloblen)) 1133 fatal("%s: bad key, not previously allowed", __func__); 1134 1135 /* Empty signature algorithm means NULL. */ 1136 if (*sigalg == '\0') { 1137 free(sigalg); 1138 sigalg = NULL; 1139 } 1140 1141 /* XXX use sshkey_froms here; need to change key_blob, etc. */ 1142 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0) 1143 fatal("%s: bad public key blob: %s", __func__, ssh_err(r)); 1144 1145 switch (key_blobtype) { 1146 case MM_USERKEY: 1147 valid_data = monitor_valid_userblob(data, datalen); 1148 auth_method = "publickey"; 1149 break; 1150 case MM_HOSTKEY: 1151 valid_data = monitor_valid_hostbasedblob(data, datalen, 1152 hostbased_cuser, hostbased_chost); 1153 auth_method = "hostbased"; 1154 break; 1155 default: 1156 valid_data = 0; 1157 break; 1158 } 1159 if (!valid_data) 1160 fatal("%s: bad signature data blob", __func__); 1161 1162 ret = sshkey_verify(key, signature, signaturelen, data, datalen, 1163 sigalg, active_state->compat); 1164 debug3("%s: %s %p signature %s", __func__, auth_method, key, 1165 (ret == 0) ? "verified" : "unverified"); 1166 auth2_record_key(authctxt, ret == 0, key); 1167 1168 free(blob); 1169 free(signature); 1170 free(data); 1171 free(sigalg); 1172 1173 if (key_blobtype == MM_USERKEY) 1174 auth_activate_options(ssh, key_opts); 1175 monitor_reset_key_state(); 1176 1177 sshkey_free(key); 1178 sshbuf_reset(m); 1179 1180 /* encode ret != 0 as positive integer, since we're sending u32 */ 1181 encoded_ret = (ret != 0); 1182 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0) 1183 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1184 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); 1185 1186 return ret == 0; 1187 } 1188 1189 static void 1190 mm_record_login(Session *s, struct passwd *pw) 1191 { 1192 struct ssh *ssh = active_state; /* XXX */ 1193 socklen_t fromlen; 1194 struct sockaddr_storage from; 1195 1196 /* 1197 * Get IP address of client. If the connection is not a socket, let 1198 * the address be 0.0.0.0. 1199 */ 1200 memset(&from, 0, sizeof(from)); 1201 fromlen = sizeof(from); 1202 if (packet_connection_is_on_socket()) { 1203 if (getpeername(packet_get_connection_in(), 1204 (struct sockaddr *)&from, &fromlen) < 0) { 1205 debug("getpeername: %.100s", strerror(errno)); 1206 cleanup_exit(255); 1207 } 1208 } 1209 /* Record that there was a login on that tty from the remote host. */ 1210 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1211 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns), 1212 (struct sockaddr *)&from, fromlen); 1213 } 1214 1215 static void 1216 mm_session_close(Session *s) 1217 { 1218 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1219 if (s->ttyfd != -1) { 1220 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1221 session_pty_cleanup2(s); 1222 } 1223 session_unused(s->self); 1224 } 1225 1226 int 1227 mm_answer_pty(int sock, struct sshbuf *m) 1228 { 1229 extern struct monitor *pmonitor; 1230 Session *s; 1231 int r, res, fd0; 1232 1233 debug3("%s entering", __func__); 1234 1235 sshbuf_reset(m); 1236 s = session_new(); 1237 if (s == NULL) 1238 goto error; 1239 s->authctxt = authctxt; 1240 s->pw = authctxt->pw; 1241 s->pid = pmonitor->m_pid; 1242 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); 1243 if (res == 0) 1244 goto error; 1245 pty_setowner(authctxt->pw, s->tty); 1246 1247 if ((r = sshbuf_put_u32(m, 1)) != 0 || 1248 (r = sshbuf_put_cstring(m, s->tty)) != 0) 1249 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1250 1251 /* We need to trick ttyslot */ 1252 if (dup2(s->ttyfd, 0) == -1) 1253 fatal("%s: dup2", __func__); 1254 1255 mm_record_login(s, authctxt->pw); 1256 1257 /* Now we can close the file descriptor again */ 1258 close(0); 1259 1260 /* send messages generated by record_login */ 1261 if ((r = sshbuf_put_stringb(m, loginmsg)) != 0) 1262 fatal("%s: put login message: %s", __func__, ssh_err(r)); 1263 sshbuf_reset(loginmsg); 1264 1265 mm_request_send(sock, MONITOR_ANS_PTY, m); 1266 1267 if (mm_send_fd(sock, s->ptyfd) == -1 || 1268 mm_send_fd(sock, s->ttyfd) == -1) 1269 fatal("%s: send fds failed", __func__); 1270 1271 /* make sure nothing uses fd 0 */ 1272 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) 1273 fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); 1274 if (fd0 != 0) 1275 error("%s: fd0 %d != 0", __func__, fd0); 1276 1277 /* slave is not needed */ 1278 close(s->ttyfd); 1279 s->ttyfd = s->ptyfd; 1280 /* no need to dup() because nobody closes ptyfd */ 1281 s->ptymaster = s->ptyfd; 1282 1283 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); 1284 1285 return (0); 1286 1287 error: 1288 if (s != NULL) 1289 mm_session_close(s); 1290 if ((r = sshbuf_put_u32(m, 0)) != 0) 1291 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1292 mm_request_send(sock, MONITOR_ANS_PTY, m); 1293 return (0); 1294 } 1295 1296 int 1297 mm_answer_pty_cleanup(int sock, struct sshbuf *m) 1298 { 1299 Session *s; 1300 char *tty; 1301 int r; 1302 1303 debug3("%s entering", __func__); 1304 1305 if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0) 1306 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1307 if ((s = session_by_tty(tty)) != NULL) 1308 mm_session_close(s); 1309 sshbuf_reset(m); 1310 free(tty); 1311 return (0); 1312 } 1313 1314 int 1315 mm_answer_term(int sock, struct sshbuf *req) 1316 { 1317 struct ssh *ssh = active_state; /* XXX */ 1318 extern struct monitor *pmonitor; 1319 int res, status; 1320 1321 debug3("%s: tearing down sessions", __func__); 1322 1323 /* The child is terminating */ 1324 session_destroy_all(ssh, &mm_session_close); 1325 1326 while (waitpid(pmonitor->m_pid, &status, 0) == -1) 1327 if (errno != EINTR) 1328 exit(1); 1329 1330 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; 1331 1332 /* Terminate process */ 1333 exit(res); 1334 } 1335 1336 void 1337 monitor_clear_keystate(struct monitor *pmonitor) 1338 { 1339 struct ssh *ssh = active_state; /* XXX */ 1340 1341 ssh_clear_newkeys(ssh, MODE_IN); 1342 ssh_clear_newkeys(ssh, MODE_OUT); 1343 sshbuf_free(child_state); 1344 child_state = NULL; 1345 } 1346 1347 void 1348 monitor_apply_keystate(struct monitor *pmonitor) 1349 { 1350 struct ssh *ssh = active_state; /* XXX */ 1351 struct kex *kex; 1352 int r; 1353 1354 debug3("%s: packet_set_state", __func__); 1355 if ((r = ssh_packet_set_state(ssh, child_state)) != 0) 1356 fatal("%s: packet_set_state: %s", __func__, ssh_err(r)); 1357 sshbuf_free(child_state); 1358 child_state = NULL; 1359 1360 if ((kex = ssh->kex) != NULL) { 1361 /* XXX set callbacks */ 1362 #ifdef WITH_OPENSSL 1363 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; 1364 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; 1365 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server; 1366 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server; 1367 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server; 1368 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; 1369 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; 1370 kex->kex[KEX_ECDH_SHA2] = kexecdh_server; 1371 #endif 1372 kex->kex[KEX_C25519_SHA256] = kexc25519_server; 1373 kex->load_host_public_key=&get_hostkey_public_by_type; 1374 kex->load_host_private_key=&get_hostkey_private_by_type; 1375 kex->host_key_index=&get_hostkey_index; 1376 kex->sign = sshd_hostkey_sign; 1377 } 1378 } 1379 1380 /* This function requries careful sanity checking */ 1381 1382 void 1383 mm_get_keystate(struct monitor *pmonitor) 1384 { 1385 debug3("%s: Waiting for new keys", __func__); 1386 1387 if ((child_state = sshbuf_new()) == NULL) 1388 fatal("%s: sshbuf_new failed", __func__); 1389 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, 1390 child_state); 1391 debug3("%s: GOT new keys", __func__); 1392 } 1393 1394 1395 /* XXX */ 1396 1397 #define FD_CLOSEONEXEC(x) do { \ 1398 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ 1399 fatal("fcntl(%d, F_SETFD)", x); \ 1400 } while (0) 1401 1402 static void 1403 monitor_openfds(struct monitor *mon, int do_logfds) 1404 { 1405 int pair[2]; 1406 #ifdef SO_ZEROIZE 1407 int on = 1; 1408 #endif 1409 1410 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) 1411 fatal("%s: socketpair: %s", __func__, strerror(errno)); 1412 #ifdef SO_ZEROIZE 1413 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1414 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno)); 1415 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0) 1416 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno)); 1417 #endif 1418 FD_CLOSEONEXEC(pair[0]); 1419 FD_CLOSEONEXEC(pair[1]); 1420 mon->m_recvfd = pair[0]; 1421 mon->m_sendfd = pair[1]; 1422 1423 if (do_logfds) { 1424 if (pipe(pair) == -1) 1425 fatal("%s: pipe: %s", __func__, strerror(errno)); 1426 FD_CLOSEONEXEC(pair[0]); 1427 FD_CLOSEONEXEC(pair[1]); 1428 mon->m_log_recvfd = pair[0]; 1429 mon->m_log_sendfd = pair[1]; 1430 } else 1431 mon->m_log_recvfd = mon->m_log_sendfd = -1; 1432 } 1433 1434 #define MM_MEMSIZE 65536 1435 1436 struct monitor * 1437 monitor_init(void) 1438 { 1439 struct monitor *mon; 1440 1441 mon = xcalloc(1, sizeof(*mon)); 1442 monitor_openfds(mon, 1); 1443 1444 return mon; 1445 } 1446 1447 void 1448 monitor_reinit(struct monitor *mon) 1449 { 1450 monitor_openfds(mon, 0); 1451 } 1452 1453 #ifdef GSSAPI 1454 int 1455 mm_answer_gss_setup_ctx(int sock, struct sshbuf *m) 1456 { 1457 gss_OID_desc goid; 1458 OM_uint32 major; 1459 size_t len; 1460 u_char *p; 1461 int r; 1462 1463 if (!options.gss_authentication) 1464 fatal("%s: GSSAPI authentication not enabled", __func__); 1465 1466 if ((r = sshbuf_get_string(m, &p, &len)) != 0) 1467 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1468 goid.elements = p; 1469 goid.length = len; 1470 1471 major = ssh_gssapi_server_ctx(&gsscontext, &goid); 1472 1473 free(goid.elements); 1474 1475 sshbuf_reset(m); 1476 if ((r = sshbuf_put_u32(m, major)) != 0) 1477 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1478 1479 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); 1480 1481 /* Now we have a context, enable the step */ 1482 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); 1483 1484 return (0); 1485 } 1486 1487 int 1488 mm_answer_gss_accept_ctx(int sock, struct sshbuf *m) 1489 { 1490 gss_buffer_desc in; 1491 gss_buffer_desc out = GSS_C_EMPTY_BUFFER; 1492 OM_uint32 major, minor; 1493 OM_uint32 flags = 0; /* GSI needs this */ 1494 int r; 1495 1496 if (!options.gss_authentication) 1497 fatal("%s: GSSAPI authentication not enabled", __func__); 1498 1499 if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0) 1500 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1501 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); 1502 free(in.value); 1503 1504 sshbuf_reset(m); 1505 if ((r = sshbuf_put_u32(m, major)) != 0 || 1506 (r = sshbuf_put_string(m, out.value, out.length)) != 0 || 1507 (r = sshbuf_put_u32(m, flags)) != 0) 1508 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1509 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); 1510 1511 gss_release_buffer(&minor, &out); 1512 1513 if (major == GSS_S_COMPLETE) { 1514 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); 1515 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1516 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); 1517 } 1518 return (0); 1519 } 1520 1521 int 1522 mm_answer_gss_checkmic(int sock, struct sshbuf *m) 1523 { 1524 gss_buffer_desc gssbuf, mic; 1525 OM_uint32 ret; 1526 int r; 1527 1528 if (!options.gss_authentication) 1529 fatal("%s: GSSAPI authentication not enabled", __func__); 1530 1531 if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 || 1532 (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0) 1533 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1534 1535 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); 1536 1537 free(gssbuf.value); 1538 free(mic.value); 1539 1540 sshbuf_reset(m); 1541 if ((r = sshbuf_put_u32(m, ret)) != 0) 1542 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1543 1544 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); 1545 1546 if (!GSS_ERROR(ret)) 1547 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); 1548 1549 return (0); 1550 } 1551 1552 int 1553 mm_answer_gss_userok(int sock, struct sshbuf *m) 1554 { 1555 int r, authenticated; 1556 const char *displayname; 1557 1558 if (!options.gss_authentication) 1559 fatal("%s: GSSAPI authentication not enabled", __func__); 1560 1561 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); 1562 1563 sshbuf_reset(m); 1564 if ((r = sshbuf_put_u32(m, authenticated)) != 0) 1565 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 1566 1567 debug3("%s: sending result %d", __func__, authenticated); 1568 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); 1569 1570 auth_method = "gssapi-with-mic"; 1571 1572 if ((displayname = ssh_gssapi_displayname()) != NULL) 1573 auth2_record_info(authctxt, "%s", displayname); 1574 1575 /* Monitor loop will terminate if authenticated */ 1576 return (authenticated); 1577 } 1578 #endif /* GSSAPI */ 1579 1580