blob: 6858478ca891a274732d98acd54ad6d601e2b2d0 [file] [log] [blame]
djm@openbsd.org141efe42015-01-14 20:05:27 +00001/* $OpenBSD: monitor.c,v 1.138 2015/01/14 20:05:27 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 Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.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 Millere7a1e5c2006-08-05 11:34:19 +100043#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100045#include <stdarg.h>
46#include <stdio.h>
Damien Miller607aede2006-09-01 15:48:19 +100047#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100048#ifdef HAVE_POLL_H
49#include <poll.h>
50#else
51# ifdef HAVE_SYS_POLL_H
52# include <sys/poll.h>
53# endif
54#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000055
56#ifdef SKEY
57#include <skey.h>
58#endif
59
Damien Miller1f0311c2014-05-15 14:24:09 +100060#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110061#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100062#endif
Damien Miller03e20032006-03-15 11:16:59 +110063
Damien Millerb84886b2008-05-19 15:05:07 +100064#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100065#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100066#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000067#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100068#include "key.h"
69#include "buffer.h"
70#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000073#include "kex.h"
74#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000075#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
76#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000077#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000078#define TARGET_OS_MAC 1
79#else
80#include "zlib.h"
81#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082#include "packet.h"
83#include "auth-options.h"
84#include "sshpty.h"
85#include "channels.h"
86#include "session.h"
87#include "sshlogin.h"
88#include "canohost.h"
89#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100090#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091#include "servconf.h"
92#include "monitor.h"
93#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100094#ifdef GSSAPI
95#include "ssh-gss.h"
96#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097#include "monitor_wrap.h"
98#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099#include "compat.h"
100#include "ssh2.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +1000101#include "roaming.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000102#include "authfd.h"
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000103#include "match.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +0000104#include "ssherr.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Darren Tucker0efd1552003-08-26 11:49:55 +1000106#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000107static Gssctxt *gsscontext = NULL;
108#endif
109
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110/* Imports */
111extern ServerOptions options;
112extern u_int utmp_len;
113extern Newkeys *current_keys[];
114extern z_stream incoming_stream;
115extern z_stream outgoing_stream;
116extern 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 */
122
123struct {
124 z_stream incoming;
125 z_stream outgoing;
126 u_char *keyin;
127 u_int keyinlen;
128 u_char *keyout;
129 u_int keyoutlen;
130 u_char *ivin;
131 u_int ivinlen;
132 u_char *ivout;
133 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000134 u_char *ssh1key;
135 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000136 int ssh1cipher;
137 int ssh1protoflags;
138 u_char *input;
139 u_int ilen;
140 u_char *output;
141 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000142 u_int64_t sent_bytes;
143 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144} child_state;
145
Damien Miller469954d2003-06-18 20:25:33 +1000146/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147
148int mm_answer_moduli(int, Buffer *);
149int mm_answer_sign(int, Buffer *);
150int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000151int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000152int mm_answer_authserv(int, Buffer *);
153int mm_answer_authpassword(int, Buffer *);
154int mm_answer_bsdauthquery(int, Buffer *);
155int mm_answer_bsdauthrespond(int, Buffer *);
156int mm_answer_skeyquery(int, Buffer *);
157int mm_answer_skeyrespond(int, Buffer *);
158int mm_answer_keyallowed(int, Buffer *);
159int mm_answer_keyverify(int, Buffer *);
160int mm_answer_pty(int, Buffer *);
161int mm_answer_pty_cleanup(int, Buffer *);
162int mm_answer_term(int, Buffer *);
163int mm_answer_rsa_keyallowed(int, Buffer *);
164int mm_answer_rsa_challenge(int, Buffer *);
165int mm_answer_rsa_response(int, Buffer *);
166int mm_answer_sesskey(int, Buffer *);
167int mm_answer_sessid(int, Buffer *);
168
Damien Miller79418552002-04-23 20:28:48 +1000169#ifdef USE_PAM
170int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000171int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172int mm_answer_pam_init_ctx(int, Buffer *);
173int mm_answer_pam_query(int, Buffer *);
174int mm_answer_pam_respond(int, Buffer *);
175int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000176#endif
177
Darren Tucker0efd1552003-08-26 11:49:55 +1000178#ifdef GSSAPI
179int mm_answer_gss_setup_ctx(int, Buffer *);
180int mm_answer_gss_accept_ctx(int, Buffer *);
181int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100182int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000183#endif
Damien Miller25162f22002-09-12 09:47:29 +1000184
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100185#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100186int mm_answer_audit_event(int, Buffer *);
187int mm_answer_audit_command(int, Buffer *);
188#endif
189
Damien Miller8f0bf232011-06-20 14:42:23 +1000190static int monitor_read_log(struct monitor *);
191
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000193
194#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000196#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197
198/* local state for key verify */
199static u_char *key_blob = NULL;
200static u_int key_bloblen = 0;
201static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000202static char *hostbased_cuser = NULL;
203static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000204static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100205static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000206static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000207static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000208static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000209
210struct mon_table {
211 enum monitor_reqtype type;
212 int flags;
213 int (*f)(int, Buffer *);
214};
215
216#define MON_ISAUTH 0x0004 /* Required for Authentication */
217#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
218#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100219#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000220
221#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
222
223#define MON_PERMIT 0x1000 /* Request is permitted */
224
225struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000226#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000228#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
230 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
231 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000232 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000233 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000234#ifdef USE_PAM
235 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000236 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000237 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
238 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
239 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
240 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000241#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100242#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100243 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100244#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245#ifdef BSD_AUTH
246 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100247 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000248#endif
249#ifdef SKEY
250 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
251 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
252#endif
253 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
254 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000255#ifdef GSSAPI
256 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
257 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
258 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100259 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000260#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000261 {0, 0, NULL}
262};
263
264struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000265#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000267#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
269 {MONITOR_REQ_PTY, 0, mm_answer_pty},
270 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
271 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100272#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100273 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
274 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
275#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 {0, 0, NULL}
277};
278
279struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000280#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000281 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
282 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
283 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
284 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100285 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
286 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
288 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
289#ifdef BSD_AUTH
290 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100291 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292#endif
293#ifdef SKEY
294 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
295 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
296#endif
Damien Millera33501b2002-05-08 12:24:42 +1000297#ifdef USE_PAM
298 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000299 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000300 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
301 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
302 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
303 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000304#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100305#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100306 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100307#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000308#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309 {0, 0, NULL}
310};
311
312struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000313#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000314 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
315 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
316 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100317#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100318 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000319 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100320#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000321#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322 {0, 0, NULL}
323};
324
325struct mon_table *mon_dispatch;
326
327/* Specifies if a certain message is allowed at the moment */
328
329static void
330monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
331{
332 while (ent->f != NULL) {
333 if (ent->type == type) {
334 ent->flags &= ~MON_PERMIT;
335 ent->flags |= permit ? MON_PERMIT : 0;
336 return;
337 }
338 ent++;
339 }
340}
341
342static void
343monitor_permit_authentications(int permit)
344{
345 struct mon_table *ent = mon_dispatch;
346
347 while (ent->f != NULL) {
348 if (ent->flags & MON_AUTH) {
349 ent->flags &= ~MON_PERMIT;
350 ent->flags |= permit ? MON_PERMIT : 0;
351 }
352 ent++;
353 }
354}
355
Darren Tucker3e33cec2003-10-02 16:12:36 +1000356void
357monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358{
359 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100360 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
362 debug3("preauth child monitor started");
363
Damien Miller8f0bf232011-06-20 14:42:23 +1000364 close(pmonitor->m_recvfd);
365 close(pmonitor->m_log_sendfd);
366 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
367
Darren Tucker3e33cec2003-10-02 16:12:36 +1000368 authctxt = _authctxt;
369 memset(authctxt, 0, sizeof(*authctxt));
370
Darren Tuckerde0de392005-03-31 23:52:04 +1000371 authctxt->loginmsg = &loginmsg;
372
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000373 if (compat20) {
374 mon_dispatch = mon_dispatch_proto20;
375
376 /* Permit requests for moduli and signatures */
377 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
378 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
379 } else {
380 mon_dispatch = mon_dispatch_proto15;
381
382 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
383 }
384
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385 /* The first few requests do not require asynchronous access */
386 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100387 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100388 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100389 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100390 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100391
392 /* Special handling for multiple required authentications */
393 if (options.num_auth_methods != 0) {
394 if (!compat20)
395 fatal("AuthenticationMethods is not supported"
396 "with SSH protocol 1");
397 if (authenticated &&
398 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000399 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100400 debug3("%s: method %s: partial", __func__,
401 auth_method);
402 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100403 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100404 }
405 }
406
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 if (authenticated) {
408 if (!(ent->flags & MON_AUTHDECIDE))
409 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000410 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411 if (authctxt->pw->pw_uid == 0 &&
412 !auth_root_allowed(auth_method))
413 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000414#ifdef USE_PAM
415 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100416 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000417 Buffer m;
418
419 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100420 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000421 MONITOR_REQ_PAM_ACCOUNT, &m);
422 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
423 buffer_free(&m);
424 }
425#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100427 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100428 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000429 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430 if (!authenticated)
431 authctxt->failures++;
432 }
433 }
434
435 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000436 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100437 if (strcmp(auth_method, "unknown") == 0)
438 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439
440 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000441 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000443 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000444
Damien Miller6a1937e2012-12-12 10:44:38 +1100445 /* Drain any buffered messages from the child */
446 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
447 ;
448
Damien Miller8f0bf232011-06-20 14:42:23 +1000449 close(pmonitor->m_sendfd);
450 close(pmonitor->m_log_recvfd);
451 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452}
453
Damien Millerbe64d432003-05-14 19:31:12 +1000454static void
455monitor_set_child_handler(pid_t pid)
456{
457 monitor_child_pid = pid;
458}
459
460static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000461monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000462{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000463 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000464}
465
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000467monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468{
Damien Miller8f0bf232011-06-20 14:42:23 +1000469 close(pmonitor->m_recvfd);
470 pmonitor->m_recvfd = -1;
471
Damien Millerbe64d432003-05-14 19:31:12 +1000472 monitor_set_child_handler(pmonitor->m_pid);
473 signal(SIGHUP, &monitor_child_handler);
474 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000475 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000476#ifdef SIGXFSZ
477 signal(SIGXFSZ, SIG_IGN);
478#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000479
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480 if (compat20) {
481 mon_dispatch = mon_dispatch_postauth20;
482
483 /* Permit requests for moduli and signatures */
484 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
485 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
486 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000487 } else {
488 mon_dispatch = mon_dispatch_postauth15;
489 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
490 }
491 if (!no_pty_flag) {
492 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
493 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
494 }
495
496 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000497 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498}
499
500void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000501monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000502{
Damien Miller2d6b8332002-06-21 15:59:49 +1000503 if (options.compression) {
504 /* The member allocation is not visible, so sync it */
505 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
506 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507}
508
Damien Miller8f0bf232011-06-20 14:42:23 +1000509static int
510monitor_read_log(struct monitor *pmonitor)
511{
512 Buffer logmsg;
513 u_int len, level;
514 char *msg;
515
516 buffer_init(&logmsg);
517
518 /* Read length */
519 buffer_append_space(&logmsg, 4);
520 if (atomicio(read, pmonitor->m_log_recvfd,
521 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
522 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100523 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000524 debug("%s: child log fd closed", __func__);
525 close(pmonitor->m_log_recvfd);
526 pmonitor->m_log_recvfd = -1;
527 return -1;
528 }
529 fatal("%s: log fd read: %s", __func__, strerror(errno));
530 }
531 len = buffer_get_int(&logmsg);
532 if (len <= 4 || len > 8192)
533 fatal("%s: invalid log message length %u", __func__, len);
534
535 /* Read severity, message */
536 buffer_clear(&logmsg);
537 buffer_append_space(&logmsg, len);
538 if (atomicio(read, pmonitor->m_log_recvfd,
539 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
540 fatal("%s: log fd read: %s", __func__, strerror(errno));
541
542 /* Log it */
543 level = buffer_get_int(&logmsg);
544 msg = buffer_get_string(&logmsg, NULL);
545 if (log_level_name(level) == NULL)
546 fatal("%s: invalid log level %u (corrupted message?)",
547 __func__, level);
548 do_log2(level, "%s [preauth]", msg);
549
550 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000551 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000552
553 return 0;
554}
555
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000557monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 struct mon_table **pent)
559{
560 Buffer m;
561 int ret;
562 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000563 struct pollfd pfd[2];
564
565 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100566 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000567 pfd[0].fd = pmonitor->m_sendfd;
568 pfd[0].events = POLLIN;
569 pfd[1].fd = pmonitor->m_log_recvfd;
570 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000571 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
572 if (errno == EINTR || errno == EAGAIN)
573 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000574 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000575 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000576 if (pfd[1].revents) {
577 /*
578 * Drain all log messages before processing next
579 * monitor request.
580 */
581 monitor_read_log(pmonitor);
582 continue;
583 }
584 if (pfd[0].revents)
585 break; /* Continues below */
586 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
588 buffer_init(&m);
589
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000590 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591 type = buffer_get_char(&m);
592
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000593 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
595 while (ent->f != NULL) {
596 if (ent->type == type)
597 break;
598 ent++;
599 }
600
601 if (ent->f != NULL) {
602 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000603 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000605 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 buffer_free(&m);
607
608 /* The child may use this request only once, disable it */
609 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000610 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611 type);
612 ent->flags &= ~MON_PERMIT;
613 }
614
615 if (pent != NULL)
616 *pent = ent;
617
618 return ret;
619 }
620
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000621 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622
623 /* NOTREACHED */
624 return (-1);
625}
626
627/* allowed key state */
628static int
629monitor_allowed_key(u_char *blob, u_int bloblen)
630{
631 /* make sure key is allowed */
632 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000633 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 return (0);
635 return (1);
636}
637
638static void
639monitor_reset_key_state(void)
640{
641 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000642 free(key_blob);
643 free(hostbased_cuser);
644 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645 key_blob = NULL;
646 key_bloblen = 0;
647 key_blobtype = MM_NOKEY;
648 hostbased_cuser = NULL;
649 hostbased_chost = NULL;
650}
651
Damien Miller1f0311c2014-05-15 14:24:09 +1000652#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000654mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655{
656 DH *dh;
657 int min, want, max;
658
659 min = buffer_get_int(m);
660 want = buffer_get_int(m);
661 max = buffer_get_int(m);
662
663 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000664 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665 /* We need to check here, too, in case the child got corrupted */
666 if (max < min || want < min || max < want)
667 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000668 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669
670 buffer_clear(m);
671
672 dh = choose_dh(min, want, max);
673 if (dh == NULL) {
674 buffer_put_char(m, 0);
675 return (0);
676 } else {
677 /* Send first bignum */
678 buffer_put_char(m, 1);
679 buffer_put_bignum2(m, dh->p);
680 buffer_put_bignum2(m, dh->g);
681
682 DH_free(dh);
683 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685 return (0);
686}
Damien Miller1f0311c2014-05-15 14:24:09 +1000687#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688
689int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000690mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000692 extern int auth_sock; /* XXX move to state struct? */
693 struct sshkey *key;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694 u_char *p;
695 u_char *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000696 size_t datlen, siglen;
697 int r, keyid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000698
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000699 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
djm@openbsd.org141efe42015-01-14 20:05:27 +0000701 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
702 (r = sshbuf_get_string(m, &p, &datlen)) != 0)
703 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
Damien Millera63128d2006-03-15 12:08:28 +1100705 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000706 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
707 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100708 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000709 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000710 fatal("%s: data length incorrect: %zu", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000711
Ben Lindstromf67e0772002-06-06 20:58:19 +0000712 /* save session id, it will be passed on the first call */
713 if (session_id2_len == 0) {
714 session_id2_len = datlen;
715 session_id2 = xmalloc(session_id2_len);
716 memcpy(session_id2, p, session_id2_len);
717 }
718
Damien Miller85b45e02013-07-20 13:21:52 +1000719 if ((key = get_hostkey_by_index(keyid)) != NULL) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000720 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen,
721 datafellows)) != 0)
722 fatal("%s: sshkey_sign failed: %s",
723 __func__, ssh_err(r));
Damien Miller85b45e02013-07-20 13:21:52 +1000724 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
djm@openbsd.org141efe42015-01-14 20:05:27 +0000725 auth_sock > 0) {
726 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
727 p, datlen, datafellows)) != 0) {
728 fatal("%s: ssh_agent_sign failed: %s",
729 __func__, ssh_err(r));
730 }
Damien Miller85b45e02013-07-20 13:21:52 +1000731 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000732 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
djm@openbsd.org141efe42015-01-14 20:05:27 +0000734 debug3("%s: signature %p(%zu)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
djm@openbsd.org141efe42015-01-14 20:05:27 +0000736 sshbuf_reset(m);
737 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
738 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
Darren Tuckera627d422013-06-02 07:31:17 +1000740 free(p);
741 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000743 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744
745 /* Turn on permissions for getpwnam */
746 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
747
748 return (0);
749}
750
751/* Retrieves the password entry and also checks if the user is permitted */
752
753int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000754mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000756 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 struct passwd *pwent;
758 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000759 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000761 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762
763 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000764 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765
Darren Tuckerb09b6772004-06-22 15:06:46 +1000766 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767
Darren Tuckerb09b6772004-06-22 15:06:46 +1000768 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769
Darren Tuckerb09b6772004-06-22 15:06:46 +1000770 authctxt->user = xstrdup(username);
771 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000772 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
774 buffer_clear(m);
775
776 if (pwent == NULL) {
777 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100778 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779 goto out;
780 }
781
782 allowed = 1;
783 authctxt->pw = pwent;
784 authctxt->valid = 1;
785
786 buffer_put_char(m, 1);
787 buffer_put_string(m, pwent, sizeof(struct passwd));
788 buffer_put_cstring(m, pwent->pw_name);
789 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000790#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000792#endif
793#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000795#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000796 buffer_put_cstring(m, pwent->pw_dir);
797 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100798
799 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100800 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000801
802#define M_CP_STROPT(x) do { \
803 if (options.x != NULL) \
804 buffer_put_cstring(m, options.x); \
805 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000806#define M_CP_STRARRAYOPT(x, nx) do { \
807 for (i = 0; i < options.nx; i++) \
808 buffer_put_cstring(m, options.x[i]); \
809 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000810 /* See comment in servconf.h */
811 COPY_MATCH_STRING_OPTS();
812#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000813#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100814
815 /* Create valid auth method lists */
816 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
817 /*
818 * The monitor will continue long enough to let the child
819 * run to it's packet_disconnect(), but it must not allow any
820 * authentication to succeed.
821 */
822 debug("%s: no valid authentication method lists", __func__);
823 }
824
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000825 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000826 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827
828 /* For SSHv1 allow authentication now */
829 if (!compat20)
830 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000831 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832 /* Allow service/style information on the auth context */
833 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000834 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
835 }
Damien Millera33501b2002-05-08 12:24:42 +1000836#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000837 if (options.use_pam)
838 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000839#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840
841 return (0);
842}
843
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000844int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000845{
846 char *banner;
847
848 buffer_clear(m);
849 banner = auth2_read_banner();
850 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000851 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000852 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000853
854 return (0);
855}
856
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000858mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000859{
860 monitor_permit_authentications(1);
861
862 authctxt->service = buffer_get_string(m, NULL);
863 authctxt->style = buffer_get_string(m, NULL);
864 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000865 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866
867 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000868 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869 authctxt->style = NULL;
870 }
871
872 return (0);
873}
874
875int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000876mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000877{
878 static int call_count;
879 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000880 int authenticated;
881 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882
883 passwd = buffer_get_string(m, &plen);
884 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000885 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000886 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100887 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000888 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
890 buffer_clear(m);
891 buffer_put_int(m, authenticated);
892
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000893 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000894 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000895
896 call_count++;
897 if (plen == 0 && call_count == 1)
898 auth_method = "none";
899 else
900 auth_method = "password";
901
902 /* Causes monitor loop to terminate if authenticated */
903 return (authenticated);
904}
905
906#ifdef BSD_AUTH
907int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000908mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909{
910 char *name, *infotxt;
911 u_int numprompts;
912 u_int *echo_on;
913 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100914 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915
Damien Millerb7df3af2003-02-24 11:55:46 +1100916 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
917 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918
919 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100920 buffer_put_int(m, success);
921 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922 buffer_put_cstring(m, prompts[0]);
923
Damien Millerb7df3af2003-02-24 11:55:46 +1100924 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
Damien Millerb7df3af2003-02-24 11:55:46 +1100927 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000928 free(name);
929 free(infotxt);
930 free(prompts);
931 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 }
933
934 return (0);
935}
936
937int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000938mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939{
940 char *response;
941 int authok;
942
943 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000947 authok = options.challenge_response_authentication &&
948 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000949 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000950 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000951 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952
953 buffer_clear(m);
954 buffer_put_int(m, authok);
955
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000956 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000957 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958
Damien Miller91a55f22013-04-23 15:18:10 +1000959 if (compat20) {
960 auth_method = "keyboard-interactive";
961 auth_submethod = "bsdauth";
962 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100963 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 return (authok != 0);
966}
967#endif
968
969#ifdef SKEY
970int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000971mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972{
973 struct skey skey;
974 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100975 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000977 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
978 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
980 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100981 buffer_put_int(m, success);
982 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983 buffer_put_cstring(m, challenge);
984
Damien Millerb7df3af2003-02-24 11:55:46 +1100985 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000986 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 return (0);
989}
990
991int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000992mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993{
994 char *response;
995 int authok;
996
997 response = buffer_get_string(m, NULL);
998
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000999 authok = (options.challenge_response_authentication &&
1000 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001 skey_haskey(authctxt->pw->pw_name) == 0 &&
1002 skey_passcheck(authctxt->pw->pw_name, response) != -1);
1003
Darren Tuckerf60845f2013-06-02 08:07:31 +10001004 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
1006 buffer_clear(m);
1007 buffer_put_int(m, authok);
1008
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001009 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001010 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
1012 auth_method = "skey";
1013
1014 return (authok != 0);
1015}
1016#endif
1017
Damien Miller79418552002-04-23 20:28:48 +10001018#ifdef USE_PAM
1019int
Darren Tucker7eda5922004-06-22 13:22:50 +10001020mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001021{
Damien Miller4e448a32003-05-14 15:11:48 +10001022 if (!options.use_pam)
1023 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1024
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001025 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001026
Damien Miller1f499fd2003-08-25 13:08:49 +10001027 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1028
Damien Miller79418552002-04-23 20:28:48 +10001029 return (0);
1030}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001031
Damien Miller1f499fd2003-08-25 13:08:49 +10001032int
Darren Tucker7eda5922004-06-22 13:22:50 +10001033mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001034{
1035 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001036
Damien Miller1f499fd2003-08-25 13:08:49 +10001037 if (!options.use_pam)
1038 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1039
1040 ret = do_pam_account();
1041
1042 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001043 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001044
Darren Tucker7eda5922004-06-22 13:22:50 +10001045 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001046
1047 return (ret);
1048}
1049
Damien Miller4f9f42a2003-05-10 19:28:02 +10001050static void *sshpam_ctxt, *sshpam_authok;
1051extern KbdintDevice sshpam_device;
1052
1053int
Darren Tucker7eda5922004-06-22 13:22:50 +10001054mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001055{
1056
1057 debug3("%s", __func__);
1058 authctxt->user = buffer_get_string(m, NULL);
1059 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1060 sshpam_authok = NULL;
1061 buffer_clear(m);
1062 if (sshpam_ctxt != NULL) {
1063 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1064 buffer_put_int(m, 1);
1065 } else {
1066 buffer_put_int(m, 0);
1067 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001068 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001069 return (0);
1070}
1071
1072int
Darren Tucker7eda5922004-06-22 13:22:50 +10001073mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001074{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001075 char *name = NULL, *info = NULL, **prompts = NULL;
1076 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001077 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001078
1079 debug3("%s", __func__);
1080 sshpam_authok = NULL;
1081 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1082 if (ret == 0 && num == 0)
1083 sshpam_authok = sshpam_ctxt;
1084 if (num > 1 || name == NULL || info == NULL)
1085 ret = -1;
1086 buffer_clear(m);
1087 buffer_put_int(m, ret);
1088 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001089 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001091 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 buffer_put_int(m, num);
1093 for (i = 0; i < num; ++i) {
1094 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001095 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096 buffer_put_int(m, echo_on[i]);
1097 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001098 free(prompts);
1099 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001100 auth_method = "keyboard-interactive";
1101 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001102 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103 return (0);
1104}
1105
1106int
Darren Tucker7eda5922004-06-22 13:22:50 +10001107mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108{
1109 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001110 u_int i, num;
1111 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001112
1113 debug3("%s", __func__);
1114 sshpam_authok = NULL;
1115 num = buffer_get_int(m);
1116 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001117 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001118 for (i = 0; i < num; ++i)
1119 resp[i] = buffer_get_string(m, NULL);
1120 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1121 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001122 free(resp[i]);
1123 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001124 } else {
1125 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1126 }
1127 buffer_clear(m);
1128 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001129 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001130 auth_method = "keyboard-interactive";
1131 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001132 if (ret == 0)
1133 sshpam_authok = sshpam_ctxt;
1134 return (0);
1135}
1136
1137int
Darren Tucker7eda5922004-06-22 13:22:50 +10001138mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001139{
1140
1141 debug3("%s", __func__);
1142 (sshpam_device.free_ctx)(sshpam_ctxt);
1143 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001144 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001145 auth_method = "keyboard-interactive";
1146 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001147 return (sshpam_authok == sshpam_ctxt);
1148}
Damien Miller79418552002-04-23 20:28:48 +10001149#endif
1150
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001152mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001153{
1154 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001155 char *cuser, *chost;
1156 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001157 u_int bloblen;
1158 enum mm_keytype type = 0;
1159 int allowed = 0;
1160
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001161 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162
1163 type = buffer_get_int(m);
1164 cuser = buffer_get_string(m, NULL);
1165 chost = buffer_get_string(m, NULL);
1166 blob = buffer_get_string(m, &bloblen);
1167
1168 key = key_from_blob(blob, bloblen);
1169
1170 if ((compat20 && type == MM_RSAHOSTKEY) ||
1171 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001172 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001173
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001174 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175
Damien Miller3e3b5142003-11-17 21:13:40 +11001176 if (key != NULL && authctxt->valid) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001177 /* These should not make it past the privsep child */
1178 if (key_type_plain(key->type) == KEY_RSA &&
1179 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1180 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1181
Darren Tucker47eede72005-03-14 23:08:12 +11001182 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001184 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001185 !auth2_userkey_already_used(authctxt, key) &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001186 match_pattern_list(sshkey_ssh_name(key),
1187 options.pubkey_key_types,
1188 strlen(options.pubkey_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001189 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001190 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001191 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001192 if (options.pubkey_authentication && allowed != 1)
1193 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 break;
1195 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001196 allowed = options.hostbased_authentication &&
djm@openbsd.org1f729f02015-01-13 07:39:19 +00001197 match_pattern_list(sshkey_ssh_name(key),
1198 options.hostbased_key_types,
1199 strlen(options.hostbased_key_types), 0) == 1 &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001200 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001201 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001202 pubkey_auth_info(authctxt, key,
1203 "client user \"%.100s\", client host \"%.100s\"",
1204 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001205 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001207#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208 case MM_RSAHOSTKEY:
1209 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001210 allowed = options.rhosts_rsa_authentication &&
1211 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001213 if (options.rhosts_rsa_authentication && allowed != 1)
1214 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001215 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001218 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001219 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001220 break;
1221 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 }
Damien Miller00111382003-03-10 11:21:17 +11001223 if (key != NULL)
1224 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
1226 /* clear temporarily storage (used by verify) */
1227 monitor_reset_key_state();
1228
1229 if (allowed) {
1230 /* Save temporarily for comparison in verify */
1231 key_blob = blob;
1232 key_bloblen = bloblen;
1233 key_blobtype = type;
1234 hostbased_cuser = cuser;
1235 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001236 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001237 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001238 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001239 free(blob);
1240 free(cuser);
1241 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001242 }
1243
1244 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001245 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246
1247 buffer_clear(m);
1248 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001249 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001251 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252
1253 if (type == MM_RSAHOSTKEY)
1254 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1255
1256 return (0);
1257}
1258
1259static int
1260monitor_valid_userblob(u_char *data, u_int datalen)
1261{
1262 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001263 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264 u_int len;
1265 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266
1267 buffer_init(&b);
1268 buffer_append(&b, data, datalen);
1269
1270 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001271 p = buffer_ptr(&b);
1272 len = buffer_len(&b);
1273 if ((session_id2 == NULL) ||
1274 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001275 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001276 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 buffer_consume(&b, session_id2_len);
1278 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001279 p = buffer_get_string(&b, &len);
1280 if ((session_id2 == NULL) ||
1281 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001282 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001284 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 }
1286 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1287 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001288 p = buffer_get_cstring(&b, NULL);
1289 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1290 authctxt->style ? ":" : "",
1291 authctxt->style ? authctxt->style : "");
1292 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001293 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001294 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 fail++;
1296 }
Darren Tuckera627d422013-06-02 07:31:17 +10001297 free(userstyle);
1298 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 buffer_skip_string(&b);
1300 if (datafellows & SSH_BUG_PKAUTH) {
1301 if (!buffer_get_char(&b))
1302 fail++;
1303 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001304 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 if (strcmp("publickey", p) != 0)
1306 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001307 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 if (!buffer_get_char(&b))
1309 fail++;
1310 buffer_skip_string(&b);
1311 }
1312 buffer_skip_string(&b);
1313 if (buffer_len(&b) != 0)
1314 fail++;
1315 buffer_free(&b);
1316 return (fail == 0);
1317}
1318
1319static int
Damien Millera10f5612002-09-12 09:49:15 +10001320monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1321 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322{
1323 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001324 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325 u_int len;
1326 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001327
1328 buffer_init(&b);
1329 buffer_append(&b, data, datalen);
1330
Ben Lindstromf67e0772002-06-06 20:58:19 +00001331 p = buffer_get_string(&b, &len);
1332 if ((session_id2 == NULL) ||
1333 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001334 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001336 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001337
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1339 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001340 p = buffer_get_cstring(&b, NULL);
1341 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1342 authctxt->style ? ":" : "",
1343 authctxt->style ? authctxt->style : "");
1344 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001345 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001346 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347 fail++;
1348 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001349 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001350 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001352 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353 if (strcmp(p, "hostbased") != 0)
1354 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001355 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356 buffer_skip_string(&b); /* pkalg */
1357 buffer_skip_string(&b); /* pkblob */
1358
1359 /* verify client host, strip trailing dot if necessary */
1360 p = buffer_get_string(&b, NULL);
1361 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1362 p[len - 1] = '\0';
1363 if (strcmp(p, chost) != 0)
1364 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366
1367 /* verify client user */
1368 p = buffer_get_string(&b, NULL);
1369 if (strcmp(p, cuser) != 0)
1370 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001371 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 if (buffer_len(&b) != 0)
1374 fail++;
1375 buffer_free(&b);
1376 return (fail == 0);
1377}
1378
1379int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001380mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381{
1382 Key *key;
1383 u_char *signature, *data, *blob;
1384 u_int signaturelen, datalen, bloblen;
1385 int verified = 0;
1386 int valid_data = 0;
1387
1388 blob = buffer_get_string(m, &bloblen);
1389 signature = buffer_get_string(m, &signaturelen);
1390 data = buffer_get_string(m, &datalen);
1391
1392 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001393 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001394 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 key = key_from_blob(blob, bloblen);
1397 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001398 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399
1400 switch (key_blobtype) {
1401 case MM_USERKEY:
1402 valid_data = monitor_valid_userblob(data, datalen);
1403 break;
1404 case MM_HOSTKEY:
1405 valid_data = monitor_valid_hostbasedblob(data, datalen,
1406 hostbased_cuser, hostbased_chost);
1407 break;
1408 default:
1409 valid_data = 0;
1410 break;
1411 }
1412 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001413 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
1415 verified = key_verify(key, signature, signaturelen, data, datalen);
1416 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001417 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001419 /* If auth was successful then record key to ensure it isn't reused */
1420 if (verified == 1)
1421 auth2_record_userkey(authctxt, key);
1422 else
1423 key_free(key);
1424
Darren Tuckera627d422013-06-02 07:31:17 +10001425 free(blob);
1426 free(signature);
1427 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
Ben Lindstrome1c09122002-06-23 00:38:24 +00001429 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1430
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 monitor_reset_key_state();
1432
1433 buffer_clear(m);
1434 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001435 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436
Darren Tuckerfbba7352006-11-07 23:16:08 +11001437 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438}
1439
1440static void
1441mm_record_login(Session *s, struct passwd *pw)
1442{
1443 socklen_t fromlen;
1444 struct sockaddr_storage from;
1445
1446 /*
1447 * Get IP address of client. If the connection is not a socket, let
1448 * the address be 0.0.0.0.
1449 */
1450 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001451 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001454 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001456 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 }
1458 }
1459 /* Record that there was a login on that tty from the remote host. */
1460 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001461 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001462 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463}
1464
1465static void
1466mm_session_close(Session *s)
1467{
Damien Miller04bd8b02003-05-25 14:38:33 +10001468 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001469 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001470 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471 session_pty_cleanup2(s);
1472 }
Damien Miller7207f642008-05-19 15:34:50 +10001473 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474}
1475
1476int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001477mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001479 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480 Session *s;
1481 int res, fd0;
1482
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001483 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484
1485 buffer_clear(m);
1486 s = session_new();
1487 if (s == NULL)
1488 goto error;
1489 s->authctxt = authctxt;
1490 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001491 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1493 if (res == 0)
1494 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 pty_setowner(authctxt->pw, s->tty);
1496
1497 buffer_put_int(m, 1);
1498 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499
1500 /* We need to trick ttyslot */
1501 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001502 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503
1504 mm_record_login(s, authctxt->pw);
1505
1506 /* Now we can close the file descriptor again */
1507 close(0);
1508
Darren Tucker09991742004-07-17 17:05:14 +10001509 /* send messages generated by record_login */
1510 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1511 buffer_clear(&loginmsg);
1512
1513 mm_request_send(sock, MONITOR_ANS_PTY, m);
1514
Damien Miller54fd7cf2007-09-17 12:04:08 +10001515 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1516 mm_send_fd(sock, s->ttyfd) == -1)
1517 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001518
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519 /* make sure nothing uses fd 0 */
1520 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001521 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001523 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524
1525 /* slave is not needed */
1526 close(s->ttyfd);
1527 s->ttyfd = s->ptyfd;
1528 /* no need to dup() because nobody closes ptyfd */
1529 s->ptymaster = s->ptyfd;
1530
Damien Miller9ab00b42006-08-05 12:40:11 +10001531 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 return (0);
1534
1535 error:
1536 if (s != NULL)
1537 mm_session_close(s);
1538 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001539 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540 return (0);
1541}
1542
1543int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001544mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545{
1546 Session *s;
1547 char *tty;
1548
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001549 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550
1551 tty = buffer_get_string(m, NULL);
1552 if ((s = session_by_tty(tty)) != NULL)
1553 mm_session_close(s);
1554 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001555 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556 return (0);
1557}
1558
Damien Miller1f0311c2014-05-15 14:24:09 +10001559#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001560int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001561mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562{
1563 BIGNUM *p;
1564 int rsafail;
1565
1566 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001567 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001568
1569 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001570 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571
1572 buffer_get_bignum2(m, p);
1573
1574 rsafail = ssh1_session_key(p);
1575
1576 buffer_clear(m);
1577 buffer_put_int(m, rsafail);
1578 buffer_put_bignum2(m, p);
1579
1580 BN_clear_free(p);
1581
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001582 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001583
1584 /* Turn on permissions for sessid passing */
1585 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1586
1587 return (0);
1588}
1589
1590int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001591mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592{
1593 int i;
1594
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001595 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001596
1597 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001598 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 for (i = 0; i < 16; i++)
1600 session_id[i] = buffer_get_char(m);
1601
1602 /* Turn on permissions for getpwnam */
1603 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1604
1605 return (0);
1606}
1607
1608int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001609mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610{
1611 BIGNUM *client_n;
1612 Key *key = NULL;
1613 u_char *blob = NULL;
1614 u_int blen = 0;
1615 int allowed = 0;
1616
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001617 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618
Damien Miller7a8f5b32006-03-31 23:14:23 +11001619 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001620 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001621 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001622 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623 buffer_get_bignum2(m, client_n);
1624 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1625 BN_clear_free(client_n);
1626 }
1627 buffer_clear(m);
1628 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001629 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630
1631 /* clear temporarily storage (used by generate challenge) */
1632 monitor_reset_key_state();
1633
1634 if (allowed && key != NULL) {
1635 key->type = KEY_RSA; /* cheat for key_to_blob */
1636 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001637 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 buffer_put_string(m, blob, blen);
1639
1640 /* Save temporarily for comparison in verify */
1641 key_blob = blob;
1642 key_bloblen = blen;
1643 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644 }
Damien Miller00111382003-03-10 11:21:17 +11001645 if (key != NULL)
1646 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001647
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001648 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649
1650 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1651 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1652 return (0);
1653}
1654
1655int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001656mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657{
1658 Key *key = NULL;
1659 u_char *blob;
1660 u_int blen;
1661
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001662 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663
1664 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 blob = buffer_get_string(m, &blen);
1667 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001670 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001672 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001673 if (key->type != KEY_RSA)
1674 fatal("%s: received bad key type %d", __func__, key->type);
1675 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001676 if (ssh1_challenge)
1677 BN_clear_free(ssh1_challenge);
1678 ssh1_challenge = auth_rsa_generate_challenge(key);
1679
1680 buffer_clear(m);
1681 buffer_put_bignum2(m, ssh1_challenge);
1682
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001683 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001684 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685
1686 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001687
Darren Tuckera627d422013-06-02 07:31:17 +10001688 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001689 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690 return (0);
1691}
1692
1693int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001694mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695{
1696 Key *key = NULL;
1697 u_char *blob, *response;
1698 u_int blen, len;
1699 int success;
1700
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001701 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001702
1703 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001704 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001706 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707
1708 blob = buffer_get_string(m, &blen);
1709 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001710 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001712 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001713 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001714 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 response = buffer_get_string(m, &len);
1716 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001717 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1719
Darren Tuckera627d422013-06-02 07:31:17 +10001720 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001721 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001722 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723
1724 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1725
1726 /* reset state */
1727 BN_clear_free(ssh1_challenge);
1728 ssh1_challenge = NULL;
1729 monitor_reset_key_state();
1730
1731 buffer_clear(m);
1732 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001733 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001734
1735 return (success);
1736}
Damien Miller1f0311c2014-05-15 14:24:09 +10001737#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738
1739int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001740mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001742 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743 int res, status;
1744
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001745 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746
1747 /* The child is terminating */
1748 session_destroy_all(&mm_session_close);
1749
Darren Tucker52358d62008-03-11 22:58:25 +11001750#ifdef USE_PAM
1751 if (options.use_pam)
1752 sshpam_cleanup();
1753#endif
1754
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001755 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001756 if (errno != EINTR)
1757 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758
1759 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1760
1761 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001762 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001763}
1764
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001765#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001766/* Report that an audit event occurred */
1767int
1768mm_answer_audit_event(int socket, Buffer *m)
1769{
1770 ssh_audit_event_t event;
1771
1772 debug3("%s entering", __func__);
1773
1774 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001775 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001776 case SSH_AUTH_FAIL_PUBKEY:
1777 case SSH_AUTH_FAIL_HOSTBASED:
1778 case SSH_AUTH_FAIL_GSSAPI:
1779 case SSH_LOGIN_EXCEED_MAXTRIES:
1780 case SSH_LOGIN_ROOT_DENIED:
1781 case SSH_CONNECTION_CLOSE:
1782 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001783 audit_event(event);
1784 break;
1785 default:
1786 fatal("Audit event type %d not permitted", event);
1787 }
1788
1789 return (0);
1790}
1791
1792int
1793mm_answer_audit_command(int socket, Buffer *m)
1794{
1795 u_int len;
1796 char *cmd;
1797
1798 debug3("%s entering", __func__);
1799 cmd = buffer_get_string(m, &len);
1800 /* sanity check command, if so how? */
1801 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001802 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001803 return (0);
1804}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001805#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001806
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001808monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001809{
1810 if (compat20) {
1811 set_newkeys(MODE_IN);
1812 set_newkeys(MODE_OUT);
1813 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001814 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001815 packet_set_encryption_key(child_state.ssh1key,
1816 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001817 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818 }
1819
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001820 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001821 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001822 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001823 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001824 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825
1826 if (!compat20) {
1827 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001828 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001829 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001830 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001831 }
1832
1833 memcpy(&incoming_stream, &child_state.incoming,
1834 sizeof(incoming_stream));
1835 memcpy(&outgoing_stream, &child_state.outgoing,
1836 sizeof(outgoing_stream));
1837
1838 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001839 if (options.compression)
1840 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001841
Damien Millerc31a0cd2014-05-15 14:37:39 +10001842 packet_set_postauth();
1843
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001844 if (options.rekey_limit || options.rekey_interval)
1845 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1846 (time_t)options.rekey_interval);
1847
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001848 /* Network I/O buffers */
1849 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001850 buffer_clear(packet_get_input());
1851 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Damien Millera5103f42014-02-04 11:20:14 +11001852 explicit_bzero(child_state.input, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001853 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001854
Darren Tuckerf7288d72009-06-21 18:12:20 +10001855 buffer_clear(packet_get_output());
1856 buffer_append(packet_get_output(), child_state.output,
1857 child_state.olen);
Damien Millera5103f42014-02-04 11:20:14 +11001858 explicit_bzero(child_state.output, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001859 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001860
1861 /* Roaming */
1862 if (compat20)
1863 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001864}
1865
1866static Kex *
1867mm_get_kex(Buffer *m)
1868{
1869 Kex *kex;
1870 void *blob;
1871 u_int bloblen;
1872
Damien Miller07d86be2006-03-26 14:19:21 +11001873 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001874 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001875 if (session_id2 == NULL ||
1876 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001877 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001878 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001879 kex->we_need = buffer_get_int(m);
Damien Miller1f0311c2014-05-15 14:24:09 +10001880#ifdef WITH_OPENSSL
Damien Millerb062c292003-03-24 09:12:09 +11001881 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001882 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001883 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001884 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001885 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Damien Miller1f0311c2014-05-15 14:24:09 +10001886#endif
Damien Millerc9831972013-11-07 12:00:23 +11001887 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001888 kex->server = 1;
1889 kex->hostkey_type = buffer_get_int(m);
1890 kex->kex_type = buffer_get_int(m);
1891 blob = buffer_get_string(m, &bloblen);
1892 buffer_init(&kex->my);
1893 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001894 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895 blob = buffer_get_string(m, &bloblen);
1896 buffer_init(&kex->peer);
1897 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001898 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001899 kex->done = 1;
1900 kex->flags = buffer_get_int(m);
1901 kex->client_version_string = buffer_get_string(m, NULL);
1902 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001903 kex->load_host_public_key=&get_hostkey_public_by_type;
1904 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001906 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001907
1908 return (kex);
1909}
1910
1911/* This function requries careful sanity checking */
1912
1913void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001914mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001915{
1916 Buffer m;
1917 u_char *blob, *p;
1918 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001919 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001920 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001921
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001922 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001923
1924 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001925 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001926 if (!compat20) {
1927 child_state.ssh1protoflags = buffer_get_int(&m);
1928 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001929 child_state.ssh1key = buffer_get_string(&m,
1930 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001931 child_state.ivout = buffer_get_string(&m,
1932 &child_state.ivoutlen);
1933 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1934 goto skip;
1935 } else {
1936 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001937 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001938 }
1939
1940 blob = buffer_get_string(&m, &bloblen);
1941 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001942 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001944 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001945 blob = buffer_get_string(&m, &bloblen);
1946 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001947 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948
1949 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001950 seqnr = buffer_get_int(&m);
1951 blocks = buffer_get_int64(&m);
1952 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001953 bytes = buffer_get_int64(&m);
1954 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001955 seqnr = buffer_get_int(&m);
1956 blocks = buffer_get_int64(&m);
1957 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001958 bytes = buffer_get_int64(&m);
1959 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001960
1961 skip:
1962 /* Get the key context */
1963 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1964 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1965
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001966 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001967 /* Get compression state */
1968 p = buffer_get_string(&m, &plen);
1969 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001970 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001971 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001972 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001973
1974 p = buffer_get_string(&m, &plen);
1975 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001976 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001977 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001978 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001979
1980 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001981 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001982 child_state.input = buffer_get_string(&m, &child_state.ilen);
1983 child_state.output = buffer_get_string(&m, &child_state.olen);
1984
Darren Tuckerc5564e12009-06-21 18:53:53 +10001985 /* Roaming */
1986 if (compat20) {
1987 child_state.sent_bytes = buffer_get_int64(&m);
1988 child_state.recv_bytes = buffer_get_int64(&m);
1989 }
1990
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001991 buffer_free(&m);
1992}
1993
1994
1995/* Allocation functions for zlib */
1996void *
1997mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1998{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001999 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002000 void *address;
2001
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00002002 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10002003 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
2004
2005 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002006
2007 return (address);
2008}
2009
2010void
2011mm_zfree(struct mm_master *mm, void *address)
2012{
2013 mm_free(mm, address);
2014}
2015
2016void
2017mm_init_compression(struct mm_master *mm)
2018{
2019 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
2020 outgoing_stream.zfree = (free_func)mm_zfree;
2021 outgoing_stream.opaque = mm;
2022
2023 incoming_stream.zalloc = (alloc_func)mm_zalloc;
2024 incoming_stream.zfree = (free_func)mm_zfree;
2025 incoming_stream.opaque = mm;
2026}
2027
2028/* XXX */
2029
2030#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10002031 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002032 fatal("fcntl(%d, F_SETFD)", x); \
2033} while (0)
2034
2035static void
Damien Miller8f0bf232011-06-20 14:42:23 +10002036monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002037{
Damien Miller8f0bf232011-06-20 14:42:23 +10002038 int pair[2];
2039
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002040 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002041 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002042 FD_CLOSEONEXEC(pair[0]);
2043 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002044 mon->m_recvfd = pair[0];
2045 mon->m_sendfd = pair[1];
2046
2047 if (do_logfds) {
2048 if (pipe(pair) == -1)
2049 fatal("%s: pipe: %s", __func__, strerror(errno));
2050 FD_CLOSEONEXEC(pair[0]);
2051 FD_CLOSEONEXEC(pair[1]);
2052 mon->m_log_recvfd = pair[0];
2053 mon->m_log_sendfd = pair[1];
2054 } else
2055 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002056}
2057
2058#define MM_MEMSIZE 65536
2059
2060struct monitor *
2061monitor_init(void)
2062{
2063 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002064
Damien Miller07d86be2006-03-26 14:19:21 +11002065 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002066
Damien Miller8f0bf232011-06-20 14:42:23 +10002067 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002068
2069 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002070 if (options.compression) {
2071 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2072 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002073
Damien Miller2d6b8332002-06-21 15:59:49 +10002074 /* Compression needs to share state across borders */
2075 mm_init_compression(mon->m_zlib);
2076 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002077
2078 return mon;
2079}
2080
2081void
2082monitor_reinit(struct monitor *mon)
2083{
Damien Miller8f0bf232011-06-20 14:42:23 +10002084 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002085}
Darren Tucker0efd1552003-08-26 11:49:55 +10002086
2087#ifdef GSSAPI
2088int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002089mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002090{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002091 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002092 OM_uint32 major;
2093 u_int len;
2094
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002095 goid.elements = buffer_get_string(m, &len);
2096 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002097
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002098 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002099
Darren Tuckera627d422013-06-02 07:31:17 +10002100 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002101
2102 buffer_clear(m);
2103 buffer_put_int(m, major);
2104
Damien Miller6fd6def2005-11-05 15:07:05 +11002105 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002106
2107 /* Now we have a context, enable the step */
2108 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2109
2110 return (0);
2111}
2112
2113int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002114mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002115{
2116 gss_buffer_desc in;
2117 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002118 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002119 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002120 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002121
Darren Tucker600ad8d2003-08-26 12:10:48 +10002122 in.value = buffer_get_string(m, &len);
2123 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002124 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002125 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002126
2127 buffer_clear(m);
2128 buffer_put_int(m, major);
2129 buffer_put_string(m, out.value, out.length);
2130 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002131 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002132
2133 gss_release_buffer(&minor, &out);
2134
Damien Miller6fd6def2005-11-05 15:07:05 +11002135 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002136 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2137 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002138 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002139 }
2140 return (0);
2141}
2142
2143int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002144mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002145{
2146 gss_buffer_desc gssbuf, mic;
2147 OM_uint32 ret;
2148 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002149
Damien Miller0425d402003-11-17 22:18:21 +11002150 gssbuf.value = buffer_get_string(m, &len);
2151 gssbuf.length = len;
2152 mic.value = buffer_get_string(m, &len);
2153 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002154
Damien Miller0425d402003-11-17 22:18:21 +11002155 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002156
Darren Tuckera627d422013-06-02 07:31:17 +10002157 free(gssbuf.value);
2158 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002159
Damien Miller0425d402003-11-17 22:18:21 +11002160 buffer_clear(m);
2161 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002162
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002163 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002164
Damien Miller0425d402003-11-17 22:18:21 +11002165 if (!GSS_ERROR(ret))
2166 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002167
Damien Miller0425d402003-11-17 22:18:21 +11002168 return (0);
2169}
2170
2171int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002172mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002173{
2174 int authenticated;
2175
2176 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2177
2178 buffer_clear(m);
2179 buffer_put_int(m, authenticated);
2180
2181 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002182 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002183
Damien Miller6fd6def2005-11-05 15:07:05 +11002184 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002185
2186 /* Monitor loop will terminate if authenticated */
2187 return (authenticated);
2188}
2189#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002190