- (dtucker) [openbsd-compat/fake-rfc2553.h] rename ssh_gai_strerror hack
[openssh-git.git] / monitor_wrap.c
blobe895f19240ae6971bcee8346d5d35d1f5ef2a986
1 /* $OpenBSD: monitor_wrap.c,v 1.60 2007/10/29 04:08:08 dtucker 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 #include <openssl/bn.h>
42 #include <openssl/dh.h>
44 #include "xmalloc.h"
45 #include "ssh.h"
46 #include "dh.h"
47 #include "buffer.h"
48 #include "key.h"
49 #include "cipher.h"
50 #include "kex.h"
51 #include "hostfile.h"
52 #include "auth.h"
53 #include "auth-options.h"
54 #include "packet.h"
55 #include "mac.h"
56 #include "log.h"
57 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
58 #undef TARGET_OS_MAC
59 #include "zlib.h"
60 #define TARGET_OS_MAC 1
61 #else
62 #include "zlib.h"
63 #endif
64 #include "monitor.h"
65 #ifdef GSSAPI
66 #include "ssh-gss.h"
67 #endif
68 #include "monitor_wrap.h"
69 #include "atomicio.h"
70 #include "monitor_fdpass.h"
71 #include "misc.h"
72 #include "servconf.h"
74 #include "channels.h"
75 #include "session.h"
76 #include "servconf.h"
78 /* Imports */
79 extern int compat20;
80 extern Newkeys *newkeys[];
81 extern z_stream incoming_stream;
82 extern z_stream outgoing_stream;
83 extern struct monitor *pmonitor;
84 extern Buffer input, output;
85 extern Buffer loginmsg;
86 extern ServerOptions options;
88 int
89 mm_is_monitor(void)
92 * m_pid is only set in the privileged part, and
93 * points to the unprivileged child.
95 return (pmonitor && pmonitor->m_pid > 0);
98 void
99 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
101 u_int mlen = buffer_len(m);
102 u_char buf[5];
104 debug3("%s entering: type %d", __func__, type);
106 put_u32(buf, mlen + 1);
107 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
108 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
109 fatal("%s: write: %s", __func__, strerror(errno));
110 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
111 fatal("%s: write: %s", __func__, strerror(errno));
114 void
115 mm_request_receive(int sock, Buffer *m)
117 u_char buf[4];
118 u_int msg_len;
120 debug3("%s entering", __func__);
122 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
123 if (errno == EPIPE)
124 cleanup_exit(255);
125 fatal("%s: read: %s", __func__, strerror(errno));
127 msg_len = get_u32(buf);
128 if (msg_len > 256 * 1024)
129 fatal("%s: read: bad msg_len %d", __func__, msg_len);
130 buffer_clear(m);
131 buffer_append_space(m, msg_len);
132 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
133 fatal("%s: read: %s", __func__, strerror(errno));
136 void
137 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
139 u_char rtype;
141 debug3("%s entering: type %d", __func__, type);
143 mm_request_receive(sock, m);
144 rtype = buffer_get_char(m);
145 if (rtype != type)
146 fatal("%s: read: rtype %d != type %d", __func__,
147 rtype, type);
150 DH *
151 mm_choose_dh(int min, int nbits, int max)
153 BIGNUM *p, *g;
154 int success = 0;
155 Buffer m;
157 buffer_init(&m);
158 buffer_put_int(&m, min);
159 buffer_put_int(&m, nbits);
160 buffer_put_int(&m, max);
162 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
164 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
165 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
167 success = buffer_get_char(&m);
168 if (success == 0)
169 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
171 if ((p = BN_new()) == NULL)
172 fatal("%s: BN_new failed", __func__);
173 if ((g = BN_new()) == NULL)
174 fatal("%s: BN_new failed", __func__);
175 buffer_get_bignum2(&m, p);
176 buffer_get_bignum2(&m, g);
178 debug3("%s: remaining %d", __func__, buffer_len(&m));
179 buffer_free(&m);
181 return (dh_new_group(g, p));
185 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
187 Kex *kex = *pmonitor->m_pkex;
188 Buffer m;
190 debug3("%s entering", __func__);
192 buffer_init(&m);
193 buffer_put_int(&m, kex->host_key_index(key));
194 buffer_put_string(&m, data, datalen);
196 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
198 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
199 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
200 *sigp = buffer_get_string(&m, lenp);
201 buffer_free(&m);
203 return (0);
206 struct passwd *
207 mm_getpwnamallow(const char *username)
209 Buffer m;
210 struct passwd *pw;
211 u_int len;
212 ServerOptions *newopts;
214 debug3("%s entering", __func__);
216 buffer_init(&m);
217 buffer_put_cstring(&m, username);
219 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
221 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
222 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
224 if (buffer_get_char(&m) == 0) {
225 pw = NULL;
226 goto out;
228 pw = buffer_get_string(&m, &len);
229 if (len != sizeof(struct passwd))
230 fatal("%s: struct passwd size mismatch", __func__);
231 pw->pw_name = buffer_get_string(&m, NULL);
232 pw->pw_passwd = buffer_get_string(&m, NULL);
233 pw->pw_gecos = buffer_get_string(&m, NULL);
234 #ifdef HAVE_PW_CLASS_IN_PASSWD
235 pw->pw_class = buffer_get_string(&m, NULL);
236 #endif
237 pw->pw_dir = buffer_get_string(&m, NULL);
238 pw->pw_shell = buffer_get_string(&m, NULL);
240 out:
241 /* copy options block as a Match directive may have changed some */
242 newopts = buffer_get_string(&m, &len);
243 if (len != sizeof(*newopts))
244 fatal("%s: option block size mismatch", __func__);
245 if (newopts->banner != NULL)
246 newopts->banner = buffer_get_string(&m, NULL);
247 copy_set_server_options(&options, newopts, 1);
248 xfree(newopts);
250 buffer_free(&m);
252 return (pw);
255 char *
256 mm_auth2_read_banner(void)
258 Buffer m;
259 char *banner;
261 debug3("%s entering", __func__);
263 buffer_init(&m);
264 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
265 buffer_clear(&m);
267 mm_request_receive_expect(pmonitor->m_recvfd,
268 MONITOR_ANS_AUTH2_READ_BANNER, &m);
269 banner = buffer_get_string(&m, NULL);
270 buffer_free(&m);
272 /* treat empty banner as missing banner */
273 if (strlen(banner) == 0) {
274 xfree(banner);
275 banner = NULL;
277 return (banner);
280 /* Inform the privileged process about service and style */
282 void
283 mm_inform_authserv(char *service, char *style)
285 Buffer m;
287 debug3("%s entering", __func__);
289 buffer_init(&m);
290 buffer_put_cstring(&m, service);
291 buffer_put_cstring(&m, style ? style : "");
293 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
295 buffer_free(&m);
298 /* Do the password authentication */
300 mm_auth_password(Authctxt *authctxt, char *password)
302 Buffer m;
303 int authenticated = 0;
305 debug3("%s entering", __func__);
307 buffer_init(&m);
308 buffer_put_cstring(&m, password);
309 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
311 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
312 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
314 authenticated = buffer_get_int(&m);
316 buffer_free(&m);
318 debug3("%s: user %sauthenticated",
319 __func__, authenticated ? "" : "not ");
320 return (authenticated);
324 mm_user_key_allowed(struct passwd *pw, Key *key)
326 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
330 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
331 Key *key)
333 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
337 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
338 char *host, Key *key)
340 int ret;
342 key->type = KEY_RSA; /* XXX hack for key_to_blob */
343 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
344 key->type = KEY_RSA1;
345 return (ret);
348 static void
349 mm_send_debug(Buffer *m)
351 char *msg;
353 while (buffer_len(m)) {
354 msg = buffer_get_string(m, NULL);
355 debug3("%s: Sending debug: %s", __func__, msg);
356 packet_send_debug("%s", msg);
357 xfree(msg);
362 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
364 Buffer m;
365 u_char *blob;
366 u_int len;
367 int allowed = 0, have_forced = 0;
369 debug3("%s entering", __func__);
371 /* Convert the key to a blob and the pass it over */
372 if (!key_to_blob(key, &blob, &len))
373 return (0);
375 buffer_init(&m);
376 buffer_put_int(&m, type);
377 buffer_put_cstring(&m, user ? user : "");
378 buffer_put_cstring(&m, host ? host : "");
379 buffer_put_string(&m, blob, len);
380 xfree(blob);
382 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
384 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
385 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
387 allowed = buffer_get_int(&m);
389 /* fake forced command */
390 auth_clear_options();
391 have_forced = buffer_get_int(&m);
392 forced_command = have_forced ? xstrdup("true") : NULL;
394 /* Send potential debug messages */
395 mm_send_debug(&m);
397 buffer_free(&m);
399 return (allowed);
403 * This key verify needs to send the key type along, because the
404 * privileged parent makes the decision if the key is allowed
405 * for authentication.
409 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
411 Buffer m;
412 u_char *blob;
413 u_int len;
414 int verified = 0;
416 debug3("%s entering", __func__);
418 /* Convert the key to a blob and the pass it over */
419 if (!key_to_blob(key, &blob, &len))
420 return (0);
422 buffer_init(&m);
423 buffer_put_string(&m, blob, len);
424 buffer_put_string(&m, sig, siglen);
425 buffer_put_string(&m, data, datalen);
426 xfree(blob);
428 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
430 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
431 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
433 verified = buffer_get_int(&m);
435 buffer_free(&m);
437 return (verified);
440 /* Export key state after authentication */
441 Newkeys *
442 mm_newkeys_from_blob(u_char *blob, int blen)
444 Buffer b;
445 u_int len;
446 Newkeys *newkey = NULL;
447 Enc *enc;
448 Mac *mac;
449 Comp *comp;
451 debug3("%s: %p(%d)", __func__, blob, blen);
452 #ifdef DEBUG_PK
453 dump_base64(stderr, blob, blen);
454 #endif
455 buffer_init(&b);
456 buffer_append(&b, blob, blen);
458 newkey = xmalloc(sizeof(*newkey));
459 enc = &newkey->enc;
460 mac = &newkey->mac;
461 comp = &newkey->comp;
463 /* Enc structure */
464 enc->name = buffer_get_string(&b, NULL);
465 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
466 enc->enabled = buffer_get_int(&b);
467 enc->block_size = buffer_get_int(&b);
468 enc->key = buffer_get_string(&b, &enc->key_len);
469 enc->iv = buffer_get_string(&b, &len);
470 if (len != enc->block_size)
471 fatal("%s: bad ivlen: expected %u != %u", __func__,
472 enc->block_size, len);
474 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
475 fatal("%s: bad cipher name %s or pointer %p", __func__,
476 enc->name, enc->cipher);
478 /* Mac structure */
479 mac->name = buffer_get_string(&b, NULL);
480 if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
481 fatal("%s: can not setup mac %s", __func__, mac->name);
482 mac->enabled = buffer_get_int(&b);
483 mac->key = buffer_get_string(&b, &len);
484 if (len > mac->key_len)
485 fatal("%s: bad mac key length: %u > %d", __func__, len,
486 mac->key_len);
487 mac->key_len = len;
489 /* Comp structure */
490 comp->type = buffer_get_int(&b);
491 comp->enabled = buffer_get_int(&b);
492 comp->name = buffer_get_string(&b, NULL);
494 len = buffer_len(&b);
495 if (len != 0)
496 error("newkeys_from_blob: remaining bytes in blob %u", len);
497 buffer_free(&b);
498 return (newkey);
502 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
504 Buffer b;
505 int len;
506 Enc *enc;
507 Mac *mac;
508 Comp *comp;
509 Newkeys *newkey = newkeys[mode];
511 debug3("%s: converting %p", __func__, newkey);
513 if (newkey == NULL) {
514 error("%s: newkey == NULL", __func__);
515 return 0;
517 enc = &newkey->enc;
518 mac = &newkey->mac;
519 comp = &newkey->comp;
521 buffer_init(&b);
522 /* Enc structure */
523 buffer_put_cstring(&b, enc->name);
524 /* The cipher struct is constant and shared, you export pointer */
525 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
526 buffer_put_int(&b, enc->enabled);
527 buffer_put_int(&b, enc->block_size);
528 buffer_put_string(&b, enc->key, enc->key_len);
529 packet_get_keyiv(mode, enc->iv, enc->block_size);
530 buffer_put_string(&b, enc->iv, enc->block_size);
532 /* Mac structure */
533 buffer_put_cstring(&b, mac->name);
534 buffer_put_int(&b, mac->enabled);
535 buffer_put_string(&b, mac->key, mac->key_len);
537 /* Comp structure */
538 buffer_put_int(&b, comp->type);
539 buffer_put_int(&b, comp->enabled);
540 buffer_put_cstring(&b, comp->name);
542 len = buffer_len(&b);
543 if (lenp != NULL)
544 *lenp = len;
545 if (blobp != NULL) {
546 *blobp = xmalloc(len);
547 memcpy(*blobp, buffer_ptr(&b), len);
549 memset(buffer_ptr(&b), 0, len);
550 buffer_free(&b);
551 return len;
554 static void
555 mm_send_kex(Buffer *m, Kex *kex)
557 buffer_put_string(m, kex->session_id, kex->session_id_len);
558 buffer_put_int(m, kex->we_need);
559 buffer_put_int(m, kex->hostkey_type);
560 buffer_put_int(m, kex->kex_type);
561 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
562 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
563 buffer_put_int(m, kex->flags);
564 buffer_put_cstring(m, kex->client_version_string);
565 buffer_put_cstring(m, kex->server_version_string);
568 void
569 mm_send_keystate(struct monitor *monitor)
571 Buffer m;
572 u_char *blob, *p;
573 u_int bloblen, plen;
574 u_int32_t seqnr, packets;
575 u_int64_t blocks;
577 buffer_init(&m);
579 if (!compat20) {
580 u_char iv[24];
581 u_char *key;
582 u_int ivlen, keylen;
584 buffer_put_int(&m, packet_get_protocol_flags());
586 buffer_put_int(&m, packet_get_ssh1_cipher());
588 debug3("%s: Sending ssh1 KEY+IV", __func__);
589 keylen = packet_get_encryption_key(NULL);
590 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
591 keylen = packet_get_encryption_key(key);
592 buffer_put_string(&m, key, keylen);
593 memset(key, 0, keylen);
594 xfree(key);
596 ivlen = packet_get_keyiv_len(MODE_OUT);
597 packet_get_keyiv(MODE_OUT, iv, ivlen);
598 buffer_put_string(&m, iv, ivlen);
599 ivlen = packet_get_keyiv_len(MODE_OUT);
600 packet_get_keyiv(MODE_IN, iv, ivlen);
601 buffer_put_string(&m, iv, ivlen);
602 goto skip;
603 } else {
604 /* Kex for rekeying */
605 mm_send_kex(&m, *monitor->m_pkex);
608 debug3("%s: Sending new keys: %p %p",
609 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
611 /* Keys from Kex */
612 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
613 fatal("%s: conversion of newkeys failed", __func__);
615 buffer_put_string(&m, blob, bloblen);
616 xfree(blob);
618 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
619 fatal("%s: conversion of newkeys failed", __func__);
621 buffer_put_string(&m, blob, bloblen);
622 xfree(blob);
624 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
625 buffer_put_int(&m, seqnr);
626 buffer_put_int64(&m, blocks);
627 buffer_put_int(&m, packets);
628 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
629 buffer_put_int(&m, seqnr);
630 buffer_put_int64(&m, blocks);
631 buffer_put_int(&m, packets);
633 debug3("%s: New keys have been sent", __func__);
634 skip:
635 /* More key context */
636 plen = packet_get_keycontext(MODE_OUT, NULL);
637 p = xmalloc(plen+1);
638 packet_get_keycontext(MODE_OUT, p);
639 buffer_put_string(&m, p, plen);
640 xfree(p);
642 plen = packet_get_keycontext(MODE_IN, NULL);
643 p = xmalloc(plen+1);
644 packet_get_keycontext(MODE_IN, p);
645 buffer_put_string(&m, p, plen);
646 xfree(p);
648 /* Compression state */
649 debug3("%s: Sending compression state", __func__);
650 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
651 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
653 /* Network I/O buffers */
654 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
655 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
657 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
658 debug3("%s: Finished sending state", __func__);
660 buffer_free(&m);
664 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
666 Buffer m;
667 char *p, *msg;
668 int success = 0;
670 buffer_init(&m);
671 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
673 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
674 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
676 success = buffer_get_int(&m);
677 if (success == 0) {
678 debug3("%s: pty alloc failed", __func__);
679 buffer_free(&m);
680 return (0);
682 p = buffer_get_string(&m, NULL);
683 msg = buffer_get_string(&m, NULL);
684 buffer_free(&m);
686 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
687 xfree(p);
689 buffer_append(&loginmsg, msg, strlen(msg));
690 xfree(msg);
692 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
693 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
694 fatal("%s: receive fds failed", __func__);
696 /* Success */
697 return (1);
700 void
701 mm_session_pty_cleanup2(Session *s)
703 Buffer m;
705 if (s->ttyfd == -1)
706 return;
707 buffer_init(&m);
708 buffer_put_cstring(&m, s->tty);
709 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
710 buffer_free(&m);
712 /* closed dup'ed master */
713 if (close(s->ptymaster) < 0)
714 error("close(s->ptymaster): %s", strerror(errno));
716 /* unlink pty from session */
717 s->ttyfd = -1;
720 #ifdef USE_PAM
721 void
722 mm_start_pam(Authctxt *authctxt)
724 Buffer m;
726 debug3("%s entering", __func__);
727 if (!options.use_pam)
728 fatal("UsePAM=no, but ended up in %s anyway", __func__);
730 buffer_init(&m);
731 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
733 buffer_free(&m);
736 u_int
737 mm_do_pam_account(void)
739 Buffer m;
740 u_int ret;
741 char *msg;
743 debug3("%s entering", __func__);
744 if (!options.use_pam)
745 fatal("UsePAM=no, but ended up in %s anyway", __func__);
747 buffer_init(&m);
748 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
750 mm_request_receive_expect(pmonitor->m_recvfd,
751 MONITOR_ANS_PAM_ACCOUNT, &m);
752 ret = buffer_get_int(&m);
753 msg = buffer_get_string(&m, NULL);
754 buffer_append(&loginmsg, msg, strlen(msg));
755 xfree(msg);
757 buffer_free(&m);
759 debug3("%s returning %d", __func__, ret);
761 return (ret);
764 void *
765 mm_sshpam_init_ctx(Authctxt *authctxt)
767 Buffer m;
768 int success;
770 debug3("%s", __func__);
771 buffer_init(&m);
772 buffer_put_cstring(&m, authctxt->user);
773 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
774 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
775 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
776 success = buffer_get_int(&m);
777 if (success == 0) {
778 debug3("%s: pam_init_ctx failed", __func__);
779 buffer_free(&m);
780 return (NULL);
782 buffer_free(&m);
783 return (authctxt);
787 mm_sshpam_query(void *ctx, char **name, char **info,
788 u_int *num, char ***prompts, u_int **echo_on)
790 Buffer m;
791 u_int i;
792 int ret;
794 debug3("%s", __func__);
795 buffer_init(&m);
796 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
797 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
798 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
799 ret = buffer_get_int(&m);
800 debug3("%s: pam_query returned %d", __func__, ret);
801 *name = buffer_get_string(&m, NULL);
802 *info = buffer_get_string(&m, NULL);
803 *num = buffer_get_int(&m);
804 if (*num > PAM_MAX_NUM_MSG)
805 fatal("%s: recieved %u PAM messages, expected <= %u",
806 __func__, *num, PAM_MAX_NUM_MSG);
807 *prompts = xcalloc((*num + 1), sizeof(char *));
808 *echo_on = xcalloc((*num + 1), sizeof(u_int));
809 for (i = 0; i < *num; ++i) {
810 (*prompts)[i] = buffer_get_string(&m, NULL);
811 (*echo_on)[i] = buffer_get_int(&m);
813 buffer_free(&m);
814 return (ret);
818 mm_sshpam_respond(void *ctx, u_int num, char **resp)
820 Buffer m;
821 u_int i;
822 int ret;
824 debug3("%s", __func__);
825 buffer_init(&m);
826 buffer_put_int(&m, num);
827 for (i = 0; i < num; ++i)
828 buffer_put_cstring(&m, resp[i]);
829 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
830 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
831 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
832 ret = buffer_get_int(&m);
833 debug3("%s: pam_respond returned %d", __func__, ret);
834 buffer_free(&m);
835 return (ret);
838 void
839 mm_sshpam_free_ctx(void *ctxtp)
841 Buffer m;
843 debug3("%s", __func__);
844 buffer_init(&m);
845 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
846 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
847 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
848 buffer_free(&m);
850 #endif /* USE_PAM */
852 /* Request process termination */
854 void
855 mm_terminate(void)
857 Buffer m;
859 buffer_init(&m);
860 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
861 buffer_free(&m);
865 mm_ssh1_session_key(BIGNUM *num)
867 int rsafail;
868 Buffer m;
870 buffer_init(&m);
871 buffer_put_bignum2(&m, num);
872 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
874 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
876 rsafail = buffer_get_int(&m);
877 buffer_get_bignum2(&m, num);
879 buffer_free(&m);
881 return (rsafail);
884 static void
885 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
886 char ***prompts, u_int **echo_on)
888 *name = xstrdup("");
889 *infotxt = xstrdup("");
890 *numprompts = 1;
891 *prompts = xcalloc(*numprompts, sizeof(char *));
892 *echo_on = xcalloc(*numprompts, sizeof(u_int));
893 (*echo_on)[0] = 0;
897 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
898 u_int *numprompts, char ***prompts, u_int **echo_on)
900 Buffer m;
901 u_int success;
902 char *challenge;
904 debug3("%s: entering", __func__);
906 buffer_init(&m);
907 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
909 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
910 &m);
911 success = buffer_get_int(&m);
912 if (success == 0) {
913 debug3("%s: no challenge", __func__);
914 buffer_free(&m);
915 return (-1);
918 /* Get the challenge, and format the response */
919 challenge = buffer_get_string(&m, NULL);
920 buffer_free(&m);
922 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
923 (*prompts)[0] = challenge;
925 debug3("%s: received challenge: %s", __func__, challenge);
927 return (0);
931 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
933 Buffer m;
934 int authok;
936 debug3("%s: entering", __func__);
937 if (numresponses != 1)
938 return (-1);
940 buffer_init(&m);
941 buffer_put_cstring(&m, responses[0]);
942 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
944 mm_request_receive_expect(pmonitor->m_recvfd,
945 MONITOR_ANS_BSDAUTHRESPOND, &m);
947 authok = buffer_get_int(&m);
948 buffer_free(&m);
950 return ((authok == 0) ? -1 : 0);
953 #ifdef SKEY
955 mm_skey_query(void *ctx, char **name, char **infotxt,
956 u_int *numprompts, char ***prompts, u_int **echo_on)
958 Buffer m;
959 u_int success;
960 char *challenge;
962 debug3("%s: entering", __func__);
964 buffer_init(&m);
965 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
967 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
968 &m);
969 success = buffer_get_int(&m);
970 if (success == 0) {
971 debug3("%s: no challenge", __func__);
972 buffer_free(&m);
973 return (-1);
976 /* Get the challenge, and format the response */
977 challenge = buffer_get_string(&m, NULL);
978 buffer_free(&m);
980 debug3("%s: received challenge: %s", __func__, challenge);
982 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
984 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
985 xfree(challenge);
987 return (0);
991 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
993 Buffer m;
994 int authok;
996 debug3("%s: entering", __func__);
997 if (numresponses != 1)
998 return (-1);
1000 buffer_init(&m);
1001 buffer_put_cstring(&m, responses[0]);
1002 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
1004 mm_request_receive_expect(pmonitor->m_recvfd,
1005 MONITOR_ANS_SKEYRESPOND, &m);
1007 authok = buffer_get_int(&m);
1008 buffer_free(&m);
1010 return ((authok == 0) ? -1 : 0);
1012 #endif /* SKEY */
1014 void
1015 mm_ssh1_session_id(u_char session_id[16])
1017 Buffer m;
1018 int i;
1020 debug3("%s entering", __func__);
1022 buffer_init(&m);
1023 for (i = 0; i < 16; i++)
1024 buffer_put_char(&m, session_id[i]);
1026 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1027 buffer_free(&m);
1031 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1033 Buffer m;
1034 Key *key;
1035 u_char *blob;
1036 u_int blen;
1037 int allowed = 0, have_forced = 0;
1039 debug3("%s entering", __func__);
1041 buffer_init(&m);
1042 buffer_put_bignum2(&m, client_n);
1044 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1045 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1047 allowed = buffer_get_int(&m);
1049 /* fake forced command */
1050 auth_clear_options();
1051 have_forced = buffer_get_int(&m);
1052 forced_command = have_forced ? xstrdup("true") : NULL;
1054 if (allowed && rkey != NULL) {
1055 blob = buffer_get_string(&m, &blen);
1056 if ((key = key_from_blob(blob, blen)) == NULL)
1057 fatal("%s: key_from_blob failed", __func__);
1058 *rkey = key;
1059 xfree(blob);
1061 mm_send_debug(&m);
1062 buffer_free(&m);
1064 return (allowed);
1067 BIGNUM *
1068 mm_auth_rsa_generate_challenge(Key *key)
1070 Buffer m;
1071 BIGNUM *challenge;
1072 u_char *blob;
1073 u_int blen;
1075 debug3("%s entering", __func__);
1077 if ((challenge = BN_new()) == NULL)
1078 fatal("%s: BN_new failed", __func__);
1080 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1081 if (key_to_blob(key, &blob, &blen) == 0)
1082 fatal("%s: key_to_blob failed", __func__);
1083 key->type = KEY_RSA1;
1085 buffer_init(&m);
1086 buffer_put_string(&m, blob, blen);
1087 xfree(blob);
1089 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1090 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1092 buffer_get_bignum2(&m, challenge);
1093 buffer_free(&m);
1095 return (challenge);
1099 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1101 Buffer m;
1102 u_char *blob;
1103 u_int blen;
1104 int success = 0;
1106 debug3("%s entering", __func__);
1108 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1109 if (key_to_blob(key, &blob, &blen) == 0)
1110 fatal("%s: key_to_blob failed", __func__);
1111 key->type = KEY_RSA1;
1113 buffer_init(&m);
1114 buffer_put_string(&m, blob, blen);
1115 buffer_put_string(&m, response, 16);
1116 xfree(blob);
1118 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1119 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1121 success = buffer_get_int(&m);
1122 buffer_free(&m);
1124 return (success);
1127 #ifdef SSH_AUDIT_EVENTS
1128 void
1129 mm_audit_event(ssh_audit_event_t event)
1131 Buffer m;
1133 debug3("%s entering", __func__);
1135 buffer_init(&m);
1136 buffer_put_int(&m, event);
1138 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1139 buffer_free(&m);
1142 void
1143 mm_audit_run_command(const char *command)
1145 Buffer m;
1147 debug3("%s entering command %s", __func__, command);
1149 buffer_init(&m);
1150 buffer_put_cstring(&m, command);
1152 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1153 buffer_free(&m);
1155 #endif /* SSH_AUDIT_EVENTS */
1157 #ifdef GSSAPI
1158 OM_uint32
1159 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1161 Buffer m;
1162 OM_uint32 major;
1164 /* Client doesn't get to see the context */
1165 *ctx = NULL;
1167 buffer_init(&m);
1168 buffer_put_string(&m, goid->elements, goid->length);
1170 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1171 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1173 major = buffer_get_int(&m);
1175 buffer_free(&m);
1176 return (major);
1179 OM_uint32
1180 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1181 gss_buffer_desc *out, OM_uint32 *flags)
1183 Buffer m;
1184 OM_uint32 major;
1185 u_int len;
1187 buffer_init(&m);
1188 buffer_put_string(&m, in->value, in->length);
1190 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1191 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1193 major = buffer_get_int(&m);
1194 out->value = buffer_get_string(&m, &len);
1195 out->length = len;
1196 if (flags)
1197 *flags = buffer_get_int(&m);
1199 buffer_free(&m);
1201 return (major);
1204 OM_uint32
1205 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1207 Buffer m;
1208 OM_uint32 major;
1210 buffer_init(&m);
1211 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1212 buffer_put_string(&m, gssmic->value, gssmic->length);
1214 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1215 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1216 &m);
1218 major = buffer_get_int(&m);
1219 buffer_free(&m);
1220 return(major);
1224 mm_ssh_gssapi_userok(char *user)
1226 Buffer m;
1227 int authenticated = 0;
1229 buffer_init(&m);
1231 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1232 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1233 &m);
1235 authenticated = buffer_get_int(&m);
1237 buffer_free(&m);
1238 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1239 return (authenticated);
1241 #endif /* GSSAPI */