blob: b830e0374d11e719e5a5cc43b4c78bb85aca025a [file] [log] [blame]
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001/* $OpenBSD: monitor.c,v 1.136 2014/12/22 07:51:30 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"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103
Darren Tucker0efd1552003-08-26 11:49:55 +1000104#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000105static Gssctxt *gsscontext = NULL;
106#endif
107
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108/* Imports */
109extern ServerOptions options;
110extern u_int utmp_len;
111extern Newkeys *current_keys[];
112extern z_stream incoming_stream;
113extern z_stream outgoing_stream;
114extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115extern Buffer auth_debug;
116extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000117extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118
119/* State exported from the child */
120
121struct {
122 z_stream incoming;
123 z_stream outgoing;
124 u_char *keyin;
125 u_int keyinlen;
126 u_char *keyout;
127 u_int keyoutlen;
128 u_char *ivin;
129 u_int ivinlen;
130 u_char *ivout;
131 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000132 u_char *ssh1key;
133 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134 int ssh1cipher;
135 int ssh1protoflags;
136 u_char *input;
137 u_int ilen;
138 u_char *output;
139 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000140 u_int64_t sent_bytes;
141 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142} child_state;
143
Damien Miller469954d2003-06-18 20:25:33 +1000144/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145
146int mm_answer_moduli(int, Buffer *);
147int mm_answer_sign(int, Buffer *);
148int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000149int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000150int mm_answer_authserv(int, Buffer *);
151int mm_answer_authpassword(int, Buffer *);
152int mm_answer_bsdauthquery(int, Buffer *);
153int mm_answer_bsdauthrespond(int, Buffer *);
154int mm_answer_skeyquery(int, Buffer *);
155int mm_answer_skeyrespond(int, Buffer *);
156int mm_answer_keyallowed(int, Buffer *);
157int mm_answer_keyverify(int, Buffer *);
158int mm_answer_pty(int, Buffer *);
159int mm_answer_pty_cleanup(int, Buffer *);
160int mm_answer_term(int, Buffer *);
161int mm_answer_rsa_keyallowed(int, Buffer *);
162int mm_answer_rsa_challenge(int, Buffer *);
163int mm_answer_rsa_response(int, Buffer *);
164int mm_answer_sesskey(int, Buffer *);
165int mm_answer_sessid(int, Buffer *);
166
Damien Miller79418552002-04-23 20:28:48 +1000167#ifdef USE_PAM
168int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000169int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000170int mm_answer_pam_init_ctx(int, Buffer *);
171int mm_answer_pam_query(int, Buffer *);
172int mm_answer_pam_respond(int, Buffer *);
173int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000174#endif
175
Darren Tucker0efd1552003-08-26 11:49:55 +1000176#ifdef GSSAPI
177int mm_answer_gss_setup_ctx(int, Buffer *);
178int mm_answer_gss_accept_ctx(int, Buffer *);
179int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100180int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000181#endif
Damien Miller25162f22002-09-12 09:47:29 +1000182
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100183#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100184int mm_answer_audit_event(int, Buffer *);
185int mm_answer_audit_command(int, Buffer *);
186#endif
187
Damien Miller8f0bf232011-06-20 14:42:23 +1000188static int monitor_read_log(struct monitor *);
189
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000191
192#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000194#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196/* local state for key verify */
197static u_char *key_blob = NULL;
198static u_int key_bloblen = 0;
199static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000200static char *hostbased_cuser = NULL;
201static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100203static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000204static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000205static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000206static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207
208struct mon_table {
209 enum monitor_reqtype type;
210 int flags;
211 int (*f)(int, Buffer *);
212};
213
214#define MON_ISAUTH 0x0004 /* Required for Authentication */
215#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
216#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100217#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
219#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
220
221#define MON_PERMIT 0x1000 /* Request is permitted */
222
223struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000224#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000225 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000226#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
228 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
229 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000230 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000232#ifdef USE_PAM
233 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000234 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000235 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
236 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
237 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
238 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000239#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100240#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100241 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100242#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243#ifdef BSD_AUTH
244 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100245 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000246#endif
247#ifdef SKEY
248 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
249 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
250#endif
251 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
252 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000253#ifdef GSSAPI
254 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
255 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
256 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100257 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000258#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000259 {0, 0, NULL}
260};
261
262struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000263#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000265#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000266 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
267 {MONITOR_REQ_PTY, 0, mm_answer_pty},
268 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
269 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100270#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100271 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
272 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
273#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 {0, 0, NULL}
275};
276
277struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000278#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
280 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
281 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
282 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100283 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
284 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
286 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
287#ifdef BSD_AUTH
288 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100289 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290#endif
291#ifdef SKEY
292 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
293 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
294#endif
Damien Millera33501b2002-05-08 12:24:42 +1000295#ifdef USE_PAM
296 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000297 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000298 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
299 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
300 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
301 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000302#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100303#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100304 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100305#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000306#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307 {0, 0, NULL}
308};
309
310struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000311#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
313 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
314 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100315#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100316 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000317 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100318#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000319#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000320 {0, 0, NULL}
321};
322
323struct mon_table *mon_dispatch;
324
325/* Specifies if a certain message is allowed at the moment */
326
327static void
328monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
329{
330 while (ent->f != NULL) {
331 if (ent->type == type) {
332 ent->flags &= ~MON_PERMIT;
333 ent->flags |= permit ? MON_PERMIT : 0;
334 return;
335 }
336 ent++;
337 }
338}
339
340static void
341monitor_permit_authentications(int permit)
342{
343 struct mon_table *ent = mon_dispatch;
344
345 while (ent->f != NULL) {
346 if (ent->flags & MON_AUTH) {
347 ent->flags &= ~MON_PERMIT;
348 ent->flags |= permit ? MON_PERMIT : 0;
349 }
350 ent++;
351 }
352}
353
Darren Tucker3e33cec2003-10-02 16:12:36 +1000354void
355monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356{
357 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100358 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359
360 debug3("preauth child monitor started");
361
Damien Miller8f0bf232011-06-20 14:42:23 +1000362 close(pmonitor->m_recvfd);
363 close(pmonitor->m_log_sendfd);
364 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
365
Darren Tucker3e33cec2003-10-02 16:12:36 +1000366 authctxt = _authctxt;
367 memset(authctxt, 0, sizeof(*authctxt));
368
Darren Tuckerde0de392005-03-31 23:52:04 +1000369 authctxt->loginmsg = &loginmsg;
370
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371 if (compat20) {
372 mon_dispatch = mon_dispatch_proto20;
373
374 /* Permit requests for moduli and signatures */
375 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
376 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
377 } else {
378 mon_dispatch = mon_dispatch_proto15;
379
380 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
381 }
382
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383 /* The first few requests do not require asynchronous access */
384 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100385 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100386 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100387 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100388 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100389
390 /* Special handling for multiple required authentications */
391 if (options.num_auth_methods != 0) {
392 if (!compat20)
393 fatal("AuthenticationMethods is not supported"
394 "with SSH protocol 1");
395 if (authenticated &&
396 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000397 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100398 debug3("%s: method %s: partial", __func__,
399 auth_method);
400 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100401 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100402 }
403 }
404
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405 if (authenticated) {
406 if (!(ent->flags & MON_AUTHDECIDE))
407 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000408 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409 if (authctxt->pw->pw_uid == 0 &&
410 !auth_root_allowed(auth_method))
411 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000412#ifdef USE_PAM
413 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100414 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000415 Buffer m;
416
417 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100418 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000419 MONITOR_REQ_PAM_ACCOUNT, &m);
420 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
421 buffer_free(&m);
422 }
423#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100425 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100426 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000427 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000428 if (!authenticated)
429 authctxt->failures++;
430 }
431 }
432
433 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000434 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100435 if (strcmp(auth_method, "unknown") == 0)
436 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437
438 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000439 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000440
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000441 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000442
Damien Miller6a1937e2012-12-12 10:44:38 +1100443 /* Drain any buffered messages from the child */
444 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
445 ;
446
Damien Miller8f0bf232011-06-20 14:42:23 +1000447 close(pmonitor->m_sendfd);
448 close(pmonitor->m_log_recvfd);
449 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450}
451
Damien Millerbe64d432003-05-14 19:31:12 +1000452static void
453monitor_set_child_handler(pid_t pid)
454{
455 monitor_child_pid = pid;
456}
457
458static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000459monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000460{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000461 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000462}
463
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000465monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466{
Damien Miller8f0bf232011-06-20 14:42:23 +1000467 close(pmonitor->m_recvfd);
468 pmonitor->m_recvfd = -1;
469
Damien Millerbe64d432003-05-14 19:31:12 +1000470 monitor_set_child_handler(pmonitor->m_pid);
471 signal(SIGHUP, &monitor_child_handler);
472 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000473 signal(SIGINT, &monitor_child_handler);
Damien Miller146218a2014-08-27 04:11:55 +1000474#ifdef SIGXFSZ
475 signal(SIGXFSZ, SIG_IGN);
476#endif
Damien Millerbe64d432003-05-14 19:31:12 +1000477
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000478 if (compat20) {
479 mon_dispatch = mon_dispatch_postauth20;
480
481 /* Permit requests for moduli and signatures */
482 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
483 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
484 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485 } else {
486 mon_dispatch = mon_dispatch_postauth15;
487 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
488 }
489 if (!no_pty_flag) {
490 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
491 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
492 }
493
494 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000495 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496}
497
498void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000499monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500{
Damien Miller2d6b8332002-06-21 15:59:49 +1000501 if (options.compression) {
502 /* The member allocation is not visible, so sync it */
503 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
504 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000505}
506
Damien Miller8f0bf232011-06-20 14:42:23 +1000507static int
508monitor_read_log(struct monitor *pmonitor)
509{
510 Buffer logmsg;
511 u_int len, level;
512 char *msg;
513
514 buffer_init(&logmsg);
515
516 /* Read length */
517 buffer_append_space(&logmsg, 4);
518 if (atomicio(read, pmonitor->m_log_recvfd,
519 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
520 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100521 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000522 debug("%s: child log fd closed", __func__);
523 close(pmonitor->m_log_recvfd);
524 pmonitor->m_log_recvfd = -1;
525 return -1;
526 }
527 fatal("%s: log fd read: %s", __func__, strerror(errno));
528 }
529 len = buffer_get_int(&logmsg);
530 if (len <= 4 || len > 8192)
531 fatal("%s: invalid log message length %u", __func__, len);
532
533 /* Read severity, message */
534 buffer_clear(&logmsg);
535 buffer_append_space(&logmsg, len);
536 if (atomicio(read, pmonitor->m_log_recvfd,
537 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
538 fatal("%s: log fd read: %s", __func__, strerror(errno));
539
540 /* Log it */
541 level = buffer_get_int(&logmsg);
542 msg = buffer_get_string(&logmsg, NULL);
543 if (log_level_name(level) == NULL)
544 fatal("%s: invalid log level %u (corrupted message?)",
545 __func__, level);
546 do_log2(level, "%s [preauth]", msg);
547
548 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000549 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000550
551 return 0;
552}
553
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000555monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556 struct mon_table **pent)
557{
558 Buffer m;
559 int ret;
560 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000561 struct pollfd pfd[2];
562
563 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100564 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000565 pfd[0].fd = pmonitor->m_sendfd;
566 pfd[0].events = POLLIN;
567 pfd[1].fd = pmonitor->m_log_recvfd;
568 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000569 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
570 if (errno == EINTR || errno == EAGAIN)
571 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000572 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000573 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000574 if (pfd[1].revents) {
575 /*
576 * Drain all log messages before processing next
577 * monitor request.
578 */
579 monitor_read_log(pmonitor);
580 continue;
581 }
582 if (pfd[0].revents)
583 break; /* Continues below */
584 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
586 buffer_init(&m);
587
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000588 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 type = buffer_get_char(&m);
590
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000591 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 while (ent->f != NULL) {
594 if (ent->type == type)
595 break;
596 ent++;
597 }
598
599 if (ent->f != NULL) {
600 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000601 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000603 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 buffer_free(&m);
605
606 /* The child may use this request only once, disable it */
607 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000608 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 type);
610 ent->flags &= ~MON_PERMIT;
611 }
612
613 if (pent != NULL)
614 *pent = ent;
615
616 return ret;
617 }
618
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000619 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 /* NOTREACHED */
622 return (-1);
623}
624
625/* allowed key state */
626static int
627monitor_allowed_key(u_char *blob, u_int bloblen)
628{
629 /* make sure key is allowed */
630 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000631 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000632 return (0);
633 return (1);
634}
635
636static void
637monitor_reset_key_state(void)
638{
639 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(key_blob);
641 free(hostbased_cuser);
642 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643 key_blob = NULL;
644 key_bloblen = 0;
645 key_blobtype = MM_NOKEY;
646 hostbased_cuser = NULL;
647 hostbased_chost = NULL;
648}
649
Damien Miller1f0311c2014-05-15 14:24:09 +1000650#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000651int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000652mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653{
654 DH *dh;
655 int min, want, max;
656
657 min = buffer_get_int(m);
658 want = buffer_get_int(m);
659 max = buffer_get_int(m);
660
661 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 /* We need to check here, too, in case the child got corrupted */
664 if (max < min || want < min || max < want)
665 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000666 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667
668 buffer_clear(m);
669
670 dh = choose_dh(min, want, max);
671 if (dh == NULL) {
672 buffer_put_char(m, 0);
673 return (0);
674 } else {
675 /* Send first bignum */
676 buffer_put_char(m, 1);
677 buffer_put_bignum2(m, dh->p);
678 buffer_put_bignum2(m, dh->g);
679
680 DH_free(dh);
681 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000682 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683 return (0);
684}
Damien Miller1f0311c2014-05-15 14:24:09 +1000685#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
Damien Miller85b45e02013-07-20 13:21:52 +1000687extern AuthenticationConnection *auth_conn;
688
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000690mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691{
692 Key *key;
693 u_char *p;
694 u_char *signature;
695 u_int siglen, datlen;
696 int keyid;
697
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000698 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
700 keyid = buffer_get_int(m);
701 p = buffer_get_string(m, &datlen);
702
Damien Millera63128d2006-03-15 12:08:28 +1100703 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000704 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
705 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100706 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000707 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000708 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709
Ben Lindstromf67e0772002-06-06 20:58:19 +0000710 /* save session id, it will be passed on the first call */
711 if (session_id2_len == 0) {
712 session_id2_len = datlen;
713 session_id2 = xmalloc(session_id2_len);
714 memcpy(session_id2, p, session_id2_len);
715 }
716
Damien Miller85b45e02013-07-20 13:21:52 +1000717 if ((key = get_hostkey_by_index(keyid)) != NULL) {
718 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
719 fatal("%s: key_sign failed", __func__);
720 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
721 auth_conn != NULL) {
722 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
723 datlen) < 0)
724 fatal("%s: ssh_agent_sign failed", __func__);
725 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000726 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
Ben Lindstromd5502182002-06-27 00:12:57 +0000728 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729
730 buffer_clear(m);
731 buffer_put_string(m, signature, siglen);
732
Darren Tuckera627d422013-06-02 07:31:17 +1000733 free(p);
734 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000736 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737
738 /* Turn on permissions for getpwnam */
739 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
740
741 return (0);
742}
743
744/* Retrieves the password entry and also checks if the user is permitted */
745
746int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000747mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000749 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000750 struct passwd *pwent;
751 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000752 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755
756 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000757 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758
Darren Tuckerb09b6772004-06-22 15:06:46 +1000759 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760
Darren Tuckerb09b6772004-06-22 15:06:46 +1000761 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762
Darren Tuckerb09b6772004-06-22 15:06:46 +1000763 authctxt->user = xstrdup(username);
764 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000765 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766
767 buffer_clear(m);
768
769 if (pwent == NULL) {
770 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100771 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772 goto out;
773 }
774
775 allowed = 1;
776 authctxt->pw = pwent;
777 authctxt->valid = 1;
778
779 buffer_put_char(m, 1);
780 buffer_put_string(m, pwent, sizeof(struct passwd));
781 buffer_put_cstring(m, pwent->pw_name);
782 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000783#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000785#endif
786#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000788#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789 buffer_put_cstring(m, pwent->pw_dir);
790 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100791
792 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100793 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000794
795#define M_CP_STROPT(x) do { \
796 if (options.x != NULL) \
797 buffer_put_cstring(m, options.x); \
798 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000799#define M_CP_STRARRAYOPT(x, nx) do { \
800 for (i = 0; i < options.nx; i++) \
801 buffer_put_cstring(m, options.x[i]); \
802 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000803 /* See comment in servconf.h */
804 COPY_MATCH_STRING_OPTS();
805#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000806#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100807
808 /* Create valid auth method lists */
809 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
810 /*
811 * The monitor will continue long enough to let the child
812 * run to it's packet_disconnect(), but it must not allow any
813 * authentication to succeed.
814 */
815 debug("%s: no valid authentication method lists", __func__);
816 }
817
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000818 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000819 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820
821 /* For SSHv1 allow authentication now */
822 if (!compat20)
823 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000824 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825 /* Allow service/style information on the auth context */
826 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000827 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
828 }
Damien Millera33501b2002-05-08 12:24:42 +1000829#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000830 if (options.use_pam)
831 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000832#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
834 return (0);
835}
836
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000837int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000838{
839 char *banner;
840
841 buffer_clear(m);
842 banner = auth2_read_banner();
843 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000844 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000845 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000846
847 return (0);
848}
849
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000851mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000852{
853 monitor_permit_authentications(1);
854
855 authctxt->service = buffer_get_string(m, NULL);
856 authctxt->style = buffer_get_string(m, NULL);
857 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000858 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000859
860 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000861 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 authctxt->style = NULL;
863 }
864
865 return (0);
866}
867
868int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000869mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870{
871 static int call_count;
872 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000873 int authenticated;
874 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875
876 passwd = buffer_get_string(m, &plen);
877 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000878 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000879 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100880 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882
883 buffer_clear(m);
884 buffer_put_int(m, authenticated);
885
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000886 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000887 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000888
889 call_count++;
890 if (plen == 0 && call_count == 1)
891 auth_method = "none";
892 else
893 auth_method = "password";
894
895 /* Causes monitor loop to terminate if authenticated */
896 return (authenticated);
897}
898
899#ifdef BSD_AUTH
900int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000901mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902{
903 char *name, *infotxt;
904 u_int numprompts;
905 u_int *echo_on;
906 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100907 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908
Damien Millerb7df3af2003-02-24 11:55:46 +1100909 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
910 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911
912 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100913 buffer_put_int(m, success);
914 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915 buffer_put_cstring(m, prompts[0]);
916
Damien Millerb7df3af2003-02-24 11:55:46 +1100917 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000918 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919
Damien Millerb7df3af2003-02-24 11:55:46 +1100920 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000921 free(name);
922 free(infotxt);
923 free(prompts);
924 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000925 }
926
927 return (0);
928}
929
930int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000931mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932{
933 char *response;
934 int authok;
935
936 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000940 authok = options.challenge_response_authentication &&
941 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000944 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
946 buffer_clear(m);
947 buffer_put_int(m, authok);
948
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000949 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000950 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951
Damien Miller91a55f22013-04-23 15:18:10 +1000952 if (compat20) {
953 auth_method = "keyboard-interactive";
954 auth_submethod = "bsdauth";
955 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100956 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957
958 return (authok != 0);
959}
960#endif
961
962#ifdef SKEY
963int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000964mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965{
966 struct skey skey;
967 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100968 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000970 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
971 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
973 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100974 buffer_put_int(m, success);
975 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976 buffer_put_cstring(m, challenge);
977
Damien Millerb7df3af2003-02-24 11:55:46 +1100978 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000979 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980
981 return (0);
982}
983
984int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000985mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986{
987 char *response;
988 int authok;
989
990 response = buffer_get_string(m, NULL);
991
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000992 authok = (options.challenge_response_authentication &&
993 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994 skey_haskey(authctxt->pw->pw_name) == 0 &&
995 skey_passcheck(authctxt->pw->pw_name, response) != -1);
996
Darren Tuckerf60845f2013-06-02 08:07:31 +1000997 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
999 buffer_clear(m);
1000 buffer_put_int(m, authok);
1001
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001002 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001003 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004
1005 auth_method = "skey";
1006
1007 return (authok != 0);
1008}
1009#endif
1010
Damien Miller79418552002-04-23 20:28:48 +10001011#ifdef USE_PAM
1012int
Darren Tucker7eda5922004-06-22 13:22:50 +10001013mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001014{
Damien Miller4e448a32003-05-14 15:11:48 +10001015 if (!options.use_pam)
1016 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1017
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001018 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001019
Damien Miller1f499fd2003-08-25 13:08:49 +10001020 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1021
Damien Miller79418552002-04-23 20:28:48 +10001022 return (0);
1023}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001024
Damien Miller1f499fd2003-08-25 13:08:49 +10001025int
Darren Tucker7eda5922004-06-22 13:22:50 +10001026mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001027{
1028 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001029
Damien Miller1f499fd2003-08-25 13:08:49 +10001030 if (!options.use_pam)
1031 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1032
1033 ret = do_pam_account();
1034
1035 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001036 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001037
Darren Tucker7eda5922004-06-22 13:22:50 +10001038 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001039
1040 return (ret);
1041}
1042
Damien Miller4f9f42a2003-05-10 19:28:02 +10001043static void *sshpam_ctxt, *sshpam_authok;
1044extern KbdintDevice sshpam_device;
1045
1046int
Darren Tucker7eda5922004-06-22 13:22:50 +10001047mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001048{
1049
1050 debug3("%s", __func__);
1051 authctxt->user = buffer_get_string(m, NULL);
1052 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1053 sshpam_authok = NULL;
1054 buffer_clear(m);
1055 if (sshpam_ctxt != NULL) {
1056 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1057 buffer_put_int(m, 1);
1058 } else {
1059 buffer_put_int(m, 0);
1060 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001061 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062 return (0);
1063}
1064
1065int
Darren Tucker7eda5922004-06-22 13:22:50 +10001066mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001067{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001068 char *name = NULL, *info = NULL, **prompts = NULL;
1069 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001070 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001071
1072 debug3("%s", __func__);
1073 sshpam_authok = NULL;
1074 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1075 if (ret == 0 && num == 0)
1076 sshpam_authok = sshpam_ctxt;
1077 if (num > 1 || name == NULL || info == NULL)
1078 ret = -1;
1079 buffer_clear(m);
1080 buffer_put_int(m, ret);
1081 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001082 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 buffer_put_int(m, num);
1086 for (i = 0; i < num; ++i) {
1087 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001088 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001089 buffer_put_int(m, echo_on[i]);
1090 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001091 free(prompts);
1092 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001093 auth_method = "keyboard-interactive";
1094 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001095 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096 return (0);
1097}
1098
1099int
Darren Tucker7eda5922004-06-22 13:22:50 +10001100mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101{
1102 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001103 u_int i, num;
1104 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001105
1106 debug3("%s", __func__);
1107 sshpam_authok = NULL;
1108 num = buffer_get_int(m);
1109 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001110 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111 for (i = 0; i < num; ++i)
1112 resp[i] = buffer_get_string(m, NULL);
1113 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1114 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001115 free(resp[i]);
1116 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001117 } else {
1118 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1119 }
1120 buffer_clear(m);
1121 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001122 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001123 auth_method = "keyboard-interactive";
1124 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001125 if (ret == 0)
1126 sshpam_authok = sshpam_ctxt;
1127 return (0);
1128}
1129
1130int
Darren Tucker7eda5922004-06-22 13:22:50 +10001131mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001132{
1133
1134 debug3("%s", __func__);
1135 (sshpam_device.free_ctx)(sshpam_ctxt);
1136 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001137 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001138 auth_method = "keyboard-interactive";
1139 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001140 return (sshpam_authok == sshpam_ctxt);
1141}
Damien Miller79418552002-04-23 20:28:48 +10001142#endif
1143
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001145mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146{
1147 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001148 char *cuser, *chost;
1149 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150 u_int bloblen;
1151 enum mm_keytype type = 0;
1152 int allowed = 0;
1153
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001154 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001155
1156 type = buffer_get_int(m);
1157 cuser = buffer_get_string(m, NULL);
1158 chost = buffer_get_string(m, NULL);
1159 blob = buffer_get_string(m, &bloblen);
1160
1161 key = key_from_blob(blob, bloblen);
1162
1163 if ((compat20 && type == MM_RSAHOSTKEY) ||
1164 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001165 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001167 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168
Damien Miller3e3b5142003-11-17 21:13:40 +11001169 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001170 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001172 allowed = options.pubkey_authentication &&
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001173 !auth2_userkey_already_used(authctxt, key) &&
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001174 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001175 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001176 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001177 if (options.pubkey_authentication && allowed != 1)
1178 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179 break;
1180 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001181 allowed = options.hostbased_authentication &&
1182 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001184 pubkey_auth_info(authctxt, key,
1185 "client user \"%.100s\", client host \"%.100s\"",
1186 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001187 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001189#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190 case MM_RSAHOSTKEY:
1191 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001192 allowed = options.rhosts_rsa_authentication &&
1193 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001195 if (options.rhosts_rsa_authentication && allowed != 1)
1196 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001197 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001198 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001199#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 break;
1203 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204 }
Damien Miller00111382003-03-10 11:21:17 +11001205 if (key != NULL)
1206 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
1208 /* clear temporarily storage (used by verify) */
1209 monitor_reset_key_state();
1210
1211 if (allowed) {
1212 /* Save temporarily for comparison in verify */
1213 key_blob = blob;
1214 key_bloblen = bloblen;
1215 key_blobtype = type;
1216 hostbased_cuser = cuser;
1217 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001218 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001219 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001220 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001221 free(blob);
1222 free(cuser);
1223 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 }
1225
1226 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001227 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228
1229 buffer_clear(m);
1230 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001231 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001232
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001233 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234
1235 if (type == MM_RSAHOSTKEY)
1236 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1237
1238 return (0);
1239}
1240
1241static int
1242monitor_valid_userblob(u_char *data, u_int datalen)
1243{
1244 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001245 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 u_int len;
1247 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248
1249 buffer_init(&b);
1250 buffer_append(&b, data, datalen);
1251
1252 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001253 p = buffer_ptr(&b);
1254 len = buffer_len(&b);
1255 if ((session_id2 == NULL) ||
1256 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001257 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001258 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 buffer_consume(&b, session_id2_len);
1260 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001261 p = buffer_get_string(&b, &len);
1262 if ((session_id2 == NULL) ||
1263 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001264 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001267 }
1268 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1269 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001270 p = buffer_get_cstring(&b, NULL);
1271 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1272 authctxt->style ? ":" : "",
1273 authctxt->style ? authctxt->style : "");
1274 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001275 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001276 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 fail++;
1278 }
Darren Tuckera627d422013-06-02 07:31:17 +10001279 free(userstyle);
1280 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281 buffer_skip_string(&b);
1282 if (datafellows & SSH_BUG_PKAUTH) {
1283 if (!buffer_get_char(&b))
1284 fail++;
1285 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001286 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 if (strcmp("publickey", p) != 0)
1288 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001289 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290 if (!buffer_get_char(&b))
1291 fail++;
1292 buffer_skip_string(&b);
1293 }
1294 buffer_skip_string(&b);
1295 if (buffer_len(&b) != 0)
1296 fail++;
1297 buffer_free(&b);
1298 return (fail == 0);
1299}
1300
1301static int
Damien Millera10f5612002-09-12 09:49:15 +10001302monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1303 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304{
1305 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001306 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 u_int len;
1308 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309
1310 buffer_init(&b);
1311 buffer_append(&b, data, datalen);
1312
Ben Lindstromf67e0772002-06-06 20:58:19 +00001313 p = buffer_get_string(&b, &len);
1314 if ((session_id2 == NULL) ||
1315 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001316 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001318 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001319
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1321 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001322 p = buffer_get_cstring(&b, NULL);
1323 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1324 authctxt->style ? ":" : "",
1325 authctxt->style ? authctxt->style : "");
1326 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001327 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001328 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 fail++;
1330 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001331 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001332 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001334 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335 if (strcmp(p, "hostbased") != 0)
1336 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001337 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338 buffer_skip_string(&b); /* pkalg */
1339 buffer_skip_string(&b); /* pkblob */
1340
1341 /* verify client host, strip trailing dot if necessary */
1342 p = buffer_get_string(&b, NULL);
1343 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1344 p[len - 1] = '\0';
1345 if (strcmp(p, chost) != 0)
1346 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001347 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
1349 /* verify client user */
1350 p = buffer_get_string(&b, NULL);
1351 if (strcmp(p, cuser) != 0)
1352 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001353 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
1355 if (buffer_len(&b) != 0)
1356 fail++;
1357 buffer_free(&b);
1358 return (fail == 0);
1359}
1360
1361int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001362mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363{
1364 Key *key;
1365 u_char *signature, *data, *blob;
1366 u_int signaturelen, datalen, bloblen;
1367 int verified = 0;
1368 int valid_data = 0;
1369
1370 blob = buffer_get_string(m, &bloblen);
1371 signature = buffer_get_string(m, &signaturelen);
1372 data = buffer_get_string(m, &datalen);
1373
1374 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001375 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001376 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377
1378 key = key_from_blob(blob, bloblen);
1379 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381
1382 switch (key_blobtype) {
1383 case MM_USERKEY:
1384 valid_data = monitor_valid_userblob(data, datalen);
1385 break;
1386 case MM_HOSTKEY:
1387 valid_data = monitor_valid_hostbasedblob(data, datalen,
1388 hostbased_cuser, hostbased_chost);
1389 break;
1390 default:
1391 valid_data = 0;
1392 break;
1393 }
1394 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001395 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396
1397 verified = key_verify(key, signature, signaturelen, data, datalen);
1398 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001399 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001401 /* If auth was successful then record key to ensure it isn't reused */
1402 if (verified == 1)
1403 auth2_record_userkey(authctxt, key);
1404 else
1405 key_free(key);
1406
Darren Tuckera627d422013-06-02 07:31:17 +10001407 free(blob);
1408 free(signature);
1409 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410
Ben Lindstrome1c09122002-06-23 00:38:24 +00001411 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1412
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413 monitor_reset_key_state();
1414
1415 buffer_clear(m);
1416 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001417 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418
Darren Tuckerfbba7352006-11-07 23:16:08 +11001419 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420}
1421
1422static void
1423mm_record_login(Session *s, struct passwd *pw)
1424{
1425 socklen_t fromlen;
1426 struct sockaddr_storage from;
1427
1428 /*
1429 * Get IP address of client. If the connection is not a socket, let
1430 * the address be 0.0.0.0.
1431 */
1432 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001433 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001436 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001438 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 }
1440 }
1441 /* Record that there was a login on that tty from the remote host. */
1442 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001443 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001444 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445}
1446
1447static void
1448mm_session_close(Session *s)
1449{
Damien Miller04bd8b02003-05-25 14:38:33 +10001450 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001451 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001452 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453 session_pty_cleanup2(s);
1454 }
Damien Miller7207f642008-05-19 15:34:50 +10001455 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001456}
1457
1458int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001459mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001461 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462 Session *s;
1463 int res, fd0;
1464
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001465 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001466
1467 buffer_clear(m);
1468 s = session_new();
1469 if (s == NULL)
1470 goto error;
1471 s->authctxt = authctxt;
1472 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001473 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1475 if (res == 0)
1476 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477 pty_setowner(authctxt->pw, s->tty);
1478
1479 buffer_put_int(m, 1);
1480 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481
1482 /* We need to trick ttyslot */
1483 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001484 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485
1486 mm_record_login(s, authctxt->pw);
1487
1488 /* Now we can close the file descriptor again */
1489 close(0);
1490
Darren Tucker09991742004-07-17 17:05:14 +10001491 /* send messages generated by record_login */
1492 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1493 buffer_clear(&loginmsg);
1494
1495 mm_request_send(sock, MONITOR_ANS_PTY, m);
1496
Damien Miller54fd7cf2007-09-17 12:04:08 +10001497 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1498 mm_send_fd(sock, s->ttyfd) == -1)
1499 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001500
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501 /* make sure nothing uses fd 0 */
1502 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001503 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001505 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506
1507 /* slave is not needed */
1508 close(s->ttyfd);
1509 s->ttyfd = s->ptyfd;
1510 /* no need to dup() because nobody closes ptyfd */
1511 s->ptymaster = s->ptyfd;
1512
Damien Miller9ab00b42006-08-05 12:40:11 +10001513 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514
1515 return (0);
1516
1517 error:
1518 if (s != NULL)
1519 mm_session_close(s);
1520 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001521 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522 return (0);
1523}
1524
1525int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001526mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527{
1528 Session *s;
1529 char *tty;
1530
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001531 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 tty = buffer_get_string(m, NULL);
1534 if ((s = session_by_tty(tty)) != NULL)
1535 mm_session_close(s);
1536 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001537 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538 return (0);
1539}
1540
Damien Miller1f0311c2014-05-15 14:24:09 +10001541#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001543mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544{
1545 BIGNUM *p;
1546 int rsafail;
1547
1548 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001549 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550
1551 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001552 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553
1554 buffer_get_bignum2(m, p);
1555
1556 rsafail = ssh1_session_key(p);
1557
1558 buffer_clear(m);
1559 buffer_put_int(m, rsafail);
1560 buffer_put_bignum2(m, p);
1561
1562 BN_clear_free(p);
1563
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001564 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565
1566 /* Turn on permissions for sessid passing */
1567 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1568
1569 return (0);
1570}
1571
1572int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001573mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574{
1575 int i;
1576
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001577 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001578
1579 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001580 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581 for (i = 0; i < 16; i++)
1582 session_id[i] = buffer_get_char(m);
1583
1584 /* Turn on permissions for getpwnam */
1585 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1586
1587 return (0);
1588}
1589
1590int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001591mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592{
1593 BIGNUM *client_n;
1594 Key *key = NULL;
1595 u_char *blob = NULL;
1596 u_int blen = 0;
1597 int allowed = 0;
1598
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001599 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600
Damien Miller7a8f5b32006-03-31 23:14:23 +11001601 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001602 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001604 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605 buffer_get_bignum2(m, client_n);
1606 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1607 BN_clear_free(client_n);
1608 }
1609 buffer_clear(m);
1610 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001611 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612
1613 /* clear temporarily storage (used by generate challenge) */
1614 monitor_reset_key_state();
1615
1616 if (allowed && key != NULL) {
1617 key->type = KEY_RSA; /* cheat for key_to_blob */
1618 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001619 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 buffer_put_string(m, blob, blen);
1621
1622 /* Save temporarily for comparison in verify */
1623 key_blob = blob;
1624 key_bloblen = blen;
1625 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626 }
Damien Miller00111382003-03-10 11:21:17 +11001627 if (key != NULL)
1628 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001630 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1633 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1634 return (0);
1635}
1636
1637int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001638mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639{
1640 Key *key = NULL;
1641 u_char *blob;
1642 u_int blen;
1643
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001644 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645
1646 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001647 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648 blob = buffer_get_string(m, &blen);
1649 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001650 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001652 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001654 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001655 if (key->type != KEY_RSA)
1656 fatal("%s: received bad key type %d", __func__, key->type);
1657 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001658 if (ssh1_challenge)
1659 BN_clear_free(ssh1_challenge);
1660 ssh1_challenge = auth_rsa_generate_challenge(key);
1661
1662 buffer_clear(m);
1663 buffer_put_bignum2(m, ssh1_challenge);
1664
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001666 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667
1668 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001669
Darren Tuckera627d422013-06-02 07:31:17 +10001670 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001671 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 return (0);
1673}
1674
1675int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001676mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677{
1678 Key *key = NULL;
1679 u_char *blob, *response;
1680 u_int blen, len;
1681 int success;
1682
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001683 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684
1685 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001686 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001688 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689
1690 blob = buffer_get_string(m, &blen);
1691 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001692 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001694 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001696 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697 response = buffer_get_string(m, &len);
1698 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001699 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1701
Darren Tuckera627d422013-06-02 07:31:17 +10001702 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001704 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705
1706 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1707
1708 /* reset state */
1709 BN_clear_free(ssh1_challenge);
1710 ssh1_challenge = NULL;
1711 monitor_reset_key_state();
1712
1713 buffer_clear(m);
1714 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001715 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001716
1717 return (success);
1718}
Damien Miller1f0311c2014-05-15 14:24:09 +10001719#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720
1721int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001722mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001724 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725 int res, status;
1726
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001727 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728
1729 /* The child is terminating */
1730 session_destroy_all(&mm_session_close);
1731
Darren Tucker52358d62008-03-11 22:58:25 +11001732#ifdef USE_PAM
1733 if (options.use_pam)
1734 sshpam_cleanup();
1735#endif
1736
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001737 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001738 if (errno != EINTR)
1739 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001740
1741 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1742
1743 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001744 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001745}
1746
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001747#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001748/* Report that an audit event occurred */
1749int
1750mm_answer_audit_event(int socket, Buffer *m)
1751{
1752 ssh_audit_event_t event;
1753
1754 debug3("%s entering", __func__);
1755
1756 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001757 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001758 case SSH_AUTH_FAIL_PUBKEY:
1759 case SSH_AUTH_FAIL_HOSTBASED:
1760 case SSH_AUTH_FAIL_GSSAPI:
1761 case SSH_LOGIN_EXCEED_MAXTRIES:
1762 case SSH_LOGIN_ROOT_DENIED:
1763 case SSH_CONNECTION_CLOSE:
1764 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001765 audit_event(event);
1766 break;
1767 default:
1768 fatal("Audit event type %d not permitted", event);
1769 }
1770
1771 return (0);
1772}
1773
1774int
1775mm_answer_audit_command(int socket, Buffer *m)
1776{
1777 u_int len;
1778 char *cmd;
1779
1780 debug3("%s entering", __func__);
1781 cmd = buffer_get_string(m, &len);
1782 /* sanity check command, if so how? */
1783 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001784 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001785 return (0);
1786}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001787#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001788
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001789void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001790monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001791{
1792 if (compat20) {
1793 set_newkeys(MODE_IN);
1794 set_newkeys(MODE_OUT);
1795 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001796 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001797 packet_set_encryption_key(child_state.ssh1key,
1798 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001799 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001800 }
1801
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001802 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001804 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001805 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001806 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807
1808 if (!compat20) {
1809 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001810 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001811 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001812 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001813 }
1814
1815 memcpy(&incoming_stream, &child_state.incoming,
1816 sizeof(incoming_stream));
1817 memcpy(&outgoing_stream, &child_state.outgoing,
1818 sizeof(outgoing_stream));
1819
1820 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001821 if (options.compression)
1822 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001823
Damien Millerc31a0cd2014-05-15 14:37:39 +10001824 packet_set_postauth();
1825
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001826 if (options.rekey_limit || options.rekey_interval)
1827 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1828 (time_t)options.rekey_interval);
1829
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001830 /* Network I/O buffers */
1831 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001832 buffer_clear(packet_get_input());
1833 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Damien Millera5103f42014-02-04 11:20:14 +11001834 explicit_bzero(child_state.input, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001835 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001836
Darren Tuckerf7288d72009-06-21 18:12:20 +10001837 buffer_clear(packet_get_output());
1838 buffer_append(packet_get_output(), child_state.output,
1839 child_state.olen);
Damien Millera5103f42014-02-04 11:20:14 +11001840 explicit_bzero(child_state.output, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001841 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001842
1843 /* Roaming */
1844 if (compat20)
1845 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001846}
1847
1848static Kex *
1849mm_get_kex(Buffer *m)
1850{
1851 Kex *kex;
1852 void *blob;
1853 u_int bloblen;
1854
Damien Miller07d86be2006-03-26 14:19:21 +11001855 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001856 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001857 if (session_id2 == NULL ||
1858 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001859 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001860 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001861 kex->we_need = buffer_get_int(m);
Damien Miller1f0311c2014-05-15 14:24:09 +10001862#ifdef WITH_OPENSSL
Damien Millerb062c292003-03-24 09:12:09 +11001863 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001864 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001865 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001866 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001867 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Damien Miller1f0311c2014-05-15 14:24:09 +10001868#endif
Damien Millerc9831972013-11-07 12:00:23 +11001869 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001870 kex->server = 1;
1871 kex->hostkey_type = buffer_get_int(m);
1872 kex->kex_type = buffer_get_int(m);
1873 blob = buffer_get_string(m, &bloblen);
1874 buffer_init(&kex->my);
1875 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001876 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877 blob = buffer_get_string(m, &bloblen);
1878 buffer_init(&kex->peer);
1879 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001880 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001881 kex->done = 1;
1882 kex->flags = buffer_get_int(m);
1883 kex->client_version_string = buffer_get_string(m, NULL);
1884 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001885 kex->load_host_public_key=&get_hostkey_public_by_type;
1886 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001887 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001888 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001889
1890 return (kex);
1891}
1892
1893/* This function requries careful sanity checking */
1894
1895void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001896mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001897{
1898 Buffer m;
1899 u_char *blob, *p;
1900 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001901 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001902 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001903
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001904 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905
1906 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001907 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908 if (!compat20) {
1909 child_state.ssh1protoflags = buffer_get_int(&m);
1910 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001911 child_state.ssh1key = buffer_get_string(&m,
1912 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001913 child_state.ivout = buffer_get_string(&m,
1914 &child_state.ivoutlen);
1915 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1916 goto skip;
1917 } else {
1918 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001919 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001920 }
1921
1922 blob = buffer_get_string(&m, &bloblen);
1923 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001924 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001925
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001926 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001927 blob = buffer_get_string(&m, &bloblen);
1928 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001929 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930
1931 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001932 seqnr = buffer_get_int(&m);
1933 blocks = buffer_get_int64(&m);
1934 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001935 bytes = buffer_get_int64(&m);
1936 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001937 seqnr = buffer_get_int(&m);
1938 blocks = buffer_get_int64(&m);
1939 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001940 bytes = buffer_get_int64(&m);
1941 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001942
1943 skip:
1944 /* Get the key context */
1945 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1946 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1947
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001948 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001949 /* Get compression state */
1950 p = buffer_get_string(&m, &plen);
1951 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001952 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001953 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001954 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001955
1956 p = buffer_get_string(&m, &plen);
1957 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001958 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001959 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001960 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001961
1962 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001963 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001964 child_state.input = buffer_get_string(&m, &child_state.ilen);
1965 child_state.output = buffer_get_string(&m, &child_state.olen);
1966
Darren Tuckerc5564e12009-06-21 18:53:53 +10001967 /* Roaming */
1968 if (compat20) {
1969 child_state.sent_bytes = buffer_get_int64(&m);
1970 child_state.recv_bytes = buffer_get_int64(&m);
1971 }
1972
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001973 buffer_free(&m);
1974}
1975
1976
1977/* Allocation functions for zlib */
1978void *
1979mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1980{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001981 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001982 void *address;
1983
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001984 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001985 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1986
1987 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001988
1989 return (address);
1990}
1991
1992void
1993mm_zfree(struct mm_master *mm, void *address)
1994{
1995 mm_free(mm, address);
1996}
1997
1998void
1999mm_init_compression(struct mm_master *mm)
2000{
2001 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
2002 outgoing_stream.zfree = (free_func)mm_zfree;
2003 outgoing_stream.opaque = mm;
2004
2005 incoming_stream.zalloc = (alloc_func)mm_zalloc;
2006 incoming_stream.zfree = (free_func)mm_zfree;
2007 incoming_stream.opaque = mm;
2008}
2009
2010/* XXX */
2011
2012#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10002013 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002014 fatal("fcntl(%d, F_SETFD)", x); \
2015} while (0)
2016
2017static void
Damien Miller8f0bf232011-06-20 14:42:23 +10002018monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002019{
Damien Miller8f0bf232011-06-20 14:42:23 +10002020 int pair[2];
2021
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002022 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002023 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002024 FD_CLOSEONEXEC(pair[0]);
2025 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002026 mon->m_recvfd = pair[0];
2027 mon->m_sendfd = pair[1];
2028
2029 if (do_logfds) {
2030 if (pipe(pair) == -1)
2031 fatal("%s: pipe: %s", __func__, strerror(errno));
2032 FD_CLOSEONEXEC(pair[0]);
2033 FD_CLOSEONEXEC(pair[1]);
2034 mon->m_log_recvfd = pair[0];
2035 mon->m_log_sendfd = pair[1];
2036 } else
2037 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002038}
2039
2040#define MM_MEMSIZE 65536
2041
2042struct monitor *
2043monitor_init(void)
2044{
2045 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002046
Damien Miller07d86be2006-03-26 14:19:21 +11002047 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002048
Damien Miller8f0bf232011-06-20 14:42:23 +10002049 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002050
2051 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002052 if (options.compression) {
2053 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2054 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002055
Damien Miller2d6b8332002-06-21 15:59:49 +10002056 /* Compression needs to share state across borders */
2057 mm_init_compression(mon->m_zlib);
2058 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002059
2060 return mon;
2061}
2062
2063void
2064monitor_reinit(struct monitor *mon)
2065{
Damien Miller8f0bf232011-06-20 14:42:23 +10002066 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002067}
Darren Tucker0efd1552003-08-26 11:49:55 +10002068
2069#ifdef GSSAPI
2070int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002071mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002072{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002073 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002074 OM_uint32 major;
2075 u_int len;
2076
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002077 goid.elements = buffer_get_string(m, &len);
2078 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002079
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002080 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002081
Darren Tuckera627d422013-06-02 07:31:17 +10002082 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002083
2084 buffer_clear(m);
2085 buffer_put_int(m, major);
2086
Damien Miller6fd6def2005-11-05 15:07:05 +11002087 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002088
2089 /* Now we have a context, enable the step */
2090 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2091
2092 return (0);
2093}
2094
2095int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002096mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002097{
2098 gss_buffer_desc in;
2099 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002100 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002101 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002102 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002103
Darren Tucker600ad8d2003-08-26 12:10:48 +10002104 in.value = buffer_get_string(m, &len);
2105 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002106 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002107 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002108
2109 buffer_clear(m);
2110 buffer_put_int(m, major);
2111 buffer_put_string(m, out.value, out.length);
2112 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002113 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002114
2115 gss_release_buffer(&minor, &out);
2116
Damien Miller6fd6def2005-11-05 15:07:05 +11002117 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002118 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2119 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002120 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002121 }
2122 return (0);
2123}
2124
2125int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002126mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002127{
2128 gss_buffer_desc gssbuf, mic;
2129 OM_uint32 ret;
2130 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002131
Damien Miller0425d402003-11-17 22:18:21 +11002132 gssbuf.value = buffer_get_string(m, &len);
2133 gssbuf.length = len;
2134 mic.value = buffer_get_string(m, &len);
2135 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002136
Damien Miller0425d402003-11-17 22:18:21 +11002137 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002138
Darren Tuckera627d422013-06-02 07:31:17 +10002139 free(gssbuf.value);
2140 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002141
Damien Miller0425d402003-11-17 22:18:21 +11002142 buffer_clear(m);
2143 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002144
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002145 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002146
Damien Miller0425d402003-11-17 22:18:21 +11002147 if (!GSS_ERROR(ret))
2148 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002149
Damien Miller0425d402003-11-17 22:18:21 +11002150 return (0);
2151}
2152
2153int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002154mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002155{
2156 int authenticated;
2157
2158 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2159
2160 buffer_clear(m);
2161 buffer_put_int(m, authenticated);
2162
2163 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002164 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002165
Damien Miller6fd6def2005-11-05 15:07:05 +11002166 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002167
2168 /* Monitor loop will terminate if authenticated */
2169 return (authenticated);
2170}
2171#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002172