]> andersk Git - openssh.git/blame - monitor_wrap.c
- (tim) [buildpkg.sh.in openssh.xml.in] Clean up Solaris 10 smf(5) bits.
[openssh.git] / monitor_wrap.c
CommitLineData
03bcbf84 1/* $OpenBSD: monitor_wrap.c,v 1.55 2007/02/19 10:45:58 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) {
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);
479 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
1588c277 480 fatal("%s: can not init 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
b5a28cbc 691 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
692 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
1853d1ef 693
694 /* Success */
695 return (1);
696}
697
698void
2362db19 699mm_session_pty_cleanup2(Session *s)
1853d1ef 700{
1853d1ef 701 Buffer m;
702
703 if (s->ttyfd == -1)
704 return;
705 buffer_init(&m);
706 buffer_put_cstring(&m, s->tty);
b5a28cbc 707 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1853d1ef 708 buffer_free(&m);
709
710 /* closed dup'ed master */
711 if (close(s->ptymaster) < 0)
712 error("close(s->ptymaster): %s", strerror(errno));
713
714 /* unlink pty from session */
715 s->ttyfd = -1;
716}
717
ad200abb 718#ifdef USE_PAM
719void
529d73ab 720mm_start_pam(Authctxt *authctxt)
ad200abb 721{
722 Buffer m;
723
1588c277 724 debug3("%s entering", __func__);
7fceb20d 725 if (!options.use_pam)
726 fatal("UsePAM=no, but ended up in %s anyway", __func__);
ad200abb 727
728 buffer_init(&m);
b5a28cbc 729 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
ad200abb 730
731 buffer_free(&m);
732}
05114c74 733
5b9e2464 734u_int
735mm_do_pam_account(void)
736{
737 Buffer m;
738 u_int ret;
ba6dd90e 739 char *msg;
5b9e2464 740
741 debug3("%s entering", __func__);
742 if (!options.use_pam)
743 fatal("UsePAM=no, but ended up in %s anyway", __func__);
744
745 buffer_init(&m);
746 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
747
aff51935 748 mm_request_receive_expect(pmonitor->m_recvfd,
5b9e2464 749 MONITOR_ANS_PAM_ACCOUNT, &m);
750 ret = buffer_get_int(&m);
ba6dd90e 751 msg = buffer_get_string(&m, NULL);
752 buffer_append(&loginmsg, msg, strlen(msg));
753 xfree(msg);
5b9e2464 754
755 buffer_free(&m);
b6453d99 756
5b9e2464 757 debug3("%s returning %d", __func__, ret);
758
759 return (ret);
760}
761
05114c74 762void *
763mm_sshpam_init_ctx(Authctxt *authctxt)
764{
765 Buffer m;
766 int success;
767
768 debug3("%s", __func__);
769 buffer_init(&m);
770 buffer_put_cstring(&m, authctxt->user);
771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
772 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
773 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
774 success = buffer_get_int(&m);
775 if (success == 0) {
776 debug3("%s: pam_init_ctx failed", __func__);
777 buffer_free(&m);
778 return (NULL);
779 }
780 buffer_free(&m);
781 return (authctxt);
782}
783
784int
785mm_sshpam_query(void *ctx, char **name, char **info,
786 u_int *num, char ***prompts, u_int **echo_on)
787{
788 Buffer m;
a1a073cc 789 u_int i;
790 int ret;
05114c74 791
792 debug3("%s", __func__);
793 buffer_init(&m);
794 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
795 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
796 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
797 ret = buffer_get_int(&m);
798 debug3("%s: pam_query returned %d", __func__, ret);
799 *name = buffer_get_string(&m, NULL);
800 *info = buffer_get_string(&m, NULL);
801 *num = buffer_get_int(&m);
01d35895 802 if (*num > PAM_MAX_NUM_MSG)
803 fatal("%s: recieved %u PAM messages, expected <= %u",
804 __func__, *num, PAM_MAX_NUM_MSG);
805 *prompts = xcalloc((*num + 1), sizeof(char *));
806 *echo_on = xcalloc((*num + 1), sizeof(u_int));
05114c74 807 for (i = 0; i < *num; ++i) {
808 (*prompts)[i] = buffer_get_string(&m, NULL);
809 (*echo_on)[i] = buffer_get_int(&m);
810 }
811 buffer_free(&m);
812 return (ret);
813}
814
815int
816mm_sshpam_respond(void *ctx, u_int num, char **resp)
817{
818 Buffer m;
a1a073cc 819 u_int i;
820 int ret;
05114c74 821
822 debug3("%s", __func__);
823 buffer_init(&m);
824 buffer_put_int(&m, num);
825 for (i = 0; i < num; ++i)
826 buffer_put_cstring(&m, resp[i]);
827 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
828 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
829 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
830 ret = buffer_get_int(&m);
831 debug3("%s: pam_respond returned %d", __func__, ret);
832 buffer_free(&m);
833 return (ret);
834}
835
836void
837mm_sshpam_free_ctx(void *ctxtp)
838{
839 Buffer m;
840
841 debug3("%s", __func__);
842 buffer_init(&m);
843 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
844 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
845 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
846 buffer_free(&m);
847}
ad200abb 848#endif /* USE_PAM */
849
1853d1ef 850/* Request process termination */
851
852void
853mm_terminate(void)
854{
855 Buffer m;
856
857 buffer_init(&m);
b5a28cbc 858 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1853d1ef 859 buffer_free(&m);
860}
861
862int
863mm_ssh1_session_key(BIGNUM *num)
864{
865 int rsafail;
866 Buffer m;
867
868 buffer_init(&m);
869 buffer_put_bignum2(&m, num);
b5a28cbc 870 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1853d1ef 871
b5a28cbc 872 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1853d1ef 873
874 rsafail = buffer_get_int(&m);
875 buffer_get_bignum2(&m, num);
876
877 buffer_free(&m);
878
879 return (rsafail);
880}
881
882static void
883mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
884 char ***prompts, u_int **echo_on)
885{
7203d6bb 886 *name = xstrdup("");
887 *infotxt = xstrdup("");
1853d1ef 888 *numprompts = 1;
52e3daed 889 *prompts = xcalloc(*numprompts, sizeof(char *));
890 *echo_on = xcalloc(*numprompts, sizeof(u_int));
1853d1ef 891 (*echo_on)[0] = 0;
892}
893
894int
895mm_bsdauth_query(void *ctx, char **name, char **infotxt,
896 u_int *numprompts, char ***prompts, u_int **echo_on)
897{
898 Buffer m;
6d6c25e3 899 u_int success;
1853d1ef 900 char *challenge;
901
1588c277 902 debug3("%s: entering", __func__);
1853d1ef 903
904 buffer_init(&m);
b5a28cbc 905 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1853d1ef 906
b5a28cbc 907 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1853d1ef 908 &m);
6d6c25e3 909 success = buffer_get_int(&m);
910 if (success == 0) {
1588c277 911 debug3("%s: no challenge", __func__);
1853d1ef 912 buffer_free(&m);
913 return (-1);
914 }
915
916 /* Get the challenge, and format the response */
917 challenge = buffer_get_string(&m, NULL);
918 buffer_free(&m);
919
920 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
921 (*prompts)[0] = challenge;
922
1588c277 923 debug3("%s: received challenge: %s", __func__, challenge);
1853d1ef 924
925 return (0);
926}
927
928int
929mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
930{
931 Buffer m;
932 int authok;
933
1588c277 934 debug3("%s: entering", __func__);
1853d1ef 935 if (numresponses != 1)
936 return (-1);
937
938 buffer_init(&m);
939 buffer_put_cstring(&m, responses[0]);
b5a28cbc 940 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1853d1ef 941
b5a28cbc 942 mm_request_receive_expect(pmonitor->m_recvfd,
1853d1ef 943 MONITOR_ANS_BSDAUTHRESPOND, &m);
944
945 authok = buffer_get_int(&m);
946 buffer_free(&m);
947
948 return ((authok == 0) ? -1 : 0);
949}
950
77751377 951#ifdef SKEY
1853d1ef 952int
953mm_skey_query(void *ctx, char **name, char **infotxt,
954 u_int *numprompts, char ***prompts, u_int **echo_on)
955{
956 Buffer m;
6d6c25e3 957 u_int success;
cecc422f 958 char *challenge;
1853d1ef 959
1588c277 960 debug3("%s: entering", __func__);
1853d1ef 961
962 buffer_init(&m);
b5a28cbc 963 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1853d1ef 964
b5a28cbc 965 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1853d1ef 966 &m);
6d6c25e3 967 success = buffer_get_int(&m);
968 if (success == 0) {
1588c277 969 debug3("%s: no challenge", __func__);
1853d1ef 970 buffer_free(&m);
971 return (-1);
972 }
973
974 /* Get the challenge, and format the response */
975 challenge = buffer_get_string(&m, NULL);
976 buffer_free(&m);
977
1588c277 978 debug3("%s: received challenge: %s", __func__, challenge);
1853d1ef 979
980 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
981
52e3daed 982 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1853d1ef 983 xfree(challenge);
984
985 return (0);
986}
987
988int
989mm_skey_respond(void *ctx, u_int numresponses, char **responses)
990{
991 Buffer m;
992 int authok;
993
1588c277 994 debug3("%s: entering", __func__);
1853d1ef 995 if (numresponses != 1)
996 return (-1);
997
998 buffer_init(&m);
999 buffer_put_cstring(&m, responses[0]);
b5a28cbc 1000 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1853d1ef 1001
b5a28cbc 1002 mm_request_receive_expect(pmonitor->m_recvfd,
1853d1ef 1003 MONITOR_ANS_SKEYRESPOND, &m);
1004
1005 authok = buffer_get_int(&m);
1006 buffer_free(&m);
1007
1008 return ((authok == 0) ? -1 : 0);
1009}
77751377 1010#endif /* SKEY */
1853d1ef 1011
1012void
1013mm_ssh1_session_id(u_char session_id[16])
1014{
1015 Buffer m;
1016 int i;
1017
1588c277 1018 debug3("%s entering", __func__);
1853d1ef 1019
1020 buffer_init(&m);
1021 for (i = 0; i < 16; i++)
1022 buffer_put_char(&m, session_id[i]);
1023
b5a28cbc 1024 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1853d1ef 1025 buffer_free(&m);
1026}
1027
1028int
1029mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1030{
1031 Buffer m;
1032 Key *key;
1033 u_char *blob;
1034 u_int blen;
92e15201 1035 int allowed = 0, have_forced = 0;
1853d1ef 1036
1588c277 1037 debug3("%s entering", __func__);
1853d1ef 1038
1039 buffer_init(&m);
1040 buffer_put_bignum2(&m, client_n);
1041
b5a28cbc 1042 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1043 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1853d1ef 1044
1045 allowed = buffer_get_int(&m);
1046
92e15201 1047 /* fake forced command */
1048 auth_clear_options();
1049 have_forced = buffer_get_int(&m);
1050 forced_command = have_forced ? xstrdup("true") : NULL;
1051
1853d1ef 1052 if (allowed && rkey != NULL) {
1053 blob = buffer_get_string(&m, &blen);
1054 if ((key = key_from_blob(blob, blen)) == NULL)
1588c277 1055 fatal("%s: key_from_blob failed", __func__);
1853d1ef 1056 *rkey = key;
1057 xfree(blob);
1058 }
1059 mm_send_debug(&m);
1060 buffer_free(&m);
1061
1062 return (allowed);
1063}
1064
1065BIGNUM *
1066mm_auth_rsa_generate_challenge(Key *key)
1067{
1068 Buffer m;
1069 BIGNUM *challenge;
1070 u_char *blob;
1071 u_int blen;
1072
1588c277 1073 debug3("%s entering", __func__);
1853d1ef 1074
1075 if ((challenge = BN_new()) == NULL)
1588c277 1076 fatal("%s: BN_new failed", __func__);
1853d1ef 1077
1078 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1079 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1080 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1081 key->type = KEY_RSA1;
1082
1083 buffer_init(&m);
1084 buffer_put_string(&m, blob, blen);
1085 xfree(blob);
1086
b5a28cbc 1087 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1088 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1853d1ef 1089
1090 buffer_get_bignum2(&m, challenge);
1091 buffer_free(&m);
1092
1093 return (challenge);
1094}
1095
1096int
1097mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1098{
1099 Buffer m;
1100 u_char *blob;
1101 u_int blen;
1102 int success = 0;
1103
1588c277 1104 debug3("%s entering", __func__);
1853d1ef 1105
1106 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1107 if (key_to_blob(key, &blob, &blen) == 0)
1588c277 1108 fatal("%s: key_to_blob failed", __func__);
1853d1ef 1109 key->type = KEY_RSA1;
1110
1111 buffer_init(&m);
1112 buffer_put_string(&m, blob, blen);
1113 buffer_put_string(&m, response, 16);
1114 xfree(blob);
1115
b5a28cbc 1116 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1117 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1853d1ef 1118
1119 success = buffer_get_int(&m);
1120 buffer_free(&m);
1121
1122 return (success);
1123}
696f6bef 1124
6039eeef 1125#ifdef SSH_AUDIT_EVENTS
c00e4d75 1126void
1127mm_audit_event(ssh_audit_event_t event)
1128{
1129 Buffer m;
1130
1131 debug3("%s entering", __func__);
1132
1133 buffer_init(&m);
1134 buffer_put_int(&m, event);
1135
1136 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1137 buffer_free(&m);
1138}
1139
1140void
1141mm_audit_run_command(const char *command)
1142{
1143 Buffer m;
1144
1145 debug3("%s entering command %s", __func__, command);
1146
1147 buffer_init(&m);
1148 buffer_put_cstring(&m, command);
1149
1150 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1151 buffer_free(&m);
1152}
6039eeef 1153#endif /* SSH_AUDIT_EVENTS */
c00e4d75 1154
7364bd04 1155#ifdef GSSAPI
1156OM_uint32
ca75d7de 1157mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
7364bd04 1158{
1159 Buffer m;
1160 OM_uint32 major;
1161
1162 /* Client doesn't get to see the context */
1163 *ctx = NULL;
1164
1165 buffer_init(&m);
ca75d7de 1166 buffer_put_string(&m, goid->elements, goid->length);
7364bd04 1167
1168 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1169 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1170
1171 major = buffer_get_int(&m);
1172
1173 buffer_free(&m);
1174 return (major);
1175}
1176
1177OM_uint32
1178mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1179 gss_buffer_desc *out, OM_uint32 *flags)
1180{
1181 Buffer m;
1182 OM_uint32 major;
f99e1ca4 1183 u_int len;
7364bd04 1184
1185 buffer_init(&m);
1186 buffer_put_string(&m, in->value, in->length);
1187
1188 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1189 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1190
1191 major = buffer_get_int(&m);
f99e1ca4 1192 out->value = buffer_get_string(&m, &len);
1193 out->length = len;
7364bd04 1194 if (flags)
1195 *flags = buffer_get_int(&m);
1196
1197 buffer_free(&m);
1198
1199 return (major);
1200}
1201
0789992b 1202OM_uint32
1203mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1204{
1205 Buffer m;
1206 OM_uint32 major;
1207
1208 buffer_init(&m);
1209 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1210 buffer_put_string(&m, gssmic->value, gssmic->length);
1211
1212 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1213 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1214 &m);
1215
1216 major = buffer_get_int(&m);
1217 buffer_free(&m);
1218 return(major);
1219}
1220
7364bd04 1221int
1222mm_ssh_gssapi_userok(char *user)
1223{
1224 Buffer m;
1225 int authenticated = 0;
1226
1227 buffer_init(&m);
1228
1229 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1230 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1231 &m);
1232
1233 authenticated = buffer_get_int(&m);
1234
1235 buffer_free(&m);
1236 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1237 return (authenticated);
1238}
1239#endif /* GSSAPI */
This page took 0.368743 seconds and 5 git commands to generate.