]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - monitor_wrap.c
Vendor import of OpenSSH 7.7p1.
[FreeBSD/FreeBSD.git] / monitor_wrap.c
1 /* $OpenBSD: monitor_wrap.c,v 1.99 2018/03/03 03:15:51 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2002 Markus Friedl <markus@openbsd.org>
5  * All rights reserved.
6  *
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.
15  *
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.
26  */
27
28 #include "includes.h"
29
30 #include <sys/types.h>
31 #include <sys/uio.h>
32
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>
40
41 #ifdef WITH_OPENSSL
42 #include <openssl/bn.h>
43 #include <openssl/dh.h>
44 #include <openssl/evp.h>
45 #endif
46
47 #include "openbsd-compat/sys-queue.h"
48 #include "xmalloc.h"
49 #include "ssh.h"
50 #ifdef WITH_OPENSSL
51 #include "dh.h"
52 #endif
53 #include "buffer.h"
54 #include "key.h"
55 #include "cipher.h"
56 #include "kex.h"
57 #include "hostfile.h"
58 #include "auth.h"
59 #include "auth-options.h"
60 #include "packet.h"
61 #include "mac.h"
62 #include "log.h"
63 #include "auth-pam.h"
64 #ifdef TARGET_OS_MAC    /* XXX Broken krb5 headers on Mac */
65 #undef TARGET_OS_MAC
66 #include "zlib.h"
67 #define TARGET_OS_MAC 1
68 #else
69 #include "zlib.h"
70 #endif
71 #include "monitor.h"
72 #ifdef GSSAPI
73 #include "ssh-gss.h"
74 #endif
75 #include "monitor_wrap.h"
76 #include "atomicio.h"
77 #include "monitor_fdpass.h"
78 #include "misc.h"
79
80 #include "channels.h"
81 #include "session.h"
82 #include "servconf.h"
83
84 #include "ssherr.h"
85
86 /* Imports */
87 extern z_stream incoming_stream;
88 extern z_stream outgoing_stream;
89 extern struct monitor *pmonitor;
90 extern Buffer loginmsg;
91 extern ServerOptions options;
92
93 void
94 mm_log_handler(LogLevel level, const char *msg, void *ctx)
95 {
96         Buffer log_msg;
97         struct monitor *mon = (struct monitor *)ctx;
98
99         if (mon->m_log_sendfd == -1)
100                 fatal("%s: no log channel", __func__);
101
102         buffer_init(&log_msg);
103         /*
104          * Placeholder for packet length. Will be filled in with the actual
105          * packet length once the packet has been constucted. This saves
106          * fragile math.
107          */
108         buffer_put_int(&log_msg, 0);
109
110         buffer_put_int(&log_msg, level);
111         buffer_put_cstring(&log_msg, msg);
112         put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4);
113         if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg),
114             buffer_len(&log_msg)) != buffer_len(&log_msg))
115                 fatal("%s: write: %s", __func__, strerror(errno));
116         buffer_free(&log_msg);
117 }
118
119 int
120 mm_is_monitor(void)
121 {
122         /*
123          * m_pid is only set in the privileged part, and
124          * points to the unprivileged child.
125          */
126         return (pmonitor && pmonitor->m_pid > 0);
127 }
128
129 void
130 mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
131 {
132         u_int mlen = buffer_len(m);
133         u_char buf[5];
134
135         debug3("%s entering: type %d", __func__, type);
136
137         put_u32(buf, mlen + 1);
138         buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
139         if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
140                 fatal("%s: write: %s", __func__, strerror(errno));
141         if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
142                 fatal("%s: write: %s", __func__, strerror(errno));
143 }
144
145 void
146 mm_request_receive(int sock, Buffer *m)
147 {
148         u_char buf[4];
149         u_int msg_len;
150
151         debug3("%s entering", __func__);
152
153         if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
154                 if (errno == EPIPE)
155                         cleanup_exit(255);
156                 fatal("%s: read: %s", __func__, strerror(errno));
157         }
158         msg_len = get_u32(buf);
159         if (msg_len > 256 * 1024)
160                 fatal("%s: read: bad msg_len %d", __func__, msg_len);
161         buffer_clear(m);
162         buffer_append_space(m, msg_len);
163         if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
164                 fatal("%s: read: %s", __func__, strerror(errno));
165 }
166
167 void
168 mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
169 {
170         u_char rtype;
171
172         debug3("%s entering: type %d", __func__, type);
173
174         mm_request_receive(sock, m);
175         rtype = buffer_get_char(m);
176         if (rtype != type)
177                 fatal("%s: read: rtype %d != type %d", __func__,
178                     rtype, type);
179 }
180
181 #ifdef WITH_OPENSSL
182 DH *
183 mm_choose_dh(int min, int nbits, int max)
184 {
185         BIGNUM *p, *g;
186         int success = 0;
187         Buffer m;
188
189         buffer_init(&m);
190         buffer_put_int(&m, min);
191         buffer_put_int(&m, nbits);
192         buffer_put_int(&m, max);
193
194         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
195
196         debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
197         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
198
199         success = buffer_get_char(&m);
200         if (success == 0)
201                 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
202
203         if ((p = BN_new()) == NULL)
204                 fatal("%s: BN_new failed", __func__);
205         if ((g = BN_new()) == NULL)
206                 fatal("%s: BN_new failed", __func__);
207         buffer_get_bignum2(&m, p);
208         buffer_get_bignum2(&m, g);
209
210         debug3("%s: remaining %d", __func__, buffer_len(&m));
211         buffer_free(&m);
212
213         return (dh_new_group(g, p));
214 }
215 #endif
216
217 int
218 mm_key_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
219     const u_char *data, u_int datalen, const char *hostkey_alg)
220 {
221         struct kex *kex = *pmonitor->m_pkex;
222         Buffer m;
223
224         debug3("%s entering", __func__);
225
226         buffer_init(&m);
227         buffer_put_int(&m, kex->host_key_index(key, 0, active_state));
228         buffer_put_string(&m, data, datalen);
229         buffer_put_cstring(&m, hostkey_alg);
230
231         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
232
233         debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
234         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
235         *sigp  = buffer_get_string(&m, lenp);
236         buffer_free(&m);
237
238         return (0);
239 }
240
241 struct passwd *
242 mm_getpwnamallow(const char *username)
243 {
244         struct ssh *ssh = active_state;         /* XXX */
245         Buffer m;
246         struct passwd *pw;
247         u_int len, i;
248         ServerOptions *newopts;
249
250         debug3("%s entering", __func__);
251
252         buffer_init(&m);
253         buffer_put_cstring(&m, username);
254
255         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
256
257         debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
258         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
259
260         if (buffer_get_char(&m) == 0) {
261                 pw = NULL;
262                 goto out;
263         }
264         pw = buffer_get_string(&m, &len);
265         if (len != sizeof(struct passwd))
266                 fatal("%s: struct passwd size mismatch", __func__);
267         pw->pw_name = buffer_get_string(&m, NULL);
268         pw->pw_passwd = buffer_get_string(&m, NULL);
269 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
270         pw->pw_gecos = buffer_get_string(&m, NULL);
271 #endif
272 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
273         pw->pw_class = buffer_get_string(&m, NULL);
274 #endif
275         pw->pw_dir = buffer_get_string(&m, NULL);
276         pw->pw_shell = buffer_get_string(&m, NULL);
277
278 out:
279         /* copy options block as a Match directive may have changed some */
280         newopts = buffer_get_string(&m, &len);
281         if (len != sizeof(*newopts))
282                 fatal("%s: option block size mismatch", __func__);
283
284 #define M_CP_STROPT(x) do { \
285                 if (newopts->x != NULL) \
286                         newopts->x = buffer_get_string(&m, NULL); \
287         } while (0)
288 #define M_CP_STRARRAYOPT(x, nx) do { \
289                 newopts->x = newopts->nx == 0 ? \
290                     NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
291                 for (i = 0; i < newopts->nx; i++) \
292                         newopts->x[i] = buffer_get_string(&m, NULL); \
293         } while (0)
294         /* See comment in servconf.h */
295         COPY_MATCH_STRING_OPTS();
296 #undef M_CP_STROPT
297 #undef M_CP_STRARRAYOPT
298
299         copy_set_server_options(&options, newopts, 1);
300         log_change_level(options.log_level);
301         process_permitopen(ssh, &options);
302         free(newopts);
303
304         buffer_free(&m);
305
306         return (pw);
307 }
308
309 char *
310 mm_auth2_read_banner(void)
311 {
312         Buffer m;
313         char *banner;
314
315         debug3("%s entering", __func__);
316
317         buffer_init(&m);
318         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
319         buffer_clear(&m);
320
321         mm_request_receive_expect(pmonitor->m_recvfd,
322             MONITOR_ANS_AUTH2_READ_BANNER, &m);
323         banner = buffer_get_string(&m, NULL);
324         buffer_free(&m);
325
326         /* treat empty banner as missing banner */
327         if (strlen(banner) == 0) {
328                 free(banner);
329                 banner = NULL;
330         }
331         return (banner);
332 }
333
334 /* Inform the privileged process about service and style */
335
336 void
337 mm_inform_authserv(char *service, char *style)
338 {
339         Buffer m;
340
341         debug3("%s entering", __func__);
342
343         buffer_init(&m);
344         buffer_put_cstring(&m, service);
345         buffer_put_cstring(&m, style ? style : "");
346
347         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
348
349         buffer_free(&m);
350 }
351
352 /* Do the password authentication */
353 int
354 mm_auth_password(struct ssh *ssh, char *password)
355 {
356         Buffer m;
357         int authenticated = 0;
358
359         debug3("%s entering", __func__);
360
361         buffer_init(&m);
362         buffer_put_cstring(&m, password);
363         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
364
365         debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
366         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
367
368         authenticated = buffer_get_int(&m);
369 #ifdef USE_PAM
370         sshpam_set_maxtries_reached(buffer_get_int(&m));
371 #endif
372
373         buffer_free(&m);
374
375         debug3("%s: user %sauthenticated",
376             __func__, authenticated ? "" : "not ");
377         return (authenticated);
378 }
379
380 int
381 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
382     int pubkey_auth_attempt, struct sshauthopt **authoptp)
383 {
384         return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
385             pubkey_auth_attempt, authoptp));
386 }
387
388 int
389 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
390     struct sshkey *key)
391 {
392         return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
393 }
394
395 int
396 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
397     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
398 {
399         Buffer m;
400         u_char *blob;
401         u_int len;
402         int r, allowed = 0;
403         struct sshauthopt *opts = NULL;
404
405         debug3("%s entering", __func__);
406
407         if (authoptp != NULL)
408                 *authoptp = NULL;
409
410         /* Convert the key to a blob and the pass it over */
411         if (!key_to_blob(key, &blob, &len))
412                 return 0;
413
414         buffer_init(&m);
415         buffer_put_int(&m, type);
416         buffer_put_cstring(&m, user ? user : "");
417         buffer_put_cstring(&m, host ? host : "");
418         buffer_put_string(&m, blob, len);
419         buffer_put_int(&m, pubkey_auth_attempt);
420         free(blob);
421
422         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
423
424         debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
425         mm_request_receive_expect(pmonitor->m_recvfd,
426             MONITOR_ANS_KEYALLOWED, &m);
427
428         allowed = buffer_get_int(&m);
429         if (allowed && type == MM_USERKEY) {
430                 if ((r = sshauthopt_deserialise(&m, &opts)) != 0)
431                         fatal("%s: sshauthopt_deserialise: %s",
432                             __func__, ssh_err(r));
433         }
434         buffer_free(&m);
435
436         if (authoptp != NULL) {
437                 *authoptp = opts;
438                 opts = NULL;
439         }
440         sshauthopt_free(opts);
441
442         return allowed;
443 }
444
445 /*
446  * This key verify needs to send the key type along, because the
447  * privileged parent makes the decision if the key is allowed
448  * for authentication.
449  */
450
451 int
452 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
453     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
454 {
455         Buffer m;
456         u_char *blob;
457         u_int len;
458         u_int encoded_ret = 0;
459
460         debug3("%s entering", __func__);
461
462         /* Convert the key to a blob and the pass it over */
463         if (!key_to_blob(key, &blob, &len))
464                 return (0);
465
466         buffer_init(&m);
467         buffer_put_string(&m, blob, len);
468         buffer_put_string(&m, sig, siglen);
469         buffer_put_string(&m, data, datalen);
470         buffer_put_cstring(&m, sigalg == NULL ? "" : sigalg);
471         free(blob);
472
473         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
474
475         debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
476         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
477
478         encoded_ret = buffer_get_int(&m);
479
480         buffer_free(&m);
481
482         if (encoded_ret != 0)
483                 return SSH_ERR_SIGNATURE_INVALID;
484         return 0;
485 }
486
487 void
488 mm_send_keystate(struct monitor *monitor)
489 {
490         struct ssh *ssh = active_state;         /* XXX */
491         struct sshbuf *m;
492         int r;
493
494         if ((m = sshbuf_new()) == NULL)
495                 fatal("%s: sshbuf_new failed", __func__);
496         if ((r = ssh_packet_get_state(ssh, m)) != 0)
497                 fatal("%s: get_state failed: %s",
498                     __func__, ssh_err(r));
499         mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
500         debug3("%s: Finished sending state", __func__);
501         sshbuf_free(m);
502 }
503
504 int
505 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
506 {
507         Buffer m;
508         char *p, *msg;
509         int success = 0, tmp1 = -1, tmp2 = -1;
510
511         /* Kludge: ensure there are fds free to receive the pty/tty */
512         if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
513             (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
514                 error("%s: cannot allocate fds for pty", __func__);
515                 if (tmp1 > 0)
516                         close(tmp1);
517                 if (tmp2 > 0)
518                         close(tmp2);
519                 return 0;
520         }
521         close(tmp1);
522         close(tmp2);
523
524         buffer_init(&m);
525         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
526
527         debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
528         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
529
530         success = buffer_get_int(&m);
531         if (success == 0) {
532                 debug3("%s: pty alloc failed", __func__);
533                 buffer_free(&m);
534                 return (0);
535         }
536         p = buffer_get_string(&m, NULL);
537         msg = buffer_get_string(&m, NULL);
538         buffer_free(&m);
539
540         strlcpy(namebuf, p, namebuflen); /* Possible truncation */
541         free(p);
542
543         buffer_append(&loginmsg, msg, strlen(msg));
544         free(msg);
545
546         if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
547             (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
548                 fatal("%s: receive fds failed", __func__);
549
550         /* Success */
551         return (1);
552 }
553
554 void
555 mm_session_pty_cleanup2(Session *s)
556 {
557         Buffer m;
558
559         if (s->ttyfd == -1)
560                 return;
561         buffer_init(&m);
562         buffer_put_cstring(&m, s->tty);
563         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
564         buffer_free(&m);
565
566         /* closed dup'ed master */
567         if (s->ptymaster != -1 && close(s->ptymaster) < 0)
568                 error("close(s->ptymaster/%d): %s",
569                     s->ptymaster, strerror(errno));
570
571         /* unlink pty from session */
572         s->ttyfd = -1;
573 }
574
575 #ifdef USE_PAM
576 void
577 mm_start_pam(Authctxt *authctxt)
578 {
579         Buffer m;
580
581         debug3("%s entering", __func__);
582         if (!options.use_pam)
583                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
584
585         buffer_init(&m);
586         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m);
587
588         buffer_free(&m);
589 }
590
591 u_int
592 mm_do_pam_account(void)
593 {
594         Buffer m;
595         u_int ret;
596         char *msg;
597
598         debug3("%s entering", __func__);
599         if (!options.use_pam)
600                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
601
602         buffer_init(&m);
603         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m);
604
605         mm_request_receive_expect(pmonitor->m_recvfd,
606             MONITOR_ANS_PAM_ACCOUNT, &m);
607         ret = buffer_get_int(&m);
608         msg = buffer_get_string(&m, NULL);
609         buffer_append(&loginmsg, msg, strlen(msg));
610         free(msg);
611
612         buffer_free(&m);
613
614         debug3("%s returning %d", __func__, ret);
615
616         return (ret);
617 }
618
619 void *
620 mm_sshpam_init_ctx(Authctxt *authctxt)
621 {
622         Buffer m;
623         int success;
624
625         debug3("%s", __func__);
626         buffer_init(&m);
627         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m);
628         debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
629         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m);
630         success = buffer_get_int(&m);
631         if (success == 0) {
632                 debug3("%s: pam_init_ctx failed", __func__);
633                 buffer_free(&m);
634                 return (NULL);
635         }
636         buffer_free(&m);
637         return (authctxt);
638 }
639
640 int
641 mm_sshpam_query(void *ctx, char **name, char **info,
642     u_int *num, char ***prompts, u_int **echo_on)
643 {
644         Buffer m;
645         u_int i;
646         int ret;
647
648         debug3("%s", __func__);
649         buffer_init(&m);
650         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m);
651         debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
652         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m);
653         ret = buffer_get_int(&m);
654         debug3("%s: pam_query returned %d", __func__, ret);
655         *name = buffer_get_string(&m, NULL);
656         *info = buffer_get_string(&m, NULL);
657         sshpam_set_maxtries_reached(buffer_get_int(&m));
658         *num = buffer_get_int(&m);
659         if (*num > PAM_MAX_NUM_MSG)
660                 fatal("%s: recieved %u PAM messages, expected <= %u",
661                     __func__, *num, PAM_MAX_NUM_MSG);
662         *prompts = xcalloc((*num + 1), sizeof(char *));
663         *echo_on = xcalloc((*num + 1), sizeof(u_int));
664         for (i = 0; i < *num; ++i) {
665                 (*prompts)[i] = buffer_get_string(&m, NULL);
666                 (*echo_on)[i] = buffer_get_int(&m);
667         }
668         buffer_free(&m);
669         return (ret);
670 }
671
672 int
673 mm_sshpam_respond(void *ctx, u_int num, char **resp)
674 {
675         Buffer m;
676         u_int i;
677         int ret;
678
679         debug3("%s", __func__);
680         buffer_init(&m);
681         buffer_put_int(&m, num);
682         for (i = 0; i < num; ++i)
683                 buffer_put_cstring(&m, resp[i]);
684         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m);
685         debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
686         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m);
687         ret = buffer_get_int(&m);
688         debug3("%s: pam_respond returned %d", __func__, ret);
689         buffer_free(&m);
690         return (ret);
691 }
692
693 void
694 mm_sshpam_free_ctx(void *ctxtp)
695 {
696         Buffer m;
697
698         debug3("%s", __func__);
699         buffer_init(&m);
700         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m);
701         debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
702         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m);
703         buffer_free(&m);
704 }
705 #endif /* USE_PAM */
706
707 /* Request process termination */
708
709 void
710 mm_terminate(void)
711 {
712         Buffer m;
713
714         buffer_init(&m);
715         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
716         buffer_free(&m);
717 }
718
719 static void
720 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
721     char ***prompts, u_int **echo_on)
722 {
723         *name = xstrdup("");
724         *infotxt = xstrdup("");
725         *numprompts = 1;
726         *prompts = xcalloc(*numprompts, sizeof(char *));
727         *echo_on = xcalloc(*numprompts, sizeof(u_int));
728         (*echo_on)[0] = 0;
729 }
730
731 int
732 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
733    u_int *numprompts, char ***prompts, u_int **echo_on)
734 {
735         Buffer m;
736         u_int success;
737         char *challenge;
738
739         debug3("%s: entering", __func__);
740
741         buffer_init(&m);
742         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
743
744         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
745             &m);
746         success = buffer_get_int(&m);
747         if (success == 0) {
748                 debug3("%s: no challenge", __func__);
749                 buffer_free(&m);
750                 return (-1);
751         }
752
753         /* Get the challenge, and format the response */
754         challenge  = buffer_get_string(&m, NULL);
755         buffer_free(&m);
756
757         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
758         (*prompts)[0] = challenge;
759
760         debug3("%s: received challenge: %s", __func__, challenge);
761
762         return (0);
763 }
764
765 int
766 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
767 {
768         Buffer m;
769         int authok;
770
771         debug3("%s: entering", __func__);
772         if (numresponses != 1)
773                 return (-1);
774
775         buffer_init(&m);
776         buffer_put_cstring(&m, responses[0]);
777         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
778
779         mm_request_receive_expect(pmonitor->m_recvfd,
780             MONITOR_ANS_BSDAUTHRESPOND, &m);
781
782         authok = buffer_get_int(&m);
783         buffer_free(&m);
784
785         return ((authok == 0) ? -1 : 0);
786 }
787
788 #ifdef SKEY
789 int
790 mm_skey_query(void *ctx, char **name, char **infotxt,
791    u_int *numprompts, char ***prompts, u_int **echo_on)
792 {
793         Buffer m;
794         u_int success;
795         char *challenge;
796
797         debug3("%s: entering", __func__);
798
799         buffer_init(&m);
800         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m);
801
802         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY,
803             &m);
804         success = buffer_get_int(&m);
805         if (success == 0) {
806                 debug3("%s: no challenge", __func__);
807                 buffer_free(&m);
808                 return (-1);
809         }
810
811         /* Get the challenge, and format the response */
812         challenge  = buffer_get_string(&m, NULL);
813         buffer_free(&m);
814
815         debug3("%s: received challenge: %s", __func__, challenge);
816
817         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
818
819         xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
820         free(challenge);
821
822         return (0);
823 }
824
825 int
826 mm_skey_respond(void *ctx, u_int numresponses, char **responses)
827 {
828         Buffer m;
829         int authok;
830
831         debug3("%s: entering", __func__);
832         if (numresponses != 1)
833                 return (-1);
834
835         buffer_init(&m);
836         buffer_put_cstring(&m, responses[0]);
837         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m);
838
839         mm_request_receive_expect(pmonitor->m_recvfd,
840             MONITOR_ANS_SKEYRESPOND, &m);
841
842         authok = buffer_get_int(&m);
843         buffer_free(&m);
844
845         return ((authok == 0) ? -1 : 0);
846 }
847 #endif /* SKEY */
848
849 #ifdef SSH_AUDIT_EVENTS
850 void
851 mm_audit_event(ssh_audit_event_t event)
852 {
853         Buffer m;
854
855         debug3("%s entering", __func__);
856
857         buffer_init(&m);
858         buffer_put_int(&m, event);
859
860         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
861         buffer_free(&m);
862 }
863
864 void
865 mm_audit_run_command(const char *command)
866 {
867         Buffer m;
868
869         debug3("%s entering command %s", __func__, command);
870
871         buffer_init(&m);
872         buffer_put_cstring(&m, command);
873
874         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
875         buffer_free(&m);
876 }
877 #endif /* SSH_AUDIT_EVENTS */
878
879 #ifdef GSSAPI
880 OM_uint32
881 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
882 {
883         Buffer m;
884         OM_uint32 major;
885
886         /* Client doesn't get to see the context */
887         *ctx = NULL;
888
889         buffer_init(&m);
890         buffer_put_string(&m, goid->elements, goid->length);
891
892         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
893         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
894
895         major = buffer_get_int(&m);
896
897         buffer_free(&m);
898         return (major);
899 }
900
901 OM_uint32
902 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
903     gss_buffer_desc *out, OM_uint32 *flags)
904 {
905         Buffer m;
906         OM_uint32 major;
907         u_int len;
908
909         buffer_init(&m);
910         buffer_put_string(&m, in->value, in->length);
911
912         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
913         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
914
915         major = buffer_get_int(&m);
916         out->value = buffer_get_string(&m, &len);
917         out->length = len;
918         if (flags)
919                 *flags = buffer_get_int(&m);
920
921         buffer_free(&m);
922
923         return (major);
924 }
925
926 OM_uint32
927 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
928 {
929         Buffer m;
930         OM_uint32 major;
931
932         buffer_init(&m);
933         buffer_put_string(&m, gssbuf->value, gssbuf->length);
934         buffer_put_string(&m, gssmic->value, gssmic->length);
935
936         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
937         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
938             &m);
939
940         major = buffer_get_int(&m);
941         buffer_free(&m);
942         return(major);
943 }
944
945 int
946 mm_ssh_gssapi_userok(char *user)
947 {
948         Buffer m;
949         int authenticated = 0;
950
951         buffer_init(&m);
952
953         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
954         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
955                                   &m);
956
957         authenticated = buffer_get_int(&m);
958
959         buffer_free(&m);
960         debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
961         return (authenticated);
962 }
963 #endif /* GSSAPI */
964