blob: 4479e0a0304241aea7e53dfe04283e57a3ff7193 [file] [log] [blame]
Damien Millerd8478b62011-05-29 21:39:36 +10001/* $OpenBSD: monitor.c,v 1.113 2011/05/23 03:30:07 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;
Damien Millerd8478b62011-05-29 21:39:36 +1000635 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000637 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638
639 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000640 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641
Darren Tuckerb09b6772004-06-22 15:06:46 +1000642 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643
Darren Tuckerb09b6772004-06-22 15:06:46 +1000644 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000645
Darren Tuckerb09b6772004-06-22 15:06:46 +1000646 authctxt->user = xstrdup(username);
647 setproctitle("%s [priv]", pwent ? username : "unknown");
648 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 buffer_clear(m);
651
652 if (pwent == NULL) {
653 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100654 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655 goto out;
656 }
657
658 allowed = 1;
659 authctxt->pw = pwent;
660 authctxt->valid = 1;
661
662 buffer_put_char(m, 1);
663 buffer_put_string(m, pwent, sizeof(struct passwd));
664 buffer_put_cstring(m, pwent->pw_name);
665 buffer_put_cstring(m, "*");
666 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000667#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000669#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670 buffer_put_cstring(m, pwent->pw_dir);
671 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100672
673 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100674 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000675
676#define M_CP_STROPT(x) do { \
677 if (options.x != NULL) \
678 buffer_put_cstring(m, options.x); \
679 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000680#define M_CP_STRARRAYOPT(x, nx) do { \
681 for (i = 0; i < options.nx; i++) \
682 buffer_put_cstring(m, options.x[i]); \
683 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000684 /* See comment in servconf.h */
685 COPY_MATCH_STRING_OPTS();
686#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000687#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000688
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000689 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000690 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691
692 /* For SSHv1 allow authentication now */
693 if (!compat20)
694 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000695 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000696 /* Allow service/style information on the auth context */
697 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000698 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
699 }
Damien Millera33501b2002-05-08 12:24:42 +1000700#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000701 if (options.use_pam)
702 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000703#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704
705 return (0);
706}
707
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000708int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000709{
710 char *banner;
711
712 buffer_clear(m);
713 banner = auth2_read_banner();
714 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000715 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000716
717 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000718 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000719
720 return (0);
721}
722
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000724mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000725{
726 monitor_permit_authentications(1);
727
728 authctxt->service = buffer_get_string(m, NULL);
729 authctxt->style = buffer_get_string(m, NULL);
730 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000731 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732
733 if (strlen(authctxt->style) == 0) {
734 xfree(authctxt->style);
735 authctxt->style = NULL;
736 }
737
738 return (0);
739}
740
741int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000742mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743{
744 static int call_count;
745 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000746 int authenticated;
747 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748
749 passwd = buffer_get_string(m, &plen);
750 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000751 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000752 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 memset(passwd, 0, strlen(passwd));
754 xfree(passwd);
755
756 buffer_clear(m);
757 buffer_put_int(m, authenticated);
758
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000759 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000760 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000761
762 call_count++;
763 if (plen == 0 && call_count == 1)
764 auth_method = "none";
765 else
766 auth_method = "password";
767
768 /* Causes monitor loop to terminate if authenticated */
769 return (authenticated);
770}
771
772#ifdef BSD_AUTH
773int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000774mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775{
776 char *name, *infotxt;
777 u_int numprompts;
778 u_int *echo_on;
779 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100780 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781
Damien Millerb7df3af2003-02-24 11:55:46 +1100782 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
783 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784
785 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100786 buffer_put_int(m, success);
787 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788 buffer_put_cstring(m, prompts[0]);
789
Damien Millerb7df3af2003-02-24 11:55:46 +1100790 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000791 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792
Damien Millerb7df3af2003-02-24 11:55:46 +1100793 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794 xfree(name);
795 xfree(infotxt);
796 xfree(prompts);
797 xfree(echo_on);
798 }
799
800 return (0);
801}
802
803int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000804mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805{
806 char *response;
807 int authok;
808
809 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000810 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811
812 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000813 authok = options.challenge_response_authentication &&
814 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000816 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000817 xfree(response);
818
819 buffer_clear(m);
820 buffer_put_int(m, authok);
821
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000822 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000823 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000824
825 auth_method = "bsdauth";
826
827 return (authok != 0);
828}
829#endif
830
831#ifdef SKEY
832int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000833mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000834{
835 struct skey skey;
836 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100837 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000839 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
840 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000841
842 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100843 buffer_put_int(m, success);
844 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845 buffer_put_cstring(m, challenge);
846
Damien Millerb7df3af2003-02-24 11:55:46 +1100847 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000848 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849
850 return (0);
851}
852
853int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000854mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000855{
856 char *response;
857 int authok;
858
859 response = buffer_get_string(m, NULL);
860
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000861 authok = (options.challenge_response_authentication &&
862 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000863 skey_haskey(authctxt->pw->pw_name) == 0 &&
864 skey_passcheck(authctxt->pw->pw_name, response) != -1);
865
866 xfree(response);
867
868 buffer_clear(m);
869 buffer_put_int(m, authok);
870
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000871 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000872 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000873
874 auth_method = "skey";
875
876 return (authok != 0);
877}
878#endif
879
Damien Miller79418552002-04-23 20:28:48 +1000880#ifdef USE_PAM
881int
Darren Tucker7eda5922004-06-22 13:22:50 +1000882mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000883{
Damien Miller4e448a32003-05-14 15:11:48 +1000884 if (!options.use_pam)
885 fatal("UsePAM not set, but ended up in %s anyway", __func__);
886
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100887 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000888
Damien Miller1f499fd2003-08-25 13:08:49 +1000889 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
890
Damien Miller79418552002-04-23 20:28:48 +1000891 return (0);
892}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000893
Damien Miller1f499fd2003-08-25 13:08:49 +1000894int
Darren Tucker7eda5922004-06-22 13:22:50 +1000895mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000896{
897 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100898
Damien Miller1f499fd2003-08-25 13:08:49 +1000899 if (!options.use_pam)
900 fatal("UsePAM not set, but ended up in %s anyway", __func__);
901
902 ret = do_pam_account();
903
904 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000905 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000906
Darren Tucker7eda5922004-06-22 13:22:50 +1000907 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000908
909 return (ret);
910}
911
Damien Miller4f9f42a2003-05-10 19:28:02 +1000912static void *sshpam_ctxt, *sshpam_authok;
913extern KbdintDevice sshpam_device;
914
915int
Darren Tucker7eda5922004-06-22 13:22:50 +1000916mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000917{
918
919 debug3("%s", __func__);
920 authctxt->user = buffer_get_string(m, NULL);
921 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
922 sshpam_authok = NULL;
923 buffer_clear(m);
924 if (sshpam_ctxt != NULL) {
925 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
926 buffer_put_int(m, 1);
927 } else {
928 buffer_put_int(m, 0);
929 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000930 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000931 return (0);
932}
933
934int
Darren Tucker7eda5922004-06-22 13:22:50 +1000935mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000936{
Darren Tucker8b7a0552010-08-03 15:50:16 +1000937 char *name = NULL, *info = NULL, **prompts = NULL;
938 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +1000939 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000940
941 debug3("%s", __func__);
942 sshpam_authok = NULL;
943 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
944 if (ret == 0 && num == 0)
945 sshpam_authok = sshpam_ctxt;
946 if (num > 1 || name == NULL || info == NULL)
947 ret = -1;
948 buffer_clear(m);
949 buffer_put_int(m, ret);
950 buffer_put_cstring(m, name);
951 xfree(name);
952 buffer_put_cstring(m, info);
953 xfree(info);
954 buffer_put_int(m, num);
955 for (i = 0; i < num; ++i) {
956 buffer_put_cstring(m, prompts[i]);
957 xfree(prompts[i]);
958 buffer_put_int(m, echo_on[i]);
959 }
960 if (prompts != NULL)
961 xfree(prompts);
962 if (echo_on != NULL)
963 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000964 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000965 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000966 return (0);
967}
968
969int
Darren Tucker7eda5922004-06-22 13:22:50 +1000970mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000971{
972 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000973 u_int i, num;
974 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000975
976 debug3("%s", __func__);
977 sshpam_authok = NULL;
978 num = buffer_get_int(m);
979 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000980 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000981 for (i = 0; i < num; ++i)
982 resp[i] = buffer_get_string(m, NULL);
983 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
984 for (i = 0; i < num; ++i)
985 xfree(resp[i]);
986 xfree(resp);
987 } else {
988 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
989 }
990 buffer_clear(m);
991 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000992 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000993 auth_method = "keyboard-interactive/pam";
994 if (ret == 0)
995 sshpam_authok = sshpam_ctxt;
996 return (0);
997}
998
999int
Darren Tucker7eda5922004-06-22 13:22:50 +10001000mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001001{
1002
1003 debug3("%s", __func__);
1004 (sshpam_device.free_ctx)(sshpam_ctxt);
1005 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001006 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +10001007 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001008 return (sshpam_authok == sshpam_ctxt);
1009}
Damien Miller79418552002-04-23 20:28:48 +10001010#endif
1011
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001013mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014{
1015 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001016 char *cuser, *chost;
1017 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 u_int bloblen;
1019 enum mm_keytype type = 0;
1020 int allowed = 0;
1021
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023
1024 type = buffer_get_int(m);
1025 cuser = buffer_get_string(m, NULL);
1026 chost = buffer_get_string(m, NULL);
1027 blob = buffer_get_string(m, &bloblen);
1028
1029 key = key_from_blob(blob, bloblen);
1030
1031 if ((compat20 && type == MM_RSAHOSTKEY) ||
1032 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001033 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001035 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001036
Damien Miller3e3b5142003-11-17 21:13:40 +11001037 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001038 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001040 allowed = options.pubkey_authentication &&
1041 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001042 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001043 if (options.pubkey_authentication && allowed != 1)
1044 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045 break;
1046 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001047 allowed = options.hostbased_authentication &&
1048 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001050 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051 break;
1052 case MM_RSAHOSTKEY:
1053 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001054 allowed = options.rhosts_rsa_authentication &&
1055 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001057 if (options.rhosts_rsa_authentication && allowed != 1)
1058 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001059 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060 break;
1061 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001062 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063 break;
1064 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001065 }
Damien Miller00111382003-03-10 11:21:17 +11001066 if (key != NULL)
1067 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068
1069 /* clear temporarily storage (used by verify) */
1070 monitor_reset_key_state();
1071
1072 if (allowed) {
1073 /* Save temporarily for comparison in verify */
1074 key_blob = blob;
1075 key_bloblen = bloblen;
1076 key_blobtype = type;
1077 hostbased_cuser = cuser;
1078 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001079 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001080 /* Log failed attempt */
1081 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001082 xfree(blob);
1083 xfree(cuser);
1084 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001085 }
1086
1087 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001088 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001089
1090 buffer_clear(m);
1091 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001092 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001093
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001094 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001095
1096 if (type == MM_RSAHOSTKEY)
1097 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1098
1099 return (0);
1100}
1101
1102static int
1103monitor_valid_userblob(u_char *data, u_int datalen)
1104{
1105 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001106 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107 u_int len;
1108 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001109
1110 buffer_init(&b);
1111 buffer_append(&b, data, datalen);
1112
1113 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001114 p = buffer_ptr(&b);
1115 len = buffer_len(&b);
1116 if ((session_id2 == NULL) ||
1117 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001118 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001119 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001120 buffer_consume(&b, session_id2_len);
1121 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001122 p = buffer_get_string(&b, &len);
1123 if ((session_id2 == NULL) ||
1124 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001125 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001126 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001127 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001128 }
1129 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1130 fail++;
1131 p = buffer_get_string(&b, NULL);
1132 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001133 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 authctxt->user, p);
1135 fail++;
1136 }
1137 xfree(p);
1138 buffer_skip_string(&b);
1139 if (datafellows & SSH_BUG_PKAUTH) {
1140 if (!buffer_get_char(&b))
1141 fail++;
1142 } else {
1143 p = buffer_get_string(&b, NULL);
1144 if (strcmp("publickey", p) != 0)
1145 fail++;
1146 xfree(p);
1147 if (!buffer_get_char(&b))
1148 fail++;
1149 buffer_skip_string(&b);
1150 }
1151 buffer_skip_string(&b);
1152 if (buffer_len(&b) != 0)
1153 fail++;
1154 buffer_free(&b);
1155 return (fail == 0);
1156}
1157
1158static int
Damien Millera10f5612002-09-12 09:49:15 +10001159monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1160 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001161{
1162 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001163 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164 u_int len;
1165 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166
1167 buffer_init(&b);
1168 buffer_append(&b, data, datalen);
1169
Ben Lindstromf67e0772002-06-06 20:58:19 +00001170 p = buffer_get_string(&b, &len);
1171 if ((session_id2 == NULL) ||
1172 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001173 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001175 xfree(p);
1176
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1178 fail++;
1179 p = buffer_get_string(&b, NULL);
1180 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001181 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001182 authctxt->user, p);
1183 fail++;
1184 }
1185 xfree(p);
1186 buffer_skip_string(&b); /* service */
1187 p = buffer_get_string(&b, NULL);
1188 if (strcmp(p, "hostbased") != 0)
1189 fail++;
1190 xfree(p);
1191 buffer_skip_string(&b); /* pkalg */
1192 buffer_skip_string(&b); /* pkblob */
1193
1194 /* verify client host, strip trailing dot if necessary */
1195 p = buffer_get_string(&b, NULL);
1196 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1197 p[len - 1] = '\0';
1198 if (strcmp(p, chost) != 0)
1199 fail++;
1200 xfree(p);
1201
1202 /* verify client user */
1203 p = buffer_get_string(&b, NULL);
1204 if (strcmp(p, cuser) != 0)
1205 fail++;
1206 xfree(p);
1207
1208 if (buffer_len(&b) != 0)
1209 fail++;
1210 buffer_free(&b);
1211 return (fail == 0);
1212}
1213
1214int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001215mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216{
1217 Key *key;
1218 u_char *signature, *data, *blob;
1219 u_int signaturelen, datalen, bloblen;
1220 int verified = 0;
1221 int valid_data = 0;
1222
1223 blob = buffer_get_string(m, &bloblen);
1224 signature = buffer_get_string(m, &signaturelen);
1225 data = buffer_get_string(m, &datalen);
1226
1227 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001228 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001229 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230
1231 key = key_from_blob(blob, bloblen);
1232 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001233 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001234
1235 switch (key_blobtype) {
1236 case MM_USERKEY:
1237 valid_data = monitor_valid_userblob(data, datalen);
1238 break;
1239 case MM_HOSTKEY:
1240 valid_data = monitor_valid_hostbasedblob(data, datalen,
1241 hostbased_cuser, hostbased_chost);
1242 break;
1243 default:
1244 valid_data = 0;
1245 break;
1246 }
1247 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249
1250 verified = key_verify(key, signature, signaturelen, data, datalen);
1251 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001252 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253
1254 key_free(key);
1255 xfree(blob);
1256 xfree(signature);
1257 xfree(data);
1258
Ben Lindstrome1c09122002-06-23 00:38:24 +00001259 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1260
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 monitor_reset_key_state();
1262
1263 buffer_clear(m);
1264 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001265 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266
Darren Tuckerfbba7352006-11-07 23:16:08 +11001267 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268}
1269
1270static void
1271mm_record_login(Session *s, struct passwd *pw)
1272{
1273 socklen_t fromlen;
1274 struct sockaddr_storage from;
1275
1276 /*
1277 * Get IP address of client. If the connection is not a socket, let
1278 * the address be 0.0.0.0.
1279 */
1280 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001281 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001282 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001284 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001285 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001286 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 }
1288 }
1289 /* Record that there was a login on that tty from the remote host. */
1290 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001291 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001292 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293}
1294
1295static void
1296mm_session_close(Session *s)
1297{
Damien Miller04bd8b02003-05-25 14:38:33 +10001298 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001300 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301 session_pty_cleanup2(s);
1302 }
Damien Miller7207f642008-05-19 15:34:50 +10001303 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304}
1305
1306int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001307mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001308{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001309 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310 Session *s;
1311 int res, fd0;
1312
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001313 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314
1315 buffer_clear(m);
1316 s = session_new();
1317 if (s == NULL)
1318 goto error;
1319 s->authctxt = authctxt;
1320 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001321 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1323 if (res == 0)
1324 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325 pty_setowner(authctxt->pw, s->tty);
1326
1327 buffer_put_int(m, 1);
1328 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
1330 /* We need to trick ttyslot */
1331 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001332 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001333
1334 mm_record_login(s, authctxt->pw);
1335
1336 /* Now we can close the file descriptor again */
1337 close(0);
1338
Darren Tucker09991742004-07-17 17:05:14 +10001339 /* send messages generated by record_login */
1340 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1341 buffer_clear(&loginmsg);
1342
1343 mm_request_send(sock, MONITOR_ANS_PTY, m);
1344
Damien Miller54fd7cf2007-09-17 12:04:08 +10001345 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1346 mm_send_fd(sock, s->ttyfd) == -1)
1347 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001348
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349 /* make sure nothing uses fd 0 */
1350 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001351 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001352 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001353 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
1355 /* slave is not needed */
1356 close(s->ttyfd);
1357 s->ttyfd = s->ptyfd;
1358 /* no need to dup() because nobody closes ptyfd */
1359 s->ptymaster = s->ptyfd;
1360
Damien Miller9ab00b42006-08-05 12:40:11 +10001361 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362
1363 return (0);
1364
1365 error:
1366 if (s != NULL)
1367 mm_session_close(s);
1368 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001369 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001370 return (0);
1371}
1372
1373int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001374mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001375{
1376 Session *s;
1377 char *tty;
1378
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001379 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380
1381 tty = buffer_get_string(m, NULL);
1382 if ((s = session_by_tty(tty)) != NULL)
1383 mm_session_close(s);
1384 buffer_clear(m);
1385 xfree(tty);
1386 return (0);
1387}
1388
1389int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001390mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391{
1392 BIGNUM *p;
1393 int rsafail;
1394
1395 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001396 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397
1398 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001399 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 buffer_get_bignum2(m, p);
1402
1403 rsafail = ssh1_session_key(p);
1404
1405 buffer_clear(m);
1406 buffer_put_int(m, rsafail);
1407 buffer_put_bignum2(m, p);
1408
1409 BN_clear_free(p);
1410
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001411 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412
1413 /* Turn on permissions for sessid passing */
1414 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1415
1416 return (0);
1417}
1418
1419int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001420mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001421{
1422 int i;
1423
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001424 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425
1426 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 for (i = 0; i < 16; i++)
1429 session_id[i] = buffer_get_char(m);
1430
1431 /* Turn on permissions for getpwnam */
1432 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1433
1434 return (0);
1435}
1436
1437int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001438mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439{
1440 BIGNUM *client_n;
1441 Key *key = NULL;
1442 u_char *blob = NULL;
1443 u_int blen = 0;
1444 int allowed = 0;
1445
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001446 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447
Damien Miller7a8f5b32006-03-31 23:14:23 +11001448 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001449 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001450 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001451 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452 buffer_get_bignum2(m, client_n);
1453 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1454 BN_clear_free(client_n);
1455 }
1456 buffer_clear(m);
1457 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001458 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459
1460 /* clear temporarily storage (used by generate challenge) */
1461 monitor_reset_key_state();
1462
1463 if (allowed && key != NULL) {
1464 key->type = KEY_RSA; /* cheat for key_to_blob */
1465 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001466 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001467 buffer_put_string(m, blob, blen);
1468
1469 /* Save temporarily for comparison in verify */
1470 key_blob = blob;
1471 key_bloblen = blen;
1472 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001473 }
Damien Miller00111382003-03-10 11:21:17 +11001474 if (key != NULL)
1475 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001477 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478
1479 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1480 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1481 return (0);
1482}
1483
1484int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001485mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486{
1487 Key *key = NULL;
1488 u_char *blob;
1489 u_int blen;
1490
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 blob = buffer_get_string(m, &blen);
1496 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001497 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001499 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001501 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001502 if (key->type != KEY_RSA)
1503 fatal("%s: received bad key type %d", __func__, key->type);
1504 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 if (ssh1_challenge)
1506 BN_clear_free(ssh1_challenge);
1507 ssh1_challenge = auth_rsa_generate_challenge(key);
1508
1509 buffer_clear(m);
1510 buffer_put_bignum2(m, ssh1_challenge);
1511
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001512 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001513 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514
1515 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001516
1517 xfree(blob);
1518 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519 return (0);
1520}
1521
1522int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001523mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524{
1525 Key *key = NULL;
1526 u_char *blob, *response;
1527 u_int blen, len;
1528 int success;
1529
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001530 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001531
1532 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001533 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001535 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001536
1537 blob = buffer_get_string(m, &blen);
1538 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001539 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001540 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001541 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001543 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544 response = buffer_get_string(m, &len);
1545 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001546 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1548
Damien Miller00111382003-03-10 11:21:17 +11001549 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001550 key_free(key);
1551 xfree(response);
1552
1553 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1554
1555 /* reset state */
1556 BN_clear_free(ssh1_challenge);
1557 ssh1_challenge = NULL;
1558 monitor_reset_key_state();
1559
1560 buffer_clear(m);
1561 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001562 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001563
1564 return (success);
1565}
1566
1567int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001568mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001570 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571 int res, status;
1572
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001573 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001574
1575 /* The child is terminating */
1576 session_destroy_all(&mm_session_close);
1577
Darren Tucker52358d62008-03-11 22:58:25 +11001578#ifdef USE_PAM
1579 if (options.use_pam)
1580 sshpam_cleanup();
1581#endif
1582
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001583 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001584 if (errno != EINTR)
1585 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001586
1587 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1588
1589 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001590 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001591}
1592
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001593#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001594/* Report that an audit event occurred */
1595int
1596mm_answer_audit_event(int socket, Buffer *m)
1597{
1598 ssh_audit_event_t event;
1599
1600 debug3("%s entering", __func__);
1601
1602 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001603 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001604 case SSH_AUTH_FAIL_PUBKEY:
1605 case SSH_AUTH_FAIL_HOSTBASED:
1606 case SSH_AUTH_FAIL_GSSAPI:
1607 case SSH_LOGIN_EXCEED_MAXTRIES:
1608 case SSH_LOGIN_ROOT_DENIED:
1609 case SSH_CONNECTION_CLOSE:
1610 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001611 audit_event(event);
1612 break;
1613 default:
1614 fatal("Audit event type %d not permitted", event);
1615 }
1616
1617 return (0);
1618}
1619
1620int
1621mm_answer_audit_command(int socket, Buffer *m)
1622{
1623 u_int len;
1624 char *cmd;
1625
1626 debug3("%s entering", __func__);
1627 cmd = buffer_get_string(m, &len);
1628 /* sanity check command, if so how? */
1629 audit_run_command(cmd);
1630 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001631 return (0);
1632}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001633#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001634
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001636monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001637{
1638 if (compat20) {
1639 set_newkeys(MODE_IN);
1640 set_newkeys(MODE_OUT);
1641 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001643 packet_set_encryption_key(child_state.ssh1key,
1644 child_state.ssh1keylen, child_state.ssh1cipher);
1645 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646 }
1647
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001648 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649 packet_set_keycontext(MODE_OUT, child_state.keyout);
1650 xfree(child_state.keyout);
1651 packet_set_keycontext(MODE_IN, child_state.keyin);
1652 xfree(child_state.keyin);
1653
1654 if (!compat20) {
1655 packet_set_iv(MODE_OUT, child_state.ivout);
1656 xfree(child_state.ivout);
1657 packet_set_iv(MODE_IN, child_state.ivin);
1658 xfree(child_state.ivin);
1659 }
1660
1661 memcpy(&incoming_stream, &child_state.incoming,
1662 sizeof(incoming_stream));
1663 memcpy(&outgoing_stream, &child_state.outgoing,
1664 sizeof(outgoing_stream));
1665
1666 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001667 if (options.compression)
1668 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669
1670 /* Network I/O buffers */
1671 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001672 buffer_clear(packet_get_input());
1673 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 memset(child_state.input, 0, child_state.ilen);
1675 xfree(child_state.input);
1676
Darren Tuckerf7288d72009-06-21 18:12:20 +10001677 buffer_clear(packet_get_output());
1678 buffer_append(packet_get_output(), child_state.output,
1679 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001680 memset(child_state.output, 0, child_state.olen);
1681 xfree(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001682
1683 /* Roaming */
1684 if (compat20)
1685 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686}
1687
1688static Kex *
1689mm_get_kex(Buffer *m)
1690{
1691 Kex *kex;
1692 void *blob;
1693 u_int bloblen;
1694
Damien Miller07d86be2006-03-26 14:19:21 +11001695 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001696 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001697 if (session_id2 == NULL ||
1698 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001699 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001700 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001701 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001702 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001703 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001704 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001705 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001706 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001707 kex->server = 1;
1708 kex->hostkey_type = buffer_get_int(m);
1709 kex->kex_type = buffer_get_int(m);
1710 blob = buffer_get_string(m, &bloblen);
1711 buffer_init(&kex->my);
1712 buffer_append(&kex->my, blob, bloblen);
1713 xfree(blob);
1714 blob = buffer_get_string(m, &bloblen);
1715 buffer_init(&kex->peer);
1716 buffer_append(&kex->peer, blob, bloblen);
1717 xfree(blob);
1718 kex->done = 1;
1719 kex->flags = buffer_get_int(m);
1720 kex->client_version_string = buffer_get_string(m, NULL);
1721 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001722 kex->load_host_public_key=&get_hostkey_public_by_type;
1723 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724 kex->host_key_index=&get_hostkey_index;
1725
1726 return (kex);
1727}
1728
1729/* This function requries careful sanity checking */
1730
1731void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001732mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001733{
1734 Buffer m;
1735 u_char *blob, *p;
1736 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001737 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001738 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001740 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741
1742 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001743 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 if (!compat20) {
1745 child_state.ssh1protoflags = buffer_get_int(&m);
1746 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001747 child_state.ssh1key = buffer_get_string(&m,
1748 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749 child_state.ivout = buffer_get_string(&m,
1750 &child_state.ivoutlen);
1751 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1752 goto skip;
1753 } else {
1754 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001755 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001756 }
1757
1758 blob = buffer_get_string(&m, &bloblen);
1759 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1760 xfree(blob);
1761
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001762 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001763 blob = buffer_get_string(&m, &bloblen);
1764 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1765 xfree(blob);
1766
1767 /* Now get sequence numbers for the packets */
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_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001773 seqnr = buffer_get_int(&m);
1774 blocks = buffer_get_int64(&m);
1775 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001776 bytes = buffer_get_int64(&m);
1777 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001778
1779 skip:
1780 /* Get the key context */
1781 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1782 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1783
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001784 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001785 /* Get compression state */
1786 p = buffer_get_string(&m, &plen);
1787 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001788 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001789 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1790 xfree(p);
1791
1792 p = buffer_get_string(&m, &plen);
1793 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001794 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1796 xfree(p);
1797
1798 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001799 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001800 child_state.input = buffer_get_string(&m, &child_state.ilen);
1801 child_state.output = buffer_get_string(&m, &child_state.olen);
1802
Darren Tuckerc5564e12009-06-21 18:53:53 +10001803 /* Roaming */
1804 if (compat20) {
1805 child_state.sent_bytes = buffer_get_int64(&m);
1806 child_state.recv_bytes = buffer_get_int64(&m);
1807 }
1808
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001809 buffer_free(&m);
1810}
1811
1812
1813/* Allocation functions for zlib */
1814void *
1815mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1816{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001817 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001818 void *address;
1819
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001820 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001821 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1822
1823 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001824
1825 return (address);
1826}
1827
1828void
1829mm_zfree(struct mm_master *mm, void *address)
1830{
1831 mm_free(mm, address);
1832}
1833
1834void
1835mm_init_compression(struct mm_master *mm)
1836{
1837 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1838 outgoing_stream.zfree = (free_func)mm_zfree;
1839 outgoing_stream.opaque = mm;
1840
1841 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1842 incoming_stream.zfree = (free_func)mm_zfree;
1843 incoming_stream.opaque = mm;
1844}
1845
1846/* XXX */
1847
1848#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001849 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001850 fatal("fcntl(%d, F_SETFD)", x); \
1851} while (0)
1852
1853static void
1854monitor_socketpair(int *pair)
1855{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001856#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001857 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001858 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001859#else
1860 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001861 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001862#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001863 FD_CLOSEONEXEC(pair[0]);
1864 FD_CLOSEONEXEC(pair[1]);
1865}
1866
1867#define MM_MEMSIZE 65536
1868
1869struct monitor *
1870monitor_init(void)
1871{
1872 struct monitor *mon;
1873 int pair[2];
1874
Damien Miller07d86be2006-03-26 14:19:21 +11001875 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001876
1877 monitor_socketpair(pair);
1878
1879 mon->m_recvfd = pair[0];
1880 mon->m_sendfd = pair[1];
1881
1882 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001883 if (options.compression) {
1884 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1885 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001886
Damien Miller2d6b8332002-06-21 15:59:49 +10001887 /* Compression needs to share state across borders */
1888 mm_init_compression(mon->m_zlib);
1889 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001890
1891 return mon;
1892}
1893
1894void
1895monitor_reinit(struct monitor *mon)
1896{
1897 int pair[2];
1898
1899 monitor_socketpair(pair);
1900
1901 mon->m_recvfd = pair[0];
1902 mon->m_sendfd = pair[1];
1903}
Darren Tucker0efd1552003-08-26 11:49:55 +10001904
1905#ifdef GSSAPI
1906int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001907mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001908{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001909 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001910 OM_uint32 major;
1911 u_int len;
1912
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001913 goid.elements = buffer_get_string(m, &len);
1914 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001915
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001916 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001917
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001918 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001919
1920 buffer_clear(m);
1921 buffer_put_int(m, major);
1922
Damien Miller6fd6def2005-11-05 15:07:05 +11001923 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001924
1925 /* Now we have a context, enable the step */
1926 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1927
1928 return (0);
1929}
1930
1931int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001932mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001933{
1934 gss_buffer_desc in;
1935 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001936 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001937 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001938 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001939
Darren Tucker600ad8d2003-08-26 12:10:48 +10001940 in.value = buffer_get_string(m, &len);
1941 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001942 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1943 xfree(in.value);
1944
1945 buffer_clear(m);
1946 buffer_put_int(m, major);
1947 buffer_put_string(m, out.value, out.length);
1948 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001949 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001950
1951 gss_release_buffer(&minor, &out);
1952
Damien Miller6fd6def2005-11-05 15:07:05 +11001953 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001954 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1955 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001956 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001957 }
1958 return (0);
1959}
1960
1961int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001962mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001963{
1964 gss_buffer_desc gssbuf, mic;
1965 OM_uint32 ret;
1966 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001967
Damien Miller0425d402003-11-17 22:18:21 +11001968 gssbuf.value = buffer_get_string(m, &len);
1969 gssbuf.length = len;
1970 mic.value = buffer_get_string(m, &len);
1971 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001972
Damien Miller0425d402003-11-17 22:18:21 +11001973 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001974
Damien Miller0425d402003-11-17 22:18:21 +11001975 xfree(gssbuf.value);
1976 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001977
Damien Miller0425d402003-11-17 22:18:21 +11001978 buffer_clear(m);
1979 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001980
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001981 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001982
Damien Miller0425d402003-11-17 22:18:21 +11001983 if (!GSS_ERROR(ret))
1984 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001985
Damien Miller0425d402003-11-17 22:18:21 +11001986 return (0);
1987}
1988
1989int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001990mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001991{
1992 int authenticated;
1993
1994 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1995
1996 buffer_clear(m);
1997 buffer_put_int(m, authenticated);
1998
1999 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002000 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002001
Damien Miller6fd6def2005-11-05 15:07:05 +11002002 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002003
2004 /* Monitor loop will terminate if authenticated */
2005 return (authenticated);
2006}
2007#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002008
2009#ifdef JPAKE
2010int
2011mm_answer_jpake_step1(int sock, Buffer *m)
2012{
2013 struct jpake_ctx *pctx;
2014 u_char *x3_proof, *x4_proof;
2015 u_int x3_proof_len, x4_proof_len;
2016
2017 if (!options.zero_knowledge_password_authentication)
2018 fatal("zero_knowledge_password_authentication disabled");
2019
2020 if (authctxt->jpake_ctx != NULL)
2021 fatal("%s: authctxt->jpake_ctx already set (%p)",
2022 __func__, authctxt->jpake_ctx);
2023 authctxt->jpake_ctx = pctx = jpake_new();
2024
2025 jpake_step1(pctx->grp,
2026 &pctx->server_id, &pctx->server_id_len,
2027 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2028 &x3_proof, &x3_proof_len,
2029 &x4_proof, &x4_proof_len);
2030
2031 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2032
2033 buffer_clear(m);
2034
2035 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2036 buffer_put_bignum2(m, pctx->g_x3);
2037 buffer_put_bignum2(m, pctx->g_x4);
2038 buffer_put_string(m, x3_proof, x3_proof_len);
2039 buffer_put_string(m, x4_proof, x4_proof_len);
2040
2041 debug3("%s: sending step1", __func__);
2042 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2043
2044 bzero(x3_proof, x3_proof_len);
2045 bzero(x4_proof, x4_proof_len);
2046 xfree(x3_proof);
2047 xfree(x4_proof);
2048
2049 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2050 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2051
2052 return 0;
2053}
2054
2055int
2056mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2057{
2058 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2059 char *hash_scheme, *salt;
2060
2061 if (pctx == NULL)
2062 fatal("%s: pctx == NULL", __func__);
2063
2064 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2065
2066 buffer_clear(m);
2067 /* pctx->s is sensitive, not returned to slave */
2068 buffer_put_cstring(m, hash_scheme);
2069 buffer_put_cstring(m, salt);
2070
2071 debug3("%s: sending pwdata", __func__);
2072 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2073
2074 bzero(hash_scheme, strlen(hash_scheme));
2075 bzero(salt, strlen(salt));
2076 xfree(hash_scheme);
2077 xfree(salt);
2078
2079 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2080
2081 return 0;
2082}
2083
2084int
2085mm_answer_jpake_step2(int sock, Buffer *m)
2086{
2087 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2088 u_char *x1_proof, *x2_proof, *x4_s_proof;
2089 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2090
2091 if (pctx == NULL)
2092 fatal("%s: pctx == NULL", __func__);
2093
2094 if ((pctx->g_x1 = BN_new()) == NULL ||
2095 (pctx->g_x2 = BN_new()) == NULL)
2096 fatal("%s: BN_new", __func__);
2097 buffer_get_bignum2(m, pctx->g_x1);
2098 buffer_get_bignum2(m, pctx->g_x2);
2099 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2100 x1_proof = buffer_get_string(m, &x1_proof_len);
2101 x2_proof = buffer_get_string(m, &x2_proof_len);
2102
2103 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2104 pctx->g_x1, pctx->g_x2, pctx->x4,
2105 pctx->client_id, pctx->client_id_len,
2106 pctx->server_id, pctx->server_id_len,
2107 x1_proof, x1_proof_len,
2108 x2_proof, x2_proof_len,
2109 &pctx->b,
2110 &x4_s_proof, &x4_s_proof_len);
2111
2112 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2113
2114 bzero(x1_proof, x1_proof_len);
2115 bzero(x2_proof, x2_proof_len);
2116 xfree(x1_proof);
2117 xfree(x2_proof);
2118
2119 buffer_clear(m);
2120
2121 buffer_put_bignum2(m, pctx->b);
2122 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2123
2124 debug3("%s: sending step2", __func__);
2125 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2126
2127 bzero(x4_s_proof, x4_s_proof_len);
2128 xfree(x4_s_proof);
2129
2130 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2131
2132 return 0;
2133}
2134
2135int
2136mm_answer_jpake_key_confirm(int sock, Buffer *m)
2137{
2138 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2139 u_char *x2_s_proof;
2140 u_int x2_s_proof_len;
2141
2142 if (pctx == NULL)
2143 fatal("%s: pctx == NULL", __func__);
2144
2145 if ((pctx->a = BN_new()) == NULL)
2146 fatal("%s: BN_new", __func__);
2147 buffer_get_bignum2(m, pctx->a);
2148 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2149
2150 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2151 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2152 pctx->server_id, pctx->server_id_len,
2153 pctx->client_id, pctx->client_id_len,
2154 session_id2, session_id2_len,
2155 x2_s_proof, x2_s_proof_len,
2156 &pctx->k,
2157 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2158
2159 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2160
2161 bzero(x2_s_proof, x2_s_proof_len);
2162 buffer_clear(m);
2163
2164 /* pctx->k is sensitive, not sent */
2165 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2166
2167 debug3("%s: sending confirmation hash", __func__);
2168 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2169
2170 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2171
2172 return 0;
2173}
2174
2175int
2176mm_answer_jpake_check_confirm(int sock, Buffer *m)
2177{
2178 int authenticated = 0;
2179 u_char *peer_confirm_hash;
2180 u_int peer_confirm_hash_len;
2181 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2182
2183 if (pctx == NULL)
2184 fatal("%s: pctx == NULL", __func__);
2185
2186 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2187
2188 authenticated = jpake_check_confirm(pctx->k,
2189 pctx->client_id, pctx->client_id_len,
2190 session_id2, session_id2_len,
2191 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2192
2193 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2194
2195 bzero(peer_confirm_hash, peer_confirm_hash_len);
2196 xfree(peer_confirm_hash);
2197
2198 buffer_clear(m);
2199 buffer_put_int(m, authenticated);
2200
2201 debug3("%s: sending result %d", __func__, authenticated);
2202 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2203
2204 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2205
2206 auth_method = "jpake-01@openssh.com";
2207 return authenticated;
2208}
2209
2210#endif /* JPAKE */