blob: 531c4f9a8518f93d681d3d9fda1733a6fc33228d [file] [log] [blame]
Damien Millera5103f42014-02-04 11:20:14 +11001/* $OpenBSD: monitor.c,v 1.131 2014/02/02 03:44:31 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien 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 Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
60
Damien Millerb84886b2008-05-19 15:05:07 +100061#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100062#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100063#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "key.h"
66#include "buffer.h"
67#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "kex.h"
71#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000072#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
73#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000074#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000075#define TARGET_OS_MAC 1
76#else
77#include "zlib.h"
78#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079#include "packet.h"
80#include "auth-options.h"
81#include "sshpty.h"
82#include "channels.h"
83#include "session.h"
84#include "sshlogin.h"
85#include "canohost.h"
86#include "log.h"
87#include "servconf.h"
88#include "monitor.h"
89#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#ifdef GSSAPI
91#include "ssh-gss.h"
92#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "monitor_wrap.h"
94#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096#include "compat.h"
97#include "ssh2.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +100098#include "roaming.h"
Damien Miller85b45e02013-07-20 13:21:52 +100099#include "authfd.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100
Darren Tucker0efd1552003-08-26 11:49:55 +1000101#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000102static Gssctxt *gsscontext = NULL;
103#endif
104
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105/* Imports */
106extern ServerOptions options;
107extern u_int utmp_len;
108extern Newkeys *current_keys[];
109extern z_stream incoming_stream;
110extern z_stream outgoing_stream;
111extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112extern Buffer auth_debug;
113extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000114extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115
116/* State exported from the child */
117
118struct {
119 z_stream incoming;
120 z_stream outgoing;
121 u_char *keyin;
122 u_int keyinlen;
123 u_char *keyout;
124 u_int keyoutlen;
125 u_char *ivin;
126 u_int ivinlen;
127 u_char *ivout;
128 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000129 u_char *ssh1key;
130 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 int ssh1cipher;
132 int ssh1protoflags;
133 u_char *input;
134 u_int ilen;
135 u_char *output;
136 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000137 u_int64_t sent_bytes;
138 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139} child_state;
140
Damien Miller469954d2003-06-18 20:25:33 +1000141/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142
143int mm_answer_moduli(int, Buffer *);
144int mm_answer_sign(int, Buffer *);
145int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000146int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147int mm_answer_authserv(int, Buffer *);
148int mm_answer_authpassword(int, Buffer *);
149int mm_answer_bsdauthquery(int, Buffer *);
150int mm_answer_bsdauthrespond(int, Buffer *);
151int mm_answer_skeyquery(int, Buffer *);
152int mm_answer_skeyrespond(int, Buffer *);
153int mm_answer_keyallowed(int, Buffer *);
154int mm_answer_keyverify(int, Buffer *);
155int mm_answer_pty(int, Buffer *);
156int mm_answer_pty_cleanup(int, Buffer *);
157int mm_answer_term(int, Buffer *);
158int mm_answer_rsa_keyallowed(int, Buffer *);
159int mm_answer_rsa_challenge(int, Buffer *);
160int mm_answer_rsa_response(int, Buffer *);
161int mm_answer_sesskey(int, Buffer *);
162int mm_answer_sessid(int, Buffer *);
163
Damien Miller79418552002-04-23 20:28:48 +1000164#ifdef USE_PAM
165int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000166int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000167int mm_answer_pam_init_ctx(int, Buffer *);
168int mm_answer_pam_query(int, Buffer *);
169int mm_answer_pam_respond(int, Buffer *);
170int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000171#endif
172
Darren Tucker0efd1552003-08-26 11:49:55 +1000173#ifdef GSSAPI
174int mm_answer_gss_setup_ctx(int, Buffer *);
175int mm_answer_gss_accept_ctx(int, Buffer *);
176int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100177int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000178#endif
Damien Miller25162f22002-09-12 09:47:29 +1000179
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100180#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100181int mm_answer_audit_event(int, Buffer *);
182int mm_answer_audit_command(int, Buffer *);
183#endif
184
Damien Miller8f0bf232011-06-20 14:42:23 +1000185static int monitor_read_log(struct monitor *);
186
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187static Authctxt *authctxt;
188static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
189
190/* local state for key verify */
191static u_char *key_blob = NULL;
192static u_int key_bloblen = 0;
193static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000194static char *hostbased_cuser = NULL;
195static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000196static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100197static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000198static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000199static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000200static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201
202struct mon_table {
203 enum monitor_reqtype type;
204 int flags;
205 int (*f)(int, Buffer *);
206};
207
208#define MON_ISAUTH 0x0004 /* Required for Authentication */
209#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
210#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100211#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000212
213#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
214
215#define MON_PERMIT 0x1000 /* Request is permitted */
216
217struct mon_table mon_dispatch_proto20[] = {
218 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
219 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
220 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
221 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000222 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000223 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000224#ifdef USE_PAM
225 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000226 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000227 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
228 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
229 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
230 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000231#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100232#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100233 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100234#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000235#ifdef BSD_AUTH
236 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100237 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000238#endif
239#ifdef SKEY
240 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
241 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
242#endif
243 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
244 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000245#ifdef GSSAPI
246 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
247 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
248 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100249 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000250#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000251 {0, 0, NULL}
252};
253
254struct mon_table mon_dispatch_postauth20[] = {
255 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
256 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
257 {MONITOR_REQ_PTY, 0, mm_answer_pty},
258 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
259 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100260#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100261 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
262 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
263#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 {0, 0, NULL}
265};
266
267struct mon_table mon_dispatch_proto15[] = {
268 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
269 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
270 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
271 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100272 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
273 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
275 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
276#ifdef BSD_AUTH
277 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100278 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279#endif
280#ifdef SKEY
281 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
282 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
283#endif
Damien Millera33501b2002-05-08 12:24:42 +1000284#ifdef USE_PAM
285 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000286 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000287 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
288 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
289 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
290 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000291#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100292#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100293 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100294#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295 {0, 0, NULL}
296};
297
298struct mon_table mon_dispatch_postauth15[] = {
299 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
300 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
301 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100302#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100303 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000304 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100305#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306 {0, 0, NULL}
307};
308
309struct mon_table *mon_dispatch;
310
311/* Specifies if a certain message is allowed at the moment */
312
313static void
314monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
315{
316 while (ent->f != NULL) {
317 if (ent->type == type) {
318 ent->flags &= ~MON_PERMIT;
319 ent->flags |= permit ? MON_PERMIT : 0;
320 return;
321 }
322 ent++;
323 }
324}
325
326static void
327monitor_permit_authentications(int permit)
328{
329 struct mon_table *ent = mon_dispatch;
330
331 while (ent->f != NULL) {
332 if (ent->flags & MON_AUTH) {
333 ent->flags &= ~MON_PERMIT;
334 ent->flags |= permit ? MON_PERMIT : 0;
335 }
336 ent++;
337 }
338}
339
Darren Tucker3e33cec2003-10-02 16:12:36 +1000340void
341monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000342{
343 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100344 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000345
346 debug3("preauth child monitor started");
347
Damien Miller8f0bf232011-06-20 14:42:23 +1000348 close(pmonitor->m_recvfd);
349 close(pmonitor->m_log_sendfd);
350 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
351
Darren Tucker3e33cec2003-10-02 16:12:36 +1000352 authctxt = _authctxt;
353 memset(authctxt, 0, sizeof(*authctxt));
354
Darren Tuckerde0de392005-03-31 23:52:04 +1000355 authctxt->loginmsg = &loginmsg;
356
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357 if (compat20) {
358 mon_dispatch = mon_dispatch_proto20;
359
360 /* Permit requests for moduli and signatures */
361 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
362 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
363 } else {
364 mon_dispatch = mon_dispatch_proto15;
365
366 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
367 }
368
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369 /* The first few requests do not require asynchronous access */
370 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100371 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100372 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100373 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100374 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100375
376 /* Special handling for multiple required authentications */
377 if (options.num_auth_methods != 0) {
378 if (!compat20)
379 fatal("AuthenticationMethods is not supported"
380 "with SSH protocol 1");
381 if (authenticated &&
382 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000383 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100384 debug3("%s: method %s: partial", __func__,
385 auth_method);
386 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100387 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100388 }
389 }
390
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000391 if (authenticated) {
392 if (!(ent->flags & MON_AUTHDECIDE))
393 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000394 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000395 if (authctxt->pw->pw_uid == 0 &&
396 !auth_root_allowed(auth_method))
397 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000398#ifdef USE_PAM
399 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100400 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000401 Buffer m;
402
403 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100404 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000405 MONITOR_REQ_PAM_ACCOUNT, &m);
406 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
407 buffer_free(&m);
408 }
409#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100411 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100412 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000413 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414 if (!authenticated)
415 authctxt->failures++;
416 }
417 }
418
419 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000420 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100421 if (strcmp(auth_method, "unknown") == 0)
422 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423
424 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000425 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000427 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000428
Damien Miller6a1937e2012-12-12 10:44:38 +1100429 /* Drain any buffered messages from the child */
430 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
431 ;
432
Damien Miller8f0bf232011-06-20 14:42:23 +1000433 close(pmonitor->m_sendfd);
434 close(pmonitor->m_log_recvfd);
435 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000436}
437
Damien Millerbe64d432003-05-14 19:31:12 +1000438static void
439monitor_set_child_handler(pid_t pid)
440{
441 monitor_child_pid = pid;
442}
443
444static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000445monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000446{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000447 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000448}
449
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000450void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000451monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000452{
Damien Miller8f0bf232011-06-20 14:42:23 +1000453 close(pmonitor->m_recvfd);
454 pmonitor->m_recvfd = -1;
455
Damien Millerbe64d432003-05-14 19:31:12 +1000456 monitor_set_child_handler(pmonitor->m_pid);
457 signal(SIGHUP, &monitor_child_handler);
458 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000459 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000460
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461 if (compat20) {
462 mon_dispatch = mon_dispatch_postauth20;
463
464 /* Permit requests for moduli and signatures */
465 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
466 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
467 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000468 } else {
469 mon_dispatch = mon_dispatch_postauth15;
470 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
471 }
472 if (!no_pty_flag) {
473 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
474 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
475 }
476
477 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000478 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479}
480
481void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000482monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483{
Damien Miller2d6b8332002-06-21 15:59:49 +1000484 if (options.compression) {
485 /* The member allocation is not visible, so sync it */
486 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
487 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488}
489
Damien Miller8f0bf232011-06-20 14:42:23 +1000490static int
491monitor_read_log(struct monitor *pmonitor)
492{
493 Buffer logmsg;
494 u_int len, level;
495 char *msg;
496
497 buffer_init(&logmsg);
498
499 /* Read length */
500 buffer_append_space(&logmsg, 4);
501 if (atomicio(read, pmonitor->m_log_recvfd,
502 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
503 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100504 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000505 debug("%s: child log fd closed", __func__);
506 close(pmonitor->m_log_recvfd);
507 pmonitor->m_log_recvfd = -1;
508 return -1;
509 }
510 fatal("%s: log fd read: %s", __func__, strerror(errno));
511 }
512 len = buffer_get_int(&logmsg);
513 if (len <= 4 || len > 8192)
514 fatal("%s: invalid log message length %u", __func__, len);
515
516 /* Read severity, message */
517 buffer_clear(&logmsg);
518 buffer_append_space(&logmsg, len);
519 if (atomicio(read, pmonitor->m_log_recvfd,
520 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
521 fatal("%s: log fd read: %s", __func__, strerror(errno));
522
523 /* Log it */
524 level = buffer_get_int(&logmsg);
525 msg = buffer_get_string(&logmsg, NULL);
526 if (log_level_name(level) == NULL)
527 fatal("%s: invalid log level %u (corrupted message?)",
528 __func__, level);
529 do_log2(level, "%s [preauth]", msg);
530
531 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000532 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000533
534 return 0;
535}
536
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000537int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000538monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000539 struct mon_table **pent)
540{
541 Buffer m;
542 int ret;
543 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000544 struct pollfd pfd[2];
545
546 for (;;) {
Damien Miller1d2c4562014-02-04 11:18:20 +1100547 memset(&pfd, 0, sizeof(pfd));
Damien Miller8f0bf232011-06-20 14:42:23 +1000548 pfd[0].fd = pmonitor->m_sendfd;
549 pfd[0].events = POLLIN;
550 pfd[1].fd = pmonitor->m_log_recvfd;
551 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000552 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
553 if (errno == EINTR || errno == EAGAIN)
554 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000555 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000556 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000557 if (pfd[1].revents) {
558 /*
559 * Drain all log messages before processing next
560 * monitor request.
561 */
562 monitor_read_log(pmonitor);
563 continue;
564 }
565 if (pfd[0].revents)
566 break; /* Continues below */
567 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568
569 buffer_init(&m);
570
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000571 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572 type = buffer_get_char(&m);
573
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000574 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575
576 while (ent->f != NULL) {
577 if (ent->type == type)
578 break;
579 ent++;
580 }
581
582 if (ent->f != NULL) {
583 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000584 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000586 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 buffer_free(&m);
588
589 /* The child may use this request only once, disable it */
590 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000591 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592 type);
593 ent->flags &= ~MON_PERMIT;
594 }
595
596 if (pent != NULL)
597 *pent = ent;
598
599 return ret;
600 }
601
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000602 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603
604 /* NOTREACHED */
605 return (-1);
606}
607
608/* allowed key state */
609static int
610monitor_allowed_key(u_char *blob, u_int bloblen)
611{
612 /* make sure key is allowed */
613 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000614 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000615 return (0);
616 return (1);
617}
618
619static void
620monitor_reset_key_state(void)
621{
622 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000623 free(key_blob);
624 free(hostbased_cuser);
625 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000626 key_blob = NULL;
627 key_bloblen = 0;
628 key_blobtype = MM_NOKEY;
629 hostbased_cuser = NULL;
630 hostbased_chost = NULL;
631}
632
633int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000634mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000635{
636 DH *dh;
637 int min, want, max;
638
639 min = buffer_get_int(m);
640 want = buffer_get_int(m);
641 max = buffer_get_int(m);
642
643 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000644 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645 /* We need to check here, too, in case the child got corrupted */
646 if (max < min || want < min || max < want)
647 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000648 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 buffer_clear(m);
651
652 dh = choose_dh(min, want, max);
653 if (dh == NULL) {
654 buffer_put_char(m, 0);
655 return (0);
656 } else {
657 /* Send first bignum */
658 buffer_put_char(m, 1);
659 buffer_put_bignum2(m, dh->p);
660 buffer_put_bignum2(m, dh->g);
661
662 DH_free(dh);
663 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000664 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665 return (0);
666}
667
Damien Miller85b45e02013-07-20 13:21:52 +1000668extern AuthenticationConnection *auth_conn;
669
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000671mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000672{
673 Key *key;
674 u_char *p;
675 u_char *signature;
676 u_int siglen, datlen;
677 int keyid;
678
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000679 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681 keyid = buffer_get_int(m);
682 p = buffer_get_string(m, &datlen);
683
Damien Millera63128d2006-03-15 12:08:28 +1100684 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000685 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
686 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100687 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000688 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000689 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
Ben Lindstromf67e0772002-06-06 20:58:19 +0000691 /* save session id, it will be passed on the first call */
692 if (session_id2_len == 0) {
693 session_id2_len = datlen;
694 session_id2 = xmalloc(session_id2_len);
695 memcpy(session_id2, p, session_id2_len);
696 }
697
Damien Miller85b45e02013-07-20 13:21:52 +1000698 if ((key = get_hostkey_by_index(keyid)) != NULL) {
699 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
700 fatal("%s: key_sign failed", __func__);
701 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
702 auth_conn != NULL) {
703 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
704 datlen) < 0)
705 fatal("%s: ssh_agent_sign failed", __func__);
706 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000707 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000708
Ben Lindstromd5502182002-06-27 00:12:57 +0000709 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
711 buffer_clear(m);
712 buffer_put_string(m, signature, siglen);
713
Darren Tuckera627d422013-06-02 07:31:17 +1000714 free(p);
715 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000717 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718
719 /* Turn on permissions for getpwnam */
720 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
721
722 return (0);
723}
724
725/* Retrieves the password entry and also checks if the user is permitted */
726
727int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000728mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000730 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 struct passwd *pwent;
732 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000733 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000735 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
737 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000738 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739
Darren Tuckerb09b6772004-06-22 15:06:46 +1000740 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
Darren Tuckerb09b6772004-06-22 15:06:46 +1000742 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
Darren Tuckerb09b6772004-06-22 15:06:46 +1000744 authctxt->user = xstrdup(username);
745 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000746 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747
748 buffer_clear(m);
749
750 if (pwent == NULL) {
751 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100752 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 goto out;
754 }
755
756 allowed = 1;
757 authctxt->pw = pwent;
758 authctxt->valid = 1;
759
760 buffer_put_char(m, 1);
761 buffer_put_string(m, pwent, sizeof(struct passwd));
762 buffer_put_cstring(m, pwent->pw_name);
763 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000764#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000766#endif
767#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000769#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770 buffer_put_cstring(m, pwent->pw_dir);
771 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100772
773 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100774 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000775
776#define M_CP_STROPT(x) do { \
777 if (options.x != NULL) \
778 buffer_put_cstring(m, options.x); \
779 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000780#define M_CP_STRARRAYOPT(x, nx) do { \
781 for (i = 0; i < options.nx; i++) \
782 buffer_put_cstring(m, options.x[i]); \
783 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000784 /* See comment in servconf.h */
785 COPY_MATCH_STRING_OPTS();
786#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000787#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100788
789 /* Create valid auth method lists */
790 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
791 /*
792 * The monitor will continue long enough to let the child
793 * run to it's packet_disconnect(), but it must not allow any
794 * authentication to succeed.
795 */
796 debug("%s: no valid authentication method lists", __func__);
797 }
798
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000799 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000800 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801
802 /* For SSHv1 allow authentication now */
803 if (!compat20)
804 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000805 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806 /* Allow service/style information on the auth context */
807 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000808 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
809 }
Damien Millera33501b2002-05-08 12:24:42 +1000810#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000811 if (options.use_pam)
812 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000813#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000814
815 return (0);
816}
817
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000818int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000819{
820 char *banner;
821
822 buffer_clear(m);
823 banner = auth2_read_banner();
824 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000825 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000826 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000827
828 return (0);
829}
830
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000831int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000832mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833{
834 monitor_permit_authentications(1);
835
836 authctxt->service = buffer_get_string(m, NULL);
837 authctxt->style = buffer_get_string(m, NULL);
838 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000839 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840
841 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000842 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843 authctxt->style = NULL;
844 }
845
846 return (0);
847}
848
849int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000850mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851{
852 static int call_count;
853 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000854 int authenticated;
855 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856
857 passwd = buffer_get_string(m, &plen);
858 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000859 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000860 auth_password(authctxt, passwd);
Damien Millera5103f42014-02-04 11:20:14 +1100861 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000862 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863
864 buffer_clear(m);
865 buffer_put_int(m, authenticated);
866
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000867 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000868 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
870 call_count++;
871 if (plen == 0 && call_count == 1)
872 auth_method = "none";
873 else
874 auth_method = "password";
875
876 /* Causes monitor loop to terminate if authenticated */
877 return (authenticated);
878}
879
880#ifdef BSD_AUTH
881int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000882mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883{
884 char *name, *infotxt;
885 u_int numprompts;
886 u_int *echo_on;
887 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100888 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
Damien Millerb7df3af2003-02-24 11:55:46 +1100890 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
891 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892
893 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100894 buffer_put_int(m, success);
895 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000896 buffer_put_cstring(m, prompts[0]);
897
Damien Millerb7df3af2003-02-24 11:55:46 +1100898 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000899 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900
Damien Millerb7df3af2003-02-24 11:55:46 +1100901 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000902 free(name);
903 free(infotxt);
904 free(prompts);
905 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906 }
907
908 return (0);
909}
910
911int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000912mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913{
914 char *response;
915 int authok;
916
917 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000918 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919
920 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000921 authok = options.challenge_response_authentication &&
922 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000923 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000924 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000925 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926
927 buffer_clear(m);
928 buffer_put_int(m, authok);
929
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000930 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000931 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
Damien Miller91a55f22013-04-23 15:18:10 +1000933 if (compat20) {
934 auth_method = "keyboard-interactive";
935 auth_submethod = "bsdauth";
936 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100937 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938
939 return (authok != 0);
940}
941#endif
942
943#ifdef SKEY
944int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000945mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946{
947 struct skey skey;
948 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100949 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000951 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
952 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
954 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100955 buffer_put_int(m, success);
956 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957 buffer_put_cstring(m, challenge);
958
Damien Millerb7df3af2003-02-24 11:55:46 +1100959 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
962 return (0);
963}
964
965int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000966mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967{
968 char *response;
969 int authok;
970
971 response = buffer_get_string(m, NULL);
972
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000973 authok = (options.challenge_response_authentication &&
974 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975 skey_haskey(authctxt->pw->pw_name) == 0 &&
976 skey_passcheck(authctxt->pw->pw_name, response) != -1);
977
Darren Tuckerf60845f2013-06-02 08:07:31 +1000978 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979
980 buffer_clear(m);
981 buffer_put_int(m, authok);
982
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000984 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
986 auth_method = "skey";
987
988 return (authok != 0);
989}
990#endif
991
Damien Miller79418552002-04-23 20:28:48 +1000992#ifdef USE_PAM
993int
Darren Tucker7eda5922004-06-22 13:22:50 +1000994mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000995{
Damien Miller4e448a32003-05-14 15:11:48 +1000996 if (!options.use_pam)
997 fatal("UsePAM not set, but ended up in %s anyway", __func__);
998
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100999 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001000
Damien Miller1f499fd2003-08-25 13:08:49 +10001001 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1002
Damien Miller79418552002-04-23 20:28:48 +10001003 return (0);
1004}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001005
Damien Miller1f499fd2003-08-25 13:08:49 +10001006int
Darren Tucker7eda5922004-06-22 13:22:50 +10001007mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001008{
1009 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001010
Damien Miller1f499fd2003-08-25 13:08:49 +10001011 if (!options.use_pam)
1012 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1013
1014 ret = do_pam_account();
1015
1016 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001017 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001018
Darren Tucker7eda5922004-06-22 13:22:50 +10001019 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001020
1021 return (ret);
1022}
1023
Damien Miller4f9f42a2003-05-10 19:28:02 +10001024static void *sshpam_ctxt, *sshpam_authok;
1025extern KbdintDevice sshpam_device;
1026
1027int
Darren Tucker7eda5922004-06-22 13:22:50 +10001028mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001029{
1030
1031 debug3("%s", __func__);
1032 authctxt->user = buffer_get_string(m, NULL);
1033 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1034 sshpam_authok = NULL;
1035 buffer_clear(m);
1036 if (sshpam_ctxt != NULL) {
1037 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1038 buffer_put_int(m, 1);
1039 } else {
1040 buffer_put_int(m, 0);
1041 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001042 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001043 return (0);
1044}
1045
1046int
Darren Tucker7eda5922004-06-22 13:22:50 +10001047mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001048{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001049 char *name = NULL, *info = NULL, **prompts = NULL;
1050 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001051 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001052
1053 debug3("%s", __func__);
1054 sshpam_authok = NULL;
1055 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1056 if (ret == 0 && num == 0)
1057 sshpam_authok = sshpam_ctxt;
1058 if (num > 1 || name == NULL || info == NULL)
1059 ret = -1;
1060 buffer_clear(m);
1061 buffer_put_int(m, ret);
1062 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001063 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001064 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001065 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066 buffer_put_int(m, num);
1067 for (i = 0; i < num; ++i) {
1068 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001069 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001070 buffer_put_int(m, echo_on[i]);
1071 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001072 free(prompts);
1073 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001074 auth_method = "keyboard-interactive";
1075 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001076 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077 return (0);
1078}
1079
1080int
Darren Tucker7eda5922004-06-22 13:22:50 +10001081mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001082{
1083 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001084 u_int i, num;
1085 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086
1087 debug3("%s", __func__);
1088 sshpam_authok = NULL;
1089 num = buffer_get_int(m);
1090 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001091 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 for (i = 0; i < num; ++i)
1093 resp[i] = buffer_get_string(m, NULL);
1094 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1095 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001096 free(resp[i]);
1097 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098 } else {
1099 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1100 }
1101 buffer_clear(m);
1102 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001103 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001104 auth_method = "keyboard-interactive";
1105 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001106 if (ret == 0)
1107 sshpam_authok = sshpam_ctxt;
1108 return (0);
1109}
1110
1111int
Darren Tucker7eda5922004-06-22 13:22:50 +10001112mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001113{
1114
1115 debug3("%s", __func__);
1116 (sshpam_device.free_ctx)(sshpam_ctxt);
1117 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001118 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, 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 return (sshpam_authok == sshpam_ctxt);
1122}
Damien Miller79418552002-04-23 20:28:48 +10001123#endif
1124
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001126mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127{
1128 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001129 char *cuser, *chost;
1130 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131 u_int bloblen;
1132 enum mm_keytype type = 0;
1133 int allowed = 0;
1134
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001135 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136
1137 type = buffer_get_int(m);
1138 cuser = buffer_get_string(m, NULL);
1139 chost = buffer_get_string(m, NULL);
1140 blob = buffer_get_string(m, &bloblen);
1141
1142 key = key_from_blob(blob, bloblen);
1143
1144 if ((compat20 && type == MM_RSAHOSTKEY) ||
1145 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001146 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001148 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001149
Damien Miller3e3b5142003-11-17 21:13:40 +11001150 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001151 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001153 allowed = options.pubkey_authentication &&
1154 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001155 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001156 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001157 if (options.pubkey_authentication && allowed != 1)
1158 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159 break;
1160 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001161 allowed = options.hostbased_authentication &&
1162 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001163 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001164 pubkey_auth_info(authctxt, key,
1165 "client user \"%.100s\", client host \"%.100s\"",
1166 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001167 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 break;
1169 case MM_RSAHOSTKEY:
1170 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001171 allowed = options.rhosts_rsa_authentication &&
1172 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001173 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001174 if (options.rhosts_rsa_authentication && allowed != 1)
1175 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001176 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 break;
1178 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001179 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 break;
1181 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 }
Damien Miller00111382003-03-10 11:21:17 +11001183 if (key != NULL)
1184 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185
1186 /* clear temporarily storage (used by verify) */
1187 monitor_reset_key_state();
1188
1189 if (allowed) {
1190 /* Save temporarily for comparison in verify */
1191 key_blob = blob;
1192 key_bloblen = bloblen;
1193 key_blobtype = type;
1194 hostbased_cuser = cuser;
1195 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001196 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001197 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001198 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001199 free(blob);
1200 free(cuser);
1201 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 }
1203
1204 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001205 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206
1207 buffer_clear(m);
1208 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001209 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001211 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212
1213 if (type == MM_RSAHOSTKEY)
1214 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1215
1216 return (0);
1217}
1218
1219static int
1220monitor_valid_userblob(u_char *data, u_int datalen)
1221{
1222 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001223 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 u_int len;
1225 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001226
1227 buffer_init(&b);
1228 buffer_append(&b, data, datalen);
1229
1230 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001231 p = buffer_ptr(&b);
1232 len = buffer_len(&b);
1233 if ((session_id2 == NULL) ||
1234 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001235 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001236 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 buffer_consume(&b, session_id2_len);
1238 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001239 p = buffer_get_string(&b, &len);
1240 if ((session_id2 == NULL) ||
1241 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001242 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001244 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 }
1246 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1247 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001248 p = buffer_get_cstring(&b, NULL);
1249 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1250 authctxt->style ? ":" : "",
1251 authctxt->style ? authctxt->style : "");
1252 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001253 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001254 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 fail++;
1256 }
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(userstyle);
1258 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 buffer_skip_string(&b);
1260 if (datafellows & SSH_BUG_PKAUTH) {
1261 if (!buffer_get_char(&b))
1262 fail++;
1263 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001264 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 if (strcmp("publickey", p) != 0)
1266 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001267 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268 if (!buffer_get_char(&b))
1269 fail++;
1270 buffer_skip_string(&b);
1271 }
1272 buffer_skip_string(&b);
1273 if (buffer_len(&b) != 0)
1274 fail++;
1275 buffer_free(&b);
1276 return (fail == 0);
1277}
1278
1279static int
Damien Millera10f5612002-09-12 09:49:15 +10001280monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1281 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282{
1283 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001284 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 u_int len;
1286 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287
1288 buffer_init(&b);
1289 buffer_append(&b, data, datalen);
1290
Ben Lindstromf67e0772002-06-06 20:58:19 +00001291 p = buffer_get_string(&b, &len);
1292 if ((session_id2 == NULL) ||
1293 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001294 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001296 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001297
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1299 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001300 p = buffer_get_cstring(&b, NULL);
1301 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1302 authctxt->style ? ":" : "",
1303 authctxt->style ? authctxt->style : "");
1304 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001305 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001306 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 fail++;
1308 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001309 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001310 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001312 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 if (strcmp(p, "hostbased") != 0)
1314 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001315 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 buffer_skip_string(&b); /* pkalg */
1317 buffer_skip_string(&b); /* pkblob */
1318
1319 /* verify client host, strip trailing dot if necessary */
1320 p = buffer_get_string(&b, NULL);
1321 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1322 p[len - 1] = '\0';
1323 if (strcmp(p, chost) != 0)
1324 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326
1327 /* verify client user */
1328 p = buffer_get_string(&b, NULL);
1329 if (strcmp(p, cuser) != 0)
1330 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001331 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
1333 if (buffer_len(&b) != 0)
1334 fail++;
1335 buffer_free(&b);
1336 return (fail == 0);
1337}
1338
1339int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001340mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341{
1342 Key *key;
1343 u_char *signature, *data, *blob;
1344 u_int signaturelen, datalen, bloblen;
1345 int verified = 0;
1346 int valid_data = 0;
1347
1348 blob = buffer_get_string(m, &bloblen);
1349 signature = buffer_get_string(m, &signaturelen);
1350 data = buffer_get_string(m, &datalen);
1351
1352 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001353 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001354 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001355
1356 key = key_from_blob(blob, bloblen);
1357 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001358 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359
1360 switch (key_blobtype) {
1361 case MM_USERKEY:
1362 valid_data = monitor_valid_userblob(data, datalen);
1363 break;
1364 case MM_HOSTKEY:
1365 valid_data = monitor_valid_hostbasedblob(data, datalen,
1366 hostbased_cuser, hostbased_chost);
1367 break;
1368 default:
1369 valid_data = 0;
1370 break;
1371 }
1372 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001373 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374
1375 verified = key_verify(key, signature, signaturelen, data, datalen);
1376 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001377 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378
1379 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001380 free(blob);
1381 free(signature);
1382 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383
Ben Lindstrome1c09122002-06-23 00:38:24 +00001384 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1385
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386 monitor_reset_key_state();
1387
1388 buffer_clear(m);
1389 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001390 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391
Darren Tuckerfbba7352006-11-07 23:16:08 +11001392 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393}
1394
1395static void
1396mm_record_login(Session *s, struct passwd *pw)
1397{
1398 socklen_t fromlen;
1399 struct sockaddr_storage from;
1400
1401 /*
1402 * Get IP address of client. If the connection is not a socket, let
1403 * the address be 0.0.0.0.
1404 */
1405 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001406 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001409 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001411 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 }
1413 }
1414 /* Record that there was a login on that tty from the remote host. */
1415 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001416 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001417 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418}
1419
1420static void
1421mm_session_close(Session *s)
1422{
Damien Miller04bd8b02003-05-25 14:38:33 +10001423 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001425 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426 session_pty_cleanup2(s);
1427 }
Damien Miller7207f642008-05-19 15:34:50 +10001428 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429}
1430
1431int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001432mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001434 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 Session *s;
1436 int res, fd0;
1437
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001438 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439
1440 buffer_clear(m);
1441 s = session_new();
1442 if (s == NULL)
1443 goto error;
1444 s->authctxt = authctxt;
1445 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001446 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1448 if (res == 0)
1449 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001450 pty_setowner(authctxt->pw, s->tty);
1451
1452 buffer_put_int(m, 1);
1453 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454
1455 /* We need to trick ttyslot */
1456 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001457 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458
1459 mm_record_login(s, authctxt->pw);
1460
1461 /* Now we can close the file descriptor again */
1462 close(0);
1463
Darren Tucker09991742004-07-17 17:05:14 +10001464 /* send messages generated by record_login */
1465 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1466 buffer_clear(&loginmsg);
1467
1468 mm_request_send(sock, MONITOR_ANS_PTY, m);
1469
Damien Miller54fd7cf2007-09-17 12:04:08 +10001470 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1471 mm_send_fd(sock, s->ttyfd) == -1)
1472 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001473
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 /* make sure nothing uses fd 0 */
1475 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001476 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001477 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001478 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479
1480 /* slave is not needed */
1481 close(s->ttyfd);
1482 s->ttyfd = s->ptyfd;
1483 /* no need to dup() because nobody closes ptyfd */
1484 s->ptymaster = s->ptyfd;
1485
Damien Miller9ab00b42006-08-05 12:40:11 +10001486 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487
1488 return (0);
1489
1490 error:
1491 if (s != NULL)
1492 mm_session_close(s);
1493 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001494 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 return (0);
1496}
1497
1498int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001499mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500{
1501 Session *s;
1502 char *tty;
1503
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001504 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505
1506 tty = buffer_get_string(m, NULL);
1507 if ((s = session_by_tty(tty)) != NULL)
1508 mm_session_close(s);
1509 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001510 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001511 return (0);
1512}
1513
1514int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001515mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516{
1517 BIGNUM *p;
1518 int rsafail;
1519
1520 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001521 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522
1523 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001524 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525
1526 buffer_get_bignum2(m, p);
1527
1528 rsafail = ssh1_session_key(p);
1529
1530 buffer_clear(m);
1531 buffer_put_int(m, rsafail);
1532 buffer_put_bignum2(m, p);
1533
1534 BN_clear_free(p);
1535
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001536 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537
1538 /* Turn on permissions for sessid passing */
1539 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1540
1541 return (0);
1542}
1543
1544int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001545mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546{
1547 int i;
1548
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001549 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550
1551 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001552 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553 for (i = 0; i < 16; i++)
1554 session_id[i] = buffer_get_char(m);
1555
1556 /* Turn on permissions for getpwnam */
1557 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1558
1559 return (0);
1560}
1561
1562int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001563mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564{
1565 BIGNUM *client_n;
1566 Key *key = NULL;
1567 u_char *blob = NULL;
1568 u_int blen = 0;
1569 int allowed = 0;
1570
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001571 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001572
Damien Miller7a8f5b32006-03-31 23:14:23 +11001573 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001574 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001576 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001577 buffer_get_bignum2(m, client_n);
1578 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1579 BN_clear_free(client_n);
1580 }
1581 buffer_clear(m);
1582 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001583 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001584
1585 /* clear temporarily storage (used by generate challenge) */
1586 monitor_reset_key_state();
1587
1588 if (allowed && key != NULL) {
1589 key->type = KEY_RSA; /* cheat for key_to_blob */
1590 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001591 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592 buffer_put_string(m, blob, blen);
1593
1594 /* Save temporarily for comparison in verify */
1595 key_blob = blob;
1596 key_bloblen = blen;
1597 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598 }
Damien Miller00111382003-03-10 11:21:17 +11001599 if (key != NULL)
1600 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001602 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001603
1604 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1605 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1606 return (0);
1607}
1608
1609int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001610mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611{
1612 Key *key = NULL;
1613 u_char *blob;
1614 u_int blen;
1615
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001616 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617
1618 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001619 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 blob = buffer_get_string(m, &blen);
1621 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001622 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001624 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001626 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001627 if (key->type != KEY_RSA)
1628 fatal("%s: received bad key type %d", __func__, key->type);
1629 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 if (ssh1_challenge)
1631 BN_clear_free(ssh1_challenge);
1632 ssh1_challenge = auth_rsa_generate_challenge(key);
1633
1634 buffer_clear(m);
1635 buffer_put_bignum2(m, ssh1_challenge);
1636
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001637 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001638 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639
1640 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001641
Darren Tuckera627d422013-06-02 07:31:17 +10001642 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001643 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644 return (0);
1645}
1646
1647int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001648mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649{
1650 Key *key = NULL;
1651 u_char *blob, *response;
1652 u_int blen, len;
1653 int success;
1654
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001655 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656
1657 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001658 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001660 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661
1662 blob = buffer_get_string(m, &blen);
1663 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001664 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001666 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001667 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 response = buffer_get_string(m, &len);
1670 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001671 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1673
Darren Tuckera627d422013-06-02 07:31:17 +10001674 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001676 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677
1678 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1679
1680 /* reset state */
1681 BN_clear_free(ssh1_challenge);
1682 ssh1_challenge = NULL;
1683 monitor_reset_key_state();
1684
1685 buffer_clear(m);
1686 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001687 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688
1689 return (success);
1690}
1691
1692int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001693mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001695 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 int res, status;
1697
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001698 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699
1700 /* The child is terminating */
1701 session_destroy_all(&mm_session_close);
1702
Darren Tucker52358d62008-03-11 22:58:25 +11001703#ifdef USE_PAM
1704 if (options.use_pam)
1705 sshpam_cleanup();
1706#endif
1707
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001708 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001709 if (errno != EINTR)
1710 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711
1712 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1713
1714 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001715 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001716}
1717
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001718#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001719/* Report that an audit event occurred */
1720int
1721mm_answer_audit_event(int socket, Buffer *m)
1722{
1723 ssh_audit_event_t event;
1724
1725 debug3("%s entering", __func__);
1726
1727 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001728 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001729 case SSH_AUTH_FAIL_PUBKEY:
1730 case SSH_AUTH_FAIL_HOSTBASED:
1731 case SSH_AUTH_FAIL_GSSAPI:
1732 case SSH_LOGIN_EXCEED_MAXTRIES:
1733 case SSH_LOGIN_ROOT_DENIED:
1734 case SSH_CONNECTION_CLOSE:
1735 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001736 audit_event(event);
1737 break;
1738 default:
1739 fatal("Audit event type %d not permitted", event);
1740 }
1741
1742 return (0);
1743}
1744
1745int
1746mm_answer_audit_command(int socket, Buffer *m)
1747{
1748 u_int len;
1749 char *cmd;
1750
1751 debug3("%s entering", __func__);
1752 cmd = buffer_get_string(m, &len);
1753 /* sanity check command, if so how? */
1754 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001755 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001756 return (0);
1757}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001758#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001759
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001760void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001761monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001762{
1763 if (compat20) {
1764 set_newkeys(MODE_IN);
1765 set_newkeys(MODE_OUT);
1766 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001767 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001768 packet_set_encryption_key(child_state.ssh1key,
1769 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001770 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771 }
1772
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001773 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001774 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001775 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001777 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001778
1779 if (!compat20) {
1780 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001781 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001783 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 }
1785
1786 memcpy(&incoming_stream, &child_state.incoming,
1787 sizeof(incoming_stream));
1788 memcpy(&outgoing_stream, &child_state.outgoing,
1789 sizeof(outgoing_stream));
1790
1791 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001792 if (options.compression)
1793 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001794
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001795 if (options.rekey_limit || options.rekey_interval)
1796 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1797 (time_t)options.rekey_interval);
1798
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001799 /* Network I/O buffers */
1800 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001801 buffer_clear(packet_get_input());
1802 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Damien Millera5103f42014-02-04 11:20:14 +11001803 explicit_bzero(child_state.input, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001804 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001805
Darren Tuckerf7288d72009-06-21 18:12:20 +10001806 buffer_clear(packet_get_output());
1807 buffer_append(packet_get_output(), child_state.output,
1808 child_state.olen);
Damien Millera5103f42014-02-04 11:20:14 +11001809 explicit_bzero(child_state.output, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001810 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001811
1812 /* Roaming */
1813 if (compat20)
1814 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001815}
1816
1817static Kex *
1818mm_get_kex(Buffer *m)
1819{
1820 Kex *kex;
1821 void *blob;
1822 u_int bloblen;
1823
Damien Miller07d86be2006-03-26 14:19:21 +11001824 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001826 if (session_id2 == NULL ||
1827 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001828 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001829 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001830 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001831 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001832 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001833 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001834 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001835 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Damien Millerc9831972013-11-07 12:00:23 +11001836 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001837 kex->server = 1;
1838 kex->hostkey_type = buffer_get_int(m);
1839 kex->kex_type = buffer_get_int(m);
1840 blob = buffer_get_string(m, &bloblen);
1841 buffer_init(&kex->my);
1842 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001843 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001844 blob = buffer_get_string(m, &bloblen);
1845 buffer_init(&kex->peer);
1846 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001847 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001848 kex->done = 1;
1849 kex->flags = buffer_get_int(m);
1850 kex->client_version_string = buffer_get_string(m, NULL);
1851 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001852 kex->load_host_public_key=&get_hostkey_public_by_type;
1853 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001854 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001855 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001856
1857 return (kex);
1858}
1859
1860/* This function requries careful sanity checking */
1861
1862void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001863mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001864{
1865 Buffer m;
1866 u_char *blob, *p;
1867 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001868 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001869 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001870
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001871 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001872
1873 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001874 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875 if (!compat20) {
1876 child_state.ssh1protoflags = buffer_get_int(&m);
1877 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001878 child_state.ssh1key = buffer_get_string(&m,
1879 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001880 child_state.ivout = buffer_get_string(&m,
1881 &child_state.ivoutlen);
1882 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1883 goto skip;
1884 } else {
1885 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001886 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001887 }
1888
1889 blob = buffer_get_string(&m, &bloblen);
1890 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001891 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001892
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001893 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001894 blob = buffer_get_string(&m, &bloblen);
1895 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001896 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001897
1898 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001899 seqnr = buffer_get_int(&m);
1900 blocks = buffer_get_int64(&m);
1901 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001902 bytes = buffer_get_int64(&m);
1903 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001904 seqnr = buffer_get_int(&m);
1905 blocks = buffer_get_int64(&m);
1906 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001907 bytes = buffer_get_int64(&m);
1908 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001909
1910 skip:
1911 /* Get the key context */
1912 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1913 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1914
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001915 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001916 /* Get compression state */
1917 p = buffer_get_string(&m, &plen);
1918 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001919 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001920 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001921 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001922
1923 p = buffer_get_string(&m, &plen);
1924 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001925 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001926 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001927 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001928
1929 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001930 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001931 child_state.input = buffer_get_string(&m, &child_state.ilen);
1932 child_state.output = buffer_get_string(&m, &child_state.olen);
1933
Darren Tuckerc5564e12009-06-21 18:53:53 +10001934 /* Roaming */
1935 if (compat20) {
1936 child_state.sent_bytes = buffer_get_int64(&m);
1937 child_state.recv_bytes = buffer_get_int64(&m);
1938 }
1939
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001940 buffer_free(&m);
1941}
1942
1943
1944/* Allocation functions for zlib */
1945void *
1946mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1947{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001948 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001949 void *address;
1950
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001951 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001952 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1953
1954 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001955
1956 return (address);
1957}
1958
1959void
1960mm_zfree(struct mm_master *mm, void *address)
1961{
1962 mm_free(mm, address);
1963}
1964
1965void
1966mm_init_compression(struct mm_master *mm)
1967{
1968 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1969 outgoing_stream.zfree = (free_func)mm_zfree;
1970 outgoing_stream.opaque = mm;
1971
1972 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1973 incoming_stream.zfree = (free_func)mm_zfree;
1974 incoming_stream.opaque = mm;
1975}
1976
1977/* XXX */
1978
1979#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001980 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001981 fatal("fcntl(%d, F_SETFD)", x); \
1982} while (0)
1983
1984static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001985monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001986{
Damien Miller8f0bf232011-06-20 14:42:23 +10001987 int pair[2];
1988
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001989 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001990 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001991 FD_CLOSEONEXEC(pair[0]);
1992 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001993 mon->m_recvfd = pair[0];
1994 mon->m_sendfd = pair[1];
1995
1996 if (do_logfds) {
1997 if (pipe(pair) == -1)
1998 fatal("%s: pipe: %s", __func__, strerror(errno));
1999 FD_CLOSEONEXEC(pair[0]);
2000 FD_CLOSEONEXEC(pair[1]);
2001 mon->m_log_recvfd = pair[0];
2002 mon->m_log_sendfd = pair[1];
2003 } else
2004 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002005}
2006
2007#define MM_MEMSIZE 65536
2008
2009struct monitor *
2010monitor_init(void)
2011{
2012 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002013
Damien Miller07d86be2006-03-26 14:19:21 +11002014 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002015
Damien Miller8f0bf232011-06-20 14:42:23 +10002016 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002017
2018 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002019 if (options.compression) {
2020 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2021 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002022
Damien Miller2d6b8332002-06-21 15:59:49 +10002023 /* Compression needs to share state across borders */
2024 mm_init_compression(mon->m_zlib);
2025 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002026
2027 return mon;
2028}
2029
2030void
2031monitor_reinit(struct monitor *mon)
2032{
Damien Miller8f0bf232011-06-20 14:42:23 +10002033 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002034}
Darren Tucker0efd1552003-08-26 11:49:55 +10002035
2036#ifdef GSSAPI
2037int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002038mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002039{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002040 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002041 OM_uint32 major;
2042 u_int len;
2043
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002044 goid.elements = buffer_get_string(m, &len);
2045 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002046
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002047 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002048
Darren Tuckera627d422013-06-02 07:31:17 +10002049 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002050
2051 buffer_clear(m);
2052 buffer_put_int(m, major);
2053
Damien Miller6fd6def2005-11-05 15:07:05 +11002054 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002055
2056 /* Now we have a context, enable the step */
2057 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2058
2059 return (0);
2060}
2061
2062int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002063mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002064{
2065 gss_buffer_desc in;
2066 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002067 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002068 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002069 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002070
Darren Tucker600ad8d2003-08-26 12:10:48 +10002071 in.value = buffer_get_string(m, &len);
2072 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002073 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002074 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002075
2076 buffer_clear(m);
2077 buffer_put_int(m, major);
2078 buffer_put_string(m, out.value, out.length);
2079 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002080 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002081
2082 gss_release_buffer(&minor, &out);
2083
Damien Miller6fd6def2005-11-05 15:07:05 +11002084 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002085 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2086 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002087 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002088 }
2089 return (0);
2090}
2091
2092int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002093mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002094{
2095 gss_buffer_desc gssbuf, mic;
2096 OM_uint32 ret;
2097 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002098
Damien Miller0425d402003-11-17 22:18:21 +11002099 gssbuf.value = buffer_get_string(m, &len);
2100 gssbuf.length = len;
2101 mic.value = buffer_get_string(m, &len);
2102 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002103
Damien Miller0425d402003-11-17 22:18:21 +11002104 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002105
Darren Tuckera627d422013-06-02 07:31:17 +10002106 free(gssbuf.value);
2107 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002108
Damien Miller0425d402003-11-17 22:18:21 +11002109 buffer_clear(m);
2110 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002111
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002112 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002113
Damien Miller0425d402003-11-17 22:18:21 +11002114 if (!GSS_ERROR(ret))
2115 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002116
Damien Miller0425d402003-11-17 22:18:21 +11002117 return (0);
2118}
2119
2120int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002121mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002122{
2123 int authenticated;
2124
2125 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2126
2127 buffer_clear(m);
2128 buffer_put_int(m, authenticated);
2129
2130 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002131 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002132
Damien Miller6fd6def2005-11-05 15:07:05 +11002133 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002134
2135 /* Monitor loop will terminate if authenticated */
2136 return (authenticated);
2137}
2138#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002139