blob: b4109657efdf546b119f9a82693e66f4e557e7d5 [file] [log] [blame]
djm@openbsd.org94885382015-06-22 23:42:16 +00001/* $OpenBSD: monitor.c,v 1.150 2015/06/22 23:42:16 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{
490 size_t len = (size_t) size * ncount;
491 void *address;
492
millert@openbsd.orgfd368342015-02-06 23:21:59 +0000493 if (len == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000494 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
495
496 address = mm_malloc(mm, len);
497
498 return (address);
499}
500
501static void
502mm_zfree(struct mm_master *mm, void *address)
503{
504 mm_free(mm, address);
505}
506
Damien Miller8f0bf232011-06-20 14:42:23 +1000507static int
508monitor_read_log(struct monitor *pmonitor)
509{
510 Buffer logmsg;
511 u_int len, level;
512 char *msg;
513
514 buffer_init(&logmsg);
515
516 /* Read length */
517 buffer_append_space(&logmsg, 4);
518 if (atomicio(read, pmonitor->m_log_recvfd,
519 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
520 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100521 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000522 debug("%s: child log fd closed", __func__);
523 close(pmonitor->m_log_recvfd);
524 pmonitor->m_log_recvfd = -1;
525 return -1;
526 }
527 fatal("%s: log fd read: %s", __func__, strerror(errno));
528 }
529 len = buffer_get_int(&logmsg);
530 if (len <= 4 || len > 8192)
531 fatal("%s: invalid log message length %u", __func__, len);
532
533 /* Read severity, message */
534 buffer_clear(&logmsg);
535 buffer_append_space(&logmsg, len);
536 if (atomicio(read, pmonitor->m_log_recvfd,
537 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
538 fatal("%s: log fd read: %s", __func__, strerror(errno));
539
540 /* Log it */
541 level = buffer_get_int(&logmsg);
542 msg = buffer_get_string(&logmsg, NULL);
543 if (log_level_name(level) == NULL)
544 fatal("%s: invalid log level %u (corrupted message?)",
545 __func__, level);
546 do_log2(level, "%s [preauth]", msg);
547
548 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000549 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000550
551 return 0;
552}
553
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000555monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556 struct mon_table **pent)
557{
558 Buffer m;
559 int ret;
560 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000561 struct pollfd pfd[2];
562
563 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100564 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000565 pfd[0].fd = pmonitor->m_sendfd;
566 pfd[0].events = POLLIN;
567 pfd[1].fd = pmonitor->m_log_recvfd;
568 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000569 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
570 if (errno == EINTR || errno == EAGAIN)
571 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000572 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000573 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000574 if (pfd[1].revents) {
575 /*
576 * Drain all log messages before processing next
577 * monitor request.
578 */
579 monitor_read_log(pmonitor);
580 continue;
581 }
582 if (pfd[0].revents)
583 break; /* Continues below */
584 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
586 buffer_init(&m);
587
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000588 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 type = buffer_get_char(&m);
590
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000591 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 while (ent->f != NULL) {
594 if (ent->type == type)
595 break;
596 ent++;
597 }
598
599 if (ent->f != NULL) {
600 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000601 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000603 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 buffer_free(&m);
605
606 /* The child may use this request only once, disable it */
607 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000608 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 type);
610 ent->flags &= ~MON_PERMIT;
611 }
612
613 if (pent != NULL)
614 *pent = ent;
615
616 return ret;
617 }
618
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000619 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 /* NOTREACHED */
622 return (-1);
623}
624
625/* allowed key state */
626static int
627monitor_allowed_key(u_char *blob, u_int bloblen)
628{
629 /* make sure key is allowed */
630 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000631 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 return (0);
633 return (1);
634}
635
636static void
637monitor_reset_key_state(void)
638{
639 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(key_blob);
641 free(hostbased_cuser);
642 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643 key_blob = NULL;
644 key_bloblen = 0;
645 key_blobtype = MM_NOKEY;
646 hostbased_cuser = NULL;
647 hostbased_chost = NULL;
648}
649
Damien Miller1f0311c2014-05-15 14:24:09 +1000650#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000652mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653{
654 DH *dh;
655 int min, want, max;
656
657 min = buffer_get_int(m);
658 want = buffer_get_int(m);
659 max = buffer_get_int(m);
660
661 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 /* We need to check here, too, in case the child got corrupted */
664 if (max < min || want < min || max < want)
665 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000666 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667
668 buffer_clear(m);
669
670 dh = choose_dh(min, want, max);
671 if (dh == NULL) {
672 buffer_put_char(m, 0);
673 return (0);
674 } else {
675 /* Send first bignum */
676 buffer_put_char(m, 1);
677 buffer_put_bignum2(m, dh->p);
678 buffer_put_bignum2(m, dh->g);
679
680 DH_free(dh);
681 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000682 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683 return (0);
684}
Damien Miller1f0311c2014-05-15 14:24:09 +1000685#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
687int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000688mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000690 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000691 extern int auth_sock; /* XXX move to state struct? */
692 struct sshkey *key;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000693 struct sshbuf *sigbuf;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694 u_char *p;
695 u_char *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000696 size_t datlen, siglen;
djm@openbsd.org523463a2015-02-16 22:13:32 +0000697 int r, keyid, is_proof = 0;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000698 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000700 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701
djm@openbsd.org141efe42015-01-14 20:05:27 +0000702 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
703 (r = sshbuf_get_string(m, &p, &datlen)) != 0)
704 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
Damien Millera63128d2006-03-15 12:08:28 +1100706 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000707 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
708 * SHA384 (48 bytes) and SHA512 (64 bytes).
djm@openbsd.org523463a2015-02-16 22:13:32 +0000709 *
710 * Otherwise, verify the signature request is for a hostkey
711 * proof.
712 *
713 * XXX perform similar check for KEX signature requests too?
714 * it's not trivial, since what is signed is the hash, rather
715 * than the full kex structure...
Damien Millera63128d2006-03-15 12:08:28 +1100716 */
djm@openbsd.org523463a2015-02-16 22:13:32 +0000717 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
718 /*
719 * Construct expected hostkey proof and compare it to what
720 * the client sent us.
721 */
722 if (session_id2_len == 0) /* hostkeys is never first */
723 fatal("%s: bad data length: %zu", __func__, datlen);
724 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
725 fatal("%s: no hostkey for index %d", __func__, keyid);
726 if ((sigbuf = sshbuf_new()) == NULL)
727 fatal("%s: sshbuf_new", __func__);
djm@openbsd.org44732de2015-02-20 22:17:21 +0000728 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
729 (r = sshbuf_put_string(sigbuf, session_id2,
djm@openbsd.org523463a2015-02-16 22:13:32 +0000730 session_id2_len) != 0) ||
djm@openbsd.org523463a2015-02-16 22:13:32 +0000731 (r = sshkey_puts(key, sigbuf)) != 0)
732 fatal("%s: couldn't prepare private key "
733 "proof buffer: %s", __func__, ssh_err(r));
734 if (datlen != sshbuf_len(sigbuf) ||
735 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
736 fatal("%s: bad data length: %zu, hostkey proof len %zu",
737 __func__, datlen, sshbuf_len(sigbuf));
738 sshbuf_free(sigbuf);
739 is_proof = 1;
740 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
Ben Lindstromf67e0772002-06-06 20:58:19 +0000742 /* save session id, it will be passed on the first call */
743 if (session_id2_len == 0) {
744 session_id2_len = datlen;
745 session_id2 = xmalloc(session_id2_len);
746 memcpy(session_id2, p, session_id2_len);
747 }
748
Damien Miller85b45e02013-07-20 13:21:52 +1000749 if ((key = get_hostkey_by_index(keyid)) != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000750 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen,
751 datafellows)) != 0)
752 fatal("%s: sshkey_sign failed: %s",
753 __func__, ssh_err(r));
djm@openbsd.org523463a2015-02-16 22:13:32 +0000754 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000755 auth_sock > 0) {
756 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
757 p, datlen, datafellows)) != 0) {
758 fatal("%s: ssh_agent_sign failed: %s",
759 __func__, ssh_err(r));
760 }
Damien Miller85b45e02013-07-20 13:21:52 +1000761 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000762 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763
djm@openbsd.org523463a2015-02-16 22:13:32 +0000764 debug3("%s: %s signature %p(%zu)", __func__,
765 is_proof ? "KEX" : "hostkey proof", signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766
djm@openbsd.org141efe42015-01-14 20:05:27 +0000767 sshbuf_reset(m);
768 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
769 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770
Darren Tuckera627d422013-06-02 07:31:17 +1000771 free(p);
772 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000774 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775
776 /* Turn on permissions for getpwnam */
777 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
778
779 return (0);
780}
781
782/* Retrieves the password entry and also checks if the user is permitted */
783
784int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000785mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000787 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788 struct passwd *pwent;
789 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000790 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000792 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793
794 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000795 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796
Darren Tuckerb09b6772004-06-22 15:06:46 +1000797 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798
Darren Tuckerb09b6772004-06-22 15:06:46 +1000799 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800
Darren Tuckerb09b6772004-06-22 15:06:46 +1000801 authctxt->user = xstrdup(username);
802 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000803 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000804
805 buffer_clear(m);
806
807 if (pwent == NULL) {
808 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100809 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810 goto out;
811 }
812
813 allowed = 1;
814 authctxt->pw = pwent;
815 authctxt->valid = 1;
816
817 buffer_put_char(m, 1);
818 buffer_put_string(m, pwent, sizeof(struct passwd));
819 buffer_put_cstring(m, pwent->pw_name);
820 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000821#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000822 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000823#endif
824#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000826#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827 buffer_put_cstring(m, pwent->pw_dir);
828 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100829
830 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100831 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000832
833#define M_CP_STROPT(x) do { \
834 if (options.x != NULL) \
835 buffer_put_cstring(m, options.x); \
836 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000837#define M_CP_STRARRAYOPT(x, nx) do { \
838 for (i = 0; i < options.nx; i++) \
839 buffer_put_cstring(m, options.x[i]); \
840 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000841 /* See comment in servconf.h */
842 COPY_MATCH_STRING_OPTS();
843#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000844#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100845
846 /* Create valid auth method lists */
847 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
848 /*
849 * The monitor will continue long enough to let the child
850 * run to it's packet_disconnect(), but it must not allow any
851 * authentication to succeed.
852 */
853 debug("%s: no valid authentication method lists", __func__);
854 }
855
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000856 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000857 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858
859 /* For SSHv1 allow authentication now */
860 if (!compat20)
861 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000862 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863 /* Allow service/style information on the auth context */
864 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000865 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
866 }
Damien Millera33501b2002-05-08 12:24:42 +1000867#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000868 if (options.use_pam)
869 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000870#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871
872 return (0);
873}
874
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000875int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000876{
877 char *banner;
878
879 buffer_clear(m);
880 banner = auth2_read_banner();
881 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000882 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000883 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000884
885 return (0);
886}
887
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000889mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890{
891 monitor_permit_authentications(1);
892
893 authctxt->service = buffer_get_string(m, NULL);
894 authctxt->style = buffer_get_string(m, NULL);
895 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000896 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
898 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000899 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900 authctxt->style = NULL;
901 }
902
903 return (0);
904}
905
906int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000907mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908{
909 static int call_count;
910 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000911 int authenticated;
912 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913
914 passwd = buffer_get_string(m, &plen);
915 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000916 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000917 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100918 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000919 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920
921 buffer_clear(m);
922 buffer_put_int(m, authenticated);
923
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000924 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 call_count++;
928 if (plen == 0 && call_count == 1)
929 auth_method = "none";
930 else
931 auth_method = "password";
932
933 /* Causes monitor loop to terminate if authenticated */
934 return (authenticated);
935}
936
937#ifdef BSD_AUTH
938int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000939mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940{
941 char *name, *infotxt;
942 u_int numprompts;
943 u_int *echo_on;
944 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100945 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
Damien Millerb7df3af2003-02-24 11:55:46 +1100947 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
948 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949
950 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100951 buffer_put_int(m, success);
952 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 buffer_put_cstring(m, prompts[0]);
954
Damien Millerb7df3af2003-02-24 11:55:46 +1100955 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000956 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957
Damien Millerb7df3af2003-02-24 11:55:46 +1100958 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000959 free(name);
960 free(infotxt);
961 free(prompts);
962 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963 }
964
965 return (0);
966}
967
968int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000969mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970{
971 char *response;
972 int authok;
973
974 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000975 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
977 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000978 authok = options.challenge_response_authentication &&
979 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000981 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000982 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983
984 buffer_clear(m);
985 buffer_put_int(m, authok);
986
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000987 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000988 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989
Damien Miller91a55f22013-04-23 15:18:10 +1000990 if (compat20) {
991 auth_method = "keyboard-interactive";
992 auth_submethod = "bsdauth";
993 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100994 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995
996 return (authok != 0);
997}
998#endif
999
1000#ifdef SKEY
1001int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001002mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003{
1004 struct skey skey;
1005 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +11001006 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007
Darren Tucker06a8cfe2004-04-14 17:24:30 +10001008 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1009 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010
1011 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001012 buffer_put_int(m, success);
1013 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014 buffer_put_cstring(m, challenge);
1015
Damien Millerb7df3af2003-02-24 11:55:46 +11001016 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001017 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018
1019 return (0);
1020}
1021
1022int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001023mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024{
1025 char *response;
1026 int authok;
1027
1028 response = buffer_get_string(m, NULL);
1029
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001030 authok = (options.challenge_response_authentication &&
1031 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032 skey_haskey(authctxt->pw->pw_name) == 0 &&
1033 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1034
Darren Tuckerf60845f2013-06-02 08:07:31 +10001035 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036
1037 buffer_clear(m);
1038 buffer_put_int(m, authok);
1039
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001040 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001041 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001042
1043 auth_method = "skey";
1044
1045 return (authok != 0);
1046}
1047#endif
1048
Damien Miller79418552002-04-23 20:28:48 +10001049#ifdef USE_PAM
1050int
Darren Tucker7eda5922004-06-22 13:22:50 +10001051mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001052{
Damien Miller4e448a32003-05-14 15:11:48 +10001053 if (!options.use_pam)
1054 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1055
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001056 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001057
Damien Miller1f499fd2003-08-25 13:08:49 +10001058 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1059
Damien Miller79418552002-04-23 20:28:48 +10001060 return (0);
1061}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062
Damien Miller1f499fd2003-08-25 13:08:49 +10001063int
Darren Tucker7eda5922004-06-22 13:22:50 +10001064mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001065{
1066 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001067
Damien Miller1f499fd2003-08-25 13:08:49 +10001068 if (!options.use_pam)
1069 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1070
1071 ret = do_pam_account();
1072
1073 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001074 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001075
Darren Tucker7eda5922004-06-22 13:22:50 +10001076 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001077
1078 return (ret);
1079}
1080
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081static void *sshpam_ctxt, *sshpam_authok;
1082extern KbdintDevice sshpam_device;
1083
1084int
Darren Tucker7eda5922004-06-22 13:22:50 +10001085mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086{
1087
1088 debug3("%s", __func__);
1089 authctxt->user = buffer_get_string(m, NULL);
1090 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1091 sshpam_authok = NULL;
1092 buffer_clear(m);
1093 if (sshpam_ctxt != NULL) {
1094 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1095 buffer_put_int(m, 1);
1096 } else {
1097 buffer_put_int(m, 0);
1098 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001099 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001100 return (0);
1101}
1102
1103int
Darren Tucker7eda5922004-06-22 13:22:50 +10001104mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001105{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001106 char *name = NULL, *info = NULL, **prompts = NULL;
1107 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001108 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001109
1110 debug3("%s", __func__);
1111 sshpam_authok = NULL;
1112 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1113 if (ret == 0 && num == 0)
1114 sshpam_authok = sshpam_ctxt;
1115 if (num > 1 || name == NULL || info == NULL)
1116 ret = -1;
1117 buffer_clear(m);
1118 buffer_put_int(m, ret);
1119 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001120 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001121 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001122 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001123 buffer_put_int(m, num);
1124 for (i = 0; i < num; ++i) {
1125 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001126 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001127 buffer_put_int(m, echo_on[i]);
1128 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001129 free(prompts);
1130 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001131 auth_method = "keyboard-interactive";
1132 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001133 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001134 return (0);
1135}
1136
1137int
Darren Tucker7eda5922004-06-22 13:22:50 +10001138mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001139{
1140 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001141 u_int i, num;
1142 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001143
1144 debug3("%s", __func__);
1145 sshpam_authok = NULL;
1146 num = buffer_get_int(m);
1147 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001148 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001149 for (i = 0; i < num; ++i)
1150 resp[i] = buffer_get_string(m, NULL);
1151 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1152 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001153 free(resp[i]);
1154 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001155 } else {
1156 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1157 }
1158 buffer_clear(m);
1159 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001160 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001161 auth_method = "keyboard-interactive";
1162 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001163 if (ret == 0)
1164 sshpam_authok = sshpam_ctxt;
1165 return (0);
1166}
1167
1168int
Darren Tucker7eda5922004-06-22 13:22:50 +10001169mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001170{
1171
1172 debug3("%s", __func__);
1173 (sshpam_device.free_ctx)(sshpam_ctxt);
1174 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001175 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001176 auth_method = "keyboard-interactive";
1177 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001178 return (sshpam_authok == sshpam_ctxt);
1179}
Damien Miller79418552002-04-23 20:28:48 +10001180#endif
1181
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001183mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184{
1185 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001186 char *cuser, *chost;
1187 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001188 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189 enum mm_keytype type = 0;
1190 int allowed = 0;
1191
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001192 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193
1194 type = buffer_get_int(m);
1195 cuser = buffer_get_string(m, NULL);
1196 chost = buffer_get_string(m, NULL);
1197 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001198 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199
1200 key = key_from_blob(blob, bloblen);
1201
1202 if ((compat20 && type == MM_RSAHOSTKEY) ||
1203 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001204 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001205
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001206 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
Damien Miller3e3b5142003-11-17 21:13:40 +11001208 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001209 /* These should not make it past the privsep child */
1210 if (key_type_plain(key->type) == KEY_RSA &&
1211 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1212 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1213
Darren Tucker47eede72005-03-14 23:08:12 +11001214 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001216 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001217 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001218 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001219 options.pubkey_key_types, 0) == 1 &&
1220 user_key_allowed(authctxt->pw, key,
1221 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001222 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001223 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001224 if (options.pubkey_authentication &&
1225 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001226 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227 break;
1228 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001229 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001230 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001231 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001232 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001234 pubkey_auth_info(authctxt, key,
1235 "client user \"%.100s\", client host \"%.100s\"",
1236 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001237 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001238 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001239#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001240 case MM_RSAHOSTKEY:
1241 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001242 allowed = options.rhosts_rsa_authentication &&
1243 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001245 if (options.rhosts_rsa_authentication && allowed != 1)
1246 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001247 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001249#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001251 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 break;
1253 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 }
Damien Miller00111382003-03-10 11:21:17 +11001255 if (key != NULL)
1256 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001257
1258 /* clear temporarily storage (used by verify) */
1259 monitor_reset_key_state();
1260
1261 if (allowed) {
1262 /* Save temporarily for comparison in verify */
1263 key_blob = blob;
1264 key_bloblen = bloblen;
1265 key_blobtype = type;
1266 hostbased_cuser = cuser;
1267 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001268 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001269 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001270 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001271 free(blob);
1272 free(cuser);
1273 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001274 }
1275
1276 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001277 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278
1279 buffer_clear(m);
1280 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001281 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001283 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284
1285 if (type == MM_RSAHOSTKEY)
1286 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1287
1288 return (0);
1289}
1290
1291static int
1292monitor_valid_userblob(u_char *data, u_int datalen)
1293{
1294 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001295 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296 u_int len;
1297 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298
1299 buffer_init(&b);
1300 buffer_append(&b, data, datalen);
1301
1302 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001303 p = buffer_ptr(&b);
1304 len = buffer_len(&b);
1305 if ((session_id2 == NULL) ||
1306 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001307 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 buffer_consume(&b, session_id2_len);
1310 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001311 p = buffer_get_string(&b, &len);
1312 if ((session_id2 == NULL) ||
1313 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001314 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001316 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 }
1318 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1319 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001320 p = buffer_get_cstring(&b, NULL);
1321 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1322 authctxt->style ? ":" : "",
1323 authctxt->style ? authctxt->style : "");
1324 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001325 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001326 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001327 fail++;
1328 }
Darren Tuckera627d422013-06-02 07:31:17 +10001329 free(userstyle);
1330 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331 buffer_skip_string(&b);
1332 if (datafellows & SSH_BUG_PKAUTH) {
1333 if (!buffer_get_char(&b))
1334 fail++;
1335 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001336 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 if (strcmp("publickey", p) != 0)
1338 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001339 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340 if (!buffer_get_char(&b))
1341 fail++;
1342 buffer_skip_string(&b);
1343 }
1344 buffer_skip_string(&b);
1345 if (buffer_len(&b) != 0)
1346 fail++;
1347 buffer_free(&b);
1348 return (fail == 0);
1349}
1350
1351static int
Damien Millera10f5612002-09-12 09:49:15 +10001352monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1353 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354{
1355 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001356 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357 u_int len;
1358 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
1360 buffer_init(&b);
1361 buffer_append(&b, data, datalen);
1362
Ben Lindstromf67e0772002-06-06 20:58:19 +00001363 p = buffer_get_string(&b, &len);
1364 if ((session_id2 == NULL) ||
1365 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001366 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001368 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001369
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1371 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001372 p = buffer_get_cstring(&b, NULL);
1373 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1374 authctxt->style ? ":" : "",
1375 authctxt->style ? authctxt->style : "");
1376 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001377 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001378 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001379 fail++;
1380 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001381 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001382 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001384 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385 if (strcmp(p, "hostbased") != 0)
1386 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001387 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388 buffer_skip_string(&b); /* pkalg */
1389 buffer_skip_string(&b); /* pkblob */
1390
1391 /* verify client host, strip trailing dot if necessary */
1392 p = buffer_get_string(&b, NULL);
1393 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1394 p[len - 1] = '\0';
1395 if (strcmp(p, chost) != 0)
1396 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001397 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398
1399 /* verify client user */
1400 p = buffer_get_string(&b, NULL);
1401 if (strcmp(p, cuser) != 0)
1402 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001403 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404
1405 if (buffer_len(&b) != 0)
1406 fail++;
1407 buffer_free(&b);
1408 return (fail == 0);
1409}
1410
1411int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001412mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413{
1414 Key *key;
1415 u_char *signature, *data, *blob;
1416 u_int signaturelen, datalen, bloblen;
1417 int verified = 0;
1418 int valid_data = 0;
1419
1420 blob = buffer_get_string(m, &bloblen);
1421 signature = buffer_get_string(m, &signaturelen);
1422 data = buffer_get_string(m, &datalen);
1423
1424 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001425 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001426 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427
1428 key = key_from_blob(blob, bloblen);
1429 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001430 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431
1432 switch (key_blobtype) {
1433 case MM_USERKEY:
1434 valid_data = monitor_valid_userblob(data, datalen);
1435 break;
1436 case MM_HOSTKEY:
1437 valid_data = monitor_valid_hostbasedblob(data, datalen,
1438 hostbased_cuser, hostbased_chost);
1439 break;
1440 default:
1441 valid_data = 0;
1442 break;
1443 }
1444 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001445 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446
1447 verified = key_verify(key, signature, signaturelen, data, datalen);
1448 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001449 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001450
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001451 /* If auth was successful then record key to ensure it isn't reused */
1452 if (verified == 1)
1453 auth2_record_userkey(authctxt, key);
1454 else
1455 key_free(key);
1456
Darren Tuckera627d422013-06-02 07:31:17 +10001457 free(blob);
1458 free(signature);
1459 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460
Ben Lindstrome1c09122002-06-23 00:38:24 +00001461 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1462
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 monitor_reset_key_state();
1464
1465 buffer_clear(m);
1466 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001467 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468
Darren Tuckerfbba7352006-11-07 23:16:08 +11001469 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470}
1471
1472static void
1473mm_record_login(Session *s, struct passwd *pw)
1474{
1475 socklen_t fromlen;
1476 struct sockaddr_storage from;
1477
djm@openbsd.org17d4d9d2015-04-17 04:32:31 +00001478 if (options.use_login)
1479 return;
1480
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 /*
1482 * Get IP address of client. If the connection is not a socket, let
1483 * the address be 0.0.0.0.
1484 */
1485 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001486 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001488 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001489 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001491 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 }
1493 }
1494 /* Record that there was a login on that tty from the remote host. */
1495 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001496 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001497 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498}
1499
1500static void
1501mm_session_close(Session *s)
1502{
Damien Miller04bd8b02003-05-25 14:38:33 +10001503 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001505 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506 session_pty_cleanup2(s);
1507 }
Damien Miller7207f642008-05-19 15:34:50 +10001508 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509}
1510
1511int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001512mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001514 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515 Session *s;
1516 int res, fd0;
1517
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001518 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519
1520 buffer_clear(m);
1521 s = session_new();
1522 if (s == NULL)
1523 goto error;
1524 s->authctxt = authctxt;
1525 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001526 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1528 if (res == 0)
1529 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530 pty_setowner(authctxt->pw, s->tty);
1531
1532 buffer_put_int(m, 1);
1533 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534
1535 /* We need to trick ttyslot */
1536 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001537 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538
1539 mm_record_login(s, authctxt->pw);
1540
1541 /* Now we can close the file descriptor again */
1542 close(0);
1543
Darren Tucker09991742004-07-17 17:05:14 +10001544 /* send messages generated by record_login */
1545 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1546 buffer_clear(&loginmsg);
1547
1548 mm_request_send(sock, MONITOR_ANS_PTY, m);
1549
Damien Miller54fd7cf2007-09-17 12:04:08 +10001550 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1551 mm_send_fd(sock, s->ttyfd) == -1)
1552 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001553
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554 /* make sure nothing uses fd 0 */
1555 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001556 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001557 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001558 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559
1560 /* slave is not needed */
1561 close(s->ttyfd);
1562 s->ttyfd = s->ptyfd;
1563 /* no need to dup() because nobody closes ptyfd */
1564 s->ptymaster = s->ptyfd;
1565
Damien Miller9ab00b42006-08-05 12:40:11 +10001566 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567
1568 return (0);
1569
1570 error:
1571 if (s != NULL)
1572 mm_session_close(s);
1573 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001574 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 return (0);
1576}
1577
1578int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001579mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001580{
1581 Session *s;
1582 char *tty;
1583
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001584 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585
1586 tty = buffer_get_string(m, NULL);
1587 if ((s = session_by_tty(tty)) != NULL)
1588 mm_session_close(s);
1589 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001590 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591 return (0);
1592}
1593
Damien Miller1f0311c2014-05-15 14:24:09 +10001594#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001596mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597{
1598 BIGNUM *p;
1599 int rsafail;
1600
1601 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001602 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603
1604 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001605 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001606
1607 buffer_get_bignum2(m, p);
1608
1609 rsafail = ssh1_session_key(p);
1610
1611 buffer_clear(m);
1612 buffer_put_int(m, rsafail);
1613 buffer_put_bignum2(m, p);
1614
1615 BN_clear_free(p);
1616
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001617 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618
1619 /* Turn on permissions for sessid passing */
1620 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1621
1622 return (0);
1623}
1624
1625int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001626mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627{
1628 int i;
1629
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001630 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001633 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001634 for (i = 0; i < 16; i++)
1635 session_id[i] = buffer_get_char(m);
1636
1637 /* Turn on permissions for getpwnam */
1638 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1639
1640 return (0);
1641}
1642
1643int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001644mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645{
1646 BIGNUM *client_n;
1647 Key *key = NULL;
1648 u_char *blob = NULL;
1649 u_int blen = 0;
1650 int allowed = 0;
1651
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001652 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653
Damien Miller7a8f5b32006-03-31 23:14:23 +11001654 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001655 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001657 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001658 buffer_get_bignum2(m, client_n);
1659 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1660 BN_clear_free(client_n);
1661 }
1662 buffer_clear(m);
1663 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001664 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665
1666 /* clear temporarily storage (used by generate challenge) */
1667 monitor_reset_key_state();
1668
1669 if (allowed && key != NULL) {
1670 key->type = KEY_RSA; /* cheat for key_to_blob */
1671 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001672 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673 buffer_put_string(m, blob, blen);
1674
1675 /* Save temporarily for comparison in verify */
1676 key_blob = blob;
1677 key_bloblen = blen;
1678 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679 }
Damien Miller00111382003-03-10 11:21:17 +11001680 if (key != NULL)
1681 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001683 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684
1685 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1686 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1687 return (0);
1688}
1689
1690int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001691mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692{
1693 Key *key = NULL;
1694 u_char *blob;
1695 u_int blen;
1696
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001697 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698
1699 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001700 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701 blob = buffer_get_string(m, &blen);
1702 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001703 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001705 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001707 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001708 if (key->type != KEY_RSA)
1709 fatal("%s: received bad key type %d", __func__, key->type);
1710 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711 if (ssh1_challenge)
1712 BN_clear_free(ssh1_challenge);
1713 ssh1_challenge = auth_rsa_generate_challenge(key);
1714
1715 buffer_clear(m);
1716 buffer_put_bignum2(m, ssh1_challenge);
1717
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001718 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001719 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720
1721 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001722
Darren Tuckera627d422013-06-02 07:31:17 +10001723 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001724 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725 return (0);
1726}
1727
1728int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001729mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001730{
1731 Key *key = NULL;
1732 u_char *blob, *response;
1733 u_int blen, len;
1734 int success;
1735
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001736 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737
1738 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001739 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001741 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001742
1743 blob = buffer_get_string(m, &blen);
1744 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001745 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001748 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001749 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001750 response = buffer_get_string(m, &len);
1751 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001752 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001753 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1754
Darren Tuckera627d422013-06-02 07:31:17 +10001755 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001757 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758
1759 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1760
1761 /* reset state */
1762 BN_clear_free(ssh1_challenge);
1763 ssh1_challenge = NULL;
1764 monitor_reset_key_state();
1765
1766 buffer_clear(m);
1767 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001768 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001769
1770 return (success);
1771}
Damien Miller1f0311c2014-05-15 14:24:09 +10001772#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773
1774int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001775mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001777 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001778 int res, status;
1779
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001780 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781
1782 /* The child is terminating */
1783 session_destroy_all(&mm_session_close);
1784
Darren Tucker52358d62008-03-11 22:58:25 +11001785#ifdef USE_PAM
1786 if (options.use_pam)
1787 sshpam_cleanup();
1788#endif
1789
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001790 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001791 if (errno != EINTR)
1792 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001793
1794 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1795
1796 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001797 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001798}
1799
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001800#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001801/* Report that an audit event occurred */
1802int
1803mm_answer_audit_event(int socket, Buffer *m)
1804{
1805 ssh_audit_event_t event;
1806
1807 debug3("%s entering", __func__);
1808
1809 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001810 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001811 case SSH_AUTH_FAIL_PUBKEY:
1812 case SSH_AUTH_FAIL_HOSTBASED:
1813 case SSH_AUTH_FAIL_GSSAPI:
1814 case SSH_LOGIN_EXCEED_MAXTRIES:
1815 case SSH_LOGIN_ROOT_DENIED:
1816 case SSH_CONNECTION_CLOSE:
1817 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001818 audit_event(event);
1819 break;
1820 default:
1821 fatal("Audit event type %d not permitted", event);
1822 }
1823
1824 return (0);
1825}
1826
1827int
1828mm_answer_audit_command(int socket, Buffer *m)
1829{
1830 u_int len;
1831 char *cmd;
1832
1833 debug3("%s entering", __func__);
1834 cmd = buffer_get_string(m, &len);
1835 /* sanity check command, if so how? */
1836 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001837 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001838 return (0);
1839}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001840#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001841
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001842void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001843monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001844{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001845 struct ssh *ssh = active_state; /* XXX */
1846 struct kex *kex;
1847 int r;
1848
1849 debug3("%s: packet_set_state", __func__);
1850 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1851 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1852 sshbuf_free(child_state);
1853 child_state = NULL;
1854
1855 if ((kex = ssh->kex) != 0) {
1856 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001857#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001858 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1859 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1860 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1861 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001862# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001863 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001864# endif
Damien Miller773dda22015-01-30 23:10:17 +11001865#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001866 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1867 kex->load_host_public_key=&get_hostkey_public_by_type;
1868 kex->load_host_private_key=&get_hostkey_private_by_type;
1869 kex->host_key_index=&get_hostkey_index;
1870 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001871 }
1872
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001874 if (options.compression) {
1875 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1876 (ssh_packet_comp_alloc_func *)mm_zalloc,
1877 (ssh_packet_comp_free_func *)mm_zfree);
1878 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001879}
1880
1881/* This function requries careful sanity checking */
1882
1883void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001884mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001885{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001886 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001887
markus@openbsd.org091c3022015-01-19 19:52:16 +00001888 if ((child_state = sshbuf_new()) == NULL)
1889 fatal("%s: sshbuf_new failed", __func__);
1890 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1891 child_state);
1892 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001893}
1894
1895
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001896/* XXX */
1897
1898#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001899 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001900 fatal("fcntl(%d, F_SETFD)", x); \
1901} while (0)
1902
1903static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001904monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905{
Damien Miller8f0bf232011-06-20 14:42:23 +10001906 int pair[2];
1907
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001909 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001910 FD_CLOSEONEXEC(pair[0]);
1911 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001912 mon->m_recvfd = pair[0];
1913 mon->m_sendfd = pair[1];
1914
1915 if (do_logfds) {
1916 if (pipe(pair) == -1)
1917 fatal("%s: pipe: %s", __func__, strerror(errno));
1918 FD_CLOSEONEXEC(pair[0]);
1919 FD_CLOSEONEXEC(pair[1]);
1920 mon->m_log_recvfd = pair[0];
1921 mon->m_log_sendfd = pair[1];
1922 } else
1923 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001924}
1925
1926#define MM_MEMSIZE 65536
1927
1928struct monitor *
1929monitor_init(void)
1930{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001931 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001932 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001933
Damien Miller07d86be2006-03-26 14:19:21 +11001934 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001935
Damien Miller8f0bf232011-06-20 14:42:23 +10001936 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937
1938 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001939 if (options.compression) {
1940 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1941 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001942
Damien Miller2d6b8332002-06-21 15:59:49 +10001943 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001944 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1945 (ssh_packet_comp_alloc_func *)mm_zalloc,
1946 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001947 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948
1949 return mon;
1950}
1951
1952void
1953monitor_reinit(struct monitor *mon)
1954{
Damien Miller8f0bf232011-06-20 14:42:23 +10001955 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001956}
Darren Tucker0efd1552003-08-26 11:49:55 +10001957
1958#ifdef GSSAPI
1959int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001960mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001961{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001962 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001963 OM_uint32 major;
1964 u_int len;
1965
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001966 goid.elements = buffer_get_string(m, &len);
1967 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001968
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001969 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001970
Darren Tuckera627d422013-06-02 07:31:17 +10001971 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001972
1973 buffer_clear(m);
1974 buffer_put_int(m, major);
1975
Damien Miller6fd6def2005-11-05 15:07:05 +11001976 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001977
1978 /* Now we have a context, enable the step */
1979 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1980
1981 return (0);
1982}
1983
1984int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001985mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001986{
1987 gss_buffer_desc in;
1988 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001989 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001990 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001991 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001992
Darren Tucker600ad8d2003-08-26 12:10:48 +10001993 in.value = buffer_get_string(m, &len);
1994 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001995 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10001996 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10001997
1998 buffer_clear(m);
1999 buffer_put_int(m, major);
2000 buffer_put_string(m, out.value, out.length);
2001 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002002 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002003
2004 gss_release_buffer(&minor, &out);
2005
Damien Miller6fd6def2005-11-05 15:07:05 +11002006 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002007 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2008 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002009 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002010 }
2011 return (0);
2012}
2013
2014int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002015mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002016{
2017 gss_buffer_desc gssbuf, mic;
2018 OM_uint32 ret;
2019 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002020
Damien Miller0425d402003-11-17 22:18:21 +11002021 gssbuf.value = buffer_get_string(m, &len);
2022 gssbuf.length = len;
2023 mic.value = buffer_get_string(m, &len);
2024 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002025
Damien Miller0425d402003-11-17 22:18:21 +11002026 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002027
Darren Tuckera627d422013-06-02 07:31:17 +10002028 free(gssbuf.value);
2029 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002030
Damien Miller0425d402003-11-17 22:18:21 +11002031 buffer_clear(m);
2032 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002033
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002034 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002035
Damien Miller0425d402003-11-17 22:18:21 +11002036 if (!GSS_ERROR(ret))
2037 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002038
Damien Miller0425d402003-11-17 22:18:21 +11002039 return (0);
2040}
2041
2042int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002043mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002044{
2045 int authenticated;
2046
2047 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2048
2049 buffer_clear(m);
2050 buffer_put_int(m, authenticated);
2051
2052 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002053 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002054
Damien Miller6fd6def2005-11-05 15:07:05 +11002055 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002056
2057 /* Monitor loop will terminate if authenticated */
2058 return (authenticated);
2059}
2060#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002061