blob: fbe965e7cccc625ac0e17c8224daa830994b2d38 [file] [log] [blame]
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001/* $OpenBSD: monitor.c,v 1.160 2016/05/02 10:26:04 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>
djm@openbsd.org1a31d022016-05-02 08:49:03 +000037#include <limits.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Miller9af21972015-02-24 09:04:32 +110043#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000044#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110045#endif
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100048#include <stdarg.h>
49#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100050#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100051#ifdef HAVE_POLL_H
52#include <poll.h>
53#else
54# ifdef HAVE_SYS_POLL_H
55# include <sys/poll.h>
56# endif
57#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000058
59#ifdef SKEY
60#include <skey.h>
61#endif
62
Damien Miller1f0311c2014-05-15 14:24:09 +100063#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110064#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100065#endif
Damien Miller03e20032006-03-15 11:16:59 +110066
Damien Millerb84886b2008-05-19 15:05:07 +100067#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100068#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "key.h"
72#include "buffer.h"
73#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000074#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100075#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076#include "kex.h"
77#include "dh.h"
Darren Tucker01558b72016-07-18 09:33:25 +100078#include "auth-pam.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000079#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
80#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000081#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000082#define TARGET_OS_MAC 1
83#else
84#include "zlib.h"
85#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000086#include "packet.h"
87#include "auth-options.h"
88#include "sshpty.h"
89#include "channels.h"
90#include "session.h"
91#include "sshlogin.h"
92#include "canohost.h"
93#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100094#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "servconf.h"
96#include "monitor.h"
97#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100098#ifdef GSSAPI
99#include "ssh-gss.h"
100#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101#include "monitor_wrap.h"
102#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103#include "compat.h"
104#include "ssh2.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000105#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000106#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000107#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108
Darren Tucker0efd1552003-08-26 11:49:55 +1000109#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000110static Gssctxt *gsscontext = NULL;
111#endif
112
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113/* Imports */
114extern ServerOptions options;
115extern u_int utmp_len;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117extern Buffer auth_debug;
118extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000119extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120
121/* State exported from the child */
markus@openbsd.org091c3022015-01-19 19:52:16 +0000122static struct sshbuf *child_state;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123
Damien Miller469954d2003-06-18 20:25:33 +1000124/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000125
126int mm_answer_moduli(int, Buffer *);
127int mm_answer_sign(int, Buffer *);
128int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000129int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130int mm_answer_authserv(int, Buffer *);
131int mm_answer_authpassword(int, Buffer *);
132int mm_answer_bsdauthquery(int, Buffer *);
133int mm_answer_bsdauthrespond(int, Buffer *);
134int mm_answer_skeyquery(int, Buffer *);
135int mm_answer_skeyrespond(int, Buffer *);
136int mm_answer_keyallowed(int, Buffer *);
137int mm_answer_keyverify(int, Buffer *);
138int mm_answer_pty(int, Buffer *);
139int mm_answer_pty_cleanup(int, Buffer *);
140int mm_answer_term(int, Buffer *);
141int mm_answer_rsa_keyallowed(int, Buffer *);
142int mm_answer_rsa_challenge(int, Buffer *);
143int mm_answer_rsa_response(int, Buffer *);
144int mm_answer_sesskey(int, Buffer *);
145int mm_answer_sessid(int, Buffer *);
146
Damien Miller79418552002-04-23 20:28:48 +1000147#ifdef USE_PAM
148int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000149int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000150int mm_answer_pam_init_ctx(int, Buffer *);
151int mm_answer_pam_query(int, Buffer *);
152int mm_answer_pam_respond(int, Buffer *);
153int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000154#endif
155
Darren Tucker0efd1552003-08-26 11:49:55 +1000156#ifdef GSSAPI
157int mm_answer_gss_setup_ctx(int, Buffer *);
158int mm_answer_gss_accept_ctx(int, Buffer *);
159int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100160int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000161#endif
Damien Miller25162f22002-09-12 09:47:29 +1000162
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100163#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100164int mm_answer_audit_event(int, Buffer *);
165int mm_answer_audit_command(int, Buffer *);
166#endif
167
Damien Miller8f0bf232011-06-20 14:42:23 +1000168static int monitor_read_log(struct monitor *);
169
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000171
172#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000173static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000174#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175
176/* local state for key verify */
177static u_char *key_blob = NULL;
178static u_int key_bloblen = 0;
179static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000180static char *hostbased_cuser = NULL;
181static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100183static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000184static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000185static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000186static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187
188struct mon_table {
189 enum monitor_reqtype type;
190 int flags;
191 int (*f)(int, Buffer *);
192};
193
194#define MON_ISAUTH 0x0004 /* Required for Authentication */
195#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
196#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100197#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198
199#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
200
201#define MON_PERMIT 0x1000 /* Request is permitted */
202
203struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000204#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000206#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
208 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
209 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000210 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000211 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000212#ifdef USE_PAM
213 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000214 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000215 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
216 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
217 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
218 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000219#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100220#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100221 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100222#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223#ifdef BSD_AUTH
224 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100225 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226#endif
227#ifdef SKEY
228 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
229 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
230#endif
231 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
232 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#ifdef GSSAPI
234 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
235 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
236 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100237 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000238#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 {0, 0, NULL}
240};
241
242struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000243#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000245#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
247 {MONITOR_REQ_PTY, 0, mm_answer_pty},
248 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
249 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100250#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100251 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
252 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
253#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000254 {0, 0, NULL}
255};
256
257struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000258#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000259 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
260 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
261 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
262 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100263 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
264 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
266 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
267#ifdef BSD_AUTH
268 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100269 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270#endif
271#ifdef SKEY
272 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
273 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
274#endif
Damien Millera33501b2002-05-08 12:24:42 +1000275#ifdef USE_PAM
276 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000277 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000278 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
279 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
280 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
281 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000282#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100283#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100284 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100285#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000286#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 {0, 0, NULL}
288};
289
290struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000291#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
293 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
294 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100295#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100296 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000297 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100298#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000299#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 {0, 0, NULL}
301};
302
303struct mon_table *mon_dispatch;
304
305/* Specifies if a certain message is allowed at the moment */
306
307static void
308monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
309{
310 while (ent->f != NULL) {
311 if (ent->type == type) {
312 ent->flags &= ~MON_PERMIT;
313 ent->flags |= permit ? MON_PERMIT : 0;
314 return;
315 }
316 ent++;
317 }
318}
319
320static void
321monitor_permit_authentications(int permit)
322{
323 struct mon_table *ent = mon_dispatch;
324
325 while (ent->f != NULL) {
326 if (ent->flags & MON_AUTH) {
327 ent->flags &= ~MON_PERMIT;
328 ent->flags |= permit ? MON_PERMIT : 0;
329 }
330 ent++;
331 }
332}
333
Darren Tucker3e33cec2003-10-02 16:12:36 +1000334void
335monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336{
337 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100338 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339
340 debug3("preauth child monitor started");
341
Damien Miller8f0bf232011-06-20 14:42:23 +1000342 close(pmonitor->m_recvfd);
343 close(pmonitor->m_log_sendfd);
344 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
345
Darren Tucker3e33cec2003-10-02 16:12:36 +1000346 authctxt = _authctxt;
347 memset(authctxt, 0, sizeof(*authctxt));
348
Darren Tuckerde0de392005-03-31 23:52:04 +1000349 authctxt->loginmsg = &loginmsg;
350
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000351 if (compat20) {
352 mon_dispatch = mon_dispatch_proto20;
353
354 /* Permit requests for moduli and signatures */
355 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
356 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
357 } else {
358 mon_dispatch = mon_dispatch_proto15;
359
360 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
361 }
362
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363 /* The first few requests do not require asynchronous access */
364 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100365 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100366 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100367 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100368 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100369
370 /* Special handling for multiple required authentications */
371 if (options.num_auth_methods != 0) {
372 if (!compat20)
373 fatal("AuthenticationMethods is not supported"
374 "with SSH protocol 1");
375 if (authenticated &&
376 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000377 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100378 debug3("%s: method %s: partial", __func__,
379 auth_method);
380 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100381 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100382 }
383 }
384
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385 if (authenticated) {
386 if (!(ent->flags & MON_AUTHDECIDE))
387 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000388 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389 if (authctxt->pw->pw_uid == 0 &&
390 !auth_root_allowed(auth_method))
391 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000392#ifdef USE_PAM
393 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100394 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000395 Buffer m;
396
397 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100398 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000399 MONITOR_REQ_PAM_ACCOUNT, &m);
400 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
401 buffer_free(&m);
402 }
403#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100405 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100406 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000407 auth_method, auth_submethod);
djm@openbsd.org94885382015-06-22 23:42:16 +0000408 if (!partial && !authenticated)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409 authctxt->failures++;
410 }
411 }
412
413 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000414 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100415 if (strcmp(auth_method, "unknown") == 0)
416 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417
418 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000419 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000421 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000422
Damien Miller6a1937e2012-12-12 10:44:38 +1100423 /* Drain any buffered messages from the child */
424 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
425 ;
426
Damien Miller8f0bf232011-06-20 14:42:23 +1000427 close(pmonitor->m_sendfd);
428 close(pmonitor->m_log_recvfd);
429 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430}
431
Damien Millerbe64d432003-05-14 19:31:12 +1000432static void
433monitor_set_child_handler(pid_t pid)
434{
435 monitor_child_pid = pid;
436}
437
438static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000439monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000440{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000441 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000442}
443
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000445monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000446{
Damien Miller8f0bf232011-06-20 14:42:23 +1000447 close(pmonitor->m_recvfd);
448 pmonitor->m_recvfd = -1;
449
Damien Millerbe64d432003-05-14 19:31:12 +1000450 monitor_set_child_handler(pmonitor->m_pid);
451 signal(SIGHUP, &monitor_child_handler);
452 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000453 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000454#ifdef SIGXFSZ
455 signal(SIGXFSZ, SIG_IGN);
456#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000457
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458 if (compat20) {
459 mon_dispatch = mon_dispatch_postauth20;
460
461 /* Permit requests for moduli and signatures */
462 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
463 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
464 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465 } else {
466 mon_dispatch = mon_dispatch_postauth15;
467 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
468 }
469 if (!no_pty_flag) {
470 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
471 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
472 }
473
474 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000475 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000476}
477
478void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000479monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480{
Damien Miller2d6b8332002-06-21 15:59:49 +1000481 if (options.compression) {
482 /* The member allocation is not visible, so sync it */
483 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
484 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485}
486
markus@openbsd.org091c3022015-01-19 19:52:16 +0000487/* Allocation functions for zlib */
488static void *
489mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
490{
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000491 if (size == 0 || ncount == 0 || ncount > SIZE_MAX / size)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000492 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
493
deraadt@openbsd.orge774e5e2015-08-21 23:29:31 +0000494 return mm_malloc(mm, size * ncount);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000495}
496
497static void
498mm_zfree(struct mm_master *mm, void *address)
499{
500 mm_free(mm, address);
501}
502
Damien Miller8f0bf232011-06-20 14:42:23 +1000503static int
504monitor_read_log(struct monitor *pmonitor)
505{
506 Buffer logmsg;
507 u_int len, level;
508 char *msg;
509
510 buffer_init(&logmsg);
511
512 /* Read length */
513 buffer_append_space(&logmsg, 4);
514 if (atomicio(read, pmonitor->m_log_recvfd,
515 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
516 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100517 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000518 debug("%s: child log fd closed", __func__);
519 close(pmonitor->m_log_recvfd);
520 pmonitor->m_log_recvfd = -1;
521 return -1;
522 }
523 fatal("%s: log fd read: %s", __func__, strerror(errno));
524 }
525 len = buffer_get_int(&logmsg);
526 if (len <= 4 || len > 8192)
527 fatal("%s: invalid log message length %u", __func__, len);
528
529 /* Read severity, message */
530 buffer_clear(&logmsg);
531 buffer_append_space(&logmsg, len);
532 if (atomicio(read, pmonitor->m_log_recvfd,
533 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
534 fatal("%s: log fd read: %s", __func__, strerror(errno));
535
536 /* Log it */
537 level = buffer_get_int(&logmsg);
538 msg = buffer_get_string(&logmsg, NULL);
539 if (log_level_name(level) == NULL)
540 fatal("%s: invalid log level %u (corrupted message?)",
541 __func__, level);
542 do_log2(level, "%s [preauth]", msg);
543
544 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000545 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000546
547 return 0;
548}
549
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000551monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552 struct mon_table **pent)
553{
554 Buffer m;
555 int ret;
556 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000557 struct pollfd pfd[2];
558
559 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100560 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000561 pfd[0].fd = pmonitor->m_sendfd;
562 pfd[0].events = POLLIN;
563 pfd[1].fd = pmonitor->m_log_recvfd;
564 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000565 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
566 if (errno == EINTR || errno == EAGAIN)
567 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000568 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000569 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000570 if (pfd[1].revents) {
571 /*
572 * Drain all log messages before processing next
573 * monitor request.
574 */
575 monitor_read_log(pmonitor);
576 continue;
577 }
578 if (pfd[0].revents)
579 break; /* Continues below */
580 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581
582 buffer_init(&m);
583
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000584 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 type = buffer_get_char(&m);
586
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000587 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 while (ent->f != NULL) {
590 if (ent->type == type)
591 break;
592 ent++;
593 }
594
595 if (ent->f != NULL) {
596 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000597 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000599 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600 buffer_free(&m);
601
602 /* The child may use this request only once, disable it */
603 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000604 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 type);
606 ent->flags &= ~MON_PERMIT;
607 }
608
609 if (pent != NULL)
610 *pent = ent;
611
612 return ret;
613 }
614
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000615 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
617 /* NOTREACHED */
618 return (-1);
619}
620
621/* allowed key state */
622static int
623monitor_allowed_key(u_char *blob, u_int bloblen)
624{
625 /* make sure key is allowed */
626 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000627 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 return (0);
629 return (1);
630}
631
632static void
633monitor_reset_key_state(void)
634{
635 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000636 free(key_blob);
637 free(hostbased_cuser);
638 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 key_blob = NULL;
640 key_bloblen = 0;
641 key_blobtype = MM_NOKEY;
642 hostbased_cuser = NULL;
643 hostbased_chost = NULL;
644}
645
Damien Miller1f0311c2014-05-15 14:24:09 +1000646#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000648mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649{
650 DH *dh;
651 int min, want, max;
652
653 min = buffer_get_int(m);
654 want = buffer_get_int(m);
655 max = buffer_get_int(m);
656
657 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000658 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 /* We need to check here, too, in case the child got corrupted */
660 if (max < min || want < min || max < want)
661 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
664 buffer_clear(m);
665
666 dh = choose_dh(min, want, max);
667 if (dh == NULL) {
668 buffer_put_char(m, 0);
669 return (0);
670 } else {
671 /* Send first bignum */
672 buffer_put_char(m, 1);
673 buffer_put_bignum2(m, dh->p);
674 buffer_put_bignum2(m, dh->g);
675
676 DH_free(dh);
677 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000678 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679 return (0);
680}
Damien Miller1f0311c2014-05-15 14:24:09 +1000681#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
683int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685{
djm@openbsd.org523463a2015-02-16 22:13:32 +0000686 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000687 extern int auth_sock; /* XXX move to state struct? */
688 struct sshkey *key;
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000689 struct sshbuf *sigbuf = NULL;
690 u_char *p = NULL, *signature = NULL;
691 char *alg = NULL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000692 size_t datlen, siglen, alglen;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000693 int r, is_proof = 0;
694 u_int keyid;
djm@openbsd.org44732de2015-02-20 22:17:21 +0000695 const char proof_req[] = "hostkeys-prove-00@openssh.com";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000697 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
djm@openbsd.org141efe42015-01-14 20:05:27 +0000699 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000700 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
701 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000702 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000703 if (keyid > INT_MAX)
704 fatal("%s: invalid key ID", __func__);
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,
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +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) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000750 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000751 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,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000757 p, datlen, alg, datafellows)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000758 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
djm@openbsd.org24c9bde2016-02-15 23:32:37 +0000771 free(alg);
Darren Tuckera627d422013-06-02 07:31:17 +1000772 free(p);
773 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776
777 /* Turn on permissions for getpwnam */
778 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
779
780 return (0);
781}
782
783/* Retrieves the password entry and also checks if the user is permitted */
784
785int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000786mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000788 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789 struct passwd *pwent;
790 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000791 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000793 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794
795 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000796 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797
Darren Tuckerb09b6772004-06-22 15:06:46 +1000798 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000799
Darren Tuckerb09b6772004-06-22 15:06:46 +1000800 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801
Darren Tuckerb09b6772004-06-22 15:06:46 +1000802 authctxt->user = xstrdup(username);
803 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000804 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805
806 buffer_clear(m);
807
808 if (pwent == NULL) {
809 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100810 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811 goto out;
812 }
813
814 allowed = 1;
815 authctxt->pw = pwent;
816 authctxt->valid = 1;
817
818 buffer_put_char(m, 1);
819 buffer_put_string(m, pwent, sizeof(struct passwd));
820 buffer_put_cstring(m, pwent->pw_name);
821 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000822#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000824#endif
825#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000827#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828 buffer_put_cstring(m, pwent->pw_dir);
829 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100830
831 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100832 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000833
834#define M_CP_STROPT(x) do { \
835 if (options.x != NULL) \
836 buffer_put_cstring(m, options.x); \
837 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000838#define M_CP_STRARRAYOPT(x, nx) do { \
839 for (i = 0; i < options.nx; i++) \
840 buffer_put_cstring(m, options.x[i]); \
841 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000842 /* See comment in servconf.h */
843 COPY_MATCH_STRING_OPTS();
844#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000845#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100846
847 /* Create valid auth method lists */
848 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
849 /*
850 * The monitor will continue long enough to let the child
851 * run to it's packet_disconnect(), but it must not allow any
852 * authentication to succeed.
853 */
854 debug("%s: no valid authentication method lists", __func__);
855 }
856
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000857 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000858 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000859
860 /* For SSHv1 allow authentication now */
861 if (!compat20)
862 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000863 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864 /* Allow service/style information on the auth context */
865 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000866 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
867 }
Damien Millera33501b2002-05-08 12:24:42 +1000868#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000869 if (options.use_pam)
870 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000871#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000872
873 return (0);
874}
875
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000876int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000877{
878 char *banner;
879
880 buffer_clear(m);
881 banner = auth2_read_banner();
882 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000883 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000884 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000885
886 return (0);
887}
888
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000890mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000891{
892 monitor_permit_authentications(1);
893
894 authctxt->service = buffer_get_string(m, NULL);
895 authctxt->style = buffer_get_string(m, NULL);
896 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000897 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898
899 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000900 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000901 authctxt->style = NULL;
902 }
903
904 return (0);
905}
906
907int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000908mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909{
910 static int call_count;
911 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000912 int authenticated;
913 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
915 passwd = buffer_get_string(m, &plen);
916 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000917 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000918 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100919 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000920 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921
922 buffer_clear(m);
923 buffer_put_int(m, authenticated);
Darren Tucker01558b72016-07-18 09:33:25 +1000924#ifdef USE_PAM
925 buffer_put_int(m, sshpam_get_maxtries_reached());
926#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000928 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000929 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000930
931 call_count++;
932 if (plen == 0 && call_count == 1)
933 auth_method = "none";
934 else
935 auth_method = "password";
936
937 /* Causes monitor loop to terminate if authenticated */
938 return (authenticated);
939}
940
941#ifdef BSD_AUTH
942int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000943mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944{
945 char *name, *infotxt;
946 u_int numprompts;
947 u_int *echo_on;
948 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100949 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950
Damien Millerb7df3af2003-02-24 11:55:46 +1100951 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
952 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
954 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100955 buffer_put_int(m, success);
956 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957 buffer_put_cstring(m, prompts[0]);
958
Damien Millerb7df3af2003-02-24 11:55:46 +1100959 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
Damien Millerb7df3af2003-02-24 11:55:46 +1100962 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000963 free(name);
964 free(infotxt);
965 free(prompts);
966 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967 }
968
969 return (0);
970}
971
972int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000973mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974{
975 char *response;
976 int authok;
977
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +0000978 if (authctxt->as == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000979 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980
981 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000982 authok = options.challenge_response_authentication &&
983 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000985 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000986 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 buffer_clear(m);
989 buffer_put_int(m, authok);
990
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000991 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000992 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993
Damien Miller91a55f22013-04-23 15:18:10 +1000994 if (compat20) {
995 auth_method = "keyboard-interactive";
996 auth_submethod = "bsdauth";
997 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100998 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999
1000 return (authok != 0);
1001}
1002#endif
1003
1004#ifdef SKEY
1005int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001006mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007{
1008 struct skey skey;
1009 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +11001010 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
Darren Tucker06a8cfe2004-04-14 17:24:30 +10001012 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
1013 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014
1015 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +11001016 buffer_put_int(m, success);
1017 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 buffer_put_cstring(m, challenge);
1019
Damien Millerb7df3af2003-02-24 11:55:46 +11001020 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001021 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022
1023 return (0);
1024}
1025
1026int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001027mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001028{
1029 char *response;
1030 int authok;
1031
1032 response = buffer_get_string(m, NULL);
1033
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001034 authok = (options.challenge_response_authentication &&
1035 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036 skey_haskey(authctxt->pw->pw_name) == 0 &&
1037 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1038
Darren Tuckerf60845f2013-06-02 08:07:31 +10001039 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040
1041 buffer_clear(m);
1042 buffer_put_int(m, authok);
1043
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001044 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001045 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046
Damien Millerac947352015-11-20 12:35:41 +11001047 auth_method = "keyboard-interactive";
1048 auth_submethod = "skey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049
1050 return (authok != 0);
1051}
1052#endif
1053
Damien Miller79418552002-04-23 20:28:48 +10001054#ifdef USE_PAM
1055int
Darren Tucker7eda5922004-06-22 13:22:50 +10001056mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001057{
Damien Miller4e448a32003-05-14 15:11:48 +10001058 if (!options.use_pam)
1059 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1060
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001061 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001062
Damien Miller1f499fd2003-08-25 13:08:49 +10001063 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1064
Damien Miller79418552002-04-23 20:28:48 +10001065 return (0);
1066}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001067
Damien Miller1f499fd2003-08-25 13:08:49 +10001068int
Darren Tucker7eda5922004-06-22 13:22:50 +10001069mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001070{
1071 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001072
Damien Miller1f499fd2003-08-25 13:08:49 +10001073 if (!options.use_pam)
1074 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1075
1076 ret = do_pam_account();
1077
1078 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001079 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001080
Darren Tucker7eda5922004-06-22 13:22:50 +10001081 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001082
1083 return (ret);
1084}
1085
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086static void *sshpam_ctxt, *sshpam_authok;
1087extern KbdintDevice sshpam_device;
1088
1089int
Darren Tucker7eda5922004-06-22 13:22:50 +10001090mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001091{
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 debug3("%s", __func__);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001093 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1094 sshpam_authok = NULL;
1095 buffer_clear(m);
1096 if (sshpam_ctxt != NULL) {
1097 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1098 buffer_put_int(m, 1);
1099 } else {
1100 buffer_put_int(m, 0);
1101 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001102 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103 return (0);
1104}
1105
1106int
Darren Tucker7eda5922004-06-22 13:22:50 +10001107mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001109 char *name = NULL, *info = NULL, **prompts = NULL;
1110 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001111 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112
1113 debug3("%s", __func__);
1114 sshpam_authok = NULL;
1115 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1116 if (ret == 0 && num == 0)
1117 sshpam_authok = sshpam_ctxt;
1118 if (num > 1 || name == NULL || info == NULL)
1119 ret = -1;
1120 buffer_clear(m);
1121 buffer_put_int(m, ret);
1122 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001123 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001125 free(info);
Darren Tucker01558b72016-07-18 09:33:25 +10001126 buffer_put_int(m, sshpam_get_maxtries_reached());
Damien Miller4f9f42a2003-05-10 19:28:02 +10001127 buffer_put_int(m, num);
1128 for (i = 0; i < num; ++i) {
1129 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001130 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001131 buffer_put_int(m, echo_on[i]);
1132 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001133 free(prompts);
1134 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001135 auth_method = "keyboard-interactive";
1136 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001137 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001138 return (0);
1139}
1140
1141int
Darren Tucker7eda5922004-06-22 13:22:50 +10001142mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001143{
1144 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001145 u_int i, num;
1146 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001147
1148 debug3("%s", __func__);
1149 sshpam_authok = NULL;
1150 num = buffer_get_int(m);
1151 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001152 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001153 for (i = 0; i < num; ++i)
1154 resp[i] = buffer_get_string(m, NULL);
1155 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1156 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001157 free(resp[i]);
1158 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001159 } else {
1160 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1161 }
1162 buffer_clear(m);
1163 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001164 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001165 auth_method = "keyboard-interactive";
1166 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001167 if (ret == 0)
1168 sshpam_authok = sshpam_ctxt;
1169 return (0);
1170}
1171
1172int
Darren Tucker7eda5922004-06-22 13:22:50 +10001173mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001174{
Damien Miller5e75f512015-08-11 13:34:12 +10001175 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001176
1177 debug3("%s", __func__);
1178 (sshpam_device.free_ctx)(sshpam_ctxt);
Damien Miller5e75f512015-08-11 13:34:12 +10001179 sshpam_ctxt = sshpam_authok = NULL;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001180 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001181 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001182 auth_method = "keyboard-interactive";
1183 auth_submethod = "pam";
Damien Miller5e75f512015-08-11 13:34:12 +10001184 return r;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001185}
Damien Miller79418552002-04-23 20:28:48 +10001186#endif
1187
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001189mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190{
1191 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001192 char *cuser, *chost;
1193 u_char *blob;
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001194 u_int bloblen, pubkey_auth_attempt;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 enum mm_keytype type = 0;
1196 int allowed = 0;
1197
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001198 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199
1200 type = buffer_get_int(m);
1201 cuser = buffer_get_string(m, NULL);
1202 chost = buffer_get_string(m, NULL);
1203 blob = buffer_get_string(m, &bloblen);
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001204 pubkey_auth_attempt = buffer_get_int(m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001205
1206 key = key_from_blob(blob, bloblen);
1207
1208 if ((compat20 && type == MM_RSAHOSTKEY) ||
1209 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001210 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001212 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213
Damien Miller3e3b5142003-11-17 21:13:40 +11001214 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001215 /* These should not make it past the privsep child */
1216 if (key_type_plain(key->type) == KEY_RSA &&
1217 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1218 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1219
Darren Tucker47eede72005-03-14 23:08:12 +11001220 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001222 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001223 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001224 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001225 options.pubkey_key_types, 0) == 1 &&
1226 user_key_allowed(authctxt->pw, key,
1227 pubkey_auth_attempt);
Damien Miller20bdcd72013-07-18 16:10:09 +10001228 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001229 auth_method = "publickey";
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001230 if (options.pubkey_authentication &&
1231 (!pubkey_auth_attempt || allowed != 1))
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001232 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 break;
1234 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001235 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001236 match_pattern_list(sshkey_ssh_name(key),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001237 options.hostbased_key_types, 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001238 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001240 pubkey_auth_info(authctxt, key,
1241 "client user \"%.100s\", client host \"%.100s\"",
1242 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001243 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001245#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 case MM_RSAHOSTKEY:
1247 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001248 allowed = options.rhosts_rsa_authentication &&
1249 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001251 if (options.rhosts_rsa_authentication && allowed != 1)
1252 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001253 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001255#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001257 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 break;
1259 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 }
Damien Miller00111382003-03-10 11:21:17 +11001261 if (key != NULL)
1262 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263
1264 /* clear temporarily storage (used by verify) */
1265 monitor_reset_key_state();
1266
1267 if (allowed) {
1268 /* Save temporarily for comparison in verify */
1269 key_blob = blob;
1270 key_bloblen = bloblen;
1271 key_blobtype = type;
1272 hostbased_cuser = cuser;
1273 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001274 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001275 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001276 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001277 free(blob);
1278 free(cuser);
1279 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280 }
1281
1282 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001283 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284
1285 buffer_clear(m);
1286 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001287 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001289 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290
1291 if (type == MM_RSAHOSTKEY)
1292 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1293
1294 return (0);
1295}
1296
1297static int
1298monitor_valid_userblob(u_char *data, u_int datalen)
1299{
1300 Buffer b;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001301 u_char *p;
1302 char *userstyle, *cp;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303 u_int len;
1304 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305
1306 buffer_init(&b);
1307 buffer_append(&b, data, datalen);
1308
1309 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001310 p = buffer_ptr(&b);
1311 len = buffer_len(&b);
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 Lindstromf67e0772002-06-06 20:58:19 +00001315 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 buffer_consume(&b, session_id2_len);
1317 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001318 p = buffer_get_string(&b, &len);
1319 if ((session_id2 == NULL) ||
1320 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001321 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001323 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 }
1325 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1326 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001327 cp = buffer_get_cstring(&b, NULL);
Damien Miller4ce189d2013-04-23 15:17:52 +10001328 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1329 authctxt->style ? ":" : "",
1330 authctxt->style ? authctxt->style : "");
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001331 if (strcmp(userstyle, cp) != 0) {
1332 logit("wrong user name passed to monitor: "
1333 "expected %s != %.100s", userstyle, cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334 fail++;
1335 }
Darren Tuckera627d422013-06-02 07:31:17 +10001336 free(userstyle);
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001337 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338 buffer_skip_string(&b);
1339 if (datafellows & SSH_BUG_PKAUTH) {
1340 if (!buffer_get_char(&b))
1341 fail++;
1342 } else {
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001343 cp = buffer_get_cstring(&b, NULL);
1344 if (strcmp("publickey", cp) != 0)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001345 fail++;
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001346 free(cp);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347 if (!buffer_get_char(&b))
1348 fail++;
1349 buffer_skip_string(&b);
1350 }
1351 buffer_skip_string(&b);
1352 if (buffer_len(&b) != 0)
1353 fail++;
1354 buffer_free(&b);
1355 return (fail == 0);
1356}
1357
1358static int
Damien Millera10f5612002-09-12 09:49:15 +10001359monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1360 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361{
1362 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001363 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364 u_int len;
1365 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 buffer_init(&b);
1368 buffer_append(&b, data, datalen);
1369
Ben Lindstromf67e0772002-06-06 20:58:19 +00001370 p = buffer_get_string(&b, &len);
1371 if ((session_id2 == NULL) ||
1372 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001373 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001375 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001376
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1378 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001379 p = buffer_get_cstring(&b, NULL);
1380 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1381 authctxt->style ? ":" : "",
1382 authctxt->style ? authctxt->style : "");
1383 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001384 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001385 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386 fail++;
1387 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001388 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001389 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001391 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392 if (strcmp(p, "hostbased") != 0)
1393 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001394 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395 buffer_skip_string(&b); /* pkalg */
1396 buffer_skip_string(&b); /* pkblob */
1397
1398 /* verify client host, strip trailing dot if necessary */
1399 p = buffer_get_string(&b, NULL);
1400 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1401 p[len - 1] = '\0';
1402 if (strcmp(p, chost) != 0)
1403 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001404 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405
1406 /* verify client user */
1407 p = buffer_get_string(&b, NULL);
1408 if (strcmp(p, cuser) != 0)
1409 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001410 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411
1412 if (buffer_len(&b) != 0)
1413 fail++;
1414 buffer_free(&b);
1415 return (fail == 0);
1416}
1417
1418int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001419mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420{
1421 Key *key;
1422 u_char *signature, *data, *blob;
1423 u_int signaturelen, datalen, bloblen;
1424 int verified = 0;
1425 int valid_data = 0;
1426
1427 blob = buffer_get_string(m, &bloblen);
1428 signature = buffer_get_string(m, &signaturelen);
1429 data = buffer_get_string(m, &datalen);
1430
1431 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001432 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434
1435 key = key_from_blob(blob, bloblen);
1436 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001437 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438
1439 switch (key_blobtype) {
1440 case MM_USERKEY:
1441 valid_data = monitor_valid_userblob(data, datalen);
1442 break;
1443 case MM_HOSTKEY:
1444 valid_data = monitor_valid_hostbasedblob(data, datalen,
1445 hostbased_cuser, hostbased_chost);
1446 break;
1447 default:
1448 valid_data = 0;
1449 break;
1450 }
1451 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001452 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453
1454 verified = key_verify(key, signature, signaturelen, data, datalen);
1455 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001456 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001458 /* If auth was successful then record key to ensure it isn't reused */
djm@openbsd.orgf54d8ac2015-09-04 04:44:08 +00001459 if (verified == 1 && key_blobtype == MM_USERKEY)
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001460 auth2_record_userkey(authctxt, key);
1461 else
1462 key_free(key);
1463
Darren Tuckera627d422013-06-02 07:31:17 +10001464 free(blob);
1465 free(signature);
1466 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467
Ben Lindstrome1c09122002-06-23 00:38:24 +00001468 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1469
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470 monitor_reset_key_state();
1471
1472 buffer_clear(m);
1473 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001474 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
Darren Tuckerfbba7352006-11-07 23:16:08 +11001476 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477}
1478
1479static void
1480mm_record_login(Session *s, struct passwd *pw)
1481{
djm@openbsd.org95767262016-03-07 19:02:43 +00001482 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001483 socklen_t fromlen;
1484 struct sockaddr_storage from;
1485
djm@openbsd.org17d4d9d2015-04-17 04:32:31 +00001486 if (options.use_login)
1487 return;
1488
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489 /*
1490 * Get IP address of client. If the connection is not a socket, let
1491 * the address be 0.0.0.0.
1492 */
1493 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001494 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001497 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001499 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 }
1501 }
1502 /* Record that there was a login on that tty from the remote host. */
1503 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
djm@openbsd.org95767262016-03-07 19:02:43 +00001504 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001505 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506}
1507
1508static void
1509mm_session_close(Session *s)
1510{
Damien Miller04bd8b02003-05-25 14:38:33 +10001511 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001513 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 session_pty_cleanup2(s);
1515 }
Damien Miller7207f642008-05-19 15:34:50 +10001516 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517}
1518
1519int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001520mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001522 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523 Session *s;
1524 int res, fd0;
1525
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001526 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527
1528 buffer_clear(m);
1529 s = session_new();
1530 if (s == NULL)
1531 goto error;
1532 s->authctxt = authctxt;
1533 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001534 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1536 if (res == 0)
1537 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538 pty_setowner(authctxt->pw, s->tty);
1539
1540 buffer_put_int(m, 1);
1541 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542
1543 /* We need to trick ttyslot */
1544 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001545 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546
1547 mm_record_login(s, authctxt->pw);
1548
1549 /* Now we can close the file descriptor again */
1550 close(0);
1551
Darren Tucker09991742004-07-17 17:05:14 +10001552 /* send messages generated by record_login */
1553 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1554 buffer_clear(&loginmsg);
1555
1556 mm_request_send(sock, MONITOR_ANS_PTY, m);
1557
Damien Miller54fd7cf2007-09-17 12:04:08 +10001558 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1559 mm_send_fd(sock, s->ttyfd) == -1)
1560 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001561
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562 /* make sure nothing uses fd 0 */
1563 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001564 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001566 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001567
1568 /* slave is not needed */
1569 close(s->ttyfd);
1570 s->ttyfd = s->ptyfd;
1571 /* no need to dup() because nobody closes ptyfd */
1572 s->ptymaster = s->ptyfd;
1573
Damien Miller9ab00b42006-08-05 12:40:11 +10001574 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575
1576 return (0);
1577
1578 error:
1579 if (s != NULL)
1580 mm_session_close(s);
1581 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001582 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583 return (0);
1584}
1585
1586int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001587mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588{
1589 Session *s;
1590 char *tty;
1591
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001592 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593
1594 tty = buffer_get_string(m, NULL);
1595 if ((s = session_by_tty(tty)) != NULL)
1596 mm_session_close(s);
1597 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001598 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 return (0);
1600}
1601
Damien Miller1f0311c2014-05-15 14:24:09 +10001602#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001604mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605{
1606 BIGNUM *p;
1607 int rsafail;
1608
1609 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001610 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611
1612 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001613 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614
1615 buffer_get_bignum2(m, p);
1616
1617 rsafail = ssh1_session_key(p);
1618
1619 buffer_clear(m);
1620 buffer_put_int(m, rsafail);
1621 buffer_put_bignum2(m, p);
1622
1623 BN_clear_free(p);
1624
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001625 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626
1627 /* Turn on permissions for sessid passing */
1628 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1629
1630 return (0);
1631}
1632
1633int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001634mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635{
1636 int i;
1637
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001638 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639
1640 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001641 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642 for (i = 0; i < 16; i++)
1643 session_id[i] = buffer_get_char(m);
1644
1645 /* Turn on permissions for getpwnam */
1646 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1647
1648 return (0);
1649}
1650
1651int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001652mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653{
1654 BIGNUM *client_n;
1655 Key *key = NULL;
1656 u_char *blob = NULL;
1657 u_int blen = 0;
1658 int allowed = 0;
1659
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001660 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661
Damien Miller7a8f5b32006-03-31 23:14:23 +11001662 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001663 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 buffer_get_bignum2(m, client_n);
1667 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1668 BN_clear_free(client_n);
1669 }
1670 buffer_clear(m);
1671 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001672 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001673
1674 /* clear temporarily storage (used by generate challenge) */
1675 monitor_reset_key_state();
1676
1677 if (allowed && key != NULL) {
1678 key->type = KEY_RSA; /* cheat for key_to_blob */
1679 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001680 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681 buffer_put_string(m, blob, blen);
1682
1683 /* Save temporarily for comparison in verify */
1684 key_blob = blob;
1685 key_bloblen = blen;
1686 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 }
Damien Miller00111382003-03-10 11:21:17 +11001688 if (key != NULL)
1689 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001691 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692
1693 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1694 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1695 return (0);
1696}
1697
1698int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001699mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700{
1701 Key *key = NULL;
1702 u_char *blob;
1703 u_int blen;
1704
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001705 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706
1707 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001708 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 blob = buffer_get_string(m, &blen);
1710 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001711 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001715 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001716 if (key->type != KEY_RSA)
1717 fatal("%s: received bad key type %d", __func__, key->type);
1718 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 if (ssh1_challenge)
1720 BN_clear_free(ssh1_challenge);
1721 ssh1_challenge = auth_rsa_generate_challenge(key);
1722
1723 buffer_clear(m);
1724 buffer_put_bignum2(m, ssh1_challenge);
1725
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001726 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001727 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728
1729 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001730
Darren Tuckera627d422013-06-02 07:31:17 +10001731 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001732 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001733 return (0);
1734}
1735
1736int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001737mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738{
1739 Key *key = NULL;
1740 u_char *blob, *response;
1741 u_int blen, len;
1742 int success;
1743
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001744 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745
1746 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001748 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001749 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001750
1751 blob = buffer_get_string(m, &blen);
1752 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001753 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001754 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001755 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001757 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758 response = buffer_get_string(m, &len);
1759 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001760 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001761 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1762
Darren Tuckera627d422013-06-02 07:31:17 +10001763 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001765 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001766
1767 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1768
1769 /* reset state */
1770 BN_clear_free(ssh1_challenge);
1771 ssh1_challenge = NULL;
1772 monitor_reset_key_state();
1773
1774 buffer_clear(m);
1775 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001776 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001777
1778 return (success);
1779}
Damien Miller1f0311c2014-05-15 14:24:09 +10001780#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781
1782int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001783mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001785 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001786 int res, status;
1787
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001788 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001789
1790 /* The child is terminating */
1791 session_destroy_all(&mm_session_close);
1792
Darren Tucker52358d62008-03-11 22:58:25 +11001793#ifdef USE_PAM
1794 if (options.use_pam)
1795 sshpam_cleanup();
1796#endif
1797
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001798 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001799 if (errno != EINTR)
1800 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801
1802 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1803
1804 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001805 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806}
1807
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001808#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001809/* Report that an audit event occurred */
1810int
1811mm_answer_audit_event(int socket, Buffer *m)
1812{
1813 ssh_audit_event_t event;
1814
1815 debug3("%s entering", __func__);
1816
1817 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001818 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001819 case SSH_AUTH_FAIL_PUBKEY:
1820 case SSH_AUTH_FAIL_HOSTBASED:
1821 case SSH_AUTH_FAIL_GSSAPI:
1822 case SSH_LOGIN_EXCEED_MAXTRIES:
1823 case SSH_LOGIN_ROOT_DENIED:
1824 case SSH_CONNECTION_CLOSE:
1825 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001826 audit_event(event);
1827 break;
1828 default:
1829 fatal("Audit event type %d not permitted", event);
1830 }
1831
1832 return (0);
1833}
1834
1835int
1836mm_answer_audit_command(int socket, Buffer *m)
1837{
1838 u_int len;
1839 char *cmd;
1840
1841 debug3("%s entering", __func__);
1842 cmd = buffer_get_string(m, &len);
1843 /* sanity check command, if so how? */
1844 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001845 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001846 return (0);
1847}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001848#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001849
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001850void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001851monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001852{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001853 struct ssh *ssh = active_state; /* XXX */
1854 struct kex *kex;
1855 int r;
1856
1857 debug3("%s: packet_set_state", __func__);
1858 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1859 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1860 sshbuf_free(child_state);
1861 child_state = NULL;
1862
mmcc@openbsd.org7d6c0362015-10-20 23:24:25 +00001863 if ((kex = ssh->kex) != NULL) {
markus@openbsd.org091c3022015-01-19 19:52:16 +00001864 /* XXX set callbacks */
Damien Miller773dda22015-01-30 23:10:17 +11001865#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +00001866 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1867 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001868 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1869 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1870 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
markus@openbsd.org091c3022015-01-19 19:52:16 +00001871 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1872 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001873# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +00001874 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Darren Tuckerf2004cd2015-02-23 05:04:21 +11001875# endif
Damien Miller773dda22015-01-30 23:10:17 +11001876#endif /* WITH_OPENSSL */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001877 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1878 kex->load_host_public_key=&get_hostkey_public_by_type;
1879 kex->load_host_private_key=&get_hostkey_private_by_type;
1880 kex->host_key_index=&get_hostkey_index;
1881 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001882 }
1883
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001884 /* Update with new address */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001885 if (options.compression) {
1886 ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
1887 (ssh_packet_comp_alloc_func *)mm_zalloc,
1888 (ssh_packet_comp_free_func *)mm_zfree);
1889 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001890}
1891
1892/* This function requries careful sanity checking */
1893
1894void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001895mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001896{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001897 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001898
markus@openbsd.org091c3022015-01-19 19:52:16 +00001899 if ((child_state = sshbuf_new()) == NULL)
1900 fatal("%s: sshbuf_new failed", __func__);
1901 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1902 child_state);
1903 debug3("%s: GOT new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001904}
1905
1906
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001907/* XXX */
1908
1909#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001910 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001911 fatal("fcntl(%d, F_SETFD)", x); \
1912} while (0)
1913
1914static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001915monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001916{
Damien Miller8f0bf232011-06-20 14:42:23 +10001917 int pair[2];
1918
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001919 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001920 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001921 FD_CLOSEONEXEC(pair[0]);
1922 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001923 mon->m_recvfd = pair[0];
1924 mon->m_sendfd = pair[1];
1925
1926 if (do_logfds) {
1927 if (pipe(pair) == -1)
1928 fatal("%s: pipe: %s", __func__, strerror(errno));
1929 FD_CLOSEONEXEC(pair[0]);
1930 FD_CLOSEONEXEC(pair[1]);
1931 mon->m_log_recvfd = pair[0];
1932 mon->m_log_sendfd = pair[1];
1933 } else
1934 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001935}
1936
1937#define MM_MEMSIZE 65536
1938
1939struct monitor *
1940monitor_init(void)
1941{
markus@openbsd.org091c3022015-01-19 19:52:16 +00001942 struct ssh *ssh = active_state; /* XXX */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001944
Damien Miller07d86be2006-03-26 14:19:21 +11001945 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001946
Damien Miller8f0bf232011-06-20 14:42:23 +10001947 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948
1949 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001950 if (options.compression) {
1951 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1952 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001953
Damien Miller2d6b8332002-06-21 15:59:49 +10001954 /* Compression needs to share state across borders */
markus@openbsd.org091c3022015-01-19 19:52:16 +00001955 ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
1956 (ssh_packet_comp_alloc_func *)mm_zalloc,
1957 (ssh_packet_comp_free_func *)mm_zfree);
Damien Miller2d6b8332002-06-21 15:59:49 +10001958 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001959
1960 return mon;
1961}
1962
1963void
1964monitor_reinit(struct monitor *mon)
1965{
Damien Miller8f0bf232011-06-20 14:42:23 +10001966 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001967}
Darren Tucker0efd1552003-08-26 11:49:55 +10001968
1969#ifdef GSSAPI
1970int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001971mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001972{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001973 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001974 OM_uint32 major;
1975 u_int len;
1976
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001977 goid.elements = buffer_get_string(m, &len);
1978 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001979
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001980 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001981
Darren Tuckera627d422013-06-02 07:31:17 +10001982 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001983
1984 buffer_clear(m);
1985 buffer_put_int(m, major);
1986
Damien Miller6fd6def2005-11-05 15:07:05 +11001987 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001988
1989 /* Now we have a context, enable the step */
1990 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1991
1992 return (0);
1993}
1994
1995int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001996mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001997{
1998 gss_buffer_desc in;
1999 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002000 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002001 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002002 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002003
Darren Tucker600ad8d2003-08-26 12:10:48 +10002004 in.value = buffer_get_string(m, &len);
2005 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002006 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002007 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002008
2009 buffer_clear(m);
2010 buffer_put_int(m, major);
2011 buffer_put_string(m, out.value, out.length);
2012 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002013 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002014
2015 gss_release_buffer(&minor, &out);
2016
Damien Miller6fd6def2005-11-05 15:07:05 +11002017 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002018 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2019 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002020 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002021 }
2022 return (0);
2023}
2024
2025int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002026mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002027{
2028 gss_buffer_desc gssbuf, mic;
2029 OM_uint32 ret;
2030 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002031
Damien Miller0425d402003-11-17 22:18:21 +11002032 gssbuf.value = buffer_get_string(m, &len);
2033 gssbuf.length = len;
2034 mic.value = buffer_get_string(m, &len);
2035 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002036
Damien Miller0425d402003-11-17 22:18:21 +11002037 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002038
Darren Tuckera627d422013-06-02 07:31:17 +10002039 free(gssbuf.value);
2040 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002041
Damien Miller0425d402003-11-17 22:18:21 +11002042 buffer_clear(m);
2043 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002044
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002045 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002046
Damien Miller0425d402003-11-17 22:18:21 +11002047 if (!GSS_ERROR(ret))
2048 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002049
Damien Miller0425d402003-11-17 22:18:21 +11002050 return (0);
2051}
2052
2053int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002054mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002055{
2056 int authenticated;
2057
2058 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2059
2060 buffer_clear(m);
2061 buffer_put_int(m, authenticated);
2062
2063 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002064 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002065
Damien Miller6fd6def2005-11-05 15:07:05 +11002066 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002067
2068 /* Monitor loop will terminate if authenticated */
2069 return (authenticated);
2070}
2071#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002072