]>
Commit | Line | Data |
---|---|---|
dc1f1948 | 1 | /* $OpenBSD: monitor_wrap.c,v 1.66 2009/05/25 06:48:01 andreas Exp $ */ |
1853d1ef | 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" | |
1853d1ef | 29 | |
b1842393 | 30 | #include <sys/types.h> |
428f6258 | 31 | #include <sys/uio.h> |
b1842393 | 32 | |
028094f4 | 33 | #include <errno.h> |
b1842393 | 34 | #include <pwd.h> |
31652869 | 35 | #include <signal.h> |
24436b92 | 36 | #include <stdarg.h> |
cf851879 | 37 | #include <stdio.h> |
00146caa | 38 | #include <string.h> |
428f6258 | 39 | #include <unistd.h> |
b1842393 | 40 | |
31652869 | 41 | #include <openssl/bn.h> |
42 | #include <openssl/dh.h> | |
5adf6b9a | 43 | #include <openssl/evp.h> |
31652869 | 44 | |
3593bdc0 | 45 | #include "openbsd-compat/sys-queue.h" |
31652869 | 46 | #include "xmalloc.h" |
1853d1ef | 47 | #include "ssh.h" |
48 | #include "dh.h" | |
31652869 | 49 | #include "buffer.h" |
50 | #include "key.h" | |
51 | #include "cipher.h" | |
1853d1ef | 52 | #include "kex.h" |
31652869 | 53 | #include "hostfile.h" |
1853d1ef | 54 | #include "auth.h" |
92e15201 | 55 | #include "auth-options.h" |
1853d1ef | 56 | #include "packet.h" |
57 | #include "mac.h" | |
58 | #include "log.h" | |
a655c012 | 59 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ |
60 | #undef TARGET_OS_MAC | |
9b08c23f | 61 | #include "zlib.h" |
a655c012 | 62 | #define TARGET_OS_MAC 1 |
63 | #else | |
64 | #include "zlib.h" | |
65 | #endif | |
1853d1ef | 66 | #include "monitor.h" |
31652869 | 67 | #ifdef GSSAPI |
68 | #include "ssh-gss.h" | |
69 | #endif | |
1853d1ef | 70 | #include "monitor_wrap.h" |
1853d1ef | 71 | #include "atomicio.h" |
72 | #include "monitor_fdpass.h" | |
51e7a012 | 73 | #include "misc.h" |
5b01421b | 74 | #include "schnorr.h" |
5adf6b9a | 75 | #include "jpake.h" |
1853d1ef | 76 | |
1853d1ef | 77 | #include "channels.h" |
78 | #include "session.h" | |
03bcbf84 | 79 | #include "servconf.h" |
1853d1ef | 80 | |
81 | /* Imports */ | |
82 | extern int compat20; | |
1853d1ef | 83 | extern z_stream incoming_stream; |
84 | extern z_stream outgoing_stream; | |
b5a28cbc | 85 | extern struct monitor *pmonitor; |
be2ca0c9 | 86 | extern Buffer loginmsg; |
7fceb20d | 87 | extern ServerOptions options; |
1853d1ef | 88 | |
2362db19 | 89 | int |
90 | mm_is_monitor(void) | |
91 | { | |
92 | /* | |
93 | * m_pid is only set in the privileged part, and | |
94 | * points to the unprivileged child. | |
95 | */ | |
74677ba3 | 96 | return (pmonitor && pmonitor->m_pid > 0); |
2362db19 | 97 | } |
98 | ||
1853d1ef | 99 | void |
ca75d7de | 100 | mm_request_send(int sock, enum monitor_reqtype type, Buffer *m) |
1853d1ef | 101 | { |
1853d1ef | 102 | u_int mlen = buffer_len(m); |
405a0d43 | 103 | u_char buf[5]; |
1853d1ef | 104 | |
1588c277 | 105 | debug3("%s entering: type %d", __func__, type); |
1853d1ef | 106 | |
51e7a012 | 107 | put_u32(buf, mlen + 1); |
7203d6bb | 108 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
ca75d7de | 109 | if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) |
05624c18 | 110 | fatal("%s: write: %s", __func__, strerror(errno)); |
ca75d7de | 111 | if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) |
05624c18 | 112 | fatal("%s: write: %s", __func__, strerror(errno)); |
1853d1ef | 113 | } |
114 | ||
115 | void | |
ca75d7de | 116 | mm_request_receive(int sock, Buffer *m) |
1853d1ef | 117 | { |
118 | u_char buf[4]; | |
1853d1ef | 119 | u_int msg_len; |
120 | ||
1588c277 | 121 | debug3("%s entering", __func__); |
1853d1ef | 122 | |
05624c18 | 123 | if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { |
124 | if (errno == EPIPE) | |
2362db19 | 125 | cleanup_exit(255); |
05624c18 | 126 | fatal("%s: read: %s", __func__, strerror(errno)); |
1853d1ef | 127 | } |
51e7a012 | 128 | msg_len = get_u32(buf); |
1853d1ef | 129 | if (msg_len > 256 * 1024) |
1588c277 | 130 | fatal("%s: read: bad msg_len %d", __func__, msg_len); |
1853d1ef | 131 | buffer_clear(m); |
132 | buffer_append_space(m, msg_len); | |
05624c18 | 133 | if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len) |
134 | fatal("%s: read: %s", __func__, strerror(errno)); | |
1853d1ef | 135 | } |
136 | ||
137 | void | |
ca75d7de | 138 | mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m) |
1853d1ef | 139 | { |
140 | u_char rtype; | |
141 | ||
1588c277 | 142 | debug3("%s entering: type %d", __func__, type); |
1853d1ef | 143 | |
ca75d7de | 144 | mm_request_receive(sock, m); |
1853d1ef | 145 | rtype = buffer_get_char(m); |
146 | if (rtype != type) | |
1588c277 | 147 | fatal("%s: read: rtype %d != type %d", __func__, |
1853d1ef | 148 | rtype, type); |
149 | } | |
150 | ||
151 | DH * | |
152 | mm_choose_dh(int min, int nbits, int max) | |
153 | { | |
154 | BIGNUM *p, *g; | |
155 | int success = 0; | |
156 | Buffer m; | |
157 | ||
158 | buffer_init(&m); | |
159 | buffer_put_int(&m, min); | |
160 | buffer_put_int(&m, nbits); | |
161 | buffer_put_int(&m, max); | |
162 | ||
b5a28cbc | 163 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
1853d1ef | 164 | |
1588c277 | 165 | debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); |
b5a28cbc | 166 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
1853d1ef | 167 | |
168 | success = buffer_get_char(&m); | |
169 | if (success == 0) | |
1588c277 | 170 | fatal("%s: MONITOR_ANS_MODULI failed", __func__); |
1853d1ef | 171 | |
172 | if ((p = BN_new()) == NULL) | |
1588c277 | 173 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 174 | if ((g = BN_new()) == NULL) |
1588c277 | 175 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 176 | buffer_get_bignum2(&m, p); |
177 | buffer_get_bignum2(&m, g); | |
178 | ||
1588c277 | 179 | debug3("%s: remaining %d", __func__, buffer_len(&m)); |
1853d1ef | 180 | buffer_free(&m); |
181 | ||
182 | return (dh_new_group(g, p)); | |
183 | } | |
184 | ||
185 | int | |
186 | mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen) | |
187 | { | |
b5a28cbc | 188 | Kex *kex = *pmonitor->m_pkex; |
1853d1ef | 189 | Buffer m; |
190 | ||
1588c277 | 191 | debug3("%s entering", __func__); |
1853d1ef | 192 | |
193 | buffer_init(&m); | |
194 | buffer_put_int(&m, kex->host_key_index(key)); | |
195 | buffer_put_string(&m, data, datalen); | |
196 | ||
b5a28cbc | 197 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
1853d1ef | 198 | |
1588c277 | 199 | debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); |
b5a28cbc | 200 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
1853d1ef | 201 | *sigp = buffer_get_string(&m, lenp); |
202 | buffer_free(&m); | |
203 | ||
204 | return (0); | |
205 | } | |
206 | ||
207 | struct passwd * | |
18a8f313 | 208 | mm_getpwnamallow(const char *username) |
1853d1ef | 209 | { |
210 | Buffer m; | |
211 | struct passwd *pw; | |
03bcbf84 | 212 | u_int len; |
213 | ServerOptions *newopts; | |
1853d1ef | 214 | |
1588c277 | 215 | debug3("%s entering", __func__); |
1853d1ef | 216 | |
217 | buffer_init(&m); | |
18a8f313 | 218 | buffer_put_cstring(&m, username); |
1853d1ef | 219 | |
b5a28cbc | 220 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
1853d1ef | 221 | |
1588c277 | 222 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); |
b5a28cbc | 223 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
1853d1ef | 224 | |
225 | if (buffer_get_char(&m) == 0) { | |
dc0cae51 | 226 | pw = NULL; |
227 | goto out; | |
1853d1ef | 228 | } |
03bcbf84 | 229 | pw = buffer_get_string(&m, &len); |
230 | if (len != sizeof(struct passwd)) | |
1588c277 | 231 | fatal("%s: struct passwd size mismatch", __func__); |
1853d1ef | 232 | pw->pw_name = buffer_get_string(&m, NULL); |
233 | pw->pw_passwd = buffer_get_string(&m, NULL); | |
234 | pw->pw_gecos = buffer_get_string(&m, NULL); | |
7b18c353 | 235 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
1853d1ef | 236 | pw->pw_class = buffer_get_string(&m, NULL); |
7b18c353 | 237 | #endif |
1853d1ef | 238 | pw->pw_dir = buffer_get_string(&m, NULL); |
239 | pw->pw_shell = buffer_get_string(&m, NULL); | |
03bcbf84 | 240 | |
dc0cae51 | 241 | out: |
03bcbf84 | 242 | /* copy options block as a Match directive may have changed some */ |
243 | newopts = buffer_get_string(&m, &len); | |
244 | if (len != sizeof(*newopts)) | |
245 | fatal("%s: option block size mismatch", __func__); | |
246 | if (newopts->banner != NULL) | |
247 | newopts->banner = buffer_get_string(&m, NULL); | |
248 | copy_set_server_options(&options, newopts, 1); | |
249 | xfree(newopts); | |
250 | ||
1853d1ef | 251 | buffer_free(&m); |
252 | ||
253 | return (pw); | |
254 | } | |
255 | ||
02cd6c56 | 256 | char * |
257 | mm_auth2_read_banner(void) | |
94a73cdc | 258 | { |
259 | Buffer m; | |
260 | char *banner; | |
261 | ||
1588c277 | 262 | debug3("%s entering", __func__); |
94a73cdc | 263 | |
264 | buffer_init(&m); | |
b5a28cbc | 265 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); |
94a73cdc | 266 | buffer_clear(&m); |
267 | ||
02cd6c56 | 268 | mm_request_receive_expect(pmonitor->m_recvfd, |
269 | MONITOR_ANS_AUTH2_READ_BANNER, &m); | |
94a73cdc | 270 | banner = buffer_get_string(&m, NULL); |
271 | buffer_free(&m); | |
7203d6bb | 272 | |
02cd6c56 | 273 | /* treat empty banner as missing banner */ |
274 | if (strlen(banner) == 0) { | |
275 | xfree(banner); | |
276 | banner = NULL; | |
277 | } | |
94a73cdc | 278 | return (banner); |
279 | } | |
280 | ||
1853d1ef | 281 | /* Inform the privileged process about service and style */ |
282 | ||
283 | void | |
284 | mm_inform_authserv(char *service, char *style) | |
285 | { | |
286 | Buffer m; | |
287 | ||
1588c277 | 288 | debug3("%s entering", __func__); |
1853d1ef | 289 | |
290 | buffer_init(&m); | |
291 | buffer_put_cstring(&m, service); | |
292 | buffer_put_cstring(&m, style ? style : ""); | |
293 | ||
b5a28cbc | 294 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
1853d1ef | 295 | |
296 | buffer_free(&m); | |
297 | } | |
298 | ||
299 | /* Do the password authentication */ | |
300 | int | |
301 | mm_auth_password(Authctxt *authctxt, char *password) | |
302 | { | |
303 | Buffer m; | |
304 | int authenticated = 0; | |
305 | ||
1588c277 | 306 | debug3("%s entering", __func__); |
1853d1ef | 307 | |
308 | buffer_init(&m); | |
309 | buffer_put_cstring(&m, password); | |
b5a28cbc | 310 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
1853d1ef | 311 | |
1588c277 | 312 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); |
b5a28cbc | 313 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
1853d1ef | 314 | |
315 | authenticated = buffer_get_int(&m); | |
316 | ||
317 | buffer_free(&m); | |
318 | ||
319 | debug3("%s: user %sauthenticated", | |
1588c277 | 320 | __func__, authenticated ? "" : "not "); |
1853d1ef | 321 | return (authenticated); |
322 | } | |
323 | ||
324 | int | |
325 | mm_user_key_allowed(struct passwd *pw, Key *key) | |
326 | { | |
327 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); | |
328 | } | |
329 | ||
330 | int | |
331 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, | |
332 | Key *key) | |
333 | { | |
334 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); | |
335 | } | |
336 | ||
337 | int | |
338 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, | |
339 | char *host, Key *key) | |
340 | { | |
341 | int ret; | |
342 | ||
343 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ | |
344 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); | |
345 | key->type = KEY_RSA1; | |
346 | return (ret); | |
347 | } | |
348 | ||
349 | static void | |
350 | mm_send_debug(Buffer *m) | |
351 | { | |
352 | char *msg; | |
353 | ||
354 | while (buffer_len(m)) { | |
355 | msg = buffer_get_string(m, NULL); | |
1588c277 | 356 | debug3("%s: Sending debug: %s", __func__, msg); |
1853d1ef | 357 | packet_send_debug("%s", msg); |
358 | xfree(msg); | |
359 | } | |
360 | } | |
361 | ||
362 | int | |
363 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) | |
364 | { | |
365 | Buffer m; | |
366 | u_char *blob; | |
367 | u_int len; | |
92e15201 | 368 | int allowed = 0, have_forced = 0; |
1853d1ef | 369 | |
1588c277 | 370 | debug3("%s entering", __func__); |
1853d1ef | 371 | |
372 | /* Convert the key to a blob and the pass it over */ | |
373 | if (!key_to_blob(key, &blob, &len)) | |
374 | return (0); | |
375 | ||
376 | buffer_init(&m); | |
377 | buffer_put_int(&m, type); | |
378 | buffer_put_cstring(&m, user ? user : ""); | |
379 | buffer_put_cstring(&m, host ? host : ""); | |
380 | buffer_put_string(&m, blob, len); | |
381 | xfree(blob); | |
382 | ||
b5a28cbc | 383 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
1853d1ef | 384 | |
1588c277 | 385 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); |
b5a28cbc | 386 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
1853d1ef | 387 | |
388 | allowed = buffer_get_int(&m); | |
389 | ||
92e15201 | 390 | /* fake forced command */ |
391 | auth_clear_options(); | |
392 | have_forced = buffer_get_int(&m); | |
393 | forced_command = have_forced ? xstrdup("true") : NULL; | |
394 | ||
1853d1ef | 395 | /* Send potential debug messages */ |
396 | mm_send_debug(&m); | |
397 | ||
398 | buffer_free(&m); | |
399 | ||
400 | return (allowed); | |
401 | } | |
402 | ||
403 | /* | |
404 | * This key verify needs to send the key type along, because the | |
405 | * privileged parent makes the decision if the key is allowed | |
406 | * for authentication. | |
407 | */ | |
408 | ||
409 | int | |
410 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) | |
411 | { | |
412 | Buffer m; | |
413 | u_char *blob; | |
414 | u_int len; | |
415 | int verified = 0; | |
416 | ||
1588c277 | 417 | debug3("%s entering", __func__); |
1853d1ef | 418 | |
419 | /* Convert the key to a blob and the pass it over */ | |
420 | if (!key_to_blob(key, &blob, &len)) | |
421 | return (0); | |
422 | ||
423 | buffer_init(&m); | |
424 | buffer_put_string(&m, blob, len); | |
425 | buffer_put_string(&m, sig, siglen); | |
426 | buffer_put_string(&m, data, datalen); | |
427 | xfree(blob); | |
428 | ||
b5a28cbc | 429 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
1853d1ef | 430 | |
1588c277 | 431 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); |
b5a28cbc | 432 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
1853d1ef | 433 | |
434 | verified = buffer_get_int(&m); | |
435 | ||
436 | buffer_free(&m); | |
437 | ||
438 | return (verified); | |
439 | } | |
440 | ||
441 | /* Export key state after authentication */ | |
442 | Newkeys * | |
443 | mm_newkeys_from_blob(u_char *blob, int blen) | |
444 | { | |
445 | Buffer b; | |
446 | u_int len; | |
447 | Newkeys *newkey = NULL; | |
448 | Enc *enc; | |
449 | Mac *mac; | |
450 | Comp *comp; | |
451 | ||
1588c277 | 452 | debug3("%s: %p(%d)", __func__, blob, blen); |
1853d1ef | 453 | #ifdef DEBUG_PK |
454 | dump_base64(stderr, blob, blen); | |
455 | #endif | |
456 | buffer_init(&b); | |
457 | buffer_append(&b, blob, blen); | |
458 | ||
459 | newkey = xmalloc(sizeof(*newkey)); | |
460 | enc = &newkey->enc; | |
461 | mac = &newkey->mac; | |
462 | comp = &newkey->comp; | |
463 | ||
464 | /* Enc structure */ | |
465 | enc->name = buffer_get_string(&b, NULL); | |
466 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); | |
467 | enc->enabled = buffer_get_int(&b); | |
468 | enc->block_size = buffer_get_int(&b); | |
469 | enc->key = buffer_get_string(&b, &enc->key_len); | |
470 | enc->iv = buffer_get_string(&b, &len); | |
471 | if (len != enc->block_size) | |
bb0640b2 | 472 | fatal("%s: bad ivlen: expected %u != %u", __func__, |
1853d1ef | 473 | enc->block_size, len); |
474 | ||
475 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) | |
1588c277 | 476 | fatal("%s: bad cipher name %s or pointer %p", __func__, |
1853d1ef | 477 | enc->name, enc->cipher); |
478 | ||
479 | /* Mac structure */ | |
480 | mac->name = buffer_get_string(&b, NULL); | |
a3de8da1 | 481 | if (mac->name == NULL || mac_setup(mac, mac->name) == -1) |
f444d0f8 | 482 | fatal("%s: can not setup mac %s", __func__, mac->name); |
1853d1ef | 483 | mac->enabled = buffer_get_int(&b); |
484 | mac->key = buffer_get_string(&b, &len); | |
485 | if (len > mac->key_len) | |
bb0640b2 | 486 | fatal("%s: bad mac key length: %u > %d", __func__, len, |
1853d1ef | 487 | mac->key_len); |
488 | mac->key_len = len; | |
489 | ||
490 | /* Comp structure */ | |
491 | comp->type = buffer_get_int(&b); | |
492 | comp->enabled = buffer_get_int(&b); | |
493 | comp->name = buffer_get_string(&b, NULL); | |
494 | ||
495 | len = buffer_len(&b); | |
496 | if (len != 0) | |
bb0640b2 | 497 | error("newkeys_from_blob: remaining bytes in blob %u", len); |
1853d1ef | 498 | buffer_free(&b); |
499 | return (newkey); | |
500 | } | |
501 | ||
502 | int | |
503 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) | |
504 | { | |
505 | Buffer b; | |
506 | int len; | |
1853d1ef | 507 | Enc *enc; |
508 | Mac *mac; | |
509 | Comp *comp; | |
dc1f1948 | 510 | Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode); |
1853d1ef | 511 | |
1588c277 | 512 | debug3("%s: converting %p", __func__, newkey); |
1853d1ef | 513 | |
514 | if (newkey == NULL) { | |
1588c277 | 515 | error("%s: newkey == NULL", __func__); |
1853d1ef | 516 | return 0; |
517 | } | |
518 | enc = &newkey->enc; | |
519 | mac = &newkey->mac; | |
520 | comp = &newkey->comp; | |
521 | ||
522 | buffer_init(&b); | |
523 | /* Enc structure */ | |
524 | buffer_put_cstring(&b, enc->name); | |
525 | /* The cipher struct is constant and shared, you export pointer */ | |
526 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); | |
527 | buffer_put_int(&b, enc->enabled); | |
528 | buffer_put_int(&b, enc->block_size); | |
529 | buffer_put_string(&b, enc->key, enc->key_len); | |
530 | packet_get_keyiv(mode, enc->iv, enc->block_size); | |
531 | buffer_put_string(&b, enc->iv, enc->block_size); | |
532 | ||
533 | /* Mac structure */ | |
534 | buffer_put_cstring(&b, mac->name); | |
535 | buffer_put_int(&b, mac->enabled); | |
536 | buffer_put_string(&b, mac->key, mac->key_len); | |
537 | ||
538 | /* Comp structure */ | |
539 | buffer_put_int(&b, comp->type); | |
540 | buffer_put_int(&b, comp->enabled); | |
541 | buffer_put_cstring(&b, comp->name); | |
542 | ||
543 | len = buffer_len(&b); | |
1853d1ef | 544 | if (lenp != NULL) |
545 | *lenp = len; | |
eb9f2fab | 546 | if (blobp != NULL) { |
547 | *blobp = xmalloc(len); | |
548 | memcpy(*blobp, buffer_ptr(&b), len); | |
549 | } | |
550 | memset(buffer_ptr(&b), 0, len); | |
551 | buffer_free(&b); | |
1853d1ef | 552 | return len; |
553 | } | |
554 | ||
555 | static void | |
556 | mm_send_kex(Buffer *m, Kex *kex) | |
557 | { | |
558 | buffer_put_string(m, kex->session_id, kex->session_id_len); | |
559 | buffer_put_int(m, kex->we_need); | |
560 | buffer_put_int(m, kex->hostkey_type); | |
561 | buffer_put_int(m, kex->kex_type); | |
562 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); | |
563 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); | |
564 | buffer_put_int(m, kex->flags); | |
565 | buffer_put_cstring(m, kex->client_version_string); | |
566 | buffer_put_cstring(m, kex->server_version_string); | |
567 | } | |
568 | ||
569 | void | |
ca75d7de | 570 | mm_send_keystate(struct monitor *monitor) |
1853d1ef | 571 | { |
dc1f1948 | 572 | Buffer m, *input, *output; |
1853d1ef | 573 | u_char *blob, *p; |
574 | u_int bloblen, plen; | |
ffd7b36b | 575 | u_int32_t seqnr, packets; |
e8e08a80 | 576 | u_int64_t blocks, bytes; |
1853d1ef | 577 | |
578 | buffer_init(&m); | |
579 | ||
580 | if (!compat20) { | |
581 | u_char iv[24]; | |
9459414c | 582 | u_char *key; |
583 | u_int ivlen, keylen; | |
1853d1ef | 584 | |
585 | buffer_put_int(&m, packet_get_protocol_flags()); | |
586 | ||
587 | buffer_put_int(&m, packet_get_ssh1_cipher()); | |
588 | ||
9459414c | 589 | debug3("%s: Sending ssh1 KEY+IV", __func__); |
590 | keylen = packet_get_encryption_key(NULL); | |
591 | key = xmalloc(keylen+1); /* add 1 if keylen == 0 */ | |
592 | keylen = packet_get_encryption_key(key); | |
593 | buffer_put_string(&m, key, keylen); | |
594 | memset(key, 0, keylen); | |
595 | xfree(key); | |
596 | ||
1853d1ef | 597 | ivlen = packet_get_keyiv_len(MODE_OUT); |
598 | packet_get_keyiv(MODE_OUT, iv, ivlen); | |
599 | buffer_put_string(&m, iv, ivlen); | |
600 | ivlen = packet_get_keyiv_len(MODE_OUT); | |
601 | packet_get_keyiv(MODE_IN, iv, ivlen); | |
602 | buffer_put_string(&m, iv, ivlen); | |
603 | goto skip; | |
604 | } else { | |
605 | /* Kex for rekeying */ | |
ca75d7de | 606 | mm_send_kex(&m, *monitor->m_pkex); |
1853d1ef | 607 | } |
608 | ||
609 | debug3("%s: Sending new keys: %p %p", | |
dc1f1948 | 610 | __func__, packet_get_newkeys(MODE_OUT), |
611 | packet_get_newkeys(MODE_IN)); | |
1853d1ef | 612 | |
613 | /* Keys from Kex */ | |
614 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) | |
1588c277 | 615 | fatal("%s: conversion of newkeys failed", __func__); |
1853d1ef | 616 | |
617 | buffer_put_string(&m, blob, bloblen); | |
618 | xfree(blob); | |
619 | ||
620 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) | |
1588c277 | 621 | fatal("%s: conversion of newkeys failed", __func__); |
1853d1ef | 622 | |
623 | buffer_put_string(&m, blob, bloblen); | |
624 | xfree(blob); | |
625 | ||
e8e08a80 | 626 | packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes); |
ffd7b36b | 627 | buffer_put_int(&m, seqnr); |
628 | buffer_put_int64(&m, blocks); | |
629 | buffer_put_int(&m, packets); | |
e8e08a80 | 630 | buffer_put_int64(&m, bytes); |
631 | packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes); | |
ffd7b36b | 632 | buffer_put_int(&m, seqnr); |
633 | buffer_put_int64(&m, blocks); | |
634 | buffer_put_int(&m, packets); | |
e8e08a80 | 635 | buffer_put_int64(&m, bytes); |
1853d1ef | 636 | |
1588c277 | 637 | debug3("%s: New keys have been sent", __func__); |
1853d1ef | 638 | skip: |
639 | /* More key context */ | |
640 | plen = packet_get_keycontext(MODE_OUT, NULL); | |
641 | p = xmalloc(plen+1); | |
642 | packet_get_keycontext(MODE_OUT, p); | |
643 | buffer_put_string(&m, p, plen); | |
644 | xfree(p); | |
645 | ||
646 | plen = packet_get_keycontext(MODE_IN, NULL); | |
647 | p = xmalloc(plen+1); | |
648 | packet_get_keycontext(MODE_IN, p); | |
649 | buffer_put_string(&m, p, plen); | |
650 | xfree(p); | |
651 | ||
652 | /* Compression state */ | |
1588c277 | 653 | debug3("%s: Sending compression state", __func__); |
1853d1ef | 654 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
655 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); | |
656 | ||
657 | /* Network I/O buffers */ | |
dc1f1948 | 658 | input = (Buffer *)packet_get_input(); |
659 | output = (Buffer *)packet_get_output(); | |
660 | buffer_put_string(&m, buffer_ptr(input), buffer_len(input)); | |
661 | buffer_put_string(&m, buffer_ptr(output), buffer_len(output)); | |
1853d1ef | 662 | |
ca75d7de | 663 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
1588c277 | 664 | debug3("%s: Finished sending state", __func__); |
1853d1ef | 665 | |
666 | buffer_free(&m); | |
667 | } | |
668 | ||
669 | int | |
148de80c | 670 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) |
1853d1ef | 671 | { |
672 | Buffer m; | |
be2ca0c9 | 673 | char *p, *msg; |
c6dca55e | 674 | int success = 0, tmp1 = -1, tmp2 = -1; |
675 | ||
676 | /* Kludge: ensure there are fds free to receive the pty/tty */ | |
677 | if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 || | |
678 | (tmp2 = dup(pmonitor->m_recvfd)) == -1) { | |
679 | error("%s: cannot allocate fds for pty", __func__); | |
680 | if (tmp1 > 0) | |
681 | close(tmp1); | |
682 | if (tmp2 > 0) | |
683 | close(tmp2); | |
684 | return 0; | |
685 | } | |
686 | close(tmp1); | |
687 | close(tmp2); | |
1853d1ef | 688 | |
689 | buffer_init(&m); | |
b5a28cbc | 690 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); |
1853d1ef | 691 | |
1588c277 | 692 | debug3("%s: waiting for MONITOR_ANS_PTY", __func__); |
b5a28cbc | 693 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); |
1853d1ef | 694 | |
695 | success = buffer_get_int(&m); | |
696 | if (success == 0) { | |
1588c277 | 697 | debug3("%s: pty alloc failed", __func__); |
1853d1ef | 698 | buffer_free(&m); |
699 | return (0); | |
700 | } | |
701 | p = buffer_get_string(&m, NULL); | |
be2ca0c9 | 702 | msg = buffer_get_string(&m, NULL); |
1853d1ef | 703 | buffer_free(&m); |
704 | ||
705 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ | |
706 | xfree(p); | |
707 | ||
be2ca0c9 | 708 | buffer_append(&loginmsg, msg, strlen(msg)); |
709 | xfree(msg); | |
710 | ||
8de7aaab | 711 | if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 || |
712 | (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1) | |
713 | fatal("%s: receive fds failed", __func__); | |
1853d1ef | 714 | |
715 | /* Success */ | |
716 | return (1); | |
717 | } | |
718 | ||
719 | void | |
2362db19 | 720 | mm_session_pty_cleanup2(Session *s) |
1853d1ef | 721 | { |
1853d1ef | 722 | Buffer m; |
723 | ||
724 | if (s->ttyfd == -1) | |
725 | return; | |
726 | buffer_init(&m); | |
727 | buffer_put_cstring(&m, s->tty); | |
b5a28cbc | 728 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
1853d1ef | 729 | buffer_free(&m); |
730 | ||
731 | /* closed dup'ed master */ | |
c6dca55e | 732 | if (s->ptymaster != -1 && close(s->ptymaster) < 0) |
733 | error("close(s->ptymaster/%d): %s", | |
734 | s->ptymaster, strerror(errno)); | |
1853d1ef | 735 | |
736 | /* unlink pty from session */ | |
737 | s->ttyfd = -1; | |
738 | } | |
739 | ||
ad200abb | 740 | #ifdef USE_PAM |
741 | void | |
529d73ab | 742 | mm_start_pam(Authctxt *authctxt) |
ad200abb | 743 | { |
744 | Buffer m; | |
745 | ||
1588c277 | 746 | debug3("%s entering", __func__); |
7fceb20d | 747 | if (!options.use_pam) |
748 | fatal("UsePAM=no, but ended up in %s anyway", __func__); | |
ad200abb | 749 | |
750 | buffer_init(&m); | |
b5a28cbc | 751 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); |
ad200abb | 752 | |
753 | buffer_free(&m); | |
754 | } | |
05114c74 | 755 | |
5b9e2464 | 756 | u_int |
757 | mm_do_pam_account(void) | |
758 | { | |
759 | Buffer m; | |
760 | u_int ret; | |
ba6dd90e | 761 | char *msg; |
5b9e2464 | 762 | |
763 | debug3("%s entering", __func__); | |
764 | if (!options.use_pam) | |
765 | fatal("UsePAM=no, but ended up in %s anyway", __func__); | |
766 | ||
767 | buffer_init(&m); | |
768 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m); | |
769 | ||
aff51935 | 770 | mm_request_receive_expect(pmonitor->m_recvfd, |
5b9e2464 | 771 | MONITOR_ANS_PAM_ACCOUNT, &m); |
772 | ret = buffer_get_int(&m); | |
ba6dd90e | 773 | msg = buffer_get_string(&m, NULL); |
774 | buffer_append(&loginmsg, msg, strlen(msg)); | |
775 | xfree(msg); | |
5b9e2464 | 776 | |
777 | buffer_free(&m); | |
b6453d99 | 778 | |
5b9e2464 | 779 | debug3("%s returning %d", __func__, ret); |
780 | ||
781 | return (ret); | |
782 | } | |
783 | ||
05114c74 | 784 | void * |
785 | mm_sshpam_init_ctx(Authctxt *authctxt) | |
786 | { | |
787 | Buffer m; | |
788 | int success; | |
789 | ||
790 | debug3("%s", __func__); | |
791 | buffer_init(&m); | |
792 | buffer_put_cstring(&m, authctxt->user); | |
793 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m); | |
794 | debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__); | |
795 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m); | |
796 | success = buffer_get_int(&m); | |
797 | if (success == 0) { | |
798 | debug3("%s: pam_init_ctx failed", __func__); | |
799 | buffer_free(&m); | |
800 | return (NULL); | |
801 | } | |
802 | buffer_free(&m); | |
803 | return (authctxt); | |
804 | } | |
805 | ||
806 | int | |
807 | mm_sshpam_query(void *ctx, char **name, char **info, | |
808 | u_int *num, char ***prompts, u_int **echo_on) | |
809 | { | |
810 | Buffer m; | |
a1a073cc | 811 | u_int i; |
812 | int ret; | |
05114c74 | 813 | |
814 | debug3("%s", __func__); | |
815 | buffer_init(&m); | |
816 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m); | |
817 | debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__); | |
818 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m); | |
819 | ret = buffer_get_int(&m); | |
820 | debug3("%s: pam_query returned %d", __func__, ret); | |
821 | *name = buffer_get_string(&m, NULL); | |
822 | *info = buffer_get_string(&m, NULL); | |
823 | *num = buffer_get_int(&m); | |
01d35895 | 824 | if (*num > PAM_MAX_NUM_MSG) |
825 | fatal("%s: recieved %u PAM messages, expected <= %u", | |
826 | __func__, *num, PAM_MAX_NUM_MSG); | |
827 | *prompts = xcalloc((*num + 1), sizeof(char *)); | |
828 | *echo_on = xcalloc((*num + 1), sizeof(u_int)); | |
05114c74 | 829 | for (i = 0; i < *num; ++i) { |
830 | (*prompts)[i] = buffer_get_string(&m, NULL); | |
831 | (*echo_on)[i] = buffer_get_int(&m); | |
832 | } | |
833 | buffer_free(&m); | |
834 | return (ret); | |
835 | } | |
836 | ||
837 | int | |
838 | mm_sshpam_respond(void *ctx, u_int num, char **resp) | |
839 | { | |
840 | Buffer m; | |
a1a073cc | 841 | u_int i; |
842 | int ret; | |
05114c74 | 843 | |
844 | debug3("%s", __func__); | |
845 | buffer_init(&m); | |
846 | buffer_put_int(&m, num); | |
847 | for (i = 0; i < num; ++i) | |
848 | buffer_put_cstring(&m, resp[i]); | |
849 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m); | |
850 | debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__); | |
851 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m); | |
852 | ret = buffer_get_int(&m); | |
853 | debug3("%s: pam_respond returned %d", __func__, ret); | |
854 | buffer_free(&m); | |
855 | return (ret); | |
856 | } | |
857 | ||
858 | void | |
859 | mm_sshpam_free_ctx(void *ctxtp) | |
860 | { | |
861 | Buffer m; | |
862 | ||
863 | debug3("%s", __func__); | |
864 | buffer_init(&m); | |
865 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m); | |
866 | debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__); | |
867 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m); | |
868 | buffer_free(&m); | |
869 | } | |
ad200abb | 870 | #endif /* USE_PAM */ |
871 | ||
1853d1ef | 872 | /* Request process termination */ |
873 | ||
874 | void | |
875 | mm_terminate(void) | |
876 | { | |
877 | Buffer m; | |
878 | ||
879 | buffer_init(&m); | |
b5a28cbc | 880 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); |
1853d1ef | 881 | buffer_free(&m); |
882 | } | |
883 | ||
884 | int | |
885 | mm_ssh1_session_key(BIGNUM *num) | |
886 | { | |
887 | int rsafail; | |
888 | Buffer m; | |
889 | ||
890 | buffer_init(&m); | |
891 | buffer_put_bignum2(&m, num); | |
b5a28cbc | 892 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
1853d1ef | 893 | |
b5a28cbc | 894 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
1853d1ef | 895 | |
896 | rsafail = buffer_get_int(&m); | |
897 | buffer_get_bignum2(&m, num); | |
898 | ||
899 | buffer_free(&m); | |
900 | ||
901 | return (rsafail); | |
902 | } | |
903 | ||
904 | static void | |
905 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, | |
906 | char ***prompts, u_int **echo_on) | |
907 | { | |
7203d6bb | 908 | *name = xstrdup(""); |
909 | *infotxt = xstrdup(""); | |
1853d1ef | 910 | *numprompts = 1; |
52e3daed | 911 | *prompts = xcalloc(*numprompts, sizeof(char *)); |
912 | *echo_on = xcalloc(*numprompts, sizeof(u_int)); | |
1853d1ef | 913 | (*echo_on)[0] = 0; |
914 | } | |
915 | ||
916 | int | |
917 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, | |
918 | u_int *numprompts, char ***prompts, u_int **echo_on) | |
919 | { | |
920 | Buffer m; | |
6d6c25e3 | 921 | u_int success; |
1853d1ef | 922 | char *challenge; |
923 | ||
1588c277 | 924 | debug3("%s: entering", __func__); |
1853d1ef | 925 | |
926 | buffer_init(&m); | |
b5a28cbc | 927 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
1853d1ef | 928 | |
b5a28cbc | 929 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
1853d1ef | 930 | &m); |
6d6c25e3 | 931 | success = buffer_get_int(&m); |
932 | if (success == 0) { | |
1588c277 | 933 | debug3("%s: no challenge", __func__); |
1853d1ef | 934 | buffer_free(&m); |
935 | return (-1); | |
936 | } | |
937 | ||
938 | /* Get the challenge, and format the response */ | |
939 | challenge = buffer_get_string(&m, NULL); | |
940 | buffer_free(&m); | |
941 | ||
942 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); | |
943 | (*prompts)[0] = challenge; | |
944 | ||
1588c277 | 945 | debug3("%s: received challenge: %s", __func__, challenge); |
1853d1ef | 946 | |
947 | return (0); | |
948 | } | |
949 | ||
950 | int | |
951 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) | |
952 | { | |
953 | Buffer m; | |
954 | int authok; | |
955 | ||
1588c277 | 956 | debug3("%s: entering", __func__); |
1853d1ef | 957 | if (numresponses != 1) |
958 | return (-1); | |
959 | ||
960 | buffer_init(&m); | |
961 | buffer_put_cstring(&m, responses[0]); | |
b5a28cbc | 962 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
1853d1ef | 963 | |
b5a28cbc | 964 | mm_request_receive_expect(pmonitor->m_recvfd, |
1853d1ef | 965 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
966 | ||
967 | authok = buffer_get_int(&m); | |
968 | buffer_free(&m); | |
969 | ||
970 | return ((authok == 0) ? -1 : 0); | |
971 | } | |
972 | ||
77751377 | 973 | #ifdef SKEY |
1853d1ef | 974 | int |
975 | mm_skey_query(void *ctx, char **name, char **infotxt, | |
976 | u_int *numprompts, char ***prompts, u_int **echo_on) | |
977 | { | |
978 | Buffer m; | |
6d6c25e3 | 979 | u_int success; |
cecc422f | 980 | char *challenge; |
1853d1ef | 981 | |
1588c277 | 982 | debug3("%s: entering", __func__); |
1853d1ef | 983 | |
984 | buffer_init(&m); | |
b5a28cbc | 985 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
1853d1ef | 986 | |
b5a28cbc | 987 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
1853d1ef | 988 | &m); |
6d6c25e3 | 989 | success = buffer_get_int(&m); |
990 | if (success == 0) { | |
1588c277 | 991 | debug3("%s: no challenge", __func__); |
1853d1ef | 992 | buffer_free(&m); |
993 | return (-1); | |
994 | } | |
995 | ||
996 | /* Get the challenge, and format the response */ | |
997 | challenge = buffer_get_string(&m, NULL); | |
998 | buffer_free(&m); | |
999 | ||
1588c277 | 1000 | debug3("%s: received challenge: %s", __func__, challenge); |
1853d1ef | 1001 | |
1002 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); | |
1003 | ||
52e3daed | 1004 | xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT); |
1853d1ef | 1005 | xfree(challenge); |
1006 | ||
1007 | return (0); | |
1008 | } | |
1009 | ||
1010 | int | |
1011 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) | |
1012 | { | |
1013 | Buffer m; | |
1014 | int authok; | |
1015 | ||
1588c277 | 1016 | debug3("%s: entering", __func__); |
1853d1ef | 1017 | if (numresponses != 1) |
1018 | return (-1); | |
1019 | ||
1020 | buffer_init(&m); | |
1021 | buffer_put_cstring(&m, responses[0]); | |
b5a28cbc | 1022 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
1853d1ef | 1023 | |
b5a28cbc | 1024 | mm_request_receive_expect(pmonitor->m_recvfd, |
1853d1ef | 1025 | MONITOR_ANS_SKEYRESPOND, &m); |
1026 | ||
1027 | authok = buffer_get_int(&m); | |
1028 | buffer_free(&m); | |
1029 | ||
1030 | return ((authok == 0) ? -1 : 0); | |
1031 | } | |
77751377 | 1032 | #endif /* SKEY */ |
1853d1ef | 1033 | |
1034 | void | |
1035 | mm_ssh1_session_id(u_char session_id[16]) | |
1036 | { | |
1037 | Buffer m; | |
1038 | int i; | |
1039 | ||
1588c277 | 1040 | debug3("%s entering", __func__); |
1853d1ef | 1041 | |
1042 | buffer_init(&m); | |
1043 | for (i = 0; i < 16; i++) | |
1044 | buffer_put_char(&m, session_id[i]); | |
1045 | ||
b5a28cbc | 1046 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
1853d1ef | 1047 | buffer_free(&m); |
1048 | } | |
1049 | ||
1050 | int | |
1051 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) | |
1052 | { | |
1053 | Buffer m; | |
1054 | Key *key; | |
1055 | u_char *blob; | |
1056 | u_int blen; | |
92e15201 | 1057 | int allowed = 0, have_forced = 0; |
1853d1ef | 1058 | |
1588c277 | 1059 | debug3("%s entering", __func__); |
1853d1ef | 1060 | |
1061 | buffer_init(&m); | |
1062 | buffer_put_bignum2(&m, client_n); | |
1063 | ||
b5a28cbc | 1064 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
1065 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); | |
1853d1ef | 1066 | |
1067 | allowed = buffer_get_int(&m); | |
1068 | ||
92e15201 | 1069 | /* fake forced command */ |
1070 | auth_clear_options(); | |
1071 | have_forced = buffer_get_int(&m); | |
1072 | forced_command = have_forced ? xstrdup("true") : NULL; | |
1073 | ||
1853d1ef | 1074 | if (allowed && rkey != NULL) { |
1075 | blob = buffer_get_string(&m, &blen); | |
1076 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1588c277 | 1077 | fatal("%s: key_from_blob failed", __func__); |
1853d1ef | 1078 | *rkey = key; |
1079 | xfree(blob); | |
1080 | } | |
1081 | mm_send_debug(&m); | |
1082 | buffer_free(&m); | |
1083 | ||
1084 | return (allowed); | |
1085 | } | |
1086 | ||
1087 | BIGNUM * | |
1088 | mm_auth_rsa_generate_challenge(Key *key) | |
1089 | { | |
1090 | Buffer m; | |
1091 | BIGNUM *challenge; | |
1092 | u_char *blob; | |
1093 | u_int blen; | |
1094 | ||
1588c277 | 1095 | debug3("%s entering", __func__); |
1853d1ef | 1096 | |
1097 | if ((challenge = BN_new()) == NULL) | |
1588c277 | 1098 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 1099 | |
1100 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ | |
1101 | if (key_to_blob(key, &blob, &blen) == 0) | |
1588c277 | 1102 | fatal("%s: key_to_blob failed", __func__); |
1853d1ef | 1103 | key->type = KEY_RSA1; |
1104 | ||
1105 | buffer_init(&m); | |
1106 | buffer_put_string(&m, blob, blen); | |
1107 | xfree(blob); | |
1108 | ||
b5a28cbc | 1109 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
1110 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); | |
1853d1ef | 1111 | |
1112 | buffer_get_bignum2(&m, challenge); | |
1113 | buffer_free(&m); | |
1114 | ||
1115 | return (challenge); | |
1116 | } | |
1117 | ||
1118 | int | |
1119 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) | |
1120 | { | |
1121 | Buffer m; | |
1122 | u_char *blob; | |
1123 | u_int blen; | |
1124 | int success = 0; | |
1125 | ||
1588c277 | 1126 | debug3("%s entering", __func__); |
1853d1ef | 1127 | |
1128 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ | |
1129 | if (key_to_blob(key, &blob, &blen) == 0) | |
1588c277 | 1130 | fatal("%s: key_to_blob failed", __func__); |
1853d1ef | 1131 | key->type = KEY_RSA1; |
1132 | ||
1133 | buffer_init(&m); | |
1134 | buffer_put_string(&m, blob, blen); | |
1135 | buffer_put_string(&m, response, 16); | |
1136 | xfree(blob); | |
1137 | ||
b5a28cbc | 1138 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
1139 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); | |
1853d1ef | 1140 | |
1141 | success = buffer_get_int(&m); | |
1142 | buffer_free(&m); | |
1143 | ||
1144 | return (success); | |
1145 | } | |
696f6bef | 1146 | |
6039eeef | 1147 | #ifdef SSH_AUDIT_EVENTS |
c00e4d75 | 1148 | void |
1149 | mm_audit_event(ssh_audit_event_t event) | |
1150 | { | |
1151 | Buffer m; | |
1152 | ||
1153 | debug3("%s entering", __func__); | |
1154 | ||
1155 | buffer_init(&m); | |
1156 | buffer_put_int(&m, event); | |
1157 | ||
1158 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m); | |
1159 | buffer_free(&m); | |
1160 | } | |
1161 | ||
1162 | void | |
1163 | mm_audit_run_command(const char *command) | |
1164 | { | |
1165 | Buffer m; | |
1166 | ||
1167 | debug3("%s entering command %s", __func__, command); | |
1168 | ||
1169 | buffer_init(&m); | |
1170 | buffer_put_cstring(&m, command); | |
1171 | ||
1172 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m); | |
1173 | buffer_free(&m); | |
1174 | } | |
6039eeef | 1175 | #endif /* SSH_AUDIT_EVENTS */ |
c00e4d75 | 1176 | |
7364bd04 | 1177 | #ifdef GSSAPI |
1178 | OM_uint32 | |
ca75d7de | 1179 | mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) |
7364bd04 | 1180 | { |
1181 | Buffer m; | |
1182 | OM_uint32 major; | |
1183 | ||
1184 | /* Client doesn't get to see the context */ | |
1185 | *ctx = NULL; | |
1186 | ||
1187 | buffer_init(&m); | |
ca75d7de | 1188 | buffer_put_string(&m, goid->elements, goid->length); |
7364bd04 | 1189 | |
1190 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m); | |
1191 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m); | |
1192 | ||
1193 | major = buffer_get_int(&m); | |
1194 | ||
1195 | buffer_free(&m); | |
1196 | return (major); | |
1197 | } | |
1198 | ||
1199 | OM_uint32 | |
1200 | mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, | |
1201 | gss_buffer_desc *out, OM_uint32 *flags) | |
1202 | { | |
1203 | Buffer m; | |
1204 | OM_uint32 major; | |
f99e1ca4 | 1205 | u_int len; |
7364bd04 | 1206 | |
1207 | buffer_init(&m); | |
1208 | buffer_put_string(&m, in->value, in->length); | |
1209 | ||
1210 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m); | |
1211 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m); | |
1212 | ||
1213 | major = buffer_get_int(&m); | |
f99e1ca4 | 1214 | out->value = buffer_get_string(&m, &len); |
1215 | out->length = len; | |
7364bd04 | 1216 | if (flags) |
1217 | *flags = buffer_get_int(&m); | |
1218 | ||
1219 | buffer_free(&m); | |
1220 | ||
1221 | return (major); | |
1222 | } | |
1223 | ||
0789992b | 1224 | OM_uint32 |
1225 | mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) | |
1226 | { | |
1227 | Buffer m; | |
1228 | OM_uint32 major; | |
1229 | ||
1230 | buffer_init(&m); | |
1231 | buffer_put_string(&m, gssbuf->value, gssbuf->length); | |
1232 | buffer_put_string(&m, gssmic->value, gssmic->length); | |
1233 | ||
1234 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m); | |
1235 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC, | |
1236 | &m); | |
1237 | ||
1238 | major = buffer_get_int(&m); | |
1239 | buffer_free(&m); | |
1240 | return(major); | |
1241 | } | |
1242 | ||
7364bd04 | 1243 | int |
1244 | mm_ssh_gssapi_userok(char *user) | |
1245 | { | |
1246 | Buffer m; | |
1247 | int authenticated = 0; | |
1248 | ||
1249 | buffer_init(&m); | |
1250 | ||
1251 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m); | |
1252 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK, | |
1253 | &m); | |
1254 | ||
1255 | authenticated = buffer_get_int(&m); | |
1256 | ||
1257 | buffer_free(&m); | |
1258 | debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); | |
1259 | return (authenticated); | |
1260 | } | |
1261 | #endif /* GSSAPI */ | |
5adf6b9a | 1262 | |
1263 | #ifdef JPAKE | |
1264 | void | |
1265 | mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s, | |
1266 | char **hash_scheme, char **salt) | |
1267 | { | |
1268 | Buffer m; | |
1269 | ||
1270 | debug3("%s entering", __func__); | |
1271 | ||
1272 | buffer_init(&m); | |
1273 | mm_request_send(pmonitor->m_recvfd, | |
1274 | MONITOR_REQ_JPAKE_GET_PWDATA, &m); | |
1275 | ||
1276 | debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__); | |
1277 | mm_request_receive_expect(pmonitor->m_recvfd, | |
1278 | MONITOR_ANS_JPAKE_GET_PWDATA, &m); | |
1279 | ||
1280 | *hash_scheme = buffer_get_string(&m, NULL); | |
1281 | *salt = buffer_get_string(&m, NULL); | |
1282 | ||
1283 | buffer_free(&m); | |
1284 | } | |
1285 | ||
1286 | void | |
5b01421b | 1287 | mm_jpake_step1(struct modp_group *grp, |
5adf6b9a | 1288 | u_char **id, u_int *id_len, |
1289 | BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, | |
1290 | u_char **priv1_proof, u_int *priv1_proof_len, | |
1291 | u_char **priv2_proof, u_int *priv2_proof_len) | |
1292 | { | |
1293 | Buffer m; | |
1294 | ||
1295 | debug3("%s entering", __func__); | |
1296 | ||
1297 | buffer_init(&m); | |
1298 | mm_request_send(pmonitor->m_recvfd, | |
1299 | MONITOR_REQ_JPAKE_STEP1, &m); | |
1300 | ||
1301 | debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__); | |
1302 | mm_request_receive_expect(pmonitor->m_recvfd, | |
1303 | MONITOR_ANS_JPAKE_STEP1, &m); | |
1304 | ||
1305 | if ((*priv1 = BN_new()) == NULL || | |
1306 | (*priv2 = BN_new()) == NULL || | |
1307 | (*g_priv1 = BN_new()) == NULL || | |
1308 | (*g_priv2 = BN_new()) == NULL) | |
1309 | fatal("%s: BN_new", __func__); | |
1310 | ||
1311 | *id = buffer_get_string(&m, id_len); | |
1312 | /* priv1 and priv2 are, well, private */ | |
1313 | buffer_get_bignum2(&m, *g_priv1); | |
1314 | buffer_get_bignum2(&m, *g_priv2); | |
1315 | *priv1_proof = buffer_get_string(&m, priv1_proof_len); | |
1316 | *priv2_proof = buffer_get_string(&m, priv2_proof_len); | |
1317 | ||
1318 | buffer_free(&m); | |
1319 | } | |
1320 | ||
1321 | void | |
5b01421b | 1322 | mm_jpake_step2(struct modp_group *grp, BIGNUM *s, |
5adf6b9a | 1323 | BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, |
1324 | const u_char *theirid, u_int theirid_len, | |
1325 | const u_char *myid, u_int myid_len, | |
1326 | const u_char *theirpub1_proof, u_int theirpub1_proof_len, | |
1327 | const u_char *theirpub2_proof, u_int theirpub2_proof_len, | |
1328 | BIGNUM **newpub, | |
1329 | u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len) | |
1330 | { | |
1331 | Buffer m; | |
1332 | ||
1333 | debug3("%s entering", __func__); | |
1334 | ||
1335 | buffer_init(&m); | |
1336 | /* monitor already has all bignums except theirpub1, theirpub2 */ | |
1337 | buffer_put_bignum2(&m, theirpub1); | |
1338 | buffer_put_bignum2(&m, theirpub2); | |
1339 | /* monitor already knows our id */ | |
1340 | buffer_put_string(&m, theirid, theirid_len); | |
1341 | buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len); | |
1342 | buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len); | |
1343 | ||
1344 | mm_request_send(pmonitor->m_recvfd, | |
1345 | MONITOR_REQ_JPAKE_STEP2, &m); | |
1346 | ||
1347 | debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__); | |
1348 | mm_request_receive_expect(pmonitor->m_recvfd, | |
1349 | MONITOR_ANS_JPAKE_STEP2, &m); | |
1350 | ||
1351 | if ((*newpub = BN_new()) == NULL) | |
1352 | fatal("%s: BN_new", __func__); | |
1353 | ||
1354 | buffer_get_bignum2(&m, *newpub); | |
1355 | *newpub_exponent_proof = buffer_get_string(&m, | |
1356 | newpub_exponent_proof_len); | |
1357 | ||
1358 | buffer_free(&m); | |
1359 | } | |
1360 | ||
1361 | void | |
5b01421b | 1362 | mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val, |
5adf6b9a | 1363 | BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2, |
1364 | BIGNUM *theirpub1, BIGNUM *theirpub2, | |
1365 | const u_char *my_id, u_int my_id_len, | |
1366 | const u_char *their_id, u_int their_id_len, | |
1367 | const u_char *sess_id, u_int sess_id_len, | |
1368 | const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len, | |
1369 | BIGNUM **k, | |
1370 | u_char **confirm_hash, u_int *confirm_hash_len) | |
1371 | { | |
1372 | Buffer m; | |
1373 | ||
1374 | debug3("%s entering", __func__); | |
1375 | ||
1376 | buffer_init(&m); | |
1377 | /* monitor already has all bignums except step2_val */ | |
1378 | buffer_put_bignum2(&m, step2_val); | |
1379 | /* monitor already knows all the ids */ | |
1380 | buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len); | |
1381 | ||
1382 | mm_request_send(pmonitor->m_recvfd, | |
1383 | MONITOR_REQ_JPAKE_KEY_CONFIRM, &m); | |
1384 | ||
1385 | debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__); | |
1386 | mm_request_receive_expect(pmonitor->m_recvfd, | |
1387 | MONITOR_ANS_JPAKE_KEY_CONFIRM, &m); | |
1388 | ||
1389 | /* 'k' is sensitive and stays in the monitor */ | |
1390 | *confirm_hash = buffer_get_string(&m, confirm_hash_len); | |
1391 | ||
1392 | buffer_free(&m); | |
1393 | } | |
1394 | ||
1395 | int | |
1396 | mm_jpake_check_confirm(const BIGNUM *k, | |
1397 | const u_char *peer_id, u_int peer_id_len, | |
1398 | const u_char *sess_id, u_int sess_id_len, | |
1399 | const u_char *peer_confirm_hash, u_int peer_confirm_hash_len) | |
1400 | { | |
1401 | Buffer m; | |
1402 | int success = 0; | |
1403 | ||
1404 | debug3("%s entering", __func__); | |
1405 | ||
1406 | buffer_init(&m); | |
1407 | /* k is dummy in slave, ignored */ | |
1408 | /* monitor knows all the ids */ | |
1409 | buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len); | |
1410 | mm_request_send(pmonitor->m_recvfd, | |
1411 | MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m); | |
1412 | ||
1413 | debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__); | |
1414 | mm_request_receive_expect(pmonitor->m_recvfd, | |
1415 | MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m); | |
1416 | ||
1417 | success = buffer_get_int(&m); | |
1418 | buffer_free(&m); | |
1419 | ||
1420 | debug3("%s: success = %d", __func__, success); | |
1421 | return success; | |
1422 | } | |
1423 | #endif /* JPAKE */ |