]> andersk Git - openssh.git/blame - monitor_wrap.c
- (dtucker) [auth-pam.c groupaccess.c monitor.c monitor_wrap.c scard-opensc.c
[openssh.git] / monitor_wrap.c
CommitLineData
51e7a012 1/* $OpenBSD: monitor_wrap.c,v 1.45 2006/03/30 09:58:15 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"
51e7a012 55#include "misc.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
51e7a012 94 put_u32(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 }
51e7a012 115 msg_len = get_u32(buf);
1853d1ef 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);
01d35895 779 if (*num > PAM_MAX_NUM_MSG)
780 fatal("%s: recieved %u PAM messages, expected <= %u",
781 __func__, *num, PAM_MAX_NUM_MSG);
782 *prompts = xcalloc((*num + 1), sizeof(char *));
783 *echo_on = xcalloc((*num + 1), sizeof(u_int));
05114c74 784 for (i = 0; i < *num; ++i) {
785 (*prompts)[i] = buffer_get_string(&m, NULL);
786 (*echo_on)[i] = buffer_get_int(&m);
787 }
788 buffer_free(&m);
789 return (ret);
790}
791
792int
793mm_sshpam_respond(void *ctx, u_int num, char **resp)
794{
795 Buffer m;
a1a073cc 796 u_int i;
797 int ret;
05114c74 798
799 debug3("%s", __func__);
800 buffer_init(&m);
801 buffer_put_int(&m, num);
802 for (i = 0; i < num; ++i)
803 buffer_put_cstring(&m, resp[i]);
804 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
805 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
806 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
807 ret = buffer_get_int(&m);
808 debug3("%s: pam_respond returned %d", __func__, ret);
809 buffer_free(&m);
810 return (ret);
811}
812
813void
814mm_sshpam_free_ctx(void *ctxtp)
815{
816 Buffer m;
817
818 debug3("%s", __func__);
819 buffer_init(&m);
820 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
821 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
822 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
823 buffer_free(&m);
824}
ad200abb 825#endif /* USE_PAM */
826
1853d1ef 827/* Request process termination */
828
829void
830mm_terminate(void)
831{
832 Buffer m;
833
834 buffer_init(&m);
b5a28cbc 835 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1853d1ef 836 buffer_free(&m);
837}
838
839int
840mm_ssh1_session_key(BIGNUM *num)
841{
842 int rsafail;
843 Buffer m;
844
845 buffer_init(&m);
846 buffer_put_bignum2(&m, num);
b5a28cbc 847 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1853d1ef 848
b5a28cbc 849 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1853d1ef 850
851 rsafail = buffer_get_int(&m);
852 buffer_get_bignum2(&m, num);
853
854 buffer_free(&m);
855
856 return (rsafail);
857}
858
859static void
860mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
861 char ***prompts, u_int **echo_on)
862{
7203d6bb 863 *name = xstrdup("");
864 *infotxt = xstrdup("");
1853d1ef 865 *numprompts = 1;
52e3daed 866 *prompts = xcalloc(*numprompts, sizeof(char *));
867 *echo_on = xcalloc(*numprompts, sizeof(u_int));
1853d1ef 868 (*echo_on)[0] = 0;
869}
870
871int
872mm_bsdauth_query(void *ctx, char **name, char **infotxt,
873 u_int *numprompts, char ***prompts, u_int **echo_on)
874{
875 Buffer m;
6d6c25e3 876 u_int success;
1853d1ef 877 char *challenge;
878
1588c277 879 debug3("%s: entering", __func__);
1853d1ef 880
881 buffer_init(&m);
b5a28cbc 882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1853d1ef 883
b5a28cbc 884 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1853d1ef 885 &m);
6d6c25e3 886 success = buffer_get_int(&m);
887 if (success == 0) {
1588c277 888 debug3("%s: no challenge", __func__);
1853d1ef 889 buffer_free(&m);
890 return (-1);
891 }
892
893 /* Get the challenge, and format the response */
894 challenge = buffer_get_string(&m, NULL);
895 buffer_free(&m);
896
897 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
898 (*prompts)[0] = challenge;
899
1588c277 900 debug3("%s: received challenge: %s", __func__, challenge);
1853d1ef 901
902 return (0);
903}
904
905int
906mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
907{
908 Buffer m;
909 int authok;
910
1588c277 911 debug3("%s: entering", __func__);
1853d1ef 912 if (numresponses != 1)
913 return (-1);
914
915 buffer_init(&m);
916 buffer_put_cstring(&m, responses[0]);
b5a28cbc 917 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1853d1ef 918
b5a28cbc 919 mm_request_receive_expect(pmonitor->m_recvfd,
1853d1ef 920 MONITOR_ANS_BSDAUTHRESPOND, &m);
921
922 authok = buffer_get_int(&m);
923 buffer_free(&m);
924
925 return ((authok == 0) ? -1 : 0);
926}
927
77751377 928#ifdef SKEY
1853d1ef 929int
930mm_skey_query(void *ctx, char **name, char **infotxt,
931 u_int *numprompts, char ***prompts, u_int **echo_on)
932{
933 Buffer m;
6d6c25e3 934 int len;
935 u_int success;
1853d1ef 936 char *p, *challenge;
937
1588c277 938 debug3("%s: entering", __func__);
1853d1ef 939
940 buffer_init(&m);
b5a28cbc 941 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1853d1ef 942
b5a28cbc 943 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1853d1ef 944 &m);
6d6c25e3 945 success = buffer_get_int(&m);
946 if (success == 0) {
1588c277 947 debug3("%s: no challenge", __func__);
1853d1ef 948 buffer_free(&m);
949 return (-1);
950 }
951
952 /* Get the challenge, and format the response */
953 challenge = buffer_get_string(&m, NULL);
954 buffer_free(&m);
955
1588c277 956 debug3("%s: received challenge: %s", __func__, challenge);
1853d1ef 957
958 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
959
52e3daed 960 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1853d1ef 961 xfree(challenge);
962
963 return (0);
964}
965
966int
967mm_skey_respond(void *ctx, u_int numresponses, char **responses)
968{
969 Buffer m;
970 int authok;
971
1588c277 972 debug3("%s: entering", __func__);
1853d1ef 973 if (numresponses != 1)
974 return (-1);
975
976 buffer_init(&m);
977 buffer_put_cstring(&m, responses[0]);
b5a28cbc 978 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1853d1ef 979
b5a28cbc 980 mm_request_receive_expect(pmonitor->m_recvfd,
1853d1ef 981 MONITOR_ANS_SKEYRESPOND, &m);
982
983 authok = buffer_get_int(&m);
984 buffer_free(&m);
985
986 return ((authok == 0) ? -1 : 0);
987}
77751377 988#endif /* SKEY */
1853d1ef 989
990void
991mm_ssh1_session_id(u_char session_id[16])
992{
993 Buffer m;
994 int i;
995
1588c277 996 debug3("%s entering", __func__);
1853d1ef 997
998 buffer_init(&m);
999 for (i = 0; i < 16; i++)
1000 buffer_put_char(&m, session_id[i]);
1001
b5a28cbc 1002 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1853d1ef 1003 buffer_free(&m);
1004}
1005
1006int
1007mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1008{
1009 Buffer m;
1010 Key *key;
1011 u_char *blob;
1012 u_int blen;
92e15201 1013 int allowed = 0, have_forced = 0;
1853d1ef 1014
1588c277 1015 debug3("%s entering", __func__);
1853d1ef 1016
1017 buffer_init(&m);
1018 buffer_put_bignum2(&m, client_n);
1019
b5a28cbc 1020 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1021 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1853d1ef 1022
1023 allowed = buffer_get_int(&m);
1024
92e15201 1025 /* fake forced command */
1026 auth_clear_options();
1027 have_forced = buffer_get_int(&m);
1028 forced_command = have_forced ? xstrdup("true") : NULL;
1029
1853d1ef 1030 if (allowed && rkey != NULL) {
1031 blob = buffer_get_string(&m, &blen);
1032 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1033 fatal("%s: key_from_blob failed", __func__);
1853d1ef 1034 *rkey = key;
1035 xfree(blob);
1036 }
1037 mm_send_debug(&m);
1038 buffer_free(&m);
1039
1040 return (allowed);
1041}
1042
1043BIGNUM *
1044mm_auth_rsa_generate_challenge(Key *key)
1045{
1046 Buffer m;
1047 BIGNUM *challenge;
1048 u_char *blob;
1049 u_int blen;
1050
1588c277 1051 debug3("%s entering", __func__);
1853d1ef 1052
1053 if ((challenge = BN_new()) == NULL)
1588c277 1054 fatal("%s: BN_new failed", __func__);
1853d1ef 1055
1056 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1057 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1058 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1059 key->type = KEY_RSA1;
1060
1061 buffer_init(&m);
1062 buffer_put_string(&m, blob, blen);
1063 xfree(blob);
1064
b5a28cbc 1065 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1066 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1853d1ef 1067
1068 buffer_get_bignum2(&m, challenge);
1069 buffer_free(&m);
1070
1071 return (challenge);
1072}
1073
1074int
1075mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1076{
1077 Buffer m;
1078 u_char *blob;
1079 u_int blen;
1080 int success = 0;
1081
1588c277 1082 debug3("%s entering", __func__);
1853d1ef 1083
1084 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1085 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1086 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1087 key->type = KEY_RSA1;
1088
1089 buffer_init(&m);
1090 buffer_put_string(&m, blob, blen);
1091 buffer_put_string(&m, response, 16);
1092 xfree(blob);
1093
b5a28cbc 1094 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1095 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1853d1ef 1096
1097 success = buffer_get_int(&m);
1098 buffer_free(&m);
1099
1100 return (success);
1101}
696f6bef 1102
6039eeef 1103#ifdef SSH_AUDIT_EVENTS
c00e4d75 1104void
1105mm_audit_event(ssh_audit_event_t event)
1106{
1107 Buffer m;
1108
1109 debug3("%s entering", __func__);
1110
1111 buffer_init(&m);
1112 buffer_put_int(&m, event);
1113
1114 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1115 buffer_free(&m);
1116}
1117
1118void
1119mm_audit_run_command(const char *command)
1120{
1121 Buffer m;
1122
1123 debug3("%s entering command %s", __func__, command);
1124
1125 buffer_init(&m);
1126 buffer_put_cstring(&m, command);
1127
1128 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1129 buffer_free(&m);
1130}
6039eeef 1131#endif /* SSH_AUDIT_EVENTS */
c00e4d75 1132
7364bd04 1133#ifdef GSSAPI
1134OM_uint32
ca75d7de 1135mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
7364bd04 1136{
1137 Buffer m;
1138 OM_uint32 major;
1139
1140 /* Client doesn't get to see the context */
1141 *ctx = NULL;
1142
1143 buffer_init(&m);
ca75d7de 1144 buffer_put_string(&m, goid->elements, goid->length);
7364bd04 1145
1146 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1147 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1148
1149 major = buffer_get_int(&m);
1150
1151 buffer_free(&m);
1152 return (major);
1153}
1154
1155OM_uint32
1156mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1157 gss_buffer_desc *out, OM_uint32 *flags)
1158{
1159 Buffer m;
1160 OM_uint32 major;
f99e1ca4 1161 u_int len;
7364bd04 1162
1163 buffer_init(&m);
1164 buffer_put_string(&m, in->value, in->length);
1165
1166 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1167 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1168
1169 major = buffer_get_int(&m);
f99e1ca4 1170 out->value = buffer_get_string(&m, &len);
1171 out->length = len;
7364bd04 1172 if (flags)
1173 *flags = buffer_get_int(&m);
1174
1175 buffer_free(&m);
1176
1177 return (major);
1178}
1179
0789992b 1180OM_uint32
1181mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1182{
1183 Buffer m;
1184 OM_uint32 major;
1185
1186 buffer_init(&m);
1187 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1188 buffer_put_string(&m, gssmic->value, gssmic->length);
1189
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1192 &m);
1193
1194 major = buffer_get_int(&m);
1195 buffer_free(&m);
1196 return(major);
1197}
1198
7364bd04 1199int
1200mm_ssh_gssapi_userok(char *user)
1201{
1202 Buffer m;
1203 int authenticated = 0;
1204
1205 buffer_init(&m);
1206
1207 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1208 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1209 &m);
1210
1211 authenticated = buffer_get_int(&m);
1212
1213 buffer_free(&m);
1214 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1215 return (authenticated);
1216}
1217#endif /* GSSAPI */
This page took 0.344631 seconds and 5 git commands to generate.