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