blob: 9e3c7cd17e0973a5a4c07c37a5afff797e0c4b8d [file] [log] [blame]
djm@openbsd.org04c091f2019-01-19 21:43:56 +00001/* $OpenBSD: monitor_wrap.c,v 1.111 2019/01/19 21:43:56 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
205 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000206 fatal("%s: BN_new failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 if ((g = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000208 fatal("%s: BN_new failed", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000209 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));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000213 debug3("%s: remaining %zu", __func__, sshbuf_len(m));
214 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215
216 return (dh_new_group(g, p));
217}
Damien Miller1f0311c2014-05-15 14:24:09 +1000218#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219
220int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000221mm_sshkey_sign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000222 const u_char *data, size_t datalen, const char *hostkey_alg, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000224 struct kex *kex = *pmonitor->m_pkex;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000225 struct sshbuf *m;
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000226 u_int ndx = kex->host_key_index(key, 0, ssh);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000227 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000229 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000231 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 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000235 (r = sshbuf_put_cstring(m, hostkey_alg)) != 0 ||
236 (r = sshbuf_put_u32(m, compat)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000237 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000239 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000241 debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000242 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, m);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +0000243 if ((r = sshbuf_get_string(m, sigp, lenp)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000244 fatal("%s: buffer error: %s", __func__, ssh_err(r));
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000245 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246
247 return (0);
248}
249
250struct passwd *
djm@openbsd.org3a00a922019-01-19 21:41:18 +0000251mm_getpwnamallow(struct ssh *ssh, const char *username)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000253 struct sshbuf *m;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000254 struct passwd *pw;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000255 size_t len;
256 u_int i;
Darren Tucker1629c072007-02-19 22:25:37 +1100257 ServerOptions *newopts;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000258 int r;
259 u_char ok;
260 const u_char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000262 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000264 if ((m = sshbuf_new()) == NULL)
265 fatal("%s: sshbuf_new failed", __func__);
266 if ((r = sshbuf_put_cstring(m, username)) != 0)
267 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000269 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000271 debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000272 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000274 if ((r = sshbuf_get_u8(m, &ok)) != 0)
275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
276 if (ok == 0) {
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100277 pw = NULL;
278 goto out;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000280
281 /* XXX don't like passing struct passwd like this */
282 pw = xcalloc(sizeof(*pw), 1);
283 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
284 fatal("%s: buffer error: %s", __func__, ssh_err(r));
285 if (len != sizeof(*pw))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000286 fatal("%s: struct passwd size mismatch", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000287 memcpy(pw, p, sizeof(*pw));
288
289 if ((r = sshbuf_get_cstring(m, &pw->pw_name, NULL)) != 0 ||
290 (r = sshbuf_get_cstring(m, &pw->pw_passwd, NULL)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000291#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000292 (r = sshbuf_get_cstring(m, &pw->pw_gecos, NULL)) != 0 ||
Damien Miller6332da22013-04-23 14:25:52 +1000293#endif
294#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000295 (r = sshbuf_get_cstring(m, &pw->pw_class, NULL)) != 0 ||
Kevin Steves7e147602002-03-22 18:07:17 +0000296#endif
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000297 (r = sshbuf_get_cstring(m, &pw->pw_dir, NULL)) != 0 ||
298 (r = sshbuf_get_cstring(m, &pw->pw_shell, NULL)) != 0)
299 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker1629c072007-02-19 22:25:37 +1100300
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100301out:
Darren Tucker1629c072007-02-19 22:25:37 +1100302 /* copy options block as a Match directive may have changed some */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000303 if ((r = sshbuf_get_string_direct(m, &p, &len)) != 0)
304 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker1629c072007-02-19 22:25:37 +1100305 if (len != sizeof(*newopts))
306 fatal("%s: option block size mismatch", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000307 newopts = xcalloc(sizeof(*newopts), 1);
308 memcpy(newopts, p, sizeof(*newopts));
Damien Millerf2e407e2011-05-20 19:04:14 +1000309
310#define M_CP_STROPT(x) do { \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000311 if (newopts->x != NULL) { \
312 if ((r = sshbuf_get_cstring(m, \
313 &newopts->x, NULL)) != 0) \
314 fatal("%s: buffer error: %s", \
315 __func__, ssh_err(r)); \
316 } \
Damien Millerf2e407e2011-05-20 19:04:14 +1000317 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000318#define M_CP_STRARRAYOPT(x, nx) do { \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000319 newopts->x = newopts->nx == 0 ? \
320 NULL : xcalloc(newopts->nx, sizeof(*newopts->x)); \
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000321 for (i = 0; i < newopts->nx; i++) { \
322 if ((r = sshbuf_get_cstring(m, \
323 &newopts->x[i], NULL)) != 0) \
324 fatal("%s: buffer error: %s", \
325 __func__, ssh_err(r)); \
326 } \
djm@openbsd.org66bf74a2017-10-02 19:33:20 +0000327 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000328 /* See comment in servconf.h */
329 COPY_MATCH_STRING_OPTS();
330#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000331#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000332
Darren Tucker1629c072007-02-19 22:25:37 +1100333 copy_set_server_options(&options, newopts, 1);
djm@openbsd.org54cd41a2017-05-17 01:24:17 +0000334 log_change_level(options.log_level);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000335 process_permitopen(ssh, &options);
Darren Tuckera627d422013-06-02 07:31:17 +1000336 free(newopts);
Darren Tucker1629c072007-02-19 22:25:37 +1100337
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000338 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339
340 return (pw);
341}
342
Darren Tucker7eb3de02003-10-15 15:56:58 +1000343char *
344mm_auth2_read_banner(void)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000345{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000346 struct sshbuf *m;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000347 char *banner;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000348 int r;
Damien Miller5ad9fd92002-05-13 11:07:41 +1000349
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000350 debug3("%s entering", __func__);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000351
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000352 if ((m = sshbuf_new()) == NULL)
353 fatal("%s: sshbuf_new failed", __func__);
354 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, m);
355 sshbuf_reset(m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000356
Darren Tucker7eb3de02003-10-15 15:56:58 +1000357 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000358 MONITOR_ANS_AUTH2_READ_BANNER, m);
359 if ((r = sshbuf_get_cstring(m, &banner, NULL)) != 0)
360 fatal("%s: buffer error: %s", __func__, ssh_err(r));
361 sshbuf_free(m);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000362
Darren Tucker7eb3de02003-10-15 15:56:58 +1000363 /* treat empty banner as missing banner */
364 if (strlen(banner) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000365 free(banner);
Darren Tucker7eb3de02003-10-15 15:56:58 +1000366 banner = NULL;
367 }
Damien Miller5ad9fd92002-05-13 11:07:41 +1000368 return (banner);
369}
370
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371/* Inform the privileged process about service and style */
372
373void
374mm_inform_authserv(char *service, char *style)
375{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000376 struct sshbuf *m;
377 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000379 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000381 if ((m = sshbuf_new()) == NULL)
382 fatal("%s: sshbuf_new failed", __func__);
383 if ((r = sshbuf_put_cstring(m, service)) != 0 ||
384 (r = sshbuf_put_cstring(m, style ? style : "")) != 0)
385 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000387 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000389 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390}
391
392/* Do the password authentication */
393int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000394mm_auth_password(struct ssh *ssh, char *password)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000396 struct sshbuf *m;
Damien Miller120a1ec2018-07-10 19:39:52 +1000397 int r, authenticated = 0;
398#ifdef USE_PAM
399 u_int maxtries = 0;
400#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000401
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000402 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000404 if ((m = sshbuf_new()) == NULL)
405 fatal("%s: sshbuf_new failed", __func__);
406 if ((r = sshbuf_put_cstring(m, password)) != 0)
407 fatal("%s: buffer error: %s", __func__, ssh_err(r));
408 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000410 debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000411 mm_request_receive_expect(pmonitor->m_recvfd,
412 MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000413
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000414 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
415 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker01558b72016-07-18 09:33:25 +1000416#ifdef USE_PAM
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000417 if ((r = sshbuf_get_u32(m, &maxtries)) != 0)
418 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller120a1ec2018-07-10 19:39:52 +1000419 if (maxtries > INT_MAX)
420 fatal("%s: bad maxtries %u", __func__, maxtries);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000421 sshpam_set_maxtries_reached(maxtries);
Darren Tucker01558b72016-07-18 09:33:25 +1000422#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000424 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425
426 debug3("%s: user %sauthenticated",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000427 __func__, authenticated ? "" : "not ");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428 return (authenticated);
429}
430
431int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000432mm_user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
433 int pubkey_auth_attempt, struct sshauthopt **authoptp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434{
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000435 return (mm_key_allowed(MM_USERKEY, NULL, NULL, key,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000436 pubkey_auth_attempt, authoptp));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437}
438
439int
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000440mm_hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
441 const char *user, const char *host, struct sshkey *key)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000443 return (mm_key_allowed(MM_HOSTKEY, user, host, key, 0, NULL));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444}
445
446int
djm@openbsd.org95767262016-03-07 19:02:43 +0000447mm_key_allowed(enum mm_keytype type, const char *user, const char *host,
djm@openbsd.org7c856852018-03-03 03:15:51 +0000448 struct sshkey *key, int pubkey_auth_attempt, struct sshauthopt **authoptp)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000450 struct sshbuf *m;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000451 int r, allowed = 0;
452 struct sshauthopt *opts = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000454 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455
djm@openbsd.org7c856852018-03-03 03:15:51 +0000456 if (authoptp != NULL)
457 *authoptp = NULL;
458
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000459 if ((m = sshbuf_new()) == NULL)
460 fatal("%s: sshbuf_new failed", __func__);
461 if ((r = sshbuf_put_u32(m, type)) != 0 ||
462 (r = sshbuf_put_cstring(m, user ? user : "")) != 0 ||
463 (r = sshbuf_put_cstring(m, host ? host : "")) != 0 ||
464 (r = sshkey_puts(key, m)) != 0 ||
465 (r = sshbuf_put_u32(m, pubkey_auth_attempt)) != 0)
466 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000468 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000470 debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000471 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000472 MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000474 if ((r = sshbuf_get_u32(m, &allowed)) != 0)
475 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org7c856852018-03-03 03:15:51 +0000476 if (allowed && type == MM_USERKEY) {
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000477 if ((r = sshauthopt_deserialise(m, &opts)) != 0)
djm@openbsd.org7c856852018-03-03 03:15:51 +0000478 fatal("%s: sshauthopt_deserialise: %s",
479 __func__, ssh_err(r));
480 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000481 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482
djm@openbsd.org7c856852018-03-03 03:15:51 +0000483 if (authoptp != NULL) {
484 *authoptp = opts;
485 opts = NULL;
486 }
487 sshauthopt_free(opts);
488
489 return allowed;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490}
491
492/*
493 * This key verify needs to send the key type along, because the
494 * privileged parent makes the decision if the key is allowed
495 * for authentication.
496 */
497
498int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000499mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000500 const u_char *data, size_t datalen, const char *sigalg, u_int compat)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000502 struct sshbuf *m;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000503 u_int encoded_ret = 0;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000504 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000505
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000506 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508
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
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000523 if ((r = sshbuf_get_u32(m, &encoded_ret)) != 0)
524 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000525
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000526 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000528 if (encoded_ret != 0)
529 return SSH_ERR_SIGNATURE_INVALID;
530 return 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531}
532
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000533void
djm@openbsd.org04c091f2019-01-19 21:43:56 +0000534mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000535{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000536 struct sshbuf *m;
537 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
markus@openbsd.org091c3022015-01-19 19:52:16 +0000539 if ((m = sshbuf_new()) == NULL)
540 fatal("%s: sshbuf_new failed", __func__);
541 if ((r = ssh_packet_get_state(ssh, m)) != 0)
542 fatal("%s: get_state failed: %s",
543 __func__, ssh_err(r));
544 mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, m);
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000545 debug3("%s: Finished sending state", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000546 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547}
548
549int
Damien Miller71a73672006-03-26 14:04:36 +1100550mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000552 struct sshbuf *m;
Darren Tucker09991742004-07-17 17:05:14 +1000553 char *p, *msg;
markus@openbsd.org2808d182018-07-09 21:26:02 +0000554 int success = 0, tmp1 = -1, tmp2 = -1, r;
Damien Miller7207f642008-05-19 15:34:50 +1000555
556 /* Kludge: ensure there are fds free to receive the pty/tty */
557 if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
558 (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
559 error("%s: cannot allocate fds for pty", __func__);
560 if (tmp1 > 0)
561 close(tmp1);
562 if (tmp2 > 0)
563 close(tmp2);
564 return 0;
565 }
566 close(tmp1);
567 close(tmp2);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000569 if ((m = sshbuf_new()) == NULL)
570 fatal("%s: sshbuf_new failed", __func__);
571 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000573 debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000574 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000576 if ((r = sshbuf_get_u32(m, &success)) != 0)
577 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000579 debug3("%s: pty alloc failed", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000580 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581 return (0);
582 }
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000583 if ((r = sshbuf_get_cstring(m, &p, NULL)) != 0 ||
584 (r = sshbuf_get_cstring(m, &msg, NULL)) != 0)
585 fatal("%s: buffer error: %s", __func__, ssh_err(r));
586 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
588 strlcpy(namebuf, p, namebuflen); /* Possible truncation */
Darren Tuckera627d422013-06-02 07:31:17 +1000589 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
markus@openbsd.org2808d182018-07-09 21:26:02 +0000591 if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
592 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000593 free(msg);
Darren Tucker09991742004-07-17 17:05:14 +1000594
Damien Miller54fd7cf2007-09-17 12:04:08 +1000595 if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
596 (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
597 fatal("%s: receive fds failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
599 /* Success */
600 return (1);
601}
602
603void
Darren Tucker3e33cec2003-10-02 16:12:36 +1000604mm_session_pty_cleanup2(Session *s)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000606 struct sshbuf *m;
607 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
609 if (s->ttyfd == -1)
610 return;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000611 if ((m = sshbuf_new()) == NULL)
612 fatal("%s: sshbuf_new failed", __func__);
613 if ((r = sshbuf_put_cstring(m, s->tty)) != 0)
614 fatal("%s: buffer error: %s", __func__, ssh_err(r));
615 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, m);
616 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617
618 /* closed dup'ed master */
Damien Miller7207f642008-05-19 15:34:50 +1000619 if (s->ptymaster != -1 && close(s->ptymaster) < 0)
620 error("close(s->ptymaster/%d): %s",
621 s->ptymaster, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
623 /* unlink pty from session */
624 s->ttyfd = -1;
625}
626
Damien Miller79418552002-04-23 20:28:48 +1000627#ifdef USE_PAM
628void
Damien Miller3f0786b2019-01-20 10:22:18 +1100629mm_start_pam(struct ssh *ssh)
Damien Miller79418552002-04-23 20:28:48 +1000630{
Damien Miller120a1ec2018-07-10 19:39:52 +1000631 struct sshbuf *m;
Damien Miller79418552002-04-23 20:28:48 +1000632
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000633 debug3("%s entering", __func__);
Damien Miller4e448a32003-05-14 15:11:48 +1000634 if (!options.use_pam)
635 fatal("UsePAM=no, but ended up in %s anyway", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000636 if ((m = sshbuf_new()) == NULL)
637 fatal("%s: sshbuf_new failed", __func__);
638 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, m);
Damien Miller79418552002-04-23 20:28:48 +1000639
Damien Miller120a1ec2018-07-10 19:39:52 +1000640 sshbuf_free(m);
Damien Miller79418552002-04-23 20:28:48 +1000641}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000642
Damien Miller1f499fd2003-08-25 13:08:49 +1000643u_int
644mm_do_pam_account(void)
645{
Damien Miller120a1ec2018-07-10 19:39:52 +1000646 struct sshbuf *m;
Damien Miller1f499fd2003-08-25 13:08:49 +1000647 u_int ret;
Darren Tucker77fc29e2004-09-11 23:07:03 +1000648 char *msg;
Damien Miller120a1ec2018-07-10 19:39:52 +1000649 size_t msglen;
650 int r;
Damien Miller1f499fd2003-08-25 13:08:49 +1000651
652 debug3("%s entering", __func__);
653 if (!options.use_pam)
654 fatal("UsePAM=no, but ended up in %s anyway", __func__);
655
Damien Miller120a1ec2018-07-10 19:39:52 +1000656 if ((m = sshbuf_new()) == NULL)
657 fatal("%s: sshbuf_new failed", __func__);
658 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000659
Damien Millera8e06ce2003-11-21 23:48:55 +1100660 mm_request_receive_expect(pmonitor->m_recvfd,
Damien Miller120a1ec2018-07-10 19:39:52 +1000661 MONITOR_ANS_PAM_ACCOUNT, m);
662 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
663 (r = sshbuf_get_cstring(m, &msg, &msglen)) != 0 ||
664 (r = sshbuf_put(loginmsg, msg, msglen)) != 0)
665 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1f499fd2003-08-25 13:08:49 +1000666
Damien Miller120a1ec2018-07-10 19:39:52 +1000667 free(msg);
668 sshbuf_free(m);
Damien Miller787b2ec2003-11-21 23:56:47 +1100669
Damien Miller1f499fd2003-08-25 13:08:49 +1000670 debug3("%s returning %d", __func__, ret);
671
672 return (ret);
673}
674
Damien Miller4f9f42a2003-05-10 19:28:02 +1000675void *
676mm_sshpam_init_ctx(Authctxt *authctxt)
677{
Damien Miller120a1ec2018-07-10 19:39:52 +1000678 struct sshbuf *m;
679 int r, success;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000680
681 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000682 if ((m = sshbuf_new()) == NULL)
683 fatal("%s: sshbuf_new failed", __func__);
684 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000685 debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000686 mm_request_receive_expect(pmonitor->m_recvfd,
687 MONITOR_ANS_PAM_INIT_CTX, m);
688 if ((r = sshbuf_get_u32(m, &success)) != 0)
689 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000690 if (success == 0) {
691 debug3("%s: pam_init_ctx failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000692 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000693 return (NULL);
694 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000695 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000696 return (authctxt);
697}
698
699int
700mm_sshpam_query(void *ctx, char **name, char **info,
701 u_int *num, char ***prompts, u_int **echo_on)
702{
Damien Miller120a1ec2018-07-10 19:39:52 +1000703 struct sshbuf *m;
704 u_int i, n;
705 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000706
707 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000708 if ((m = sshbuf_new()) == NULL)
709 fatal("%s: sshbuf_new failed", __func__);
710 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000711 debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000712 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, m);
713 if ((r = sshbuf_get_u32(m, &ret)) != 0 ||
714 (r = sshbuf_get_cstring(m, name, NULL)) != 0 ||
715 (r = sshbuf_get_cstring(m, info, NULL)) != 0 ||
716 (r = sshbuf_get_u32(m, &n)) != 0 ||
717 (r = sshbuf_get_u32(m, num)) != 0)
718 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000719 debug3("%s: pam_query returned %d", __func__, ret);
Damien Miller120a1ec2018-07-10 19:39:52 +1000720 sshpam_set_maxtries_reached(n);
Darren Tuckerd8093e42006-05-04 16:24:34 +1000721 if (*num > PAM_MAX_NUM_MSG)
Damien Miller10479cc2018-04-10 10:19:02 +1000722 fatal("%s: received %u PAM messages, expected <= %u",
Darren Tuckerd8093e42006-05-04 16:24:34 +1000723 __func__, *num, PAM_MAX_NUM_MSG);
724 *prompts = xcalloc((*num + 1), sizeof(char *));
725 *echo_on = xcalloc((*num + 1), sizeof(u_int));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000726 for (i = 0; i < *num; ++i) {
Damien Miller120a1ec2018-07-10 19:39:52 +1000727 if ((r = sshbuf_get_cstring(m, &((*prompts)[i]), NULL)) != 0 ||
728 (r = sshbuf_get_u32(m, &((*echo_on)[i]))) != 0)
729 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000730 }
Damien Miller120a1ec2018-07-10 19:39:52 +1000731 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000732 return (ret);
733}
734
735int
736mm_sshpam_respond(void *ctx, u_int num, char **resp)
737{
Damien Miller120a1ec2018-07-10 19:39:52 +1000738 struct sshbuf *m;
739 u_int n, i;
740 int r, ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000741
742 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000743 if ((m = sshbuf_new()) == NULL)
744 fatal("%s: sshbuf_new failed", __func__);
745 if ((r = sshbuf_put_u32(m, num)) != 0)
746 fatal("%s: buffer error: %s", __func__, ssh_err(r));
747 for (i = 0; i < num; ++i) {
748 if ((r = sshbuf_put_cstring(m, resp[i])) != 0)
749 fatal("%s: buffer error: %s", __func__, ssh_err(r));
750 }
751 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000752 debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000753 mm_request_receive_expect(pmonitor->m_recvfd,
754 MONITOR_ANS_PAM_RESPOND, m);
755 if ((r = sshbuf_get_u32(m, &n)) != 0)
756 fatal("%s: buffer error: %s", __func__, ssh_err(r));
757 ret = (int)n; /* XXX */
Damien Miller4f9f42a2003-05-10 19:28:02 +1000758 debug3("%s: pam_respond returned %d", __func__, ret);
Damien Miller120a1ec2018-07-10 19:39:52 +1000759 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000760 return (ret);
761}
762
763void
764mm_sshpam_free_ctx(void *ctxtp)
765{
Damien Miller120a1ec2018-07-10 19:39:52 +1000766 struct sshbuf *m;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000767
768 debug3("%s", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000769 if ((m = sshbuf_new()) == NULL)
770 fatal("%s: sshbuf_new failed", __func__);
771 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000772 debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000773 mm_request_receive_expect(pmonitor->m_recvfd,
774 MONITOR_ANS_PAM_FREE_CTX, m);
775 sshbuf_free(m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000776}
Damien Miller79418552002-04-23 20:28:48 +1000777#endif /* USE_PAM */
778
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779/* Request process termination */
780
781void
782mm_terminate(void)
783{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000784 struct sshbuf *m;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000786 if ((m = sshbuf_new()) == NULL)
787 fatal("%s: sshbuf_new failed", __func__);
788 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, m);
789 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790}
791
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792static void
793mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
794 char ***prompts, u_int **echo_on)
795{
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000796 *name = xstrdup("");
797 *infotxt = xstrdup("");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798 *numprompts = 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100799 *prompts = xcalloc(*numprompts, sizeof(char *));
800 *echo_on = xcalloc(*numprompts, sizeof(u_int));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801 (*echo_on)[0] = 0;
802}
803
804int
805mm_bsdauth_query(void *ctx, char **name, char **infotxt,
806 u_int *numprompts, char ***prompts, u_int **echo_on)
807{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000808 struct sshbuf *m;
Damien Millerb7df3af2003-02-24 11:55:46 +1100809 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810 char *challenge;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000811 int r;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000813 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000814
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000815 if ((m = sshbuf_new()) == NULL)
816 fatal("%s: sshbuf_new failed", __func__);
817 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000818
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000819 mm_request_receive_expect(pmonitor->m_recvfd,
820 MONITOR_ANS_BSDAUTHQUERY, m);
821 if ((r = sshbuf_get_u32(m, &success)) != 0)
822 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerb7df3af2003-02-24 11:55:46 +1100823 if (success == 0) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000824 debug3("%s: no challenge", __func__);
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000825 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826 return (-1);
827 }
828
829 /* Get the challenge, and format the response */
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000830 if ((r = sshbuf_get_cstring(m, &challenge, NULL)) != 0)
831 fatal("%s: buffer error: %s", __func__, ssh_err(r));
832 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
834 mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
835 (*prompts)[0] = challenge;
836
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000837 debug3("%s: received challenge: %s", __func__, challenge);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
839 return (0);
840}
841
842int
843mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
844{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000845 struct sshbuf *m;
846 int r, authok;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000847
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000848 debug3("%s: entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849 if (numresponses != 1)
850 return (-1);
851
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000852 if ((m = sshbuf_new()) == NULL)
853 fatal("%s: sshbuf_new failed", __func__);
854 if ((r = sshbuf_put_cstring(m, responses[0])) != 0)
855 fatal("%s: buffer error: %s", __func__, ssh_err(r));
856 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000858 mm_request_receive_expect(pmonitor->m_recvfd,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000859 MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000860
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000861 if ((r = sshbuf_get_u32(m, &authok)) != 0)
862 fatal("%s: buffer error: %s", __func__, ssh_err(r));
863 sshbuf_free(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864
865 return ((authok == 0) ? -1 : 0);
866}
867
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100868#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100869void
Damien Miller9b655dc2019-01-20 14:55:27 +1100870mm_audit_event(struct ssh *ssh, ssh_audit_event_t event)
Darren Tucker269a1ea2005-02-03 00:20:53 +1100871{
Damien Miller120a1ec2018-07-10 19:39:52 +1000872 struct sshbuf *m;
Darren Tucker1dd32c22018-07-13 13:38:10 +1000873 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +1100874
875 debug3("%s entering", __func__);
876
Damien Miller120a1ec2018-07-10 19:39:52 +1000877 if ((m = sshbuf_new()) == NULL)
Darren Tucker1dd32c22018-07-13 13:38:10 +1000878 fatal("%s: sshbuf_new failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000879 if ((r = sshbuf_put_u32(m, event)) != 0)
880 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100881
Damien Miller120a1ec2018-07-10 19:39:52 +1000882 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, m);
883 sshbuf_free(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100884}
885
886void
887mm_audit_run_command(const char *command)
888{
Damien Miller120a1ec2018-07-10 19:39:52 +1000889 struct sshbuf *m;
Darren Tucker1dd32c22018-07-13 13:38:10 +1000890 int r;
Darren Tucker269a1ea2005-02-03 00:20:53 +1100891
892 debug3("%s entering command %s", __func__, command);
893
Damien Miller120a1ec2018-07-10 19:39:52 +1000894 if ((m = sshbuf_new()) == NULL)
Darren Tucker1dd32c22018-07-13 13:38:10 +1000895 fatal("%s: sshbuf_new failed", __func__);
Damien Miller120a1ec2018-07-10 19:39:52 +1000896 if ((r = sshbuf_put_cstring(m, command)) != 0)
897 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker269a1ea2005-02-03 00:20:53 +1100898
Damien Miller120a1ec2018-07-10 19:39:52 +1000899 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
900 sshbuf_free(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100901}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100902#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +1100903
Darren Tucker0efd1552003-08-26 11:49:55 +1000904#ifdef GSSAPI
905OM_uint32
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000906mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
Darren Tucker0efd1552003-08-26 11:49:55 +1000907{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000908 struct sshbuf *m;
Darren Tucker0efd1552003-08-26 11:49:55 +1000909 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000910 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000911
912 /* Client doesn't get to see the context */
913 *ctx = NULL;
914
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000915 if ((m = sshbuf_new()) == NULL)
916 fatal("%s: sshbuf_new failed", __func__);
917 if ((r = sshbuf_put_string(m, goid->elements, goid->length)) != 0)
918 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000919
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000920 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, m);
921 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000922
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000923 if ((r = sshbuf_get_u32(m, &major)) != 0)
924 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000925
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000926 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000927 return (major);
928}
929
930OM_uint32
931mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000932 gss_buffer_desc *out, OM_uint32 *flagsp)
Darren Tucker0efd1552003-08-26 11:49:55 +1000933{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000934 struct sshbuf *m;
Darren Tucker0efd1552003-08-26 11:49:55 +1000935 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000936 u_int flags;
937 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000938
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000939 if ((m = sshbuf_new()) == NULL)
940 fatal("%s: sshbuf_new failed", __func__);
941 if ((r = sshbuf_put_string(m, in->value, in->length)) != 0)
942 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000943
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000944 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, m);
945 mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000946
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000947 if ((r = sshbuf_get_u32(m, &major)) != 0 ||
djm@openbsd.org0f3958c2018-07-10 09:13:30 +0000948 (r = ssh_gssapi_get_buffer_desc(m, out)) != 0)
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000949 fatal("%s: buffer error: %s", __func__, ssh_err(r));
950 if (flagsp != NULL) {
951 if ((r = sshbuf_get_u32(m, &flags)) != 0)
952 fatal("%s: buffer error: %s", __func__, ssh_err(r));
953 *flagsp = flags;
954 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000955
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000956 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000957
958 return (major);
959}
960
Damien Miller0425d402003-11-17 22:18:21 +1100961OM_uint32
962mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
963{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000964 struct sshbuf *m;
Damien Miller0425d402003-11-17 22:18:21 +1100965 OM_uint32 major;
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000966 int r;
Damien Miller0425d402003-11-17 22:18:21 +1100967
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000968 if ((m = sshbuf_new()) == NULL)
969 fatal("%s: sshbuf_new failed", __func__);
970 if ((r = sshbuf_put_string(m, gssbuf->value, gssbuf->length)) != 0 ||
971 (r = sshbuf_put_string(m, gssmic->value, gssmic->length)) != 0)
972 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100973
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000974 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, m);
975 mm_request_receive_expect(pmonitor->m_recvfd,
976 MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller0425d402003-11-17 22:18:21 +1100977
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000978 if ((r = sshbuf_get_u32(m, &major)) != 0)
979 fatal("%s: buffer error: %s", __func__, ssh_err(r));
980 sshbuf_free(m);
Damien Miller0425d402003-11-17 22:18:21 +1100981 return(major);
982}
983
Darren Tucker0efd1552003-08-26 11:49:55 +1000984int
985mm_ssh_gssapi_userok(char *user)
986{
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000987 struct sshbuf *m;
988 int r, authenticated = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000989
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000990 if ((m = sshbuf_new()) == NULL)
991 fatal("%s: sshbuf_new failed", __func__);
Darren Tucker0efd1552003-08-26 11:49:55 +1000992
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000993 mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, m);
994 mm_request_receive_expect(pmonitor->m_recvfd,
995 MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +1000996
markus@openbsd.org235c7c42018-07-09 21:53:45 +0000997 if ((r = sshbuf_get_u32(m, &authenticated)) != 0)
998 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000999
markus@openbsd.org235c7c42018-07-09 21:53:45 +00001000 sshbuf_free(m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001001 debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
1002 return (authenticated);
1003}
1004#endif /* GSSAPI */