blob: c3a418319507fb928c80eb3c92fd8b9301af3b9b [file] [log] [blame]
Damien Millerf2e407e2011-05-20 19:04:14 +10001/* $OpenBSD: monitor.c,v 1.112 2011/05/20 03:25:45 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047
48#ifdef SKEY
49#include <skey.h>
50#endif
51
Damien Miller03e20032006-03-15 11:16:59 +110052#include <openssl/dh.h>
53
Damien Millerb84886b2008-05-19 15:05:07 +100054#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000056#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100057#include "key.h"
58#include "buffer.h"
59#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100061#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000062#include "kex.h"
63#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000064#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
65#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000066#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000067#define TARGET_OS_MAC 1
68#else
69#include "zlib.h"
70#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000071#include "packet.h"
72#include "auth-options.h"
73#include "sshpty.h"
74#include "channels.h"
75#include "session.h"
76#include "sshlogin.h"
77#include "canohost.h"
78#include "log.h"
79#include "servconf.h"
80#include "monitor.h"
81#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100082#ifdef GSSAPI
83#include "ssh-gss.h"
84#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085#include "monitor_wrap.h"
86#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088#include "compat.h"
89#include "ssh2.h"
Damien Miller01ed2272008-11-05 16:20:46 +110090#include "jpake.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +100091#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
Darren Tucker0efd1552003-08-26 11:49:55 +100093#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +100094static Gssctxt *gsscontext = NULL;
95#endif
96
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097/* Imports */
98extern ServerOptions options;
99extern u_int utmp_len;
100extern Newkeys *current_keys[];
101extern z_stream incoming_stream;
102extern z_stream outgoing_stream;
103extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104extern Buffer auth_debug;
105extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000106extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000107
108/* State exported from the child */
109
110struct {
111 z_stream incoming;
112 z_stream outgoing;
113 u_char *keyin;
114 u_int keyinlen;
115 u_char *keyout;
116 u_int keyoutlen;
117 u_char *ivin;
118 u_int ivinlen;
119 u_char *ivout;
120 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000121 u_char *ssh1key;
122 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123 int ssh1cipher;
124 int ssh1protoflags;
125 u_char *input;
126 u_int ilen;
127 u_char *output;
128 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000129 u_int64_t sent_bytes;
130 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131} child_state;
132
Damien Miller469954d2003-06-18 20:25:33 +1000133/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134
135int mm_answer_moduli(int, Buffer *);
136int mm_answer_sign(int, Buffer *);
137int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000138int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139int mm_answer_authserv(int, Buffer *);
140int mm_answer_authpassword(int, Buffer *);
141int mm_answer_bsdauthquery(int, Buffer *);
142int mm_answer_bsdauthrespond(int, Buffer *);
143int mm_answer_skeyquery(int, Buffer *);
144int mm_answer_skeyrespond(int, Buffer *);
145int mm_answer_keyallowed(int, Buffer *);
146int mm_answer_keyverify(int, Buffer *);
147int mm_answer_pty(int, Buffer *);
148int mm_answer_pty_cleanup(int, Buffer *);
149int mm_answer_term(int, Buffer *);
150int mm_answer_rsa_keyallowed(int, Buffer *);
151int mm_answer_rsa_challenge(int, Buffer *);
152int mm_answer_rsa_response(int, Buffer *);
153int mm_answer_sesskey(int, Buffer *);
154int mm_answer_sessid(int, Buffer *);
Damien Miller01ed2272008-11-05 16:20:46 +1100155int mm_answer_jpake_get_pwdata(int, Buffer *);
156int mm_answer_jpake_step1(int, Buffer *);
157int mm_answer_jpake_step2(int, Buffer *);
158int mm_answer_jpake_key_confirm(int, Buffer *);
159int mm_answer_jpake_check_confirm(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160
Damien Miller79418552002-04-23 20:28:48 +1000161#ifdef USE_PAM
162int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000163int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000164int mm_answer_pam_init_ctx(int, Buffer *);
165int mm_answer_pam_query(int, Buffer *);
166int mm_answer_pam_respond(int, Buffer *);
167int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000168#endif
169
Darren Tucker0efd1552003-08-26 11:49:55 +1000170#ifdef GSSAPI
171int mm_answer_gss_setup_ctx(int, Buffer *);
172int mm_answer_gss_accept_ctx(int, Buffer *);
173int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100174int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000175#endif
Damien Miller25162f22002-09-12 09:47:29 +1000176
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100177#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100178int mm_answer_audit_event(int, Buffer *);
179int mm_answer_audit_command(int, Buffer *);
180#endif
181
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000182static Authctxt *authctxt;
183static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
184
185/* local state for key verify */
186static u_char *key_blob = NULL;
187static u_int key_bloblen = 0;
188static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000189static char *hostbased_cuser = NULL;
190static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000192static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000193static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000194static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196struct mon_table {
197 enum monitor_reqtype type;
198 int flags;
199 int (*f)(int, Buffer *);
200};
201
202#define MON_ISAUTH 0x0004 /* Required for Authentication */
203#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
204#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100205#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206
207#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
208
209#define MON_PERMIT 0x1000 /* Request is permitted */
210
211struct mon_table mon_dispatch_proto20[] = {
212 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
213 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
214 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
215 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000216 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000217 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000218#ifdef USE_PAM
219 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000220 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000221 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
222 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
223 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
224 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000225#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100226#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100227 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100228#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000229#ifdef BSD_AUTH
230 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100231 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232#endif
233#ifdef SKEY
234 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
235 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
236#endif
237 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
238 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000239#ifdef GSSAPI
240 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
241 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
242 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100243 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000244#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100245#ifdef JPAKE
246 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
247 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
248 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
249 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
250 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
251#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000252 {0, 0, NULL}
253};
254
255struct mon_table mon_dispatch_postauth20[] = {
256 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
257 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
258 {MONITOR_REQ_PTY, 0, mm_answer_pty},
259 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
260 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100261#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100262 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
263 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
264#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000265 {0, 0, NULL}
266};
267
268struct mon_table mon_dispatch_proto15[] = {
269 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
270 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
271 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
272 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100273 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
274 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
276 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
277#ifdef BSD_AUTH
278 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100279 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000280#endif
281#ifdef SKEY
282 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
283 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
284#endif
Damien Millera33501b2002-05-08 12:24:42 +1000285#ifdef USE_PAM
286 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000287 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000288 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
289 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
290 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
291 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000292#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100293#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100294 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100295#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 {0, 0, NULL}
297};
298
299struct mon_table mon_dispatch_postauth15[] = {
300 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
301 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
302 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100303#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100304 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000305 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100306#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307 {0, 0, NULL}
308};
309
310struct mon_table *mon_dispatch;
311
312/* Specifies if a certain message is allowed at the moment */
313
314static void
315monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
316{
317 while (ent->f != NULL) {
318 if (ent->type == type) {
319 ent->flags &= ~MON_PERMIT;
320 ent->flags |= permit ? MON_PERMIT : 0;
321 return;
322 }
323 ent++;
324 }
325}
326
327static void
328monitor_permit_authentications(int permit)
329{
330 struct mon_table *ent = mon_dispatch;
331
332 while (ent->f != NULL) {
333 if (ent->flags & MON_AUTH) {
334 ent->flags &= ~MON_PERMIT;
335 ent->flags |= permit ? MON_PERMIT : 0;
336 }
337 ent++;
338 }
339}
340
Darren Tucker3e33cec2003-10-02 16:12:36 +1000341void
342monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343{
344 struct mon_table *ent;
345 int authenticated = 0;
346
347 debug3("preauth child monitor started");
348
Darren Tucker3e33cec2003-10-02 16:12:36 +1000349 authctxt = _authctxt;
350 memset(authctxt, 0, sizeof(*authctxt));
351
Darren Tuckerde0de392005-03-31 23:52:04 +1000352 authctxt->loginmsg = &loginmsg;
353
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000354 if (compat20) {
355 mon_dispatch = mon_dispatch_proto20;
356
357 /* Permit requests for moduli and signatures */
358 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
359 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
360 } else {
361 mon_dispatch = mon_dispatch_proto15;
362
363 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
364 }
365
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366 /* The first few requests do not require asynchronous access */
367 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100368 auth_method = "unknown";
Darren Tuckerfbba7352006-11-07 23:16:08 +1100369 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 if (authenticated) {
371 if (!(ent->flags & MON_AUTHDECIDE))
372 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000373 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374 if (authctxt->pw->pw_uid == 0 &&
375 !auth_root_allowed(auth_method))
376 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000377#ifdef USE_PAM
378 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100379 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000380 Buffer m;
381
382 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100383 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000384 MONITOR_REQ_PAM_ACCOUNT, &m);
385 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
386 buffer_free(&m);
387 }
388#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000389 }
390
Damien Miller7a8f5b32006-03-31 23:14:23 +1100391 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000392 auth_log(authctxt, authenticated, auth_method,
393 compat20 ? " ssh2" : "");
394 if (!authenticated)
395 authctxt->failures++;
396 }
Damien Miller01ed2272008-11-05 16:20:46 +1100397#ifdef JPAKE
398 /* Cleanup JPAKE context after authentication */
399 if (ent->flags & MON_AUTHDECIDE) {
400 if (authctxt->jpake_ctx != NULL) {
401 jpake_free(authctxt->jpake_ctx);
402 authctxt->jpake_ctx = NULL;
403 }
404 }
405#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 }
407
408 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000409 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100410 if (strcmp(auth_method, "unknown") == 0)
411 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000412
413 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000414 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000416 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000417}
418
Damien Millerbe64d432003-05-14 19:31:12 +1000419static void
420monitor_set_child_handler(pid_t pid)
421{
422 monitor_child_pid = pid;
423}
424
425static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000426monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000427{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000428 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000429}
430
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000431void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433{
Damien Millerbe64d432003-05-14 19:31:12 +1000434 monitor_set_child_handler(pmonitor->m_pid);
435 signal(SIGHUP, &monitor_child_handler);
436 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000437 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000438
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439 if (compat20) {
440 mon_dispatch = mon_dispatch_postauth20;
441
442 /* Permit requests for moduli and signatures */
443 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
444 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
445 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000446 } else {
447 mon_dispatch = mon_dispatch_postauth15;
448 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
449 }
450 if (!no_pty_flag) {
451 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
452 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
453 }
454
455 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000456 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000457}
458
459void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000460monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461{
Damien Miller2d6b8332002-06-21 15:59:49 +1000462 if (options.compression) {
463 /* The member allocation is not visible, so sync it */
464 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
465 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466}
467
468int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000469monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 struct mon_table **pent)
471{
472 Buffer m;
473 int ret;
474 u_char type;
475
476 buffer_init(&m);
477
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000478 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000479 type = buffer_get_char(&m);
480
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000481 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482
483 while (ent->f != NULL) {
484 if (ent->type == type)
485 break;
486 ent++;
487 }
488
489 if (ent->f != NULL) {
490 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000491 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000493 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000494 buffer_free(&m);
495
496 /* The child may use this request only once, disable it */
497 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000498 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499 type);
500 ent->flags &= ~MON_PERMIT;
501 }
502
503 if (pent != NULL)
504 *pent = ent;
505
506 return ret;
507 }
508
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510
511 /* NOTREACHED */
512 return (-1);
513}
514
515/* allowed key state */
516static int
517monitor_allowed_key(u_char *blob, u_int bloblen)
518{
519 /* make sure key is allowed */
520 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000521 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522 return (0);
523 return (1);
524}
525
526static void
527monitor_reset_key_state(void)
528{
529 /* reset state */
530 if (key_blob != NULL)
531 xfree(key_blob);
532 if (hostbased_cuser != NULL)
533 xfree(hostbased_cuser);
534 if (hostbased_chost != NULL)
535 xfree(hostbased_chost);
536 key_blob = NULL;
537 key_bloblen = 0;
538 key_blobtype = MM_NOKEY;
539 hostbased_cuser = NULL;
540 hostbased_chost = NULL;
541}
542
543int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000544mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000545{
546 DH *dh;
547 int min, want, max;
548
549 min = buffer_get_int(m);
550 want = buffer_get_int(m);
551 max = buffer_get_int(m);
552
553 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000554 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555 /* We need to check here, too, in case the child got corrupted */
556 if (max < min || want < min || max < want)
557 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000558 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000559
560 buffer_clear(m);
561
562 dh = choose_dh(min, want, max);
563 if (dh == NULL) {
564 buffer_put_char(m, 0);
565 return (0);
566 } else {
567 /* Send first bignum */
568 buffer_put_char(m, 1);
569 buffer_put_bignum2(m, dh->p);
570 buffer_put_bignum2(m, dh->g);
571
572 DH_free(dh);
573 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000574 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575 return (0);
576}
577
578int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000579mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580{
581 Key *key;
582 u_char *p;
583 u_char *signature;
584 u_int siglen, datlen;
585 int keyid;
586
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000587 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
589 keyid = buffer_get_int(m);
590 p = buffer_get_string(m, &datlen);
591
Damien Millera63128d2006-03-15 12:08:28 +1100592 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000593 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
594 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100595 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000596 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000597 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
Ben Lindstromf67e0772002-06-06 20:58:19 +0000599 /* save session id, it will be passed on the first call */
600 if (session_id2_len == 0) {
601 session_id2_len = datlen;
602 session_id2 = xmalloc(session_id2_len);
603 memcpy(session_id2, p, session_id2_len);
604 }
605
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000607 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000609 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610
Ben Lindstromd5502182002-06-27 00:12:57 +0000611 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
613 buffer_clear(m);
614 buffer_put_string(m, signature, siglen);
615
616 xfree(p);
617 xfree(signature);
618
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000619 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 /* Turn on permissions for getpwnam */
622 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
623
624 return (0);
625}
626
627/* Retrieves the password entry and also checks if the user is permitted */
628
629int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000630mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000632 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633 struct passwd *pwent;
634 int allowed = 0;
635
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000636 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000637
638 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000639 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640
Darren Tuckerb09b6772004-06-22 15:06:46 +1000641 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000642
Darren Tuckerb09b6772004-06-22 15:06:46 +1000643 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644
Darren Tuckerb09b6772004-06-22 15:06:46 +1000645 authctxt->user = xstrdup(username);
646 setproctitle("%s [priv]", pwent ? username : "unknown");
647 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648
649 buffer_clear(m);
650
651 if (pwent == NULL) {
652 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100653 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654 goto out;
655 }
656
657 allowed = 1;
658 authctxt->pw = pwent;
659 authctxt->valid = 1;
660
661 buffer_put_char(m, 1);
662 buffer_put_string(m, pwent, sizeof(struct passwd));
663 buffer_put_cstring(m, pwent->pw_name);
664 buffer_put_cstring(m, "*");
665 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000666#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000668#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000669 buffer_put_cstring(m, pwent->pw_dir);
670 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100671
672 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100673 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000674
675#define M_CP_STROPT(x) do { \
676 if (options.x != NULL) \
677 buffer_put_cstring(m, options.x); \
678 } while (0)
679 /* See comment in servconf.h */
680 COPY_MATCH_STRING_OPTS();
681#undef M_CP_STROPT
682
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000683 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685
686 /* For SSHv1 allow authentication now */
687 if (!compat20)
688 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000689 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 /* Allow service/style information on the auth context */
691 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000692 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
693 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000694
Damien Millera33501b2002-05-08 12:24:42 +1000695#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000696 if (options.use_pam)
697 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000698#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
700 return (0);
701}
702
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000703int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000704{
705 char *banner;
706
707 buffer_clear(m);
708 banner = auth2_read_banner();
709 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000710 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000711
712 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000713 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000714
715 return (0);
716}
717
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000719mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720{
721 monitor_permit_authentications(1);
722
723 authctxt->service = buffer_get_string(m, NULL);
724 authctxt->style = buffer_get_string(m, NULL);
725 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000726 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727
728 if (strlen(authctxt->style) == 0) {
729 xfree(authctxt->style);
730 authctxt->style = NULL;
731 }
732
733 return (0);
734}
735
736int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000737mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738{
739 static int call_count;
740 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000741 int authenticated;
742 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
744 passwd = buffer_get_string(m, &plen);
745 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000746 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000747 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 memset(passwd, 0, strlen(passwd));
749 xfree(passwd);
750
751 buffer_clear(m);
752 buffer_put_int(m, authenticated);
753
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
757 call_count++;
758 if (plen == 0 && call_count == 1)
759 auth_method = "none";
760 else
761 auth_method = "password";
762
763 /* Causes monitor loop to terminate if authenticated */
764 return (authenticated);
765}
766
767#ifdef BSD_AUTH
768int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000769mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770{
771 char *name, *infotxt;
772 u_int numprompts;
773 u_int *echo_on;
774 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100775 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776
Damien Millerb7df3af2003-02-24 11:55:46 +1100777 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
778 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779
780 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100781 buffer_put_int(m, success);
782 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 buffer_put_cstring(m, prompts[0]);
784
Damien Millerb7df3af2003-02-24 11:55:46 +1100785 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000786 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787
Damien Millerb7df3af2003-02-24 11:55:46 +1100788 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000789 xfree(name);
790 xfree(infotxt);
791 xfree(prompts);
792 xfree(echo_on);
793 }
794
795 return (0);
796}
797
798int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000799mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000800{
801 char *response;
802 int authok;
803
804 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000805 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000806
807 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000808 authok = options.challenge_response_authentication &&
809 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000810 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000811 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000812 xfree(response);
813
814 buffer_clear(m);
815 buffer_put_int(m, authok);
816
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000817 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000818 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000819
820 auth_method = "bsdauth";
821
822 return (authok != 0);
823}
824#endif
825
826#ifdef SKEY
827int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000828mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000829{
830 struct skey skey;
831 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100832 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000834 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
835 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000836
837 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100838 buffer_put_int(m, success);
839 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000840 buffer_put_cstring(m, challenge);
841
Damien Millerb7df3af2003-02-24 11:55:46 +1100842 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000843 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000844
845 return (0);
846}
847
848int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000849mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000850{
851 char *response;
852 int authok;
853
854 response = buffer_get_string(m, NULL);
855
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000856 authok = (options.challenge_response_authentication &&
857 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858 skey_haskey(authctxt->pw->pw_name) == 0 &&
859 skey_passcheck(authctxt->pw->pw_name, response) != -1);
860
861 xfree(response);
862
863 buffer_clear(m);
864 buffer_put_int(m, authok);
865
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000866 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000867 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000868
869 auth_method = "skey";
870
871 return (authok != 0);
872}
873#endif
874
Damien Miller79418552002-04-23 20:28:48 +1000875#ifdef USE_PAM
876int
Darren Tucker7eda5922004-06-22 13:22:50 +1000877mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000878{
Damien Miller4e448a32003-05-14 15:11:48 +1000879 if (!options.use_pam)
880 fatal("UsePAM not set, but ended up in %s anyway", __func__);
881
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100882 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000883
Damien Miller1f499fd2003-08-25 13:08:49 +1000884 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
885
Damien Miller79418552002-04-23 20:28:48 +1000886 return (0);
887}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000888
Damien Miller1f499fd2003-08-25 13:08:49 +1000889int
Darren Tucker7eda5922004-06-22 13:22:50 +1000890mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000891{
892 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100893
Damien Miller1f499fd2003-08-25 13:08:49 +1000894 if (!options.use_pam)
895 fatal("UsePAM not set, but ended up in %s anyway", __func__);
896
897 ret = do_pam_account();
898
899 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000900 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000901
Darren Tucker7eda5922004-06-22 13:22:50 +1000902 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000903
904 return (ret);
905}
906
Damien Miller4f9f42a2003-05-10 19:28:02 +1000907static void *sshpam_ctxt, *sshpam_authok;
908extern KbdintDevice sshpam_device;
909
910int
Darren Tucker7eda5922004-06-22 13:22:50 +1000911mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000912{
913
914 debug3("%s", __func__);
915 authctxt->user = buffer_get_string(m, NULL);
916 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
917 sshpam_authok = NULL;
918 buffer_clear(m);
919 if (sshpam_ctxt != NULL) {
920 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
921 buffer_put_int(m, 1);
922 } else {
923 buffer_put_int(m, 0);
924 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000925 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000926 return (0);
927}
928
929int
Darren Tucker7eda5922004-06-22 13:22:50 +1000930mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000931{
Darren Tucker8b7a0552010-08-03 15:50:16 +1000932 char *name = NULL, *info = NULL, **prompts = NULL;
933 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +1000934 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000935
936 debug3("%s", __func__);
937 sshpam_authok = NULL;
938 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
939 if (ret == 0 && num == 0)
940 sshpam_authok = sshpam_ctxt;
941 if (num > 1 || name == NULL || info == NULL)
942 ret = -1;
943 buffer_clear(m);
944 buffer_put_int(m, ret);
945 buffer_put_cstring(m, name);
946 xfree(name);
947 buffer_put_cstring(m, info);
948 xfree(info);
949 buffer_put_int(m, num);
950 for (i = 0; i < num; ++i) {
951 buffer_put_cstring(m, prompts[i]);
952 xfree(prompts[i]);
953 buffer_put_int(m, echo_on[i]);
954 }
955 if (prompts != NULL)
956 xfree(prompts);
957 if (echo_on != NULL)
958 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000959 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000960 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961 return (0);
962}
963
964int
Darren Tucker7eda5922004-06-22 13:22:50 +1000965mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966{
967 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000968 u_int i, num;
969 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000970
971 debug3("%s", __func__);
972 sshpam_authok = NULL;
973 num = buffer_get_int(m);
974 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000975 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000976 for (i = 0; i < num; ++i)
977 resp[i] = buffer_get_string(m, NULL);
978 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
979 for (i = 0; i < num; ++i)
980 xfree(resp[i]);
981 xfree(resp);
982 } else {
983 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
984 }
985 buffer_clear(m);
986 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000987 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000988 auth_method = "keyboard-interactive/pam";
989 if (ret == 0)
990 sshpam_authok = sshpam_ctxt;
991 return (0);
992}
993
994int
Darren Tucker7eda5922004-06-22 13:22:50 +1000995mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000996{
997
998 debug3("%s", __func__);
999 (sshpam_device.free_ctx)(sshpam_ctxt);
1000 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001001 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +10001002 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001003 return (sshpam_authok == sshpam_ctxt);
1004}
Damien Miller79418552002-04-23 20:28:48 +10001005#endif
1006
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001008mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009{
1010 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001011 char *cuser, *chost;
1012 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013 u_int bloblen;
1014 enum mm_keytype type = 0;
1015 int allowed = 0;
1016
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001017 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018
1019 type = buffer_get_int(m);
1020 cuser = buffer_get_string(m, NULL);
1021 chost = buffer_get_string(m, NULL);
1022 blob = buffer_get_string(m, &bloblen);
1023
1024 key = key_from_blob(blob, bloblen);
1025
1026 if ((compat20 && type == MM_RSAHOSTKEY) ||
1027 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001028 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001030 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031
Damien Miller3e3b5142003-11-17 21:13:40 +11001032 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001033 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001035 allowed = options.pubkey_authentication &&
1036 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001037 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001038 if (options.pubkey_authentication && allowed != 1)
1039 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001040 break;
1041 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001042 allowed = options.hostbased_authentication &&
1043 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001044 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001045 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046 break;
1047 case MM_RSAHOSTKEY:
1048 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001049 allowed = options.rhosts_rsa_authentication &&
1050 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001052 if (options.rhosts_rsa_authentication && allowed != 1)
1053 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001054 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001055 break;
1056 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001057 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 break;
1059 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060 }
Damien Miller00111382003-03-10 11:21:17 +11001061 if (key != NULL)
1062 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063
1064 /* clear temporarily storage (used by verify) */
1065 monitor_reset_key_state();
1066
1067 if (allowed) {
1068 /* Save temporarily for comparison in verify */
1069 key_blob = blob;
1070 key_bloblen = bloblen;
1071 key_blobtype = type;
1072 hostbased_cuser = cuser;
1073 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001074 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001075 /* Log failed attempt */
1076 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001077 xfree(blob);
1078 xfree(cuser);
1079 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001080 }
1081
1082 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001083 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001084
1085 buffer_clear(m);
1086 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001087 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001089 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001090
1091 if (type == MM_RSAHOSTKEY)
1092 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1093
1094 return (0);
1095}
1096
1097static int
1098monitor_valid_userblob(u_char *data, u_int datalen)
1099{
1100 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001101 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102 u_int len;
1103 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001104
1105 buffer_init(&b);
1106 buffer_append(&b, data, datalen);
1107
1108 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001109 p = buffer_ptr(&b);
1110 len = buffer_len(&b);
1111 if ((session_id2 == NULL) ||
1112 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001113 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001114 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001115 buffer_consume(&b, session_id2_len);
1116 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001117 p = buffer_get_string(&b, &len);
1118 if ((session_id2 == NULL) ||
1119 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001120 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001121 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001122 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001123 }
1124 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1125 fail++;
1126 p = buffer_get_string(&b, NULL);
1127 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001128 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129 authctxt->user, p);
1130 fail++;
1131 }
1132 xfree(p);
1133 buffer_skip_string(&b);
1134 if (datafellows & SSH_BUG_PKAUTH) {
1135 if (!buffer_get_char(&b))
1136 fail++;
1137 } else {
1138 p = buffer_get_string(&b, NULL);
1139 if (strcmp("publickey", p) != 0)
1140 fail++;
1141 xfree(p);
1142 if (!buffer_get_char(&b))
1143 fail++;
1144 buffer_skip_string(&b);
1145 }
1146 buffer_skip_string(&b);
1147 if (buffer_len(&b) != 0)
1148 fail++;
1149 buffer_free(&b);
1150 return (fail == 0);
1151}
1152
1153static int
Damien Millera10f5612002-09-12 09:49:15 +10001154monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1155 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156{
1157 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001158 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159 u_int len;
1160 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001161
1162 buffer_init(&b);
1163 buffer_append(&b, data, datalen);
1164
Ben Lindstromf67e0772002-06-06 20:58:19 +00001165 p = buffer_get_string(&b, &len);
1166 if ((session_id2 == NULL) ||
1167 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001168 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001169 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001170 xfree(p);
1171
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001172 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1173 fail++;
1174 p = buffer_get_string(&b, NULL);
1175 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001176 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 authctxt->user, p);
1178 fail++;
1179 }
1180 xfree(p);
1181 buffer_skip_string(&b); /* service */
1182 p = buffer_get_string(&b, NULL);
1183 if (strcmp(p, "hostbased") != 0)
1184 fail++;
1185 xfree(p);
1186 buffer_skip_string(&b); /* pkalg */
1187 buffer_skip_string(&b); /* pkblob */
1188
1189 /* verify client host, strip trailing dot if necessary */
1190 p = buffer_get_string(&b, NULL);
1191 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1192 p[len - 1] = '\0';
1193 if (strcmp(p, chost) != 0)
1194 fail++;
1195 xfree(p);
1196
1197 /* verify client user */
1198 p = buffer_get_string(&b, NULL);
1199 if (strcmp(p, cuser) != 0)
1200 fail++;
1201 xfree(p);
1202
1203 if (buffer_len(&b) != 0)
1204 fail++;
1205 buffer_free(&b);
1206 return (fail == 0);
1207}
1208
1209int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001210mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001211{
1212 Key *key;
1213 u_char *signature, *data, *blob;
1214 u_int signaturelen, datalen, bloblen;
1215 int verified = 0;
1216 int valid_data = 0;
1217
1218 blob = buffer_get_string(m, &bloblen);
1219 signature = buffer_get_string(m, &signaturelen);
1220 data = buffer_get_string(m, &datalen);
1221
1222 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001223 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001224 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
1226 key = key_from_blob(blob, bloblen);
1227 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001228 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229
1230 switch (key_blobtype) {
1231 case MM_USERKEY:
1232 valid_data = monitor_valid_userblob(data, datalen);
1233 break;
1234 case MM_HOSTKEY:
1235 valid_data = monitor_valid_hostbasedblob(data, datalen,
1236 hostbased_cuser, hostbased_chost);
1237 break;
1238 default:
1239 valid_data = 0;
1240 break;
1241 }
1242 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001243 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244
1245 verified = key_verify(key, signature, signaturelen, data, datalen);
1246 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001247 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248
1249 key_free(key);
1250 xfree(blob);
1251 xfree(signature);
1252 xfree(data);
1253
Ben Lindstrome1c09122002-06-23 00:38:24 +00001254 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1255
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 monitor_reset_key_state();
1257
1258 buffer_clear(m);
1259 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001260 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261
Darren Tuckerfbba7352006-11-07 23:16:08 +11001262 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263}
1264
1265static void
1266mm_record_login(Session *s, struct passwd *pw)
1267{
1268 socklen_t fromlen;
1269 struct sockaddr_storage from;
1270
1271 /*
1272 * Get IP address of client. If the connection is not a socket, let
1273 * the address be 0.0.0.0.
1274 */
1275 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001276 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001279 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001280 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001281 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 }
1283 }
1284 /* Record that there was a login on that tty from the remote host. */
1285 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001286 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001287 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288}
1289
1290static void
1291mm_session_close(Session *s)
1292{
Damien Miller04bd8b02003-05-25 14:38:33 +10001293 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001294 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001295 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001296 session_pty_cleanup2(s);
1297 }
Damien Miller7207f642008-05-19 15:34:50 +10001298 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299}
1300
1301int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001302mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001303{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001304 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 Session *s;
1306 int res, fd0;
1307
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001308 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309
1310 buffer_clear(m);
1311 s = session_new();
1312 if (s == NULL)
1313 goto error;
1314 s->authctxt = authctxt;
1315 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001316 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1318 if (res == 0)
1319 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 pty_setowner(authctxt->pw, s->tty);
1321
1322 buffer_put_int(m, 1);
1323 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001324
1325 /* We need to trick ttyslot */
1326 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001327 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001328
1329 mm_record_login(s, authctxt->pw);
1330
1331 /* Now we can close the file descriptor again */
1332 close(0);
1333
Darren Tucker09991742004-07-17 17:05:14 +10001334 /* send messages generated by record_login */
1335 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1336 buffer_clear(&loginmsg);
1337
1338 mm_request_send(sock, MONITOR_ANS_PTY, m);
1339
Damien Miller54fd7cf2007-09-17 12:04:08 +10001340 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1341 mm_send_fd(sock, s->ttyfd) == -1)
1342 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001343
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344 /* make sure nothing uses fd 0 */
1345 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001346 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001348 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349
1350 /* slave is not needed */
1351 close(s->ttyfd);
1352 s->ttyfd = s->ptyfd;
1353 /* no need to dup() because nobody closes ptyfd */
1354 s->ptymaster = s->ptyfd;
1355
Damien Miller9ab00b42006-08-05 12:40:11 +10001356 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357
1358 return (0);
1359
1360 error:
1361 if (s != NULL)
1362 mm_session_close(s);
1363 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001364 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365 return (0);
1366}
1367
1368int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001369mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370{
1371 Session *s;
1372 char *tty;
1373
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001374 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001375
1376 tty = buffer_get_string(m, NULL);
1377 if ((s = session_by_tty(tty)) != NULL)
1378 mm_session_close(s);
1379 buffer_clear(m);
1380 xfree(tty);
1381 return (0);
1382}
1383
1384int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001385mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386{
1387 BIGNUM *p;
1388 int rsafail;
1389
1390 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001391 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392
1393 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001394 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 buffer_get_bignum2(m, p);
1397
1398 rsafail = ssh1_session_key(p);
1399
1400 buffer_clear(m);
1401 buffer_put_int(m, rsafail);
1402 buffer_put_bignum2(m, p);
1403
1404 BN_clear_free(p);
1405
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001406 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407
1408 /* Turn on permissions for sessid passing */
1409 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1410
1411 return (0);
1412}
1413
1414int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001415mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416{
1417 int i;
1418
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001419 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420
1421 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001422 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423 for (i = 0; i < 16; i++)
1424 session_id[i] = buffer_get_char(m);
1425
1426 /* Turn on permissions for getpwnam */
1427 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1428
1429 return (0);
1430}
1431
1432int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001433mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434{
1435 BIGNUM *client_n;
1436 Key *key = NULL;
1437 u_char *blob = NULL;
1438 u_int blen = 0;
1439 int allowed = 0;
1440
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001441 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442
Damien Miller7a8f5b32006-03-31 23:14:23 +11001443 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001444 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001446 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 buffer_get_bignum2(m, client_n);
1448 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1449 BN_clear_free(client_n);
1450 }
1451 buffer_clear(m);
1452 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001453 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454
1455 /* clear temporarily storage (used by generate challenge) */
1456 monitor_reset_key_state();
1457
1458 if (allowed && key != NULL) {
1459 key->type = KEY_RSA; /* cheat for key_to_blob */
1460 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462 buffer_put_string(m, blob, blen);
1463
1464 /* Save temporarily for comparison in verify */
1465 key_blob = blob;
1466 key_bloblen = blen;
1467 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468 }
Damien Miller00111382003-03-10 11:21:17 +11001469 if (key != NULL)
1470 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001472 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473
1474 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1475 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1476 return (0);
1477}
1478
1479int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001480mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481{
1482 Key *key = NULL;
1483 u_char *blob;
1484 u_int blen;
1485
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001486 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487
1488 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001489 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490 blob = buffer_get_string(m, &blen);
1491 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001492 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001493 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001496 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001497 if (key->type != KEY_RSA)
1498 fatal("%s: received bad key type %d", __func__, key->type);
1499 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 if (ssh1_challenge)
1501 BN_clear_free(ssh1_challenge);
1502 ssh1_challenge = auth_rsa_generate_challenge(key);
1503
1504 buffer_clear(m);
1505 buffer_put_bignum2(m, ssh1_challenge);
1506
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001507 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001508 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509
1510 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001511
1512 xfree(blob);
1513 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 return (0);
1515}
1516
1517int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001518mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519{
1520 Key *key = NULL;
1521 u_char *blob, *response;
1522 u_int blen, len;
1523 int success;
1524
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001525 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526
1527 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001528 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001530 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001531
1532 blob = buffer_get_string(m, &blen);
1533 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001534 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001535 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001536 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001538 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539 response = buffer_get_string(m, &len);
1540 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001541 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1543
Damien Miller00111382003-03-10 11:21:17 +11001544 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545 key_free(key);
1546 xfree(response);
1547
1548 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1549
1550 /* reset state */
1551 BN_clear_free(ssh1_challenge);
1552 ssh1_challenge = NULL;
1553 monitor_reset_key_state();
1554
1555 buffer_clear(m);
1556 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001557 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001558
1559 return (success);
1560}
1561
1562int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001563mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001565 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001566 int res, status;
1567
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001568 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569
1570 /* The child is terminating */
1571 session_destroy_all(&mm_session_close);
1572
Darren Tucker52358d62008-03-11 22:58:25 +11001573#ifdef USE_PAM
1574 if (options.use_pam)
1575 sshpam_cleanup();
1576#endif
1577
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001578 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001579 if (errno != EINTR)
1580 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581
1582 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1583
1584 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001585 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586}
1587
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001588#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001589/* Report that an audit event occurred */
1590int
1591mm_answer_audit_event(int socket, Buffer *m)
1592{
1593 ssh_audit_event_t event;
1594
1595 debug3("%s entering", __func__);
1596
1597 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001598 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001599 case SSH_AUTH_FAIL_PUBKEY:
1600 case SSH_AUTH_FAIL_HOSTBASED:
1601 case SSH_AUTH_FAIL_GSSAPI:
1602 case SSH_LOGIN_EXCEED_MAXTRIES:
1603 case SSH_LOGIN_ROOT_DENIED:
1604 case SSH_CONNECTION_CLOSE:
1605 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001606 audit_event(event);
1607 break;
1608 default:
1609 fatal("Audit event type %d not permitted", event);
1610 }
1611
1612 return (0);
1613}
1614
1615int
1616mm_answer_audit_command(int socket, Buffer *m)
1617{
1618 u_int len;
1619 char *cmd;
1620
1621 debug3("%s entering", __func__);
1622 cmd = buffer_get_string(m, &len);
1623 /* sanity check command, if so how? */
1624 audit_run_command(cmd);
1625 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001626 return (0);
1627}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001628#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001629
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001631monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632{
1633 if (compat20) {
1634 set_newkeys(MODE_IN);
1635 set_newkeys(MODE_OUT);
1636 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001637 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001638 packet_set_encryption_key(child_state.ssh1key,
1639 child_state.ssh1keylen, child_state.ssh1cipher);
1640 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001641 }
1642
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001643 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644 packet_set_keycontext(MODE_OUT, child_state.keyout);
1645 xfree(child_state.keyout);
1646 packet_set_keycontext(MODE_IN, child_state.keyin);
1647 xfree(child_state.keyin);
1648
1649 if (!compat20) {
1650 packet_set_iv(MODE_OUT, child_state.ivout);
1651 xfree(child_state.ivout);
1652 packet_set_iv(MODE_IN, child_state.ivin);
1653 xfree(child_state.ivin);
1654 }
1655
1656 memcpy(&incoming_stream, &child_state.incoming,
1657 sizeof(incoming_stream));
1658 memcpy(&outgoing_stream, &child_state.outgoing,
1659 sizeof(outgoing_stream));
1660
1661 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001662 if (options.compression)
1663 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664
1665 /* Network I/O buffers */
1666 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001667 buffer_clear(packet_get_input());
1668 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 memset(child_state.input, 0, child_state.ilen);
1670 xfree(child_state.input);
1671
Darren Tuckerf7288d72009-06-21 18:12:20 +10001672 buffer_clear(packet_get_output());
1673 buffer_append(packet_get_output(), child_state.output,
1674 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675 memset(child_state.output, 0, child_state.olen);
1676 xfree(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001677
1678 /* Roaming */
1679 if (compat20)
1680 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681}
1682
1683static Kex *
1684mm_get_kex(Buffer *m)
1685{
1686 Kex *kex;
1687 void *blob;
1688 u_int bloblen;
1689
Damien Miller07d86be2006-03-26 14:19:21 +11001690 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001691 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001692 if (session_id2 == NULL ||
1693 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001694 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001695 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001697 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001698 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001699 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001700 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001701 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001702 kex->server = 1;
1703 kex->hostkey_type = buffer_get_int(m);
1704 kex->kex_type = buffer_get_int(m);
1705 blob = buffer_get_string(m, &bloblen);
1706 buffer_init(&kex->my);
1707 buffer_append(&kex->my, blob, bloblen);
1708 xfree(blob);
1709 blob = buffer_get_string(m, &bloblen);
1710 buffer_init(&kex->peer);
1711 buffer_append(&kex->peer, blob, bloblen);
1712 xfree(blob);
1713 kex->done = 1;
1714 kex->flags = buffer_get_int(m);
1715 kex->client_version_string = buffer_get_string(m, NULL);
1716 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001717 kex->load_host_public_key=&get_hostkey_public_by_type;
1718 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 kex->host_key_index=&get_hostkey_index;
1720
1721 return (kex);
1722}
1723
1724/* This function requries careful sanity checking */
1725
1726void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001727mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001728{
1729 Buffer m;
1730 u_char *blob, *p;
1731 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001732 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001733 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001734
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001735 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001736
1737 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001738 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739 if (!compat20) {
1740 child_state.ssh1protoflags = buffer_get_int(&m);
1741 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001742 child_state.ssh1key = buffer_get_string(&m,
1743 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 child_state.ivout = buffer_get_string(&m,
1745 &child_state.ivoutlen);
1746 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1747 goto skip;
1748 } else {
1749 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001750 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001751 }
1752
1753 blob = buffer_get_string(&m, &bloblen);
1754 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1755 xfree(blob);
1756
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001757 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758 blob = buffer_get_string(&m, &bloblen);
1759 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1760 xfree(blob);
1761
1762 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001763 seqnr = buffer_get_int(&m);
1764 blocks = buffer_get_int64(&m);
1765 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001766 bytes = buffer_get_int64(&m);
1767 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001768 seqnr = buffer_get_int(&m);
1769 blocks = buffer_get_int64(&m);
1770 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001771 bytes = buffer_get_int64(&m);
1772 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001773
1774 skip:
1775 /* Get the key context */
1776 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1777 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1778
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001779 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001780 /* Get compression state */
1781 p = buffer_get_string(&m, &plen);
1782 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001783 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001784 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1785 xfree(p);
1786
1787 p = buffer_get_string(&m, &plen);
1788 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001789 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001790 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1791 xfree(p);
1792
1793 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001794 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795 child_state.input = buffer_get_string(&m, &child_state.ilen);
1796 child_state.output = buffer_get_string(&m, &child_state.olen);
1797
Darren Tuckerc5564e12009-06-21 18:53:53 +10001798 /* Roaming */
1799 if (compat20) {
1800 child_state.sent_bytes = buffer_get_int64(&m);
1801 child_state.recv_bytes = buffer_get_int64(&m);
1802 }
1803
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001804 buffer_free(&m);
1805}
1806
1807
1808/* Allocation functions for zlib */
1809void *
1810mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1811{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001812 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001813 void *address;
1814
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001815 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001816 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1817
1818 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819
1820 return (address);
1821}
1822
1823void
1824mm_zfree(struct mm_master *mm, void *address)
1825{
1826 mm_free(mm, address);
1827}
1828
1829void
1830mm_init_compression(struct mm_master *mm)
1831{
1832 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1833 outgoing_stream.zfree = (free_func)mm_zfree;
1834 outgoing_stream.opaque = mm;
1835
1836 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1837 incoming_stream.zfree = (free_func)mm_zfree;
1838 incoming_stream.opaque = mm;
1839}
1840
1841/* XXX */
1842
1843#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001844 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001845 fatal("fcntl(%d, F_SETFD)", x); \
1846} while (0)
1847
1848static void
1849monitor_socketpair(int *pair)
1850{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001851#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001852 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001853 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001854#else
1855 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001856 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001857#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858 FD_CLOSEONEXEC(pair[0]);
1859 FD_CLOSEONEXEC(pair[1]);
1860}
1861
1862#define MM_MEMSIZE 65536
1863
1864struct monitor *
1865monitor_init(void)
1866{
1867 struct monitor *mon;
1868 int pair[2];
1869
Damien Miller07d86be2006-03-26 14:19:21 +11001870 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001871
1872 monitor_socketpair(pair);
1873
1874 mon->m_recvfd = pair[0];
1875 mon->m_sendfd = pair[1];
1876
1877 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001878 if (options.compression) {
1879 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1880 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001881
Damien Miller2d6b8332002-06-21 15:59:49 +10001882 /* Compression needs to share state across borders */
1883 mm_init_compression(mon->m_zlib);
1884 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001885
1886 return mon;
1887}
1888
1889void
1890monitor_reinit(struct monitor *mon)
1891{
1892 int pair[2];
1893
1894 monitor_socketpair(pair);
1895
1896 mon->m_recvfd = pair[0];
1897 mon->m_sendfd = pair[1];
1898}
Darren Tucker0efd1552003-08-26 11:49:55 +10001899
1900#ifdef GSSAPI
1901int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001902mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001903{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001904 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001905 OM_uint32 major;
1906 u_int len;
1907
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001908 goid.elements = buffer_get_string(m, &len);
1909 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001910
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001911 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001912
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001913 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001914
1915 buffer_clear(m);
1916 buffer_put_int(m, major);
1917
Damien Miller6fd6def2005-11-05 15:07:05 +11001918 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001919
1920 /* Now we have a context, enable the step */
1921 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1922
1923 return (0);
1924}
1925
1926int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001927mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001928{
1929 gss_buffer_desc in;
1930 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001931 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001932 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001933 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001934
Darren Tucker600ad8d2003-08-26 12:10:48 +10001935 in.value = buffer_get_string(m, &len);
1936 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001937 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1938 xfree(in.value);
1939
1940 buffer_clear(m);
1941 buffer_put_int(m, major);
1942 buffer_put_string(m, out.value, out.length);
1943 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001944 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001945
1946 gss_release_buffer(&minor, &out);
1947
Damien Miller6fd6def2005-11-05 15:07:05 +11001948 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001949 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1950 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001951 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001952 }
1953 return (0);
1954}
1955
1956int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001957mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001958{
1959 gss_buffer_desc gssbuf, mic;
1960 OM_uint32 ret;
1961 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001962
Damien Miller0425d402003-11-17 22:18:21 +11001963 gssbuf.value = buffer_get_string(m, &len);
1964 gssbuf.length = len;
1965 mic.value = buffer_get_string(m, &len);
1966 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001967
Damien Miller0425d402003-11-17 22:18:21 +11001968 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001969
Damien Miller0425d402003-11-17 22:18:21 +11001970 xfree(gssbuf.value);
1971 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001972
Damien Miller0425d402003-11-17 22:18:21 +11001973 buffer_clear(m);
1974 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001975
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001976 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001977
Damien Miller0425d402003-11-17 22:18:21 +11001978 if (!GSS_ERROR(ret))
1979 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001980
Damien Miller0425d402003-11-17 22:18:21 +11001981 return (0);
1982}
1983
1984int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001985mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001986{
1987 int authenticated;
1988
1989 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1990
1991 buffer_clear(m);
1992 buffer_put_int(m, authenticated);
1993
1994 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001995 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001996
Damien Miller6fd6def2005-11-05 15:07:05 +11001997 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001998
1999 /* Monitor loop will terminate if authenticated */
2000 return (authenticated);
2001}
2002#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002003
2004#ifdef JPAKE
2005int
2006mm_answer_jpake_step1(int sock, Buffer *m)
2007{
2008 struct jpake_ctx *pctx;
2009 u_char *x3_proof, *x4_proof;
2010 u_int x3_proof_len, x4_proof_len;
2011
2012 if (!options.zero_knowledge_password_authentication)
2013 fatal("zero_knowledge_password_authentication disabled");
2014
2015 if (authctxt->jpake_ctx != NULL)
2016 fatal("%s: authctxt->jpake_ctx already set (%p)",
2017 __func__, authctxt->jpake_ctx);
2018 authctxt->jpake_ctx = pctx = jpake_new();
2019
2020 jpake_step1(pctx->grp,
2021 &pctx->server_id, &pctx->server_id_len,
2022 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2023 &x3_proof, &x3_proof_len,
2024 &x4_proof, &x4_proof_len);
2025
2026 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2027
2028 buffer_clear(m);
2029
2030 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2031 buffer_put_bignum2(m, pctx->g_x3);
2032 buffer_put_bignum2(m, pctx->g_x4);
2033 buffer_put_string(m, x3_proof, x3_proof_len);
2034 buffer_put_string(m, x4_proof, x4_proof_len);
2035
2036 debug3("%s: sending step1", __func__);
2037 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2038
2039 bzero(x3_proof, x3_proof_len);
2040 bzero(x4_proof, x4_proof_len);
2041 xfree(x3_proof);
2042 xfree(x4_proof);
2043
2044 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2045 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2046
2047 return 0;
2048}
2049
2050int
2051mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2052{
2053 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2054 char *hash_scheme, *salt;
2055
2056 if (pctx == NULL)
2057 fatal("%s: pctx == NULL", __func__);
2058
2059 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2060
2061 buffer_clear(m);
2062 /* pctx->s is sensitive, not returned to slave */
2063 buffer_put_cstring(m, hash_scheme);
2064 buffer_put_cstring(m, salt);
2065
2066 debug3("%s: sending pwdata", __func__);
2067 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2068
2069 bzero(hash_scheme, strlen(hash_scheme));
2070 bzero(salt, strlen(salt));
2071 xfree(hash_scheme);
2072 xfree(salt);
2073
2074 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2075
2076 return 0;
2077}
2078
2079int
2080mm_answer_jpake_step2(int sock, Buffer *m)
2081{
2082 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2083 u_char *x1_proof, *x2_proof, *x4_s_proof;
2084 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2085
2086 if (pctx == NULL)
2087 fatal("%s: pctx == NULL", __func__);
2088
2089 if ((pctx->g_x1 = BN_new()) == NULL ||
2090 (pctx->g_x2 = BN_new()) == NULL)
2091 fatal("%s: BN_new", __func__);
2092 buffer_get_bignum2(m, pctx->g_x1);
2093 buffer_get_bignum2(m, pctx->g_x2);
2094 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2095 x1_proof = buffer_get_string(m, &x1_proof_len);
2096 x2_proof = buffer_get_string(m, &x2_proof_len);
2097
2098 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2099 pctx->g_x1, pctx->g_x2, pctx->x4,
2100 pctx->client_id, pctx->client_id_len,
2101 pctx->server_id, pctx->server_id_len,
2102 x1_proof, x1_proof_len,
2103 x2_proof, x2_proof_len,
2104 &pctx->b,
2105 &x4_s_proof, &x4_s_proof_len);
2106
2107 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2108
2109 bzero(x1_proof, x1_proof_len);
2110 bzero(x2_proof, x2_proof_len);
2111 xfree(x1_proof);
2112 xfree(x2_proof);
2113
2114 buffer_clear(m);
2115
2116 buffer_put_bignum2(m, pctx->b);
2117 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2118
2119 debug3("%s: sending step2", __func__);
2120 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2121
2122 bzero(x4_s_proof, x4_s_proof_len);
2123 xfree(x4_s_proof);
2124
2125 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2126
2127 return 0;
2128}
2129
2130int
2131mm_answer_jpake_key_confirm(int sock, Buffer *m)
2132{
2133 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2134 u_char *x2_s_proof;
2135 u_int x2_s_proof_len;
2136
2137 if (pctx == NULL)
2138 fatal("%s: pctx == NULL", __func__);
2139
2140 if ((pctx->a = BN_new()) == NULL)
2141 fatal("%s: BN_new", __func__);
2142 buffer_get_bignum2(m, pctx->a);
2143 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2144
2145 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2146 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2147 pctx->server_id, pctx->server_id_len,
2148 pctx->client_id, pctx->client_id_len,
2149 session_id2, session_id2_len,
2150 x2_s_proof, x2_s_proof_len,
2151 &pctx->k,
2152 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2153
2154 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2155
2156 bzero(x2_s_proof, x2_s_proof_len);
2157 buffer_clear(m);
2158
2159 /* pctx->k is sensitive, not sent */
2160 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2161
2162 debug3("%s: sending confirmation hash", __func__);
2163 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2164
2165 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2166
2167 return 0;
2168}
2169
2170int
2171mm_answer_jpake_check_confirm(int sock, Buffer *m)
2172{
2173 int authenticated = 0;
2174 u_char *peer_confirm_hash;
2175 u_int peer_confirm_hash_len;
2176 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2177
2178 if (pctx == NULL)
2179 fatal("%s: pctx == NULL", __func__);
2180
2181 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2182
2183 authenticated = jpake_check_confirm(pctx->k,
2184 pctx->client_id, pctx->client_id_len,
2185 session_id2, session_id2_len,
2186 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2187
2188 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2189
2190 bzero(peer_confirm_hash, peer_confirm_hash_len);
2191 xfree(peer_confirm_hash);
2192
2193 buffer_clear(m);
2194 buffer_put_int(m, authenticated);
2195
2196 debug3("%s: sending result %d", __func__, authenticated);
2197 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2198
2199 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2200
2201 auth_method = "jpake-01@openssh.com";
2202 return authenticated;
2203}
2204
2205#endif /* JPAKE */