blob: a38647d49010fa6d01c2561ea22647b25510557d [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: monitor.c,v 1.132 2014/04/29 18:01:49 markus 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 Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100047#ifdef HAVE_POLL_H
48#include <poll.h>
49#else
50# ifdef HAVE_SYS_POLL_H
51# include <sys/poll.h>
52# endif
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054
55#ifdef SKEY
56#include <skey.h>
57#endif
58
Damien Miller1f0311c2014-05-15 14:24:09 +100059#ifdef WITH_OPENSSL
Damien Miller03e20032006-03-15 11:16:59 +110060#include <openssl/dh.h>
Damien Miller1f0311c2014-05-15 14:24:09 +100061#endif
Damien Miller03e20032006-03-15 11:16:59 +110062
Damien Millerb84886b2008-05-19 15:05:07 +100063#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100064#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100067#include "key.h"
68#include "buffer.h"
69#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072#include "kex.h"
73#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000074#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
75#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000076#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000077#define TARGET_OS_MAC 1
78#else
79#include "zlib.h"
80#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000081#include "packet.h"
82#include "auth-options.h"
83#include "sshpty.h"
84#include "channels.h"
85#include "session.h"
86#include "sshlogin.h"
87#include "canohost.h"
88#include "log.h"
89#include "servconf.h"
90#include "monitor.h"
91#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100092#ifdef GSSAPI
93#include "ssh-gss.h"
94#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "monitor_wrap.h"
96#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000098#include "compat.h"
99#include "ssh2.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +1000100#include "roaming.h"
Damien Miller85b45e02013-07-20 13:21:52 +1000101#include "authfd.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102
Darren Tucker0efd1552003-08-26 11:49:55 +1000103#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000104static Gssctxt *gsscontext = NULL;
105#endif
106
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107/* Imports */
108extern ServerOptions options;
109extern u_int utmp_len;
110extern Newkeys *current_keys[];
111extern z_stream incoming_stream;
112extern z_stream outgoing_stream;
113extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114extern Buffer auth_debug;
115extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000116extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000117
118/* State exported from the child */
119
120struct {
121 z_stream incoming;
122 z_stream outgoing;
123 u_char *keyin;
124 u_int keyinlen;
125 u_char *keyout;
126 u_int keyoutlen;
127 u_char *ivin;
128 u_int ivinlen;
129 u_char *ivout;
130 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000131 u_char *ssh1key;
132 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133 int ssh1cipher;
134 int ssh1protoflags;
135 u_char *input;
136 u_int ilen;
137 u_char *output;
138 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000139 u_int64_t sent_bytes;
140 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141} child_state;
142
Damien Miller469954d2003-06-18 20:25:33 +1000143/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000144
145int mm_answer_moduli(int, Buffer *);
146int mm_answer_sign(int, Buffer *);
147int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000148int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000149int mm_answer_authserv(int, Buffer *);
150int mm_answer_authpassword(int, Buffer *);
151int mm_answer_bsdauthquery(int, Buffer *);
152int mm_answer_bsdauthrespond(int, Buffer *);
153int mm_answer_skeyquery(int, Buffer *);
154int mm_answer_skeyrespond(int, Buffer *);
155int mm_answer_keyallowed(int, Buffer *);
156int mm_answer_keyverify(int, Buffer *);
157int mm_answer_pty(int, Buffer *);
158int mm_answer_pty_cleanup(int, Buffer *);
159int mm_answer_term(int, Buffer *);
160int mm_answer_rsa_keyallowed(int, Buffer *);
161int mm_answer_rsa_challenge(int, Buffer *);
162int mm_answer_rsa_response(int, Buffer *);
163int mm_answer_sesskey(int, Buffer *);
164int mm_answer_sessid(int, Buffer *);
165
Damien Miller79418552002-04-23 20:28:48 +1000166#ifdef USE_PAM
167int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000168int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000169int mm_answer_pam_init_ctx(int, Buffer *);
170int mm_answer_pam_query(int, Buffer *);
171int mm_answer_pam_respond(int, Buffer *);
172int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000173#endif
174
Darren Tucker0efd1552003-08-26 11:49:55 +1000175#ifdef GSSAPI
176int mm_answer_gss_setup_ctx(int, Buffer *);
177int mm_answer_gss_accept_ctx(int, Buffer *);
178int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100179int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000180#endif
Damien Miller25162f22002-09-12 09:47:29 +1000181
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100182#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100183int mm_answer_audit_event(int, Buffer *);
184int mm_answer_audit_command(int, Buffer *);
185#endif
186
Damien Miller8f0bf232011-06-20 14:42:23 +1000187static int monitor_read_log(struct monitor *);
188
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189static Authctxt *authctxt;
Damien Miller1f0311c2014-05-15 14:24:09 +1000190
191#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
Damien Miller1f0311c2014-05-15 14:24:09 +1000193#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000194
195/* local state for key verify */
196static u_char *key_blob = NULL;
197static u_int key_bloblen = 0;
198static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000199static char *hostbased_cuser = NULL;
200static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100202static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000203static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000204static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000205static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207struct mon_table {
208 enum monitor_reqtype type;
209 int flags;
210 int (*f)(int, Buffer *);
211};
212
213#define MON_ISAUTH 0x0004 /* Required for Authentication */
214#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
215#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100216#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
219
220#define MON_PERMIT 0x1000 /* Request is permitted */
221
222struct mon_table mon_dispatch_proto20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000223#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000224 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000225#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
227 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
228 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000229 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000231#ifdef USE_PAM
232 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000233 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000234 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
235 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
236 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
237 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000238#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100239#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100240 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100241#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242#ifdef BSD_AUTH
243 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100244 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245#endif
246#ifdef SKEY
247 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
248 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
249#endif
250 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
251 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000252#ifdef GSSAPI
253 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
254 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
255 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100256 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000257#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258 {0, 0, NULL}
259};
260
261struct mon_table mon_dispatch_postauth20[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000262#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
Damien Miller1f0311c2014-05-15 14:24:09 +1000264#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
266 {MONITOR_REQ_PTY, 0, mm_answer_pty},
267 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
268 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100269#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100270 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
271 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
272#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273 {0, 0, NULL}
274};
275
276struct mon_table mon_dispatch_proto15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000277#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000278 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
279 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
280 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
281 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100282 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
283 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000284 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
285 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
286#ifdef BSD_AUTH
287 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100288 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289#endif
290#ifdef SKEY
291 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
292 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
293#endif
Damien Millera33501b2002-05-08 12:24:42 +1000294#ifdef USE_PAM
295 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000296 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000297 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
298 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
299 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
300 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000301#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100302#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100303 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100304#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000305#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306 {0, 0, NULL}
307};
308
309struct mon_table mon_dispatch_postauth15[] = {
Damien Miller1f0311c2014-05-15 14:24:09 +1000310#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000311 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
312 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
313 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100314#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100315 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000316 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100317#endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000318#endif /* WITH_SSH1 */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000319 {0, 0, NULL}
320};
321
322struct mon_table *mon_dispatch;
323
324/* Specifies if a certain message is allowed at the moment */
325
326static void
327monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
328{
329 while (ent->f != NULL) {
330 if (ent->type == type) {
331 ent->flags &= ~MON_PERMIT;
332 ent->flags |= permit ? MON_PERMIT : 0;
333 return;
334 }
335 ent++;
336 }
337}
338
339static void
340monitor_permit_authentications(int permit)
341{
342 struct mon_table *ent = mon_dispatch;
343
344 while (ent->f != NULL) {
345 if (ent->flags & MON_AUTH) {
346 ent->flags &= ~MON_PERMIT;
347 ent->flags |= permit ? MON_PERMIT : 0;
348 }
349 ent++;
350 }
351}
352
Darren Tucker3e33cec2003-10-02 16:12:36 +1000353void
354monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355{
356 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100357 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
359 debug3("preauth child monitor started");
360
Damien Miller8f0bf232011-06-20 14:42:23 +1000361 close(pmonitor->m_recvfd);
362 close(pmonitor->m_log_sendfd);
363 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
364
Darren Tucker3e33cec2003-10-02 16:12:36 +1000365 authctxt = _authctxt;
366 memset(authctxt, 0, sizeof(*authctxt));
367
Darren Tuckerde0de392005-03-31 23:52:04 +1000368 authctxt->loginmsg = &loginmsg;
369
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 if (compat20) {
371 mon_dispatch = mon_dispatch_proto20;
372
373 /* Permit requests for moduli and signatures */
374 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
375 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
376 } else {
377 mon_dispatch = mon_dispatch_proto15;
378
379 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
380 }
381
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382 /* The first few requests do not require asynchronous access */
383 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100384 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100385 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100386 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100387 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100388
389 /* Special handling for multiple required authentications */
390 if (options.num_auth_methods != 0) {
391 if (!compat20)
392 fatal("AuthenticationMethods is not supported"
393 "with SSH protocol 1");
394 if (authenticated &&
395 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000396 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100397 debug3("%s: method %s: partial", __func__,
398 auth_method);
399 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100400 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100401 }
402 }
403
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 if (authenticated) {
405 if (!(ent->flags & MON_AUTHDECIDE))
406 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000407 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 if (authctxt->pw->pw_uid == 0 &&
409 !auth_root_allowed(auth_method))
410 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000411#ifdef USE_PAM
412 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100413 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000414 Buffer m;
415
416 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100417 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000418 MONITOR_REQ_PAM_ACCOUNT, &m);
419 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
420 buffer_free(&m);
421 }
422#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100424 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100425 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000426 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427 if (!authenticated)
428 authctxt->failures++;
429 }
430 }
431
432 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000433 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100434 if (strcmp(auth_method, "unknown") == 0)
435 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000436
437 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000438 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000440 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000441
Damien Miller6a1937e2012-12-12 10:44:38 +1100442 /* Drain any buffered messages from the child */
443 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
444 ;
445
Damien Miller8f0bf232011-06-20 14:42:23 +1000446 close(pmonitor->m_sendfd);
447 close(pmonitor->m_log_recvfd);
448 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000449}
450
Damien Millerbe64d432003-05-14 19:31:12 +1000451static void
452monitor_set_child_handler(pid_t pid)
453{
454 monitor_child_pid = pid;
455}
456
457static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000458monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000459{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000460 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000461}
462
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000464monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465{
Damien Miller8f0bf232011-06-20 14:42:23 +1000466 close(pmonitor->m_recvfd);
467 pmonitor->m_recvfd = -1;
468
Damien Millerbe64d432003-05-14 19:31:12 +1000469 monitor_set_child_handler(pmonitor->m_pid);
470 signal(SIGHUP, &monitor_child_handler);
471 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000472 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000473
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474 if (compat20) {
475 mon_dispatch = mon_dispatch_postauth20;
476
477 /* Permit requests for moduli and signatures */
478 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
479 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
480 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000481 } else {
482 mon_dispatch = mon_dispatch_postauth15;
483 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
484 }
485 if (!no_pty_flag) {
486 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
487 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
488 }
489
490 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000491 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492}
493
494void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000495monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000496{
Damien Miller2d6b8332002-06-21 15:59:49 +1000497 if (options.compression) {
498 /* The member allocation is not visible, so sync it */
499 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
500 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501}
502
Damien Miller8f0bf232011-06-20 14:42:23 +1000503static int
504monitor_read_log(struct monitor *pmonitor)
505{
506 Buffer logmsg;
507 u_int len, level;
508 char *msg;
509
510 buffer_init(&logmsg);
511
512 /* Read length */
513 buffer_append_space(&logmsg, 4);
514 if (atomicio(read, pmonitor->m_log_recvfd,
515 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
516 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100517 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000518 debug("%s: child log fd closed", __func__);
519 close(pmonitor->m_log_recvfd);
520 pmonitor->m_log_recvfd = -1;
521 return -1;
522 }
523 fatal("%s: log fd read: %s", __func__, strerror(errno));
524 }
525 len = buffer_get_int(&logmsg);
526 if (len <= 4 || len > 8192)
527 fatal("%s: invalid log message length %u", __func__, len);
528
529 /* Read severity, message */
530 buffer_clear(&logmsg);
531 buffer_append_space(&logmsg, len);
532 if (atomicio(read, pmonitor->m_log_recvfd,
533 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
534 fatal("%s: log fd read: %s", __func__, strerror(errno));
535
536 /* Log it */
537 level = buffer_get_int(&logmsg);
538 msg = buffer_get_string(&logmsg, NULL);
539 if (log_level_name(level) == NULL)
540 fatal("%s: invalid log level %u (corrupted message?)",
541 __func__, level);
542 do_log2(level, "%s [preauth]", msg);
543
544 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000545 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000546
547 return 0;
548}
549
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000551monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552 struct mon_table **pent)
553{
554 Buffer m;
555 int ret;
556 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000557 struct pollfd pfd[2];
558
559 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100560 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000561 pfd[0].fd = pmonitor->m_sendfd;
562 pfd[0].events = POLLIN;
563 pfd[1].fd = pmonitor->m_log_recvfd;
564 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000565 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
566 if (errno == EINTR || errno == EAGAIN)
567 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000568 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000569 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000570 if (pfd[1].revents) {
571 /*
572 * Drain all log messages before processing next
573 * monitor request.
574 */
575 monitor_read_log(pmonitor);
576 continue;
577 }
578 if (pfd[0].revents)
579 break; /* Continues below */
580 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581
582 buffer_init(&m);
583
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000584 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 type = buffer_get_char(&m);
586
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000587 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 while (ent->f != NULL) {
590 if (ent->type == type)
591 break;
592 ent++;
593 }
594
595 if (ent->f != NULL) {
596 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000597 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000599 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000600 buffer_free(&m);
601
602 /* The child may use this request only once, disable it */
603 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000604 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 type);
606 ent->flags &= ~MON_PERMIT;
607 }
608
609 if (pent != NULL)
610 *pent = ent;
611
612 return ret;
613 }
614
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000615 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
617 /* NOTREACHED */
618 return (-1);
619}
620
621/* allowed key state */
622static int
623monitor_allowed_key(u_char *blob, u_int bloblen)
624{
625 /* make sure key is allowed */
626 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000627 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628 return (0);
629 return (1);
630}
631
632static void
633monitor_reset_key_state(void)
634{
635 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000636 free(key_blob);
637 free(hostbased_cuser);
638 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 key_blob = NULL;
640 key_bloblen = 0;
641 key_blobtype = MM_NOKEY;
642 hostbased_cuser = NULL;
643 hostbased_chost = NULL;
644}
645
Damien Miller1f0311c2014-05-15 14:24:09 +1000646#ifdef WITH_OPENSSL
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000648mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649{
650 DH *dh;
651 int min, want, max;
652
653 min = buffer_get_int(m);
654 want = buffer_get_int(m);
655 max = buffer_get_int(m);
656
657 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000658 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 /* We need to check here, too, in case the child got corrupted */
660 if (max < min || want < min || max < want)
661 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000662 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663
664 buffer_clear(m);
665
666 dh = choose_dh(min, want, max);
667 if (dh == NULL) {
668 buffer_put_char(m, 0);
669 return (0);
670 } else {
671 /* Send first bignum */
672 buffer_put_char(m, 1);
673 buffer_put_bignum2(m, dh->p);
674 buffer_put_bignum2(m, dh->g);
675
676 DH_free(dh);
677 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000678 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679 return (0);
680}
Damien Miller1f0311c2014-05-15 14:24:09 +1000681#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682
Damien Miller85b45e02013-07-20 13:21:52 +1000683extern AuthenticationConnection *auth_conn;
684
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000686mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687{
688 Key *key;
689 u_char *p;
690 u_char *signature;
691 u_int siglen, datlen;
692 int keyid;
693
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000694 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695
696 keyid = buffer_get_int(m);
697 p = buffer_get_string(m, &datlen);
698
Damien Millera63128d2006-03-15 12:08:28 +1100699 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000700 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
701 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100702 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000703 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000704 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
Ben Lindstromf67e0772002-06-06 20:58:19 +0000706 /* save session id, it will be passed on the first call */
707 if (session_id2_len == 0) {
708 session_id2_len = datlen;
709 session_id2 = xmalloc(session_id2_len);
710 memcpy(session_id2, p, session_id2_len);
711 }
712
Damien Miller85b45e02013-07-20 13:21:52 +1000713 if ((key = get_hostkey_by_index(keyid)) != NULL) {
714 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
715 fatal("%s: key_sign failed", __func__);
716 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
717 auth_conn != NULL) {
718 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
719 datlen) < 0)
720 fatal("%s: ssh_agent_sign failed", __func__);
721 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000722 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723
Ben Lindstromd5502182002-06-27 00:12:57 +0000724 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725
726 buffer_clear(m);
727 buffer_put_string(m, signature, siglen);
728
Darren Tuckera627d422013-06-02 07:31:17 +1000729 free(p);
730 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000732 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
734 /* Turn on permissions for getpwnam */
735 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
736
737 return (0);
738}
739
740/* Retrieves the password entry and also checks if the user is permitted */
741
742int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000743mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000745 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746 struct passwd *pwent;
747 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000748 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000750 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751
752 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000753 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754
Darren Tuckerb09b6772004-06-22 15:06:46 +1000755 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
Darren Tuckerb09b6772004-06-22 15:06:46 +1000757 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758
Darren Tuckerb09b6772004-06-22 15:06:46 +1000759 authctxt->user = xstrdup(username);
760 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000761 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762
763 buffer_clear(m);
764
765 if (pwent == NULL) {
766 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100767 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 goto out;
769 }
770
771 allowed = 1;
772 authctxt->pw = pwent;
773 authctxt->valid = 1;
774
775 buffer_put_char(m, 1);
776 buffer_put_string(m, pwent, sizeof(struct passwd));
777 buffer_put_cstring(m, pwent->pw_name);
778 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000779#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000780 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000781#endif
782#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000784#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785 buffer_put_cstring(m, pwent->pw_dir);
786 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100787
788 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100789 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000790
791#define M_CP_STROPT(x) do { \
792 if (options.x != NULL) \
793 buffer_put_cstring(m, options.x); \
794 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000795#define M_CP_STRARRAYOPT(x, nx) do { \
796 for (i = 0; i < options.nx; i++) \
797 buffer_put_cstring(m, options.x[i]); \
798 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000799 /* See comment in servconf.h */
800 COPY_MATCH_STRING_OPTS();
801#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000802#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100803
804 /* Create valid auth method lists */
805 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
806 /*
807 * The monitor will continue long enough to let the child
808 * run to it's packet_disconnect(), but it must not allow any
809 * authentication to succeed.
810 */
811 debug("%s: no valid authentication method lists", __func__);
812 }
813
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000814 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000815 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000816
817 /* For SSHv1 allow authentication now */
818 if (!compat20)
819 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000820 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000821 /* Allow service/style information on the auth context */
822 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000823 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
824 }
Damien Millera33501b2002-05-08 12:24:42 +1000825#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000826 if (options.use_pam)
827 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000828#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000829
830 return (0);
831}
832
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000833int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000834{
835 char *banner;
836
837 buffer_clear(m);
838 banner = auth2_read_banner();
839 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000840 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000841 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000842
843 return (0);
844}
845
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000847mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848{
849 monitor_permit_authentications(1);
850
851 authctxt->service = buffer_get_string(m, NULL);
852 authctxt->style = buffer_get_string(m, NULL);
853 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000854 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855
856 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000857 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858 authctxt->style = NULL;
859 }
860
861 return (0);
862}
863
864int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000865mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866{
867 static int call_count;
868 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000869 int authenticated;
870 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000871
872 passwd = buffer_get_string(m, &plen);
873 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000874 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000875 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100876 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000877 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878
879 buffer_clear(m);
880 buffer_put_int(m, authenticated);
881
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000882 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000883 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000884
885 call_count++;
886 if (plen == 0 && call_count == 1)
887 auth_method = "none";
888 else
889 auth_method = "password";
890
891 /* Causes monitor loop to terminate if authenticated */
892 return (authenticated);
893}
894
895#ifdef BSD_AUTH
896int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000897mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898{
899 char *name, *infotxt;
900 u_int numprompts;
901 u_int *echo_on;
902 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100903 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904
Damien Millerb7df3af2003-02-24 11:55:46 +1100905 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
906 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000907
908 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100909 buffer_put_int(m, success);
910 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911 buffer_put_cstring(m, prompts[0]);
912
Damien Millerb7df3af2003-02-24 11:55:46 +1100913 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000914 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915
Damien Millerb7df3af2003-02-24 11:55:46 +1100916 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000917 free(name);
918 free(infotxt);
919 free(prompts);
920 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921 }
922
923 return (0);
924}
925
926int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000927mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928{
929 char *response;
930 int authok;
931
932 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000933 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000934
935 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000936 authok = options.challenge_response_authentication &&
937 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000939 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000940 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
942 buffer_clear(m);
943 buffer_put_int(m, authok);
944
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000945 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000946 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947
Damien Miller91a55f22013-04-23 15:18:10 +1000948 if (compat20) {
949 auth_method = "keyboard-interactive";
950 auth_submethod = "bsdauth";
951 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100952 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
954 return (authok != 0);
955}
956#endif
957
958#ifdef SKEY
959int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961{
962 struct skey skey;
963 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100964 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000966 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
967 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
969 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100970 buffer_put_int(m, success);
971 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972 buffer_put_cstring(m, challenge);
973
Damien Millerb7df3af2003-02-24 11:55:46 +1100974 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000975 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
977 return (0);
978}
979
980int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000981mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000982{
983 char *response;
984 int authok;
985
986 response = buffer_get_string(m, NULL);
987
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000988 authok = (options.challenge_response_authentication &&
989 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 skey_haskey(authctxt->pw->pw_name) == 0 &&
991 skey_passcheck(authctxt->pw->pw_name, response) != -1);
992
Darren Tuckerf60845f2013-06-02 08:07:31 +1000993 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
995 buffer_clear(m);
996 buffer_put_int(m, authok);
997
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000998 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000999 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001000
1001 auth_method = "skey";
1002
1003 return (authok != 0);
1004}
1005#endif
1006
Damien Miller79418552002-04-23 20:28:48 +10001007#ifdef USE_PAM
1008int
Darren Tucker7eda5922004-06-22 13:22:50 +10001009mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001010{
Damien Miller4e448a32003-05-14 15:11:48 +10001011 if (!options.use_pam)
1012 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1013
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001014 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001015
Damien Miller1f499fd2003-08-25 13:08:49 +10001016 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1017
Damien Miller79418552002-04-23 20:28:48 +10001018 return (0);
1019}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001020
Damien Miller1f499fd2003-08-25 13:08:49 +10001021int
Darren Tucker7eda5922004-06-22 13:22:50 +10001022mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001023{
1024 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001025
Damien Miller1f499fd2003-08-25 13:08:49 +10001026 if (!options.use_pam)
1027 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1028
1029 ret = do_pam_account();
1030
1031 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001032 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001033
Darren Tucker7eda5922004-06-22 13:22:50 +10001034 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001035
1036 return (ret);
1037}
1038
Damien Miller4f9f42a2003-05-10 19:28:02 +10001039static void *sshpam_ctxt, *sshpam_authok;
1040extern KbdintDevice sshpam_device;
1041
1042int
Darren Tucker7eda5922004-06-22 13:22:50 +10001043mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001044{
1045
1046 debug3("%s", __func__);
1047 authctxt->user = buffer_get_string(m, NULL);
1048 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1049 sshpam_authok = NULL;
1050 buffer_clear(m);
1051 if (sshpam_ctxt != NULL) {
1052 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1053 buffer_put_int(m, 1);
1054 } else {
1055 buffer_put_int(m, 0);
1056 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001057 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001058 return (0);
1059}
1060
1061int
Darren Tucker7eda5922004-06-22 13:22:50 +10001062mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001063{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001064 char *name = NULL, *info = NULL, **prompts = NULL;
1065 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001066 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001067
1068 debug3("%s", __func__);
1069 sshpam_authok = NULL;
1070 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1071 if (ret == 0 && num == 0)
1072 sshpam_authok = sshpam_ctxt;
1073 if (num > 1 || name == NULL || info == NULL)
1074 ret = -1;
1075 buffer_clear(m);
1076 buffer_put_int(m, ret);
1077 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001078 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001080 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001081 buffer_put_int(m, num);
1082 for (i = 0; i < num; ++i) {
1083 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001084 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001085 buffer_put_int(m, echo_on[i]);
1086 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001087 free(prompts);
1088 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001089 auth_method = "keyboard-interactive";
1090 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001091 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 return (0);
1093}
1094
1095int
Darren Tucker7eda5922004-06-22 13:22:50 +10001096mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001097{
1098 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001099 u_int i, num;
1100 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001101
1102 debug3("%s", __func__);
1103 sshpam_authok = NULL;
1104 num = buffer_get_int(m);
1105 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001106 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001107 for (i = 0; i < num; ++i)
1108 resp[i] = buffer_get_string(m, NULL);
1109 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1110 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001111 free(resp[i]);
1112 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001113 } else {
1114 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1115 }
1116 buffer_clear(m);
1117 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001118 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001119 auth_method = "keyboard-interactive";
1120 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001121 if (ret == 0)
1122 sshpam_authok = sshpam_ctxt;
1123 return (0);
1124}
1125
1126int
Darren Tucker7eda5922004-06-22 13:22:50 +10001127mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001128{
1129
1130 debug3("%s", __func__);
1131 (sshpam_device.free_ctx)(sshpam_ctxt);
1132 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001133 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001134 auth_method = "keyboard-interactive";
1135 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001136 return (sshpam_authok == sshpam_ctxt);
1137}
Damien Miller79418552002-04-23 20:28:48 +10001138#endif
1139
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001141mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001142{
1143 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001144 char *cuser, *chost;
1145 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146 u_int bloblen;
1147 enum mm_keytype type = 0;
1148 int allowed = 0;
1149
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001150 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151
1152 type = buffer_get_int(m);
1153 cuser = buffer_get_string(m, NULL);
1154 chost = buffer_get_string(m, NULL);
1155 blob = buffer_get_string(m, &bloblen);
1156
1157 key = key_from_blob(blob, bloblen);
1158
1159 if ((compat20 && type == MM_RSAHOSTKEY) ||
1160 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001161 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001163 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164
Damien Miller3e3b5142003-11-17 21:13:40 +11001165 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001166 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001168 allowed = options.pubkey_authentication &&
1169 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001170 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001171 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001172 if (options.pubkey_authentication && allowed != 1)
1173 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 break;
1175 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001176 allowed = options.hostbased_authentication &&
1177 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001179 pubkey_auth_info(authctxt, key,
1180 "client user \"%.100s\", client host \"%.100s\"",
1181 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001182 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001184#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 case MM_RSAHOSTKEY:
1186 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001187 allowed = options.rhosts_rsa_authentication &&
1188 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001190 if (options.rhosts_rsa_authentication && allowed != 1)
1191 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001192 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193 break;
Damien Miller1f0311c2014-05-15 14:24:09 +10001194#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001196 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197 break;
1198 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199 }
Damien Miller00111382003-03-10 11:21:17 +11001200 if (key != NULL)
1201 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
1203 /* clear temporarily storage (used by verify) */
1204 monitor_reset_key_state();
1205
1206 if (allowed) {
1207 /* Save temporarily for comparison in verify */
1208 key_blob = blob;
1209 key_bloblen = bloblen;
1210 key_blobtype = type;
1211 hostbased_cuser = cuser;
1212 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001213 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001214 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001215 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001216 free(blob);
1217 free(cuser);
1218 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219 }
1220
1221 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001222 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223
1224 buffer_clear(m);
1225 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001226 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001228 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229
1230 if (type == MM_RSAHOSTKEY)
1231 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1232
1233 return (0);
1234}
1235
1236static int
1237monitor_valid_userblob(u_char *data, u_int datalen)
1238{
1239 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001240 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 u_int len;
1242 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243
1244 buffer_init(&b);
1245 buffer_append(&b, data, datalen);
1246
1247 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001248 p = buffer_ptr(&b);
1249 len = buffer_len(&b);
1250 if ((session_id2 == NULL) ||
1251 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001252 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001253 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 buffer_consume(&b, session_id2_len);
1255 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001256 p = buffer_get_string(&b, &len);
1257 if ((session_id2 == NULL) ||
1258 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001259 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001261 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 }
1263 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1264 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001265 p = buffer_get_cstring(&b, NULL);
1266 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1267 authctxt->style ? ":" : "",
1268 authctxt->style ? authctxt->style : "");
1269 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001270 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001271 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 fail++;
1273 }
Darren Tuckera627d422013-06-02 07:31:17 +10001274 free(userstyle);
1275 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276 buffer_skip_string(&b);
1277 if (datafellows & SSH_BUG_PKAUTH) {
1278 if (!buffer_get_char(&b))
1279 fail++;
1280 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001281 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 if (strcmp("publickey", p) != 0)
1283 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001284 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 if (!buffer_get_char(&b))
1286 fail++;
1287 buffer_skip_string(&b);
1288 }
1289 buffer_skip_string(&b);
1290 if (buffer_len(&b) != 0)
1291 fail++;
1292 buffer_free(&b);
1293 return (fail == 0);
1294}
1295
1296static int
Damien Millera10f5612002-09-12 09:49:15 +10001297monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1298 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299{
1300 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001301 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302 u_int len;
1303 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304
1305 buffer_init(&b);
1306 buffer_append(&b, data, datalen);
1307
Ben Lindstromf67e0772002-06-06 20:58:19 +00001308 p = buffer_get_string(&b, &len);
1309 if ((session_id2 == NULL) ||
1310 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001311 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001313 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001314
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1316 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001317 p = buffer_get_cstring(&b, NULL);
1318 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1319 authctxt->style ? ":" : "",
1320 authctxt->style ? authctxt->style : "");
1321 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001322 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001323 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 fail++;
1325 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001326 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001327 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001328 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001329 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330 if (strcmp(p, "hostbased") != 0)
1331 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001332 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333 buffer_skip_string(&b); /* pkalg */
1334 buffer_skip_string(&b); /* pkblob */
1335
1336 /* verify client host, strip trailing dot if necessary */
1337 p = buffer_get_string(&b, NULL);
1338 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1339 p[len - 1] = '\0';
1340 if (strcmp(p, chost) != 0)
1341 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001342 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343
1344 /* verify client user */
1345 p = buffer_get_string(&b, NULL);
1346 if (strcmp(p, cuser) != 0)
1347 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001348 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349
1350 if (buffer_len(&b) != 0)
1351 fail++;
1352 buffer_free(&b);
1353 return (fail == 0);
1354}
1355
1356int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001357mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358{
1359 Key *key;
1360 u_char *signature, *data, *blob;
1361 u_int signaturelen, datalen, bloblen;
1362 int verified = 0;
1363 int valid_data = 0;
1364
1365 blob = buffer_get_string(m, &bloblen);
1366 signature = buffer_get_string(m, &signaturelen);
1367 data = buffer_get_string(m, &datalen);
1368
1369 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001370 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001371 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 key = key_from_blob(blob, bloblen);
1374 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001375 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
1377 switch (key_blobtype) {
1378 case MM_USERKEY:
1379 valid_data = monitor_valid_userblob(data, datalen);
1380 break;
1381 case MM_HOSTKEY:
1382 valid_data = monitor_valid_hostbasedblob(data, datalen,
1383 hostbased_cuser, hostbased_chost);
1384 break;
1385 default:
1386 valid_data = 0;
1387 break;
1388 }
1389 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001390 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391
1392 verified = key_verify(key, signature, signaturelen, data, datalen);
1393 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001394 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001397 free(blob);
1398 free(signature);
1399 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
Ben Lindstrome1c09122002-06-23 00:38:24 +00001401 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1402
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403 monitor_reset_key_state();
1404
1405 buffer_clear(m);
1406 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001407 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408
Darren Tuckerfbba7352006-11-07 23:16:08 +11001409 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410}
1411
1412static void
1413mm_record_login(Session *s, struct passwd *pw)
1414{
1415 socklen_t fromlen;
1416 struct sockaddr_storage from;
1417
1418 /*
1419 * Get IP address of client. If the connection is not a socket, let
1420 * the address be 0.0.0.0.
1421 */
1422 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001423 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001426 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001428 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429 }
1430 }
1431 /* Record that there was a login on that tty from the remote host. */
1432 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001433 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001434 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435}
1436
1437static void
1438mm_session_close(Session *s)
1439{
Damien Miller04bd8b02003-05-25 14:38:33 +10001440 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001442 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443 session_pty_cleanup2(s);
1444 }
Damien Miller7207f642008-05-19 15:34:50 +10001445 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446}
1447
1448int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001449mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001450{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001451 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452 Session *s;
1453 int res, fd0;
1454
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001455 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001456
1457 buffer_clear(m);
1458 s = session_new();
1459 if (s == NULL)
1460 goto error;
1461 s->authctxt = authctxt;
1462 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001463 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1465 if (res == 0)
1466 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 pty_setowner(authctxt->pw, s->tty);
1468
1469 buffer_put_int(m, 1);
1470 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
1472 /* We need to trick ttyslot */
1473 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
1476 mm_record_login(s, authctxt->pw);
1477
1478 /* Now we can close the file descriptor again */
1479 close(0);
1480
Darren Tucker09991742004-07-17 17:05:14 +10001481 /* send messages generated by record_login */
1482 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1483 buffer_clear(&loginmsg);
1484
1485 mm_request_send(sock, MONITOR_ANS_PTY, m);
1486
Damien Miller54fd7cf2007-09-17 12:04:08 +10001487 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1488 mm_send_fd(sock, s->ttyfd) == -1)
1489 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001490
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491 /* make sure nothing uses fd 0 */
1492 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001493 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001495 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496
1497 /* slave is not needed */
1498 close(s->ttyfd);
1499 s->ttyfd = s->ptyfd;
1500 /* no need to dup() because nobody closes ptyfd */
1501 s->ptymaster = s->ptyfd;
1502
Damien Miller9ab00b42006-08-05 12:40:11 +10001503 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504
1505 return (0);
1506
1507 error:
1508 if (s != NULL)
1509 mm_session_close(s);
1510 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001511 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512 return (0);
1513}
1514
1515int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001516mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517{
1518 Session *s;
1519 char *tty;
1520
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001521 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522
1523 tty = buffer_get_string(m, NULL);
1524 if ((s = session_by_tty(tty)) != NULL)
1525 mm_session_close(s);
1526 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001527 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528 return (0);
1529}
1530
Damien Miller1f0311c2014-05-15 14:24:09 +10001531#ifdef WITH_SSH1
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001533mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534{
1535 BIGNUM *p;
1536 int rsafail;
1537
1538 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001539 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540
1541 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001542 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001543
1544 buffer_get_bignum2(m, p);
1545
1546 rsafail = ssh1_session_key(p);
1547
1548 buffer_clear(m);
1549 buffer_put_int(m, rsafail);
1550 buffer_put_bignum2(m, p);
1551
1552 BN_clear_free(p);
1553
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001554 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001555
1556 /* Turn on permissions for sessid passing */
1557 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1558
1559 return (0);
1560}
1561
1562int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001563mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564{
1565 int i;
1566
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001567 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001568
1569 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001570 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571 for (i = 0; i < 16; i++)
1572 session_id[i] = buffer_get_char(m);
1573
1574 /* Turn on permissions for getpwnam */
1575 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1576
1577 return (0);
1578}
1579
1580int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001581mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582{
1583 BIGNUM *client_n;
1584 Key *key = NULL;
1585 u_char *blob = NULL;
1586 u_int blen = 0;
1587 int allowed = 0;
1588
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001589 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590
Damien Miller7a8f5b32006-03-31 23:14:23 +11001591 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001592 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001594 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595 buffer_get_bignum2(m, client_n);
1596 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1597 BN_clear_free(client_n);
1598 }
1599 buffer_clear(m);
1600 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001601 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602
1603 /* clear temporarily storage (used by generate challenge) */
1604 monitor_reset_key_state();
1605
1606 if (allowed && key != NULL) {
1607 key->type = KEY_RSA; /* cheat for key_to_blob */
1608 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001609 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610 buffer_put_string(m, blob, blen);
1611
1612 /* Save temporarily for comparison in verify */
1613 key_blob = blob;
1614 key_bloblen = blen;
1615 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616 }
Damien Miller00111382003-03-10 11:21:17 +11001617 if (key != NULL)
1618 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001619
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001620 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001621
1622 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1623 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1624 return (0);
1625}
1626
1627int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001628mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629{
1630 Key *key = NULL;
1631 u_char *blob;
1632 u_int blen;
1633
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001634 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635
1636 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001637 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 blob = buffer_get_string(m, &blen);
1639 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001640 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001641 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001642 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001644 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001645 if (key->type != KEY_RSA)
1646 fatal("%s: received bad key type %d", __func__, key->type);
1647 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001648 if (ssh1_challenge)
1649 BN_clear_free(ssh1_challenge);
1650 ssh1_challenge = auth_rsa_generate_challenge(key);
1651
1652 buffer_clear(m);
1653 buffer_put_bignum2(m, ssh1_challenge);
1654
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001655 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001656 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657
1658 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001659
Darren Tuckera627d422013-06-02 07:31:17 +10001660 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001661 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662 return (0);
1663}
1664
1665int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001666mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667{
1668 Key *key = NULL;
1669 u_char *blob, *response;
1670 u_int blen, len;
1671 int success;
1672
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001673 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674
1675 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001676 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001678 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679
1680 blob = buffer_get_string(m, &blen);
1681 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001682 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001684 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001686 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 response = buffer_get_string(m, &len);
1688 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001689 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1691
Darren Tuckera627d422013-06-02 07:31:17 +10001692 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001694 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695
1696 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1697
1698 /* reset state */
1699 BN_clear_free(ssh1_challenge);
1700 ssh1_challenge = NULL;
1701 monitor_reset_key_state();
1702
1703 buffer_clear(m);
1704 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001705 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706
1707 return (success);
1708}
Damien Miller1f0311c2014-05-15 14:24:09 +10001709#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710
1711int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001712mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001713{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001714 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 int res, status;
1716
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001717 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718
1719 /* The child is terminating */
1720 session_destroy_all(&mm_session_close);
1721
Darren Tucker52358d62008-03-11 22:58:25 +11001722#ifdef USE_PAM
1723 if (options.use_pam)
1724 sshpam_cleanup();
1725#endif
1726
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001727 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001728 if (errno != EINTR)
1729 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001730
1731 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1732
1733 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001734 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735}
1736
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001737#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001738/* Report that an audit event occurred */
1739int
1740mm_answer_audit_event(int socket, Buffer *m)
1741{
1742 ssh_audit_event_t event;
1743
1744 debug3("%s entering", __func__);
1745
1746 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001747 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001748 case SSH_AUTH_FAIL_PUBKEY:
1749 case SSH_AUTH_FAIL_HOSTBASED:
1750 case SSH_AUTH_FAIL_GSSAPI:
1751 case SSH_LOGIN_EXCEED_MAXTRIES:
1752 case SSH_LOGIN_ROOT_DENIED:
1753 case SSH_CONNECTION_CLOSE:
1754 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001755 audit_event(event);
1756 break;
1757 default:
1758 fatal("Audit event type %d not permitted", event);
1759 }
1760
1761 return (0);
1762}
1763
1764int
1765mm_answer_audit_command(int socket, Buffer *m)
1766{
1767 u_int len;
1768 char *cmd;
1769
1770 debug3("%s entering", __func__);
1771 cmd = buffer_get_string(m, &len);
1772 /* sanity check command, if so how? */
1773 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001774 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001775 return (0);
1776}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001777#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001778
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001779void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001780monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781{
1782 if (compat20) {
1783 set_newkeys(MODE_IN);
1784 set_newkeys(MODE_OUT);
1785 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001786 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001787 packet_set_encryption_key(child_state.ssh1key,
1788 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001789 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001790 }
1791
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001792 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001793 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001794 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001796 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001797
1798 if (!compat20) {
1799 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001800 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001802 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803 }
1804
1805 memcpy(&incoming_stream, &child_state.incoming,
1806 sizeof(incoming_stream));
1807 memcpy(&outgoing_stream, &child_state.outgoing,
1808 sizeof(outgoing_stream));
1809
1810 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001811 if (options.compression)
1812 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001813
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001814 if (options.rekey_limit || options.rekey_interval)
1815 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1816 (time_t)options.rekey_interval);
1817
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818 /* Network I/O buffers */
1819 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001820 buffer_clear(packet_get_input());
1821 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Damien Millera5103f42014-02-04 11:20:14 +11001822 explicit_bzero(child_state.input, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001823 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001824
Darren Tuckerf7288d72009-06-21 18:12:20 +10001825 buffer_clear(packet_get_output());
1826 buffer_append(packet_get_output(), child_state.output,
1827 child_state.olen);
Damien Millera5103f42014-02-04 11:20:14 +11001828 explicit_bzero(child_state.output, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001829 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001830
1831 /* Roaming */
1832 if (compat20)
1833 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001834}
1835
1836static Kex *
1837mm_get_kex(Buffer *m)
1838{
1839 Kex *kex;
1840 void *blob;
1841 u_int bloblen;
1842
Damien Miller07d86be2006-03-26 14:19:21 +11001843 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001844 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001845 if (session_id2 == NULL ||
1846 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001847 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001848 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001849 kex->we_need = buffer_get_int(m);
Damien Miller1f0311c2014-05-15 14:24:09 +10001850#ifdef WITH_OPENSSL
Damien Millerb062c292003-03-24 09:12:09 +11001851 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001852 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001853 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001854 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001855 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Damien Miller1f0311c2014-05-15 14:24:09 +10001856#endif
Damien Millerc9831972013-11-07 12:00:23 +11001857 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858 kex->server = 1;
1859 kex->hostkey_type = buffer_get_int(m);
1860 kex->kex_type = buffer_get_int(m);
1861 blob = buffer_get_string(m, &bloblen);
1862 buffer_init(&kex->my);
1863 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001864 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001865 blob = buffer_get_string(m, &bloblen);
1866 buffer_init(&kex->peer);
1867 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001868 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001869 kex->done = 1;
1870 kex->flags = buffer_get_int(m);
1871 kex->client_version_string = buffer_get_string(m, NULL);
1872 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001873 kex->load_host_public_key=&get_hostkey_public_by_type;
1874 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001876 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877
1878 return (kex);
1879}
1880
1881/* This function requries careful sanity checking */
1882
1883void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001884mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001885{
1886 Buffer m;
1887 u_char *blob, *p;
1888 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001889 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001890 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001891
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001892 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001893
1894 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001895 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001896 if (!compat20) {
1897 child_state.ssh1protoflags = buffer_get_int(&m);
1898 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001899 child_state.ssh1key = buffer_get_string(&m,
1900 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001901 child_state.ivout = buffer_get_string(&m,
1902 &child_state.ivoutlen);
1903 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1904 goto skip;
1905 } else {
1906 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001907 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908 }
1909
1910 blob = buffer_get_string(&m, &bloblen);
1911 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001912 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001913
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001914 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001915 blob = buffer_get_string(&m, &bloblen);
1916 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001917 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001918
1919 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001920 seqnr = buffer_get_int(&m);
1921 blocks = buffer_get_int64(&m);
1922 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001923 bytes = buffer_get_int64(&m);
1924 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001925 seqnr = buffer_get_int(&m);
1926 blocks = buffer_get_int64(&m);
1927 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001928 bytes = buffer_get_int64(&m);
1929 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930
1931 skip:
1932 /* Get the key context */
1933 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1934 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1935
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001936 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937 /* Get compression state */
1938 p = buffer_get_string(&m, &plen);
1939 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001940 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001941 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001942 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943
1944 p = buffer_get_string(&m, &plen);
1945 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001946 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001947 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001948 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001949
1950 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001951 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001952 child_state.input = buffer_get_string(&m, &child_state.ilen);
1953 child_state.output = buffer_get_string(&m, &child_state.olen);
1954
Darren Tuckerc5564e12009-06-21 18:53:53 +10001955 /* Roaming */
1956 if (compat20) {
1957 child_state.sent_bytes = buffer_get_int64(&m);
1958 child_state.recv_bytes = buffer_get_int64(&m);
1959 }
1960
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001961 buffer_free(&m);
1962}
1963
1964
1965/* Allocation functions for zlib */
1966void *
1967mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1968{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001969 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001970 void *address;
1971
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001972 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001973 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1974
1975 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001976
1977 return (address);
1978}
1979
1980void
1981mm_zfree(struct mm_master *mm, void *address)
1982{
1983 mm_free(mm, address);
1984}
1985
1986void
1987mm_init_compression(struct mm_master *mm)
1988{
1989 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1990 outgoing_stream.zfree = (free_func)mm_zfree;
1991 outgoing_stream.opaque = mm;
1992
1993 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1994 incoming_stream.zfree = (free_func)mm_zfree;
1995 incoming_stream.opaque = mm;
1996}
1997
1998/* XXX */
1999
2000#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10002001 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002002 fatal("fcntl(%d, F_SETFD)", x); \
2003} while (0)
2004
2005static void
Damien Miller8f0bf232011-06-20 14:42:23 +10002006monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002007{
Damien Miller8f0bf232011-06-20 14:42:23 +10002008 int pair[2];
2009
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002010 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002011 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002012 FD_CLOSEONEXEC(pair[0]);
2013 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002014 mon->m_recvfd = pair[0];
2015 mon->m_sendfd = pair[1];
2016
2017 if (do_logfds) {
2018 if (pipe(pair) == -1)
2019 fatal("%s: pipe: %s", __func__, strerror(errno));
2020 FD_CLOSEONEXEC(pair[0]);
2021 FD_CLOSEONEXEC(pair[1]);
2022 mon->m_log_recvfd = pair[0];
2023 mon->m_log_sendfd = pair[1];
2024 } else
2025 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002026}
2027
2028#define MM_MEMSIZE 65536
2029
2030struct monitor *
2031monitor_init(void)
2032{
2033 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002034
Damien Miller07d86be2006-03-26 14:19:21 +11002035 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002036
Damien Miller8f0bf232011-06-20 14:42:23 +10002037 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002038
2039 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002040 if (options.compression) {
2041 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2042 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002043
Damien Miller2d6b8332002-06-21 15:59:49 +10002044 /* Compression needs to share state across borders */
2045 mm_init_compression(mon->m_zlib);
2046 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002047
2048 return mon;
2049}
2050
2051void
2052monitor_reinit(struct monitor *mon)
2053{
Damien Miller8f0bf232011-06-20 14:42:23 +10002054 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002055}
Darren Tucker0efd1552003-08-26 11:49:55 +10002056
2057#ifdef GSSAPI
2058int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002059mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002060{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002061 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002062 OM_uint32 major;
2063 u_int len;
2064
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002065 goid.elements = buffer_get_string(m, &len);
2066 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002067
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002068 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002069
Darren Tuckera627d422013-06-02 07:31:17 +10002070 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002071
2072 buffer_clear(m);
2073 buffer_put_int(m, major);
2074
Damien Miller6fd6def2005-11-05 15:07:05 +11002075 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002076
2077 /* Now we have a context, enable the step */
2078 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2079
2080 return (0);
2081}
2082
2083int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002084mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002085{
2086 gss_buffer_desc in;
2087 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002088 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002089 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002090 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002091
Darren Tucker600ad8d2003-08-26 12:10:48 +10002092 in.value = buffer_get_string(m, &len);
2093 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002094 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002095 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002096
2097 buffer_clear(m);
2098 buffer_put_int(m, major);
2099 buffer_put_string(m, out.value, out.length);
2100 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002101 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002102
2103 gss_release_buffer(&minor, &out);
2104
Damien Miller6fd6def2005-11-05 15:07:05 +11002105 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002106 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2107 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002108 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002109 }
2110 return (0);
2111}
2112
2113int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002114mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002115{
2116 gss_buffer_desc gssbuf, mic;
2117 OM_uint32 ret;
2118 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002119
Damien Miller0425d402003-11-17 22:18:21 +11002120 gssbuf.value = buffer_get_string(m, &len);
2121 gssbuf.length = len;
2122 mic.value = buffer_get_string(m, &len);
2123 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002124
Damien Miller0425d402003-11-17 22:18:21 +11002125 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002126
Darren Tuckera627d422013-06-02 07:31:17 +10002127 free(gssbuf.value);
2128 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002129
Damien Miller0425d402003-11-17 22:18:21 +11002130 buffer_clear(m);
2131 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002132
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002133 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002134
Damien Miller0425d402003-11-17 22:18:21 +11002135 if (!GSS_ERROR(ret))
2136 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002137
Damien Miller0425d402003-11-17 22:18:21 +11002138 return (0);
2139}
2140
2141int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002142mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002143{
2144 int authenticated;
2145
2146 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2147
2148 buffer_clear(m);
2149 buffer_put_int(m, authenticated);
2150
2151 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002152 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002153
Damien Miller6fd6def2005-11-05 15:07:05 +11002154 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002155
2156 /* Monitor loop will terminate if authenticated */
2157 return (authenticated);
2158}
2159#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002160