blob: 3260d473d5822f154bf2eb8e77d0793980e5d123 [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 Miller6ff3cad2006-03-15 11:52:09 +110028RCSID("$OpenBSD: monitor.c,v 1.68 2006/02/20 17:02:44 stevesk 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
546 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000547 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548
Ben Lindstromf67e0772002-06-06 20:58:19 +0000549 /* save session id, it will be passed on the first call */
550 if (session_id2_len == 0) {
551 session_id2_len = datlen;
552 session_id2 = xmalloc(session_id2_len);
553 memcpy(session_id2, p, session_id2_len);
554 }
555
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000556 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000557 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000559 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000560
Ben Lindstromd5502182002-06-27 00:12:57 +0000561 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000562
563 buffer_clear(m);
564 buffer_put_string(m, signature, siglen);
565
566 xfree(p);
567 xfree(signature);
568
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000569 mm_request_send(sock, MONITOR_ANS_SIGN, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570
571 /* Turn on permissions for getpwnam */
572 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
573
574 return (0);
575}
576
577/* Retrieves the password entry and also checks if the user is permitted */
578
579int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000580mm_answer_pwnamallow(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581{
Darren Tuckerb09b6772004-06-22 15:06:46 +1000582 char *username;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000583 struct passwd *pwent;
584 int allowed = 0;
585
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000586 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000587
588 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000589 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
Darren Tuckerb09b6772004-06-22 15:06:46 +1000591 username = buffer_get_string(m, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592
Darren Tuckerb09b6772004-06-22 15:06:46 +1000593 pwent = getpwnamallow(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
Darren Tuckerb09b6772004-06-22 15:06:46 +1000595 authctxt->user = xstrdup(username);
596 setproctitle("%s [priv]", pwent ? username : "unknown");
597 xfree(username);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000598
599 buffer_clear(m);
600
601 if (pwent == NULL) {
602 buffer_put_char(m, 0);
Damien Millerf96d1832003-11-18 22:01:48 +1100603 authctxt->pw = fakepw();
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000604 goto out;
605 }
606
607 allowed = 1;
608 authctxt->pw = pwent;
609 authctxt->valid = 1;
610
611 buffer_put_char(m, 1);
612 buffer_put_string(m, pwent, sizeof(struct passwd));
613 buffer_put_cstring(m, pwent->pw_name);
614 buffer_put_cstring(m, "*");
615 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000616#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000618#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000619 buffer_put_cstring(m, pwent->pw_dir);
620 buffer_put_cstring(m, pwent->pw_shell);
621
622 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000623 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000624 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625
626 /* For SSHv1 allow authentication now */
627 if (!compat20)
628 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000629 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000630 /* Allow service/style information on the auth context */
631 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000632 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
633 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634
Damien Millera33501b2002-05-08 12:24:42 +1000635#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000636 if (options.use_pam)
637 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000638#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100639#ifdef SSH_AUDIT_EVENTS
Darren Tucker3745e2b2005-03-06 22:31:35 +1100640 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100641#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000642
643 return (0);
644}
645
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000646int mm_answer_auth2_read_banner(int sock, Buffer *m)
Damien Miller5ad9fd92002-05-13 11:07:41 +1000647{
648 char *banner;
649
650 buffer_clear(m);
651 banner = auth2_read_banner();
652 buffer_put_cstring(m, banner != NULL ? banner : "");
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000653 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000654
655 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000656 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000657
658 return (0);
659}
660
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000661int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000662mm_answer_authserv(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663{
664 monitor_permit_authentications(1);
665
666 authctxt->service = buffer_get_string(m, NULL);
667 authctxt->style = buffer_get_string(m, NULL);
668 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000669 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670
671 if (strlen(authctxt->style) == 0) {
672 xfree(authctxt->style);
673 authctxt->style = NULL;
674 }
675
676 return (0);
677}
678
679int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000680mm_answer_authpassword(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000681{
682 static int call_count;
683 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000684 int authenticated;
685 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686
687 passwd = buffer_get_string(m, &plen);
688 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000689 authenticated = options.password_authentication &&
Damien Miller856f0be2003-09-03 07:32:45 +1000690 auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000691 memset(passwd, 0, strlen(passwd));
692 xfree(passwd);
693
694 buffer_clear(m);
695 buffer_put_int(m, authenticated);
696
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000697 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000698 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000699
700 call_count++;
701 if (plen == 0 && call_count == 1)
702 auth_method = "none";
703 else
704 auth_method = "password";
705
706 /* Causes monitor loop to terminate if authenticated */
707 return (authenticated);
708}
709
710#ifdef BSD_AUTH
711int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000712mm_answer_bsdauthquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713{
714 char *name, *infotxt;
715 u_int numprompts;
716 u_int *echo_on;
717 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100718 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719
Damien Millerb7df3af2003-02-24 11:55:46 +1100720 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
721 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000722
723 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100724 buffer_put_int(m, success);
725 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000726 buffer_put_cstring(m, prompts[0]);
727
Damien Millerb7df3af2003-02-24 11:55:46 +1100728 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000729 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000730
Damien Millerb7df3af2003-02-24 11:55:46 +1100731 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000732 xfree(name);
733 xfree(infotxt);
734 xfree(prompts);
735 xfree(echo_on);
736 }
737
738 return (0);
739}
740
741int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000742mm_answer_bsdauthrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743{
744 char *response;
745 int authok;
746
747 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000748 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000749
750 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000751 authok = options.challenge_response_authentication &&
752 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000753 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000754 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000755 xfree(response);
756
757 buffer_clear(m);
758 buffer_put_int(m, authok);
759
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000760 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000761 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000762
763 auth_method = "bsdauth";
764
765 return (authok != 0);
766}
767#endif
768
769#ifdef SKEY
770int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000771mm_answer_skeyquery(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772{
773 struct skey skey;
774 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100775 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000776
Darren Tucker06a8cfe2004-04-14 17:24:30 +1000777 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
778 sizeof(challenge)) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000779
780 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100781 buffer_put_int(m, success);
782 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000783 buffer_put_cstring(m, challenge);
784
Damien Millerb7df3af2003-02-24 11:55:46 +1100785 debug3("%s: sending challenge success: %u", __func__, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000786 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787
788 return (0);
789}
790
791int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000792mm_answer_skeyrespond(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793{
794 char *response;
795 int authok;
796
797 response = buffer_get_string(m, NULL);
798
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000799 authok = (options.challenge_response_authentication &&
800 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000801 skey_haskey(authctxt->pw->pw_name) == 0 &&
802 skey_passcheck(authctxt->pw->pw_name, response) != -1);
803
804 xfree(response);
805
806 buffer_clear(m);
807 buffer_put_int(m, authok);
808
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000809 debug3("%s: sending authenticated: %d", __func__, authok);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000810 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811
812 auth_method = "skey";
813
814 return (authok != 0);
815}
816#endif
817
Damien Miller79418552002-04-23 20:28:48 +1000818#ifdef USE_PAM
819int
Darren Tucker7eda5922004-06-22 13:22:50 +1000820mm_answer_pam_start(int sock, Buffer *m)
Damien Miller79418552002-04-23 20:28:48 +1000821{
Damien Miller4e448a32003-05-14 15:11:48 +1000822 if (!options.use_pam)
823 fatal("UsePAM not set, but ended up in %s anyway", __func__);
824
Darren Tuckerdbf7a742004-03-08 23:04:06 +1100825 start_pam(authctxt);
Damien Miller79418552002-04-23 20:28:48 +1000826
Damien Miller1f499fd2003-08-25 13:08:49 +1000827 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
828
Damien Miller79418552002-04-23 20:28:48 +1000829 return (0);
830}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000831
Damien Miller1f499fd2003-08-25 13:08:49 +1000832int
Darren Tucker7eda5922004-06-22 13:22:50 +1000833mm_answer_pam_account(int sock, Buffer *m)
Damien Miller1f499fd2003-08-25 13:08:49 +1000834{
835 u_int ret;
Damien Miller787b2ec2003-11-21 23:56:47 +1100836
Damien Miller1f499fd2003-08-25 13:08:49 +1000837 if (!options.use_pam)
838 fatal("UsePAM not set, but ended up in %s anyway", __func__);
839
840 ret = do_pam_account();
841
842 buffer_put_int(m, ret);
Darren Tuckerd4f04ae2005-09-30 10:23:21 +1000843 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
Damien Miller1f499fd2003-08-25 13:08:49 +1000844
Darren Tucker7eda5922004-06-22 13:22:50 +1000845 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
Damien Miller1f499fd2003-08-25 13:08:49 +1000846
847 return (ret);
848}
849
Damien Miller4f9f42a2003-05-10 19:28:02 +1000850static void *sshpam_ctxt, *sshpam_authok;
851extern KbdintDevice sshpam_device;
852
853int
Darren Tucker7eda5922004-06-22 13:22:50 +1000854mm_answer_pam_init_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000855{
856
857 debug3("%s", __func__);
858 authctxt->user = buffer_get_string(m, NULL);
859 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
860 sshpam_authok = NULL;
861 buffer_clear(m);
862 if (sshpam_ctxt != NULL) {
863 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
864 buffer_put_int(m, 1);
865 } else {
866 buffer_put_int(m, 0);
867 }
Darren Tucker7eda5922004-06-22 13:22:50 +1000868 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000869 return (0);
870}
871
872int
Darren Tucker7eda5922004-06-22 13:22:50 +1000873mm_answer_pam_query(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000874{
875 char *name, *info, **prompts;
Damien Miller04b65332005-07-17 17:53:31 +1000876 u_int i, num, *echo_on;
877 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000878
879 debug3("%s", __func__);
880 sshpam_authok = NULL;
881 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
882 if (ret == 0 && num == 0)
883 sshpam_authok = sshpam_ctxt;
884 if (num > 1 || name == NULL || info == NULL)
885 ret = -1;
886 buffer_clear(m);
887 buffer_put_int(m, ret);
888 buffer_put_cstring(m, name);
889 xfree(name);
890 buffer_put_cstring(m, info);
891 xfree(info);
892 buffer_put_int(m, num);
893 for (i = 0; i < num; ++i) {
894 buffer_put_cstring(m, prompts[i]);
895 xfree(prompts[i]);
896 buffer_put_int(m, echo_on[i]);
897 }
898 if (prompts != NULL)
899 xfree(prompts);
900 if (echo_on != NULL)
901 xfree(echo_on);
Darren Tucker7eda5922004-06-22 13:22:50 +1000902 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000903 return (0);
904}
905
906int
Darren Tucker7eda5922004-06-22 13:22:50 +1000907mm_answer_pam_respond(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000908{
909 char **resp;
Damien Miller04b65332005-07-17 17:53:31 +1000910 u_int i, num;
911 int ret;
Damien Miller4f9f42a2003-05-10 19:28:02 +1000912
913 debug3("%s", __func__);
914 sshpam_authok = NULL;
915 num = buffer_get_int(m);
916 if (num > 0) {
917 resp = xmalloc(num * sizeof(char *));
918 for (i = 0; i < num; ++i)
919 resp[i] = buffer_get_string(m, NULL);
920 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
921 for (i = 0; i < num; ++i)
922 xfree(resp[i]);
923 xfree(resp);
924 } else {
925 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
926 }
927 buffer_clear(m);
928 buffer_put_int(m, ret);
Darren Tucker7eda5922004-06-22 13:22:50 +1000929 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000930 auth_method = "keyboard-interactive/pam";
931 if (ret == 0)
932 sshpam_authok = sshpam_ctxt;
933 return (0);
934}
935
936int
Darren Tucker7eda5922004-06-22 13:22:50 +1000937mm_answer_pam_free_ctx(int sock, Buffer *m)
Damien Miller4f9f42a2003-05-10 19:28:02 +1000938{
939
940 debug3("%s", __func__);
941 (sshpam_device.free_ctx)(sshpam_ctxt);
942 buffer_clear(m);
Darren Tucker7eda5922004-06-22 13:22:50 +1000943 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000944 return (sshpam_authok == sshpam_ctxt);
945}
Damien Miller79418552002-04-23 20:28:48 +1000946#endif
947
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000948static void
949mm_append_debug(Buffer *m)
950{
951 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953 buffer_append(m, buffer_ptr(&auth_debug),
954 buffer_len(&auth_debug));
955 buffer_clear(&auth_debug);
956 }
957}
958
959int
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960mm_answer_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961{
962 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000963 char *cuser, *chost;
964 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965 u_int bloblen;
966 enum mm_keytype type = 0;
967 int allowed = 0;
968
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000969 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000970
971 type = buffer_get_int(m);
972 cuser = buffer_get_string(m, NULL);
973 chost = buffer_get_string(m, NULL);
974 blob = buffer_get_string(m, &bloblen);
975
976 key = key_from_blob(blob, bloblen);
977
978 if ((compat20 && type == MM_RSAHOSTKEY) ||
979 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000980 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000981
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000982 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000983
Damien Miller3e3b5142003-11-17 21:13:40 +1100984 if (key != NULL && authctxt->valid) {
Darren Tucker47eede72005-03-14 23:08:12 +1100985 switch (type) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000987 allowed = options.pubkey_authentication &&
988 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989 break;
990 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000991 allowed = options.hostbased_authentication &&
992 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000993 cuser, chost, key);
994 break;
995 case MM_RSAHOSTKEY:
996 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000997 allowed = options.rhosts_rsa_authentication &&
998 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999 cuser, chost, key);
1000 break;
1001 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001002 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 break;
1004 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 }
Damien Miller00111382003-03-10 11:21:17 +11001006 if (key != NULL)
1007 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001008
1009 /* clear temporarily storage (used by verify) */
1010 monitor_reset_key_state();
1011
1012 if (allowed) {
1013 /* Save temporarily for comparison in verify */
1014 key_blob = blob;
1015 key_bloblen = bloblen;
1016 key_blobtype = type;
1017 hostbased_cuser = cuser;
1018 hostbased_chost = chost;
1019 }
1020
1021 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001022 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023
1024 buffer_clear(m);
1025 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001026 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027
1028 mm_append_debug(m);
1029
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001030 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031
1032 if (type == MM_RSAHOSTKEY)
1033 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1034
1035 return (0);
1036}
1037
1038static int
1039monitor_valid_userblob(u_char *data, u_int datalen)
1040{
1041 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001042 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001043 u_int len;
1044 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001045
1046 buffer_init(&b);
1047 buffer_append(&b, data, datalen);
1048
1049 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001050 p = buffer_ptr(&b);
1051 len = buffer_len(&b);
1052 if ((session_id2 == NULL) ||
1053 (len < session_id2_len) ||
1054 (memcmp(p, session_id2, session_id2_len) != 0))
1055 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001056 buffer_consume(&b, session_id2_len);
1057 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001058 p = buffer_get_string(&b, &len);
1059 if ((session_id2 == NULL) ||
1060 (len != session_id2_len) ||
1061 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001063 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001064 }
1065 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1066 fail++;
1067 p = buffer_get_string(&b, NULL);
1068 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001069 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001070 authctxt->user, p);
1071 fail++;
1072 }
1073 xfree(p);
1074 buffer_skip_string(&b);
1075 if (datafellows & SSH_BUG_PKAUTH) {
1076 if (!buffer_get_char(&b))
1077 fail++;
1078 } else {
1079 p = buffer_get_string(&b, NULL);
1080 if (strcmp("publickey", p) != 0)
1081 fail++;
1082 xfree(p);
1083 if (!buffer_get_char(&b))
1084 fail++;
1085 buffer_skip_string(&b);
1086 }
1087 buffer_skip_string(&b);
1088 if (buffer_len(&b) != 0)
1089 fail++;
1090 buffer_free(&b);
1091 return (fail == 0);
1092}
1093
1094static int
Damien Millera10f5612002-09-12 09:49:15 +10001095monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1096 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001097{
1098 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001099 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001100 u_int len;
1101 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001102
1103 buffer_init(&b);
1104 buffer_append(&b, data, datalen);
1105
Ben Lindstromf67e0772002-06-06 20:58:19 +00001106 p = buffer_get_string(&b, &len);
1107 if ((session_id2 == NULL) ||
1108 (len != session_id2_len) ||
1109 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001110 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001111 xfree(p);
1112
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001113 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1114 fail++;
1115 p = buffer_get_string(&b, NULL);
1116 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001117 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118 authctxt->user, p);
1119 fail++;
1120 }
1121 xfree(p);
1122 buffer_skip_string(&b); /* service */
1123 p = buffer_get_string(&b, NULL);
1124 if (strcmp(p, "hostbased") != 0)
1125 fail++;
1126 xfree(p);
1127 buffer_skip_string(&b); /* pkalg */
1128 buffer_skip_string(&b); /* pkblob */
1129
1130 /* verify client host, strip trailing dot if necessary */
1131 p = buffer_get_string(&b, NULL);
1132 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1133 p[len - 1] = '\0';
1134 if (strcmp(p, chost) != 0)
1135 fail++;
1136 xfree(p);
1137
1138 /* verify client user */
1139 p = buffer_get_string(&b, NULL);
1140 if (strcmp(p, cuser) != 0)
1141 fail++;
1142 xfree(p);
1143
1144 if (buffer_len(&b) != 0)
1145 fail++;
1146 buffer_free(&b);
1147 return (fail == 0);
1148}
1149
1150int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001151mm_answer_keyverify(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152{
1153 Key *key;
1154 u_char *signature, *data, *blob;
1155 u_int signaturelen, datalen, bloblen;
1156 int verified = 0;
1157 int valid_data = 0;
1158
1159 blob = buffer_get_string(m, &bloblen);
1160 signature = buffer_get_string(m, &signaturelen);
1161 data = buffer_get_string(m, &datalen);
1162
1163 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001164 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001165 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166
1167 key = key_from_blob(blob, bloblen);
1168 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001169 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170
1171 switch (key_blobtype) {
1172 case MM_USERKEY:
1173 valid_data = monitor_valid_userblob(data, datalen);
1174 break;
1175 case MM_HOSTKEY:
1176 valid_data = monitor_valid_hostbasedblob(data, datalen,
1177 hostbased_cuser, hostbased_chost);
1178 break;
1179 default:
1180 valid_data = 0;
1181 break;
1182 }
1183 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001184 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185
1186 verified = key_verify(key, signature, signaturelen, data, datalen);
1187 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001188 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001189
1190 key_free(key);
1191 xfree(blob);
1192 xfree(signature);
1193 xfree(data);
1194
Ben Lindstrome1c09122002-06-23 00:38:24 +00001195 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1196
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001197 monitor_reset_key_state();
1198
1199 buffer_clear(m);
1200 buffer_put_int(m, verified);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001201 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203 return (verified);
1204}
1205
1206static void
1207mm_record_login(Session *s, struct passwd *pw)
1208{
1209 socklen_t fromlen;
1210 struct sockaddr_storage from;
1211
1212 /*
1213 * Get IP address of client. If the connection is not a socket, let
1214 * the address be 0.0.0.0.
1215 */
1216 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001217 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001218 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219 if (getpeername(packet_get_connection_in(),
1220 (struct sockaddr *) & from, &fromlen) < 0) {
1221 debug("getpeername: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +10001222 cleanup_exit(255);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001223 }
1224 }
1225 /* Record that there was a login on that tty from the remote host. */
1226 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001227 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001228 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229}
1230
1231static void
1232mm_session_close(Session *s)
1233{
Damien Miller04bd8b02003-05-25 14:38:33 +10001234 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001236 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001237 session_pty_cleanup2(s);
1238 }
1239 s->used = 0;
1240}
1241
1242int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001243mm_answer_pty(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001245 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001246 Session *s;
1247 int res, fd0;
1248
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001249 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001250
1251 buffer_clear(m);
1252 s = session_new();
1253 if (s == NULL)
1254 goto error;
1255 s->authctxt = authctxt;
1256 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001257 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001258 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1259 if (res == 0)
1260 goto error;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001261 pty_setowner(authctxt->pw, s->tty);
1262
1263 buffer_put_int(m, 1);
1264 buffer_put_cstring(m, s->tty);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265
1266 /* We need to trick ttyslot */
1267 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001268 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269
1270 mm_record_login(s, authctxt->pw);
1271
1272 /* Now we can close the file descriptor again */
1273 close(0);
1274
Darren Tucker09991742004-07-17 17:05:14 +10001275 /* send messages generated by record_login */
1276 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1277 buffer_clear(&loginmsg);
1278
1279 mm_request_send(sock, MONITOR_ANS_PTY, m);
1280
1281 mm_send_fd(sock, s->ptyfd);
1282 mm_send_fd(sock, s->ttyfd);
1283
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001284 /* make sure nothing uses fd 0 */
1285 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001286 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001287 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001288 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001289
1290 /* slave is not needed */
1291 close(s->ttyfd);
1292 s->ttyfd = s->ptyfd;
1293 /* no need to dup() because nobody closes ptyfd */
1294 s->ptymaster = s->ptyfd;
1295
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001296 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297
1298 return (0);
1299
1300 error:
1301 if (s != NULL)
1302 mm_session_close(s);
1303 buffer_put_int(m, 0);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001304 mm_request_send(sock, MONITOR_ANS_PTY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001305 return (0);
1306}
1307
1308int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001309mm_answer_pty_cleanup(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001310{
1311 Session *s;
1312 char *tty;
1313
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001314 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001315
1316 tty = buffer_get_string(m, NULL);
1317 if ((s = session_by_tty(tty)) != NULL)
1318 mm_session_close(s);
1319 buffer_clear(m);
1320 xfree(tty);
1321 return (0);
1322}
1323
1324int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001325mm_answer_sesskey(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001326{
1327 BIGNUM *p;
1328 int rsafail;
1329
1330 /* Turn off permissions */
Darren Tucker5b530262005-02-09 09:52:17 +11001331 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001332
1333 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001334 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001335
1336 buffer_get_bignum2(m, p);
1337
1338 rsafail = ssh1_session_key(p);
1339
1340 buffer_clear(m);
1341 buffer_put_int(m, rsafail);
1342 buffer_put_bignum2(m, p);
1343
1344 BN_clear_free(p);
1345
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001346 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347
1348 /* Turn on permissions for sessid passing */
1349 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1350
1351 return (0);
1352}
1353
1354int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001355mm_answer_sessid(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001356{
1357 int i;
1358
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001359 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360
1361 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001362 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363 for (i = 0; i < 16; i++)
1364 session_id[i] = buffer_get_char(m);
1365
1366 /* Turn on permissions for getpwnam */
1367 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1368
1369 return (0);
1370}
1371
1372int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001373mm_answer_rsa_keyallowed(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374{
1375 BIGNUM *client_n;
1376 Key *key = NULL;
1377 u_char *blob = NULL;
1378 u_int blen = 0;
1379 int allowed = 0;
1380
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001381 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001383 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001385 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386 buffer_get_bignum2(m, client_n);
1387 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1388 BN_clear_free(client_n);
1389 }
1390 buffer_clear(m);
1391 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001392 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001393
1394 /* clear temporarily storage (used by generate challenge) */
1395 monitor_reset_key_state();
1396
1397 if (allowed && key != NULL) {
1398 key->type = KEY_RSA; /* cheat for key_to_blob */
1399 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001400 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001401 buffer_put_string(m, blob, blen);
1402
1403 /* Save temporarily for comparison in verify */
1404 key_blob = blob;
1405 key_bloblen = blen;
1406 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001407 }
Damien Miller00111382003-03-10 11:21:17 +11001408 if (key != NULL)
1409 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001410
1411 mm_append_debug(m);
1412
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001413 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414
1415 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1416 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1417 return (0);
1418}
1419
1420int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001421mm_answer_rsa_challenge(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001422{
1423 Key *key = NULL;
1424 u_char *blob;
1425 u_int blen;
1426
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
1429 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001430 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001431 blob = buffer_get_string(m, &blen);
1432 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001435 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001437 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001438
1439 if (ssh1_challenge)
1440 BN_clear_free(ssh1_challenge);
1441 ssh1_challenge = auth_rsa_generate_challenge(key);
1442
1443 buffer_clear(m);
1444 buffer_put_bignum2(m, ssh1_challenge);
1445
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001446 debug3("%s sending reply", __func__);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001447 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001448
1449 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001450
1451 xfree(blob);
1452 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001453 return (0);
1454}
1455
1456int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001457mm_answer_rsa_response(int sock, Buffer *m)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001458{
1459 Key *key = NULL;
1460 u_char *blob, *response;
1461 u_int blen, len;
1462 int success;
1463
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001464 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465
1466 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001467 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001468 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001469 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001470
1471 blob = buffer_get_string(m, &blen);
1472 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001473 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001474 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001475 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001476 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001477 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001478 response = buffer_get_string(m, &len);
1479 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001480 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001481 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1482
Damien Miller00111382003-03-10 11:21:17 +11001483 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001484 key_free(key);
1485 xfree(response);
1486
1487 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1488
1489 /* reset state */
1490 BN_clear_free(ssh1_challenge);
1491 ssh1_challenge = NULL;
1492 monitor_reset_key_state();
1493
1494 buffer_clear(m);
1495 buffer_put_int(m, success);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001496 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497
1498 return (success);
1499}
1500
1501int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001502mm_answer_term(int sock, Buffer *req)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001504 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505 int res, status;
1506
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001507 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001508
1509 /* The child is terminating */
1510 session_destroy_all(&mm_session_close);
1511
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001512 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001513 if (errno != EINTR)
1514 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001515
1516 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1517
1518 /* Terminate process */
Darren Tucker1f8311c2004-05-13 16:39:33 +10001519 exit(res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520}
1521
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001522#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +11001523/* Report that an audit event occurred */
1524int
1525mm_answer_audit_event(int socket, Buffer *m)
1526{
1527 ssh_audit_event_t event;
1528
1529 debug3("%s entering", __func__);
1530
1531 event = buffer_get_int(m);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001532 switch(event) {
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001533 case SSH_AUTH_FAIL_PUBKEY:
1534 case SSH_AUTH_FAIL_HOSTBASED:
1535 case SSH_AUTH_FAIL_GSSAPI:
1536 case SSH_LOGIN_EXCEED_MAXTRIES:
1537 case SSH_LOGIN_ROOT_DENIED:
1538 case SSH_CONNECTION_CLOSE:
1539 case SSH_INVALID_USER:
Darren Tucker269a1ea2005-02-03 00:20:53 +11001540 audit_event(event);
1541 break;
1542 default:
1543 fatal("Audit event type %d not permitted", event);
1544 }
1545
1546 return (0);
1547}
1548
1549int
1550mm_answer_audit_command(int socket, Buffer *m)
1551{
1552 u_int len;
1553 char *cmd;
1554
1555 debug3("%s entering", __func__);
1556 cmd = buffer_get_string(m, &len);
1557 /* sanity check command, if so how? */
1558 audit_run_command(cmd);
1559 xfree(cmd);
Darren Tucker269a1ea2005-02-03 00:20:53 +11001560 return (0);
1561}
Darren Tucker2e0cf0d2005-02-08 21:52:47 +11001562#endif /* SSH_AUDIT_EVENTS */
Darren Tucker269a1ea2005-02-03 00:20:53 +11001563
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001564void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001565monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001566{
1567 if (compat20) {
1568 set_newkeys(MODE_IN);
1569 set_newkeys(MODE_OUT);
1570 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001572 packet_set_encryption_key(child_state.ssh1key,
1573 child_state.ssh1keylen, child_state.ssh1cipher);
1574 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001575 }
1576
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001577 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001578 packet_set_keycontext(MODE_OUT, child_state.keyout);
1579 xfree(child_state.keyout);
1580 packet_set_keycontext(MODE_IN, child_state.keyin);
1581 xfree(child_state.keyin);
1582
1583 if (!compat20) {
1584 packet_set_iv(MODE_OUT, child_state.ivout);
1585 xfree(child_state.ivout);
1586 packet_set_iv(MODE_IN, child_state.ivin);
1587 xfree(child_state.ivin);
1588 }
1589
1590 memcpy(&incoming_stream, &child_state.incoming,
1591 sizeof(incoming_stream));
1592 memcpy(&outgoing_stream, &child_state.outgoing,
1593 sizeof(outgoing_stream));
1594
1595 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001596 if (options.compression)
1597 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001598
1599 /* Network I/O buffers */
1600 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1601 buffer_clear(&input);
1602 buffer_append(&input, child_state.input, child_state.ilen);
1603 memset(child_state.input, 0, child_state.ilen);
1604 xfree(child_state.input);
1605
1606 buffer_clear(&output);
1607 buffer_append(&output, child_state.output, child_state.olen);
1608 memset(child_state.output, 0, child_state.olen);
1609 xfree(child_state.output);
1610}
1611
1612static Kex *
1613mm_get_kex(Buffer *m)
1614{
1615 Kex *kex;
1616 void *blob;
1617 u_int bloblen;
1618
1619 kex = xmalloc(sizeof(*kex));
1620 memset(kex, 0, sizeof(*kex));
1621 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001622 if ((session_id2 == NULL) ||
1623 (kex->session_id_len != session_id2_len) ||
1624 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1625 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001627 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
Damien Millerf675fc42004-06-15 10:30:09 +10001628 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
Damien Millerb062c292003-03-24 09:12:09 +11001629 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001630 kex->server = 1;
1631 kex->hostkey_type = buffer_get_int(m);
1632 kex->kex_type = buffer_get_int(m);
1633 blob = buffer_get_string(m, &bloblen);
1634 buffer_init(&kex->my);
1635 buffer_append(&kex->my, blob, bloblen);
1636 xfree(blob);
1637 blob = buffer_get_string(m, &bloblen);
1638 buffer_init(&kex->peer);
1639 buffer_append(&kex->peer, blob, bloblen);
1640 xfree(blob);
1641 kex->done = 1;
1642 kex->flags = buffer_get_int(m);
1643 kex->client_version_string = buffer_get_string(m, NULL);
1644 kex->server_version_string = buffer_get_string(m, NULL);
1645 kex->load_host_key=&get_hostkey_by_type;
1646 kex->host_key_index=&get_hostkey_index;
1647
1648 return (kex);
1649}
1650
1651/* This function requries careful sanity checking */
1652
1653void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001654mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655{
1656 Buffer m;
1657 u_char *blob, *p;
1658 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001659 u_int32_t seqnr, packets;
1660 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001662 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663
1664 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001665 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 if (!compat20) {
1667 child_state.ssh1protoflags = buffer_get_int(&m);
1668 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001669 child_state.ssh1key = buffer_get_string(&m,
1670 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001671 child_state.ivout = buffer_get_string(&m,
1672 &child_state.ivoutlen);
1673 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1674 goto skip;
1675 } else {
1676 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001677 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678 }
1679
1680 blob = buffer_get_string(&m, &bloblen);
1681 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1682 xfree(blob);
1683
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001684 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001685 blob = buffer_get_string(&m, &bloblen);
1686 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1687 xfree(blob);
1688
1689 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001690 seqnr = buffer_get_int(&m);
1691 blocks = buffer_get_int64(&m);
1692 packets = buffer_get_int(&m);
1693 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1694 seqnr = buffer_get_int(&m);
1695 blocks = buffer_get_int64(&m);
1696 packets = buffer_get_int(&m);
1697 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001698
1699 skip:
1700 /* Get the key context */
1701 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1702 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1703
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001704 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001705 /* Get compression state */
1706 p = buffer_get_string(&m, &plen);
1707 if (plen != sizeof(child_state.outgoing))
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.outgoing, p, sizeof(child_state.outgoing));
1710 xfree(p);
1711
1712 p = buffer_get_string(&m, &plen);
1713 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001714 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001715 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1716 xfree(p);
1717
1718 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001719 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001720 child_state.input = buffer_get_string(&m, &child_state.ilen);
1721 child_state.output = buffer_get_string(&m, &child_state.olen);
1722
1723 buffer_free(&m);
1724}
1725
1726
1727/* Allocation functions for zlib */
1728void *
1729mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1730{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001731 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732 void *address;
1733
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001734 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001735 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1736
1737 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001738
1739 return (address);
1740}
1741
1742void
1743mm_zfree(struct mm_master *mm, void *address)
1744{
1745 mm_free(mm, address);
1746}
1747
1748void
1749mm_init_compression(struct mm_master *mm)
1750{
1751 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1752 outgoing_stream.zfree = (free_func)mm_zfree;
1753 outgoing_stream.opaque = mm;
1754
1755 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1756 incoming_stream.zfree = (free_func)mm_zfree;
1757 incoming_stream.opaque = mm;
1758}
1759
1760/* XXX */
1761
1762#define FD_CLOSEONEXEC(x) do { \
1763 if (fcntl(x, F_SETFD, 1) == -1) \
1764 fatal("fcntl(%d, F_SETFD)", x); \
1765} while (0)
1766
1767static void
1768monitor_socketpair(int *pair)
1769{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001770#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001771 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001772 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001773#else
1774 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001775 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001776#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001777 FD_CLOSEONEXEC(pair[0]);
1778 FD_CLOSEONEXEC(pair[1]);
1779}
1780
1781#define MM_MEMSIZE 65536
1782
1783struct monitor *
1784monitor_init(void)
1785{
1786 struct monitor *mon;
1787 int pair[2];
1788
1789 mon = xmalloc(sizeof(*mon));
1790
Darren Tuckera8be9e22004-02-06 16:40:27 +11001791 mon->m_pid = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001792 monitor_socketpair(pair);
1793
1794 mon->m_recvfd = pair[0];
1795 mon->m_sendfd = pair[1];
1796
1797 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001798 if (options.compression) {
1799 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1800 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001801
Damien Miller2d6b8332002-06-21 15:59:49 +10001802 /* Compression needs to share state across borders */
1803 mm_init_compression(mon->m_zlib);
1804 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001805
1806 return mon;
1807}
1808
1809void
1810monitor_reinit(struct monitor *mon)
1811{
1812 int pair[2];
1813
1814 monitor_socketpair(pair);
1815
1816 mon->m_recvfd = pair[0];
1817 mon->m_sendfd = pair[1];
1818}
Darren Tucker0efd1552003-08-26 11:49:55 +10001819
1820#ifdef GSSAPI
1821int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001822mm_answer_gss_setup_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001823{
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001824 gss_OID_desc goid;
Darren Tucker0efd1552003-08-26 11:49:55 +10001825 OM_uint32 major;
1826 u_int len;
1827
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001828 goid.elements = buffer_get_string(m, &len);
1829 goid.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001830
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001831 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
Darren Tucker0efd1552003-08-26 11:49:55 +10001832
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001833 xfree(goid.elements);
Darren Tucker0efd1552003-08-26 11:49:55 +10001834
1835 buffer_clear(m);
1836 buffer_put_int(m, major);
1837
Damien Miller6fd6def2005-11-05 15:07:05 +11001838 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001839
1840 /* Now we have a context, enable the step */
1841 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1842
1843 return (0);
1844}
1845
1846int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001847mm_answer_gss_accept_ctx(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001848{
1849 gss_buffer_desc in;
1850 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
Damien Miller6fd6def2005-11-05 15:07:05 +11001851 OM_uint32 major, minor;
Darren Tucker0efd1552003-08-26 11:49:55 +10001852 OM_uint32 flags = 0; /* GSI needs this */
Darren Tucker600ad8d2003-08-26 12:10:48 +10001853 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001854
Darren Tucker600ad8d2003-08-26 12:10:48 +10001855 in.value = buffer_get_string(m, &len);
1856 in.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +10001857 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1858 xfree(in.value);
1859
1860 buffer_clear(m);
1861 buffer_put_int(m, major);
1862 buffer_put_string(m, out.value, out.length);
1863 buffer_put_int(m, flags);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001864 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001865
1866 gss_release_buffer(&minor, &out);
1867
Damien Miller6fd6def2005-11-05 15:07:05 +11001868 if (major == GSS_S_COMPLETE) {
Darren Tucker0efd1552003-08-26 11:49:55 +10001869 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1870 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller0425d402003-11-17 22:18:21 +11001871 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
Darren Tucker0efd1552003-08-26 11:49:55 +10001872 }
1873 return (0);
1874}
1875
1876int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001877mm_answer_gss_checkmic(int sock, Buffer *m)
Damien Miller0425d402003-11-17 22:18:21 +11001878{
1879 gss_buffer_desc gssbuf, mic;
1880 OM_uint32 ret;
1881 u_int len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001882
Damien Miller0425d402003-11-17 22:18:21 +11001883 gssbuf.value = buffer_get_string(m, &len);
1884 gssbuf.length = len;
1885 mic.value = buffer_get_string(m, &len);
1886 mic.length = len;
Damien Miller787b2ec2003-11-21 23:56:47 +11001887
Damien Miller0425d402003-11-17 22:18:21 +11001888 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +11001889
Damien Miller0425d402003-11-17 22:18:21 +11001890 xfree(gssbuf.value);
1891 xfree(mic.value);
Damien Miller787b2ec2003-11-21 23:56:47 +11001892
Damien Miller0425d402003-11-17 22:18:21 +11001893 buffer_clear(m);
1894 buffer_put_int(m, ret);
Damien Miller787b2ec2003-11-21 23:56:47 +11001895
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001896 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
Damien Miller787b2ec2003-11-21 23:56:47 +11001897
Damien Miller0425d402003-11-17 22:18:21 +11001898 if (!GSS_ERROR(ret))
1899 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
Damien Miller787b2ec2003-11-21 23:56:47 +11001900
Damien Miller0425d402003-11-17 22:18:21 +11001901 return (0);
1902}
1903
1904int
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001905mm_answer_gss_userok(int sock, Buffer *m)
Darren Tucker0efd1552003-08-26 11:49:55 +10001906{
1907 int authenticated;
1908
1909 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1910
1911 buffer_clear(m);
1912 buffer_put_int(m, authenticated);
1913
1914 debug3("%s: sending result %d", __func__, authenticated);
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001915 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
Darren Tucker0efd1552003-08-26 11:49:55 +10001916
Damien Miller6fd6def2005-11-05 15:07:05 +11001917 auth_method = "gssapi-with-mic";
Darren Tucker0efd1552003-08-26 11:49:55 +10001918
1919 /* Monitor loop will terminate if authenticated */
1920 return (authenticated);
1921}
1922#endif /* GSSAPI */