blob: 5df45ce457b79dbbe948e7ce3b439f556a9fdde5 [file] [log] [blame]
Damien Miller9ab00b42006-08-05 12:40:11 +10001/* $OpenBSD: monitor.c,v 1.86 2006/08/04 20:46:05 stevesk 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 Millere7a1e5c2006-08-05 11:34:19 +100043#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <string.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000045
46#ifdef SKEY
47#include <skey.h>
48#endif
49
Damien Miller03e20032006-03-15 11:16:59 +110050#include <openssl/dh.h>
51
Damien Millerd7834352006-08-05 12:39:39 +100052#include "xmalloc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000053#include "ssh.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "key.h"
55#include "buffer.h"
56#include "hostfile.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000057#include "auth.h"
Damien Millerd7834352006-08-05 12:39:39 +100058#include "cipher.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "kex.h"
60#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000061#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
62#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000063#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000064#define TARGET_OS_MAC 1
65#else
66#include "zlib.h"
67#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000068#include "packet.h"
69#include "auth-options.h"
70#include "sshpty.h"
71#include "channels.h"
72#include "session.h"
73#include "sshlogin.h"
74#include "canohost.h"
75#include "log.h"
76#include "servconf.h"
77#include "monitor.h"
78#include "monitor_mm.h"
Damien Millerd7834352006-08-05 12:39:39 +100079#ifdef GSSAPI
80#include "ssh-gss.h"
81#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082#include "monitor_wrap.h"
83#include "monitor_fdpass.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084#include "misc.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085#include "compat.h"
86#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000087
Darren Tucker0efd1552003-08-26 11:49:55 +100088#ifdef GSSAPI
Darren Tucker0efd1552003-08-26 11:49:55 +100089static Gssctxt *gsscontext = NULL;
90#endif
91
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092/* Imports */
93extern ServerOptions options;
94extern u_int utmp_len;
95extern Newkeys *current_keys[];
96extern z_stream incoming_stream;
97extern z_stream outgoing_stream;
98extern u_char session_id[];
99extern Buffer input, output;
100extern Buffer auth_debug;
101extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +1000102extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000103
104/* State exported from the child */
105
106struct {
107 z_stream incoming;
108 z_stream outgoing;
109 u_char *keyin;
110 u_int keyinlen;
111 u_char *keyout;
112 u_int keyoutlen;
113 u_char *ivin;
114 u_int ivinlen;
115 u_char *ivout;
116 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000117 u_char *ssh1key;
118 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119 int ssh1cipher;
120 int ssh1protoflags;
121 u_char *input;
122 u_int ilen;
123 u_char *output;
124 u_int olen;
125} child_state;
126
Damien Miller469954d2003-06-18 20:25:33 +1000127/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000128
129int mm_answer_moduli(int, Buffer *);
130int mm_answer_sign(int, Buffer *);
131int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000132int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000133int mm_answer_authserv(int, Buffer *);
134int mm_answer_authpassword(int, Buffer *);
135int mm_answer_bsdauthquery(int, Buffer *);
136int mm_answer_bsdauthrespond(int, Buffer *);
137int mm_answer_skeyquery(int, Buffer *);
138int mm_answer_skeyrespond(int, Buffer *);
139int mm_answer_keyallowed(int, Buffer *);
140int mm_answer_keyverify(int, Buffer *);
141int mm_answer_pty(int, Buffer *);
142int mm_answer_pty_cleanup(int, Buffer *);
143int mm_answer_term(int, Buffer *);
144int mm_answer_rsa_keyallowed(int, Buffer *);
145int mm_answer_rsa_challenge(int, Buffer *);
146int mm_answer_rsa_response(int, Buffer *);
147int mm_answer_sesskey(int, Buffer *);
148int mm_answer_sessid(int, Buffer *);
149
Damien Miller79418552002-04-23 20:28:48 +1000150#ifdef USE_PAM
151int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000152int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000153int mm_answer_pam_init_ctx(int, Buffer *);
154int mm_answer_pam_query(int, Buffer *);
155int mm_answer_pam_respond(int, Buffer *);
156int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000157#endif
158
Darren Tucker0efd1552003-08-26 11:49:55 +1000159#ifdef GSSAPI
160int mm_answer_gss_setup_ctx(int, Buffer *);
161int mm_answer_gss_accept_ctx(int, Buffer *);
162int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100163int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000164#endif
Damien Miller25162f22002-09-12 09:47:29 +1000165
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100166#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100167int mm_answer_audit_event(int, Buffer *);
168int mm_answer_audit_command(int, Buffer *);
169#endif
170
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000171static Authctxt *authctxt;
172static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
173
174/* local state for key verify */
175static u_char *key_blob = NULL;
176static u_int key_bloblen = 0;
177static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000178static char *hostbased_cuser = NULL;
179static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000180static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000181static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000182static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000183static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000184
185struct mon_table {
186 enum monitor_reqtype type;
187 int flags;
188 int (*f)(int, Buffer *);
189};
190
191#define MON_ISAUTH 0x0004 /* Required for Authentication */
192#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
193#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100194#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000195
196#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
197
198#define MON_PERMIT 0x1000 /* Request is permitted */
199
200struct mon_table mon_dispatch_proto20[] = {
201 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
202 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
203 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
204 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000205 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000207#ifdef USE_PAM
208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000209 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000210 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
211 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
212 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
213 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000214#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100215#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100216 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100217#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000218#ifdef BSD_AUTH
219 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100220 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000221#endif
222#ifdef SKEY
223 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
224 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
225#endif
226 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
227 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000228#ifdef GSSAPI
229 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
230 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
231 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100232 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000234 {0, 0, NULL}
235};
236
237struct mon_table mon_dispatch_postauth20[] = {
238 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
239 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
240 {MONITOR_REQ_PTY, 0, mm_answer_pty},
241 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
242 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100243#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100244 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
245 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
246#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247 {0, 0, NULL}
248};
249
250struct mon_table mon_dispatch_proto15[] = {
251 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
252 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
253 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
254 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100255 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
256 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
258 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
259#ifdef BSD_AUTH
260 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100261 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000262#endif
263#ifdef SKEY
264 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
265 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
266#endif
Damien Millera33501b2002-05-08 12:24:42 +1000267#ifdef USE_PAM
268 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000269 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000270 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
271 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
272 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
273 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000274#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100275#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100276 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100277#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000278 {0, 0, NULL}
279};
280
281struct mon_table mon_dispatch_postauth15[] = {
282 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
283 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
284 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100285#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100286 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100287 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100288#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000289 {0, 0, NULL}
290};
291
292struct mon_table *mon_dispatch;
293
294/* Specifies if a certain message is allowed at the moment */
295
296static void
297monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
298{
299 while (ent->f != NULL) {
300 if (ent->type == type) {
301 ent->flags &= ~MON_PERMIT;
302 ent->flags |= permit ? MON_PERMIT : 0;
303 return;
304 }
305 ent++;
306 }
307}
308
309static void
310monitor_permit_authentications(int permit)
311{
312 struct mon_table *ent = mon_dispatch;
313
314 while (ent->f != NULL) {
315 if (ent->flags & MON_AUTH) {
316 ent->flags &= ~MON_PERMIT;
317 ent->flags |= permit ? MON_PERMIT : 0;
318 }
319 ent++;
320 }
321}
322
Darren Tucker3e33cec2003-10-02 16:12:36 +1000323void
324monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325{
326 struct mon_table *ent;
327 int authenticated = 0;
328
329 debug3("preauth child monitor started");
330
Darren Tucker3e33cec2003-10-02 16:12:36 +1000331 authctxt = _authctxt;
332 memset(authctxt, 0, sizeof(*authctxt));
333
Darren Tuckerde0de392005-03-31 23:52:04 +1000334 authctxt->loginmsg = &loginmsg;
335
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 if (compat20) {
337 mon_dispatch = mon_dispatch_proto20;
338
339 /* Permit requests for moduli and signatures */
340 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
341 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
342 } else {
343 mon_dispatch = mon_dispatch_proto15;
344
345 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
346 }
347
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 /* The first few requests do not require asynchronous access */
349 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100350 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000351 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000352 if (authenticated) {
353 if (!(ent->flags & MON_AUTHDECIDE))
354 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000355 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 if (authctxt->pw->pw_uid == 0 &&
357 !auth_root_allowed(auth_method))
358 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000359#ifdef USE_PAM
360 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100361 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000362 Buffer m;
363
364 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100365 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000366 MONITOR_REQ_PAM_ACCOUNT, &m);
367 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
368 buffer_free(&m);
369 }
370#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371 }
372
Damien Miller7a8f5b32006-03-31 23:14:23 +1100373 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000374 auth_log(authctxt, authenticated, auth_method,
375 compat20 ? " ssh2" : "");
376 if (!authenticated)
377 authctxt->failures++;
378 }
379 }
380
381 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000382 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100383 if (strcmp(auth_method, "unknown") == 0)
384 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385
386 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000389 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000390}
391
Damien Millerbe64d432003-05-14 19:31:12 +1000392static void
393monitor_set_child_handler(pid_t pid)
394{
395 monitor_child_pid = pid;
396}
397
398static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000399monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000400{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000401 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000402}
403
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000404void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000405monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000406{
Damien Millerbe64d432003-05-14 19:31:12 +1000407 monitor_set_child_handler(pmonitor->m_pid);
408 signal(SIGHUP, &monitor_child_handler);
409 signal(SIGTERM, &monitor_child_handler);
410
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411 if (compat20) {
412 mon_dispatch = mon_dispatch_postauth20;
413
414 /* Permit requests for moduli and signatures */
415 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
416 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
417 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418 } else {
419 mon_dispatch = mon_dispatch_postauth15;
420 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
421 }
422 if (!no_pty_flag) {
423 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
424 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
425 }
426
427 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000428 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000429}
430
431void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433{
Damien Miller2d6b8332002-06-21 15:59:49 +1000434 if (options.compression) {
435 /* The member allocation is not visible, so sync it */
436 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
437 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000438}
439
440int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000441monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442 struct mon_table **pent)
443{
444 Buffer m;
445 int ret;
446 u_char type;
447
448 buffer_init(&m);
449
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000450 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000451 type = buffer_get_char(&m);
452
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000453 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000454
455 while (ent->f != NULL) {
456 if (ent->type == type)
457 break;
458 ent++;
459 }
460
461 if (ent->f != NULL) {
462 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000463 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000465 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466 buffer_free(&m);
467
468 /* The child may use this request only once, disable it */
469 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000470 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471 type);
472 ent->flags &= ~MON_PERMIT;
473 }
474
475 if (pent != NULL)
476 *pent = ent;
477
478 return ret;
479 }
480
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000481 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482
483 /* NOTREACHED */
484 return (-1);
485}
486
487/* allowed key state */
488static int
489monitor_allowed_key(u_char *blob, u_int bloblen)
490{
491 /* make sure key is allowed */
492 if (key_blob == NULL || key_bloblen != bloblen ||
493 memcmp(key_blob, blob, key_bloblen))
494 return (0);
495 return (1);
496}
497
498static void
499monitor_reset_key_state(void)
500{
501 /* reset state */
502 if (key_blob != NULL)
503 xfree(key_blob);
504 if (hostbased_cuser != NULL)
505 xfree(hostbased_cuser);
506 if (hostbased_chost != NULL)
507 xfree(hostbased_chost);
508 key_blob = NULL;
509 key_bloblen = 0;
510 key_blobtype = MM_NOKEY;
511 hostbased_cuser = NULL;
512 hostbased_chost = NULL;
513}
514
515int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000516mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517{
518 DH *dh;
519 int min, want, max;
520
521 min = buffer_get_int(m);
522 want = buffer_get_int(m);
523 max = buffer_get_int(m);
524
525 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000526 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 /* We need to check here, too, in case the child got corrupted */
528 if (max < min || want < min || max < want)
529 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000530 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000531
532 buffer_clear(m);
533
534 dh = choose_dh(min, want, max);
535 if (dh == NULL) {
536 buffer_put_char(m, 0);
537 return (0);
538 } else {
539 /* Send first bignum */
540 buffer_put_char(m, 1);
541 buffer_put_bignum2(m, dh->p);
542 buffer_put_bignum2(m, dh->g);
543
544 DH_free(dh);
545 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000546 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547 return (0);
548}
549
550int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000551mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552{
553 Key *key;
554 u_char *p;
555 u_char *signature;
556 u_int siglen, datlen;
557 int keyid;
558
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000559 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560
561 keyid = buffer_get_int(m);
562 p = buffer_get_string(m, &datlen);
563
Damien Millera63128d2006-03-15 12:08:28 +1100564 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100565 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100566 * SHA256 (32 byte) hashes
567 */
568 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000569 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
Ben Lindstromf67e0772002-06-06 20:58:19 +0000571 /* save session id, it will be passed on the first call */
572 if (session_id2_len == 0) {
573 session_id2_len = datlen;
574 session_id2 = xmalloc(session_id2_len);
575 memcpy(session_id2, p, session_id2_len);
576 }
577
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000578 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000579 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000581 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000582
Ben Lindstromd5502182002-06-27 00:12:57 +0000583 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
585 buffer_clear(m);
586 buffer_put_string(m, signature, siglen);
587
588 xfree(p);
589 xfree(signature);
590
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000591 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 /* Turn on permissions for getpwnam */
594 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
595
596 return (0);
597}
598
599/* Retrieves the password entry and also checks if the user is permitted */
600
601int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000602mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000604 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605 struct passwd *pwent;
606 int allowed = 0;
607
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000608 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609
610 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000611 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
Darren Tuckerb09b6772004-06-22 15:06:46 +1000613 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000614
Darren Tuckerb09b6772004-06-22 15:06:46 +1000615 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000616
Darren Tuckerb09b6772004-06-22 15:06:46 +1000617 authctxt->user = xstrdup(username);
618 setproctitle("%s [priv]", pwent ? username : "unknown");
619 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000620
621 buffer_clear(m);
622
623 if (pwent == NULL) {
624 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100625 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000626 goto out;
627 }
628
629 allowed = 1;
630 authctxt->pw = pwent;
631 authctxt->valid = 1;
632
633 buffer_put_char(m, 1);
634 buffer_put_string(m, pwent, sizeof(struct passwd));
635 buffer_put_cstring(m, pwent->pw_name);
636 buffer_put_cstring(m, "*");
637 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000638#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000640#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000641 buffer_put_cstring(m, pwent->pw_dir);
642 buffer_put_cstring(m, pwent->pw_shell);
643
644 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000645 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000646 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000647
648 /* For SSHv1 allow authentication now */
649 if (!compat20)
650 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000651 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000652 /* Allow service/style information on the auth context */
653 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000654 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
655 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656
Damien Millera33501b2002-05-08 12:24:42 +1000657#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000658 if (options.use_pam)
659 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000660#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100661#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100662 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100663#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664
665 return (0);
666}
667
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000668int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000669{
670 char *banner;
671
672 buffer_clear(m);
673 banner = auth2_read_banner();
674 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000675 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000676
677 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000678 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000679
680 return (0);
681}
682
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685{
686 monitor_permit_authentications(1);
687
688 authctxt->service = buffer_get_string(m, NULL);
689 authctxt->style = buffer_get_string(m, NULL);
690 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000691 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692
693 if (strlen(authctxt->style) == 0) {
694 xfree(authctxt->style);
695 authctxt->style = NULL;
696 }
697
698 return (0);
699}
700
701int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000702mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703{
704 static int call_count;
705 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000706 int authenticated;
707 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000708
709 passwd = buffer_get_string(m, &plen);
710 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000711 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000712 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713 memset(passwd, 0, strlen(passwd));
714 xfree(passwd);
715
716 buffer_clear(m);
717 buffer_put_int(m, authenticated);
718
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000719 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000720 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000721
722 call_count++;
723 if (plen == 0 && call_count == 1)
724 auth_method = "none";
725 else
726 auth_method = "password";
727
728 /* Causes monitor loop to terminate if authenticated */
729 return (authenticated);
730}
731
732#ifdef BSD_AUTH
733int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000734mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735{
736 char *name, *infotxt;
737 u_int numprompts;
738 u_int *echo_on;
739 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100740 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000741
Damien Millerb7df3af2003-02-24 11:55:46 +1100742 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
743 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744
745 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100746 buffer_put_int(m, success);
747 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 buffer_put_cstring(m, prompts[0]);
749
Damien Millerb7df3af2003-02-24 11:55:46 +1100750 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000751 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752
Damien Millerb7df3af2003-02-24 11:55:46 +1100753 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000754 xfree(name);
755 xfree(infotxt);
756 xfree(prompts);
757 xfree(echo_on);
758 }
759
760 return (0);
761}
762
763int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000764mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000765{
766 char *response;
767 int authok;
768
769 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000770 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000771
772 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000773 authok = options.challenge_response_authentication &&
774 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000775 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000776 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000777 xfree(response);
778
779 buffer_clear(m);
780 buffer_put_int(m, authok);
781
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000782 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000783 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000784
785 auth_method = "bsdauth";
786
787 return (authok != 0);
788}
789#endif
790
791#ifdef SKEY
792int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000793mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000794{
795 struct skey skey;
796 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100797 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000798
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000799 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
800 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801
802 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100803 buffer_put_int(m, success);
804 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805 buffer_put_cstring(m, challenge);
806
Damien Millerb7df3af2003-02-24 11:55:46 +1100807 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000808 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000809
810 return (0);
811}
812
813int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000814mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815{
816 char *response;
817 int authok;
818
819 response = buffer_get_string(m, NULL);
820
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000821 authok = (options.challenge_response_authentication &&
822 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000823 skey_haskey(authctxt->pw->pw_name) == 0 &&
824 skey_passcheck(authctxt->pw->pw_name, response) != -1);
825
826 xfree(response);
827
828 buffer_clear(m);
829 buffer_put_int(m, authok);
830
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000831 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000832 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000833
834 auth_method = "skey";
835
836 return (authok != 0);
837}
838#endif
839
Damien Miller79418552002-04-23 20:28:48 +1000840#ifdef USE_PAM
841int
Darren Tucker7eda5922004-06-22 13:22:50 +1000842mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000843{
Damien Miller4e448a32003-05-14 15:11:48 +1000844 if (!options.use_pam)
845 fatal("UsePAM not set, but ended up in %s anyway", __func__);
846
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100847 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000848
Damien Miller1f499fd2003-08-25 13:08:49 +1000849 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
850
Damien Miller79418552002-04-23 20:28:48 +1000851 return (0);
852}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000853
Damien Miller1f499fd2003-08-25 13:08:49 +1000854int
Darren Tucker7eda5922004-06-22 13:22:50 +1000855mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000856{
857 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100858
Damien Miller1f499fd2003-08-25 13:08:49 +1000859 if (!options.use_pam)
860 fatal("UsePAM not set, but ended up in %s anyway", __func__);
861
862 ret = do_pam_account();
863
864 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000865 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000866
Darren Tucker7eda5922004-06-22 13:22:50 +1000867 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000868
869 return (ret);
870}
871
Damien Miller4f9f42a2003-05-10 19:28:02 +1000872static void *sshpam_ctxt, *sshpam_authok;
873extern KbdintDevice sshpam_device;
874
875int
Darren Tucker7eda5922004-06-22 13:22:50 +1000876mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000877{
878
879 debug3("%s", __func__);
880 authctxt->user = buffer_get_string(m, NULL);
881 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
882 sshpam_authok = NULL;
883 buffer_clear(m);
884 if (sshpam_ctxt != NULL) {
885 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
886 buffer_put_int(m, 1);
887 } else {
888 buffer_put_int(m, 0);
889 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000890 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000891 return (0);
892}
893
894int
Darren Tucker7eda5922004-06-22 13:22:50 +1000895mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000896{
897 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000898 u_int i, num, *echo_on;
899 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000900
901 debug3("%s", __func__);
902 sshpam_authok = NULL;
903 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
904 if (ret == 0 && num == 0)
905 sshpam_authok = sshpam_ctxt;
906 if (num > 1 || name == NULL || info == NULL)
907 ret = -1;
908 buffer_clear(m);
909 buffer_put_int(m, ret);
910 buffer_put_cstring(m, name);
911 xfree(name);
912 buffer_put_cstring(m, info);
913 xfree(info);
914 buffer_put_int(m, num);
915 for (i = 0; i < num; ++i) {
916 buffer_put_cstring(m, prompts[i]);
917 xfree(prompts[i]);
918 buffer_put_int(m, echo_on[i]);
919 }
920 if (prompts != NULL)
921 xfree(prompts);
922 if (echo_on != NULL)
923 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000924 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000925 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000926 return (0);
927}
928
929int
Darren Tucker7eda5922004-06-22 13:22:50 +1000930mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000931{
932 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000933 u_int i, num;
934 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000935
936 debug3("%s", __func__);
937 sshpam_authok = NULL;
938 num = buffer_get_int(m);
939 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000940 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000941 for (i = 0; i < num; ++i)
942 resp[i] = buffer_get_string(m, NULL);
943 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
944 for (i = 0; i < num; ++i)
945 xfree(resp[i]);
946 xfree(resp);
947 } else {
948 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
949 }
950 buffer_clear(m);
951 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000952 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000953 auth_method = "keyboard-interactive/pam";
954 if (ret == 0)
955 sshpam_authok = sshpam_ctxt;
956 return (0);
957}
958
959int
Darren Tucker7eda5922004-06-22 13:22:50 +1000960mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000961{
962
963 debug3("%s", __func__);
964 (sshpam_device.free_ctx)(sshpam_ctxt);
965 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000966 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000967 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000968 return (sshpam_authok == sshpam_ctxt);
969}
Damien Miller79418552002-04-23 20:28:48 +1000970#endif
971
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972static void
973mm_append_debug(Buffer *m)
974{
975 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977 buffer_append(m, buffer_ptr(&auth_debug),
978 buffer_len(&auth_debug));
979 buffer_clear(&auth_debug);
980 }
981}
982
983int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000984mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985{
986 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000987 char *cuser, *chost;
988 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989 u_int bloblen;
990 enum mm_keytype type = 0;
991 int allowed = 0;
992
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000993 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000994
995 type = buffer_get_int(m);
996 cuser = buffer_get_string(m, NULL);
997 chost = buffer_get_string(m, NULL);
998 blob = buffer_get_string(m, &bloblen);
999
1000 key = key_from_blob(blob, bloblen);
1001
1002 if ((compat20 && type == MM_RSAHOSTKEY) ||
1003 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001004 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001006 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007
Damien Miller3e3b5142003-11-17 21:13:40 +11001008 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001009 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001011 allowed = options.pubkey_authentication &&
1012 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001013 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001014 break;
1015 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001016 allowed = options.hostbased_authentication &&
1017 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001019 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020 break;
1021 case MM_RSAHOSTKEY:
1022 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001023 allowed = options.rhosts_rsa_authentication &&
1024 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001026 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 break;
1028 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001029 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030 break;
1031 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001032 }
Damien Miller00111382003-03-10 11:21:17 +11001033 if (key != NULL)
1034 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001035
1036 /* clear temporarily storage (used by verify) */
1037 monitor_reset_key_state();
1038
1039 if (allowed) {
1040 /* Save temporarily for comparison in verify */
1041 key_blob = blob;
1042 key_bloblen = bloblen;
1043 key_blobtype = type;
1044 hostbased_cuser = cuser;
1045 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001046 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001047 /* Log failed attempt */
1048 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001049 xfree(blob);
1050 xfree(cuser);
1051 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052 }
1053
1054 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001055 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056
1057 buffer_clear(m);
1058 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001059 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060
1061 mm_append_debug(m);
1062
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001063 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064
1065 if (type == MM_RSAHOSTKEY)
1066 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1067
1068 return (0);
1069}
1070
1071static int
1072monitor_valid_userblob(u_char *data, u_int datalen)
1073{
1074 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001075 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001076 u_int len;
1077 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001078
1079 buffer_init(&b);
1080 buffer_append(&b, data, datalen);
1081
1082 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001083 p = buffer_ptr(&b);
1084 len = buffer_len(&b);
1085 if ((session_id2 == NULL) ||
1086 (len < session_id2_len) ||
1087 (memcmp(p, session_id2, session_id2_len) != 0))
1088 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001089 buffer_consume(&b, session_id2_len);
1090 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001091 p = buffer_get_string(&b, &len);
1092 if ((session_id2 == NULL) ||
1093 (len != session_id2_len) ||
1094 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001095 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001096 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001097 }
1098 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1099 fail++;
1100 p = buffer_get_string(&b, NULL);
1101 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001102 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001103 authctxt->user, p);
1104 fail++;
1105 }
1106 xfree(p);
1107 buffer_skip_string(&b);
1108 if (datafellows & SSH_BUG_PKAUTH) {
1109 if (!buffer_get_char(&b))
1110 fail++;
1111 } else {
1112 p = buffer_get_string(&b, NULL);
1113 if (strcmp("publickey", p) != 0)
1114 fail++;
1115 xfree(p);
1116 if (!buffer_get_char(&b))
1117 fail++;
1118 buffer_skip_string(&b);
1119 }
1120 buffer_skip_string(&b);
1121 if (buffer_len(&b) != 0)
1122 fail++;
1123 buffer_free(&b);
1124 return (fail == 0);
1125}
1126
1127static int
Damien Millera10f5612002-09-12 09:49:15 +10001128monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1129 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130{
1131 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001132 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001133 u_int len;
1134 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135
1136 buffer_init(&b);
1137 buffer_append(&b, data, datalen);
1138
Ben Lindstromf67e0772002-06-06 20:58:19 +00001139 p = buffer_get_string(&b, &len);
1140 if ((session_id2 == NULL) ||
1141 (len != session_id2_len) ||
1142 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001144 xfree(p);
1145
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1147 fail++;
1148 p = buffer_get_string(&b, NULL);
1149 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001150 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151 authctxt->user, p);
1152 fail++;
1153 }
1154 xfree(p);
1155 buffer_skip_string(&b); /* service */
1156 p = buffer_get_string(&b, NULL);
1157 if (strcmp(p, "hostbased") != 0)
1158 fail++;
1159 xfree(p);
1160 buffer_skip_string(&b); /* pkalg */
1161 buffer_skip_string(&b); /* pkblob */
1162
1163 /* verify client host, strip trailing dot if necessary */
1164 p = buffer_get_string(&b, NULL);
1165 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1166 p[len - 1] = '\0';
1167 if (strcmp(p, chost) != 0)
1168 fail++;
1169 xfree(p);
1170
1171 /* verify client user */
1172 p = buffer_get_string(&b, NULL);
1173 if (strcmp(p, cuser) != 0)
1174 fail++;
1175 xfree(p);
1176
1177 if (buffer_len(&b) != 0)
1178 fail++;
1179 buffer_free(&b);
1180 return (fail == 0);
1181}
1182
1183int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001184mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185{
1186 Key *key;
1187 u_char *signature, *data, *blob;
1188 u_int signaturelen, datalen, bloblen;
1189 int verified = 0;
1190 int valid_data = 0;
1191
1192 blob = buffer_get_string(m, &bloblen);
1193 signature = buffer_get_string(m, &signaturelen);
1194 data = buffer_get_string(m, &datalen);
1195
1196 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001197 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001198 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199
1200 key = key_from_blob(blob, bloblen);
1201 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001202 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203
1204 switch (key_blobtype) {
1205 case MM_USERKEY:
1206 valid_data = monitor_valid_userblob(data, datalen);
1207 break;
1208 case MM_HOSTKEY:
1209 valid_data = monitor_valid_hostbasedblob(data, datalen,
1210 hostbased_cuser, hostbased_chost);
1211 break;
1212 default:
1213 valid_data = 0;
1214 break;
1215 }
1216 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001217 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001218
1219 verified = key_verify(key, signature, signaturelen, data, datalen);
1220 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001221 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222
1223 key_free(key);
1224 xfree(blob);
1225 xfree(signature);
1226 xfree(data);
1227
Ben Lindstrome1c09122002-06-23 00:38:24 +00001228 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1229
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001230 monitor_reset_key_state();
1231
1232 buffer_clear(m);
1233 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001234 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236 return (verified);
1237}
1238
1239static void
1240mm_record_login(Session *s, struct passwd *pw)
1241{
1242 socklen_t fromlen;
1243 struct sockaddr_storage from;
1244
1245 /*
1246 * Get IP address of client. If the connection is not a socket, let
1247 * the address be 0.0.0.0.
1248 */
1249 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001250 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001251 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001253 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001255 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001256 }
1257 }
1258 /* Record that there was a login on that tty from the remote host. */
1259 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001260 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001261 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262}
1263
1264static void
1265mm_session_close(Session *s)
1266{
Damien Miller04bd8b02003-05-25 14:38:33 +10001267 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268 if (s->ttyfd != -1) {
Damien Miller9ab00b42006-08-05 12:40:11 +10001269 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270 session_pty_cleanup2(s);
1271 }
1272 s->used = 0;
1273}
1274
1275int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001276mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001277{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001278 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001279 Session *s;
1280 int res, fd0;
1281
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001282 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283
1284 buffer_clear(m);
1285 s = session_new();
1286 if (s == NULL)
1287 goto error;
1288 s->authctxt = authctxt;
1289 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001290 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1292 if (res == 0)
1293 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001294 pty_setowner(authctxt->pw, s->tty);
1295
1296 buffer_put_int(m, 1);
1297 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001298
1299 /* We need to trick ttyslot */
1300 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001301 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302
1303 mm_record_login(s, authctxt->pw);
1304
1305 /* Now we can close the file descriptor again */
1306 close(0);
1307
Darren Tucker09991742004-07-17 17:05:14 +10001308 /* send messages generated by record_login */
1309 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1310 buffer_clear(&loginmsg);
1311
1312 mm_request_send(sock, MONITOR_ANS_PTY, m);
1313
1314 mm_send_fd(sock, s->ptyfd);
1315 mm_send_fd(sock, s->ttyfd);
1316
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001317 /* make sure nothing uses fd 0 */
1318 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001319 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001321 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322
1323 /* slave is not needed */
1324 close(s->ttyfd);
1325 s->ttyfd = s->ptyfd;
1326 /* no need to dup() because nobody closes ptyfd */
1327 s->ptymaster = s->ptyfd;
1328
Damien Miller9ab00b42006-08-05 12:40:11 +10001329 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330
1331 return (0);
1332
1333 error:
1334 if (s != NULL)
1335 mm_session_close(s);
1336 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001337 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001338 return (0);
1339}
1340
1341int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001342mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001343{
1344 Session *s;
1345 char *tty;
1346
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001347 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001348
1349 tty = buffer_get_string(m, NULL);
1350 if ((s = session_by_tty(tty)) != NULL)
1351 mm_session_close(s);
1352 buffer_clear(m);
1353 xfree(tty);
1354 return (0);
1355}
1356
1357int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001358mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359{
1360 BIGNUM *p;
1361 int rsafail;
1362
1363 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001364 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365
1366 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001367 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368
1369 buffer_get_bignum2(m, p);
1370
1371 rsafail = ssh1_session_key(p);
1372
1373 buffer_clear(m);
1374 buffer_put_int(m, rsafail);
1375 buffer_put_bignum2(m, p);
1376
1377 BN_clear_free(p);
1378
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001379 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380
1381 /* Turn on permissions for sessid passing */
1382 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1383
1384 return (0);
1385}
1386
1387int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001388mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389{
1390 int i;
1391
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001392 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393
1394 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001395 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396 for (i = 0; i < 16; i++)
1397 session_id[i] = buffer_get_char(m);
1398
1399 /* Turn on permissions for getpwnam */
1400 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1401
1402 return (0);
1403}
1404
1405int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001406mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407{
1408 BIGNUM *client_n;
1409 Key *key = NULL;
1410 u_char *blob = NULL;
1411 u_int blen = 0;
1412 int allowed = 0;
1413
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001414 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001415
Damien Miller7a8f5b32006-03-31 23:14:23 +11001416 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001417 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001419 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001420 buffer_get_bignum2(m, client_n);
1421 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1422 BN_clear_free(client_n);
1423 }
1424 buffer_clear(m);
1425 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001426 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427
1428 /* clear temporarily storage (used by generate challenge) */
1429 monitor_reset_key_state();
1430
1431 if (allowed && key != NULL) {
1432 key->type = KEY_RSA; /* cheat for key_to_blob */
1433 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001434 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 buffer_put_string(m, blob, blen);
1436
1437 /* Save temporarily for comparison in verify */
1438 key_blob = blob;
1439 key_bloblen = blen;
1440 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001441 }
Damien Miller00111382003-03-10 11:21:17 +11001442 if (key != NULL)
1443 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001444
1445 mm_append_debug(m);
1446
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001447 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448
1449 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1450 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1451 return (0);
1452}
1453
1454int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001455mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001456{
1457 Key *key = NULL;
1458 u_char *blob;
1459 u_int blen;
1460
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462
1463 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001464 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 blob = buffer_get_string(m, &blen);
1466 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001467 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001471 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472
1473 if (ssh1_challenge)
1474 BN_clear_free(ssh1_challenge);
1475 ssh1_challenge = auth_rsa_generate_challenge(key);
1476
1477 buffer_clear(m);
1478 buffer_put_bignum2(m, ssh1_challenge);
1479
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001480 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001481 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482
1483 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001484
1485 xfree(blob);
1486 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001487 return (0);
1488}
1489
1490int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001491mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492{
1493 Key *key = NULL;
1494 u_char *blob, *response;
1495 u_int blen, len;
1496 int success;
1497
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001498 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499
1500 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001501 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001503 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504
1505 blob = buffer_get_string(m, &blen);
1506 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001507 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001509 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001511 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512 response = buffer_get_string(m, &len);
1513 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001514 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1516
Damien Miller00111382003-03-10 11:21:17 +11001517 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518 key_free(key);
1519 xfree(response);
1520
1521 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1522
1523 /* reset state */
1524 BN_clear_free(ssh1_challenge);
1525 ssh1_challenge = NULL;
1526 monitor_reset_key_state();
1527
1528 buffer_clear(m);
1529 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001530 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001531
1532 return (success);
1533}
1534
1535int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001536mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001537{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001538 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539 int res, status;
1540
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001541 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001542
1543 /* The child is terminating */
1544 session_destroy_all(&mm_session_close);
1545
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001546 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001547 if (errno != EINTR)
1548 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001549
1550 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1551
1552 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001553 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554}
1555
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001556#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001557/* Report that an audit event occurred */
1558int
1559mm_answer_audit_event(int socket, Buffer *m)
1560{
1561 ssh_audit_event_t event;
1562
1563 debug3("%s entering", __func__);
1564
1565 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001566 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001567 case SSH_AUTH_FAIL_PUBKEY:
1568 case SSH_AUTH_FAIL_HOSTBASED:
1569 case SSH_AUTH_FAIL_GSSAPI:
1570 case SSH_LOGIN_EXCEED_MAXTRIES:
1571 case SSH_LOGIN_ROOT_DENIED:
1572 case SSH_CONNECTION_CLOSE:
1573 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001574 audit_event(event);
1575 break;
1576 default:
1577 fatal("Audit event type %d not permitted", event);
1578 }
1579
1580 return (0);
1581}
1582
1583int
1584mm_answer_audit_command(int socket, Buffer *m)
1585{
1586 u_int len;
1587 char *cmd;
1588
1589 debug3("%s entering", __func__);
1590 cmd = buffer_get_string(m, &len);
1591 /* sanity check command, if so how? */
1592 audit_run_command(cmd);
1593 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001594 return (0);
1595}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001596#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001597
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001599monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600{
1601 if (compat20) {
1602 set_newkeys(MODE_IN);
1603 set_newkeys(MODE_OUT);
1604 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001605 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001606 packet_set_encryption_key(child_state.ssh1key,
1607 child_state.ssh1keylen, child_state.ssh1cipher);
1608 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609 }
1610
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001611 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612 packet_set_keycontext(MODE_OUT, child_state.keyout);
1613 xfree(child_state.keyout);
1614 packet_set_keycontext(MODE_IN, child_state.keyin);
1615 xfree(child_state.keyin);
1616
1617 if (!compat20) {
1618 packet_set_iv(MODE_OUT, child_state.ivout);
1619 xfree(child_state.ivout);
1620 packet_set_iv(MODE_IN, child_state.ivin);
1621 xfree(child_state.ivin);
1622 }
1623
1624 memcpy(&incoming_stream, &child_state.incoming,
1625 sizeof(incoming_stream));
1626 memcpy(&outgoing_stream, &child_state.outgoing,
1627 sizeof(outgoing_stream));
1628
1629 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001630 if (options.compression)
1631 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632
1633 /* Network I/O buffers */
1634 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1635 buffer_clear(&input);
1636 buffer_append(&input, child_state.input, child_state.ilen);
1637 memset(child_state.input, 0, child_state.ilen);
1638 xfree(child_state.input);
1639
1640 buffer_clear(&output);
1641 buffer_append(&output, child_state.output, child_state.olen);
1642 memset(child_state.output, 0, child_state.olen);
1643 xfree(child_state.output);
1644}
1645
1646static Kex *
1647mm_get_kex(Buffer *m)
1648{
1649 Kex *kex;
1650 void *blob;
1651 u_int bloblen;
1652
Damien Miller07d86be2006-03-26 14:19:21 +11001653 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001654 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001655 if ((session_id2 == NULL) ||
1656 (kex->session_id_len != session_id2_len) ||
1657 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1658 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001660 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001661 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001662 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001663 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664 kex->server = 1;
1665 kex->hostkey_type = buffer_get_int(m);
1666 kex->kex_type = buffer_get_int(m);
1667 blob = buffer_get_string(m, &bloblen);
1668 buffer_init(&kex->my);
1669 buffer_append(&kex->my, blob, bloblen);
1670 xfree(blob);
1671 blob = buffer_get_string(m, &bloblen);
1672 buffer_init(&kex->peer);
1673 buffer_append(&kex->peer, blob, bloblen);
1674 xfree(blob);
1675 kex->done = 1;
1676 kex->flags = buffer_get_int(m);
1677 kex->client_version_string = buffer_get_string(m, NULL);
1678 kex->server_version_string = buffer_get_string(m, NULL);
1679 kex->load_host_key=&get_hostkey_by_type;
1680 kex->host_key_index=&get_hostkey_index;
1681
1682 return (kex);
1683}
1684
1685/* This function requries careful sanity checking */
1686
1687void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001688mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689{
1690 Buffer m;
1691 u_char *blob, *p;
1692 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001693 u_int32_t seqnr, packets;
1694 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001695
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001696 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697
1698 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001699 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001700 if (!compat20) {
1701 child_state.ssh1protoflags = buffer_get_int(&m);
1702 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001703 child_state.ssh1key = buffer_get_string(&m,
1704 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 child_state.ivout = buffer_get_string(&m,
1706 &child_state.ivoutlen);
1707 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1708 goto skip;
1709 } else {
1710 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001711 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001712 }
1713
1714 blob = buffer_get_string(&m, &bloblen);
1715 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1716 xfree(blob);
1717
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001718 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001719 blob = buffer_get_string(&m, &bloblen);
1720 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1721 xfree(blob);
1722
1723 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001724 seqnr = buffer_get_int(&m);
1725 blocks = buffer_get_int64(&m);
1726 packets = buffer_get_int(&m);
1727 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1728 seqnr = buffer_get_int(&m);
1729 blocks = buffer_get_int64(&m);
1730 packets = buffer_get_int(&m);
1731 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732
1733 skip:
1734 /* Get the key context */
1735 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1736 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1737
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001738 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001739 /* Get compression state */
1740 p = buffer_get_string(&m, &plen);
1741 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001742 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1744 xfree(p);
1745
1746 p = buffer_get_string(&m, &plen);
1747 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001748 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1750 xfree(p);
1751
1752 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001753 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001754 child_state.input = buffer_get_string(&m, &child_state.ilen);
1755 child_state.output = buffer_get_string(&m, &child_state.olen);
1756
1757 buffer_free(&m);
1758}
1759
1760
1761/* Allocation functions for zlib */
1762void *
1763mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1764{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001765 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001766 void *address;
1767
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001768 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001769 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1770
1771 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772
1773 return (address);
1774}
1775
1776void
1777mm_zfree(struct mm_master *mm, void *address)
1778{
1779 mm_free(mm, address);
1780}
1781
1782void
1783mm_init_compression(struct mm_master *mm)
1784{
1785 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1786 outgoing_stream.zfree = (free_func)mm_zfree;
1787 outgoing_stream.opaque = mm;
1788
1789 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1790 incoming_stream.zfree = (free_func)mm_zfree;
1791 incoming_stream.opaque = mm;
1792}
1793
1794/* XXX */
1795
1796#define FD_CLOSEONEXEC(x) do { \
1797 if (fcntl(x, F_SETFD, 1) == -1) \
1798 fatal("fcntl(%d, F_SETFD)", x); \
1799} while (0)
1800
1801static void
1802monitor_socketpair(int *pair)
1803{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001804#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001805 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001806 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001807#else
1808 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001809 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001810#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001811 FD_CLOSEONEXEC(pair[0]);
1812 FD_CLOSEONEXEC(pair[1]);
1813}
1814
1815#define MM_MEMSIZE 65536
1816
1817struct monitor *
1818monitor_init(void)
1819{
1820 struct monitor *mon;
1821 int pair[2];
1822
Damien Miller07d86be2006-03-26 14:19:21 +11001823 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001824
1825 monitor_socketpair(pair);
1826
1827 mon->m_recvfd = pair[0];
1828 mon->m_sendfd = pair[1];
1829
1830 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001831 if (options.compression) {
1832 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1833 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001834
Damien Miller2d6b8332002-06-21 15:59:49 +10001835 /* Compression needs to share state across borders */
1836 mm_init_compression(mon->m_zlib);
1837 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001838
1839 return mon;
1840}
1841
1842void
1843monitor_reinit(struct monitor *mon)
1844{
1845 int pair[2];
1846
1847 monitor_socketpair(pair);
1848
1849 mon->m_recvfd = pair[0];
1850 mon->m_sendfd = pair[1];
1851}
Darren Tucker0efd1552003-08-26 11:49:55 +10001852
1853#ifdef GSSAPI
1854int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001855mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001856{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001857 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001858 OM_uint32 major;
1859 u_int len;
1860
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001861 goid.elements = buffer_get_string(m, &len);
1862 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001863
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001864 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001865
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001866 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001867
1868 buffer_clear(m);
1869 buffer_put_int(m, major);
1870
Damien Miller6fd6def2005-11-05 15:07:05 +11001871 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001872
1873 /* Now we have a context, enable the step */
1874 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1875
1876 return (0);
1877}
1878
1879int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001880mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001881{
1882 gss_buffer_desc in;
1883 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001884 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001885 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001886 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001887
Darren Tucker600ad8d2003-08-26 12:10:48 +10001888 in.value = buffer_get_string(m, &len);
1889 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001890 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1891 xfree(in.value);
1892
1893 buffer_clear(m);
1894 buffer_put_int(m, major);
1895 buffer_put_string(m, out.value, out.length);
1896 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001897 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001898
1899 gss_release_buffer(&minor, &out);
1900
Damien Miller6fd6def2005-11-05 15:07:05 +11001901 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001902 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1903 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001905 }
1906 return (0);
1907}
1908
1909int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001910mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001911{
1912 gss_buffer_desc gssbuf, mic;
1913 OM_uint32 ret;
1914 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001915
Damien Miller0425d402003-11-17 22:18:21 +11001916 gssbuf.value = buffer_get_string(m, &len);
1917 gssbuf.length = len;
1918 mic.value = buffer_get_string(m, &len);
1919 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001920
Damien Miller0425d402003-11-17 22:18:21 +11001921 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001922
Damien Miller0425d402003-11-17 22:18:21 +11001923 xfree(gssbuf.value);
1924 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001925
Damien Miller0425d402003-11-17 22:18:21 +11001926 buffer_clear(m);
1927 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001928
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001929 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001930
Damien Miller0425d402003-11-17 22:18:21 +11001931 if (!GSS_ERROR(ret))
1932 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001933
Damien Miller0425d402003-11-17 22:18:21 +11001934 return (0);
1935}
1936
1937int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001938mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001939{
1940 int authenticated;
1941
1942 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1943
1944 buffer_clear(m);
1945 buffer_put_int(m, authenticated);
1946
1947 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001948 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001949
Damien Miller6fd6def2005-11-05 15:07:05 +11001950 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001951
1952 /* Monitor loop will terminate if authenticated */
1953 return (authenticated);
1954}
1955#endif /* GSSAPI */