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