1 /* $OpenBSD: monitor_wrap.c,v 1.117 2019/12/15 18:57:30 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/uio.h> 30 #include <sys/queue.h> 31 32 #include <errno.h> 33 #include <pwd.h> 34 #include <signal.h> 35 #include <stdio.h> 36 #include <string.h> 37 #include <stdarg.h> 38 #include <unistd.h> 39 40 #ifdef WITH_OPENSSL 41 #include <openssl/bn.h> 42 #include <openssl/dh.h> 43 #endif 44 45 #include "xmalloc.h" 46 #include "ssh.h" 47 #ifdef WITH_OPENSSL 48 #include "dh.h" 49 #endif 50 #include "sshbuf.h" 51 #include "sshkey.h" 52 #include "cipher.h" 53 #include "kex.h" 54 #include "hostfile.h" 55 #include "auth.h" 56 #include "auth-options.h" 57 #include "packet.h" 58 #include "mac.h" 59 #include "log.h" 60 #include "monitor.h" 61 #ifdef GSSAPI 62 #include "ssh-gss.h" 63 #endif 64 #include "monitor_wrap.h" 65 #include "atomicio.h" 66 #include "monitor_fdpass.h" 67 #include "misc.h" 68 69 #include "channels.h" 70 #include "session.h" 71 #include "servconf.h" 72 73 #include "ssherr.h" 74 75 /* Imports */ 76 extern struct monitor *pmonitor; 77 extern struct sshbuf *loginmsg; 78 extern ServerOptions options; 79 80 void 81 mm_log_handler(LogLevel level, const char *msg, void *ctx) 82 { 83 struct sshbuf *log_msg; 84 struct monitor *mon = (struct monitor *)ctx; 85 int r; 86 size_t len; 87 88 if (mon->m_log_sendfd == -1) 89 fatal("%s: no log channel", __func__); 90 91 if ((log_msg = sshbuf_new()) == NULL) 92 fatal("%s: sshbuf_new failed", __func__); 93 94 if ((r = sshbuf_put_u32(log_msg, 0)) != 0 || /* length; filled below */ 95 (r = sshbuf_put_u32(log_msg, level)) != 0 || 96 (r = sshbuf_put_cstring(log_msg, msg)) != 0) 97 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 98 if ((len = sshbuf_len(log_msg)) < 4 || len > 0xffffffff) 99 fatal("%s: bad length %zu", __func__, len); 100 POKE_U32(sshbuf_mutable_ptr(log_msg), len - 4); 101 if (atomicio(vwrite, mon->m_log_sendfd, 102 sshbuf_mutable_ptr(log_msg), len) != len) 103 fatal("%s: write: %s", __func__, strerror(errno)); 104 sshbuf_free(log_msg); 105 } 106 107 int 108 mm_is_monitor(void) 109 { 110 /* 111 * m_pid is only set in the privileged part, and 112 * points to the unprivileged child. 113 */ 114 return (pmonitor && pmonitor->m_pid > 0); 115 } 116 117 void 118 mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m) 119 { 120 size_t mlen = sshbuf_len(m); 121 u_char buf[5]; 122 123 debug3("%s entering: type %d", __func__, type); 124 125 if (mlen >= 0xffffffff) 126 fatal("%s: bad length %zu", __func__, mlen); 127 POKE_U32(buf, mlen + 1); 128 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ 129 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) 130 fatal("%s: write: %s", __func__, strerror(errno)); 131 if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen) 132 fatal("%s: write: %s", __func__, strerror(errno)); 133 } 134 135 void 136 mm_request_receive(int sock, struct sshbuf *m) 137 { 138 u_char buf[4], *p = NULL; 139 u_int msg_len; 140 int r; 141 142 debug3("%s entering", __func__); 143 144 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { 145 if (errno == EPIPE) 146 cleanup_exit(255); 147 fatal("%s: read: %s", __func__, strerror(errno)); 148 } 149 msg_len = PEEK_U32(buf); 150 if (msg_len > 256 * 1024) 151 fatal("%s: read: bad msg_len %d", __func__, msg_len); 152 sshbuf_reset(m); 153 if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) 154 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 155 if (atomicio(read, sock, p, msg_len) != msg_len) 156 fatal("%s: read: %s", __func__, strerror(errno)); 157 } 158 159 void 160 mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m) 161 { 162 u_char rtype; 163 int r; 164 165 debug3("%s entering: type %d", __func__, type); 166 167 mm_request_receive(sock, m); 168 if ((r = sshbuf_get_u8(m, &rtype)) != 0) 169 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 170 if (rtype != type) 171 fatal("%s: read: rtype %d != type %d", __func__, 172 rtype, type); 173 } 174 175 #ifdef WITH_OPENSSL 176 DH * 177 mm_choose_dh(int min, int nbits, int max) 178 { 179 BIGNUM *p, *g; 180 int r; 181 u_char success = 0; 182 struct sshbuf *m; 183 184 if ((m = sshbuf_new()) == NULL) 185 fatal("%s: sshbuf_new failed", __func__); 186 if ((r = sshbuf_put_u32(m, min)) != 0 || 187 (r = sshbuf_put_u32(m, nbits)) != 0 || 188 (r = sshbuf_put_u32(m, max)) != 0) 189 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 190 191 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m); 192 193 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); 194 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m); 195 196 if ((r = sshbuf_get_u8(m, &success)) != 0) 197 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 198 if (success == 0) 199 fatal("%s: MONITOR_ANS_MODULI failed", __func__); 200 201 if ((r = sshbuf_get_bignum2(m, &p)) != 0 || 202 (r = sshbuf_get_bignum2(m, &g)) != 0) 203 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 204 205 debug3("%s: remaining %zu", __func__, sshbuf_len(m)); 206 sshbuf_free(m); 207 208 return (dh_new_group(g, p)); 209 } 210 #endif 211 212 int 213 mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp, 214 const u_char *data, size_t datalen, const char *hostkey_alg, 215 const char *sk_provider, u_int compat) 216 { 217 struct kex *kex = *pmonitor->m_pkex; 218 struct sshbuf *m; 219 u_int ndx = kex->host_key_index(key, 0, ssh); 220 int r; 221 222 debug3("%s entering", __func__); 223 if ((m = sshbuf_new()) == NULL) 224 fatal("%s: sshbuf_new failed", __func__); 225 if ((r = sshbuf_put_u32(m, ndx)) != 0 || 226 (r = sshbuf_put_string(m, data, datalen)) != 0 || 227 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 || 228 (r = sshbuf_put_u32(m, compat)) != 0) 229 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 230 231 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m); 232 233 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); 234 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m); 235 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0) 236 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 237 sshbuf_free(m); 238 239 return (0); 240 } 241 242 struct passwd * 243 mm_getpwnamallow(struct ssh *ssh, const char *username) 244 { 245 struct sshbuf *m; 246 struct passwd *pw; 247 size_t len; 248 u_int i; 249 ServerOptions *newopts; 250 int r; 251 u_char ok; 252 const u_char *p; 253 254 debug3("%s entering", __func__); 255 256 if ((m = sshbuf_new()) == NULL) 257 fatal("%s: sshbuf_new failed", __func__); 258 if ((r = sshbuf_put_cstring(m, username)) != 0) 259 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 260 261 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m); 262 263 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); 264 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m); 265 266 if ((r = sshbuf_get_u8(m, &ok)) != 0) 267 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 268 if (ok == 0) { 269 pw = NULL; 270 goto out; 271 } 272 273 /* XXX don't like passing struct passwd like this */ 274 pw = xcalloc(sizeof(*pw), 1); 275 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0) 276 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 277 if (len != sizeof(*pw)) 278 fatal("%s: struct passwd size mismatch", __func__); 279 memcpy(pw, p, sizeof(*pw)); 280 281 if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 || 282 (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 || 283 (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 || 284 (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 || 285 (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 || 286 (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0) 287 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 288 289 out: 290 /* copy options block as a Match directive may have changed some */ 291 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0) 292 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 293 if (len != sizeof(*newopts)) 294 fatal("%s: option block size mismatch", __func__); 295 newopts = xcalloc(sizeof(*newopts), 1); 296 memcpy(newopts, p, sizeof(*newopts)); 297 298 #define M_CP_STROPT(x) do { \ 299 if (newopts->x != NULL) { \ 300 if ((r = sshbuf_get_cstring(m, \ 301 &newopts->x, NULL)) != 0) \ 302 fatal("%s: buffer error: %s", \ 303 __func__, ssh_err(r)); \ 304 } \ 305 } while (0) 306 #define M_CP_STRARRAYOPT(x, nx) do { \ 307 newopts->x = newopts->nx == 0 ? \ 308 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \ 309 for (i = 0; i < newopts->nx; i++) { \ 310 if ((r = sshbuf_get_cstring(m, \ 311 &newopts->x[i], NULL)) != 0) \ 312 fatal("%s: buffer error: %s", \ 313 __func__, ssh_err(r)); \ 314 } \ 315 } while (0) 316 /* See comment in servconf.h */ 317 COPY_MATCH_STRING_OPTS(); 318 #undef M_CP_STROPT 319 #undef M_CP_STRARRAYOPT 320 321 copy_set_server_options(&options, newopts, 1); 322 log_change_level(options.log_level); 323 process_permitopen(ssh, &options); 324 free(newopts); 325 326 sshbuf_free(m); 327 328 return (pw); 329 } 330 331 char * 332 mm_auth2_read_banner(void) 333 { 334 struct sshbuf *m; 335 char *banner; 336 int r; 337 338 debug3("%s entering", __func__); 339 340 if ((m = sshbuf_new()) == NULL) 341 fatal("%s: sshbuf_new failed", __func__); 342 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m); 343 sshbuf_reset(m); 344 345 mm_request_receive_expect(pmonitor->m_recvfd, 346 MONITOR_ANS_AUTH2_READ_BANNER, m); 347 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0) 348 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 349 sshbuf_free(m); 350 351 /* treat empty banner as missing banner */ 352 if (strlen(banner) == 0) { 353 free(banner); 354 banner = NULL; 355 } 356 return (banner); 357 } 358 359 /* Inform the privileged process about service and style */ 360 361 void 362 mm_inform_authserv(char *service, char *style) 363 { 364 struct sshbuf *m; 365 int r; 366 367 debug3("%s entering", __func__); 368 369 if ((m = sshbuf_new()) == NULL) 370 fatal("%s: sshbuf_new failed", __func__); 371 if ((r = sshbuf_put_cstring(m, service)) != 0 || 372 (r = sshbuf_put_cstring(m, style ? style : "")) != 0) 373 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 374 375 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m); 376 377 sshbuf_free(m); 378 } 379 380 /* Do the password authentication */ 381 int 382 mm_auth_password(struct ssh *ssh, char *password) 383 { 384 struct sshbuf *m; 385 int r, authenticated = 0; 386 387 debug3("%s entering", __func__); 388 389 if ((m = sshbuf_new()) == NULL) 390 fatal("%s: sshbuf_new failed", __func__); 391 if ((r = sshbuf_put_cstring(m, password)) != 0) 392 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 393 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m); 394 395 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); 396 mm_request_receive_expect(pmonitor->m_recvfd, 397 MONITOR_ANS_AUTHPASSWORD, m); 398 399 if ((r = sshbuf_get_u32(m, &authenticated)) != 0) 400 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 401 402 sshbuf_free(m); 403 404 debug3("%s: user %sauthenticated", 405 __func__, authenticated ? "" : "not "); 406 return (authenticated); 407 } 408 409 int 410 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 411 int pubkey_auth_attempt, struct sshauthopt **authoptp) 412 { 413 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key, 414 pubkey_auth_attempt, authoptp)); 415 } 416 417 int 418 mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw, 419 const char *user, const char *host, struct sshkey *key) 420 { 421 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL)); 422 } 423 424 int 425 mm_key_allowed(enum mm_keytype type, const char *user, const char *host, 426 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp) 427 { 428 struct sshbuf *m; 429 int r, allowed = 0; 430 struct sshauthopt *opts = NULL; 431 432 debug3("%s entering", __func__); 433 434 if (authoptp != NULL) 435 *authoptp = NULL; 436 437 if ((m = sshbuf_new()) == NULL) 438 fatal("%s: sshbuf_new failed", __func__); 439 if ((r = sshbuf_put_u32(m, type)) != 0 || 440 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 || 441 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 || 442 (r = sshkey_puts(key, m)) != 0 || 443 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0) 444 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 445 446 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m); 447 448 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); 449 mm_request_receive_expect(pmonitor->m_recvfd, 450 MONITOR_ANS_KEYALLOWED, m); 451 452 if ((r = sshbuf_get_u32(m, &allowed)) != 0) 453 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 454 if (allowed && type == MM_USERKEY) { 455 if ((r = sshauthopt_deserialise(m, &opts)) != 0) 456 fatal("%s: sshauthopt_deserialise: %s", 457 __func__, ssh_err(r)); 458 } 459 sshbuf_free(m); 460 461 if (authoptp != NULL) { 462 *authoptp = opts; 463 opts = NULL; 464 } 465 sshauthopt_free(opts); 466 467 return allowed; 468 } 469 470 /* 471 * This key verify needs to send the key type along, because the 472 * privileged parent makes the decision if the key is allowed 473 * for authentication. 474 */ 475 476 int 477 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen, 478 const u_char *data, size_t datalen, const char *sigalg, u_int compat, 479 struct sshkey_sig_details **sig_detailsp) 480 { 481 struct sshbuf *m; 482 u_int encoded_ret = 0; 483 int r; 484 u_char sig_details_present, flags; 485 u_int counter; 486 487 debug3("%s entering", __func__); 488 489 if (sig_detailsp != NULL) 490 *sig_detailsp = NULL; 491 if ((m = sshbuf_new()) == NULL) 492 fatal("%s: sshbuf_new failed", __func__); 493 if ((r = sshkey_puts(key, m)) != 0 || 494 (r = sshbuf_put_string(m, sig, siglen)) != 0 || 495 (r = sshbuf_put_string(m, data, datalen)) != 0 || 496 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0) 497 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 498 499 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m); 500 501 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); 502 mm_request_receive_expect(pmonitor->m_recvfd, 503 MONITOR_ANS_KEYVERIFY, m); 504 505 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 || 506 (r = sshbuf_get_u8(m, &sig_details_present)) != 0) 507 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 508 if (sig_details_present && encoded_ret == 0) { 509 if ((r = sshbuf_get_u32(m, &counter)) != 0 || 510 (r = sshbuf_get_u8(m, &flags)) != 0) 511 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 512 if (sig_detailsp != NULL) { 513 *sig_detailsp = xcalloc(1, sizeof(**sig_detailsp)); 514 (*sig_detailsp)->sk_counter = counter; 515 (*sig_detailsp)->sk_flags = flags; 516 } 517 } 518 519 sshbuf_free(m); 520 521 if (encoded_ret != 0) 522 return SSH_ERR_SIGNATURE_INVALID; 523 return 0; 524 } 525 526 void 527 mm_send_keystate(struct ssh *ssh, struct monitor *monitor) 528 { 529 struct sshbuf *m; 530 int r; 531 532 if ((m = sshbuf_new()) == NULL) 533 fatal("%s: sshbuf_new failed", __func__); 534 if ((r = ssh_packet_get_state(ssh, m)) != 0) 535 fatal("%s: get_state failed: %s", 536 __func__, ssh_err(r)); 537 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m); 538 debug3("%s: Finished sending state", __func__); 539 sshbuf_free(m); 540 } 541 542 int 543 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) 544 { 545 struct sshbuf *m; 546 char *p, *msg; 547 int success = 0, tmp1 = -1, tmp2 = -1, r; 548 549 /* Kludge: ensure there are fds free to receive the pty/tty */ 550 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 || 551 (tmp2 = dup(pmonitor->m_recvfd)) == -1) { 552 error("%s: cannot allocate fds for pty", __func__); 553 if (tmp1 > 0) 554 close(tmp1); 555 if (tmp2 > 0) 556 close(tmp2); 557 return 0; 558 } 559 close(tmp1); 560 close(tmp2); 561 562 if ((m = sshbuf_new()) == NULL) 563 fatal("%s: sshbuf_new failed", __func__); 564 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m); 565 566 debug3("%s: waiting for MONITOR_ANS_PTY", __func__); 567 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m); 568 569 if ((r = sshbuf_get_u32(m, &success)) != 0) 570 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 571 if (success == 0) { 572 debug3("%s: pty alloc failed", __func__); 573 sshbuf_free(m); 574 return (0); 575 } 576 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 || 577 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0) 578 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 579 sshbuf_free(m); 580 581 strlcpy(namebuf, p, namebuflen); /* Possible truncation */ 582 free(p); 583 584 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0) 585 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 586 free(msg); 587 588 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 || 589 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1) 590 fatal("%s: receive fds failed", __func__); 591 592 /* Success */ 593 return (1); 594 } 595 596 void 597 mm_session_pty_cleanup2(Session *s) 598 { 599 struct sshbuf *m; 600 int r; 601 602 if (s->ttyfd == -1) 603 return; 604 if ((m = sshbuf_new()) == NULL) 605 fatal("%s: sshbuf_new failed", __func__); 606 if ((r = sshbuf_put_cstring(m, s->tty)) != 0) 607 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 608 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m); 609 sshbuf_free(m); 610 611 /* closed dup'ed master */ 612 if (s->ptymaster != -1 && close(s->ptymaster) == -1) 613 error("close(s->ptymaster/%d): %s", 614 s->ptymaster, strerror(errno)); 615 616 /* unlink pty from session */ 617 s->ttyfd = -1; 618 } 619 620 /* Request process termination */ 621 622 void 623 mm_terminate(void) 624 { 625 struct sshbuf *m; 626 627 if ((m = sshbuf_new()) == NULL) 628 fatal("%s: sshbuf_new failed", __func__); 629 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m); 630 sshbuf_free(m); 631 } 632 633 static void 634 mm_chall_setup(char **name, char **infotxt, u_int *numprompts, 635 char ***prompts, u_int **echo_on) 636 { 637 *name = xstrdup(""); 638 *infotxt = xstrdup(""); 639 *numprompts = 1; 640 *prompts = xcalloc(*numprompts, sizeof(char *)); 641 *echo_on = xcalloc(*numprompts, sizeof(u_int)); 642 (*echo_on)[0] = 0; 643 } 644 645 int 646 mm_bsdauth_query(void *ctx, char **name, char **infotxt, 647 u_int *numprompts, char ***prompts, u_int **echo_on) 648 { 649 struct sshbuf *m; 650 u_int success; 651 char *challenge; 652 int r; 653 654 debug3("%s: entering", __func__); 655 656 if ((m = sshbuf_new()) == NULL) 657 fatal("%s: sshbuf_new failed", __func__); 658 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m); 659 660 mm_request_receive_expect(pmonitor->m_recvfd, 661 MONITOR_ANS_BSDAUTHQUERY, m); 662 if ((r = sshbuf_get_u32(m, &success)) != 0) 663 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 664 if (success == 0) { 665 debug3("%s: no challenge", __func__); 666 sshbuf_free(m); 667 return (-1); 668 } 669 670 /* Get the challenge, and format the response */ 671 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0) 672 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 673 sshbuf_free(m); 674 675 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); 676 (*prompts)[0] = challenge; 677 678 debug3("%s: received challenge: %s", __func__, challenge); 679 680 return (0); 681 } 682 683 int 684 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) 685 { 686 struct sshbuf *m; 687 int r, authok; 688 689 debug3("%s: entering", __func__); 690 if (numresponses != 1) 691 return (-1); 692 693 if ((m = sshbuf_new()) == NULL) 694 fatal("%s: sshbuf_new failed", __func__); 695 if ((r = sshbuf_put_cstring(m, responses[0])) != 0) 696 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 697 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m); 698 699 mm_request_receive_expect(pmonitor->m_recvfd, 700 MONITOR_ANS_BSDAUTHRESPOND, m); 701 702 if ((r = sshbuf_get_u32(m, &authok)) != 0) 703 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 704 sshbuf_free(m); 705 706 return ((authok == 0) ? -1 : 0); 707 } 708 709 #ifdef GSSAPI 710 OM_uint32 711 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) 712 { 713 struct sshbuf *m; 714 OM_uint32 major; 715 int r; 716 717 /* Client doesn't get to see the context */ 718 *ctx = NULL; 719 720 if ((m = sshbuf_new()) == NULL) 721 fatal("%s: sshbuf_new failed", __func__); 722 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0) 723 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 724 725 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m); 726 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m); 727 728 if ((r = sshbuf_get_u32(m, &major)) != 0) 729 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 730 731 sshbuf_free(m); 732 return (major); 733 } 734 735 OM_uint32 736 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, 737 gss_buffer_desc *out, OM_uint32 *flagsp) 738 { 739 struct sshbuf *m; 740 OM_uint32 major; 741 u_int flags; 742 int r; 743 744 if ((m = sshbuf_new()) == NULL) 745 fatal("%s: sshbuf_new failed", __func__); 746 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0) 747 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 748 749 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m); 750 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m); 751 752 if ((r = sshbuf_get_u32(m, &major)) != 0 || 753 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0) 754 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 755 if (flagsp != NULL) { 756 if ((r = sshbuf_get_u32(m, &flags)) != 0) 757 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 758 *flagsp = flags; 759 } 760 761 sshbuf_free(m); 762 763 return (major); 764 } 765 766 OM_uint32 767 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) 768 { 769 struct sshbuf *m; 770 OM_uint32 major; 771 int r; 772 773 if ((m = sshbuf_new()) == NULL) 774 fatal("%s: sshbuf_new failed", __func__); 775 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 || 776 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0) 777 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 778 779 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m); 780 mm_request_receive_expect(pmonitor->m_recvfd, 781 MONITOR_ANS_GSSCHECKMIC, m); 782 783 if ((r = sshbuf_get_u32(m, &major)) != 0) 784 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 785 sshbuf_free(m); 786 return(major); 787 } 788 789 int 790 mm_ssh_gssapi_userok(char *user) 791 { 792 struct sshbuf *m; 793 int r, authenticated = 0; 794 795 if ((m = sshbuf_new()) == NULL) 796 fatal("%s: sshbuf_new failed", __func__); 797 798 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m); 799 mm_request_receive_expect(pmonitor->m_recvfd, 800 MONITOR_ANS_GSSUSEROK, m); 801 802 if ((r = sshbuf_get_u32(m, &authenticated)) != 0) 803 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 804 805 sshbuf_free(m); 806 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); 807 return (authenticated); 808 } 809 #endif /* GSSAPI */ 810