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.5 2002/03/25 20:12:10 stevesk 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: %ld", __FUNCTION__, (long)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: %ld != msg_len", __FUNCTION__, (long)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 #ifdef HAVE_PW_CLASS_IN_PASSWD
201 pw->pw_class = buffer_get_string(&m, NULL);
203 pw->pw_dir = buffer_get_string(&m, NULL);
204 pw->pw_shell = buffer_get_string(&m, NULL);
210 /* Inform the privileged process about service and style */
213 mm_inform_authserv(char *service, char *style)
217 debug3("%s entering", __FUNCTION__);
220 buffer_put_cstring(&m, service);
221 buffer_put_cstring(&m, style ? style : "");
223 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
228 /* Do the password authentication */
230 mm_auth_password(Authctxt *authctxt, char *password)
233 int authenticated = 0;
235 debug3("%s entering", __FUNCTION__);
238 buffer_put_cstring(&m, password);
239 mm_request_send(monitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
241 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __FUNCTION__);
242 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
244 authenticated = buffer_get_int(&m);
248 debug3("%s: user %sauthenticated",
249 __FUNCTION__, authenticated ? "" : "not ");
250 return (authenticated);
254 mm_user_key_allowed(struct passwd *pw, Key *key)
256 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
260 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
263 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
267 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
268 char *host, Key *key)
272 key->type = KEY_RSA; /* XXX hack for key_to_blob */
273 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
274 key->type = KEY_RSA1;
279 mm_send_debug(Buffer *m)
283 while (buffer_len(m)) {
284 msg = buffer_get_string(m, NULL);
285 debug3("%s: Sending debug: %s", __FUNCTION__, msg);
286 packet_send_debug("%s", msg);
292 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
299 debug3("%s entering", __FUNCTION__);
301 /* Convert the key to a blob and the pass it over */
302 if (!key_to_blob(key, &blob, &len))
306 buffer_put_int(&m, type);
307 buffer_put_cstring(&m, user ? user : "");
308 buffer_put_cstring(&m, host ? host : "");
309 buffer_put_string(&m, blob, len);
312 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
314 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __FUNCTION__);
315 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
317 allowed = buffer_get_int(&m);
319 /* Send potential debug messages */
328 * This key verify needs to send the key type along, because the
329 * privileged parent makes the decision if the key is allowed
330 * for authentication.
334 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
341 debug3("%s entering", __FUNCTION__);
343 /* Convert the key to a blob and the pass it over */
344 if (!key_to_blob(key, &blob, &len))
348 buffer_put_string(&m, blob, len);
349 buffer_put_string(&m, sig, siglen);
350 buffer_put_string(&m, data, datalen);
353 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
355 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __FUNCTION__);
356 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
358 verified = buffer_get_int(&m);
365 /* Export key state after authentication */
367 mm_newkeys_from_blob(u_char *blob, int blen)
371 Newkeys *newkey = NULL;
376 debug3("%s: %p(%d)", __FUNCTION__, blob, blen);
378 dump_base64(stderr, blob, blen);
381 buffer_append(&b, blob, blen);
383 newkey = xmalloc(sizeof(*newkey));
386 comp = &newkey->comp;
389 enc->name = buffer_get_string(&b, NULL);
390 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
391 enc->enabled = buffer_get_int(&b);
392 enc->block_size = buffer_get_int(&b);
393 enc->key = buffer_get_string(&b, &enc->key_len);
394 enc->iv = buffer_get_string(&b, &len);
395 if (len != enc->block_size)
396 fatal("%s: bad ivlen: expected %d != %d", __FUNCTION__,
397 enc->block_size, len);
399 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
400 fatal("%s: bad cipher name %s or pointer %p", __FUNCTION__,
401 enc->name, enc->cipher);
404 mac->name = buffer_get_string(&b, NULL);
405 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
406 fatal("%s: can not init mac %s", __FUNCTION__, mac->name);
407 mac->enabled = buffer_get_int(&b);
408 mac->key = buffer_get_string(&b, &len);
409 if (len > mac->key_len)
410 fatal("%s: bad mac key lenght: %d > %d", __FUNCTION__, len,
415 comp->type = buffer_get_int(&b);
416 comp->enabled = buffer_get_int(&b);
417 comp->name = buffer_get_string(&b, NULL);
419 len = buffer_len(&b);
421 error("newkeys_from_blob: remaining bytes in blob %d", len);
427 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
435 Newkeys *newkey = newkeys[mode];
437 debug3("%s: converting %p", __FUNCTION__, newkey);
439 if (newkey == NULL) {
440 error("%s: newkey == NULL", __FUNCTION__);
445 comp = &newkey->comp;
449 buffer_put_cstring(&b, enc->name);
450 /* The cipher struct is constant and shared, you export pointer */
451 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
452 buffer_put_int(&b, enc->enabled);
453 buffer_put_int(&b, enc->block_size);
454 buffer_put_string(&b, enc->key, enc->key_len);
455 packet_get_keyiv(mode, enc->iv, enc->block_size);
456 buffer_put_string(&b, enc->iv, enc->block_size);
459 buffer_put_cstring(&b, mac->name);
460 buffer_put_int(&b, mac->enabled);
461 buffer_put_string(&b, mac->key, mac->key_len);
464 buffer_put_int(&b, comp->type);
465 buffer_put_int(&b, comp->enabled);
466 buffer_put_cstring(&b, comp->name);
468 len = buffer_len(&b);
470 memcpy(buf, buffer_ptr(&b), len);
471 memset(buffer_ptr(&b), 0, len);
481 mm_send_kex(Buffer *m, Kex *kex)
483 buffer_put_string(m, kex->session_id, kex->session_id_len);
484 buffer_put_int(m, kex->we_need);
485 buffer_put_int(m, kex->hostkey_type);
486 buffer_put_int(m, kex->kex_type);
487 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
488 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
489 buffer_put_int(m, kex->flags);
490 buffer_put_cstring(m, kex->client_version_string);
491 buffer_put_cstring(m, kex->server_version_string);
495 mm_send_keystate(struct monitor *monitor)
507 buffer_put_int(&m, packet_get_protocol_flags());
509 buffer_put_int(&m, packet_get_ssh1_cipher());
511 debug3("%s: Sending ssh1 IV", __FUNCTION__);
512 ivlen = packet_get_keyiv_len(MODE_OUT);
513 packet_get_keyiv(MODE_OUT, iv, ivlen);
514 buffer_put_string(&m, iv, ivlen);
515 ivlen = packet_get_keyiv_len(MODE_OUT);
516 packet_get_keyiv(MODE_IN, iv, ivlen);
517 buffer_put_string(&m, iv, ivlen);
520 /* Kex for rekeying */
521 mm_send_kex(&m, *monitor->m_pkex);
524 debug3("%s: Sending new keys: %p %p",
525 __FUNCTION__, newkeys[MODE_OUT], newkeys[MODE_IN]);
528 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
529 fatal("%s: conversion of newkeys failed", __FUNCTION__);
531 buffer_put_string(&m, blob, bloblen);
534 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
535 fatal("%s: conversion of newkeys failed", __FUNCTION__);
537 buffer_put_string(&m, blob, bloblen);
540 buffer_put_int(&m, packet_get_seqnr(MODE_OUT));
541 buffer_put_int(&m, packet_get_seqnr(MODE_IN));
543 debug3("%s: New keys have been sent", __FUNCTION__);
545 /* More key context */
546 plen = packet_get_keycontext(MODE_OUT, NULL);
548 packet_get_keycontext(MODE_OUT, p);
549 buffer_put_string(&m, p, plen);
552 plen = packet_get_keycontext(MODE_IN, NULL);
554 packet_get_keycontext(MODE_IN, p);
555 buffer_put_string(&m, p, plen);
558 /* Compression state */
559 debug3("%s: Sending compression state", __FUNCTION__);
560 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
561 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
563 /* Network I/O buffers */
564 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
565 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
567 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
568 debug3("%s: Finished sending state", __FUNCTION__);
574 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
581 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTY, &m);
583 debug3("%s: waiting for MONITOR_ANS_PTY", __FUNCTION__);
584 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_PTY, &m);
586 success = buffer_get_int(&m);
588 debug3("%s: pty alloc failed", __FUNCTION__);
592 p = buffer_get_string(&m, NULL);
595 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
598 *ptyfd = mm_receive_fd(monitor->m_recvfd);
599 *ttyfd = mm_receive_fd(monitor->m_recvfd);
606 mm_session_pty_cleanup2(void *session)
608 Session *s = session;
614 buffer_put_cstring(&m, s->tty);
615 mm_request_send(monitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
618 /* closed dup'ed master */
619 if (close(s->ptymaster) < 0)
620 error("close(s->ptymaster): %s", strerror(errno));
622 /* unlink pty from session */
626 /* Request process termination */
634 mm_request_send(monitor->m_recvfd, MONITOR_REQ_TERM, &m);
639 mm_ssh1_session_key(BIGNUM *num)
645 buffer_put_bignum2(&m, num);
646 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
648 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
650 rsafail = buffer_get_int(&m);
651 buffer_get_bignum2(&m, num);
659 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
660 char ***prompts, u_int **echo_on)
663 *infotxt = xstrdup("");
665 *prompts = xmalloc(*numprompts * sizeof(char*));
666 *echo_on = xmalloc(*numprompts * sizeof(u_int));
671 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
672 u_int *numprompts, char ***prompts, u_int **echo_on)
678 debug3("%s: entering", __FUNCTION__);
681 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
683 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
685 res = buffer_get_int(&m);
687 debug3("%s: no challenge", __FUNCTION__);
692 /* Get the challenge, and format the response */
693 challenge = buffer_get_string(&m, NULL);
696 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
697 (*prompts)[0] = challenge;
699 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
705 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
710 debug3("%s: entering", __FUNCTION__);
711 if (numresponses != 1)
715 buffer_put_cstring(&m, responses[0]);
716 mm_request_send(monitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
718 mm_request_receive_expect(monitor->m_recvfd,
719 MONITOR_ANS_BSDAUTHRESPOND, &m);
721 authok = buffer_get_int(&m);
724 return ((authok == 0) ? -1 : 0);
728 mm_skey_query(void *ctx, char **name, char **infotxt,
729 u_int *numprompts, char ***prompts, u_int **echo_on)
735 debug3("%s: entering", __FUNCTION__);
738 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
740 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
742 res = buffer_get_int(&m);
744 debug3("%s: no challenge", __FUNCTION__);
749 /* Get the challenge, and format the response */
750 challenge = buffer_get_string(&m, NULL);
753 debug3("%s: received challenge: %s", __FUNCTION__, challenge);
755 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
757 len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
759 strlcpy(p, challenge, len);
760 strlcat(p, SKEY_PROMPT, len);
768 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
773 debug3("%s: entering", __FUNCTION__);
774 if (numresponses != 1)
778 buffer_put_cstring(&m, responses[0]);
779 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
781 mm_request_receive_expect(monitor->m_recvfd,
782 MONITOR_ANS_SKEYRESPOND, &m);
784 authok = buffer_get_int(&m);
787 return ((authok == 0) ? -1 : 0);
791 mm_ssh1_session_id(u_char session_id[16])
796 debug3("%s entering", __FUNCTION__);
799 for (i = 0; i < 16; i++)
800 buffer_put_char(&m, session_id[i]);
802 mm_request_send(monitor->m_recvfd, MONITOR_REQ_SESSID, &m);
807 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
815 debug3("%s entering", __FUNCTION__);
818 buffer_put_bignum2(&m, client_n);
820 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
821 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
823 allowed = buffer_get_int(&m);
825 if (allowed && rkey != NULL) {
826 blob = buffer_get_string(&m, &blen);
827 if ((key = key_from_blob(blob, blen)) == NULL)
828 fatal("%s: key_from_blob failed", __FUNCTION__);
839 mm_auth_rsa_generate_challenge(Key *key)
846 debug3("%s entering", __FUNCTION__);
848 if ((challenge = BN_new()) == NULL)
849 fatal("%s: BN_new failed", __FUNCTION__);
851 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
852 if (key_to_blob(key, &blob, &blen) == 0)
853 fatal("%s: key_to_blob failed", __FUNCTION__);
854 key->type = KEY_RSA1;
857 buffer_put_string(&m, blob, blen);
860 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
861 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
863 buffer_get_bignum2(&m, challenge);
870 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
877 debug3("%s entering", __FUNCTION__);
879 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
880 if (key_to_blob(key, &blob, &blen) == 0)
881 fatal("%s: key_to_blob failed", __FUNCTION__);
882 key->type = KEY_RSA1;
885 buffer_put_string(&m, blob, blen);
886 buffer_put_string(&m, response, 16);
889 mm_request_send(monitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
890 mm_request_receive_expect(monitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
892 success = buffer_get_int(&m);