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