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