- jmc@cvs.openbsd.org 2006/07/18 08:03:09
[openssh-git.git] / monitor_wrap.c
blob9ec60b6afd6bf4e374d2dfbfff55a2b2898e17ee
1 /* $OpenBSD: monitor_wrap.c,v 1.47 2006/07/11 20:07:25 stevesk 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>
32 #include <openssl/bn.h>
33 #include <openssl/dh.h>
35 #include <errno.h>
36 #include <pwd.h>
38 #include "ssh.h"
39 #include "dh.h"
40 #include "kex.h"
41 #include "auth.h"
42 #include "auth-options.h"
43 #include "buffer.h"
44 #include "bufaux.h"
45 #include "packet.h"
46 #include "mac.h"
47 #include "log.h"
48 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
49 #undef TARGET_OS_MAC
50 #include "zlib.h"
51 #define TARGET_OS_MAC 1
52 #else
53 #include "zlib.h"
54 #endif
55 #include "monitor.h"
56 #include "monitor_wrap.h"
57 #include "xmalloc.h"
58 #include "atomicio.h"
59 #include "monitor_fdpass.h"
60 #include "misc.h"
61 #include "servconf.h"
63 #include "auth.h"
64 #include "channels.h"
65 #include "session.h"
67 #ifdef GSSAPI
68 #include "ssh-gss.h"
69 #endif
71 /* Imports */
72 extern int compat20;
73 extern Newkeys *newkeys[];
74 extern z_stream incoming_stream;
75 extern z_stream outgoing_stream;
76 extern struct monitor *pmonitor;
77 extern Buffer input, output;
78 extern Buffer loginmsg;
79 extern ServerOptions options;
81 int
82 mm_is_monitor(void)
85 * m_pid is only set in the privileged part, and
86 * points to the unprivileged child.
88 return (pmonitor && pmonitor->m_pid > 0);
91 void
92 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
94 u_int mlen = buffer_len(m);
95 u_char buf[5];
97 debug3("%s entering: type %d", __func__, type);
99 put_u32(buf, mlen + 1);
100 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
101 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
102 fatal("%s: write: %s", __func__, strerror(errno));
103 if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
104 fatal("%s: write: %s", __func__, strerror(errno));
107 void
108 mm_request_receive(int sock, Buffer *m)
110 u_char buf[4];
111 u_int msg_len;
113 debug3("%s entering", __func__);
115 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
116 if (errno == EPIPE)
117 cleanup_exit(255);
118 fatal("%s: read: %s", __func__, strerror(errno));
120 msg_len = get_u32(buf);
121 if (msg_len > 256 * 1024)
122 fatal("%s: read: bad msg_len %d", __func__, msg_len);
123 buffer_clear(m);
124 buffer_append_space(m, msg_len);
125 if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
126 fatal("%s: read: %s", __func__, strerror(errno));
129 void
130 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
132 u_char rtype;
134 debug3("%s entering: type %d", __func__, type);
136 mm_request_receive(sock, m);
137 rtype = buffer_get_char(m);
138 if (rtype != type)
139 fatal("%s: read: rtype %d != type %d", __func__,
140 rtype, type);
143 DH *
144 mm_choose_dh(int min, int nbits, int max)
146 BIGNUM *p, *g;
147 int success = 0;
148 Buffer m;
150 buffer_init(&m);
151 buffer_put_int(&m, min);
152 buffer_put_int(&m, nbits);
153 buffer_put_int(&m, max);
155 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
157 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
158 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
160 success = buffer_get_char(&m);
161 if (success == 0)
162 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
164 if ((p = BN_new()) == NULL)
165 fatal("%s: BN_new failed", __func__);
166 if ((g = BN_new()) == NULL)
167 fatal("%s: BN_new failed", __func__);
168 buffer_get_bignum2(&m, p);
169 buffer_get_bignum2(&m, g);
171 debug3("%s: remaining %d", __func__, buffer_len(&m));
172 buffer_free(&m);
174 return (dh_new_group(g, p));
178 mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
180 Kex *kex = *pmonitor->m_pkex;
181 Buffer m;
183 debug3("%s entering", __func__);
185 buffer_init(&m);
186 buffer_put_int(&m, kex->host_key_index(key));
187 buffer_put_string(&m, data, datalen);
189 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
191 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
192 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
193 *sigp = buffer_get_string(&m, lenp);
194 buffer_free(&m);
196 return (0);
199 struct passwd *
200 mm_getpwnamallow(const char *username)
202 Buffer m;
203 struct passwd *pw;
204 u_int pwlen;
206 debug3("%s entering", __func__);
208 buffer_init(&m);
209 buffer_put_cstring(&m, username);
211 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
213 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
214 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
216 if (buffer_get_char(&m) == 0) {
217 buffer_free(&m);
218 return (NULL);
220 pw = buffer_get_string(&m, &pwlen);
221 if (pwlen != sizeof(struct passwd))
222 fatal("%s: struct passwd size mismatch", __func__);
223 pw->pw_name = buffer_get_string(&m, NULL);
224 pw->pw_passwd = buffer_get_string(&m, NULL);
225 pw->pw_gecos = buffer_get_string(&m, NULL);
226 #ifdef HAVE_PW_CLASS_IN_PASSWD
227 pw->pw_class = buffer_get_string(&m, NULL);
228 #endif
229 pw->pw_dir = buffer_get_string(&m, NULL);
230 pw->pw_shell = buffer_get_string(&m, NULL);
231 buffer_free(&m);
233 return (pw);
236 char *
237 mm_auth2_read_banner(void)
239 Buffer m;
240 char *banner;
242 debug3("%s entering", __func__);
244 buffer_init(&m);
245 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
246 buffer_clear(&m);
248 mm_request_receive_expect(pmonitor->m_recvfd,
249 MONITOR_ANS_AUTH2_READ_BANNER, &m);
250 banner = buffer_get_string(&m, NULL);
251 buffer_free(&m);
253 /* treat empty banner as missing banner */
254 if (strlen(banner) == 0) {
255 xfree(banner);
256 banner = NULL;
258 return (banner);
261 /* Inform the privileged process about service and style */
263 void
264 mm_inform_authserv(char *service, char *style)
266 Buffer m;
268 debug3("%s entering", __func__);
270 buffer_init(&m);
271 buffer_put_cstring(&m, service);
272 buffer_put_cstring(&m, style ? style : "");
274 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
276 buffer_free(&m);
279 /* Do the password authentication */
281 mm_auth_password(Authctxt *authctxt, char *password)
283 Buffer m;
284 int authenticated = 0;
286 debug3("%s entering", __func__);
288 buffer_init(&m);
289 buffer_put_cstring(&m, password);
290 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
292 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
293 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
295 authenticated = buffer_get_int(&m);
297 buffer_free(&m);
299 debug3("%s: user %sauthenticated",
300 __func__, authenticated ? "" : "not ");
301 return (authenticated);
305 mm_user_key_allowed(struct passwd *pw, Key *key)
307 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
311 mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
312 Key *key)
314 return (mm_key_allowed(MM_HOSTKEY, user, host, key));
318 mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
319 char *host, Key *key)
321 int ret;
323 key->type = KEY_RSA; /* XXX hack for key_to_blob */
324 ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
325 key->type = KEY_RSA1;
326 return (ret);
329 static void
330 mm_send_debug(Buffer *m)
332 char *msg;
334 while (buffer_len(m)) {
335 msg = buffer_get_string(m, NULL);
336 debug3("%s: Sending debug: %s", __func__, msg);
337 packet_send_debug("%s", msg);
338 xfree(msg);
343 mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
345 Buffer m;
346 u_char *blob;
347 u_int len;
348 int allowed = 0, have_forced = 0;
350 debug3("%s entering", __func__);
352 /* Convert the key to a blob and the pass it over */
353 if (!key_to_blob(key, &blob, &len))
354 return (0);
356 buffer_init(&m);
357 buffer_put_int(&m, type);
358 buffer_put_cstring(&m, user ? user : "");
359 buffer_put_cstring(&m, host ? host : "");
360 buffer_put_string(&m, blob, len);
361 xfree(blob);
363 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
365 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
366 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
368 allowed = buffer_get_int(&m);
370 /* fake forced command */
371 auth_clear_options();
372 have_forced = buffer_get_int(&m);
373 forced_command = have_forced ? xstrdup("true") : NULL;
375 /* Send potential debug messages */
376 mm_send_debug(&m);
378 buffer_free(&m);
380 return (allowed);
384 * This key verify needs to send the key type along, because the
385 * privileged parent makes the decision if the key is allowed
386 * for authentication.
390 mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
392 Buffer m;
393 u_char *blob;
394 u_int len;
395 int verified = 0;
397 debug3("%s entering", __func__);
399 /* Convert the key to a blob and the pass it over */
400 if (!key_to_blob(key, &blob, &len))
401 return (0);
403 buffer_init(&m);
404 buffer_put_string(&m, blob, len);
405 buffer_put_string(&m, sig, siglen);
406 buffer_put_string(&m, data, datalen);
407 xfree(blob);
409 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
411 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
412 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
414 verified = buffer_get_int(&m);
416 buffer_free(&m);
418 return (verified);
421 /* Export key state after authentication */
422 Newkeys *
423 mm_newkeys_from_blob(u_char *blob, int blen)
425 Buffer b;
426 u_int len;
427 Newkeys *newkey = NULL;
428 Enc *enc;
429 Mac *mac;
430 Comp *comp;
432 debug3("%s: %p(%d)", __func__, blob, blen);
433 #ifdef DEBUG_PK
434 dump_base64(stderr, blob, blen);
435 #endif
436 buffer_init(&b);
437 buffer_append(&b, blob, blen);
439 newkey = xmalloc(sizeof(*newkey));
440 enc = &newkey->enc;
441 mac = &newkey->mac;
442 comp = &newkey->comp;
444 /* Enc structure */
445 enc->name = buffer_get_string(&b, NULL);
446 buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
447 enc->enabled = buffer_get_int(&b);
448 enc->block_size = buffer_get_int(&b);
449 enc->key = buffer_get_string(&b, &enc->key_len);
450 enc->iv = buffer_get_string(&b, &len);
451 if (len != enc->block_size)
452 fatal("%s: bad ivlen: expected %u != %u", __func__,
453 enc->block_size, len);
455 if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
456 fatal("%s: bad cipher name %s or pointer %p", __func__,
457 enc->name, enc->cipher);
459 /* Mac structure */
460 mac->name = buffer_get_string(&b, NULL);
461 if (mac->name == NULL || mac_init(mac, mac->name) == -1)
462 fatal("%s: can not init mac %s", __func__, mac->name);
463 mac->enabled = buffer_get_int(&b);
464 mac->key = buffer_get_string(&b, &len);
465 if (len > mac->key_len)
466 fatal("%s: bad mac key length: %u > %d", __func__, len,
467 mac->key_len);
468 mac->key_len = len;
470 /* Comp structure */
471 comp->type = buffer_get_int(&b);
472 comp->enabled = buffer_get_int(&b);
473 comp->name = buffer_get_string(&b, NULL);
475 len = buffer_len(&b);
476 if (len != 0)
477 error("newkeys_from_blob: remaining bytes in blob %u", len);
478 buffer_free(&b);
479 return (newkey);
483 mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
485 Buffer b;
486 int len;
487 Enc *enc;
488 Mac *mac;
489 Comp *comp;
490 Newkeys *newkey = newkeys[mode];
492 debug3("%s: converting %p", __func__, newkey);
494 if (newkey == NULL) {
495 error("%s: newkey == NULL", __func__);
496 return 0;
498 enc = &newkey->enc;
499 mac = &newkey->mac;
500 comp = &newkey->comp;
502 buffer_init(&b);
503 /* Enc structure */
504 buffer_put_cstring(&b, enc->name);
505 /* The cipher struct is constant and shared, you export pointer */
506 buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
507 buffer_put_int(&b, enc->enabled);
508 buffer_put_int(&b, enc->block_size);
509 buffer_put_string(&b, enc->key, enc->key_len);
510 packet_get_keyiv(mode, enc->iv, enc->block_size);
511 buffer_put_string(&b, enc->iv, enc->block_size);
513 /* Mac structure */
514 buffer_put_cstring(&b, mac->name);
515 buffer_put_int(&b, mac->enabled);
516 buffer_put_string(&b, mac->key, mac->key_len);
518 /* Comp structure */
519 buffer_put_int(&b, comp->type);
520 buffer_put_int(&b, comp->enabled);
521 buffer_put_cstring(&b, comp->name);
523 len = buffer_len(&b);
524 if (lenp != NULL)
525 *lenp = len;
526 if (blobp != NULL) {
527 *blobp = xmalloc(len);
528 memcpy(*blobp, buffer_ptr(&b), len);
530 memset(buffer_ptr(&b), 0, len);
531 buffer_free(&b);
532 return len;
535 static void
536 mm_send_kex(Buffer *m, Kex *kex)
538 buffer_put_string(m, kex->session_id, kex->session_id_len);
539 buffer_put_int(m, kex->we_need);
540 buffer_put_int(m, kex->hostkey_type);
541 buffer_put_int(m, kex->kex_type);
542 buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
543 buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
544 buffer_put_int(m, kex->flags);
545 buffer_put_cstring(m, kex->client_version_string);
546 buffer_put_cstring(m, kex->server_version_string);
549 void
550 mm_send_keystate(struct monitor *monitor)
552 Buffer m;
553 u_char *blob, *p;
554 u_int bloblen, plen;
555 u_int32_t seqnr, packets;
556 u_int64_t blocks;
558 buffer_init(&m);
560 if (!compat20) {
561 u_char iv[24];
562 u_char *key;
563 u_int ivlen, keylen;
565 buffer_put_int(&m, packet_get_protocol_flags());
567 buffer_put_int(&m, packet_get_ssh1_cipher());
569 debug3("%s: Sending ssh1 KEY+IV", __func__);
570 keylen = packet_get_encryption_key(NULL);
571 key = xmalloc(keylen+1); /* add 1 if keylen == 0 */
572 keylen = packet_get_encryption_key(key);
573 buffer_put_string(&m, key, keylen);
574 memset(key, 0, keylen);
575 xfree(key);
577 ivlen = packet_get_keyiv_len(MODE_OUT);
578 packet_get_keyiv(MODE_OUT, iv, ivlen);
579 buffer_put_string(&m, iv, ivlen);
580 ivlen = packet_get_keyiv_len(MODE_OUT);
581 packet_get_keyiv(MODE_IN, iv, ivlen);
582 buffer_put_string(&m, iv, ivlen);
583 goto skip;
584 } else {
585 /* Kex for rekeying */
586 mm_send_kex(&m, *monitor->m_pkex);
589 debug3("%s: Sending new keys: %p %p",
590 __func__, newkeys[MODE_OUT], newkeys[MODE_IN]);
592 /* Keys from Kex */
593 if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
594 fatal("%s: conversion of newkeys failed", __func__);
596 buffer_put_string(&m, blob, bloblen);
597 xfree(blob);
599 if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
600 fatal("%s: conversion of newkeys failed", __func__);
602 buffer_put_string(&m, blob, bloblen);
603 xfree(blob);
605 packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
606 buffer_put_int(&m, seqnr);
607 buffer_put_int64(&m, blocks);
608 buffer_put_int(&m, packets);
609 packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
610 buffer_put_int(&m, seqnr);
611 buffer_put_int64(&m, blocks);
612 buffer_put_int(&m, packets);
614 debug3("%s: New keys have been sent", __func__);
615 skip:
616 /* More key context */
617 plen = packet_get_keycontext(MODE_OUT, NULL);
618 p = xmalloc(plen+1);
619 packet_get_keycontext(MODE_OUT, p);
620 buffer_put_string(&m, p, plen);
621 xfree(p);
623 plen = packet_get_keycontext(MODE_IN, NULL);
624 p = xmalloc(plen+1);
625 packet_get_keycontext(MODE_IN, p);
626 buffer_put_string(&m, p, plen);
627 xfree(p);
629 /* Compression state */
630 debug3("%s: Sending compression state", __func__);
631 buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
632 buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
634 /* Network I/O buffers */
635 buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input));
636 buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output));
638 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
639 debug3("%s: Finished sending state", __func__);
641 buffer_free(&m);
645 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
647 Buffer m;
648 char *p, *msg;
649 int success = 0;
651 buffer_init(&m);
652 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
654 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
655 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
657 success = buffer_get_int(&m);
658 if (success == 0) {
659 debug3("%s: pty alloc failed", __func__);
660 buffer_free(&m);
661 return (0);
663 p = buffer_get_string(&m, NULL);
664 msg = buffer_get_string(&m, NULL);
665 buffer_free(&m);
667 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
668 xfree(p);
670 buffer_append(&loginmsg, msg, strlen(msg));
671 xfree(msg);
673 *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
674 *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
676 /* Success */
677 return (1);
680 void
681 mm_session_pty_cleanup2(Session *s)
683 Buffer m;
685 if (s->ttyfd == -1)
686 return;
687 buffer_init(&m);
688 buffer_put_cstring(&m, s->tty);
689 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
690 buffer_free(&m);
692 /* closed dup'ed master */
693 if (close(s->ptymaster) < 0)
694 error("close(s->ptymaster): %s", strerror(errno));
696 /* unlink pty from session */
697 s->ttyfd = -1;
700 #ifdef USE_PAM
701 void
702 mm_start_pam(Authctxt *authctxt)
704 Buffer m;
706 debug3("%s entering", __func__);
707 if (!options.use_pam)
708 fatal("UsePAM=no, but ended up in %s anyway", __func__);
710 buffer_init(&m);
711 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
713 buffer_free(&m);
716 u_int
717 mm_do_pam_account(void)
719 Buffer m;
720 u_int ret;
721 char *msg;
723 debug3("%s entering", __func__);
724 if (!options.use_pam)
725 fatal("UsePAM=no, but ended up in %s anyway", __func__);
727 buffer_init(&m);
728 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
730 mm_request_receive_expect(pmonitor->m_recvfd,
731 MONITOR_ANS_PAM_ACCOUNT, &m);
732 ret = buffer_get_int(&m);
733 msg = buffer_get_string(&m, NULL);
734 buffer_append(&loginmsg, msg, strlen(msg));
735 xfree(msg);
737 buffer_free(&m);
739 debug3("%s returning %d", __func__, ret);
741 return (ret);
744 void *
745 mm_sshpam_init_ctx(Authctxt *authctxt)
747 Buffer m;
748 int success;
750 debug3("%s", __func__);
751 buffer_init(&m);
752 buffer_put_cstring(&m, authctxt->user);
753 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
754 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
755 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
756 success = buffer_get_int(&m);
757 if (success == 0) {
758 debug3("%s: pam_init_ctx failed", __func__);
759 buffer_free(&m);
760 return (NULL);
762 buffer_free(&m);
763 return (authctxt);
767 mm_sshpam_query(void *ctx, char **name, char **info,
768 u_int *num, char ***prompts, u_int **echo_on)
770 Buffer m;
771 u_int i;
772 int ret;
774 debug3("%s", __func__);
775 buffer_init(&m);
776 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
777 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
778 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
779 ret = buffer_get_int(&m);
780 debug3("%s: pam_query returned %d", __func__, ret);
781 *name = buffer_get_string(&m, NULL);
782 *info = buffer_get_string(&m, NULL);
783 *num = buffer_get_int(&m);
784 if (*num > PAM_MAX_NUM_MSG)
785 fatal("%s: recieved %u PAM messages, expected <= %u",
786 __func__, *num, PAM_MAX_NUM_MSG);
787 *prompts = xcalloc((*num + 1), sizeof(char *));
788 *echo_on = xcalloc((*num + 1), sizeof(u_int));
789 for (i = 0; i < *num; ++i) {
790 (*prompts)[i] = buffer_get_string(&m, NULL);
791 (*echo_on)[i] = buffer_get_int(&m);
793 buffer_free(&m);
794 return (ret);
798 mm_sshpam_respond(void *ctx, u_int num, char **resp)
800 Buffer m;
801 u_int i;
802 int ret;
804 debug3("%s", __func__);
805 buffer_init(&m);
806 buffer_put_int(&m, num);
807 for (i = 0; i < num; ++i)
808 buffer_put_cstring(&m, resp[i]);
809 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
810 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
811 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
812 ret = buffer_get_int(&m);
813 debug3("%s: pam_respond returned %d", __func__, ret);
814 buffer_free(&m);
815 return (ret);
818 void
819 mm_sshpam_free_ctx(void *ctxtp)
821 Buffer m;
823 debug3("%s", __func__);
824 buffer_init(&m);
825 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
826 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
827 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
828 buffer_free(&m);
830 #endif /* USE_PAM */
832 /* Request process termination */
834 void
835 mm_terminate(void)
837 Buffer m;
839 buffer_init(&m);
840 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
841 buffer_free(&m);
845 mm_ssh1_session_key(BIGNUM *num)
847 int rsafail;
848 Buffer m;
850 buffer_init(&m);
851 buffer_put_bignum2(&m, num);
852 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
854 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
856 rsafail = buffer_get_int(&m);
857 buffer_get_bignum2(&m, num);
859 buffer_free(&m);
861 return (rsafail);
864 static void
865 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
866 char ***prompts, u_int **echo_on)
868 *name = xstrdup("");
869 *infotxt = xstrdup("");
870 *numprompts = 1;
871 *prompts = xcalloc(*numprompts, sizeof(char *));
872 *echo_on = xcalloc(*numprompts, sizeof(u_int));
873 (*echo_on)[0] = 0;
877 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
878 u_int *numprompts, char ***prompts, u_int **echo_on)
880 Buffer m;
881 u_int success;
882 char *challenge;
884 debug3("%s: entering", __func__);
886 buffer_init(&m);
887 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
889 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
890 &m);
891 success = buffer_get_int(&m);
892 if (success == 0) {
893 debug3("%s: no challenge", __func__);
894 buffer_free(&m);
895 return (-1);
898 /* Get the challenge, and format the response */
899 challenge = buffer_get_string(&m, NULL);
900 buffer_free(&m);
902 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
903 (*prompts)[0] = challenge;
905 debug3("%s: received challenge: %s", __func__, challenge);
907 return (0);
911 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
913 Buffer m;
914 int authok;
916 debug3("%s: entering", __func__);
917 if (numresponses != 1)
918 return (-1);
920 buffer_init(&m);
921 buffer_put_cstring(&m, responses[0]);
922 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
924 mm_request_receive_expect(pmonitor->m_recvfd,
925 MONITOR_ANS_BSDAUTHRESPOND, &m);
927 authok = buffer_get_int(&m);
928 buffer_free(&m);
930 return ((authok == 0) ? -1 : 0);
933 #ifdef SKEY
935 mm_skey_query(void *ctx, char **name, char **infotxt,
936 u_int *numprompts, char ***prompts, u_int **echo_on)
938 Buffer m;
939 int len;
940 u_int success;
941 char *p, *challenge;
943 debug3("%s: entering", __func__);
945 buffer_init(&m);
946 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
948 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
949 &m);
950 success = buffer_get_int(&m);
951 if (success == 0) {
952 debug3("%s: no challenge", __func__);
953 buffer_free(&m);
954 return (-1);
957 /* Get the challenge, and format the response */
958 challenge = buffer_get_string(&m, NULL);
959 buffer_free(&m);
961 debug3("%s: received challenge: %s", __func__, challenge);
963 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
965 xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
966 xfree(challenge);
968 return (0);
972 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
974 Buffer m;
975 int authok;
977 debug3("%s: entering", __func__);
978 if (numresponses != 1)
979 return (-1);
981 buffer_init(&m);
982 buffer_put_cstring(&m, responses[0]);
983 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
985 mm_request_receive_expect(pmonitor->m_recvfd,
986 MONITOR_ANS_SKEYRESPOND, &m);
988 authok = buffer_get_int(&m);
989 buffer_free(&m);
991 return ((authok == 0) ? -1 : 0);
993 #endif /* SKEY */
995 void
996 mm_ssh1_session_id(u_char session_id[16])
998 Buffer m;
999 int i;
1001 debug3("%s entering", __func__);
1003 buffer_init(&m);
1004 for (i = 0; i < 16; i++)
1005 buffer_put_char(&m, session_id[i]);
1007 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1008 buffer_free(&m);
1012 mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1014 Buffer m;
1015 Key *key;
1016 u_char *blob;
1017 u_int blen;
1018 int allowed = 0, have_forced = 0;
1020 debug3("%s entering", __func__);
1022 buffer_init(&m);
1023 buffer_put_bignum2(&m, client_n);
1025 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
1026 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1028 allowed = buffer_get_int(&m);
1030 /* fake forced command */
1031 auth_clear_options();
1032 have_forced = buffer_get_int(&m);
1033 forced_command = have_forced ? xstrdup("true") : NULL;
1035 if (allowed && rkey != NULL) {
1036 blob = buffer_get_string(&m, &blen);
1037 if ((key = key_from_blob(blob, blen)) == NULL)
1038 fatal("%s: key_from_blob failed", __func__);
1039 *rkey = key;
1040 xfree(blob);
1042 mm_send_debug(&m);
1043 buffer_free(&m);
1045 return (allowed);
1048 BIGNUM *
1049 mm_auth_rsa_generate_challenge(Key *key)
1051 Buffer m;
1052 BIGNUM *challenge;
1053 u_char *blob;
1054 u_int blen;
1056 debug3("%s entering", __func__);
1058 if ((challenge = BN_new()) == NULL)
1059 fatal("%s: BN_new failed", __func__);
1061 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1062 if (key_to_blob(key, &blob, &blen) == 0)
1063 fatal("%s: key_to_blob failed", __func__);
1064 key->type = KEY_RSA1;
1066 buffer_init(&m);
1067 buffer_put_string(&m, blob, blen);
1068 xfree(blob);
1070 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
1071 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1073 buffer_get_bignum2(&m, challenge);
1074 buffer_free(&m);
1076 return (challenge);
1080 mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
1082 Buffer m;
1083 u_char *blob;
1084 u_int blen;
1085 int success = 0;
1087 debug3("%s entering", __func__);
1089 key->type = KEY_RSA; /* XXX cheat for key_to_blob */
1090 if (key_to_blob(key, &blob, &blen) == 0)
1091 fatal("%s: key_to_blob failed", __func__);
1092 key->type = KEY_RSA1;
1094 buffer_init(&m);
1095 buffer_put_string(&m, blob, blen);
1096 buffer_put_string(&m, response, 16);
1097 xfree(blob);
1099 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
1100 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1102 success = buffer_get_int(&m);
1103 buffer_free(&m);
1105 return (success);
1108 #ifdef SSH_AUDIT_EVENTS
1109 void
1110 mm_audit_event(ssh_audit_event_t event)
1112 Buffer m;
1114 debug3("%s entering", __func__);
1116 buffer_init(&m);
1117 buffer_put_int(&m, event);
1119 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
1120 buffer_free(&m);
1123 void
1124 mm_audit_run_command(const char *command)
1126 Buffer m;
1128 debug3("%s entering command %s", __func__, command);
1130 buffer_init(&m);
1131 buffer_put_cstring(&m, command);
1133 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
1134 buffer_free(&m);
1136 #endif /* SSH_AUDIT_EVENTS */
1138 #ifdef GSSAPI
1139 OM_uint32
1140 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1142 Buffer m;
1143 OM_uint32 major;
1145 /* Client doesn't get to see the context */
1146 *ctx = NULL;
1148 buffer_init(&m);
1149 buffer_put_string(&m, goid->elements, goid->length);
1151 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
1152 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
1154 major = buffer_get_int(&m);
1156 buffer_free(&m);
1157 return (major);
1160 OM_uint32
1161 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
1162 gss_buffer_desc *out, OM_uint32 *flags)
1164 Buffer m;
1165 OM_uint32 major;
1166 u_int len;
1168 buffer_init(&m);
1169 buffer_put_string(&m, in->value, in->length);
1171 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
1172 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
1174 major = buffer_get_int(&m);
1175 out->value = buffer_get_string(&m, &len);
1176 out->length = len;
1177 if (flags)
1178 *flags = buffer_get_int(&m);
1180 buffer_free(&m);
1182 return (major);
1185 OM_uint32
1186 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1188 Buffer m;
1189 OM_uint32 major;
1191 buffer_init(&m);
1192 buffer_put_string(&m, gssbuf->value, gssbuf->length);
1193 buffer_put_string(&m, gssmic->value, gssmic->length);
1195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
1196 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
1197 &m);
1199 major = buffer_get_int(&m);
1200 buffer_free(&m);
1201 return(major);
1205 mm_ssh_gssapi_userok(char *user)
1207 Buffer m;
1208 int authenticated = 0;
1210 buffer_init(&m);
1212 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
1213 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
1214 &m);
1216 authenticated = buffer_get_int(&m);
1218 buffer_free(&m);
1219 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1220 return (authenticated);
1222 #endif /* GSSAPI */