- (djm) OpenBSD CVS Sync
[openssh-git.git] / monitor_wrap.c
blob431b3e495415f5f631b7654222e7f1eee4bbf65e
1 /* $OpenBSD: monitor_wrap.c,v 1.54 2006/08/12 20:46:46 miod Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
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 <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include <openssl/bn.h>
41 #include <openssl/dh.h>
43 #include "xmalloc.h"
44 #include "ssh.h"
45 #include "dh.h"
46 #include "buffer.h"
47 #include "key.h"
48 #include "cipher.h"
49 #include "kex.h"
50 #include "hostfile.h"
51 #include "auth.h"
52 #include "auth-options.h"
53 #include "packet.h"
54 #include "mac.h"
55 #include "log.h"
56 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
57 #undef TARGET_OS_MAC
58 #include "zlib.h"
59 #define TARGET_OS_MAC 1
60 #else
61 #include "zlib.h"
62 #endif
63 #include "monitor.h"
64 #ifdef GSSAPI
65 #include "ssh-gss.h"
66 #endif
67 #include "monitor_wrap.h"
68 #include "atomicio.h"
69 #include "monitor_fdpass.h"
70 #include "misc.h"
71 #include "servconf.h"
73 #include "channels.h"
74 #include "session.h"
76 /* Imports */
77 extern int compat20;
78 extern Newkeys *newkeys[];
79 extern z_stream incoming_stream;
80 extern z_stream outgoing_stream;
81 extern struct monitor *pmonitor;
82 extern Buffer input, output;
83 extern Buffer loginmsg;
84 extern ServerOptions options;
86 int
87 mm_is_monitor(void)
90 * m_pid is only set in the privileged part, and
91 * points to the unprivileged child.
93 return (pmonitor && pmonitor->m_pid > 0);
96 void
97 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
99 u_int mlen = buffer_len(m);
100 u_char buf[5];
102 debug3("%s entering: type %d", __func__, type);
104 put_u32(buf, mlen + 1);
105 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
106 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
107 fatal("%s: write: %s", __func__, strerror(errno));
108 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
109 fatal("%s: write: %s", __func__, strerror(errno));
112 void
113 mm_request_receive(int sock, Buffer *m)
115 u_char buf[4];
116 u_int msg_len;
118 debug3("%s entering", __func__);
120 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
121 if (errno == EPIPE)
122 cleanup_exit(255);
123 fatal("%s: read: %s", __func__, strerror(errno));
125 msg_len = get_u32(buf);
126 if (msg_len > 256 * 1024)
127 fatal("%s: read: bad msg_len %d", __func__, msg_len);
128 buffer_clear(m);
129 buffer_append_space(m, msg_len);
130 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
131 fatal("%s: read: %s", __func__, strerror(errno));
134 void
135 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
137 u_char rtype;
139 debug3("%s entering: type %d", __func__, type);
141 mm_request_receive(sock, m);
142 rtype = buffer_get_char(m);
143 if (rtype != type)
144 fatal("%s: read: rtype %d != type %d", __func__,
145 rtype, type);
148 DH *
149 mm_choose_dh(int min, int nbits, int max)
151 BIGNUM *p, *g;
152 int success = 0;
153 Buffer m;
155 buffer_init(&m);
156 buffer_put_int(&m, min);
157 buffer_put_int(&m, nbits);
158 buffer_put_int(&m, max);
160 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
162 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
163 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
165 success = buffer_get_char(&m);
166 if (success == 0)
167 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
169 if ((p = BN_new()) == NULL)
170 fatal("%s: BN_new failed", __func__);
171 if ((g = BN_new()) == NULL)
172 fatal("%s: BN_new failed", __func__);
173 buffer_get_bignum2(&m, p);
174 buffer_get_bignum2(&m, g);
176 debug3("%s: remaining %d", __func__, buffer_len(&m));
177 buffer_free(&m);
179 return (dh_new_group(g, p));
183 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
185 Kex *kex = *pmonitor->m_pkex;
186 Buffer m;
188 debug3("%s entering", __func__);
190 buffer_init(&m);
191 buffer_put_int(&m, kex->host_key_index(key));
192 buffer_put_string(&m, data, datalen);
194 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
196 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
197 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
198 *sigp = buffer_get_string(&m, lenp);
199 buffer_free(&m);
201 return (0);
204 struct passwd *
205 mm_getpwnamallow(const char *username)
207 Buffer m;
208 struct passwd *pw;
209 u_int pwlen;
211 debug3("%s entering", __func__);
213 buffer_init(&m);
214 buffer_put_cstring(&m, username);
216 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
218 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
219 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
221 if (buffer_get_char(&m) == 0) {
222 buffer_free(&m);
223 return (NULL);
225 pw = buffer_get_string(&m, &pwlen);
226 if (pwlen != sizeof(struct passwd))
227 fatal("%s: struct passwd size mismatch", __func__);
228 pw->pw_name = buffer_get_string(&m, NULL);
229 pw->pw_passwd = buffer_get_string(&m, NULL);
230 pw->pw_gecos = buffer_get_string(&m, NULL);
231 #ifdef HAVE_PW_CLASS_IN_PASSWD
232 pw->pw_class = buffer_get_string(&m, NULL);
233 #endif
234 pw->pw_dir = buffer_get_string(&m, NULL);
235 pw->pw_shell = buffer_get_string(&m, NULL);
236 buffer_free(&m);
238 return (pw);
241 char *
242 mm_auth2_read_banner(void)
244 Buffer m;
245 char *banner;
247 debug3("%s entering", __func__);
249 buffer_init(&m);
250 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
251 buffer_clear(&m);
253 mm_request_receive_expect(pmonitor->m_recvfd,
254 MONITOR_ANS_AUTH2_READ_BANNER, &m);
255 banner = buffer_get_string(&m, NULL);
256 buffer_free(&m);
258 /* treat empty banner as missing banner */
259 if (strlen(banner) == 0) {
260 xfree(banner);
261 banner = NULL;
263 return (banner);
266 /* Inform the privileged process about service and style */
268 void
269 mm_inform_authserv(char *service, char *style)
271 Buffer m;
273 debug3("%s entering", __func__);
275 buffer_init(&m);
276 buffer_put_cstring(&m, service);
277 buffer_put_cstring(&m, style ? style : "");
279 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
281 buffer_free(&m);
284 /* Do the password authentication */
286 mm_auth_password(Authctxt *authctxt, char *password)
288 Buffer m;
289 int authenticated = 0;
291 debug3("%s entering", __func__);
293 buffer_init(&m);
294 buffer_put_cstring(&m, password);
295 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
297 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
298 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
300 authenticated = buffer_get_int(&m);
302 buffer_free(&m);
304 debug3("%s: user %sauthenticated",
305 __func__, authenticated ? "" : "not ");
306 return (authenticated);
310 mm_user_key_allowed(struct passwd *pw, Key *key)
312 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
316 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
317 Key *key)
319 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
323 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
324 char *host, Key *key)
326 int ret;
328 key->type = KEY_RSA; /* XXX hack for key_to_blob */
329 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
330 key->type = KEY_RSA1;
331 return (ret);
334 static void
335 mm_send_debug(Buffer *m)
337 char *msg;
339 while (buffer_len(m)) {
340 msg = buffer_get_string(m, NULL);
341 debug3("%s: Sending debug: %s", __func__, msg);
342 packet_send_debug("%s", msg);
343 xfree(msg);
348 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
350 Buffer m;
351 u_char *blob;
352 u_int len;
353 int allowed = 0, have_forced = 0;
355 debug3("%s entering", __func__);
357 /* Convert the key to a blob and the pass it over */
358 if (!key_to_blob(key, &blob, &len))
359 return (0);
361 buffer_init(&m);
362 buffer_put_int(&m, type);
363 buffer_put_cstring(&m, user ? user : "");
364 buffer_put_cstring(&m, host ? host : "");
365 buffer_put_string(&m, blob, len);
366 xfree(blob);
368 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
370 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
371 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
373 allowed = buffer_get_int(&m);
375 /* fake forced command */
376 auth_clear_options();
377 have_forced = buffer_get_int(&m);
378 forced_command = have_forced ? xstrdup("true") : NULL;
380 /* Send potential debug messages */
381 mm_send_debug(&m);
383 buffer_free(&m);
385 return (allowed);
389 * This key verify needs to send the key type along, because the
390 * privileged parent makes the decision if the key is allowed
391 * for authentication.
395 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
397 Buffer m;
398 u_char *blob;
399 u_int len;
400 int verified = 0;
402 debug3("%s entering", __func__);
404 /* Convert the key to a blob and the pass it over */
405 if (!key_to_blob(key, &blob, &len))
406 return (0);
408 buffer_init(&m);
409 buffer_put_string(&m, blob, len);
410 buffer_put_string(&m, sig, siglen);
411 buffer_put_string(&m, data, datalen);
412 xfree(blob);
414 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
416 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
417 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
419 verified = buffer_get_int(&m);
421 buffer_free(&m);
423 return (verified);
426 /* Export key state after authentication */
427 Newkeys *
428 mm_newkeys_from_blob(u_char *blob, int blen)
430 Buffer b;
431 u_int len;
432 Newkeys *newkey = NULL;
433 Enc *enc;
434 Mac *mac;
435 Comp *comp;
437 debug3("%s: %p(%d)", __func__, blob, blen);
438 #ifdef DEBUG_PK
439 dump_base64(stderr, blob, blen);
440 #endif
441 buffer_init(&b);
442 buffer_append(&b, blob, blen);
444 newkey = xmalloc(sizeof(*newkey));
445 enc = &newkey->enc;
446 mac = &newkey->mac;
447 comp = &newkey->comp;
449 /* Enc structure */
450 enc->name = buffer_get_string(&b, NULL);
451 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
452 enc->enabled = buffer_get_int(&b);
453 enc->block_size = buffer_get_int(&b);
454 enc->key = buffer_get_string(&b, &enc->key_len);
455 enc->iv = buffer_get_string(&b, &len);
456 if (len != enc->block_size)
457 fatal("%s: bad ivlen: expected %u != %u", __func__,
458 enc->block_size, len);
460 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
461 fatal("%s: bad cipher name %s or pointer %p", __func__,
462 enc->name, enc->cipher);
464 /* Mac structure */
465 mac->name = buffer_get_string(&b, NULL);
466 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
467 fatal("%s: can not init mac %s", __func__, mac->name);
468 mac->enabled = buffer_get_int(&b);
469 mac->key = buffer_get_string(&b, &len);
470 if (len > mac->key_len)
471 fatal("%s: bad mac key length: %u > %d", __func__, len,
472 mac->key_len);
473 mac->key_len = len;
475 /* Comp structure */
476 comp->type = buffer_get_int(&b);
477 comp->enabled = buffer_get_int(&b);
478 comp->name = buffer_get_string(&b, NULL);
480 len = buffer_len(&b);
481 if (len != 0)
482 error("newkeys_from_blob: remaining bytes in blob %u", len);
483 buffer_free(&b);
484 return (newkey);
488 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
490 Buffer b;
491 int len;
492 Enc *enc;
493 Mac *mac;
494 Comp *comp;
495 Newkeys *newkey = newkeys[mode];
497 debug3("%s: converting %p", __func__, newkey);
499 if (newkey == NULL) {
500 error("%s: newkey == NULL", __func__);
501 return 0;
503 enc = &newkey->enc;
504 mac = &newkey->mac;
505 comp = &newkey->comp;
507 buffer_init(&b);
508 /* Enc structure */
509 buffer_put_cstring(&b, enc->name);
510 /* The cipher struct is constant and shared, you export pointer */
511 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
512 buffer_put_int(&b, enc->enabled);
513 buffer_put_int(&b, enc->block_size);
514 buffer_put_string(&b, enc->key, enc->key_len);
515 packet_get_keyiv(mode, enc->iv, enc->block_size);
516 buffer_put_string(&b, enc->iv, enc->block_size);
518 /* Mac structure */
519 buffer_put_cstring(&b, mac->name);
520 buffer_put_int(&b, mac->enabled);
521 buffer_put_string(&b, mac->key, mac->key_len);
523 /* Comp structure */
524 buffer_put_int(&b, comp->type);
525 buffer_put_int(&b, comp->enabled);
526 buffer_put_cstring(&b, comp->name);
528 len = buffer_len(&b);
529 if (lenp != NULL)
530 *lenp = len;
531 if (blobp != NULL) {
532 *blobp = xmalloc(len);
533 memcpy(*blobp, buffer_ptr(&b), len);
535 memset(buffer_ptr(&b), 0, len);
536 buffer_free(&b);
537 return len;
540 static void
541 mm_send_kex(Buffer *m, Kex *kex)
543 buffer_put_string(m, kex->session_id, kex->session_id_len);
544 buffer_put_int(m, kex->we_need);
545 buffer_put_int(m, kex->hostkey_type);
546 buffer_put_int(m, kex->kex_type);
547 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
548 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
549 buffer_put_int(m, kex->flags);
550 buffer_put_cstring(m, kex->client_version_string);
551 buffer_put_cstring(m, kex->server_version_string);
554 void
555 mm_send_keystate(struct monitor *monitor)
557 Buffer m;
558 u_char *blob, *p;
559 u_int bloblen, plen;
560 u_int32_t seqnr, packets;
561 u_int64_t blocks;
563 buffer_init(&m);
565 if (!compat20) {
566 u_char iv[24];
567 u_char *key;
568 u_int ivlen, keylen;
570 buffer_put_int(&m, packet_get_protocol_flags());
572 buffer_put_int(&m, packet_get_ssh1_cipher());
574 debug3("%s: Sending ssh1 KEY+IV", __func__);
575 keylen = packet_get_encryption_key(NULL);
576 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
577 keylen = packet_get_encryption_key(key);
578 buffer_put_string(&m, key, keylen);
579 memset(key, 0, keylen);
580 xfree(key);
582 ivlen = packet_get_keyiv_len(MODE_OUT);
583 packet_get_keyiv(MODE_OUT, iv, ivlen);
584 buffer_put_string(&m, iv, ivlen);
585 ivlen = packet_get_keyiv_len(MODE_OUT);
586 packet_get_keyiv(MODE_IN, iv, ivlen);
587 buffer_put_string(&m, iv, ivlen);
588 goto skip;
589 } else {
590 /* Kex for rekeying */
591 mm_send_kex(&m, *monitor->m_pkex);
594 debug3("%s: Sending new keys: %p %p",
595 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
597 /* Keys from Kex */
598 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
599 fatal("%s: conversion of newkeys failed", __func__);
601 buffer_put_string(&m, blob, bloblen);
602 xfree(blob);
604 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
605 fatal("%s: conversion of newkeys failed", __func__);
607 buffer_put_string(&m, blob, bloblen);
608 xfree(blob);
610 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
611 buffer_put_int(&m, seqnr);
612 buffer_put_int64(&m, blocks);
613 buffer_put_int(&m, packets);
614 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
615 buffer_put_int(&m, seqnr);
616 buffer_put_int64(&m, blocks);
617 buffer_put_int(&m, packets);
619 debug3("%s: New keys have been sent", __func__);
620 skip:
621 /* More key context */
622 plen = packet_get_keycontext(MODE_OUT, NULL);
623 p = xmalloc(plen+1);
624 packet_get_keycontext(MODE_OUT, p);
625 buffer_put_string(&m, p, plen);
626 xfree(p);
628 plen = packet_get_keycontext(MODE_IN, NULL);
629 p = xmalloc(plen+1);
630 packet_get_keycontext(MODE_IN, p);
631 buffer_put_string(&m, p, plen);
632 xfree(p);
634 /* Compression state */
635 debug3("%s: Sending compression state", __func__);
636 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
637 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
639 /* Network I/O buffers */
640 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
641 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
643 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
644 debug3("%s: Finished sending state", __func__);
646 buffer_free(&m);
650 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
652 Buffer m;
653 char *p, *msg;
654 int success = 0;
656 buffer_init(&m);
657 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
659 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
660 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
662 success = buffer_get_int(&m);
663 if (success == 0) {
664 debug3("%s: pty alloc failed", __func__);
665 buffer_free(&m);
666 return (0);
668 p = buffer_get_string(&m, NULL);
669 msg = buffer_get_string(&m, NULL);
670 buffer_free(&m);
672 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
673 xfree(p);
675 buffer_append(&loginmsg, msg, strlen(msg));
676 xfree(msg);
678 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
679 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
681 /* Success */
682 return (1);
685 void
686 mm_session_pty_cleanup2(Session *s)
688 Buffer m;
690 if (s->ttyfd == -1)
691 return;
692 buffer_init(&m);
693 buffer_put_cstring(&m, s->tty);
694 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
695 buffer_free(&m);
697 /* closed dup'ed master */
698 if (close(s->ptymaster) < 0)
699 error("close(s->ptymaster): %s", strerror(errno));
701 /* unlink pty from session */
702 s->ttyfd = -1;
705 #ifdef USE_PAM
706 void
707 mm_start_pam(Authctxt *authctxt)
709 Buffer m;
711 debug3("%s entering", __func__);
712 if (!options.use_pam)
713 fatal("UsePAM=no, but ended up in %s anyway", __func__);
715 buffer_init(&m);
716 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
718 buffer_free(&m);
721 u_int
722 mm_do_pam_account(void)
724 Buffer m;
725 u_int ret;
726 char *msg;
728 debug3("%s entering", __func__);
729 if (!options.use_pam)
730 fatal("UsePAM=no, but ended up in %s anyway", __func__);
732 buffer_init(&m);
733 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
735 mm_request_receive_expect(pmonitor->m_recvfd,
736 MONITOR_ANS_PAM_ACCOUNT, &m);
737 ret = buffer_get_int(&m);
738 msg = buffer_get_string(&m, NULL);
739 buffer_append(&loginmsg, msg, strlen(msg));
740 xfree(msg);
742 buffer_free(&m);
744 debug3("%s returning %d", __func__, ret);
746 return (ret);
749 void *
750 mm_sshpam_init_ctx(Authctxt *authctxt)
752 Buffer m;
753 int success;
755 debug3("%s", __func__);
756 buffer_init(&m);
757 buffer_put_cstring(&m, authctxt->user);
758 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
759 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
760 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
761 success = buffer_get_int(&m);
762 if (success == 0) {
763 debug3("%s: pam_init_ctx failed", __func__);
764 buffer_free(&m);
765 return (NULL);
767 buffer_free(&m);
768 return (authctxt);
772 mm_sshpam_query(void *ctx, char **name, char **info,
773 u_int *num, char ***prompts, u_int **echo_on)
775 Buffer m;
776 u_int i;
777 int ret;
779 debug3("%s", __func__);
780 buffer_init(&m);
781 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
782 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
783 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
784 ret = buffer_get_int(&m);
785 debug3("%s: pam_query returned %d", __func__, ret);
786 *name = buffer_get_string(&m, NULL);
787 *info = buffer_get_string(&m, NULL);
788 *num = buffer_get_int(&m);
789 if (*num > PAM_MAX_NUM_MSG)
790 fatal("%s: recieved %u PAM messages, expected <= %u",
791 __func__, *num, PAM_MAX_NUM_MSG);
792 *prompts = xcalloc((*num + 1), sizeof(char *));
793 *echo_on = xcalloc((*num + 1), sizeof(u_int));
794 for (i = 0; i < *num; ++i) {
795 (*prompts)[i] = buffer_get_string(&m, NULL);
796 (*echo_on)[i] = buffer_get_int(&m);
798 buffer_free(&m);
799 return (ret);
803 mm_sshpam_respond(void *ctx, u_int num, char **resp)
805 Buffer m;
806 u_int i;
807 int ret;
809 debug3("%s", __func__);
810 buffer_init(&m);
811 buffer_put_int(&m, num);
812 for (i = 0; i < num; ++i)
813 buffer_put_cstring(&m, resp[i]);
814 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
815 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
816 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
817 ret = buffer_get_int(&m);
818 debug3("%s: pam_respond returned %d", __func__, ret);
819 buffer_free(&m);
820 return (ret);
823 void
824 mm_sshpam_free_ctx(void *ctxtp)
826 Buffer m;
828 debug3("%s", __func__);
829 buffer_init(&m);
830 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
831 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
832 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
833 buffer_free(&m);
835 #endif /* USE_PAM */
837 /* Request process termination */
839 void
840 mm_terminate(void)
842 Buffer m;
844 buffer_init(&m);
845 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
846 buffer_free(&m);
850 mm_ssh1_session_key(BIGNUM *num)
852 int rsafail;
853 Buffer m;
855 buffer_init(&m);
856 buffer_put_bignum2(&m, num);
857 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
859 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
861 rsafail = buffer_get_int(&m);
862 buffer_get_bignum2(&m, num);
864 buffer_free(&m);
866 return (rsafail);
869 static void
870 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
871 char ***prompts, u_int **echo_on)
873 *name = xstrdup("");
874 *infotxt = xstrdup("");
875 *numprompts = 1;
876 *prompts = xcalloc(*numprompts, sizeof(char *));
877 *echo_on = xcalloc(*numprompts, sizeof(u_int));
878 (*echo_on)[0] = 0;
882 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
883 u_int *numprompts, char ***prompts, u_int **echo_on)
885 Buffer m;
886 u_int success;
887 char *challenge;
889 debug3("%s: entering", __func__);
891 buffer_init(&m);
892 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
894 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
895 &m);
896 success = buffer_get_int(&m);
897 if (success == 0) {
898 debug3("%s: no challenge", __func__);
899 buffer_free(&m);
900 return (-1);
903 /* Get the challenge, and format the response */
904 challenge = buffer_get_string(&m, NULL);
905 buffer_free(&m);
907 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
908 (*prompts)[0] = challenge;
910 debug3("%s: received challenge: %s", __func__, challenge);
912 return (0);
916 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
918 Buffer m;
919 int authok;
921 debug3("%s: entering", __func__);
922 if (numresponses != 1)
923 return (-1);
925 buffer_init(&m);
926 buffer_put_cstring(&m, responses[0]);
927 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
929 mm_request_receive_expect(pmonitor->m_recvfd,
930 MONITOR_ANS_BSDAUTHRESPOND, &m);
932 authok = buffer_get_int(&m);
933 buffer_free(&m);
935 return ((authok == 0) ? -1 : 0);
938 #ifdef SKEY
940 mm_skey_query(void *ctx, char **name, char **infotxt,
941 u_int *numprompts, char ***prompts, u_int **echo_on)
943 Buffer m;
944 u_int success;
945 char *challenge;
947 debug3("%s: entering", __func__);
949 buffer_init(&m);
950 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
952 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
953 &m);
954 success = buffer_get_int(&m);
955 if (success == 0) {
956 debug3("%s: no challenge", __func__);
957 buffer_free(&m);
958 return (-1);
961 /* Get the challenge, and format the response */
962 challenge = buffer_get_string(&m, NULL);
963 buffer_free(&m);
965 debug3("%s: received challenge: %s", __func__, challenge);
967 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
969 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
970 xfree(challenge);
972 return (0);
976 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
978 Buffer m;
979 int authok;
981 debug3("%s: entering", __func__);
982 if (numresponses != 1)
983 return (-1);
985 buffer_init(&m);
986 buffer_put_cstring(&m, responses[0]);
987 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
989 mm_request_receive_expect(pmonitor->m_recvfd,
990 MONITOR_ANS_SKEYRESPOND, &m);
992 authok = buffer_get_int(&m);
993 buffer_free(&m);
995 return ((authok == 0) ? -1 : 0);
997 #endif /* SKEY */
999 void
1000 mm_ssh1_session_id(u_char session_id[16])
1002 Buffer m;
1003 int i;
1005 debug3("%s entering", __func__);
1007 buffer_init(&m);
1008 for (i = 0; i < 16; i++)
1009 buffer_put_char(&m, session_id[i]);
1011 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1012 buffer_free(&m);
1016 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1018 Buffer m;
1019 Key *key;
1020 u_char *blob;
1021 u_int blen;
1022 int allowed = 0, have_forced = 0;
1024 debug3("%s entering", __func__);
1026 buffer_init(&m);
1027 buffer_put_bignum2(&m, client_n);
1029 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1030 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1032 allowed = buffer_get_int(&m);
1034 /* fake forced command */
1035 auth_clear_options();
1036 have_forced = buffer_get_int(&m);
1037 forced_command = have_forced ? xstrdup("true") : NULL;
1039 if (allowed && rkey != NULL) {
1040 blob = buffer_get_string(&m, &blen);
1041 if ((key = key_from_blob(blob, blen)) == NULL)
1042 fatal("%s: key_from_blob failed", __func__);
1043 *rkey = key;
1044 xfree(blob);
1046 mm_send_debug(&m);
1047 buffer_free(&m);
1049 return (allowed);
1052 BIGNUM *
1053 mm_auth_rsa_generate_challenge(Key *key)
1055 Buffer m;
1056 BIGNUM *challenge;
1057 u_char *blob;
1058 u_int blen;
1060 debug3("%s entering", __func__);
1062 if ((challenge = BN_new()) == NULL)
1063 fatal("%s: BN_new failed", __func__);
1065 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1066 if (key_to_blob(key, &blob, &blen) == 0)
1067 fatal("%s: key_to_blob failed", __func__);
1068 key->type = KEY_RSA1;
1070 buffer_init(&m);
1071 buffer_put_string(&m, blob, blen);
1072 xfree(blob);
1074 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1075 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1077 buffer_get_bignum2(&m, challenge);
1078 buffer_free(&m);
1080 return (challenge);
1084 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1086 Buffer m;
1087 u_char *blob;
1088 u_int blen;
1089 int success = 0;
1091 debug3("%s entering", __func__);
1093 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1094 if (key_to_blob(key, &blob, &blen) == 0)
1095 fatal("%s: key_to_blob failed", __func__);
1096 key->type = KEY_RSA1;
1098 buffer_init(&m);
1099 buffer_put_string(&m, blob, blen);
1100 buffer_put_string(&m, response, 16);
1101 xfree(blob);
1103 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1104 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1106 success = buffer_get_int(&m);
1107 buffer_free(&m);
1109 return (success);
1112 #ifdef SSH_AUDIT_EVENTS
1113 void
1114 mm_audit_event(ssh_audit_event_t event)
1116 Buffer m;
1118 debug3("%s entering", __func__);
1120 buffer_init(&m);
1121 buffer_put_int(&m, event);
1123 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1124 buffer_free(&m);
1127 void
1128 mm_audit_run_command(const char *command)
1130 Buffer m;
1132 debug3("%s entering command %s", __func__, command);
1134 buffer_init(&m);
1135 buffer_put_cstring(&m, command);
1137 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1138 buffer_free(&m);
1140 #endif /* SSH_AUDIT_EVENTS */
1142 #ifdef GSSAPI
1143 OM_uint32
1144 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1146 Buffer m;
1147 OM_uint32 major;
1149 /* Client doesn't get to see the context */
1150 *ctx = NULL;
1152 buffer_init(&m);
1153 buffer_put_string(&m, goid->elements, goid->length);
1155 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1156 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1158 major = buffer_get_int(&m);
1160 buffer_free(&m);
1161 return (major);
1164 OM_uint32
1165 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1166 gss_buffer_desc *out, OM_uint32 *flags)
1168 Buffer m;
1169 OM_uint32 major;
1170 u_int len;
1172 buffer_init(&m);
1173 buffer_put_string(&m, in->value, in->length);
1175 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1176 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1178 major = buffer_get_int(&m);
1179 out->value = buffer_get_string(&m, &len);
1180 out->length = len;
1181 if (flags)
1182 *flags = buffer_get_int(&m);
1184 buffer_free(&m);
1186 return (major);
1189 OM_uint32
1190 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1192 Buffer m;
1193 OM_uint32 major;
1195 buffer_init(&m);
1196 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1197 buffer_put_string(&m, gssmic->value, gssmic->length);
1199 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1200 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1201 &m);
1203 major = buffer_get_int(&m);
1204 buffer_free(&m);
1205 return(major);
1209 mm_ssh_gssapi_userok(char *user)
1211 Buffer m;
1212 int authenticated = 0;
1214 buffer_init(&m);
1216 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1217 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1218 &m);
1220 authenticated = buffer_get_int(&m);
1222 buffer_free(&m);
1223 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1224 return (authenticated);
1226 #endif /* GSSAPI */