blob: f306794d4fb682c81d3bfe4509ede950743d310d [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 Miller469954d2003-06-18 20:25:33 +100028RCSID("$OpenBSD: monitor.c,v 1.43 2003/06/12 07:57:38 markus Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
40#include "zlib.h"
41#include "packet.h"
42#include "auth-options.h"
43#include "sshpty.h"
44#include "channels.h"
45#include "session.h"
46#include "sshlogin.h"
47#include "canohost.h"
48#include "log.h"
49#include "servconf.h"
50#include "monitor.h"
51#include "monitor_mm.h"
52#include "monitor_wrap.h"
53#include "monitor_fdpass.h"
54#include "xmalloc.h"
55#include "misc.h"
56#include "buffer.h"
57#include "bufaux.h"
58#include "compat.h"
59#include "ssh2.h"
60#include "mpaux.h"
61
62/* Imports */
63extern ServerOptions options;
64extern u_int utmp_len;
65extern Newkeys *current_keys[];
66extern z_stream incoming_stream;
67extern z_stream outgoing_stream;
68extern u_char session_id[];
69extern Buffer input, output;
70extern Buffer auth_debug;
71extern int auth_debug_init;
72
73/* State exported from the child */
74
75struct {
76 z_stream incoming;
77 z_stream outgoing;
78 u_char *keyin;
79 u_int keyinlen;
80 u_char *keyout;
81 u_int keyoutlen;
82 u_char *ivin;
83 u_int ivinlen;
84 u_char *ivout;
85 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000086 u_char *ssh1key;
87 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 int ssh1cipher;
89 int ssh1protoflags;
90 u_char *input;
91 u_int ilen;
92 u_char *output;
93 u_int olen;
94} child_state;
95
Damien Miller469954d2003-06-18 20:25:33 +100096/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097
98int mm_answer_moduli(int, Buffer *);
99int mm_answer_sign(int, Buffer *);
100int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000101int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102int mm_answer_authserv(int, Buffer *);
103int mm_answer_authpassword(int, Buffer *);
104int mm_answer_bsdauthquery(int, Buffer *);
105int mm_answer_bsdauthrespond(int, Buffer *);
106int mm_answer_skeyquery(int, Buffer *);
107int mm_answer_skeyrespond(int, Buffer *);
108int mm_answer_keyallowed(int, Buffer *);
109int mm_answer_keyverify(int, Buffer *);
110int mm_answer_pty(int, Buffer *);
111int mm_answer_pty_cleanup(int, Buffer *);
112int mm_answer_term(int, Buffer *);
113int mm_answer_rsa_keyallowed(int, Buffer *);
114int mm_answer_rsa_challenge(int, Buffer *);
115int mm_answer_rsa_response(int, Buffer *);
116int mm_answer_sesskey(int, Buffer *);
117int mm_answer_sessid(int, Buffer *);
118
Damien Miller79418552002-04-23 20:28:48 +1000119#ifdef USE_PAM
120int mm_answer_pam_start(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000121int mm_answer_pam_init_ctx(int, Buffer *);
122int mm_answer_pam_query(int, Buffer *);
123int mm_answer_pam_respond(int, Buffer *);
124int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000125#endif
126
Damien Millerd94e5492002-09-27 13:25:58 +1000127#ifdef KRB4
128int mm_answer_krb4(int, Buffer *);
129#endif
Damien Miller25162f22002-09-12 09:47:29 +1000130#ifdef KRB5
131int mm_answer_krb5(int, Buffer *);
132#endif
133
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134static Authctxt *authctxt;
135static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
136
137/* local state for key verify */
138static u_char *key_blob = NULL;
139static u_int key_bloblen = 0;
140static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000141static char *hostbased_cuser = NULL;
142static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143static char *auth_method = "unknown";
Ben Lindstromf67e0772002-06-06 20:58:19 +0000144static int session_id2_len = 0;
145static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000146static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000147
148struct mon_table {
149 enum monitor_reqtype type;
150 int flags;
151 int (*f)(int, Buffer *);
152};
153
154#define MON_ISAUTH 0x0004 /* Required for Authentication */
155#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
156#define MON_ONCE 0x0010 /* Disable after calling */
157
158#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
159
160#define MON_PERMIT 0x1000 /* Request is permitted */
161
162struct mon_table mon_dispatch_proto20[] = {
163 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
164 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
165 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
166 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000167 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000168 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000169#ifdef USE_PAM
170 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000171 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
172 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
173 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
174 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000175#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000176#ifdef BSD_AUTH
177 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
178 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
179#endif
180#ifdef SKEY
181 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
182 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
183#endif
184 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
185 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Damien Miller3ab496b2003-05-14 13:47:37 +1000186#ifdef KRB5
187 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
188#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000189 {0, 0, NULL}
190};
191
192struct mon_table mon_dispatch_postauth20[] = {
193 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
194 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
195 {MONITOR_REQ_PTY, 0, mm_answer_pty},
196 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
197 {MONITOR_REQ_TERM, 0, mm_answer_term},
198 {0, 0, NULL}
199};
200
201struct mon_table mon_dispatch_proto15[] = {
202 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
203 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
204 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
205 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
206 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
207 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
208 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
209 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
210#ifdef BSD_AUTH
211 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
212 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
213#endif
214#ifdef SKEY
215 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
216 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
217#endif
Damien Millera33501b2002-05-08 12:24:42 +1000218#ifdef USE_PAM
219 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000224#endif
Damien Millerd94e5492002-09-27 13:25:58 +1000225#ifdef KRB4
226 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
227#endif
Damien Miller25162f22002-09-12 09:47:29 +1000228#ifdef KRB5
229 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
230#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000231 {0, 0, NULL}
232};
233
234struct mon_table mon_dispatch_postauth15[] = {
235 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
236 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
237 {MONITOR_REQ_TERM, 0, mm_answer_term},
238 {0, 0, NULL}
239};
240
241struct mon_table *mon_dispatch;
242
243/* Specifies if a certain message is allowed at the moment */
244
245static void
246monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
247{
248 while (ent->f != NULL) {
249 if (ent->type == type) {
250 ent->flags &= ~MON_PERMIT;
251 ent->flags |= permit ? MON_PERMIT : 0;
252 return;
253 }
254 ent++;
255 }
256}
257
258static void
259monitor_permit_authentications(int permit)
260{
261 struct mon_table *ent = mon_dispatch;
262
263 while (ent->f != NULL) {
264 if (ent->flags & MON_AUTH) {
265 ent->flags &= ~MON_PERMIT;
266 ent->flags |= permit ? MON_PERMIT : 0;
267 }
268 ent++;
269 }
270}
271
272Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000273monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000274{
275 struct mon_table *ent;
276 int authenticated = 0;
277
278 debug3("preauth child monitor started");
279
280 if (compat20) {
281 mon_dispatch = mon_dispatch_proto20;
282
283 /* Permit requests for moduli and signatures */
284 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
285 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
286 } else {
287 mon_dispatch = mon_dispatch_proto15;
288
289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
290 }
291
292 authctxt = authctxt_new();
293
294 /* The first few requests do not require asynchronous access */
295 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000296 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297 if (authenticated) {
298 if (!(ent->flags & MON_AUTHDECIDE))
299 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000300 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000301 if (authctxt->pw->pw_uid == 0 &&
302 !auth_root_allowed(auth_method))
303 authenticated = 0;
304 }
305
306 if (ent->flags & MON_AUTHDECIDE) {
307 auth_log(authctxt, authenticated, auth_method,
308 compat20 ? " ssh2" : "");
309 if (!authenticated)
310 authctxt->failures++;
311 }
312 }
313
314 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000315 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000316
317 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000318 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000319
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000320 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321
322 return (authctxt);
323}
324
Damien Millerbe64d432003-05-14 19:31:12 +1000325static void
326monitor_set_child_handler(pid_t pid)
327{
328 monitor_child_pid = pid;
329}
330
331static void
332monitor_child_handler(int signal)
333{
334 kill(monitor_child_pid, signal);
335}
336
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000337void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000338monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339{
Damien Millerbe64d432003-05-14 19:31:12 +1000340 monitor_set_child_handler(pmonitor->m_pid);
341 signal(SIGHUP, &monitor_child_handler);
342 signal(SIGTERM, &monitor_child_handler);
343
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 if (compat20) {
345 mon_dispatch = mon_dispatch_postauth20;
346
347 /* Permit requests for moduli and signatures */
348 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
349 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
350 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
351
352 } else {
353 mon_dispatch = mon_dispatch_postauth15;
354 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
355 }
356 if (!no_pty_flag) {
357 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
358 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
359 }
360
361 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000362 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000363}
364
365void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000366monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000367{
Damien Miller2d6b8332002-06-21 15:59:49 +1000368 if (options.compression) {
369 /* The member allocation is not visible, so sync it */
370 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
371 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372}
373
374int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000375monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376 struct mon_table **pent)
377{
378 Buffer m;
379 int ret;
380 u_char type;
381
382 buffer_init(&m);
383
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000384 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385 type = buffer_get_char(&m);
386
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
389 while (ent->f != NULL) {
390 if (ent->type == type)
391 break;
392 ent++;
393 }
394
395 if (ent->f != NULL) {
396 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000397 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000398 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000399 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000400 buffer_free(&m);
401
402 /* The child may use this request only once, disable it */
403 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000404 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000405 type);
406 ent->flags &= ~MON_PERMIT;
407 }
408
409 if (pent != NULL)
410 *pent = ent;
411
412 return ret;
413 }
414
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000415 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000416
417 /* NOTREACHED */
418 return (-1);
419}
420
421/* allowed key state */
422static int
423monitor_allowed_key(u_char *blob, u_int bloblen)
424{
425 /* make sure key is allowed */
426 if (key_blob == NULL || key_bloblen != bloblen ||
427 memcmp(key_blob, blob, key_bloblen))
428 return (0);
429 return (1);
430}
431
432static void
433monitor_reset_key_state(void)
434{
435 /* reset state */
436 if (key_blob != NULL)
437 xfree(key_blob);
438 if (hostbased_cuser != NULL)
439 xfree(hostbased_cuser);
440 if (hostbased_chost != NULL)
441 xfree(hostbased_chost);
442 key_blob = NULL;
443 key_bloblen = 0;
444 key_blobtype = MM_NOKEY;
445 hostbased_cuser = NULL;
446 hostbased_chost = NULL;
447}
448
449int
450mm_answer_moduli(int socket, Buffer *m)
451{
452 DH *dh;
453 int min, want, max;
454
455 min = buffer_get_int(m);
456 want = buffer_get_int(m);
457 max = buffer_get_int(m);
458
459 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000460 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000461 /* We need to check here, too, in case the child got corrupted */
462 if (max < min || want < min || max < want)
463 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000464 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000465
466 buffer_clear(m);
467
468 dh = choose_dh(min, want, max);
469 if (dh == NULL) {
470 buffer_put_char(m, 0);
471 return (0);
472 } else {
473 /* Send first bignum */
474 buffer_put_char(m, 1);
475 buffer_put_bignum2(m, dh->p);
476 buffer_put_bignum2(m, dh->g);
477
478 DH_free(dh);
479 }
480 mm_request_send(socket, MONITOR_ANS_MODULI, m);
481 return (0);
482}
483
484int
485mm_answer_sign(int socket, Buffer *m)
486{
487 Key *key;
488 u_char *p;
489 u_char *signature;
490 u_int siglen, datlen;
491 int keyid;
492
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000493 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000494
495 keyid = buffer_get_int(m);
496 p = buffer_get_string(m, &datlen);
497
498 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000499 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000500
Ben Lindstromf67e0772002-06-06 20:58:19 +0000501 /* save session id, it will be passed on the first call */
502 if (session_id2_len == 0) {
503 session_id2_len = datlen;
504 session_id2 = xmalloc(session_id2_len);
505 memcpy(session_id2, p, session_id2_len);
506 }
507
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000508 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000509 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000510 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000511 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000512
Ben Lindstromd5502182002-06-27 00:12:57 +0000513 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514
515 buffer_clear(m);
516 buffer_put_string(m, signature, siglen);
517
518 xfree(p);
519 xfree(signature);
520
521 mm_request_send(socket, MONITOR_ANS_SIGN, m);
522
523 /* Turn on permissions for getpwnam */
524 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
525
526 return (0);
527}
528
529/* Retrieves the password entry and also checks if the user is permitted */
530
531int
532mm_answer_pwnamallow(int socket, Buffer *m)
533{
534 char *login;
535 struct passwd *pwent;
536 int allowed = 0;
537
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000538 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000539
540 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000541 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542
543 login = buffer_get_string(m, NULL);
544
545 pwent = getpwnamallow(login);
546
547 authctxt->user = xstrdup(login);
548 setproctitle("%s [priv]", pwent ? login : "unknown");
549 xfree(login);
550
551 buffer_clear(m);
552
553 if (pwent == NULL) {
554 buffer_put_char(m, 0);
555 goto out;
556 }
557
558 allowed = 1;
559 authctxt->pw = pwent;
560 authctxt->valid = 1;
561
562 buffer_put_char(m, 1);
563 buffer_put_string(m, pwent, sizeof(struct passwd));
564 buffer_put_cstring(m, pwent->pw_name);
565 buffer_put_cstring(m, "*");
566 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000567#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000569#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000570 buffer_put_cstring(m, pwent->pw_dir);
571 buffer_put_cstring(m, pwent->pw_shell);
572
573 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000574 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000575 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
576
577 /* For SSHv1 allow authentication now */
578 if (!compat20)
579 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000580 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000581 /* Allow service/style information on the auth context */
582 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000583 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
584 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000585
Damien Millera33501b2002-05-08 12:24:42 +1000586#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000587 if (options.use_pam)
588 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000589#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590
591 return (0);
592}
593
Damien Miller5ad9fd92002-05-13 11:07:41 +1000594int mm_answer_auth2_read_banner(int socket, Buffer *m)
595{
596 char *banner;
597
598 buffer_clear(m);
599 banner = auth2_read_banner();
600 buffer_put_cstring(m, banner != NULL ? banner : "");
601 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
602
603 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000604 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000605
606 return (0);
607}
608
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000609int
610mm_answer_authserv(int socket, Buffer *m)
611{
612 monitor_permit_authentications(1);
613
614 authctxt->service = buffer_get_string(m, NULL);
615 authctxt->style = buffer_get_string(m, NULL);
616 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000617 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618
619 if (strlen(authctxt->style) == 0) {
620 xfree(authctxt->style);
621 authctxt->style = NULL;
622 }
623
624 return (0);
625}
626
627int
628mm_answer_authpassword(int socket, Buffer *m)
629{
630 static int call_count;
631 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000632 int authenticated;
633 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634
635 passwd = buffer_get_string(m, &plen);
636 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000637 authenticated = options.password_authentication &&
Damien Millereab4bae2003-04-29 23:22:40 +1000638 auth_password(authctxt, passwd) && authctxt->valid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000639 memset(passwd, 0, strlen(passwd));
640 xfree(passwd);
641
642 buffer_clear(m);
643 buffer_put_int(m, authenticated);
644
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000645 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
647
648 call_count++;
649 if (plen == 0 && call_count == 1)
650 auth_method = "none";
651 else
652 auth_method = "password";
653
654 /* Causes monitor loop to terminate if authenticated */
655 return (authenticated);
656}
657
658#ifdef BSD_AUTH
659int
660mm_answer_bsdauthquery(int socket, Buffer *m)
661{
662 char *name, *infotxt;
663 u_int numprompts;
664 u_int *echo_on;
665 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100666 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000667
Damien Millerb7df3af2003-02-24 11:55:46 +1100668 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
669 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000670
671 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100672 buffer_put_int(m, success);
673 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674 buffer_put_cstring(m, prompts[0]);
675
Damien Millerb7df3af2003-02-24 11:55:46 +1100676 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000677 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
678
Damien Millerb7df3af2003-02-24 11:55:46 +1100679 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680 xfree(name);
681 xfree(infotxt);
682 xfree(prompts);
683 xfree(echo_on);
684 }
685
686 return (0);
687}
688
689int
690mm_answer_bsdauthrespond(int socket, Buffer *m)
691{
692 char *response;
693 int authok;
694
695 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000696 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000697
698 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000699 authok = options.challenge_response_authentication &&
700 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000701 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000702 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703 xfree(response);
704
705 buffer_clear(m);
706 buffer_put_int(m, authok);
707
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000708 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
710
711 auth_method = "bsdauth";
712
713 return (authok != 0);
714}
715#endif
716
717#ifdef SKEY
718int
719mm_answer_skeyquery(int socket, Buffer *m)
720{
721 struct skey skey;
722 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100723 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000724
Damien Millerb7df3af2003-02-24 11:55:46 +1100725 success = skeychallenge(&skey, authctxt->user, challenge) < 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, challenge);
731
Damien Millerb7df3af2003-02-24 11:55:46 +1100732 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
734
735 return (0);
736}
737
738int
739mm_answer_skeyrespond(int socket, Buffer *m)
740{
741 char *response;
742 int authok;
743
744 response = buffer_get_string(m, NULL);
745
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000746 authok = (options.challenge_response_authentication &&
747 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000748 skey_haskey(authctxt->pw->pw_name) == 0 &&
749 skey_passcheck(authctxt->pw->pw_name, response) != -1);
750
751 xfree(response);
752
753 buffer_clear(m);
754 buffer_put_int(m, authok);
755
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000756 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
758
759 auth_method = "skey";
760
761 return (authok != 0);
762}
763#endif
764
Damien Miller79418552002-04-23 20:28:48 +1000765#ifdef USE_PAM
766int
767mm_answer_pam_start(int socket, Buffer *m)
768{
769 char *user;
770
Damien Miller4e448a32003-05-14 15:11:48 +1000771 if (!options.use_pam)
772 fatal("UsePAM not set, but ended up in %s anyway", __func__);
773
Damien Miller79418552002-04-23 20:28:48 +1000774 user = buffer_get_string(m, NULL);
775
776 start_pam(user);
777
778 xfree(user);
779
780 return (0);
781}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000782
783static void *sshpam_ctxt, *sshpam_authok;
784extern KbdintDevice sshpam_device;
785
786int
787mm_answer_pam_init_ctx(int socket, Buffer *m)
788{
789
790 debug3("%s", __func__);
791 authctxt->user = buffer_get_string(m, NULL);
792 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
793 sshpam_authok = NULL;
794 buffer_clear(m);
795 if (sshpam_ctxt != NULL) {
796 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
797 buffer_put_int(m, 1);
798 } else {
799 buffer_put_int(m, 0);
800 }
801 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
802 return (0);
803}
804
805int
806mm_answer_pam_query(int socket, Buffer *m)
807{
808 char *name, *info, **prompts;
809 u_int num, *echo_on;
810 int i, ret;
811
812 debug3("%s", __func__);
813 sshpam_authok = NULL;
814 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
815 if (ret == 0 && num == 0)
816 sshpam_authok = sshpam_ctxt;
817 if (num > 1 || name == NULL || info == NULL)
818 ret = -1;
819 buffer_clear(m);
820 buffer_put_int(m, ret);
821 buffer_put_cstring(m, name);
822 xfree(name);
823 buffer_put_cstring(m, info);
824 xfree(info);
825 buffer_put_int(m, num);
826 for (i = 0; i < num; ++i) {
827 buffer_put_cstring(m, prompts[i]);
828 xfree(prompts[i]);
829 buffer_put_int(m, echo_on[i]);
830 }
831 if (prompts != NULL)
832 xfree(prompts);
833 if (echo_on != NULL)
834 xfree(echo_on);
835 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
836 return (0);
837}
838
839int
840mm_answer_pam_respond(int socket, Buffer *m)
841{
842 char **resp;
843 u_int num;
844 int i, ret;
845
846 debug3("%s", __func__);
847 sshpam_authok = NULL;
848 num = buffer_get_int(m);
849 if (num > 0) {
850 resp = xmalloc(num * sizeof(char *));
851 for (i = 0; i < num; ++i)
852 resp[i] = buffer_get_string(m, NULL);
853 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
854 for (i = 0; i < num; ++i)
855 xfree(resp[i]);
856 xfree(resp);
857 } else {
858 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
859 }
860 buffer_clear(m);
861 buffer_put_int(m, ret);
862 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
863 auth_method = "keyboard-interactive/pam";
864 if (ret == 0)
865 sshpam_authok = sshpam_ctxt;
866 return (0);
867}
868
869int
870mm_answer_pam_free_ctx(int socket, Buffer *m)
871{
872
873 debug3("%s", __func__);
874 (sshpam_device.free_ctx)(sshpam_ctxt);
875 buffer_clear(m);
876 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
877 return (sshpam_authok == sshpam_ctxt);
878}
Damien Miller79418552002-04-23 20:28:48 +1000879#endif
880
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881static void
882mm_append_debug(Buffer *m)
883{
884 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000885 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886 buffer_append(m, buffer_ptr(&auth_debug),
887 buffer_len(&auth_debug));
888 buffer_clear(&auth_debug);
889 }
890}
891
892int
893mm_answer_keyallowed(int socket, Buffer *m)
894{
895 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000896 char *cuser, *chost;
897 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898 u_int bloblen;
899 enum mm_keytype type = 0;
900 int allowed = 0;
901
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000902 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000903
904 type = buffer_get_int(m);
905 cuser = buffer_get_string(m, NULL);
906 chost = buffer_get_string(m, NULL);
907 blob = buffer_get_string(m, &bloblen);
908
909 key = key_from_blob(blob, bloblen);
910
911 if ((compat20 && type == MM_RSAHOSTKEY) ||
912 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000913 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000915 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000916
917 if (key != NULL && authctxt->pw != NULL) {
918 switch(type) {
919 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000920 allowed = options.pubkey_authentication &&
921 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000922 break;
923 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000924 allowed = options.hostbased_authentication &&
925 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926 cuser, chost, key);
927 break;
928 case MM_RSAHOSTKEY:
929 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000930 allowed = options.rhosts_rsa_authentication &&
931 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932 cuser, chost, key);
933 break;
934 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000935 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936 break;
937 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000938 }
Damien Miller00111382003-03-10 11:21:17 +1100939 if (key != NULL)
940 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000941
942 /* clear temporarily storage (used by verify) */
943 monitor_reset_key_state();
944
945 if (allowed) {
946 /* Save temporarily for comparison in verify */
947 key_blob = blob;
948 key_bloblen = bloblen;
949 key_blobtype = type;
950 hostbased_cuser = cuser;
951 hostbased_chost = chost;
952 }
953
954 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000955 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000956
957 buffer_clear(m);
958 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +1100959 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960
961 mm_append_debug(m);
962
963 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
964
965 if (type == MM_RSAHOSTKEY)
966 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
967
968 return (0);
969}
970
971static int
972monitor_valid_userblob(u_char *data, u_int datalen)
973{
974 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +1000975 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976 u_int len;
977 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978
979 buffer_init(&b);
980 buffer_append(&b, data, datalen);
981
982 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000983 p = buffer_ptr(&b);
984 len = buffer_len(&b);
985 if ((session_id2 == NULL) ||
986 (len < session_id2_len) ||
987 (memcmp(p, session_id2, session_id2_len) != 0))
988 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000989 buffer_consume(&b, session_id2_len);
990 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000991 p = buffer_get_string(&b, &len);
992 if ((session_id2 == NULL) ||
993 (len != session_id2_len) ||
994 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000995 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000996 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000997 }
998 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
999 fail++;
1000 p = buffer_get_string(&b, NULL);
1001 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001002 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001003 authctxt->user, p);
1004 fail++;
1005 }
1006 xfree(p);
1007 buffer_skip_string(&b);
1008 if (datafellows & SSH_BUG_PKAUTH) {
1009 if (!buffer_get_char(&b))
1010 fail++;
1011 } else {
1012 p = buffer_get_string(&b, NULL);
1013 if (strcmp("publickey", p) != 0)
1014 fail++;
1015 xfree(p);
1016 if (!buffer_get_char(&b))
1017 fail++;
1018 buffer_skip_string(&b);
1019 }
1020 buffer_skip_string(&b);
1021 if (buffer_len(&b) != 0)
1022 fail++;
1023 buffer_free(&b);
1024 return (fail == 0);
1025}
1026
1027static int
Damien Millera10f5612002-09-12 09:49:15 +10001028monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1029 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001030{
1031 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001032 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001033 u_int len;
1034 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001035
1036 buffer_init(&b);
1037 buffer_append(&b, data, datalen);
1038
Ben Lindstromf67e0772002-06-06 20:58:19 +00001039 p = buffer_get_string(&b, &len);
1040 if ((session_id2 == NULL) ||
1041 (len != session_id2_len) ||
1042 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001043 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001044 xfree(p);
1045
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1047 fail++;
1048 p = buffer_get_string(&b, NULL);
1049 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001050 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001051 authctxt->user, p);
1052 fail++;
1053 }
1054 xfree(p);
1055 buffer_skip_string(&b); /* service */
1056 p = buffer_get_string(&b, NULL);
1057 if (strcmp(p, "hostbased") != 0)
1058 fail++;
1059 xfree(p);
1060 buffer_skip_string(&b); /* pkalg */
1061 buffer_skip_string(&b); /* pkblob */
1062
1063 /* verify client host, strip trailing dot if necessary */
1064 p = buffer_get_string(&b, NULL);
1065 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1066 p[len - 1] = '\0';
1067 if (strcmp(p, chost) != 0)
1068 fail++;
1069 xfree(p);
1070
1071 /* verify client user */
1072 p = buffer_get_string(&b, NULL);
1073 if (strcmp(p, cuser) != 0)
1074 fail++;
1075 xfree(p);
1076
1077 if (buffer_len(&b) != 0)
1078 fail++;
1079 buffer_free(&b);
1080 return (fail == 0);
1081}
1082
1083int
1084mm_answer_keyverify(int socket, Buffer *m)
1085{
1086 Key *key;
1087 u_char *signature, *data, *blob;
1088 u_int signaturelen, datalen, bloblen;
1089 int verified = 0;
1090 int valid_data = 0;
1091
1092 blob = buffer_get_string(m, &bloblen);
1093 signature = buffer_get_string(m, &signaturelen);
1094 data = buffer_get_string(m, &datalen);
1095
1096 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001097 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001098 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099
1100 key = key_from_blob(blob, bloblen);
1101 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001102 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001103
1104 switch (key_blobtype) {
1105 case MM_USERKEY:
1106 valid_data = monitor_valid_userblob(data, datalen);
1107 break;
1108 case MM_HOSTKEY:
1109 valid_data = monitor_valid_hostbasedblob(data, datalen,
1110 hostbased_cuser, hostbased_chost);
1111 break;
1112 default:
1113 valid_data = 0;
1114 break;
1115 }
1116 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001117 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001118
1119 verified = key_verify(key, signature, signaturelen, data, datalen);
1120 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001121 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001122
1123 key_free(key);
1124 xfree(blob);
1125 xfree(signature);
1126 xfree(data);
1127
Ben Lindstrome1c09122002-06-23 00:38:24 +00001128 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1129
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001130 monitor_reset_key_state();
1131
1132 buffer_clear(m);
1133 buffer_put_int(m, verified);
1134 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1135
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001136 return (verified);
1137}
1138
1139static void
1140mm_record_login(Session *s, struct passwd *pw)
1141{
1142 socklen_t fromlen;
1143 struct sockaddr_storage from;
1144
1145 /*
1146 * Get IP address of client. If the connection is not a socket, let
1147 * the address be 0.0.0.0.
1148 */
1149 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001150 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001152 if (getpeername(packet_get_connection_in(),
1153 (struct sockaddr *) & from, &fromlen) < 0) {
1154 debug("getpeername: %.100s", strerror(errno));
1155 fatal_cleanup();
1156 }
1157 }
1158 /* Record that there was a login on that tty from the remote host. */
1159 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001160 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001161 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162}
1163
1164static void
1165mm_session_close(Session *s)
1166{
Damien Miller04bd8b02003-05-25 14:38:33 +10001167 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001169 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1171 session_pty_cleanup2(s);
1172 }
1173 s->used = 0;
1174}
1175
1176int
1177mm_answer_pty(int socket, Buffer *m)
1178{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001179 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 Session *s;
1181 int res, fd0;
1182
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001183 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184
1185 buffer_clear(m);
1186 s = session_new();
1187 if (s == NULL)
1188 goto error;
1189 s->authctxt = authctxt;
1190 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001191 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001192 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1193 if (res == 0)
1194 goto error;
1195 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1196 pty_setowner(authctxt->pw, s->tty);
1197
1198 buffer_put_int(m, 1);
1199 buffer_put_cstring(m, s->tty);
1200 mm_request_send(socket, MONITOR_ANS_PTY, m);
1201
1202 mm_send_fd(socket, s->ptyfd);
1203 mm_send_fd(socket, s->ttyfd);
1204
1205 /* We need to trick ttyslot */
1206 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001207 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208
1209 mm_record_login(s, authctxt->pw);
1210
1211 /* Now we can close the file descriptor again */
1212 close(0);
1213
1214 /* make sure nothing uses fd 0 */
1215 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001216 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001217 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001218 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001219
1220 /* slave is not needed */
1221 close(s->ttyfd);
1222 s->ttyfd = s->ptyfd;
1223 /* no need to dup() because nobody closes ptyfd */
1224 s->ptymaster = s->ptyfd;
1225
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001226 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001227
1228 return (0);
1229
1230 error:
1231 if (s != NULL)
1232 mm_session_close(s);
1233 buffer_put_int(m, 0);
1234 mm_request_send(socket, MONITOR_ANS_PTY, m);
1235 return (0);
1236}
1237
1238int
1239mm_answer_pty_cleanup(int socket, Buffer *m)
1240{
1241 Session *s;
1242 char *tty;
1243
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001244 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245
1246 tty = buffer_get_string(m, NULL);
1247 if ((s = session_by_tty(tty)) != NULL)
1248 mm_session_close(s);
1249 buffer_clear(m);
1250 xfree(tty);
1251 return (0);
1252}
1253
1254int
1255mm_answer_sesskey(int socket, Buffer *m)
1256{
1257 BIGNUM *p;
1258 int rsafail;
1259
1260 /* Turn off permissions */
1261 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1262
1263 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001264 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001265
1266 buffer_get_bignum2(m, p);
1267
1268 rsafail = ssh1_session_key(p);
1269
1270 buffer_clear(m);
1271 buffer_put_int(m, rsafail);
1272 buffer_put_bignum2(m, p);
1273
1274 BN_clear_free(p);
1275
1276 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1277
1278 /* Turn on permissions for sessid passing */
1279 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1280
1281 return (0);
1282}
1283
1284int
1285mm_answer_sessid(int socket, Buffer *m)
1286{
1287 int i;
1288
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001289 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290
1291 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001292 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293 for (i = 0; i < 16; i++)
1294 session_id[i] = buffer_get_char(m);
1295
1296 /* Turn on permissions for getpwnam */
1297 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1298
1299 return (0);
1300}
1301
1302int
1303mm_answer_rsa_keyallowed(int socket, Buffer *m)
1304{
1305 BIGNUM *client_n;
1306 Key *key = NULL;
1307 u_char *blob = NULL;
1308 u_int blen = 0;
1309 int allowed = 0;
1310
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001311 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001312
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001313 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001315 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316 buffer_get_bignum2(m, client_n);
1317 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1318 BN_clear_free(client_n);
1319 }
1320 buffer_clear(m);
1321 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001322 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323
1324 /* clear temporarily storage (used by generate challenge) */
1325 monitor_reset_key_state();
1326
1327 if (allowed && key != NULL) {
1328 key->type = KEY_RSA; /* cheat for key_to_blob */
1329 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001330 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001331 buffer_put_string(m, blob, blen);
1332
1333 /* Save temporarily for comparison in verify */
1334 key_blob = blob;
1335 key_bloblen = blen;
1336 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 }
Damien Miller00111382003-03-10 11:21:17 +11001338 if (key != NULL)
1339 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340
1341 mm_append_debug(m);
1342
1343 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1344
1345 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1346 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1347 return (0);
1348}
1349
1350int
1351mm_answer_rsa_challenge(int socket, Buffer *m)
1352{
1353 Key *key = NULL;
1354 u_char *blob;
1355 u_int blen;
1356
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001357 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001358
1359 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001360 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001361 blob = buffer_get_string(m, &blen);
1362 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001363 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001364 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001365 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001367 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368
1369 if (ssh1_challenge)
1370 BN_clear_free(ssh1_challenge);
1371 ssh1_challenge = auth_rsa_generate_challenge(key);
1372
1373 buffer_clear(m);
1374 buffer_put_bignum2(m, ssh1_challenge);
1375
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001376 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1378
1379 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001380
1381 xfree(blob);
1382 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383 return (0);
1384}
1385
1386int
1387mm_answer_rsa_response(int socket, Buffer *m)
1388{
1389 Key *key = NULL;
1390 u_char *blob, *response;
1391 u_int blen, len;
1392 int success;
1393
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001394 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001395
1396 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001397 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001398 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001399 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400
1401 blob = buffer_get_string(m, &blen);
1402 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001403 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001404 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001405 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001406 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001407 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001408 response = buffer_get_string(m, &len);
1409 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001410 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1412
Damien Miller00111382003-03-10 11:21:17 +11001413 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001414 key_free(key);
1415 xfree(response);
1416
1417 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1418
1419 /* reset state */
1420 BN_clear_free(ssh1_challenge);
1421 ssh1_challenge = NULL;
1422 monitor_reset_key_state();
1423
1424 buffer_clear(m);
1425 buffer_put_int(m, success);
1426 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1427
1428 return (success);
1429}
1430
Damien Millerd94e5492002-09-27 13:25:58 +10001431#ifdef KRB4
1432int
1433mm_answer_krb4(int socket, Buffer *m)
1434{
1435 KTEXT_ST auth, reply;
1436 char *client, *p;
1437 int success;
1438 u_int alen;
1439
1440 reply.length = auth.length = 0;
1441
1442 p = buffer_get_string(m, &alen);
1443 if (alen >= MAX_KTXT_LEN)
1444 fatal("%s: auth too large", __func__);
1445 memcpy(auth.dat, p, alen);
1446 auth.length = alen;
1447 memset(p, 0, alen);
1448 xfree(p);
1449
1450 success = options.kerberos_authentication &&
1451 authctxt->valid &&
1452 auth_krb4(authctxt, &auth, &client, &reply);
1453
1454 memset(auth.dat, 0, alen);
1455 buffer_clear(m);
1456 buffer_put_int(m, success);
1457
1458 if (success) {
1459 buffer_put_cstring(m, client);
1460 buffer_put_string(m, reply.dat, reply.length);
1461 if (client)
1462 xfree(client);
1463 if (reply.length)
1464 memset(reply.dat, 0, reply.length);
1465 }
1466
1467 debug3("%s: sending result %d", __func__, success);
1468 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1469
1470 auth_method = "kerberos";
1471
1472 /* Causes monitor loop to terminate if authenticated */
1473 return (success);
1474}
1475#endif
Damien Miller25162f22002-09-12 09:47:29 +10001476
1477#ifdef KRB5
1478int
1479mm_answer_krb5(int socket, Buffer *m)
1480{
1481 krb5_data tkt, reply;
1482 char *client_user;
1483 u_int len;
1484 int success;
1485
1486 /* use temporary var to avoid size issues on 64bit arch */
1487 tkt.data = buffer_get_string(m, &len);
1488 tkt.length = len;
1489
Damien Milleref73f502002-09-25 12:20:17 +10001490 success = options.kerberos_authentication &&
1491 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001492 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001493
1494 if (tkt.length)
1495 xfree(tkt.data);
1496
1497 buffer_clear(m);
1498 buffer_put_int(m, success);
1499
1500 if (success) {
1501 buffer_put_cstring(m, client_user);
1502 buffer_put_string(m, reply.data, reply.length);
1503 if (client_user)
1504 xfree(client_user);
1505 if (reply.length)
1506 xfree(reply.data);
1507 }
1508 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1509
Damien Miller3ab496b2003-05-14 13:47:37 +10001510 auth_method = "kerberos";
1511
Damien Miller25162f22002-09-12 09:47:29 +10001512 return success;
1513}
1514#endif
1515
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001516int
1517mm_answer_term(int socket, Buffer *req)
1518{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001519 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001520 int res, status;
1521
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001522 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001523
1524 /* The child is terminating */
1525 session_destroy_all(&mm_session_close);
1526
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001527 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001528 if (errno != EINTR)
1529 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530
1531 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1532
1533 /* Terminate process */
1534 exit (res);
1535}
1536
1537void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001538monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001539{
1540 if (compat20) {
1541 set_newkeys(MODE_IN);
1542 set_newkeys(MODE_OUT);
1543 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001544 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001545 packet_set_encryption_key(child_state.ssh1key,
1546 child_state.ssh1keylen, child_state.ssh1cipher);
1547 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001548 }
1549
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001550 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001551 packet_set_keycontext(MODE_OUT, child_state.keyout);
1552 xfree(child_state.keyout);
1553 packet_set_keycontext(MODE_IN, child_state.keyin);
1554 xfree(child_state.keyin);
1555
1556 if (!compat20) {
1557 packet_set_iv(MODE_OUT, child_state.ivout);
1558 xfree(child_state.ivout);
1559 packet_set_iv(MODE_IN, child_state.ivin);
1560 xfree(child_state.ivin);
1561 }
1562
1563 memcpy(&incoming_stream, &child_state.incoming,
1564 sizeof(incoming_stream));
1565 memcpy(&outgoing_stream, &child_state.outgoing,
1566 sizeof(outgoing_stream));
1567
1568 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001569 if (options.compression)
1570 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001571
1572 /* Network I/O buffers */
1573 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1574 buffer_clear(&input);
1575 buffer_append(&input, child_state.input, child_state.ilen);
1576 memset(child_state.input, 0, child_state.ilen);
1577 xfree(child_state.input);
1578
1579 buffer_clear(&output);
1580 buffer_append(&output, child_state.output, child_state.olen);
1581 memset(child_state.output, 0, child_state.olen);
1582 xfree(child_state.output);
1583}
1584
1585static Kex *
1586mm_get_kex(Buffer *m)
1587{
1588 Kex *kex;
1589 void *blob;
1590 u_int bloblen;
1591
1592 kex = xmalloc(sizeof(*kex));
1593 memset(kex, 0, sizeof(*kex));
1594 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001595 if ((session_id2 == NULL) ||
1596 (kex->session_id_len != session_id2_len) ||
1597 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1598 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001599 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001600 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1601 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001602 kex->server = 1;
1603 kex->hostkey_type = buffer_get_int(m);
1604 kex->kex_type = buffer_get_int(m);
1605 blob = buffer_get_string(m, &bloblen);
1606 buffer_init(&kex->my);
1607 buffer_append(&kex->my, blob, bloblen);
1608 xfree(blob);
1609 blob = buffer_get_string(m, &bloblen);
1610 buffer_init(&kex->peer);
1611 buffer_append(&kex->peer, blob, bloblen);
1612 xfree(blob);
1613 kex->done = 1;
1614 kex->flags = buffer_get_int(m);
1615 kex->client_version_string = buffer_get_string(m, NULL);
1616 kex->server_version_string = buffer_get_string(m, NULL);
1617 kex->load_host_key=&get_hostkey_by_type;
1618 kex->host_key_index=&get_hostkey_index;
1619
1620 return (kex);
1621}
1622
1623/* This function requries careful sanity checking */
1624
1625void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001626mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627{
1628 Buffer m;
1629 u_char *blob, *p;
1630 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001631 u_int32_t seqnr, packets;
1632 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001634 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001635
1636 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001637 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001638 if (!compat20) {
1639 child_state.ssh1protoflags = buffer_get_int(&m);
1640 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001641 child_state.ssh1key = buffer_get_string(&m,
1642 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001643 child_state.ivout = buffer_get_string(&m,
1644 &child_state.ivoutlen);
1645 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1646 goto skip;
1647 } else {
1648 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001649 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001650 }
1651
1652 blob = buffer_get_string(&m, &bloblen);
1653 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1654 xfree(blob);
1655
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001656 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001657 blob = buffer_get_string(&m, &bloblen);
1658 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1659 xfree(blob);
1660
1661 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001662 seqnr = buffer_get_int(&m);
1663 blocks = buffer_get_int64(&m);
1664 packets = buffer_get_int(&m);
1665 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1666 seqnr = buffer_get_int(&m);
1667 blocks = buffer_get_int64(&m);
1668 packets = buffer_get_int(&m);
1669 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001670
1671 skip:
1672 /* Get the key context */
1673 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1674 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1675
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001676 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001677 /* Get compression state */
1678 p = buffer_get_string(&m, &plen);
1679 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001680 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001681 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1682 xfree(p);
1683
1684 p = buffer_get_string(&m, &plen);
1685 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001686 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1688 xfree(p);
1689
1690 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001691 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692 child_state.input = buffer_get_string(&m, &child_state.ilen);
1693 child_state.output = buffer_get_string(&m, &child_state.olen);
1694
1695 buffer_free(&m);
1696}
1697
1698
1699/* Allocation functions for zlib */
1700void *
1701mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1702{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001703 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001704 void *address;
1705
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001706 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001707 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1708
1709 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001710
1711 return (address);
1712}
1713
1714void
1715mm_zfree(struct mm_master *mm, void *address)
1716{
1717 mm_free(mm, address);
1718}
1719
1720void
1721mm_init_compression(struct mm_master *mm)
1722{
1723 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1724 outgoing_stream.zfree = (free_func)mm_zfree;
1725 outgoing_stream.opaque = mm;
1726
1727 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1728 incoming_stream.zfree = (free_func)mm_zfree;
1729 incoming_stream.opaque = mm;
1730}
1731
1732/* XXX */
1733
1734#define FD_CLOSEONEXEC(x) do { \
1735 if (fcntl(x, F_SETFD, 1) == -1) \
1736 fatal("fcntl(%d, F_SETFD)", x); \
1737} while (0)
1738
1739static void
1740monitor_socketpair(int *pair)
1741{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001742#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001743 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001744 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001745#else
1746 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001747 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001748#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001749 FD_CLOSEONEXEC(pair[0]);
1750 FD_CLOSEONEXEC(pair[1]);
1751}
1752
1753#define MM_MEMSIZE 65536
1754
1755struct monitor *
1756monitor_init(void)
1757{
1758 struct monitor *mon;
1759 int pair[2];
1760
1761 mon = xmalloc(sizeof(*mon));
1762
1763 monitor_socketpair(pair);
1764
1765 mon->m_recvfd = pair[0];
1766 mon->m_sendfd = pair[1];
1767
1768 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001769 if (options.compression) {
1770 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1771 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001772
Damien Miller2d6b8332002-06-21 15:59:49 +10001773 /* Compression needs to share state across borders */
1774 mm_init_compression(mon->m_zlib);
1775 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001776
1777 return mon;
1778}
1779
1780void
1781monitor_reinit(struct monitor *mon)
1782{
1783 int pair[2];
1784
1785 monitor_socketpair(pair);
1786
1787 mon->m_recvfd = pair[0];
1788 mon->m_sendfd = pair[1];
1789}