blob: 6fe0afd7ea99e59d92dbac6b88b843aa33f60fc8 [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"
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +000028RCSID("$OpenBSD: monitor.c,v 1.12 2002/06/04 19:42:35 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;
86 int ssh1cipher;
87 int ssh1protoflags;
88 u_char *input;
89 u_int ilen;
90 u_char *output;
91 u_int olen;
92} child_state;
93
94/* Functions on the montior that answer unprivileged requests */
95
96int mm_answer_moduli(int, Buffer *);
97int mm_answer_sign(int, Buffer *);
98int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +100099int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000100int mm_answer_authserv(int, Buffer *);
101int mm_answer_authpassword(int, Buffer *);
102int mm_answer_bsdauthquery(int, Buffer *);
103int mm_answer_bsdauthrespond(int, Buffer *);
104int mm_answer_skeyquery(int, Buffer *);
105int mm_answer_skeyrespond(int, Buffer *);
106int mm_answer_keyallowed(int, Buffer *);
107int mm_answer_keyverify(int, Buffer *);
108int mm_answer_pty(int, Buffer *);
109int mm_answer_pty_cleanup(int, Buffer *);
110int mm_answer_term(int, Buffer *);
111int mm_answer_rsa_keyallowed(int, Buffer *);
112int mm_answer_rsa_challenge(int, Buffer *);
113int mm_answer_rsa_response(int, Buffer *);
114int mm_answer_sesskey(int, Buffer *);
115int mm_answer_sessid(int, Buffer *);
116
Damien Miller79418552002-04-23 20:28:48 +1000117#ifdef USE_PAM
118int mm_answer_pam_start(int, Buffer *);
119#endif
120
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000121static Authctxt *authctxt;
122static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
123
124/* local state for key verify */
125static u_char *key_blob = NULL;
126static u_int key_bloblen = 0;
127static int key_blobtype = MM_NOKEY;
128static u_char *hostbased_cuser = NULL;
129static u_char *hostbased_chost = NULL;
130static char *auth_method = "unknown";
131
132struct mon_table {
133 enum monitor_reqtype type;
134 int flags;
135 int (*f)(int, Buffer *);
136};
137
138#define MON_ISAUTH 0x0004 /* Required for Authentication */
139#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
140#define MON_ONCE 0x0010 /* Disable after calling */
141
142#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
143
144#define MON_PERMIT 0x1000 /* Request is permitted */
145
146struct mon_table mon_dispatch_proto20[] = {
147 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
148 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
149 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
150 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000151 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000152 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000153#ifdef USE_PAM
154 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000155#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156#ifdef BSD_AUTH
157 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
158 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
159#endif
160#ifdef SKEY
161 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
162 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
163#endif
164 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
165 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
166 {0, 0, NULL}
167};
168
169struct mon_table mon_dispatch_postauth20[] = {
170 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
171 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
172 {MONITOR_REQ_PTY, 0, mm_answer_pty},
173 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
174 {MONITOR_REQ_TERM, 0, mm_answer_term},
175 {0, 0, NULL}
176};
177
178struct mon_table mon_dispatch_proto15[] = {
179 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
180 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
181 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
182 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
183 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
184 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
185 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
186 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
Damien Millera33501b2002-05-08 12:24:42 +1000187#ifdef USE_PAM
188 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
189#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000190#ifdef BSD_AUTH
191 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
192 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
193#endif
194#ifdef SKEY
195 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
196 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
197#endif
Damien Millera33501b2002-05-08 12:24:42 +1000198#ifdef USE_PAM
199 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
200#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000201 {0, 0, NULL}
202};
203
204struct mon_table mon_dispatch_postauth15[] = {
205 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
206 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
207 {MONITOR_REQ_TERM, 0, mm_answer_term},
208 {0, 0, NULL}
209};
210
211struct mon_table *mon_dispatch;
212
213/* Specifies if a certain message is allowed at the moment */
214
215static void
216monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
217{
218 while (ent->f != NULL) {
219 if (ent->type == type) {
220 ent->flags &= ~MON_PERMIT;
221 ent->flags |= permit ? MON_PERMIT : 0;
222 return;
223 }
224 ent++;
225 }
226}
227
228static void
229monitor_permit_authentications(int permit)
230{
231 struct mon_table *ent = mon_dispatch;
232
233 while (ent->f != NULL) {
234 if (ent->flags & MON_AUTH) {
235 ent->flags &= ~MON_PERMIT;
236 ent->flags |= permit ? MON_PERMIT : 0;
237 }
238 ent++;
239 }
240}
241
242Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000243monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000244{
245 struct mon_table *ent;
246 int authenticated = 0;
247
248 debug3("preauth child monitor started");
249
250 if (compat20) {
251 mon_dispatch = mon_dispatch_proto20;
252
253 /* Permit requests for moduli and signatures */
254 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
255 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
256 } else {
257 mon_dispatch = mon_dispatch_proto15;
258
259 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
260 }
261
262 authctxt = authctxt_new();
263
264 /* The first few requests do not require asynchronous access */
265 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000266 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000267 if (authenticated) {
268 if (!(ent->flags & MON_AUTHDECIDE))
269 fatal("%s: unexpected authentication from %d",
270 __FUNCTION__, ent->type);
271 if (authctxt->pw->pw_uid == 0 &&
272 !auth_root_allowed(auth_method))
273 authenticated = 0;
Damien Miller79418552002-04-23 20:28:48 +1000274#ifdef USE_PAM
275 if (!do_pam_account(authctxt->pw->pw_name, NULL))
276 authenticated = 0;
277#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000278 }
279
280 if (ent->flags & MON_AUTHDECIDE) {
281 auth_log(authctxt, authenticated, auth_method,
282 compat20 ? " ssh2" : "");
283 if (!authenticated)
284 authctxt->failures++;
285 }
286 }
287
288 if (!authctxt->valid)
289 fatal("%s: authenticated invalid user", __FUNCTION__);
290
291 debug("%s: %s has been authenticated by privileged process",
292 __FUNCTION__, authctxt->user);
293
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000294 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000295
296 return (authctxt);
297}
298
299void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000300monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000301{
302 if (compat20) {
303 mon_dispatch = mon_dispatch_postauth20;
304
305 /* Permit requests for moduli and signatures */
306 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
307 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
308 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
309
310 } else {
311 mon_dispatch = mon_dispatch_postauth15;
312 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
313 }
314 if (!no_pty_flag) {
315 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
316 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
317 }
318
319 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000320 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000321}
322
323void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000324monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325{
326 /* The member allocation is not visible, so sync it */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000327 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328}
329
330int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000331monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000332 struct mon_table **pent)
333{
334 Buffer m;
335 int ret;
336 u_char type;
337
338 buffer_init(&m);
339
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000340 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000341 type = buffer_get_char(&m);
342
343 debug3("%s: checking request %d", __FUNCTION__, type);
344
345 while (ent->f != NULL) {
346 if (ent->type == type)
347 break;
348 ent++;
349 }
350
351 if (ent->f != NULL) {
352 if (!(ent->flags & MON_PERMIT))
353 fatal("%s: unpermitted request %d", __FUNCTION__,
354 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000355 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 buffer_free(&m);
357
358 /* The child may use this request only once, disable it */
359 if (ent->flags & MON_ONCE) {
360 debug2("%s: %d used once, disabling now", __FUNCTION__,
361 type);
362 ent->flags &= ~MON_PERMIT;
363 }
364
365 if (pent != NULL)
366 *pent = ent;
367
368 return ret;
369 }
370
Ben Lindstrom2e9d8662002-03-26 02:49:34 +0000371 fatal("%s: unsupported request: %d", __FUNCTION__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372
373 /* NOTREACHED */
374 return (-1);
375}
376
377/* allowed key state */
378static int
379monitor_allowed_key(u_char *blob, u_int bloblen)
380{
381 /* make sure key is allowed */
382 if (key_blob == NULL || key_bloblen != bloblen ||
383 memcmp(key_blob, blob, key_bloblen))
384 return (0);
385 return (1);
386}
387
388static void
389monitor_reset_key_state(void)
390{
391 /* reset state */
392 if (key_blob != NULL)
393 xfree(key_blob);
394 if (hostbased_cuser != NULL)
395 xfree(hostbased_cuser);
396 if (hostbased_chost != NULL)
397 xfree(hostbased_chost);
398 key_blob = NULL;
399 key_bloblen = 0;
400 key_blobtype = MM_NOKEY;
401 hostbased_cuser = NULL;
402 hostbased_chost = NULL;
403}
404
405int
406mm_answer_moduli(int socket, Buffer *m)
407{
408 DH *dh;
409 int min, want, max;
410
411 min = buffer_get_int(m);
412 want = buffer_get_int(m);
413 max = buffer_get_int(m);
414
415 debug3("%s: got parameters: %d %d %d",
416 __FUNCTION__, min, want, max);
417 /* We need to check here, too, in case the child got corrupted */
418 if (max < min || want < min || max < want)
419 fatal("%s: bad parameters: %d %d %d",
420 __FUNCTION__, min, want, max);
421
422 buffer_clear(m);
423
424 dh = choose_dh(min, want, max);
425 if (dh == NULL) {
426 buffer_put_char(m, 0);
427 return (0);
428 } else {
429 /* Send first bignum */
430 buffer_put_char(m, 1);
431 buffer_put_bignum2(m, dh->p);
432 buffer_put_bignum2(m, dh->g);
433
434 DH_free(dh);
435 }
436 mm_request_send(socket, MONITOR_ANS_MODULI, m);
437 return (0);
438}
439
440int
441mm_answer_sign(int socket, Buffer *m)
442{
443 Key *key;
444 u_char *p;
445 u_char *signature;
446 u_int siglen, datlen;
447 int keyid;
448
449 debug3("%s", __FUNCTION__);
450
451 keyid = buffer_get_int(m);
452 p = buffer_get_string(m, &datlen);
453
454 if (datlen != 20)
455 fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
456
457 if ((key = get_hostkey_by_index(keyid)) == NULL)
458 fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
459 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
460 fatal("%s: key_sign failed", __FUNCTION__);
461
462 debug3("%s: signature %p(%d)", __FUNCTION__, signature, siglen);
463
464 buffer_clear(m);
465 buffer_put_string(m, signature, siglen);
466
467 xfree(p);
468 xfree(signature);
469
470 mm_request_send(socket, MONITOR_ANS_SIGN, m);
471
472 /* Turn on permissions for getpwnam */
473 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
474
475 return (0);
476}
477
478/* Retrieves the password entry and also checks if the user is permitted */
479
480int
481mm_answer_pwnamallow(int socket, Buffer *m)
482{
483 char *login;
484 struct passwd *pwent;
485 int allowed = 0;
486
487 debug3("%s", __FUNCTION__);
488
489 if (authctxt->attempt++ != 0)
490 fatal("%s: multiple attempts for getpwnam", __FUNCTION__);
491
492 login = buffer_get_string(m, NULL);
493
494 pwent = getpwnamallow(login);
495
496 authctxt->user = xstrdup(login);
497 setproctitle("%s [priv]", pwent ? login : "unknown");
498 xfree(login);
499
500 buffer_clear(m);
501
502 if (pwent == NULL) {
503 buffer_put_char(m, 0);
504 goto out;
505 }
506
507 allowed = 1;
508 authctxt->pw = pwent;
509 authctxt->valid = 1;
510
511 buffer_put_char(m, 1);
512 buffer_put_string(m, pwent, sizeof(struct passwd));
513 buffer_put_cstring(m, pwent->pw_name);
514 buffer_put_cstring(m, "*");
515 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000516#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000518#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519 buffer_put_cstring(m, pwent->pw_dir);
520 buffer_put_cstring(m, pwent->pw_shell);
521
522 out:
523 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __FUNCTION__, allowed);
524 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
525
526 /* For SSHv1 allow authentication now */
527 if (!compat20)
528 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000529 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000530 /* Allow service/style information on the auth context */
531 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000532 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
533 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534
Damien Millera33501b2002-05-08 12:24:42 +1000535#ifdef USE_PAM
536 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
537#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000538
539 return (0);
540}
541
Damien Miller5ad9fd92002-05-13 11:07:41 +1000542int mm_answer_auth2_read_banner(int socket, Buffer *m)
543{
544 char *banner;
545
546 buffer_clear(m);
547 banner = auth2_read_banner();
548 buffer_put_cstring(m, banner != NULL ? banner : "");
549 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
550
551 if (banner != NULL)
552 free(banner);
553
554 return (0);
555}
556
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557int
558mm_answer_authserv(int socket, Buffer *m)
559{
560 monitor_permit_authentications(1);
561
562 authctxt->service = buffer_get_string(m, NULL);
563 authctxt->style = buffer_get_string(m, NULL);
564 debug3("%s: service=%s, style=%s",
565 __FUNCTION__, authctxt->service, authctxt->style);
566
567 if (strlen(authctxt->style) == 0) {
568 xfree(authctxt->style);
569 authctxt->style = NULL;
570 }
571
572 return (0);
573}
574
575int
576mm_answer_authpassword(int socket, Buffer *m)
577{
578 static int call_count;
579 char *passwd;
580 int authenticated, plen;
581
582 passwd = buffer_get_string(m, &plen);
583 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000584 authenticated = options.password_authentication &&
585 authctxt->valid && auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000586 memset(passwd, 0, strlen(passwd));
587 xfree(passwd);
588
589 buffer_clear(m);
590 buffer_put_int(m, authenticated);
591
592 debug3("%s: sending result %d", __FUNCTION__, authenticated);
593 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
594
595 call_count++;
596 if (plen == 0 && call_count == 1)
597 auth_method = "none";
598 else
599 auth_method = "password";
600
601 /* Causes monitor loop to terminate if authenticated */
602 return (authenticated);
603}
604
605#ifdef BSD_AUTH
606int
607mm_answer_bsdauthquery(int socket, Buffer *m)
608{
609 char *name, *infotxt;
610 u_int numprompts;
611 u_int *echo_on;
612 char **prompts;
613 int res;
614
615 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
616 &prompts, &echo_on);
617
618 buffer_clear(m);
619 buffer_put_int(m, res);
620 if (res != -1)
621 buffer_put_cstring(m, prompts[0]);
622
623 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
624 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
625
626 if (res != -1) {
627 xfree(name);
628 xfree(infotxt);
629 xfree(prompts);
630 xfree(echo_on);
631 }
632
633 return (0);
634}
635
636int
637mm_answer_bsdauthrespond(int socket, Buffer *m)
638{
639 char *response;
640 int authok;
641
642 if (authctxt->as == 0)
643 fatal("%s: no bsd auth session", __FUNCTION__);
644
645 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000646 authok = options.challenge_response_authentication &&
647 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 authctxt->as = NULL;
649 debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
650 xfree(response);
651
652 buffer_clear(m);
653 buffer_put_int(m, authok);
654
655 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
656 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
657
658 auth_method = "bsdauth";
659
660 return (authok != 0);
661}
662#endif
663
664#ifdef SKEY
665int
666mm_answer_skeyquery(int socket, Buffer *m)
667{
668 struct skey skey;
669 char challenge[1024];
670 int res;
671
672 res = skeychallenge(&skey, authctxt->user, challenge);
673
674 buffer_clear(m);
675 buffer_put_int(m, res);
676 if (res != -1)
677 buffer_put_cstring(m, challenge);
678
679 debug3("%s: sending challenge res: %d", __FUNCTION__, res);
680 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
681
682 return (0);
683}
684
685int
686mm_answer_skeyrespond(int socket, Buffer *m)
687{
688 char *response;
689 int authok;
690
691 response = buffer_get_string(m, NULL);
692
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000693 authok = (options.challenge_response_authentication &&
694 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000695 skey_haskey(authctxt->pw->pw_name) == 0 &&
696 skey_passcheck(authctxt->pw->pw_name, response) != -1);
697
698 xfree(response);
699
700 buffer_clear(m);
701 buffer_put_int(m, authok);
702
703 debug3("%s: sending authenticated: %d", __FUNCTION__, authok);
704 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
705
706 auth_method = "skey";
707
708 return (authok != 0);
709}
710#endif
711
Damien Miller79418552002-04-23 20:28:48 +1000712#ifdef USE_PAM
713int
714mm_answer_pam_start(int socket, Buffer *m)
715{
716 char *user;
717
718 user = buffer_get_string(m, NULL);
719
720 start_pam(user);
721
722 xfree(user);
723
724 return (0);
725}
726#endif
727
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728static void
729mm_append_debug(Buffer *m)
730{
731 if (auth_debug_init && buffer_len(&auth_debug)) {
732 debug3("%s: Appending debug messages for child", __FUNCTION__);
733 buffer_append(m, buffer_ptr(&auth_debug),
734 buffer_len(&auth_debug));
735 buffer_clear(&auth_debug);
736 }
737}
738
739int
740mm_answer_keyallowed(int socket, Buffer *m)
741{
742 Key *key;
743 u_char *cuser, *chost, *blob;
744 u_int bloblen;
745 enum mm_keytype type = 0;
746 int allowed = 0;
747
748 debug3("%s entering", __FUNCTION__);
749
750 type = buffer_get_int(m);
751 cuser = buffer_get_string(m, NULL);
752 chost = buffer_get_string(m, NULL);
753 blob = buffer_get_string(m, &bloblen);
754
755 key = key_from_blob(blob, bloblen);
756
757 if ((compat20 && type == MM_RSAHOSTKEY) ||
758 (!compat20 && type != MM_RSAHOSTKEY))
759 fatal("%s: key type and protocol mismatch", __FUNCTION__);
760
761 debug3("%s: key_from_blob: %p", __FUNCTION__, key);
762
763 if (key != NULL && authctxt->pw != NULL) {
764 switch(type) {
765 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000766 allowed = options.pubkey_authentication &&
767 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000768 break;
769 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000770 allowed = options.hostbased_authentication &&
771 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772 cuser, chost, key);
773 break;
774 case MM_RSAHOSTKEY:
775 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000776 allowed = options.rhosts_rsa_authentication &&
777 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778 cuser, chost, key);
779 break;
780 default:
781 fatal("%s: unknown key type %d", __FUNCTION__, type);
782 break;
783 }
784 key_free(key);
785 }
786
787 /* clear temporarily storage (used by verify) */
788 monitor_reset_key_state();
789
790 if (allowed) {
791 /* Save temporarily for comparison in verify */
792 key_blob = blob;
793 key_bloblen = bloblen;
794 key_blobtype = type;
795 hostbased_cuser = cuser;
796 hostbased_chost = chost;
797 }
798
799 debug3("%s: key %p is %s",
800 __FUNCTION__, key, allowed ? "allowed" : "disallowed");
801
802 buffer_clear(m);
803 buffer_put_int(m, allowed);
804
805 mm_append_debug(m);
806
807 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
808
809 if (type == MM_RSAHOSTKEY)
810 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
811
812 return (0);
813}
814
815static int
816monitor_valid_userblob(u_char *data, u_int datalen)
817{
818 Buffer b;
819 u_char *p;
820 u_int len;
821 int fail = 0;
822 int session_id2_len = 20 /*XXX should get from [net] */;
823
824 buffer_init(&b);
825 buffer_append(&b, data, datalen);
826
827 if (datafellows & SSH_OLD_SESSIONID) {
828 buffer_consume(&b, session_id2_len);
829 } else {
830 xfree(buffer_get_string(&b, &len));
831 if (len != session_id2_len)
832 fail++;
833 }
834 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
835 fail++;
836 p = buffer_get_string(&b, NULL);
837 if (strcmp(authctxt->user, p) != 0) {
838 log("wrong user name passed to monitor: expected %s != %.100s",
839 authctxt->user, p);
840 fail++;
841 }
842 xfree(p);
843 buffer_skip_string(&b);
844 if (datafellows & SSH_BUG_PKAUTH) {
845 if (!buffer_get_char(&b))
846 fail++;
847 } else {
848 p = buffer_get_string(&b, NULL);
849 if (strcmp("publickey", p) != 0)
850 fail++;
851 xfree(p);
852 if (!buffer_get_char(&b))
853 fail++;
854 buffer_skip_string(&b);
855 }
856 buffer_skip_string(&b);
857 if (buffer_len(&b) != 0)
858 fail++;
859 buffer_free(&b);
860 return (fail == 0);
861}
862
863static int
864monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
865 u_char *chost)
866{
867 Buffer b;
868 u_char *p;
869 u_int len;
870 int fail = 0;
871 int session_id2_len = 20 /*XXX should get from [net] */;
872
873 buffer_init(&b);
874 buffer_append(&b, data, datalen);
875
876 xfree(buffer_get_string(&b, &len));
877 if (len != session_id2_len)
878 fail++;
879 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
880 fail++;
881 p = buffer_get_string(&b, NULL);
882 if (strcmp(authctxt->user, p) != 0) {
883 log("wrong user name passed to monitor: expected %s != %.100s",
884 authctxt->user, p);
885 fail++;
886 }
887 xfree(p);
888 buffer_skip_string(&b); /* service */
889 p = buffer_get_string(&b, NULL);
890 if (strcmp(p, "hostbased") != 0)
891 fail++;
892 xfree(p);
893 buffer_skip_string(&b); /* pkalg */
894 buffer_skip_string(&b); /* pkblob */
895
896 /* verify client host, strip trailing dot if necessary */
897 p = buffer_get_string(&b, NULL);
898 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
899 p[len - 1] = '\0';
900 if (strcmp(p, chost) != 0)
901 fail++;
902 xfree(p);
903
904 /* verify client user */
905 p = buffer_get_string(&b, NULL);
906 if (strcmp(p, cuser) != 0)
907 fail++;
908 xfree(p);
909
910 if (buffer_len(&b) != 0)
911 fail++;
912 buffer_free(&b);
913 return (fail == 0);
914}
915
916int
917mm_answer_keyverify(int socket, Buffer *m)
918{
919 Key *key;
920 u_char *signature, *data, *blob;
921 u_int signaturelen, datalen, bloblen;
922 int verified = 0;
923 int valid_data = 0;
924
925 blob = buffer_get_string(m, &bloblen);
926 signature = buffer_get_string(m, &signaturelen);
927 data = buffer_get_string(m, &datalen);
928
929 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +0000930 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931 fatal("%s: bad key, not previously allowed", __FUNCTION__);
932
933 key = key_from_blob(blob, bloblen);
934 if (key == NULL)
935 fatal("%s: bad public key blob", __FUNCTION__);
936
937 switch (key_blobtype) {
938 case MM_USERKEY:
939 valid_data = monitor_valid_userblob(data, datalen);
940 break;
941 case MM_HOSTKEY:
942 valid_data = monitor_valid_hostbasedblob(data, datalen,
943 hostbased_cuser, hostbased_chost);
944 break;
945 default:
946 valid_data = 0;
947 break;
948 }
949 if (!valid_data)
950 fatal("%s: bad signature data blob", __FUNCTION__);
951
952 verified = key_verify(key, signature, signaturelen, data, datalen);
953 debug3("%s: key %p signature %s",
954 __FUNCTION__, key, verified ? "verified" : "unverified");
955
956 key_free(key);
957 xfree(blob);
958 xfree(signature);
959 xfree(data);
960
961 monitor_reset_key_state();
962
963 buffer_clear(m);
964 buffer_put_int(m, verified);
965 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
966
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000967 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
969 return (verified);
970}
971
972static void
973mm_record_login(Session *s, struct passwd *pw)
974{
975 socklen_t fromlen;
976 struct sockaddr_storage from;
977
978 /*
979 * Get IP address of client. If the connection is not a socket, let
980 * the address be 0.0.0.0.
981 */
982 memset(&from, 0, sizeof(from));
983 if (packet_connection_is_on_socket()) {
984 fromlen = sizeof(from);
985 if (getpeername(packet_get_connection_in(),
986 (struct sockaddr *) & from, &fromlen) < 0) {
987 debug("getpeername: %.100s", strerror(errno));
988 fatal_cleanup();
989 }
990 }
991 /* Record that there was a login on that tty from the remote host. */
992 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
993 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
994 (struct sockaddr *)&from);
995}
996
997static void
998mm_session_close(Session *s)
999{
1000 debug3("%s: session %d pid %d", __FUNCTION__, s->self, s->pid);
1001 if (s->ttyfd != -1) {
1002 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ptyfd);
1003 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1004 session_pty_cleanup2(s);
1005 }
1006 s->used = 0;
1007}
1008
1009int
1010mm_answer_pty(int socket, Buffer *m)
1011{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001012 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013 Session *s;
1014 int res, fd0;
1015
1016 debug3("%s entering", __FUNCTION__);
1017
1018 buffer_clear(m);
1019 s = session_new();
1020 if (s == NULL)
1021 goto error;
1022 s->authctxt = authctxt;
1023 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001024 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1026 if (res == 0)
1027 goto error;
1028 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1029 pty_setowner(authctxt->pw, s->tty);
1030
1031 buffer_put_int(m, 1);
1032 buffer_put_cstring(m, s->tty);
1033 mm_request_send(socket, MONITOR_ANS_PTY, m);
1034
1035 mm_send_fd(socket, s->ptyfd);
1036 mm_send_fd(socket, s->ttyfd);
1037
1038 /* We need to trick ttyslot */
1039 if (dup2(s->ttyfd, 0) == -1)
1040 fatal("%s: dup2", __FUNCTION__);
1041
1042 mm_record_login(s, authctxt->pw);
1043
1044 /* Now we can close the file descriptor again */
1045 close(0);
1046
1047 /* make sure nothing uses fd 0 */
1048 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1049 fatal("%s: open(/dev/null): %s", __FUNCTION__, strerror(errno));
1050 if (fd0 != 0)
1051 error("%s: fd0 %d != 0", __FUNCTION__, fd0);
1052
1053 /* slave is not needed */
1054 close(s->ttyfd);
1055 s->ttyfd = s->ptyfd;
1056 /* no need to dup() because nobody closes ptyfd */
1057 s->ptymaster = s->ptyfd;
1058
1059 debug3("%s: tty %s ptyfd %d", __FUNCTION__, s->tty, s->ttyfd);
1060
1061 return (0);
1062
1063 error:
1064 if (s != NULL)
1065 mm_session_close(s);
1066 buffer_put_int(m, 0);
1067 mm_request_send(socket, MONITOR_ANS_PTY, m);
1068 return (0);
1069}
1070
1071int
1072mm_answer_pty_cleanup(int socket, Buffer *m)
1073{
1074 Session *s;
1075 char *tty;
1076
1077 debug3("%s entering", __FUNCTION__);
1078
1079 tty = buffer_get_string(m, NULL);
1080 if ((s = session_by_tty(tty)) != NULL)
1081 mm_session_close(s);
1082 buffer_clear(m);
1083 xfree(tty);
1084 return (0);
1085}
1086
1087int
1088mm_answer_sesskey(int socket, Buffer *m)
1089{
1090 BIGNUM *p;
1091 int rsafail;
1092
1093 /* Turn off permissions */
1094 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1095
1096 if ((p = BN_new()) == NULL)
1097 fatal("%s: BN_new", __FUNCTION__);
1098
1099 buffer_get_bignum2(m, p);
1100
1101 rsafail = ssh1_session_key(p);
1102
1103 buffer_clear(m);
1104 buffer_put_int(m, rsafail);
1105 buffer_put_bignum2(m, p);
1106
1107 BN_clear_free(p);
1108
1109 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1110
1111 /* Turn on permissions for sessid passing */
1112 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1113
1114 return (0);
1115}
1116
1117int
1118mm_answer_sessid(int socket, Buffer *m)
1119{
1120 int i;
1121
1122 debug3("%s entering", __FUNCTION__);
1123
1124 if (buffer_len(m) != 16)
1125 fatal("%s: bad ssh1 session id", __FUNCTION__);
1126 for (i = 0; i < 16; i++)
1127 session_id[i] = buffer_get_char(m);
1128
1129 /* Turn on permissions for getpwnam */
1130 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1131
1132 return (0);
1133}
1134
1135int
1136mm_answer_rsa_keyallowed(int socket, Buffer *m)
1137{
1138 BIGNUM *client_n;
1139 Key *key = NULL;
1140 u_char *blob = NULL;
1141 u_int blen = 0;
1142 int allowed = 0;
1143
1144 debug3("%s entering", __FUNCTION__);
1145
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001146 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147 if ((client_n = BN_new()) == NULL)
1148 fatal("%s: BN_new", __FUNCTION__);
1149 buffer_get_bignum2(m, client_n);
1150 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1151 BN_clear_free(client_n);
1152 }
1153 buffer_clear(m);
1154 buffer_put_int(m, allowed);
1155
1156 /* clear temporarily storage (used by generate challenge) */
1157 monitor_reset_key_state();
1158
1159 if (allowed && key != NULL) {
1160 key->type = KEY_RSA; /* cheat for key_to_blob */
1161 if (key_to_blob(key, &blob, &blen) == 0)
1162 fatal("%s: key_to_blob failed", __FUNCTION__);
1163 buffer_put_string(m, blob, blen);
1164
1165 /* Save temporarily for comparison in verify */
1166 key_blob = blob;
1167 key_bloblen = blen;
1168 key_blobtype = MM_RSAUSERKEY;
1169 key_free(key);
1170 }
1171
1172 mm_append_debug(m);
1173
1174 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1175
1176 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1177 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1178 return (0);
1179}
1180
1181int
1182mm_answer_rsa_challenge(int socket, Buffer *m)
1183{
1184 Key *key = NULL;
1185 u_char *blob;
1186 u_int blen;
1187
1188 debug3("%s entering", __FUNCTION__);
1189
1190 if (!authctxt->valid)
1191 fatal("%s: authctxt not valid", __FUNCTION__);
1192 blob = buffer_get_string(m, &blen);
1193 if (!monitor_allowed_key(blob, blen))
1194 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1195 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1196 fatal("%s: key type mismatch", __FUNCTION__);
1197 if ((key = key_from_blob(blob, blen)) == NULL)
1198 fatal("%s: received bad key", __FUNCTION__);
1199
1200 if (ssh1_challenge)
1201 BN_clear_free(ssh1_challenge);
1202 ssh1_challenge = auth_rsa_generate_challenge(key);
1203
1204 buffer_clear(m);
1205 buffer_put_bignum2(m, ssh1_challenge);
1206
1207 debug3("%s sending reply", __FUNCTION__);
1208 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1209
1210 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1211 return (0);
1212}
1213
1214int
1215mm_answer_rsa_response(int socket, Buffer *m)
1216{
1217 Key *key = NULL;
1218 u_char *blob, *response;
1219 u_int blen, len;
1220 int success;
1221
1222 debug3("%s entering", __FUNCTION__);
1223
1224 if (!authctxt->valid)
1225 fatal("%s: authctxt not valid", __FUNCTION__);
1226 if (ssh1_challenge == NULL)
1227 fatal("%s: no ssh1_challenge", __FUNCTION__);
1228
1229 blob = buffer_get_string(m, &blen);
1230 if (!monitor_allowed_key(blob, blen))
1231 fatal("%s: bad key, not previously allowed", __FUNCTION__);
1232 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1233 fatal("%s: key type mismatch: %d", __FUNCTION__, key_blobtype);
1234 if ((key = key_from_blob(blob, blen)) == NULL)
1235 fatal("%s: received bad key", __FUNCTION__);
1236 response = buffer_get_string(m, &len);
1237 if (len != 16)
1238 fatal("%s: received bad response to challenge", __FUNCTION__);
1239 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1240
1241 key_free(key);
1242 xfree(response);
1243
1244 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1245
1246 /* reset state */
1247 BN_clear_free(ssh1_challenge);
1248 ssh1_challenge = NULL;
1249 monitor_reset_key_state();
1250
1251 buffer_clear(m);
1252 buffer_put_int(m, success);
1253 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1254
1255 return (success);
1256}
1257
1258int
1259mm_answer_term(int socket, Buffer *req)
1260{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001261 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 int res, status;
1263
1264 debug3("%s: tearing down sessions", __FUNCTION__);
1265
1266 /* The child is terminating */
1267 session_destroy_all(&mm_session_close);
1268
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001269 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001270 if (errno != EINTR)
1271 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272
1273 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1274
1275 /* Terminate process */
1276 exit (res);
1277}
1278
1279void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001280monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001281{
1282 if (compat20) {
1283 set_newkeys(MODE_IN);
1284 set_newkeys(MODE_OUT);
1285 } else {
1286 u_char key[SSH_SESSION_KEY_LENGTH];
1287
1288 memset(key, 'a', sizeof(key));
1289 packet_set_protocol_flags(child_state.ssh1protoflags);
1290 packet_set_encryption_key(key, SSH_SESSION_KEY_LENGTH,
1291 child_state.ssh1cipher);
1292 }
1293
1294 packet_set_keycontext(MODE_OUT, child_state.keyout);
1295 xfree(child_state.keyout);
1296 packet_set_keycontext(MODE_IN, child_state.keyin);
1297 xfree(child_state.keyin);
1298
1299 if (!compat20) {
1300 packet_set_iv(MODE_OUT, child_state.ivout);
1301 xfree(child_state.ivout);
1302 packet_set_iv(MODE_IN, child_state.ivin);
1303 xfree(child_state.ivin);
1304 }
1305
1306 memcpy(&incoming_stream, &child_state.incoming,
1307 sizeof(incoming_stream));
1308 memcpy(&outgoing_stream, &child_state.outgoing,
1309 sizeof(outgoing_stream));
1310
1311 /* Update with new address */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001312 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313
1314 /* Network I/O buffers */
1315 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1316 buffer_clear(&input);
1317 buffer_append(&input, child_state.input, child_state.ilen);
1318 memset(child_state.input, 0, child_state.ilen);
1319 xfree(child_state.input);
1320
1321 buffer_clear(&output);
1322 buffer_append(&output, child_state.output, child_state.olen);
1323 memset(child_state.output, 0, child_state.olen);
1324 xfree(child_state.output);
1325}
1326
1327static Kex *
1328mm_get_kex(Buffer *m)
1329{
1330 Kex *kex;
1331 void *blob;
1332 u_int bloblen;
1333
1334 kex = xmalloc(sizeof(*kex));
1335 memset(kex, 0, sizeof(*kex));
1336 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1337 kex->we_need = buffer_get_int(m);
1338 kex->server = 1;
1339 kex->hostkey_type = buffer_get_int(m);
1340 kex->kex_type = buffer_get_int(m);
1341 blob = buffer_get_string(m, &bloblen);
1342 buffer_init(&kex->my);
1343 buffer_append(&kex->my, blob, bloblen);
1344 xfree(blob);
1345 blob = buffer_get_string(m, &bloblen);
1346 buffer_init(&kex->peer);
1347 buffer_append(&kex->peer, blob, bloblen);
1348 xfree(blob);
1349 kex->done = 1;
1350 kex->flags = buffer_get_int(m);
1351 kex->client_version_string = buffer_get_string(m, NULL);
1352 kex->server_version_string = buffer_get_string(m, NULL);
1353 kex->load_host_key=&get_hostkey_by_type;
1354 kex->host_key_index=&get_hostkey_index;
1355
1356 return (kex);
1357}
1358
1359/* This function requries careful sanity checking */
1360
1361void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001362mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001363{
1364 Buffer m;
1365 u_char *blob, *p;
1366 u_int bloblen, plen;
1367
1368 debug3("%s: Waiting for new keys", __FUNCTION__);
1369
1370 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001371 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001372 if (!compat20) {
1373 child_state.ssh1protoflags = buffer_get_int(&m);
1374 child_state.ssh1cipher = buffer_get_int(&m);
1375 child_state.ivout = buffer_get_string(&m,
1376 &child_state.ivoutlen);
1377 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1378 goto skip;
1379 } else {
1380 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001381 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382 }
1383
1384 blob = buffer_get_string(&m, &bloblen);
1385 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1386 xfree(blob);
1387
1388 debug3("%s: Waiting for second key", __FUNCTION__);
1389 blob = buffer_get_string(&m, &bloblen);
1390 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1391 xfree(blob);
1392
1393 /* Now get sequence numbers for the packets */
1394 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1395 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1396
1397 skip:
1398 /* Get the key context */
1399 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1400 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1401
1402 debug3("%s: Getting compression state", __FUNCTION__);
1403 /* Get compression state */
1404 p = buffer_get_string(&m, &plen);
1405 if (plen != sizeof(child_state.outgoing))
1406 fatal("%s: bad request size", __FUNCTION__);
1407 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1408 xfree(p);
1409
1410 p = buffer_get_string(&m, &plen);
1411 if (plen != sizeof(child_state.incoming))
1412 fatal("%s: bad request size", __FUNCTION__);
1413 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1414 xfree(p);
1415
1416 /* Network I/O buffers */
1417 debug3("%s: Getting Network I/O buffers", __FUNCTION__);
1418 child_state.input = buffer_get_string(&m, &child_state.ilen);
1419 child_state.output = buffer_get_string(&m, &child_state.olen);
1420
1421 buffer_free(&m);
1422}
1423
1424
1425/* Allocation functions for zlib */
1426void *
1427mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1428{
1429 void *address;
1430
1431 address = mm_malloc(mm, size * ncount);
1432
1433 return (address);
1434}
1435
1436void
1437mm_zfree(struct mm_master *mm, void *address)
1438{
1439 mm_free(mm, address);
1440}
1441
1442void
1443mm_init_compression(struct mm_master *mm)
1444{
1445 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1446 outgoing_stream.zfree = (free_func)mm_zfree;
1447 outgoing_stream.opaque = mm;
1448
1449 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1450 incoming_stream.zfree = (free_func)mm_zfree;
1451 incoming_stream.opaque = mm;
1452}
1453
1454/* XXX */
1455
1456#define FD_CLOSEONEXEC(x) do { \
1457 if (fcntl(x, F_SETFD, 1) == -1) \
1458 fatal("fcntl(%d, F_SETFD)", x); \
1459} while (0)
1460
1461static void
1462monitor_socketpair(int *pair)
1463{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001464#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001465 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1466 fatal("%s: socketpair", __FUNCTION__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001467#else
1468 fatal("%s: UsePrivilegeSeparation=yes not supported",
1469 __FUNCTION__);
1470#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001471 FD_CLOSEONEXEC(pair[0]);
1472 FD_CLOSEONEXEC(pair[1]);
1473}
1474
1475#define MM_MEMSIZE 65536
1476
1477struct monitor *
1478monitor_init(void)
1479{
1480 struct monitor *mon;
1481 int pair[2];
1482
1483 mon = xmalloc(sizeof(*mon));
1484
1485 monitor_socketpair(pair);
1486
1487 mon->m_recvfd = pair[0];
1488 mon->m_sendfd = pair[1];
1489
1490 /* Used to share zlib space across processes */
1491 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1492 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1493
1494 /* Compression needs to share state across borders */
1495 mm_init_compression(mon->m_zlib);
1496
1497 return mon;
1498}
1499
1500void
1501monitor_reinit(struct monitor *mon)
1502{
1503 int pair[2];
1504
1505 monitor_socketpair(pair);
1506
1507 mon->m_recvfd = pair[0];
1508 mon->m_sendfd = pair[1];
1509}