blob: 80b1a8fba291ca86b3a590b776792dc48748c246 [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"
Darren Tucker6aaa58c2003-08-02 22:24:49 +100028RCSID("$OpenBSD: monitor.c,v 1.45 2003/07/22 13:35:22 markus Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
40#include "zlib.h"
41#include "packet.h"
42#include "auth-options.h"
43#include "sshpty.h"
44#include "channels.h"
45#include "session.h"
46#include "sshlogin.h"
47#include "canohost.h"
48#include "log.h"
49#include "servconf.h"
50#include "monitor.h"
51#include "monitor_mm.h"
52#include "monitor_wrap.h"
53#include "monitor_fdpass.h"
54#include "xmalloc.h"
55#include "misc.h"
56#include "buffer.h"
57#include "bufaux.h"
58#include "compat.h"
59#include "ssh2.h"
60#include "mpaux.h"
61
62/* Imports */
63extern ServerOptions options;
64extern u_int utmp_len;
65extern Newkeys *current_keys[];
66extern z_stream incoming_stream;
67extern z_stream outgoing_stream;
68extern u_char session_id[];
69extern Buffer input, output;
70extern Buffer auth_debug;
71extern int auth_debug_init;
72
73/* State exported from the child */
74
75struct {
76 z_stream incoming;
77 z_stream outgoing;
78 u_char *keyin;
79 u_int keyinlen;
80 u_char *keyout;
81 u_int keyoutlen;
82 u_char *ivin;
83 u_int ivinlen;
84 u_char *ivout;
85 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000086 u_char *ssh1key;
87 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 int ssh1cipher;
89 int ssh1protoflags;
90 u_char *input;
91 u_int ilen;
92 u_char *output;
93 u_int olen;
94} child_state;
95
Damien Miller469954d2003-06-18 20:25:33 +100096/* Functions on the monitor that answer unprivileged requests */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000097
98int mm_answer_moduli(int, Buffer *);
99int mm_answer_sign(int, Buffer *);
100int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000101int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102int mm_answer_authserv(int, Buffer *);
103int mm_answer_authpassword(int, Buffer *);
104int mm_answer_bsdauthquery(int, Buffer *);
105int mm_answer_bsdauthrespond(int, Buffer *);
106int mm_answer_skeyquery(int, Buffer *);
107int mm_answer_skeyrespond(int, Buffer *);
108int mm_answer_keyallowed(int, Buffer *);
109int mm_answer_keyverify(int, Buffer *);
110int mm_answer_pty(int, Buffer *);
111int mm_answer_pty_cleanup(int, Buffer *);
112int mm_answer_term(int, Buffer *);
113int mm_answer_rsa_keyallowed(int, Buffer *);
114int mm_answer_rsa_challenge(int, Buffer *);
115int mm_answer_rsa_response(int, Buffer *);
116int mm_answer_sesskey(int, Buffer *);
117int mm_answer_sessid(int, Buffer *);
118
Damien Miller79418552002-04-23 20:28:48 +1000119#ifdef USE_PAM
120int mm_answer_pam_start(int, Buffer *);
Damien Miller1f499fd2003-08-25 13:08:49 +1000121int mm_answer_pam_account(int, Buffer *);
Damien Miller4f9f42a2003-05-10 19:28:02 +1000122int mm_answer_pam_init_ctx(int, Buffer *);
123int mm_answer_pam_query(int, Buffer *);
124int mm_answer_pam_respond(int, Buffer *);
125int mm_answer_pam_free_ctx(int, Buffer *);
Damien Miller79418552002-04-23 20:28:48 +1000126#endif
127
Damien Miller25162f22002-09-12 09:47:29 +1000128#ifdef KRB5
129int mm_answer_krb5(int, Buffer *);
130#endif
131
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000132static Authctxt *authctxt;
133static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
134
135/* local state for key verify */
136static u_char *key_blob = NULL;
137static u_int key_bloblen = 0;
138static int key_blobtype = MM_NOKEY;
Damien Millera10f5612002-09-12 09:49:15 +1000139static char *hostbased_cuser = NULL;
140static char *hostbased_chost = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000141static char *auth_method = "unknown";
Darren Tucker502d3842003-06-28 12:38:01 +1000142static u_int session_id2_len = 0;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000143static u_char *session_id2 = NULL;
Damien Millerbe64d432003-05-14 19:31:12 +1000144static pid_t monitor_child_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000145
146struct mon_table {
147 enum monitor_reqtype type;
148 int flags;
149 int (*f)(int, Buffer *);
150};
151
152#define MON_ISAUTH 0x0004 /* Required for Authentication */
153#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
154#define MON_ONCE 0x0010 /* Disable after calling */
155
156#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
157
158#define MON_PERMIT 0x1000 /* Request is permitted */
159
160struct mon_table mon_dispatch_proto20[] = {
161 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
162 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
163 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
164 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000165 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000166 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000167#ifdef USE_PAM
168 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Damien Miller1f499fd2003-08-25 13:08:49 +1000169 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
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 Miller1f499fd2003-08-25 13:08:49 +1000219 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
Damien Miller4f9f42a2003-05-10 19:28:02 +1000220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
Damien Millera33501b2002-05-08 12:24:42 +1000224#endif
Damien Miller25162f22002-09-12 09:47:29 +1000225#ifdef KRB5
226 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
227#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000228 {0, 0, NULL}
229};
230
231struct mon_table mon_dispatch_postauth15[] = {
232 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
233 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
234 {MONITOR_REQ_TERM, 0, mm_answer_term},
235 {0, 0, NULL}
236};
237
238struct mon_table *mon_dispatch;
239
240/* Specifies if a certain message is allowed at the moment */
241
242static void
243monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
244{
245 while (ent->f != NULL) {
246 if (ent->type == type) {
247 ent->flags &= ~MON_PERMIT;
248 ent->flags |= permit ? MON_PERMIT : 0;
249 return;
250 }
251 ent++;
252 }
253}
254
255static void
256monitor_permit_authentications(int permit)
257{
258 struct mon_table *ent = mon_dispatch;
259
260 while (ent->f != NULL) {
261 if (ent->flags & MON_AUTH) {
262 ent->flags &= ~MON_PERMIT;
263 ent->flags |= permit ? MON_PERMIT : 0;
264 }
265 ent++;
266 }
267}
268
269Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000270monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000271{
272 struct mon_table *ent;
273 int authenticated = 0;
274
275 debug3("preauth child monitor started");
276
277 if (compat20) {
278 mon_dispatch = mon_dispatch_proto20;
279
280 /* Permit requests for moduli and signatures */
281 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
282 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
283 } else {
284 mon_dispatch = mon_dispatch_proto15;
285
286 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
287 }
288
289 authctxt = authctxt_new();
290
291 /* The first few requests do not require asynchronous access */
292 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000293 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294 if (authenticated) {
295 if (!(ent->flags & MON_AUTHDECIDE))
296 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000297 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000298 if (authctxt->pw->pw_uid == 0 &&
299 !auth_root_allowed(auth_method))
300 authenticated = 0;
Damien Miller1f499fd2003-08-25 13:08:49 +1000301#ifdef USE_PAM
302 /* PAM needs to perform account checks after auth */
303 if (options.use_pam) {
304 Buffer m;
305
306 buffer_init(&m);
307 mm_request_receive_expect(pmonitor->m_sendfd,
308 MONITOR_REQ_PAM_ACCOUNT, &m);
309 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
310 buffer_free(&m);
311 }
312#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000313 }
314
315 if (ent->flags & MON_AUTHDECIDE) {
316 auth_log(authctxt, authenticated, auth_method,
317 compat20 ? " ssh2" : "");
318 if (!authenticated)
319 authctxt->failures++;
320 }
321 }
322
323 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000324 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000325
326 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000327 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000328
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000329 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000330
331 return (authctxt);
332}
333
Damien Millerbe64d432003-05-14 19:31:12 +1000334static void
335monitor_set_child_handler(pid_t pid)
336{
337 monitor_child_pid = pid;
338}
339
340static void
341monitor_child_handler(int signal)
342{
343 kill(monitor_child_pid, signal);
344}
345
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000346void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000347monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000348{
Damien Millerbe64d432003-05-14 19:31:12 +1000349 monitor_set_child_handler(pmonitor->m_pid);
350 signal(SIGHUP, &monitor_child_handler);
351 signal(SIGTERM, &monitor_child_handler);
352
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000353 if (compat20) {
354 mon_dispatch = mon_dispatch_postauth20;
355
356 /* Permit requests for moduli and signatures */
357 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
358 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
359 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
360
361 } else {
362 mon_dispatch = mon_dispatch_postauth15;
363 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
364 }
365 if (!no_pty_flag) {
366 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
367 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
368 }
369
370 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000371 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000372}
373
374void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000375monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000376{
Damien Miller2d6b8332002-06-21 15:59:49 +1000377 if (options.compression) {
378 /* The member allocation is not visible, so sync it */
379 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
380 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000381}
382
383int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000384monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000385 struct mon_table **pent)
386{
387 Buffer m;
388 int ret;
389 u_char type;
390
391 buffer_init(&m);
392
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000393 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000394 type = buffer_get_char(&m);
395
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000396 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000397
398 while (ent->f != NULL) {
399 if (ent->type == type)
400 break;
401 ent++;
402 }
403
404 if (ent->f != NULL) {
405 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000406 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000407 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000408 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000409 buffer_free(&m);
410
411 /* The child may use this request only once, disable it */
412 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000413 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000414 type);
415 ent->flags &= ~MON_PERMIT;
416 }
417
418 if (pent != NULL)
419 *pent = ent;
420
421 return ret;
422 }
423
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000424 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000425
426 /* NOTREACHED */
427 return (-1);
428}
429
430/* allowed key state */
431static int
432monitor_allowed_key(u_char *blob, u_int bloblen)
433{
434 /* make sure key is allowed */
435 if (key_blob == NULL || key_bloblen != bloblen ||
436 memcmp(key_blob, blob, key_bloblen))
437 return (0);
438 return (1);
439}
440
441static void
442monitor_reset_key_state(void)
443{
444 /* reset state */
445 if (key_blob != NULL)
446 xfree(key_blob);
447 if (hostbased_cuser != NULL)
448 xfree(hostbased_cuser);
449 if (hostbased_chost != NULL)
450 xfree(hostbased_chost);
451 key_blob = NULL;
452 key_bloblen = 0;
453 key_blobtype = MM_NOKEY;
454 hostbased_cuser = NULL;
455 hostbased_chost = NULL;
456}
457
458int
459mm_answer_moduli(int socket, Buffer *m)
460{
461 DH *dh;
462 int min, want, max;
463
464 min = buffer_get_int(m);
465 want = buffer_get_int(m);
466 max = buffer_get_int(m);
467
468 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000469 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000470 /* We need to check here, too, in case the child got corrupted */
471 if (max < min || want < min || max < want)
472 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000473 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000474
475 buffer_clear(m);
476
477 dh = choose_dh(min, want, max);
478 if (dh == NULL) {
479 buffer_put_char(m, 0);
480 return (0);
481 } else {
482 /* Send first bignum */
483 buffer_put_char(m, 1);
484 buffer_put_bignum2(m, dh->p);
485 buffer_put_bignum2(m, dh->g);
486
487 DH_free(dh);
488 }
489 mm_request_send(socket, MONITOR_ANS_MODULI, m);
490 return (0);
491}
492
493int
494mm_answer_sign(int socket, Buffer *m)
495{
496 Key *key;
497 u_char *p;
498 u_char *signature;
499 u_int siglen, datlen;
500 int keyid;
501
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000502 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000503
504 keyid = buffer_get_int(m);
505 p = buffer_get_string(m, &datlen);
506
507 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000508 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000509
Ben Lindstromf67e0772002-06-06 20:58:19 +0000510 /* save session id, it will be passed on the first call */
511 if (session_id2_len == 0) {
512 session_id2_len = datlen;
513 session_id2 = xmalloc(session_id2_len);
514 memcpy(session_id2, p, session_id2_len);
515 }
516
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000517 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000518 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000519 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000520 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000521
Ben Lindstromd5502182002-06-27 00:12:57 +0000522 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000523
524 buffer_clear(m);
525 buffer_put_string(m, signature, siglen);
526
527 xfree(p);
528 xfree(signature);
529
530 mm_request_send(socket, MONITOR_ANS_SIGN, m);
531
532 /* Turn on permissions for getpwnam */
533 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
534
535 return (0);
536}
537
538/* Retrieves the password entry and also checks if the user is permitted */
539
540int
541mm_answer_pwnamallow(int socket, Buffer *m)
542{
543 char *login;
544 struct passwd *pwent;
545 int allowed = 0;
546
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000547 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548
549 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000550 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000551
552 login = buffer_get_string(m, NULL);
553
554 pwent = getpwnamallow(login);
555
556 authctxt->user = xstrdup(login);
557 setproctitle("%s [priv]", pwent ? login : "unknown");
558 xfree(login);
559
560 buffer_clear(m);
561
562 if (pwent == NULL) {
563 buffer_put_char(m, 0);
564 goto out;
565 }
566
567 allowed = 1;
568 authctxt->pw = pwent;
569 authctxt->valid = 1;
570
571 buffer_put_char(m, 1);
572 buffer_put_string(m, pwent, sizeof(struct passwd));
573 buffer_put_cstring(m, pwent->pw_name);
574 buffer_put_cstring(m, "*");
575 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000576#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000577 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000578#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000579 buffer_put_cstring(m, pwent->pw_dir);
580 buffer_put_cstring(m, pwent->pw_shell);
581
582 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000583 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000584 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
585
586 /* For SSHv1 allow authentication now */
587 if (!compat20)
588 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000589 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000590 /* Allow service/style information on the auth context */
591 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000592 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
593 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000594
Damien Millera33501b2002-05-08 12:24:42 +1000595#ifdef USE_PAM
Damien Miller4e448a32003-05-14 15:11:48 +1000596 if (options.use_pam)
597 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
Damien Millera33501b2002-05-08 12:24:42 +1000598#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000599
600 return (0);
601}
602
Damien Miller5ad9fd92002-05-13 11:07:41 +1000603int mm_answer_auth2_read_banner(int socket, Buffer *m)
604{
605 char *banner;
606
607 buffer_clear(m);
608 banner = auth2_read_banner();
609 buffer_put_cstring(m, banner != NULL ? banner : "");
610 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
611
612 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000613 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000614
615 return (0);
616}
617
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000618int
619mm_answer_authserv(int socket, Buffer *m)
620{
621 monitor_permit_authentications(1);
622
623 authctxt->service = buffer_get_string(m, NULL);
624 authctxt->style = buffer_get_string(m, NULL);
625 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000626 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000627
628 if (strlen(authctxt->style) == 0) {
629 xfree(authctxt->style);
630 authctxt->style = NULL;
631 }
632
633 return (0);
634}
635
636int
637mm_answer_authpassword(int socket, Buffer *m)
638{
639 static int call_count;
640 char *passwd;
Ben Lindstrom7cea16b2002-07-23 21:13:40 +0000641 int authenticated;
642 u_int plen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000643
644 passwd = buffer_get_string(m, &plen);
645 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000646 authenticated = options.password_authentication &&
Damien Millereab4bae2003-04-29 23:22:40 +1000647 auth_password(authctxt, passwd) && authctxt->valid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000648 memset(passwd, 0, strlen(passwd));
649 xfree(passwd);
650
651 buffer_clear(m);
652 buffer_put_int(m, authenticated);
653
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000654 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000655 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
656
657 call_count++;
658 if (plen == 0 && call_count == 1)
659 auth_method = "none";
660 else
661 auth_method = "password";
662
663 /* Causes monitor loop to terminate if authenticated */
664 return (authenticated);
665}
666
667#ifdef BSD_AUTH
668int
669mm_answer_bsdauthquery(int socket, Buffer *m)
670{
671 char *name, *infotxt;
672 u_int numprompts;
673 u_int *echo_on;
674 char **prompts;
Damien Millerb7df3af2003-02-24 11:55:46 +1100675 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000676
Damien Millerb7df3af2003-02-24 11:55:46 +1100677 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
678 &prompts, &echo_on) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000679
680 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100681 buffer_put_int(m, success);
682 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000683 buffer_put_cstring(m, prompts[0]);
684
Damien Millerb7df3af2003-02-24 11:55:46 +1100685 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000686 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
687
Damien Millerb7df3af2003-02-24 11:55:46 +1100688 if (success) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000689 xfree(name);
690 xfree(infotxt);
691 xfree(prompts);
692 xfree(echo_on);
693 }
694
695 return (0);
696}
697
698int
699mm_answer_bsdauthrespond(int socket, Buffer *m)
700{
701 char *response;
702 int authok;
703
704 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000705 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000706
707 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000708 authok = options.challenge_response_authentication &&
709 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000710 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000711 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000712 xfree(response);
713
714 buffer_clear(m);
715 buffer_put_int(m, authok);
716
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000717 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000718 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
719
720 auth_method = "bsdauth";
721
722 return (authok != 0);
723}
724#endif
725
726#ifdef SKEY
727int
728mm_answer_skeyquery(int socket, Buffer *m)
729{
730 struct skey skey;
731 char challenge[1024];
Damien Millerb7df3af2003-02-24 11:55:46 +1100732 u_int success;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000733
Damien Millerb7df3af2003-02-24 11:55:46 +1100734 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000735
736 buffer_clear(m);
Damien Millerb7df3af2003-02-24 11:55:46 +1100737 buffer_put_int(m, success);
738 if (success)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000739 buffer_put_cstring(m, challenge);
740
Damien Millerb7df3af2003-02-24 11:55:46 +1100741 debug3("%s: sending challenge success: %u", __func__, success);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000742 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
743
744 return (0);
745}
746
747int
748mm_answer_skeyrespond(int socket, Buffer *m)
749{
750 char *response;
751 int authok;
752
753 response = buffer_get_string(m, NULL);
754
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000755 authok = (options.challenge_response_authentication &&
756 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000757 skey_haskey(authctxt->pw->pw_name) == 0 &&
758 skey_passcheck(authctxt->pw->pw_name, response) != -1);
759
760 xfree(response);
761
762 buffer_clear(m);
763 buffer_put_int(m, authok);
764
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000765 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000766 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
767
768 auth_method = "skey";
769
770 return (authok != 0);
771}
772#endif
773
Damien Miller79418552002-04-23 20:28:48 +1000774#ifdef USE_PAM
775int
776mm_answer_pam_start(int socket, Buffer *m)
777{
778 char *user;
779
Damien Miller4e448a32003-05-14 15:11:48 +1000780 if (!options.use_pam)
781 fatal("UsePAM not set, but ended up in %s anyway", __func__);
782
Damien Miller79418552002-04-23 20:28:48 +1000783 user = buffer_get_string(m, NULL);
784
785 start_pam(user);
786
787 xfree(user);
788
Damien Miller1f499fd2003-08-25 13:08:49 +1000789 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
790
Damien Miller79418552002-04-23 20:28:48 +1000791 return (0);
792}
Damien Miller4f9f42a2003-05-10 19:28:02 +1000793
Damien Miller1f499fd2003-08-25 13:08:49 +1000794int
795mm_answer_pam_account(int socket, Buffer *m)
796{
797 u_int ret;
798
799 if (!options.use_pam)
800 fatal("UsePAM not set, but ended up in %s anyway", __func__);
801
802 ret = do_pam_account();
803
804 buffer_put_int(m, ret);
805
806 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
807
808 return (ret);
809}
810
Damien Miller4f9f42a2003-05-10 19:28:02 +1000811static void *sshpam_ctxt, *sshpam_authok;
812extern KbdintDevice sshpam_device;
813
814int
815mm_answer_pam_init_ctx(int socket, Buffer *m)
816{
817
818 debug3("%s", __func__);
819 authctxt->user = buffer_get_string(m, NULL);
820 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
821 sshpam_authok = NULL;
822 buffer_clear(m);
823 if (sshpam_ctxt != NULL) {
824 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
825 buffer_put_int(m, 1);
826 } else {
827 buffer_put_int(m, 0);
828 }
829 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
830 return (0);
831}
832
833int
834mm_answer_pam_query(int socket, Buffer *m)
835{
836 char *name, *info, **prompts;
837 u_int num, *echo_on;
838 int i, ret;
839
840 debug3("%s", __func__);
841 sshpam_authok = NULL;
842 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
843 if (ret == 0 && num == 0)
844 sshpam_authok = sshpam_ctxt;
845 if (num > 1 || name == NULL || info == NULL)
846 ret = -1;
847 buffer_clear(m);
848 buffer_put_int(m, ret);
849 buffer_put_cstring(m, name);
850 xfree(name);
851 buffer_put_cstring(m, info);
852 xfree(info);
853 buffer_put_int(m, num);
854 for (i = 0; i < num; ++i) {
855 buffer_put_cstring(m, prompts[i]);
856 xfree(prompts[i]);
857 buffer_put_int(m, echo_on[i]);
858 }
859 if (prompts != NULL)
860 xfree(prompts);
861 if (echo_on != NULL)
862 xfree(echo_on);
863 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
864 return (0);
865}
866
867int
868mm_answer_pam_respond(int socket, Buffer *m)
869{
870 char **resp;
871 u_int num;
872 int i, ret;
873
874 debug3("%s", __func__);
875 sshpam_authok = NULL;
876 num = buffer_get_int(m);
877 if (num > 0) {
878 resp = xmalloc(num * sizeof(char *));
879 for (i = 0; i < num; ++i)
880 resp[i] = buffer_get_string(m, NULL);
881 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
882 for (i = 0; i < num; ++i)
883 xfree(resp[i]);
884 xfree(resp);
885 } else {
886 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
887 }
888 buffer_clear(m);
889 buffer_put_int(m, ret);
890 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
891 auth_method = "keyboard-interactive/pam";
892 if (ret == 0)
893 sshpam_authok = sshpam_ctxt;
894 return (0);
895}
896
897int
898mm_answer_pam_free_ctx(int socket, Buffer *m)
899{
900
901 debug3("%s", __func__);
902 (sshpam_device.free_ctx)(sshpam_ctxt);
903 buffer_clear(m);
904 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
905 return (sshpam_authok == sshpam_ctxt);
906}
Damien Miller79418552002-04-23 20:28:48 +1000907#endif
908
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000909static void
910mm_append_debug(Buffer *m)
911{
912 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000913 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000914 buffer_append(m, buffer_ptr(&auth_debug),
915 buffer_len(&auth_debug));
916 buffer_clear(&auth_debug);
917 }
918}
919
920int
921mm_answer_keyallowed(int socket, Buffer *m)
922{
923 Key *key;
Damien Millera10f5612002-09-12 09:49:15 +1000924 char *cuser, *chost;
925 u_char *blob;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000926 u_int bloblen;
927 enum mm_keytype type = 0;
928 int allowed = 0;
929
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000930 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000931
932 type = buffer_get_int(m);
933 cuser = buffer_get_string(m, NULL);
934 chost = buffer_get_string(m, NULL);
935 blob = buffer_get_string(m, &bloblen);
936
937 key = key_from_blob(blob, bloblen);
938
939 if ((compat20 && type == MM_RSAHOSTKEY) ||
940 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000941 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000942
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000943 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000944
945 if (key != NULL && authctxt->pw != NULL) {
946 switch(type) {
947 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000948 allowed = options.pubkey_authentication &&
949 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000950 break;
951 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000952 allowed = options.hostbased_authentication &&
953 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000954 cuser, chost, key);
955 break;
956 case MM_RSAHOSTKEY:
957 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000958 allowed = options.rhosts_rsa_authentication &&
959 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000960 cuser, chost, key);
961 break;
962 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000963 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000964 break;
965 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000966 }
Damien Miller00111382003-03-10 11:21:17 +1100967 if (key != NULL)
968 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000969
970 /* clear temporarily storage (used by verify) */
971 monitor_reset_key_state();
972
973 if (allowed) {
974 /* Save temporarily for comparison in verify */
975 key_blob = blob;
976 key_bloblen = bloblen;
977 key_blobtype = type;
978 hostbased_cuser = cuser;
979 hostbased_chost = chost;
980 }
981
982 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000983 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984
985 buffer_clear(m);
986 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +1100987 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000988
989 mm_append_debug(m);
990
991 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
992
993 if (type == MM_RSAHOSTKEY)
994 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
995
996 return (0);
997}
998
999static int
1000monitor_valid_userblob(u_char *data, u_int datalen)
1001{
1002 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001003 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001004 u_int len;
1005 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001006
1007 buffer_init(&b);
1008 buffer_append(&b, data, datalen);
1009
1010 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001011 p = buffer_ptr(&b);
1012 len = buffer_len(&b);
1013 if ((session_id2 == NULL) ||
1014 (len < session_id2_len) ||
1015 (memcmp(p, session_id2, session_id2_len) != 0))
1016 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001017 buffer_consume(&b, session_id2_len);
1018 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +00001019 p = buffer_get_string(&b, &len);
1020 if ((session_id2 == NULL) ||
1021 (len != session_id2_len) ||
1022 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001023 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001024 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001025 }
1026 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1027 fail++;
1028 p = buffer_get_string(&b, NULL);
1029 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001030 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001031 authctxt->user, p);
1032 fail++;
1033 }
1034 xfree(p);
1035 buffer_skip_string(&b);
1036 if (datafellows & SSH_BUG_PKAUTH) {
1037 if (!buffer_get_char(&b))
1038 fail++;
1039 } else {
1040 p = buffer_get_string(&b, NULL);
1041 if (strcmp("publickey", p) != 0)
1042 fail++;
1043 xfree(p);
1044 if (!buffer_get_char(&b))
1045 fail++;
1046 buffer_skip_string(&b);
1047 }
1048 buffer_skip_string(&b);
1049 if (buffer_len(&b) != 0)
1050 fail++;
1051 buffer_free(&b);
1052 return (fail == 0);
1053}
1054
1055static int
Damien Millera10f5612002-09-12 09:49:15 +10001056monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1057 char *chost)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001058{
1059 Buffer b;
Damien Millera10f5612002-09-12 09:49:15 +10001060 char *p;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001061 u_int len;
1062 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001063
1064 buffer_init(&b);
1065 buffer_append(&b, data, datalen);
1066
Ben Lindstromf67e0772002-06-06 20:58:19 +00001067 p = buffer_get_string(&b, &len);
1068 if ((session_id2 == NULL) ||
1069 (len != session_id2_len) ||
1070 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001071 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +00001072 xfree(p);
1073
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001074 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1075 fail++;
1076 p = buffer_get_string(&b, NULL);
1077 if (strcmp(authctxt->user, p) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001078 logit("wrong user name passed to monitor: expected %s != %.100s",
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001079 authctxt->user, p);
1080 fail++;
1081 }
1082 xfree(p);
1083 buffer_skip_string(&b); /* service */
1084 p = buffer_get_string(&b, NULL);
1085 if (strcmp(p, "hostbased") != 0)
1086 fail++;
1087 xfree(p);
1088 buffer_skip_string(&b); /* pkalg */
1089 buffer_skip_string(&b); /* pkblob */
1090
1091 /* verify client host, strip trailing dot if necessary */
1092 p = buffer_get_string(&b, NULL);
1093 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1094 p[len - 1] = '\0';
1095 if (strcmp(p, chost) != 0)
1096 fail++;
1097 xfree(p);
1098
1099 /* verify client user */
1100 p = buffer_get_string(&b, NULL);
1101 if (strcmp(p, cuser) != 0)
1102 fail++;
1103 xfree(p);
1104
1105 if (buffer_len(&b) != 0)
1106 fail++;
1107 buffer_free(&b);
1108 return (fail == 0);
1109}
1110
1111int
1112mm_answer_keyverify(int socket, Buffer *m)
1113{
1114 Key *key;
1115 u_char *signature, *data, *blob;
1116 u_int signaturelen, datalen, bloblen;
1117 int verified = 0;
1118 int valid_data = 0;
1119
1120 blob = buffer_get_string(m, &bloblen);
1121 signature = buffer_get_string(m, &signaturelen);
1122 data = buffer_get_string(m, &datalen);
1123
1124 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +00001125 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001126 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001127
1128 key = key_from_blob(blob, bloblen);
1129 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001130 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001131
1132 switch (key_blobtype) {
1133 case MM_USERKEY:
1134 valid_data = monitor_valid_userblob(data, datalen);
1135 break;
1136 case MM_HOSTKEY:
1137 valid_data = monitor_valid_hostbasedblob(data, datalen,
1138 hostbased_cuser, hostbased_chost);
1139 break;
1140 default:
1141 valid_data = 0;
1142 break;
1143 }
1144 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001145 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001146
1147 verified = key_verify(key, signature, signaturelen, data, datalen);
1148 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001149 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001150
1151 key_free(key);
1152 xfree(blob);
1153 xfree(signature);
1154 xfree(data);
1155
Ben Lindstrome1c09122002-06-23 00:38:24 +00001156 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1157
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001158 monitor_reset_key_state();
1159
1160 buffer_clear(m);
1161 buffer_put_int(m, verified);
1162 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1163
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001164 return (verified);
1165}
1166
1167static void
1168mm_record_login(Session *s, struct passwd *pw)
1169{
1170 socklen_t fromlen;
1171 struct sockaddr_storage from;
1172
1173 /*
1174 * Get IP address of client. If the connection is not a socket, let
1175 * the address be 0.0.0.0.
1176 */
1177 memset(&from, 0, sizeof(from));
Damien Millerebc23062002-09-04 16:45:09 +10001178 fromlen = sizeof(from);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001179 if (packet_connection_is_on_socket()) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001180 if (getpeername(packet_get_connection_in(),
1181 (struct sockaddr *) & from, &fromlen) < 0) {
1182 debug("getpeername: %.100s", strerror(errno));
1183 fatal_cleanup();
1184 }
1185 }
1186 /* Record that there was a login on that tty from the remote host. */
1187 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
Damien Miller3a961dc2003-06-03 10:25:48 +10001188 get_remote_name_or_ip(utmp_len, options.use_dns),
Damien Millerebc23062002-09-04 16:45:09 +10001189 (struct sockaddr *)&from, fromlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001190}
1191
1192static void
1193mm_session_close(Session *s)
1194{
Damien Miller04bd8b02003-05-25 14:38:33 +10001195 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001196 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001197 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001198 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1199 session_pty_cleanup2(s);
1200 }
1201 s->used = 0;
1202}
1203
1204int
1205mm_answer_pty(int socket, Buffer *m)
1206{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001207 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001208 Session *s;
1209 int res, fd0;
1210
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001211 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001212
1213 buffer_clear(m);
1214 s = session_new();
1215 if (s == NULL)
1216 goto error;
1217 s->authctxt = authctxt;
1218 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001219 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001220 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1221 if (res == 0)
1222 goto error;
1223 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1224 pty_setowner(authctxt->pw, s->tty);
1225
1226 buffer_put_int(m, 1);
1227 buffer_put_cstring(m, s->tty);
1228 mm_request_send(socket, MONITOR_ANS_PTY, m);
1229
1230 mm_send_fd(socket, s->ptyfd);
1231 mm_send_fd(socket, s->ttyfd);
1232
1233 /* We need to trick ttyslot */
1234 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001235 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001236
1237 mm_record_login(s, authctxt->pw);
1238
1239 /* Now we can close the file descriptor again */
1240 close(0);
1241
1242 /* make sure nothing uses fd 0 */
1243 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001244 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001245 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001246 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247
1248 /* slave is not needed */
1249 close(s->ttyfd);
1250 s->ttyfd = s->ptyfd;
1251 /* no need to dup() because nobody closes ptyfd */
1252 s->ptymaster = s->ptyfd;
1253
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001254 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255
1256 return (0);
1257
1258 error:
1259 if (s != NULL)
1260 mm_session_close(s);
1261 buffer_put_int(m, 0);
1262 mm_request_send(socket, MONITOR_ANS_PTY, m);
1263 return (0);
1264}
1265
1266int
1267mm_answer_pty_cleanup(int socket, Buffer *m)
1268{
1269 Session *s;
1270 char *tty;
1271
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001272 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001273
1274 tty = buffer_get_string(m, NULL);
1275 if ((s = session_by_tty(tty)) != NULL)
1276 mm_session_close(s);
1277 buffer_clear(m);
1278 xfree(tty);
1279 return (0);
1280}
1281
1282int
1283mm_answer_sesskey(int socket, Buffer *m)
1284{
1285 BIGNUM *p;
1286 int rsafail;
1287
1288 /* Turn off permissions */
1289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1290
1291 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001292 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293
1294 buffer_get_bignum2(m, p);
1295
1296 rsafail = ssh1_session_key(p);
1297
1298 buffer_clear(m);
1299 buffer_put_int(m, rsafail);
1300 buffer_put_bignum2(m, p);
1301
1302 BN_clear_free(p);
1303
1304 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1305
1306 /* Turn on permissions for sessid passing */
1307 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1308
1309 return (0);
1310}
1311
1312int
1313mm_answer_sessid(int socket, Buffer *m)
1314{
1315 int i;
1316
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001317 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001318
1319 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001320 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001321 for (i = 0; i < 16; i++)
1322 session_id[i] = buffer_get_char(m);
1323
1324 /* Turn on permissions for getpwnam */
1325 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1326
1327 return (0);
1328}
1329
1330int
1331mm_answer_rsa_keyallowed(int socket, Buffer *m)
1332{
1333 BIGNUM *client_n;
1334 Key *key = NULL;
1335 u_char *blob = NULL;
1336 u_int blen = 0;
1337 int allowed = 0;
1338
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001339 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001340
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001341 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001342 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001343 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001344 buffer_get_bignum2(m, client_n);
1345 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1346 BN_clear_free(client_n);
1347 }
1348 buffer_clear(m);
1349 buffer_put_int(m, allowed);
Damien Miller06ebedf2003-02-24 12:03:38 +11001350 buffer_put_int(m, forced_command != NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001351
1352 /* clear temporarily storage (used by generate challenge) */
1353 monitor_reset_key_state();
1354
1355 if (allowed && key != NULL) {
1356 key->type = KEY_RSA; /* cheat for key_to_blob */
1357 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001358 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001359 buffer_put_string(m, blob, blen);
1360
1361 /* Save temporarily for comparison in verify */
1362 key_blob = blob;
1363 key_bloblen = blen;
1364 key_blobtype = MM_RSAUSERKEY;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001365 }
Damien Miller00111382003-03-10 11:21:17 +11001366 if (key != NULL)
1367 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001368
1369 mm_append_debug(m);
1370
1371 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1372
1373 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1374 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1375 return (0);
1376}
1377
1378int
1379mm_answer_rsa_challenge(int socket, Buffer *m)
1380{
1381 Key *key = NULL;
1382 u_char *blob;
1383 u_int blen;
1384
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001385 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001386
1387 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001388 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001389 blob = buffer_get_string(m, &blen);
1390 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001391 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001392 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001393 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001395 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001396
1397 if (ssh1_challenge)
1398 BN_clear_free(ssh1_challenge);
1399 ssh1_challenge = auth_rsa_generate_challenge(key);
1400
1401 buffer_clear(m);
1402 buffer_put_bignum2(m, ssh1_challenge);
1403
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001404 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001405 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1406
1407 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
Damien Miller00111382003-03-10 11:21:17 +11001408
1409 xfree(blob);
1410 key_free(key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001411 return (0);
1412}
1413
1414int
1415mm_answer_rsa_response(int socket, Buffer *m)
1416{
1417 Key *key = NULL;
1418 u_char *blob, *response;
1419 u_int blen, len;
1420 int success;
1421
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001422 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001423
1424 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001425 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001426 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001427 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001428
1429 blob = buffer_get_string(m, &blen);
1430 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001431 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001432 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001435 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001436 response = buffer_get_string(m, &len);
1437 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001438 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001439 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1440
Damien Miller00111382003-03-10 11:21:17 +11001441 xfree(blob);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001442 key_free(key);
1443 xfree(response);
1444
1445 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1446
1447 /* reset state */
1448 BN_clear_free(ssh1_challenge);
1449 ssh1_challenge = NULL;
1450 monitor_reset_key_state();
1451
1452 buffer_clear(m);
1453 buffer_put_int(m, success);
1454 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1455
1456 return (success);
1457}
1458
Damien Miller25162f22002-09-12 09:47:29 +10001459#ifdef KRB5
1460int
1461mm_answer_krb5(int socket, Buffer *m)
1462{
1463 krb5_data tkt, reply;
1464 char *client_user;
1465 u_int len;
1466 int success;
1467
1468 /* use temporary var to avoid size issues on 64bit arch */
1469 tkt.data = buffer_get_string(m, &len);
1470 tkt.length = len;
1471
Damien Milleref73f502002-09-25 12:20:17 +10001472 success = options.kerberos_authentication &&
1473 authctxt->valid &&
Damien Miller7db40c92002-09-25 12:19:39 +10001474 auth_krb5(authctxt, &tkt, &client_user, &reply);
Damien Miller25162f22002-09-12 09:47:29 +10001475
1476 if (tkt.length)
1477 xfree(tkt.data);
1478
1479 buffer_clear(m);
1480 buffer_put_int(m, success);
1481
1482 if (success) {
1483 buffer_put_cstring(m, client_user);
1484 buffer_put_string(m, reply.data, reply.length);
1485 if (client_user)
1486 xfree(client_user);
1487 if (reply.length)
1488 xfree(reply.data);
1489 }
1490 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1491
Damien Miller3ab496b2003-05-14 13:47:37 +10001492 auth_method = "kerberos";
1493
Damien Miller25162f22002-09-12 09:47:29 +10001494 return success;
1495}
1496#endif
1497
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001498int
1499mm_answer_term(int socket, Buffer *req)
1500{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001501 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 int res, status;
1503
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001504 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001505
1506 /* The child is terminating */
1507 session_destroy_all(&mm_session_close);
1508
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001509 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001510 if (errno != EINTR)
1511 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001512
1513 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1514
1515 /* Terminate process */
1516 exit (res);
1517}
1518
1519void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001520monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001521{
1522 if (compat20) {
1523 set_newkeys(MODE_IN);
1524 set_newkeys(MODE_OUT);
1525 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001526 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001527 packet_set_encryption_key(child_state.ssh1key,
1528 child_state.ssh1keylen, child_state.ssh1cipher);
1529 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001530 }
1531
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001532 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001533 packet_set_keycontext(MODE_OUT, child_state.keyout);
1534 xfree(child_state.keyout);
1535 packet_set_keycontext(MODE_IN, child_state.keyin);
1536 xfree(child_state.keyin);
1537
1538 if (!compat20) {
1539 packet_set_iv(MODE_OUT, child_state.ivout);
1540 xfree(child_state.ivout);
1541 packet_set_iv(MODE_IN, child_state.ivin);
1542 xfree(child_state.ivin);
1543 }
1544
1545 memcpy(&incoming_stream, &child_state.incoming,
1546 sizeof(incoming_stream));
1547 memcpy(&outgoing_stream, &child_state.outgoing,
1548 sizeof(outgoing_stream));
1549
1550 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001551 if (options.compression)
1552 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001553
1554 /* Network I/O buffers */
1555 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1556 buffer_clear(&input);
1557 buffer_append(&input, child_state.input, child_state.ilen);
1558 memset(child_state.input, 0, child_state.ilen);
1559 xfree(child_state.input);
1560
1561 buffer_clear(&output);
1562 buffer_append(&output, child_state.output, child_state.olen);
1563 memset(child_state.output, 0, child_state.olen);
1564 xfree(child_state.output);
1565}
1566
1567static Kex *
1568mm_get_kex(Buffer *m)
1569{
1570 Kex *kex;
1571 void *blob;
1572 u_int bloblen;
1573
1574 kex = xmalloc(sizeof(*kex));
1575 memset(kex, 0, sizeof(*kex));
1576 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001577 if ((session_id2 == NULL) ||
1578 (kex->session_id_len != session_id2_len) ||
1579 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1580 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001581 kex->we_need = buffer_get_int(m);
Damien Millerb062c292003-03-24 09:12:09 +11001582 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1583 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001584 kex->server = 1;
1585 kex->hostkey_type = buffer_get_int(m);
1586 kex->kex_type = buffer_get_int(m);
1587 blob = buffer_get_string(m, &bloblen);
1588 buffer_init(&kex->my);
1589 buffer_append(&kex->my, blob, bloblen);
1590 xfree(blob);
1591 blob = buffer_get_string(m, &bloblen);
1592 buffer_init(&kex->peer);
1593 buffer_append(&kex->peer, blob, bloblen);
1594 xfree(blob);
1595 kex->done = 1;
1596 kex->flags = buffer_get_int(m);
1597 kex->client_version_string = buffer_get_string(m, NULL);
1598 kex->server_version_string = buffer_get_string(m, NULL);
1599 kex->load_host_key=&get_hostkey_by_type;
1600 kex->host_key_index=&get_hostkey_index;
1601
1602 return (kex);
1603}
1604
1605/* This function requries careful sanity checking */
1606
1607void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001608mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001609{
1610 Buffer m;
1611 u_char *blob, *p;
1612 u_int bloblen, plen;
Damien Millera5539d22003-04-09 20:50:06 +10001613 u_int32_t seqnr, packets;
1614 u_int64_t blocks;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001615
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001616 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001617
1618 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001619 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001620 if (!compat20) {
1621 child_state.ssh1protoflags = buffer_get_int(&m);
1622 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001623 child_state.ssh1key = buffer_get_string(&m,
1624 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001625 child_state.ivout = buffer_get_string(&m,
1626 &child_state.ivoutlen);
1627 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1628 goto skip;
1629 } else {
1630 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001631 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001632 }
1633
1634 blob = buffer_get_string(&m, &bloblen);
1635 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1636 xfree(blob);
1637
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001638 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001639 blob = buffer_get_string(&m, &bloblen);
1640 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1641 xfree(blob);
1642
1643 /* Now get sequence numbers for the packets */
Damien Millera5539d22003-04-09 20:50:06 +10001644 seqnr = buffer_get_int(&m);
1645 blocks = buffer_get_int64(&m);
1646 packets = buffer_get_int(&m);
1647 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1648 seqnr = buffer_get_int(&m);
1649 blocks = buffer_get_int64(&m);
1650 packets = buffer_get_int(&m);
1651 packet_set_state(MODE_IN, seqnr, blocks, packets);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001652
1653 skip:
1654 /* Get the key context */
1655 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1656 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1657
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001658 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001659 /* Get compression state */
1660 p = buffer_get_string(&m, &plen);
1661 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001662 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001663 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1664 xfree(p);
1665
1666 p = buffer_get_string(&m, &plen);
1667 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001668 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001669 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1670 xfree(p);
1671
1672 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001673 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001674 child_state.input = buffer_get_string(&m, &child_state.ilen);
1675 child_state.output = buffer_get_string(&m, &child_state.olen);
1676
1677 buffer_free(&m);
1678}
1679
1680
1681/* Allocation functions for zlib */
1682void *
1683mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1684{
Ben Lindstrom41ee2b02002-11-09 15:47:47 +00001685 size_t len = (size_t) size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001686 void *address;
1687
Ben Lindstrom0a4f7542002-08-20 18:36:25 +00001688 if (len == 0 || ncount > SIZE_T_MAX / size)
Damien Miller530a7542002-06-26 23:27:11 +10001689 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1690
1691 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001692
1693 return (address);
1694}
1695
1696void
1697mm_zfree(struct mm_master *mm, void *address)
1698{
1699 mm_free(mm, address);
1700}
1701
1702void
1703mm_init_compression(struct mm_master *mm)
1704{
1705 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1706 outgoing_stream.zfree = (free_func)mm_zfree;
1707 outgoing_stream.opaque = mm;
1708
1709 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1710 incoming_stream.zfree = (free_func)mm_zfree;
1711 incoming_stream.opaque = mm;
1712}
1713
1714/* XXX */
1715
1716#define FD_CLOSEONEXEC(x) do { \
1717 if (fcntl(x, F_SETFD, 1) == -1) \
1718 fatal("fcntl(%d, F_SETFD)", x); \
1719} while (0)
1720
1721static void
1722monitor_socketpair(int *pair)
1723{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001724#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001725 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001726 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001727#else
1728 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001729 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001730#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001731 FD_CLOSEONEXEC(pair[0]);
1732 FD_CLOSEONEXEC(pair[1]);
1733}
1734
1735#define MM_MEMSIZE 65536
1736
1737struct monitor *
1738monitor_init(void)
1739{
1740 struct monitor *mon;
1741 int pair[2];
1742
1743 mon = xmalloc(sizeof(*mon));
1744
1745 monitor_socketpair(pair);
1746
1747 mon->m_recvfd = pair[0];
1748 mon->m_sendfd = pair[1];
1749
1750 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001751 if (options.compression) {
1752 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1753 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001754
Damien Miller2d6b8332002-06-21 15:59:49 +10001755 /* Compression needs to share state across borders */
1756 mm_init_compression(mon->m_zlib);
1757 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001758
1759 return mon;
1760}
1761
1762void
1763monitor_reinit(struct monitor *mon)
1764{
1765 int pair[2];
1766
1767 monitor_socketpair(pair);
1768
1769 mon->m_recvfd = pair[0];
1770 mon->m_sendfd = pair[1];
1771}