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