]>
Commit | Line | Data |
---|---|---|
1853d1ef | 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" | |
b77a87e5 | 28 | RCSID("$OpenBSD: monitor_wrap.c,v 1.20 2002/11/21 23:03:51 deraadt Exp $"); |
1853d1ef | 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; | |
b5a28cbc | 59 | extern struct monitor *pmonitor; |
1853d1ef | 60 | extern Buffer input, output; |
61 | ||
62 | void | |
63 | mm_request_send(int socket, enum monitor_reqtype type, Buffer *m) | |
64 | { | |
1853d1ef | 65 | u_int mlen = buffer_len(m); |
405a0d43 | 66 | u_char buf[5]; |
1853d1ef | 67 | |
1588c277 | 68 | debug3("%s entering: type %d", __func__, type); |
1853d1ef | 69 | |
70 | PUT_32BIT(buf, mlen + 1); | |
7203d6bb | 71 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
1853d1ef | 72 | if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf)) |
1588c277 | 73 | fatal("%s: write", __func__); |
1853d1ef | 74 | if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen) |
1588c277 | 75 | fatal("%s: write", __func__); |
1853d1ef | 76 | } |
77 | ||
78 | void | |
79 | mm_request_receive(int socket, Buffer *m) | |
80 | { | |
81 | u_char buf[4]; | |
1853d1ef | 82 | u_int msg_len; |
405a0d43 | 83 | ssize_t res; |
1853d1ef | 84 | |
1588c277 | 85 | debug3("%s entering", __func__); |
1853d1ef | 86 | |
87 | res = atomicio(read, socket, buf, sizeof(buf)); | |
88 | if (res != sizeof(buf)) { | |
89 | if (res == 0) | |
90 | fatal_cleanup(); | |
1588c277 | 91 | fatal("%s: read: %ld", __func__, (long)res); |
1853d1ef | 92 | } |
93 | msg_len = GET_32BIT(buf); | |
94 | if (msg_len > 256 * 1024) | |
1588c277 | 95 | fatal("%s: read: bad msg_len %d", __func__, msg_len); |
1853d1ef | 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) | |
1588c277 | 100 | fatal("%s: read: %ld != msg_len", __func__, (long)res); |
1853d1ef | 101 | } |
102 | ||
103 | void | |
104 | mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m) | |
105 | { | |
106 | u_char rtype; | |
107 | ||
1588c277 | 108 | debug3("%s entering: type %d", __func__, type); |
1853d1ef | 109 | |
110 | mm_request_receive(socket, m); | |
111 | rtype = buffer_get_char(m); | |
112 | if (rtype != type) | |
1588c277 | 113 | fatal("%s: read: rtype %d != type %d", __func__, |
1853d1ef | 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 | ||
b5a28cbc | 129 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
1853d1ef | 130 | |
1588c277 | 131 | debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); |
b5a28cbc | 132 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
1853d1ef | 133 | |
134 | success = buffer_get_char(&m); | |
135 | if (success == 0) | |
1588c277 | 136 | fatal("%s: MONITOR_ANS_MODULI failed", __func__); |
1853d1ef | 137 | |
138 | if ((p = BN_new()) == NULL) | |
1588c277 | 139 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 140 | if ((g = BN_new()) == NULL) |
1588c277 | 141 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 142 | buffer_get_bignum2(&m, p); |
143 | buffer_get_bignum2(&m, g); | |
144 | ||
1588c277 | 145 | debug3("%s: remaining %d", __func__, buffer_len(&m)); |
1853d1ef | 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 | { | |
b5a28cbc | 154 | Kex *kex = *pmonitor->m_pkex; |
1853d1ef | 155 | Buffer m; |
156 | ||
1588c277 | 157 | debug3("%s entering", __func__); |
1853d1ef | 158 | |
159 | buffer_init(&m); | |
160 | buffer_put_int(&m, kex->host_key_index(key)); | |
161 | buffer_put_string(&m, data, datalen); | |
162 | ||
b5a28cbc | 163 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
1853d1ef | 164 | |
1588c277 | 165 | debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); |
b5a28cbc | 166 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
1853d1ef | 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 | ||
1588c277 | 180 | debug3("%s entering", __func__); |
1853d1ef | 181 | |
182 | buffer_init(&m); | |
183 | buffer_put_cstring(&m, login); | |
184 | ||
b5a28cbc | 185 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
1853d1ef | 186 | |
1588c277 | 187 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); |
b5a28cbc | 188 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
1853d1ef | 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)) | |
1588c277 | 196 | fatal("%s: struct passwd size mismatch", __func__); |
1853d1ef | 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); | |
7b18c353 | 200 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
1853d1ef | 201 | pw->pw_class = buffer_get_string(&m, NULL); |
7b18c353 | 202 | #endif |
1853d1ef | 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 | ||
343288b8 | 210 | char *mm_auth2_read_banner(void) |
94a73cdc | 211 | { |
212 | Buffer m; | |
213 | char *banner; | |
214 | ||
1588c277 | 215 | debug3("%s entering", __func__); |
94a73cdc | 216 | |
217 | buffer_init(&m); | |
b5a28cbc | 218 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); |
94a73cdc | 219 | buffer_clear(&m); |
220 | ||
b5a28cbc | 221 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m); |
94a73cdc | 222 | banner = buffer_get_string(&m, NULL); |
223 | buffer_free(&m); | |
7203d6bb | 224 | |
94a73cdc | 225 | return (banner); |
226 | } | |
227 | ||
1853d1ef | 228 | /* Inform the privileged process about service and style */ |
229 | ||
230 | void | |
231 | mm_inform_authserv(char *service, char *style) | |
232 | { | |
233 | Buffer m; | |
234 | ||
1588c277 | 235 | debug3("%s entering", __func__); |
1853d1ef | 236 | |
237 | buffer_init(&m); | |
238 | buffer_put_cstring(&m, service); | |
239 | buffer_put_cstring(&m, style ? style : ""); | |
240 | ||
b5a28cbc | 241 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
1853d1ef | 242 | |
243 | buffer_free(&m); | |
244 | } | |
245 | ||
246 | /* Do the password authentication */ | |
247 | int | |
248 | mm_auth_password(Authctxt *authctxt, char *password) | |
249 | { | |
250 | Buffer m; | |
251 | int authenticated = 0; | |
252 | ||
1588c277 | 253 | debug3("%s entering", __func__); |
1853d1ef | 254 | |
255 | buffer_init(&m); | |
256 | buffer_put_cstring(&m, password); | |
b5a28cbc | 257 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
1853d1ef | 258 | |
1588c277 | 259 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); |
b5a28cbc | 260 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
1853d1ef | 261 | |
262 | authenticated = buffer_get_int(&m); | |
263 | ||
264 | buffer_free(&m); | |
265 | ||
266 | debug3("%s: user %sauthenticated", | |
1588c277 | 267 | __func__, authenticated ? "" : "not "); |
1853d1ef | 268 | return (authenticated); |
269 | } | |
270 | ||
271 | int | |
272 | mm_user_key_allowed(struct passwd *pw, Key *key) | |
273 | { | |
274 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); | |
275 | } | |
276 | ||
277 | int | |
278 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, | |
279 | Key *key) | |
280 | { | |
281 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); | |
282 | } | |
283 | ||
284 | int | |
285 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, | |
286 | char *host, Key *key) | |
287 | { | |
288 | int ret; | |
289 | ||
290 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ | |
291 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); | |
292 | key->type = KEY_RSA1; | |
293 | return (ret); | |
294 | } | |
295 | ||
296 | static void | |
297 | mm_send_debug(Buffer *m) | |
298 | { | |
299 | char *msg; | |
300 | ||
301 | while (buffer_len(m)) { | |
302 | msg = buffer_get_string(m, NULL); | |
1588c277 | 303 | debug3("%s: Sending debug: %s", __func__, msg); |
1853d1ef | 304 | packet_send_debug("%s", msg); |
305 | xfree(msg); | |
306 | } | |
307 | } | |
308 | ||
309 | int | |
310 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) | |
311 | { | |
312 | Buffer m; | |
313 | u_char *blob; | |
314 | u_int len; | |
315 | int allowed = 0; | |
316 | ||
1588c277 | 317 | debug3("%s entering", __func__); |
1853d1ef | 318 | |
319 | /* Convert the key to a blob and the pass it over */ | |
320 | if (!key_to_blob(key, &blob, &len)) | |
321 | return (0); | |
322 | ||
323 | buffer_init(&m); | |
324 | buffer_put_int(&m, type); | |
325 | buffer_put_cstring(&m, user ? user : ""); | |
326 | buffer_put_cstring(&m, host ? host : ""); | |
327 | buffer_put_string(&m, blob, len); | |
328 | xfree(blob); | |
329 | ||
b5a28cbc | 330 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
1853d1ef | 331 | |
1588c277 | 332 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); |
b5a28cbc | 333 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
1853d1ef | 334 | |
335 | allowed = buffer_get_int(&m); | |
336 | ||
337 | /* Send potential debug messages */ | |
338 | mm_send_debug(&m); | |
339 | ||
340 | buffer_free(&m); | |
341 | ||
342 | return (allowed); | |
343 | } | |
344 | ||
345 | /* | |
346 | * This key verify needs to send the key type along, because the | |
347 | * privileged parent makes the decision if the key is allowed | |
348 | * for authentication. | |
349 | */ | |
350 | ||
351 | int | |
352 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) | |
353 | { | |
354 | Buffer m; | |
355 | u_char *blob; | |
356 | u_int len; | |
357 | int verified = 0; | |
358 | ||
1588c277 | 359 | debug3("%s entering", __func__); |
1853d1ef | 360 | |
361 | /* Convert the key to a blob and the pass it over */ | |
362 | if (!key_to_blob(key, &blob, &len)) | |
363 | return (0); | |
364 | ||
365 | buffer_init(&m); | |
366 | buffer_put_string(&m, blob, len); | |
367 | buffer_put_string(&m, sig, siglen); | |
368 | buffer_put_string(&m, data, datalen); | |
369 | xfree(blob); | |
370 | ||
b5a28cbc | 371 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
1853d1ef | 372 | |
1588c277 | 373 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); |
b5a28cbc | 374 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
1853d1ef | 375 | |
376 | verified = buffer_get_int(&m); | |
377 | ||
378 | buffer_free(&m); | |
379 | ||
380 | return (verified); | |
381 | } | |
382 | ||
383 | /* Export key state after authentication */ | |
384 | Newkeys * | |
385 | mm_newkeys_from_blob(u_char *blob, int blen) | |
386 | { | |
387 | Buffer b; | |
388 | u_int len; | |
389 | Newkeys *newkey = NULL; | |
390 | Enc *enc; | |
391 | Mac *mac; | |
392 | Comp *comp; | |
393 | ||
1588c277 | 394 | debug3("%s: %p(%d)", __func__, blob, blen); |
1853d1ef | 395 | #ifdef DEBUG_PK |
396 | dump_base64(stderr, blob, blen); | |
397 | #endif | |
398 | buffer_init(&b); | |
399 | buffer_append(&b, blob, blen); | |
400 | ||
401 | newkey = xmalloc(sizeof(*newkey)); | |
402 | enc = &newkey->enc; | |
403 | mac = &newkey->mac; | |
404 | comp = &newkey->comp; | |
405 | ||
406 | /* Enc structure */ | |
407 | enc->name = buffer_get_string(&b, NULL); | |
408 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); | |
409 | enc->enabled = buffer_get_int(&b); | |
410 | enc->block_size = buffer_get_int(&b); | |
411 | enc->key = buffer_get_string(&b, &enc->key_len); | |
412 | enc->iv = buffer_get_string(&b, &len); | |
413 | if (len != enc->block_size) | |
bb0640b2 | 414 | fatal("%s: bad ivlen: expected %u != %u", __func__, |
1853d1ef | 415 | enc->block_size, len); |
416 | ||
417 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) | |
1588c277 | 418 | fatal("%s: bad cipher name %s or pointer %p", __func__, |
1853d1ef | 419 | enc->name, enc->cipher); |
420 | ||
421 | /* Mac structure */ | |
422 | mac->name = buffer_get_string(&b, NULL); | |
423 | if (mac->name == NULL || mac_init(mac, mac->name) == -1) | |
1588c277 | 424 | fatal("%s: can not init mac %s", __func__, mac->name); |
1853d1ef | 425 | mac->enabled = buffer_get_int(&b); |
426 | mac->key = buffer_get_string(&b, &len); | |
427 | if (len > mac->key_len) | |
bb0640b2 | 428 | fatal("%s: bad mac key length: %u > %d", __func__, len, |
1853d1ef | 429 | mac->key_len); |
430 | mac->key_len = len; | |
431 | ||
432 | /* Comp structure */ | |
433 | comp->type = buffer_get_int(&b); | |
434 | comp->enabled = buffer_get_int(&b); | |
435 | comp->name = buffer_get_string(&b, NULL); | |
436 | ||
437 | len = buffer_len(&b); | |
438 | if (len != 0) | |
bb0640b2 | 439 | error("newkeys_from_blob: remaining bytes in blob %u", len); |
1853d1ef | 440 | buffer_free(&b); |
441 | return (newkey); | |
442 | } | |
443 | ||
444 | int | |
445 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) | |
446 | { | |
447 | Buffer b; | |
448 | int len; | |
1853d1ef | 449 | Enc *enc; |
450 | Mac *mac; | |
451 | Comp *comp; | |
452 | Newkeys *newkey = newkeys[mode]; | |
453 | ||
1588c277 | 454 | debug3("%s: converting %p", __func__, newkey); |
1853d1ef | 455 | |
456 | if (newkey == NULL) { | |
1588c277 | 457 | error("%s: newkey == NULL", __func__); |
1853d1ef | 458 | return 0; |
459 | } | |
460 | enc = &newkey->enc; | |
461 | mac = &newkey->mac; | |
462 | comp = &newkey->comp; | |
463 | ||
464 | buffer_init(&b); | |
465 | /* Enc structure */ | |
466 | buffer_put_cstring(&b, enc->name); | |
467 | /* The cipher struct is constant and shared, you export pointer */ | |
468 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); | |
469 | buffer_put_int(&b, enc->enabled); | |
470 | buffer_put_int(&b, enc->block_size); | |
471 | buffer_put_string(&b, enc->key, enc->key_len); | |
472 | packet_get_keyiv(mode, enc->iv, enc->block_size); | |
473 | buffer_put_string(&b, enc->iv, enc->block_size); | |
474 | ||
475 | /* Mac structure */ | |
476 | buffer_put_cstring(&b, mac->name); | |
477 | buffer_put_int(&b, mac->enabled); | |
478 | buffer_put_string(&b, mac->key, mac->key_len); | |
479 | ||
480 | /* Comp structure */ | |
481 | buffer_put_int(&b, comp->type); | |
482 | buffer_put_int(&b, comp->enabled); | |
483 | buffer_put_cstring(&b, comp->name); | |
484 | ||
485 | len = buffer_len(&b); | |
1853d1ef | 486 | if (lenp != NULL) |
487 | *lenp = len; | |
eb9f2fab | 488 | if (blobp != NULL) { |
489 | *blobp = xmalloc(len); | |
490 | memcpy(*blobp, buffer_ptr(&b), len); | |
491 | } | |
492 | memset(buffer_ptr(&b), 0, len); | |
493 | buffer_free(&b); | |
1853d1ef | 494 | return len; |
495 | } | |
496 | ||
497 | static void | |
498 | mm_send_kex(Buffer *m, Kex *kex) | |
499 | { | |
500 | buffer_put_string(m, kex->session_id, kex->session_id_len); | |
501 | buffer_put_int(m, kex->we_need); | |
502 | buffer_put_int(m, kex->hostkey_type); | |
503 | buffer_put_int(m, kex->kex_type); | |
504 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); | |
505 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); | |
506 | buffer_put_int(m, kex->flags); | |
507 | buffer_put_cstring(m, kex->client_version_string); | |
508 | buffer_put_cstring(m, kex->server_version_string); | |
509 | } | |
510 | ||
511 | void | |
b5a28cbc | 512 | mm_send_keystate(struct monitor *pmonitor) |
1853d1ef | 513 | { |
514 | Buffer m; | |
515 | u_char *blob, *p; | |
516 | u_int bloblen, plen; | |
517 | ||
518 | buffer_init(&m); | |
519 | ||
520 | if (!compat20) { | |
521 | u_char iv[24]; | |
9459414c | 522 | u_char *key; |
523 | u_int ivlen, keylen; | |
1853d1ef | 524 | |
525 | buffer_put_int(&m, packet_get_protocol_flags()); | |
526 | ||
527 | buffer_put_int(&m, packet_get_ssh1_cipher()); | |
528 | ||
9459414c | 529 | debug3("%s: Sending ssh1 KEY+IV", __func__); |
530 | keylen = packet_get_encryption_key(NULL); | |
531 | key = xmalloc(keylen+1); /* add 1 if keylen == 0 */ | |
532 | keylen = packet_get_encryption_key(key); | |
533 | buffer_put_string(&m, key, keylen); | |
534 | memset(key, 0, keylen); | |
535 | xfree(key); | |
536 | ||
1853d1ef | 537 | ivlen = packet_get_keyiv_len(MODE_OUT); |
538 | packet_get_keyiv(MODE_OUT, iv, ivlen); | |
539 | buffer_put_string(&m, iv, ivlen); | |
540 | ivlen = packet_get_keyiv_len(MODE_OUT); | |
541 | packet_get_keyiv(MODE_IN, iv, ivlen); | |
542 | buffer_put_string(&m, iv, ivlen); | |
543 | goto skip; | |
544 | } else { | |
545 | /* Kex for rekeying */ | |
b5a28cbc | 546 | mm_send_kex(&m, *pmonitor->m_pkex); |
1853d1ef | 547 | } |
548 | ||
549 | debug3("%s: Sending new keys: %p %p", | |
1588c277 | 550 | __func__, newkeys[MODE_OUT], newkeys[MODE_IN]); |
1853d1ef | 551 | |
552 | /* Keys from Kex */ | |
553 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) | |
1588c277 | 554 | fatal("%s: conversion of newkeys failed", __func__); |
1853d1ef | 555 | |
556 | buffer_put_string(&m, blob, bloblen); | |
557 | xfree(blob); | |
558 | ||
559 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) | |
1588c277 | 560 | fatal("%s: conversion of newkeys failed", __func__); |
1853d1ef | 561 | |
562 | buffer_put_string(&m, blob, bloblen); | |
563 | xfree(blob); | |
564 | ||
565 | buffer_put_int(&m, packet_get_seqnr(MODE_OUT)); | |
566 | buffer_put_int(&m, packet_get_seqnr(MODE_IN)); | |
567 | ||
1588c277 | 568 | debug3("%s: New keys have been sent", __func__); |
1853d1ef | 569 | skip: |
570 | /* More key context */ | |
571 | plen = packet_get_keycontext(MODE_OUT, NULL); | |
572 | p = xmalloc(plen+1); | |
573 | packet_get_keycontext(MODE_OUT, p); | |
574 | buffer_put_string(&m, p, plen); | |
575 | xfree(p); | |
576 | ||
577 | plen = packet_get_keycontext(MODE_IN, NULL); | |
578 | p = xmalloc(plen+1); | |
579 | packet_get_keycontext(MODE_IN, p); | |
580 | buffer_put_string(&m, p, plen); | |
581 | xfree(p); | |
582 | ||
583 | /* Compression state */ | |
1588c277 | 584 | debug3("%s: Sending compression state", __func__); |
1853d1ef | 585 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
586 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); | |
587 | ||
588 | /* Network I/O buffers */ | |
589 | buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input)); | |
590 | buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output)); | |
591 | ||
b5a28cbc | 592 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
1588c277 | 593 | debug3("%s: Finished sending state", __func__); |
1853d1ef | 594 | |
595 | buffer_free(&m); | |
596 | } | |
597 | ||
598 | int | |
599 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) | |
600 | { | |
601 | Buffer m; | |
661e45a0 | 602 | char *p; |
1853d1ef | 603 | int success = 0; |
604 | ||
605 | buffer_init(&m); | |
b5a28cbc | 606 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); |
1853d1ef | 607 | |
1588c277 | 608 | debug3("%s: waiting for MONITOR_ANS_PTY", __func__); |
b5a28cbc | 609 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); |
1853d1ef | 610 | |
611 | success = buffer_get_int(&m); | |
612 | if (success == 0) { | |
1588c277 | 613 | debug3("%s: pty alloc failed", __func__); |
1853d1ef | 614 | buffer_free(&m); |
615 | return (0); | |
616 | } | |
617 | p = buffer_get_string(&m, NULL); | |
618 | buffer_free(&m); | |
619 | ||
620 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ | |
621 | xfree(p); | |
622 | ||
b5a28cbc | 623 | *ptyfd = mm_receive_fd(pmonitor->m_recvfd); |
624 | *ttyfd = mm_receive_fd(pmonitor->m_recvfd); | |
1853d1ef | 625 | |
626 | /* Success */ | |
627 | return (1); | |
628 | } | |
629 | ||
630 | void | |
631 | mm_session_pty_cleanup2(void *session) | |
632 | { | |
633 | Session *s = session; | |
634 | Buffer m; | |
635 | ||
636 | if (s->ttyfd == -1) | |
637 | return; | |
638 | buffer_init(&m); | |
639 | buffer_put_cstring(&m, s->tty); | |
b5a28cbc | 640 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
1853d1ef | 641 | buffer_free(&m); |
642 | ||
643 | /* closed dup'ed master */ | |
644 | if (close(s->ptymaster) < 0) | |
645 | error("close(s->ptymaster): %s", strerror(errno)); | |
646 | ||
647 | /* unlink pty from session */ | |
648 | s->ttyfd = -1; | |
649 | } | |
650 | ||
ad200abb | 651 | #ifdef USE_PAM |
652 | void | |
653 | mm_start_pam(char *user) | |
654 | { | |
655 | Buffer m; | |
656 | ||
1588c277 | 657 | debug3("%s entering", __func__); |
ad200abb | 658 | |
659 | buffer_init(&m); | |
660 | buffer_put_cstring(&m, user); | |
661 | ||
b5a28cbc | 662 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); |
ad200abb | 663 | |
664 | buffer_free(&m); | |
665 | } | |
666 | #endif /* USE_PAM */ | |
667 | ||
1853d1ef | 668 | /* Request process termination */ |
669 | ||
670 | void | |
671 | mm_terminate(void) | |
672 | { | |
673 | Buffer m; | |
674 | ||
675 | buffer_init(&m); | |
b5a28cbc | 676 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); |
1853d1ef | 677 | buffer_free(&m); |
678 | } | |
679 | ||
680 | int | |
681 | mm_ssh1_session_key(BIGNUM *num) | |
682 | { | |
683 | int rsafail; | |
684 | Buffer m; | |
685 | ||
686 | buffer_init(&m); | |
687 | buffer_put_bignum2(&m, num); | |
b5a28cbc | 688 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
1853d1ef | 689 | |
b5a28cbc | 690 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
1853d1ef | 691 | |
692 | rsafail = buffer_get_int(&m); | |
693 | buffer_get_bignum2(&m, num); | |
694 | ||
695 | buffer_free(&m); | |
696 | ||
697 | return (rsafail); | |
698 | } | |
699 | ||
700 | static void | |
701 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, | |
702 | char ***prompts, u_int **echo_on) | |
703 | { | |
7203d6bb | 704 | *name = xstrdup(""); |
705 | *infotxt = xstrdup(""); | |
1853d1ef | 706 | *numprompts = 1; |
343288b8 | 707 | *prompts = xmalloc(*numprompts * sizeof(char *)); |
1853d1ef | 708 | *echo_on = xmalloc(*numprompts * sizeof(u_int)); |
709 | (*echo_on)[0] = 0; | |
710 | } | |
711 | ||
712 | int | |
713 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, | |
714 | u_int *numprompts, char ***prompts, u_int **echo_on) | |
715 | { | |
716 | Buffer m; | |
717 | int res; | |
718 | char *challenge; | |
719 | ||
1588c277 | 720 | debug3("%s: entering", __func__); |
1853d1ef | 721 | |
722 | buffer_init(&m); | |
b5a28cbc | 723 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
1853d1ef | 724 | |
b5a28cbc | 725 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
1853d1ef | 726 | &m); |
727 | res = buffer_get_int(&m); | |
728 | if (res == -1) { | |
1588c277 | 729 | debug3("%s: no challenge", __func__); |
1853d1ef | 730 | buffer_free(&m); |
731 | return (-1); | |
732 | } | |
733 | ||
734 | /* Get the challenge, and format the response */ | |
735 | challenge = buffer_get_string(&m, NULL); | |
736 | buffer_free(&m); | |
737 | ||
738 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); | |
739 | (*prompts)[0] = challenge; | |
740 | ||
1588c277 | 741 | debug3("%s: received challenge: %s", __func__, challenge); |
1853d1ef | 742 | |
743 | return (0); | |
744 | } | |
745 | ||
746 | int | |
747 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) | |
748 | { | |
749 | Buffer m; | |
750 | int authok; | |
751 | ||
1588c277 | 752 | debug3("%s: entering", __func__); |
1853d1ef | 753 | if (numresponses != 1) |
754 | return (-1); | |
755 | ||
756 | buffer_init(&m); | |
757 | buffer_put_cstring(&m, responses[0]); | |
b5a28cbc | 758 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
1853d1ef | 759 | |
b5a28cbc | 760 | mm_request_receive_expect(pmonitor->m_recvfd, |
1853d1ef | 761 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
762 | ||
763 | authok = buffer_get_int(&m); | |
764 | buffer_free(&m); | |
765 | ||
766 | return ((authok == 0) ? -1 : 0); | |
767 | } | |
768 | ||
769 | int | |
770 | mm_skey_query(void *ctx, char **name, char **infotxt, | |
771 | u_int *numprompts, char ***prompts, u_int **echo_on) | |
772 | { | |
773 | Buffer m; | |
774 | int len, res; | |
775 | char *p, *challenge; | |
776 | ||
1588c277 | 777 | debug3("%s: entering", __func__); |
1853d1ef | 778 | |
779 | buffer_init(&m); | |
b5a28cbc | 780 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
1853d1ef | 781 | |
b5a28cbc | 782 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
1853d1ef | 783 | &m); |
784 | res = buffer_get_int(&m); | |
785 | if (res == -1) { | |
1588c277 | 786 | debug3("%s: no challenge", __func__); |
1853d1ef | 787 | buffer_free(&m); |
788 | return (-1); | |
789 | } | |
790 | ||
791 | /* Get the challenge, and format the response */ | |
792 | challenge = buffer_get_string(&m, NULL); | |
793 | buffer_free(&m); | |
794 | ||
1588c277 | 795 | debug3("%s: received challenge: %s", __func__, challenge); |
1853d1ef | 796 | |
797 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); | |
798 | ||
799 | len = strlen(challenge) + strlen(SKEY_PROMPT) + 1; | |
800 | p = xmalloc(len); | |
801 | strlcpy(p, challenge, len); | |
802 | strlcat(p, SKEY_PROMPT, len); | |
803 | (*prompts)[0] = p; | |
804 | xfree(challenge); | |
805 | ||
806 | return (0); | |
807 | } | |
808 | ||
809 | int | |
810 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) | |
811 | { | |
812 | Buffer m; | |
813 | int authok; | |
814 | ||
1588c277 | 815 | debug3("%s: entering", __func__); |
1853d1ef | 816 | if (numresponses != 1) |
817 | return (-1); | |
818 | ||
819 | buffer_init(&m); | |
820 | buffer_put_cstring(&m, responses[0]); | |
b5a28cbc | 821 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
1853d1ef | 822 | |
b5a28cbc | 823 | mm_request_receive_expect(pmonitor->m_recvfd, |
1853d1ef | 824 | MONITOR_ANS_SKEYRESPOND, &m); |
825 | ||
826 | authok = buffer_get_int(&m); | |
827 | buffer_free(&m); | |
828 | ||
829 | return ((authok == 0) ? -1 : 0); | |
830 | } | |
831 | ||
832 | void | |
833 | mm_ssh1_session_id(u_char session_id[16]) | |
834 | { | |
835 | Buffer m; | |
836 | int i; | |
837 | ||
1588c277 | 838 | debug3("%s entering", __func__); |
1853d1ef | 839 | |
840 | buffer_init(&m); | |
841 | for (i = 0; i < 16; i++) | |
842 | buffer_put_char(&m, session_id[i]); | |
843 | ||
b5a28cbc | 844 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
1853d1ef | 845 | buffer_free(&m); |
846 | } | |
847 | ||
848 | int | |
849 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) | |
850 | { | |
851 | Buffer m; | |
852 | Key *key; | |
853 | u_char *blob; | |
854 | u_int blen; | |
855 | int allowed = 0; | |
856 | ||
1588c277 | 857 | debug3("%s entering", __func__); |
1853d1ef | 858 | |
859 | buffer_init(&m); | |
860 | buffer_put_bignum2(&m, client_n); | |
861 | ||
b5a28cbc | 862 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
863 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); | |
1853d1ef | 864 | |
865 | allowed = buffer_get_int(&m); | |
866 | ||
867 | if (allowed && rkey != NULL) { | |
868 | blob = buffer_get_string(&m, &blen); | |
869 | if ((key = key_from_blob(blob, blen)) == NULL) | |
1588c277 | 870 | fatal("%s: key_from_blob failed", __func__); |
1853d1ef | 871 | *rkey = key; |
872 | xfree(blob); | |
873 | } | |
874 | mm_send_debug(&m); | |
875 | buffer_free(&m); | |
876 | ||
877 | return (allowed); | |
878 | } | |
879 | ||
880 | BIGNUM * | |
881 | mm_auth_rsa_generate_challenge(Key *key) | |
882 | { | |
883 | Buffer m; | |
884 | BIGNUM *challenge; | |
885 | u_char *blob; | |
886 | u_int blen; | |
887 | ||
1588c277 | 888 | debug3("%s entering", __func__); |
1853d1ef | 889 | |
890 | if ((challenge = BN_new()) == NULL) | |
1588c277 | 891 | fatal("%s: BN_new failed", __func__); |
1853d1ef | 892 | |
893 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ | |
894 | if (key_to_blob(key, &blob, &blen) == 0) | |
1588c277 | 895 | fatal("%s: key_to_blob failed", __func__); |
1853d1ef | 896 | key->type = KEY_RSA1; |
897 | ||
898 | buffer_init(&m); | |
899 | buffer_put_string(&m, blob, blen); | |
900 | xfree(blob); | |
901 | ||
b5a28cbc | 902 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
903 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); | |
1853d1ef | 904 | |
905 | buffer_get_bignum2(&m, challenge); | |
906 | buffer_free(&m); | |
907 | ||
908 | return (challenge); | |
909 | } | |
910 | ||
911 | int | |
912 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) | |
913 | { | |
914 | Buffer m; | |
915 | u_char *blob; | |
916 | u_int blen; | |
917 | int success = 0; | |
918 | ||
1588c277 | 919 | debug3("%s entering", __func__); |
1853d1ef | 920 | |
921 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ | |
922 | if (key_to_blob(key, &blob, &blen) == 0) | |
1588c277 | 923 | fatal("%s: key_to_blob failed", __func__); |
1853d1ef | 924 | key->type = KEY_RSA1; |
925 | ||
926 | buffer_init(&m); | |
927 | buffer_put_string(&m, blob, blen); | |
928 | buffer_put_string(&m, response, 16); | |
929 | xfree(blob); | |
930 | ||
b5a28cbc | 931 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
932 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); | |
1853d1ef | 933 | |
934 | success = buffer_get_int(&m); | |
935 | buffer_free(&m); | |
936 | ||
937 | return (success); | |
938 | } | |
696f6bef | 939 | |
2db9d8aa | 940 | #ifdef KRB4 |
941 | int | |
942 | mm_auth_krb4(Authctxt *authctxt, void *_auth, char **client, void *_reply) | |
943 | { | |
944 | KTEXT auth, reply; | |
945 | Buffer m; | |
946 | u_int rlen; | |
947 | int success = 0; | |
948 | char *p; | |
949 | ||
950 | debug3("%s entering", __func__); | |
951 | auth = _auth; | |
952 | reply = _reply; | |
953 | ||
954 | buffer_init(&m); | |
955 | buffer_put_string(&m, auth->dat, auth->length); | |
956 | ||
957 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB4, &m); | |
958 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB4, &m); | |
959 | ||
960 | success = buffer_get_int(&m); | |
961 | if (success) { | |
962 | *client = buffer_get_string(&m, NULL); | |
963 | p = buffer_get_string(&m, &rlen); | |
964 | if (rlen >= MAX_KTXT_LEN) | |
965 | fatal("%s: reply from monitor too large", __func__); | |
966 | reply->length = rlen; | |
967 | memcpy(reply->dat, p, rlen); | |
968 | memset(p, 0, rlen); | |
969 | xfree(p); | |
970 | } | |
971 | buffer_free(&m); | |
b77a87e5 | 972 | return (success); |
2db9d8aa | 973 | } |
974 | #endif | |
975 | ||
696f6bef | 976 | #ifdef KRB5 |
977 | int | |
978 | mm_auth_krb5(void *ctx, void *argp, char **userp, void *resp) | |
979 | { | |
980 | krb5_data *tkt, *reply; | |
981 | Buffer m; | |
982 | int success; | |
983 | ||
984 | debug3("%s entering", __func__); | |
985 | tkt = (krb5_data *) argp; | |
986 | reply = (krb5_data *) resp; | |
987 | ||
988 | buffer_init(&m); | |
989 | buffer_put_string(&m, tkt->data, tkt->length); | |
990 | ||
991 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KRB5, &m); | |
992 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KRB5, &m); | |
993 | ||
994 | success = buffer_get_int(&m); | |
995 | if (success) { | |
996 | u_int len; | |
997 | ||
998 | *userp = buffer_get_string(&m, NULL); | |
999 | reply->data = buffer_get_string(&m, &len); | |
1000 | reply->length = len; | |
1001 | } else { | |
1002 | memset(reply, 0, sizeof(*reply)); | |
1003 | *userp = NULL; | |
1004 | } | |
1005 | ||
1006 | buffer_free(&m); | |
1007 | return (success); | |
1008 | } | |
1009 | #endif |