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