blob: 0af23d65022d6ad6d34f551fc24048470516f82c [file] [log] [blame]
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001/* $OpenBSD: monitor.c,v 1.153 2015/09/04 04:44:08 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"
Darren Tuckerb422afa2009-06-21 18:58:46 +1000103#include "roaming.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000104#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000105#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000106#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
Darren Tucker0efd1552003-08-26 11:49:55 +1000108#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000109static Gssctxt *gsscontext = NULL;
110#endif
111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112/* Imports */
113extern ServerOptions options;
114extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern Buffer auth_debug;
117extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000118extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119
120/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000121static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000122
Damien Miller469954d2003-06-18 20:25:33 +1000123/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000124
125int mm_answer_moduli(int, Buffer *);
126int mm_answer_sign(int, Buffer *);
127int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000128int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000129int mm_answer_authserv(int, Buffer *);
130int mm_answer_authpassword(int, Buffer *);
131int mm_answer_bsdauthquery(int, Buffer *);
132int mm_answer_bsdauthrespond(int, Buffer *);
133int mm_answer_skeyquery(int, Buffer *);
134int mm_answer_skeyrespond(int, Buffer *);
135int mm_answer_keyallowed(int, Buffer *);
136int mm_answer_keyverify(int, Buffer *);
137int mm_answer_pty(int, Buffer *);
138int mm_answer_pty_cleanup(int, Buffer *);
139int mm_answer_term(int, Buffer *);
140int mm_answer_rsa_keyallowed(int, Buffer *);
141int mm_answer_rsa_challenge(int, Buffer *);
142int mm_answer_rsa_response(int, Buffer *);
143int mm_answer_sesskey(int, Buffer *);
144int mm_answer_sessid(int, Buffer *);
145
Damien Miller79418552002-04-23 20:28:48 +1000146#ifdef USE_PAM
147int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000148int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000149int mm_answer_pam_init_ctx(int, Buffer *);
150int mm_answer_pam_query(int, Buffer *);
151int mm_answer_pam_respond(int, Buffer *);
152int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000153#endif
154
Darren Tucker0efd1552003-08-26 11:49:55 +1000155#ifdef GSSAPI
156int mm_answer_gss_setup_ctx(int, Buffer *);
157int mm_answer_gss_accept_ctx(int, Buffer *);
158int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100159int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000160#endif
Damien Miller25162f22002-09-12 09:47:29 +1000161
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100162#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100163int mm_answer_audit_event(int, Buffer *);
164int mm_answer_audit_command(int, Buffer *);
165#endif
166
Damien Miller8f0bf232011-06-20 14:42:23 +1000167static int monitor_read_log(struct monitor *);
168
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000170
171#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000173#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000174
175/* local state for key verify */
176static u_char *key_blob = NULL;
177static u_int key_bloblen = 0;
178static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000179static char *hostbased_cuser = NULL;
180static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000181static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100182static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000183static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000184static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000185static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000186
187struct mon_table {
188 enum monitor_reqtype type;
189 int flags;
190 int (*f)(int, Buffer *);
191};
192
193#define MON_ISAUTH 0x0004 /* Required for Authentication */
194#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
195#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100196#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
198#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
199
200#define MON_PERMIT 0x1000 /* Request is permitted */
201
202struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000203#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000205#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
207 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
208 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000209 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000211#ifdef USE_PAM
212 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000213 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000214 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
215 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
216 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
217 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000218#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100219#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100220 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100221#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000222#ifdef BSD_AUTH
223 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100224 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225#endif
226#ifdef SKEY
227 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
228 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
229#endif
230 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
231 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000232#ifdef GSSAPI
233 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
234 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
235 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100236 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000237#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238 {0, 0, NULL}
239};
240
241struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000242#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000244#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
246 {MONITOR_REQ_PTY, 0, mm_answer_pty},
247 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
248 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100249#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100250 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
251 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
252#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000253 {0, 0, NULL}
254};
255
256struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000257#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
259 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
260 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
261 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100262 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
263 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
265 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
266#ifdef BSD_AUTH
267 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100268 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000269#endif
270#ifdef SKEY
271 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
272 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
273#endif
Damien Millera33501b2002-05-08 12:24:42 +1000274#ifdef USE_PAM
275 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000276 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000277 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
278 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
279 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
280 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000281#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100282#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100283 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100284#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000285#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 {0, 0, NULL}
287};
288
289struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000290#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
292 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
293 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100294#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100295 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000296 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100297#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000298#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000299 {0, 0, NULL}
300};
301
302struct mon_table *mon_dispatch;
303
304/* Specifies if a certain message is allowed at the moment */
305
306static void
307monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
308{
309 while (ent->f != NULL) {
310 if (ent->type == type) {
311 ent->flags &= ~MON_PERMIT;
312 ent->flags |= permit ? MON_PERMIT : 0;
313 return;
314 }
315 ent++;
316 }
317}
318
319static void
320monitor_permit_authentications(int permit)
321{
322 struct mon_table *ent = mon_dispatch;
323
324 while (ent->f != NULL) {
325 if (ent->flags & MON_AUTH) {
326 ent->flags &= ~MON_PERMIT;
327 ent->flags |= permit ? MON_PERMIT : 0;
328 }
329 ent++;
330 }
331}
332
Darren Tucker3e33cec2003-10-02 16:12:36 +1000333void
334monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335{
336 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100337 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000338
339 debug3("preauth child monitor started");
340
Damien Miller8f0bf232011-06-20 14:42:23 +1000341 close(pmonitor->m_recvfd);
342 close(pmonitor->m_log_sendfd);
343 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
344
Darren Tucker3e33cec2003-10-02 16:12:36 +1000345 authctxt = _authctxt;
346 memset(authctxt, 0, sizeof(*authctxt));
347
Darren Tuckerde0de392005-03-31 23:52:04 +1000348 authctxt->loginmsg = &loginmsg;
349
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350 if (compat20) {
351 mon_dispatch = mon_dispatch_proto20;
352
353 /* Permit requests for moduli and signatures */
354 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
355 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
356 } else {
357 mon_dispatch = mon_dispatch_proto15;
358
359 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
360 }
361
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000362 /* The first few requests do not require asynchronous access */
363 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100364 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100365 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100366 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100367 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100368
369 /* Special handling for multiple required authentications */
370 if (options.num_auth_methods != 0) {
371 if (!compat20)
372 fatal("AuthenticationMethods is not supported"
373 "with SSH protocol 1");
374 if (authenticated &&
375 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000376 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100377 debug3("%s: method %s: partial", __func__,
378 auth_method);
379 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100380 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100381 }
382 }
383
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384 if (authenticated) {
385 if (!(ent->flags & MON_AUTHDECIDE))
386 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388 if (authctxt->pw->pw_uid == 0 &&
389 !auth_root_allowed(auth_method))
390 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000391#ifdef USE_PAM
392 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100393 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000394 Buffer m;
395
396 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100397 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000398 MONITOR_REQ_PAM_ACCOUNT, &m);
399 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
400 buffer_free(&m);
401 }
402#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100404 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100405 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000406 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000407 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 authctxt->failures++;
409 }
410 }
411
412 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000413 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100414 if (strcmp(auth_method, "unknown") == 0)
415 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
417 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000418 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000419
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000420 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000421
Damien Miller6a1937e2012-12-12 10:44:38 +1100422 /* Drain any buffered messages from the child */
423 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
424 ;
425
Damien Miller8f0bf232011-06-20 14:42:23 +1000426 close(pmonitor->m_sendfd);
427 close(pmonitor->m_log_recvfd);
428 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429}
430
Damien Millerbe64d432003-05-14 19:31:12 +1000431static void
432monitor_set_child_handler(pid_t pid)
433{
434 monitor_child_pid = pid;
435}
436
437static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000438monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000439{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000440 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000441}
442
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000443void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000444monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445{
Damien Miller8f0bf232011-06-20 14:42:23 +1000446 close(pmonitor->m_recvfd);
447 pmonitor->m_recvfd = -1;
448
Damien Millerbe64d432003-05-14 19:31:12 +1000449 monitor_set_child_handler(pmonitor->m_pid);
450 signal(SIGHUP, &monitor_child_handler);
451 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000452 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000453#ifdef SIGXFSZ
454 signal(SIGXFSZ, SIG_IGN);
455#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000456
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457 if (compat20) {
458 mon_dispatch = mon_dispatch_postauth20;
459
460 /* Permit requests for moduli and signatures */
461 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
462 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
463 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 } else {
465 mon_dispatch = mon_dispatch_postauth15;
466 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
467 }
468 if (!no_pty_flag) {
469 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
470 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
471 }
472
473 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000474 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000475}
476
477void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000478monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479{
Damien Miller2d6b8332002-06-21 15:59:49 +1000480 if (options.compression) {
481 /* The member allocation is not visible, so sync it */
482 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
483 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484}
485
markus@openbsd.org091c3022015-01-19 19:52:16 +0000486/* Allocation functions for zlib */
487static void *
488mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
489{
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000490 if (size == 0 || ncount == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000491 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
492
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000493 return mm_malloc(mm, size * ncount);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494}
495
496static void
497mm_zfree(struct mm_master *mm, void *address)
498{
499 mm_free(mm, address);
500}
501
Damien Miller8f0bf232011-06-20 14:42:23 +1000502static int
503monitor_read_log(struct monitor *pmonitor)
504{
505 Buffer logmsg;
506 u_int len, level;
507 char *msg;
508
509 buffer_init(&logmsg);
510
511 /* Read length */
512 buffer_append_space(&logmsg, 4);
513 if (atomicio(read, pmonitor->m_log_recvfd,
514 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
515 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100516 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000517 debug("%s: child log fd closed", __func__);
518 close(pmonitor->m_log_recvfd);
519 pmonitor->m_log_recvfd = -1;
520 return -1;
521 }
522 fatal("%s: log fd read: %s", __func__, strerror(errno));
523 }
524 len = buffer_get_int(&logmsg);
525 if (len <= 4 || len > 8192)
526 fatal("%s: invalid log message length %u", __func__, len);
527
528 /* Read severity, message */
529 buffer_clear(&logmsg);
530 buffer_append_space(&logmsg, len);
531 if (atomicio(read, pmonitor->m_log_recvfd,
532 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
533 fatal("%s: log fd read: %s", __func__, strerror(errno));
534
535 /* Log it */
536 level = buffer_get_int(&logmsg);
537 msg = buffer_get_string(&logmsg, NULL);
538 if (log_level_name(level) == NULL)
539 fatal("%s: invalid log level %u (corrupted message?)",
540 __func__, level);
541 do_log2(level, "%s [preauth]", msg);
542
543 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000544 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000545
546 return 0;
547}
548
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000549int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000550monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 struct mon_table **pent)
552{
553 Buffer m;
554 int ret;
555 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000556 struct pollfd pfd[2];
557
558 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100559 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000560 pfd[0].fd = pmonitor->m_sendfd;
561 pfd[0].events = POLLIN;
562 pfd[1].fd = pmonitor->m_log_recvfd;
563 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000564 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
565 if (errno == EINTR || errno == EAGAIN)
566 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000567 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000568 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000569 if (pfd[1].revents) {
570 /*
571 * Drain all log messages before processing next
572 * monitor request.
573 */
574 monitor_read_log(pmonitor);
575 continue;
576 }
577 if (pfd[0].revents)
578 break; /* Continues below */
579 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580
581 buffer_init(&m);
582
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000583 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584 type = buffer_get_char(&m);
585
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000586 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
588 while (ent->f != NULL) {
589 if (ent->type == type)
590 break;
591 ent++;
592 }
593
594 if (ent->f != NULL) {
595 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000596 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000598 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599 buffer_free(&m);
600
601 /* The child may use this request only once, disable it */
602 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000603 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 type);
605 ent->flags &= ~MON_PERMIT;
606 }
607
608 if (pent != NULL)
609 *pent = ent;
610
611 return ret;
612 }
613
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000614 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615
616 /* NOTREACHED */
617 return (-1);
618}
619
620/* allowed key state */
621static int
622monitor_allowed_key(u_char *blob, u_int bloblen)
623{
624 /* make sure key is allowed */
625 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000626 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627 return (0);
628 return (1);
629}
630
631static void
632monitor_reset_key_state(void)
633{
634 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000635 free(key_blob);
636 free(hostbased_cuser);
637 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638 key_blob = NULL;
639 key_bloblen = 0;
640 key_blobtype = MM_NOKEY;
641 hostbased_cuser = NULL;
642 hostbased_chost = NULL;
643}
644
Damien Miller1f0311c2014-05-15 14:24:09 +1000645#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000647mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648{
649 DH *dh;
650 int min, want, max;
651
652 min = buffer_get_int(m);
653 want = buffer_get_int(m);
654 max = buffer_get_int(m);
655
656 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000657 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658 /* We need to check here, too, in case the child got corrupted */
659 if (max < min || want < min || max < want)
660 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000661 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000662
663 buffer_clear(m);
664
665 dh = choose_dh(min, want, max);
666 if (dh == NULL) {
667 buffer_put_char(m, 0);
668 return (0);
669 } else {
670 /* Send first bignum */
671 buffer_put_char(m, 1);
672 buffer_put_bignum2(m, dh->p);
673 buffer_put_bignum2(m, dh->g);
674
675 DH_free(dh);
676 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000677 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000678 return (0);
679}
Damien Miller1f0311c2014-05-15 14:24:09 +1000680#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681
682int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000683mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000685 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000686 extern int auth_sock; /* XXX move to state struct? */
687 struct sshkey *key;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000688 struct sshbuf *sigbuf;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 u_char *p;
690 u_char *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000691 size_t datlen, siglen;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000692 int r, keyid, is_proof = 0;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000693 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000695 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
djm@openbsd.org141efe42015-01-14 20:05:27 +0000697 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
698 (r = sshbuf_get_string(m, &p, &datlen)) != 0)
699 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) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000745 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen,
746 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,
752 p, datlen, datafellows)) != 0) {
753 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
Darren Tuckera627d422013-06-02 07:31:17 +1000766 free(p);
767 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000769 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770
771 /* Turn on permissions for getpwnam */
772 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
773
774 return (0);
775}
776
777/* Retrieves the password entry and also checks if the user is permitted */
778
779int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000780mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000782 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 struct passwd *pwent;
784 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000785 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000787 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788
789 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000790 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
Darren Tuckerb09b6772004-06-22 15:06:46 +1000792 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793
Darren Tuckerb09b6772004-06-22 15:06:46 +1000794 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795
Darren Tuckerb09b6772004-06-22 15:06:46 +1000796 authctxt->user = xstrdup(username);
797 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000798 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000799
800 buffer_clear(m);
801
802 if (pwent == NULL) {
803 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100804 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805 goto out;
806 }
807
808 allowed = 1;
809 authctxt->pw = pwent;
810 authctxt->valid = 1;
811
812 buffer_put_char(m, 1);
813 buffer_put_string(m, pwent, sizeof(struct passwd));
814 buffer_put_cstring(m, pwent->pw_name);
815 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000816#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000818#endif
819#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000821#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822 buffer_put_cstring(m, pwent->pw_dir);
823 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100824
825 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100826 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000827
828#define M_CP_STROPT(x) do { \
829 if (options.x != NULL) \
830 buffer_put_cstring(m, options.x); \
831 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000832#define M_CP_STRARRAYOPT(x, nx) do { \
833 for (i = 0; i < options.nx; i++) \
834 buffer_put_cstring(m, options.x[i]); \
835 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000836 /* See comment in servconf.h */
837 COPY_MATCH_STRING_OPTS();
838#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000839#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100840
841 /* Create valid auth method lists */
842 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
843 /*
844 * The monitor will continue long enough to let the child
845 * run to it's packet_disconnect(), but it must not allow any
846 * authentication to succeed.
847 */
848 debug("%s: no valid authentication method lists", __func__);
849 }
850
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000851 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000852 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853
854 /* For SSHv1 allow authentication now */
855 if (!compat20)
856 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000857 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858 /* Allow service/style information on the auth context */
859 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000860 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
861 }
Damien Millera33501b2002-05-08 12:24:42 +1000862#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000863 if (options.use_pam)
864 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000865#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866
867 return (0);
868}
869
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000870int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000871{
872 char *banner;
873
874 buffer_clear(m);
875 banner = auth2_read_banner();
876 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000877 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000878 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000879
880 return (0);
881}
882
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000884mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885{
886 monitor_permit_authentications(1);
887
888 authctxt->service = buffer_get_string(m, NULL);
889 authctxt->style = buffer_get_string(m, NULL);
890 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000891 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892
893 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000894 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895 authctxt->style = NULL;
896 }
897
898 return (0);
899}
900
901int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000902mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903{
904 static int call_count;
905 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000906 int authenticated;
907 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
909 passwd = buffer_get_string(m, &plen);
910 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000911 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000912 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100913 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000914 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915
916 buffer_clear(m);
917 buffer_put_int(m, authenticated);
918
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000919 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000920 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921
922 call_count++;
923 if (plen == 0 && call_count == 1)
924 auth_method = "none";
925 else
926 auth_method = "password";
927
928 /* Causes monitor loop to terminate if authenticated */
929 return (authenticated);
930}
931
932#ifdef BSD_AUTH
933int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000934mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935{
936 char *name, *infotxt;
937 u_int numprompts;
938 u_int *echo_on;
939 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100940 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
Damien Millerb7df3af2003-02-24 11:55:46 +1100942 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
943 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100946 buffer_put_int(m, success);
947 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948 buffer_put_cstring(m, prompts[0]);
949
Damien Millerb7df3af2003-02-24 11:55:46 +1100950 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000951 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952
Damien Millerb7df3af2003-02-24 11:55:46 +1100953 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(name);
955 free(infotxt);
956 free(prompts);
957 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 }
959
960 return (0);
961}
962
963int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000964mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965{
966 char *response;
967 int authok;
968
969 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000970 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971
972 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000973 authok = options.challenge_response_authentication &&
974 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000977 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978
979 buffer_clear(m);
980 buffer_put_int(m, authok);
981
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000982 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000983 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
Damien Miller91a55f22013-04-23 15:18:10 +1000985 if (compat20) {
986 auth_method = "keyboard-interactive";
987 auth_submethod = "bsdauth";
988 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100989 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990
991 return (authok != 0);
992}
993#endif
994
995#ifdef SKEY
996int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000997mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998{
999 struct skey skey;
1000 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +11001001 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
Darren Tucker06a8cfe2004-04-14 17:24:30 +10001003 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1004 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
1006 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001007 buffer_put_int(m, success);
1008 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 buffer_put_cstring(m, challenge);
1010
Damien Millerb7df3af2003-02-24 11:55:46 +11001011 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001012 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013
1014 return (0);
1015}
1016
1017int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001018mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019{
1020 char *response;
1021 int authok;
1022
1023 response = buffer_get_string(m, NULL);
1024
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001025 authok = (options.challenge_response_authentication &&
1026 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 skey_haskey(authctxt->pw->pw_name) == 0 &&
1028 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1029
Darren Tuckerf60845f2013-06-02 08:07:31 +10001030 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031
1032 buffer_clear(m);
1033 buffer_put_int(m, authok);
1034
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001035 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001036 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037
1038 auth_method = "skey";
1039
1040 return (authok != 0);
1041}
1042#endif
1043
Damien Miller79418552002-04-23 20:28:48 +10001044#ifdef USE_PAM
1045int
Darren Tucker7eda5922004-06-22 13:22:50 +10001046mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001047{
Damien Miller4e448a32003-05-14 15:11:48 +10001048 if (!options.use_pam)
1049 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1050
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001051 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001052
Damien Miller1f499fd2003-08-25 13:08:49 +10001053 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1054
Damien Miller79418552002-04-23 20:28:48 +10001055 return (0);
1056}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057
Damien Miller1f499fd2003-08-25 13:08:49 +10001058int
Darren Tucker7eda5922004-06-22 13:22:50 +10001059mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001060{
1061 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001062
Damien Miller1f499fd2003-08-25 13:08:49 +10001063 if (!options.use_pam)
1064 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1065
1066 ret = do_pam_account();
1067
1068 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001069 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001070
Darren Tucker7eda5922004-06-22 13:22:50 +10001071 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001072
1073 return (ret);
1074}
1075
Damien Miller4f9f42a2003-05-10 19:28:02 +10001076static void *sshpam_ctxt, *sshpam_authok;
1077extern KbdintDevice sshpam_device;
1078
1079int
Darren Tucker7eda5922004-06-22 13:22:50 +10001080mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082 debug3("%s", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1084 sshpam_authok = NULL;
1085 buffer_clear(m);
1086 if (sshpam_ctxt != NULL) {
1087 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1088 buffer_put_int(m, 1);
1089 } else {
1090 buffer_put_int(m, 0);
1091 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001092 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001093 return (0);
1094}
1095
1096int
Darren Tucker7eda5922004-06-22 13:22:50 +10001097mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001099 char *name = NULL, *info = NULL, **prompts = NULL;
1100 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001101 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102
1103 debug3("%s", __func__);
1104 sshpam_authok = NULL;
1105 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1106 if (ret == 0 && num == 0)
1107 sshpam_authok = sshpam_ctxt;
1108 if (num > 1 || name == NULL || info == NULL)
1109 ret = -1;
1110 buffer_clear(m);
1111 buffer_put_int(m, ret);
1112 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001113 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001114 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001115 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001116 buffer_put_int(m, num);
1117 for (i = 0; i < num; ++i) {
1118 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001119 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001120 buffer_put_int(m, echo_on[i]);
1121 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001122 free(prompts);
1123 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001124 auth_method = "keyboard-interactive";
1125 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001126 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001127 return (0);
1128}
1129
1130int
Darren Tucker7eda5922004-06-22 13:22:50 +10001131mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001132{
1133 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001134 u_int i, num;
1135 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001136
1137 debug3("%s", __func__);
1138 sshpam_authok = NULL;
1139 num = buffer_get_int(m);
1140 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001141 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001142 for (i = 0; i < num; ++i)
1143 resp[i] = buffer_get_string(m, NULL);
1144 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1145 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001146 free(resp[i]);
1147 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001148 } else {
1149 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1150 }
1151 buffer_clear(m);
1152 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001153 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001154 auth_method = "keyboard-interactive";
1155 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001156 if (ret == 0)
1157 sshpam_authok = sshpam_ctxt;
1158 return (0);
1159}
1160
1161int
Darren Tucker7eda5922004-06-22 13:22:50 +10001162mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001163{
Damien Miller5e75f512015-08-11 13:34:12 +10001164 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001165
1166 debug3("%s", __func__);
1167 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001168 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001169 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001170 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001171 auth_method = "keyboard-interactive";
1172 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001173 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001174}
Damien Miller79418552002-04-23 20:28:48 +10001175#endif
1176
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001178mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179{
1180 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001181 char *cuser, *chost;
1182 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001183 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184 enum mm_keytype type = 0;
1185 int allowed = 0;
1186
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001187 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188
1189 type = buffer_get_int(m);
1190 cuser = buffer_get_string(m, NULL);
1191 chost = buffer_get_string(m, NULL);
1192 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001193 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194
1195 key = key_from_blob(blob, bloblen);
1196
1197 if ((compat20 && type == MM_RSAHOSTKEY) ||
1198 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001199 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
Damien Miller3e3b5142003-11-17 21:13:40 +11001203 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001204 /* These should not make it past the privsep child */
1205 if (key_type_plain(key->type) == KEY_RSA &&
1206 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1207 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1208
Darren Tucker47eede72005-03-14 23:08:12 +11001209 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001211 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001212 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001213 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001214 options.pubkey_key_types, 0) == 1 &&
1215 user_key_allowed(authctxt->pw, key,
1216 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001217 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001218 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001219 if (options.pubkey_authentication &&
1220 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001221 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 break;
1223 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001224 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001225 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001226 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001227 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001229 pubkey_auth_info(authctxt, key,
1230 "client user \"%.100s\", client host \"%.100s\"",
1231 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001232 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001234#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 case MM_RSAHOSTKEY:
1236 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001237 allowed = options.rhosts_rsa_authentication &&
1238 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001240 if (options.rhosts_rsa_authentication && allowed != 1)
1241 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001242 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001244#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001246 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 break;
1248 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249 }
Damien Miller00111382003-03-10 11:21:17 +11001250 if (key != NULL)
1251 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252
1253 /* clear temporarily storage (used by verify) */
1254 monitor_reset_key_state();
1255
1256 if (allowed) {
1257 /* Save temporarily for comparison in verify */
1258 key_blob = blob;
1259 key_bloblen = bloblen;
1260 key_blobtype = type;
1261 hostbased_cuser = cuser;
1262 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001263 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001264 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001265 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(blob);
1267 free(cuser);
1268 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 }
1270
1271 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001272 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
1274 buffer_clear(m);
1275 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001276 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001278 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279
1280 if (type == MM_RSAHOSTKEY)
1281 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1282
1283 return (0);
1284}
1285
1286static int
1287monitor_valid_userblob(u_char *data, u_int datalen)
1288{
1289 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001290 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291 u_int len;
1292 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293
1294 buffer_init(&b);
1295 buffer_append(&b, data, datalen);
1296
1297 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001298 p = buffer_ptr(&b);
1299 len = buffer_len(&b);
1300 if ((session_id2 == NULL) ||
1301 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001302 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001303 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304 buffer_consume(&b, session_id2_len);
1305 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001306 p = buffer_get_string(&b, &len);
1307 if ((session_id2 == NULL) ||
1308 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001309 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001311 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 }
1313 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1314 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001315 p = buffer_get_cstring(&b, NULL);
1316 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1317 authctxt->style ? ":" : "",
1318 authctxt->style ? authctxt->style : "");
1319 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001320 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001321 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322 fail++;
1323 }
Darren Tuckera627d422013-06-02 07:31:17 +10001324 free(userstyle);
1325 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326 buffer_skip_string(&b);
1327 if (datafellows & SSH_BUG_PKAUTH) {
1328 if (!buffer_get_char(&b))
1329 fail++;
1330 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001331 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332 if (strcmp("publickey", p) != 0)
1333 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001334 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335 if (!buffer_get_char(&b))
1336 fail++;
1337 buffer_skip_string(&b);
1338 }
1339 buffer_skip_string(&b);
1340 if (buffer_len(&b) != 0)
1341 fail++;
1342 buffer_free(&b);
1343 return (fail == 0);
1344}
1345
1346static int
Damien Millera10f5612002-09-12 09:49:15 +10001347monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1348 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349{
1350 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001351 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352 u_int len;
1353 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
1355 buffer_init(&b);
1356 buffer_append(&b, data, datalen);
1357
Ben Lindstromf67e0772002-06-06 20:58:19 +00001358 p = buffer_get_string(&b, &len);
1359 if ((session_id2 == NULL) ||
1360 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001361 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001363 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001364
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1366 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001367 p = buffer_get_cstring(&b, NULL);
1368 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1369 authctxt->style ? ":" : "",
1370 authctxt->style ? authctxt->style : "");
1371 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001372 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001373 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374 fail++;
1375 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001376 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001377 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001379 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 if (strcmp(p, "hostbased") != 0)
1381 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001382 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383 buffer_skip_string(&b); /* pkalg */
1384 buffer_skip_string(&b); /* pkblob */
1385
1386 /* verify client host, strip trailing dot if necessary */
1387 p = buffer_get_string(&b, NULL);
1388 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1389 p[len - 1] = '\0';
1390 if (strcmp(p, chost) != 0)
1391 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001392 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393
1394 /* verify client user */
1395 p = buffer_get_string(&b, NULL);
1396 if (strcmp(p, cuser) != 0)
1397 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001398 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399
1400 if (buffer_len(&b) != 0)
1401 fail++;
1402 buffer_free(&b);
1403 return (fail == 0);
1404}
1405
1406int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001407mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408{
1409 Key *key;
1410 u_char *signature, *data, *blob;
1411 u_int signaturelen, datalen, bloblen;
1412 int verified = 0;
1413 int valid_data = 0;
1414
1415 blob = buffer_get_string(m, &bloblen);
1416 signature = buffer_get_string(m, &signaturelen);
1417 data = buffer_get_string(m, &datalen);
1418
1419 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001420 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001421 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422
1423 key = key_from_blob(blob, bloblen);
1424 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001425 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426
1427 switch (key_blobtype) {
1428 case MM_USERKEY:
1429 valid_data = monitor_valid_userblob(data, datalen);
1430 break;
1431 case MM_HOSTKEY:
1432 valid_data = monitor_valid_hostbasedblob(data, datalen,
1433 hostbased_cuser, hostbased_chost);
1434 break;
1435 default:
1436 valid_data = 0;
1437 break;
1438 }
1439 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001440 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441
1442 verified = key_verify(key, signature, signaturelen, data, datalen);
1443 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001444 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001446 /* If auth was successful then record key to ensure it isn't reused */
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001447 if (verified == 1 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001448 auth2_record_userkey(authctxt, key);
1449 else
1450 key_free(key);
1451
Darren Tuckera627d422013-06-02 07:31:17 +10001452 free(blob);
1453 free(signature);
1454 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455
Ben Lindstrome1c09122002-06-23 00:38:24 +00001456 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1457
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458 monitor_reset_key_state();
1459
1460 buffer_clear(m);
1461 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001462 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463
Darren Tuckerfbba7352006-11-07 23:16:08 +11001464 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465}
1466
1467static void
1468mm_record_login(Session *s, struct passwd *pw)
1469{
1470 socklen_t fromlen;
1471 struct sockaddr_storage from;
1472
djm@openbsd.org17d4d9d2015-04-17 04:32:31 +00001473 if (options.use_login)
1474 return;
1475
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476 /*
1477 * Get IP address of client. If the connection is not a socket, let
1478 * the address be 0.0.0.0.
1479 */
1480 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001481 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001484 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001486 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 }
1488 }
1489 /* Record that there was a login on that tty from the remote host. */
1490 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001491 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001492 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493}
1494
1495static void
1496mm_session_close(Session *s)
1497{
Damien Miller04bd8b02003-05-25 14:38:33 +10001498 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001500 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501 session_pty_cleanup2(s);
1502 }
Damien Miller7207f642008-05-19 15:34:50 +10001503 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504}
1505
1506int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001507mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001509 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510 Session *s;
1511 int res, fd0;
1512
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001513 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514
1515 buffer_clear(m);
1516 s = session_new();
1517 if (s == NULL)
1518 goto error;
1519 s->authctxt = authctxt;
1520 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001521 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1523 if (res == 0)
1524 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525 pty_setowner(authctxt->pw, s->tty);
1526
1527 buffer_put_int(m, 1);
1528 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529
1530 /* We need to trick ttyslot */
1531 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001532 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533
1534 mm_record_login(s, authctxt->pw);
1535
1536 /* Now we can close the file descriptor again */
1537 close(0);
1538
Darren Tucker09991742004-07-17 17:05:14 +10001539 /* send messages generated by record_login */
1540 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1541 buffer_clear(&loginmsg);
1542
1543 mm_request_send(sock, MONITOR_ANS_PTY, m);
1544
Damien Miller54fd7cf2007-09-17 12:04:08 +10001545 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1546 mm_send_fd(sock, s->ttyfd) == -1)
1547 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001548
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001549 /* make sure nothing uses fd 0 */
1550 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001551 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001552 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001553 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554
1555 /* slave is not needed */
1556 close(s->ttyfd);
1557 s->ttyfd = s->ptyfd;
1558 /* no need to dup() because nobody closes ptyfd */
1559 s->ptymaster = s->ptyfd;
1560
Damien Miller9ab00b42006-08-05 12:40:11 +10001561 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562
1563 return (0);
1564
1565 error:
1566 if (s != NULL)
1567 mm_session_close(s);
1568 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001569 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001570 return (0);
1571}
1572
1573int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001574mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575{
1576 Session *s;
1577 char *tty;
1578
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001579 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580
1581 tty = buffer_get_string(m, NULL);
1582 if ((s = session_by_tty(tty)) != NULL)
1583 mm_session_close(s);
1584 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001585 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586 return (0);
1587}
1588
Damien Miller1f0311c2014-05-15 14:24:09 +10001589#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001591mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592{
1593 BIGNUM *p;
1594 int rsafail;
1595
1596 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001597 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598
1599 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001600 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601
1602 buffer_get_bignum2(m, p);
1603
1604 rsafail = ssh1_session_key(p);
1605
1606 buffer_clear(m);
1607 buffer_put_int(m, rsafail);
1608 buffer_put_bignum2(m, p);
1609
1610 BN_clear_free(p);
1611
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001612 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613
1614 /* Turn on permissions for sessid passing */
1615 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1616
1617 return (0);
1618}
1619
1620int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001621mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001622{
1623 int i;
1624
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001625 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626
1627 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001628 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629 for (i = 0; i < 16; i++)
1630 session_id[i] = buffer_get_char(m);
1631
1632 /* Turn on permissions for getpwnam */
1633 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1634
1635 return (0);
1636}
1637
1638int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001639mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640{
1641 BIGNUM *client_n;
1642 Key *key = NULL;
1643 u_char *blob = NULL;
1644 u_int blen = 0;
1645 int allowed = 0;
1646
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001647 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648
Damien Miller7a8f5b32006-03-31 23:14:23 +11001649 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001650 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001652 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653 buffer_get_bignum2(m, client_n);
1654 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1655 BN_clear_free(client_n);
1656 }
1657 buffer_clear(m);
1658 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001659 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660
1661 /* clear temporarily storage (used by generate challenge) */
1662 monitor_reset_key_state();
1663
1664 if (allowed && key != NULL) {
1665 key->type = KEY_RSA; /* cheat for key_to_blob */
1666 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001667 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001668 buffer_put_string(m, blob, blen);
1669
1670 /* Save temporarily for comparison in verify */
1671 key_blob = blob;
1672 key_bloblen = blen;
1673 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 }
Damien Miller00111382003-03-10 11:21:17 +11001675 if (key != NULL)
1676 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001678 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679
1680 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1681 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1682 return (0);
1683}
1684
1685int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001686mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687{
1688 Key *key = NULL;
1689 u_char *blob;
1690 u_int blen;
1691
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001692 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693
1694 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001695 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 blob = buffer_get_string(m, &blen);
1697 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001698 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001700 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001702 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001703 if (key->type != KEY_RSA)
1704 fatal("%s: received bad key type %d", __func__, key->type);
1705 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706 if (ssh1_challenge)
1707 BN_clear_free(ssh1_challenge);
1708 ssh1_challenge = auth_rsa_generate_challenge(key);
1709
1710 buffer_clear(m);
1711 buffer_put_bignum2(m, ssh1_challenge);
1712
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001714 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715
1716 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001717
Darren Tuckera627d422013-06-02 07:31:17 +10001718 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001719 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720 return (0);
1721}
1722
1723int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001724mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725{
1726 Key *key = NULL;
1727 u_char *blob, *response;
1728 u_int blen, len;
1729 int success;
1730
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001731 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732
1733 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001734 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001736 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737
1738 blob = buffer_get_string(m, &blen);
1739 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001740 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001742 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001744 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745 response = buffer_get_string(m, &len);
1746 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001748 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1749
Darren Tuckera627d422013-06-02 07:31:17 +10001750 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001751 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001752 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001753
1754 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1755
1756 /* reset state */
1757 BN_clear_free(ssh1_challenge);
1758 ssh1_challenge = NULL;
1759 monitor_reset_key_state();
1760
1761 buffer_clear(m);
1762 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001763 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764
1765 return (success);
1766}
Damien Miller1f0311c2014-05-15 14:24:09 +10001767#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001768
1769int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001770mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001772 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773 int res, status;
1774
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001775 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776
1777 /* The child is terminating */
1778 session_destroy_all(&mm_session_close);
1779
Darren Tucker52358d62008-03-11 22:58:25 +11001780#ifdef USE_PAM
1781 if (options.use_pam)
1782 sshpam_cleanup();
1783#endif
1784
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001785 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001786 if (errno != EINTR)
1787 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001788
1789 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1790
1791 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001792 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001793}
1794
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001795#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001796/* Report that an audit event occurred */
1797int
1798mm_answer_audit_event(int socket, Buffer *m)
1799{
1800 ssh_audit_event_t event;
1801
1802 debug3("%s entering", __func__);
1803
1804 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001805 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001806 case SSH_AUTH_FAIL_PUBKEY:
1807 case SSH_AUTH_FAIL_HOSTBASED:
1808 case SSH_AUTH_FAIL_GSSAPI:
1809 case SSH_LOGIN_EXCEED_MAXTRIES:
1810 case SSH_LOGIN_ROOT_DENIED:
1811 case SSH_CONNECTION_CLOSE:
1812 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001813 audit_event(event);
1814 break;
1815 default:
1816 fatal("Audit event type %d not permitted", event);
1817 }
1818
1819 return (0);
1820}
1821
1822int
1823mm_answer_audit_command(int socket, Buffer *m)
1824{
1825 u_int len;
1826 char *cmd;
1827
1828 debug3("%s entering", __func__);
1829 cmd = buffer_get_string(m, &len);
1830 /* sanity check command, if so how? */
1831 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001832 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001833 return (0);
1834}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001835#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001836
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001837void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001838monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001839{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001840 struct ssh *ssh = active_state; /* XXX */
1841 struct kex *kex;
1842 int r;
1843
1844 debug3("%s: packet_set_state", __func__);
1845 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1846 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1847 sshbuf_free(child_state);
1848 child_state = NULL;
1849
1850 if ((kex = ssh->kex) != 0) {
1851 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001852#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001853 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1854 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1855 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1856 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001857# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001858 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001859# endif
Damien Miller773dda22015-01-30 23:10:17 +11001860#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001861 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1862 kex->load_host_public_key=&get_hostkey_public_by_type;
1863 kex->load_host_private_key=&get_hostkey_private_by_type;
1864 kex->host_key_index=&get_hostkey_index;
1865 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001866 }
1867
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001868 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001869 if (options.compression) {
1870 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1871 (ssh_packet_comp_alloc_func *)mm_zalloc,
1872 (ssh_packet_comp_free_func *)mm_zfree);
1873 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001874}
1875
1876/* This function requries careful sanity checking */
1877
1878void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001879mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001880{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001881 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001882
markus@openbsd.org091c3022015-01-19 19:52:16 +00001883 if ((child_state = sshbuf_new()) == NULL)
1884 fatal("%s: sshbuf_new failed", __func__);
1885 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1886 child_state);
1887 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001888}
1889
1890
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001891/* XXX */
1892
1893#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001894 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895 fatal("fcntl(%d, F_SETFD)", x); \
1896} while (0)
1897
1898static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001899monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001900{
Damien Miller8f0bf232011-06-20 14:42:23 +10001901 int pair[2];
1902
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001903 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001904 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905 FD_CLOSEONEXEC(pair[0]);
1906 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001907 mon->m_recvfd = pair[0];
1908 mon->m_sendfd = pair[1];
1909
1910 if (do_logfds) {
1911 if (pipe(pair) == -1)
1912 fatal("%s: pipe: %s", __func__, strerror(errno));
1913 FD_CLOSEONEXEC(pair[0]);
1914 FD_CLOSEONEXEC(pair[1]);
1915 mon->m_log_recvfd = pair[0];
1916 mon->m_log_sendfd = pair[1];
1917 } else
1918 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001919}
1920
1921#define MM_MEMSIZE 65536
1922
1923struct monitor *
1924monitor_init(void)
1925{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001926 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001927 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001928
Damien Miller07d86be2006-03-26 14:19:21 +11001929 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930
Damien Miller8f0bf232011-06-20 14:42:23 +10001931 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932
1933 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001934 if (options.compression) {
1935 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1936 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937
Damien Miller2d6b8332002-06-21 15:59:49 +10001938 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001939 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1940 (ssh_packet_comp_alloc_func *)mm_zalloc,
1941 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001942 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943
1944 return mon;
1945}
1946
1947void
1948monitor_reinit(struct monitor *mon)
1949{
Damien Miller8f0bf232011-06-20 14:42:23 +10001950 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001951}
Darren Tucker0efd1552003-08-26 11:49:55 +10001952
1953#ifdef GSSAPI
1954int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001955mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001956{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001957 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001958 OM_uint32 major;
1959 u_int len;
1960
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001961 goid.elements = buffer_get_string(m, &len);
1962 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001963
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001964 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001965
Darren Tuckera627d422013-06-02 07:31:17 +10001966 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001967
1968 buffer_clear(m);
1969 buffer_put_int(m, major);
1970
Damien Miller6fd6def2005-11-05 15:07:05 +11001971 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001972
1973 /* Now we have a context, enable the step */
1974 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1975
1976 return (0);
1977}
1978
1979int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001980mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001981{
1982 gss_buffer_desc in;
1983 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001984 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001985 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001986 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001987
Darren Tucker600ad8d2003-08-26 12:10:48 +10001988 in.value = buffer_get_string(m, &len);
1989 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001990 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001991 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001992
1993 buffer_clear(m);
1994 buffer_put_int(m, major);
1995 buffer_put_string(m, out.value, out.length);
1996 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001997 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001998
1999 gss_release_buffer(&minor, &out);
2000
Damien Miller6fd6def2005-11-05 15:07:05 +11002001 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002002 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2003 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002004 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002005 }
2006 return (0);
2007}
2008
2009int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002010mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002011{
2012 gss_buffer_desc gssbuf, mic;
2013 OM_uint32 ret;
2014 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002015
Damien Miller0425d402003-11-17 22:18:21 +11002016 gssbuf.value = buffer_get_string(m, &len);
2017 gssbuf.length = len;
2018 mic.value = buffer_get_string(m, &len);
2019 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002020
Damien Miller0425d402003-11-17 22:18:21 +11002021 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002022
Darren Tuckera627d422013-06-02 07:31:17 +10002023 free(gssbuf.value);
2024 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002025
Damien Miller0425d402003-11-17 22:18:21 +11002026 buffer_clear(m);
2027 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002028
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002029 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002030
Damien Miller0425d402003-11-17 22:18:21 +11002031 if (!GSS_ERROR(ret))
2032 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002033
Damien Miller0425d402003-11-17 22:18:21 +11002034 return (0);
2035}
2036
2037int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002038mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002039{
2040 int authenticated;
2041
2042 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2043
2044 buffer_clear(m);
2045 buffer_put_int(m, authenticated);
2046
2047 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002048 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002049
Damien Miller6fd6def2005-11-05 15:07:05 +11002050 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002051
2052 /* Monitor loop will terminate if authenticated */
2053 return (authenticated);
2054}
2055#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002056