blob: 6b780e48020bbfdf5aa95685ac8ba9c072781b0b [file] [log] [blame]
djm@openbsd.org95767262016-03-07 19:02:43 +00001/* $OpenBSD: monitor.c,v 1.158 2016/03/07 19:02:43 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"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100032#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110033#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000034
Darren Tucker39972492006-07-12 22:22:46 +100035#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110037#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110038#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110039#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100040#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110041#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110042#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000043#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110044#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100046#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100047#include <stdarg.h>
48#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100049#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100050#ifdef HAVE_POLL_H
51#include <poll.h>
52#else
53# ifdef HAVE_SYS_POLL_H
54# include <sys/poll.h>
55# endif
56#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057
58#ifdef SKEY
59#include <skey.h>
60#endif
61
Damien Miller1f0311c2014-05-15 14:24:09 +100062#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110063#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100064#endif
Damien Miller03e20032006-03-15 11:16:59 +110065
Damien Millerb84886b2008-05-19 15:05:07 +100066#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100067#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100068#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000069#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "key.h"
71#include "buffer.h"
72#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100074#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000075#include "kex.h"
76#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000077#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
78#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000079#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000080#define TARGET_OS_MAC 1
81#else
82#include "zlib.h"
83#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084#include "packet.h"
85#include "auth-options.h"
86#include "sshpty.h"
87#include "channels.h"
88#include "session.h"
89#include "sshlogin.h"
90#include "canohost.h"
91#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100092#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "servconf.h"
94#include "monitor.h"
95#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100096#ifdef GSSAPI
97#include "ssh-gss.h"
98#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099#include "monitor_wrap.h"
100#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101#include "compat.h"
102#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000103#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000104#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000105#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106
Darren Tucker0efd1552003-08-26 11:49:55 +1000107#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000108static Gssctxt *gsscontext = NULL;
109#endif
110
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111/* Imports */
112extern ServerOptions options;
113extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern Buffer auth_debug;
116extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000117extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118
119/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000120static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121
Damien Miller469954d2003-06-18 20:25:33 +1000122/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
124int mm_answer_moduli(int, Buffer *);
125int mm_answer_sign(int, Buffer *);
126int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000127int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128int mm_answer_authserv(int, Buffer *);
129int mm_answer_authpassword(int, Buffer *);
130int mm_answer_bsdauthquery(int, Buffer *);
131int mm_answer_bsdauthrespond(int, Buffer *);
132int mm_answer_skeyquery(int, Buffer *);
133int mm_answer_skeyrespond(int, Buffer *);
134int mm_answer_keyallowed(int, Buffer *);
135int mm_answer_keyverify(int, Buffer *);
136int mm_answer_pty(int, Buffer *);
137int mm_answer_pty_cleanup(int, Buffer *);
138int mm_answer_term(int, Buffer *);
139int mm_answer_rsa_keyallowed(int, Buffer *);
140int mm_answer_rsa_challenge(int, Buffer *);
141int mm_answer_rsa_response(int, Buffer *);
142int mm_answer_sesskey(int, Buffer *);
143int mm_answer_sessid(int, Buffer *);
144
Damien Miller79418552002-04-23 20:28:48 +1000145#ifdef USE_PAM
146int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000147int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000148int mm_answer_pam_init_ctx(int, Buffer *);
149int mm_answer_pam_query(int, Buffer *);
150int mm_answer_pam_respond(int, Buffer *);
151int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000152#endif
153
Darren Tucker0efd1552003-08-26 11:49:55 +1000154#ifdef GSSAPI
155int mm_answer_gss_setup_ctx(int, Buffer *);
156int mm_answer_gss_accept_ctx(int, Buffer *);
157int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100158int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000159#endif
Damien Miller25162f22002-09-12 09:47:29 +1000160
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100161#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100162int mm_answer_audit_event(int, Buffer *);
163int mm_answer_audit_command(int, Buffer *);
164#endif
165
Damien Miller8f0bf232011-06-20 14:42:23 +1000166static int monitor_read_log(struct monitor *);
167
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000169
170#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000172#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173
174/* local state for key verify */
175static u_char *key_blob = NULL;
176static u_int key_bloblen = 0;
177static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000178static char *hostbased_cuser = NULL;
179static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100181static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000182static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000183static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000184static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185
186struct mon_table {
187 enum monitor_reqtype type;
188 int flags;
189 int (*f)(int, Buffer *);
190};
191
192#define MON_ISAUTH 0x0004 /* Required for Authentication */
193#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
194#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100195#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196
197#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
198
199#define MON_PERMIT 0x1000 /* Request is permitted */
200
201struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000202#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000204#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
206 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
207 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000208 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000210#ifdef USE_PAM
211 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000212 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000213 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
214 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
215 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
216 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000217#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100218#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100219 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100220#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#ifdef BSD_AUTH
222 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100223 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224#endif
225#ifdef SKEY
226 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
227 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
228#endif
229 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
230 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000231#ifdef GSSAPI
232 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
233 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
234 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100235 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000236#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000237 {0, 0, NULL}
238};
239
240struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000241#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000243#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
245 {MONITOR_REQ_PTY, 0, mm_answer_pty},
246 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
247 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100248#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100249 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
250 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
251#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252 {0, 0, NULL}
253};
254
255struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000256#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
258 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
259 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
260 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100261 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
262 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
264 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
265#ifdef BSD_AUTH
266 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100267 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268#endif
269#ifdef SKEY
270 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
271 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
272#endif
Damien Millera33501b2002-05-08 12:24:42 +1000273#ifdef USE_PAM
274 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000275 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000276 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
277 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
278 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
279 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000280#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100281#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100282 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100283#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000284#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285 {0, 0, NULL}
286};
287
288struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000289#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
291 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
292 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100293#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100294 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000295 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100296#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000297#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000298 {0, 0, NULL}
299};
300
301struct mon_table *mon_dispatch;
302
303/* Specifies if a certain message is allowed at the moment */
304
305static void
306monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
307{
308 while (ent->f != NULL) {
309 if (ent->type == type) {
310 ent->flags &= ~MON_PERMIT;
311 ent->flags |= permit ? MON_PERMIT : 0;
312 return;
313 }
314 ent++;
315 }
316}
317
318static void
319monitor_permit_authentications(int permit)
320{
321 struct mon_table *ent = mon_dispatch;
322
323 while (ent->f != NULL) {
324 if (ent->flags & MON_AUTH) {
325 ent->flags &= ~MON_PERMIT;
326 ent->flags |= permit ? MON_PERMIT : 0;
327 }
328 ent++;
329 }
330}
331
Darren Tucker3e33cec2003-10-02 16:12:36 +1000332void
333monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334{
335 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100336 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337
338 debug3("preauth child monitor started");
339
Damien Miller8f0bf232011-06-20 14:42:23 +1000340 close(pmonitor->m_recvfd);
341 close(pmonitor->m_log_sendfd);
342 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
343
Darren Tucker3e33cec2003-10-02 16:12:36 +1000344 authctxt = _authctxt;
345 memset(authctxt, 0, sizeof(*authctxt));
346
Darren Tuckerde0de392005-03-31 23:52:04 +1000347 authctxt->loginmsg = &loginmsg;
348
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 if (compat20) {
350 mon_dispatch = mon_dispatch_proto20;
351
352 /* Permit requests for moduli and signatures */
353 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
354 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
355 } else {
356 mon_dispatch = mon_dispatch_proto15;
357
358 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
359 }
360
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361 /* The first few requests do not require asynchronous access */
362 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100363 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100364 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100365 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100366 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100367
368 /* Special handling for multiple required authentications */
369 if (options.num_auth_methods != 0) {
370 if (!compat20)
371 fatal("AuthenticationMethods is not supported"
372 "with SSH protocol 1");
373 if (authenticated &&
374 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000375 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100376 debug3("%s: method %s: partial", __func__,
377 auth_method);
378 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100379 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100380 }
381 }
382
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383 if (authenticated) {
384 if (!(ent->flags & MON_AUTHDECIDE))
385 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000386 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387 if (authctxt->pw->pw_uid == 0 &&
388 !auth_root_allowed(auth_method))
389 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000390#ifdef USE_PAM
391 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100392 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000393 Buffer m;
394
395 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100396 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000397 MONITOR_REQ_PAM_ACCOUNT, &m);
398 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
399 buffer_free(&m);
400 }
401#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000402 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100403 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100404 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000405 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000406 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 authctxt->failures++;
408 }
409 }
410
411 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000412 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100413 if (strcmp(auth_method, "unknown") == 0)
414 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415
416 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000417 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000419 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000420
Damien Miller6a1937e2012-12-12 10:44:38 +1100421 /* Drain any buffered messages from the child */
422 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
423 ;
424
Damien Miller8f0bf232011-06-20 14:42:23 +1000425 close(pmonitor->m_sendfd);
426 close(pmonitor->m_log_recvfd);
427 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428}
429
Damien Millerbe64d432003-05-14 19:31:12 +1000430static void
431monitor_set_child_handler(pid_t pid)
432{
433 monitor_child_pid = pid;
434}
435
436static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000437monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000438{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000439 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000440}
441
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000443monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444{
Damien Miller8f0bf232011-06-20 14:42:23 +1000445 close(pmonitor->m_recvfd);
446 pmonitor->m_recvfd = -1;
447
Damien Millerbe64d432003-05-14 19:31:12 +1000448 monitor_set_child_handler(pmonitor->m_pid);
449 signal(SIGHUP, &monitor_child_handler);
450 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000451 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000452#ifdef SIGXFSZ
453 signal(SIGXFSZ, SIG_IGN);
454#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000455
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456 if (compat20) {
457 mon_dispatch = mon_dispatch_postauth20;
458
459 /* Permit requests for moduli and signatures */
460 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
461 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
462 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 } else {
464 mon_dispatch = mon_dispatch_postauth15;
465 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
466 }
467 if (!no_pty_flag) {
468 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
469 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
470 }
471
472 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000473 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474}
475
476void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000477monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478{
Damien Miller2d6b8332002-06-21 15:59:49 +1000479 if (options.compression) {
480 /* The member allocation is not visible, so sync it */
481 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
482 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483}
484
markus@openbsd.org091c3022015-01-19 19:52:16 +0000485/* Allocation functions for zlib */
486static void *
487mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
488{
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000489 if (size == 0 || ncount == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000490 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
491
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000492 return mm_malloc(mm, size * ncount);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000493}
494
495static void
496mm_zfree(struct mm_master *mm, void *address)
497{
498 mm_free(mm, address);
499}
500
Damien Miller8f0bf232011-06-20 14:42:23 +1000501static int
502monitor_read_log(struct monitor *pmonitor)
503{
504 Buffer logmsg;
505 u_int len, level;
506 char *msg;
507
508 buffer_init(&logmsg);
509
510 /* Read length */
511 buffer_append_space(&logmsg, 4);
512 if (atomicio(read, pmonitor->m_log_recvfd,
513 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
514 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100515 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000516 debug("%s: child log fd closed", __func__);
517 close(pmonitor->m_log_recvfd);
518 pmonitor->m_log_recvfd = -1;
519 return -1;
520 }
521 fatal("%s: log fd read: %s", __func__, strerror(errno));
522 }
523 len = buffer_get_int(&logmsg);
524 if (len <= 4 || len > 8192)
525 fatal("%s: invalid log message length %u", __func__, len);
526
527 /* Read severity, message */
528 buffer_clear(&logmsg);
529 buffer_append_space(&logmsg, len);
530 if (atomicio(read, pmonitor->m_log_recvfd,
531 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
532 fatal("%s: log fd read: %s", __func__, strerror(errno));
533
534 /* Log it */
535 level = buffer_get_int(&logmsg);
536 msg = buffer_get_string(&logmsg, NULL);
537 if (log_level_name(level) == NULL)
538 fatal("%s: invalid log level %u (corrupted message?)",
539 __func__, level);
540 do_log2(level, "%s [preauth]", msg);
541
542 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000543 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000544
545 return 0;
546}
547
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000549monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 struct mon_table **pent)
551{
552 Buffer m;
553 int ret;
554 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000555 struct pollfd pfd[2];
556
557 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100558 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000559 pfd[0].fd = pmonitor->m_sendfd;
560 pfd[0].events = POLLIN;
561 pfd[1].fd = pmonitor->m_log_recvfd;
562 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000563 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
564 if (errno == EINTR || errno == EAGAIN)
565 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000566 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000567 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000568 if (pfd[1].revents) {
569 /*
570 * Drain all log messages before processing next
571 * monitor request.
572 */
573 monitor_read_log(pmonitor);
574 continue;
575 }
576 if (pfd[0].revents)
577 break; /* Continues below */
578 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579
580 buffer_init(&m);
581
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000582 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 type = buffer_get_char(&m);
584
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000585 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
587 while (ent->f != NULL) {
588 if (ent->type == type)
589 break;
590 ent++;
591 }
592
593 if (ent->f != NULL) {
594 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000595 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000597 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 buffer_free(&m);
599
600 /* The child may use this request only once, disable it */
601 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000602 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 type);
604 ent->flags &= ~MON_PERMIT;
605 }
606
607 if (pent != NULL)
608 *pent = ent;
609
610 return ret;
611 }
612
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
615 /* NOTREACHED */
616 return (-1);
617}
618
619/* allowed key state */
620static int
621monitor_allowed_key(u_char *blob, u_int bloblen)
622{
623 /* make sure key is allowed */
624 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000625 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000626 return (0);
627 return (1);
628}
629
630static void
631monitor_reset_key_state(void)
632{
633 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000634 free(key_blob);
635 free(hostbased_cuser);
636 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637 key_blob = NULL;
638 key_bloblen = 0;
639 key_blobtype = MM_NOKEY;
640 hostbased_cuser = NULL;
641 hostbased_chost = NULL;
642}
643
Damien Miller1f0311c2014-05-15 14:24:09 +1000644#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000646mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647{
648 DH *dh;
649 int min, want, max;
650
651 min = buffer_get_int(m);
652 want = buffer_get_int(m);
653 max = buffer_get_int(m);
654
655 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000656 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657 /* We need to check here, too, in case the child got corrupted */
658 if (max < min || want < min || max < want)
659 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000660 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661
662 buffer_clear(m);
663
664 dh = choose_dh(min, want, max);
665 if (dh == NULL) {
666 buffer_put_char(m, 0);
667 return (0);
668 } else {
669 /* Send first bignum */
670 buffer_put_char(m, 1);
671 buffer_put_bignum2(m, dh->p);
672 buffer_put_bignum2(m, dh->g);
673
674 DH_free(dh);
675 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000676 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677 return (0);
678}
Damien Miller1f0311c2014-05-15 14:24:09 +1000679#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000682mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000684 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000685 extern int auth_sock; /* XXX move to state struct? */
686 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000687 struct sshbuf *sigbuf = NULL;
688 u_char *p = NULL, *signature = NULL;
689 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000690 size_t datlen, siglen, alglen;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000691 int r, keyid, is_proof = 0;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000692 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000694 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
djm@openbsd.org141efe42015-01-14 20:05:27 +0000696 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000697 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
698 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
Damien Millera63128d2006-03-15 12:08:28 +1100701 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000702 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
703 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000704 *
705 * Otherwise, verify the signature request is for a hostkey
706 * proof.
707 *
708 * XXX perform similar check for KEX signature requests too?
709 * it's not trivial, since what is signed is the hash, rather
710 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100711 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000712 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
713 /*
714 * Construct expected hostkey proof and compare it to what
715 * the client sent us.
716 */
717 if (session_id2_len == 0) /* hostkeys is never first */
718 fatal("%s: bad data length: %zu", __func__, datlen);
719 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
720 fatal("%s: no hostkey for index %d", __func__, keyid);
721 if ((sigbuf = sshbuf_new()) == NULL)
722 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000723 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
724 (r = sshbuf_put_string(sigbuf, session_id2,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000725 session_id2_len)) != 0 ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000726 (r = sshkey_puts(key, sigbuf)) != 0)
727 fatal("%s: couldn't prepare private key "
728 "proof buffer: %s", __func__, ssh_err(r));
729 if (datlen != sshbuf_len(sigbuf) ||
730 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
731 fatal("%s: bad data length: %zu, hostkey proof len %zu",
732 __func__, datlen, sshbuf_len(sigbuf));
733 sshbuf_free(sigbuf);
734 is_proof = 1;
735 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
Ben Lindstromf67e0772002-06-06 20:58:19 +0000737 /* save session id, it will be passed on the first call */
738 if (session_id2_len == 0) {
739 session_id2_len = datlen;
740 session_id2 = xmalloc(session_id2_len);
741 memcpy(session_id2, p, session_id2_len);
742 }
743
Damien Miller85b45e02013-07-20 13:21:52 +1000744 if ((key = get_hostkey_by_index(keyid)) != NULL) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000745 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000746 datafellows)) != 0)
747 fatal("%s: sshkey_sign failed: %s",
748 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000749 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000750 auth_sock > 0) {
751 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000752 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000753 fatal("%s: ssh_agent_sign failed: %s",
754 __func__, ssh_err(r));
755 }
Damien Miller85b45e02013-07-20 13:21:52 +1000756 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000757 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758
djm@openbsd.org523463a2015-02-16 22:13:32 +0000759 debug3("%s: %s signature %p(%zu)", __func__,
760 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
djm@openbsd.org141efe42015-01-14 20:05:27 +0000762 sshbuf_reset(m);
763 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
764 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000766 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000767 free(p);
768 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000770 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771
772 /* Turn on permissions for getpwnam */
773 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
774
775 return (0);
776}
777
778/* Retrieves the password entry and also checks if the user is permitted */
779
780int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000781mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000783 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784 struct passwd *pwent;
785 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000786 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000788 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789
790 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000791 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
Darren Tuckerb09b6772004-06-22 15:06:46 +1000793 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794
Darren Tuckerb09b6772004-06-22 15:06:46 +1000795 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796
Darren Tuckerb09b6772004-06-22 15:06:46 +1000797 authctxt->user = xstrdup(username);
798 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000799 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800
801 buffer_clear(m);
802
803 if (pwent == NULL) {
804 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100805 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806 goto out;
807 }
808
809 allowed = 1;
810 authctxt->pw = pwent;
811 authctxt->valid = 1;
812
813 buffer_put_char(m, 1);
814 buffer_put_string(m, pwent, sizeof(struct passwd));
815 buffer_put_cstring(m, pwent->pw_name);
816 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000817#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000818 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000819#endif
820#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000822#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823 buffer_put_cstring(m, pwent->pw_dir);
824 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100825
826 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100827 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000828
829#define M_CP_STROPT(x) do { \
830 if (options.x != NULL) \
831 buffer_put_cstring(m, options.x); \
832 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000833#define M_CP_STRARRAYOPT(x, nx) do { \
834 for (i = 0; i < options.nx; i++) \
835 buffer_put_cstring(m, options.x[i]); \
836 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000837 /* See comment in servconf.h */
838 COPY_MATCH_STRING_OPTS();
839#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000840#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100841
842 /* Create valid auth method lists */
843 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
844 /*
845 * The monitor will continue long enough to let the child
846 * run to it's packet_disconnect(), but it must not allow any
847 * authentication to succeed.
848 */
849 debug("%s: no valid authentication method lists", __func__);
850 }
851
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000852 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000853 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000854
855 /* For SSHv1 allow authentication now */
856 if (!compat20)
857 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000858 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000859 /* Allow service/style information on the auth context */
860 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000861 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
862 }
Damien Millera33501b2002-05-08 12:24:42 +1000863#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000864 if (options.use_pam)
865 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000866#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000867
868 return (0);
869}
870
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000871int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000872{
873 char *banner;
874
875 buffer_clear(m);
876 banner = auth2_read_banner();
877 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000878 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000879 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000880
881 return (0);
882}
883
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000885mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886{
887 monitor_permit_authentications(1);
888
889 authctxt->service = buffer_get_string(m, NULL);
890 authctxt->style = buffer_get_string(m, NULL);
891 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000892 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000893
894 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000895 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000896 authctxt->style = NULL;
897 }
898
899 return (0);
900}
901
902int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000903mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904{
905 static int call_count;
906 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000907 int authenticated;
908 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909
910 passwd = buffer_get_string(m, &plen);
911 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000912 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000913 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100914 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000915 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916
917 buffer_clear(m);
918 buffer_put_int(m, authenticated);
919
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000920 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000921 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922
923 call_count++;
924 if (plen == 0 && call_count == 1)
925 auth_method = "none";
926 else
927 auth_method = "password";
928
929 /* Causes monitor loop to terminate if authenticated */
930 return (authenticated);
931}
932
933#ifdef BSD_AUTH
934int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000935mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936{
937 char *name, *infotxt;
938 u_int numprompts;
939 u_int *echo_on;
940 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100941 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
Damien Millerb7df3af2003-02-24 11:55:46 +1100943 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
944 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100947 buffer_put_int(m, success);
948 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 buffer_put_cstring(m, prompts[0]);
950
Damien Millerb7df3af2003-02-24 11:55:46 +1100951 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000952 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
Damien Millerb7df3af2003-02-24 11:55:46 +1100954 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000955 free(name);
956 free(infotxt);
957 free(prompts);
958 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959 }
960
961 return (0);
962}
963
964int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000965mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966{
967 char *response;
968 int authok;
969
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000970 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000971 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
973 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000974 authok = options.challenge_response_authentication &&
975 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000977 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000978 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
980 buffer_clear(m);
981 buffer_put_int(m, authok);
982
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000984 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
Damien Miller91a55f22013-04-23 15:18:10 +1000986 if (compat20) {
987 auth_method = "keyboard-interactive";
988 auth_submethod = "bsdauth";
989 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100990 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991
992 return (authok != 0);
993}
994#endif
995
996#ifdef SKEY
997int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000998mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999{
1000 struct skey skey;
1001 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +11001002 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003
Darren Tucker06a8cfe2004-04-14 17:24:30 +10001004 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1005 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006
1007 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001008 buffer_put_int(m, success);
1009 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 buffer_put_cstring(m, challenge);
1011
Damien Millerb7df3af2003-02-24 11:55:46 +11001012 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001013 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
1015 return (0);
1016}
1017
1018int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001019mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020{
1021 char *response;
1022 int authok;
1023
1024 response = buffer_get_string(m, NULL);
1025
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001026 authok = (options.challenge_response_authentication &&
1027 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001028 skey_haskey(authctxt->pw->pw_name) == 0 &&
1029 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1030
Darren Tuckerf60845f2013-06-02 08:07:31 +10001031 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032
1033 buffer_clear(m);
1034 buffer_put_int(m, authok);
1035
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001036 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001037 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038
Damien Millerac947352015-11-20 12:35:41 +11001039 auth_method = "keyboard-interactive";
1040 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001041
1042 return (authok != 0);
1043}
1044#endif
1045
Damien Miller79418552002-04-23 20:28:48 +10001046#ifdef USE_PAM
1047int
Darren Tucker7eda5922004-06-22 13:22:50 +10001048mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001049{
Damien Miller4e448a32003-05-14 15:11:48 +10001050 if (!options.use_pam)
1051 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1052
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001053 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001054
Damien Miller1f499fd2003-08-25 13:08:49 +10001055 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1056
Damien Miller79418552002-04-23 20:28:48 +10001057 return (0);
1058}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001059
Damien Miller1f499fd2003-08-25 13:08:49 +10001060int
Darren Tucker7eda5922004-06-22 13:22:50 +10001061mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001062{
1063 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001064
Damien Miller1f499fd2003-08-25 13:08:49 +10001065 if (!options.use_pam)
1066 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1067
1068 ret = do_pam_account();
1069
1070 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001071 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001072
Darren Tucker7eda5922004-06-22 13:22:50 +10001073 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001074
1075 return (ret);
1076}
1077
Damien Miller4f9f42a2003-05-10 19:28:02 +10001078static void *sshpam_ctxt, *sshpam_authok;
1079extern KbdintDevice sshpam_device;
1080
1081int
Darren Tucker7eda5922004-06-22 13:22:50 +10001082mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001084 debug3("%s", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1086 sshpam_authok = NULL;
1087 buffer_clear(m);
1088 if (sshpam_ctxt != NULL) {
1089 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1090 buffer_put_int(m, 1);
1091 } else {
1092 buffer_put_int(m, 0);
1093 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001094 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001095 return (0);
1096}
1097
1098int
Darren Tucker7eda5922004-06-22 13:22:50 +10001099mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001101 char *name = NULL, *info = NULL, **prompts = NULL;
1102 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001103 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104
1105 debug3("%s", __func__);
1106 sshpam_authok = NULL;
1107 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1108 if (ret == 0 && num == 0)
1109 sshpam_authok = sshpam_ctxt;
1110 if (num > 1 || name == NULL || info == NULL)
1111 ret = -1;
1112 buffer_clear(m);
1113 buffer_put_int(m, ret);
1114 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001115 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001116 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001117 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118 buffer_put_int(m, num);
1119 for (i = 0; i < num; ++i) {
1120 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001121 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001122 buffer_put_int(m, echo_on[i]);
1123 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001124 free(prompts);
1125 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001126 auth_method = "keyboard-interactive";
1127 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001128 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001129 return (0);
1130}
1131
1132int
Darren Tucker7eda5922004-06-22 13:22:50 +10001133mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001134{
1135 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001136 u_int i, num;
1137 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001138
1139 debug3("%s", __func__);
1140 sshpam_authok = NULL;
1141 num = buffer_get_int(m);
1142 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001143 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001144 for (i = 0; i < num; ++i)
1145 resp[i] = buffer_get_string(m, NULL);
1146 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1147 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001148 free(resp[i]);
1149 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001150 } else {
1151 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1152 }
1153 buffer_clear(m);
1154 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001155 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001156 auth_method = "keyboard-interactive";
1157 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001158 if (ret == 0)
1159 sshpam_authok = sshpam_ctxt;
1160 return (0);
1161}
1162
1163int
Darren Tucker7eda5922004-06-22 13:22:50 +10001164mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001165{
Damien Miller5e75f512015-08-11 13:34:12 +10001166 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001167
1168 debug3("%s", __func__);
1169 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001170 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001171 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001172 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001173 auth_method = "keyboard-interactive";
1174 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001175 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001176}
Damien Miller79418552002-04-23 20:28:48 +10001177#endif
1178
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001180mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001181{
1182 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001183 char *cuser, *chost;
1184 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001185 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001186 enum mm_keytype type = 0;
1187 int allowed = 0;
1188
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001189 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190
1191 type = buffer_get_int(m);
1192 cuser = buffer_get_string(m, NULL);
1193 chost = buffer_get_string(m, NULL);
1194 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001195 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196
1197 key = key_from_blob(blob, bloblen);
1198
1199 if ((compat20 && type == MM_RSAHOSTKEY) ||
1200 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001203 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204
Damien Miller3e3b5142003-11-17 21:13:40 +11001205 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001206 /* These should not make it past the privsep child */
1207 if (key_type_plain(key->type) == KEY_RSA &&
1208 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1209 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1210
Darren Tucker47eede72005-03-14 23:08:12 +11001211 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001213 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001214 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001215 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001216 options.pubkey_key_types, 0) == 1 &&
1217 user_key_allowed(authctxt->pw, key,
1218 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001219 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001220 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001221 if (options.pubkey_authentication &&
1222 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001223 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 break;
1225 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001226 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001227 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001228 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001229 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001231 pubkey_auth_info(authctxt, key,
1232 "client user \"%.100s\", client host \"%.100s\"",
1233 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001234 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001236#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 case MM_RSAHOSTKEY:
1238 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001239 allowed = options.rhosts_rsa_authentication &&
1240 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001242 if (options.rhosts_rsa_authentication && allowed != 1)
1243 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001244 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001246#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 break;
1250 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251 }
Damien Miller00111382003-03-10 11:21:17 +11001252 if (key != NULL)
1253 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254
1255 /* clear temporarily storage (used by verify) */
1256 monitor_reset_key_state();
1257
1258 if (allowed) {
1259 /* Save temporarily for comparison in verify */
1260 key_blob = blob;
1261 key_bloblen = bloblen;
1262 key_blobtype = type;
1263 hostbased_cuser = cuser;
1264 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001265 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001266 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001267 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001268 free(blob);
1269 free(cuser);
1270 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271 }
1272
1273 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001274 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
1276 buffer_clear(m);
1277 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001278 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001280 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281
1282 if (type == MM_RSAHOSTKEY)
1283 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1284
1285 return (0);
1286}
1287
1288static int
1289monitor_valid_userblob(u_char *data, u_int datalen)
1290{
1291 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001292 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 u_int len;
1294 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
1296 buffer_init(&b);
1297 buffer_append(&b, data, datalen);
1298
1299 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001300 p = buffer_ptr(&b);
1301 len = buffer_len(&b);
1302 if ((session_id2 == NULL) ||
1303 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001305 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306 buffer_consume(&b, session_id2_len);
1307 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308 p = buffer_get_string(&b, &len);
1309 if ((session_id2 == NULL) ||
1310 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001311 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 }
1315 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1316 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001317 p = buffer_get_cstring(&b, NULL);
1318 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1319 authctxt->style ? ":" : "",
1320 authctxt->style ? authctxt->style : "");
1321 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001322 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001323 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 fail++;
1325 }
Darren Tuckera627d422013-06-02 07:31:17 +10001326 free(userstyle);
1327 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001328 buffer_skip_string(&b);
1329 if (datafellows & SSH_BUG_PKAUTH) {
1330 if (!buffer_get_char(&b))
1331 fail++;
1332 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001333 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334 if (strcmp("publickey", p) != 0)
1335 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001336 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 if (!buffer_get_char(&b))
1338 fail++;
1339 buffer_skip_string(&b);
1340 }
1341 buffer_skip_string(&b);
1342 if (buffer_len(&b) != 0)
1343 fail++;
1344 buffer_free(&b);
1345 return (fail == 0);
1346}
1347
1348static int
Damien Millera10f5612002-09-12 09:49:15 +10001349monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1350 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351{
1352 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001353 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354 u_int len;
1355 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356
1357 buffer_init(&b);
1358 buffer_append(&b, data, datalen);
1359
Ben Lindstromf67e0772002-06-06 20:58:19 +00001360 p = buffer_get_string(&b, &len);
1361 if ((session_id2 == NULL) ||
1362 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001363 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001366
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1368 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001369 p = buffer_get_cstring(&b, NULL);
1370 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1371 authctxt->style ? ":" : "",
1372 authctxt->style ? authctxt->style : "");
1373 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001374 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001375 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376 fail++;
1377 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001378 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001379 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001381 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382 if (strcmp(p, "hostbased") != 0)
1383 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001384 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385 buffer_skip_string(&b); /* pkalg */
1386 buffer_skip_string(&b); /* pkblob */
1387
1388 /* verify client host, strip trailing dot if necessary */
1389 p = buffer_get_string(&b, NULL);
1390 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1391 p[len - 1] = '\0';
1392 if (strcmp(p, chost) != 0)
1393 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 /* verify client user */
1397 p = buffer_get_string(&b, NULL);
1398 if (strcmp(p, cuser) != 0)
1399 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001400 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401
1402 if (buffer_len(&b) != 0)
1403 fail++;
1404 buffer_free(&b);
1405 return (fail == 0);
1406}
1407
1408int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001409mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410{
1411 Key *key;
1412 u_char *signature, *data, *blob;
1413 u_int signaturelen, datalen, bloblen;
1414 int verified = 0;
1415 int valid_data = 0;
1416
1417 blob = buffer_get_string(m, &bloblen);
1418 signature = buffer_get_string(m, &signaturelen);
1419 data = buffer_get_string(m, &datalen);
1420
1421 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001422 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001423 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424
1425 key = key_from_blob(blob, bloblen);
1426 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
1429 switch (key_blobtype) {
1430 case MM_USERKEY:
1431 valid_data = monitor_valid_userblob(data, datalen);
1432 break;
1433 case MM_HOSTKEY:
1434 valid_data = monitor_valid_hostbasedblob(data, datalen,
1435 hostbased_cuser, hostbased_chost);
1436 break;
1437 default:
1438 valid_data = 0;
1439 break;
1440 }
1441 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001442 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443
1444 verified = key_verify(key, signature, signaturelen, data, datalen);
1445 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001446 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001448 /* If auth was successful then record key to ensure it isn't reused */
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001449 if (verified == 1 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001450 auth2_record_userkey(authctxt, key);
1451 else
1452 key_free(key);
1453
Darren Tuckera627d422013-06-02 07:31:17 +10001454 free(blob);
1455 free(signature);
1456 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457
Ben Lindstrome1c09122002-06-23 00:38:24 +00001458 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1459
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 monitor_reset_key_state();
1461
1462 buffer_clear(m);
1463 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001464 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
Darren Tuckerfbba7352006-11-07 23:16:08 +11001466 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467}
1468
1469static void
1470mm_record_login(Session *s, struct passwd *pw)
1471{
djm@openbsd.org95767262016-03-07 19:02:43 +00001472 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473 socklen_t fromlen;
1474 struct sockaddr_storage from;
1475
djm@openbsd.org17d4d9d2015-04-17 04:32:31 +00001476 if (options.use_login)
1477 return;
1478
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479 /*
1480 * Get IP address of client. If the connection is not a socket, let
1481 * the address be 0.0.0.0.
1482 */
1483 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001484 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001487 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001488 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001489 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 }
1491 }
1492 /* Record that there was a login on that tty from the remote host. */
1493 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001494 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001495 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496}
1497
1498static void
1499mm_session_close(Session *s)
1500{
Damien Miller04bd8b02003-05-25 14:38:33 +10001501 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001503 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 session_pty_cleanup2(s);
1505 }
Damien Miller7207f642008-05-19 15:34:50 +10001506 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507}
1508
1509int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001510mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001512 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513 Session *s;
1514 int res, fd0;
1515
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001516 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517
1518 buffer_clear(m);
1519 s = session_new();
1520 if (s == NULL)
1521 goto error;
1522 s->authctxt = authctxt;
1523 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001524 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1526 if (res == 0)
1527 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528 pty_setowner(authctxt->pw, s->tty);
1529
1530 buffer_put_int(m, 1);
1531 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 /* We need to trick ttyslot */
1534 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001535 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001536
1537 mm_record_login(s, authctxt->pw);
1538
1539 /* Now we can close the file descriptor again */
1540 close(0);
1541
Darren Tucker09991742004-07-17 17:05:14 +10001542 /* send messages generated by record_login */
1543 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1544 buffer_clear(&loginmsg);
1545
1546 mm_request_send(sock, MONITOR_ANS_PTY, m);
1547
Damien Miller54fd7cf2007-09-17 12:04:08 +10001548 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1549 mm_send_fd(sock, s->ttyfd) == -1)
1550 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001551
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001552 /* make sure nothing uses fd 0 */
1553 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001554 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001555 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001556 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001557
1558 /* slave is not needed */
1559 close(s->ttyfd);
1560 s->ttyfd = s->ptyfd;
1561 /* no need to dup() because nobody closes ptyfd */
1562 s->ptymaster = s->ptyfd;
1563
Damien Miller9ab00b42006-08-05 12:40:11 +10001564 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565
1566 return (0);
1567
1568 error:
1569 if (s != NULL)
1570 mm_session_close(s);
1571 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001572 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001573 return (0);
1574}
1575
1576int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001577mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001578{
1579 Session *s;
1580 char *tty;
1581
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001582 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583
1584 tty = buffer_get_string(m, NULL);
1585 if ((s = session_by_tty(tty)) != NULL)
1586 mm_session_close(s);
1587 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001588 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001589 return (0);
1590}
1591
Damien Miller1f0311c2014-05-15 14:24:09 +10001592#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001594mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595{
1596 BIGNUM *p;
1597 int rsafail;
1598
1599 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001600 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601
1602 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001603 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604
1605 buffer_get_bignum2(m, p);
1606
1607 rsafail = ssh1_session_key(p);
1608
1609 buffer_clear(m);
1610 buffer_put_int(m, rsafail);
1611 buffer_put_bignum2(m, p);
1612
1613 BN_clear_free(p);
1614
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001615 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616
1617 /* Turn on permissions for sessid passing */
1618 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1619
1620 return (0);
1621}
1622
1623int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001624mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625{
1626 int i;
1627
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001628 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629
1630 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001631 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 for (i = 0; i < 16; i++)
1633 session_id[i] = buffer_get_char(m);
1634
1635 /* Turn on permissions for getpwnam */
1636 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1637
1638 return (0);
1639}
1640
1641int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001642mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643{
1644 BIGNUM *client_n;
1645 Key *key = NULL;
1646 u_char *blob = NULL;
1647 u_int blen = 0;
1648 int allowed = 0;
1649
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001650 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651
Damien Miller7a8f5b32006-03-31 23:14:23 +11001652 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001653 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001655 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656 buffer_get_bignum2(m, client_n);
1657 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1658 BN_clear_free(client_n);
1659 }
1660 buffer_clear(m);
1661 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001662 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663
1664 /* clear temporarily storage (used by generate challenge) */
1665 monitor_reset_key_state();
1666
1667 if (allowed && key != NULL) {
1668 key->type = KEY_RSA; /* cheat for key_to_blob */
1669 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001670 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 buffer_put_string(m, blob, blen);
1672
1673 /* Save temporarily for comparison in verify */
1674 key_blob = blob;
1675 key_bloblen = blen;
1676 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 }
Damien Miller00111382003-03-10 11:21:17 +11001678 if (key != NULL)
1679 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001681 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682
1683 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1684 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1685 return (0);
1686}
1687
1688int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001689mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690{
1691 Key *key = NULL;
1692 u_char *blob;
1693 u_int blen;
1694
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001695 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696
1697 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001698 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699 blob = buffer_get_string(m, &blen);
1700 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001701 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001702 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001703 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001705 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001706 if (key->type != KEY_RSA)
1707 fatal("%s: received bad key type %d", __func__, key->type);
1708 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 if (ssh1_challenge)
1710 BN_clear_free(ssh1_challenge);
1711 ssh1_challenge = auth_rsa_generate_challenge(key);
1712
1713 buffer_clear(m);
1714 buffer_put_bignum2(m, ssh1_challenge);
1715
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001716 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001717 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718
1719 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001720
Darren Tuckera627d422013-06-02 07:31:17 +10001721 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001722 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723 return (0);
1724}
1725
1726int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001727mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728{
1729 Key *key = NULL;
1730 u_char *blob, *response;
1731 u_int blen, len;
1732 int success;
1733
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001734 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735
1736 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001737 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001739 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740
1741 blob = buffer_get_string(m, &blen);
1742 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001743 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001745 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001748 response = buffer_get_string(m, &len);
1749 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001750 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001751 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1752
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001754 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001755 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756
1757 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1758
1759 /* reset state */
1760 BN_clear_free(ssh1_challenge);
1761 ssh1_challenge = NULL;
1762 monitor_reset_key_state();
1763
1764 buffer_clear(m);
1765 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001766 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001767
1768 return (success);
1769}
Damien Miller1f0311c2014-05-15 14:24:09 +10001770#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771
1772int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001773mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001774{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001775 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 int res, status;
1777
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001778 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001779
1780 /* The child is terminating */
1781 session_destroy_all(&mm_session_close);
1782
Darren Tucker52358d62008-03-11 22:58:25 +11001783#ifdef USE_PAM
1784 if (options.use_pam)
1785 sshpam_cleanup();
1786#endif
1787
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001788 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001789 if (errno != EINTR)
1790 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001791
1792 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1793
1794 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001795 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001796}
1797
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001798#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001799/* Report that an audit event occurred */
1800int
1801mm_answer_audit_event(int socket, Buffer *m)
1802{
1803 ssh_audit_event_t event;
1804
1805 debug3("%s entering", __func__);
1806
1807 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001808 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001809 case SSH_AUTH_FAIL_PUBKEY:
1810 case SSH_AUTH_FAIL_HOSTBASED:
1811 case SSH_AUTH_FAIL_GSSAPI:
1812 case SSH_LOGIN_EXCEED_MAXTRIES:
1813 case SSH_LOGIN_ROOT_DENIED:
1814 case SSH_CONNECTION_CLOSE:
1815 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001816 audit_event(event);
1817 break;
1818 default:
1819 fatal("Audit event type %d not permitted", event);
1820 }
1821
1822 return (0);
1823}
1824
1825int
1826mm_answer_audit_command(int socket, Buffer *m)
1827{
1828 u_int len;
1829 char *cmd;
1830
1831 debug3("%s entering", __func__);
1832 cmd = buffer_get_string(m, &len);
1833 /* sanity check command, if so how? */
1834 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001835 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001836 return (0);
1837}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001838#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001839
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001840void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001841monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001842{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001843 struct ssh *ssh = active_state; /* XXX */
1844 struct kex *kex;
1845 int r;
1846
1847 debug3("%s: packet_set_state", __func__);
1848 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1849 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1850 sshbuf_free(child_state);
1851 child_state = NULL;
1852
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001853 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001854 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001855#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001856 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1857 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1858 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1859 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001860# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001861 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001862# endif
Damien Miller773dda22015-01-30 23:10:17 +11001863#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001864 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1865 kex->load_host_public_key=&get_hostkey_public_by_type;
1866 kex->load_host_private_key=&get_hostkey_private_by_type;
1867 kex->host_key_index=&get_hostkey_index;
1868 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001869 }
1870
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001871 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001872 if (options.compression) {
1873 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1874 (ssh_packet_comp_alloc_func *)mm_zalloc,
1875 (ssh_packet_comp_free_func *)mm_zfree);
1876 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877}
1878
1879/* This function requries careful sanity checking */
1880
1881void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001882mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001883{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001884 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001885
markus@openbsd.org091c3022015-01-19 19:52:16 +00001886 if ((child_state = sshbuf_new()) == NULL)
1887 fatal("%s: sshbuf_new failed", __func__);
1888 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1889 child_state);
1890 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001891}
1892
1893
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001894/* XXX */
1895
1896#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001897 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001898 fatal("fcntl(%d, F_SETFD)", x); \
1899} while (0)
1900
1901static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001902monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001903{
Damien Miller8f0bf232011-06-20 14:42:23 +10001904 int pair[2];
1905
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001906 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001907 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908 FD_CLOSEONEXEC(pair[0]);
1909 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001910 mon->m_recvfd = pair[0];
1911 mon->m_sendfd = pair[1];
1912
1913 if (do_logfds) {
1914 if (pipe(pair) == -1)
1915 fatal("%s: pipe: %s", __func__, strerror(errno));
1916 FD_CLOSEONEXEC(pair[0]);
1917 FD_CLOSEONEXEC(pair[1]);
1918 mon->m_log_recvfd = pair[0];
1919 mon->m_log_sendfd = pair[1];
1920 } else
1921 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001922}
1923
1924#define MM_MEMSIZE 65536
1925
1926struct monitor *
1927monitor_init(void)
1928{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001929 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001931
Damien Miller07d86be2006-03-26 14:19:21 +11001932 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001933
Damien Miller8f0bf232011-06-20 14:42:23 +10001934 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001935
1936 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001937 if (options.compression) {
1938 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1939 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001940
Damien Miller2d6b8332002-06-21 15:59:49 +10001941 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1943 (ssh_packet_comp_alloc_func *)mm_zalloc,
1944 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001945 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001946
1947 return mon;
1948}
1949
1950void
1951monitor_reinit(struct monitor *mon)
1952{
Damien Miller8f0bf232011-06-20 14:42:23 +10001953 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001954}
Darren Tucker0efd1552003-08-26 11:49:55 +10001955
1956#ifdef GSSAPI
1957int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001958mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001959{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001960 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001961 OM_uint32 major;
1962 u_int len;
1963
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001964 goid.elements = buffer_get_string(m, &len);
1965 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001966
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001967 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001968
Darren Tuckera627d422013-06-02 07:31:17 +10001969 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001970
1971 buffer_clear(m);
1972 buffer_put_int(m, major);
1973
Damien Miller6fd6def2005-11-05 15:07:05 +11001974 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001975
1976 /* Now we have a context, enable the step */
1977 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1978
1979 return (0);
1980}
1981
1982int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001983mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001984{
1985 gss_buffer_desc in;
1986 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001987 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001988 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001989 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001990
Darren Tucker600ad8d2003-08-26 12:10:48 +10001991 in.value = buffer_get_string(m, &len);
1992 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001993 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001994 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001995
1996 buffer_clear(m);
1997 buffer_put_int(m, major);
1998 buffer_put_string(m, out.value, out.length);
1999 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002000 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002001
2002 gss_release_buffer(&minor, &out);
2003
Damien Miller6fd6def2005-11-05 15:07:05 +11002004 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002005 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2006 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002007 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002008 }
2009 return (0);
2010}
2011
2012int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002013mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002014{
2015 gss_buffer_desc gssbuf, mic;
2016 OM_uint32 ret;
2017 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002018
Damien Miller0425d402003-11-17 22:18:21 +11002019 gssbuf.value = buffer_get_string(m, &len);
2020 gssbuf.length = len;
2021 mic.value = buffer_get_string(m, &len);
2022 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002023
Damien Miller0425d402003-11-17 22:18:21 +11002024 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002025
Darren Tuckera627d422013-06-02 07:31:17 +10002026 free(gssbuf.value);
2027 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002028
Damien Miller0425d402003-11-17 22:18:21 +11002029 buffer_clear(m);
2030 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002031
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002032 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002033
Damien Miller0425d402003-11-17 22:18:21 +11002034 if (!GSS_ERROR(ret))
2035 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002036
Damien Miller0425d402003-11-17 22:18:21 +11002037 return (0);
2038}
2039
2040int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002041mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002042{
2043 int authenticated;
2044
2045 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2046
2047 buffer_clear(m);
2048 buffer_put_int(m, authenticated);
2049
2050 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002051 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002052
Damien Miller6fd6def2005-11-05 15:07:05 +11002053 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002054
2055 /* Monitor loop will terminate if authenticated */
2056 return (authenticated);
2057}
2058#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002059