blob: 46241fbbd54ee5fc0649eb0dfdee0ce8204c0ecd [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 Millera5539d22003-04-09 20:50:06 +100028RCSID("$OpenBSD: monitor.c,v 1.37 2003/04/02 09:48:07 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},
185 {0, 0, NULL}
186};
187
188struct mon_table mon_dispatch_postauth20[] = {
189 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
190 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
191 {MONITOR_REQ_PTY, 0, mm_answer_pty},
192 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
193 {MONITOR_REQ_TERM, 0, mm_answer_term},
194 {0, 0, NULL}
195};
196
197struct mon_table mon_dispatch_proto15[] = {
198 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
199 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
200 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
201 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
202 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
203 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
204 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
205 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
206#ifdef BSD_AUTH
207 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
208 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
209#endif
210#ifdef SKEY
211 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
212 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
213#endif
Damien Millera33501b2002-05-08 12:24:42 +1000214#ifdef USE_PAM
215 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000216 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
217 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
218 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
219 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000220#endif
Damien Millerd94e5492002-09-27 13:25:58 +1000221#ifdef KRB4
222 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
223#endif
Damien Miller25162f22002-09-12 09:47:29 +1000224#ifdef KRB5
225 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
226#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000227 {0, 0, NULL}
228};
229
230struct mon_table mon_dispatch_postauth15[] = {
231 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
232 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
233 {MONITOR_REQ_TERM, 0, mm_answer_term},
234 {0, 0, NULL}
235};
236
237struct mon_table *mon_dispatch;
238
239/* Specifies if a certain message is allowed at the moment */
240
241static void
242monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
243{
244 while (ent->f != NULL) {
245 if (ent->type == type) {
246 ent->flags &= ~MON_PERMIT;
247 ent->flags |= permit ? MON_PERMIT : 0;
248 return;
249 }
250 ent++;
251 }
252}
253
254static void
255monitor_permit_authentications(int permit)
256{
257 struct mon_table *ent = mon_dispatch;
258
259 while (ent->f != NULL) {
260 if (ent->flags & MON_AUTH) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
263 }
264 ent++;
265 }
266}
267
268Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000269monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000270{
271 struct mon_table *ent;
272 int authenticated = 0;
273
274 debug3("preauth child monitor started");
275
276 if (compat20) {
277 mon_dispatch = mon_dispatch_proto20;
278
279 /* Permit requests for moduli and signatures */
280 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
281 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
282 } else {
283 mon_dispatch = mon_dispatch_proto15;
284
285 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
286 }
287
288 authctxt = authctxt_new();
289
290 /* The first few requests do not require asynchronous access */
291 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000292 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000293 if (authenticated) {
294 if (!(ent->flags & MON_AUTHDECIDE))
295 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000296 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000297 if (authctxt->pw->pw_uid == 0 &&
298 !auth_root_allowed(auth_method))
299 authenticated = 0;
300 }
301
302 if (ent->flags & MON_AUTHDECIDE) {
303 auth_log(authctxt, authenticated, auth_method,
304 compat20 ? " ssh2" : "");
305 if (!authenticated)
306 authctxt->failures++;
307 }
308 }
309
310 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000311 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000312
313 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000314 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000315
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000316 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000317
318 return (authctxt);
319}
320
321void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000322monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000323{
324 if (compat20) {
325 mon_dispatch = mon_dispatch_postauth20;
326
327 /* Permit requests for moduli and signatures */
328 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
329 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
330 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
331
332 } else {
333 mon_dispatch = mon_dispatch_postauth15;
334 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
335 }
336 if (!no_pty_flag) {
337 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
338 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
339 }
340
341 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000342 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000343}
344
345void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000346monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347{
Damien Miller2d6b8332002-06-21 15:59:49 +1000348 if (options.compression) {
349 /* The member allocation is not visible, so sync it */
350 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
351 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000352}
353
354int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000355monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000356 struct mon_table **pent)
357{
358 Buffer m;
359 int ret;
360 u_char type;
361
362 buffer_init(&m);
363
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000364 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000365 type = buffer_get_char(&m);
366
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000367 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000368
369 while (ent->f != NULL) {
370 if (ent->type == type)
371 break;
372 ent++;
373 }
374
375 if (ent->f != NULL) {
376 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000377 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000378 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000379 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000380 buffer_free(&m);
381
382 /* The child may use this request only once, disable it */
383 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000384 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385 type);
386 ent->flags &= ~MON_PERMIT;
387 }
388
389 if (pent != NULL)
390 *pent = ent;
391
392 return ret;
393 }
394
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000395 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000396
397 /* NOTREACHED */
398 return (-1);
399}
400
401/* allowed key state */
402static int
403monitor_allowed_key(u_char *blob, u_int bloblen)
404{
405 /* make sure key is allowed */
406 if (key_blob == NULL || key_bloblen != bloblen ||
407 memcmp(key_blob, blob, key_bloblen))
408 return (0);
409 return (1);
410}
411
412static void
413monitor_reset_key_state(void)
414{
415 /* reset state */
416 if (key_blob != NULL)
417 xfree(key_blob);
418 if (hostbased_cuser != NULL)
419 xfree(hostbased_cuser);
420 if (hostbased_chost != NULL)
421 xfree(hostbased_chost);
422 key_blob = NULL;
423 key_bloblen = 0;
424 key_blobtype = MM_NOKEY;
425 hostbased_cuser = NULL;
426 hostbased_chost = NULL;
427}
428
429int
430mm_answer_moduli(int socket, Buffer *m)
431{
432 DH *dh;
433 int min, want, max;
434
435 min = buffer_get_int(m);
436 want = buffer_get_int(m);
437 max = buffer_get_int(m);
438
439 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000440 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000441 /* We need to check here, too, in case the child got corrupted */
442 if (max < min || want < min || max < want)
443 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000444 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000445
446 buffer_clear(m);
447
448 dh = choose_dh(min, want, max);
449 if (dh == NULL) {
450 buffer_put_char(m, 0);
451 return (0);
452 } else {
453 /* Send first bignum */
454 buffer_put_char(m, 1);
455 buffer_put_bignum2(m, dh->p);
456 buffer_put_bignum2(m, dh->g);
457
458 DH_free(dh);
459 }
460 mm_request_send(socket, MONITOR_ANS_MODULI, m);
461 return (0);
462}
463
464int
465mm_answer_sign(int socket, Buffer *m)
466{
467 Key *key;
468 u_char *p;
469 u_char *signature;
470 u_int siglen, datlen;
471 int keyid;
472
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000473 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474
475 keyid = buffer_get_int(m);
476 p = buffer_get_string(m, &datlen);
477
478 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000479 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000480
Ben Lindstromf67e0772002-06-06 20:58:19 +0000481 /* save session id, it will be passed on the first call */
482 if (session_id2_len == 0) {
483 session_id2_len = datlen;
484 session_id2 = xmalloc(session_id2_len);
485 memcpy(session_id2, p, session_id2_len);
486 }
487
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000488 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000489 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000490 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000491 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000492
Ben Lindstromd5502182002-06-27 00:12:57 +0000493 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000494
495 buffer_clear(m);
496 buffer_put_string(m, signature, siglen);
497
498 xfree(p);
499 xfree(signature);
500
501 mm_request_send(socket, MONITOR_ANS_SIGN, m);
502
503 /* Turn on permissions for getpwnam */
504 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
505
506 return (0);
507}
508
509/* Retrieves the password entry and also checks if the user is permitted */
510
511int
512mm_answer_pwnamallow(int socket, Buffer *m)
513{
514 char *login;
515 struct passwd *pwent;
516 int allowed = 0;
517
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000518 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519
520 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000521 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000522
523 login = buffer_get_string(m, NULL);
524
525 pwent = getpwnamallow(login);
526
527 authctxt->user = xstrdup(login);
528 setproctitle("%s [priv]", pwent ? login : "unknown");
529 xfree(login);
530
531 buffer_clear(m);
532
533 if (pwent == NULL) {
534 buffer_put_char(m, 0);
535 goto out;
536 }
537
538 allowed = 1;
539 authctxt->pw = pwent;
540 authctxt->valid = 1;
541
542 buffer_put_char(m, 1);
543 buffer_put_string(m, pwent, sizeof(struct passwd));
544 buffer_put_cstring(m, pwent->pw_name);
545 buffer_put_cstring(m, "*");
546 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000547#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000549#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000550 buffer_put_cstring(m, pwent->pw_dir);
551 buffer_put_cstring(m, pwent->pw_shell);
552
553 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000554 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000555 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
556
557 /* For SSHv1 allow authentication now */
558 if (!compat20)
559 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000560 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000561 /* Allow service/style information on the auth context */
562 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000563 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
564 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000565
Damien Millera33501b2002-05-08 12:24:42 +1000566#ifdef USE_PAM
567 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
568#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000569
570 return (0);
571}
572
Damien Miller5ad9fd92002-05-13 11:07:41 +1000573int mm_answer_auth2_read_banner(int socket, Buffer *m)
574{
575 char *banner;
576
577 buffer_clear(m);
578 banner = auth2_read_banner();
579 buffer_put_cstring(m, banner != NULL ? banner : "");
580 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
581
582 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000583 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000584
585 return (0);
586}
587
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000588int
589mm_answer_authserv(int socket, Buffer *m)
590{
591 monitor_permit_authentications(1);
592
593 authctxt->service = buffer_get_string(m, NULL);
594 authctxt->style = buffer_get_string(m, NULL);
595 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000596 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000597
598 if (strlen(authctxt->style) == 0) {
599 xfree(authctxt->style);
600 authctxt->style = NULL;
601 }
602
603 return (0);
604}
605
606int
607mm_answer_authpassword(int socket, Buffer *m)
608{
609 static int call_count;
610 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000611 int authenticated;
612 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000613
614 passwd = buffer_get_string(m, &plen);
615 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000616 authenticated = options.password_authentication &&
Damien Millereab4bae2003-04-29 23:22:40 +1000617 auth_password(authctxt, passwd) && authctxt->valid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618 memset(passwd, 0, strlen(passwd));
619 xfree(passwd);
620
621 buffer_clear(m);
622 buffer_put_int(m, authenticated);
623
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000624 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000625 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
626
627 call_count++;
628 if (plen == 0 && call_count == 1)
629 auth_method = "none";
630 else
631 auth_method = "password";
632
633 /* Causes monitor loop to terminate if authenticated */
634 return (authenticated);
635}
636
637#ifdef BSD_AUTH
638int
639mm_answer_bsdauthquery(int socket, Buffer *m)
640{
641 char *name, *infotxt;
642 u_int numprompts;
643 u_int *echo_on;
644 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100645 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000646
Damien Millerb7df3af2003-02-24 11:55:46 +1100647 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
648 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000649
650 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100651 buffer_put_int(m, success);
652 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000653 buffer_put_cstring(m, prompts[0]);
654
Damien Millerb7df3af2003-02-24 11:55:46 +1100655 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000656 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
657
Damien Millerb7df3af2003-02-24 11:55:46 +1100658 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000659 xfree(name);
660 xfree(infotxt);
661 xfree(prompts);
662 xfree(echo_on);
663 }
664
665 return (0);
666}
667
668int
669mm_answer_bsdauthrespond(int socket, Buffer *m)
670{
671 char *response;
672 int authok;
673
674 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000675 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
677 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000678 authok = options.challenge_response_authentication &&
679 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000680 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000681 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000682 xfree(response);
683
684 buffer_clear(m);
685 buffer_put_int(m, authok);
686
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000687 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000688 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
689
690 auth_method = "bsdauth";
691
692 return (authok != 0);
693}
694#endif
695
696#ifdef SKEY
697int
698mm_answer_skeyquery(int socket, Buffer *m)
699{
700 struct skey skey;
701 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100702 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000703
Damien Millerb7df3af2003-02-24 11:55:46 +1100704 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705
706 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100707 buffer_put_int(m, success);
708 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000709 buffer_put_cstring(m, challenge);
710
Damien Millerb7df3af2003-02-24 11:55:46 +1100711 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
713
714 return (0);
715}
716
717int
718mm_answer_skeyrespond(int socket, Buffer *m)
719{
720 char *response;
721 int authok;
722
723 response = buffer_get_string(m, NULL);
724
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000725 authok = (options.challenge_response_authentication &&
726 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000727 skey_haskey(authctxt->pw->pw_name) == 0 &&
728 skey_passcheck(authctxt->pw->pw_name, response) != -1);
729
730 xfree(response);
731
732 buffer_clear(m);
733 buffer_put_int(m, authok);
734
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000735 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000736 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
737
738 auth_method = "skey";
739
740 return (authok != 0);
741}
742#endif
743
Damien Miller79418552002-04-23 20:28:48 +1000744#ifdef USE_PAM
745int
746mm_answer_pam_start(int socket, Buffer *m)
747{
748 char *user;
749
750 user = buffer_get_string(m, NULL);
751
752 start_pam(user);
753
754 xfree(user);
755
756 return (0);
757}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000758
759static void *sshpam_ctxt, *sshpam_authok;
760extern KbdintDevice sshpam_device;
761
762int
763mm_answer_pam_init_ctx(int socket, Buffer *m)
764{
765
766 debug3("%s", __func__);
767 authctxt->user = buffer_get_string(m, NULL);
768 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
769 sshpam_authok = NULL;
770 buffer_clear(m);
771 if (sshpam_ctxt != NULL) {
772 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
773 buffer_put_int(m, 1);
774 } else {
775 buffer_put_int(m, 0);
776 }
777 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
778 return (0);
779}
780
781int
782mm_answer_pam_query(int socket, Buffer *m)
783{
784 char *name, *info, **prompts;
785 u_int num, *echo_on;
786 int i, ret;
787
788 debug3("%s", __func__);
789 sshpam_authok = NULL;
790 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
791 if (ret == 0 && num == 0)
792 sshpam_authok = sshpam_ctxt;
793 if (num > 1 || name == NULL || info == NULL)
794 ret = -1;
795 buffer_clear(m);
796 buffer_put_int(m, ret);
797 buffer_put_cstring(m, name);
798 xfree(name);
799 buffer_put_cstring(m, info);
800 xfree(info);
801 buffer_put_int(m, num);
802 for (i = 0; i < num; ++i) {
803 buffer_put_cstring(m, prompts[i]);
804 xfree(prompts[i]);
805 buffer_put_int(m, echo_on[i]);
806 }
807 if (prompts != NULL)
808 xfree(prompts);
809 if (echo_on != NULL)
810 xfree(echo_on);
811 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
812 return (0);
813}
814
815int
816mm_answer_pam_respond(int socket, Buffer *m)
817{
818 char **resp;
819 u_int num;
820 int i, ret;
821
822 debug3("%s", __func__);
823 sshpam_authok = NULL;
824 num = buffer_get_int(m);
825 if (num > 0) {
826 resp = xmalloc(num * sizeof(char *));
827 for (i = 0; i < num; ++i)
828 resp[i] = buffer_get_string(m, NULL);
829 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
830 for (i = 0; i < num; ++i)
831 xfree(resp[i]);
832 xfree(resp);
833 } else {
834 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
835 }
836 buffer_clear(m);
837 buffer_put_int(m, ret);
838 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
839 auth_method = "keyboard-interactive/pam";
840 if (ret == 0)
841 sshpam_authok = sshpam_ctxt;
842 return (0);
843}
844
845int
846mm_answer_pam_free_ctx(int socket, Buffer *m)
847{
848
849 debug3("%s", __func__);
850 (sshpam_device.free_ctx)(sshpam_ctxt);
851 buffer_clear(m);
852 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
853 return (sshpam_authok == sshpam_ctxt);
854}
Damien Miller79418552002-04-23 20:28:48 +1000855#endif
856
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000857static void
858mm_append_debug(Buffer *m)
859{
860 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000861 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000862 buffer_append(m, buffer_ptr(&auth_debug),
863 buffer_len(&auth_debug));
864 buffer_clear(&auth_debug);
865 }
866}
867
868int
869mm_answer_keyallowed(int socket, Buffer *m)
870{
871 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000872 char *cuser, *chost;
873 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000874 u_int bloblen;
875 enum mm_keytype type = 0;
876 int allowed = 0;
877
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000878 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000879
880 type = buffer_get_int(m);
881 cuser = buffer_get_string(m, NULL);
882 chost = buffer_get_string(m, NULL);
883 blob = buffer_get_string(m, &bloblen);
884
885 key = key_from_blob(blob, bloblen);
886
887 if ((compat20 && type == MM_RSAHOSTKEY) ||
888 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000889 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000890
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000891 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000892
893 if (key != NULL && authctxt->pw != NULL) {
894 switch(type) {
895 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000896 allowed = options.pubkey_authentication &&
897 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000898 break;
899 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000900 allowed = options.hostbased_authentication &&
901 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000902 cuser, chost, key);
903 break;
904 case MM_RSAHOSTKEY:
905 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000906 allowed = options.rhosts_rsa_authentication &&
907 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000908 cuser, chost, key);
909 break;
910 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000911 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000912 break;
913 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 }
Damien Miller00111382003-03-10 11:21:17 +1100915 if (key != NULL)
916 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000917
918 /* clear temporarily storage (used by verify) */
919 monitor_reset_key_state();
920
921 if (allowed) {
922 /* Save temporarily for comparison in verify */
923 key_blob = blob;
924 key_bloblen = bloblen;
925 key_blobtype = type;
926 hostbased_cuser = cuser;
927 hostbased_chost = chost;
928 }
929
930 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000931 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000932
933 buffer_clear(m);
934 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +1100935 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000936
937 mm_append_debug(m);
938
939 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
940
941 if (type == MM_RSAHOSTKEY)
942 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
943
944 return (0);
945}
946
947static int
948monitor_valid_userblob(u_char *data, u_int datalen)
949{
950 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +1000951 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000952 u_int len;
953 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954
955 buffer_init(&b);
956 buffer_append(&b, data, datalen);
957
958 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000959 p = buffer_ptr(&b);
960 len = buffer_len(&b);
961 if ((session_id2 == NULL) ||
962 (len < session_id2_len) ||
963 (memcmp(p, session_id2, session_id2_len) != 0))
964 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000965 buffer_consume(&b, session_id2_len);
966 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000967 p = buffer_get_string(&b, &len);
968 if ((session_id2 == NULL) ||
969 (len != session_id2_len) ||
970 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000971 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000972 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000973 }
974 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
975 fail++;
976 p = buffer_get_string(&b, NULL);
977 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000978 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000979 authctxt->user, p);
980 fail++;
981 }
982 xfree(p);
983 buffer_skip_string(&b);
984 if (datafellows & SSH_BUG_PKAUTH) {
985 if (!buffer_get_char(&b))
986 fail++;
987 } else {
988 p = buffer_get_string(&b, NULL);
989 if (strcmp("publickey", p) != 0)
990 fail++;
991 xfree(p);
992 if (!buffer_get_char(&b))
993 fail++;
994 buffer_skip_string(&b);
995 }
996 buffer_skip_string(&b);
997 if (buffer_len(&b) != 0)
998 fail++;
999 buffer_free(&b);
1000 return (fail == 0);
1001}
1002
1003static int
Damien Millera10f5612002-09-12 09:49:15 +10001004monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1005 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006{
1007 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001008 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001009 u_int len;
1010 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001011
1012 buffer_init(&b);
1013 buffer_append(&b, data, datalen);
1014
Ben Lindstromf67e0772002-06-06 20:58:19 +00001015 p = buffer_get_string(&b, &len);
1016 if ((session_id2 == NULL) ||
1017 (len != session_id2_len) ||
1018 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001019 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001020 xfree(p);
1021
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1023 fail++;
1024 p = buffer_get_string(&b, NULL);
1025 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001026 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001027 authctxt->user, p);
1028 fail++;
1029 }
1030 xfree(p);
1031 buffer_skip_string(&b); /* service */
1032 p = buffer_get_string(&b, NULL);
1033 if (strcmp(p, "hostbased") != 0)
1034 fail++;
1035 xfree(p);
1036 buffer_skip_string(&b); /* pkalg */
1037 buffer_skip_string(&b); /* pkblob */
1038
1039 /* verify client host, strip trailing dot if necessary */
1040 p = buffer_get_string(&b, NULL);
1041 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1042 p[len - 1] = '\0';
1043 if (strcmp(p, chost) != 0)
1044 fail++;
1045 xfree(p);
1046
1047 /* verify client user */
1048 p = buffer_get_string(&b, NULL);
1049 if (strcmp(p, cuser) != 0)
1050 fail++;
1051 xfree(p);
1052
1053 if (buffer_len(&b) != 0)
1054 fail++;
1055 buffer_free(&b);
1056 return (fail == 0);
1057}
1058
1059int
1060mm_answer_keyverify(int socket, Buffer *m)
1061{
1062 Key *key;
1063 u_char *signature, *data, *blob;
1064 u_int signaturelen, datalen, bloblen;
1065 int verified = 0;
1066 int valid_data = 0;
1067
1068 blob = buffer_get_string(m, &bloblen);
1069 signature = buffer_get_string(m, &signaturelen);
1070 data = buffer_get_string(m, &datalen);
1071
1072 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001073 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001074 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001075
1076 key = key_from_blob(blob, bloblen);
1077 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001078 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001079
1080 switch (key_blobtype) {
1081 case MM_USERKEY:
1082 valid_data = monitor_valid_userblob(data, datalen);
1083 break;
1084 case MM_HOSTKEY:
1085 valid_data = monitor_valid_hostbasedblob(data, datalen,
1086 hostbased_cuser, hostbased_chost);
1087 break;
1088 default:
1089 valid_data = 0;
1090 break;
1091 }
1092 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001093 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001094
1095 verified = key_verify(key, signature, signaturelen, data, datalen);
1096 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001097 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001098
1099 key_free(key);
1100 xfree(blob);
1101 xfree(signature);
1102 xfree(data);
1103
Ben Lindstrome1c09122002-06-23 00:38:24 +00001104 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1105
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001106 monitor_reset_key_state();
1107
1108 buffer_clear(m);
1109 buffer_put_int(m, verified);
1110 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1111
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001112 return (verified);
1113}
1114
1115static void
1116mm_record_login(Session *s, struct passwd *pw)
1117{
1118 socklen_t fromlen;
1119 struct sockaddr_storage from;
1120
1121 /*
1122 * Get IP address of client. If the connection is not a socket, let
1123 * the address be 0.0.0.0.
1124 */
1125 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001126 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001128 if (getpeername(packet_get_connection_in(),
1129 (struct sockaddr *) & from, &fromlen) < 0) {
1130 debug("getpeername: %.100s", strerror(errno));
1131 fatal_cleanup();
1132 }
1133 }
1134 /* Record that there was a login on that tty from the remote host. */
1135 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1136 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
Damien Millerebc23062002-09-04 16:45:09 +10001137 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001138}
1139
1140static void
1141mm_session_close(Session *s)
1142{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001143 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001145 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1147 session_pty_cleanup2(s);
1148 }
1149 s->used = 0;
1150}
1151
1152int
1153mm_answer_pty(int socket, Buffer *m)
1154{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001155 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001156 Session *s;
1157 int res, fd0;
1158
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001159 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001160
1161 buffer_clear(m);
1162 s = session_new();
1163 if (s == NULL)
1164 goto error;
1165 s->authctxt = authctxt;
1166 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001167 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1169 if (res == 0)
1170 goto error;
1171 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1172 pty_setowner(authctxt->pw, s->tty);
1173
1174 buffer_put_int(m, 1);
1175 buffer_put_cstring(m, s->tty);
1176 mm_request_send(socket, MONITOR_ANS_PTY, m);
1177
1178 mm_send_fd(socket, s->ptyfd);
1179 mm_send_fd(socket, s->ttyfd);
1180
1181 /* We need to trick ttyslot */
1182 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001183 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184
1185 mm_record_login(s, authctxt->pw);
1186
1187 /* Now we can close the file descriptor again */
1188 close(0);
1189
1190 /* make sure nothing uses fd 0 */
1191 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001192 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001193 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001194 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001195
1196 /* slave is not needed */
1197 close(s->ttyfd);
1198 s->ttyfd = s->ptyfd;
1199 /* no need to dup() because nobody closes ptyfd */
1200 s->ptymaster = s->ptyfd;
1201
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001202 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001203
1204 return (0);
1205
1206 error:
1207 if (s != NULL)
1208 mm_session_close(s);
1209 buffer_put_int(m, 0);
1210 mm_request_send(socket, MONITOR_ANS_PTY, m);
1211 return (0);
1212}
1213
1214int
1215mm_answer_pty_cleanup(int socket, Buffer *m)
1216{
1217 Session *s;
1218 char *tty;
1219
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001220 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001221
1222 tty = buffer_get_string(m, NULL);
1223 if ((s = session_by_tty(tty)) != NULL)
1224 mm_session_close(s);
1225 buffer_clear(m);
1226 xfree(tty);
1227 return (0);
1228}
1229
1230int
1231mm_answer_sesskey(int socket, Buffer *m)
1232{
1233 BIGNUM *p;
1234 int rsafail;
1235
1236 /* Turn off permissions */
1237 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1238
1239 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001240 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001241
1242 buffer_get_bignum2(m, p);
1243
1244 rsafail = ssh1_session_key(p);
1245
1246 buffer_clear(m);
1247 buffer_put_int(m, rsafail);
1248 buffer_put_bignum2(m, p);
1249
1250 BN_clear_free(p);
1251
1252 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1253
1254 /* Turn on permissions for sessid passing */
1255 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1256
1257 return (0);
1258}
1259
1260int
1261mm_answer_sessid(int socket, Buffer *m)
1262{
1263 int i;
1264
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001265 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001266
1267 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001268 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001269 for (i = 0; i < 16; i++)
1270 session_id[i] = buffer_get_char(m);
1271
1272 /* Turn on permissions for getpwnam */
1273 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1274
1275 return (0);
1276}
1277
1278int
1279mm_answer_rsa_keyallowed(int socket, Buffer *m)
1280{
1281 BIGNUM *client_n;
1282 Key *key = NULL;
1283 u_char *blob = NULL;
1284 u_int blen = 0;
1285 int allowed = 0;
1286
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001287 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001288
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001289 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001290 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001291 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001292 buffer_get_bignum2(m, client_n);
1293 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1294 BN_clear_free(client_n);
1295 }
1296 buffer_clear(m);
1297 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001298 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001299
1300 /* clear temporarily storage (used by generate challenge) */
1301 monitor_reset_key_state();
1302
1303 if (allowed && key != NULL) {
1304 key->type = KEY_RSA; /* cheat for key_to_blob */
1305 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001306 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 buffer_put_string(m, blob, blen);
1308
1309 /* Save temporarily for comparison in verify */
1310 key_blob = blob;
1311 key_bloblen = blen;
1312 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001313 }
Damien Miller00111382003-03-10 11:21:17 +11001314 if (key != NULL)
1315 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001316
1317 mm_append_debug(m);
1318
1319 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1320
1321 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1322 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1323 return (0);
1324}
1325
1326int
1327mm_answer_rsa_challenge(int socket, Buffer *m)
1328{
1329 Key *key = NULL;
1330 u_char *blob;
1331 u_int blen;
1332
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001333 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334
1335 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001336 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001337 blob = buffer_get_string(m, &blen);
1338 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001339 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001341 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001343 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344
1345 if (ssh1_challenge)
1346 BN_clear_free(ssh1_challenge);
1347 ssh1_challenge = auth_rsa_generate_challenge(key);
1348
1349 buffer_clear(m);
1350 buffer_put_bignum2(m, ssh1_challenge);
1351
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001352 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001353 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1354
1355 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001356
1357 xfree(blob);
1358 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359 return (0);
1360}
1361
1362int
1363mm_answer_rsa_response(int socket, Buffer *m)
1364{
1365 Key *key = NULL;
1366 u_char *blob, *response;
1367 u_int blen, len;
1368 int success;
1369
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001370 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001371
1372 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001373 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001374 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001375 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001376
1377 blob = buffer_get_string(m, &blen);
1378 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001379 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001380 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001381 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001382 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001383 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001384 response = buffer_get_string(m, &len);
1385 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001386 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001387 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1388
Damien Miller00111382003-03-10 11:21:17 +11001389 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001390 key_free(key);
1391 xfree(response);
1392
1393 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1394
1395 /* reset state */
1396 BN_clear_free(ssh1_challenge);
1397 ssh1_challenge = NULL;
1398 monitor_reset_key_state();
1399
1400 buffer_clear(m);
1401 buffer_put_int(m, success);
1402 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1403
1404 return (success);
1405}
1406
Damien Millerd94e5492002-09-27 13:25:58 +10001407#ifdef KRB4
1408int
1409mm_answer_krb4(int socket, Buffer *m)
1410{
1411 KTEXT_ST auth, reply;
1412 char *client, *p;
1413 int success;
1414 u_int alen;
1415
1416 reply.length = auth.length = 0;
1417
1418 p = buffer_get_string(m, &alen);
1419 if (alen >= MAX_KTXT_LEN)
1420 fatal("%s: auth too large", __func__);
1421 memcpy(auth.dat, p, alen);
1422 auth.length = alen;
1423 memset(p, 0, alen);
1424 xfree(p);
1425
1426 success = options.kerberos_authentication &&
1427 authctxt->valid &&
1428 auth_krb4(authctxt, &auth, &client, &reply);
1429
1430 memset(auth.dat, 0, alen);
1431 buffer_clear(m);
1432 buffer_put_int(m, success);
1433
1434 if (success) {
1435 buffer_put_cstring(m, client);
1436 buffer_put_string(m, reply.dat, reply.length);
1437 if (client)
1438 xfree(client);
1439 if (reply.length)
1440 memset(reply.dat, 0, reply.length);
1441 }
1442
1443 debug3("%s: sending result %d", __func__, success);
1444 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1445
1446 auth_method = "kerberos";
1447
1448 /* Causes monitor loop to terminate if authenticated */
1449 return (success);
1450}
1451#endif
Damien Miller25162f22002-09-12 09:47:29 +10001452
1453#ifdef KRB5
1454int
1455mm_answer_krb5(int socket, Buffer *m)
1456{
1457 krb5_data tkt, reply;
1458 char *client_user;
1459 u_int len;
1460 int success;
1461
1462 /* use temporary var to avoid size issues on 64bit arch */
1463 tkt.data = buffer_get_string(m, &len);
1464 tkt.length = len;
1465
Damien Milleref73f502002-09-25 12:20:17 +10001466 success = options.kerberos_authentication &&
1467 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001468 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001469
1470 if (tkt.length)
1471 xfree(tkt.data);
1472
1473 buffer_clear(m);
1474 buffer_put_int(m, success);
1475
1476 if (success) {
1477 buffer_put_cstring(m, client_user);
1478 buffer_put_string(m, reply.data, reply.length);
1479 if (client_user)
1480 xfree(client_user);
1481 if (reply.length)
1482 xfree(reply.data);
1483 }
1484 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1485
1486 return success;
1487}
1488#endif
1489
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001490int
1491mm_answer_term(int socket, Buffer *req)
1492{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001493 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001494 int res, status;
1495
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001496 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001497
1498 /* The child is terminating */
1499 session_destroy_all(&mm_session_close);
1500
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001501 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001502 if (errno != EINTR)
1503 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001504
1505 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1506
1507 /* Terminate process */
1508 exit (res);
1509}
1510
1511void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001512monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001513{
1514 if (compat20) {
1515 set_newkeys(MODE_IN);
1516 set_newkeys(MODE_OUT);
1517 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001518 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001519 packet_set_encryption_key(child_state.ssh1key,
1520 child_state.ssh1keylen, child_state.ssh1cipher);
1521 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001522 }
1523
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001524 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525 packet_set_keycontext(MODE_OUT, child_state.keyout);
1526 xfree(child_state.keyout);
1527 packet_set_keycontext(MODE_IN, child_state.keyin);
1528 xfree(child_state.keyin);
1529
1530 if (!compat20) {
1531 packet_set_iv(MODE_OUT, child_state.ivout);
1532 xfree(child_state.ivout);
1533 packet_set_iv(MODE_IN, child_state.ivin);
1534 xfree(child_state.ivin);
1535 }
1536
1537 memcpy(&incoming_stream, &child_state.incoming,
1538 sizeof(incoming_stream));
1539 memcpy(&outgoing_stream, &child_state.outgoing,
1540 sizeof(outgoing_stream));
1541
1542 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001543 if (options.compression)
1544 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001545
1546 /* Network I/O buffers */
1547 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1548 buffer_clear(&input);
1549 buffer_append(&input, child_state.input, child_state.ilen);
1550 memset(child_state.input, 0, child_state.ilen);
1551 xfree(child_state.input);
1552
1553 buffer_clear(&output);
1554 buffer_append(&output, child_state.output, child_state.olen);
1555 memset(child_state.output, 0, child_state.olen);
1556 xfree(child_state.output);
1557}
1558
1559static Kex *
1560mm_get_kex(Buffer *m)
1561{
1562 Kex *kex;
1563 void *blob;
1564 u_int bloblen;
1565
1566 kex = xmalloc(sizeof(*kex));
1567 memset(kex, 0, sizeof(*kex));
1568 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001569 if ((session_id2 == NULL) ||
1570 (kex->session_id_len != session_id2_len) ||
1571 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1572 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001573 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001574 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1575 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001576 kex->server = 1;
1577 kex->hostkey_type = buffer_get_int(m);
1578 kex->kex_type = buffer_get_int(m);
1579 blob = buffer_get_string(m, &bloblen);
1580 buffer_init(&kex->my);
1581 buffer_append(&kex->my, blob, bloblen);
1582 xfree(blob);
1583 blob = buffer_get_string(m, &bloblen);
1584 buffer_init(&kex->peer);
1585 buffer_append(&kex->peer, blob, bloblen);
1586 xfree(blob);
1587 kex->done = 1;
1588 kex->flags = buffer_get_int(m);
1589 kex->client_version_string = buffer_get_string(m, NULL);
1590 kex->server_version_string = buffer_get_string(m, NULL);
1591 kex->load_host_key=&get_hostkey_by_type;
1592 kex->host_key_index=&get_hostkey_index;
1593
1594 return (kex);
1595}
1596
1597/* This function requries careful sanity checking */
1598
1599void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001600mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001601{
1602 Buffer m;
1603 u_char *blob, *p;
1604 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001605 u_int32_t seqnr, packets;
1606 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001607
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001608 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609
1610 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001611 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001612 if (!compat20) {
1613 child_state.ssh1protoflags = buffer_get_int(&m);
1614 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001615 child_state.ssh1key = buffer_get_string(&m,
1616 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617 child_state.ivout = buffer_get_string(&m,
1618 &child_state.ivoutlen);
1619 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1620 goto skip;
1621 } else {
1622 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001623 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001624 }
1625
1626 blob = buffer_get_string(&m, &bloblen);
1627 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1628 xfree(blob);
1629
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001630 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001631 blob = buffer_get_string(&m, &bloblen);
1632 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1633 xfree(blob);
1634
1635 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001636 seqnr = buffer_get_int(&m);
1637 blocks = buffer_get_int64(&m);
1638 packets = buffer_get_int(&m);
1639 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1640 seqnr = buffer_get_int(&m);
1641 blocks = buffer_get_int64(&m);
1642 packets = buffer_get_int(&m);
1643 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001644
1645 skip:
1646 /* Get the key context */
1647 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1648 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1649
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001650 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001651 /* Get compression state */
1652 p = buffer_get_string(&m, &plen);
1653 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001654 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001655 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1656 xfree(p);
1657
1658 p = buffer_get_string(&m, &plen);
1659 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001660 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001661 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1662 xfree(p);
1663
1664 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001665 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001666 child_state.input = buffer_get_string(&m, &child_state.ilen);
1667 child_state.output = buffer_get_string(&m, &child_state.olen);
1668
1669 buffer_free(&m);
1670}
1671
1672
1673/* Allocation functions for zlib */
1674void *
1675mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1676{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001677 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001678 void *address;
1679
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001680 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001681 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1682
1683 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001684
1685 return (address);
1686}
1687
1688void
1689mm_zfree(struct mm_master *mm, void *address)
1690{
1691 mm_free(mm, address);
1692}
1693
1694void
1695mm_init_compression(struct mm_master *mm)
1696{
1697 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1698 outgoing_stream.zfree = (free_func)mm_zfree;
1699 outgoing_stream.opaque = mm;
1700
1701 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1702 incoming_stream.zfree = (free_func)mm_zfree;
1703 incoming_stream.opaque = mm;
1704}
1705
1706/* XXX */
1707
1708#define FD_CLOSEONEXEC(x) do { \
1709 if (fcntl(x, F_SETFD, 1) == -1) \
1710 fatal("fcntl(%d, F_SETFD)", x); \
1711} while (0)
1712
1713static void
1714monitor_socketpair(int *pair)
1715{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001716#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001717 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001718 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001719#else
1720 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001721 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001722#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001723 FD_CLOSEONEXEC(pair[0]);
1724 FD_CLOSEONEXEC(pair[1]);
1725}
1726
1727#define MM_MEMSIZE 65536
1728
1729struct monitor *
1730monitor_init(void)
1731{
1732 struct monitor *mon;
1733 int pair[2];
1734
1735 mon = xmalloc(sizeof(*mon));
1736
1737 monitor_socketpair(pair);
1738
1739 mon->m_recvfd = pair[0];
1740 mon->m_sendfd = pair[1];
1741
1742 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001743 if (options.compression) {
1744 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1745 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001746
Damien Miller2d6b8332002-06-21 15:59:49 +10001747 /* Compression needs to share state across borders */
1748 mm_init_compression(mon->m_zlib);
1749 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001750
1751 return mon;
1752}
1753
1754void
1755monitor_reinit(struct monitor *mon)
1756{
1757 int pair[2];
1758
1759 monitor_socketpair(pair);
1760
1761 mon->m_recvfd = pair[0];
1762 mon->m_sendfd = pair[1];
1763}