blob: e6f648b0b0badbd3de433dad15458f9274721414 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller6fd6def2005-11-05 15:07:05 +110028RCSID("$OpenBSD: monitor.c,v 1.64 2005/10/13 22:24:31 stevesk Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000040#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
41#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000042#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000043#define TARGET_OS_MAC 1
44#else
45#include "zlib.h"
46#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047#include "packet.h"
48#include "auth-options.h"
49#include "sshpty.h"
50#include "channels.h"
51#include "session.h"
52#include "sshlogin.h"
53#include "canohost.h"
54#include "log.h"
55#include "servconf.h"
56#include "monitor.h"
57#include "monitor_mm.h"
58#include "monitor_wrap.h"
59#include "monitor_fdpass.h"
60#include "xmalloc.h"
61#include "misc.h"
62#include "buffer.h"
63#include "bufaux.h"
64#include "compat.h"
65#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000066
Darren Tucker0efd1552003-08-26 11:49:55 +100067#ifdef GSSAPI
68#include "ssh-gss.h"
69static Gssctxt *gsscontext = NULL;
70#endif
71
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072/* Imports */
73extern ServerOptions options;
74extern u_int utmp_len;
75extern Newkeys *current_keys[];
76extern z_stream incoming_stream;
77extern z_stream outgoing_stream;
78extern u_char session_id[];
79extern Buffer input, output;
80extern Buffer auth_debug;
81extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100082extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083
84/* State exported from the child */
85
86struct {
87 z_stream incoming;
88 z_stream outgoing;
89 u_char *keyin;
90 u_int keyinlen;
91 u_char *keyout;
92 u_int keyoutlen;
93 u_char *ivin;
94 u_int ivinlen;
95 u_char *ivout;
96 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000097 u_char *ssh1key;
98 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000099 int ssh1cipher;
100 int ssh1protoflags;
101 u_char *input;
102 u_int ilen;
103 u_char *output;
104 u_int olen;
105} child_state;
106
Damien Miller469954d2003-06-18 20:25:33 +1000107/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000108
109int mm_answer_moduli(int, Buffer *);
110int mm_answer_sign(int, Buffer *);
111int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000112int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000113int mm_answer_authserv(int, Buffer *);
114int mm_answer_authpassword(int, Buffer *);
115int mm_answer_bsdauthquery(int, Buffer *);
116int mm_answer_bsdauthrespond(int, Buffer *);
117int mm_answer_skeyquery(int, Buffer *);
118int mm_answer_skeyrespond(int, Buffer *);
119int mm_answer_keyallowed(int, Buffer *);
120int mm_answer_keyverify(int, Buffer *);
121int mm_answer_pty(int, Buffer *);
122int mm_answer_pty_cleanup(int, Buffer *);
123int mm_answer_term(int, Buffer *);
124int mm_answer_rsa_keyallowed(int, Buffer *);
125int mm_answer_rsa_challenge(int, Buffer *);
126int mm_answer_rsa_response(int, Buffer *);
127int mm_answer_sesskey(int, Buffer *);
128int mm_answer_sessid(int, Buffer *);
129
Damien Miller79418552002-04-23 20:28:48 +1000130#ifdef USE_PAM
131int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000132int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000133int mm_answer_pam_init_ctx(int, Buffer *);
134int mm_answer_pam_query(int, Buffer *);
135int mm_answer_pam_respond(int, Buffer *);
136int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000137#endif
138
Darren Tucker0efd1552003-08-26 11:49:55 +1000139#ifdef GSSAPI
140int mm_answer_gss_setup_ctx(int, Buffer *);
141int mm_answer_gss_accept_ctx(int, Buffer *);
142int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100143int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000144#endif
Damien Miller25162f22002-09-12 09:47:29 +1000145
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100146#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100147int mm_answer_audit_event(int, Buffer *);
148int mm_answer_audit_command(int, Buffer *);
149#endif
150
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000151static Authctxt *authctxt;
152static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
153
154/* local state for key verify */
155static u_char *key_blob = NULL;
156static u_int key_bloblen = 0;
157static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000158static char *hostbased_cuser = NULL;
159static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000161static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000162static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000163static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000164
165struct mon_table {
166 enum monitor_reqtype type;
167 int flags;
168 int (*f)(int, Buffer *);
169};
170
171#define MON_ISAUTH 0x0004 /* Required for Authentication */
172#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
173#define MON_ONCE 0x0010 /* Disable after calling */
174
175#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
176
177#define MON_PERMIT 0x1000 /* Request is permitted */
178
179struct mon_table mon_dispatch_proto20[] = {
180 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
181 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
182 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
183 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000184 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000185 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000186#ifdef USE_PAM
187 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000188 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000189 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
190 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
191 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
192 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000193#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100194#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100195 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100196#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000197#ifdef BSD_AUTH
198 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
199 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
200#endif
201#ifdef SKEY
202 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
203 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
204#endif
205 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
206 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000207#ifdef GSSAPI
208 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
209 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
210 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100211 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000212#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000213 {0, 0, NULL}
214};
215
216struct mon_table mon_dispatch_postauth20[] = {
217 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
218 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
219 {MONITOR_REQ_PTY, 0, mm_answer_pty},
220 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
221 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100222#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100223 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
224 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
225#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000226 {0, 0, NULL}
227};
228
229struct mon_table mon_dispatch_proto15[] = {
230 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
231 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
232 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
233 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
234 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
235 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
236 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
237 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
238#ifdef BSD_AUTH
239 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
240 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
241#endif
242#ifdef SKEY
243 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
244 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
245#endif
Damien Millera33501b2002-05-08 12:24:42 +1000246#ifdef USE_PAM
247 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000248 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000249 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
250 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
251 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
252 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000253#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100254#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100255 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100256#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000257 {0, 0, NULL}
258};
259
260struct mon_table mon_dispatch_postauth15[] = {
261 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
262 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
263 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100264#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100265 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100266 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100267#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 {0, 0, NULL}
269};
270
271struct mon_table *mon_dispatch;
272
273/* Specifies if a certain message is allowed at the moment */
274
275static void
276monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
277{
278 while (ent->f != NULL) {
279 if (ent->type == type) {
280 ent->flags &= ~MON_PERMIT;
281 ent->flags |= permit ? MON_PERMIT : 0;
282 return;
283 }
284 ent++;
285 }
286}
287
288static void
289monitor_permit_authentications(int permit)
290{
291 struct mon_table *ent = mon_dispatch;
292
293 while (ent->f != NULL) {
294 if (ent->flags & MON_AUTH) {
295 ent->flags &= ~MON_PERMIT;
296 ent->flags |= permit ? MON_PERMIT : 0;
297 }
298 ent++;
299 }
300}
301
Darren Tucker3e33cec2003-10-02 16:12:36 +1000302void
303monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000304{
305 struct mon_table *ent;
306 int authenticated = 0;
307
308 debug3("preauth child monitor started");
309
Darren Tucker3e33cec2003-10-02 16:12:36 +1000310 authctxt = _authctxt;
311 memset(authctxt, 0, sizeof(*authctxt));
312
Darren Tuckerde0de392005-03-31 23:52:04 +1000313 authctxt->loginmsg = &loginmsg;
314
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315 if (compat20) {
316 mon_dispatch = mon_dispatch_proto20;
317
318 /* Permit requests for moduli and signatures */
319 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
320 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
321 } else {
322 mon_dispatch = mon_dispatch_proto15;
323
324 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
325 }
326
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000327 /* The first few requests do not require asynchronous access */
328 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000329 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000330 if (authenticated) {
331 if (!(ent->flags & MON_AUTHDECIDE))
332 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000333 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000334 if (authctxt->pw->pw_uid == 0 &&
335 !auth_root_allowed(auth_method))
336 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000337#ifdef USE_PAM
338 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100339 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000340 Buffer m;
341
342 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100343 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000344 MONITOR_REQ_PAM_ACCOUNT, &m);
345 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
346 buffer_free(&m);
347 }
348#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000349 }
350
351 if (ent->flags & MON_AUTHDECIDE) {
352 auth_log(authctxt, authenticated, auth_method,
353 compat20 ? " ssh2" : "");
354 if (!authenticated)
355 authctxt->failures++;
356 }
357 }
358
359 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000360 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000361
362 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000363 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000365 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000366}
367
Damien Millerbe64d432003-05-14 19:31:12 +1000368static void
369monitor_set_child_handler(pid_t pid)
370{
371 monitor_child_pid = pid;
372}
373
374static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000375monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000376{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000377 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000378}
379
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000381monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000382{
Damien Millerbe64d432003-05-14 19:31:12 +1000383 monitor_set_child_handler(pmonitor->m_pid);
384 signal(SIGHUP, &monitor_child_handler);
385 signal(SIGTERM, &monitor_child_handler);
386
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000387 if (compat20) {
388 mon_dispatch = mon_dispatch_postauth20;
389
390 /* Permit requests for moduli and signatures */
391 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
392 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
393 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394 } else {
395 mon_dispatch = mon_dispatch_postauth15;
396 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
397 }
398 if (!no_pty_flag) {
399 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
401 }
402
403 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000404 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405}
406
407void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409{
Damien Miller2d6b8332002-06-21 15:59:49 +1000410 if (options.compression) {
411 /* The member allocation is not visible, so sync it */
412 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
413 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414}
415
416int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000417monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000418 struct mon_table **pent)
419{
420 Buffer m;
421 int ret;
422 u_char type;
423
424 buffer_init(&m);
425
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000426 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000427 type = buffer_get_char(&m);
428
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000429 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000430
431 while (ent->f != NULL) {
432 if (ent->type == type)
433 break;
434 ent++;
435 }
436
437 if (ent->f != NULL) {
438 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000439 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000440 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000441 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000442 buffer_free(&m);
443
444 /* The child may use this request only once, disable it */
445 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000446 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000447 type);
448 ent->flags &= ~MON_PERMIT;
449 }
450
451 if (pent != NULL)
452 *pent = ent;
453
454 return ret;
455 }
456
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000457 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000458
459 /* NOTREACHED */
460 return (-1);
461}
462
463/* allowed key state */
464static int
465monitor_allowed_key(u_char *blob, u_int bloblen)
466{
467 /* make sure key is allowed */
468 if (key_blob == NULL || key_bloblen != bloblen ||
469 memcmp(key_blob, blob, key_bloblen))
470 return (0);
471 return (1);
472}
473
474static void
475monitor_reset_key_state(void)
476{
477 /* reset state */
478 if (key_blob != NULL)
479 xfree(key_blob);
480 if (hostbased_cuser != NULL)
481 xfree(hostbased_cuser);
482 if (hostbased_chost != NULL)
483 xfree(hostbased_chost);
484 key_blob = NULL;
485 key_bloblen = 0;
486 key_blobtype = MM_NOKEY;
487 hostbased_cuser = NULL;
488 hostbased_chost = NULL;
489}
490
491int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000492mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493{
494 DH *dh;
495 int min, want, max;
496
497 min = buffer_get_int(m);
498 want = buffer_get_int(m);
499 max = buffer_get_int(m);
500
501 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000502 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503 /* We need to check here, too, in case the child got corrupted */
504 if (max < min || want < min || max < want)
505 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000506 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000507
508 buffer_clear(m);
509
510 dh = choose_dh(min, want, max);
511 if (dh == NULL) {
512 buffer_put_char(m, 0);
513 return (0);
514 } else {
515 /* Send first bignum */
516 buffer_put_char(m, 1);
517 buffer_put_bignum2(m, dh->p);
518 buffer_put_bignum2(m, dh->g);
519
520 DH_free(dh);
521 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000522 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523 return (0);
524}
525
526int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000527mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000528{
529 Key *key;
530 u_char *p;
531 u_char *signature;
532 u_int siglen, datlen;
533 int keyid;
534
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000535 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000536
537 keyid = buffer_get_int(m);
538 p = buffer_get_string(m, &datlen);
539
540 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000541 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
Ben Lindstromf67e0772002-06-06 20:58:19 +0000543 /* save session id, it will be passed on the first call */
544 if (session_id2_len == 0) {
545 session_id2_len = datlen;
546 session_id2 = xmalloc(session_id2_len);
547 memcpy(session_id2, p, session_id2_len);
548 }
549
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000551 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000553 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000554
Ben Lindstromd5502182002-06-27 00:12:57 +0000555 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556
557 buffer_clear(m);
558 buffer_put_string(m, signature, siglen);
559
560 xfree(p);
561 xfree(signature);
562
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000563 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
565 /* Turn on permissions for getpwnam */
566 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
567
568 return (0);
569}
570
571/* Retrieves the password entry and also checks if the user is permitted */
572
573int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000574mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000576 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 struct passwd *pwent;
578 int allowed = 0;
579
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000580 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581
582 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000583 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584
Darren Tuckerb09b6772004-06-22 15:06:46 +1000585 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586
Darren Tuckerb09b6772004-06-22 15:06:46 +1000587 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588
Darren Tuckerb09b6772004-06-22 15:06:46 +1000589 authctxt->user = xstrdup(username);
590 setproctitle("%s [priv]", pwent ? username : "unknown");
591 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
593 buffer_clear(m);
594
595 if (pwent == NULL) {
596 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100597 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598 goto out;
599 }
600
601 allowed = 1;
602 authctxt->pw = pwent;
603 authctxt->valid = 1;
604
605 buffer_put_char(m, 1);
606 buffer_put_string(m, pwent, sizeof(struct passwd));
607 buffer_put_cstring(m, pwent->pw_name);
608 buffer_put_cstring(m, "*");
609 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000610#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000611 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000612#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613 buffer_put_cstring(m, pwent->pw_dir);
614 buffer_put_cstring(m, pwent->pw_shell);
615
616 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000617 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000618 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000619
620 /* For SSHv1 allow authentication now */
621 if (!compat20)
622 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000623 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000624 /* Allow service/style information on the auth context */
625 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000626 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
627 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000628
Damien Millera33501b2002-05-08 12:24:42 +1000629#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000630 if (options.use_pam)
631 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000632#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100633#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100634 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100635#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000636
637 return (0);
638}
639
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000640int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000641{
642 char *banner;
643
644 buffer_clear(m);
645 banner = auth2_read_banner();
646 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000647 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000648
649 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000650 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000651
652 return (0);
653}
654
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000656mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657{
658 monitor_permit_authentications(1);
659
660 authctxt->service = buffer_get_string(m, NULL);
661 authctxt->style = buffer_get_string(m, NULL);
662 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000663 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000664
665 if (strlen(authctxt->style) == 0) {
666 xfree(authctxt->style);
667 authctxt->style = NULL;
668 }
669
670 return (0);
671}
672
673int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000674mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000675{
676 static int call_count;
677 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000678 int authenticated;
679 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681 passwd = buffer_get_string(m, &plen);
682 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000683 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000684 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685 memset(passwd, 0, strlen(passwd));
686 xfree(passwd);
687
688 buffer_clear(m);
689 buffer_put_int(m, authenticated);
690
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000691 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000692 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000693
694 call_count++;
695 if (plen == 0 && call_count == 1)
696 auth_method = "none";
697 else
698 auth_method = "password";
699
700 /* Causes monitor loop to terminate if authenticated */
701 return (authenticated);
702}
703
704#ifdef BSD_AUTH
705int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000706mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707{
708 char *name, *infotxt;
709 u_int numprompts;
710 u_int *echo_on;
711 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100712 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713
Damien Millerb7df3af2003-02-24 11:55:46 +1100714 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
715 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716
717 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100718 buffer_put_int(m, success);
719 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000720 buffer_put_cstring(m, prompts[0]);
721
Damien Millerb7df3af2003-02-24 11:55:46 +1100722 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000723 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
Damien Millerb7df3af2003-02-24 11:55:46 +1100725 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726 xfree(name);
727 xfree(infotxt);
728 xfree(prompts);
729 xfree(echo_on);
730 }
731
732 return (0);
733}
734
735int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000736mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000737{
738 char *response;
739 int authok;
740
741 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000742 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743
744 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000745 authok = options.challenge_response_authentication &&
746 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000748 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749 xfree(response);
750
751 buffer_clear(m);
752 buffer_put_int(m, authok);
753
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000755 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000756
757 auth_method = "bsdauth";
758
759 return (authok != 0);
760}
761#endif
762
763#ifdef SKEY
764int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000765mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766{
767 struct skey skey;
768 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100769 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000771 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
772 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000773
774 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100775 buffer_put_int(m, success);
776 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000777 buffer_put_cstring(m, challenge);
778
Damien Millerb7df3af2003-02-24 11:55:46 +1100779 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000780 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000781
782 return (0);
783}
784
785int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000786mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787{
788 char *response;
789 int authok;
790
791 response = buffer_get_string(m, NULL);
792
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000793 authok = (options.challenge_response_authentication &&
794 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000795 skey_haskey(authctxt->pw->pw_name) == 0 &&
796 skey_passcheck(authctxt->pw->pw_name, response) != -1);
797
798 xfree(response);
799
800 buffer_clear(m);
801 buffer_put_int(m, authok);
802
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000803 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000804 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805
806 auth_method = "skey";
807
808 return (authok != 0);
809}
810#endif
811
Damien Miller79418552002-04-23 20:28:48 +1000812#ifdef USE_PAM
813int
Darren Tucker7eda5922004-06-22 13:22:50 +1000814mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000815{
Damien Miller4e448a32003-05-14 15:11:48 +1000816 if (!options.use_pam)
817 fatal("UsePAM not set, but ended up in %s anyway", __func__);
818
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100819 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000820
Damien Miller1f499fd2003-08-25 13:08:49 +1000821 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
822
Damien Miller79418552002-04-23 20:28:48 +1000823 return (0);
824}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000825
Damien Miller1f499fd2003-08-25 13:08:49 +1000826int
Darren Tucker7eda5922004-06-22 13:22:50 +1000827mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000828{
829 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100830
Damien Miller1f499fd2003-08-25 13:08:49 +1000831 if (!options.use_pam)
832 fatal("UsePAM not set, but ended up in %s anyway", __func__);
833
834 ret = do_pam_account();
835
836 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000837 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000838
Darren Tucker7eda5922004-06-22 13:22:50 +1000839 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000840
841 return (ret);
842}
843
Damien Miller4f9f42a2003-05-10 19:28:02 +1000844static void *sshpam_ctxt, *sshpam_authok;
845extern KbdintDevice sshpam_device;
846
847int
Darren Tucker7eda5922004-06-22 13:22:50 +1000848mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000849{
850
851 debug3("%s", __func__);
852 authctxt->user = buffer_get_string(m, NULL);
853 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
854 sshpam_authok = NULL;
855 buffer_clear(m);
856 if (sshpam_ctxt != NULL) {
857 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
858 buffer_put_int(m, 1);
859 } else {
860 buffer_put_int(m, 0);
861 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000862 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000863 return (0);
864}
865
866int
Darren Tucker7eda5922004-06-22 13:22:50 +1000867mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000868{
869 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000870 u_int i, num, *echo_on;
871 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000872
873 debug3("%s", __func__);
874 sshpam_authok = NULL;
875 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
876 if (ret == 0 && num == 0)
877 sshpam_authok = sshpam_ctxt;
878 if (num > 1 || name == NULL || info == NULL)
879 ret = -1;
880 buffer_clear(m);
881 buffer_put_int(m, ret);
882 buffer_put_cstring(m, name);
883 xfree(name);
884 buffer_put_cstring(m, info);
885 xfree(info);
886 buffer_put_int(m, num);
887 for (i = 0; i < num; ++i) {
888 buffer_put_cstring(m, prompts[i]);
889 xfree(prompts[i]);
890 buffer_put_int(m, echo_on[i]);
891 }
892 if (prompts != NULL)
893 xfree(prompts);
894 if (echo_on != NULL)
895 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000896 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000897 return (0);
898}
899
900int
Darren Tucker7eda5922004-06-22 13:22:50 +1000901mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000902{
903 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000904 u_int i, num;
905 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000906
907 debug3("%s", __func__);
908 sshpam_authok = NULL;
909 num = buffer_get_int(m);
910 if (num > 0) {
911 resp = xmalloc(num * sizeof(char *));
912 for (i = 0; i < num; ++i)
913 resp[i] = buffer_get_string(m, NULL);
914 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
915 for (i = 0; i < num; ++i)
916 xfree(resp[i]);
917 xfree(resp);
918 } else {
919 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
920 }
921 buffer_clear(m);
922 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000923 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000924 auth_method = "keyboard-interactive/pam";
925 if (ret == 0)
926 sshpam_authok = sshpam_ctxt;
927 return (0);
928}
929
930int
Darren Tucker7eda5922004-06-22 13:22:50 +1000931mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000932{
933
934 debug3("%s", __func__);
935 (sshpam_device.free_ctx)(sshpam_ctxt);
936 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000937 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000938 return (sshpam_authok == sshpam_ctxt);
939}
Damien Miller79418552002-04-23 20:28:48 +1000940#endif
941
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942static void
943mm_append_debug(Buffer *m)
944{
945 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000946 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000947 buffer_append(m, buffer_ptr(&auth_debug),
948 buffer_len(&auth_debug));
949 buffer_clear(&auth_debug);
950 }
951}
952
953int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000954mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000955{
956 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000957 char *cuser, *chost;
958 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959 u_int bloblen;
960 enum mm_keytype type = 0;
961 int allowed = 0;
962
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000963 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964
965 type = buffer_get_int(m);
966 cuser = buffer_get_string(m, NULL);
967 chost = buffer_get_string(m, NULL);
968 blob = buffer_get_string(m, &bloblen);
969
970 key = key_from_blob(blob, bloblen);
971
972 if ((compat20 && type == MM_RSAHOSTKEY) ||
973 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000974 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000975
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000976 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000977
Damien Miller3e3b5142003-11-17 21:13:40 +1100978 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100979 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000981 allowed = options.pubkey_authentication &&
982 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983 break;
984 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000985 allowed = options.hostbased_authentication &&
986 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987 cuser, chost, key);
988 break;
989 case MM_RSAHOSTKEY:
990 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000991 allowed = options.rhosts_rsa_authentication &&
992 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993 cuser, chost, key);
994 break;
995 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000996 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 break;
998 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999 }
Damien Miller00111382003-03-10 11:21:17 +11001000 if (key != NULL)
1001 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001002
1003 /* clear temporarily storage (used by verify) */
1004 monitor_reset_key_state();
1005
1006 if (allowed) {
1007 /* Save temporarily for comparison in verify */
1008 key_blob = blob;
1009 key_bloblen = bloblen;
1010 key_blobtype = type;
1011 hostbased_cuser = cuser;
1012 hostbased_chost = chost;
1013 }
1014
1015 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001016 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017
1018 buffer_clear(m);
1019 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001020 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021
1022 mm_append_debug(m);
1023
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001024 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025
1026 if (type == MM_RSAHOSTKEY)
1027 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1028
1029 return (0);
1030}
1031
1032static int
1033monitor_valid_userblob(u_char *data, u_int datalen)
1034{
1035 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001036 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037 u_int len;
1038 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039
1040 buffer_init(&b);
1041 buffer_append(&b, data, datalen);
1042
1043 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001044 p = buffer_ptr(&b);
1045 len = buffer_len(&b);
1046 if ((session_id2 == NULL) ||
1047 (len < session_id2_len) ||
1048 (memcmp(p, session_id2, session_id2_len) != 0))
1049 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001050 buffer_consume(&b, session_id2_len);
1051 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001052 p = buffer_get_string(&b, &len);
1053 if ((session_id2 == NULL) ||
1054 (len != session_id2_len) ||
1055 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001057 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058 }
1059 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1060 fail++;
1061 p = buffer_get_string(&b, NULL);
1062 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001063 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064 authctxt->user, p);
1065 fail++;
1066 }
1067 xfree(p);
1068 buffer_skip_string(&b);
1069 if (datafellows & SSH_BUG_PKAUTH) {
1070 if (!buffer_get_char(&b))
1071 fail++;
1072 } else {
1073 p = buffer_get_string(&b, NULL);
1074 if (strcmp("publickey", p) != 0)
1075 fail++;
1076 xfree(p);
1077 if (!buffer_get_char(&b))
1078 fail++;
1079 buffer_skip_string(&b);
1080 }
1081 buffer_skip_string(&b);
1082 if (buffer_len(&b) != 0)
1083 fail++;
1084 buffer_free(&b);
1085 return (fail == 0);
1086}
1087
1088static int
Damien Millera10f5612002-09-12 09:49:15 +10001089monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1090 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001091{
1092 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001093 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094 u_int len;
1095 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096
1097 buffer_init(&b);
1098 buffer_append(&b, data, datalen);
1099
Ben Lindstromf67e0772002-06-06 20:58:19 +00001100 p = buffer_get_string(&b, &len);
1101 if ((session_id2 == NULL) ||
1102 (len != session_id2_len) ||
1103 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001104 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001105 xfree(p);
1106
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001107 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1108 fail++;
1109 p = buffer_get_string(&b, NULL);
1110 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001111 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001112 authctxt->user, p);
1113 fail++;
1114 }
1115 xfree(p);
1116 buffer_skip_string(&b); /* service */
1117 p = buffer_get_string(&b, NULL);
1118 if (strcmp(p, "hostbased") != 0)
1119 fail++;
1120 xfree(p);
1121 buffer_skip_string(&b); /* pkalg */
1122 buffer_skip_string(&b); /* pkblob */
1123
1124 /* verify client host, strip trailing dot if necessary */
1125 p = buffer_get_string(&b, NULL);
1126 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1127 p[len - 1] = '\0';
1128 if (strcmp(p, chost) != 0)
1129 fail++;
1130 xfree(p);
1131
1132 /* verify client user */
1133 p = buffer_get_string(&b, NULL);
1134 if (strcmp(p, cuser) != 0)
1135 fail++;
1136 xfree(p);
1137
1138 if (buffer_len(&b) != 0)
1139 fail++;
1140 buffer_free(&b);
1141 return (fail == 0);
1142}
1143
1144int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001145mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146{
1147 Key *key;
1148 u_char *signature, *data, *blob;
1149 u_int signaturelen, datalen, bloblen;
1150 int verified = 0;
1151 int valid_data = 0;
1152
1153 blob = buffer_get_string(m, &bloblen);
1154 signature = buffer_get_string(m, &signaturelen);
1155 data = buffer_get_string(m, &datalen);
1156
1157 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001158 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001159 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160
1161 key = key_from_blob(blob, bloblen);
1162 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001163 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164
1165 switch (key_blobtype) {
1166 case MM_USERKEY:
1167 valid_data = monitor_valid_userblob(data, datalen);
1168 break;
1169 case MM_HOSTKEY:
1170 valid_data = monitor_valid_hostbasedblob(data, datalen,
1171 hostbased_cuser, hostbased_chost);
1172 break;
1173 default:
1174 valid_data = 0;
1175 break;
1176 }
1177 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001178 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179
1180 verified = key_verify(key, signature, signaturelen, data, datalen);
1181 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001182 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183
1184 key_free(key);
1185 xfree(blob);
1186 xfree(signature);
1187 xfree(data);
1188
Ben Lindstrome1c09122002-06-23 00:38:24 +00001189 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1190
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191 monitor_reset_key_state();
1192
1193 buffer_clear(m);
1194 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001195 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197 return (verified);
1198}
1199
1200static void
1201mm_record_login(Session *s, struct passwd *pw)
1202{
1203 socklen_t fromlen;
1204 struct sockaddr_storage from;
1205
1206 /*
1207 * Get IP address of client. If the connection is not a socket, let
1208 * the address be 0.0.0.0.
1209 */
1210 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001211 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213 if (getpeername(packet_get_connection_in(),
1214 (struct sockaddr *) & from, &fromlen) < 0) {
1215 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001216 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 }
1218 }
1219 /* Record that there was a login on that tty from the remote host. */
1220 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001221 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001222 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223}
1224
1225static void
1226mm_session_close(Session *s)
1227{
Damien Miller04bd8b02003-05-25 14:38:33 +10001228 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001230 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001231 session_pty_cleanup2(s);
1232 }
1233 s->used = 0;
1234}
1235
1236int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001237mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001238{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001239 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001240 Session *s;
1241 int res, fd0;
1242
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001243 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244
1245 buffer_clear(m);
1246 s = session_new();
1247 if (s == NULL)
1248 goto error;
1249 s->authctxt = authctxt;
1250 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001251 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001252 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1253 if (res == 0)
1254 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 pty_setowner(authctxt->pw, s->tty);
1256
1257 buffer_put_int(m, 1);
1258 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259
1260 /* We need to trick ttyslot */
1261 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001262 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001263
1264 mm_record_login(s, authctxt->pw);
1265
1266 /* Now we can close the file descriptor again */
1267 close(0);
1268
Darren Tucker09991742004-07-17 17:05:14 +10001269 /* send messages generated by record_login */
1270 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1271 buffer_clear(&loginmsg);
1272
1273 mm_request_send(sock, MONITOR_ANS_PTY, m);
1274
1275 mm_send_fd(sock, s->ptyfd);
1276 mm_send_fd(sock, s->ttyfd);
1277
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001278 /* make sure nothing uses fd 0 */
1279 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001280 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001282 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283
1284 /* slave is not needed */
1285 close(s->ttyfd);
1286 s->ttyfd = s->ptyfd;
1287 /* no need to dup() because nobody closes ptyfd */
1288 s->ptymaster = s->ptyfd;
1289
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001290 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291
1292 return (0);
1293
1294 error:
1295 if (s != NULL)
1296 mm_session_close(s);
1297 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001298 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 return (0);
1300}
1301
1302int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001303mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001304{
1305 Session *s;
1306 char *tty;
1307
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001308 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309
1310 tty = buffer_get_string(m, NULL);
1311 if ((s = session_by_tty(tty)) != NULL)
1312 mm_session_close(s);
1313 buffer_clear(m);
1314 xfree(tty);
1315 return (0);
1316}
1317
1318int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001319mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320{
1321 BIGNUM *p;
1322 int rsafail;
1323
1324 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001325 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326
1327 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001328 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001329
1330 buffer_get_bignum2(m, p);
1331
1332 rsafail = ssh1_session_key(p);
1333
1334 buffer_clear(m);
1335 buffer_put_int(m, rsafail);
1336 buffer_put_bignum2(m, p);
1337
1338 BN_clear_free(p);
1339
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001340 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341
1342 /* Turn on permissions for sessid passing */
1343 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1344
1345 return (0);
1346}
1347
1348int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001349mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001350{
1351 int i;
1352
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001353 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001354
1355 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001356 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001357 for (i = 0; i < 16; i++)
1358 session_id[i] = buffer_get_char(m);
1359
1360 /* Turn on permissions for getpwnam */
1361 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1362
1363 return (0);
1364}
1365
1366int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001367mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368{
1369 BIGNUM *client_n;
1370 Key *key = NULL;
1371 u_char *blob = NULL;
1372 u_int blen = 0;
1373 int allowed = 0;
1374
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001375 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001377 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001379 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 buffer_get_bignum2(m, client_n);
1381 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1382 BN_clear_free(client_n);
1383 }
1384 buffer_clear(m);
1385 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001386 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387
1388 /* clear temporarily storage (used by generate challenge) */
1389 monitor_reset_key_state();
1390
1391 if (allowed && key != NULL) {
1392 key->type = KEY_RSA; /* cheat for key_to_blob */
1393 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001394 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395 buffer_put_string(m, blob, blen);
1396
1397 /* Save temporarily for comparison in verify */
1398 key_blob = blob;
1399 key_bloblen = blen;
1400 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401 }
Damien Miller00111382003-03-10 11:21:17 +11001402 if (key != NULL)
1403 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404
1405 mm_append_debug(m);
1406
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001407 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408
1409 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1410 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1411 return (0);
1412}
1413
1414int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001415mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416{
1417 Key *key = NULL;
1418 u_char *blob;
1419 u_int blen;
1420
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001421 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422
1423 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001424 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001425 blob = buffer_get_string(m, &blen);
1426 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001429 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001431 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432
1433 if (ssh1_challenge)
1434 BN_clear_free(ssh1_challenge);
1435 ssh1_challenge = auth_rsa_generate_challenge(key);
1436
1437 buffer_clear(m);
1438 buffer_put_bignum2(m, ssh1_challenge);
1439
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001440 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001441 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442
1443 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001444
1445 xfree(blob);
1446 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001447 return (0);
1448}
1449
1450int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001451mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452{
1453 Key *key = NULL;
1454 u_char *blob, *response;
1455 u_int blen, len;
1456 int success;
1457
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001458 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001459
1460 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001461 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001463 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001464
1465 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: %d", __func__, key_blobtype);
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 response = buffer_get_string(m, &len);
1473 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001474 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001475 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1476
Damien Miller00111382003-03-10 11:21:17 +11001477 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 key_free(key);
1479 xfree(response);
1480
1481 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1482
1483 /* reset state */
1484 BN_clear_free(ssh1_challenge);
1485 ssh1_challenge = NULL;
1486 monitor_reset_key_state();
1487
1488 buffer_clear(m);
1489 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001490 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001491
1492 return (success);
1493}
1494
1495int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001496mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001498 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499 int res, status;
1500
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001501 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502
1503 /* The child is terminating */
1504 session_destroy_all(&mm_session_close);
1505
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001506 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001507 if (errno != EINTR)
1508 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509
1510 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1511
1512 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001513 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514}
1515
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001516#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001517/* Report that an audit event occurred */
1518int
1519mm_answer_audit_event(int socket, Buffer *m)
1520{
1521 ssh_audit_event_t event;
1522
1523 debug3("%s entering", __func__);
1524
1525 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001526 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001527 case SSH_AUTH_FAIL_PUBKEY:
1528 case SSH_AUTH_FAIL_HOSTBASED:
1529 case SSH_AUTH_FAIL_GSSAPI:
1530 case SSH_LOGIN_EXCEED_MAXTRIES:
1531 case SSH_LOGIN_ROOT_DENIED:
1532 case SSH_CONNECTION_CLOSE:
1533 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001534 audit_event(event);
1535 break;
1536 default:
1537 fatal("Audit event type %d not permitted", event);
1538 }
1539
1540 return (0);
1541}
1542
1543int
1544mm_answer_audit_command(int socket, Buffer *m)
1545{
1546 u_int len;
1547 char *cmd;
1548
1549 debug3("%s entering", __func__);
1550 cmd = buffer_get_string(m, &len);
1551 /* sanity check command, if so how? */
1552 audit_run_command(cmd);
1553 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001554 return (0);
1555}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001556#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001557
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001558void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001559monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001560{
1561 if (compat20) {
1562 set_newkeys(MODE_IN);
1563 set_newkeys(MODE_OUT);
1564 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001565 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001566 packet_set_encryption_key(child_state.ssh1key,
1567 child_state.ssh1keylen, child_state.ssh1cipher);
1568 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001569 }
1570
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001571 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001572 packet_set_keycontext(MODE_OUT, child_state.keyout);
1573 xfree(child_state.keyout);
1574 packet_set_keycontext(MODE_IN, child_state.keyin);
1575 xfree(child_state.keyin);
1576
1577 if (!compat20) {
1578 packet_set_iv(MODE_OUT, child_state.ivout);
1579 xfree(child_state.ivout);
1580 packet_set_iv(MODE_IN, child_state.ivin);
1581 xfree(child_state.ivin);
1582 }
1583
1584 memcpy(&incoming_stream, &child_state.incoming,
1585 sizeof(incoming_stream));
1586 memcpy(&outgoing_stream, &child_state.outgoing,
1587 sizeof(outgoing_stream));
1588
1589 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001590 if (options.compression)
1591 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001592
1593 /* Network I/O buffers */
1594 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1595 buffer_clear(&input);
1596 buffer_append(&input, child_state.input, child_state.ilen);
1597 memset(child_state.input, 0, child_state.ilen);
1598 xfree(child_state.input);
1599
1600 buffer_clear(&output);
1601 buffer_append(&output, child_state.output, child_state.olen);
1602 memset(child_state.output, 0, child_state.olen);
1603 xfree(child_state.output);
1604}
1605
1606static Kex *
1607mm_get_kex(Buffer *m)
1608{
1609 Kex *kex;
1610 void *blob;
1611 u_int bloblen;
1612
1613 kex = xmalloc(sizeof(*kex));
1614 memset(kex, 0, sizeof(*kex));
1615 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001616 if ((session_id2 == NULL) ||
1617 (kex->session_id_len != session_id2_len) ||
1618 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1619 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001621 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001622 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001623 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624 kex->server = 1;
1625 kex->hostkey_type = buffer_get_int(m);
1626 kex->kex_type = buffer_get_int(m);
1627 blob = buffer_get_string(m, &bloblen);
1628 buffer_init(&kex->my);
1629 buffer_append(&kex->my, blob, bloblen);
1630 xfree(blob);
1631 blob = buffer_get_string(m, &bloblen);
1632 buffer_init(&kex->peer);
1633 buffer_append(&kex->peer, blob, bloblen);
1634 xfree(blob);
1635 kex->done = 1;
1636 kex->flags = buffer_get_int(m);
1637 kex->client_version_string = buffer_get_string(m, NULL);
1638 kex->server_version_string = buffer_get_string(m, NULL);
1639 kex->load_host_key=&get_hostkey_by_type;
1640 kex->host_key_index=&get_hostkey_index;
1641
1642 return (kex);
1643}
1644
1645/* This function requries careful sanity checking */
1646
1647void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001648mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001649{
1650 Buffer m;
1651 u_char *blob, *p;
1652 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001653 u_int32_t seqnr, packets;
1654 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001656 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657
1658 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001659 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660 if (!compat20) {
1661 child_state.ssh1protoflags = buffer_get_int(&m);
1662 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001663 child_state.ssh1key = buffer_get_string(&m,
1664 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001665 child_state.ivout = buffer_get_string(&m,
1666 &child_state.ivoutlen);
1667 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1668 goto skip;
1669 } else {
1670 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001671 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001672 }
1673
1674 blob = buffer_get_string(&m, &bloblen);
1675 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1676 xfree(blob);
1677
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001678 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001679 blob = buffer_get_string(&m, &bloblen);
1680 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1681 xfree(blob);
1682
1683 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001684 seqnr = buffer_get_int(&m);
1685 blocks = buffer_get_int64(&m);
1686 packets = buffer_get_int(&m);
1687 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1688 seqnr = buffer_get_int(&m);
1689 blocks = buffer_get_int64(&m);
1690 packets = buffer_get_int(&m);
1691 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692
1693 skip:
1694 /* Get the key context */
1695 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1696 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1697
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001698 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001699 /* Get compression state */
1700 p = buffer_get_string(&m, &plen);
1701 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001702 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1704 xfree(p);
1705
1706 p = buffer_get_string(&m, &plen);
1707 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001708 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001709 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1710 xfree(p);
1711
1712 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714 child_state.input = buffer_get_string(&m, &child_state.ilen);
1715 child_state.output = buffer_get_string(&m, &child_state.olen);
1716
1717 buffer_free(&m);
1718}
1719
1720
1721/* Allocation functions for zlib */
1722void *
1723mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1724{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001725 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001726 void *address;
1727
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001728 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001729 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1730
1731 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732
1733 return (address);
1734}
1735
1736void
1737mm_zfree(struct mm_master *mm, void *address)
1738{
1739 mm_free(mm, address);
1740}
1741
1742void
1743mm_init_compression(struct mm_master *mm)
1744{
1745 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1746 outgoing_stream.zfree = (free_func)mm_zfree;
1747 outgoing_stream.opaque = mm;
1748
1749 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1750 incoming_stream.zfree = (free_func)mm_zfree;
1751 incoming_stream.opaque = mm;
1752}
1753
1754/* XXX */
1755
1756#define FD_CLOSEONEXEC(x) do { \
1757 if (fcntl(x, F_SETFD, 1) == -1) \
1758 fatal("fcntl(%d, F_SETFD)", x); \
1759} while (0)
1760
1761static void
1762monitor_socketpair(int *pair)
1763{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001764#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001765 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001766 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001767#else
1768 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001769 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001770#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771 FD_CLOSEONEXEC(pair[0]);
1772 FD_CLOSEONEXEC(pair[1]);
1773}
1774
1775#define MM_MEMSIZE 65536
1776
1777struct monitor *
1778monitor_init(void)
1779{
1780 struct monitor *mon;
1781 int pair[2];
1782
1783 mon = xmalloc(sizeof(*mon));
1784
Darren Tuckera8be9e22004-02-06 16:40:27 +11001785 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001786 monitor_socketpair(pair);
1787
1788 mon->m_recvfd = pair[0];
1789 mon->m_sendfd = pair[1];
1790
1791 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001792 if (options.compression) {
1793 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1794 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001795
Damien Miller2d6b8332002-06-21 15:59:49 +10001796 /* Compression needs to share state across borders */
1797 mm_init_compression(mon->m_zlib);
1798 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001799
1800 return mon;
1801}
1802
1803void
1804monitor_reinit(struct monitor *mon)
1805{
1806 int pair[2];
1807
1808 monitor_socketpair(pair);
1809
1810 mon->m_recvfd = pair[0];
1811 mon->m_sendfd = pair[1];
1812}
Darren Tucker0efd1552003-08-26 11:49:55 +10001813
1814#ifdef GSSAPI
1815int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001816mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001817{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001818 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001819 OM_uint32 major;
1820 u_int len;
1821
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001822 goid.elements = buffer_get_string(m, &len);
1823 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001824
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001825 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001826
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001827 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001828
1829 buffer_clear(m);
1830 buffer_put_int(m, major);
1831
Damien Miller6fd6def2005-11-05 15:07:05 +11001832 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001833
1834 /* Now we have a context, enable the step */
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1836
1837 return (0);
1838}
1839
1840int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001841mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001842{
1843 gss_buffer_desc in;
1844 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001845 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001846 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001847 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001848
Darren Tucker600ad8d2003-08-26 12:10:48 +10001849 in.value = buffer_get_string(m, &len);
1850 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001851 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1852 xfree(in.value);
1853
1854 buffer_clear(m);
1855 buffer_put_int(m, major);
1856 buffer_put_string(m, out.value, out.length);
1857 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001858 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001859
1860 gss_release_buffer(&minor, &out);
1861
Damien Miller6fd6def2005-11-05 15:07:05 +11001862 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001863 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1864 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001865 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001866 }
1867 return (0);
1868}
1869
1870int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001871mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001872{
1873 gss_buffer_desc gssbuf, mic;
1874 OM_uint32 ret;
1875 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001876
Damien Miller0425d402003-11-17 22:18:21 +11001877 gssbuf.value = buffer_get_string(m, &len);
1878 gssbuf.length = len;
1879 mic.value = buffer_get_string(m, &len);
1880 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001881
Damien Miller0425d402003-11-17 22:18:21 +11001882 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001883
Damien Miller0425d402003-11-17 22:18:21 +11001884 xfree(gssbuf.value);
1885 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001886
Damien Miller0425d402003-11-17 22:18:21 +11001887 buffer_clear(m);
1888 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001889
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001890 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001891
Damien Miller0425d402003-11-17 22:18:21 +11001892 if (!GSS_ERROR(ret))
1893 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001894
Damien Miller0425d402003-11-17 22:18:21 +11001895 return (0);
1896}
1897
1898int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001899mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001900{
1901 int authenticated;
1902
1903 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1904
1905 buffer_clear(m);
1906 buffer_put_int(m, authenticated);
1907
1908 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001909 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001910
Damien Miller6fd6def2005-11-05 15:07:05 +11001911 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001912
1913 /* Monitor loop will terminate if authenticated */
1914 return (authenticated);
1915}
1916#endif /* GSSAPI */