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