]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/monitor_wrap.c
MFH r338661 through r339253.
[FreeBSD/FreeBSD.git] / crypto / openssh / monitor_wrap.c
1 /* $OpenBSD: monitor_wrap.c,v 1.107 2018/07/20 03:46:34 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 ((p = BN_new()) == NULL)
206                 fatal("%s: BN_new failed", __func__);
207         if ((g = BN_new()) == NULL)
208                 fatal("%s: BN_new failed", __func__);
209         if ((r = sshbuf_get_bignum2(m, p)) != 0 ||
210             (r = sshbuf_get_bignum2(m, g)) != 0)
211                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
212
213         debug3("%s: remaining %zu", __func__, sshbuf_len(m));
214         sshbuf_free(m);
215
216         return (dh_new_group(g, p));
217 }
218 #endif
219
220 int
221 mm_sshkey_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
222     const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
223 {
224         struct kex *kex = *pmonitor->m_pkex;
225         struct sshbuf *m;
226         u_int ndx = kex->host_key_index(key, 0, active_state);
227         int r;
228
229         debug3("%s entering", __func__);
230
231         if ((m = sshbuf_new()) == NULL)
232                 fatal("%s: sshbuf_new failed", __func__);
233         if ((r = sshbuf_put_u32(m, ndx)) != 0 ||
234             (r = sshbuf_put_string(m, data, datalen)) != 0 ||
235             (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
236             (r = sshbuf_put_u32(m, compat)) != 0)
237                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
238
239         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
240
241         debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
242         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
243         if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
244                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
245         sshbuf_free(m);
246
247         return (0);
248 }
249
250 login_cap_t *
251 mm_login_getpwclass(const struct passwd *pwent)
252 {
253         int r;
254         struct sshbuf *m;
255         char rc;
256         login_cap_t *lc;
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_passwd(m, pwent)) != 0)
263                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
264
265         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GETPWCLASS, m);
266
267         debug3("%s: waiting for MONITOR_ANS_GETPWCLASS", __func__);
268         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GETPWCLASS, m);
269
270         if ((r = sshbuf_get_u8(m, &rc)) != 0)
271                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
272
273         if (rc == 0) {
274                 lc = NULL;
275                 goto out;
276         }
277
278         lc = xmalloc(sizeof(*lc));
279         if ((r = sshbuf_get_cstring(m, &lc->lc_class, NULL)) != 0 ||
280             (r = sshbuf_get_cstring(m, &lc->lc_cap, NULL)) != 0 ||
281             (r = sshbuf_get_cstring(m, &lc->lc_style, NULL)) != 0)
282                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
283
284  out:
285         sshbuf_free(m);
286
287         return (lc);
288 }
289
290 void
291 mm_login_close(login_cap_t *lc)
292 {
293         if (lc == NULL)
294                 return;
295         free(lc->lc_style);
296         free(lc->lc_class);
297         free(lc->lc_cap);
298         free(lc);
299 }
300
301 struct passwd *
302 mm_getpwnamallow(const char *username)
303 {
304         struct ssh *ssh = active_state;         /* XXX */
305         struct sshbuf *m;
306         struct passwd *pw;
307         size_t len;
308         u_int i;
309         ServerOptions *newopts;
310         int r;
311         u_char ok;
312         const u_char *p;
313
314         debug3("%s entering", __func__);
315
316         if ((m = sshbuf_new()) == NULL)
317                 fatal("%s: sshbuf_new failed", __func__);
318         if ((r = sshbuf_put_cstring(m, username)) != 0)
319                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
320
321         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
322
323         debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
324         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
325
326         if ((r = sshbuf_get_u8(m, &ok)) != 0)
327                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
328         if (ok == 0) {
329                 pw = NULL;
330                 goto out;
331         }
332
333         pw = sshbuf_get_passwd(m);
334         if (pw == NULL)
335                 fatal("%s: receive get struct passwd failed", __func__);
336
337 out:
338         /* copy options block as a Match directive may have changed some */
339         if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
340                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
341         if (len != sizeof(*newopts))
342                 fatal("%s: option block size mismatch", __func__);
343         newopts = xcalloc(sizeof(*newopts), 1);
344         memcpy(newopts, p, sizeof(*newopts));
345
346 #define M_CP_STROPT(x) do { \
347                 if (newopts->x != NULL) { \
348                         if ((r = sshbuf_get_cstring(m, \
349                             &newopts->x, NULL)) != 0) \
350                                 fatal("%s: buffer error: %s", \
351                                     __func__, ssh_err(r)); \
352                 } \
353         } while (0)
354 #define M_CP_STRARRAYOPT(x, nx) do { \
355                 newopts->x = newopts->nx == 0 ? \
356                     NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
357                 for (i = 0; i < newopts->nx; i++) { \
358                         if ((r = sshbuf_get_cstring(m, \
359                             &newopts->x[i], NULL)) != 0) \
360                                 fatal("%s: buffer error: %s", \
361                                     __func__, ssh_err(r)); \
362                 } \
363         } while (0)
364         /* See comment in servconf.h */
365         COPY_MATCH_STRING_OPTS();
366 #undef M_CP_STROPT
367 #undef M_CP_STRARRAYOPT
368
369         copy_set_server_options(&options, newopts, 1);
370         log_change_level(options.log_level);
371         process_permitopen(ssh, &options);
372         free(newopts);
373
374         sshbuf_free(m);
375
376         return (pw);
377 }
378
379 char *
380 mm_auth2_read_banner(void)
381 {
382         struct sshbuf *m;
383         char *banner;
384         int r;
385
386         debug3("%s entering", __func__);
387
388         if ((m = sshbuf_new()) == NULL)
389                 fatal("%s: sshbuf_new failed", __func__);
390         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
391         sshbuf_reset(m);
392
393         mm_request_receive_expect(pmonitor->m_recvfd,
394             MONITOR_ANS_AUTH2_READ_BANNER, m);
395         if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
396                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
397         sshbuf_free(m);
398
399         /* treat empty banner as missing banner */
400         if (strlen(banner) == 0) {
401                 free(banner);
402                 banner = NULL;
403         }
404         return (banner);
405 }
406
407 /* Inform the privileged process about service and style */
408
409 void
410 mm_inform_authserv(char *service, char *style)
411 {
412         struct sshbuf *m;
413         int r;
414
415         debug3("%s entering", __func__);
416
417         if ((m = sshbuf_new()) == NULL)
418                 fatal("%s: sshbuf_new failed", __func__);
419         if ((r = sshbuf_put_cstring(m, service)) != 0 ||
420             (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
421                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
422
423         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
424
425         sshbuf_free(m);
426 }
427
428 /* Do the password authentication */
429 int
430 mm_auth_password(struct ssh *ssh, char *password)
431 {
432         struct sshbuf *m;
433         int r, authenticated = 0;
434 #ifdef USE_PAM
435         u_int maxtries = 0;
436 #endif
437
438         debug3("%s entering", __func__);
439
440         if ((m = sshbuf_new()) == NULL)
441                 fatal("%s: sshbuf_new failed", __func__);
442         if ((r = sshbuf_put_cstring(m, password)) != 0)
443                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
444         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
445
446         debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
447         mm_request_receive_expect(pmonitor->m_recvfd,
448             MONITOR_ANS_AUTHPASSWORD, m);
449
450         if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
451                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
452 #ifdef USE_PAM
453         if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
454                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
455         if (maxtries > INT_MAX)
456                 fatal("%s: bad maxtries %u", __func__, maxtries);
457         sshpam_set_maxtries_reached(maxtries);
458 #endif
459
460         sshbuf_free(m);
461
462         debug3("%s: user %sauthenticated",
463             __func__, authenticated ? "" : "not ");
464         return (authenticated);
465 }
466
467 int
468 mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
469     int pubkey_auth_attempt, struct sshauthopt **authoptp)
470 {
471         return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
472             pubkey_auth_attempt, authoptp));
473 }
474
475 int
476 mm_hostbased_key_allowed(struct passwd *pw, const char *user, const char *host,
477     struct sshkey *key)
478 {
479         return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
480 }
481
482 int
483 mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
484     struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
485 {
486         struct sshbuf *m;
487         int r, allowed = 0;
488         struct sshauthopt *opts = NULL;
489
490         debug3("%s entering", __func__);
491
492         if (authoptp != NULL)
493                 *authoptp = NULL;
494
495         if ((m = sshbuf_new()) == NULL)
496                 fatal("%s: sshbuf_new failed", __func__);
497         if ((r = sshbuf_put_u32(m, type)) != 0 ||
498             (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
499             (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
500             (r = sshkey_puts(key, m)) != 0 ||
501             (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
502                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
503
504         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
505
506         debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
507         mm_request_receive_expect(pmonitor->m_recvfd,
508             MONITOR_ANS_KEYALLOWED, m);
509
510         if ((r = sshbuf_get_u32(m, &allowed)) != 0)
511                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
512         if (allowed && type == MM_USERKEY) {
513                 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
514                         fatal("%s: sshauthopt_deserialise: %s",
515                             __func__, ssh_err(r));
516         }
517         sshbuf_free(m);
518
519         if (authoptp != NULL) {
520                 *authoptp = opts;
521                 opts = NULL;
522         }
523         sshauthopt_free(opts);
524
525         return allowed;
526 }
527
528 /*
529  * This key verify needs to send the key type along, because the
530  * privileged parent makes the decision if the key is allowed
531  * for authentication.
532  */
533
534 int
535 mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
536     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
537 {
538         struct sshbuf *m;
539         u_int encoded_ret = 0;
540         int r;
541
542         debug3("%s entering", __func__);
543
544
545         if ((m = sshbuf_new()) == NULL)
546                 fatal("%s: sshbuf_new failed", __func__);
547         if ((r = sshkey_puts(key, m)) != 0 ||
548             (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
549             (r = sshbuf_put_string(m, data, datalen)) != 0 ||
550             (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
551                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
552
553         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
554
555         debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
556         mm_request_receive_expect(pmonitor->m_recvfd,
557             MONITOR_ANS_KEYVERIFY, m);
558
559         if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
560                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
561
562         sshbuf_free(m);
563
564         if (encoded_ret != 0)
565                 return SSH_ERR_SIGNATURE_INVALID;
566         return 0;
567 }
568
569 void
570 mm_send_keystate(struct monitor *monitor)
571 {
572         struct ssh *ssh = active_state;         /* XXX */
573         struct sshbuf *m;
574         int r;
575
576         if ((m = sshbuf_new()) == NULL)
577                 fatal("%s: sshbuf_new failed", __func__);
578         if ((r = ssh_packet_get_state(ssh, m)) != 0)
579                 fatal("%s: get_state failed: %s",
580                     __func__, ssh_err(r));
581         mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
582         debug3("%s: Finished sending state", __func__);
583         sshbuf_free(m);
584 }
585
586 int
587 mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
588 {
589         struct sshbuf *m;
590         char *p, *msg;
591         int success = 0, tmp1 = -1, tmp2 = -1, r;
592
593         /* Kludge: ensure there are fds free to receive the pty/tty */
594         if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
595             (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
596                 error("%s: cannot allocate fds for pty", __func__);
597                 if (tmp1 > 0)
598                         close(tmp1);
599                 if (tmp2 > 0)
600                         close(tmp2);
601                 return 0;
602         }
603         close(tmp1);
604         close(tmp2);
605
606         if ((m = sshbuf_new()) == NULL)
607                 fatal("%s: sshbuf_new failed", __func__);
608         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
609
610         debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
611         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
612
613         if ((r = sshbuf_get_u32(m, &success)) != 0)
614                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
615         if (success == 0) {
616                 debug3("%s: pty alloc failed", __func__);
617                 sshbuf_free(m);
618                 return (0);
619         }
620         if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
621             (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
622                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
623         sshbuf_free(m);
624
625         strlcpy(namebuf, p, namebuflen); /* Possible truncation */
626         free(p);
627
628         if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
629                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
630         free(msg);
631
632         if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
633             (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
634                 fatal("%s: receive fds failed", __func__);
635
636         /* Success */
637         return (1);
638 }
639
640 void
641 mm_session_pty_cleanup2(Session *s)
642 {
643         struct sshbuf *m;
644         int r;
645
646         if (s->ttyfd == -1)
647                 return;
648         if ((m = sshbuf_new()) == NULL)
649                 fatal("%s: sshbuf_new failed", __func__);
650         if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
651                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
652         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
653         sshbuf_free(m);
654
655         /* closed dup'ed master */
656         if (s->ptymaster != -1 && close(s->ptymaster) < 0)
657                 error("close(s->ptymaster/%d): %s",
658                     s->ptymaster, strerror(errno));
659
660         /* unlink pty from session */
661         s->ttyfd = -1;
662 }
663
664 #ifdef USE_PAM
665 void
666 mm_start_pam(Authctxt *authctxt)
667 {
668         struct sshbuf *m;
669
670         debug3("%s entering", __func__);
671         if (!options.use_pam)
672                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
673         if ((m = sshbuf_new()) == NULL)
674                 fatal("%s: sshbuf_new failed", __func__);
675         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
676
677         sshbuf_free(m);
678 }
679
680 u_int
681 mm_do_pam_account(void)
682 {
683         struct sshbuf *m;
684         u_int ret;
685         char *msg;
686         size_t msglen;
687         int r;
688
689         debug3("%s entering", __func__);
690         if (!options.use_pam)
691                 fatal("UsePAM=no, but ended up in %s anyway", __func__);
692
693         if ((m = sshbuf_new()) == NULL)
694                 fatal("%s: sshbuf_new failed", __func__);
695         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
696
697         mm_request_receive_expect(pmonitor->m_recvfd,
698             MONITOR_ANS_PAM_ACCOUNT, m);
699         if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
700             (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
701             (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
702                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
703
704         free(msg);
705         sshbuf_free(m);
706
707         debug3("%s returning %d", __func__, ret);
708
709         return (ret);
710 }
711
712 void *
713 mm_sshpam_init_ctx(Authctxt *authctxt)
714 {
715         struct sshbuf *m;
716         int r, success;
717
718         debug3("%s", __func__);
719         if ((m = sshbuf_new()) == NULL)
720                 fatal("%s: sshbuf_new failed", __func__);
721         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
722         debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
723         mm_request_receive_expect(pmonitor->m_recvfd,
724             MONITOR_ANS_PAM_INIT_CTX, m);
725         if ((r = sshbuf_get_u32(m, &success)) != 0)
726                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
727         if (success == 0) {
728                 debug3("%s: pam_init_ctx failed", __func__);
729                 sshbuf_free(m);
730                 return (NULL);
731         }
732         sshbuf_free(m);
733         return (authctxt);
734 }
735
736 int
737 mm_sshpam_query(void *ctx, char **name, char **info,
738     u_int *num, char ***prompts, u_int **echo_on)
739 {
740         struct sshbuf *m;
741         u_int i, n;
742         int r, ret;
743
744         debug3("%s", __func__);
745         if ((m = sshbuf_new()) == NULL)
746                 fatal("%s: sshbuf_new failed", __func__);
747         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
748         debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
749         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
750         if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
751             (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
752             (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
753             (r = sshbuf_get_u32(m, &n)) != 0 ||
754             (r = sshbuf_get_u32(m, num)) != 0)
755                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
756         debug3("%s: pam_query returned %d", __func__, ret);
757         sshpam_set_maxtries_reached(n);
758         if (*num > PAM_MAX_NUM_MSG)
759                 fatal("%s: received %u PAM messages, expected <= %u",
760                     __func__, *num, PAM_MAX_NUM_MSG);
761         *prompts = xcalloc((*num + 1), sizeof(char *));
762         *echo_on = xcalloc((*num + 1), sizeof(u_int));
763         for (i = 0; i < *num; ++i) {
764                 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
765                     (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
766                         fatal("%s: buffer error: %s", __func__, ssh_err(r));
767         }
768         sshbuf_free(m);
769         return (ret);
770 }
771
772 int
773 mm_sshpam_respond(void *ctx, u_int num, char **resp)
774 {
775         struct sshbuf *m;
776         u_int n, i;
777         int r, ret;
778
779         debug3("%s", __func__);
780         if ((m = sshbuf_new()) == NULL)
781                 fatal("%s: sshbuf_new failed", __func__);
782         if ((r = sshbuf_put_u32(m, num)) != 0)
783                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
784         for (i = 0; i < num; ++i) {
785                 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
786                         fatal("%s: buffer error: %s", __func__, ssh_err(r));
787         }
788         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
789         debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
790         mm_request_receive_expect(pmonitor->m_recvfd,
791             MONITOR_ANS_PAM_RESPOND, m);
792         if ((r = sshbuf_get_u32(m, &n)) != 0)
793                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
794         ret = (int)n; /* XXX */
795         debug3("%s: pam_respond returned %d", __func__, ret);
796         sshbuf_free(m);
797         return (ret);
798 }
799
800 void
801 mm_sshpam_free_ctx(void *ctxtp)
802 {
803         struct sshbuf *m;
804
805         debug3("%s", __func__);
806         if ((m = sshbuf_new()) == NULL)
807                 fatal("%s: sshbuf_new failed", __func__);
808         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
809         debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
810         mm_request_receive_expect(pmonitor->m_recvfd,
811             MONITOR_ANS_PAM_FREE_CTX, m);
812         sshbuf_free(m);
813 }
814 #endif /* USE_PAM */
815
816 /* Request process termination */
817
818 void
819 mm_terminate(void)
820 {
821         struct sshbuf *m;
822
823         if ((m = sshbuf_new()) == NULL)
824                 fatal("%s: sshbuf_new failed", __func__);
825         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
826         sshbuf_free(m);
827 }
828
829 static void
830 mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
831     char ***prompts, u_int **echo_on)
832 {
833         *name = xstrdup("");
834         *infotxt = xstrdup("");
835         *numprompts = 1;
836         *prompts = xcalloc(*numprompts, sizeof(char *));
837         *echo_on = xcalloc(*numprompts, sizeof(u_int));
838         (*echo_on)[0] = 0;
839 }
840
841 int
842 mm_bsdauth_query(void *ctx, char **name, char **infotxt,
843    u_int *numprompts, char ***prompts, u_int **echo_on)
844 {
845         struct sshbuf *m;
846         u_int success;
847         char *challenge;
848         int r;
849
850         debug3("%s: entering", __func__);
851
852         if ((m = sshbuf_new()) == NULL)
853                 fatal("%s: sshbuf_new failed", __func__);
854         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
855
856         mm_request_receive_expect(pmonitor->m_recvfd,
857             MONITOR_ANS_BSDAUTHQUERY, m);
858         if ((r = sshbuf_get_u32(m, &success)) != 0)
859                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
860         if (success == 0) {
861                 debug3("%s: no challenge", __func__);
862                 sshbuf_free(m);
863                 return (-1);
864         }
865
866         /* Get the challenge, and format the response */
867         if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
868                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
869         sshbuf_free(m);
870
871         mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
872         (*prompts)[0] = challenge;
873
874         debug3("%s: received challenge: %s", __func__, challenge);
875
876         return (0);
877 }
878
879 int
880 mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
881 {
882         struct sshbuf *m;
883         int r, authok;
884
885         debug3("%s: entering", __func__);
886         if (numresponses != 1)
887                 return (-1);
888
889         if ((m = sshbuf_new()) == NULL)
890                 fatal("%s: sshbuf_new failed", __func__);
891         if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
892                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
893         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
894
895         mm_request_receive_expect(pmonitor->m_recvfd,
896             MONITOR_ANS_BSDAUTHRESPOND, m);
897
898         if ((r = sshbuf_get_u32(m, &authok)) != 0)
899                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
900         sshbuf_free(m);
901
902         return ((authok == 0) ? -1 : 0);
903 }
904
905 #ifdef SSH_AUDIT_EVENTS
906 void
907 mm_audit_event(ssh_audit_event_t event)
908 {
909         struct sshbuf *m;
910         int r;
911
912         debug3("%s entering", __func__);
913
914         if ((m = sshbuf_new()) == NULL)
915                 fatal("%s: sshbuf_new failed", __func__);
916         if ((r = sshbuf_put_u32(m, event)) != 0)
917                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
918
919         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
920         sshbuf_free(m);
921 }
922
923 void
924 mm_audit_run_command(const char *command)
925 {
926         struct sshbuf *m;
927         int r;
928
929         debug3("%s entering command %s", __func__, command);
930
931         if ((m = sshbuf_new()) == NULL)
932                 fatal("%s: sshbuf_new failed", __func__);
933         if ((r = sshbuf_put_cstring(m, command)) != 0)
934                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
935
936         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
937         sshbuf_free(m);
938 }
939 #endif /* SSH_AUDIT_EVENTS */
940
941 #ifdef GSSAPI
942 OM_uint32
943 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
944 {
945         struct sshbuf *m;
946         OM_uint32 major;
947         int r;
948
949         /* Client doesn't get to see the context */
950         *ctx = NULL;
951
952         if ((m = sshbuf_new()) == NULL)
953                 fatal("%s: sshbuf_new failed", __func__);
954         if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
955                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
956
957         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
958         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
959
960         if ((r = sshbuf_get_u32(m, &major)) != 0)
961                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
962
963         sshbuf_free(m);
964         return (major);
965 }
966
967 OM_uint32
968 mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
969     gss_buffer_desc *out, OM_uint32 *flagsp)
970 {
971         struct sshbuf *m;
972         OM_uint32 major;
973         u_int flags;
974         int r;
975
976         if ((m = sshbuf_new()) == NULL)
977                 fatal("%s: sshbuf_new failed", __func__);
978         if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
979                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
980
981         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
982         mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
983
984         if ((r = sshbuf_get_u32(m, &major)) != 0 ||
985             (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
986                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
987         if (flagsp != NULL) {
988                 if ((r = sshbuf_get_u32(m, &flags)) != 0)
989                         fatal("%s: buffer error: %s", __func__, ssh_err(r));
990                 *flagsp = flags;
991         }
992
993         sshbuf_free(m);
994
995         return (major);
996 }
997
998 OM_uint32
999 mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
1000 {
1001         struct sshbuf *m;
1002         OM_uint32 major;
1003         int r;
1004
1005         if ((m = sshbuf_new()) == NULL)
1006                 fatal("%s: sshbuf_new failed", __func__);
1007         if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
1008             (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
1009                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1010
1011         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
1012         mm_request_receive_expect(pmonitor->m_recvfd,
1013             MONITOR_ANS_GSSCHECKMIC, m);
1014
1015         if ((r = sshbuf_get_u32(m, &major)) != 0)
1016                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1017         sshbuf_free(m);
1018         return(major);
1019 }
1020
1021 int
1022 mm_ssh_gssapi_userok(char *user)
1023 {
1024         struct sshbuf *m;
1025         int r, authenticated = 0;
1026
1027         if ((m = sshbuf_new()) == NULL)
1028                 fatal("%s: sshbuf_new failed", __func__);
1029
1030         mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1031         mm_request_receive_expect(pmonitor->m_recvfd,
1032             MONITOR_ANS_GSSUSEROK, m);
1033
1034         if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1035                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1036
1037         sshbuf_free(m);
1038         debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1039         return (authenticated);
1040 }
1041 #endif /* GSSAPI */