blob: a5263d7466db6447ead0d67046c3f955289b56dd [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: monitor.c,v 1.81 2006/07/11 20:07:25 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 Millere3b60b52006-07-10 21:08:03 +100031#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110032#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100035#include <fcntl.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110036#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110037#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110038#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100039#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110040#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000041
42#ifdef SKEY
43#include <skey.h>
44#endif
45
Damien Miller03e20032006-03-15 11:16:59 +110046#include <openssl/dh.h>
47
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000048#include "ssh.h"
49#include "auth.h"
50#include "kex.h"
51#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000052#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
53#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000054#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000055#define TARGET_OS_MAC 1
56#else
57#include "zlib.h"
58#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000059#include "packet.h"
60#include "auth-options.h"
61#include "sshpty.h"
62#include "channels.h"
63#include "session.h"
64#include "sshlogin.h"
65#include "canohost.h"
66#include "log.h"
67#include "servconf.h"
68#include "monitor.h"
69#include "monitor_mm.h"
70#include "monitor_wrap.h"
71#include "monitor_fdpass.h"
72#include "xmalloc.h"
73#include "misc.h"
74#include "buffer.h"
75#include "bufaux.h"
76#include "compat.h"
77#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000078
Darren Tucker0efd1552003-08-26 11:49:55 +100079#ifdef GSSAPI
80#include "ssh-gss.h"
81static Gssctxt *gsscontext = NULL;
82#endif
83
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084/* Imports */
85extern ServerOptions options;
86extern u_int utmp_len;
87extern Newkeys *current_keys[];
88extern z_stream incoming_stream;
89extern z_stream outgoing_stream;
90extern u_char session_id[];
91extern Buffer input, output;
92extern Buffer auth_debug;
93extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100094extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000095
96/* State exported from the child */
97
98struct {
99 z_stream incoming;
100 z_stream outgoing;
101 u_char *keyin;
102 u_int keyinlen;
103 u_char *keyout;
104 u_int keyoutlen;
105 u_char *ivin;
106 u_int ivinlen;
107 u_char *ivout;
108 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000109 u_char *ssh1key;
110 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111 int ssh1cipher;
112 int ssh1protoflags;
113 u_char *input;
114 u_int ilen;
115 u_char *output;
116 u_int olen;
117} child_state;
118
Damien Miller469954d2003-06-18 20:25:33 +1000119/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000120
121int mm_answer_moduli(int, Buffer *);
122int mm_answer_sign(int, Buffer *);
123int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000124int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000125int mm_answer_authserv(int, Buffer *);
126int mm_answer_authpassword(int, Buffer *);
127int mm_answer_bsdauthquery(int, Buffer *);
128int mm_answer_bsdauthrespond(int, Buffer *);
129int mm_answer_skeyquery(int, Buffer *);
130int mm_answer_skeyrespond(int, Buffer *);
131int mm_answer_keyallowed(int, Buffer *);
132int mm_answer_keyverify(int, Buffer *);
133int mm_answer_pty(int, Buffer *);
134int mm_answer_pty_cleanup(int, Buffer *);
135int mm_answer_term(int, Buffer *);
136int mm_answer_rsa_keyallowed(int, Buffer *);
137int mm_answer_rsa_challenge(int, Buffer *);
138int mm_answer_rsa_response(int, Buffer *);
139int mm_answer_sesskey(int, Buffer *);
140int mm_answer_sessid(int, Buffer *);
141
Damien Miller79418552002-04-23 20:28:48 +1000142#ifdef USE_PAM
143int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000144int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000145int mm_answer_pam_init_ctx(int, Buffer *);
146int mm_answer_pam_query(int, Buffer *);
147int mm_answer_pam_respond(int, Buffer *);
148int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000149#endif
150
Darren Tucker0efd1552003-08-26 11:49:55 +1000151#ifdef GSSAPI
152int mm_answer_gss_setup_ctx(int, Buffer *);
153int mm_answer_gss_accept_ctx(int, Buffer *);
154int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100155int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000156#endif
Damien Miller25162f22002-09-12 09:47:29 +1000157
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100158#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100159int mm_answer_audit_event(int, Buffer *);
160int mm_answer_audit_command(int, Buffer *);
161#endif
162
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163static Authctxt *authctxt;
164static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
165
166/* local state for key verify */
167static u_char *key_blob = NULL;
168static u_int key_bloblen = 0;
169static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000170static char *hostbased_cuser = NULL;
171static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000172static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000173static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000174static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000175static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176
177struct mon_table {
178 enum monitor_reqtype type;
179 int flags;
180 int (*f)(int, Buffer *);
181};
182
183#define MON_ISAUTH 0x0004 /* Required for Authentication */
184#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
185#define MON_ONCE 0x0010 /* Disable after calling */
Damien Miller7a8f5b32006-03-31 23:14:23 +1100186#define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000187
188#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
189
190#define MON_PERMIT 0x1000 /* Request is permitted */
191
192struct mon_table mon_dispatch_proto20[] = {
193 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
194 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
195 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
196 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000197 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000198 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000199#ifdef USE_PAM
200 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000201 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000202 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
203 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
204 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
205 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000206#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100207#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100208 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100209#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000210#ifdef BSD_AUTH
211 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100212 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213#endif
214#ifdef SKEY
215 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
216 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
217#endif
218 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
219 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000220#ifdef GSSAPI
221 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
222 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
223 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100224 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000225#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 {0, 0, NULL}
227};
228
229struct mon_table mon_dispatch_postauth20[] = {
230 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
231 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
232 {MONITOR_REQ_PTY, 0, mm_answer_pty},
233 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
234 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100235#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100236 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
237 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
238#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000239 {0, 0, NULL}
240};
241
242struct mon_table mon_dispatch_proto15[] = {
243 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
244 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
245 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
246 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller7a8f5b32006-03-31 23:14:23 +1100247 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
248 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000249 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
250 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
251#ifdef BSD_AUTH
252 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100253 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000254#endif
255#ifdef SKEY
256 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
257 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
258#endif
Damien Millera33501b2002-05-08 12:24:42 +1000259#ifdef USE_PAM
260 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000261 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000262 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
263 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
264 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
265 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000266#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100267#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100268 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100269#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270 {0, 0, NULL}
271};
272
273struct mon_table mon_dispatch_postauth15[] = {
274 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
275 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
276 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100277#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100278 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100279 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100280#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000281 {0, 0, NULL}
282};
283
284struct mon_table *mon_dispatch;
285
286/* Specifies if a certain message is allowed at the moment */
287
288static void
289monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
290{
291 while (ent->f != NULL) {
292 if (ent->type == type) {
293 ent->flags &= ~MON_PERMIT;
294 ent->flags |= permit ? MON_PERMIT : 0;
295 return;
296 }
297 ent++;
298 }
299}
300
301static void
302monitor_permit_authentications(int permit)
303{
304 struct mon_table *ent = mon_dispatch;
305
306 while (ent->f != NULL) {
307 if (ent->flags & MON_AUTH) {
308 ent->flags &= ~MON_PERMIT;
309 ent->flags |= permit ? MON_PERMIT : 0;
310 }
311 ent++;
312 }
313}
314
Darren Tucker3e33cec2003-10-02 16:12:36 +1000315void
316monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000317{
318 struct mon_table *ent;
319 int authenticated = 0;
320
321 debug3("preauth child monitor started");
322
Darren Tucker3e33cec2003-10-02 16:12:36 +1000323 authctxt = _authctxt;
324 memset(authctxt, 0, sizeof(*authctxt));
325
Darren Tuckerde0de392005-03-31 23:52:04 +1000326 authctxt->loginmsg = &loginmsg;
327
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328 if (compat20) {
329 mon_dispatch = mon_dispatch_proto20;
330
331 /* Permit requests for moduli and signatures */
332 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
333 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
334 } else {
335 mon_dispatch = mon_dispatch_proto15;
336
337 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
338 }
339
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340 /* The first few requests do not require asynchronous access */
341 while (!authenticated) {
Damien Miller7a8f5b32006-03-31 23:14:23 +1100342 auth_method = "unknown";
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000343 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 if (authenticated) {
345 if (!(ent->flags & MON_AUTHDECIDE))
346 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000347 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 if (authctxt->pw->pw_uid == 0 &&
349 !auth_root_allowed(auth_method))
350 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000351#ifdef USE_PAM
352 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100353 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000354 Buffer m;
355
356 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100357 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000358 MONITOR_REQ_PAM_ACCOUNT, &m);
359 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
360 buffer_free(&m);
361 }
362#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363 }
364
Damien Miller7a8f5b32006-03-31 23:14:23 +1100365 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366 auth_log(authctxt, authenticated, auth_method,
367 compat20 ? " ssh2" : "");
368 if (!authenticated)
369 authctxt->failures++;
370 }
371 }
372
373 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000374 fatal("%s: authenticated invalid user", __func__);
Damien Miller7a8f5b32006-03-31 23:14:23 +1100375 if (strcmp(auth_method, "unknown") == 0)
376 fatal("%s: authentication method name unknown", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377
378 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000379 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000381 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382}
383
Damien Millerbe64d432003-05-14 19:31:12 +1000384static void
385monitor_set_child_handler(pid_t pid)
386{
387 monitor_child_pid = pid;
388}
389
390static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000391monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000392{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000393 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000394}
395
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000397monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398{
Damien Millerbe64d432003-05-14 19:31:12 +1000399 monitor_set_child_handler(pmonitor->m_pid);
400 signal(SIGHUP, &monitor_child_handler);
401 signal(SIGTERM, &monitor_child_handler);
402
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000403 if (compat20) {
404 mon_dispatch = mon_dispatch_postauth20;
405
406 /* Permit requests for moduli and signatures */
407 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
408 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
409 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000410 } else {
411 mon_dispatch = mon_dispatch_postauth15;
412 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
413 }
414 if (!no_pty_flag) {
415 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
416 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
417 }
418
419 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000420 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000421}
422
423void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000424monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425{
Damien Miller2d6b8332002-06-21 15:59:49 +1000426 if (options.compression) {
427 /* The member allocation is not visible, so sync it */
428 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
429 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430}
431
432int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000433monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000434 struct mon_table **pent)
435{
436 Buffer m;
437 int ret;
438 u_char type;
439
440 buffer_init(&m);
441
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000442 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000443 type = buffer_get_char(&m);
444
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000445 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000446
447 while (ent->f != NULL) {
448 if (ent->type == type)
449 break;
450 ent++;
451 }
452
453 if (ent->f != NULL) {
454 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000455 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000456 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000457 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458 buffer_free(&m);
459
460 /* The child may use this request only once, disable it */
461 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000462 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000463 type);
464 ent->flags &= ~MON_PERMIT;
465 }
466
467 if (pent != NULL)
468 *pent = ent;
469
470 return ret;
471 }
472
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000473 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474
475 /* NOTREACHED */
476 return (-1);
477}
478
479/* allowed key state */
480static int
481monitor_allowed_key(u_char *blob, u_int bloblen)
482{
483 /* make sure key is allowed */
484 if (key_blob == NULL || key_bloblen != bloblen ||
485 memcmp(key_blob, blob, key_bloblen))
486 return (0);
487 return (1);
488}
489
490static void
491monitor_reset_key_state(void)
492{
493 /* reset state */
494 if (key_blob != NULL)
495 xfree(key_blob);
496 if (hostbased_cuser != NULL)
497 xfree(hostbased_cuser);
498 if (hostbased_chost != NULL)
499 xfree(hostbased_chost);
500 key_blob = NULL;
501 key_bloblen = 0;
502 key_blobtype = MM_NOKEY;
503 hostbased_cuser = NULL;
504 hostbased_chost = NULL;
505}
506
507int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000508mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509{
510 DH *dh;
511 int min, want, max;
512
513 min = buffer_get_int(m);
514 want = buffer_get_int(m);
515 max = buffer_get_int(m);
516
517 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000518 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519 /* We need to check here, too, in case the child got corrupted */
520 if (max < min || want < min || max < want)
521 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000522 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
524 buffer_clear(m);
525
526 dh = choose_dh(min, want, max);
527 if (dh == NULL) {
528 buffer_put_char(m, 0);
529 return (0);
530 } else {
531 /* Send first bignum */
532 buffer_put_char(m, 1);
533 buffer_put_bignum2(m, dh->p);
534 buffer_put_bignum2(m, dh->g);
535
536 DH_free(dh);
537 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000538 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000539 return (0);
540}
541
542int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000543mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544{
545 Key *key;
546 u_char *p;
547 u_char *signature;
548 u_int siglen, datlen;
549 int keyid;
550
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000551 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552
553 keyid = buffer_get_int(m);
554 p = buffer_get_string(m, &datlen);
555
Damien Millera63128d2006-03-15 12:08:28 +1100556 /*
Damien Millerc91e5562006-03-26 13:58:55 +1100557 * Supported KEX types will only return SHA1 (20 byte) or
Damien Millera63128d2006-03-15 12:08:28 +1100558 * SHA256 (32 byte) hashes
559 */
560 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000561 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
Ben Lindstromf67e0772002-06-06 20:58:19 +0000563 /* save session id, it will be passed on the first call */
564 if (session_id2_len == 0) {
565 session_id2_len = datlen;
566 session_id2 = xmalloc(session_id2_len);
567 memcpy(session_id2, p, session_id2_len);
568 }
569
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000571 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000572 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000573 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574
Ben Lindstromd5502182002-06-27 00:12:57 +0000575 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 buffer_clear(m);
578 buffer_put_string(m, signature, siglen);
579
580 xfree(p);
581 xfree(signature);
582
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000583 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
585 /* Turn on permissions for getpwnam */
586 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
587
588 return (0);
589}
590
591/* Retrieves the password entry and also checks if the user is permitted */
592
593int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000594mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000595{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000596 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597 struct passwd *pwent;
598 int allowed = 0;
599
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000600 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
602 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000603 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604
Darren Tuckerb09b6772004-06-22 15:06:46 +1000605 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000606
Darren Tuckerb09b6772004-06-22 15:06:46 +1000607 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608
Darren Tuckerb09b6772004-06-22 15:06:46 +1000609 authctxt->user = xstrdup(username);
610 setproctitle("%s [priv]", pwent ? username : "unknown");
611 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000612
613 buffer_clear(m);
614
615 if (pwent == NULL) {
616 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100617 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618 goto out;
619 }
620
621 allowed = 1;
622 authctxt->pw = pwent;
623 authctxt->valid = 1;
624
625 buffer_put_char(m, 1);
626 buffer_put_string(m, pwent, sizeof(struct passwd));
627 buffer_put_cstring(m, pwent->pw_name);
628 buffer_put_cstring(m, "*");
629 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000630#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000631 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000632#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000633 buffer_put_cstring(m, pwent->pw_dir);
634 buffer_put_cstring(m, pwent->pw_shell);
635
636 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000637 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000638 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639
640 /* For SSHv1 allow authentication now */
641 if (!compat20)
642 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000643 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000644 /* Allow service/style information on the auth context */
645 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000646 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
647 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648
Damien Millera33501b2002-05-08 12:24:42 +1000649#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000650 if (options.use_pam)
651 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000652#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100653#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100654 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100655#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656
657 return (0);
658}
659
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000660int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000661{
662 char *banner;
663
664 buffer_clear(m);
665 banner = auth2_read_banner();
666 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000667 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000668
669 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000670 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000671
672 return (0);
673}
674
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000676mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677{
678 monitor_permit_authentications(1);
679
680 authctxt->service = buffer_get_string(m, NULL);
681 authctxt->style = buffer_get_string(m, NULL);
682 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000683 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684
685 if (strlen(authctxt->style) == 0) {
686 xfree(authctxt->style);
687 authctxt->style = NULL;
688 }
689
690 return (0);
691}
692
693int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000694mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695{
696 static int call_count;
697 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000698 int authenticated;
699 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000700
701 passwd = buffer_get_string(m, &plen);
702 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000703 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000704 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705 memset(passwd, 0, strlen(passwd));
706 xfree(passwd);
707
708 buffer_clear(m);
709 buffer_put_int(m, authenticated);
710
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000711 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000712 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
714 call_count++;
715 if (plen == 0 && call_count == 1)
716 auth_method = "none";
717 else
718 auth_method = "password";
719
720 /* Causes monitor loop to terminate if authenticated */
721 return (authenticated);
722}
723
724#ifdef BSD_AUTH
725int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000726mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727{
728 char *name, *infotxt;
729 u_int numprompts;
730 u_int *echo_on;
731 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100732 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Damien Millerb7df3af2003-02-24 11:55:46 +1100734 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
735 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736
737 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100738 buffer_put_int(m, success);
739 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740 buffer_put_cstring(m, prompts[0]);
741
Damien Millerb7df3af2003-02-24 11:55:46 +1100742 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000743 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000744
Damien Millerb7df3af2003-02-24 11:55:46 +1100745 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000746 xfree(name);
747 xfree(infotxt);
748 xfree(prompts);
749 xfree(echo_on);
750 }
751
752 return (0);
753}
754
755int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000756mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757{
758 char *response;
759 int authok;
760
761 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000762 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000763
764 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000765 authok = options.challenge_response_authentication &&
766 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000767 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000768 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769 xfree(response);
770
771 buffer_clear(m);
772 buffer_put_int(m, authok);
773
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000774 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776
777 auth_method = "bsdauth";
778
779 return (authok != 0);
780}
781#endif
782
783#ifdef SKEY
784int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000785mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000786{
787 struct skey skey;
788 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100789 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000790
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000791 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
792 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793
794 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100795 buffer_put_int(m, success);
796 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797 buffer_put_cstring(m, challenge);
798
Damien Millerb7df3af2003-02-24 11:55:46 +1100799 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000800 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801
802 return (0);
803}
804
805int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000806mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807{
808 char *response;
809 int authok;
810
811 response = buffer_get_string(m, NULL);
812
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000813 authok = (options.challenge_response_authentication &&
814 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815 skey_haskey(authctxt->pw->pw_name) == 0 &&
816 skey_passcheck(authctxt->pw->pw_name, response) != -1);
817
818 xfree(response);
819
820 buffer_clear(m);
821 buffer_put_int(m, authok);
822
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000823 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000824 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000825
826 auth_method = "skey";
827
828 return (authok != 0);
829}
830#endif
831
Damien Miller79418552002-04-23 20:28:48 +1000832#ifdef USE_PAM
833int
Darren Tucker7eda5922004-06-22 13:22:50 +1000834mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000835{
Damien Miller4e448a32003-05-14 15:11:48 +1000836 if (!options.use_pam)
837 fatal("UsePAM not set, but ended up in %s anyway", __func__);
838
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100839 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000840
Damien Miller1f499fd2003-08-25 13:08:49 +1000841 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
842
Damien Miller79418552002-04-23 20:28:48 +1000843 return (0);
844}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000845
Damien Miller1f499fd2003-08-25 13:08:49 +1000846int
Darren Tucker7eda5922004-06-22 13:22:50 +1000847mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000848{
849 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100850
Damien Miller1f499fd2003-08-25 13:08:49 +1000851 if (!options.use_pam)
852 fatal("UsePAM not set, but ended up in %s anyway", __func__);
853
854 ret = do_pam_account();
855
856 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000857 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000858
Darren Tucker7eda5922004-06-22 13:22:50 +1000859 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000860
861 return (ret);
862}
863
Damien Miller4f9f42a2003-05-10 19:28:02 +1000864static void *sshpam_ctxt, *sshpam_authok;
865extern KbdintDevice sshpam_device;
866
867int
Darren Tucker7eda5922004-06-22 13:22:50 +1000868mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000869{
870
871 debug3("%s", __func__);
872 authctxt->user = buffer_get_string(m, NULL);
873 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
874 sshpam_authok = NULL;
875 buffer_clear(m);
876 if (sshpam_ctxt != NULL) {
877 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
878 buffer_put_int(m, 1);
879 } else {
880 buffer_put_int(m, 0);
881 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000882 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000883 return (0);
884}
885
886int
Darren Tucker7eda5922004-06-22 13:22:50 +1000887mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000888{
889 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000890 u_int i, num, *echo_on;
891 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000892
893 debug3("%s", __func__);
894 sshpam_authok = NULL;
895 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
896 if (ret == 0 && num == 0)
897 sshpam_authok = sshpam_ctxt;
898 if (num > 1 || name == NULL || info == NULL)
899 ret = -1;
900 buffer_clear(m);
901 buffer_put_int(m, ret);
902 buffer_put_cstring(m, name);
903 xfree(name);
904 buffer_put_cstring(m, info);
905 xfree(info);
906 buffer_put_int(m, num);
907 for (i = 0; i < num; ++i) {
908 buffer_put_cstring(m, prompts[i]);
909 xfree(prompts[i]);
910 buffer_put_int(m, echo_on[i]);
911 }
912 if (prompts != NULL)
913 xfree(prompts);
914 if (echo_on != NULL)
915 xfree(echo_on);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000916 auth_method = "keyboard-interactive/pam";
Darren Tucker7eda5922004-06-22 13:22:50 +1000917 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000918 return (0);
919}
920
921int
Darren Tucker7eda5922004-06-22 13:22:50 +1000922mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000923{
924 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000925 u_int i, num;
926 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000927
928 debug3("%s", __func__);
929 sshpam_authok = NULL;
930 num = buffer_get_int(m);
931 if (num > 0) {
Darren Tuckerd8093e42006-05-04 16:24:34 +1000932 resp = xcalloc(num, sizeof(char *));
Damien Miller4f9f42a2003-05-10 19:28:02 +1000933 for (i = 0; i < num; ++i)
934 resp[i] = buffer_get_string(m, NULL);
935 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
936 for (i = 0; i < num; ++i)
937 xfree(resp[i]);
938 xfree(resp);
939 } else {
940 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
941 }
942 buffer_clear(m);
943 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000944 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000945 auth_method = "keyboard-interactive/pam";
946 if (ret == 0)
947 sshpam_authok = sshpam_ctxt;
948 return (0);
949}
950
951int
Darren Tucker7eda5922004-06-22 13:22:50 +1000952mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000953{
954
955 debug3("%s", __func__);
956 (sshpam_device.free_ctx)(sshpam_ctxt);
957 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000958 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Darren Tuckerf14b2aa2006-05-21 18:26:40 +1000959 auth_method = "keyboard-interactive/pam";
Damien Miller4f9f42a2003-05-10 19:28:02 +1000960 return (sshpam_authok == sshpam_ctxt);
961}
Damien Miller79418552002-04-23 20:28:48 +1000962#endif
963
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964static void
965mm_append_debug(Buffer *m)
966{
967 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000968 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 buffer_append(m, buffer_ptr(&auth_debug),
970 buffer_len(&auth_debug));
971 buffer_clear(&auth_debug);
972 }
973}
974
975int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000976mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977{
978 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000979 char *cuser, *chost;
980 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981 u_int bloblen;
982 enum mm_keytype type = 0;
983 int allowed = 0;
984
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000985 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986
987 type = buffer_get_int(m);
988 cuser = buffer_get_string(m, NULL);
989 chost = buffer_get_string(m, NULL);
990 blob = buffer_get_string(m, &bloblen);
991
992 key = key_from_blob(blob, bloblen);
993
994 if ((compat20 && type == MM_RSAHOSTKEY) ||
995 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000998 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999
Damien Miller3e3b5142003-11-17 21:13:40 +11001000 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +11001001 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001003 allowed = options.pubkey_authentication &&
1004 user_key_allowed(authctxt->pw, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001005 auth_method = "publickey";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006 break;
1007 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001008 allowed = options.hostbased_authentication &&
1009 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001010 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001011 auth_method = "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012 break;
1013 case MM_RSAHOSTKEY:
1014 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001015 allowed = options.rhosts_rsa_authentication &&
1016 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 cuser, chost, key);
Damien Miller7a8f5b32006-03-31 23:14:23 +11001018 auth_method = "rsa";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019 break;
1020 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001021 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022 break;
1023 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024 }
Damien Miller00111382003-03-10 11:21:17 +11001025 if (key != NULL)
1026 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
1028 /* clear temporarily storage (used by verify) */
1029 monitor_reset_key_state();
1030
1031 if (allowed) {
1032 /* Save temporarily for comparison in verify */
1033 key_blob = blob;
1034 key_bloblen = bloblen;
1035 key_blobtype = type;
1036 hostbased_cuser = cuser;
1037 hostbased_chost = chost;
Damien Miller96937bd2006-03-26 14:01:54 +11001038 } else {
Damien Miller7a8f5b32006-03-31 23:14:23 +11001039 /* Log failed attempt */
1040 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
Damien Miller96937bd2006-03-26 14:01:54 +11001041 xfree(blob);
1042 xfree(cuser);
1043 xfree(chost);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001044 }
1045
1046 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001047 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001048
1049 buffer_clear(m);
1050 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001051 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001052
1053 mm_append_debug(m);
1054
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001055 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056
1057 if (type == MM_RSAHOSTKEY)
1058 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1059
1060 return (0);
1061}
1062
1063static int
1064monitor_valid_userblob(u_char *data, u_int datalen)
1065{
1066 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001067 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068 u_int len;
1069 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070
1071 buffer_init(&b);
1072 buffer_append(&b, data, datalen);
1073
1074 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001075 p = buffer_ptr(&b);
1076 len = buffer_len(&b);
1077 if ((session_id2 == NULL) ||
1078 (len < session_id2_len) ||
1079 (memcmp(p, session_id2, session_id2_len) != 0))
1080 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001081 buffer_consume(&b, session_id2_len);
1082 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001083 p = buffer_get_string(&b, &len);
1084 if ((session_id2 == NULL) ||
1085 (len != session_id2_len) ||
1086 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001087 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001088 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001089 }
1090 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1091 fail++;
1092 p = buffer_get_string(&b, NULL);
1093 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001094 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001095 authctxt->user, p);
1096 fail++;
1097 }
1098 xfree(p);
1099 buffer_skip_string(&b);
1100 if (datafellows & SSH_BUG_PKAUTH) {
1101 if (!buffer_get_char(&b))
1102 fail++;
1103 } else {
1104 p = buffer_get_string(&b, NULL);
1105 if (strcmp("publickey", p) != 0)
1106 fail++;
1107 xfree(p);
1108 if (!buffer_get_char(&b))
1109 fail++;
1110 buffer_skip_string(&b);
1111 }
1112 buffer_skip_string(&b);
1113 if (buffer_len(&b) != 0)
1114 fail++;
1115 buffer_free(&b);
1116 return (fail == 0);
1117}
1118
1119static int
Damien Millera10f5612002-09-12 09:49:15 +10001120monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1121 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122{
1123 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001124 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001125 u_int len;
1126 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127
1128 buffer_init(&b);
1129 buffer_append(&b, data, datalen);
1130
Ben Lindstromf67e0772002-06-06 20:58:19 +00001131 p = buffer_get_string(&b, &len);
1132 if ((session_id2 == NULL) ||
1133 (len != session_id2_len) ||
1134 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001136 xfree(p);
1137
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1139 fail++;
1140 p = buffer_get_string(&b, NULL);
1141 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001142 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001143 authctxt->user, p);
1144 fail++;
1145 }
1146 xfree(p);
1147 buffer_skip_string(&b); /* service */
1148 p = buffer_get_string(&b, NULL);
1149 if (strcmp(p, "hostbased") != 0)
1150 fail++;
1151 xfree(p);
1152 buffer_skip_string(&b); /* pkalg */
1153 buffer_skip_string(&b); /* pkblob */
1154
1155 /* verify client host, strip trailing dot if necessary */
1156 p = buffer_get_string(&b, NULL);
1157 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1158 p[len - 1] = '\0';
1159 if (strcmp(p, chost) != 0)
1160 fail++;
1161 xfree(p);
1162
1163 /* verify client user */
1164 p = buffer_get_string(&b, NULL);
1165 if (strcmp(p, cuser) != 0)
1166 fail++;
1167 xfree(p);
1168
1169 if (buffer_len(&b) != 0)
1170 fail++;
1171 buffer_free(&b);
1172 return (fail == 0);
1173}
1174
1175int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001176mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001177{
1178 Key *key;
1179 u_char *signature, *data, *blob;
1180 u_int signaturelen, datalen, bloblen;
1181 int verified = 0;
1182 int valid_data = 0;
1183
1184 blob = buffer_get_string(m, &bloblen);
1185 signature = buffer_get_string(m, &signaturelen);
1186 data = buffer_get_string(m, &datalen);
1187
1188 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001189 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001190 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191
1192 key = key_from_blob(blob, bloblen);
1193 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001194 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195
1196 switch (key_blobtype) {
1197 case MM_USERKEY:
1198 valid_data = monitor_valid_userblob(data, datalen);
1199 break;
1200 case MM_HOSTKEY:
1201 valid_data = monitor_valid_hostbasedblob(data, datalen,
1202 hostbased_cuser, hostbased_chost);
1203 break;
1204 default:
1205 valid_data = 0;
1206 break;
1207 }
1208 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001209 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210
1211 verified = key_verify(key, signature, signaturelen, data, datalen);
1212 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001213 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001214
1215 key_free(key);
1216 xfree(blob);
1217 xfree(signature);
1218 xfree(data);
1219
Ben Lindstrome1c09122002-06-23 00:38:24 +00001220 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1221
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 monitor_reset_key_state();
1223
1224 buffer_clear(m);
1225 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001226 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 return (verified);
1229}
1230
1231static void
1232mm_record_login(Session *s, struct passwd *pw)
1233{
1234 socklen_t fromlen;
1235 struct sockaddr_storage from;
1236
1237 /*
1238 * Get IP address of client. If the connection is not a socket, let
1239 * the address be 0.0.0.0.
1240 */
1241 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001242 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001243 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 if (getpeername(packet_get_connection_in(),
Damien Miller9f3bd532006-03-26 14:07:52 +11001245 (struct sockaddr *)&from, &fromlen) < 0) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001247 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248 }
1249 }
1250 /* Record that there was a login on that tty from the remote host. */
1251 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001252 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001253 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254}
1255
1256static void
1257mm_session_close(Session *s)
1258{
Damien Miller04bd8b02003-05-25 14:38:33 +10001259 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001261 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 session_pty_cleanup2(s);
1263 }
1264 s->used = 0;
1265}
1266
1267int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001268mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001270 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001271 Session *s;
1272 int res, fd0;
1273
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001274 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275
1276 buffer_clear(m);
1277 s = session_new();
1278 if (s == NULL)
1279 goto error;
1280 s->authctxt = authctxt;
1281 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001282 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1284 if (res == 0)
1285 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286 pty_setowner(authctxt->pw, s->tty);
1287
1288 buffer_put_int(m, 1);
1289 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290
1291 /* We need to trick ttyslot */
1292 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001293 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001294
1295 mm_record_login(s, authctxt->pw);
1296
1297 /* Now we can close the file descriptor again */
1298 close(0);
1299
Darren Tucker09991742004-07-17 17:05:14 +10001300 /* send messages generated by record_login */
1301 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1302 buffer_clear(&loginmsg);
1303
1304 mm_request_send(sock, MONITOR_ANS_PTY, m);
1305
1306 mm_send_fd(sock, s->ptyfd);
1307 mm_send_fd(sock, s->ttyfd);
1308
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 /* make sure nothing uses fd 0 */
1310 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001311 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001313 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314
1315 /* slave is not needed */
1316 close(s->ttyfd);
1317 s->ttyfd = s->ptyfd;
1318 /* no need to dup() because nobody closes ptyfd */
1319 s->ptymaster = s->ptyfd;
1320
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001321 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001322
1323 return (0);
1324
1325 error:
1326 if (s != NULL)
1327 mm_session_close(s);
1328 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001329 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330 return (0);
1331}
1332
1333int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001334mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335{
1336 Session *s;
1337 char *tty;
1338
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001339 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340
1341 tty = buffer_get_string(m, NULL);
1342 if ((s = session_by_tty(tty)) != NULL)
1343 mm_session_close(s);
1344 buffer_clear(m);
1345 xfree(tty);
1346 return (0);
1347}
1348
1349int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001350mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351{
1352 BIGNUM *p;
1353 int rsafail;
1354
1355 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001356 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357
1358 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001359 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360
1361 buffer_get_bignum2(m, p);
1362
1363 rsafail = ssh1_session_key(p);
1364
1365 buffer_clear(m);
1366 buffer_put_int(m, rsafail);
1367 buffer_put_bignum2(m, p);
1368
1369 BN_clear_free(p);
1370
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001371 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372
1373 /* Turn on permissions for sessid passing */
1374 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1375
1376 return (0);
1377}
1378
1379int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001380mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381{
1382 int i;
1383
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001384 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001385
1386 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001387 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388 for (i = 0; i < 16; i++)
1389 session_id[i] = buffer_get_char(m);
1390
1391 /* Turn on permissions for getpwnam */
1392 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1393
1394 return (0);
1395}
1396
1397int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001398mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001399{
1400 BIGNUM *client_n;
1401 Key *key = NULL;
1402 u_char *blob = NULL;
1403 u_int blen = 0;
1404 int allowed = 0;
1405
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001406 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407
Damien Miller7a8f5b32006-03-31 23:14:23 +11001408 auth_method = "rsa";
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001409 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001411 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 buffer_get_bignum2(m, client_n);
1413 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1414 BN_clear_free(client_n);
1415 }
1416 buffer_clear(m);
1417 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001418 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001419
1420 /* clear temporarily storage (used by generate challenge) */
1421 monitor_reset_key_state();
1422
1423 if (allowed && key != NULL) {
1424 key->type = KEY_RSA; /* cheat for key_to_blob */
1425 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001426 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001427 buffer_put_string(m, blob, blen);
1428
1429 /* Save temporarily for comparison in verify */
1430 key_blob = blob;
1431 key_bloblen = blen;
1432 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001433 }
Damien Miller00111382003-03-10 11:21:17 +11001434 if (key != NULL)
1435 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436
1437 mm_append_debug(m);
1438
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001439 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440
1441 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1442 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1443 return (0);
1444}
1445
1446int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001447mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448{
1449 Key *key = NULL;
1450 u_char *blob;
1451 u_int blen;
1452
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001453 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001454
1455 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001456 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 blob = buffer_get_string(m, &blen);
1458 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001459 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001463 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464
1465 if (ssh1_challenge)
1466 BN_clear_free(ssh1_challenge);
1467 ssh1_challenge = auth_rsa_generate_challenge(key);
1468
1469 buffer_clear(m);
1470 buffer_put_bignum2(m, ssh1_challenge);
1471
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001472 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001473 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474
1475 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001476
1477 xfree(blob);
1478 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001479 return (0);
1480}
1481
1482int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001483mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484{
1485 Key *key = NULL;
1486 u_char *blob, *response;
1487 u_int blen, len;
1488 int success;
1489
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001490 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491
1492 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001493 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001495 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496
1497 blob = buffer_get_string(m, &blen);
1498 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001499 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001501 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001503 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504 response = buffer_get_string(m, &len);
1505 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001506 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1508
Damien Miller00111382003-03-10 11:21:17 +11001509 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001510 key_free(key);
1511 xfree(response);
1512
1513 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1514
1515 /* reset state */
1516 BN_clear_free(ssh1_challenge);
1517 ssh1_challenge = NULL;
1518 monitor_reset_key_state();
1519
1520 buffer_clear(m);
1521 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001522 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523
1524 return (success);
1525}
1526
1527int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001528mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001530 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001531 int res, status;
1532
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001533 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534
1535 /* The child is terminating */
1536 session_destroy_all(&mm_session_close);
1537
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001538 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001539 if (errno != EINTR)
1540 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001541
1542 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1543
1544 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001545 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001546}
1547
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001548#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001549/* Report that an audit event occurred */
1550int
1551mm_answer_audit_event(int socket, Buffer *m)
1552{
1553 ssh_audit_event_t event;
1554
1555 debug3("%s entering", __func__);
1556
1557 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001558 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001559 case SSH_AUTH_FAIL_PUBKEY:
1560 case SSH_AUTH_FAIL_HOSTBASED:
1561 case SSH_AUTH_FAIL_GSSAPI:
1562 case SSH_LOGIN_EXCEED_MAXTRIES:
1563 case SSH_LOGIN_ROOT_DENIED:
1564 case SSH_CONNECTION_CLOSE:
1565 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001566 audit_event(event);
1567 break;
1568 default:
1569 fatal("Audit event type %d not permitted", event);
1570 }
1571
1572 return (0);
1573}
1574
1575int
1576mm_answer_audit_command(int socket, Buffer *m)
1577{
1578 u_int len;
1579 char *cmd;
1580
1581 debug3("%s entering", __func__);
1582 cmd = buffer_get_string(m, &len);
1583 /* sanity check command, if so how? */
1584 audit_run_command(cmd);
1585 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001586 return (0);
1587}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001588#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001589
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001590void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001591monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592{
1593 if (compat20) {
1594 set_newkeys(MODE_IN);
1595 set_newkeys(MODE_OUT);
1596 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001597 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001598 packet_set_encryption_key(child_state.ssh1key,
1599 child_state.ssh1keylen, child_state.ssh1cipher);
1600 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601 }
1602
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001603 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001604 packet_set_keycontext(MODE_OUT, child_state.keyout);
1605 xfree(child_state.keyout);
1606 packet_set_keycontext(MODE_IN, child_state.keyin);
1607 xfree(child_state.keyin);
1608
1609 if (!compat20) {
1610 packet_set_iv(MODE_OUT, child_state.ivout);
1611 xfree(child_state.ivout);
1612 packet_set_iv(MODE_IN, child_state.ivin);
1613 xfree(child_state.ivin);
1614 }
1615
1616 memcpy(&incoming_stream, &child_state.incoming,
1617 sizeof(incoming_stream));
1618 memcpy(&outgoing_stream, &child_state.outgoing,
1619 sizeof(outgoing_stream));
1620
1621 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001622 if (options.compression)
1623 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624
1625 /* Network I/O buffers */
1626 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1627 buffer_clear(&input);
1628 buffer_append(&input, child_state.input, child_state.ilen);
1629 memset(child_state.input, 0, child_state.ilen);
1630 xfree(child_state.input);
1631
1632 buffer_clear(&output);
1633 buffer_append(&output, child_state.output, child_state.olen);
1634 memset(child_state.output, 0, child_state.olen);
1635 xfree(child_state.output);
1636}
1637
1638static Kex *
1639mm_get_kex(Buffer *m)
1640{
1641 Kex *kex;
1642 void *blob;
1643 u_int bloblen;
1644
Damien Miller07d86be2006-03-26 14:19:21 +11001645 kex = xcalloc(1, sizeof(*kex));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001646 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001647 if ((session_id2 == NULL) ||
1648 (kex->session_id_len != session_id2_len) ||
1649 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1650 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001652 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001653 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001654 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001655 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001656 kex->server = 1;
1657 kex->hostkey_type = buffer_get_int(m);
1658 kex->kex_type = buffer_get_int(m);
1659 blob = buffer_get_string(m, &bloblen);
1660 buffer_init(&kex->my);
1661 buffer_append(&kex->my, blob, bloblen);
1662 xfree(blob);
1663 blob = buffer_get_string(m, &bloblen);
1664 buffer_init(&kex->peer);
1665 buffer_append(&kex->peer, blob, bloblen);
1666 xfree(blob);
1667 kex->done = 1;
1668 kex->flags = buffer_get_int(m);
1669 kex->client_version_string = buffer_get_string(m, NULL);
1670 kex->server_version_string = buffer_get_string(m, NULL);
1671 kex->load_host_key=&get_hostkey_by_type;
1672 kex->host_key_index=&get_hostkey_index;
1673
1674 return (kex);
1675}
1676
1677/* This function requries careful sanity checking */
1678
1679void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001680mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681{
1682 Buffer m;
1683 u_char *blob, *p;
1684 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001685 u_int32_t seqnr, packets;
1686 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001688 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001689
1690 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001691 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692 if (!compat20) {
1693 child_state.ssh1protoflags = buffer_get_int(&m);
1694 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001695 child_state.ssh1key = buffer_get_string(&m,
1696 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001697 child_state.ivout = buffer_get_string(&m,
1698 &child_state.ivoutlen);
1699 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1700 goto skip;
1701 } else {
1702 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001703 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 }
1705
1706 blob = buffer_get_string(&m, &bloblen);
1707 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1708 xfree(blob);
1709
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001710 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001711 blob = buffer_get_string(&m, &bloblen);
1712 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1713 xfree(blob);
1714
1715 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001716 seqnr = buffer_get_int(&m);
1717 blocks = buffer_get_int64(&m);
1718 packets = buffer_get_int(&m);
1719 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1720 seqnr = buffer_get_int(&m);
1721 blocks = buffer_get_int64(&m);
1722 packets = buffer_get_int(&m);
1723 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001724
1725 skip:
1726 /* Get the key context */
1727 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1728 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1729
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001730 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001731 /* Get compression state */
1732 p = buffer_get_string(&m, &plen);
1733 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001734 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001735 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1736 xfree(p);
1737
1738 p = buffer_get_string(&m, &plen);
1739 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001740 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001741 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1742 xfree(p);
1743
1744 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001745 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746 child_state.input = buffer_get_string(&m, &child_state.ilen);
1747 child_state.output = buffer_get_string(&m, &child_state.olen);
1748
1749 buffer_free(&m);
1750}
1751
1752
1753/* Allocation functions for zlib */
1754void *
1755mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1756{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001757 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758 void *address;
1759
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001760 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001761 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1762
1763 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001764
1765 return (address);
1766}
1767
1768void
1769mm_zfree(struct mm_master *mm, void *address)
1770{
1771 mm_free(mm, address);
1772}
1773
1774void
1775mm_init_compression(struct mm_master *mm)
1776{
1777 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1778 outgoing_stream.zfree = (free_func)mm_zfree;
1779 outgoing_stream.opaque = mm;
1780
1781 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1782 incoming_stream.zfree = (free_func)mm_zfree;
1783 incoming_stream.opaque = mm;
1784}
1785
1786/* XXX */
1787
1788#define FD_CLOSEONEXEC(x) do { \
1789 if (fcntl(x, F_SETFD, 1) == -1) \
1790 fatal("fcntl(%d, F_SETFD)", x); \
1791} while (0)
1792
1793static void
1794monitor_socketpair(int *pair)
1795{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001796#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001797 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001798 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001799#else
1800 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001801 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001802#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001803 FD_CLOSEONEXEC(pair[0]);
1804 FD_CLOSEONEXEC(pair[1]);
1805}
1806
1807#define MM_MEMSIZE 65536
1808
1809struct monitor *
1810monitor_init(void)
1811{
1812 struct monitor *mon;
1813 int pair[2];
1814
Damien Miller07d86be2006-03-26 14:19:21 +11001815 mon = xcalloc(1, sizeof(*mon));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001816
1817 monitor_socketpair(pair);
1818
1819 mon->m_recvfd = pair[0];
1820 mon->m_sendfd = pair[1];
1821
1822 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001823 if (options.compression) {
1824 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1825 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001826
Damien Miller2d6b8332002-06-21 15:59:49 +10001827 /* Compression needs to share state across borders */
1828 mm_init_compression(mon->m_zlib);
1829 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001830
1831 return mon;
1832}
1833
1834void
1835monitor_reinit(struct monitor *mon)
1836{
1837 int pair[2];
1838
1839 monitor_socketpair(pair);
1840
1841 mon->m_recvfd = pair[0];
1842 mon->m_sendfd = pair[1];
1843}
Darren Tucker0efd1552003-08-26 11:49:55 +10001844
1845#ifdef GSSAPI
1846int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001847mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001848{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001849 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001850 OM_uint32 major;
1851 u_int len;
1852
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001853 goid.elements = buffer_get_string(m, &len);
1854 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001855
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001856 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001857
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001858 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001859
1860 buffer_clear(m);
1861 buffer_put_int(m, major);
1862
Damien Miller6fd6def2005-11-05 15:07:05 +11001863 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001864
1865 /* Now we have a context, enable the step */
1866 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1867
1868 return (0);
1869}
1870
1871int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001872mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001873{
1874 gss_buffer_desc in;
1875 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001876 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001877 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001878 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001879
Darren Tucker600ad8d2003-08-26 12:10:48 +10001880 in.value = buffer_get_string(m, &len);
1881 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001882 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1883 xfree(in.value);
1884
1885 buffer_clear(m);
1886 buffer_put_int(m, major);
1887 buffer_put_string(m, out.value, out.length);
1888 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001889 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001890
1891 gss_release_buffer(&minor, &out);
1892
Damien Miller6fd6def2005-11-05 15:07:05 +11001893 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001894 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1895 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001896 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001897 }
1898 return (0);
1899}
1900
1901int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001902mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001903{
1904 gss_buffer_desc gssbuf, mic;
1905 OM_uint32 ret;
1906 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001907
Damien Miller0425d402003-11-17 22:18:21 +11001908 gssbuf.value = buffer_get_string(m, &len);
1909 gssbuf.length = len;
1910 mic.value = buffer_get_string(m, &len);
1911 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001912
Damien Miller0425d402003-11-17 22:18:21 +11001913 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001914
Damien Miller0425d402003-11-17 22:18:21 +11001915 xfree(gssbuf.value);
1916 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001917
Damien Miller0425d402003-11-17 22:18:21 +11001918 buffer_clear(m);
1919 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001920
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001921 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001922
Damien Miller0425d402003-11-17 22:18:21 +11001923 if (!GSS_ERROR(ret))
1924 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001925
Damien Miller0425d402003-11-17 22:18:21 +11001926 return (0);
1927}
1928
1929int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001930mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001931{
1932 int authenticated;
1933
1934 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1935
1936 buffer_clear(m);
1937 buffer_put_int(m, authenticated);
1938
1939 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001940 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001941
Damien Miller6fd6def2005-11-05 15:07:05 +11001942 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001943
1944 /* Monitor loop will terminate if authenticated */
1945 return (authenticated);
1946}
1947#endif /* GSSAPI */