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