blob: 44dff98c93c0da8f1b601bcf6c4866386701f8da [file] [log] [blame]
Damien Miller85b45e02013-07-20 13:21:52 +10001/* $OpenBSD: monitor.c,v 1.127 2013/07/19 07:37:48 markus Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100047#ifdef HAVE_POLL_H
48#include <poll.h>
49#else
50# ifdef HAVE_SYS_POLL_H
51# include <sys/poll.h>
52# endif
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054
55#ifdef SKEY
56#include <skey.h>
57#endif
58
Damien 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"
Damien Miller85b45e02013-07-20 13:21:52 +1000100#include "authfd.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101
Darren Tucker0efd1552003-08-26 11:49:55 +1000102#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000103static Gssctxt *gsscontext = NULL;
104#endif
105
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000106/* Imports */
107extern ServerOptions options;
108extern u_int utmp_len;
109extern Newkeys *current_keys[];
110extern z_stream incoming_stream;
111extern z_stream outgoing_stream;
112extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113extern Buffer auth_debug;
114extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000115extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116
117/* State exported from the child */
118
119struct {
120 z_stream incoming;
121 z_stream outgoing;
122 u_char *keyin;
123 u_int keyinlen;
124 u_char *keyout;
125 u_int keyoutlen;
126 u_char *ivin;
127 u_int ivinlen;
128 u_char *ivout;
129 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000130 u_char *ssh1key;
131 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132 int ssh1cipher;
133 int ssh1protoflags;
134 u_char *input;
135 u_int ilen;
136 u_char *output;
137 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000138 u_int64_t sent_bytes;
139 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000140} child_state;
141
Damien Miller469954d2003-06-18 20:25:33 +1000142/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143
144int mm_answer_moduli(int, Buffer *);
145int mm_answer_sign(int, Buffer *);
146int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000147int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000148int mm_answer_authserv(int, Buffer *);
149int mm_answer_authpassword(int, Buffer *);
150int mm_answer_bsdauthquery(int, Buffer *);
151int mm_answer_bsdauthrespond(int, Buffer *);
152int mm_answer_skeyquery(int, Buffer *);
153int mm_answer_skeyrespond(int, Buffer *);
154int mm_answer_keyallowed(int, Buffer *);
155int mm_answer_keyverify(int, Buffer *);
156int mm_answer_pty(int, Buffer *);
157int mm_answer_pty_cleanup(int, Buffer *);
158int mm_answer_term(int, Buffer *);
159int mm_answer_rsa_keyallowed(int, Buffer *);
160int mm_answer_rsa_challenge(int, Buffer *);
161int mm_answer_rsa_response(int, Buffer *);
162int mm_answer_sesskey(int, Buffer *);
163int mm_answer_sessid(int, Buffer *);
Damien Miller01ed2272008-11-05 16:20:46 +1100164int mm_answer_jpake_get_pwdata(int, Buffer *);
165int mm_answer_jpake_step1(int, Buffer *);
166int mm_answer_jpake_step2(int, Buffer *);
167int mm_answer_jpake_key_confirm(int, Buffer *);
168int mm_answer_jpake_check_confirm(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000169
Damien Miller79418552002-04-23 20:28:48 +1000170#ifdef USE_PAM
171int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000172int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000173int mm_answer_pam_init_ctx(int, Buffer *);
174int mm_answer_pam_query(int, Buffer *);
175int mm_answer_pam_respond(int, Buffer *);
176int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000177#endif
178
Darren Tucker0efd1552003-08-26 11:49:55 +1000179#ifdef GSSAPI
180int mm_answer_gss_setup_ctx(int, Buffer *);
181int mm_answer_gss_accept_ctx(int, Buffer *);
182int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100183int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000184#endif
Damien Miller25162f22002-09-12 09:47:29 +1000185
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100186#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100187int mm_answer_audit_event(int, Buffer *);
188int mm_answer_audit_command(int, Buffer *);
189#endif
190
Damien Miller8f0bf232011-06-20 14:42:23 +1000191static int monitor_read_log(struct monitor *);
192
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000193static Authctxt *authctxt;
194static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
195
196/* local state for key verify */
197static u_char *key_blob = NULL;
198static u_int key_bloblen = 0;
199static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000200static char *hostbased_cuser = NULL;
201static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202static char *auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100203static char *auth_submethod = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +1000204static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000205static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000206static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000207
208struct mon_table {
209 enum monitor_reqtype type;
210 int flags;
211 int (*f)(int, Buffer *);
212};
213
214#define MON_ISAUTH 0x0004 /* Required for Authentication */
215#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
216#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100217#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218
219#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
220
221#define MON_PERMIT 0x1000 /* Request is permitted */
222
223struct mon_table mon_dispatch_proto20[] = {
224 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
225 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
226 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
227 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000228 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000230#ifdef USE_PAM
231 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000232 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000233 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
234 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
235 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
236 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000237#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100238#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100239 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100240#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000241#ifdef BSD_AUTH
242 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100243 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244#endif
245#ifdef SKEY
246 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
247 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
248#endif
249 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
250 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000251#ifdef GSSAPI
252 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
253 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
254 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100255 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000256#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100257#ifdef JPAKE
258 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
259 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
260 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
261 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
262 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
263#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000264 {0, 0, NULL}
265};
266
267struct mon_table mon_dispatch_postauth20[] = {
268 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
269 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
270 {MONITOR_REQ_PTY, 0, mm_answer_pty},
271 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
272 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100273#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100274 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
275 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
276#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000277 {0, 0, NULL}
278};
279
280struct mon_table mon_dispatch_proto15[] = {
281 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
282 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
283 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
284 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100285 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
286 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000287 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
288 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
289#ifdef BSD_AUTH
290 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100291 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292#endif
293#ifdef SKEY
294 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
295 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
296#endif
Damien Millera33501b2002-05-08 12:24:42 +1000297#ifdef USE_PAM
298 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000299 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000300 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
301 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
302 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
303 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000304#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100305#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100306 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100307#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000308 {0, 0, NULL}
309};
310
311struct mon_table mon_dispatch_postauth15[] = {
312 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
313 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
314 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100315#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100316 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000317 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100318#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000319 {0, 0, NULL}
320};
321
322struct mon_table *mon_dispatch;
323
324/* Specifies if a certain message is allowed at the moment */
325
326static void
327monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
328{
329 while (ent->f != NULL) {
330 if (ent->type == type) {
331 ent->flags &= ~MON_PERMIT;
332 ent->flags |= permit ? MON_PERMIT : 0;
333 return;
334 }
335 ent++;
336 }
337}
338
339static void
340monitor_permit_authentications(int permit)
341{
342 struct mon_table *ent = mon_dispatch;
343
344 while (ent->f != NULL) {
345 if (ent->flags & MON_AUTH) {
346 ent->flags &= ~MON_PERMIT;
347 ent->flags |= permit ? MON_PERMIT : 0;
348 }
349 ent++;
350 }
351}
352
Darren Tucker3e33cec2003-10-02 16:12:36 +1000353void
354monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355{
356 struct mon_table *ent;
Damien Miller15b05cf2012-12-03 09:53:20 +1100357 int authenticated = 0, partial = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000358
359 debug3("preauth child monitor started");
360
Damien Miller8f0bf232011-06-20 14:42:23 +1000361 close(pmonitor->m_recvfd);
362 close(pmonitor->m_log_sendfd);
363 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
364
Darren Tucker3e33cec2003-10-02 16:12:36 +1000365 authctxt = _authctxt;
366 memset(authctxt, 0, sizeof(*authctxt));
367
Darren Tuckerde0de392005-03-31 23:52:04 +1000368 authctxt->loginmsg = &loginmsg;
369
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 if (compat20) {
371 mon_dispatch = mon_dispatch_proto20;
372
373 /* Permit requests for moduli and signatures */
374 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
375 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
376 } else {
377 mon_dispatch = mon_dispatch_proto15;
378
379 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
380 }
381
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382 /* The first few requests do not require asynchronous access */
383 while (!authenticated) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100384 partial = 0;
Damien Miller7a8f5b32006-03-31 23:14:23 +1100385 auth_method = "unknown";
Damien Miller15b05cf2012-12-03 09:53:20 +1100386 auth_submethod = NULL;
Darren Tuckerfbba7352006-11-07 23:16:08 +1100387 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Damien Millera6e3f012012-11-04 23:21:40 +1100388
389 /* Special handling for multiple required authentications */
390 if (options.num_auth_methods != 0) {
391 if (!compat20)
392 fatal("AuthenticationMethods is not supported"
393 "with SSH protocol 1");
394 if (authenticated &&
395 !auth2_update_methods_lists(authctxt,
Damien Miller91a55f22013-04-23 15:18:10 +1000396 auth_method, auth_submethod)) {
Damien Millera6e3f012012-11-04 23:21:40 +1100397 debug3("%s: method %s: partial", __func__,
398 auth_method);
399 authenticated = 0;
Damien Miller15b05cf2012-12-03 09:53:20 +1100400 partial = 1;
Damien Millera6e3f012012-11-04 23:21:40 +1100401 }
402 }
403
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404 if (authenticated) {
405 if (!(ent->flags & MON_AUTHDECIDE))
406 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000407 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000408 if (authctxt->pw->pw_uid == 0 &&
409 !auth_root_allowed(auth_method))
410 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000411#ifdef USE_PAM
412 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100413 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000414 Buffer m;
415
416 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100417 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000418 MONITOR_REQ_PAM_ACCOUNT, &m);
419 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
420 buffer_free(&m);
421 }
422#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000423 }
Damien Miller7a8f5b32006-03-31 23:14:23 +1100424 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Damien Miller15b05cf2012-12-03 09:53:20 +1100425 auth_log(authctxt, authenticated, partial,
Darren Tucker0acca372013-06-02 07:41:51 +1000426 auth_method, auth_submethod);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427 if (!authenticated)
428 authctxt->failures++;
429 }
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
Damien Miller85b45e02013-07-20 13:21:52 +1000690extern AuthenticationConnection *auth_conn;
691
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000693mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694{
695 Key *key;
696 u_char *p;
697 u_char *signature;
698 u_int siglen, datlen;
699 int keyid;
700
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000702
703 keyid = buffer_get_int(m);
704 p = buffer_get_string(m, &datlen);
705
Damien Millera63128d2006-03-15 12:08:28 +1100706 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000707 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
708 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100709 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000710 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000711 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712
Ben Lindstromf67e0772002-06-06 20:58:19 +0000713 /* save session id, it will be passed on the first call */
714 if (session_id2_len == 0) {
715 session_id2_len = datlen;
716 session_id2 = xmalloc(session_id2_len);
717 memcpy(session_id2, p, session_id2_len);
718 }
719
Damien Miller85b45e02013-07-20 13:21:52 +1000720 if ((key = get_hostkey_by_index(keyid)) != NULL) {
721 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
722 fatal("%s: key_sign failed", __func__);
723 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
724 auth_conn != NULL) {
725 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
726 datlen) < 0)
727 fatal("%s: ssh_agent_sign failed", __func__);
728 } else
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000729 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
Ben Lindstromd5502182002-06-27 00:12:57 +0000731 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
733 buffer_clear(m);
734 buffer_put_string(m, signature, siglen);
735
Darren Tuckera627d422013-06-02 07:31:17 +1000736 free(p);
737 free(signature);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000739 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
741 /* Turn on permissions for getpwnam */
742 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
743
744 return (0);
745}
746
747/* Retrieves the password entry and also checks if the user is permitted */
748
749int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000750mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000751{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000752 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 struct passwd *pwent;
754 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000755 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000757 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000758
759 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
Darren Tuckerb09b6772004-06-22 15:06:46 +1000762 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763
Darren Tuckerb09b6772004-06-22 15:06:46 +1000764 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765
Darren Tuckerb09b6772004-06-22 15:06:46 +1000766 authctxt->user = xstrdup(username);
767 setproctitle("%s [priv]", pwent ? username : "unknown");
Darren Tuckera627d422013-06-02 07:31:17 +1000768 free(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769
770 buffer_clear(m);
771
772 if (pwent == NULL) {
773 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100774 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775 goto out;
776 }
777
778 allowed = 1;
779 authctxt->pw = pwent;
780 authctxt->valid = 1;
781
782 buffer_put_char(m, 1);
783 buffer_put_string(m, pwent, sizeof(struct passwd));
784 buffer_put_cstring(m, pwent->pw_name);
785 buffer_put_cstring(m, "*");
Damien Miller6332da22013-04-23 14:25:52 +1000786#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787 buffer_put_cstring(m, pwent->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000788#endif
789#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000791#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792 buffer_put_cstring(m, pwent->pw_dir);
793 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100794
795 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100796 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000797
798#define M_CP_STROPT(x) do { \
799 if (options.x != NULL) \
800 buffer_put_cstring(m, options.x); \
801 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000802#define M_CP_STRARRAYOPT(x, nx) do { \
803 for (i = 0; i < options.nx; i++) \
804 buffer_put_cstring(m, options.x[i]); \
805 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000806 /* See comment in servconf.h */
807 COPY_MATCH_STRING_OPTS();
808#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000809#undef M_CP_STRARRAYOPT
Damien Millera6e3f012012-11-04 23:21:40 +1100810
811 /* Create valid auth method lists */
812 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
813 /*
814 * The monitor will continue long enough to let the child
815 * run to it's packet_disconnect(), but it must not allow any
816 * authentication to succeed.
817 */
818 debug("%s: no valid authentication method lists", __func__);
819 }
820
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000821 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000822 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823
824 /* For SSHv1 allow authentication now */
825 if (!compat20)
826 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000827 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000828 /* Allow service/style information on the auth context */
829 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000830 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
831 }
Damien Millera33501b2002-05-08 12:24:42 +1000832#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000833 if (options.use_pam)
834 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000835#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836
837 return (0);
838}
839
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000840int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000841{
842 char *banner;
843
844 buffer_clear(m);
845 banner = auth2_read_banner();
846 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000847 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Darren Tuckera627d422013-06-02 07:31:17 +1000848 free(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000849
850 return (0);
851}
852
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000853int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000854mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855{
856 monitor_permit_authentications(1);
857
858 authctxt->service = buffer_get_string(m, NULL);
859 authctxt->style = buffer_get_string(m, NULL);
860 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000861 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862
863 if (strlen(authctxt->style) == 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000864 free(authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000865 authctxt->style = NULL;
866 }
867
868 return (0);
869}
870
871int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000872mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873{
874 static int call_count;
875 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000876 int authenticated;
877 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000878
879 passwd = buffer_get_string(m, &plen);
880 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000881 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000882 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883 memset(passwd, 0, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000884 free(passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000885
886 buffer_clear(m);
887 buffer_put_int(m, authenticated);
888
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000889 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000890 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000891
892 call_count++;
893 if (plen == 0 && call_count == 1)
894 auth_method = "none";
895 else
896 auth_method = "password";
897
898 /* Causes monitor loop to terminate if authenticated */
899 return (authenticated);
900}
901
902#ifdef BSD_AUTH
903int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000904mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905{
906 char *name, *infotxt;
907 u_int numprompts;
908 u_int *echo_on;
909 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100910 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000911
Damien Millerb7df3af2003-02-24 11:55:46 +1100912 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
913 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
915 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100916 buffer_put_int(m, success);
917 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000918 buffer_put_cstring(m, prompts[0]);
919
Damien Millerb7df3af2003-02-24 11:55:46 +1100920 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000921 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922
Damien Millerb7df3af2003-02-24 11:55:46 +1100923 if (success) {
Darren Tuckera627d422013-06-02 07:31:17 +1000924 free(name);
925 free(infotxt);
926 free(prompts);
927 free(echo_on);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000928 }
929
930 return (0);
931}
932
933int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000934mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000935{
936 char *response;
937 int authok;
938
939 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000940 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
942 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000943 authok = options.challenge_response_authentication &&
944 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000945 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000946 debug3("%s: <%s> = <%d>", __func__, response, authok);
Darren Tuckera627d422013-06-02 07:31:17 +1000947 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948
949 buffer_clear(m);
950 buffer_put_int(m, authok);
951
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000953 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
Damien Miller91a55f22013-04-23 15:18:10 +1000955 if (compat20) {
956 auth_method = "keyboard-interactive";
957 auth_submethod = "bsdauth";
958 } else
Damien Millera6e3f012012-11-04 23:21:40 +1100959 auth_method = "bsdauth";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960
961 return (authok != 0);
962}
963#endif
964
965#ifdef SKEY
966int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000967mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968{
969 struct skey skey;
970 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100971 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000973 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
974 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
976 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100977 buffer_put_int(m, success);
978 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979 buffer_put_cstring(m, challenge);
980
Damien Millerb7df3af2003-02-24 11:55:46 +1100981 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000982 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983
984 return (0);
985}
986
987int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000988mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989{
990 char *response;
991 int authok;
992
993 response = buffer_get_string(m, NULL);
994
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000995 authok = (options.challenge_response_authentication &&
996 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 skey_haskey(authctxt->pw->pw_name) == 0 &&
998 skey_passcheck(authctxt->pw->pw_name, response) != -1);
999
Darren Tuckerf60845f2013-06-02 08:07:31 +10001000 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001001
1002 buffer_clear(m);
1003 buffer_put_int(m, authok);
1004
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001005 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001006 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007
1008 auth_method = "skey";
1009
1010 return (authok != 0);
1011}
1012#endif
1013
Damien Miller79418552002-04-23 20:28:48 +10001014#ifdef USE_PAM
1015int
Darren Tucker7eda5922004-06-22 13:22:50 +10001016mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +10001017{
Damien Miller4e448a32003-05-14 15:11:48 +10001018 if (!options.use_pam)
1019 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1020
Darren Tuckerdbf7a742004-03-08 23:04:06 +11001021 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +10001022
Damien Miller1f499fd2003-08-25 13:08:49 +10001023 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1024
Damien Miller79418552002-04-23 20:28:48 +10001025 return (0);
1026}
Damien Miller4f9f42a2003-05-10 19:28:02 +10001027
Damien Miller1f499fd2003-08-25 13:08:49 +10001028int
Darren Tucker7eda5922004-06-22 13:22:50 +10001029mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +10001030{
1031 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +11001032
Damien Miller1f499fd2003-08-25 13:08:49 +10001033 if (!options.use_pam)
1034 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1035
1036 ret = do_pam_account();
1037
1038 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001039 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001040
Darren Tucker7eda5922004-06-22 13:22:50 +10001041 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001042
1043 return (ret);
1044}
1045
Damien Miller4f9f42a2003-05-10 19:28:02 +10001046static void *sshpam_ctxt, *sshpam_authok;
1047extern KbdintDevice sshpam_device;
1048
1049int
Darren Tucker7eda5922004-06-22 13:22:50 +10001050mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001051{
1052
1053 debug3("%s", __func__);
1054 authctxt->user = buffer_get_string(m, NULL);
1055 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1056 sshpam_authok = NULL;
1057 buffer_clear(m);
1058 if (sshpam_ctxt != NULL) {
1059 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1060 buffer_put_int(m, 1);
1061 } else {
1062 buffer_put_int(m, 0);
1063 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001064 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001065 return (0);
1066}
1067
1068int
Darren Tucker7eda5922004-06-22 13:22:50 +10001069mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001070{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001071 char *name = NULL, *info = NULL, **prompts = NULL;
1072 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001073 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001074
1075 debug3("%s", __func__);
1076 sshpam_authok = NULL;
1077 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1078 if (ret == 0 && num == 0)
1079 sshpam_authok = sshpam_ctxt;
1080 if (num > 1 || name == NULL || info == NULL)
1081 ret = -1;
1082 buffer_clear(m);
1083 buffer_put_int(m, ret);
1084 buffer_put_cstring(m, name);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001085 free(name);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001086 buffer_put_cstring(m, info);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001087 free(info);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001088 buffer_put_int(m, num);
1089 for (i = 0; i < num; ++i) {
1090 buffer_put_cstring(m, prompts[i]);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001091 free(prompts[i]);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001092 buffer_put_int(m, echo_on[i]);
1093 }
Darren Tuckerf60845f2013-06-02 08:07:31 +10001094 free(prompts);
1095 free(echo_on);
Damien Miller15b05cf2012-12-03 09:53:20 +11001096 auth_method = "keyboard-interactive";
1097 auth_submethod = "pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001098 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001099 return (0);
1100}
1101
1102int
Darren Tucker7eda5922004-06-22 13:22:50 +10001103mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001104{
1105 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001106 u_int i, num;
1107 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001108
1109 debug3("%s", __func__);
1110 sshpam_authok = NULL;
1111 num = buffer_get_int(m);
1112 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001113 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001114 for (i = 0; i < num; ++i)
1115 resp[i] = buffer_get_string(m, NULL);
1116 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1117 for (i = 0; i < num; ++i)
Darren Tuckerf60845f2013-06-02 08:07:31 +10001118 free(resp[i]);
1119 free(resp);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001120 } else {
1121 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1122 }
1123 buffer_clear(m);
1124 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001125 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001126 auth_method = "keyboard-interactive";
1127 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001128 if (ret == 0)
1129 sshpam_authok = sshpam_ctxt;
1130 return (0);
1131}
1132
1133int
Darren Tucker7eda5922004-06-22 13:22:50 +10001134mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001135{
1136
1137 debug3("%s", __func__);
1138 (sshpam_device.free_ctx)(sshpam_ctxt);
1139 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001140 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller15b05cf2012-12-03 09:53:20 +11001141 auth_method = "keyboard-interactive";
1142 auth_submethod = "pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001143 return (sshpam_authok == sshpam_ctxt);
1144}
Damien Miller79418552002-04-23 20:28:48 +10001145#endif
1146
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001148mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001149{
1150 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001151 char *cuser, *chost;
1152 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001153 u_int bloblen;
1154 enum mm_keytype type = 0;
1155 int allowed = 0;
1156
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001157 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001158
1159 type = buffer_get_int(m);
1160 cuser = buffer_get_string(m, NULL);
1161 chost = buffer_get_string(m, NULL);
1162 blob = buffer_get_string(m, &bloblen);
1163
1164 key = key_from_blob(blob, bloblen);
1165
1166 if ((compat20 && type == MM_RSAHOSTKEY) ||
1167 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001168 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001170 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001171
Damien Miller3e3b5142003-11-17 21:13:40 +11001172 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001173 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001175 allowed = options.pubkey_authentication &&
1176 user_key_allowed(authctxt->pw, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001177 pubkey_auth_info(authctxt, key, NULL);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001178 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001179 if (options.pubkey_authentication && allowed != 1)
1180 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001181 break;
1182 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001183 allowed = options.hostbased_authentication &&
1184 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 cuser, chost, key);
Damien Miller20bdcd72013-07-18 16:10:09 +10001186 pubkey_auth_info(authctxt, key,
1187 "client user \"%.100s\", client host \"%.100s\"",
1188 cuser, chost);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001189 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190 break;
1191 case MM_RSAHOSTKEY:
1192 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001193 allowed = options.rhosts_rsa_authentication &&
1194 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001196 if (options.rhosts_rsa_authentication && allowed != 1)
1197 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001198 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199 break;
1200 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 break;
1203 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204 }
Damien Miller00111382003-03-10 11:21:17 +11001205 if (key != NULL)
1206 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207
1208 /* clear temporarily storage (used by verify) */
1209 monitor_reset_key_state();
1210
1211 if (allowed) {
1212 /* Save temporarily for comparison in verify */
1213 key_blob = blob;
1214 key_bloblen = bloblen;
1215 key_blobtype = type;
1216 hostbased_cuser = cuser;
1217 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001218 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001219 /* Log failed attempt */
Darren Tucker0acca372013-06-02 07:41:51 +10001220 auth_log(authctxt, 0, 0, auth_method, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10001221 free(blob);
1222 free(cuser);
1223 free(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001224 }
1225
1226 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001227 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228
1229 buffer_clear(m);
1230 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001231 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001232
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001233 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234
1235 if (type == MM_RSAHOSTKEY)
1236 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1237
1238 return (0);
1239}
1240
1241static int
1242monitor_valid_userblob(u_char *data, u_int datalen)
1243{
1244 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001245 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 u_int len;
1247 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248
1249 buffer_init(&b);
1250 buffer_append(&b, data, datalen);
1251
1252 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001253 p = buffer_ptr(&b);
1254 len = buffer_len(&b);
1255 if ((session_id2 == NULL) ||
1256 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001257 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001258 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 buffer_consume(&b, session_id2_len);
1260 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001261 p = buffer_get_string(&b, &len);
1262 if ((session_id2 == NULL) ||
1263 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001264 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001267 }
1268 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1269 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001270 p = buffer_get_cstring(&b, NULL);
1271 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1272 authctxt->style ? ":" : "",
1273 authctxt->style ? authctxt->style : "");
1274 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001275 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001276 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 fail++;
1278 }
Darren Tuckera627d422013-06-02 07:31:17 +10001279 free(userstyle);
1280 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281 buffer_skip_string(&b);
1282 if (datafellows & SSH_BUG_PKAUTH) {
1283 if (!buffer_get_char(&b))
1284 fail++;
1285 } else {
Damien Miller4ce189d2013-04-23 15:17:52 +10001286 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 if (strcmp("publickey", p) != 0)
1288 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001289 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290 if (!buffer_get_char(&b))
1291 fail++;
1292 buffer_skip_string(&b);
1293 }
1294 buffer_skip_string(&b);
1295 if (buffer_len(&b) != 0)
1296 fail++;
1297 buffer_free(&b);
1298 return (fail == 0);
1299}
1300
1301static int
Damien Millera10f5612002-09-12 09:49:15 +10001302monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1303 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304{
1305 Buffer b;
Damien Miller4ce189d2013-04-23 15:17:52 +10001306 char *p, *userstyle;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 u_int len;
1308 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309
1310 buffer_init(&b);
1311 buffer_append(&b, data, datalen);
1312
Ben Lindstromf67e0772002-06-06 20:58:19 +00001313 p = buffer_get_string(&b, &len);
1314 if ((session_id2 == NULL) ||
1315 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001316 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001318 free(p);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001319
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1321 fail++;
Damien Miller4ce189d2013-04-23 15:17:52 +10001322 p = buffer_get_cstring(&b, NULL);
1323 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1324 authctxt->style ? ":" : "",
1325 authctxt->style ? authctxt->style : "");
1326 if (strcmp(userstyle, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001327 logit("wrong user name passed to monitor: expected %s != %.100s",
Damien Miller4ce189d2013-04-23 15:17:52 +10001328 userstyle, p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329 fail++;
1330 }
Damien Miller4ce189d2013-04-23 15:17:52 +10001331 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +10001332 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333 buffer_skip_string(&b); /* service */
Damien Miller4ce189d2013-04-23 15:17:52 +10001334 p = buffer_get_cstring(&b, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335 if (strcmp(p, "hostbased") != 0)
1336 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001337 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338 buffer_skip_string(&b); /* pkalg */
1339 buffer_skip_string(&b); /* pkblob */
1340
1341 /* verify client host, strip trailing dot if necessary */
1342 p = buffer_get_string(&b, NULL);
1343 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1344 p[len - 1] = '\0';
1345 if (strcmp(p, chost) != 0)
1346 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001347 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
1349 /* verify client user */
1350 p = buffer_get_string(&b, NULL);
1351 if (strcmp(p, cuser) != 0)
1352 fail++;
Darren Tuckera627d422013-06-02 07:31:17 +10001353 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
1355 if (buffer_len(&b) != 0)
1356 fail++;
1357 buffer_free(&b);
1358 return (fail == 0);
1359}
1360
1361int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001362mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363{
1364 Key *key;
1365 u_char *signature, *data, *blob;
1366 u_int signaturelen, datalen, bloblen;
1367 int verified = 0;
1368 int valid_data = 0;
1369
1370 blob = buffer_get_string(m, &bloblen);
1371 signature = buffer_get_string(m, &signaturelen);
1372 data = buffer_get_string(m, &datalen);
1373
1374 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001375 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001376 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377
1378 key = key_from_blob(blob, bloblen);
1379 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381
1382 switch (key_blobtype) {
1383 case MM_USERKEY:
1384 valid_data = monitor_valid_userblob(data, datalen);
1385 break;
1386 case MM_HOSTKEY:
1387 valid_data = monitor_valid_hostbasedblob(data, datalen,
1388 hostbased_cuser, hostbased_chost);
1389 break;
1390 default:
1391 valid_data = 0;
1392 break;
1393 }
1394 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001395 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396
1397 verified = key_verify(key, signature, signaturelen, data, datalen);
1398 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001399 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001402 free(blob);
1403 free(signature);
1404 free(data);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405
Ben Lindstrome1c09122002-06-23 00:38:24 +00001406 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1407
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 monitor_reset_key_state();
1409
1410 buffer_clear(m);
1411 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001412 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001413
Darren Tuckerfbba7352006-11-07 23:16:08 +11001414 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415}
1416
1417static void
1418mm_record_login(Session *s, struct passwd *pw)
1419{
1420 socklen_t fromlen;
1421 struct sockaddr_storage from;
1422
1423 /*
1424 * Get IP address of client. If the connection is not a socket, let
1425 * the address be 0.0.0.0.
1426 */
1427 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001428 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001429 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001431 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001433 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 }
1435 }
1436 /* Record that there was a login on that tty from the remote host. */
1437 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001438 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001439 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440}
1441
1442static void
1443mm_session_close(Session *s)
1444{
Damien Miller04bd8b02003-05-25 14:38:33 +10001445 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001446 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001447 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448 session_pty_cleanup2(s);
1449 }
Damien Miller7207f642008-05-19 15:34:50 +10001450 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001451}
1452
1453int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001454mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001455{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001456 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 Session *s;
1458 int res, fd0;
1459
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001460 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001461
1462 buffer_clear(m);
1463 s = session_new();
1464 if (s == NULL)
1465 goto error;
1466 s->authctxt = authctxt;
1467 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001468 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001469 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1470 if (res == 0)
1471 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472 pty_setowner(authctxt->pw, s->tty);
1473
1474 buffer_put_int(m, 1);
1475 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476
1477 /* We need to trick ttyslot */
1478 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001479 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480
1481 mm_record_login(s, authctxt->pw);
1482
1483 /* Now we can close the file descriptor again */
1484 close(0);
1485
Darren Tucker09991742004-07-17 17:05:14 +10001486 /* send messages generated by record_login */
1487 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1488 buffer_clear(&loginmsg);
1489
1490 mm_request_send(sock, MONITOR_ANS_PTY, m);
1491
Damien Miller54fd7cf2007-09-17 12:04:08 +10001492 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1493 mm_send_fd(sock, s->ttyfd) == -1)
1494 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001495
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496 /* make sure nothing uses fd 0 */
1497 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001498 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001500 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501
1502 /* slave is not needed */
1503 close(s->ttyfd);
1504 s->ttyfd = s->ptyfd;
1505 /* no need to dup() because nobody closes ptyfd */
1506 s->ptymaster = s->ptyfd;
1507
Damien Miller9ab00b42006-08-05 12:40:11 +10001508 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509
1510 return (0);
1511
1512 error:
1513 if (s != NULL)
1514 mm_session_close(s);
1515 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001516 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001517 return (0);
1518}
1519
1520int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001521mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522{
1523 Session *s;
1524 char *tty;
1525
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001526 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527
1528 tty = buffer_get_string(m, NULL);
1529 if ((s = session_by_tty(tty)) != NULL)
1530 mm_session_close(s);
1531 buffer_clear(m);
Darren Tuckera627d422013-06-02 07:31:17 +10001532 free(tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533 return (0);
1534}
1535
1536int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001537mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538{
1539 BIGNUM *p;
1540 int rsafail;
1541
1542 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001543 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544
1545 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001546 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547
1548 buffer_get_bignum2(m, p);
1549
1550 rsafail = ssh1_session_key(p);
1551
1552 buffer_clear(m);
1553 buffer_put_int(m, rsafail);
1554 buffer_put_bignum2(m, p);
1555
1556 BN_clear_free(p);
1557
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001558 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001559
1560 /* Turn on permissions for sessid passing */
1561 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1562
1563 return (0);
1564}
1565
1566int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001567mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001568{
1569 int i;
1570
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001571 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001572
1573 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001574 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 for (i = 0; i < 16; i++)
1576 session_id[i] = buffer_get_char(m);
1577
1578 /* Turn on permissions for getpwnam */
1579 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1580
1581 return (0);
1582}
1583
1584int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001585mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586{
1587 BIGNUM *client_n;
1588 Key *key = NULL;
1589 u_char *blob = NULL;
1590 u_int blen = 0;
1591 int allowed = 0;
1592
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001593 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001594
Damien Miller7a8f5b32006-03-31 23:14:23 +11001595 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001596 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001598 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 buffer_get_bignum2(m, client_n);
1600 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1601 BN_clear_free(client_n);
1602 }
1603 buffer_clear(m);
1604 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001605 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001606
1607 /* clear temporarily storage (used by generate challenge) */
1608 monitor_reset_key_state();
1609
1610 if (allowed && key != NULL) {
1611 key->type = KEY_RSA; /* cheat for key_to_blob */
1612 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001613 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614 buffer_put_string(m, blob, blen);
1615
1616 /* Save temporarily for comparison in verify */
1617 key_blob = blob;
1618 key_bloblen = blen;
1619 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 }
Damien Miller00111382003-03-10 11:21:17 +11001621 if (key != NULL)
1622 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001624 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625
1626 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1627 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1628 return (0);
1629}
1630
1631int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001632mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633{
1634 Key *key = NULL;
1635 u_char *blob;
1636 u_int blen;
1637
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001638 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639
1640 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001641 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642 blob = buffer_get_string(m, &blen);
1643 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001644 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001646 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001647 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001648 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001649 if (key->type != KEY_RSA)
1650 fatal("%s: received bad key type %d", __func__, key->type);
1651 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652 if (ssh1_challenge)
1653 BN_clear_free(ssh1_challenge);
1654 ssh1_challenge = auth_rsa_generate_challenge(key);
1655
1656 buffer_clear(m);
1657 buffer_put_bignum2(m, ssh1_challenge);
1658
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001659 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001660 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661
1662 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001663
Darren Tuckera627d422013-06-02 07:31:17 +10001664 free(blob);
Damien Miller00111382003-03-10 11:21:17 +11001665 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 return (0);
1667}
1668
1669int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001670mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671{
1672 Key *key = NULL;
1673 u_char *blob, *response;
1674 u_int blen, len;
1675 int success;
1676
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001677 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678
1679 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001680 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001682 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683
1684 blob = buffer_get_string(m, &blen);
1685 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001686 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001688 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001690 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691 response = buffer_get_string(m, &len);
1692 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001693 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001694 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1695
Darren Tuckera627d422013-06-02 07:31:17 +10001696 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001698 free(response);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699
1700 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1701
1702 /* reset state */
1703 BN_clear_free(ssh1_challenge);
1704 ssh1_challenge = NULL;
1705 monitor_reset_key_state();
1706
1707 buffer_clear(m);
1708 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001709 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710
1711 return (success);
1712}
1713
1714int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001715mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001716{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001717 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001718 int res, status;
1719
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001720 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001721
1722 /* The child is terminating */
1723 session_destroy_all(&mm_session_close);
1724
Darren Tucker52358d62008-03-11 22:58:25 +11001725#ifdef USE_PAM
1726 if (options.use_pam)
1727 sshpam_cleanup();
1728#endif
1729
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001730 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001731 if (errno != EINTR)
1732 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001733
1734 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1735
1736 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001737 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738}
1739
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001740#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001741/* Report that an audit event occurred */
1742int
1743mm_answer_audit_event(int socket, Buffer *m)
1744{
1745 ssh_audit_event_t event;
1746
1747 debug3("%s entering", __func__);
1748
1749 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001750 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001751 case SSH_AUTH_FAIL_PUBKEY:
1752 case SSH_AUTH_FAIL_HOSTBASED:
1753 case SSH_AUTH_FAIL_GSSAPI:
1754 case SSH_LOGIN_EXCEED_MAXTRIES:
1755 case SSH_LOGIN_ROOT_DENIED:
1756 case SSH_CONNECTION_CLOSE:
1757 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001758 audit_event(event);
1759 break;
1760 default:
1761 fatal("Audit event type %d not permitted", event);
1762 }
1763
1764 return (0);
1765}
1766
1767int
1768mm_answer_audit_command(int socket, Buffer *m)
1769{
1770 u_int len;
1771 char *cmd;
1772
1773 debug3("%s entering", __func__);
1774 cmd = buffer_get_string(m, &len);
1775 /* sanity check command, if so how? */
1776 audit_run_command(cmd);
Darren Tuckerf60845f2013-06-02 08:07:31 +10001777 free(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001778 return (0);
1779}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001780#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001781
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001783monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784{
1785 if (compat20) {
1786 set_newkeys(MODE_IN);
1787 set_newkeys(MODE_OUT);
1788 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001789 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001790 packet_set_encryption_key(child_state.ssh1key,
1791 child_state.ssh1keylen, child_state.ssh1cipher);
Darren Tuckera627d422013-06-02 07:31:17 +10001792 free(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001793 }
1794
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001795 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001796 packet_set_keycontext(MODE_OUT, child_state.keyout);
Darren Tuckera627d422013-06-02 07:31:17 +10001797 free(child_state.keyout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001798 packet_set_keycontext(MODE_IN, child_state.keyin);
Darren Tuckera627d422013-06-02 07:31:17 +10001799 free(child_state.keyin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001800
1801 if (!compat20) {
1802 packet_set_iv(MODE_OUT, child_state.ivout);
Darren Tuckera627d422013-06-02 07:31:17 +10001803 free(child_state.ivout);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804 packet_set_iv(MODE_IN, child_state.ivin);
Darren Tuckera627d422013-06-02 07:31:17 +10001805 free(child_state.ivin);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806 }
1807
1808 memcpy(&incoming_stream, &child_state.incoming,
1809 sizeof(incoming_stream));
1810 memcpy(&outgoing_stream, &child_state.outgoing,
1811 sizeof(outgoing_stream));
1812
1813 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001814 if (options.compression)
1815 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001816
Darren Tucker5f96f3b2013-05-16 20:29:28 +10001817 if (options.rekey_limit || options.rekey_interval)
1818 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1819 (time_t)options.rekey_interval);
1820
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001821 /* Network I/O buffers */
1822 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001823 buffer_clear(packet_get_input());
1824 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001825 memset(child_state.input, 0, child_state.ilen);
Darren Tuckera627d422013-06-02 07:31:17 +10001826 free(child_state.input);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001827
Darren Tuckerf7288d72009-06-21 18:12:20 +10001828 buffer_clear(packet_get_output());
1829 buffer_append(packet_get_output(), child_state.output,
1830 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001831 memset(child_state.output, 0, child_state.olen);
Darren Tuckera627d422013-06-02 07:31:17 +10001832 free(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001833
1834 /* Roaming */
1835 if (compat20)
1836 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001837}
1838
1839static Kex *
1840mm_get_kex(Buffer *m)
1841{
1842 Kex *kex;
1843 void *blob;
1844 u_int bloblen;
1845
Damien Miller07d86be2006-03-26 14:19:21 +11001846 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001847 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001848 if (session_id2 == NULL ||
1849 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001850 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001851 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001852 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001853 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001854 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001855 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001856 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001857 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858 kex->server = 1;
1859 kex->hostkey_type = buffer_get_int(m);
1860 kex->kex_type = buffer_get_int(m);
1861 blob = buffer_get_string(m, &bloblen);
1862 buffer_init(&kex->my);
1863 buffer_append(&kex->my, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001864 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001865 blob = buffer_get_string(m, &bloblen);
1866 buffer_init(&kex->peer);
1867 buffer_append(&kex->peer, blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001868 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001869 kex->done = 1;
1870 kex->flags = buffer_get_int(m);
1871 kex->client_version_string = buffer_get_string(m, NULL);
1872 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001873 kex->load_host_public_key=&get_hostkey_public_by_type;
1874 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001875 kex->host_key_index=&get_hostkey_index;
Damien Miller85b45e02013-07-20 13:21:52 +10001876 kex->sign = sshd_hostkey_sign;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001877
1878 return (kex);
1879}
1880
1881/* This function requries careful sanity checking */
1882
1883void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001884mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001885{
1886 Buffer m;
1887 u_char *blob, *p;
1888 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001889 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001890 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001891
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001892 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001893
1894 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001895 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001896 if (!compat20) {
1897 child_state.ssh1protoflags = buffer_get_int(&m);
1898 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001899 child_state.ssh1key = buffer_get_string(&m,
1900 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001901 child_state.ivout = buffer_get_string(&m,
1902 &child_state.ivoutlen);
1903 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1904 goto skip;
1905 } else {
1906 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001907 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001908 }
1909
1910 blob = buffer_get_string(&m, &bloblen);
1911 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001912 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001913
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001914 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001915 blob = buffer_get_string(&m, &bloblen);
1916 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001917 free(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001918
1919 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001920 seqnr = buffer_get_int(&m);
1921 blocks = buffer_get_int64(&m);
1922 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001923 bytes = buffer_get_int64(&m);
1924 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001925 seqnr = buffer_get_int(&m);
1926 blocks = buffer_get_int64(&m);
1927 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001928 bytes = buffer_get_int64(&m);
1929 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001930
1931 skip:
1932 /* Get the key context */
1933 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1934 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1935
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001936 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001937 /* Get compression state */
1938 p = buffer_get_string(&m, &plen);
1939 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001940 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001941 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
Darren Tuckera627d422013-06-02 07:31:17 +10001942 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001943
1944 p = buffer_get_string(&m, &plen);
1945 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001946 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001947 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
Darren Tuckera627d422013-06-02 07:31:17 +10001948 free(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001949
1950 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001951 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001952 child_state.input = buffer_get_string(&m, &child_state.ilen);
1953 child_state.output = buffer_get_string(&m, &child_state.olen);
1954
Darren Tuckerc5564e12009-06-21 18:53:53 +10001955 /* Roaming */
1956 if (compat20) {
1957 child_state.sent_bytes = buffer_get_int64(&m);
1958 child_state.recv_bytes = buffer_get_int64(&m);
1959 }
1960
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001961 buffer_free(&m);
1962}
1963
1964
1965/* Allocation functions for zlib */
1966void *
1967mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1968{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001969 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001970 void *address;
1971
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001972 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001973 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1974
1975 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001976
1977 return (address);
1978}
1979
1980void
1981mm_zfree(struct mm_master *mm, void *address)
1982{
1983 mm_free(mm, address);
1984}
1985
1986void
1987mm_init_compression(struct mm_master *mm)
1988{
1989 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1990 outgoing_stream.zfree = (free_func)mm_zfree;
1991 outgoing_stream.opaque = mm;
1992
1993 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1994 incoming_stream.zfree = (free_func)mm_zfree;
1995 incoming_stream.opaque = mm;
1996}
1997
1998/* XXX */
1999
2000#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10002001 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002002 fatal("fcntl(%d, F_SETFD)", x); \
2003} while (0)
2004
2005static void
Damien Miller8f0bf232011-06-20 14:42:23 +10002006monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002007{
Damien Miller8f0bf232011-06-20 14:42:23 +10002008 int pair[2];
2009
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002010 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10002011 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002012 FD_CLOSEONEXEC(pair[0]);
2013 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10002014 mon->m_recvfd = pair[0];
2015 mon->m_sendfd = pair[1];
2016
2017 if (do_logfds) {
2018 if (pipe(pair) == -1)
2019 fatal("%s: pipe: %s", __func__, strerror(errno));
2020 FD_CLOSEONEXEC(pair[0]);
2021 FD_CLOSEONEXEC(pair[1]);
2022 mon->m_log_recvfd = pair[0];
2023 mon->m_log_sendfd = pair[1];
2024 } else
2025 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002026}
2027
2028#define MM_MEMSIZE 65536
2029
2030struct monitor *
2031monitor_init(void)
2032{
2033 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002034
Damien Miller07d86be2006-03-26 14:19:21 +11002035 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002036
Damien Miller8f0bf232011-06-20 14:42:23 +10002037 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002038
2039 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10002040 if (options.compression) {
2041 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2042 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002043
Damien Miller2d6b8332002-06-21 15:59:49 +10002044 /* Compression needs to share state across borders */
2045 mm_init_compression(mon->m_zlib);
2046 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002047
2048 return mon;
2049}
2050
2051void
2052monitor_reinit(struct monitor *mon)
2053{
Damien Miller8f0bf232011-06-20 14:42:23 +10002054 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002055}
Darren Tucker0efd1552003-08-26 11:49:55 +10002056
2057#ifdef GSSAPI
2058int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002059mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002060{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002061 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002062 OM_uint32 major;
2063 u_int len;
2064
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002065 goid.elements = buffer_get_string(m, &len);
2066 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002067
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002068 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002069
Darren Tuckera627d422013-06-02 07:31:17 +10002070 free(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002071
2072 buffer_clear(m);
2073 buffer_put_int(m, major);
2074
Damien Miller6fd6def2005-11-05 15:07:05 +11002075 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002076
2077 /* Now we have a context, enable the step */
2078 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2079
2080 return (0);
2081}
2082
2083int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002084mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002085{
2086 gss_buffer_desc in;
2087 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002088 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002089 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002090 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002091
Darren Tucker600ad8d2003-08-26 12:10:48 +10002092 in.value = buffer_get_string(m, &len);
2093 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002094 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
Darren Tuckera627d422013-06-02 07:31:17 +10002095 free(in.value);
Darren Tucker0efd1552003-08-26 11:49:55 +10002096
2097 buffer_clear(m);
2098 buffer_put_int(m, major);
2099 buffer_put_string(m, out.value, out.length);
2100 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002101 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002102
2103 gss_release_buffer(&minor, &out);
2104
Damien Miller6fd6def2005-11-05 15:07:05 +11002105 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002106 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2107 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002108 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002109 }
2110 return (0);
2111}
2112
2113int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002114mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002115{
2116 gss_buffer_desc gssbuf, mic;
2117 OM_uint32 ret;
2118 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002119
Damien Miller0425d402003-11-17 22:18:21 +11002120 gssbuf.value = buffer_get_string(m, &len);
2121 gssbuf.length = len;
2122 mic.value = buffer_get_string(m, &len);
2123 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002124
Damien Miller0425d402003-11-17 22:18:21 +11002125 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002126
Darren Tuckera627d422013-06-02 07:31:17 +10002127 free(gssbuf.value);
2128 free(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002129
Damien Miller0425d402003-11-17 22:18:21 +11002130 buffer_clear(m);
2131 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002132
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002133 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002134
Damien Miller0425d402003-11-17 22:18:21 +11002135 if (!GSS_ERROR(ret))
2136 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002137
Damien Miller0425d402003-11-17 22:18:21 +11002138 return (0);
2139}
2140
2141int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002142mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002143{
2144 int authenticated;
2145
2146 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2147
2148 buffer_clear(m);
2149 buffer_put_int(m, authenticated);
2150
2151 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002152 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002153
Damien Miller6fd6def2005-11-05 15:07:05 +11002154 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002155
2156 /* Monitor loop will terminate if authenticated */
2157 return (authenticated);
2158}
2159#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002160
2161#ifdef JPAKE
2162int
2163mm_answer_jpake_step1(int sock, Buffer *m)
2164{
2165 struct jpake_ctx *pctx;
2166 u_char *x3_proof, *x4_proof;
2167 u_int x3_proof_len, x4_proof_len;
2168
2169 if (!options.zero_knowledge_password_authentication)
2170 fatal("zero_knowledge_password_authentication disabled");
2171
2172 if (authctxt->jpake_ctx != NULL)
2173 fatal("%s: authctxt->jpake_ctx already set (%p)",
2174 __func__, authctxt->jpake_ctx);
2175 authctxt->jpake_ctx = pctx = jpake_new();
2176
2177 jpake_step1(pctx->grp,
2178 &pctx->server_id, &pctx->server_id_len,
2179 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2180 &x3_proof, &x3_proof_len,
2181 &x4_proof, &x4_proof_len);
2182
2183 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2184
2185 buffer_clear(m);
2186
2187 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2188 buffer_put_bignum2(m, pctx->g_x3);
2189 buffer_put_bignum2(m, pctx->g_x4);
2190 buffer_put_string(m, x3_proof, x3_proof_len);
2191 buffer_put_string(m, x4_proof, x4_proof_len);
2192
2193 debug3("%s: sending step1", __func__);
2194 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2195
2196 bzero(x3_proof, x3_proof_len);
2197 bzero(x4_proof, x4_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002198 free(x3_proof);
2199 free(x4_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002200
2201 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2202 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2203
2204 return 0;
2205}
2206
2207int
2208mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2209{
2210 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2211 char *hash_scheme, *salt;
2212
2213 if (pctx == NULL)
2214 fatal("%s: pctx == NULL", __func__);
2215
2216 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2217
2218 buffer_clear(m);
2219 /* pctx->s is sensitive, not returned to slave */
2220 buffer_put_cstring(m, hash_scheme);
2221 buffer_put_cstring(m, salt);
2222
2223 debug3("%s: sending pwdata", __func__);
2224 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2225
2226 bzero(hash_scheme, strlen(hash_scheme));
2227 bzero(salt, strlen(salt));
Darren Tuckera627d422013-06-02 07:31:17 +10002228 free(hash_scheme);
2229 free(salt);
Damien Miller01ed2272008-11-05 16:20:46 +11002230
2231 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2232
2233 return 0;
2234}
2235
2236int
2237mm_answer_jpake_step2(int sock, Buffer *m)
2238{
2239 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2240 u_char *x1_proof, *x2_proof, *x4_s_proof;
2241 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2242
2243 if (pctx == NULL)
2244 fatal("%s: pctx == NULL", __func__);
2245
2246 if ((pctx->g_x1 = BN_new()) == NULL ||
2247 (pctx->g_x2 = BN_new()) == NULL)
2248 fatal("%s: BN_new", __func__);
2249 buffer_get_bignum2(m, pctx->g_x1);
2250 buffer_get_bignum2(m, pctx->g_x2);
2251 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2252 x1_proof = buffer_get_string(m, &x1_proof_len);
2253 x2_proof = buffer_get_string(m, &x2_proof_len);
2254
2255 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2256 pctx->g_x1, pctx->g_x2, pctx->x4,
2257 pctx->client_id, pctx->client_id_len,
2258 pctx->server_id, pctx->server_id_len,
2259 x1_proof, x1_proof_len,
2260 x2_proof, x2_proof_len,
2261 &pctx->b,
2262 &x4_s_proof, &x4_s_proof_len);
2263
2264 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2265
2266 bzero(x1_proof, x1_proof_len);
2267 bzero(x2_proof, x2_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002268 free(x1_proof);
2269 free(x2_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002270
2271 buffer_clear(m);
2272
2273 buffer_put_bignum2(m, pctx->b);
2274 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2275
2276 debug3("%s: sending step2", __func__);
2277 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2278
2279 bzero(x4_s_proof, x4_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002280 free(x4_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11002281
2282 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2283
2284 return 0;
2285}
2286
2287int
2288mm_answer_jpake_key_confirm(int sock, Buffer *m)
2289{
2290 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2291 u_char *x2_s_proof;
2292 u_int x2_s_proof_len;
2293
2294 if (pctx == NULL)
2295 fatal("%s: pctx == NULL", __func__);
2296
2297 if ((pctx->a = BN_new()) == NULL)
2298 fatal("%s: BN_new", __func__);
2299 buffer_get_bignum2(m, pctx->a);
2300 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2301
2302 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2303 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2304 pctx->server_id, pctx->server_id_len,
2305 pctx->client_id, pctx->client_id_len,
2306 session_id2, session_id2_len,
2307 x2_s_proof, x2_s_proof_len,
2308 &pctx->k,
2309 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2310
2311 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2312
2313 bzero(x2_s_proof, x2_s_proof_len);
2314 buffer_clear(m);
2315
2316 /* pctx->k is sensitive, not sent */
2317 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2318
2319 debug3("%s: sending confirmation hash", __func__);
2320 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2321
2322 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2323
2324 return 0;
2325}
2326
2327int
2328mm_answer_jpake_check_confirm(int sock, Buffer *m)
2329{
2330 int authenticated = 0;
2331 u_char *peer_confirm_hash;
2332 u_int peer_confirm_hash_len;
2333 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2334
2335 if (pctx == NULL)
2336 fatal("%s: pctx == NULL", __func__);
2337
2338 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2339
2340 authenticated = jpake_check_confirm(pctx->k,
2341 pctx->client_id, pctx->client_id_len,
2342 session_id2, session_id2_len,
2343 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2344
2345 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2346
2347 bzero(peer_confirm_hash, peer_confirm_hash_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002348 free(peer_confirm_hash);
Damien Miller01ed2272008-11-05 16:20:46 +11002349
2350 buffer_clear(m);
2351 buffer_put_int(m, authenticated);
2352
2353 debug3("%s: sending result %d", __func__, authenticated);
2354 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2355
2356 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2357
2358 auth_method = "jpake-01@openssh.com";
2359 return authenticated;
2360}
2361
2362#endif /* JPAKE */