]> andersk Git - openssh.git/blame_incremental - monitor_wrap.c
- (dtucker) [contrib/cygwin/ssh-host-config] Add SeTcbPrivilege privilege
[openssh.git] / monitor_wrap.c
... / ...
CommitLineData
1/* $OpenBSD: monitor_wrap.c,v 1.54 2006/08/12 20:46:46 miod Exp $ */
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"
29
30#include <sys/types.h>
31#include <sys/uio.h>
32
33#include <errno.h>
34#include <pwd.h>
35#include <signal.h>
36#include <stdio.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <openssl/bn.h>
41#include <openssl/dh.h>
42
43#include "xmalloc.h"
44#include "ssh.h"
45#include "dh.h"
46#include "buffer.h"
47#include "key.h"
48#include "cipher.h"
49#include "kex.h"
50#include "hostfile.h"
51#include "auth.h"
52#include "auth-options.h"
53#include "packet.h"
54#include "mac.h"
55#include "log.h"
56#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
57#undef TARGET_OS_MAC
58#include "zlib.h"
59#define TARGET_OS_MAC 1
60#else
61#include "zlib.h"
62#endif
63#include "monitor.h"
64#ifdef GSSAPI
65#include "ssh-gss.h"
66#endif
67#include "monitor_wrap.h"
68#include "atomicio.h"
69#include "monitor_fdpass.h"
70#include "misc.h"
71#include "servconf.h"
72
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;
81extern struct monitor *pmonitor;
82extern Buffer input, output;
83extern Buffer loginmsg;
84extern ServerOptions options;
85
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 */
93 return (pmonitor && pmonitor->m_pid > 0);
94}
95
96void
97mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
98{
99 u_int mlen = buffer_len(m);
100 u_char buf[5];
101
102 debug3("%s entering: type %d", __func__, type);
103
104 put_u32(buf, mlen + 1);
105 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
106 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
107 fatal("%s: write: %s", __func__, strerror(errno));
108 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
109 fatal("%s: write: %s", __func__, strerror(errno));
110}
111
112void
113mm_request_receive(int sock, Buffer *m)
114{
115 u_char buf[4];
116 u_int msg_len;
117
118 debug3("%s entering", __func__);
119
120 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
121 if (errno == EPIPE)
122 cleanup_exit(255);
123 fatal("%s: read: %s", __func__, strerror(errno));
124 }
125 msg_len = get_u32(buf);
126 if (msg_len > 256 * 1024)
127 fatal("%s: read: bad msg_len %d", __func__, msg_len);
128 buffer_clear(m);
129 buffer_append_space(m, msg_len);
130 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
131 fatal("%s: read: %s", __func__, strerror(errno));
132}
133
134void
135mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
136{
137 u_char rtype;
138
139 debug3("%s entering: type %d", __func__, type);
140
141 mm_request_receive(sock, m);
142 rtype = buffer_get_char(m);
143 if (rtype != type)
144 fatal("%s: read: rtype %d != type %d", __func__,
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
160 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
161
162 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
163 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
164
165 success = buffer_get_char(&m);
166 if (success == 0)
167 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
168
169 if ((p = BN_new()) == NULL)
170 fatal("%s: BN_new failed", __func__);
171 if ((g = BN_new()) == NULL)
172 fatal("%s: BN_new failed", __func__);
173 buffer_get_bignum2(&m, p);
174 buffer_get_bignum2(&m, g);
175
176 debug3("%s: remaining %d", __func__, buffer_len(&m));
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{
185 Kex *kex = *pmonitor->m_pkex;
186 Buffer m;
187
188 debug3("%s entering", __func__);
189
190 buffer_init(&m);
191 buffer_put_int(&m, kex->host_key_index(key));
192 buffer_put_string(&m, data, datalen);
193
194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
195
196 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
197 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
198 *sigp = buffer_get_string(&m, lenp);
199 buffer_free(&m);
200
201 return (0);
202}
203
204struct passwd *
205mm_getpwnamallow(const char *username)
206{
207 Buffer m;
208 struct passwd *pw;
209 u_int pwlen;
210
211 debug3("%s entering", __func__);
212
213 buffer_init(&m);
214 buffer_put_cstring(&m, username);
215
216 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
217
218 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
219 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
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))
227 fatal("%s: struct passwd size mismatch", __func__);
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);
231#ifdef HAVE_PW_CLASS_IN_PASSWD
232 pw->pw_class = buffer_get_string(&m, NULL);
233#endif
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
241char *
242mm_auth2_read_banner(void)
243{
244 Buffer m;
245 char *banner;
246
247 debug3("%s entering", __func__);
248
249 buffer_init(&m);
250 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
251 buffer_clear(&m);
252
253 mm_request_receive_expect(pmonitor->m_recvfd,
254 MONITOR_ANS_AUTH2_READ_BANNER, &m);
255 banner = buffer_get_string(&m, NULL);
256 buffer_free(&m);
257
258 /* treat empty banner as missing banner */
259 if (strlen(banner) == 0) {
260 xfree(banner);
261 banner = NULL;
262 }
263 return (banner);
264}
265
266/* Inform the privileged process about service and style */
267
268void
269mm_inform_authserv(char *service, char *style)
270{
271 Buffer m;
272
273 debug3("%s entering", __func__);
274
275 buffer_init(&m);
276 buffer_put_cstring(&m, service);
277 buffer_put_cstring(&m, style ? style : "");
278
279 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
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
291 debug3("%s entering", __func__);
292
293 buffer_init(&m);
294 buffer_put_cstring(&m, password);
295 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
296
297 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
298 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
299
300 authenticated = buffer_get_int(&m);
301
302 buffer_free(&m);
303
304 debug3("%s: user %sauthenticated",
305 __func__, authenticated ? "" : "not ");
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);
341 debug3("%s: Sending debug: %s", __func__, msg);
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;
353 int allowed = 0, have_forced = 0;
354
355 debug3("%s entering", __func__);
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
368 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
369
370 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
371 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
372
373 allowed = buffer_get_int(&m);
374
375 /* fake forced command */
376 auth_clear_options();
377 have_forced = buffer_get_int(&m);
378 forced_command = have_forced ? xstrdup("true") : NULL;
379
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
402 debug3("%s entering", __func__);
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
414 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
415
416 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
417 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
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
437 debug3("%s: %p(%d)", __func__, blob, blen);
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)
457 fatal("%s: bad ivlen: expected %u != %u", __func__,
458 enc->block_size, len);
459
460 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
461 fatal("%s: bad cipher name %s or pointer %p", __func__,
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)
467 fatal("%s: can not init mac %s", __func__, mac->name);
468 mac->enabled = buffer_get_int(&b);
469 mac->key = buffer_get_string(&b, &len);
470 if (len > mac->key_len)
471 fatal("%s: bad mac key length: %u > %d", __func__, len,
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)
482 error("newkeys_from_blob: remaining bytes in blob %u", len);
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;
492 Enc *enc;
493 Mac *mac;
494 Comp *comp;
495 Newkeys *newkey = newkeys[mode];
496
497 debug3("%s: converting %p", __func__, newkey);
498
499 if (newkey == NULL) {
500 error("%s: newkey == NULL", __func__);
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);
529 if (lenp != NULL)
530 *lenp = len;
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);
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
555mm_send_keystate(struct monitor *monitor)
556{
557 Buffer m;
558 u_char *blob, *p;
559 u_int bloblen, plen;
560 u_int32_t seqnr, packets;
561 u_int64_t blocks;
562
563 buffer_init(&m);
564
565 if (!compat20) {
566 u_char iv[24];
567 u_char *key;
568 u_int ivlen, keylen;
569
570 buffer_put_int(&m, packet_get_protocol_flags());
571
572 buffer_put_int(&m, packet_get_ssh1_cipher());
573
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
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 */
591 mm_send_kex(&m, *monitor->m_pkex);
592 }
593
594 debug3("%s: Sending new keys: %p %p",
595 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
596
597 /* Keys from Kex */
598 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
599 fatal("%s: conversion of newkeys failed", __func__);
600
601 buffer_put_string(&m, blob, bloblen);
602 xfree(blob);
603
604 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
605 fatal("%s: conversion of newkeys failed", __func__);
606
607 buffer_put_string(&m, blob, bloblen);
608 xfree(blob);
609
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);
614 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
615 buffer_put_int(&m, seqnr);
616 buffer_put_int64(&m, blocks);
617 buffer_put_int(&m, packets);
618
619 debug3("%s: New keys have been sent", __func__);
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 */
635 debug3("%s: Sending compression state", __func__);
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
643 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
644 debug3("%s: Finished sending state", __func__);
645
646 buffer_free(&m);
647}
648
649int
650mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
651{
652 Buffer m;
653 char *p, *msg;
654 int success = 0;
655
656 buffer_init(&m);
657 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
658
659 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
660 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
661
662 success = buffer_get_int(&m);
663 if (success == 0) {
664 debug3("%s: pty alloc failed", __func__);
665 buffer_free(&m);
666 return (0);
667 }
668 p = buffer_get_string(&m, NULL);
669 msg = buffer_get_string(&m, NULL);
670 buffer_free(&m);
671
672 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
673 xfree(p);
674
675 buffer_append(&loginmsg, msg, strlen(msg));
676 xfree(msg);
677
678 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
679 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
680
681 /* Success */
682 return (1);
683}
684
685void
686mm_session_pty_cleanup2(Session *s)
687{
688 Buffer m;
689
690 if (s->ttyfd == -1)
691 return;
692 buffer_init(&m);
693 buffer_put_cstring(&m, s->tty);
694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
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
705#ifdef USE_PAM
706void
707mm_start_pam(Authctxt *authctxt)
708{
709 Buffer m;
710
711 debug3("%s entering", __func__);
712 if (!options.use_pam)
713 fatal("UsePAM=no, but ended up in %s anyway", __func__);
714
715 buffer_init(&m);
716 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
717
718 buffer_free(&m);
719}
720
721u_int
722mm_do_pam_account(void)
723{
724 Buffer m;
725 u_int ret;
726 char *msg;
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
735 mm_request_receive_expect(pmonitor->m_recvfd,
736 MONITOR_ANS_PAM_ACCOUNT, &m);
737 ret = buffer_get_int(&m);
738 msg = buffer_get_string(&m, NULL);
739 buffer_append(&loginmsg, msg, strlen(msg));
740 xfree(msg);
741
742 buffer_free(&m);
743
744 debug3("%s returning %d", __func__, ret);
745
746 return (ret);
747}
748
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;
776 u_int i;
777 int ret;
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);
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));
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;
806 u_int i;
807 int ret;
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}
835#endif /* USE_PAM */
836
837/* Request process termination */
838
839void
840mm_terminate(void)
841{
842 Buffer m;
843
844 buffer_init(&m);
845 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
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);
857 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
858
859 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
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{
873 *name = xstrdup("");
874 *infotxt = xstrdup("");
875 *numprompts = 1;
876 *prompts = xcalloc(*numprompts, sizeof(char *));
877 *echo_on = xcalloc(*numprompts, sizeof(u_int));
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;
886 u_int success;
887 char *challenge;
888
889 debug3("%s: entering", __func__);
890
891 buffer_init(&m);
892 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
893
894 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
895 &m);
896 success = buffer_get_int(&m);
897 if (success == 0) {
898 debug3("%s: no challenge", __func__);
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
910 debug3("%s: received challenge: %s", __func__, challenge);
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
921 debug3("%s: entering", __func__);
922 if (numresponses != 1)
923 return (-1);
924
925 buffer_init(&m);
926 buffer_put_cstring(&m, responses[0]);
927 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
928
929 mm_request_receive_expect(pmonitor->m_recvfd,
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
938#ifdef SKEY
939int
940mm_skey_query(void *ctx, char **name, char **infotxt,
941 u_int *numprompts, char ***prompts, u_int **echo_on)
942{
943 Buffer m;
944 u_int success;
945 char *challenge;
946
947 debug3("%s: entering", __func__);
948
949 buffer_init(&m);
950 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
951
952 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
953 &m);
954 success = buffer_get_int(&m);
955 if (success == 0) {
956 debug3("%s: no challenge", __func__);
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
965 debug3("%s: received challenge: %s", __func__, challenge);
966
967 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
968
969 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
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
981 debug3("%s: entering", __func__);
982 if (numresponses != 1)
983 return (-1);
984
985 buffer_init(&m);
986 buffer_put_cstring(&m, responses[0]);
987 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
988
989 mm_request_receive_expect(pmonitor->m_recvfd,
990 MONITOR_ANS_SKEYRESPOND, &m);
991
992 authok = buffer_get_int(&m);
993 buffer_free(&m);
994
995 return ((authok == 0) ? -1 : 0);
996}
997#endif /* SKEY */
998
999void
1000mm_ssh1_session_id(u_char session_id[16])
1001{
1002 Buffer m;
1003 int i;
1004
1005 debug3("%s entering", __func__);
1006
1007 buffer_init(&m);
1008 for (i = 0; i < 16; i++)
1009 buffer_put_char(&m, session_id[i]);
1010
1011 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
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;
1022 int allowed = 0, have_forced = 0;
1023
1024 debug3("%s entering", __func__);
1025
1026 buffer_init(&m);
1027 buffer_put_bignum2(&m, client_n);
1028
1029 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1030 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1031
1032 allowed = buffer_get_int(&m);
1033
1034 /* fake forced command */
1035 auth_clear_options();
1036 have_forced = buffer_get_int(&m);
1037 forced_command = have_forced ? xstrdup("true") : NULL;
1038
1039 if (allowed && rkey != NULL) {
1040 blob = buffer_get_string(&m, &blen);
1041 if ((key = key_from_blob(blob, blen)) == NULL)
1042 fatal("%s: key_from_blob failed", __func__);
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
1060 debug3("%s entering", __func__);
1061
1062 if ((challenge = BN_new()) == NULL)
1063 fatal("%s: BN_new failed", __func__);
1064
1065 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1066 if (key_to_blob(key, &blob, &blen) == 0)
1067 fatal("%s: key_to_blob failed", __func__);
1068 key->type = KEY_RSA1;
1069
1070 buffer_init(&m);
1071 buffer_put_string(&m, blob, blen);
1072 xfree(blob);
1073
1074 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1075 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
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
1091 debug3("%s entering", __func__);
1092
1093 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1094 if (key_to_blob(key, &blob, &blen) == 0)
1095 fatal("%s: key_to_blob failed", __func__);
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
1103 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1104 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1105
1106 success = buffer_get_int(&m);
1107 buffer_free(&m);
1108
1109 return (success);
1110}
1111
1112#ifdef SSH_AUDIT_EVENTS
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}
1140#endif /* SSH_AUDIT_EVENTS */
1141
1142#ifdef GSSAPI
1143OM_uint32
1144mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
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);
1153 buffer_put_string(&m, goid->elements, goid->length);
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;
1170 u_int len;
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);
1179 out->value = buffer_get_string(&m, &len);
1180 out->length = len;
1181 if (flags)
1182 *flags = buffer_get_int(&m);
1183
1184 buffer_free(&m);
1185
1186 return (major);
1187}
1188
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
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.047044 seconds and 5 git commands to generate.