blob: bb8003c6706e61c032df859b8ef2404591fd379a [file] [log] [blame]
Damien Miller8f0bf232011-06-20 14:42:23 +10001/* $OpenBSD: monitor.c,v 1.114 2011/06/17 21:44:30 djm Exp $ */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00002/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100031#include <sys/param.h>
Damien Millere3b60b52006-07-10 21:08:03 +100032#include <sys/socket.h>
Damien Millerd7834352006-08-05 12:39:39 +100033#include "openbsd-compat/sys-tree.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110034#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
Darren Tucker39972492006-07-12 22:22:46 +100036#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100037#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110039#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110040#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100041#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110042#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100043#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100044#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Miller607aede2006-09-01 15:48:19 +100046#include <unistd.h>
Damien Miller8f0bf232011-06-20 14:42:23 +100047#ifdef HAVE_POLL_H
48#include <poll.h>
49#else
50# ifdef HAVE_SYS_POLL_H
51# include <sys/poll.h>
52# endif
53#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000054
55#ifdef SKEY
56#include <skey.h>
57#endif
58
Damien Miller03e20032006-03-15 11:16:59 +110059#include <openssl/dh.h>
60
Damien Millerb84886b2008-05-19 15:05:07 +100061#include "openbsd-compat/sys-queue.h"
Damien Miller8f0bf232011-06-20 14:42:23 +100062#include "atomicio.h"
Damien Millerd7834352006-08-05 12:39:39 +100063#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000064#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "key.h"
66#include "buffer.h"
67#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000070#include "kex.h"
71#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000072#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
73#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000074#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000075#define TARGET_OS_MAC 1
76#else
77#include "zlib.h"
78#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000079#include "packet.h"
80#include "auth-options.h"
81#include "sshpty.h"
82#include "channels.h"
83#include "session.h"
84#include "sshlogin.h"
85#include "canohost.h"
86#include "log.h"
87#include "servconf.h"
88#include "monitor.h"
89#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100090#ifdef GSSAPI
91#include "ssh-gss.h"
92#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000093#include "monitor_wrap.h"
94#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000096#include "compat.h"
97#include "ssh2.h"
Damien Miller01ed2272008-11-05 16:20:46 +110098#include "jpake.h"
Darren Tuckerb422afa2009-06-21 18:58:46 +100099#include "roaming.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100
Darren Tucker0efd1552003-08-26 11:49:55 +1000101#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +1000102static Gssctxt *gsscontext = NULL;
103#endif
104
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105/* Imports */
106extern ServerOptions options;
107extern u_int utmp_len;
108extern Newkeys *current_keys[];
109extern z_stream incoming_stream;
110extern z_stream outgoing_stream;
111extern u_char session_id[];
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000112extern Buffer auth_debug;
113extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000114extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115
116/* State exported from the child */
117
118struct {
119 z_stream incoming;
120 z_stream outgoing;
121 u_char *keyin;
122 u_int keyinlen;
123 u_char *keyout;
124 u_int keyoutlen;
125 u_char *ivin;
126 u_int ivinlen;
127 u_char *ivout;
128 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000129 u_char *ssh1key;
130 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000131 int ssh1cipher;
132 int ssh1protoflags;
133 u_char *input;
134 u_int ilen;
135 u_char *output;
136 u_int olen;
Darren Tuckerc5564e12009-06-21 18:53:53 +1000137 u_int64_t sent_bytes;
138 u_int64_t recv_bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139} child_state;
140
Damien Miller469954d2003-06-18 20:25:33 +1000141/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142
143int mm_answer_moduli(int, Buffer *);
144int mm_answer_sign(int, Buffer *);
145int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000146int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147int mm_answer_authserv(int, Buffer *);
148int mm_answer_authpassword(int, Buffer *);
149int mm_answer_bsdauthquery(int, Buffer *);
150int mm_answer_bsdauthrespond(int, Buffer *);
151int mm_answer_skeyquery(int, Buffer *);
152int mm_answer_skeyrespond(int, Buffer *);
153int mm_answer_keyallowed(int, Buffer *);
154int mm_answer_keyverify(int, Buffer *);
155int mm_answer_pty(int, Buffer *);
156int mm_answer_pty_cleanup(int, Buffer *);
157int mm_answer_term(int, Buffer *);
158int mm_answer_rsa_keyallowed(int, Buffer *);
159int mm_answer_rsa_challenge(int, Buffer *);
160int mm_answer_rsa_response(int, Buffer *);
161int mm_answer_sesskey(int, Buffer *);
162int mm_answer_sessid(int, Buffer *);
Damien Miller01ed2272008-11-05 16:20:46 +1100163int mm_answer_jpake_get_pwdata(int, Buffer *);
164int mm_answer_jpake_step1(int, Buffer *);
165int mm_answer_jpake_step2(int, Buffer *);
166int mm_answer_jpake_key_confirm(int, Buffer *);
167int mm_answer_jpake_check_confirm(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168
Damien Miller79418552002-04-23 20:28:48 +1000169#ifdef USE_PAM
170int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000171int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000172int mm_answer_pam_init_ctx(int, Buffer *);
173int mm_answer_pam_query(int, Buffer *);
174int mm_answer_pam_respond(int, Buffer *);
175int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000176#endif
177
Darren Tucker0efd1552003-08-26 11:49:55 +1000178#ifdef GSSAPI
179int mm_answer_gss_setup_ctx(int, Buffer *);
180int mm_answer_gss_accept_ctx(int, Buffer *);
181int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100182int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000183#endif
Damien Miller25162f22002-09-12 09:47:29 +1000184
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100185#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100186int mm_answer_audit_event(int, Buffer *);
187int mm_answer_audit_command(int, Buffer *);
188#endif
189
Damien Miller8f0bf232011-06-20 14:42:23 +1000190static int monitor_read_log(struct monitor *);
191
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000192static Authctxt *authctxt;
193static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
194
195/* local state for key verify */
196static u_char *key_blob = NULL;
197static u_int key_bloblen = 0;
198static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000199static char *hostbased_cuser = NULL;
200static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000202static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000203static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000204static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000205
206struct mon_table {
207 enum monitor_reqtype type;
208 int flags;
209 int (*f)(int, Buffer *);
210};
211
212#define MON_ISAUTH 0x0004 /* Required for Authentication */
213#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
214#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100215#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000216
217#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
218
219#define MON_PERMIT 0x1000 /* Request is permitted */
220
221struct mon_table mon_dispatch_proto20[] = {
222 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
223 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
224 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
225 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000226 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000228#ifdef USE_PAM
229 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000230 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000231 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
232 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
233 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
234 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000235#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100236#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100237 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100238#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239#ifdef BSD_AUTH
240 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100241 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000242#endif
243#ifdef SKEY
244 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
245 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
246#endif
247 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
248 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000249#ifdef GSSAPI
250 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
251 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
252 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100253 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000254#endif
Damien Miller01ed2272008-11-05 16:20:46 +1100255#ifdef JPAKE
256 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
257 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
258 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
259 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
260 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
261#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000262 {0, 0, NULL}
263};
264
265struct mon_table mon_dispatch_postauth20[] = {
266 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
267 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
268 {MONITOR_REQ_PTY, 0, mm_answer_pty},
269 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
270 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100271#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100272 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
273 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
274#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000275 {0, 0, NULL}
276};
277
278struct mon_table mon_dispatch_proto15[] = {
279 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
280 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
281 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
282 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100283 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
284 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
286 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
287#ifdef BSD_AUTH
288 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100289 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000290#endif
291#ifdef SKEY
292 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
293 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
294#endif
Damien Millera33501b2002-05-08 12:24:42 +1000295#ifdef USE_PAM
296 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000297 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000298 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
299 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
300 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
301 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000302#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100303#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100304 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100305#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000306 {0, 0, NULL}
307};
308
309struct mon_table mon_dispatch_postauth15[] = {
310 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
311 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
312 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100313#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100314 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker5965ae12006-09-17 12:00:13 +1000315 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100316#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000317 {0, 0, NULL}
318};
319
320struct mon_table *mon_dispatch;
321
322/* Specifies if a certain message is allowed at the moment */
323
324static void
325monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
326{
327 while (ent->f != NULL) {
328 if (ent->type == type) {
329 ent->flags &= ~MON_PERMIT;
330 ent->flags |= permit ? MON_PERMIT : 0;
331 return;
332 }
333 ent++;
334 }
335}
336
337static void
338monitor_permit_authentications(int permit)
339{
340 struct mon_table *ent = mon_dispatch;
341
342 while (ent->f != NULL) {
343 if (ent->flags & MON_AUTH) {
344 ent->flags &= ~MON_PERMIT;
345 ent->flags |= permit ? MON_PERMIT : 0;
346 }
347 ent++;
348 }
349}
350
Darren Tucker3e33cec2003-10-02 16:12:36 +1000351void
352monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353{
354 struct mon_table *ent;
355 int authenticated = 0;
356
357 debug3("preauth child monitor started");
358
Damien Miller8f0bf232011-06-20 14:42:23 +1000359 close(pmonitor->m_recvfd);
360 close(pmonitor->m_log_sendfd);
361 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
362
Darren Tucker3e33cec2003-10-02 16:12:36 +1000363 authctxt = _authctxt;
364 memset(authctxt, 0, sizeof(*authctxt));
365
Darren Tuckerde0de392005-03-31 23:52:04 +1000366 authctxt->loginmsg = &loginmsg;
367
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368 if (compat20) {
369 mon_dispatch = mon_dispatch_proto20;
370
371 /* Permit requests for moduli and signatures */
372 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
373 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
374 } else {
375 mon_dispatch = mon_dispatch_proto15;
376
377 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
378 }
379
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380 /* The first few requests do not require asynchronous access */
381 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100382 auth_method = "unknown";
Darren Tuckerfbba7352006-11-07 23:16:08 +1100383 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000384 if (authenticated) {
385 if (!(ent->flags & MON_AUTHDECIDE))
386 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388 if (authctxt->pw->pw_uid == 0 &&
389 !auth_root_allowed(auth_method))
390 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000391#ifdef USE_PAM
392 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100393 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000394 Buffer m;
395
396 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100397 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000398 MONITOR_REQ_PAM_ACCOUNT, &m);
399 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
400 buffer_free(&m);
401 }
402#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403 }
404
Damien Miller7a8f5b32006-03-31 23:14:23 +1100405 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406 auth_log(authctxt, authenticated, auth_method,
407 compat20 ? " ssh2" : "");
408 if (!authenticated)
409 authctxt->failures++;
410 }
Damien Miller01ed2272008-11-05 16:20:46 +1100411#ifdef JPAKE
412 /* Cleanup JPAKE context after authentication */
413 if (ent->flags & MON_AUTHDECIDE) {
414 if (authctxt->jpake_ctx != NULL) {
415 jpake_free(authctxt->jpake_ctx);
416 authctxt->jpake_ctx = NULL;
417 }
418 }
419#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420 }
421
Damien Miller8f0bf232011-06-20 14:42:23 +1000422 /* Drain any buffered messages from the child */
423 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
424 ;
425
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000426 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000427 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100428 if (strcmp(auth_method, "unknown") == 0)
429 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430
431 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000432 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000434 mm_get_keystate(pmonitor);
Damien Miller8f0bf232011-06-20 14:42:23 +1000435
436 close(pmonitor->m_sendfd);
437 close(pmonitor->m_log_recvfd);
438 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000439}
440
Damien Millerbe64d432003-05-14 19:31:12 +1000441static void
442monitor_set_child_handler(pid_t pid)
443{
444 monitor_child_pid = pid;
445}
446
447static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000448monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000449{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000450 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000451}
452
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000454monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000455{
Damien Miller8f0bf232011-06-20 14:42:23 +1000456 close(pmonitor->m_recvfd);
457 pmonitor->m_recvfd = -1;
458
Damien Millerbe64d432003-05-14 19:31:12 +1000459 monitor_set_child_handler(pmonitor->m_pid);
460 signal(SIGHUP, &monitor_child_handler);
461 signal(SIGTERM, &monitor_child_handler);
Darren Tucker7fa339b2007-05-20 15:10:16 +1000462 signal(SIGINT, &monitor_child_handler);
Damien Millerbe64d432003-05-14 19:31:12 +1000463
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 if (compat20) {
465 mon_dispatch = mon_dispatch_postauth20;
466
467 /* Permit requests for moduli and signatures */
468 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
469 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
470 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471 } else {
472 mon_dispatch = mon_dispatch_postauth15;
473 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
474 }
475 if (!no_pty_flag) {
476 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
477 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
478 }
479
480 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000481 monitor_read(pmonitor, mon_dispatch, NULL);
Damien Miller8f0bf232011-06-20 14:42:23 +1000482
483 close(pmonitor->m_sendfd);
484 pmonitor->m_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000485}
486
487void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000488monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000489{
Damien Miller2d6b8332002-06-21 15:59:49 +1000490 if (options.compression) {
491 /* The member allocation is not visible, so sync it */
492 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
493 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000494}
495
Damien Miller8f0bf232011-06-20 14:42:23 +1000496static int
497monitor_read_log(struct monitor *pmonitor)
498{
499 Buffer logmsg;
500 u_int len, level;
501 char *msg;
502
503 buffer_init(&logmsg);
504
505 /* Read length */
506 buffer_append_space(&logmsg, 4);
507 if (atomicio(read, pmonitor->m_log_recvfd,
508 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
509 if (errno == EPIPE) {
510 debug("%s: child log fd closed", __func__);
511 close(pmonitor->m_log_recvfd);
512 pmonitor->m_log_recvfd = -1;
513 return -1;
514 }
515 fatal("%s: log fd read: %s", __func__, strerror(errno));
516 }
517 len = buffer_get_int(&logmsg);
518 if (len <= 4 || len > 8192)
519 fatal("%s: invalid log message length %u", __func__, len);
520
521 /* Read severity, message */
522 buffer_clear(&logmsg);
523 buffer_append_space(&logmsg, len);
524 if (atomicio(read, pmonitor->m_log_recvfd,
525 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
526 fatal("%s: log fd read: %s", __func__, strerror(errno));
527
528 /* Log it */
529 level = buffer_get_int(&logmsg);
530 msg = buffer_get_string(&logmsg, NULL);
531 if (log_level_name(level) == NULL)
532 fatal("%s: invalid log level %u (corrupted message?)",
533 __func__, level);
534 do_log2(level, "%s [preauth]", msg);
535
536 buffer_free(&logmsg);
537 xfree(msg);
538
539 return 0;
540}
541
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000543monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544 struct mon_table **pent)
545{
546 Buffer m;
547 int ret;
548 u_char type;
Damien Miller8f0bf232011-06-20 14:42:23 +1000549 struct pollfd pfd[2];
550
551 for (;;) {
552 bzero(&pfd, sizeof(pfd));
553 pfd[0].fd = pmonitor->m_sendfd;
554 pfd[0].events = POLLIN;
555 pfd[1].fd = pmonitor->m_log_recvfd;
556 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
557 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1)
558 fatal("%s: poll: %s", __func__, strerror(errno));
559 if (pfd[1].revents) {
560 /*
561 * Drain all log messages before processing next
562 * monitor request.
563 */
564 monitor_read_log(pmonitor);
565 continue;
566 }
567 if (pfd[0].revents)
568 break; /* Continues below */
569 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 buffer_init(&m);
572
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000573 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574 type = buffer_get_char(&m);
575
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000576 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577
578 while (ent->f != NULL) {
579 if (ent->type == type)
580 break;
581 ent++;
582 }
583
584 if (ent->f != NULL) {
585 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000586 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000588 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589 buffer_free(&m);
590
591 /* The child may use this request only once, disable it */
592 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000593 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594 type);
595 ent->flags &= ~MON_PERMIT;
596 }
597
598 if (pent != NULL)
599 *pent = ent;
600
601 return ret;
602 }
603
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000604 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
606 /* NOTREACHED */
607 return (-1);
608}
609
610/* allowed key state */
611static int
612monitor_allowed_key(u_char *blob, u_int bloblen)
613{
614 /* make sure key is allowed */
615 if (key_blob == NULL || key_bloblen != bloblen ||
Damien Millerea1651c2010-07-16 13:58:37 +1000616 timingsafe_bcmp(key_blob, blob, key_bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617 return (0);
618 return (1);
619}
620
621static void
622monitor_reset_key_state(void)
623{
624 /* reset state */
625 if (key_blob != NULL)
626 xfree(key_blob);
627 if (hostbased_cuser != NULL)
628 xfree(hostbased_cuser);
629 if (hostbased_chost != NULL)
630 xfree(hostbased_chost);
631 key_blob = NULL;
632 key_bloblen = 0;
633 key_blobtype = MM_NOKEY;
634 hostbased_cuser = NULL;
635 hostbased_chost = NULL;
636}
637
638int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000639mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000640{
641 DH *dh;
642 int min, want, max;
643
644 min = buffer_get_int(m);
645 want = buffer_get_int(m);
646 max = buffer_get_int(m);
647
648 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000649 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650 /* We need to check here, too, in case the child got corrupted */
651 if (max < min || want < min || max < want)
652 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000653 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654
655 buffer_clear(m);
656
657 dh = choose_dh(min, want, max);
658 if (dh == NULL) {
659 buffer_put_char(m, 0);
660 return (0);
661 } else {
662 /* Send first bignum */
663 buffer_put_char(m, 1);
664 buffer_put_bignum2(m, dh->p);
665 buffer_put_bignum2(m, dh->g);
666
667 DH_free(dh);
668 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000669 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670 return (0);
671}
672
673int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000674mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675{
676 Key *key;
677 u_char *p;
678 u_char *signature;
679 u_int siglen, datlen;
680 int keyid;
681
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000682 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683
684 keyid = buffer_get_int(m);
685 p = buffer_get_string(m, &datlen);
686
Damien Millera63128d2006-03-15 12:08:28 +1100687 /*
Damien Miller041ab7c2010-09-10 11:23:34 +1000688 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
689 * SHA384 (48 bytes) and SHA512 (64 bytes).
Damien Millera63128d2006-03-15 12:08:28 +1100690 */
Damien Miller041ab7c2010-09-10 11:23:34 +1000691 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
Ben Lindstromd5502182002-06-27 00:12:57 +0000692 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
Ben Lindstromf67e0772002-06-06 20:58:19 +0000694 /* save session id, it will be passed on the first call */
695 if (session_id2_len == 0) {
696 session_id2_len = datlen;
697 session_id2 = xmalloc(session_id2_len);
698 memcpy(session_id2, p, session_id2_len);
699 }
700
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000704 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
Ben Lindstromd5502182002-06-27 00:12:57 +0000706 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707
708 buffer_clear(m);
709 buffer_put_string(m, signature, siglen);
710
711 xfree(p);
712 xfree(signature);
713
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000714 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000715
716 /* Turn on permissions for getpwnam */
717 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
718
719 return (0);
720}
721
722/* Retrieves the password entry and also checks if the user is permitted */
723
724int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000725mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000727 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728 struct passwd *pwent;
729 int allowed = 0;
Damien Millerd8478b62011-05-29 21:39:36 +1000730 u_int i;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000732 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
734 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000735 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
Darren Tuckerb09b6772004-06-22 15:06:46 +1000737 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738
Darren Tuckerb09b6772004-06-22 15:06:46 +1000739 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740
Darren Tuckerb09b6772004-06-22 15:06:46 +1000741 authctxt->user = xstrdup(username);
742 setproctitle("%s [priv]", pwent ? username : "unknown");
743 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744
745 buffer_clear(m);
746
747 if (pwent == NULL) {
748 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100749 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000750 goto out;
751 }
752
753 allowed = 1;
754 authctxt->pw = pwent;
755 authctxt->valid = 1;
756
757 buffer_put_char(m, 1);
758 buffer_put_string(m, pwent, sizeof(struct passwd));
759 buffer_put_cstring(m, pwent->pw_name);
760 buffer_put_cstring(m, "*");
761 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000762#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000764#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765 buffer_put_cstring(m, pwent->pw_dir);
766 buffer_put_cstring(m, pwent->pw_shell);
Darren Tucker2f8b3d92007-12-02 23:02:15 +1100767
768 out:
Darren Tucker1629c072007-02-19 22:25:37 +1100769 buffer_put_string(m, &options, sizeof(options));
Damien Millerf2e407e2011-05-20 19:04:14 +1000770
771#define M_CP_STROPT(x) do { \
772 if (options.x != NULL) \
773 buffer_put_cstring(m, options.x); \
774 } while (0)
Damien Millerd8478b62011-05-29 21:39:36 +1000775#define M_CP_STRARRAYOPT(x, nx) do { \
776 for (i = 0; i < options.nx; i++) \
777 buffer_put_cstring(m, options.x[i]); \
778 } while (0)
Damien Millerf2e407e2011-05-20 19:04:14 +1000779 /* See comment in servconf.h */
780 COPY_MATCH_STRING_OPTS();
781#undef M_CP_STROPT
Damien Millerd8478b62011-05-29 21:39:36 +1000782#undef M_CP_STRARRAYOPT
Damien Millerf2e407e2011-05-20 19:04:14 +1000783
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000784 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000785 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786
787 /* For SSHv1 allow authentication now */
788 if (!compat20)
789 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000790 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791 /* Allow service/style information on the auth context */
792 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000793 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
794 }
Damien Millera33501b2002-05-08 12:24:42 +1000795#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000796 if (options.use_pam)
797 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000798#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000799
800 return (0);
801}
802
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000803int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000804{
805 char *banner;
806
807 buffer_clear(m);
808 banner = auth2_read_banner();
809 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000810 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000811
812 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000813 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000814
815 return (0);
816}
817
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000818int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000819mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000820{
821 monitor_permit_authentications(1);
822
823 authctxt->service = buffer_get_string(m, NULL);
824 authctxt->style = buffer_get_string(m, NULL);
825 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000826 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000827
828 if (strlen(authctxt->style) == 0) {
829 xfree(authctxt->style);
830 authctxt->style = NULL;
831 }
832
833 return (0);
834}
835
836int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000837mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000838{
839 static int call_count;
840 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000841 int authenticated;
842 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843
844 passwd = buffer_get_string(m, &plen);
845 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000846 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000847 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000848 memset(passwd, 0, strlen(passwd));
849 xfree(passwd);
850
851 buffer_clear(m);
852 buffer_put_int(m, authenticated);
853
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000854 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000855 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000856
857 call_count++;
858 if (plen == 0 && call_count == 1)
859 auth_method = "none";
860 else
861 auth_method = "password";
862
863 /* Causes monitor loop to terminate if authenticated */
864 return (authenticated);
865}
866
867#ifdef BSD_AUTH
868int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000869mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000870{
871 char *name, *infotxt;
872 u_int numprompts;
873 u_int *echo_on;
874 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100875 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000876
Damien Millerb7df3af2003-02-24 11:55:46 +1100877 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
878 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879
880 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100881 buffer_put_int(m, success);
882 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000883 buffer_put_cstring(m, prompts[0]);
884
Damien Millerb7df3af2003-02-24 11:55:46 +1100885 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000886 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000887
Damien Millerb7df3af2003-02-24 11:55:46 +1100888 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889 xfree(name);
890 xfree(infotxt);
891 xfree(prompts);
892 xfree(echo_on);
893 }
894
895 return (0);
896}
897
898int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000899mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900{
901 char *response;
902 int authok;
903
904 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000905 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000906
907 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000908 authok = options.challenge_response_authentication &&
909 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000910 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000911 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 xfree(response);
913
914 buffer_clear(m);
915 buffer_put_int(m, authok);
916
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000917 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000918 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919
920 auth_method = "bsdauth";
921
922 return (authok != 0);
923}
924#endif
925
926#ifdef SKEY
927int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000928mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000929{
930 struct skey skey;
931 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100932 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000933
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000934 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
935 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100938 buffer_put_int(m, success);
939 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000940 buffer_put_cstring(m, challenge);
941
Damien Millerb7df3af2003-02-24 11:55:46 +1100942 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000943 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 return (0);
946}
947
948int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000949mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950{
951 char *response;
952 int authok;
953
954 response = buffer_get_string(m, NULL);
955
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000956 authok = (options.challenge_response_authentication &&
957 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000958 skey_haskey(authctxt->pw->pw_name) == 0 &&
959 skey_passcheck(authctxt->pw->pw_name, response) != -1);
960
961 xfree(response);
962
963 buffer_clear(m);
964 buffer_put_int(m, authok);
965
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000966 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000967 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
969 auth_method = "skey";
970
971 return (authok != 0);
972}
973#endif
974
Damien Miller79418552002-04-23 20:28:48 +1000975#ifdef USE_PAM
976int
Darren Tucker7eda5922004-06-22 13:22:50 +1000977mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000978{
Damien Miller4e448a32003-05-14 15:11:48 +1000979 if (!options.use_pam)
980 fatal("UsePAM not set, but ended up in %s anyway", __func__);
981
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100982 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000983
Damien Miller1f499fd2003-08-25 13:08:49 +1000984 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
985
Damien Miller79418552002-04-23 20:28:48 +1000986 return (0);
987}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000988
Damien Miller1f499fd2003-08-25 13:08:49 +1000989int
Darren Tucker7eda5922004-06-22 13:22:50 +1000990mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000991{
992 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100993
Damien Miller1f499fd2003-08-25 13:08:49 +1000994 if (!options.use_pam)
995 fatal("UsePAM not set, but ended up in %s anyway", __func__);
996
997 ret = do_pam_account();
998
999 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +10001000 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +10001001
Darren Tucker7eda5922004-06-22 13:22:50 +10001002 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +10001003
1004 return (ret);
1005}
1006
Damien Miller4f9f42a2003-05-10 19:28:02 +10001007static void *sshpam_ctxt, *sshpam_authok;
1008extern KbdintDevice sshpam_device;
1009
1010int
Darren Tucker7eda5922004-06-22 13:22:50 +10001011mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001012{
1013
1014 debug3("%s", __func__);
1015 authctxt->user = buffer_get_string(m, NULL);
1016 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1017 sshpam_authok = NULL;
1018 buffer_clear(m);
1019 if (sshpam_ctxt != NULL) {
1020 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1021 buffer_put_int(m, 1);
1022 } else {
1023 buffer_put_int(m, 0);
1024 }
Darren Tucker7eda5922004-06-22 13:22:50 +10001025 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001026 return (0);
1027}
1028
1029int
Darren Tucker7eda5922004-06-22 13:22:50 +10001030mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001031{
Darren Tucker8b7a0552010-08-03 15:50:16 +10001032 char *name = NULL, *info = NULL, **prompts = NULL;
1033 u_int i, num = 0, *echo_on = 0;
Damien Miller04b65332005-07-17 17:53:31 +10001034 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001035
1036 debug3("%s", __func__);
1037 sshpam_authok = NULL;
1038 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1039 if (ret == 0 && num == 0)
1040 sshpam_authok = sshpam_ctxt;
1041 if (num > 1 || name == NULL || info == NULL)
1042 ret = -1;
1043 buffer_clear(m);
1044 buffer_put_int(m, ret);
1045 buffer_put_cstring(m, name);
1046 xfree(name);
1047 buffer_put_cstring(m, info);
1048 xfree(info);
1049 buffer_put_int(m, num);
1050 for (i = 0; i < num; ++i) {
1051 buffer_put_cstring(m, prompts[i]);
1052 xfree(prompts[i]);
1053 buffer_put_int(m, echo_on[i]);
1054 }
1055 if (prompts != NULL)
1056 xfree(prompts);
1057 if (echo_on != NULL)
1058 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +10001059 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +10001060 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001061 return (0);
1062}
1063
1064int
Darren Tucker7eda5922004-06-22 13:22:50 +10001065mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001066{
1067 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +10001068 u_int i, num;
1069 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +10001070
1071 debug3("%s", __func__);
1072 sshpam_authok = NULL;
1073 num = buffer_get_int(m);
1074 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +10001075 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +10001076 for (i = 0; i < num; ++i)
1077 resp[i] = buffer_get_string(m, NULL);
1078 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1079 for (i = 0; i < num; ++i)
1080 xfree(resp[i]);
1081 xfree(resp);
1082 } else {
1083 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1084 }
1085 buffer_clear(m);
1086 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +10001087 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +10001088 auth_method = "keyboard-interactive/pam";
1089 if (ret == 0)
1090 sshpam_authok = sshpam_ctxt;
1091 return (0);
1092}
1093
1094int
Darren Tucker7eda5922004-06-22 13:22:50 +10001095mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +10001096{
1097
1098 debug3("%s", __func__);
1099 (sshpam_device.free_ctx)(sshpam_ctxt);
1100 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +10001101 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +10001102 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +10001103 return (sshpam_authok == sshpam_ctxt);
1104}
Damien Miller79418552002-04-23 20:28:48 +10001105#endif
1106
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001108mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001109{
1110 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +10001111 char *cuser, *chost;
1112 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001113 u_int bloblen;
1114 enum mm_keytype type = 0;
1115 int allowed = 0;
1116
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001117 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118
1119 type = buffer_get_int(m);
1120 cuser = buffer_get_string(m, NULL);
1121 chost = buffer_get_string(m, NULL);
1122 blob = buffer_get_string(m, &bloblen);
1123
1124 key = key_from_blob(blob, bloblen);
1125
1126 if ((compat20 && type == MM_RSAHOSTKEY) ||
1127 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001128 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001129
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001130 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131
Damien Miller3e3b5142003-11-17 21:13:40 +11001132 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001133 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001135 allowed = options.pubkey_authentication &&
1136 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001137 auth_method = "publickey";
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001138 if (options.pubkey_authentication && allowed != 1)
1139 auth_clear_options();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001140 break;
1141 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001142 allowed = options.hostbased_authentication &&
1143 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001145 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146 break;
1147 case MM_RSAHOSTKEY:
1148 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001149 allowed = options.rhosts_rsa_authentication &&
1150 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151 cuser, chost, key);
Darren Tuckerf2c16d32008-06-14 08:59:49 +10001152 if (options.rhosts_rsa_authentication && allowed != 1)
1153 auth_clear_options();
Damien Miller7a8f5b32006-03-31 23:14:23 +11001154 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001155 break;
1156 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001157 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001158 break;
1159 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160 }
Damien Miller00111382003-03-10 11:21:17 +11001161 if (key != NULL)
1162 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001163
1164 /* clear temporarily storage (used by verify) */
1165 monitor_reset_key_state();
1166
1167 if (allowed) {
1168 /* Save temporarily for comparison in verify */
1169 key_blob = blob;
1170 key_bloblen = bloblen;
1171 key_blobtype = type;
1172 hostbased_cuser = cuser;
1173 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001174 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001175 /* Log failed attempt */
1176 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001177 xfree(blob);
1178 xfree(cuser);
1179 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 }
1181
1182 debug3("%s: key %p is %s",
Darren Tucker2784f1f2008-07-04 13:51:45 +10001183 __func__, key, allowed ? "allowed" : "not allowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184
1185 buffer_clear(m);
1186 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001187 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001188
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001189 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190
1191 if (type == MM_RSAHOSTKEY)
1192 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1193
1194 return (0);
1195}
1196
1197static int
1198monitor_valid_userblob(u_char *data, u_int datalen)
1199{
1200 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001201 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202 u_int len;
1203 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001204
1205 buffer_init(&b);
1206 buffer_append(&b, data, datalen);
1207
1208 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001209 p = buffer_ptr(&b);
1210 len = buffer_len(&b);
1211 if ((session_id2 == NULL) ||
1212 (len < session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001213 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstromf67e0772002-06-06 20:58:19 +00001214 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001215 buffer_consume(&b, session_id2_len);
1216 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001217 p = buffer_get_string(&b, &len);
1218 if ((session_id2 == NULL) ||
1219 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001220 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001222 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 }
1224 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1225 fail++;
1226 p = buffer_get_string(&b, NULL);
1227 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001228 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 authctxt->user, p);
1230 fail++;
1231 }
1232 xfree(p);
1233 buffer_skip_string(&b);
1234 if (datafellows & SSH_BUG_PKAUTH) {
1235 if (!buffer_get_char(&b))
1236 fail++;
1237 } else {
1238 p = buffer_get_string(&b, NULL);
1239 if (strcmp("publickey", p) != 0)
1240 fail++;
1241 xfree(p);
1242 if (!buffer_get_char(&b))
1243 fail++;
1244 buffer_skip_string(&b);
1245 }
1246 buffer_skip_string(&b);
1247 if (buffer_len(&b) != 0)
1248 fail++;
1249 buffer_free(&b);
1250 return (fail == 0);
1251}
1252
1253static int
Damien Millera10f5612002-09-12 09:49:15 +10001254monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1255 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256{
1257 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001258 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259 u_int len;
1260 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261
1262 buffer_init(&b);
1263 buffer_append(&b, data, datalen);
1264
Ben Lindstromf67e0772002-06-06 20:58:19 +00001265 p = buffer_get_string(&b, &len);
1266 if ((session_id2 == NULL) ||
1267 (len != session_id2_len) ||
Damien Millerea1651c2010-07-16 13:58:37 +10001268 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001270 xfree(p);
1271
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1273 fail++;
1274 p = buffer_get_string(&b, NULL);
1275 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001276 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277 authctxt->user, p);
1278 fail++;
1279 }
1280 xfree(p);
1281 buffer_skip_string(&b); /* service */
1282 p = buffer_get_string(&b, NULL);
1283 if (strcmp(p, "hostbased") != 0)
1284 fail++;
1285 xfree(p);
1286 buffer_skip_string(&b); /* pkalg */
1287 buffer_skip_string(&b); /* pkblob */
1288
1289 /* verify client host, strip trailing dot if necessary */
1290 p = buffer_get_string(&b, NULL);
1291 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1292 p[len - 1] = '\0';
1293 if (strcmp(p, chost) != 0)
1294 fail++;
1295 xfree(p);
1296
1297 /* verify client user */
1298 p = buffer_get_string(&b, NULL);
1299 if (strcmp(p, cuser) != 0)
1300 fail++;
1301 xfree(p);
1302
1303 if (buffer_len(&b) != 0)
1304 fail++;
1305 buffer_free(&b);
1306 return (fail == 0);
1307}
1308
1309int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001310mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311{
1312 Key *key;
1313 u_char *signature, *data, *blob;
1314 u_int signaturelen, datalen, bloblen;
1315 int verified = 0;
1316 int valid_data = 0;
1317
1318 blob = buffer_get_string(m, &bloblen);
1319 signature = buffer_get_string(m, &signaturelen);
1320 data = buffer_get_string(m, &datalen);
1321
1322 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001323 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001324 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001325
1326 key = key_from_blob(blob, bloblen);
1327 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001328 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
1330 switch (key_blobtype) {
1331 case MM_USERKEY:
1332 valid_data = monitor_valid_userblob(data, datalen);
1333 break;
1334 case MM_HOSTKEY:
1335 valid_data = monitor_valid_hostbasedblob(data, datalen,
1336 hostbased_cuser, hostbased_chost);
1337 break;
1338 default:
1339 valid_data = 0;
1340 break;
1341 }
1342 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001343 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344
1345 verified = key_verify(key, signature, signaturelen, data, datalen);
1346 debug3("%s: key %p signature %s",
Darren Tuckerfbba7352006-11-07 23:16:08 +11001347 __func__, key, (verified == 1) ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
1349 key_free(key);
1350 xfree(blob);
1351 xfree(signature);
1352 xfree(data);
1353
Ben Lindstrome1c09122002-06-23 00:38:24 +00001354 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1355
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356 monitor_reset_key_state();
1357
1358 buffer_clear(m);
1359 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001360 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361
Darren Tuckerfbba7352006-11-07 23:16:08 +11001362 return (verified == 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363}
1364
1365static void
1366mm_record_login(Session *s, struct passwd *pw)
1367{
1368 socklen_t fromlen;
1369 struct sockaddr_storage from;
1370
1371 /*
1372 * Get IP address of client. If the connection is not a socket, let
1373 * the address be 0.0.0.0.
1374 */
1375 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001376 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001379 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001381 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382 }
1383 }
1384 /* Record that there was a login on that tty from the remote host. */
1385 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001386 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001387 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388}
1389
1390static void
1391mm_session_close(Session *s)
1392{
Damien Miller04bd8b02003-05-25 14:38:33 +10001393 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001395 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 session_pty_cleanup2(s);
1397 }
Damien Miller7207f642008-05-19 15:34:50 +10001398 session_unused(s->self);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399}
1400
1401int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001402mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001403{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001404 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405 Session *s;
1406 int res, fd0;
1407
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001408 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409
1410 buffer_clear(m);
1411 s = session_new();
1412 if (s == NULL)
1413 goto error;
1414 s->authctxt = authctxt;
1415 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001416 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001417 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1418 if (res == 0)
1419 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 pty_setowner(authctxt->pw, s->tty);
1421
1422 buffer_put_int(m, 1);
1423 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001424
1425 /* We need to trick ttyslot */
1426 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
1429 mm_record_login(s, authctxt->pw);
1430
1431 /* Now we can close the file descriptor again */
1432 close(0);
1433
Darren Tucker09991742004-07-17 17:05:14 +10001434 /* send messages generated by record_login */
1435 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1436 buffer_clear(&loginmsg);
1437
1438 mm_request_send(sock, MONITOR_ANS_PTY, m);
1439
Damien Miller54fd7cf2007-09-17 12:04:08 +10001440 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1441 mm_send_fd(sock, s->ttyfd) == -1)
1442 fatal("%s: send fds failed", __func__);
Darren Tucker09991742004-07-17 17:05:14 +10001443
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444 /* make sure nothing uses fd 0 */
1445 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001446 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001448 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001449
1450 /* slave is not needed */
1451 close(s->ttyfd);
1452 s->ttyfd = s->ptyfd;
1453 /* no need to dup() because nobody closes ptyfd */
1454 s->ptymaster = s->ptyfd;
1455
Damien Miller9ab00b42006-08-05 12:40:11 +10001456 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457
1458 return (0);
1459
1460 error:
1461 if (s != NULL)
1462 mm_session_close(s);
1463 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001464 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 return (0);
1466}
1467
1468int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001469mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470{
1471 Session *s;
1472 char *tty;
1473
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475
1476 tty = buffer_get_string(m, NULL);
1477 if ((s = session_by_tty(tty)) != NULL)
1478 mm_session_close(s);
1479 buffer_clear(m);
1480 xfree(tty);
1481 return (0);
1482}
1483
1484int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001485mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486{
1487 BIGNUM *p;
1488 int rsafail;
1489
1490 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001491 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001494 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495
1496 buffer_get_bignum2(m, p);
1497
1498 rsafail = ssh1_session_key(p);
1499
1500 buffer_clear(m);
1501 buffer_put_int(m, rsafail);
1502 buffer_put_bignum2(m, p);
1503
1504 BN_clear_free(p);
1505
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001506 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507
1508 /* Turn on permissions for sessid passing */
1509 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1510
1511 return (0);
1512}
1513
1514int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001515mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516{
1517 int i;
1518
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001519 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520
1521 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001522 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523 for (i = 0; i < 16; i++)
1524 session_id[i] = buffer_get_char(m);
1525
1526 /* Turn on permissions for getpwnam */
1527 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1528
1529 return (0);
1530}
1531
1532int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001533mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534{
1535 BIGNUM *client_n;
1536 Key *key = NULL;
1537 u_char *blob = NULL;
1538 u_int blen = 0;
1539 int allowed = 0;
1540
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001541 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542
Damien Miller7a8f5b32006-03-31 23:14:23 +11001543 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001544 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001546 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001547 buffer_get_bignum2(m, client_n);
1548 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1549 BN_clear_free(client_n);
1550 }
1551 buffer_clear(m);
1552 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001553 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554
1555 /* clear temporarily storage (used by generate challenge) */
1556 monitor_reset_key_state();
1557
1558 if (allowed && key != NULL) {
1559 key->type = KEY_RSA; /* cheat for key_to_blob */
1560 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001561 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001562 buffer_put_string(m, blob, blen);
1563
1564 /* Save temporarily for comparison in verify */
1565 key_blob = blob;
1566 key_bloblen = blen;
1567 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001568 }
Damien Miller00111382003-03-10 11:21:17 +11001569 if (key != NULL)
1570 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001572 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001573
1574 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1575 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1576 return (0);
1577}
1578
1579int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001580mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581{
1582 Key *key = NULL;
1583 u_char *blob;
1584 u_int blen;
1585
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001586 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001587
1588 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001589 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590 blob = buffer_get_string(m, &blen);
1591 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001592 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001593 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001594 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001595 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001596 fatal("%s: received bad key", __func__);
Damien Miller923e8bb2009-02-14 16:33:31 +11001597 if (key->type != KEY_RSA)
1598 fatal("%s: received bad key type %d", __func__, key->type);
1599 key->type = KEY_RSA1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600 if (ssh1_challenge)
1601 BN_clear_free(ssh1_challenge);
1602 ssh1_challenge = auth_rsa_generate_challenge(key);
1603
1604 buffer_clear(m);
1605 buffer_put_bignum2(m, ssh1_challenge);
1606
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001607 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001608 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609
1610 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001611
1612 xfree(blob);
1613 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001614 return (0);
1615}
1616
1617int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001618mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001619{
1620 Key *key = NULL;
1621 u_char *blob, *response;
1622 u_int blen, len;
1623 int success;
1624
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001625 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626
1627 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001628 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001629 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001630 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631
1632 blob = buffer_get_string(m, &blen);
1633 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001634 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001636 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001637 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001638 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639 response = buffer_get_string(m, &len);
1640 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001641 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001642 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1643
Damien Miller00111382003-03-10 11:21:17 +11001644 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001645 key_free(key);
1646 xfree(response);
1647
1648 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1649
1650 /* reset state */
1651 BN_clear_free(ssh1_challenge);
1652 ssh1_challenge = NULL;
1653 monitor_reset_key_state();
1654
1655 buffer_clear(m);
1656 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001657 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001658
1659 return (success);
1660}
1661
1662int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001663mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001665 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 int res, status;
1667
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669
1670 /* The child is terminating */
1671 session_destroy_all(&mm_session_close);
1672
Darren Tucker52358d62008-03-11 22:58:25 +11001673#ifdef USE_PAM
1674 if (options.use_pam)
1675 sshpam_cleanup();
1676#endif
1677
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001678 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001679 if (errno != EINTR)
1680 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681
1682 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1683
1684 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001685 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686}
1687
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001688#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001689/* Report that an audit event occurred */
1690int
1691mm_answer_audit_event(int socket, Buffer *m)
1692{
1693 ssh_audit_event_t event;
1694
1695 debug3("%s entering", __func__);
1696
1697 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001698 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001699 case SSH_AUTH_FAIL_PUBKEY:
1700 case SSH_AUTH_FAIL_HOSTBASED:
1701 case SSH_AUTH_FAIL_GSSAPI:
1702 case SSH_LOGIN_EXCEED_MAXTRIES:
1703 case SSH_LOGIN_ROOT_DENIED:
1704 case SSH_CONNECTION_CLOSE:
1705 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001706 audit_event(event);
1707 break;
1708 default:
1709 fatal("Audit event type %d not permitted", event);
1710 }
1711
1712 return (0);
1713}
1714
1715int
1716mm_answer_audit_command(int socket, Buffer *m)
1717{
1718 u_int len;
1719 char *cmd;
1720
1721 debug3("%s entering", __func__);
1722 cmd = buffer_get_string(m, &len);
1723 /* sanity check command, if so how? */
1724 audit_run_command(cmd);
1725 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001726 return (0);
1727}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001728#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001729
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001730void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001731monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732{
1733 if (compat20) {
1734 set_newkeys(MODE_IN);
1735 set_newkeys(MODE_OUT);
1736 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001738 packet_set_encryption_key(child_state.ssh1key,
1739 child_state.ssh1keylen, child_state.ssh1cipher);
1740 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741 }
1742
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001743 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001744 packet_set_keycontext(MODE_OUT, child_state.keyout);
1745 xfree(child_state.keyout);
1746 packet_set_keycontext(MODE_IN, child_state.keyin);
1747 xfree(child_state.keyin);
1748
1749 if (!compat20) {
1750 packet_set_iv(MODE_OUT, child_state.ivout);
1751 xfree(child_state.ivout);
1752 packet_set_iv(MODE_IN, child_state.ivin);
1753 xfree(child_state.ivin);
1754 }
1755
1756 memcpy(&incoming_stream, &child_state.incoming,
1757 sizeof(incoming_stream));
1758 memcpy(&outgoing_stream, &child_state.outgoing,
1759 sizeof(outgoing_stream));
1760
1761 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001762 if (options.compression)
1763 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764
1765 /* Network I/O buffers */
1766 /* XXX inefficient for large buffers, need: buffer_init_from_string */
Darren Tuckerf7288d72009-06-21 18:12:20 +10001767 buffer_clear(packet_get_input());
1768 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001769 memset(child_state.input, 0, child_state.ilen);
1770 xfree(child_state.input);
1771
Darren Tuckerf7288d72009-06-21 18:12:20 +10001772 buffer_clear(packet_get_output());
1773 buffer_append(packet_get_output(), child_state.output,
1774 child_state.olen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001775 memset(child_state.output, 0, child_state.olen);
1776 xfree(child_state.output);
Darren Tuckerc5564e12009-06-21 18:53:53 +10001777
1778 /* Roaming */
1779 if (compat20)
1780 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001781}
1782
1783static Kex *
1784mm_get_kex(Buffer *m)
1785{
1786 Kex *kex;
1787 void *blob;
1788 u_int bloblen;
1789
Damien Miller07d86be2006-03-26 14:19:21 +11001790 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001791 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Damien Miller8a0268f2010-07-16 13:57:51 +10001792 if (session_id2 == NULL ||
1793 kex->session_id_len != session_id2_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001794 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
Ben Lindstromf67e0772002-06-06 20:58:19 +00001795 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001796 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001797 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001798 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001799 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001800 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Damien Millereb8b60e2010-08-31 22:41:14 +10001801 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001802 kex->server = 1;
1803 kex->hostkey_type = buffer_get_int(m);
1804 kex->kex_type = buffer_get_int(m);
1805 blob = buffer_get_string(m, &bloblen);
1806 buffer_init(&kex->my);
1807 buffer_append(&kex->my, blob, bloblen);
1808 xfree(blob);
1809 blob = buffer_get_string(m, &bloblen);
1810 buffer_init(&kex->peer);
1811 buffer_append(&kex->peer, blob, bloblen);
1812 xfree(blob);
1813 kex->done = 1;
1814 kex->flags = buffer_get_int(m);
1815 kex->client_version_string = buffer_get_string(m, NULL);
1816 kex->server_version_string = buffer_get_string(m, NULL);
Damien Miller0a80ca12010-02-27 07:55:05 +11001817 kex->load_host_public_key=&get_hostkey_public_by_type;
1818 kex->load_host_private_key=&get_hostkey_private_by_type;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001819 kex->host_key_index=&get_hostkey_index;
1820
1821 return (kex);
1822}
1823
1824/* This function requries careful sanity checking */
1825
1826void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001827mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001828{
1829 Buffer m;
1830 u_char *blob, *p;
1831 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001832 u_int32_t seqnr, packets;
Damien Millerb61f3fc2008-07-11 17:36:48 +10001833 u_int64_t blocks, bytes;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001834
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001835 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001836
1837 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001838 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001839 if (!compat20) {
1840 child_state.ssh1protoflags = buffer_get_int(&m);
1841 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001842 child_state.ssh1key = buffer_get_string(&m,
1843 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001844 child_state.ivout = buffer_get_string(&m,
1845 &child_state.ivoutlen);
1846 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1847 goto skip;
1848 } else {
1849 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001850 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001851 }
1852
1853 blob = buffer_get_string(&m, &bloblen);
1854 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1855 xfree(blob);
1856
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001857 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001858 blob = buffer_get_string(&m, &bloblen);
1859 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1860 xfree(blob);
1861
1862 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001863 seqnr = buffer_get_int(&m);
1864 blocks = buffer_get_int64(&m);
1865 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001866 bytes = buffer_get_int64(&m);
1867 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
Damien Millera5539d22003-04-09 20:50:06 +10001868 seqnr = buffer_get_int(&m);
1869 blocks = buffer_get_int64(&m);
1870 packets = buffer_get_int(&m);
Damien Millerb61f3fc2008-07-11 17:36:48 +10001871 bytes = buffer_get_int64(&m);
1872 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001873
1874 skip:
1875 /* Get the key context */
1876 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1877 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1878
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001879 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001880 /* Get compression state */
1881 p = buffer_get_string(&m, &plen);
1882 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001883 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001884 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1885 xfree(p);
1886
1887 p = buffer_get_string(&m, &plen);
1888 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001889 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001890 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1891 xfree(p);
1892
1893 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001894 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001895 child_state.input = buffer_get_string(&m, &child_state.ilen);
1896 child_state.output = buffer_get_string(&m, &child_state.olen);
1897
Darren Tuckerc5564e12009-06-21 18:53:53 +10001898 /* Roaming */
1899 if (compat20) {
1900 child_state.sent_bytes = buffer_get_int64(&m);
1901 child_state.recv_bytes = buffer_get_int64(&m);
1902 }
1903
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001904 buffer_free(&m);
1905}
1906
1907
1908/* Allocation functions for zlib */
1909void *
1910mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1911{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001912 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001913 void *address;
1914
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001915 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001916 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1917
1918 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001919
1920 return (address);
1921}
1922
1923void
1924mm_zfree(struct mm_master *mm, void *address)
1925{
1926 mm_free(mm, address);
1927}
1928
1929void
1930mm_init_compression(struct mm_master *mm)
1931{
1932 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1933 outgoing_stream.zfree = (free_func)mm_zfree;
1934 outgoing_stream.opaque = mm;
1935
1936 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1937 incoming_stream.zfree = (free_func)mm_zfree;
1938 incoming_stream.opaque = mm;
1939}
1940
1941/* XXX */
1942
1943#define FD_CLOSEONEXEC(x) do { \
Damien Miller814ace02011-05-20 19:02:47 +10001944 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001945 fatal("fcntl(%d, F_SETFD)", x); \
1946} while (0)
1947
1948static void
Damien Miller8f0bf232011-06-20 14:42:23 +10001949monitor_openfds(struct monitor *mon, int do_logfds)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001950{
Damien Miller8f0bf232011-06-20 14:42:23 +10001951 int pair[2];
1952
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001953 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Damien Miller8f0bf232011-06-20 14:42:23 +10001954 fatal("%s: socketpair: %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001955 FD_CLOSEONEXEC(pair[0]);
1956 FD_CLOSEONEXEC(pair[1]);
Damien Miller8f0bf232011-06-20 14:42:23 +10001957 mon->m_recvfd = pair[0];
1958 mon->m_sendfd = pair[1];
1959
1960 if (do_logfds) {
1961 if (pipe(pair) == -1)
1962 fatal("%s: pipe: %s", __func__, strerror(errno));
1963 FD_CLOSEONEXEC(pair[0]);
1964 FD_CLOSEONEXEC(pair[1]);
1965 mon->m_log_recvfd = pair[0];
1966 mon->m_log_sendfd = pair[1];
1967 } else
1968 mon->m_log_recvfd = mon->m_log_sendfd = -1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001969}
1970
1971#define MM_MEMSIZE 65536
1972
1973struct monitor *
1974monitor_init(void)
1975{
1976 struct monitor *mon;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001977
Damien Miller07d86be2006-03-26 14:19:21 +11001978 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001979
Damien Miller8f0bf232011-06-20 14:42:23 +10001980 monitor_openfds(mon, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001981
1982 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001983 if (options.compression) {
1984 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1985 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001986
Damien Miller2d6b8332002-06-21 15:59:49 +10001987 /* Compression needs to share state across borders */
1988 mm_init_compression(mon->m_zlib);
1989 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001990
1991 return mon;
1992}
1993
1994void
1995monitor_reinit(struct monitor *mon)
1996{
Damien Miller8f0bf232011-06-20 14:42:23 +10001997 monitor_openfds(mon, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001998}
Darren Tucker0efd1552003-08-26 11:49:55 +10001999
2000#ifdef GSSAPI
2001int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002002mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002003{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002004 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10002005 OM_uint32 major;
2006 u_int len;
2007
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002008 goid.elements = buffer_get_string(m, &len);
2009 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002010
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002011 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10002012
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002013 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10002014
2015 buffer_clear(m);
2016 buffer_put_int(m, major);
2017
Damien Miller6fd6def2005-11-05 15:07:05 +11002018 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002019
2020 /* Now we have a context, enable the step */
2021 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2022
2023 return (0);
2024}
2025
2026int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002027mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002028{
2029 gss_buffer_desc in;
2030 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11002031 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10002032 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10002033 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002034
Darren Tucker600ad8d2003-08-26 12:10:48 +10002035 in.value = buffer_get_string(m, &len);
2036 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10002037 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2038 xfree(in.value);
2039
2040 buffer_clear(m);
2041 buffer_put_int(m, major);
2042 buffer_put_string(m, out.value, out.length);
2043 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002044 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002045
2046 gss_release_buffer(&minor, &out);
2047
Damien Miller6fd6def2005-11-05 15:07:05 +11002048 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10002049 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2050 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11002051 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10002052 }
2053 return (0);
2054}
2055
2056int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002057mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11002058{
2059 gss_buffer_desc gssbuf, mic;
2060 OM_uint32 ret;
2061 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002062
Damien Miller0425d402003-11-17 22:18:21 +11002063 gssbuf.value = buffer_get_string(m, &len);
2064 gssbuf.length = len;
2065 mic.value = buffer_get_string(m, &len);
2066 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11002067
Damien Miller0425d402003-11-17 22:18:21 +11002068 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11002069
Damien Miller0425d402003-11-17 22:18:21 +11002070 xfree(gssbuf.value);
2071 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11002072
Damien Miller0425d402003-11-17 22:18:21 +11002073 buffer_clear(m);
2074 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11002075
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002076 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11002077
Damien Miller0425d402003-11-17 22:18:21 +11002078 if (!GSS_ERROR(ret))
2079 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11002080
Damien Miller0425d402003-11-17 22:18:21 +11002081 return (0);
2082}
2083
2084int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002085mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10002086{
2087 int authenticated;
2088
2089 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2090
2091 buffer_clear(m);
2092 buffer_put_int(m, authenticated);
2093
2094 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10002095 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10002096
Damien Miller6fd6def2005-11-05 15:07:05 +11002097 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10002098
2099 /* Monitor loop will terminate if authenticated */
2100 return (authenticated);
2101}
2102#endif /* GSSAPI */
Damien Miller01ed2272008-11-05 16:20:46 +11002103
2104#ifdef JPAKE
2105int
2106mm_answer_jpake_step1(int sock, Buffer *m)
2107{
2108 struct jpake_ctx *pctx;
2109 u_char *x3_proof, *x4_proof;
2110 u_int x3_proof_len, x4_proof_len;
2111
2112 if (!options.zero_knowledge_password_authentication)
2113 fatal("zero_knowledge_password_authentication disabled");
2114
2115 if (authctxt->jpake_ctx != NULL)
2116 fatal("%s: authctxt->jpake_ctx already set (%p)",
2117 __func__, authctxt->jpake_ctx);
2118 authctxt->jpake_ctx = pctx = jpake_new();
2119
2120 jpake_step1(pctx->grp,
2121 &pctx->server_id, &pctx->server_id_len,
2122 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2123 &x3_proof, &x3_proof_len,
2124 &x4_proof, &x4_proof_len);
2125
2126 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2127
2128 buffer_clear(m);
2129
2130 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2131 buffer_put_bignum2(m, pctx->g_x3);
2132 buffer_put_bignum2(m, pctx->g_x4);
2133 buffer_put_string(m, x3_proof, x3_proof_len);
2134 buffer_put_string(m, x4_proof, x4_proof_len);
2135
2136 debug3("%s: sending step1", __func__);
2137 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2138
2139 bzero(x3_proof, x3_proof_len);
2140 bzero(x4_proof, x4_proof_len);
2141 xfree(x3_proof);
2142 xfree(x4_proof);
2143
2144 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2145 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2146
2147 return 0;
2148}
2149
2150int
2151mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2152{
2153 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2154 char *hash_scheme, *salt;
2155
2156 if (pctx == NULL)
2157 fatal("%s: pctx == NULL", __func__);
2158
2159 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2160
2161 buffer_clear(m);
2162 /* pctx->s is sensitive, not returned to slave */
2163 buffer_put_cstring(m, hash_scheme);
2164 buffer_put_cstring(m, salt);
2165
2166 debug3("%s: sending pwdata", __func__);
2167 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2168
2169 bzero(hash_scheme, strlen(hash_scheme));
2170 bzero(salt, strlen(salt));
2171 xfree(hash_scheme);
2172 xfree(salt);
2173
2174 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2175
2176 return 0;
2177}
2178
2179int
2180mm_answer_jpake_step2(int sock, Buffer *m)
2181{
2182 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2183 u_char *x1_proof, *x2_proof, *x4_s_proof;
2184 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2185
2186 if (pctx == NULL)
2187 fatal("%s: pctx == NULL", __func__);
2188
2189 if ((pctx->g_x1 = BN_new()) == NULL ||
2190 (pctx->g_x2 = BN_new()) == NULL)
2191 fatal("%s: BN_new", __func__);
2192 buffer_get_bignum2(m, pctx->g_x1);
2193 buffer_get_bignum2(m, pctx->g_x2);
2194 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2195 x1_proof = buffer_get_string(m, &x1_proof_len);
2196 x2_proof = buffer_get_string(m, &x2_proof_len);
2197
2198 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2199 pctx->g_x1, pctx->g_x2, pctx->x4,
2200 pctx->client_id, pctx->client_id_len,
2201 pctx->server_id, pctx->server_id_len,
2202 x1_proof, x1_proof_len,
2203 x2_proof, x2_proof_len,
2204 &pctx->b,
2205 &x4_s_proof, &x4_s_proof_len);
2206
2207 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2208
2209 bzero(x1_proof, x1_proof_len);
2210 bzero(x2_proof, x2_proof_len);
2211 xfree(x1_proof);
2212 xfree(x2_proof);
2213
2214 buffer_clear(m);
2215
2216 buffer_put_bignum2(m, pctx->b);
2217 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2218
2219 debug3("%s: sending step2", __func__);
2220 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2221
2222 bzero(x4_s_proof, x4_s_proof_len);
2223 xfree(x4_s_proof);
2224
2225 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2226
2227 return 0;
2228}
2229
2230int
2231mm_answer_jpake_key_confirm(int sock, Buffer *m)
2232{
2233 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2234 u_char *x2_s_proof;
2235 u_int x2_s_proof_len;
2236
2237 if (pctx == NULL)
2238 fatal("%s: pctx == NULL", __func__);
2239
2240 if ((pctx->a = BN_new()) == NULL)
2241 fatal("%s: BN_new", __func__);
2242 buffer_get_bignum2(m, pctx->a);
2243 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2244
2245 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2246 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2247 pctx->server_id, pctx->server_id_len,
2248 pctx->client_id, pctx->client_id_len,
2249 session_id2, session_id2_len,
2250 x2_s_proof, x2_s_proof_len,
2251 &pctx->k,
2252 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2253
2254 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2255
2256 bzero(x2_s_proof, x2_s_proof_len);
2257 buffer_clear(m);
2258
2259 /* pctx->k is sensitive, not sent */
2260 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2261
2262 debug3("%s: sending confirmation hash", __func__);
2263 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2264
2265 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2266
2267 return 0;
2268}
2269
2270int
2271mm_answer_jpake_check_confirm(int sock, Buffer *m)
2272{
2273 int authenticated = 0;
2274 u_char *peer_confirm_hash;
2275 u_int peer_confirm_hash_len;
2276 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2277
2278 if (pctx == NULL)
2279 fatal("%s: pctx == NULL", __func__);
2280
2281 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2282
2283 authenticated = jpake_check_confirm(pctx->k,
2284 pctx->client_id, pctx->client_id_len,
2285 session_id2, session_id2_len,
2286 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2287
2288 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2289
2290 bzero(peer_confirm_hash, peer_confirm_hash_len);
2291 xfree(peer_confirm_hash);
2292
2293 buffer_clear(m);
2294 buffer_put_int(m, authenticated);
2295
2296 debug3("%s: sending result %d", __func__, authenticated);
2297 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2298
2299 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2300
2301 auth_method = "jpake-01@openssh.com";
2302 return authenticated;
2303}
2304
2305#endif /* JPAKE */