blob: 132f60df93000bbfdfe2f2ab43eb031bc8a1d905 [file] [log] [blame]
Darren Tuckera627d422013-06-02 07:31:17 +10001/* $OpenBSD: monitor.c,v 1.124 2013/05/17 00:13:13 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,
425 auth_method, auth_submethod,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 compat20 ? " ssh2" : "");
427 if (!authenticated)
428 authctxt->failures++;
429 }
Damien Miller01ed2272008-11-05 16:20:46 +1100430#ifdef JPAKE
431 /* Cleanup JPAKE context after authentication */
432 if (ent->flags & MON_AUTHDECIDE) {
433 if (authctxt->jpake_ctx != NULL) {
434 jpake_free(authctxt->jpake_ctx);
435 authctxt->jpake_ctx = NULL;
436 }
437 }
438#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439 }
440
441 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000442 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100443 if (strcmp(auth_method, "unknown") == 0)
444 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445
446 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000449 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000450
Damien Miller6a1937e2012-12-12 10:44:38 +1100451 /* Drain any buffered messages from the child */
452 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
453 ;
454
Damien Miller8f0bf232011-06-20 14:42:23 +1000455 close(pmonitor->m_sendfd);
456 close(pmonitor->m_log_recvfd);
457 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458}
459
Damien Millerbe64d432003-05-14 19:31:12 +1000460static void
461monitor_set_child_handler(pid_t pid)
462{
463 monitor_child_pid = pid;
464}
465
466static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000467monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000468{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000469 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000470}
471
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000473monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474{
Damien Miller8f0bf232011-06-20 14:42:23 +1000475 close(pmonitor->m_recvfd);
476 pmonitor->m_recvfd = -1;
477
Damien Millerbe64d432003-05-14 19:31:12 +1000478 monitor_set_child_handler(pmonitor->m_pid);
479 signal(SIGHUP, &monitor_child_handler);
480 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000481 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000482
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483 if (compat20) {
484 mon_dispatch = mon_dispatch_postauth20;
485
486 /* Permit requests for moduli and signatures */
487 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
488 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
489 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490 } else {
491 mon_dispatch = mon_dispatch_postauth15;
492 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
493 }
494 if (!no_pty_flag) {
495 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
496 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
497 }
498
499 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000500 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501}
502
503void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000504monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000505{
Damien Miller2d6b8332002-06-21 15:59:49 +1000506 if (options.compression) {
507 /* The member allocation is not visible, so sync it */
508 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
509 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510}
511
Damien Miller8f0bf232011-06-20 14:42:23 +1000512static int
513monitor_read_log(struct monitor *pmonitor)
514{
515 Buffer logmsg;
516 u_int len, level;
517 char *msg;
518
519 buffer_init(&logmsg);
520
521 /* Read length */
522 buffer_append_space(&logmsg, 4);
523 if (atomicio(read, pmonitor->m_log_recvfd,
524 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
525 if (errno == EPIPE) {
Damien Millera2876db2012-02-11 08:16:06 +1100526 buffer_free(&logmsg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000527 debug("%s: child log fd closed", __func__);
528 close(pmonitor->m_log_recvfd);
529 pmonitor->m_log_recvfd = -1;
530 return -1;
531 }
532 fatal("%s: log fd read: %s", __func__, strerror(errno));
533 }
534 len = buffer_get_int(&logmsg);
535 if (len <= 4 || len > 8192)
536 fatal("%s: invalid log message length %u", __func__, len);
537
538 /* Read severity, message */
539 buffer_clear(&logmsg);
540 buffer_append_space(&logmsg, len);
541 if (atomicio(read, pmonitor->m_log_recvfd,
542 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
543 fatal("%s: log fd read: %s", __func__, strerror(errno));
544
545 /* Log it */
546 level = buffer_get_int(&logmsg);
547 msg = buffer_get_string(&logmsg, NULL);
548 if (log_level_name(level) == NULL)
549 fatal("%s: invalid log level %u (corrupted message?)",
550 __func__, level);
551 do_log2(level, "%s [preauth]", msg);
552
553 buffer_free(&logmsg);
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(msg);
Damien Miller8f0bf232011-06-20 14:42:23 +1000555
556 return 0;
557}
558
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000560monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 struct mon_table **pent)
562{
563 Buffer m;
564 int ret;
565 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000566 struct pollfd pfd[2];
567
568 for (;;) {
569 bzero(&pfd, sizeof(pfd));
570 pfd[0].fd = pmonitor->m_sendfd;
571 pfd[0].events = POLLIN;
572 pfd[1].fd = pmonitor->m_log_recvfd;
573 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
Damien Miller7741ce82011-08-06 06:15:15 +1000574 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
575 if (errno == EINTR || errno == EAGAIN)
576 continue;
Damien Miller8f0bf232011-06-20 14:42:23 +1000577 fatal("%s: poll: %s", __func__, strerror(errno));
Damien Miller7741ce82011-08-06 06:15:15 +1000578 }
Damien Miller8f0bf232011-06-20 14:42:23 +1000579 if (pfd[1].revents) {
580 /*
581 * Drain all log messages before processing next
582 * monitor request.
583 */
584 monitor_read_log(pmonitor);
585 continue;
586 }
587 if (pfd[0].revents)
588 break; /* Continues below */
589 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 buffer_init(&m);
592
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000593 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594 type = buffer_get_char(&m);
595
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000596 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 while (ent->f != NULL) {
599 if (ent->type == type)
600 break;
601 ent++;
602 }
603
604 if (ent->f != NULL) {
605 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000606 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000607 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000608 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609 buffer_free(&m);
610
611 /* The child may use this request only once, disable it */
612 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000613 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614 type);
615 ent->flags &= ~MON_PERMIT;
616 }
617
618 if (pent != NULL)
619 *pent = ent;
620
621 return ret;
622 }
623
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000624 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625
626 /* NOTREACHED */
627 return (-1);
628}
629
630/* allowed key state */
631static int
632monitor_allowed_key(u_char *blob, u_int bloblen)
633{
634 /* make sure key is allowed */
635 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000636 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637 return (0);
638 return (1);
639}
640
641static void
642monitor_reset_key_state(void)
643{
644 /* reset state */
Darren Tuckera627d422013-06-02 07:31:17 +1000645 free(key_blob);
646 free(hostbased_cuser);
647 free(hostbased_chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 key_blob = NULL;
649 key_bloblen = 0;
650 key_blobtype = MM_NOKEY;
651 hostbased_cuser = NULL;
652 hostbased_chost = NULL;
653}
654
655int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000656mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657{
658 DH *dh;
659 int min, want, max;
660
661 min = buffer_get_int(m);
662 want = buffer_get_int(m);
663 max = buffer_get_int(m);
664
665 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000666 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667 /* We need to check here, too, in case the child got corrupted */
668 if (max < min || want < min || max < want)
669 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000670 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000671
672 buffer_clear(m);
673
674 dh = choose_dh(min, want, max);
675 if (dh == NULL) {
676 buffer_put_char(m, 0);
677 return (0);
678 } else {
679 /* Send first bignum */
680 buffer_put_char(m, 1);
681 buffer_put_bignum2(m, dh->p);
682 buffer_put_bignum2(m, dh->g);
683
684 DH_free(dh);
685 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000686 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000687 return (0);
688}
689
690int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000691mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692{
693 Key *key;
694 u_char *p;
695 u_char *signature;
696 u_int siglen, datlen;
697 int keyid;
698
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000699 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
701 keyid = buffer_get_int(m);
702 p = buffer_get_string(m, &datlen);
703
Damien Millera63128d2006-03-15 12:08:28 +1100704 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000705 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
706 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100707 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000708 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000709 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710
Ben Lindstromf67e0772002-06-06 20:58:19 +0000711 /* save session id, it will be passed on the first call */
712 if (session_id2_len == 0) {
713 session_id2_len = datlen;
714 session_id2 = xmalloc(session_id2_len);
715 memcpy(session_id2, p, session_id2_len);
716 }
717
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000719 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000721 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
Ben Lindstromd5502182002-06-27 00:12:57 +0000723 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
725 buffer_clear(m);
726 buffer_put_string(m, signature, siglen);
727
Darren Tuckera627d422013-06-02 07:31:17 +1000728 free(p);
729 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000731 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
733 /* Turn on permissions for getpwnam */
734 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
735
736 return (0);
737}
738
739/* Retrieves the password entry and also checks if the user is permitted */
740
741int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000742mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000744 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000745 struct passwd *pwent;
746 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000747 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000749 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000750
751 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000752 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753
Darren Tuckerb09b6772004-06-22 15:06:46 +1000754 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755
Darren Tuckerb09b6772004-06-22 15:06:46 +1000756 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757
Darren Tuckerb09b6772004-06-22 15:06:46 +1000758 authctxt->user = xstrdup(username);
759 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000760 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
762 buffer_clear(m);
763
764 if (pwent == NULL) {
765 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100766 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767 goto out;
768 }
769
770 allowed = 1;
771 authctxt->pw = pwent;
772 authctxt->valid = 1;
773
774 buffer_put_char(m, 1);
775 buffer_put_string(m, pwent, sizeof(struct passwd));
776 buffer_put_cstring(m, pwent->pw_name);
777 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000778#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000780#endif
781#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000783#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784 buffer_put_cstring(m, pwent->pw_dir);
785 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100786
787 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100788 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000789
790#define M_CP_STROPT(x) do { \
791 if (options.x != NULL) \
792 buffer_put_cstring(m, options.x); \
793 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000794#define M_CP_STRARRAYOPT(x, nx) do { \
795 for (i = 0; i < options.nx; i++) \
796 buffer_put_cstring(m, options.x[i]); \
797 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000798 /* See comment in servconf.h */
799 COPY_MATCH_STRING_OPTS();
800#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000801#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100802
803 /* Create valid auth method lists */
804 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
805 /*
806 * The monitor will continue long enough to let the child
807 * run to it's packet_disconnect(), but it must not allow any
808 * authentication to succeed.
809 */
810 debug("%s: no valid authentication method lists", __func__);
811 }
812
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000813 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000814 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815
816 /* For SSHv1 allow authentication now */
817 if (!compat20)
818 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000819 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820 /* Allow service/style information on the auth context */
821 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000822 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
823 }
Damien Millera33501b2002-05-08 12:24:42 +1000824#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000825 if (options.use_pam)
826 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000827#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828
829 return (0);
830}
831
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000832int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000833{
834 char *banner;
835
836 buffer_clear(m);
837 banner = auth2_read_banner();
838 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000839 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000840 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000841
842 return (0);
843}
844
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000846mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000847{
848 monitor_permit_authentications(1);
849
850 authctxt->service = buffer_get_string(m, NULL);
851 authctxt->style = buffer_get_string(m, NULL);
852 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000853 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000854
855 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000856 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857 authctxt->style = NULL;
858 }
859
860 return (0);
861}
862
863int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000864mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865{
866 static int call_count;
867 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000868 int authenticated;
869 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870
871 passwd = buffer_get_string(m, &plen);
872 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000873 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000874 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000875 memset(passwd, 0, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000876 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000877
878 buffer_clear(m);
879 buffer_put_int(m, authenticated);
880
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000881 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000882 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883
884 call_count++;
885 if (plen == 0 && call_count == 1)
886 auth_method = "none";
887 else
888 auth_method = "password";
889
890 /* Causes monitor loop to terminate if authenticated */
891 return (authenticated);
892}
893
894#ifdef BSD_AUTH
895int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000896mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897{
898 char *name, *infotxt;
899 u_int numprompts;
900 u_int *echo_on;
901 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100902 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903
Damien Millerb7df3af2003-02-24 11:55:46 +1100904 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
905 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906
907 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100908 buffer_put_int(m, success);
909 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910 buffer_put_cstring(m, prompts[0]);
911
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000913 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
Damien Millerb7df3af2003-02-24 11:55:46 +1100915 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000916 free(name);
917 free(infotxt);
918 free(prompts);
919 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000920 }
921
922 return (0);
923}
924
925int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000926mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000927{
928 char *response;
929 int authok;
930
931 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000932 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933
934 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000935 authok = options.challenge_response_authentication &&
936 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000937 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000938 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000939 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940
941 buffer_clear(m);
942 buffer_put_int(m, authok);
943
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000944 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000945 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000946
Damien Miller91a55f22013-04-23 15:18:10 +1000947 if (compat20) {
948 auth_method = "keyboard-interactive";
949 auth_submethod = "bsdauth";
950 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100951 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952
953 return (authok != 0);
954}
955#endif
956
957#ifdef SKEY
958int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000959mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960{
961 struct skey skey;
962 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100963 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000965 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
966 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000967
968 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100969 buffer_put_int(m, success);
970 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 buffer_put_cstring(m, challenge);
972
Damien Millerb7df3af2003-02-24 11:55:46 +1100973 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000974 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 return (0);
977}
978
979int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000980mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981{
982 char *response;
983 int authok;
984
985 response = buffer_get_string(m, NULL);
986
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000987 authok = (options.challenge_response_authentication &&
988 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989 skey_haskey(authctxt->pw->pw_name) == 0 &&
990 skey_passcheck(authctxt->pw->pw_name, response) != -1);
991
992 xfree(response);
993
994 buffer_clear(m);
995 buffer_put_int(m, authok);
996
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000997 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000998 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999
1000 auth_method = "skey";
1001
1002 return (authok != 0);
1003}
1004#endif
1005
Damien Miller79418552002-04-23 20:28:48 +10001006#ifdef USE_PAM
1007int
Darren Tucker7eda5922004-06-22 13:22:50 +10001008mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001009{
Damien Miller4e448a32003-05-14 15:11:48 +10001010 if (!options.use_pam)
1011 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1012
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001013 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001014
Damien Miller1f499fd2003-08-25 13:08:49 +10001015 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1016
Damien Miller79418552002-04-23 20:28:48 +10001017 return (0);
1018}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001019
Damien Miller1f499fd2003-08-25 13:08:49 +10001020int
Darren Tucker7eda5922004-06-22 13:22:50 +10001021mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001022{
1023 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001024
Damien Miller1f499fd2003-08-25 13:08:49 +10001025 if (!options.use_pam)
1026 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1027
1028 ret = do_pam_account();
1029
1030 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001031 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001032
Darren Tucker7eda5922004-06-22 13:22:50 +10001033 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001034
1035 return (ret);
1036}
1037
Damien Miller4f9f42a2003-05-10 19:28:02 +10001038static void *sshpam_ctxt, *sshpam_authok;
1039extern KbdintDevice sshpam_device;
1040
1041int
Darren Tucker7eda5922004-06-22 13:22:50 +10001042mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001043{
1044
1045 debug3("%s", __func__);
1046 authctxt->user = buffer_get_string(m, NULL);
1047 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1048 sshpam_authok = NULL;
1049 buffer_clear(m);
1050 if (sshpam_ctxt != NULL) {
1051 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1052 buffer_put_int(m, 1);
1053 } else {
1054 buffer_put_int(m, 0);
1055 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001056 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001057 return (0);
1058}
1059
1060int
Darren Tucker7eda5922004-06-22 13:22:50 +10001061mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001062{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001063 char *name = NULL, *info = NULL, **prompts = NULL;
1064 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001065 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066
1067 debug3("%s", __func__);
1068 sshpam_authok = NULL;
1069 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1070 if (ret == 0 && num == 0)
1071 sshpam_authok = sshpam_ctxt;
1072 if (num > 1 || name == NULL || info == NULL)
1073 ret = -1;
1074 buffer_clear(m);
1075 buffer_put_int(m, ret);
1076 buffer_put_cstring(m, name);
1077 xfree(name);
1078 buffer_put_cstring(m, info);
1079 xfree(info);
1080 buffer_put_int(m, num);
1081 for (i = 0; i < num; ++i) {
1082 buffer_put_cstring(m, prompts[i]);
1083 xfree(prompts[i]);
1084 buffer_put_int(m, echo_on[i]);
1085 }
1086 if (prompts != NULL)
1087 xfree(prompts);
1088 if (echo_on != NULL)
1089 xfree(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001090 auth_method = "keyboard-interactive";
1091 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001092 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001093 return (0);
1094}
1095
1096int
Darren Tucker7eda5922004-06-22 13:22:50 +10001097mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001098{
1099 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001100 u_int i, num;
1101 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001102
1103 debug3("%s", __func__);
1104 sshpam_authok = NULL;
1105 num = buffer_get_int(m);
1106 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001107 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108 for (i = 0; i < num; ++i)
1109 resp[i] = buffer_get_string(m, NULL);
1110 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1111 for (i = 0; i < num; ++i)
1112 xfree(resp[i]);
1113 xfree(resp);
1114 } else {
1115 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1116 }
1117 buffer_clear(m);
1118 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001119 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001120 auth_method = "keyboard-interactive";
1121 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001122 if (ret == 0)
1123 sshpam_authok = sshpam_ctxt;
1124 return (0);
1125}
1126
1127int
Darren Tucker7eda5922004-06-22 13:22:50 +10001128mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001129{
1130
1131 debug3("%s", __func__);
1132 (sshpam_device.free_ctx)(sshpam_ctxt);
1133 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001134 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001135 auth_method = "keyboard-interactive";
1136 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001137 return (sshpam_authok == sshpam_ctxt);
1138}
Damien Miller79418552002-04-23 20:28:48 +10001139#endif
1140
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001141int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001142mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143{
1144 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001145 char *cuser, *chost;
1146 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147 u_int bloblen;
1148 enum mm_keytype type = 0;
1149 int allowed = 0;
1150
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001151 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152
1153 type = buffer_get_int(m);
1154 cuser = buffer_get_string(m, NULL);
1155 chost = buffer_get_string(m, NULL);
1156 blob = buffer_get_string(m, &bloblen);
1157
1158 key = key_from_blob(blob, bloblen);
1159
1160 if ((compat20 && type == MM_RSAHOSTKEY) ||
1161 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001162 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001163
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001164 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001165
Damien Miller3e3b5142003-11-17 21:13:40 +11001166 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001167 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001169 allowed = options.pubkey_authentication &&
1170 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001171 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001172 if (options.pubkey_authentication && allowed != 1)
1173 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 break;
1175 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001176 allowed = options.hostbased_authentication &&
1177 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001178 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001179 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 break;
1181 case MM_RSAHOSTKEY:
1182 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001183 allowed = options.rhosts_rsa_authentication &&
1184 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001186 if (options.rhosts_rsa_authentication && allowed != 1)
1187 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001188 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189 break;
1190 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001191 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001192 break;
1193 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001194 }
Damien Miller00111382003-03-10 11:21:17 +11001195 if (key != NULL)
1196 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197
1198 /* clear temporarily storage (used by verify) */
1199 monitor_reset_key_state();
1200
1201 if (allowed) {
1202 /* Save temporarily for comparison in verify */
1203 key_blob = blob;
1204 key_bloblen = bloblen;
1205 key_blobtype = type;
1206 hostbased_cuser = cuser;
1207 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001208 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001209 /* Log failed attempt */
Damien Miller15b05cf2012-12-03 09:53:20 +11001210 auth_log(authctxt, 0, 0, auth_method, NULL,
1211 compat20 ? " ssh2" : "");
Darren Tuckera627d422013-06-02 07:31:17 +10001212 free(blob);
1213 free(cuser);
1214 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 }
1216
1217 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001218 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219
1220 buffer_clear(m);
1221 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001222 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001224 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
1226 if (type == MM_RSAHOSTKEY)
1227 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1228
1229 return (0);
1230}
1231
1232static int
1233monitor_valid_userblob(u_char *data, u_int datalen)
1234{
1235 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001236 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 u_int len;
1238 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239
1240 buffer_init(&b);
1241 buffer_append(&b, data, datalen);
1242
1243 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001244 p = buffer_ptr(&b);
1245 len = buffer_len(&b);
1246 if ((session_id2 == NULL) ||
1247 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001248 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001249 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 buffer_consume(&b, session_id2_len);
1251 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001252 p = buffer_get_string(&b, &len);
1253 if ((session_id2 == NULL) ||
1254 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001255 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001257 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 }
1259 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1260 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001261 p = buffer_get_cstring(&b, NULL);
1262 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1263 authctxt->style ? ":" : "",
1264 authctxt->style ? authctxt->style : "");
1265 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001266 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001267 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268 fail++;
1269 }
Darren Tuckera627d422013-06-02 07:31:17 +10001270 free(userstyle);
1271 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 buffer_skip_string(&b);
1273 if (datafellows & SSH_BUG_PKAUTH) {
1274 if (!buffer_get_char(&b))
1275 fail++;
1276 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001277 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 if (strcmp("publickey", p) != 0)
1279 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001280 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281 if (!buffer_get_char(&b))
1282 fail++;
1283 buffer_skip_string(&b);
1284 }
1285 buffer_skip_string(&b);
1286 if (buffer_len(&b) != 0)
1287 fail++;
1288 buffer_free(&b);
1289 return (fail == 0);
1290}
1291
1292static int
Damien Millera10f5612002-09-12 09:49:15 +10001293monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1294 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295{
1296 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001297 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298 u_int len;
1299 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001300
1301 buffer_init(&b);
1302 buffer_append(&b, data, datalen);
1303
Ben Lindstromf67e0772002-06-06 20:58:19 +00001304 p = buffer_get_string(&b, &len);
1305 if ((session_id2 == NULL) ||
1306 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001307 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001309 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001310
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1312 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001313 p = buffer_get_cstring(&b, NULL);
1314 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1315 authctxt->style ? ":" : "",
1316 authctxt->style ? authctxt->style : "");
1317 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001318 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001319 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 fail++;
1321 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001322 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001323 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001325 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326 if (strcmp(p, "hostbased") != 0)
1327 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001328 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 buffer_skip_string(&b); /* pkalg */
1330 buffer_skip_string(&b); /* pkblob */
1331
1332 /* verify client host, strip trailing dot if necessary */
1333 p = buffer_get_string(&b, NULL);
1334 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1335 p[len - 1] = '\0';
1336 if (strcmp(p, chost) != 0)
1337 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001338 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339
1340 /* verify client user */
1341 p = buffer_get_string(&b, NULL);
1342 if (strcmp(p, cuser) != 0)
1343 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001344 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001345
1346 if (buffer_len(&b) != 0)
1347 fail++;
1348 buffer_free(&b);
1349 return (fail == 0);
1350}
1351
1352int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001353mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354{
1355 Key *key;
1356 u_char *signature, *data, *blob;
1357 u_int signaturelen, datalen, bloblen;
1358 int verified = 0;
1359 int valid_data = 0;
1360
1361 blob = buffer_get_string(m, &bloblen);
1362 signature = buffer_get_string(m, &signaturelen);
1363 data = buffer_get_string(m, &datalen);
1364
1365 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001366 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001367 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368
1369 key = key_from_blob(blob, bloblen);
1370 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001371 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 switch (key_blobtype) {
1374 case MM_USERKEY:
1375 valid_data = monitor_valid_userblob(data, datalen);
1376 break;
1377 case MM_HOSTKEY:
1378 valid_data = monitor_valid_hostbasedblob(data, datalen,
1379 hostbased_cuser, hostbased_chost);
1380 break;
1381 default:
1382 valid_data = 0;
1383 break;
1384 }
1385 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001386 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387
1388 verified = key_verify(key, signature, signaturelen, data, datalen);
1389 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001390 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391
1392 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001393 free(blob);
1394 free(signature);
1395 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396
Ben Lindstrome1c09122002-06-23 00:38:24 +00001397 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1398
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399 monitor_reset_key_state();
1400
1401 buffer_clear(m);
1402 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001403 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404
Darren Tuckerfbba7352006-11-07 23:16:08 +11001405 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406}
1407
1408static void
1409mm_record_login(Session *s, struct passwd *pw)
1410{
1411 socklen_t fromlen;
1412 struct sockaddr_storage from;
1413
1414 /*
1415 * Get IP address of client. If the connection is not a socket, let
1416 * the address be 0.0.0.0.
1417 */
1418 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001419 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001422 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001424 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 }
1426 }
1427 /* Record that there was a login on that tty from the remote host. */
1428 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001429 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001430 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431}
1432
1433static void
1434mm_session_close(Session *s)
1435{
Damien Miller04bd8b02003-05-25 14:38:33 +10001436 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001437 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001438 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 session_pty_cleanup2(s);
1440 }
Damien Miller7207f642008-05-19 15:34:50 +10001441 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442}
1443
1444int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001445mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001447 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448 Session *s;
1449 int res, fd0;
1450
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452
1453 buffer_clear(m);
1454 s = session_new();
1455 if (s == NULL)
1456 goto error;
1457 s->authctxt = authctxt;
1458 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001459 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1461 if (res == 0)
1462 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463 pty_setowner(authctxt->pw, s->tty);
1464
1465 buffer_put_int(m, 1);
1466 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467
1468 /* We need to trick ttyslot */
1469 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001470 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
1472 mm_record_login(s, authctxt->pw);
1473
1474 /* Now we can close the file descriptor again */
1475 close(0);
1476
Darren Tucker09991742004-07-17 17:05:14 +10001477 /* send messages generated by record_login */
1478 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1479 buffer_clear(&loginmsg);
1480
1481 mm_request_send(sock, MONITOR_ANS_PTY, m);
1482
Damien Miller54fd7cf2007-09-17 12:04:08 +10001483 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1484 mm_send_fd(sock, s->ttyfd) == -1)
1485 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001486
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 /* make sure nothing uses fd 0 */
1488 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001489 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 /* slave is not needed */
1494 close(s->ttyfd);
1495 s->ttyfd = s->ptyfd;
1496 /* no need to dup() because nobody closes ptyfd */
1497 s->ptymaster = s->ptyfd;
1498
Damien Miller9ab00b42006-08-05 12:40:11 +10001499 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500
1501 return (0);
1502
1503 error:
1504 if (s != NULL)
1505 mm_session_close(s);
1506 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001507 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508 return (0);
1509}
1510
1511int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001512mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513{
1514 Session *s;
1515 char *tty;
1516
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001517 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518
1519 tty = buffer_get_string(m, NULL);
1520 if ((s = session_by_tty(tty)) != NULL)
1521 mm_session_close(s);
1522 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001523 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524 return (0);
1525}
1526
1527int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001528mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529{
1530 BIGNUM *p;
1531 int rsafail;
1532
1533 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001534 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535
1536 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001537 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538
1539 buffer_get_bignum2(m, p);
1540
1541 rsafail = ssh1_session_key(p);
1542
1543 buffer_clear(m);
1544 buffer_put_int(m, rsafail);
1545 buffer_put_bignum2(m, p);
1546
1547 BN_clear_free(p);
1548
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001549 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550
1551 /* Turn on permissions for sessid passing */
1552 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1553
1554 return (0);
1555}
1556
1557int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001558mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559{
1560 int i;
1561
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001562 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563
1564 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001565 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001566 for (i = 0; i < 16; i++)
1567 session_id[i] = buffer_get_char(m);
1568
1569 /* Turn on permissions for getpwnam */
1570 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1571
1572 return (0);
1573}
1574
1575int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001576mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001577{
1578 BIGNUM *client_n;
1579 Key *key = NULL;
1580 u_char *blob = NULL;
1581 u_int blen = 0;
1582 int allowed = 0;
1583
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001584 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585
Damien Miller7a8f5b32006-03-31 23:14:23 +11001586 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001587 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001588 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001589 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590 buffer_get_bignum2(m, client_n);
1591 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1592 BN_clear_free(client_n);
1593 }
1594 buffer_clear(m);
1595 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001596 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597
1598 /* clear temporarily storage (used by generate challenge) */
1599 monitor_reset_key_state();
1600
1601 if (allowed && key != NULL) {
1602 key->type = KEY_RSA; /* cheat for key_to_blob */
1603 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001604 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605 buffer_put_string(m, blob, blen);
1606
1607 /* Save temporarily for comparison in verify */
1608 key_blob = blob;
1609 key_bloblen = blen;
1610 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001611 }
Damien Miller00111382003-03-10 11:21:17 +11001612 if (key != NULL)
1613 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001615 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616
1617 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1618 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1619 return (0);
1620}
1621
1622int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001623mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624{
1625 Key *key = NULL;
1626 u_char *blob;
1627 u_int blen;
1628
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001629 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630
1631 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001632 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633 blob = buffer_get_string(m, &blen);
1634 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001635 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001636 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001637 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001639 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001640 if (key->type != KEY_RSA)
1641 fatal("%s: received bad key type %d", __func__, key->type);
1642 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643 if (ssh1_challenge)
1644 BN_clear_free(ssh1_challenge);
1645 ssh1_challenge = auth_rsa_generate_challenge(key);
1646
1647 buffer_clear(m);
1648 buffer_put_bignum2(m, ssh1_challenge);
1649
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001650 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001651 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652
1653 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001654
Darren Tuckera627d422013-06-02 07:31:17 +10001655 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001656 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657 return (0);
1658}
1659
1660int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001661mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001662{
1663 Key *key = NULL;
1664 u_char *blob, *response;
1665 u_int blen, len;
1666 int success;
1667
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669
1670 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001671 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001673 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674
1675 blob = buffer_get_string(m, &blen);
1676 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001677 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001679 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001681 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001682 response = buffer_get_string(m, &len);
1683 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001684 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1686
Darren Tuckera627d422013-06-02 07:31:17 +10001687 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001688 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001689 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690
1691 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1692
1693 /* reset state */
1694 BN_clear_free(ssh1_challenge);
1695 ssh1_challenge = NULL;
1696 monitor_reset_key_state();
1697
1698 buffer_clear(m);
1699 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001700 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701
1702 return (success);
1703}
1704
1705int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001706mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001708 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 int res, status;
1710
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001711 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712
1713 /* The child is terminating */
1714 session_destroy_all(&mm_session_close);
1715
Darren Tucker52358d62008-03-11 22:58:25 +11001716#ifdef USE_PAM
1717 if (options.use_pam)
1718 sshpam_cleanup();
1719#endif
1720
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001721 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001722 if (errno != EINTR)
1723 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724
1725 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1726
1727 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001728 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001729}
1730
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001731#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001732/* Report that an audit event occurred */
1733int
1734mm_answer_audit_event(int socket, Buffer *m)
1735{
1736 ssh_audit_event_t event;
1737
1738 debug3("%s entering", __func__);
1739
1740 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001741 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001742 case SSH_AUTH_FAIL_PUBKEY:
1743 case SSH_AUTH_FAIL_HOSTBASED:
1744 case SSH_AUTH_FAIL_GSSAPI:
1745 case SSH_LOGIN_EXCEED_MAXTRIES:
1746 case SSH_LOGIN_ROOT_DENIED:
1747 case SSH_CONNECTION_CLOSE:
1748 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001749 audit_event(event);
1750 break;
1751 default:
1752 fatal("Audit event type %d not permitted", event);
1753 }
1754
1755 return (0);
1756}
1757
1758int
1759mm_answer_audit_command(int socket, Buffer *m)
1760{
1761 u_int len;
1762 char *cmd;
1763
1764 debug3("%s entering", __func__);
1765 cmd = buffer_get_string(m, &len);
1766 /* sanity check command, if so how? */
1767 audit_run_command(cmd);
1768 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001769 return (0);
1770}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001771#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001772
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001774monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001775{
1776 if (compat20) {
1777 set_newkeys(MODE_IN);
1778 set_newkeys(MODE_OUT);
1779 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001780 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001781 packet_set_encryption_key(child_state.ssh1key,
1782 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001783 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 }
1785
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001786 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001787 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001788 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001789 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001790 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001791
1792 if (!compat20) {
1793 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001794 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001796 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001797 }
1798
1799 memcpy(&incoming_stream, &child_state.incoming,
1800 sizeof(incoming_stream));
1801 memcpy(&outgoing_stream, &child_state.outgoing,
1802 sizeof(outgoing_stream));
1803
1804 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001805 if (options.compression)
1806 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001807
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001808 if (options.rekey_limit || options.rekey_interval)
1809 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1810 (time_t)options.rekey_interval);
1811
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001812 /* Network I/O buffers */
1813 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001814 buffer_clear(packet_get_input());
1815 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001816 memset(child_state.input, 0, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001817 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818
Darren Tuckerf7288d72009-06-21 18:12:20 +10001819 buffer_clear(packet_get_output());
1820 buffer_append(packet_get_output(), child_state.output,
1821 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001822 memset(child_state.output, 0, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001823 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001824
1825 /* Roaming */
1826 if (compat20)
1827 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001828}
1829
1830static Kex *
1831mm_get_kex(Buffer *m)
1832{
1833 Kex *kex;
1834 void *blob;
1835 u_int bloblen;
1836
Damien Miller07d86be2006-03-26 14:19:21 +11001837 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001838 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001839 if (session_id2 == NULL ||
1840 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001841 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001842 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001843 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001844 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001845 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001846 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001847 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001848 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001849 kex->server = 1;
1850 kex->hostkey_type = buffer_get_int(m);
1851 kex->kex_type = buffer_get_int(m);
1852 blob = buffer_get_string(m, &bloblen);
1853 buffer_init(&kex->my);
1854 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001855 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001856 blob = buffer_get_string(m, &bloblen);
1857 buffer_init(&kex->peer);
1858 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001859 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001860 kex->done = 1;
1861 kex->flags = buffer_get_int(m);
1862 kex->client_version_string = buffer_get_string(m, NULL);
1863 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001864 kex->load_host_public_key=&get_hostkey_public_by_type;
1865 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001866 kex->host_key_index=&get_hostkey_index;
1867
1868 return (kex);
1869}
1870
1871/* This function requries careful sanity checking */
1872
1873void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001874mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875{
1876 Buffer m;
1877 u_char *blob, *p;
1878 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001879 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001880 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001881
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001882 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001883
1884 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001885 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001886 if (!compat20) {
1887 child_state.ssh1protoflags = buffer_get_int(&m);
1888 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001889 child_state.ssh1key = buffer_get_string(&m,
1890 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001891 child_state.ivout = buffer_get_string(&m,
1892 &child_state.ivoutlen);
1893 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1894 goto skip;
1895 } else {
1896 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001897 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001898 }
1899
1900 blob = buffer_get_string(&m, &bloblen);
1901 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001902 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001903
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001904 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001905 blob = buffer_get_string(&m, &bloblen);
1906 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001907 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908
1909 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001910 seqnr = buffer_get_int(&m);
1911 blocks = buffer_get_int64(&m);
1912 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001913 bytes = buffer_get_int64(&m);
1914 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001915 seqnr = buffer_get_int(&m);
1916 blocks = buffer_get_int64(&m);
1917 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001918 bytes = buffer_get_int64(&m);
1919 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001920
1921 skip:
1922 /* Get the key context */
1923 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1924 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1925
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001926 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001927 /* Get compression state */
1928 p = buffer_get_string(&m, &plen);
1929 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001930 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001931 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001932 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001933
1934 p = buffer_get_string(&m, &plen);
1935 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001936 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001938 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001939
1940 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001941 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001942 child_state.input = buffer_get_string(&m, &child_state.ilen);
1943 child_state.output = buffer_get_string(&m, &child_state.olen);
1944
Darren Tuckerc5564e12009-06-21 18:53:53 +10001945 /* Roaming */
1946 if (compat20) {
1947 child_state.sent_bytes = buffer_get_int64(&m);
1948 child_state.recv_bytes = buffer_get_int64(&m);
1949 }
1950
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001951 buffer_free(&m);
1952}
1953
1954
1955/* Allocation functions for zlib */
1956void *
1957mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1958{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001959 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001960 void *address;
1961
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001962 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001963 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1964
1965 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001966
1967 return (address);
1968}
1969
1970void
1971mm_zfree(struct mm_master *mm, void *address)
1972{
1973 mm_free(mm, address);
1974}
1975
1976void
1977mm_init_compression(struct mm_master *mm)
1978{
1979 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1980 outgoing_stream.zfree = (free_func)mm_zfree;
1981 outgoing_stream.opaque = mm;
1982
1983 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1984 incoming_stream.zfree = (free_func)mm_zfree;
1985 incoming_stream.opaque = mm;
1986}
1987
1988/* XXX */
1989
1990#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001991 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001992 fatal("fcntl(%d, F_SETFD)", x); \
1993} while (0)
1994
1995static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001996monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001997{
Damien Miller8f0bf232011-06-20 14:42:23 +10001998 int pair[2];
1999
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002000 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002001 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002002 FD_CLOSEONEXEC(pair[0]);
2003 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002004 mon->m_recvfd = pair[0];
2005 mon->m_sendfd = pair[1];
2006
2007 if (do_logfds) {
2008 if (pipe(pair) == -1)
2009 fatal("%s: pipe: %s", __func__, strerror(errno));
2010 FD_CLOSEONEXEC(pair[0]);
2011 FD_CLOSEONEXEC(pair[1]);
2012 mon->m_log_recvfd = pair[0];
2013 mon->m_log_sendfd = pair[1];
2014 } else
2015 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002016}
2017
2018#define MM_MEMSIZE 65536
2019
2020struct monitor *
2021monitor_init(void)
2022{
2023 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002024
Damien Miller07d86be2006-03-26 14:19:21 +11002025 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002026
Damien Miller8f0bf232011-06-20 14:42:23 +10002027 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002028
2029 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002030 if (options.compression) {
2031 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2032 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002033
Damien Miller2d6b8332002-06-21 15:59:49 +10002034 /* Compression needs to share state across borders */
2035 mm_init_compression(mon->m_zlib);
2036 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002037
2038 return mon;
2039}
2040
2041void
2042monitor_reinit(struct monitor *mon)
2043{
Damien Miller8f0bf232011-06-20 14:42:23 +10002044 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002045}
Darren Tucker0efd1552003-08-26 11:49:55 +10002046
2047#ifdef GSSAPI
2048int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002049mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002050{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002051 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002052 OM_uint32 major;
2053 u_int len;
2054
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002055 goid.elements = buffer_get_string(m, &len);
2056 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002057
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002058 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002059
Darren Tuckera627d422013-06-02 07:31:17 +10002060 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002061
2062 buffer_clear(m);
2063 buffer_put_int(m, major);
2064
Damien Miller6fd6def2005-11-05 15:07:05 +11002065 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002066
2067 /* Now we have a context, enable the step */
2068 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2069
2070 return (0);
2071}
2072
2073int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002074mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002075{
2076 gss_buffer_desc in;
2077 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002078 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002079 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002080 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002081
Darren Tucker600ad8d2003-08-26 12:10:48 +10002082 in.value = buffer_get_string(m, &len);
2083 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002084 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002085 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002086
2087 buffer_clear(m);
2088 buffer_put_int(m, major);
2089 buffer_put_string(m, out.value, out.length);
2090 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002091 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002092
2093 gss_release_buffer(&minor, &out);
2094
Damien Miller6fd6def2005-11-05 15:07:05 +11002095 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002096 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2097 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002098 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002099 }
2100 return (0);
2101}
2102
2103int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002104mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002105{
2106 gss_buffer_desc gssbuf, mic;
2107 OM_uint32 ret;
2108 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002109
Damien Miller0425d402003-11-17 22:18:21 +11002110 gssbuf.value = buffer_get_string(m, &len);
2111 gssbuf.length = len;
2112 mic.value = buffer_get_string(m, &len);
2113 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002114
Damien Miller0425d402003-11-17 22:18:21 +11002115 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002116
Darren Tuckera627d422013-06-02 07:31:17 +10002117 free(gssbuf.value);
2118 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002119
Damien Miller0425d402003-11-17 22:18:21 +11002120 buffer_clear(m);
2121 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002122
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002123 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002124
Damien Miller0425d402003-11-17 22:18:21 +11002125 if (!GSS_ERROR(ret))
2126 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002127
Damien Miller0425d402003-11-17 22:18:21 +11002128 return (0);
2129}
2130
2131int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002132mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002133{
2134 int authenticated;
2135
2136 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2137
2138 buffer_clear(m);
2139 buffer_put_int(m, authenticated);
2140
2141 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002142 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002143
Damien Miller6fd6def2005-11-05 15:07:05 +11002144 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002145
2146 /* Monitor loop will terminate if authenticated */
2147 return (authenticated);
2148}
2149#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002150
2151#ifdef JPAKE
2152int
2153mm_answer_jpake_step1(int sock, Buffer *m)
2154{
2155 struct jpake_ctx *pctx;
2156 u_char *x3_proof, *x4_proof;
2157 u_int x3_proof_len, x4_proof_len;
2158
2159 if (!options.zero_knowledge_password_authentication)
2160 fatal("zero_knowledge_password_authentication disabled");
2161
2162 if (authctxt->jpake_ctx != NULL)
2163 fatal("%s: authctxt->jpake_ctx already set (%p)",
2164 __func__, authctxt->jpake_ctx);
2165 authctxt->jpake_ctx = pctx = jpake_new();
2166
2167 jpake_step1(pctx->grp,
2168 &pctx->server_id, &pctx->server_id_len,
2169 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2170 &x3_proof, &x3_proof_len,
2171 &x4_proof, &x4_proof_len);
2172
2173 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2174
2175 buffer_clear(m);
2176
2177 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2178 buffer_put_bignum2(m, pctx->g_x3);
2179 buffer_put_bignum2(m, pctx->g_x4);
2180 buffer_put_string(m, x3_proof, x3_proof_len);
2181 buffer_put_string(m, x4_proof, x4_proof_len);
2182
2183 debug3("%s: sending step1", __func__);
2184 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2185
2186 bzero(x3_proof, x3_proof_len);
2187 bzero(x4_proof, x4_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002188 free(x3_proof);
2189 free(x4_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002190
2191 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2192 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2193
2194 return 0;
2195}
2196
2197int
2198mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2199{
2200 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2201 char *hash_scheme, *salt;
2202
2203 if (pctx == NULL)
2204 fatal("%s: pctx == NULL", __func__);
2205
2206 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2207
2208 buffer_clear(m);
2209 /* pctx->s is sensitive, not returned to slave */
2210 buffer_put_cstring(m, hash_scheme);
2211 buffer_put_cstring(m, salt);
2212
2213 debug3("%s: sending pwdata", __func__);
2214 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2215
2216 bzero(hash_scheme, strlen(hash_scheme));
2217 bzero(salt, strlen(salt));
Darren Tuckera627d422013-06-02 07:31:17 +10002218 free(hash_scheme);
2219 free(salt);
Damien Miller01ed2272008-11-05 16:20:46 +11002220
2221 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2222
2223 return 0;
2224}
2225
2226int
2227mm_answer_jpake_step2(int sock, Buffer *m)
2228{
2229 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2230 u_char *x1_proof, *x2_proof, *x4_s_proof;
2231 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2232
2233 if (pctx == NULL)
2234 fatal("%s: pctx == NULL", __func__);
2235
2236 if ((pctx->g_x1 = BN_new()) == NULL ||
2237 (pctx->g_x2 = BN_new()) == NULL)
2238 fatal("%s: BN_new", __func__);
2239 buffer_get_bignum2(m, pctx->g_x1);
2240 buffer_get_bignum2(m, pctx->g_x2);
2241 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2242 x1_proof = buffer_get_string(m, &x1_proof_len);
2243 x2_proof = buffer_get_string(m, &x2_proof_len);
2244
2245 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2246 pctx->g_x1, pctx->g_x2, pctx->x4,
2247 pctx->client_id, pctx->client_id_len,
2248 pctx->server_id, pctx->server_id_len,
2249 x1_proof, x1_proof_len,
2250 x2_proof, x2_proof_len,
2251 &pctx->b,
2252 &x4_s_proof, &x4_s_proof_len);
2253
2254 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2255
2256 bzero(x1_proof, x1_proof_len);
2257 bzero(x2_proof, x2_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002258 free(x1_proof);
2259 free(x2_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002260
2261 buffer_clear(m);
2262
2263 buffer_put_bignum2(m, pctx->b);
2264 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2265
2266 debug3("%s: sending step2", __func__);
2267 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2268
2269 bzero(x4_s_proof, x4_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002270 free(x4_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002271
2272 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2273
2274 return 0;
2275}
2276
2277int
2278mm_answer_jpake_key_confirm(int sock, Buffer *m)
2279{
2280 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2281 u_char *x2_s_proof;
2282 u_int x2_s_proof_len;
2283
2284 if (pctx == NULL)
2285 fatal("%s: pctx == NULL", __func__);
2286
2287 if ((pctx->a = BN_new()) == NULL)
2288 fatal("%s: BN_new", __func__);
2289 buffer_get_bignum2(m, pctx->a);
2290 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2291
2292 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2293 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2294 pctx->server_id, pctx->server_id_len,
2295 pctx->client_id, pctx->client_id_len,
2296 session_id2, session_id2_len,
2297 x2_s_proof, x2_s_proof_len,
2298 &pctx->k,
2299 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2300
2301 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2302
2303 bzero(x2_s_proof, x2_s_proof_len);
2304 buffer_clear(m);
2305
2306 /* pctx->k is sensitive, not sent */
2307 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2308
2309 debug3("%s: sending confirmation hash", __func__);
2310 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2311
2312 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2313
2314 return 0;
2315}
2316
2317int
2318mm_answer_jpake_check_confirm(int sock, Buffer *m)
2319{
2320 int authenticated = 0;
2321 u_char *peer_confirm_hash;
2322 u_int peer_confirm_hash_len;
2323 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2324
2325 if (pctx == NULL)
2326 fatal("%s: pctx == NULL", __func__);
2327
2328 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2329
2330 authenticated = jpake_check_confirm(pctx->k,
2331 pctx->client_id, pctx->client_id_len,
2332 session_id2, session_id2_len,
2333 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2334
2335 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2336
2337 bzero(peer_confirm_hash, peer_confirm_hash_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002338 free(peer_confirm_hash);
Damien Miller01ed2272008-11-05 16:20:46 +11002339
2340 buffer_clear(m);
2341 buffer_put_int(m, authenticated);
2342
2343 debug3("%s: sending result %d", __func__, authenticated);
2344 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2345
2346 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2347
2348 auth_method = "jpake-01@openssh.com";
2349 return authenticated;
2350}
2351
2352#endif /* JPAKE */