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