Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> |
| 3 | * Copyright 2002 Markus Friedl <markus@openbsd.org> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Ben Lindstrom | c861547 | 2002-03-26 03:20:45 +0000 | [diff] [blame] | 28 | RCSID("$OpenBSD: monitor_wrap.c,v 1.5 2002/03/25 20:12:10 stevesk Exp $"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 29 | |
| 30 | #include <openssl/bn.h> |
| 31 | #include <openssl/dh.h> |
| 32 | |
| 33 | #include "ssh.h" |
| 34 | #include "dh.h" |
| 35 | #include "kex.h" |
| 36 | #include "auth.h" |
| 37 | #include "buffer.h" |
| 38 | #include "bufaux.h" |
| 39 | #include "packet.h" |
| 40 | #include "mac.h" |
| 41 | #include "log.h" |
| 42 | #include "zlib.h" |
| 43 | #include "monitor.h" |
| 44 | #include "monitor_wrap.h" |
| 45 | #include "xmalloc.h" |
| 46 | #include "atomicio.h" |
| 47 | #include "monitor_fdpass.h" |
| 48 | #include "getput.h" |
| 49 | |
| 50 | #include "auth.h" |
| 51 | #include "channels.h" |
| 52 | #include "session.h" |
| 53 | |
| 54 | /* Imports */ |
| 55 | extern int compat20; |
| 56 | extern Newkeys *newkeys[]; |
| 57 | extern z_stream incoming_stream; |
| 58 | extern z_stream outgoing_stream; |
| 59 | extern struct monitor *monitor; |
| 60 | extern Buffer input, output; |
| 61 | |
| 62 | void |
| 63 | mm_request_send(int socket, enum monitor_reqtype type, Buffer *m) |
| 64 | { |
| 65 | u_char buf[5]; |
| 66 | u_int mlen = buffer_len(m); |
| 67 | |
| 68 | debug3("%s entering: type %d", __FUNCTION__, type); |
| 69 | |
| 70 | PUT_32BIT(buf, mlen + 1); |
| 71 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
| 72 | if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf)) |
| 73 | fatal("%s: write", __FUNCTION__); |
| 74 | if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen) |
| 75 | fatal("%s: write", __FUNCTION__); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | mm_request_receive(int socket, Buffer *m) |
| 80 | { |
| 81 | u_char buf[4]; |
| 82 | ssize_t res; |
| 83 | u_int msg_len; |
| 84 | |
| 85 | debug3("%s entering", __FUNCTION__); |
| 86 | |
| 87 | res = atomicio(read, socket, buf, sizeof(buf)); |
| 88 | if (res != sizeof(buf)) { |
| 89 | if (res == 0) |
| 90 | fatal_cleanup(); |
Ben Lindstrom | c861547 | 2002-03-26 03:20:45 +0000 | [diff] [blame] | 91 | fatal("%s: read: %ld", __FUNCTION__, (long)res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 92 | } |
| 93 | msg_len = GET_32BIT(buf); |
| 94 | if (msg_len > 256 * 1024) |
| 95 | fatal("%s: read: bad msg_len %d", __FUNCTION__, msg_len); |
| 96 | buffer_clear(m); |
| 97 | buffer_append_space(m, msg_len); |
| 98 | res = atomicio(read, socket, buffer_ptr(m), msg_len); |
| 99 | if (res != msg_len) |
Ben Lindstrom | c861547 | 2002-03-26 03:20:45 +0000 | [diff] [blame] | 100 | fatal("%s: read: %ld != msg_len", __FUNCTION__, (long)res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m) |
| 105 | { |
| 106 | u_char rtype; |
| 107 | |
| 108 | debug3("%s entering: type %d", __FUNCTION__, type); |
| 109 | |
| 110 | mm_request_receive(socket, m); |
| 111 | rtype = buffer_get_char(m); |
| 112 | if (rtype != type) |
| 113 | fatal("%s: read: rtype %d != type %d", __FUNCTION__, |
| 114 | rtype, type); |
| 115 | } |
| 116 | |
| 117 | DH * |
| 118 | mm_choose_dh(int min, int nbits, int max) |
| 119 | { |
| 120 | BIGNUM *p, *g; |
| 121 | int success = 0; |
| 122 | Buffer m; |
| 123 | |
| 124 | buffer_init(&m); |
| 125 | buffer_put_int(&m, min); |
| 126 | buffer_put_int(&m, nbits); |
| 127 | buffer_put_int(&m, max); |
| 128 | |
| 129 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
| 130 | |
| 131 | debug3("%s: waiting for MONITOR_ANS_MODULI", __FUNCTION__); |
| 132 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
| 133 | |
| 134 | success = buffer_get_char(&m); |
| 135 | if (success == 0) |
| 136 | fatal("%s: MONITOR_ANS_MODULI failed", __FUNCTION__); |
| 137 | |
| 138 | if ((p = BN_new()) == NULL) |
| 139 | fatal("%s: BN_new failed", __FUNCTION__); |
| 140 | if ((g = BN_new()) == NULL) |
| 141 | fatal("%s: BN_new failed", __FUNCTION__); |
| 142 | buffer_get_bignum2(&m, p); |
| 143 | buffer_get_bignum2(&m, g); |
| 144 | |
| 145 | debug3("%s: remaining %d", __FUNCTION__, buffer_len(&m)); |
| 146 | buffer_free(&m); |
| 147 | |
| 148 | return (dh_new_group(g, p)); |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen) |
| 153 | { |
| 154 | Kex *kex = *monitor->m_pkex; |
| 155 | Buffer m; |
| 156 | |
| 157 | debug3("%s entering", __FUNCTION__); |
| 158 | |
| 159 | buffer_init(&m); |
| 160 | buffer_put_int(&m, kex->host_key_index(key)); |
| 161 | buffer_put_string(&m, data, datalen); |
| 162 | |
| 163 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
| 164 | |
| 165 | debug3("%s: waiting for MONITOR_ANS_SIGN", __FUNCTION__); |
| 166 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
| 167 | *sigp = buffer_get_string(&m, lenp); |
| 168 | buffer_free(&m); |
| 169 | |
| 170 | return (0); |
| 171 | } |
| 172 | |
| 173 | struct passwd * |
| 174 | mm_getpwnamallow(const char *login) |
| 175 | { |
| 176 | Buffer m; |
| 177 | struct passwd *pw; |
| 178 | u_int pwlen; |
| 179 | |
| 180 | debug3("%s entering", __FUNCTION__); |
| 181 | |
| 182 | buffer_init(&m); |
| 183 | buffer_put_cstring(&m, login); |
| 184 | |
| 185 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
| 186 | |
| 187 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __FUNCTION__); |
| 188 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
| 189 | |
| 190 | if (buffer_get_char(&m) == 0) { |
| 191 | buffer_free(&m); |
| 192 | return (NULL); |
| 193 | } |
| 194 | pw = buffer_get_string(&m, &pwlen); |
| 195 | if (pwlen != sizeof(struct passwd)) |
| 196 | fatal("%s: struct passwd size mismatch", __FUNCTION__); |
| 197 | pw->pw_name = buffer_get_string(&m, NULL); |
| 198 | pw->pw_passwd = buffer_get_string(&m, NULL); |
| 199 | pw->pw_gecos = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 200 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 201 | pw->pw_class = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 202 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 203 | pw->pw_dir = buffer_get_string(&m, NULL); |
| 204 | pw->pw_shell = buffer_get_string(&m, NULL); |
| 205 | buffer_free(&m); |
| 206 | |
| 207 | return (pw); |
| 208 | } |
| 209 | |
| 210 | /* Inform the privileged process about service and style */ |
| 211 | |
| 212 | void |
| 213 | mm_inform_authserv(char *service, char *style) |
| 214 | { |
| 215 | Buffer m; |
| 216 | |
| 217 | debug3("%s entering", __FUNCTION__); |
| 218 | |
| 219 | buffer_init(&m); |
| 220 | buffer_put_cstring(&m, service); |
| 221 | buffer_put_cstring(&m, style ? style : ""); |
| 222 | |
| 223 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
| 224 | |
| 225 | buffer_free(&m); |
| 226 | } |
| 227 | |
| 228 | /* Do the password authentication */ |
| 229 | int |
| 230 | mm_auth_password(Authctxt *authctxt, char *password) |
| 231 | { |
| 232 | Buffer m; |
| 233 | int authenticated = 0; |
| 234 | |
| 235 | debug3("%s entering", __FUNCTION__); |
| 236 | |
| 237 | buffer_init(&m); |
| 238 | buffer_put_cstring(&m, password); |
| 239 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
| 240 | |
| 241 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __FUNCTION__); |
| 242 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
| 243 | |
| 244 | authenticated = buffer_get_int(&m); |
| 245 | |
| 246 | buffer_free(&m); |
| 247 | |
| 248 | debug3("%s: user %sauthenticated", |
| 249 | __FUNCTION__, authenticated ? "" : "not "); |
| 250 | return (authenticated); |
| 251 | } |
| 252 | |
| 253 | int |
| 254 | mm_user_key_allowed(struct passwd *pw, Key *key) |
| 255 | { |
| 256 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); |
| 257 | } |
| 258 | |
| 259 | int |
| 260 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, |
| 261 | Key *key) |
| 262 | { |
| 263 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); |
| 264 | } |
| 265 | |
| 266 | int |
| 267 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, |
| 268 | char *host, Key *key) |
| 269 | { |
| 270 | int ret; |
| 271 | |
| 272 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ |
| 273 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); |
| 274 | key->type = KEY_RSA1; |
| 275 | return (ret); |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | mm_send_debug(Buffer *m) |
| 280 | { |
| 281 | char *msg; |
| 282 | |
| 283 | while (buffer_len(m)) { |
| 284 | msg = buffer_get_string(m, NULL); |
| 285 | debug3("%s: Sending debug: %s", __FUNCTION__, msg); |
| 286 | packet_send_debug("%s", msg); |
| 287 | xfree(msg); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | int |
| 292 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) |
| 293 | { |
| 294 | Buffer m; |
| 295 | u_char *blob; |
| 296 | u_int len; |
| 297 | int allowed = 0; |
| 298 | |
| 299 | debug3("%s entering", __FUNCTION__); |
| 300 | |
| 301 | /* Convert the key to a blob and the pass it over */ |
| 302 | if (!key_to_blob(key, &blob, &len)) |
| 303 | return (0); |
| 304 | |
| 305 | buffer_init(&m); |
| 306 | buffer_put_int(&m, type); |
| 307 | buffer_put_cstring(&m, user ? user : ""); |
| 308 | buffer_put_cstring(&m, host ? host : ""); |
| 309 | buffer_put_string(&m, blob, len); |
| 310 | xfree(blob); |
| 311 | |
| 312 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
| 313 | |
| 314 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __FUNCTION__); |
| 315 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
| 316 | |
| 317 | allowed = buffer_get_int(&m); |
| 318 | |
| 319 | /* Send potential debug messages */ |
| 320 | mm_send_debug(&m); |
| 321 | |
| 322 | buffer_free(&m); |
| 323 | |
| 324 | return (allowed); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * This key verify needs to send the key type along, because the |
| 329 | * privileged parent makes the decision if the key is allowed |
| 330 | * for authentication. |
| 331 | */ |
| 332 | |
| 333 | int |
| 334 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) |
| 335 | { |
| 336 | Buffer m; |
| 337 | u_char *blob; |
| 338 | u_int len; |
| 339 | int verified = 0; |
| 340 | |
| 341 | debug3("%s entering", __FUNCTION__); |
| 342 | |
| 343 | /* Convert the key to a blob and the pass it over */ |
| 344 | if (!key_to_blob(key, &blob, &len)) |
| 345 | return (0); |
| 346 | |
| 347 | buffer_init(&m); |
| 348 | buffer_put_string(&m, blob, len); |
| 349 | buffer_put_string(&m, sig, siglen); |
| 350 | buffer_put_string(&m, data, datalen); |
| 351 | xfree(blob); |
| 352 | |
| 353 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
| 354 | |
| 355 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __FUNCTION__); |
| 356 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
| 357 | |
| 358 | verified = buffer_get_int(&m); |
| 359 | |
| 360 | buffer_free(&m); |
| 361 | |
| 362 | return (verified); |
| 363 | } |
| 364 | |
| 365 | /* Export key state after authentication */ |
| 366 | Newkeys * |
| 367 | mm_newkeys_from_blob(u_char *blob, int blen) |
| 368 | { |
| 369 | Buffer b; |
| 370 | u_int len; |
| 371 | Newkeys *newkey = NULL; |
| 372 | Enc *enc; |
| 373 | Mac *mac; |
| 374 | Comp *comp; |
| 375 | |
| 376 | debug3("%s: %p(%d)", __FUNCTION__, blob, blen); |
| 377 | #ifdef DEBUG_PK |
| 378 | dump_base64(stderr, blob, blen); |
| 379 | #endif |
| 380 | buffer_init(&b); |
| 381 | buffer_append(&b, blob, blen); |
| 382 | |
| 383 | newkey = xmalloc(sizeof(*newkey)); |
| 384 | enc = &newkey->enc; |
| 385 | mac = &newkey->mac; |
| 386 | comp = &newkey->comp; |
| 387 | |
| 388 | /* Enc structure */ |
| 389 | enc->name = buffer_get_string(&b, NULL); |
| 390 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); |
| 391 | enc->enabled = buffer_get_int(&b); |
| 392 | enc->block_size = buffer_get_int(&b); |
| 393 | enc->key = buffer_get_string(&b, &enc->key_len); |
| 394 | enc->iv = buffer_get_string(&b, &len); |
| 395 | if (len != enc->block_size) |
| 396 | fatal("%s: bad ivlen: expected %d != %d", __FUNCTION__, |
| 397 | enc->block_size, len); |
| 398 | |
| 399 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) |
| 400 | fatal("%s: bad cipher name %s or pointer %p", __FUNCTION__, |
| 401 | enc->name, enc->cipher); |
| 402 | |
| 403 | /* Mac structure */ |
| 404 | mac->name = buffer_get_string(&b, NULL); |
| 405 | if (mac->name == NULL || mac_init(mac, mac->name) == -1) |
| 406 | fatal("%s: can not init mac %s", __FUNCTION__, mac->name); |
| 407 | mac->enabled = buffer_get_int(&b); |
| 408 | mac->key = buffer_get_string(&b, &len); |
| 409 | if (len > mac->key_len) |
| 410 | fatal("%s: bad mac key lenght: %d > %d", __FUNCTION__, len, |
| 411 | mac->key_len); |
| 412 | mac->key_len = len; |
| 413 | |
| 414 | /* Comp structure */ |
| 415 | comp->type = buffer_get_int(&b); |
| 416 | comp->enabled = buffer_get_int(&b); |
| 417 | comp->name = buffer_get_string(&b, NULL); |
| 418 | |
| 419 | len = buffer_len(&b); |
| 420 | if (len != 0) |
| 421 | error("newkeys_from_blob: remaining bytes in blob %d", len); |
| 422 | buffer_free(&b); |
| 423 | return (newkey); |
| 424 | } |
| 425 | |
| 426 | int |
| 427 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) |
| 428 | { |
| 429 | Buffer b; |
| 430 | int len; |
| 431 | u_char *buf; |
| 432 | Enc *enc; |
| 433 | Mac *mac; |
| 434 | Comp *comp; |
| 435 | Newkeys *newkey = newkeys[mode]; |
| 436 | |
| 437 | debug3("%s: converting %p", __FUNCTION__, newkey); |
| 438 | |
| 439 | if (newkey == NULL) { |
| 440 | error("%s: newkey == NULL", __FUNCTION__); |
| 441 | return 0; |
| 442 | } |
| 443 | enc = &newkey->enc; |
| 444 | mac = &newkey->mac; |
| 445 | comp = &newkey->comp; |
| 446 | |
| 447 | buffer_init(&b); |
| 448 | /* Enc structure */ |
| 449 | buffer_put_cstring(&b, enc->name); |
| 450 | /* The cipher struct is constant and shared, you export pointer */ |
| 451 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); |
| 452 | buffer_put_int(&b, enc->enabled); |
| 453 | buffer_put_int(&b, enc->block_size); |
| 454 | buffer_put_string(&b, enc->key, enc->key_len); |
| 455 | packet_get_keyiv(mode, enc->iv, enc->block_size); |
| 456 | buffer_put_string(&b, enc->iv, enc->block_size); |
| 457 | |
| 458 | /* Mac structure */ |
| 459 | buffer_put_cstring(&b, mac->name); |
| 460 | buffer_put_int(&b, mac->enabled); |
| 461 | buffer_put_string(&b, mac->key, mac->key_len); |
| 462 | |
| 463 | /* Comp structure */ |
| 464 | buffer_put_int(&b, comp->type); |
| 465 | buffer_put_int(&b, comp->enabled); |
| 466 | buffer_put_cstring(&b, comp->name); |
| 467 | |
| 468 | len = buffer_len(&b); |
| 469 | buf = xmalloc(len); |
| 470 | memcpy(buf, buffer_ptr(&b), len); |
| 471 | memset(buffer_ptr(&b), 0, len); |
| 472 | buffer_free(&b); |
| 473 | if (lenp != NULL) |
| 474 | *lenp = len; |
| 475 | if (blobp != NULL) |
| 476 | *blobp = buf; |
| 477 | return len; |
| 478 | } |
| 479 | |
| 480 | static void |
| 481 | mm_send_kex(Buffer *m, Kex *kex) |
| 482 | { |
| 483 | buffer_put_string(m, kex->session_id, kex->session_id_len); |
| 484 | buffer_put_int(m, kex->we_need); |
| 485 | buffer_put_int(m, kex->hostkey_type); |
| 486 | buffer_put_int(m, kex->kex_type); |
| 487 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); |
| 488 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); |
| 489 | buffer_put_int(m, kex->flags); |
| 490 | buffer_put_cstring(m, kex->client_version_string); |
| 491 | buffer_put_cstring(m, kex->server_version_string); |
| 492 | } |
| 493 | |
| 494 | void |
| 495 | mm_send_keystate(struct monitor *monitor) |
| 496 | { |
| 497 | Buffer m; |
| 498 | u_char *blob, *p; |
| 499 | u_int bloblen, plen; |
| 500 | |
| 501 | buffer_init(&m); |
| 502 | |
| 503 | if (!compat20) { |
| 504 | u_char iv[24]; |
| 505 | int ivlen; |
| 506 | |
| 507 | buffer_put_int(&m, packet_get_protocol_flags()); |
| 508 | |
| 509 | buffer_put_int(&m, packet_get_ssh1_cipher()); |
| 510 | |
| 511 | debug3("%s: Sending ssh1 IV", __FUNCTION__); |
| 512 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 513 | packet_get_keyiv(MODE_OUT, iv, ivlen); |
| 514 | buffer_put_string(&m, iv, ivlen); |
| 515 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 516 | packet_get_keyiv(MODE_IN, iv, ivlen); |
| 517 | buffer_put_string(&m, iv, ivlen); |
| 518 | goto skip; |
| 519 | } else { |
| 520 | /* Kex for rekeying */ |
| 521 | mm_send_kex(&m, *monitor->m_pkex); |
| 522 | } |
| 523 | |
| 524 | debug3("%s: Sending new keys: %p %p", |
| 525 | __FUNCTION__, newkeys[MODE_OUT], newkeys[MODE_IN]); |
| 526 | |
| 527 | /* Keys from Kex */ |
| 528 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) |
| 529 | fatal("%s: conversion of newkeys failed", __FUNCTION__); |
| 530 | |
| 531 | buffer_put_string(&m, blob, bloblen); |
| 532 | xfree(blob); |
| 533 | |
| 534 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) |
| 535 | fatal("%s: conversion of newkeys failed", __FUNCTION__); |
| 536 | |
| 537 | buffer_put_string(&m, blob, bloblen); |
| 538 | xfree(blob); |
| 539 | |
| 540 | buffer_put_int(&m, packet_get_seqnr(MODE_OUT)); |
| 541 | buffer_put_int(&m, packet_get_seqnr(MODE_IN)); |
| 542 | |
| 543 | debug3("%s: New keys have been sent", __FUNCTION__); |
| 544 | skip: |
| 545 | /* More key context */ |
| 546 | plen = packet_get_keycontext(MODE_OUT, NULL); |
| 547 | p = xmalloc(plen+1); |
| 548 | packet_get_keycontext(MODE_OUT, p); |
| 549 | buffer_put_string(&m, p, plen); |
| 550 | xfree(p); |
| 551 | |
| 552 | plen = packet_get_keycontext(MODE_IN, NULL); |
| 553 | p = xmalloc(plen+1); |
| 554 | packet_get_keycontext(MODE_IN, p); |
| 555 | buffer_put_string(&m, p, plen); |
| 556 | xfree(p); |
| 557 | |
| 558 | /* Compression state */ |
| 559 | debug3("%s: Sending compression state", __FUNCTION__); |
| 560 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
| 561 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); |
| 562 | |
| 563 | /* Network I/O buffers */ |
| 564 | buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input)); |
| 565 | buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output)); |
| 566 | |
| 567 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
| 568 | debug3("%s: Finished sending state", __FUNCTION__); |
| 569 | |
| 570 | buffer_free(&m); |
| 571 | } |
| 572 | |
| 573 | int |
| 574 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
| 575 | { |
| 576 | Buffer m; |
| 577 | u_char *p; |
| 578 | int success = 0; |
| 579 | |
| 580 | buffer_init(&m); |
| 581 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTY, &m); |
| 582 | |
| 583 | debug3("%s: waiting for MONITOR_ANS_PTY", __FUNCTION__); |
| 584 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PTY, &m); |
| 585 | |
| 586 | success = buffer_get_int(&m); |
| 587 | if (success == 0) { |
| 588 | debug3("%s: pty alloc failed", __FUNCTION__); |
| 589 | buffer_free(&m); |
| 590 | return (0); |
| 591 | } |
| 592 | p = buffer_get_string(&m, NULL); |
| 593 | buffer_free(&m); |
| 594 | |
| 595 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ |
| 596 | xfree(p); |
| 597 | |
| 598 | *ptyfd = mm_receive_fd(monitor->m_recvfd); |
| 599 | *ttyfd = mm_receive_fd(monitor->m_recvfd); |
| 600 | |
| 601 | /* Success */ |
| 602 | return (1); |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | mm_session_pty_cleanup2(void *session) |
| 607 | { |
| 608 | Session *s = session; |
| 609 | Buffer m; |
| 610 | |
| 611 | if (s->ttyfd == -1) |
| 612 | return; |
| 613 | buffer_init(&m); |
| 614 | buffer_put_cstring(&m, s->tty); |
| 615 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
| 616 | buffer_free(&m); |
| 617 | |
| 618 | /* closed dup'ed master */ |
| 619 | if (close(s->ptymaster) < 0) |
| 620 | error("close(s->ptymaster): %s", strerror(errno)); |
| 621 | |
| 622 | /* unlink pty from session */ |
| 623 | s->ttyfd = -1; |
| 624 | } |
| 625 | |
| 626 | /* Request process termination */ |
| 627 | |
| 628 | void |
| 629 | mm_terminate(void) |
| 630 | { |
| 631 | Buffer m; |
| 632 | |
| 633 | buffer_init(&m); |
| 634 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_TERM, &m); |
| 635 | buffer_free(&m); |
| 636 | } |
| 637 | |
| 638 | int |
| 639 | mm_ssh1_session_key(BIGNUM *num) |
| 640 | { |
| 641 | int rsafail; |
| 642 | Buffer m; |
| 643 | |
| 644 | buffer_init(&m); |
| 645 | buffer_put_bignum2(&m, num); |
| 646 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
| 647 | |
| 648 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
| 649 | |
| 650 | rsafail = buffer_get_int(&m); |
| 651 | buffer_get_bignum2(&m, num); |
| 652 | |
| 653 | buffer_free(&m); |
| 654 | |
| 655 | return (rsafail); |
| 656 | } |
| 657 | |
| 658 | static void |
| 659 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, |
| 660 | char ***prompts, u_int **echo_on) |
| 661 | { |
| 662 | *name = xstrdup(""); |
| 663 | *infotxt = xstrdup(""); |
| 664 | *numprompts = 1; |
| 665 | *prompts = xmalloc(*numprompts * sizeof(char*)); |
| 666 | *echo_on = xmalloc(*numprompts * sizeof(u_int)); |
| 667 | (*echo_on)[0] = 0; |
| 668 | } |
| 669 | |
| 670 | int |
| 671 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, |
| 672 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 673 | { |
| 674 | Buffer m; |
| 675 | int res; |
| 676 | char *challenge; |
| 677 | |
| 678 | debug3("%s: entering", __FUNCTION__); |
| 679 | |
| 680 | buffer_init(&m); |
| 681 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
| 682 | |
| 683 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
| 684 | &m); |
| 685 | res = buffer_get_int(&m); |
| 686 | if (res == -1) { |
| 687 | debug3("%s: no challenge", __FUNCTION__); |
| 688 | buffer_free(&m); |
| 689 | return (-1); |
| 690 | } |
| 691 | |
| 692 | /* Get the challenge, and format the response */ |
| 693 | challenge = buffer_get_string(&m, NULL); |
| 694 | buffer_free(&m); |
| 695 | |
| 696 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 697 | (*prompts)[0] = challenge; |
| 698 | |
| 699 | debug3("%s: received challenge: %s", __FUNCTION__, challenge); |
| 700 | |
| 701 | return (0); |
| 702 | } |
| 703 | |
| 704 | int |
| 705 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) |
| 706 | { |
| 707 | Buffer m; |
| 708 | int authok; |
| 709 | |
| 710 | debug3("%s: entering", __FUNCTION__); |
| 711 | if (numresponses != 1) |
| 712 | return (-1); |
| 713 | |
| 714 | buffer_init(&m); |
| 715 | buffer_put_cstring(&m, responses[0]); |
| 716 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
| 717 | |
| 718 | mm_request_receive_expect(monitor->m_recvfd, |
| 719 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
| 720 | |
| 721 | authok = buffer_get_int(&m); |
| 722 | buffer_free(&m); |
| 723 | |
| 724 | return ((authok == 0) ? -1 : 0); |
| 725 | } |
| 726 | |
| 727 | int |
| 728 | mm_skey_query(void *ctx, char **name, char **infotxt, |
| 729 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 730 | { |
| 731 | Buffer m; |
| 732 | int len, res; |
| 733 | char *p, *challenge; |
| 734 | |
| 735 | debug3("%s: entering", __FUNCTION__); |
| 736 | |
| 737 | buffer_init(&m); |
| 738 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
| 739 | |
| 740 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
| 741 | &m); |
| 742 | res = buffer_get_int(&m); |
| 743 | if (res == -1) { |
| 744 | debug3("%s: no challenge", __FUNCTION__); |
| 745 | buffer_free(&m); |
| 746 | return (-1); |
| 747 | } |
| 748 | |
| 749 | /* Get the challenge, and format the response */ |
| 750 | challenge = buffer_get_string(&m, NULL); |
| 751 | buffer_free(&m); |
| 752 | |
| 753 | debug3("%s: received challenge: %s", __FUNCTION__, challenge); |
| 754 | |
| 755 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 756 | |
| 757 | len = strlen(challenge) + strlen(SKEY_PROMPT) + 1; |
| 758 | p = xmalloc(len); |
| 759 | strlcpy(p, challenge, len); |
| 760 | strlcat(p, SKEY_PROMPT, len); |
| 761 | (*prompts)[0] = p; |
| 762 | xfree(challenge); |
| 763 | |
| 764 | return (0); |
| 765 | } |
| 766 | |
| 767 | int |
| 768 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) |
| 769 | { |
| 770 | Buffer m; |
| 771 | int authok; |
| 772 | |
| 773 | debug3("%s: entering", __FUNCTION__); |
| 774 | if (numresponses != 1) |
| 775 | return (-1); |
| 776 | |
| 777 | buffer_init(&m); |
| 778 | buffer_put_cstring(&m, responses[0]); |
| 779 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
| 780 | |
| 781 | mm_request_receive_expect(monitor->m_recvfd, |
| 782 | MONITOR_ANS_SKEYRESPOND, &m); |
| 783 | |
| 784 | authok = buffer_get_int(&m); |
| 785 | buffer_free(&m); |
| 786 | |
| 787 | return ((authok == 0) ? -1 : 0); |
| 788 | } |
| 789 | |
| 790 | void |
| 791 | mm_ssh1_session_id(u_char session_id[16]) |
| 792 | { |
| 793 | Buffer m; |
| 794 | int i; |
| 795 | |
| 796 | debug3("%s entering", __FUNCTION__); |
| 797 | |
| 798 | buffer_init(&m); |
| 799 | for (i = 0; i < 16; i++) |
| 800 | buffer_put_char(&m, session_id[i]); |
| 801 | |
| 802 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
| 803 | buffer_free(&m); |
| 804 | } |
| 805 | |
| 806 | int |
| 807 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) |
| 808 | { |
| 809 | Buffer m; |
| 810 | Key *key; |
| 811 | u_char *blob; |
| 812 | u_int blen; |
| 813 | int allowed = 0; |
| 814 | |
| 815 | debug3("%s entering", __FUNCTION__); |
| 816 | |
| 817 | buffer_init(&m); |
| 818 | buffer_put_bignum2(&m, client_n); |
| 819 | |
| 820 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
| 821 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); |
| 822 | |
| 823 | allowed = buffer_get_int(&m); |
| 824 | |
| 825 | if (allowed && rkey != NULL) { |
| 826 | blob = buffer_get_string(&m, &blen); |
| 827 | if ((key = key_from_blob(blob, blen)) == NULL) |
| 828 | fatal("%s: key_from_blob failed", __FUNCTION__); |
| 829 | *rkey = key; |
| 830 | xfree(blob); |
| 831 | } |
| 832 | mm_send_debug(&m); |
| 833 | buffer_free(&m); |
| 834 | |
| 835 | return (allowed); |
| 836 | } |
| 837 | |
| 838 | BIGNUM * |
| 839 | mm_auth_rsa_generate_challenge(Key *key) |
| 840 | { |
| 841 | Buffer m; |
| 842 | BIGNUM *challenge; |
| 843 | u_char *blob; |
| 844 | u_int blen; |
| 845 | |
| 846 | debug3("%s entering", __FUNCTION__); |
| 847 | |
| 848 | if ((challenge = BN_new()) == NULL) |
| 849 | fatal("%s: BN_new failed", __FUNCTION__); |
| 850 | |
| 851 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 852 | if (key_to_blob(key, &blob, &blen) == 0) |
| 853 | fatal("%s: key_to_blob failed", __FUNCTION__); |
| 854 | key->type = KEY_RSA1; |
| 855 | |
| 856 | buffer_init(&m); |
| 857 | buffer_put_string(&m, blob, blen); |
| 858 | xfree(blob); |
| 859 | |
| 860 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
| 861 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); |
| 862 | |
| 863 | buffer_get_bignum2(&m, challenge); |
| 864 | buffer_free(&m); |
| 865 | |
| 866 | return (challenge); |
| 867 | } |
| 868 | |
| 869 | int |
| 870 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) |
| 871 | { |
| 872 | Buffer m; |
| 873 | u_char *blob; |
| 874 | u_int blen; |
| 875 | int success = 0; |
| 876 | |
| 877 | debug3("%s entering", __FUNCTION__); |
| 878 | |
| 879 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 880 | if (key_to_blob(key, &blob, &blen) == 0) |
| 881 | fatal("%s: key_to_blob failed", __FUNCTION__); |
| 882 | key->type = KEY_RSA1; |
| 883 | |
| 884 | buffer_init(&m); |
| 885 | buffer_put_string(&m, blob, blen); |
| 886 | buffer_put_string(&m, response, 16); |
| 887 | xfree(blob); |
| 888 | |
| 889 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
| 890 | mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); |
| 891 | |
| 892 | success = buffer_get_int(&m); |
| 893 | buffer_free(&m); |
| 894 | |
| 895 | return (success); |
| 896 | } |