blob: 30849a3719e7382e0553081615b7fafde4c33efe [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 Millera63128d2006-03-15 12:08:28 +110028RCSID("$OpenBSD: monitor.c,v 1.69 2006/03/07 09:07:40 djm Exp $");
Damien Miller9cf6d072006-03-15 11:29:24 +110029
30#include <sys/types.h>
31#include <sys/wait.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000032
Damien Miller03e20032006-03-15 11:16:59 +110033#include <paths.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110034#include <signal.h>
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000035
36#ifdef SKEY
37#include <skey.h>
38#endif
39
Damien Miller03e20032006-03-15 11:16:59 +110040#include <openssl/dh.h>
41
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000042#include "ssh.h"
43#include "auth.h"
44#include "kex.h"
45#include "dh.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000046#ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
47#undef TARGET_OS_MAC
Ben Lindstrom1b9f2a62004-04-08 05:11:03 +000048#include "zlib.h"
Ben Lindstrom036768e2004-04-08 16:12:30 +000049#define TARGET_OS_MAC 1
50#else
51#include "zlib.h"
52#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000053#include "packet.h"
54#include "auth-options.h"
55#include "sshpty.h"
56#include "channels.h"
57#include "session.h"
58#include "sshlogin.h"
59#include "canohost.h"
60#include "log.h"
61#include "servconf.h"
62#include "monitor.h"
63#include "monitor_mm.h"
64#include "monitor_wrap.h"
65#include "monitor_fdpass.h"
66#include "xmalloc.h"
67#include "misc.h"
68#include "buffer.h"
69#include "bufaux.h"
70#include "compat.h"
71#include "ssh2.h"
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000072
Darren Tucker0efd1552003-08-26 11:49:55 +100073#ifdef GSSAPI
74#include "ssh-gss.h"
75static Gssctxt *gsscontext = NULL;
76#endif
77
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000078/* Imports */
79extern ServerOptions options;
80extern u_int utmp_len;
81extern Newkeys *current_keys[];
82extern z_stream incoming_stream;
83extern z_stream outgoing_stream;
84extern u_char session_id[];
85extern Buffer input, output;
86extern Buffer auth_debug;
87extern int auth_debug_init;
Darren Tucker09991742004-07-17 17:05:14 +100088extern Buffer loginmsg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000089
90/* State exported from the child */
91
92struct {
93 z_stream incoming;
94 z_stream outgoing;
95 u_char *keyin;
96 u_int keyinlen;
97 u_char *keyout;
98 u_int keyoutlen;
99 u_char *ivin;
100 u_int ivinlen;
101 u_char *ivout;
102 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +0000103 u_char *ssh1key;
104 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105 int ssh1cipher;
106 int ssh1protoflags;
107 u_char *input;
108 u_int ilen;
109 u_char *output;
110 u_int olen;
111} child_state;
112
Damien Miller469954d2003-06-18 20:25:33 +1000113/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000114
115int mm_answer_moduli(int, Buffer *);
116int mm_answer_sign(int, Buffer *);
117int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000118int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000119int mm_answer_authserv(int, Buffer *);
120int mm_answer_authpassword(int, Buffer *);
121int mm_answer_bsdauthquery(int, Buffer *);
122int mm_answer_bsdauthrespond(int, Buffer *);
123int mm_answer_skeyquery(int, Buffer *);
124int mm_answer_skeyrespond(int, Buffer *);
125int mm_answer_keyallowed(int, Buffer *);
126int mm_answer_keyverify(int, Buffer *);
127int mm_answer_pty(int, Buffer *);
128int mm_answer_pty_cleanup(int, Buffer *);
129int mm_answer_term(int, Buffer *);
130int mm_answer_rsa_keyallowed(int, Buffer *);
131int mm_answer_rsa_challenge(int, Buffer *);
132int mm_answer_rsa_response(int, Buffer *);
133int mm_answer_sesskey(int, Buffer *);
134int mm_answer_sessid(int, Buffer *);
135
Damien Miller79418552002-04-23 20:28:48 +1000136#ifdef USE_PAM
137int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000138int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000139int mm_answer_pam_init_ctx(int, Buffer *);
140int mm_answer_pam_query(int, Buffer *);
141int mm_answer_pam_respond(int, Buffer *);
142int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000143#endif
144
Darren Tucker0efd1552003-08-26 11:49:55 +1000145#ifdef GSSAPI
146int mm_answer_gss_setup_ctx(int, Buffer *);
147int mm_answer_gss_accept_ctx(int, Buffer *);
148int mm_answer_gss_userok(int, Buffer *);
Damien Miller0425d402003-11-17 22:18:21 +1100149int mm_answer_gss_checkmic(int, Buffer *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000150#endif
Damien Miller25162f22002-09-12 09:47:29 +1000151
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100152#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100153int mm_answer_audit_event(int, Buffer *);
154int mm_answer_audit_command(int, Buffer *);
155#endif
156
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000157static Authctxt *authctxt;
158static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
159
160/* local state for key verify */
161static u_char *key_blob = NULL;
162static u_int key_bloblen = 0;
163static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000164static char *hostbased_cuser = NULL;
165static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000167static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000168static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000169static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000170
171struct mon_table {
172 enum monitor_reqtype type;
173 int flags;
174 int (*f)(int, Buffer *);
175};
176
177#define MON_ISAUTH 0x0004 /* Required for Authentication */
178#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
179#define MON_ONCE 0x0010 /* Disable after calling */
180
181#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
182
183#define MON_PERMIT 0x1000 /* Request is permitted */
184
185struct mon_table mon_dispatch_proto20[] = {
186 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
187 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
188 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
189 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000190 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000191 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000192#ifdef USE_PAM
193 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000194 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000195 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
196 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
197 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
198 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000199#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100200#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100201 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100202#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000203#ifdef BSD_AUTH
204 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100205 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000206#endif
207#ifdef SKEY
208 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
209 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
210#endif
211 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
212 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Darren Tucker0efd1552003-08-26 11:49:55 +1000213#ifdef GSSAPI
214 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
215 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
216 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
Damien Miller0425d402003-11-17 22:18:21 +1100217 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
Darren Tucker0efd1552003-08-26 11:49:55 +1000218#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000219 {0, 0, NULL}
220};
221
222struct mon_table mon_dispatch_postauth20[] = {
223 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
224 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
225 {MONITOR_REQ_PTY, 0, mm_answer_pty},
226 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
227 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100228#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100229 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
230 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
231#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000232 {0, 0, NULL}
233};
234
235struct mon_table mon_dispatch_proto15[] = {
236 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
237 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
238 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
239 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
240 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
241 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
242 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
243 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
244#ifdef BSD_AUTH
245 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Damien Miller0b70b542006-03-15 11:20:03 +1100246 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000247#endif
248#ifdef SKEY
249 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
250 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
251#endif
Damien Millera33501b2002-05-08 12:24:42 +1000252#ifdef USE_PAM
253 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000254 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000255 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
256 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
257 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
258 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000259#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100260#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100261 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100262#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000263 {0, 0, NULL}
264};
265
266struct mon_table mon_dispatch_postauth15[] = {
267 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
268 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
269 {MONITOR_REQ_TERM, 0, mm_answer_term},
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100270#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100271 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Darren Tucker3745e2b2005-03-06 22:31:35 +1100272 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
Darren Tucker269a1ea2005-02-03 00:20:53 +1100273#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274 {0, 0, NULL}
275};
276
277struct mon_table *mon_dispatch;
278
279/* Specifies if a certain message is allowed at the moment */
280
281static void
282monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
283{
284 while (ent->f != NULL) {
285 if (ent->type == type) {
286 ent->flags &= ~MON_PERMIT;
287 ent->flags |= permit ? MON_PERMIT : 0;
288 return;
289 }
290 ent++;
291 }
292}
293
294static void
295monitor_permit_authentications(int permit)
296{
297 struct mon_table *ent = mon_dispatch;
298
299 while (ent->f != NULL) {
300 if (ent->flags & MON_AUTH) {
301 ent->flags &= ~MON_PERMIT;
302 ent->flags |= permit ? MON_PERMIT : 0;
303 }
304 ent++;
305 }
306}
307
Darren Tucker3e33cec2003-10-02 16:12:36 +1000308void
309monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000310{
311 struct mon_table *ent;
312 int authenticated = 0;
313
314 debug3("preauth child monitor started");
315
Darren Tucker3e33cec2003-10-02 16:12:36 +1000316 authctxt = _authctxt;
317 memset(authctxt, 0, sizeof(*authctxt));
318
Darren Tuckerde0de392005-03-31 23:52:04 +1000319 authctxt->loginmsg = &loginmsg;
320
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321 if (compat20) {
322 mon_dispatch = mon_dispatch_proto20;
323
324 /* Permit requests for moduli and signatures */
325 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
326 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
327 } else {
328 mon_dispatch = mon_dispatch_proto15;
329
330 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
331 }
332
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000333 /* The first few requests do not require asynchronous access */
334 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000335 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000336 if (authenticated) {
337 if (!(ent->flags & MON_AUTHDECIDE))
338 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000339 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000340 if (authctxt->pw->pw_uid == 0 &&
341 !auth_root_allowed(auth_method))
342 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000343#ifdef USE_PAM
344 /* PAM needs to perform account checks after auth */
Damien Miller6aef38f2003-11-18 10:45:20 +1100345 if (options.use_pam && authenticated) {
Damien Miller1f499fd2003-08-25 13:08:49 +1000346 Buffer m;
347
348 buffer_init(&m);
Damien Millera8e06ce2003-11-21 23:48:55 +1100349 mm_request_receive_expect(pmonitor->m_sendfd,
Damien Miller1f499fd2003-08-25 13:08:49 +1000350 MONITOR_REQ_PAM_ACCOUNT, &m);
351 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
352 buffer_free(&m);
353 }
354#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355 }
356
357 if (ent->flags & MON_AUTHDECIDE) {
358 auth_log(authctxt, authenticated, auth_method,
359 compat20 ? " ssh2" : "");
360 if (!authenticated)
361 authctxt->failures++;
362 }
363 }
364
365 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000366 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367
368 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000369 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372}
373
Damien Millerbe64d432003-05-14 19:31:12 +1000374static void
375monitor_set_child_handler(pid_t pid)
376{
377 monitor_child_pid = pid;
378}
379
380static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000381monitor_child_handler(int sig)
Damien Millerbe64d432003-05-14 19:31:12 +1000382{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000383 kill(monitor_child_pid, sig);
Damien Millerbe64d432003-05-14 19:31:12 +1000384}
385
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000386void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000387monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388{
Damien Millerbe64d432003-05-14 19:31:12 +1000389 monitor_set_child_handler(pmonitor->m_pid);
390 signal(SIGHUP, &monitor_child_handler);
391 signal(SIGTERM, &monitor_child_handler);
392
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000393 if (compat20) {
394 mon_dispatch = mon_dispatch_postauth20;
395
396 /* Permit requests for moduli and signatures */
397 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
398 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
399 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400 } else {
401 mon_dispatch = mon_dispatch_postauth15;
402 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
403 }
404 if (!no_pty_flag) {
405 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
406 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
407 }
408
409 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000410 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000411}
412
413void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000414monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000415{
Damien Miller2d6b8332002-06-21 15:59:49 +1000416 if (options.compression) {
417 /* The member allocation is not visible, so sync it */
418 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
419 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420}
421
422int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000423monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424 struct mon_table **pent)
425{
426 Buffer m;
427 int ret;
428 u_char type;
429
430 buffer_init(&m);
431
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000432 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433 type = buffer_get_char(&m);
434
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000435 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000436
437 while (ent->f != NULL) {
438 if (ent->type == type)
439 break;
440 ent++;
441 }
442
443 if (ent->f != NULL) {
444 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000445 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000446 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000447 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448 buffer_free(&m);
449
450 /* The child may use this request only once, disable it */
451 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000452 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453 type);
454 ent->flags &= ~MON_PERMIT;
455 }
456
457 if (pent != NULL)
458 *pent = ent;
459
460 return ret;
461 }
462
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000463 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000464
465 /* NOTREACHED */
466 return (-1);
467}
468
469/* allowed key state */
470static int
471monitor_allowed_key(u_char *blob, u_int bloblen)
472{
473 /* make sure key is allowed */
474 if (key_blob == NULL || key_bloblen != bloblen ||
475 memcmp(key_blob, blob, key_bloblen))
476 return (0);
477 return (1);
478}
479
480static void
481monitor_reset_key_state(void)
482{
483 /* reset state */
484 if (key_blob != NULL)
485 xfree(key_blob);
486 if (hostbased_cuser != NULL)
487 xfree(hostbased_cuser);
488 if (hostbased_chost != NULL)
489 xfree(hostbased_chost);
490 key_blob = NULL;
491 key_bloblen = 0;
492 key_blobtype = MM_NOKEY;
493 hostbased_cuser = NULL;
494 hostbased_chost = NULL;
495}
496
497int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000498mm_answer_moduli(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000499{
500 DH *dh;
501 int min, want, max;
502
503 min = buffer_get_int(m);
504 want = buffer_get_int(m);
505 max = buffer_get_int(m);
506
507 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000508 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509 /* We need to check here, too, in case the child got corrupted */
510 if (max < min || want < min || max < want)
511 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000512 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000513
514 buffer_clear(m);
515
516 dh = choose_dh(min, want, max);
517 if (dh == NULL) {
518 buffer_put_char(m, 0);
519 return (0);
520 } else {
521 /* Send first bignum */
522 buffer_put_char(m, 1);
523 buffer_put_bignum2(m, dh->p);
524 buffer_put_bignum2(m, dh->g);
525
526 DH_free(dh);
527 }
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000528 mm_request_send(sock, MONITOR_ANS_MODULI, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 return (0);
530}
531
532int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000533mm_answer_sign(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534{
535 Key *key;
536 u_char *p;
537 u_char *signature;
538 u_int siglen, datlen;
539 int keyid;
540
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000541 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
543 keyid = buffer_get_int(m);
544 p = buffer_get_string(m, &datlen);
545
Damien Millera63128d2006-03-15 12:08:28 +1100546 /*
547 * Supported KEX types will only return SHA1 (20 byte) or
548 * SHA256 (32 byte) hashes
549 */
550 if (datlen != 20 && datlen != 32)
Ben Lindstromd5502182002-06-27 00:12:57 +0000551 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000552
Ben Lindstromf67e0772002-06-06 20:58:19 +0000553 /* save session id, it will be passed on the first call */
554 if (session_id2_len == 0) {
555 session_id2_len = datlen;
556 session_id2 = xmalloc(session_id2_len);
557 memcpy(session_id2, p, session_id2_len);
558 }
559
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000561 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000563 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564
Ben Lindstromd5502182002-06-27 00:12:57 +0000565 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000566
567 buffer_clear(m);
568 buffer_put_string(m, signature, siglen);
569
570 xfree(p);
571 xfree(signature);
572
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000573 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000574
575 /* Turn on permissions for getpwnam */
576 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
577
578 return (0);
579}
580
581/* Retrieves the password entry and also checks if the user is permitted */
582
583int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000584mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000586 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587 struct passwd *pwent;
588 int allowed = 0;
589
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000590 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000591
592 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000593 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
Darren Tuckerb09b6772004-06-22 15:06:46 +1000595 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596
Darren Tuckerb09b6772004-06-22 15:06:46 +1000597 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
Darren Tuckerb09b6772004-06-22 15:06:46 +1000599 authctxt->user = xstrdup(username);
600 setproctitle("%s [priv]", pwent ? username : "unknown");
601 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000602
603 buffer_clear(m);
604
605 if (pwent == NULL) {
606 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100607 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000608 goto out;
609 }
610
611 allowed = 1;
612 authctxt->pw = pwent;
613 authctxt->valid = 1;
614
615 buffer_put_char(m, 1);
616 buffer_put_string(m, pwent, sizeof(struct passwd));
617 buffer_put_cstring(m, pwent->pw_name);
618 buffer_put_cstring(m, "*");
619 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000620#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000621 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000622#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000623 buffer_put_cstring(m, pwent->pw_dir);
624 buffer_put_cstring(m, pwent->pw_shell);
625
626 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000627 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000628 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000629
630 /* For SSHv1 allow authentication now */
631 if (!compat20)
632 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000633 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 /* Allow service/style information on the auth context */
635 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000636 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
637 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000638
Damien Millera33501b2002-05-08 12:24:42 +1000639#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000640 if (options.use_pam)
641 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000642#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100643#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100644 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100645#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646
647 return (0);
648}
649
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000650int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000651{
652 char *banner;
653
654 buffer_clear(m);
655 banner = auth2_read_banner();
656 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000657 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000658
659 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000660 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000661
662 return (0);
663}
664
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000665int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000666mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667{
668 monitor_permit_authentications(1);
669
670 authctxt->service = buffer_get_string(m, NULL);
671 authctxt->style = buffer_get_string(m, NULL);
672 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000673 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674
675 if (strlen(authctxt->style) == 0) {
676 xfree(authctxt->style);
677 authctxt->style = NULL;
678 }
679
680 return (0);
681}
682
683int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000684mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000685{
686 static int call_count;
687 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000688 int authenticated;
689 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690
691 passwd = buffer_get_string(m, &plen);
692 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000693 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000694 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695 memset(passwd, 0, strlen(passwd));
696 xfree(passwd);
697
698 buffer_clear(m);
699 buffer_put_int(m, authenticated);
700
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000701 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000702 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
704 call_count++;
705 if (plen == 0 && call_count == 1)
706 auth_method = "none";
707 else
708 auth_method = "password";
709
710 /* Causes monitor loop to terminate if authenticated */
711 return (authenticated);
712}
713
714#ifdef BSD_AUTH
715int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000716mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000717{
718 char *name, *infotxt;
719 u_int numprompts;
720 u_int *echo_on;
721 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100722 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000723
Damien Millerb7df3af2003-02-24 11:55:46 +1100724 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
725 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726
727 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100728 buffer_put_int(m, success);
729 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730 buffer_put_cstring(m, prompts[0]);
731
Damien Millerb7df3af2003-02-24 11:55:46 +1100732 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000733 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000734
Damien Millerb7df3af2003-02-24 11:55:46 +1100735 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 xfree(name);
737 xfree(infotxt);
738 xfree(prompts);
739 xfree(echo_on);
740 }
741
742 return (0);
743}
744
745int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000746mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000747{
748 char *response;
749 int authok;
750
751 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000752 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753
754 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000755 authok = options.challenge_response_authentication &&
756 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000758 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759 xfree(response);
760
761 buffer_clear(m);
762 buffer_put_int(m, authok);
763
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000764 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000765 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766
767 auth_method = "bsdauth";
768
769 return (authok != 0);
770}
771#endif
772
773#ifdef SKEY
774int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000775mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776{
777 struct skey skey;
778 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100779 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000780
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000781 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
782 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783
784 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100785 buffer_put_int(m, success);
786 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787 buffer_put_cstring(m, challenge);
788
Damien Millerb7df3af2003-02-24 11:55:46 +1100789 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000790 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000791
792 return (0);
793}
794
795int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000796mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797{
798 char *response;
799 int authok;
800
801 response = buffer_get_string(m, NULL);
802
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000803 authok = (options.challenge_response_authentication &&
804 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000805 skey_haskey(authctxt->pw->pw_name) == 0 &&
806 skey_passcheck(authctxt->pw->pw_name, response) != -1);
807
808 xfree(response);
809
810 buffer_clear(m);
811 buffer_put_int(m, authok);
812
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000813 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000814 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000815
816 auth_method = "skey";
817
818 return (authok != 0);
819}
820#endif
821
Damien Miller79418552002-04-23 20:28:48 +1000822#ifdef USE_PAM
823int
Darren Tucker7eda5922004-06-22 13:22:50 +1000824mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000825{
Damien Miller4e448a32003-05-14 15:11:48 +1000826 if (!options.use_pam)
827 fatal("UsePAM not set, but ended up in %s anyway", __func__);
828
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100829 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000830
Damien Miller1f499fd2003-08-25 13:08:49 +1000831 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
832
Damien Miller79418552002-04-23 20:28:48 +1000833 return (0);
834}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000835
Damien Miller1f499fd2003-08-25 13:08:49 +1000836int
Darren Tucker7eda5922004-06-22 13:22:50 +1000837mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000838{
839 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100840
Damien Miller1f499fd2003-08-25 13:08:49 +1000841 if (!options.use_pam)
842 fatal("UsePAM not set, but ended up in %s anyway", __func__);
843
844 ret = do_pam_account();
845
846 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000847 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000848
Darren Tucker7eda5922004-06-22 13:22:50 +1000849 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000850
851 return (ret);
852}
853
Damien Miller4f9f42a2003-05-10 19:28:02 +1000854static void *sshpam_ctxt, *sshpam_authok;
855extern KbdintDevice sshpam_device;
856
857int
Darren Tucker7eda5922004-06-22 13:22:50 +1000858mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000859{
860
861 debug3("%s", __func__);
862 authctxt->user = buffer_get_string(m, NULL);
863 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
864 sshpam_authok = NULL;
865 buffer_clear(m);
866 if (sshpam_ctxt != NULL) {
867 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
868 buffer_put_int(m, 1);
869 } else {
870 buffer_put_int(m, 0);
871 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000872 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000873 return (0);
874}
875
876int
Darren Tucker7eda5922004-06-22 13:22:50 +1000877mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000878{
879 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000880 u_int i, num, *echo_on;
881 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000882
883 debug3("%s", __func__);
884 sshpam_authok = NULL;
885 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
886 if (ret == 0 && num == 0)
887 sshpam_authok = sshpam_ctxt;
888 if (num > 1 || name == NULL || info == NULL)
889 ret = -1;
890 buffer_clear(m);
891 buffer_put_int(m, ret);
892 buffer_put_cstring(m, name);
893 xfree(name);
894 buffer_put_cstring(m, info);
895 xfree(info);
896 buffer_put_int(m, num);
897 for (i = 0; i < num; ++i) {
898 buffer_put_cstring(m, prompts[i]);
899 xfree(prompts[i]);
900 buffer_put_int(m, echo_on[i]);
901 }
902 if (prompts != NULL)
903 xfree(prompts);
904 if (echo_on != NULL)
905 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000906 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000907 return (0);
908}
909
910int
Darren Tucker7eda5922004-06-22 13:22:50 +1000911mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000912{
913 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000914 u_int i, num;
915 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000916
917 debug3("%s", __func__);
918 sshpam_authok = NULL;
919 num = buffer_get_int(m);
920 if (num > 0) {
921 resp = xmalloc(num * sizeof(char *));
922 for (i = 0; i < num; ++i)
923 resp[i] = buffer_get_string(m, NULL);
924 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
925 for (i = 0; i < num; ++i)
926 xfree(resp[i]);
927 xfree(resp);
928 } else {
929 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
930 }
931 buffer_clear(m);
932 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000933 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000934 auth_method = "keyboard-interactive/pam";
935 if (ret == 0)
936 sshpam_authok = sshpam_ctxt;
937 return (0);
938}
939
940int
Darren Tucker7eda5922004-06-22 13:22:50 +1000941mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000942{
943
944 debug3("%s", __func__);
945 (sshpam_device.free_ctx)(sshpam_ctxt);
946 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000947 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000948 return (sshpam_authok == sshpam_ctxt);
949}
Damien Miller79418552002-04-23 20:28:48 +1000950#endif
951
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952static void
953mm_append_debug(Buffer *m)
954{
955 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000956 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957 buffer_append(m, buffer_ptr(&auth_debug),
958 buffer_len(&auth_debug));
959 buffer_clear(&auth_debug);
960 }
961}
962
963int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000964mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965{
966 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000967 char *cuser, *chost;
968 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969 u_int bloblen;
970 enum mm_keytype type = 0;
971 int allowed = 0;
972
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000973 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000974
975 type = buffer_get_int(m);
976 cuser = buffer_get_string(m, NULL);
977 chost = buffer_get_string(m, NULL);
978 blob = buffer_get_string(m, &bloblen);
979
980 key = key_from_blob(blob, bloblen);
981
982 if ((compat20 && type == MM_RSAHOSTKEY) ||
983 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000984 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000985
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
Damien Miller3e3b5142003-11-17 21:13:40 +1100988 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100989 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000991 allowed = options.pubkey_authentication &&
992 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993 break;
994 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000995 allowed = options.hostbased_authentication &&
996 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 cuser, chost, key);
998 break;
999 case MM_RSAHOSTKEY:
1000 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001001 allowed = options.rhosts_rsa_authentication &&
1002 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 cuser, chost, key);
1004 break;
1005 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001006 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001007 break;
1008 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 }
Damien Miller00111382003-03-10 11:21:17 +11001010 if (key != NULL)
1011 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001012
1013 /* clear temporarily storage (used by verify) */
1014 monitor_reset_key_state();
1015
1016 if (allowed) {
1017 /* Save temporarily for comparison in verify */
1018 key_blob = blob;
1019 key_bloblen = bloblen;
1020 key_blobtype = type;
1021 hostbased_cuser = cuser;
1022 hostbased_chost = chost;
1023 }
1024
1025 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001026 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
1028 buffer_clear(m);
1029 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001030 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031
1032 mm_append_debug(m);
1033
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001034 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001035
1036 if (type == MM_RSAHOSTKEY)
1037 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1038
1039 return (0);
1040}
1041
1042static int
1043monitor_valid_userblob(u_char *data, u_int datalen)
1044{
1045 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001046 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001047 u_int len;
1048 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049
1050 buffer_init(&b);
1051 buffer_append(&b, data, datalen);
1052
1053 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001054 p = buffer_ptr(&b);
1055 len = buffer_len(&b);
1056 if ((session_id2 == NULL) ||
1057 (len < session_id2_len) ||
1058 (memcmp(p, session_id2, session_id2_len) != 0))
1059 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001060 buffer_consume(&b, session_id2_len);
1061 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001062 p = buffer_get_string(&b, &len);
1063 if ((session_id2 == NULL) ||
1064 (len != session_id2_len) ||
1065 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001066 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001067 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001068 }
1069 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1070 fail++;
1071 p = buffer_get_string(&b, NULL);
1072 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001073 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074 authctxt->user, p);
1075 fail++;
1076 }
1077 xfree(p);
1078 buffer_skip_string(&b);
1079 if (datafellows & SSH_BUG_PKAUTH) {
1080 if (!buffer_get_char(&b))
1081 fail++;
1082 } else {
1083 p = buffer_get_string(&b, NULL);
1084 if (strcmp("publickey", p) != 0)
1085 fail++;
1086 xfree(p);
1087 if (!buffer_get_char(&b))
1088 fail++;
1089 buffer_skip_string(&b);
1090 }
1091 buffer_skip_string(&b);
1092 if (buffer_len(&b) != 0)
1093 fail++;
1094 buffer_free(&b);
1095 return (fail == 0);
1096}
1097
1098static int
Damien Millera10f5612002-09-12 09:49:15 +10001099monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1100 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101{
1102 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001103 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001104 u_int len;
1105 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001106
1107 buffer_init(&b);
1108 buffer_append(&b, data, datalen);
1109
Ben Lindstromf67e0772002-06-06 20:58:19 +00001110 p = buffer_get_string(&b, &len);
1111 if ((session_id2 == NULL) ||
1112 (len != session_id2_len) ||
1113 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001115 xfree(p);
1116
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001117 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1118 fail++;
1119 p = buffer_get_string(&b, NULL);
1120 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001121 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122 authctxt->user, p);
1123 fail++;
1124 }
1125 xfree(p);
1126 buffer_skip_string(&b); /* service */
1127 p = buffer_get_string(&b, NULL);
1128 if (strcmp(p, "hostbased") != 0)
1129 fail++;
1130 xfree(p);
1131 buffer_skip_string(&b); /* pkalg */
1132 buffer_skip_string(&b); /* pkblob */
1133
1134 /* verify client host, strip trailing dot if necessary */
1135 p = buffer_get_string(&b, NULL);
1136 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1137 p[len - 1] = '\0';
1138 if (strcmp(p, chost) != 0)
1139 fail++;
1140 xfree(p);
1141
1142 /* verify client user */
1143 p = buffer_get_string(&b, NULL);
1144 if (strcmp(p, cuser) != 0)
1145 fail++;
1146 xfree(p);
1147
1148 if (buffer_len(&b) != 0)
1149 fail++;
1150 buffer_free(&b);
1151 return (fail == 0);
1152}
1153
1154int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001155mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156{
1157 Key *key;
1158 u_char *signature, *data, *blob;
1159 u_int signaturelen, datalen, bloblen;
1160 int verified = 0;
1161 int valid_data = 0;
1162
1163 blob = buffer_get_string(m, &bloblen);
1164 signature = buffer_get_string(m, &signaturelen);
1165 data = buffer_get_string(m, &datalen);
1166
1167 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001168 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001169 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170
1171 key = key_from_blob(blob, bloblen);
1172 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001173 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001174
1175 switch (key_blobtype) {
1176 case MM_USERKEY:
1177 valid_data = monitor_valid_userblob(data, datalen);
1178 break;
1179 case MM_HOSTKEY:
1180 valid_data = monitor_valid_hostbasedblob(data, datalen,
1181 hostbased_cuser, hostbased_chost);
1182 break;
1183 default:
1184 valid_data = 0;
1185 break;
1186 }
1187 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001188 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189
1190 verified = key_verify(key, signature, signaturelen, data, datalen);
1191 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001192 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193
1194 key_free(key);
1195 xfree(blob);
1196 xfree(signature);
1197 xfree(data);
1198
Ben Lindstrome1c09122002-06-23 00:38:24 +00001199 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1200
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001201 monitor_reset_key_state();
1202
1203 buffer_clear(m);
1204 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001205 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001206
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001207 return (verified);
1208}
1209
1210static void
1211mm_record_login(Session *s, struct passwd *pw)
1212{
1213 socklen_t fromlen;
1214 struct sockaddr_storage from;
1215
1216 /*
1217 * Get IP address of client. If the connection is not a socket, let
1218 * the address be 0.0.0.0.
1219 */
1220 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001221 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001222 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 if (getpeername(packet_get_connection_in(),
1224 (struct sockaddr *) & from, &fromlen) < 0) {
1225 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001226 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227 }
1228 }
1229 /* Record that there was a login on that tty from the remote host. */
1230 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001231 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001232 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233}
1234
1235static void
1236mm_session_close(Session *s)
1237{
Damien Miller04bd8b02003-05-25 14:38:33 +10001238 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001239 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001240 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241 session_pty_cleanup2(s);
1242 }
1243 s->used = 0;
1244}
1245
1246int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001247mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001249 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250 Session *s;
1251 int res, fd0;
1252
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001253 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001254
1255 buffer_clear(m);
1256 s = session_new();
1257 if (s == NULL)
1258 goto error;
1259 s->authctxt = authctxt;
1260 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001261 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1263 if (res == 0)
1264 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265 pty_setowner(authctxt->pw, s->tty);
1266
1267 buffer_put_int(m, 1);
1268 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269
1270 /* We need to trick ttyslot */
1271 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001272 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
1274 mm_record_login(s, authctxt->pw);
1275
1276 /* Now we can close the file descriptor again */
1277 close(0);
1278
Darren Tucker09991742004-07-17 17:05:14 +10001279 /* send messages generated by record_login */
1280 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1281 buffer_clear(&loginmsg);
1282
1283 mm_request_send(sock, MONITOR_ANS_PTY, m);
1284
1285 mm_send_fd(sock, s->ptyfd);
1286 mm_send_fd(sock, s->ttyfd);
1287
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288 /* make sure nothing uses fd 0 */
1289 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001290 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001291 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001292 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293
1294 /* slave is not needed */
1295 close(s->ttyfd);
1296 s->ttyfd = s->ptyfd;
1297 /* no need to dup() because nobody closes ptyfd */
1298 s->ptymaster = s->ptyfd;
1299
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001300 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001301
1302 return (0);
1303
1304 error:
1305 if (s != NULL)
1306 mm_session_close(s);
1307 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001308 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001309 return (0);
1310}
1311
1312int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001313mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314{
1315 Session *s;
1316 char *tty;
1317
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001318 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001319
1320 tty = buffer_get_string(m, NULL);
1321 if ((s = session_by_tty(tty)) != NULL)
1322 mm_session_close(s);
1323 buffer_clear(m);
1324 xfree(tty);
1325 return (0);
1326}
1327
1328int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001329mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001330{
1331 BIGNUM *p;
1332 int rsafail;
1333
1334 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001335 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001336
1337 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001338 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001339
1340 buffer_get_bignum2(m, p);
1341
1342 rsafail = ssh1_session_key(p);
1343
1344 buffer_clear(m);
1345 buffer_put_int(m, rsafail);
1346 buffer_put_bignum2(m, p);
1347
1348 BN_clear_free(p);
1349
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001350 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351
1352 /* Turn on permissions for sessid passing */
1353 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1354
1355 return (0);
1356}
1357
1358int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001359mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360{
1361 int i;
1362
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001363 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364
1365 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001366 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001367 for (i = 0; i < 16; i++)
1368 session_id[i] = buffer_get_char(m);
1369
1370 /* Turn on permissions for getpwnam */
1371 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1372
1373 return (0);
1374}
1375
1376int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001377mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378{
1379 BIGNUM *client_n;
1380 Key *key = NULL;
1381 u_char *blob = NULL;
1382 u_int blen = 0;
1383 int allowed = 0;
1384
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001385 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001387 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001389 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 buffer_get_bignum2(m, client_n);
1391 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1392 BN_clear_free(client_n);
1393 }
1394 buffer_clear(m);
1395 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001396 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397
1398 /* clear temporarily storage (used by generate challenge) */
1399 monitor_reset_key_state();
1400
1401 if (allowed && key != NULL) {
1402 key->type = KEY_RSA; /* cheat for key_to_blob */
1403 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001404 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405 buffer_put_string(m, blob, blen);
1406
1407 /* Save temporarily for comparison in verify */
1408 key_blob = blob;
1409 key_bloblen = blen;
1410 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 }
Damien Miller00111382003-03-10 11:21:17 +11001412 if (key != NULL)
1413 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
1415 mm_append_debug(m);
1416
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001417 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001418
1419 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1420 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1421 return (0);
1422}
1423
1424int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001425mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426{
1427 Key *key = NULL;
1428 u_char *blob;
1429 u_int blen;
1430
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001431 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432
1433 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001434 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001435 blob = buffer_get_string(m, &blen);
1436 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001437 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001441 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442
1443 if (ssh1_challenge)
1444 BN_clear_free(ssh1_challenge);
1445 ssh1_challenge = auth_rsa_generate_challenge(key);
1446
1447 buffer_clear(m);
1448 buffer_put_bignum2(m, ssh1_challenge);
1449
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001450 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001451 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001452
1453 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001454
1455 xfree(blob);
1456 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 return (0);
1458}
1459
1460int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001461mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001462{
1463 Key *key = NULL;
1464 u_char *blob, *response;
1465 u_int blen, len;
1466 int success;
1467
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001468 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001469
1470 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001471 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001472 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474
1475 blob = buffer_get_string(m, &blen);
1476 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001477 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001479 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001480 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001481 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001482 response = buffer_get_string(m, &len);
1483 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001484 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001485 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1486
Damien Miller00111382003-03-10 11:21:17 +11001487 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001488 key_free(key);
1489 xfree(response);
1490
1491 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1492
1493 /* reset state */
1494 BN_clear_free(ssh1_challenge);
1495 ssh1_challenge = NULL;
1496 monitor_reset_key_state();
1497
1498 buffer_clear(m);
1499 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001500 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001501
1502 return (success);
1503}
1504
1505int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001506mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001508 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001509 int res, status;
1510
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001511 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512
1513 /* The child is terminating */
1514 session_destroy_all(&mm_session_close);
1515
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001516 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001517 if (errno != EINTR)
1518 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001519
1520 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1521
1522 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001523 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001524}
1525
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001526#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001527/* Report that an audit event occurred */
1528int
1529mm_answer_audit_event(int socket, Buffer *m)
1530{
1531 ssh_audit_event_t event;
1532
1533 debug3("%s entering", __func__);
1534
1535 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001536 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001537 case SSH_AUTH_FAIL_PUBKEY:
1538 case SSH_AUTH_FAIL_HOSTBASED:
1539 case SSH_AUTH_FAIL_GSSAPI:
1540 case SSH_LOGIN_EXCEED_MAXTRIES:
1541 case SSH_LOGIN_ROOT_DENIED:
1542 case SSH_CONNECTION_CLOSE:
1543 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001544 audit_event(event);
1545 break;
1546 default:
1547 fatal("Audit event type %d not permitted", event);
1548 }
1549
1550 return (0);
1551}
1552
1553int
1554mm_answer_audit_command(int socket, Buffer *m)
1555{
1556 u_int len;
1557 char *cmd;
1558
1559 debug3("%s entering", __func__);
1560 cmd = buffer_get_string(m, &len);
1561 /* sanity check command, if so how? */
1562 audit_run_command(cmd);
1563 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001564 return (0);
1565}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001566#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001567
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001568void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001569monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001570{
1571 if (compat20) {
1572 set_newkeys(MODE_IN);
1573 set_newkeys(MODE_OUT);
1574 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001576 packet_set_encryption_key(child_state.ssh1key,
1577 child_state.ssh1keylen, child_state.ssh1cipher);
1578 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001579 }
1580
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001581 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582 packet_set_keycontext(MODE_OUT, child_state.keyout);
1583 xfree(child_state.keyout);
1584 packet_set_keycontext(MODE_IN, child_state.keyin);
1585 xfree(child_state.keyin);
1586
1587 if (!compat20) {
1588 packet_set_iv(MODE_OUT, child_state.ivout);
1589 xfree(child_state.ivout);
1590 packet_set_iv(MODE_IN, child_state.ivin);
1591 xfree(child_state.ivin);
1592 }
1593
1594 memcpy(&incoming_stream, &child_state.incoming,
1595 sizeof(incoming_stream));
1596 memcpy(&outgoing_stream, &child_state.outgoing,
1597 sizeof(outgoing_stream));
1598
1599 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001600 if (options.compression)
1601 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602
1603 /* Network I/O buffers */
1604 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1605 buffer_clear(&input);
1606 buffer_append(&input, child_state.input, child_state.ilen);
1607 memset(child_state.input, 0, child_state.ilen);
1608 xfree(child_state.input);
1609
1610 buffer_clear(&output);
1611 buffer_append(&output, child_state.output, child_state.olen);
1612 memset(child_state.output, 0, child_state.olen);
1613 xfree(child_state.output);
1614}
1615
1616static Kex *
1617mm_get_kex(Buffer *m)
1618{
1619 Kex *kex;
1620 void *blob;
1621 u_int bloblen;
1622
1623 kex = xmalloc(sizeof(*kex));
1624 memset(kex, 0, sizeof(*kex));
1625 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001626 if ((session_id2 == NULL) ||
1627 (kex->session_id_len != session_id2_len) ||
1628 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1629 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001631 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001632 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001633 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Damien Millera63128d2006-03-15 12:08:28 +11001634 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635 kex->server = 1;
1636 kex->hostkey_type = buffer_get_int(m);
1637 kex->kex_type = buffer_get_int(m);
1638 blob = buffer_get_string(m, &bloblen);
1639 buffer_init(&kex->my);
1640 buffer_append(&kex->my, blob, bloblen);
1641 xfree(blob);
1642 blob = buffer_get_string(m, &bloblen);
1643 buffer_init(&kex->peer);
1644 buffer_append(&kex->peer, blob, bloblen);
1645 xfree(blob);
1646 kex->done = 1;
1647 kex->flags = buffer_get_int(m);
1648 kex->client_version_string = buffer_get_string(m, NULL);
1649 kex->server_version_string = buffer_get_string(m, NULL);
1650 kex->load_host_key=&get_hostkey_by_type;
1651 kex->host_key_index=&get_hostkey_index;
1652
1653 return (kex);
1654}
1655
1656/* This function requries careful sanity checking */
1657
1658void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001659mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660{
1661 Buffer m;
1662 u_char *blob, *p;
1663 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001664 u_int32_t seqnr, packets;
1665 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001667 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001668
1669 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001670 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 if (!compat20) {
1672 child_state.ssh1protoflags = buffer_get_int(&m);
1673 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001674 child_state.ssh1key = buffer_get_string(&m,
1675 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001676 child_state.ivout = buffer_get_string(&m,
1677 &child_state.ivoutlen);
1678 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1679 goto skip;
1680 } else {
1681 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001682 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001683 }
1684
1685 blob = buffer_get_string(&m, &bloblen);
1686 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1687 xfree(blob);
1688
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001689 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001690 blob = buffer_get_string(&m, &bloblen);
1691 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1692 xfree(blob);
1693
1694 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001695 seqnr = buffer_get_int(&m);
1696 blocks = buffer_get_int64(&m);
1697 packets = buffer_get_int(&m);
1698 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1699 seqnr = buffer_get_int(&m);
1700 blocks = buffer_get_int64(&m);
1701 packets = buffer_get_int(&m);
1702 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001703
1704 skip:
1705 /* Get the key context */
1706 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1707 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1708
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001709 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710 /* Get compression state */
1711 p = buffer_get_string(&m, &plen);
1712 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001713 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001714 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1715 xfree(p);
1716
1717 p = buffer_get_string(&m, &plen);
1718 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001719 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1721 xfree(p);
1722
1723 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001724 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725 child_state.input = buffer_get_string(&m, &child_state.ilen);
1726 child_state.output = buffer_get_string(&m, &child_state.olen);
1727
1728 buffer_free(&m);
1729}
1730
1731
1732/* Allocation functions for zlib */
1733void *
1734mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1735{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001736 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001737 void *address;
1738
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001739 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001740 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1741
1742 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743
1744 return (address);
1745}
1746
1747void
1748mm_zfree(struct mm_master *mm, void *address)
1749{
1750 mm_free(mm, address);
1751}
1752
1753void
1754mm_init_compression(struct mm_master *mm)
1755{
1756 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1757 outgoing_stream.zfree = (free_func)mm_zfree;
1758 outgoing_stream.opaque = mm;
1759
1760 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1761 incoming_stream.zfree = (free_func)mm_zfree;
1762 incoming_stream.opaque = mm;
1763}
1764
1765/* XXX */
1766
1767#define FD_CLOSEONEXEC(x) do { \
1768 if (fcntl(x, F_SETFD, 1) == -1) \
1769 fatal("fcntl(%d, F_SETFD)", x); \
1770} while (0)
1771
1772static void
1773monitor_socketpair(int *pair)
1774{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001775#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001777 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001778#else
1779 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001780 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001781#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001782 FD_CLOSEONEXEC(pair[0]);
1783 FD_CLOSEONEXEC(pair[1]);
1784}
1785
1786#define MM_MEMSIZE 65536
1787
1788struct monitor *
1789monitor_init(void)
1790{
1791 struct monitor *mon;
1792 int pair[2];
1793
1794 mon = xmalloc(sizeof(*mon));
1795
Darren Tuckera8be9e22004-02-06 16:40:27 +11001796 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001797 monitor_socketpair(pair);
1798
1799 mon->m_recvfd = pair[0];
1800 mon->m_sendfd = pair[1];
1801
1802 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001803 if (options.compression) {
1804 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1805 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001806
Damien Miller2d6b8332002-06-21 15:59:49 +10001807 /* Compression needs to share state across borders */
1808 mm_init_compression(mon->m_zlib);
1809 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001810
1811 return mon;
1812}
1813
1814void
1815monitor_reinit(struct monitor *mon)
1816{
1817 int pair[2];
1818
1819 monitor_socketpair(pair);
1820
1821 mon->m_recvfd = pair[0];
1822 mon->m_sendfd = pair[1];
1823}
Darren Tucker0efd1552003-08-26 11:49:55 +10001824
1825#ifdef GSSAPI
1826int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001827mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001828{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001829 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001830 OM_uint32 major;
1831 u_int len;
1832
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001833 goid.elements = buffer_get_string(m, &len);
1834 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001835
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001836 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001837
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001838 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001839
1840 buffer_clear(m);
1841 buffer_put_int(m, major);
1842
Damien Miller6fd6def2005-11-05 15:07:05 +11001843 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001844
1845 /* Now we have a context, enable the step */
1846 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1847
1848 return (0);
1849}
1850
1851int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001852mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001853{
1854 gss_buffer_desc in;
1855 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001856 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001857 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001858 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001859
Darren Tucker600ad8d2003-08-26 12:10:48 +10001860 in.value = buffer_get_string(m, &len);
1861 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001862 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1863 xfree(in.value);
1864
1865 buffer_clear(m);
1866 buffer_put_int(m, major);
1867 buffer_put_string(m, out.value, out.length);
1868 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001869 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001870
1871 gss_release_buffer(&minor, &out);
1872
Damien Miller6fd6def2005-11-05 15:07:05 +11001873 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001874 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1875 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001877 }
1878 return (0);
1879}
1880
1881int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001882mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001883{
1884 gss_buffer_desc gssbuf, mic;
1885 OM_uint32 ret;
1886 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001887
Damien Miller0425d402003-11-17 22:18:21 +11001888 gssbuf.value = buffer_get_string(m, &len);
1889 gssbuf.length = len;
1890 mic.value = buffer_get_string(m, &len);
1891 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001892
Damien Miller0425d402003-11-17 22:18:21 +11001893 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001894
Damien Miller0425d402003-11-17 22:18:21 +11001895 xfree(gssbuf.value);
1896 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001897
Damien Miller0425d402003-11-17 22:18:21 +11001898 buffer_clear(m);
1899 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001900
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001901 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001902
Damien Miller0425d402003-11-17 22:18:21 +11001903 if (!GSS_ERROR(ret))
1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001905
Damien Miller0425d402003-11-17 22:18:21 +11001906 return (0);
1907}
1908
1909int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001910mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001911{
1912 int authenticated;
1913
1914 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1915
1916 buffer_clear(m);
1917 buffer_put_int(m, authenticated);
1918
1919 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001920 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001921
Damien Miller6fd6def2005-11-05 15:07:05 +11001922 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001923
1924 /* Monitor loop will terminate if authenticated */
1925 return (authenticated);
1926}
1927#endif /* GSSAPI */