blob: 7286126f557377241f80710445a24edf1a8dcd05 [file] [log] [blame]
Darren Tucker0acca372013-06-02 07:41:51 +10001/* $OpenBSD: monitor.c,v 1.125 2013/05/19 02:42:42 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"
Damien Miller01ed2272008-11-05 16:20:46 +110098#include "jpake.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +100099#include "roaming.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 *);
Damien Miller01ed2272008-11-05 16:20:46 +1100163int mm_answer_jpake_get_pwdata(int, Buffer *);
164int mm_answer_jpake_step1(int, Buffer *);
165int mm_answer_jpake_step2(int, Buffer *);
166int mm_answer_jpake_key_confirm(int, Buffer *);
167int mm_answer_jpake_check_confirm(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
Damien Miller79418552002-04-23 20:28:48 +1000169#ifdef USE_PAM
170int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000171int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172int mm_answer_pam_init_ctx(int, Buffer *);
173int mm_answer_pam_query(int, Buffer *);
174int mm_answer_pam_respond(int, Buffer *);
175int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000176#endif
177
Darren Tucker0efd1552003-08-26 11:49:55 +1000178#ifdef GSSAPI
179int mm_answer_gss_setup_ctx(int, Buffer *);
180int mm_answer_gss_accept_ctx(int, Buffer *);
181int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100182int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000183#endif
Damien Miller25162f22002-09-12 09:47:29 +1000184
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100185#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100186int mm_answer_audit_event(int, Buffer *);
187int mm_answer_audit_command(int, Buffer *);
188#endif
189
Damien Miller8f0bf232011-06-20 14:42:23 +1000190static int monitor_read_log(struct monitor *);
191
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192static Authctxt *authctxt;
193static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
194
195/* local state for key verify */
196static u_char *key_blob = NULL;
197static u_int key_bloblen = 0;
198static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000199static char *hostbased_cuser = NULL;
200static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100202static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000203static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000204static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000205static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207struct mon_table {
208 enum monitor_reqtype type;
209 int flags;
210 int (*f)(int, Buffer *);
211};
212
213#define MON_ISAUTH 0x0004 /* Required for Authentication */
214#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
215#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100216#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217
218#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
219
220#define MON_PERMIT 0x1000 /* Request is permitted */
221
222struct mon_table mon_dispatch_proto20[] = {
223 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
224 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
225 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
226 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000227 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000229#ifdef USE_PAM
230 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000231 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000232 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
233 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
234 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
235 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000236#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100237#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100238 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100239#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000240#ifdef BSD_AUTH
241 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100242 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000243#endif
244#ifdef SKEY
245 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
246 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
247#endif
248 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
249 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000250#ifdef GSSAPI
251 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
252 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
253 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100254 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000255#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100256#ifdef JPAKE
257 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
258 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
259 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
260 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
261 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
262#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 {0, 0, NULL}
264};
265
266struct mon_table mon_dispatch_postauth20[] = {
267 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
268 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
269 {MONITOR_REQ_PTY, 0, mm_answer_pty},
270 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
271 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100272#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100273 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
274 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
275#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000276 {0, 0, NULL}
277};
278
279struct mon_table mon_dispatch_proto15[] = {
280 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
281 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
282 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
283 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100284 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
285 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000286 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
287 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
288#ifdef BSD_AUTH
289 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100290 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291#endif
292#ifdef SKEY
293 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
294 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
295#endif
Damien Millera33501b2002-05-08 12:24:42 +1000296#ifdef USE_PAM
297 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000298 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000299 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
300 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
301 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
302 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000303#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100304#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100305 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100306#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307 {0, 0, NULL}
308};
309
310struct mon_table mon_dispatch_postauth15[] = {
311 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
312 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
313 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100314#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100315 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000316 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100317#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318 {0, 0, NULL}
319};
320
321struct mon_table *mon_dispatch;
322
323/* Specifies if a certain message is allowed at the moment */
324
325static void
326monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
327{
328 while (ent->f != NULL) {
329 if (ent->type == type) {
330 ent->flags &= ~MON_PERMIT;
331 ent->flags |= permit ? MON_PERMIT : 0;
332 return;
333 }
334 ent++;
335 }
336}
337
338static void
339monitor_permit_authentications(int permit)
340{
341 struct mon_table *ent = mon_dispatch;
342
343 while (ent->f != NULL) {
344 if (ent->flags & MON_AUTH) {
345 ent->flags &= ~MON_PERMIT;
346 ent->flags |= permit ? MON_PERMIT : 0;
347 }
348 ent++;
349 }
350}
351
Darren Tucker3e33cec2003-10-02 16:12:36 +1000352void
353monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354{
355 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100356 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357
358 debug3("preauth child monitor started");
359
Damien Miller8f0bf232011-06-20 14:42:23 +1000360 close(pmonitor->m_recvfd);
361 close(pmonitor->m_log_sendfd);
362 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
363
Darren Tucker3e33cec2003-10-02 16:12:36 +1000364 authctxt = _authctxt;
365 memset(authctxt, 0, sizeof(*authctxt));
366
Darren Tuckerde0de392005-03-31 23:52:04 +1000367 authctxt->loginmsg = &loginmsg;
368
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000369 if (compat20) {
370 mon_dispatch = mon_dispatch_proto20;
371
372 /* Permit requests for moduli and signatures */
373 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
374 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
375 } else {
376 mon_dispatch = mon_dispatch_proto15;
377
378 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
379 }
380
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381 /* The first few requests do not require asynchronous access */
382 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100383 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100384 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100385 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100386 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100387
388 /* Special handling for multiple required authentications */
389 if (options.num_auth_methods != 0) {
390 if (!compat20)
391 fatal("AuthenticationMethods is not supported"
392 "with SSH protocol 1");
393 if (authenticated &&
394 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000395 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100396 debug3("%s: method %s: partial", __func__,
397 auth_method);
398 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100399 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100400 }
401 }
402
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403 if (authenticated) {
404 if (!(ent->flags & MON_AUTHDECIDE))
405 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 if (authctxt->pw->pw_uid == 0 &&
408 !auth_root_allowed(auth_method))
409 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000410#ifdef USE_PAM
411 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100412 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000413 Buffer m;
414
415 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100416 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000417 MONITOR_REQ_PAM_ACCOUNT, &m);
418 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
419 buffer_free(&m);
420 }
421#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000422 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100423 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100424 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000425 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 if (!authenticated)
427 authctxt->failures++;
428 }
Damien Miller01ed2272008-11-05 16:20:46 +1100429#ifdef JPAKE
430 /* Cleanup JPAKE context after authentication */
431 if (ent->flags & MON_AUTHDECIDE) {
432 if (authctxt->jpake_ctx != NULL) {
433 jpake_free(authctxt->jpake_ctx);
434 authctxt->jpake_ctx = NULL;
435 }
436 }
437#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438 }
439
440 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000441 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100442 if (strcmp(auth_method, "unknown") == 0)
443 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444
445 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000446 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000447
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000448 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000449
Damien Miller6a1937e2012-12-12 10:44:38 +1100450 /* Drain any buffered messages from the child */
451 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
452 ;
453
Damien Miller8f0bf232011-06-20 14:42:23 +1000454 close(pmonitor->m_sendfd);
455 close(pmonitor->m_log_recvfd);
456 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457}
458
Damien Millerbe64d432003-05-14 19:31:12 +1000459static void
460monitor_set_child_handler(pid_t pid)
461{
462 monitor_child_pid = pid;
463}
464
465static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000466monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000467{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000468 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000469}
470
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000472monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473{
Damien Miller8f0bf232011-06-20 14:42:23 +1000474 close(pmonitor->m_recvfd);
475 pmonitor->m_recvfd = -1;
476
Damien Millerbe64d432003-05-14 19:31:12 +1000477 monitor_set_child_handler(pmonitor->m_pid);
478 signal(SIGHUP, &monitor_child_handler);
479 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000480 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000481
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482 if (compat20) {
483 mon_dispatch = mon_dispatch_postauth20;
484
485 /* Permit requests for moduli and signatures */
486 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
487 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
488 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000489 } else {
490 mon_dispatch = mon_dispatch_postauth15;
491 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
492 }
493 if (!no_pty_flag) {
494 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
495 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
496 }
497
498 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000499 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500}
501
502void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000503monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000504{
Damien Miller2d6b8332002-06-21 15:59:49 +1000505 if (options.compression) {
506 /* The member allocation is not visible, so sync it */
507 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
508 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509}
510
Damien Miller8f0bf232011-06-20 14:42:23 +1000511static int
512monitor_read_log(struct monitor *pmonitor)
513{
514 Buffer logmsg;
515 u_int len, level;
516 char *msg;
517
518 buffer_init(&logmsg);
519
520 /* Read length */
521 buffer_append_space(&logmsg, 4);
522 if (atomicio(read, pmonitor->m_log_recvfd,
523 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
524 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100525 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000526 debug("%s: child log fd closed", __func__);
527 close(pmonitor->m_log_recvfd);
528 pmonitor->m_log_recvfd = -1;
529 return -1;
530 }
531 fatal("%s: log fd read: %s", __func__, strerror(errno));
532 }
533 len = buffer_get_int(&logmsg);
534 if (len <= 4 || len > 8192)
535 fatal("%s: invalid log message length %u", __func__, len);
536
537 /* Read severity, message */
538 buffer_clear(&logmsg);
539 buffer_append_space(&logmsg, len);
540 if (atomicio(read, pmonitor->m_log_recvfd,
541 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
542 fatal("%s: log fd read: %s", __func__, strerror(errno));
543
544 /* Log it */
545 level = buffer_get_int(&logmsg);
546 msg = buffer_get_string(&logmsg, NULL);
547 if (log_level_name(level) == NULL)
548 fatal("%s: invalid log level %u (corrupted message?)",
549 __func__, level);
550 do_log2(level, "%s [preauth]", msg);
551
552 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000554
555 return 0;
556}
557
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000559monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560 struct mon_table **pent)
561{
562 Buffer m;
563 int ret;
564 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000565 struct pollfd pfd[2];
566
567 for (;;) {
568 bzero(&pfd, sizeof(pfd));
569 pfd[0].fd = pmonitor->m_sendfd;
570 pfd[0].events = POLLIN;
571 pfd[1].fd = pmonitor->m_log_recvfd;
572 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000573 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
574 if (errno == EINTR || errno == EAGAIN)
575 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000576 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000577 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000578 if (pfd[1].revents) {
579 /*
580 * Drain all log messages before processing next
581 * monitor request.
582 */
583 monitor_read_log(pmonitor);
584 continue;
585 }
586 if (pfd[0].revents)
587 break; /* Continues below */
588 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589
590 buffer_init(&m);
591
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000592 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000593 type = buffer_get_char(&m);
594
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000595 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596
597 while (ent->f != NULL) {
598 if (ent->type == type)
599 break;
600 ent++;
601 }
602
603 if (ent->f != NULL) {
604 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000605 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000607 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608 buffer_free(&m);
609
610 /* The child may use this request only once, disable it */
611 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000612 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613 type);
614 ent->flags &= ~MON_PERMIT;
615 }
616
617 if (pent != NULL)
618 *pent = ent;
619
620 return ret;
621 }
622
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000623 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624
625 /* NOTREACHED */
626 return (-1);
627}
628
629/* allowed key state */
630static int
631monitor_allowed_key(u_char *blob, u_int bloblen)
632{
633 /* make sure key is allowed */
634 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000635 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636 return (0);
637 return (1);
638}
639
640static void
641monitor_reset_key_state(void)
642{
643 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000644 free(key_blob);
645 free(hostbased_cuser);
646 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647 key_blob = NULL;
648 key_bloblen = 0;
649 key_blobtype = MM_NOKEY;
650 hostbased_cuser = NULL;
651 hostbased_chost = NULL;
652}
653
654int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000655mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656{
657 DH *dh;
658 int min, want, max;
659
660 min = buffer_get_int(m);
661 want = buffer_get_int(m);
662 max = buffer_get_int(m);
663
664 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000665 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666 /* We need to check here, too, in case the child got corrupted */
667 if (max < min || want < min || max < want)
668 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000669 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670
671 buffer_clear(m);
672
673 dh = choose_dh(min, want, max);
674 if (dh == NULL) {
675 buffer_put_char(m, 0);
676 return (0);
677 } else {
678 /* Send first bignum */
679 buffer_put_char(m, 1);
680 buffer_put_bignum2(m, dh->p);
681 buffer_put_bignum2(m, dh->g);
682
683 DH_free(dh);
684 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000685 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686 return (0);
687}
688
689int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000690mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691{
692 Key *key;
693 u_char *p;
694 u_char *signature;
695 u_int siglen, datlen;
696 int keyid;
697
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000698 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
700 keyid = buffer_get_int(m);
701 p = buffer_get_string(m, &datlen);
702
Damien Millera63128d2006-03-15 12:08:28 +1100703 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000704 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
705 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100706 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000707 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000708 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709
Ben Lindstromf67e0772002-06-06 20:58:19 +0000710 /* save session id, it will be passed on the first call */
711 if (session_id2_len == 0) {
712 session_id2_len = datlen;
713 session_id2 = xmalloc(session_id2_len);
714 memcpy(session_id2, p, session_id2_len);
715 }
716
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000718 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000720 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721
Ben Lindstromd5502182002-06-27 00:12:57 +0000722 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723
724 buffer_clear(m);
725 buffer_put_string(m, signature, siglen);
726
Darren Tuckera627d422013-06-02 07:31:17 +1000727 free(p);
728 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000729
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000730 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
732 /* Turn on permissions for getpwnam */
733 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
734
735 return (0);
736}
737
738/* Retrieves the password entry and also checks if the user is permitted */
739
740int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000741mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000743 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744 struct passwd *pwent;
745 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000746 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000748 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749
750 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000751 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752
Darren Tuckerb09b6772004-06-22 15:06:46 +1000753 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754
Darren Tuckerb09b6772004-06-22 15:06:46 +1000755 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
Darren Tuckerb09b6772004-06-22 15:06:46 +1000757 authctxt->user = xstrdup(username);
758 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000759 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000760
761 buffer_clear(m);
762
763 if (pwent == NULL) {
764 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100765 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766 goto out;
767 }
768
769 allowed = 1;
770 authctxt->pw = pwent;
771 authctxt->valid = 1;
772
773 buffer_put_char(m, 1);
774 buffer_put_string(m, pwent, sizeof(struct passwd));
775 buffer_put_cstring(m, pwent->pw_name);
776 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000777#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000779#endif
780#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000782#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 buffer_put_cstring(m, pwent->pw_dir);
784 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100785
786 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100787 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000788
789#define M_CP_STROPT(x) do { \
790 if (options.x != NULL) \
791 buffer_put_cstring(m, options.x); \
792 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000793#define M_CP_STRARRAYOPT(x, nx) do { \
794 for (i = 0; i < options.nx; i++) \
795 buffer_put_cstring(m, options.x[i]); \
796 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000797 /* See comment in servconf.h */
798 COPY_MATCH_STRING_OPTS();
799#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000800#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100801
802 /* Create valid auth method lists */
803 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
804 /*
805 * The monitor will continue long enough to let the child
806 * run to it's packet_disconnect(), but it must not allow any
807 * authentication to succeed.
808 */
809 debug("%s: no valid authentication method lists", __func__);
810 }
811
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000812 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000813 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000814
815 /* For SSHv1 allow authentication now */
816 if (!compat20)
817 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000818 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819 /* Allow service/style information on the auth context */
820 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000821 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
822 }
Damien Millera33501b2002-05-08 12:24:42 +1000823#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000824 if (options.use_pam)
825 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000826#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827
828 return (0);
829}
830
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000831int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000832{
833 char *banner;
834
835 buffer_clear(m);
836 banner = auth2_read_banner();
837 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000838 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000839 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000840
841 return (0);
842}
843
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000845mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000846{
847 monitor_permit_authentications(1);
848
849 authctxt->service = buffer_get_string(m, NULL);
850 authctxt->style = buffer_get_string(m, NULL);
851 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000852 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853
854 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000855 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856 authctxt->style = NULL;
857 }
858
859 return (0);
860}
861
862int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000863mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864{
865 static int call_count;
866 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000867 int authenticated;
868 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869
870 passwd = buffer_get_string(m, &plen);
871 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000872 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000873 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 memset(passwd, 0, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000875 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
877 buffer_clear(m);
878 buffer_put_int(m, authenticated);
879
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000880 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000881 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000882
883 call_count++;
884 if (plen == 0 && call_count == 1)
885 auth_method = "none";
886 else
887 auth_method = "password";
888
889 /* Causes monitor loop to terminate if authenticated */
890 return (authenticated);
891}
892
893#ifdef BSD_AUTH
894int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000895mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000896{
897 char *name, *infotxt;
898 u_int numprompts;
899 u_int *echo_on;
900 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100901 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902
Damien Millerb7df3af2003-02-24 11:55:46 +1100903 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
904 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905
906 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100907 buffer_put_int(m, success);
908 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 buffer_put_cstring(m, prompts[0]);
910
Damien Millerb7df3af2003-02-24 11:55:46 +1100911 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000912 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000913
Damien Millerb7df3af2003-02-24 11:55:46 +1100914 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000915 free(name);
916 free(infotxt);
917 free(prompts);
918 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919 }
920
921 return (0);
922}
923
924int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000925mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926{
927 char *response;
928 int authok;
929
930 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000931 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
933 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000934 authok = options.challenge_response_authentication &&
935 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000937 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000938 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939
940 buffer_clear(m);
941 buffer_put_int(m, authok);
942
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000944 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945
Damien Miller91a55f22013-04-23 15:18:10 +1000946 if (compat20) {
947 auth_method = "keyboard-interactive";
948 auth_submethod = "bsdauth";
949 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100950 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000951
952 return (authok != 0);
953}
954#endif
955
956#ifdef SKEY
957int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000958mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959{
960 struct skey skey;
961 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100962 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000963
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000964 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
965 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966
967 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100968 buffer_put_int(m, success);
969 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970 buffer_put_cstring(m, challenge);
971
Damien Millerb7df3af2003-02-24 11:55:46 +1100972 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000973 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974
975 return (0);
976}
977
978int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000979mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980{
981 char *response;
982 int authok;
983
984 response = buffer_get_string(m, NULL);
985
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000986 authok = (options.challenge_response_authentication &&
987 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988 skey_haskey(authctxt->pw->pw_name) == 0 &&
989 skey_passcheck(authctxt->pw->pw_name, response) != -1);
990
Darren Tuckerf60845f2013-06-02 08:07:31 +1000991 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000992
993 buffer_clear(m);
994 buffer_put_int(m, authok);
995
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000997 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000998
999 auth_method = "skey";
1000
1001 return (authok != 0);
1002}
1003#endif
1004
Damien Miller79418552002-04-23 20:28:48 +10001005#ifdef USE_PAM
1006int
Darren Tucker7eda5922004-06-22 13:22:50 +10001007mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001008{
Damien Miller4e448a32003-05-14 15:11:48 +10001009 if (!options.use_pam)
1010 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1011
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001012 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001013
Damien Miller1f499fd2003-08-25 13:08:49 +10001014 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1015
Damien Miller79418552002-04-23 20:28:48 +10001016 return (0);
1017}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001018
Damien Miller1f499fd2003-08-25 13:08:49 +10001019int
Darren Tucker7eda5922004-06-22 13:22:50 +10001020mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001021{
1022 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001023
Damien Miller1f499fd2003-08-25 13:08:49 +10001024 if (!options.use_pam)
1025 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1026
1027 ret = do_pam_account();
1028
1029 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001030 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001031
Darren Tucker7eda5922004-06-22 13:22:50 +10001032 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001033
1034 return (ret);
1035}
1036
Damien Miller4f9f42a2003-05-10 19:28:02 +10001037static void *sshpam_ctxt, *sshpam_authok;
1038extern KbdintDevice sshpam_device;
1039
1040int
Darren Tucker7eda5922004-06-22 13:22:50 +10001041mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001042{
1043
1044 debug3("%s", __func__);
1045 authctxt->user = buffer_get_string(m, NULL);
1046 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1047 sshpam_authok = NULL;
1048 buffer_clear(m);
1049 if (sshpam_ctxt != NULL) {
1050 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1051 buffer_put_int(m, 1);
1052 } else {
1053 buffer_put_int(m, 0);
1054 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001055 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001056 return (0);
1057}
1058
1059int
Darren Tucker7eda5922004-06-22 13:22:50 +10001060mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001061{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001062 char *name = NULL, *info = NULL, **prompts = NULL;
1063 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001064 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001065
1066 debug3("%s", __func__);
1067 sshpam_authok = NULL;
1068 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1069 if (ret == 0 && num == 0)
1070 sshpam_authok = sshpam_ctxt;
1071 if (num > 1 || name == NULL || info == NULL)
1072 ret = -1;
1073 buffer_clear(m);
1074 buffer_put_int(m, ret);
1075 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001076 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001077 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001078 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001079 buffer_put_int(m, num);
1080 for (i = 0; i < num; ++i) {
1081 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001082 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001083 buffer_put_int(m, echo_on[i]);
1084 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001085 free(prompts);
1086 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001087 auth_method = "keyboard-interactive";
1088 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001089 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001090 return (0);
1091}
1092
1093int
Darren Tucker7eda5922004-06-22 13:22:50 +10001094mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001095{
1096 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001097 u_int i, num;
1098 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001099
1100 debug3("%s", __func__);
1101 sshpam_authok = NULL;
1102 num = buffer_get_int(m);
1103 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001104 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001105 for (i = 0; i < num; ++i)
1106 resp[i] = buffer_get_string(m, NULL);
1107 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1108 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001109 free(resp[i]);
1110 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001111 } else {
1112 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1113 }
1114 buffer_clear(m);
1115 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001116 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001117 auth_method = "keyboard-interactive";
1118 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001119 if (ret == 0)
1120 sshpam_authok = sshpam_ctxt;
1121 return (0);
1122}
1123
1124int
Darren Tucker7eda5922004-06-22 13:22:50 +10001125mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001126{
1127
1128 debug3("%s", __func__);
1129 (sshpam_device.free_ctx)(sshpam_ctxt);
1130 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001131 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001132 auth_method = "keyboard-interactive";
1133 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001134 return (sshpam_authok == sshpam_ctxt);
1135}
Damien Miller79418552002-04-23 20:28:48 +10001136#endif
1137
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001139mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140{
1141 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001142 char *cuser, *chost;
1143 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144 u_int bloblen;
1145 enum mm_keytype type = 0;
1146 int allowed = 0;
1147
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001148 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001149
1150 type = buffer_get_int(m);
1151 cuser = buffer_get_string(m, NULL);
1152 chost = buffer_get_string(m, NULL);
1153 blob = buffer_get_string(m, &bloblen);
1154
1155 key = key_from_blob(blob, bloblen);
1156
1157 if ((compat20 && type == MM_RSAHOSTKEY) ||
1158 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001159 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001161 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162
Damien Miller3e3b5142003-11-17 21:13:40 +11001163 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001164 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001166 allowed = options.pubkey_authentication &&
1167 user_key_allowed(authctxt->pw, key);
Darren Tucker0acca372013-06-02 07:41:51 +10001168 pubkey_auth_info(authctxt, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001169 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001170 if (options.pubkey_authentication && allowed != 1)
1171 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 break;
1173 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001174 allowed = options.hostbased_authentication &&
1175 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001176 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001177 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178 break;
1179 case MM_RSAHOSTKEY:
1180 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001181 allowed = options.rhosts_rsa_authentication &&
1182 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001184 if (options.rhosts_rsa_authentication && allowed != 1)
1185 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001186 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001187 break;
1188 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001189 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190 break;
1191 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001192 }
Damien Miller00111382003-03-10 11:21:17 +11001193 if (key != NULL)
1194 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195
1196 /* clear temporarily storage (used by verify) */
1197 monitor_reset_key_state();
1198
1199 if (allowed) {
1200 /* Save temporarily for comparison in verify */
1201 key_blob = blob;
1202 key_bloblen = bloblen;
1203 key_blobtype = type;
1204 hostbased_cuser = cuser;
1205 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001206 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001207 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001208 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001209 free(blob);
1210 free(cuser);
1211 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 }
1213
1214 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001215 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216
1217 buffer_clear(m);
1218 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001219 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001220
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001221 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222
1223 if (type == MM_RSAHOSTKEY)
1224 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1225
1226 return (0);
1227}
1228
1229static int
1230monitor_valid_userblob(u_char *data, u_int datalen)
1231{
1232 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001233 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234 u_int len;
1235 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236
1237 buffer_init(&b);
1238 buffer_append(&b, data, datalen);
1239
1240 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001241 p = buffer_ptr(&b);
1242 len = buffer_len(&b);
1243 if ((session_id2 == NULL) ||
1244 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001245 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001246 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 buffer_consume(&b, session_id2_len);
1248 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001249 p = buffer_get_string(&b, &len);
1250 if ((session_id2 == NULL) ||
1251 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001252 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001254 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 }
1256 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1257 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001258 p = buffer_get_cstring(&b, NULL);
1259 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1260 authctxt->style ? ":" : "",
1261 authctxt->style ? authctxt->style : "");
1262 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001263 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001264 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 fail++;
1266 }
Darren Tuckera627d422013-06-02 07:31:17 +10001267 free(userstyle);
1268 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 buffer_skip_string(&b);
1270 if (datafellows & SSH_BUG_PKAUTH) {
1271 if (!buffer_get_char(&b))
1272 fail++;
1273 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001274 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275 if (strcmp("publickey", p) != 0)
1276 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001277 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 if (!buffer_get_char(&b))
1279 fail++;
1280 buffer_skip_string(&b);
1281 }
1282 buffer_skip_string(&b);
1283 if (buffer_len(&b) != 0)
1284 fail++;
1285 buffer_free(&b);
1286 return (fail == 0);
1287}
1288
1289static int
Damien Millera10f5612002-09-12 09:49:15 +10001290monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1291 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292{
1293 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001294 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295 u_int len;
1296 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
1298 buffer_init(&b);
1299 buffer_append(&b, data, datalen);
1300
Ben Lindstromf67e0772002-06-06 20:58:19 +00001301 p = buffer_get_string(&b, &len);
1302 if ((session_id2 == NULL) ||
1303 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001304 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001306 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001307
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1309 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001310 p = buffer_get_cstring(&b, NULL);
1311 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1312 authctxt->style ? ":" : "",
1313 authctxt->style ? authctxt->style : "");
1314 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001315 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001316 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 fail++;
1318 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001319 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001320 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001322 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323 if (strcmp(p, "hostbased") != 0)
1324 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001325 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326 buffer_skip_string(&b); /* pkalg */
1327 buffer_skip_string(&b); /* pkblob */
1328
1329 /* verify client host, strip trailing dot if necessary */
1330 p = buffer_get_string(&b, NULL);
1331 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1332 p[len - 1] = '\0';
1333 if (strcmp(p, chost) != 0)
1334 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001335 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336
1337 /* verify client user */
1338 p = buffer_get_string(&b, NULL);
1339 if (strcmp(p, cuser) != 0)
1340 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001341 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342
1343 if (buffer_len(&b) != 0)
1344 fail++;
1345 buffer_free(&b);
1346 return (fail == 0);
1347}
1348
1349int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001350mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351{
1352 Key *key;
1353 u_char *signature, *data, *blob;
1354 u_int signaturelen, datalen, bloblen;
1355 int verified = 0;
1356 int valid_data = 0;
1357
1358 blob = buffer_get_string(m, &bloblen);
1359 signature = buffer_get_string(m, &signaturelen);
1360 data = buffer_get_string(m, &datalen);
1361
1362 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001363 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001364 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365
1366 key = key_from_blob(blob, bloblen);
1367 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001368 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001369
1370 switch (key_blobtype) {
1371 case MM_USERKEY:
1372 valid_data = monitor_valid_userblob(data, datalen);
1373 break;
1374 case MM_HOSTKEY:
1375 valid_data = monitor_valid_hostbasedblob(data, datalen,
1376 hostbased_cuser, hostbased_chost);
1377 break;
1378 default:
1379 valid_data = 0;
1380 break;
1381 }
1382 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001383 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 verified = key_verify(key, signature, signaturelen, data, datalen);
1386 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001387 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388
1389 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001390 free(blob);
1391 free(signature);
1392 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393
Ben Lindstrome1c09122002-06-23 00:38:24 +00001394 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1395
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 monitor_reset_key_state();
1397
1398 buffer_clear(m);
1399 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001400 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401
Darren Tuckerfbba7352006-11-07 23:16:08 +11001402 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403}
1404
1405static void
1406mm_record_login(Session *s, struct passwd *pw)
1407{
1408 socklen_t fromlen;
1409 struct sockaddr_storage from;
1410
1411 /*
1412 * Get IP address of client. If the connection is not a socket, let
1413 * the address be 0.0.0.0.
1414 */
1415 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001416 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001419 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001421 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422 }
1423 }
1424 /* Record that there was a login on that tty from the remote host. */
1425 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001426 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001427 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428}
1429
1430static void
1431mm_session_close(Session *s)
1432{
Damien Miller04bd8b02003-05-25 14:38:33 +10001433 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001435 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436 session_pty_cleanup2(s);
1437 }
Damien Miller7207f642008-05-19 15:34:50 +10001438 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439}
1440
1441int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001442mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001443{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001444 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 Session *s;
1446 int res, fd0;
1447
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001448 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449
1450 buffer_clear(m);
1451 s = session_new();
1452 if (s == NULL)
1453 goto error;
1454 s->authctxt = authctxt;
1455 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001456 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1458 if (res == 0)
1459 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 pty_setowner(authctxt->pw, s->tty);
1461
1462 buffer_put_int(m, 1);
1463 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464
1465 /* We need to trick ttyslot */
1466 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001467 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468
1469 mm_record_login(s, authctxt->pw);
1470
1471 /* Now we can close the file descriptor again */
1472 close(0);
1473
Darren Tucker09991742004-07-17 17:05:14 +10001474 /* send messages generated by record_login */
1475 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1476 buffer_clear(&loginmsg);
1477
1478 mm_request_send(sock, MONITOR_ANS_PTY, m);
1479
Damien Miller54fd7cf2007-09-17 12:04:08 +10001480 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1481 mm_send_fd(sock, s->ttyfd) == -1)
1482 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001483
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 /* make sure nothing uses fd 0 */
1485 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001486 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001488 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001489
1490 /* slave is not needed */
1491 close(s->ttyfd);
1492 s->ttyfd = s->ptyfd;
1493 /* no need to dup() because nobody closes ptyfd */
1494 s->ptymaster = s->ptyfd;
1495
Damien Miller9ab00b42006-08-05 12:40:11 +10001496 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497
1498 return (0);
1499
1500 error:
1501 if (s != NULL)
1502 mm_session_close(s);
1503 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001504 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 return (0);
1506}
1507
1508int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001509mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510{
1511 Session *s;
1512 char *tty;
1513
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001514 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515
1516 tty = buffer_get_string(m, NULL);
1517 if ((s = session_by_tty(tty)) != NULL)
1518 mm_session_close(s);
1519 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001520 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521 return (0);
1522}
1523
1524int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001525mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526{
1527 BIGNUM *p;
1528 int rsafail;
1529
1530 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001531 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532
1533 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001534 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535
1536 buffer_get_bignum2(m, p);
1537
1538 rsafail = ssh1_session_key(p);
1539
1540 buffer_clear(m);
1541 buffer_put_int(m, rsafail);
1542 buffer_put_bignum2(m, p);
1543
1544 BN_clear_free(p);
1545
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001546 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547
1548 /* Turn on permissions for sessid passing */
1549 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1550
1551 return (0);
1552}
1553
1554int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001555mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001556{
1557 int i;
1558
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001559 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001560
1561 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001562 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563 for (i = 0; i < 16; i++)
1564 session_id[i] = buffer_get_char(m);
1565
1566 /* Turn on permissions for getpwnam */
1567 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1568
1569 return (0);
1570}
1571
1572int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001573mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574{
1575 BIGNUM *client_n;
1576 Key *key = NULL;
1577 u_char *blob = NULL;
1578 u_int blen = 0;
1579 int allowed = 0;
1580
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001581 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582
Damien Miller7a8f5b32006-03-31 23:14:23 +11001583 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001584 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001586 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001587 buffer_get_bignum2(m, client_n);
1588 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1589 BN_clear_free(client_n);
1590 }
1591 buffer_clear(m);
1592 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001593 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001594
1595 /* clear temporarily storage (used by generate challenge) */
1596 monitor_reset_key_state();
1597
1598 if (allowed && key != NULL) {
1599 key->type = KEY_RSA; /* cheat for key_to_blob */
1600 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001601 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602 buffer_put_string(m, blob, blen);
1603
1604 /* Save temporarily for comparison in verify */
1605 key_blob = blob;
1606 key_bloblen = blen;
1607 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001608 }
Damien Miller00111382003-03-10 11:21:17 +11001609 if (key != NULL)
1610 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001612 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001613
1614 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1615 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1616 return (0);
1617}
1618
1619int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001620mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001621{
1622 Key *key = NULL;
1623 u_char *blob;
1624 u_int blen;
1625
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001626 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627
1628 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001629 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 blob = buffer_get_string(m, &blen);
1631 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001632 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001634 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001636 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001637 if (key->type != KEY_RSA)
1638 fatal("%s: received bad key type %d", __func__, key->type);
1639 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640 if (ssh1_challenge)
1641 BN_clear_free(ssh1_challenge);
1642 ssh1_challenge = auth_rsa_generate_challenge(key);
1643
1644 buffer_clear(m);
1645 buffer_put_bignum2(m, ssh1_challenge);
1646
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001647 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001648 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649
1650 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001651
Darren Tuckera627d422013-06-02 07:31:17 +10001652 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001653 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654 return (0);
1655}
1656
1657int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001658mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659{
1660 Key *key = NULL;
1661 u_char *blob, *response;
1662 u_int blen, len;
1663 int success;
1664
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666
1667 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001670 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671
1672 blob = buffer_get_string(m, &blen);
1673 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001674 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001676 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001678 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679 response = buffer_get_string(m, &len);
1680 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001681 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1683
Darren Tuckera627d422013-06-02 07:31:17 +10001684 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001686 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687
1688 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1689
1690 /* reset state */
1691 BN_clear_free(ssh1_challenge);
1692 ssh1_challenge = NULL;
1693 monitor_reset_key_state();
1694
1695 buffer_clear(m);
1696 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001697 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698
1699 return (success);
1700}
1701
1702int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001703mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001705 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001706 int res, status;
1707
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001708 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709
1710 /* The child is terminating */
1711 session_destroy_all(&mm_session_close);
1712
Darren Tucker52358d62008-03-11 22:58:25 +11001713#ifdef USE_PAM
1714 if (options.use_pam)
1715 sshpam_cleanup();
1716#endif
1717
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001718 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001719 if (errno != EINTR)
1720 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001721
1722 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1723
1724 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001725 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001726}
1727
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001728#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001729/* Report that an audit event occurred */
1730int
1731mm_answer_audit_event(int socket, Buffer *m)
1732{
1733 ssh_audit_event_t event;
1734
1735 debug3("%s entering", __func__);
1736
1737 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001738 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001739 case SSH_AUTH_FAIL_PUBKEY:
1740 case SSH_AUTH_FAIL_HOSTBASED:
1741 case SSH_AUTH_FAIL_GSSAPI:
1742 case SSH_LOGIN_EXCEED_MAXTRIES:
1743 case SSH_LOGIN_ROOT_DENIED:
1744 case SSH_CONNECTION_CLOSE:
1745 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001746 audit_event(event);
1747 break;
1748 default:
1749 fatal("Audit event type %d not permitted", event);
1750 }
1751
1752 return (0);
1753}
1754
1755int
1756mm_answer_audit_command(int socket, Buffer *m)
1757{
1758 u_int len;
1759 char *cmd;
1760
1761 debug3("%s entering", __func__);
1762 cmd = buffer_get_string(m, &len);
1763 /* sanity check command, if so how? */
1764 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001765 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001766 return (0);
1767}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001768#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001769
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001770void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001771monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772{
1773 if (compat20) {
1774 set_newkeys(MODE_IN);
1775 set_newkeys(MODE_OUT);
1776 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001777 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001778 packet_set_encryption_key(child_state.ssh1key,
1779 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001780 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781 }
1782
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001783 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001785 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001786 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001787 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001788
1789 if (!compat20) {
1790 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001791 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001792 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001793 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001794 }
1795
1796 memcpy(&incoming_stream, &child_state.incoming,
1797 sizeof(incoming_stream));
1798 memcpy(&outgoing_stream, &child_state.outgoing,
1799 sizeof(outgoing_stream));
1800
1801 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001802 if (options.compression)
1803 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001805 if (options.rekey_limit || options.rekey_interval)
1806 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1807 (time_t)options.rekey_interval);
1808
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001809 /* Network I/O buffers */
1810 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001811 buffer_clear(packet_get_input());
1812 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001813 memset(child_state.input, 0, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001814 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001815
Darren Tuckerf7288d72009-06-21 18:12:20 +10001816 buffer_clear(packet_get_output());
1817 buffer_append(packet_get_output(), child_state.output,
1818 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819 memset(child_state.output, 0, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001820 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001821
1822 /* Roaming */
1823 if (compat20)
1824 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825}
1826
1827static Kex *
1828mm_get_kex(Buffer *m)
1829{
1830 Kex *kex;
1831 void *blob;
1832 u_int bloblen;
1833
Damien Miller07d86be2006-03-26 14:19:21 +11001834 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001835 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001836 if (session_id2 == NULL ||
1837 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001838 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001839 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001840 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001841 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001842 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001843 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001844 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001845 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001846 kex->server = 1;
1847 kex->hostkey_type = buffer_get_int(m);
1848 kex->kex_type = buffer_get_int(m);
1849 blob = buffer_get_string(m, &bloblen);
1850 buffer_init(&kex->my);
1851 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001852 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001853 blob = buffer_get_string(m, &bloblen);
1854 buffer_init(&kex->peer);
1855 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001856 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001857 kex->done = 1;
1858 kex->flags = buffer_get_int(m);
1859 kex->client_version_string = buffer_get_string(m, NULL);
1860 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001861 kex->load_host_public_key=&get_hostkey_public_by_type;
1862 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001863 kex->host_key_index=&get_hostkey_index;
1864
1865 return (kex);
1866}
1867
1868/* This function requries careful sanity checking */
1869
1870void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001871mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001872{
1873 Buffer m;
1874 u_char *blob, *p;
1875 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001876 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001877 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001878
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001879 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001880
1881 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001882 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001883 if (!compat20) {
1884 child_state.ssh1protoflags = buffer_get_int(&m);
1885 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001886 child_state.ssh1key = buffer_get_string(&m,
1887 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001888 child_state.ivout = buffer_get_string(&m,
1889 &child_state.ivoutlen);
1890 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1891 goto skip;
1892 } else {
1893 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001894 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895 }
1896
1897 blob = buffer_get_string(&m, &bloblen);
1898 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001899 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001900
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001901 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001902 blob = buffer_get_string(&m, &bloblen);
1903 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001904 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905
1906 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001907 seqnr = buffer_get_int(&m);
1908 blocks = buffer_get_int64(&m);
1909 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001910 bytes = buffer_get_int64(&m);
1911 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001912 seqnr = buffer_get_int(&m);
1913 blocks = buffer_get_int64(&m);
1914 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001915 bytes = buffer_get_int64(&m);
1916 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001917
1918 skip:
1919 /* Get the key context */
1920 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1921 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1922
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001923 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001924 /* Get compression state */
1925 p = buffer_get_string(&m, &plen);
1926 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001927 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001928 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001929 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930
1931 p = buffer_get_string(&m, &plen);
1932 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001933 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001934 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001935 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001936
1937 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001938 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001939 child_state.input = buffer_get_string(&m, &child_state.ilen);
1940 child_state.output = buffer_get_string(&m, &child_state.olen);
1941
Darren Tuckerc5564e12009-06-21 18:53:53 +10001942 /* Roaming */
1943 if (compat20) {
1944 child_state.sent_bytes = buffer_get_int64(&m);
1945 child_state.recv_bytes = buffer_get_int64(&m);
1946 }
1947
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001948 buffer_free(&m);
1949}
1950
1951
1952/* Allocation functions for zlib */
1953void *
1954mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1955{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001956 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001957 void *address;
1958
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001959 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001960 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1961
1962 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001963
1964 return (address);
1965}
1966
1967void
1968mm_zfree(struct mm_master *mm, void *address)
1969{
1970 mm_free(mm, address);
1971}
1972
1973void
1974mm_init_compression(struct mm_master *mm)
1975{
1976 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1977 outgoing_stream.zfree = (free_func)mm_zfree;
1978 outgoing_stream.opaque = mm;
1979
1980 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1981 incoming_stream.zfree = (free_func)mm_zfree;
1982 incoming_stream.opaque = mm;
1983}
1984
1985/* XXX */
1986
1987#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001988 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001989 fatal("fcntl(%d, F_SETFD)", x); \
1990} while (0)
1991
1992static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001993monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001994{
Damien Miller8f0bf232011-06-20 14:42:23 +10001995 int pair[2];
1996
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001997 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001998 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001999 FD_CLOSEONEXEC(pair[0]);
2000 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002001 mon->m_recvfd = pair[0];
2002 mon->m_sendfd = pair[1];
2003
2004 if (do_logfds) {
2005 if (pipe(pair) == -1)
2006 fatal("%s: pipe: %s", __func__, strerror(errno));
2007 FD_CLOSEONEXEC(pair[0]);
2008 FD_CLOSEONEXEC(pair[1]);
2009 mon->m_log_recvfd = pair[0];
2010 mon->m_log_sendfd = pair[1];
2011 } else
2012 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002013}
2014
2015#define MM_MEMSIZE 65536
2016
2017struct monitor *
2018monitor_init(void)
2019{
2020 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002021
Damien Miller07d86be2006-03-26 14:19:21 +11002022 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002023
Damien Miller8f0bf232011-06-20 14:42:23 +10002024 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002025
2026 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002027 if (options.compression) {
2028 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2029 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002030
Damien Miller2d6b8332002-06-21 15:59:49 +10002031 /* Compression needs to share state across borders */
2032 mm_init_compression(mon->m_zlib);
2033 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002034
2035 return mon;
2036}
2037
2038void
2039monitor_reinit(struct monitor *mon)
2040{
Damien Miller8f0bf232011-06-20 14:42:23 +10002041 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002042}
Darren Tucker0efd1552003-08-26 11:49:55 +10002043
2044#ifdef GSSAPI
2045int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002046mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002047{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002048 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002049 OM_uint32 major;
2050 u_int len;
2051
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002052 goid.elements = buffer_get_string(m, &len);
2053 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002054
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002055 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002056
Darren Tuckera627d422013-06-02 07:31:17 +10002057 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002058
2059 buffer_clear(m);
2060 buffer_put_int(m, major);
2061
Damien Miller6fd6def2005-11-05 15:07:05 +11002062 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002063
2064 /* Now we have a context, enable the step */
2065 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2066
2067 return (0);
2068}
2069
2070int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002071mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002072{
2073 gss_buffer_desc in;
2074 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002075 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002076 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002077 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002078
Darren Tucker600ad8d2003-08-26 12:10:48 +10002079 in.value = buffer_get_string(m, &len);
2080 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002081 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002082 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002083
2084 buffer_clear(m);
2085 buffer_put_int(m, major);
2086 buffer_put_string(m, out.value, out.length);
2087 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002088 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002089
2090 gss_release_buffer(&minor, &out);
2091
Damien Miller6fd6def2005-11-05 15:07:05 +11002092 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002093 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2094 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002095 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002096 }
2097 return (0);
2098}
2099
2100int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002101mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002102{
2103 gss_buffer_desc gssbuf, mic;
2104 OM_uint32 ret;
2105 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002106
Damien Miller0425d402003-11-17 22:18:21 +11002107 gssbuf.value = buffer_get_string(m, &len);
2108 gssbuf.length = len;
2109 mic.value = buffer_get_string(m, &len);
2110 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002111
Damien Miller0425d402003-11-17 22:18:21 +11002112 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002113
Darren Tuckera627d422013-06-02 07:31:17 +10002114 free(gssbuf.value);
2115 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002116
Damien Miller0425d402003-11-17 22:18:21 +11002117 buffer_clear(m);
2118 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002119
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002120 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002121
Damien Miller0425d402003-11-17 22:18:21 +11002122 if (!GSS_ERROR(ret))
2123 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002124
Damien Miller0425d402003-11-17 22:18:21 +11002125 return (0);
2126}
2127
2128int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002129mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002130{
2131 int authenticated;
2132
2133 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2134
2135 buffer_clear(m);
2136 buffer_put_int(m, authenticated);
2137
2138 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002139 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002140
Damien Miller6fd6def2005-11-05 15:07:05 +11002141 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002142
2143 /* Monitor loop will terminate if authenticated */
2144 return (authenticated);
2145}
2146#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002147
2148#ifdef JPAKE
2149int
2150mm_answer_jpake_step1(int sock, Buffer *m)
2151{
2152 struct jpake_ctx *pctx;
2153 u_char *x3_proof, *x4_proof;
2154 u_int x3_proof_len, x4_proof_len;
2155
2156 if (!options.zero_knowledge_password_authentication)
2157 fatal("zero_knowledge_password_authentication disabled");
2158
2159 if (authctxt->jpake_ctx != NULL)
2160 fatal("%s: authctxt->jpake_ctx already set (%p)",
2161 __func__, authctxt->jpake_ctx);
2162 authctxt->jpake_ctx = pctx = jpake_new();
2163
2164 jpake_step1(pctx->grp,
2165 &pctx->server_id, &pctx->server_id_len,
2166 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2167 &x3_proof, &x3_proof_len,
2168 &x4_proof, &x4_proof_len);
2169
2170 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2171
2172 buffer_clear(m);
2173
2174 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2175 buffer_put_bignum2(m, pctx->g_x3);
2176 buffer_put_bignum2(m, pctx->g_x4);
2177 buffer_put_string(m, x3_proof, x3_proof_len);
2178 buffer_put_string(m, x4_proof, x4_proof_len);
2179
2180 debug3("%s: sending step1", __func__);
2181 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2182
2183 bzero(x3_proof, x3_proof_len);
2184 bzero(x4_proof, x4_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002185 free(x3_proof);
2186 free(x4_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002187
2188 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2189 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2190
2191 return 0;
2192}
2193
2194int
2195mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2196{
2197 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2198 char *hash_scheme, *salt;
2199
2200 if (pctx == NULL)
2201 fatal("%s: pctx == NULL", __func__);
2202
2203 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2204
2205 buffer_clear(m);
2206 /* pctx->s is sensitive, not returned to slave */
2207 buffer_put_cstring(m, hash_scheme);
2208 buffer_put_cstring(m, salt);
2209
2210 debug3("%s: sending pwdata", __func__);
2211 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2212
2213 bzero(hash_scheme, strlen(hash_scheme));
2214 bzero(salt, strlen(salt));
Darren Tuckera627d422013-06-02 07:31:17 +10002215 free(hash_scheme);
2216 free(salt);
Damien Miller01ed2272008-11-05 16:20:46 +11002217
2218 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2219
2220 return 0;
2221}
2222
2223int
2224mm_answer_jpake_step2(int sock, Buffer *m)
2225{
2226 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2227 u_char *x1_proof, *x2_proof, *x4_s_proof;
2228 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2229
2230 if (pctx == NULL)
2231 fatal("%s: pctx == NULL", __func__);
2232
2233 if ((pctx->g_x1 = BN_new()) == NULL ||
2234 (pctx->g_x2 = BN_new()) == NULL)
2235 fatal("%s: BN_new", __func__);
2236 buffer_get_bignum2(m, pctx->g_x1);
2237 buffer_get_bignum2(m, pctx->g_x2);
2238 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2239 x1_proof = buffer_get_string(m, &x1_proof_len);
2240 x2_proof = buffer_get_string(m, &x2_proof_len);
2241
2242 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2243 pctx->g_x1, pctx->g_x2, pctx->x4,
2244 pctx->client_id, pctx->client_id_len,
2245 pctx->server_id, pctx->server_id_len,
2246 x1_proof, x1_proof_len,
2247 x2_proof, x2_proof_len,
2248 &pctx->b,
2249 &x4_s_proof, &x4_s_proof_len);
2250
2251 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2252
2253 bzero(x1_proof, x1_proof_len);
2254 bzero(x2_proof, x2_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002255 free(x1_proof);
2256 free(x2_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002257
2258 buffer_clear(m);
2259
2260 buffer_put_bignum2(m, pctx->b);
2261 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2262
2263 debug3("%s: sending step2", __func__);
2264 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2265
2266 bzero(x4_s_proof, x4_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002267 free(x4_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002268
2269 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2270
2271 return 0;
2272}
2273
2274int
2275mm_answer_jpake_key_confirm(int sock, Buffer *m)
2276{
2277 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2278 u_char *x2_s_proof;
2279 u_int x2_s_proof_len;
2280
2281 if (pctx == NULL)
2282 fatal("%s: pctx == NULL", __func__);
2283
2284 if ((pctx->a = BN_new()) == NULL)
2285 fatal("%s: BN_new", __func__);
2286 buffer_get_bignum2(m, pctx->a);
2287 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2288
2289 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2290 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2291 pctx->server_id, pctx->server_id_len,
2292 pctx->client_id, pctx->client_id_len,
2293 session_id2, session_id2_len,
2294 x2_s_proof, x2_s_proof_len,
2295 &pctx->k,
2296 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2297
2298 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2299
2300 bzero(x2_s_proof, x2_s_proof_len);
2301 buffer_clear(m);
2302
2303 /* pctx->k is sensitive, not sent */
2304 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2305
2306 debug3("%s: sending confirmation hash", __func__);
2307 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2308
2309 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2310
2311 return 0;
2312}
2313
2314int
2315mm_answer_jpake_check_confirm(int sock, Buffer *m)
2316{
2317 int authenticated = 0;
2318 u_char *peer_confirm_hash;
2319 u_int peer_confirm_hash_len;
2320 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2321
2322 if (pctx == NULL)
2323 fatal("%s: pctx == NULL", __func__);
2324
2325 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2326
2327 authenticated = jpake_check_confirm(pctx->k,
2328 pctx->client_id, pctx->client_id_len,
2329 session_id2, session_id2_len,
2330 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2331
2332 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2333
2334 bzero(peer_confirm_hash, peer_confirm_hash_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002335 free(peer_confirm_hash);
Damien Miller01ed2272008-11-05 16:20:46 +11002336
2337 buffer_clear(m);
2338 buffer_put_int(m, authenticated);
2339
2340 debug3("%s: sending result %d", __func__, authenticated);
2341 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2342
2343 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2344
2345 auth_method = "jpake-01@openssh.com";
2346 return authenticated;
2347}
2348
2349#endif /* JPAKE */