blob: 4ad3f3d21534d4bb02c59df252290e20e1eb47fb [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 Millerd94e5492002-09-27 13:25:58 +100028RCSID("$OpenBSD: monitor.c,v 1.29 2002/09/26 11:38:43 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
96/* Functions on the montior that answer unprivileged requests */
97
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 *);
121#endif
122
Damien Millerd94e5492002-09-27 13:25:58 +1000123#ifdef KRB4
124int mm_answer_krb4(int, Buffer *);
125#endif
Damien Miller25162f22002-09-12 09:47:29 +1000126#ifdef KRB5
127int mm_answer_krb5(int, Buffer *);
128#endif
129
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000130static Authctxt *authctxt;
131static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
132
133/* local state for key verify */
134static u_char *key_blob = NULL;
135static u_int key_bloblen = 0;
136static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000137static char *hostbased_cuser = NULL;
138static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000139static char *auth_method = "unknown";
Ben Lindstromf67e0772002-06-06 20:58:19 +0000140static int session_id2_len = 0;
141static u_char *session_id2 = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000142
143struct mon_table {
144 enum monitor_reqtype type;
145 int flags;
146 int (*f)(int, Buffer *);
147};
148
149#define MON_ISAUTH 0x0004 /* Required for Authentication */
150#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
151#define MON_ONCE 0x0010 /* Disable after calling */
152
153#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
154
155#define MON_PERMIT 0x1000 /* Request is permitted */
156
157struct mon_table mon_dispatch_proto20[] = {
158 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
159 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
160 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
161 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000162 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000163 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000164#ifdef USE_PAM
165 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000166#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167#ifdef BSD_AUTH
168 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
169 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
170#endif
171#ifdef SKEY
172 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
173 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
174#endif
175 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
176 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
177 {0, 0, NULL}
178};
179
180struct mon_table mon_dispatch_postauth20[] = {
181 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
182 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
183 {MONITOR_REQ_PTY, 0, mm_answer_pty},
184 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
185 {MONITOR_REQ_TERM, 0, mm_answer_term},
186 {0, 0, NULL}
187};
188
189struct mon_table mon_dispatch_proto15[] = {
190 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
191 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
192 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
193 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
194 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
195 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
196 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
197 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
198#ifdef BSD_AUTH
199 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
200 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
201#endif
202#ifdef SKEY
203 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
204 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
205#endif
Damien Millera33501b2002-05-08 12:24:42 +1000206#ifdef USE_PAM
207 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
208#endif
Damien Millerd94e5492002-09-27 13:25:58 +1000209#ifdef KRB4
210 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
211#endif
Damien Miller25162f22002-09-12 09:47:29 +1000212#ifdef KRB5
213 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
214#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000215 {0, 0, NULL}
216};
217
218struct mon_table mon_dispatch_postauth15[] = {
219 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
220 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
221 {MONITOR_REQ_TERM, 0, mm_answer_term},
222 {0, 0, NULL}
223};
224
225struct mon_table *mon_dispatch;
226
227/* Specifies if a certain message is allowed at the moment */
228
229static void
230monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
231{
232 while (ent->f != NULL) {
233 if (ent->type == type) {
234 ent->flags &= ~MON_PERMIT;
235 ent->flags |= permit ? MON_PERMIT : 0;
236 return;
237 }
238 ent++;
239 }
240}
241
242static void
243monitor_permit_authentications(int permit)
244{
245 struct mon_table *ent = mon_dispatch;
246
247 while (ent->f != NULL) {
248 if (ent->flags & MON_AUTH) {
249 ent->flags &= ~MON_PERMIT;
250 ent->flags |= permit ? MON_PERMIT : 0;
251 }
252 ent++;
253 }
254}
255
256Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000257monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000258{
259 struct mon_table *ent;
260 int authenticated = 0;
261
262 debug3("preauth child monitor started");
263
264 if (compat20) {
265 mon_dispatch = mon_dispatch_proto20;
266
267 /* Permit requests for moduli and signatures */
268 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
269 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
270 } else {
271 mon_dispatch = mon_dispatch_proto15;
272
273 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
274 }
275
276 authctxt = authctxt_new();
277
278 /* The first few requests do not require asynchronous access */
279 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000280 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000281 if (authenticated) {
282 if (!(ent->flags & MON_AUTHDECIDE))
283 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000284 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000285 if (authctxt->pw->pw_uid == 0 &&
286 !auth_root_allowed(auth_method))
287 authenticated = 0;
Damien Miller79418552002-04-23 20:28:48 +1000288#ifdef USE_PAM
289 if (!do_pam_account(authctxt->pw->pw_name, NULL))
290 authenticated = 0;
291#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000292 }
293
294 if (ent->flags & MON_AUTHDECIDE) {
295 auth_log(authctxt, authenticated, auth_method,
296 compat20 ? " ssh2" : "");
297 if (!authenticated)
298 authctxt->failures++;
299 }
300 }
301
302 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000303 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000304
305 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000306 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000307
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000308 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000309
310 return (authctxt);
311}
312
313void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000314monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315{
316 if (compat20) {
317 mon_dispatch = mon_dispatch_postauth20;
318
319 /* Permit requests for moduli and signatures */
320 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
321 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
322 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
323
324 } else {
325 mon_dispatch = mon_dispatch_postauth15;
326 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
327 }
328 if (!no_pty_flag) {
329 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
330 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
331 }
332
333 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000334 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335}
336
337void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000338monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000339{
Damien Miller2d6b8332002-06-21 15:59:49 +1000340 if (options.compression) {
341 /* The member allocation is not visible, so sync it */
342 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
343 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344}
345
346int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000347monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348 struct mon_table **pent)
349{
350 Buffer m;
351 int ret;
352 u_char type;
353
354 buffer_init(&m);
355
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000356 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357 type = buffer_get_char(&m);
358
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000359 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000360
361 while (ent->f != NULL) {
362 if (ent->type == type)
363 break;
364 ent++;
365 }
366
367 if (ent->f != NULL) {
368 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000369 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000370 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372 buffer_free(&m);
373
374 /* The child may use this request only once, disable it */
375 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000376 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000377 type);
378 ent->flags &= ~MON_PERMIT;
379 }
380
381 if (pent != NULL)
382 *pent = ent;
383
384 return ret;
385 }
386
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388
389 /* NOTREACHED */
390 return (-1);
391}
392
393/* allowed key state */
394static int
395monitor_allowed_key(u_char *blob, u_int bloblen)
396{
397 /* make sure key is allowed */
398 if (key_blob == NULL || key_bloblen != bloblen ||
399 memcmp(key_blob, blob, key_bloblen))
400 return (0);
401 return (1);
402}
403
404static void
405monitor_reset_key_state(void)
406{
407 /* reset state */
408 if (key_blob != NULL)
409 xfree(key_blob);
410 if (hostbased_cuser != NULL)
411 xfree(hostbased_cuser);
412 if (hostbased_chost != NULL)
413 xfree(hostbased_chost);
414 key_blob = NULL;
415 key_bloblen = 0;
416 key_blobtype = MM_NOKEY;
417 hostbased_cuser = NULL;
418 hostbased_chost = NULL;
419}
420
421int
422mm_answer_moduli(int socket, Buffer *m)
423{
424 DH *dh;
425 int min, want, max;
426
427 min = buffer_get_int(m);
428 want = buffer_get_int(m);
429 max = buffer_get_int(m);
430
431 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000432 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000433 /* We need to check here, too, in case the child got corrupted */
434 if (max < min || want < min || max < want)
435 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000436 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000437
438 buffer_clear(m);
439
440 dh = choose_dh(min, want, max);
441 if (dh == NULL) {
442 buffer_put_char(m, 0);
443 return (0);
444 } else {
445 /* Send first bignum */
446 buffer_put_char(m, 1);
447 buffer_put_bignum2(m, dh->p);
448 buffer_put_bignum2(m, dh->g);
449
450 DH_free(dh);
451 }
452 mm_request_send(socket, MONITOR_ANS_MODULI, m);
453 return (0);
454}
455
456int
457mm_answer_sign(int socket, Buffer *m)
458{
459 Key *key;
460 u_char *p;
461 u_char *signature;
462 u_int siglen, datlen;
463 int keyid;
464
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000465 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000466
467 keyid = buffer_get_int(m);
468 p = buffer_get_string(m, &datlen);
469
470 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000471 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000472
Ben Lindstromf67e0772002-06-06 20:58:19 +0000473 /* save session id, it will be passed on the first call */
474 if (session_id2_len == 0) {
475 session_id2_len = datlen;
476 session_id2 = xmalloc(session_id2_len);
477 memcpy(session_id2, p, session_id2_len);
478 }
479
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000481 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000482 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000483 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000484
Ben Lindstromd5502182002-06-27 00:12:57 +0000485 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000486
487 buffer_clear(m);
488 buffer_put_string(m, signature, siglen);
489
490 xfree(p);
491 xfree(signature);
492
493 mm_request_send(socket, MONITOR_ANS_SIGN, m);
494
495 /* Turn on permissions for getpwnam */
496 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
497
498 return (0);
499}
500
501/* Retrieves the password entry and also checks if the user is permitted */
502
503int
504mm_answer_pwnamallow(int socket, Buffer *m)
505{
506 char *login;
507 struct passwd *pwent;
508 int allowed = 0;
509
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000510 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000511
512 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000513 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000514
515 login = buffer_get_string(m, NULL);
516
517 pwent = getpwnamallow(login);
518
519 authctxt->user = xstrdup(login);
520 setproctitle("%s [priv]", pwent ? login : "unknown");
521 xfree(login);
522
523 buffer_clear(m);
524
525 if (pwent == NULL) {
526 buffer_put_char(m, 0);
527 goto out;
528 }
529
530 allowed = 1;
531 authctxt->pw = pwent;
532 authctxt->valid = 1;
533
534 buffer_put_char(m, 1);
535 buffer_put_string(m, pwent, sizeof(struct passwd));
536 buffer_put_cstring(m, pwent->pw_name);
537 buffer_put_cstring(m, "*");
538 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000539#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000541#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000542 buffer_put_cstring(m, pwent->pw_dir);
543 buffer_put_cstring(m, pwent->pw_shell);
544
545 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000546 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000547 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
548
549 /* For SSHv1 allow authentication now */
550 if (!compat20)
551 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000552 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553 /* Allow service/style information on the auth context */
554 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000555 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
556 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000557
Damien Millera33501b2002-05-08 12:24:42 +1000558#ifdef USE_PAM
559 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
560#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561
562 return (0);
563}
564
Damien Miller5ad9fd92002-05-13 11:07:41 +1000565int mm_answer_auth2_read_banner(int socket, Buffer *m)
566{
567 char *banner;
568
569 buffer_clear(m);
570 banner = auth2_read_banner();
571 buffer_put_cstring(m, banner != NULL ? banner : "");
572 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
573
574 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000575 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000576
577 return (0);
578}
579
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000580int
581mm_answer_authserv(int socket, Buffer *m)
582{
583 monitor_permit_authentications(1);
584
585 authctxt->service = buffer_get_string(m, NULL);
586 authctxt->style = buffer_get_string(m, NULL);
587 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000588 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000589
590 if (strlen(authctxt->style) == 0) {
591 xfree(authctxt->style);
592 authctxt->style = NULL;
593 }
594
595 return (0);
596}
597
598int
599mm_answer_authpassword(int socket, Buffer *m)
600{
601 static int call_count;
602 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000603 int authenticated;
604 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000605
606 passwd = buffer_get_string(m, &plen);
607 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000608 authenticated = options.password_authentication &&
609 authctxt->valid && auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000610 memset(passwd, 0, strlen(passwd));
611 xfree(passwd);
612
613 buffer_clear(m);
614 buffer_put_int(m, authenticated);
615
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000616 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
618
619 call_count++;
620 if (plen == 0 && call_count == 1)
621 auth_method = "none";
622 else
623 auth_method = "password";
624
625 /* Causes monitor loop to terminate if authenticated */
626 return (authenticated);
627}
628
629#ifdef BSD_AUTH
630int
631mm_answer_bsdauthquery(int socket, Buffer *m)
632{
633 char *name, *infotxt;
634 u_int numprompts;
635 u_int *echo_on;
636 char **prompts;
637 int res;
638
639 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
640 &prompts, &echo_on);
641
642 buffer_clear(m);
643 buffer_put_int(m, res);
644 if (res != -1)
645 buffer_put_cstring(m, prompts[0]);
646
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000647 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
649
650 if (res != -1) {
651 xfree(name);
652 xfree(infotxt);
653 xfree(prompts);
654 xfree(echo_on);
655 }
656
657 return (0);
658}
659
660int
661mm_answer_bsdauthrespond(int socket, Buffer *m)
662{
663 char *response;
664 int authok;
665
666 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000667 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000668
669 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000670 authok = options.challenge_response_authentication &&
671 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000672 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000673 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000674 xfree(response);
675
676 buffer_clear(m);
677 buffer_put_int(m, authok);
678
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000679 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
681
682 auth_method = "bsdauth";
683
684 return (authok != 0);
685}
686#endif
687
688#ifdef SKEY
689int
690mm_answer_skeyquery(int socket, Buffer *m)
691{
692 struct skey skey;
693 char challenge[1024];
694 int res;
695
696 res = skeychallenge(&skey, authctxt->user, challenge);
697
698 buffer_clear(m);
699 buffer_put_int(m, res);
700 if (res != -1)
701 buffer_put_cstring(m, challenge);
702
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000703 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000704 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
705
706 return (0);
707}
708
709int
710mm_answer_skeyrespond(int socket, Buffer *m)
711{
712 char *response;
713 int authok;
714
715 response = buffer_get_string(m, NULL);
716
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000717 authok = (options.challenge_response_authentication &&
718 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000719 skey_haskey(authctxt->pw->pw_name) == 0 &&
720 skey_passcheck(authctxt->pw->pw_name, response) != -1);
721
722 xfree(response);
723
724 buffer_clear(m);
725 buffer_put_int(m, authok);
726
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000727 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000728 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
729
730 auth_method = "skey";
731
732 return (authok != 0);
733}
734#endif
735
Damien Miller79418552002-04-23 20:28:48 +1000736#ifdef USE_PAM
737int
738mm_answer_pam_start(int socket, Buffer *m)
739{
740 char *user;
741
742 user = buffer_get_string(m, NULL);
743
744 start_pam(user);
745
746 xfree(user);
747
748 return (0);
749}
750#endif
751
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000752static void
753mm_append_debug(Buffer *m)
754{
755 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000756 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 buffer_append(m, buffer_ptr(&auth_debug),
758 buffer_len(&auth_debug));
759 buffer_clear(&auth_debug);
760 }
761}
762
763int
764mm_answer_keyallowed(int socket, Buffer *m)
765{
766 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000767 char *cuser, *chost;
768 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000769 u_int bloblen;
770 enum mm_keytype type = 0;
771 int allowed = 0;
772
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000773 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000774
775 type = buffer_get_int(m);
776 cuser = buffer_get_string(m, NULL);
777 chost = buffer_get_string(m, NULL);
778 blob = buffer_get_string(m, &bloblen);
779
780 key = key_from_blob(blob, bloblen);
781
782 if ((compat20 && type == MM_RSAHOSTKEY) ||
783 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000784 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000785
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000786 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000787
788 if (key != NULL && authctxt->pw != NULL) {
789 switch(type) {
790 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000791 allowed = options.pubkey_authentication &&
792 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000793 break;
794 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000795 allowed = options.hostbased_authentication &&
796 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000797 cuser, chost, key);
798 break;
799 case MM_RSAHOSTKEY:
800 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000801 allowed = options.rhosts_rsa_authentication &&
802 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000803 cuser, chost, key);
804 break;
805 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000806 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000807 break;
808 }
809 key_free(key);
810 }
811
812 /* clear temporarily storage (used by verify) */
813 monitor_reset_key_state();
814
815 if (allowed) {
816 /* Save temporarily for comparison in verify */
817 key_blob = blob;
818 key_bloblen = bloblen;
819 key_blobtype = type;
820 hostbased_cuser = cuser;
821 hostbased_chost = chost;
822 }
823
824 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000825 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000826
827 buffer_clear(m);
828 buffer_put_int(m, allowed);
829
830 mm_append_debug(m);
831
832 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
833
834 if (type == MM_RSAHOSTKEY)
835 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
836
837 return (0);
838}
839
840static int
841monitor_valid_userblob(u_char *data, u_int datalen)
842{
843 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +1000844 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000845 u_int len;
846 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000847
848 buffer_init(&b);
849 buffer_append(&b, data, datalen);
850
851 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000852 p = buffer_ptr(&b);
853 len = buffer_len(&b);
854 if ((session_id2 == NULL) ||
855 (len < session_id2_len) ||
856 (memcmp(p, session_id2, session_id2_len) != 0))
857 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000858 buffer_consume(&b, session_id2_len);
859 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000860 p = buffer_get_string(&b, &len);
861 if ((session_id2 == NULL) ||
862 (len != session_id2_len) ||
863 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000865 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000866 }
867 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
868 fail++;
869 p = buffer_get_string(&b, NULL);
870 if (strcmp(authctxt->user, p) != 0) {
871 log("wrong user name passed to monitor: expected %s != %.100s",
872 authctxt->user, p);
873 fail++;
874 }
875 xfree(p);
876 buffer_skip_string(&b);
877 if (datafellows & SSH_BUG_PKAUTH) {
878 if (!buffer_get_char(&b))
879 fail++;
880 } else {
881 p = buffer_get_string(&b, NULL);
882 if (strcmp("publickey", p) != 0)
883 fail++;
884 xfree(p);
885 if (!buffer_get_char(&b))
886 fail++;
887 buffer_skip_string(&b);
888 }
889 buffer_skip_string(&b);
890 if (buffer_len(&b) != 0)
891 fail++;
892 buffer_free(&b);
893 return (fail == 0);
894}
895
896static int
Damien Millera10f5612002-09-12 09:49:15 +1000897monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
898 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899{
900 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +1000901 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902 u_int len;
903 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000904
905 buffer_init(&b);
906 buffer_append(&b, data, datalen);
907
Ben Lindstromf67e0772002-06-06 20:58:19 +0000908 p = buffer_get_string(&b, &len);
909 if ((session_id2 == NULL) ||
910 (len != session_id2_len) ||
911 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000913 xfree(p);
914
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
916 fail++;
917 p = buffer_get_string(&b, NULL);
918 if (strcmp(authctxt->user, p) != 0) {
919 log("wrong user name passed to monitor: expected %s != %.100s",
920 authctxt->user, p);
921 fail++;
922 }
923 xfree(p);
924 buffer_skip_string(&b); /* service */
925 p = buffer_get_string(&b, NULL);
926 if (strcmp(p, "hostbased") != 0)
927 fail++;
928 xfree(p);
929 buffer_skip_string(&b); /* pkalg */
930 buffer_skip_string(&b); /* pkblob */
931
932 /* verify client host, strip trailing dot if necessary */
933 p = buffer_get_string(&b, NULL);
934 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
935 p[len - 1] = '\0';
936 if (strcmp(p, chost) != 0)
937 fail++;
938 xfree(p);
939
940 /* verify client user */
941 p = buffer_get_string(&b, NULL);
942 if (strcmp(p, cuser) != 0)
943 fail++;
944 xfree(p);
945
946 if (buffer_len(&b) != 0)
947 fail++;
948 buffer_free(&b);
949 return (fail == 0);
950}
951
952int
953mm_answer_keyverify(int socket, Buffer *m)
954{
955 Key *key;
956 u_char *signature, *data, *blob;
957 u_int signaturelen, datalen, bloblen;
958 int verified = 0;
959 int valid_data = 0;
960
961 blob = buffer_get_string(m, &bloblen);
962 signature = buffer_get_string(m, &signaturelen);
963 data = buffer_get_string(m, &datalen);
964
965 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +0000966 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000967 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000968
969 key = key_from_blob(blob, bloblen);
970 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000971 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
973 switch (key_blobtype) {
974 case MM_USERKEY:
975 valid_data = monitor_valid_userblob(data, datalen);
976 break;
977 case MM_HOSTKEY:
978 valid_data = monitor_valid_hostbasedblob(data, datalen,
979 hostbased_cuser, hostbased_chost);
980 break;
981 default:
982 valid_data = 0;
983 break;
984 }
985 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000986 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000987
988 verified = key_verify(key, signature, signaturelen, data, datalen);
989 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000990 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000991
992 key_free(key);
993 xfree(blob);
994 xfree(signature);
995 xfree(data);
996
Ben Lindstrome1c09122002-06-23 00:38:24 +0000997 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
998
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000999 monitor_reset_key_state();
1000
1001 buffer_clear(m);
1002 buffer_put_int(m, verified);
1003 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1004
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001005 return (verified);
1006}
1007
1008static void
1009mm_record_login(Session *s, struct passwd *pw)
1010{
1011 socklen_t fromlen;
1012 struct sockaddr_storage from;
1013
1014 /*
1015 * Get IP address of client. If the connection is not a socket, let
1016 * the address be 0.0.0.0.
1017 */
1018 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001019 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001020 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001021 if (getpeername(packet_get_connection_in(),
1022 (struct sockaddr *) & from, &fromlen) < 0) {
1023 debug("getpeername: %.100s", strerror(errno));
1024 fatal_cleanup();
1025 }
1026 }
1027 /* Record that there was a login on that tty from the remote host. */
1028 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1029 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Damien Millerebc23062002-09-04 16:45:09 +10001030 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031}
1032
1033static void
1034mm_session_close(Session *s)
1035{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001036 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001037 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001038 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001039 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1040 session_pty_cleanup2(s);
1041 }
1042 s->used = 0;
1043}
1044
1045int
1046mm_answer_pty(int socket, Buffer *m)
1047{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001048 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001049 Session *s;
1050 int res, fd0;
1051
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001052 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001053
1054 buffer_clear(m);
1055 s = session_new();
1056 if (s == NULL)
1057 goto error;
1058 s->authctxt = authctxt;
1059 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001060 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001061 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1062 if (res == 0)
1063 goto error;
1064 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1065 pty_setowner(authctxt->pw, s->tty);
1066
1067 buffer_put_int(m, 1);
1068 buffer_put_cstring(m, s->tty);
1069 mm_request_send(socket, MONITOR_ANS_PTY, m);
1070
1071 mm_send_fd(socket, s->ptyfd);
1072 mm_send_fd(socket, s->ttyfd);
1073
1074 /* We need to trick ttyslot */
1075 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001076 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001077
1078 mm_record_login(s, authctxt->pw);
1079
1080 /* Now we can close the file descriptor again */
1081 close(0);
1082
1083 /* make sure nothing uses fd 0 */
1084 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001085 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001087 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001088
1089 /* slave is not needed */
1090 close(s->ttyfd);
1091 s->ttyfd = s->ptyfd;
1092 /* no need to dup() because nobody closes ptyfd */
1093 s->ptymaster = s->ptyfd;
1094
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001095 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001096
1097 return (0);
1098
1099 error:
1100 if (s != NULL)
1101 mm_session_close(s);
1102 buffer_put_int(m, 0);
1103 mm_request_send(socket, MONITOR_ANS_PTY, m);
1104 return (0);
1105}
1106
1107int
1108mm_answer_pty_cleanup(int socket, Buffer *m)
1109{
1110 Session *s;
1111 char *tty;
1112
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001113 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001114
1115 tty = buffer_get_string(m, NULL);
1116 if ((s = session_by_tty(tty)) != NULL)
1117 mm_session_close(s);
1118 buffer_clear(m);
1119 xfree(tty);
1120 return (0);
1121}
1122
1123int
1124mm_answer_sesskey(int socket, Buffer *m)
1125{
1126 BIGNUM *p;
1127 int rsafail;
1128
1129 /* Turn off permissions */
1130 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1131
1132 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001133 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134
1135 buffer_get_bignum2(m, p);
1136
1137 rsafail = ssh1_session_key(p);
1138
1139 buffer_clear(m);
1140 buffer_put_int(m, rsafail);
1141 buffer_put_bignum2(m, p);
1142
1143 BN_clear_free(p);
1144
1145 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1146
1147 /* Turn on permissions for sessid passing */
1148 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1149
1150 return (0);
1151}
1152
1153int
1154mm_answer_sessid(int socket, Buffer *m)
1155{
1156 int i;
1157
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001158 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001159
1160 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001161 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001162 for (i = 0; i < 16; i++)
1163 session_id[i] = buffer_get_char(m);
1164
1165 /* Turn on permissions for getpwnam */
1166 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1167
1168 return (0);
1169}
1170
1171int
1172mm_answer_rsa_keyallowed(int socket, Buffer *m)
1173{
1174 BIGNUM *client_n;
1175 Key *key = NULL;
1176 u_char *blob = NULL;
1177 u_int blen = 0;
1178 int allowed = 0;
1179
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001180 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001181
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001182 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001183 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001184 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001185 buffer_get_bignum2(m, client_n);
1186 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1187 BN_clear_free(client_n);
1188 }
1189 buffer_clear(m);
1190 buffer_put_int(m, allowed);
1191
1192 /* clear temporarily storage (used by generate challenge) */
1193 monitor_reset_key_state();
1194
1195 if (allowed && key != NULL) {
1196 key->type = KEY_RSA; /* cheat for key_to_blob */
1197 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001198 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001199 buffer_put_string(m, blob, blen);
1200
1201 /* Save temporarily for comparison in verify */
1202 key_blob = blob;
1203 key_bloblen = blen;
1204 key_blobtype = MM_RSAUSERKEY;
1205 key_free(key);
1206 }
1207
1208 mm_append_debug(m);
1209
1210 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1211
1212 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1213 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1214 return (0);
1215}
1216
1217int
1218mm_answer_rsa_challenge(int socket, Buffer *m)
1219{
1220 Key *key = NULL;
1221 u_char *blob;
1222 u_int blen;
1223
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001224 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001225
1226 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001227 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228 blob = buffer_get_string(m, &blen);
1229 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001230 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001231 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001232 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001233 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001234 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001235
1236 if (ssh1_challenge)
1237 BN_clear_free(ssh1_challenge);
1238 ssh1_challenge = auth_rsa_generate_challenge(key);
1239
1240 buffer_clear(m);
1241 buffer_put_bignum2(m, ssh1_challenge);
1242
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001243 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1245
1246 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1247 return (0);
1248}
1249
1250int
1251mm_answer_rsa_response(int socket, Buffer *m)
1252{
1253 Key *key = NULL;
1254 u_char *blob, *response;
1255 u_int blen, len;
1256 int success;
1257
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001258 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001259
1260 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001261 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001262 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001263 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001264
1265 blob = buffer_get_string(m, &blen);
1266 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001267 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001268 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001269 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001270 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001271 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001272 response = buffer_get_string(m, &len);
1273 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001274 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001275 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1276
1277 key_free(key);
1278 xfree(response);
1279
1280 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1281
1282 /* reset state */
1283 BN_clear_free(ssh1_challenge);
1284 ssh1_challenge = NULL;
1285 monitor_reset_key_state();
1286
1287 buffer_clear(m);
1288 buffer_put_int(m, success);
1289 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1290
1291 return (success);
1292}
1293
Damien Millerd94e5492002-09-27 13:25:58 +10001294#ifdef KRB4
1295int
1296mm_answer_krb4(int socket, Buffer *m)
1297{
1298 KTEXT_ST auth, reply;
1299 char *client, *p;
1300 int success;
1301 u_int alen;
1302
1303 reply.length = auth.length = 0;
1304
1305 p = buffer_get_string(m, &alen);
1306 if (alen >= MAX_KTXT_LEN)
1307 fatal("%s: auth too large", __func__);
1308 memcpy(auth.dat, p, alen);
1309 auth.length = alen;
1310 memset(p, 0, alen);
1311 xfree(p);
1312
1313 success = options.kerberos_authentication &&
1314 authctxt->valid &&
1315 auth_krb4(authctxt, &auth, &client, &reply);
1316
1317 memset(auth.dat, 0, alen);
1318 buffer_clear(m);
1319 buffer_put_int(m, success);
1320
1321 if (success) {
1322 buffer_put_cstring(m, client);
1323 buffer_put_string(m, reply.dat, reply.length);
1324 if (client)
1325 xfree(client);
1326 if (reply.length)
1327 memset(reply.dat, 0, reply.length);
1328 }
1329
1330 debug3("%s: sending result %d", __func__, success);
1331 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1332
1333 auth_method = "kerberos";
1334
1335 /* Causes monitor loop to terminate if authenticated */
1336 return (success);
1337}
1338#endif
Damien Miller25162f22002-09-12 09:47:29 +10001339
1340#ifdef KRB5
1341int
1342mm_answer_krb5(int socket, Buffer *m)
1343{
1344 krb5_data tkt, reply;
1345 char *client_user;
1346 u_int len;
1347 int success;
1348
1349 /* use temporary var to avoid size issues on 64bit arch */
1350 tkt.data = buffer_get_string(m, &len);
1351 tkt.length = len;
1352
Damien Milleref73f502002-09-25 12:20:17 +10001353 success = options.kerberos_authentication &&
1354 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001355 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001356
1357 if (tkt.length)
1358 xfree(tkt.data);
1359
1360 buffer_clear(m);
1361 buffer_put_int(m, success);
1362
1363 if (success) {
1364 buffer_put_cstring(m, client_user);
1365 buffer_put_string(m, reply.data, reply.length);
1366 if (client_user)
1367 xfree(client_user);
1368 if (reply.length)
1369 xfree(reply.data);
1370 }
1371 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1372
1373 return success;
1374}
1375#endif
1376
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001377int
1378mm_answer_term(int socket, Buffer *req)
1379{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001380 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381 int res, status;
1382
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001383 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384
1385 /* The child is terminating */
1386 session_destroy_all(&mm_session_close);
1387
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001388 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001389 if (errno != EINTR)
1390 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391
1392 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1393
1394 /* Terminate process */
1395 exit (res);
1396}
1397
1398void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001399monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001400{
1401 if (compat20) {
1402 set_newkeys(MODE_IN);
1403 set_newkeys(MODE_OUT);
1404 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001406 packet_set_encryption_key(child_state.ssh1key,
1407 child_state.ssh1keylen, child_state.ssh1cipher);
1408 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409 }
1410
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001411 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001412 packet_set_keycontext(MODE_OUT, child_state.keyout);
1413 xfree(child_state.keyout);
1414 packet_set_keycontext(MODE_IN, child_state.keyin);
1415 xfree(child_state.keyin);
1416
1417 if (!compat20) {
1418 packet_set_iv(MODE_OUT, child_state.ivout);
1419 xfree(child_state.ivout);
1420 packet_set_iv(MODE_IN, child_state.ivin);
1421 xfree(child_state.ivin);
1422 }
1423
1424 memcpy(&incoming_stream, &child_state.incoming,
1425 sizeof(incoming_stream));
1426 memcpy(&outgoing_stream, &child_state.outgoing,
1427 sizeof(outgoing_stream));
1428
1429 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001430 if (options.compression)
1431 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432
1433 /* Network I/O buffers */
1434 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1435 buffer_clear(&input);
1436 buffer_append(&input, child_state.input, child_state.ilen);
1437 memset(child_state.input, 0, child_state.ilen);
1438 xfree(child_state.input);
1439
1440 buffer_clear(&output);
1441 buffer_append(&output, child_state.output, child_state.olen);
1442 memset(child_state.output, 0, child_state.olen);
1443 xfree(child_state.output);
1444}
1445
1446static Kex *
1447mm_get_kex(Buffer *m)
1448{
1449 Kex *kex;
1450 void *blob;
1451 u_int bloblen;
1452
1453 kex = xmalloc(sizeof(*kex));
1454 memset(kex, 0, sizeof(*kex));
1455 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001456 if ((session_id2 == NULL) ||
1457 (kex->session_id_len != session_id2_len) ||
1458 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1459 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001460 kex->we_need = buffer_get_int(m);
1461 kex->server = 1;
1462 kex->hostkey_type = buffer_get_int(m);
1463 kex->kex_type = buffer_get_int(m);
1464 blob = buffer_get_string(m, &bloblen);
1465 buffer_init(&kex->my);
1466 buffer_append(&kex->my, blob, bloblen);
1467 xfree(blob);
1468 blob = buffer_get_string(m, &bloblen);
1469 buffer_init(&kex->peer);
1470 buffer_append(&kex->peer, blob, bloblen);
1471 xfree(blob);
1472 kex->done = 1;
1473 kex->flags = buffer_get_int(m);
1474 kex->client_version_string = buffer_get_string(m, NULL);
1475 kex->server_version_string = buffer_get_string(m, NULL);
1476 kex->load_host_key=&get_hostkey_by_type;
1477 kex->host_key_index=&get_hostkey_index;
1478
1479 return (kex);
1480}
1481
1482/* This function requries careful sanity checking */
1483
1484void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001485mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001486{
1487 Buffer m;
1488 u_char *blob, *p;
1489 u_int bloblen, plen;
1490
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001491 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001492
1493 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001494 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001495 if (!compat20) {
1496 child_state.ssh1protoflags = buffer_get_int(&m);
1497 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001498 child_state.ssh1key = buffer_get_string(&m,
1499 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001500 child_state.ivout = buffer_get_string(&m,
1501 &child_state.ivoutlen);
1502 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1503 goto skip;
1504 } else {
1505 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001506 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001507 }
1508
1509 blob = buffer_get_string(&m, &bloblen);
1510 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1511 xfree(blob);
1512
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001513 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001514 blob = buffer_get_string(&m, &bloblen);
1515 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1516 xfree(blob);
1517
1518 /* Now get sequence numbers for the packets */
1519 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1520 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1521
1522 skip:
1523 /* Get the key context */
1524 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1525 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1526
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001527 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001528 /* Get compression state */
1529 p = buffer_get_string(&m, &plen);
1530 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001531 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001532 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1533 xfree(p);
1534
1535 p = buffer_get_string(&m, &plen);
1536 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001537 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001538 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1539 xfree(p);
1540
1541 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001542 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001543 child_state.input = buffer_get_string(&m, &child_state.ilen);
1544 child_state.output = buffer_get_string(&m, &child_state.olen);
1545
1546 buffer_free(&m);
1547}
1548
1549
1550/* Allocation functions for zlib */
1551void *
1552mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1553{
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001554 size_t len = size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001555 void *address;
1556
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001557 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001558 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1559
1560 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001561
1562 return (address);
1563}
1564
1565void
1566mm_zfree(struct mm_master *mm, void *address)
1567{
1568 mm_free(mm, address);
1569}
1570
1571void
1572mm_init_compression(struct mm_master *mm)
1573{
1574 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1575 outgoing_stream.zfree = (free_func)mm_zfree;
1576 outgoing_stream.opaque = mm;
1577
1578 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1579 incoming_stream.zfree = (free_func)mm_zfree;
1580 incoming_stream.opaque = mm;
1581}
1582
1583/* XXX */
1584
1585#define FD_CLOSEONEXEC(x) do { \
1586 if (fcntl(x, F_SETFD, 1) == -1) \
1587 fatal("fcntl(%d, F_SETFD)", x); \
1588} while (0)
1589
1590static void
1591monitor_socketpair(int *pair)
1592{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001593#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001594 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001595 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001596#else
1597 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001598 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001599#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001600 FD_CLOSEONEXEC(pair[0]);
1601 FD_CLOSEONEXEC(pair[1]);
1602}
1603
1604#define MM_MEMSIZE 65536
1605
1606struct monitor *
1607monitor_init(void)
1608{
1609 struct monitor *mon;
1610 int pair[2];
1611
1612 mon = xmalloc(sizeof(*mon));
1613
1614 monitor_socketpair(pair);
1615
1616 mon->m_recvfd = pair[0];
1617 mon->m_sendfd = pair[1];
1618
1619 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001620 if (options.compression) {
1621 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1622 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001623
Damien Miller2d6b8332002-06-21 15:59:49 +10001624 /* Compression needs to share state across borders */
1625 mm_init_compression(mon->m_zlib);
1626 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001627
1628 return mon;
1629}
1630
1631void
1632monitor_reinit(struct monitor *mon)
1633{
1634 int pair[2];
1635
1636 monitor_socketpair(pair);
1637
1638 mon->m_recvfd = pair[0];
1639 mon->m_sendfd = pair[1];
1640}