blob: 001a8fa1c99a26d547464586877e84cc637f8703 [file] [log] [blame]
djm@openbsd.org56584cc2019-12-15 18:57:30 +00001/* $OpenBSD: monitor_wrap.c,v 1.117 2019/12/15 18:57:30 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100031#include <sys/uio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100034#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038#include <string.h>
Darren Tucker1a3d6e72006-08-05 18:46:47 +100039#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100040
Damien Miller1f0311c2014-05-15 14:24:09 +100041#ifdef WITH_OPENSSL
Damien Millerd7834352006-08-05 12:39:39 +100042#include <openssl/bn.h>
43#include <openssl/dh.h>
Damien Miller01ed2272008-11-05 16:20:46 +110044#include <openssl/evp.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100045#endif
Damien Millerd7834352006-08-05 12:39:39 +100046
Damien Millerb84886b2008-05-19 15:05:07 +100047#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000049#include "ssh.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100050#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000051#include "dh.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100052#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +000053#include "sshbuf.h"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000054#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "kex.h"
Damien Millerd7834352006-08-05 12:39:39 +100057#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058#include "auth.h"
Damien Miller06ebedf2003-02-24 12:03:38 +110059#include "auth-options.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060#include "packet.h"
61#include "mac.h"
62#include "log.h"
Darren Tucker01558b72016-07-18 09:33:25 +100063#include "auth-pam.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064#include "monitor.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#ifdef GSSAPI
66#include "ssh-gss.h"
67#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "monitor_wrap.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000069#include "atomicio.h"
70#include "monitor_fdpass.h"
Damien Miller3f941882006-03-31 23:13:02 +110071#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073#include "channels.h"
74#include "session.h"
Darren Tucker1629c072007-02-19 22:25:37 +110075#include "servconf.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076
markus@openbsd.org091c3022015-01-19 19:52:16 +000077#include "ssherr.h"
78
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079/* Imports */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +000080extern struct monitor *pmonitor;
markus@openbsd.org2808d182018-07-09 21:26:02 +000081extern struct sshbuf *loginmsg;
Damien Miller4e448a32003-05-14 15:11:48 +100082extern ServerOptions options;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083
Damien Miller8f0bf232011-06-20 14:42:23 +100084void
85mm_log_handler(LogLevel level, const char *msg, void *ctx)
86{
markus@openbsd.org235c7c42018-07-09 21:53:45 +000087 struct sshbuf *log_msg;
Damien Miller8f0bf232011-06-20 14:42:23 +100088 struct monitor *mon = (struct monitor *)ctx;
markus@openbsd.org235c7c42018-07-09 21:53:45 +000089 int r;
90 size_t len;
Damien Miller8f0bf232011-06-20 14:42:23 +100091
92 if (mon->m_log_sendfd == -1)
93 fatal("%s: no log channel", __func__);
94
markus@openbsd.org235c7c42018-07-09 21:53:45 +000095 if ((log_msg = sshbuf_new()) == NULL)
96 fatal("%s: sshbuf_new failed", __func__);
Damien Miller8f0bf232011-06-20 14:42:23 +100097
markus@openbsd.org235c7c42018-07-09 21:53:45 +000098 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)
Damien Miller8f0bf232011-06-20 14:42:23 +1000107 fatal("%s: write: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000108 sshbuf_free(log_msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000109}
110
Darren Tucker3e33cec2003-10-02 16:12:36 +1000111int
112mm_is_monitor(void)
113{
114 /*
115 * m_pid is only set in the privileged part, and
116 * points to the unprivileged child.
117 */
Darren Tuckera47c9bc2003-11-03 20:03:25 +1100118 return (pmonitor && pmonitor->m_pid > 0);
Darren Tucker3e33cec2003-10-02 16:12:36 +1000119}
120
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121void
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000122mm_request_send(int sock, enum monitor_reqtype type, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000124 size_t mlen = sshbuf_len(m);
Ben Lindstromb1bdc5a2002-07-04 00:09:26 +0000125 u_char buf[5];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000127 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000129 if (mlen >= 0xffffffff)
130 fatal("%s: bad length %zu", __func__, mlen);
131 POKE_U32(buf, mlen + 1);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000132 buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000133 if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
Damien Millerb253cc42005-05-26 12:23:44 +1000134 fatal("%s: write: %s", __func__, strerror(errno));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000135 if (atomicio(vwrite, sock, sshbuf_mutable_ptr(m), mlen) != mlen)
Damien Millerb253cc42005-05-26 12:23:44 +1000136 fatal("%s: write: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000137}
138
139void
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000140mm_request_receive(int sock, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000142 u_char buf[4], *p = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143 u_int msg_len;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000144 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000146 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147
Damien Millerb253cc42005-05-26 12:23:44 +1000148 if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
Damien Miller4d24b3b2015-03-20 09:11:59 +1100149 if (errno == EPIPE)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000150 cleanup_exit(255);
Damien Millerb253cc42005-05-26 12:23:44 +1000151 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000152 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000153 msg_len = PEEK_U32(buf);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000154 if (msg_len > 256 * 1024)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000155 fatal("%s: read: bad msg_len %d", __func__, msg_len);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000156 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)
Damien Millerb253cc42005-05-26 12:23:44 +1000160 fatal("%s: read: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000161}
162
163void
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000164mm_request_receive_expect(int sock, enum monitor_reqtype type, struct sshbuf *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000165{
166 u_char rtype;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000167 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000169 debug3("%s entering: type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000171 mm_request_receive(sock, m);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000172 if ((r = sshbuf_get_u8(m, &rtype)) != 0)
173 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174 if (rtype != type)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000175 fatal("%s: read: rtype %d != type %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176 rtype, type);
177}
178
Damien Miller1f0311c2014-05-15 14:24:09 +1000179#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180DH *
181mm_choose_dh(int min, int nbits, int max)
182{
183 BIGNUM *p, *g;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000184 int r;
185 u_char success = 0;
186 struct sshbuf *m;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000188 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));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000195 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000197 debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000198 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000199
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000200 if ((r = sshbuf_get_u8(m, &success)) != 0)
201 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 if (success == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000203 fatal("%s: MONITOR_ANS_MODULI failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204
djm@openbsd.org7be85722019-01-21 09:54:11 +0000205 if ((r = sshbuf_get_bignum2(m, &p)) != 0 ||
206 (r = sshbuf_get_bignum2(m, &g)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000207 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000208
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000209 debug3("%s: remaining %zu", __func__, sshbuf_len(m));
210 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211
212 return (dh_new_group(g, p));
213}
Damien Miller1f0311c2014-05-15 14:24:09 +1000214#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
216int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000217mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000218 const u_char *data, size_t datalen, const char *hostkey_alg,
219 const char *sk_provider, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000221 struct kex *kex = *pmonitor->m_pkex;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000222 struct sshbuf *m;
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000223 u_int ndx = kex->host_key_index(key, 0, ssh);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000224 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000226 debug3("%s entering", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000227 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 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000231 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
232 (r = sshbuf_put_u32(m, compat)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000233 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000235 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000236
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000237 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000238 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000239 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000240 fatal("%s: buffer error: %s", __func__, ssh_err(r));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000241 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242
243 return (0);
244}
245
246struct passwd *
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000247mm_getpwnamallow(struct ssh *ssh, const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000249 struct sshbuf *m;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000250 struct passwd *pw;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000251 size_t len;
252 u_int i;
Darren Tucker1629c072007-02-19 22:25:37 +1100253 ServerOptions *newopts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000254 int r;
255 u_char ok;
256 const u_char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000258 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000259
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000260 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));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000265 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000267 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000268 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000270 if ((r = sshbuf_get_u8(m, &ok)) != 0)
271 fatal("%s: buffer error: %s", __func__, ssh_err(r));
272 if (ok == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100273 pw = NULL;
274 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000276
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))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000282 fatal("%s: struct passwd size mismatch", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000283 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 ||
Damien Miller6332da22013-04-23 14:25:52 +1000287#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000288 (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000289#endif
290#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000291 (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000292#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000293 (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));
Darren Tucker1629c072007-02-19 22:25:37 +1100296
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100297out:
Darren Tucker1629c072007-02-19 22:25:37 +1100298 /* copy options block as a Match directive may have changed some */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000299 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
300 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker1629c072007-02-19 22:25:37 +1100301 if (len != sizeof(*newopts))
302 fatal("%s: option block size mismatch", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000303 newopts = xcalloc(sizeof(*newopts), 1);
304 memcpy(newopts, p, sizeof(*newopts));
Damien Millerf2e407e2011-05-20 19:04:14 +1000305
306#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000307 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 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000313 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000314#define M_CP_STRARRAYOPT(x, nx) do { \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000315 newopts->x = newopts->nx == 0 ? \
316 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000317 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 } \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000323 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000324 /* See comment in servconf.h */
325 COPY_MATCH_STRING_OPTS();
326#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000327#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000328
Darren Tucker1629c072007-02-19 22:25:37 +1100329 copy_set_server_options(&options, newopts, 1);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000330 log_change_level(options.log_level);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000331 process_permitopen(ssh, &options);
Darren Tuckera627d422013-06-02 07:31:17 +1000332 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100333
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000334 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335
336 return (pw);
337}
338
Darren Tucker7eb3de02003-10-15 15:56:58 +1000339char *
340mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000341{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000342 struct sshbuf *m;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000343 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000344 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000347
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000348 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);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000352
Darren Tucker7eb3de02003-10-15 15:56:58 +1000353 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000354 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);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000358
Darren Tucker7eb3de02003-10-15 15:56:58 +1000359 /* treat empty banner as missing banner */
360 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000361 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000362 banner = NULL;
363 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000364 return (banner);
365}
366
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367/* Inform the privileged process about service and style */
368
369void
370mm_inform_authserv(char *service, char *style)
371{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000372 struct sshbuf *m;
373 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000375 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000377 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));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000383 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000385 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386}
387
388/* Do the password authentication */
389int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000390mm_auth_password(struct ssh *ssh, char *password)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000392 struct sshbuf *m;
Damien Miller120a1ec2018-07-10 19:39:52 +1000393 int r, authenticated = 0;
394#ifdef USE_PAM
395 u_int maxtries = 0;
396#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000398 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000400 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);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000407 mm_request_receive_expect(pmonitor->m_recvfd,
408 MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000410 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
411 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000412#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000413 if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
414 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller120a1ec2018-07-10 19:39:52 +1000415 if (maxtries > INT_MAX)
416 fatal("%s: bad maxtries %u", __func__, maxtries);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000417 sshpam_set_maxtries_reached(maxtries);
Darren Tucker01558b72016-07-18 09:33:25 +1000418#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000420 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421
422 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000423 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424 return (authenticated);
425}
426
427int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000428mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
429 int pubkey_auth_attempt, struct sshauthopt **authoptp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000431 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000432 pubkey_auth_attempt, authoptp));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433}
434
435int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000436mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
437 const char *user, const char *host, struct sshkey *key)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000439 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000440}
441
442int
djm@openbsd.org95767262016-03-07 19:02:43 +0000443mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000444 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000446 struct sshbuf *m;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000447 int r, allowed = 0;
448 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000450 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451
djm@openbsd.org7c856852018-03-03 03:15:51 +0000452 if (authoptp != NULL)
453 *authoptp = NULL;
454
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000455 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));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000464 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000466 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000467 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000468 MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000470 if ((r = sshbuf_get_u32(m, &allowed)) != 0)
471 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000472 if (allowed && type == MM_USERKEY) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000473 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000474 fatal("%s: sshauthopt_deserialise: %s",
475 __func__, ssh_err(r));
476 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000477 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478
djm@openbsd.org7c856852018-03-03 03:15:51 +0000479 if (authoptp != NULL) {
480 *authoptp = opts;
481 opts = NULL;
482 }
483 sshauthopt_free(opts);
484
485 return allowed;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486}
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
494int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000495mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000496 const u_char *data, size_t datalen, const char *sigalg, u_int compat,
497 struct sshkey_sig_details **sig_detailsp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000499 struct sshbuf *m;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000500 u_int encoded_ret = 0;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000501 int r;
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000502 u_char sig_details_present, flags;
503 u_int counter;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000505 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000506
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000507 if (sig_detailsp != NULL)
508 *sig_detailsp = NULL;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000509 if ((m = sshbuf_new()) == NULL)
510 fatal("%s: sshbuf_new failed", __func__);
511 if ((r = sshkey_puts(key, m)) != 0 ||
512 (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
513 (r = sshbuf_put_string(m, data, datalen)) != 0 ||
514 (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
515 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000516
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000517 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000518
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000519 debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000520 mm_request_receive_expect(pmonitor->m_recvfd,
521 MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000523 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0 ||
524 (r = sshbuf_get_u8(m, &sig_details_present)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000525 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000526 if (sig_details_present && encoded_ret == 0) {
527 if ((r = sshbuf_get_u32(m, &counter)) != 0 ||
528 (r = sshbuf_get_u8(m, &flags)) != 0)
529 fatal("%s: buffer error: %s", __func__, ssh_err(r));
530 if (sig_detailsp != NULL) {
531 *sig_detailsp = xcalloc(1, sizeof(**sig_detailsp));
532 (*sig_detailsp)->sk_counter = counter;
533 (*sig_detailsp)->sk_flags = flags;
534 }
535 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000537 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000539 if (encoded_ret != 0)
540 return SSH_ERR_SIGNATURE_INVALID;
541 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542}
543
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544void
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000545mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000546{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000547 struct sshbuf *m;
548 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549
markus@openbsd.org091c3022015-01-19 19:52:16 +0000550 if ((m = sshbuf_new()) == NULL)
551 fatal("%s: sshbuf_new failed", __func__);
552 if ((r = ssh_packet_get_state(ssh, m)) != 0)
553 fatal("%s: get_state failed: %s",
554 __func__, ssh_err(r));
555 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000556 debug3("%s: Finished sending state", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000557 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558}
559
560int
Damien Miller71a73672006-03-26 14:04:36 +1100561mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000563 struct sshbuf *m;
Darren Tucker09991742004-07-17 17:05:14 +1000564 char *p, *msg;
markus@openbsd.org2808d182018-07-09 21:26:02 +0000565 int success = 0, tmp1 = -1, tmp2 = -1, r;
Damien Miller7207f642008-05-19 15:34:50 +1000566
567 /* Kludge: ensure there are fds free to receive the pty/tty */
568 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
569 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
570 error("%s: cannot allocate fds for pty", __func__);
571 if (tmp1 > 0)
572 close(tmp1);
573 if (tmp2 > 0)
574 close(tmp2);
575 return 0;
576 }
577 close(tmp1);
578 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000580 if ((m = sshbuf_new()) == NULL)
581 fatal("%s: sshbuf_new failed", __func__);
582 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000584 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000585 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000587 if ((r = sshbuf_get_u32(m, &success)) != 0)
588 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000590 debug3("%s: pty alloc failed", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000591 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592 return (0);
593 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000594 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
595 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
596 fatal("%s: buffer error: %s", __func__, ssh_err(r));
597 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
599 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000600 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
markus@openbsd.org2808d182018-07-09 21:26:02 +0000602 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
603 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000604 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000605
Damien Miller54fd7cf2007-09-17 12:04:08 +1000606 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
607 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
608 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609
610 /* Success */
611 return (1);
612}
613
614void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000615mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000617 struct sshbuf *m;
618 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000619
620 if (s->ttyfd == -1)
621 return;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000622 if ((m = sshbuf_new()) == NULL)
623 fatal("%s: sshbuf_new failed", __func__);
624 if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
625 fatal("%s: buffer error: %s", __func__, ssh_err(r));
626 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
627 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628
629 /* closed dup'ed master */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000630 if (s->ptymaster != -1 && close(s->ptymaster) == -1)
Damien Miller7207f642008-05-19 15:34:50 +1000631 error("close(s->ptymaster/%d): %s",
632 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633
634 /* unlink pty from session */
635 s->ttyfd = -1;
636}
637
Damien Miller79418552002-04-23 20:28:48 +1000638#ifdef USE_PAM
639void
Damien Miller3f0786b2019-01-20 10:22:18 +1100640mm_start_pam(struct ssh *ssh)
Damien Miller79418552002-04-23 20:28:48 +1000641{
Damien Miller120a1ec2018-07-10 19:39:52 +1000642 struct sshbuf *m;
Damien Miller79418552002-04-23 20:28:48 +1000643
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000644 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000645 if (!options.use_pam)
646 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000647 if ((m = sshbuf_new()) == NULL)
648 fatal("%s: sshbuf_new failed", __func__);
649 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
Damien Miller79418552002-04-23 20:28:48 +1000650
Damien Miller120a1ec2018-07-10 19:39:52 +1000651 sshbuf_free(m);
Damien Miller79418552002-04-23 20:28:48 +1000652}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000653
Damien Miller1f499fd2003-08-25 13:08:49 +1000654u_int
655mm_do_pam_account(void)
656{
Damien Miller120a1ec2018-07-10 19:39:52 +1000657 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000658 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000659 char *msg;
Damien Miller120a1ec2018-07-10 19:39:52 +1000660 size_t msglen;
661 int r;
Damien Miller1f499fd2003-08-25 13:08:49 +1000662
663 debug3("%s entering", __func__);
664 if (!options.use_pam)
665 fatal("UsePAM=no, but ended up in %s anyway", __func__);
666
Damien Miller120a1ec2018-07-10 19:39:52 +1000667 if ((m = sshbuf_new()) == NULL)
668 fatal("%s: sshbuf_new failed", __func__);
669 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000670
Damien Millera8e06ce2003-11-21 23:48:55 +1100671 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000672 MONITOR_ANS_PAM_ACCOUNT, m);
673 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
674 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
675 (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
676 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +1000677
Damien Miller120a1ec2018-07-10 19:39:52 +1000678 free(msg);
679 sshbuf_free(m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100680
Damien Miller1f499fd2003-08-25 13:08:49 +1000681 debug3("%s returning %d", __func__, ret);
682
683 return (ret);
684}
685
Damien Miller4f9f42a2003-05-10 19:28:02 +1000686void *
687mm_sshpam_init_ctx(Authctxt *authctxt)
688{
Damien Miller120a1ec2018-07-10 19:39:52 +1000689 struct sshbuf *m;
690 int r, success;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000691
692 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000693 if ((m = sshbuf_new()) == NULL)
694 fatal("%s: sshbuf_new failed", __func__);
695 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000696 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000697 mm_request_receive_expect(pmonitor->m_recvfd,
698 MONITOR_ANS_PAM_INIT_CTX, m);
699 if ((r = sshbuf_get_u32(m, &success)) != 0)
700 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000701 if (success == 0) {
702 debug3("%s: pam_init_ctx failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000703 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000704 return (NULL);
705 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000706 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000707 return (authctxt);
708}
709
710int
711mm_sshpam_query(void *ctx, char **name, char **info,
712 u_int *num, char ***prompts, u_int **echo_on)
713{
Damien Miller120a1ec2018-07-10 19:39:52 +1000714 struct sshbuf *m;
715 u_int i, n;
716 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000717
718 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000719 if ((m = sshbuf_new()) == NULL)
720 fatal("%s: sshbuf_new failed", __func__);
721 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000722 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000723 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
724 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
725 (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
726 (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
727 (r = sshbuf_get_u32(m, &n)) != 0 ||
728 (r = sshbuf_get_u32(m, num)) != 0)
729 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000730 debug3("%s: pam_query returned %d", __func__, ret);
Damien Miller120a1ec2018-07-10 19:39:52 +1000731 sshpam_set_maxtries_reached(n);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000732 if (*num > PAM_MAX_NUM_MSG)
Damien Miller10479cc2018-04-10 10:19:02 +1000733 fatal("%s: received %u PAM messages, expected <= %u",
Darren Tuckerd8093e42006-05-04 16:24:34 +1000734 __func__, *num, PAM_MAX_NUM_MSG);
735 *prompts = xcalloc((*num + 1), sizeof(char *));
736 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000737 for (i = 0; i < *num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000738 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
739 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
740 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000741 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000742 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000743 return (ret);
744}
745
746int
747mm_sshpam_respond(void *ctx, u_int num, char **resp)
748{
Damien Miller120a1ec2018-07-10 19:39:52 +1000749 struct sshbuf *m;
750 u_int n, i;
751 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000752
753 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000754 if ((m = sshbuf_new()) == NULL)
755 fatal("%s: sshbuf_new failed", __func__);
756 if ((r = sshbuf_put_u32(m, num)) != 0)
757 fatal("%s: buffer error: %s", __func__, ssh_err(r));
758 for (i = 0; i < num; ++i) {
759 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
760 fatal("%s: buffer error: %s", __func__, ssh_err(r));
761 }
762 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000763 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000764 mm_request_receive_expect(pmonitor->m_recvfd,
765 MONITOR_ANS_PAM_RESPOND, m);
766 if ((r = sshbuf_get_u32(m, &n)) != 0)
767 fatal("%s: buffer error: %s", __func__, ssh_err(r));
768 ret = (int)n; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000769 debug3("%s: pam_respond returned %d", __func__, ret);
Damien Miller120a1ec2018-07-10 19:39:52 +1000770 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000771 return (ret);
772}
773
774void
775mm_sshpam_free_ctx(void *ctxtp)
776{
Damien Miller120a1ec2018-07-10 19:39:52 +1000777 struct sshbuf *m;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000778
779 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000780 if ((m = sshbuf_new()) == NULL)
781 fatal("%s: sshbuf_new failed", __func__);
782 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000783 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000784 mm_request_receive_expect(pmonitor->m_recvfd,
785 MONITOR_ANS_PAM_FREE_CTX, m);
786 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000787}
Damien Miller79418552002-04-23 20:28:48 +1000788#endif /* USE_PAM */
789
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790/* Request process termination */
791
792void
793mm_terminate(void)
794{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000795 struct sshbuf *m;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000797 if ((m = sshbuf_new()) == NULL)
798 fatal("%s: sshbuf_new failed", __func__);
799 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
800 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801}
802
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000803static void
804mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
805 char ***prompts, u_int **echo_on)
806{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000807 *name = xstrdup("");
808 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000809 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100810 *prompts = xcalloc(*numprompts, sizeof(char *));
811 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812 (*echo_on)[0] = 0;
813}
814
815int
816mm_bsdauth_query(void *ctx, char **name, char **infotxt,
817 u_int *numprompts, char ***prompts, u_int **echo_on)
818{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000819 struct sshbuf *m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100820 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821 char *challenge;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000822 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000824 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000826 if ((m = sshbuf_new()) == NULL)
827 fatal("%s: sshbuf_new failed", __func__);
828 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000829
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000830 mm_request_receive_expect(pmonitor->m_recvfd,
831 MONITOR_ANS_BSDAUTHQUERY, m);
832 if ((r = sshbuf_get_u32(m, &success)) != 0)
833 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerb7df3af2003-02-24 11:55:46 +1100834 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000835 debug3("%s: no challenge", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000836 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000837 return (-1);
838 }
839
840 /* Get the challenge, and format the response */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000841 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
842 fatal("%s: buffer error: %s", __func__, ssh_err(r));
843 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844
845 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
846 (*prompts)[0] = challenge;
847
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000848 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849
850 return (0);
851}
852
853int
854mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
855{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000856 struct sshbuf *m;
857 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000859 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000860 if (numresponses != 1)
861 return (-1);
862
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000863 if ((m = sshbuf_new()) == NULL)
864 fatal("%s: sshbuf_new failed", __func__);
865 if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
866 fatal("%s: buffer error: %s", __func__, ssh_err(r));
867 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000869 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000870 MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000872 if ((r = sshbuf_get_u32(m, &authok)) != 0)
873 fatal("%s: buffer error: %s", __func__, ssh_err(r));
874 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875
876 return ((authok == 0) ? -1 : 0);
877}
878
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100879#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100880void
Damien Miller9b655dc2019-01-20 14:55:27 +1100881mm_audit_event(struct ssh *ssh, ssh_audit_event_t event)
Darren Tucker269a1ea2005-02-03 00:20:53 +1100882{
Damien Miller120a1ec2018-07-10 19:39:52 +1000883 struct sshbuf *m;
Darren Tucker1dd32c22018-07-13 13:38:10 +1000884 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +1100885
886 debug3("%s entering", __func__);
887
Damien Miller120a1ec2018-07-10 19:39:52 +1000888 if ((m = sshbuf_new()) == NULL)
Darren Tucker1dd32c22018-07-13 13:38:10 +1000889 fatal("%s: sshbuf_new failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000890 if ((r = sshbuf_put_u32(m, event)) != 0)
891 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100892
Damien Miller120a1ec2018-07-10 19:39:52 +1000893 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
894 sshbuf_free(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100895}
896
897void
898mm_audit_run_command(const char *command)
899{
Damien Miller120a1ec2018-07-10 19:39:52 +1000900 struct sshbuf *m;
Darren Tucker1dd32c22018-07-13 13:38:10 +1000901 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +1100902
903 debug3("%s entering command %s", __func__, command);
904
Damien Miller120a1ec2018-07-10 19:39:52 +1000905 if ((m = sshbuf_new()) == NULL)
Darren Tucker1dd32c22018-07-13 13:38:10 +1000906 fatal("%s: sshbuf_new failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000907 if ((r = sshbuf_put_cstring(m, command)) != 0)
908 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100909
Damien Miller120a1ec2018-07-10 19:39:52 +1000910 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
911 sshbuf_free(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100912}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100913#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +1100914
Darren Tucker0efd1552003-08-26 11:49:55 +1000915#ifdef GSSAPI
916OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000917mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +1000918{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000919 struct sshbuf *m;
Darren Tucker0efd1552003-08-26 11:49:55 +1000920 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000921 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000922
923 /* Client doesn't get to see the context */
924 *ctx = NULL;
925
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000926 if ((m = sshbuf_new()) == NULL)
927 fatal("%s: sshbuf_new failed", __func__);
928 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
929 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000930
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000931 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
932 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000933
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000934 if ((r = sshbuf_get_u32(m, &major)) != 0)
935 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000936
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000937 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000938 return (major);
939}
940
941OM_uint32
942mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000943 gss_buffer_desc *out, OM_uint32 *flagsp)
Darren Tucker0efd1552003-08-26 11:49:55 +1000944{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000945 struct sshbuf *m;
Darren Tucker0efd1552003-08-26 11:49:55 +1000946 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000947 u_int flags;
948 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000949
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000950 if ((m = sshbuf_new()) == NULL)
951 fatal("%s: sshbuf_new failed", __func__);
952 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
953 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000954
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000955 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
956 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000957
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000958 if ((r = sshbuf_get_u32(m, &major)) != 0 ||
djm@openbsd.org0f3958c2018-07-10 09:13:30 +0000959 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000960 fatal("%s: buffer error: %s", __func__, ssh_err(r));
961 if (flagsp != NULL) {
962 if ((r = sshbuf_get_u32(m, &flags)) != 0)
963 fatal("%s: buffer error: %s", __func__, ssh_err(r));
964 *flagsp = flags;
965 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000966
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000967 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000968
969 return (major);
970}
971
Damien Miller0425d402003-11-17 22:18:21 +1100972OM_uint32
973mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
974{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000975 struct sshbuf *m;
Damien Miller0425d402003-11-17 22:18:21 +1100976 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000977 int r;
Damien Miller0425d402003-11-17 22:18:21 +1100978
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000979 if ((m = sshbuf_new()) == NULL)
980 fatal("%s: sshbuf_new failed", __func__);
981 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
982 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
983 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100984
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000985 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
986 mm_request_receive_expect(pmonitor->m_recvfd,
987 MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller0425d402003-11-17 22:18:21 +1100988
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000989 if ((r = sshbuf_get_u32(m, &major)) != 0)
990 fatal("%s: buffer error: %s", __func__, ssh_err(r));
991 sshbuf_free(m);
Damien Miller0425d402003-11-17 22:18:21 +1100992 return(major);
993}
994
Darren Tucker0efd1552003-08-26 11:49:55 +1000995int
996mm_ssh_gssapi_userok(char *user)
997{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000998 struct sshbuf *m;
999 int r, authenticated = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +10001000
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001001 if ((m = sshbuf_new()) == NULL)
1002 fatal("%s: sshbuf_new failed", __func__);
Darren Tucker0efd1552003-08-26 11:49:55 +10001003
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001004 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
1005 mm_request_receive_expect(pmonitor->m_recvfd,
1006 MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001007
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001008 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
1009 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +10001010
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001011 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001012 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1013 return (authenticated);
1014}
1015#endif /* GSSAPI */