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