2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 RCSID("$OpenBSD: monitor_wrap.c,v 1.4 2002/03/19 14:27:39 markus Exp $");
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
44 #include "monitor_wrap.h"
47 #include "monitor_fdpass.h"
56 extern Newkeys *newkeys[];
57 extern z_stream incoming_stream;
58 extern z_stream outgoing_stream;
59 extern struct monitor *monitor;
60 extern Buffer input, output;
63 mm_request_send(int socket, enum monitor_reqtype type, Buffer *m)
66 u_int mlen = buffer_len(m);
68 debug3("%s entering: type %d", __FUNCTION__, type);
70 PUT_32BIT(buf, mlen + 1);
71 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
72 if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf))
73 fatal("%s: write", __FUNCTION__);
74 if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen)
75 fatal("%s: write", __FUNCTION__);
79 mm_request_receive(int socket, Buffer *m)
85 debug3("%s entering", __FUNCTION__);
87 res = atomicio(read, socket, buf, sizeof(buf));
88 if (res != sizeof(buf)) {
91 fatal("%s: read: %d", __FUNCTION__, res);
93 msg_len = GET_32BIT(buf);
94 if (msg_len > 256 * 1024)
95 fatal("%s: read: bad msg_len %d", __FUNCTION__, msg_len);
97 buffer_append_space(m, msg_len);
98 res = atomicio(read, socket, buffer_ptr(m), msg_len);
100 fatal("%s: read: %d != msg_len", __FUNCTION__, res);
104 mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m)
108 debug3("%s entering: type %d", __FUNCTION__, type);
110 mm_request_receive(socket, m);
111 rtype = buffer_get_char(m);
113 fatal("%s: read: rtype %d != type %d", __FUNCTION__,
118 mm_choose_dh(int min, int nbits, int max)
125 buffer_put_int(&m, min);
126 buffer_put_int(&m, nbits);
127 buffer_put_int(&m, max);
129 mm_request_send(monitor->m_recvfd, MONITOR_REQ_MODULI, &m);
131 debug3("%s: waiting for MONITOR_ANS_MODULI", __FUNCTION__);
132 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_MODULI, &m);
134 success = buffer_get_char(&m);
136 fatal("%s: MONITOR_ANS_MODULI failed", __FUNCTION__);
138 if ((p = BN_new()) == NULL)
139 fatal("%s: BN_new failed", __FUNCTION__);
140 if ((g = BN_new()) == NULL)
141 fatal("%s: BN_new failed", __FUNCTION__);
142 buffer_get_bignum2(&m, p);
143 buffer_get_bignum2(&m, g);
145 debug3("%s: remaining %d", __FUNCTION__, buffer_len(&m));
148 return (dh_new_group(g, p));
152 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
154 Kex *kex = *monitor->m_pkex;
157 debug3("%s entering", __FUNCTION__);
160 buffer_put_int(&m, kex->host_key_index(key));
161 buffer_put_string(&m, data, datalen);
163 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SIGN, &m);
165 debug3("%s: waiting for MONITOR_ANS_SIGN", __FUNCTION__);
166 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SIGN, &m);
167 *sigp = buffer_get_string(&m, lenp);
174 mm_getpwnamallow(const char *login)
180 debug3("%s entering", __FUNCTION__);
183 buffer_put_cstring(&m, login);
185 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
187 debug3("%s: waiting for MONITOR_ANS_PWNAM", __FUNCTION__);
188 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
190 if (buffer_get_char(&m) == 0) {
194 pw = buffer_get_string(&m, &pwlen);
195 if (pwlen != sizeof(struct passwd))
196 fatal("%s: struct passwd size mismatch", __FUNCTION__);
197 pw->pw_name = buffer_get_string(&m, NULL);
198 pw->pw_passwd = buffer_get_string(&m, NULL);
199 pw->pw_gecos = buffer_get_string(&m, NULL);
200 pw->pw_class = buffer_get_string(&m, NULL);
201 pw->pw_dir = buffer_get_string(&m, NULL);
202 pw->pw_shell = buffer_get_string(&m, NULL);
208 /* Inform the privileged process about service and style */
211 mm_inform_authserv(char *service, char *style)
215 debug3("%s entering", __FUNCTION__);
218 buffer_put_cstring(&m, service);
219 buffer_put_cstring(&m, style ? style : "");
221 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
226 /* Do the password authentication */
228 mm_auth_password(Authctxt *authctxt, char *password)
231 int authenticated = 0;
233 debug3("%s entering", __FUNCTION__);
236 buffer_put_cstring(&m, password);
237 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
239 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __FUNCTION__);
240 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
242 authenticated = buffer_get_int(&m);
246 debug3("%s: user %sauthenticated",
247 __FUNCTION__, authenticated ? "" : "not ");
248 return (authenticated);
252 mm_user_key_allowed(struct passwd *pw, Key *key)
254 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
258 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
261 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
265 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
266 char *host, Key *key)
270 key->type = KEY_RSA; /* XXX hack for key_to_blob */
271 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
272 key->type = KEY_RSA1;
277 mm_send_debug(Buffer *m)
281 while (buffer_len(m)) {
282 msg = buffer_get_string(m, NULL);
283 debug3("%s: Sending debug: %s", __FUNCTION__, msg);
284 packet_send_debug("%s", msg);
290 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
297 debug3("%s entering", __FUNCTION__);
299 /* Convert the key to a blob and the pass it over */
300 if (!key_to_blob(key, &blob, &len))
304 buffer_put_int(&m, type);
305 buffer_put_cstring(&m, user ? user : "");
306 buffer_put_cstring(&m, host ? host : "");
307 buffer_put_string(&m, blob, len);
310 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
312 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __FUNCTION__);
313 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
315 allowed = buffer_get_int(&m);
317 /* Send potential debug messages */
326 * This key verify needs to send the key type along, because the
327 * privileged parent makes the decision if the key is allowed
328 * for authentication.
332 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
339 debug3("%s entering", __FUNCTION__);
341 /* Convert the key to a blob and the pass it over */
342 if (!key_to_blob(key, &blob, &len))
346 buffer_put_string(&m, blob, len);
347 buffer_put_string(&m, sig, siglen);
348 buffer_put_string(&m, data, datalen);
351 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
353 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __FUNCTION__);
354 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
356 verified = buffer_get_int(&m);
363 /* Export key state after authentication */
365 mm_newkeys_from_blob(u_char *blob, int blen)
369 Newkeys *newkey = NULL;
374 debug3("%s: %p(%d)", __FUNCTION__, blob, blen);
376 dump_base64(stderr, blob, blen);
379 buffer_append(&b, blob, blen);
381 newkey = xmalloc(sizeof(*newkey));
384 comp = &newkey->comp;
387 enc->name = buffer_get_string(&b, NULL);
388 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
389 enc->enabled = buffer_get_int(&b);
390 enc->block_size = buffer_get_int(&b);
391 enc->key = buffer_get_string(&b, &enc->key_len);
392 enc->iv = buffer_get_string(&b, &len);
393 if (len != enc->block_size)
394 fatal("%s: bad ivlen: expected %d != %d", __FUNCTION__,
395 enc->block_size, len);
397 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
398 fatal("%s: bad cipher name %s or pointer %p", __FUNCTION__,
399 enc->name, enc->cipher);
402 mac->name = buffer_get_string(&b, NULL);
403 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
404 fatal("%s: can not init mac %s", __FUNCTION__, mac->name);
405 mac->enabled = buffer_get_int(&b);
406 mac->key = buffer_get_string(&b, &len);
407 if (len > mac->key_len)
408 fatal("%s: bad mac key lenght: %d > %d", __FUNCTION__, len,
413 comp->type = buffer_get_int(&b);
414 comp->enabled = buffer_get_int(&b);
415 comp->name = buffer_get_string(&b, NULL);
417 len = buffer_len(&b);
419 error("newkeys_from_blob: remaining bytes in blob %d", len);
425 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
433 Newkeys *newkey = newkeys[mode];
435 debug3("%s: converting %p", __FUNCTION__, newkey);
437 if (newkey == NULL) {
438 error("%s: newkey == NULL", __FUNCTION__);
443 comp = &newkey->comp;
447 buffer_put_cstring(&b, enc->name);
448 /* The cipher struct is constant and shared, you export pointer */
449 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
450 buffer_put_int(&b, enc->enabled);
451 buffer_put_int(&b, enc->block_size);
452 buffer_put_string(&b, enc->key, enc->key_len);
453 packet_get_keyiv(mode, enc->iv, enc->block_size);
454 buffer_put_string(&b, enc->iv, enc->block_size);
457 buffer_put_cstring(&b, mac->name);
458 buffer_put_int(&b, mac->enabled);
459 buffer_put_string(&b, mac->key, mac->key_len);
462 buffer_put_int(&b, comp->type);
463 buffer_put_int(&b, comp->enabled);
464 buffer_put_cstring(&b, comp->name);
466 len = buffer_len(&b);
468 memcpy(buf, buffer_ptr(&b), len);
469 memset(buffer_ptr(&b), 0, len);
479 mm_send_kex(Buffer *m, Kex *kex)
481 buffer_put_string(m, kex->session_id, kex->session_id_len);
482 buffer_put_int(m, kex->we_need);
483 buffer_put_int(m, kex->hostkey_type);
484 buffer_put_int(m, kex->kex_type);
485 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
486 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
487 buffer_put_int(m, kex->flags);
488 buffer_put_cstring(m, kex->client_version_string);
489 buffer_put_cstring(m, kex->server_version_string);
493 mm_send_keystate(struct monitor *monitor)
505 buffer_put_int(&m, packet_get_protocol_flags());
507 buffer_put_int(&m, packet_get_ssh1_cipher());
509 debug3("%s: Sending ssh1 IV", __FUNCTION__);
510 ivlen = packet_get_keyiv_len(MODE_OUT);
511 packet_get_keyiv(MODE_OUT, iv, ivlen);
512 buffer_put_string(&m, iv, ivlen);
513 ivlen = packet_get_keyiv_len(MODE_OUT);
514 packet_get_keyiv(MODE_IN, iv, ivlen);
515 buffer_put_string(&m, iv, ivlen);
518 /* Kex for rekeying */
519 mm_send_kex(&m, *monitor->m_pkex);
522 debug3("%s: Sending new keys: %p %p",
523 __FUNCTION__, newkeys[MODE_OUT], newkeys[MODE_IN]);
526 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
527 fatal("%s: conversion of newkeys failed", __FUNCTION__);
529 buffer_put_string(&m, blob, bloblen);
532 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
533 fatal("%s: conversion of newkeys failed", __FUNCTION__);
535 buffer_put_string(&m, blob, bloblen);
538 buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
539 buffer_put_int(&m, packet_get_seqnr(MODE_IN));
541 debug3("%s: New keys have been sent", __FUNCTION__);
543 /* More key context */
544 plen = packet_get_keycontext(MODE_OUT, NULL);
546 packet_get_keycontext(MODE_OUT, p);
547 buffer_put_string(&m, p, plen);
550 plen = packet_get_keycontext(MODE_IN, NULL);
552 packet_get_keycontext(MODE_IN, p);
553 buffer_put_string(&m, p, plen);
556 /* Compression state */
557 debug3("%s: Sending compression state", __FUNCTION__);
558 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
559 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
561 /* Network I/O buffers */
562 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
563 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
565 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
566 debug3("%s: Finished sending state", __FUNCTION__);
572 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
579 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTY, &m);
581 debug3("%s: waiting for MONITOR_ANS_PTY", __FUNCTION__);
582 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PTY, &m);
584 success = buffer_get_int(&m);
586 debug3("%s: pty alloc failed", __FUNCTION__);
590 p = buffer_get_string(&m, NULL);
593 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
596 *ptyfd = mm_receive_fd(monitor->m_recvfd);
597 *ttyfd = mm_receive_fd(monitor->m_recvfd);
604 mm_session_pty_cleanup2(void *session)
606 Session *s = session;
612 buffer_put_cstring(&m, s->tty);
613 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
616 /* closed dup'ed master */
617 if (close(s->ptymaster) < 0)
618 error("close(s->ptymaster): %s", strerror(errno));
620 /* unlink pty from session */
624 /* Request process termination */
632 mm_request_send(monitor->m_recvfd, MONITOR_REQ_TERM, &m);
637 mm_ssh1_session_key(BIGNUM *num)
643 buffer_put_bignum2(&m, num);
644 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
646 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
648 rsafail = buffer_get_int(&m);
649 buffer_get_bignum2(&m, num);
657 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
658 char ***prompts, u_int **echo_on)
661 *infotxt = xstrdup("");
663 *prompts = xmalloc(*numprompts * sizeof(char*));
664 *echo_on = xmalloc(*numprompts * sizeof(u_int));
669 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
670 u_int *numprompts, char ***prompts, u_int **echo_on)
676 debug3("%s: entering", __FUNCTION__);
679 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
681 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
683 res = buffer_get_int(&m);
685 debug3("%s: no challenge", __FUNCTION__);
690 /* Get the challenge, and format the response */
691 challenge = buffer_get_string(&m, NULL);
694 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
695 (*prompts)[0] = challenge;
697 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
703 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
708 debug3("%s: entering", __FUNCTION__);
709 if (numresponses != 1)
713 buffer_put_cstring(&m, responses[0]);
714 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
716 mm_request_receive_expect(monitor->m_recvfd,
717 MONITOR_ANS_BSDAUTHRESPOND, &m);
719 authok = buffer_get_int(&m);
722 return ((authok == 0) ? -1 : 0);
726 mm_skey_query(void *ctx, char **name, char **infotxt,
727 u_int *numprompts, char ***prompts, u_int **echo_on)
733 debug3("%s: entering", __FUNCTION__);
736 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
738 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
740 res = buffer_get_int(&m);
742 debug3("%s: no challenge", __FUNCTION__);
747 /* Get the challenge, and format the response */
748 challenge = buffer_get_string(&m, NULL);
751 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
753 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
755 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
757 strlcpy(p, challenge, len);
758 strlcat(p, SKEY_PROMPT, len);
766 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
771 debug3("%s: entering", __FUNCTION__);
772 if (numresponses != 1)
776 buffer_put_cstring(&m, responses[0]);
777 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
779 mm_request_receive_expect(monitor->m_recvfd,
780 MONITOR_ANS_SKEYRESPOND, &m);
782 authok = buffer_get_int(&m);
785 return ((authok == 0) ? -1 : 0);
789 mm_ssh1_session_id(u_char session_id[16])
794 debug3("%s entering", __FUNCTION__);
797 for (i = 0; i < 16; i++)
798 buffer_put_char(&m, session_id[i]);
800 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSID, &m);
805 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
813 debug3("%s entering", __FUNCTION__);
816 buffer_put_bignum2(&m, client_n);
818 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
819 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
821 allowed = buffer_get_int(&m);
823 if (allowed && rkey != NULL) {
824 blob = buffer_get_string(&m, &blen);
825 if ((key = key_from_blob(blob, blen)) == NULL)
826 fatal("%s: key_from_blob failed", __FUNCTION__);
837 mm_auth_rsa_generate_challenge(Key *key)
844 debug3("%s entering", __FUNCTION__);
846 if ((challenge = BN_new()) == NULL)
847 fatal("%s: BN_new failed", __FUNCTION__);
849 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
850 if (key_to_blob(key, &blob, &blen) == 0)
851 fatal("%s: key_to_blob failed", __FUNCTION__);
852 key->type = KEY_RSA1;
855 buffer_put_string(&m, blob, blen);
858 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
859 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
861 buffer_get_bignum2(&m, challenge);
868 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
875 debug3("%s entering", __FUNCTION__);
877 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
878 if (key_to_blob(key, &blob, &blen) == 0)
879 fatal("%s: key_to_blob failed", __FUNCTION__);
880 key->type = KEY_RSA1;
883 buffer_put_string(&m, blob, blen);
884 buffer_put_string(&m, response, 16);
887 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
888 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
890 success = buffer_get_int(&m);