sysconf.3: Fix typo.
[dragonfly.git] / crypto / openssh / monitor_wrap.c
blob45dc16951e5b9dab321a39a2bff5bdc28a8dc989
1 /* $OpenBSD: monitor_wrap.c,v 1.80 2014/04/29 18:01:49 markus Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
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.
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.
28 #include "includes.h"
30 #include <sys/types.h>
31 #include <sys/uio.h>
33 #include <errno.h>
34 #include <pwd.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
41 #ifdef WITH_OPENSSL
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 #endif
47 #include "openbsd-compat/sys-queue.h"
48 #include "xmalloc.h"
49 #include "ssh.h"
50 #ifdef WITH_OPENSSL
51 #include "dh.h"
52 #endif
53 #include "buffer.h"
54 #include "key.h"
55 #include "cipher.h"
56 #include "kex.h"
57 #include "hostfile.h"
58 #include "auth.h"
59 #include "auth-options.h"
60 #include "packet.h"
61 #include "mac.h"
62 #include "log.h"
63 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
64 #undef TARGET_OS_MAC
65 #include "zlib.h"
66 #define TARGET_OS_MAC 1
67 #else
68 #include "zlib.h"
69 #endif
70 #include "monitor.h"
71 #ifdef GSSAPI
72 #include "ssh-gss.h"
73 #endif
74 #include "monitor_wrap.h"
75 #include "atomicio.h"
76 #include "monitor_fdpass.h"
77 #include "misc.h"
78 #include "uuencode.h"
80 #include "channels.h"
81 #include "session.h"
82 #include "servconf.h"
83 #include "roaming.h"
85 /* Imports */
86 extern int compat20;
87 extern z_stream incoming_stream;
88 extern z_stream outgoing_stream;
89 extern struct monitor *pmonitor;
90 extern Buffer loginmsg;
91 extern ServerOptions options;
93 void
94 mm_log_handler(LogLevel level, const char *msg, void *ctx)
96 Buffer log_msg;
97 struct monitor *mon = (struct monitor *)ctx;
99 if (mon->m_log_sendfd == -1)
100 fatal("%s: no log channel", __func__);
102 buffer_init(&log_msg);
104 * Placeholder for packet length. Will be filled in with the actual
105 * packet length once the packet has been constucted. This saves
106 * fragile math.
108 buffer_put_int(&log_msg, 0);
110 buffer_put_int(&log_msg, level);
111 buffer_put_cstring(&log_msg, msg);
112 put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
113 if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
114 buffer_len(&log_msg)) != buffer_len(&log_msg))
115 fatal("%s: write: %s", __func__, strerror(errno));
116 buffer_free(&log_msg);
120 mm_is_monitor(void)
123 * m_pid is only set in the privileged part, and
124 * points to the unprivileged child.
126 return (pmonitor && pmonitor->m_pid > 0);
129 void
130 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
132 u_int mlen = buffer_len(m);
133 u_char buf[5];
135 debug3("%s entering: type %d", __func__, type);
137 put_u32(buf, mlen + 1);
138 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
139 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
140 fatal("%s: write: %s", __func__, strerror(errno));
141 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
142 fatal("%s: write: %s", __func__, strerror(errno));
145 void
146 mm_request_receive(int sock, Buffer *m)
148 u_char buf[4];
149 u_int msg_len;
151 debug3("%s entering", __func__);
153 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
154 if (errno == EPIPE)
155 cleanup_exit(255);
156 fatal("%s: read: %s", __func__, strerror(errno));
158 msg_len = get_u32(buf);
159 if (msg_len > 256 * 1024)
160 fatal("%s: read: bad msg_len %d", __func__, msg_len);
161 buffer_clear(m);
162 buffer_append_space(m, msg_len);
163 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
164 fatal("%s: read: %s", __func__, strerror(errno));
167 void
168 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
170 u_char rtype;
172 debug3("%s entering: type %d", __func__, type);
174 mm_request_receive(sock, m);
175 rtype = buffer_get_char(m);
176 if (rtype != type)
177 fatal("%s: read: rtype %d != type %d", __func__,
178 rtype, type);
181 #ifdef WITH_OPENSSL
182 DH *
183 mm_choose_dh(int min, int nbits, int max)
185 BIGNUM *p, *g;
186 int success = 0;
187 Buffer m;
189 buffer_init(&m);
190 buffer_put_int(&m, min);
191 buffer_put_int(&m, nbits);
192 buffer_put_int(&m, max);
194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
196 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
197 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
199 success = buffer_get_char(&m);
200 if (success == 0)
201 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
203 if ((p = BN_new()) == NULL)
204 fatal("%s: BN_new failed", __func__);
205 if ((g = BN_new()) == NULL)
206 fatal("%s: BN_new failed", __func__);
207 buffer_get_bignum2(&m, p);
208 buffer_get_bignum2(&m, g);
210 debug3("%s: remaining %d", __func__, buffer_len(&m));
211 buffer_free(&m);
213 return (dh_new_group(g, p));
215 #endif
218 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
220 Kex *kex = *pmonitor->m_pkex;
221 Buffer m;
223 debug3("%s entering", __func__);
225 buffer_init(&m);
226 buffer_put_int(&m, kex->host_key_index(key));
227 buffer_put_string(&m, data, datalen);
229 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
231 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
232 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
233 *sigp = buffer_get_string(&m, lenp);
234 buffer_free(&m);
236 return (0);
239 struct passwd *
240 mm_getpwnamallow(const char *username)
242 Buffer m;
243 struct passwd *pw;
244 u_int len, i;
245 ServerOptions *newopts;
247 debug3("%s entering", __func__);
249 buffer_init(&m);
250 buffer_put_cstring(&m, username);
252 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
254 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
255 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
257 if (buffer_get_char(&m) == 0) {
258 pw = NULL;
259 goto out;
261 pw = buffer_get_string(&m, &len);
262 if (len != sizeof(struct passwd))
263 fatal("%s: struct passwd size mismatch", __func__);
264 pw->pw_name = buffer_get_string(&m, NULL);
265 pw->pw_passwd = buffer_get_string(&m, NULL);
266 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
267 pw->pw_gecos = buffer_get_string(&m, NULL);
268 #endif
269 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
270 pw->pw_class = buffer_get_string(&m, NULL);
271 #endif
272 pw->pw_dir = buffer_get_string(&m, NULL);
273 pw->pw_shell = buffer_get_string(&m, NULL);
275 out:
276 /* copy options block as a Match directive may have changed some */
277 newopts = buffer_get_string(&m, &len);
278 if (len != sizeof(*newopts))
279 fatal("%s: option block size mismatch", __func__);
281 #define M_CP_STROPT(x) do { \
282 if (newopts->x != NULL) \
283 newopts->x = buffer_get_string(&m, NULL); \
284 } while (0)
285 #define M_CP_STRARRAYOPT(x, nx) do { \
286 for (i = 0; i < newopts->nx; i++) \
287 newopts->x[i] = buffer_get_string(&m, NULL); \
288 } while (0)
289 /* See comment in servconf.h */
290 COPY_MATCH_STRING_OPTS();
291 #undef M_CP_STROPT
292 #undef M_CP_STRARRAYOPT
294 copy_set_server_options(&options, newopts, 1);
295 free(newopts);
297 buffer_free(&m);
299 return (pw);
302 char *
303 mm_auth2_read_banner(void)
305 Buffer m;
306 char *banner;
308 debug3("%s entering", __func__);
310 buffer_init(&m);
311 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
312 buffer_clear(&m);
314 mm_request_receive_expect(pmonitor->m_recvfd,
315 MONITOR_ANS_AUTH2_READ_BANNER, &m);
316 banner = buffer_get_string(&m, NULL);
317 buffer_free(&m);
319 /* treat empty banner as missing banner */
320 if (strlen(banner) == 0) {
321 free(banner);
322 banner = NULL;
324 return (banner);
327 /* Inform the privileged process about service and style */
329 void
330 mm_inform_authserv(char *service, char *style)
332 Buffer m;
334 debug3("%s entering", __func__);
336 buffer_init(&m);
337 buffer_put_cstring(&m, service);
338 buffer_put_cstring(&m, style ? style : "");
340 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
342 buffer_free(&m);
345 /* Do the password authentication */
347 mm_auth_password(Authctxt *authctxt, char *password)
349 Buffer m;
350 int authenticated = 0;
352 debug3("%s entering", __func__);
354 buffer_init(&m);
355 buffer_put_cstring(&m, password);
356 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
358 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
359 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
361 authenticated = buffer_get_int(&m);
363 buffer_free(&m);
365 debug3("%s: user %sauthenticated",
366 __func__, authenticated ? "" : "not ");
367 return (authenticated);
371 mm_user_key_allowed(struct passwd *pw, Key *key)
373 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
377 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
378 Key *key)
380 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
384 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
385 char *host, Key *key)
387 int ret;
389 key->type = KEY_RSA; /* XXX hack for key_to_blob */
390 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
391 key->type = KEY_RSA1;
392 return (ret);
396 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
398 Buffer m;
399 u_char *blob;
400 u_int len;
401 int allowed = 0, have_forced = 0;
403 debug3("%s entering", __func__);
405 /* Convert the key to a blob and the pass it over */
406 if (!key_to_blob(key, &blob, &len))
407 return (0);
409 buffer_init(&m);
410 buffer_put_int(&m, type);
411 buffer_put_cstring(&m, user ? user : "");
412 buffer_put_cstring(&m, host ? host : "");
413 buffer_put_string(&m, blob, len);
414 free(blob);
416 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
418 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
419 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
421 allowed = buffer_get_int(&m);
423 /* fake forced command */
424 auth_clear_options();
425 have_forced = buffer_get_int(&m);
426 forced_command = have_forced ? xstrdup("true") : NULL;
428 buffer_free(&m);
430 return (allowed);
434 * This key verify needs to send the key type along, because the
435 * privileged parent makes the decision if the key is allowed
436 * for authentication.
440 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
442 Buffer m;
443 u_char *blob;
444 u_int len;
445 int verified = 0;
447 debug3("%s entering", __func__);
449 /* Convert the key to a blob and the pass it over */
450 if (!key_to_blob(key, &blob, &len))
451 return (0);
453 buffer_init(&m);
454 buffer_put_string(&m, blob, len);
455 buffer_put_string(&m, sig, siglen);
456 buffer_put_string(&m, data, datalen);
457 free(blob);
459 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
461 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
462 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
464 verified = buffer_get_int(&m);
466 buffer_free(&m);
468 return (verified);
471 /* Export key state after authentication */
472 Newkeys *
473 mm_newkeys_from_blob(u_char *blob, int blen)
475 Buffer b;
476 u_int len;
477 Newkeys *newkey = NULL;
478 Enc *enc;
479 Mac *mac;
480 Comp *comp;
482 debug3("%s: %p(%d)", __func__, blob, blen);
483 #ifdef DEBUG_PK
484 dump_base64(stderr, blob, blen);
485 #endif
486 buffer_init(&b);
487 buffer_append(&b, blob, blen);
489 newkey = xcalloc(1, sizeof(*newkey));
490 enc = &newkey->enc;
491 mac = &newkey->mac;
492 comp = &newkey->comp;
494 /* Enc structure */
495 enc->name = buffer_get_string(&b, NULL);
496 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
497 enc->enabled = buffer_get_int(&b);
498 enc->block_size = buffer_get_int(&b);
499 enc->key = buffer_get_string(&b, &enc->key_len);
500 enc->iv = buffer_get_string(&b, &enc->iv_len);
502 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
503 fatal("%s: bad cipher name %s or pointer %p", __func__,
504 enc->name, enc->cipher);
506 /* Mac structure */
507 if (cipher_authlen(enc->cipher) == 0) {
508 mac->name = buffer_get_string(&b, NULL);
509 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
510 fatal("%s: can not setup mac %s", __func__, mac->name);
511 mac->enabled = buffer_get_int(&b);
512 mac->key = buffer_get_string(&b, &len);
513 if (len > mac->key_len)
514 fatal("%s: bad mac key length: %u > %d", __func__, len,
515 mac->key_len);
516 mac->key_len = len;
519 /* Comp structure */
520 comp->type = buffer_get_int(&b);
521 comp->enabled = buffer_get_int(&b);
522 comp->name = buffer_get_string(&b, NULL);
524 len = buffer_len(&b);
525 if (len != 0)
526 error("newkeys_from_blob: remaining bytes in blob %u", len);
527 buffer_free(&b);
528 return (newkey);
532 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
534 Buffer b;
535 int len;
536 Enc *enc;
537 Mac *mac;
538 Comp *comp;
539 Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
541 debug3("%s: converting %p", __func__, newkey);
543 if (newkey == NULL) {
544 error("%s: newkey == NULL", __func__);
545 return 0;
547 enc = &newkey->enc;
548 mac = &newkey->mac;
549 comp = &newkey->comp;
551 buffer_init(&b);
552 /* Enc structure */
553 buffer_put_cstring(&b, enc->name);
554 /* The cipher struct is constant and shared, you export pointer */
555 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
556 buffer_put_int(&b, enc->enabled);
557 buffer_put_int(&b, enc->block_size);
558 buffer_put_string(&b, enc->key, enc->key_len);
559 packet_get_keyiv(mode, enc->iv, enc->iv_len);
560 buffer_put_string(&b, enc->iv, enc->iv_len);
562 /* Mac structure */
563 if (cipher_authlen(enc->cipher) == 0) {
564 buffer_put_cstring(&b, mac->name);
565 buffer_put_int(&b, mac->enabled);
566 buffer_put_string(&b, mac->key, mac->key_len);
569 /* Comp structure */
570 buffer_put_int(&b, comp->type);
571 buffer_put_int(&b, comp->enabled);
572 buffer_put_cstring(&b, comp->name);
574 len = buffer_len(&b);
575 if (lenp != NULL)
576 *lenp = len;
577 if (blobp != NULL) {
578 *blobp = xmalloc(len);
579 memcpy(*blobp, buffer_ptr(&b), len);
581 explicit_bzero(buffer_ptr(&b), len);
582 buffer_free(&b);
583 return len;
586 static void
587 mm_send_kex(Buffer *m, Kex *kex)
589 buffer_put_string(m, kex->session_id, kex->session_id_len);
590 buffer_put_int(m, kex->we_need);
591 buffer_put_int(m, kex->hostkey_type);
592 buffer_put_int(m, kex->kex_type);
593 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
594 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
595 buffer_put_int(m, kex->flags);
596 buffer_put_cstring(m, kex->client_version_string);
597 buffer_put_cstring(m, kex->server_version_string);
600 void
601 mm_send_keystate(struct monitor *monitor)
603 Buffer m, *input, *output;
604 u_char *blob, *p;
605 u_int bloblen, plen;
606 u_int32_t seqnr, packets;
607 u_int64_t blocks, bytes;
609 buffer_init(&m);
611 if (!compat20) {
612 u_char iv[24];
613 u_char *key;
614 u_int ivlen, keylen;
616 buffer_put_int(&m, packet_get_protocol_flags());
618 buffer_put_int(&m, packet_get_ssh1_cipher());
620 debug3("%s: Sending ssh1 KEY+IV", __func__);
621 keylen = packet_get_encryption_key(NULL);
622 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
623 keylen = packet_get_encryption_key(key);
624 buffer_put_string(&m, key, keylen);
625 explicit_bzero(key, keylen);
626 free(key);
628 ivlen = packet_get_keyiv_len(MODE_OUT);
629 packet_get_keyiv(MODE_OUT, iv, ivlen);
630 buffer_put_string(&m, iv, ivlen);
631 ivlen = packet_get_keyiv_len(MODE_IN);
632 packet_get_keyiv(MODE_IN, iv, ivlen);
633 buffer_put_string(&m, iv, ivlen);
634 goto skip;
635 } else {
636 /* Kex for rekeying */
637 mm_send_kex(&m, *monitor->m_pkex);
640 debug3("%s: Sending new keys: %p %p",
641 __func__, packet_get_newkeys(MODE_OUT),
642 packet_get_newkeys(MODE_IN));
644 /* Keys from Kex */
645 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
646 fatal("%s: conversion of newkeys failed", __func__);
648 buffer_put_string(&m, blob, bloblen);
649 free(blob);
651 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
652 fatal("%s: conversion of newkeys failed", __func__);
654 buffer_put_string(&m, blob, bloblen);
655 free(blob);
657 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
658 buffer_put_int(&m, seqnr);
659 buffer_put_int64(&m, blocks);
660 buffer_put_int(&m, packets);
661 buffer_put_int64(&m, bytes);
662 packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
663 buffer_put_int(&m, seqnr);
664 buffer_put_int64(&m, blocks);
665 buffer_put_int(&m, packets);
666 buffer_put_int64(&m, bytes);
668 debug3("%s: New keys have been sent", __func__);
669 skip:
670 /* More key context */
671 plen = packet_get_keycontext(MODE_OUT, NULL);
672 p = xmalloc(plen+1);
673 packet_get_keycontext(MODE_OUT, p);
674 buffer_put_string(&m, p, plen);
675 free(p);
677 plen = packet_get_keycontext(MODE_IN, NULL);
678 p = xmalloc(plen+1);
679 packet_get_keycontext(MODE_IN, p);
680 buffer_put_string(&m, p, plen);
681 free(p);
683 /* Compression state */
684 debug3("%s: Sending compression state", __func__);
685 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
686 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
688 /* Network I/O buffers */
689 input = (Buffer *)packet_get_input();
690 output = (Buffer *)packet_get_output();
691 buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
692 buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
694 /* Roaming */
695 if (compat20) {
696 buffer_put_int64(&m, get_sent_bytes());
697 buffer_put_int64(&m, get_recv_bytes());
700 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
701 debug3("%s: Finished sending state", __func__);
703 buffer_free(&m);
707 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
709 Buffer m;
710 char *p, *msg;
711 int success = 0, tmp1 = -1, tmp2 = -1;
713 /* Kludge: ensure there are fds free to receive the pty/tty */
714 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
715 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
716 error("%s: cannot allocate fds for pty", __func__);
717 if (tmp1 > 0)
718 close(tmp1);
719 if (tmp2 > 0)
720 close(tmp2);
721 return 0;
723 close(tmp1);
724 close(tmp2);
726 buffer_init(&m);
727 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
729 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
730 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
732 success = buffer_get_int(&m);
733 if (success == 0) {
734 debug3("%s: pty alloc failed", __func__);
735 buffer_free(&m);
736 return (0);
738 p = buffer_get_string(&m, NULL);
739 msg = buffer_get_string(&m, NULL);
740 buffer_free(&m);
742 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
743 free(p);
745 buffer_append(&loginmsg, msg, strlen(msg));
746 free(msg);
748 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
749 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
750 fatal("%s: receive fds failed", __func__);
752 /* Success */
753 return (1);
756 void
757 mm_session_pty_cleanup2(Session *s)
759 Buffer m;
761 if (s->ttyfd == -1)
762 return;
763 buffer_init(&m);
764 buffer_put_cstring(&m, s->tty);
765 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
766 buffer_free(&m);
768 /* closed dup'ed master */
769 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
770 error("close(s->ptymaster/%d): %s",
771 s->ptymaster, strerror(errno));
773 /* unlink pty from session */
774 s->ttyfd = -1;
777 #ifdef USE_PAM
778 void
779 mm_start_pam(Authctxt *authctxt)
781 Buffer m;
783 debug3("%s entering", __func__);
784 if (!options.use_pam)
785 fatal("UsePAM=no, but ended up in %s anyway", __func__);
787 buffer_init(&m);
788 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
790 buffer_free(&m);
793 u_int
794 mm_do_pam_account(void)
796 Buffer m;
797 u_int ret;
798 char *msg;
800 debug3("%s entering", __func__);
801 if (!options.use_pam)
802 fatal("UsePAM=no, but ended up in %s anyway", __func__);
804 buffer_init(&m);
805 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
807 mm_request_receive_expect(pmonitor->m_recvfd,
808 MONITOR_ANS_PAM_ACCOUNT, &m);
809 ret = buffer_get_int(&m);
810 msg = buffer_get_string(&m, NULL);
811 buffer_append(&loginmsg, msg, strlen(msg));
812 free(msg);
814 buffer_free(&m);
816 debug3("%s returning %d", __func__, ret);
818 return (ret);
821 void *
822 mm_sshpam_init_ctx(Authctxt *authctxt)
824 Buffer m;
825 int success;
827 debug3("%s", __func__);
828 buffer_init(&m);
829 buffer_put_cstring(&m, authctxt->user);
830 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
831 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
832 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
833 success = buffer_get_int(&m);
834 if (success == 0) {
835 debug3("%s: pam_init_ctx failed", __func__);
836 buffer_free(&m);
837 return (NULL);
839 buffer_free(&m);
840 return (authctxt);
844 mm_sshpam_query(void *ctx, char **name, char **info,
845 u_int *num, char ***prompts, u_int **echo_on)
847 Buffer m;
848 u_int i;
849 int ret;
851 debug3("%s", __func__);
852 buffer_init(&m);
853 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
854 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
855 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
856 ret = buffer_get_int(&m);
857 debug3("%s: pam_query returned %d", __func__, ret);
858 *name = buffer_get_string(&m, NULL);
859 *info = buffer_get_string(&m, NULL);
860 *num = buffer_get_int(&m);
861 if (*num > PAM_MAX_NUM_MSG)
862 fatal("%s: recieved %u PAM messages, expected <= %u",
863 __func__, *num, PAM_MAX_NUM_MSG);
864 *prompts = xcalloc((*num + 1), sizeof(char *));
865 *echo_on = xcalloc((*num + 1), sizeof(u_int));
866 for (i = 0; i < *num; ++i) {
867 (*prompts)[i] = buffer_get_string(&m, NULL);
868 (*echo_on)[i] = buffer_get_int(&m);
870 buffer_free(&m);
871 return (ret);
875 mm_sshpam_respond(void *ctx, u_int num, char **resp)
877 Buffer m;
878 u_int i;
879 int ret;
881 debug3("%s", __func__);
882 buffer_init(&m);
883 buffer_put_int(&m, num);
884 for (i = 0; i < num; ++i)
885 buffer_put_cstring(&m, resp[i]);
886 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
887 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
888 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
889 ret = buffer_get_int(&m);
890 debug3("%s: pam_respond returned %d", __func__, ret);
891 buffer_free(&m);
892 return (ret);
895 void
896 mm_sshpam_free_ctx(void *ctxtp)
898 Buffer m;
900 debug3("%s", __func__);
901 buffer_init(&m);
902 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
903 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
904 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
905 buffer_free(&m);
907 #endif /* USE_PAM */
909 /* Request process termination */
911 void
912 mm_terminate(void)
914 Buffer m;
916 buffer_init(&m);
917 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
918 buffer_free(&m);
921 #ifdef WITH_SSH1
923 mm_ssh1_session_key(BIGNUM *num)
925 int rsafail;
926 Buffer m;
928 buffer_init(&m);
929 buffer_put_bignum2(&m, num);
930 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
932 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
934 rsafail = buffer_get_int(&m);
935 buffer_get_bignum2(&m, num);
937 buffer_free(&m);
939 return (rsafail);
941 #endif
943 static void
944 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
945 char ***prompts, u_int **echo_on)
947 *name = xstrdup("");
948 *infotxt = xstrdup("");
949 *numprompts = 1;
950 *prompts = xcalloc(*numprompts, sizeof(char *));
951 *echo_on = xcalloc(*numprompts, sizeof(u_int));
952 (*echo_on)[0] = 0;
956 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
957 u_int *numprompts, char ***prompts, u_int **echo_on)
959 Buffer m;
960 u_int success;
961 char *challenge;
963 debug3("%s: entering", __func__);
965 buffer_init(&m);
966 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
968 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
969 &m);
970 success = buffer_get_int(&m);
971 if (success == 0) {
972 debug3("%s: no challenge", __func__);
973 buffer_free(&m);
974 return (-1);
977 /* Get the challenge, and format the response */
978 challenge = buffer_get_string(&m, NULL);
979 buffer_free(&m);
981 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
982 (*prompts)[0] = challenge;
984 debug3("%s: received challenge: %s", __func__, challenge);
986 return (0);
990 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
992 Buffer m;
993 int authok;
995 debug3("%s: entering", __func__);
996 if (numresponses != 1)
997 return (-1);
999 buffer_init(&m);
1000 buffer_put_cstring(&m, responses[0]);
1001 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1003 mm_request_receive_expect(pmonitor->m_recvfd,
1004 MONITOR_ANS_BSDAUTHRESPOND, &m);
1006 authok = buffer_get_int(&m);
1007 buffer_free(&m);
1009 return ((authok == 0) ? -1 : 0);
1012 #ifdef SKEY
1014 mm_skey_query(void *ctx, char **name, char **infotxt,
1015 u_int *numprompts, char ***prompts, u_int **echo_on)
1017 Buffer m;
1018 u_int success;
1019 char *challenge;
1021 debug3("%s: entering", __func__);
1023 buffer_init(&m);
1024 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
1026 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
1027 &m);
1028 success = buffer_get_int(&m);
1029 if (success == 0) {
1030 debug3("%s: no challenge", __func__);
1031 buffer_free(&m);
1032 return (-1);
1035 /* Get the challenge, and format the response */
1036 challenge = buffer_get_string(&m, NULL);
1037 buffer_free(&m);
1039 debug3("%s: received challenge: %s", __func__, challenge);
1041 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
1043 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
1044 free(challenge);
1046 return (0);
1050 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
1052 Buffer m;
1053 int authok;
1055 debug3("%s: entering", __func__);
1056 if (numresponses != 1)
1057 return (-1);
1059 buffer_init(&m);
1060 buffer_put_cstring(&m, responses[0]);
1061 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1063 mm_request_receive_expect(pmonitor->m_recvfd,
1064 MONITOR_ANS_SKEYRESPOND, &m);
1066 authok = buffer_get_int(&m);
1067 buffer_free(&m);
1069 return ((authok == 0) ? -1 : 0);
1071 #endif /* SKEY */
1073 void
1074 mm_ssh1_session_id(u_char session_id[16])
1076 Buffer m;
1077 int i;
1079 debug3("%s entering", __func__);
1081 buffer_init(&m);
1082 for (i = 0; i < 16; i++)
1083 buffer_put_char(&m, session_id[i]);
1085 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1086 buffer_free(&m);
1089 #ifdef WITH_SSH1
1091 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1093 Buffer m;
1094 Key *key;
1095 u_char *blob;
1096 u_int blen;
1097 int allowed = 0, have_forced = 0;
1099 debug3("%s entering", __func__);
1101 buffer_init(&m);
1102 buffer_put_bignum2(&m, client_n);
1104 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1105 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1107 allowed = buffer_get_int(&m);
1109 /* fake forced command */
1110 auth_clear_options();
1111 have_forced = buffer_get_int(&m);
1112 forced_command = have_forced ? xstrdup("true") : NULL;
1114 if (allowed && rkey != NULL) {
1115 blob = buffer_get_string(&m, &blen);
1116 if ((key = key_from_blob(blob, blen)) == NULL)
1117 fatal("%s: key_from_blob failed", __func__);
1118 *rkey = key;
1119 free(blob);
1121 buffer_free(&m);
1123 return (allowed);
1126 BIGNUM *
1127 mm_auth_rsa_generate_challenge(Key *key)
1129 Buffer m;
1130 BIGNUM *challenge;
1131 u_char *blob;
1132 u_int blen;
1134 debug3("%s entering", __func__);
1136 if ((challenge = BN_new()) == NULL)
1137 fatal("%s: BN_new failed", __func__);
1139 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1140 if (key_to_blob(key, &blob, &blen) == 0)
1141 fatal("%s: key_to_blob failed", __func__);
1142 key->type = KEY_RSA1;
1144 buffer_init(&m);
1145 buffer_put_string(&m, blob, blen);
1146 free(blob);
1148 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1149 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1151 buffer_get_bignum2(&m, challenge);
1152 buffer_free(&m);
1154 return (challenge);
1158 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1160 Buffer m;
1161 u_char *blob;
1162 u_int blen;
1163 int success = 0;
1165 debug3("%s entering", __func__);
1167 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1168 if (key_to_blob(key, &blob, &blen) == 0)
1169 fatal("%s: key_to_blob failed", __func__);
1170 key->type = KEY_RSA1;
1172 buffer_init(&m);
1173 buffer_put_string(&m, blob, blen);
1174 buffer_put_string(&m, response, 16);
1175 free(blob);
1177 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1178 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1180 success = buffer_get_int(&m);
1181 buffer_free(&m);
1183 return (success);
1185 #endif
1187 #ifdef SSH_AUDIT_EVENTS
1188 void
1189 mm_audit_event(ssh_audit_event_t event)
1191 Buffer m;
1193 debug3("%s entering", __func__);
1195 buffer_init(&m);
1196 buffer_put_int(&m, event);
1198 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1199 buffer_free(&m);
1202 void
1203 mm_audit_run_command(const char *command)
1205 Buffer m;
1207 debug3("%s entering command %s", __func__, command);
1209 buffer_init(&m);
1210 buffer_put_cstring(&m, command);
1212 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1213 buffer_free(&m);
1215 #endif /* SSH_AUDIT_EVENTS */
1217 #ifdef GSSAPI
1218 OM_uint32
1219 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1221 Buffer m;
1222 OM_uint32 major;
1224 /* Client doesn't get to see the context */
1225 *ctx = NULL;
1227 buffer_init(&m);
1228 buffer_put_string(&m, goid->elements, goid->length);
1230 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1231 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1233 major = buffer_get_int(&m);
1235 buffer_free(&m);
1236 return (major);
1239 OM_uint32
1240 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1241 gss_buffer_desc *out, OM_uint32 *flags)
1243 Buffer m;
1244 OM_uint32 major;
1245 u_int len;
1247 buffer_init(&m);
1248 buffer_put_string(&m, in->value, in->length);
1250 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1251 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1253 major = buffer_get_int(&m);
1254 out->value = buffer_get_string(&m, &len);
1255 out->length = len;
1256 if (flags)
1257 *flags = buffer_get_int(&m);
1259 buffer_free(&m);
1261 return (major);
1264 OM_uint32
1265 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1267 Buffer m;
1268 OM_uint32 major;
1270 buffer_init(&m);
1271 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1272 buffer_put_string(&m, gssmic->value, gssmic->length);
1274 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1275 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1276 &m);
1278 major = buffer_get_int(&m);
1279 buffer_free(&m);
1280 return(major);
1284 mm_ssh_gssapi_userok(char *user)
1286 Buffer m;
1287 int authenticated = 0;
1289 buffer_init(&m);
1291 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1292 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1293 &m);
1295 authenticated = buffer_get_int(&m);
1297 buffer_free(&m);
1298 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1299 return (authenticated);
1301 #endif /* GSSAPI */