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