blob: 36f9a6c2001792c72f697315353615a8ee28483d [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 Miller3ab496b2003-05-14 13:47:37 +100028RCSID("$OpenBSD: monitor.c,v 1.39 2003/05/14 02:15:47 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 *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000121int mm_answer_pam_init_ctx(int, Buffer *);
122int mm_answer_pam_query(int, Buffer *);
123int mm_answer_pam_respond(int, Buffer *);
124int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000125#endif
126
Damien Millerd94e5492002-09-27 13:25:58 +1000127#ifdef KRB4
128int mm_answer_krb4(int, Buffer *);
129#endif
Damien Miller25162f22002-09-12 09:47:29 +1000130#ifdef KRB5
131int mm_answer_krb5(int, Buffer *);
132#endif
133
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000134static Authctxt *authctxt;
135static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
136
137/* local state for key verify */
138static u_char *key_blob = NULL;
139static u_int key_bloblen = 0;
140static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000141static char *hostbased_cuser = NULL;
142static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000143static char *auth_method = "unknown";
Ben Lindstromf67e0772002-06-06 20:58:19 +0000144static int session_id2_len = 0;
145static u_char *session_id2 = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000146
147struct mon_table {
148 enum monitor_reqtype type;
149 int flags;
150 int (*f)(int, Buffer *);
151};
152
153#define MON_ISAUTH 0x0004 /* Required for Authentication */
154#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
155#define MON_ONCE 0x0010 /* Disable after calling */
156
157#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
158
159#define MON_PERMIT 0x1000 /* Request is permitted */
160
161struct mon_table mon_dispatch_proto20[] = {
162 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
163 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
164 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
165 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000166 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000167 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000168#ifdef USE_PAM
169 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000170 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
171 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
172 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
173 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000174#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000175#ifdef BSD_AUTH
176 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
177 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
178#endif
179#ifdef SKEY
180 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
181 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
182#endif
183 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
184 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
Damien Miller3ab496b2003-05-14 13:47:37 +1000185#ifdef KRB5
186 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
187#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000188 {0, 0, NULL}
189};
190
191struct mon_table mon_dispatch_postauth20[] = {
192 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
193 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
194 {MONITOR_REQ_PTY, 0, mm_answer_pty},
195 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
196 {MONITOR_REQ_TERM, 0, mm_answer_term},
197 {0, 0, NULL}
198};
199
200struct mon_table mon_dispatch_proto15[] = {
201 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
202 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
203 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
204 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
205 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
206 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
207 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
208 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
209#ifdef BSD_AUTH
210 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
211 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
212#endif
213#ifdef SKEY
214 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
215 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
216#endif
Damien Millera33501b2002-05-08 12:24:42 +1000217#ifdef USE_PAM
218 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000219 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
220 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
221 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
222 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000223#endif
Damien Millerd94e5492002-09-27 13:25:58 +1000224#ifdef KRB4
225 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
226#endif
Damien Miller25162f22002-09-12 09:47:29 +1000227#ifdef KRB5
228 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
229#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000230 {0, 0, NULL}
231};
232
233struct mon_table mon_dispatch_postauth15[] = {
234 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
235 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
236 {MONITOR_REQ_TERM, 0, mm_answer_term},
237 {0, 0, NULL}
238};
239
240struct mon_table *mon_dispatch;
241
242/* Specifies if a certain message is allowed at the moment */
243
244static void
245monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
246{
247 while (ent->f != NULL) {
248 if (ent->type == type) {
249 ent->flags &= ~MON_PERMIT;
250 ent->flags |= permit ? MON_PERMIT : 0;
251 return;
252 }
253 ent++;
254 }
255}
256
257static void
258monitor_permit_authentications(int permit)
259{
260 struct mon_table *ent = mon_dispatch;
261
262 while (ent->f != NULL) {
263 if (ent->flags & MON_AUTH) {
264 ent->flags &= ~MON_PERMIT;
265 ent->flags |= permit ? MON_PERMIT : 0;
266 }
267 ent++;
268 }
269}
270
271Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000272monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000273{
274 struct mon_table *ent;
275 int authenticated = 0;
276
277 debug3("preauth child monitor started");
278
279 if (compat20) {
280 mon_dispatch = mon_dispatch_proto20;
281
282 /* Permit requests for moduli and signatures */
283 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
284 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
285 } else {
286 mon_dispatch = mon_dispatch_proto15;
287
288 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
289 }
290
291 authctxt = authctxt_new();
292
293 /* The first few requests do not require asynchronous access */
294 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000295 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296 if (authenticated) {
297 if (!(ent->flags & MON_AUTHDECIDE))
298 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000299 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000300 if (authctxt->pw->pw_uid == 0 &&
301 !auth_root_allowed(auth_method))
302 authenticated = 0;
303 }
304
305 if (ent->flags & MON_AUTHDECIDE) {
306 auth_log(authctxt, authenticated, auth_method,
307 compat20 ? " ssh2" : "");
308 if (!authenticated)
309 authctxt->failures++;
310 }
311 }
312
313 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000314 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315
316 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000317 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000318
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000319 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000320
321 return (authctxt);
322}
323
324void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000325monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000326{
327 if (compat20) {
328 mon_dispatch = mon_dispatch_postauth20;
329
330 /* Permit requests for moduli and signatures */
331 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
332 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
333 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
334
335 } else {
336 mon_dispatch = mon_dispatch_postauth15;
337 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
338 }
339 if (!no_pty_flag) {
340 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
341 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
342 }
343
344 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000345 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346}
347
348void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000349monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000350{
Damien Miller2d6b8332002-06-21 15:59:49 +1000351 if (options.compression) {
352 /* The member allocation is not visible, so sync it */
353 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
354 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000355}
356
357int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000358monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359 struct mon_table **pent)
360{
361 Buffer m;
362 int ret;
363 u_char type;
364
365 buffer_init(&m);
366
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000367 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368 type = buffer_get_char(&m);
369
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000370 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000371
372 while (ent->f != NULL) {
373 if (ent->type == type)
374 break;
375 ent++;
376 }
377
378 if (ent->f != NULL) {
379 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000380 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000382 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000383 buffer_free(&m);
384
385 /* The child may use this request only once, disable it */
386 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000387 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000388 type);
389 ent->flags &= ~MON_PERMIT;
390 }
391
392 if (pent != NULL)
393 *pent = ent;
394
395 return ret;
396 }
397
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000398 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000399
400 /* NOTREACHED */
401 return (-1);
402}
403
404/* allowed key state */
405static int
406monitor_allowed_key(u_char *blob, u_int bloblen)
407{
408 /* make sure key is allowed */
409 if (key_blob == NULL || key_bloblen != bloblen ||
410 memcmp(key_blob, blob, key_bloblen))
411 return (0);
412 return (1);
413}
414
415static void
416monitor_reset_key_state(void)
417{
418 /* reset state */
419 if (key_blob != NULL)
420 xfree(key_blob);
421 if (hostbased_cuser != NULL)
422 xfree(hostbased_cuser);
423 if (hostbased_chost != NULL)
424 xfree(hostbased_chost);
425 key_blob = NULL;
426 key_bloblen = 0;
427 key_blobtype = MM_NOKEY;
428 hostbased_cuser = NULL;
429 hostbased_chost = NULL;
430}
431
432int
433mm_answer_moduli(int socket, Buffer *m)
434{
435 DH *dh;
436 int min, want, max;
437
438 min = buffer_get_int(m);
439 want = buffer_get_int(m);
440 max = buffer_get_int(m);
441
442 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000443 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000444 /* We need to check here, too, in case the child got corrupted */
445 if (max < min || want < min || max < want)
446 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000447 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000448
449 buffer_clear(m);
450
451 dh = choose_dh(min, want, max);
452 if (dh == NULL) {
453 buffer_put_char(m, 0);
454 return (0);
455 } else {
456 /* Send first bignum */
457 buffer_put_char(m, 1);
458 buffer_put_bignum2(m, dh->p);
459 buffer_put_bignum2(m, dh->g);
460
461 DH_free(dh);
462 }
463 mm_request_send(socket, MONITOR_ANS_MODULI, m);
464 return (0);
465}
466
467int
468mm_answer_sign(int socket, Buffer *m)
469{
470 Key *key;
471 u_char *p;
472 u_char *signature;
473 u_int siglen, datlen;
474 int keyid;
475
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000476 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000477
478 keyid = buffer_get_int(m);
479 p = buffer_get_string(m, &datlen);
480
481 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000482 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000483
Ben Lindstromf67e0772002-06-06 20:58:19 +0000484 /* save session id, it will be passed on the first call */
485 if (session_id2_len == 0) {
486 session_id2_len = datlen;
487 session_id2 = xmalloc(session_id2_len);
488 memcpy(session_id2, p, session_id2_len);
489 }
490
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000491 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000492 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000493 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000494 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000495
Ben Lindstromd5502182002-06-27 00:12:57 +0000496 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000497
498 buffer_clear(m);
499 buffer_put_string(m, signature, siglen);
500
501 xfree(p);
502 xfree(signature);
503
504 mm_request_send(socket, MONITOR_ANS_SIGN, m);
505
506 /* Turn on permissions for getpwnam */
507 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
508
509 return (0);
510}
511
512/* Retrieves the password entry and also checks if the user is permitted */
513
514int
515mm_answer_pwnamallow(int socket, Buffer *m)
516{
517 char *login;
518 struct passwd *pwent;
519 int allowed = 0;
520
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000521 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522
523 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000524 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000525
526 login = buffer_get_string(m, NULL);
527
528 pwent = getpwnamallow(login);
529
530 authctxt->user = xstrdup(login);
531 setproctitle("%s [priv]", pwent ? login : "unknown");
532 xfree(login);
533
534 buffer_clear(m);
535
536 if (pwent == NULL) {
537 buffer_put_char(m, 0);
538 goto out;
539 }
540
541 allowed = 1;
542 authctxt->pw = pwent;
543 authctxt->valid = 1;
544
545 buffer_put_char(m, 1);
546 buffer_put_string(m, pwent, sizeof(struct passwd));
547 buffer_put_cstring(m, pwent->pw_name);
548 buffer_put_cstring(m, "*");
549 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000550#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000552#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000553 buffer_put_cstring(m, pwent->pw_dir);
554 buffer_put_cstring(m, pwent->pw_shell);
555
556 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000557 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000558 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
559
560 /* For SSHv1 allow authentication now */
561 if (!compat20)
562 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000563 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000564 /* Allow service/style information on the auth context */
565 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000566 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
567 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000568
Damien Millera33501b2002-05-08 12:24:42 +1000569#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000570 if (options.use_pam)
571 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000572#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000573
574 return (0);
575}
576
Damien Miller5ad9fd92002-05-13 11:07:41 +1000577int mm_answer_auth2_read_banner(int socket, Buffer *m)
578{
579 char *banner;
580
581 buffer_clear(m);
582 banner = auth2_read_banner();
583 buffer_put_cstring(m, banner != NULL ? banner : "");
584 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
585
586 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000587 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000588
589 return (0);
590}
591
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000592int
593mm_answer_authserv(int socket, Buffer *m)
594{
595 monitor_permit_authentications(1);
596
597 authctxt->service = buffer_get_string(m, NULL);
598 authctxt->style = buffer_get_string(m, NULL);
599 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000600 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000601
602 if (strlen(authctxt->style) == 0) {
603 xfree(authctxt->style);
604 authctxt->style = NULL;
605 }
606
607 return (0);
608}
609
610int
611mm_answer_authpassword(int socket, Buffer *m)
612{
613 static int call_count;
614 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000615 int authenticated;
616 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000617
618 passwd = buffer_get_string(m, &plen);
619 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000620 authenticated = options.password_authentication &&
Damien Millereab4bae2003-04-29 23:22:40 +1000621 auth_password(authctxt, passwd) && authctxt->valid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000622 memset(passwd, 0, strlen(passwd));
623 xfree(passwd);
624
625 buffer_clear(m);
626 buffer_put_int(m, authenticated);
627
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000628 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000629 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
630
631 call_count++;
632 if (plen == 0 && call_count == 1)
633 auth_method = "none";
634 else
635 auth_method = "password";
636
637 /* Causes monitor loop to terminate if authenticated */
638 return (authenticated);
639}
640
641#ifdef BSD_AUTH
642int
643mm_answer_bsdauthquery(int socket, Buffer *m)
644{
645 char *name, *infotxt;
646 u_int numprompts;
647 u_int *echo_on;
648 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100649 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000650
Damien Millerb7df3af2003-02-24 11:55:46 +1100651 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
652 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653
654 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100655 buffer_put_int(m, success);
656 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000657 buffer_put_cstring(m, prompts[0]);
658
Damien Millerb7df3af2003-02-24 11:55:46 +1100659 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
661
Damien Millerb7df3af2003-02-24 11:55:46 +1100662 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000663 xfree(name);
664 xfree(infotxt);
665 xfree(prompts);
666 xfree(echo_on);
667 }
668
669 return (0);
670}
671
672int
673mm_answer_bsdauthrespond(int socket, Buffer *m)
674{
675 char *response;
676 int authok;
677
678 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000679 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680
681 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000682 authok = options.challenge_response_authentication &&
683 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000684 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000685 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686 xfree(response);
687
688 buffer_clear(m);
689 buffer_put_int(m, authok);
690
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000691 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000692 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
693
694 auth_method = "bsdauth";
695
696 return (authok != 0);
697}
698#endif
699
700#ifdef SKEY
701int
702mm_answer_skeyquery(int socket, Buffer *m)
703{
704 struct skey skey;
705 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100706 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000707
Damien Millerb7df3af2003-02-24 11:55:46 +1100708 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709
710 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100711 buffer_put_int(m, success);
712 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000713 buffer_put_cstring(m, challenge);
714
Damien Millerb7df3af2003-02-24 11:55:46 +1100715 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000716 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
717
718 return (0);
719}
720
721int
722mm_answer_skeyrespond(int socket, Buffer *m)
723{
724 char *response;
725 int authok;
726
727 response = buffer_get_string(m, NULL);
728
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000729 authok = (options.challenge_response_authentication &&
730 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000731 skey_haskey(authctxt->pw->pw_name) == 0 &&
732 skey_passcheck(authctxt->pw->pw_name, response) != -1);
733
734 xfree(response);
735
736 buffer_clear(m);
737 buffer_put_int(m, authok);
738
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000739 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000740 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
741
742 auth_method = "skey";
743
744 return (authok != 0);
745}
746#endif
747
Damien Miller79418552002-04-23 20:28:48 +1000748#ifdef USE_PAM
749int
750mm_answer_pam_start(int socket, Buffer *m)
751{
752 char *user;
753
Damien Miller4e448a32003-05-14 15:11:48 +1000754 if (!options.use_pam)
755 fatal("UsePAM not set, but ended up in %s anyway", __func__);
756
Damien Miller79418552002-04-23 20:28:48 +1000757 user = buffer_get_string(m, NULL);
758
759 start_pam(user);
760
761 xfree(user);
762
763 return (0);
764}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000765
766static void *sshpam_ctxt, *sshpam_authok;
767extern KbdintDevice sshpam_device;
768
769int
770mm_answer_pam_init_ctx(int socket, Buffer *m)
771{
772
773 debug3("%s", __func__);
774 authctxt->user = buffer_get_string(m, NULL);
775 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
776 sshpam_authok = NULL;
777 buffer_clear(m);
778 if (sshpam_ctxt != NULL) {
779 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
780 buffer_put_int(m, 1);
781 } else {
782 buffer_put_int(m, 0);
783 }
784 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
785 return (0);
786}
787
788int
789mm_answer_pam_query(int socket, Buffer *m)
790{
791 char *name, *info, **prompts;
792 u_int num, *echo_on;
793 int i, ret;
794
795 debug3("%s", __func__);
796 sshpam_authok = NULL;
797 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
798 if (ret == 0 && num == 0)
799 sshpam_authok = sshpam_ctxt;
800 if (num > 1 || name == NULL || info == NULL)
801 ret = -1;
802 buffer_clear(m);
803 buffer_put_int(m, ret);
804 buffer_put_cstring(m, name);
805 xfree(name);
806 buffer_put_cstring(m, info);
807 xfree(info);
808 buffer_put_int(m, num);
809 for (i = 0; i < num; ++i) {
810 buffer_put_cstring(m, prompts[i]);
811 xfree(prompts[i]);
812 buffer_put_int(m, echo_on[i]);
813 }
814 if (prompts != NULL)
815 xfree(prompts);
816 if (echo_on != NULL)
817 xfree(echo_on);
818 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
819 return (0);
820}
821
822int
823mm_answer_pam_respond(int socket, Buffer *m)
824{
825 char **resp;
826 u_int num;
827 int i, ret;
828
829 debug3("%s", __func__);
830 sshpam_authok = NULL;
831 num = buffer_get_int(m);
832 if (num > 0) {
833 resp = xmalloc(num * sizeof(char *));
834 for (i = 0; i < num; ++i)
835 resp[i] = buffer_get_string(m, NULL);
836 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
837 for (i = 0; i < num; ++i)
838 xfree(resp[i]);
839 xfree(resp);
840 } else {
841 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
842 }
843 buffer_clear(m);
844 buffer_put_int(m, ret);
845 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
846 auth_method = "keyboard-interactive/pam";
847 if (ret == 0)
848 sshpam_authok = sshpam_ctxt;
849 return (0);
850}
851
852int
853mm_answer_pam_free_ctx(int socket, Buffer *m)
854{
855
856 debug3("%s", __func__);
857 (sshpam_device.free_ctx)(sshpam_ctxt);
858 buffer_clear(m);
859 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
860 return (sshpam_authok == sshpam_ctxt);
861}
Damien Miller79418552002-04-23 20:28:48 +1000862#endif
863
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000864static void
865mm_append_debug(Buffer *m)
866{
867 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000868 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000869 buffer_append(m, buffer_ptr(&auth_debug),
870 buffer_len(&auth_debug));
871 buffer_clear(&auth_debug);
872 }
873}
874
875int
876mm_answer_keyallowed(int socket, Buffer *m)
877{
878 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000879 char *cuser, *chost;
880 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000881 u_int bloblen;
882 enum mm_keytype type = 0;
883 int allowed = 0;
884
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000885 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000886
887 type = buffer_get_int(m);
888 cuser = buffer_get_string(m, NULL);
889 chost = buffer_get_string(m, NULL);
890 blob = buffer_get_string(m, &bloblen);
891
892 key = key_from_blob(blob, bloblen);
893
894 if ((compat20 && type == MM_RSAHOSTKEY) ||
895 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000896 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000898 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000899
900 if (key != NULL && authctxt->pw != NULL) {
901 switch(type) {
902 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000903 allowed = options.pubkey_authentication &&
904 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000905 break;
906 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000907 allowed = options.hostbased_authentication &&
908 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909 cuser, chost, key);
910 break;
911 case MM_RSAHOSTKEY:
912 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000913 allowed = options.rhosts_rsa_authentication &&
914 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000915 cuser, chost, key);
916 break;
917 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000918 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000919 break;
920 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000921 }
Damien Miller00111382003-03-10 11:21:17 +1100922 if (key != NULL)
923 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000924
925 /* clear temporarily storage (used by verify) */
926 monitor_reset_key_state();
927
928 if (allowed) {
929 /* Save temporarily for comparison in verify */
930 key_blob = blob;
931 key_bloblen = bloblen;
932 key_blobtype = type;
933 hostbased_cuser = cuser;
934 hostbased_chost = chost;
935 }
936
937 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000938 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000939
940 buffer_clear(m);
941 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +1100942 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000943
944 mm_append_debug(m);
945
946 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
947
948 if (type == MM_RSAHOSTKEY)
949 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
950
951 return (0);
952}
953
954static int
955monitor_valid_userblob(u_char *data, u_int datalen)
956{
957 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +1000958 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000959 u_int len;
960 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000961
962 buffer_init(&b);
963 buffer_append(&b, data, datalen);
964
965 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000966 p = buffer_ptr(&b);
967 len = buffer_len(&b);
968 if ((session_id2 == NULL) ||
969 (len < session_id2_len) ||
970 (memcmp(p, session_id2, session_id2_len) != 0))
971 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972 buffer_consume(&b, session_id2_len);
973 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000974 p = buffer_get_string(&b, &len);
975 if ((session_id2 == NULL) ||
976 (len != session_id2_len) ||
977 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000978 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000979 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000980 }
981 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
982 fail++;
983 p = buffer_get_string(&b, NULL);
984 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000985 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000986 authctxt->user, p);
987 fail++;
988 }
989 xfree(p);
990 buffer_skip_string(&b);
991 if (datafellows & SSH_BUG_PKAUTH) {
992 if (!buffer_get_char(&b))
993 fail++;
994 } else {
995 p = buffer_get_string(&b, NULL);
996 if (strcmp("publickey", p) != 0)
997 fail++;
998 xfree(p);
999 if (!buffer_get_char(&b))
1000 fail++;
1001 buffer_skip_string(&b);
1002 }
1003 buffer_skip_string(&b);
1004 if (buffer_len(&b) != 0)
1005 fail++;
1006 buffer_free(&b);
1007 return (fail == 0);
1008}
1009
1010static int
Damien Millera10f5612002-09-12 09:49:15 +10001011monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1012 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001013{
1014 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001015 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001016 u_int len;
1017 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001018
1019 buffer_init(&b);
1020 buffer_append(&b, data, datalen);
1021
Ben Lindstromf67e0772002-06-06 20:58:19 +00001022 p = buffer_get_string(&b, &len);
1023 if ((session_id2 == NULL) ||
1024 (len != session_id2_len) ||
1025 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001026 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001027 xfree(p);
1028
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001029 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1030 fail++;
1031 p = buffer_get_string(&b, NULL);
1032 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001033 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 authctxt->user, p);
1035 fail++;
1036 }
1037 xfree(p);
1038 buffer_skip_string(&b); /* service */
1039 p = buffer_get_string(&b, NULL);
1040 if (strcmp(p, "hostbased") != 0)
1041 fail++;
1042 xfree(p);
1043 buffer_skip_string(&b); /* pkalg */
1044 buffer_skip_string(&b); /* pkblob */
1045
1046 /* verify client host, strip trailing dot if necessary */
1047 p = buffer_get_string(&b, NULL);
1048 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1049 p[len - 1] = '\0';
1050 if (strcmp(p, chost) != 0)
1051 fail++;
1052 xfree(p);
1053
1054 /* verify client user */
1055 p = buffer_get_string(&b, NULL);
1056 if (strcmp(p, cuser) != 0)
1057 fail++;
1058 xfree(p);
1059
1060 if (buffer_len(&b) != 0)
1061 fail++;
1062 buffer_free(&b);
1063 return (fail == 0);
1064}
1065
1066int
1067mm_answer_keyverify(int socket, Buffer *m)
1068{
1069 Key *key;
1070 u_char *signature, *data, *blob;
1071 u_int signaturelen, datalen, bloblen;
1072 int verified = 0;
1073 int valid_data = 0;
1074
1075 blob = buffer_get_string(m, &bloblen);
1076 signature = buffer_get_string(m, &signaturelen);
1077 data = buffer_get_string(m, &datalen);
1078
1079 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001080 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001081 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001082
1083 key = key_from_blob(blob, bloblen);
1084 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001085 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001086
1087 switch (key_blobtype) {
1088 case MM_USERKEY:
1089 valid_data = monitor_valid_userblob(data, datalen);
1090 break;
1091 case MM_HOSTKEY:
1092 valid_data = monitor_valid_hostbasedblob(data, datalen,
1093 hostbased_cuser, hostbased_chost);
1094 break;
1095 default:
1096 valid_data = 0;
1097 break;
1098 }
1099 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001100 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001101
1102 verified = key_verify(key, signature, signaturelen, data, datalen);
1103 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001104 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001105
1106 key_free(key);
1107 xfree(blob);
1108 xfree(signature);
1109 xfree(data);
1110
Ben Lindstrome1c09122002-06-23 00:38:24 +00001111 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1112
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001113 monitor_reset_key_state();
1114
1115 buffer_clear(m);
1116 buffer_put_int(m, verified);
1117 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1118
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001119 return (verified);
1120}
1121
1122static void
1123mm_record_login(Session *s, struct passwd *pw)
1124{
1125 socklen_t fromlen;
1126 struct sockaddr_storage from;
1127
1128 /*
1129 * Get IP address of client. If the connection is not a socket, let
1130 * the address be 0.0.0.0.
1131 */
1132 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001133 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001134 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001135 if (getpeername(packet_get_connection_in(),
1136 (struct sockaddr *) & from, &fromlen) < 0) {
1137 debug("getpeername: %.100s", strerror(errno));
1138 fatal_cleanup();
1139 }
1140 }
1141 /* Record that there was a login on that tty from the remote host. */
1142 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1143 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Damien Millerebc23062002-09-04 16:45:09 +10001144 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001145}
1146
1147static void
1148mm_session_close(Session *s)
1149{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001150 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001151 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001152 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001153 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1154 session_pty_cleanup2(s);
1155 }
1156 s->used = 0;
1157}
1158
1159int
1160mm_answer_pty(int socket, Buffer *m)
1161{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001162 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001163 Session *s;
1164 int res, fd0;
1165
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001166 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001167
1168 buffer_clear(m);
1169 s = session_new();
1170 if (s == NULL)
1171 goto error;
1172 s->authctxt = authctxt;
1173 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001174 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001175 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1176 if (res == 0)
1177 goto error;
1178 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1179 pty_setowner(authctxt->pw, s->tty);
1180
1181 buffer_put_int(m, 1);
1182 buffer_put_cstring(m, s->tty);
1183 mm_request_send(socket, MONITOR_ANS_PTY, m);
1184
1185 mm_send_fd(socket, s->ptyfd);
1186 mm_send_fd(socket, s->ttyfd);
1187
1188 /* We need to trick ttyslot */
1189 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001190 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001191
1192 mm_record_login(s, authctxt->pw);
1193
1194 /* Now we can close the file descriptor again */
1195 close(0);
1196
1197 /* make sure nothing uses fd 0 */
1198 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001199 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001200 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001201 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001202
1203 /* slave is not needed */
1204 close(s->ttyfd);
1205 s->ttyfd = s->ptyfd;
1206 /* no need to dup() because nobody closes ptyfd */
1207 s->ptymaster = s->ptyfd;
1208
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001209 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210
1211 return (0);
1212
1213 error:
1214 if (s != NULL)
1215 mm_session_close(s);
1216 buffer_put_int(m, 0);
1217 mm_request_send(socket, MONITOR_ANS_PTY, m);
1218 return (0);
1219}
1220
1221int
1222mm_answer_pty_cleanup(int socket, Buffer *m)
1223{
1224 Session *s;
1225 char *tty;
1226
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001227 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001228
1229 tty = buffer_get_string(m, NULL);
1230 if ((s = session_by_tty(tty)) != NULL)
1231 mm_session_close(s);
1232 buffer_clear(m);
1233 xfree(tty);
1234 return (0);
1235}
1236
1237int
1238mm_answer_sesskey(int socket, Buffer *m)
1239{
1240 BIGNUM *p;
1241 int rsafail;
1242
1243 /* Turn off permissions */
1244 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1245
1246 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001247 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001248
1249 buffer_get_bignum2(m, p);
1250
1251 rsafail = ssh1_session_key(p);
1252
1253 buffer_clear(m);
1254 buffer_put_int(m, rsafail);
1255 buffer_put_bignum2(m, p);
1256
1257 BN_clear_free(p);
1258
1259 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1260
1261 /* Turn on permissions for sessid passing */
1262 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1263
1264 return (0);
1265}
1266
1267int
1268mm_answer_sessid(int socket, Buffer *m)
1269{
1270 int i;
1271
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001272 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
1274 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001275 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001276 for (i = 0; i < 16; i++)
1277 session_id[i] = buffer_get_char(m);
1278
1279 /* Turn on permissions for getpwnam */
1280 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1281
1282 return (0);
1283}
1284
1285int
1286mm_answer_rsa_keyallowed(int socket, Buffer *m)
1287{
1288 BIGNUM *client_n;
1289 Key *key = NULL;
1290 u_char *blob = NULL;
1291 u_int blen = 0;
1292 int allowed = 0;
1293
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001294 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001295
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001296 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001297 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001298 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299 buffer_get_bignum2(m, client_n);
1300 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1301 BN_clear_free(client_n);
1302 }
1303 buffer_clear(m);
1304 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001305 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001306
1307 /* clear temporarily storage (used by generate challenge) */
1308 monitor_reset_key_state();
1309
1310 if (allowed && key != NULL) {
1311 key->type = KEY_RSA; /* cheat for key_to_blob */
1312 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001313 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 buffer_put_string(m, blob, blen);
1315
1316 /* Save temporarily for comparison in verify */
1317 key_blob = blob;
1318 key_bloblen = blen;
1319 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001320 }
Damien Miller00111382003-03-10 11:21:17 +11001321 if (key != NULL)
1322 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001323
1324 mm_append_debug(m);
1325
1326 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1327
1328 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1329 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1330 return (0);
1331}
1332
1333int
1334mm_answer_rsa_challenge(int socket, Buffer *m)
1335{
1336 Key *key = NULL;
1337 u_char *blob;
1338 u_int blen;
1339
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001340 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001341
1342 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001343 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344 blob = buffer_get_string(m, &blen);
1345 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001346 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001347 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001348 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001349 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001350 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351
1352 if (ssh1_challenge)
1353 BN_clear_free(ssh1_challenge);
1354 ssh1_challenge = auth_rsa_generate_challenge(key);
1355
1356 buffer_clear(m);
1357 buffer_put_bignum2(m, ssh1_challenge);
1358
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001359 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001360 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1361
1362 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001363
1364 xfree(blob);
1365 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001366 return (0);
1367}
1368
1369int
1370mm_answer_rsa_response(int socket, Buffer *m)
1371{
1372 Key *key = NULL;
1373 u_char *blob, *response;
1374 u_int blen, len;
1375 int success;
1376
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001377 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001378
1379 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001380 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001381 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001382 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001383
1384 blob = buffer_get_string(m, &blen);
1385 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001386 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001388 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001390 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001391 response = buffer_get_string(m, &len);
1392 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001393 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1395
Damien Miller00111382003-03-10 11:21:17 +11001396 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397 key_free(key);
1398 xfree(response);
1399
1400 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1401
1402 /* reset state */
1403 BN_clear_free(ssh1_challenge);
1404 ssh1_challenge = NULL;
1405 monitor_reset_key_state();
1406
1407 buffer_clear(m);
1408 buffer_put_int(m, success);
1409 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1410
1411 return (success);
1412}
1413
Damien Millerd94e5492002-09-27 13:25:58 +10001414#ifdef KRB4
1415int
1416mm_answer_krb4(int socket, Buffer *m)
1417{
1418 KTEXT_ST auth, reply;
1419 char *client, *p;
1420 int success;
1421 u_int alen;
1422
1423 reply.length = auth.length = 0;
1424
1425 p = buffer_get_string(m, &alen);
1426 if (alen >= MAX_KTXT_LEN)
1427 fatal("%s: auth too large", __func__);
1428 memcpy(auth.dat, p, alen);
1429 auth.length = alen;
1430 memset(p, 0, alen);
1431 xfree(p);
1432
1433 success = options.kerberos_authentication &&
1434 authctxt->valid &&
1435 auth_krb4(authctxt, &auth, &client, &reply);
1436
1437 memset(auth.dat, 0, alen);
1438 buffer_clear(m);
1439 buffer_put_int(m, success);
1440
1441 if (success) {
1442 buffer_put_cstring(m, client);
1443 buffer_put_string(m, reply.dat, reply.length);
1444 if (client)
1445 xfree(client);
1446 if (reply.length)
1447 memset(reply.dat, 0, reply.length);
1448 }
1449
1450 debug3("%s: sending result %d", __func__, success);
1451 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1452
1453 auth_method = "kerberos";
1454
1455 /* Causes monitor loop to terminate if authenticated */
1456 return (success);
1457}
1458#endif
Damien Miller25162f22002-09-12 09:47:29 +10001459
1460#ifdef KRB5
1461int
1462mm_answer_krb5(int socket, Buffer *m)
1463{
1464 krb5_data tkt, reply;
1465 char *client_user;
1466 u_int len;
1467 int success;
1468
1469 /* use temporary var to avoid size issues on 64bit arch */
1470 tkt.data = buffer_get_string(m, &len);
1471 tkt.length = len;
1472
Damien Milleref73f502002-09-25 12:20:17 +10001473 success = options.kerberos_authentication &&
1474 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001475 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001476
1477 if (tkt.length)
1478 xfree(tkt.data);
1479
1480 buffer_clear(m);
1481 buffer_put_int(m, success);
1482
1483 if (success) {
1484 buffer_put_cstring(m, client_user);
1485 buffer_put_string(m, reply.data, reply.length);
1486 if (client_user)
1487 xfree(client_user);
1488 if (reply.length)
1489 xfree(reply.data);
1490 }
1491 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1492
Damien Miller3ab496b2003-05-14 13:47:37 +10001493 auth_method = "kerberos";
1494
Damien Miller25162f22002-09-12 09:47:29 +10001495 return success;
1496}
1497#endif
1498
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001499int
1500mm_answer_term(int socket, Buffer *req)
1501{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001502 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001503 int res, status;
1504
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001505 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001506
1507 /* The child is terminating */
1508 session_destroy_all(&mm_session_close);
1509
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001510 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001511 if (errno != EINTR)
1512 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513
1514 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1515
1516 /* Terminate process */
1517 exit (res);
1518}
1519
1520void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001521monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522{
1523 if (compat20) {
1524 set_newkeys(MODE_IN);
1525 set_newkeys(MODE_OUT);
1526 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001527 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001528 packet_set_encryption_key(child_state.ssh1key,
1529 child_state.ssh1keylen, child_state.ssh1cipher);
1530 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001531 }
1532
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001533 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001534 packet_set_keycontext(MODE_OUT, child_state.keyout);
1535 xfree(child_state.keyout);
1536 packet_set_keycontext(MODE_IN, child_state.keyin);
1537 xfree(child_state.keyin);
1538
1539 if (!compat20) {
1540 packet_set_iv(MODE_OUT, child_state.ivout);
1541 xfree(child_state.ivout);
1542 packet_set_iv(MODE_IN, child_state.ivin);
1543 xfree(child_state.ivin);
1544 }
1545
1546 memcpy(&incoming_stream, &child_state.incoming,
1547 sizeof(incoming_stream));
1548 memcpy(&outgoing_stream, &child_state.outgoing,
1549 sizeof(outgoing_stream));
1550
1551 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001552 if (options.compression)
1553 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001554
1555 /* Network I/O buffers */
1556 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1557 buffer_clear(&input);
1558 buffer_append(&input, child_state.input, child_state.ilen);
1559 memset(child_state.input, 0, child_state.ilen);
1560 xfree(child_state.input);
1561
1562 buffer_clear(&output);
1563 buffer_append(&output, child_state.output, child_state.olen);
1564 memset(child_state.output, 0, child_state.olen);
1565 xfree(child_state.output);
1566}
1567
1568static Kex *
1569mm_get_kex(Buffer *m)
1570{
1571 Kex *kex;
1572 void *blob;
1573 u_int bloblen;
1574
1575 kex = xmalloc(sizeof(*kex));
1576 memset(kex, 0, sizeof(*kex));
1577 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001578 if ((session_id2 == NULL) ||
1579 (kex->session_id_len != session_id2_len) ||
1580 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1581 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001582 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001583 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1584 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001585 kex->server = 1;
1586 kex->hostkey_type = buffer_get_int(m);
1587 kex->kex_type = buffer_get_int(m);
1588 blob = buffer_get_string(m, &bloblen);
1589 buffer_init(&kex->my);
1590 buffer_append(&kex->my, blob, bloblen);
1591 xfree(blob);
1592 blob = buffer_get_string(m, &bloblen);
1593 buffer_init(&kex->peer);
1594 buffer_append(&kex->peer, blob, bloblen);
1595 xfree(blob);
1596 kex->done = 1;
1597 kex->flags = buffer_get_int(m);
1598 kex->client_version_string = buffer_get_string(m, NULL);
1599 kex->server_version_string = buffer_get_string(m, NULL);
1600 kex->load_host_key=&get_hostkey_by_type;
1601 kex->host_key_index=&get_hostkey_index;
1602
1603 return (kex);
1604}
1605
1606/* This function requries careful sanity checking */
1607
1608void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001609mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001610{
1611 Buffer m;
1612 u_char *blob, *p;
1613 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001614 u_int32_t seqnr, packets;
1615 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001616
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001617 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001618
1619 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001620 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001621 if (!compat20) {
1622 child_state.ssh1protoflags = buffer_get_int(&m);
1623 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001624 child_state.ssh1key = buffer_get_string(&m,
1625 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001626 child_state.ivout = buffer_get_string(&m,
1627 &child_state.ivoutlen);
1628 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1629 goto skip;
1630 } else {
1631 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001632 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001633 }
1634
1635 blob = buffer_get_string(&m, &bloblen);
1636 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1637 xfree(blob);
1638
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001639 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001640 blob = buffer_get_string(&m, &bloblen);
1641 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1642 xfree(blob);
1643
1644 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001645 seqnr = buffer_get_int(&m);
1646 blocks = buffer_get_int64(&m);
1647 packets = buffer_get_int(&m);
1648 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1649 seqnr = buffer_get_int(&m);
1650 blocks = buffer_get_int64(&m);
1651 packets = buffer_get_int(&m);
1652 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001653
1654 skip:
1655 /* Get the key context */
1656 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1657 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1658
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001659 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001660 /* Get compression state */
1661 p = buffer_get_string(&m, &plen);
1662 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001663 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001664 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1665 xfree(p);
1666
1667 p = buffer_get_string(&m, &plen);
1668 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001669 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001670 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1671 xfree(p);
1672
1673 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001674 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001675 child_state.input = buffer_get_string(&m, &child_state.ilen);
1676 child_state.output = buffer_get_string(&m, &child_state.olen);
1677
1678 buffer_free(&m);
1679}
1680
1681
1682/* Allocation functions for zlib */
1683void *
1684mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1685{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001686 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001687 void *address;
1688
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001689 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001690 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1691
1692 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001693
1694 return (address);
1695}
1696
1697void
1698mm_zfree(struct mm_master *mm, void *address)
1699{
1700 mm_free(mm, address);
1701}
1702
1703void
1704mm_init_compression(struct mm_master *mm)
1705{
1706 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1707 outgoing_stream.zfree = (free_func)mm_zfree;
1708 outgoing_stream.opaque = mm;
1709
1710 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1711 incoming_stream.zfree = (free_func)mm_zfree;
1712 incoming_stream.opaque = mm;
1713}
1714
1715/* XXX */
1716
1717#define FD_CLOSEONEXEC(x) do { \
1718 if (fcntl(x, F_SETFD, 1) == -1) \
1719 fatal("fcntl(%d, F_SETFD)", x); \
1720} while (0)
1721
1722static void
1723monitor_socketpair(int *pair)
1724{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001725#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001726 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001727 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001728#else
1729 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001730 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001731#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001732 FD_CLOSEONEXEC(pair[0]);
1733 FD_CLOSEONEXEC(pair[1]);
1734}
1735
1736#define MM_MEMSIZE 65536
1737
1738struct monitor *
1739monitor_init(void)
1740{
1741 struct monitor *mon;
1742 int pair[2];
1743
1744 mon = xmalloc(sizeof(*mon));
1745
1746 monitor_socketpair(pair);
1747
1748 mon->m_recvfd = pair[0];
1749 mon->m_sendfd = pair[1];
1750
1751 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001752 if (options.compression) {
1753 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1754 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001755
Damien Miller2d6b8332002-06-21 15:59:49 +10001756 /* Compression needs to share state across borders */
1757 mm_init_compression(mon->m_zlib);
1758 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001759
1760 return mon;
1761}
1762
1763void
1764monitor_reinit(struct monitor *mon)
1765{
1766 int pair[2];
1767
1768 monitor_socketpair(pair);
1769
1770 mon->m_recvfd = pair[0];
1771 mon->m_sendfd = pair[1];
1772}