blob: 148b02e51dae57949907593a4ec19c5cfc788be5 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Ben Lindstromeec16fc2002-07-04 00:06:15 +000028RCSID("$OpenBSD: monitor.c,v 1.21 2002/06/27 10:35:47 deraadt Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000029
30#include <openssl/dh.h>
31
32#ifdef SKEY
33#include <skey.h>
34#endif
35
36#include "ssh.h"
37#include "auth.h"
38#include "kex.h"
39#include "dh.h"
40#include "zlib.h"
41#include "packet.h"
42#include "auth-options.h"
43#include "sshpty.h"
44#include "channels.h"
45#include "session.h"
46#include "sshlogin.h"
47#include "canohost.h"
48#include "log.h"
49#include "servconf.h"
50#include "monitor.h"
51#include "monitor_mm.h"
52#include "monitor_wrap.h"
53#include "monitor_fdpass.h"
54#include "xmalloc.h"
55#include "misc.h"
56#include "buffer.h"
57#include "bufaux.h"
58#include "compat.h"
59#include "ssh2.h"
60#include "mpaux.h"
61
62/* Imports */
63extern ServerOptions options;
64extern u_int utmp_len;
65extern Newkeys *current_keys[];
66extern z_stream incoming_stream;
67extern z_stream outgoing_stream;
68extern u_char session_id[];
69extern Buffer input, output;
70extern Buffer auth_debug;
71extern int auth_debug_init;
72
73/* State exported from the child */
74
75struct {
76 z_stream incoming;
77 z_stream outgoing;
78 u_char *keyin;
79 u_int keyinlen;
80 u_char *keyout;
81 u_int keyoutlen;
82 u_char *ivin;
83 u_int ivinlen;
84 u_char *ivout;
85 u_int ivoutlen;
Ben Lindstrom402c6cc2002-06-21 00:43:42 +000086 u_char *ssh1key;
87 u_int ssh1keylen;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000088 int ssh1cipher;
89 int ssh1protoflags;
90 u_char *input;
91 u_int ilen;
92 u_char *output;
93 u_int olen;
94} child_state;
95
96/* Functions on the montior that answer unprivileged requests */
97
98int mm_answer_moduli(int, Buffer *);
99int mm_answer_sign(int, Buffer *);
100int mm_answer_pwnamallow(int, Buffer *);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000101int mm_answer_auth2_read_banner(int, Buffer *);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102int mm_answer_authserv(int, Buffer *);
103int mm_answer_authpassword(int, Buffer *);
104int mm_answer_bsdauthquery(int, Buffer *);
105int mm_answer_bsdauthrespond(int, Buffer *);
106int mm_answer_skeyquery(int, Buffer *);
107int mm_answer_skeyrespond(int, Buffer *);
108int mm_answer_keyallowed(int, Buffer *);
109int mm_answer_keyverify(int, Buffer *);
110int mm_answer_pty(int, Buffer *);
111int mm_answer_pty_cleanup(int, Buffer *);
112int mm_answer_term(int, Buffer *);
113int mm_answer_rsa_keyallowed(int, Buffer *);
114int mm_answer_rsa_challenge(int, Buffer *);
115int mm_answer_rsa_response(int, Buffer *);
116int mm_answer_sesskey(int, Buffer *);
117int mm_answer_sessid(int, Buffer *);
118
Damien Miller79418552002-04-23 20:28:48 +1000119#ifdef USE_PAM
120int mm_answer_pam_start(int, Buffer *);
121#endif
122
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000123static Authctxt *authctxt;
124static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
125
126/* local state for key verify */
127static u_char *key_blob = NULL;
128static u_int key_bloblen = 0;
129static int key_blobtype = MM_NOKEY;
130static u_char *hostbased_cuser = NULL;
131static u_char *hostbased_chost = NULL;
132static char *auth_method = "unknown";
Ben Lindstromf67e0772002-06-06 20:58:19 +0000133static int session_id2_len = 0;
134static u_char *session_id2 = NULL;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000135
136struct mon_table {
137 enum monitor_reqtype type;
138 int flags;
139 int (*f)(int, Buffer *);
140};
141
142#define MON_ISAUTH 0x0004 /* Required for Authentication */
143#define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
144#define MON_ONCE 0x0010 /* Disable after calling */
145
146#define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
147
148#define MON_PERMIT 0x1000 /* Request is permitted */
149
150struct mon_table mon_dispatch_proto20[] = {
151 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
152 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
153 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
154 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
Damien Miller5ad9fd92002-05-13 11:07:41 +1000155 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000156 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
Damien Miller79418552002-04-23 20:28:48 +1000157#ifdef USE_PAM
158 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
Kevin Stevesbd1901b2002-04-01 18:04:35 +0000159#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000160#ifdef BSD_AUTH
161 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
162 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
163#endif
164#ifdef SKEY
165 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
166 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
167#endif
168 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
169 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
170 {0, 0, NULL}
171};
172
173struct mon_table mon_dispatch_postauth20[] = {
174 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
175 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
176 {MONITOR_REQ_PTY, 0, mm_answer_pty},
177 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
178 {MONITOR_REQ_TERM, 0, mm_answer_term},
179 {0, 0, NULL}
180};
181
182struct mon_table mon_dispatch_proto15[] = {
183 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
184 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
185 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
186 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
187 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
188 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
189 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
190 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
191#ifdef BSD_AUTH
192 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
193 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
194#endif
195#ifdef SKEY
196 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
197 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
198#endif
Damien Millera33501b2002-05-08 12:24:42 +1000199#ifdef USE_PAM
200 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
201#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000202 {0, 0, NULL}
203};
204
205struct mon_table mon_dispatch_postauth15[] = {
206 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
207 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
208 {MONITOR_REQ_TERM, 0, mm_answer_term},
209 {0, 0, NULL}
210};
211
212struct mon_table *mon_dispatch;
213
214/* Specifies if a certain message is allowed at the moment */
215
216static void
217monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
218{
219 while (ent->f != NULL) {
220 if (ent->type == type) {
221 ent->flags &= ~MON_PERMIT;
222 ent->flags |= permit ? MON_PERMIT : 0;
223 return;
224 }
225 ent++;
226 }
227}
228
229static void
230monitor_permit_authentications(int permit)
231{
232 struct mon_table *ent = mon_dispatch;
233
234 while (ent->f != NULL) {
235 if (ent->flags & MON_AUTH) {
236 ent->flags &= ~MON_PERMIT;
237 ent->flags |= permit ? MON_PERMIT : 0;
238 }
239 ent++;
240 }
241}
242
243Authctxt *
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000244monitor_child_preauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000245{
246 struct mon_table *ent;
247 int authenticated = 0;
248
249 debug3("preauth child monitor started");
250
251 if (compat20) {
252 mon_dispatch = mon_dispatch_proto20;
253
254 /* Permit requests for moduli and signatures */
255 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
256 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
257 } else {
258 mon_dispatch = mon_dispatch_proto15;
259
260 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
261 }
262
263 authctxt = authctxt_new();
264
265 /* The first few requests do not require asynchronous access */
266 while (!authenticated) {
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000267 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000268 if (authenticated) {
269 if (!(ent->flags & MON_AUTHDECIDE))
270 fatal("%s: unexpected authentication from %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000271 __func__, ent->type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000272 if (authctxt->pw->pw_uid == 0 &&
273 !auth_root_allowed(auth_method))
274 authenticated = 0;
Damien Miller79418552002-04-23 20:28:48 +1000275#ifdef USE_PAM
276 if (!do_pam_account(authctxt->pw->pw_name, NULL))
277 authenticated = 0;
278#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000279 }
280
281 if (ent->flags & MON_AUTHDECIDE) {
282 auth_log(authctxt, authenticated, auth_method,
283 compat20 ? " ssh2" : "");
284 if (!authenticated)
285 authctxt->failures++;
286 }
287 }
288
289 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000290 fatal("%s: authenticated invalid user", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000291
292 debug("%s: %s has been authenticated by privileged process",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000293 __func__, authctxt->user);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000294
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000295 mm_get_keystate(pmonitor);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000296
297 return (authctxt);
298}
299
300void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000301monitor_child_postauth(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000302{
303 if (compat20) {
304 mon_dispatch = mon_dispatch_postauth20;
305
306 /* Permit requests for moduli and signatures */
307 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
308 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
309 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
310
311 } else {
312 mon_dispatch = mon_dispatch_postauth15;
313 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
314 }
315 if (!no_pty_flag) {
316 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
317 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
318 }
319
320 for (;;)
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000321 monitor_read(pmonitor, mon_dispatch, NULL);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000322}
323
324void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000325monitor_sync(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000326{
Damien Miller2d6b8332002-06-21 15:59:49 +1000327 if (options.compression) {
328 /* The member allocation is not visible, so sync it */
329 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
330 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000331}
332
333int
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000334monitor_read(struct monitor *pmonitor, struct mon_table *ent,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000335 struct mon_table **pent)
336{
337 Buffer m;
338 int ret;
339 u_char type;
340
341 buffer_init(&m);
342
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000343 mm_request_receive(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000344 type = buffer_get_char(&m);
345
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000346 debug3("%s: checking request %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000347
348 while (ent->f != NULL) {
349 if (ent->type == type)
350 break;
351 ent++;
352 }
353
354 if (ent->f != NULL) {
355 if (!(ent->flags & MON_PERMIT))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000356 fatal("%s: unpermitted request %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000357 type);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +0000358 ret = (*ent->f)(pmonitor->m_sendfd, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000359 buffer_free(&m);
360
361 /* The child may use this request only once, disable it */
362 if (ent->flags & MON_ONCE) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000363 debug2("%s: %d used once, disabling now", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000364 type);
365 ent->flags &= ~MON_PERMIT;
366 }
367
368 if (pent != NULL)
369 *pent = ent;
370
371 return ret;
372 }
373
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000374 fatal("%s: unsupported request: %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000375
376 /* NOTREACHED */
377 return (-1);
378}
379
380/* allowed key state */
381static int
382monitor_allowed_key(u_char *blob, u_int bloblen)
383{
384 /* make sure key is allowed */
385 if (key_blob == NULL || key_bloblen != bloblen ||
386 memcmp(key_blob, blob, key_bloblen))
387 return (0);
388 return (1);
389}
390
391static void
392monitor_reset_key_state(void)
393{
394 /* reset state */
395 if (key_blob != NULL)
396 xfree(key_blob);
397 if (hostbased_cuser != NULL)
398 xfree(hostbased_cuser);
399 if (hostbased_chost != NULL)
400 xfree(hostbased_chost);
401 key_blob = NULL;
402 key_bloblen = 0;
403 key_blobtype = MM_NOKEY;
404 hostbased_cuser = NULL;
405 hostbased_chost = NULL;
406}
407
408int
409mm_answer_moduli(int socket, Buffer *m)
410{
411 DH *dh;
412 int min, want, max;
413
414 min = buffer_get_int(m);
415 want = buffer_get_int(m);
416 max = buffer_get_int(m);
417
418 debug3("%s: got parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000419 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000420 /* We need to check here, too, in case the child got corrupted */
421 if (max < min || want < min || max < want)
422 fatal("%s: bad parameters: %d %d %d",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000423 __func__, min, want, max);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000424
425 buffer_clear(m);
426
427 dh = choose_dh(min, want, max);
428 if (dh == NULL) {
429 buffer_put_char(m, 0);
430 return (0);
431 } else {
432 /* Send first bignum */
433 buffer_put_char(m, 1);
434 buffer_put_bignum2(m, dh->p);
435 buffer_put_bignum2(m, dh->g);
436
437 DH_free(dh);
438 }
439 mm_request_send(socket, MONITOR_ANS_MODULI, m);
440 return (0);
441}
442
443int
444mm_answer_sign(int socket, Buffer *m)
445{
446 Key *key;
447 u_char *p;
448 u_char *signature;
449 u_int siglen, datlen;
450 int keyid;
451
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000452 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000453
454 keyid = buffer_get_int(m);
455 p = buffer_get_string(m, &datlen);
456
457 if (datlen != 20)
Ben Lindstromd5502182002-06-27 00:12:57 +0000458 fatal("%s: data length incorrect: %u", __func__, datlen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000459
Ben Lindstromf67e0772002-06-06 20:58:19 +0000460 /* save session id, it will be passed on the first call */
461 if (session_id2_len == 0) {
462 session_id2_len = datlen;
463 session_id2 = xmalloc(session_id2_len);
464 memcpy(session_id2, p, session_id2_len);
465 }
466
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000467 if ((key = get_hostkey_by_index(keyid)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000468 fatal("%s: no hostkey from index %d", __func__, keyid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000469 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000470 fatal("%s: key_sign failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000471
Ben Lindstromd5502182002-06-27 00:12:57 +0000472 debug3("%s: signature %p(%u)", __func__, signature, siglen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000473
474 buffer_clear(m);
475 buffer_put_string(m, signature, siglen);
476
477 xfree(p);
478 xfree(signature);
479
480 mm_request_send(socket, MONITOR_ANS_SIGN, m);
481
482 /* Turn on permissions for getpwnam */
483 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
484
485 return (0);
486}
487
488/* Retrieves the password entry and also checks if the user is permitted */
489
490int
491mm_answer_pwnamallow(int socket, Buffer *m)
492{
493 char *login;
494 struct passwd *pwent;
495 int allowed = 0;
496
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000497 debug3("%s", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000498
499 if (authctxt->attempt++ != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000500 fatal("%s: multiple attempts for getpwnam", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000501
502 login = buffer_get_string(m, NULL);
503
504 pwent = getpwnamallow(login);
505
506 authctxt->user = xstrdup(login);
507 setproctitle("%s [priv]", pwent ? login : "unknown");
508 xfree(login);
509
510 buffer_clear(m);
511
512 if (pwent == NULL) {
513 buffer_put_char(m, 0);
514 goto out;
515 }
516
517 allowed = 1;
518 authctxt->pw = pwent;
519 authctxt->valid = 1;
520
521 buffer_put_char(m, 1);
522 buffer_put_string(m, pwent, sizeof(struct passwd));
523 buffer_put_cstring(m, pwent->pw_name);
524 buffer_put_cstring(m, "*");
525 buffer_put_cstring(m, pwent->pw_gecos);
Kevin Steves7e147602002-03-22 18:07:17 +0000526#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000527 buffer_put_cstring(m, pwent->pw_class);
Kevin Steves7e147602002-03-22 18:07:17 +0000528#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000529 buffer_put_cstring(m, pwent->pw_dir);
530 buffer_put_cstring(m, pwent->pw_shell);
531
532 out:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000533 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000534 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
535
536 /* For SSHv1 allow authentication now */
537 if (!compat20)
538 monitor_permit_authentications(1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000539 else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000540 /* Allow service/style information on the auth context */
541 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000542 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
543 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000544
Damien Millera33501b2002-05-08 12:24:42 +1000545#ifdef USE_PAM
546 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
547#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000548
549 return (0);
550}
551
Damien Miller5ad9fd92002-05-13 11:07:41 +1000552int mm_answer_auth2_read_banner(int socket, Buffer *m)
553{
554 char *banner;
555
556 buffer_clear(m);
557 banner = auth2_read_banner();
558 buffer_put_cstring(m, banner != NULL ? banner : "");
559 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
560
561 if (banner != NULL)
Ben Lindstromeec16fc2002-07-04 00:06:15 +0000562 xfree(banner);
Damien Miller5ad9fd92002-05-13 11:07:41 +1000563
564 return (0);
565}
566
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000567int
568mm_answer_authserv(int socket, Buffer *m)
569{
570 monitor_permit_authentications(1);
571
572 authctxt->service = buffer_get_string(m, NULL);
573 authctxt->style = buffer_get_string(m, NULL);
574 debug3("%s: service=%s, style=%s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000575 __func__, authctxt->service, authctxt->style);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000576
577 if (strlen(authctxt->style) == 0) {
578 xfree(authctxt->style);
579 authctxt->style = NULL;
580 }
581
582 return (0);
583}
584
585int
586mm_answer_authpassword(int socket, Buffer *m)
587{
588 static int call_count;
589 char *passwd;
590 int authenticated, plen;
591
592 passwd = buffer_get_string(m, &plen);
593 /* Only authenticate if the context is valid */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000594 authenticated = options.password_authentication &&
595 authctxt->valid && auth_password(authctxt, passwd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000596 memset(passwd, 0, strlen(passwd));
597 xfree(passwd);
598
599 buffer_clear(m);
600 buffer_put_int(m, authenticated);
601
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000602 debug3("%s: sending result %d", __func__, authenticated);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000603 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
604
605 call_count++;
606 if (plen == 0 && call_count == 1)
607 auth_method = "none";
608 else
609 auth_method = "password";
610
611 /* Causes monitor loop to terminate if authenticated */
612 return (authenticated);
613}
614
615#ifdef BSD_AUTH
616int
617mm_answer_bsdauthquery(int socket, Buffer *m)
618{
619 char *name, *infotxt;
620 u_int numprompts;
621 u_int *echo_on;
622 char **prompts;
623 int res;
624
625 res = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
626 &prompts, &echo_on);
627
628 buffer_clear(m);
629 buffer_put_int(m, res);
630 if (res != -1)
631 buffer_put_cstring(m, prompts[0]);
632
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000633 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000634 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
635
636 if (res != -1) {
637 xfree(name);
638 xfree(infotxt);
639 xfree(prompts);
640 xfree(echo_on);
641 }
642
643 return (0);
644}
645
646int
647mm_answer_bsdauthrespond(int socket, Buffer *m)
648{
649 char *response;
650 int authok;
651
652 if (authctxt->as == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000653 fatal("%s: no bsd auth session", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000654
655 response = buffer_get_string(m, NULL);
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000656 authok = options.challenge_response_authentication &&
657 auth_userresponse(authctxt->as, response, 0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000658 authctxt->as = NULL;
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000659 debug3("%s: <%s> = <%d>", __func__, response, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000660 xfree(response);
661
662 buffer_clear(m);
663 buffer_put_int(m, authok);
664
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000665 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000666 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
667
668 auth_method = "bsdauth";
669
670 return (authok != 0);
671}
672#endif
673
674#ifdef SKEY
675int
676mm_answer_skeyquery(int socket, Buffer *m)
677{
678 struct skey skey;
679 char challenge[1024];
680 int res;
681
682 res = skeychallenge(&skey, authctxt->user, challenge);
683
684 buffer_clear(m);
685 buffer_put_int(m, res);
686 if (res != -1)
687 buffer_put_cstring(m, challenge);
688
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000689 debug3("%s: sending challenge res: %d", __func__, res);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000690 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
691
692 return (0);
693}
694
695int
696mm_answer_skeyrespond(int socket, Buffer *m)
697{
698 char *response;
699 int authok;
700
701 response = buffer_get_string(m, NULL);
702
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000703 authok = (options.challenge_response_authentication &&
704 authctxt->valid &&
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000705 skey_haskey(authctxt->pw->pw_name) == 0 &&
706 skey_passcheck(authctxt->pw->pw_name, response) != -1);
707
708 xfree(response);
709
710 buffer_clear(m);
711 buffer_put_int(m, authok);
712
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000713 debug3("%s: sending authenticated: %d", __func__, authok);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000714 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
715
716 auth_method = "skey";
717
718 return (authok != 0);
719}
720#endif
721
Damien Miller79418552002-04-23 20:28:48 +1000722#ifdef USE_PAM
723int
724mm_answer_pam_start(int socket, Buffer *m)
725{
726 char *user;
727
728 user = buffer_get_string(m, NULL);
729
730 start_pam(user);
731
732 xfree(user);
733
734 return (0);
735}
736#endif
737
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000738static void
739mm_append_debug(Buffer *m)
740{
741 if (auth_debug_init && buffer_len(&auth_debug)) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000742 debug3("%s: Appending debug messages for child", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000743 buffer_append(m, buffer_ptr(&auth_debug),
744 buffer_len(&auth_debug));
745 buffer_clear(&auth_debug);
746 }
747}
748
749int
750mm_answer_keyallowed(int socket, Buffer *m)
751{
752 Key *key;
753 u_char *cuser, *chost, *blob;
754 u_int bloblen;
755 enum mm_keytype type = 0;
756 int allowed = 0;
757
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000758 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000759
760 type = buffer_get_int(m);
761 cuser = buffer_get_string(m, NULL);
762 chost = buffer_get_string(m, NULL);
763 blob = buffer_get_string(m, &bloblen);
764
765 key = key_from_blob(blob, bloblen);
766
767 if ((compat20 && type == MM_RSAHOSTKEY) ||
768 (!compat20 && type != MM_RSAHOSTKEY))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000769 fatal("%s: key type and protocol mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000770
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000771 debug3("%s: key_from_blob: %p", __func__, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000772
773 if (key != NULL && authctxt->pw != NULL) {
774 switch(type) {
775 case MM_USERKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000776 allowed = options.pubkey_authentication &&
777 user_key_allowed(authctxt->pw, key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000778 break;
779 case MM_HOSTKEY:
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000780 allowed = options.hostbased_authentication &&
781 hostbased_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000782 cuser, chost, key);
783 break;
784 case MM_RSAHOSTKEY:
785 key->type = KEY_RSA1; /* XXX */
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +0000786 allowed = options.rhosts_rsa_authentication &&
787 auth_rhosts_rsa_key_allowed(authctxt->pw,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000788 cuser, chost, key);
789 break;
790 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000791 fatal("%s: unknown key type %d", __func__, type);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000792 break;
793 }
794 key_free(key);
795 }
796
797 /* clear temporarily storage (used by verify) */
798 monitor_reset_key_state();
799
800 if (allowed) {
801 /* Save temporarily for comparison in verify */
802 key_blob = blob;
803 key_bloblen = bloblen;
804 key_blobtype = type;
805 hostbased_cuser = cuser;
806 hostbased_chost = chost;
807 }
808
809 debug3("%s: key %p is %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000810 __func__, key, allowed ? "allowed" : "disallowed");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000811
812 buffer_clear(m);
813 buffer_put_int(m, allowed);
814
815 mm_append_debug(m);
816
817 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
818
819 if (type == MM_RSAHOSTKEY)
820 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
821
822 return (0);
823}
824
825static int
826monitor_valid_userblob(u_char *data, u_int datalen)
827{
828 Buffer b;
829 u_char *p;
830 u_int len;
831 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000832
833 buffer_init(&b);
834 buffer_append(&b, data, datalen);
835
836 if (datafellows & SSH_OLD_SESSIONID) {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000837 p = buffer_ptr(&b);
838 len = buffer_len(&b);
839 if ((session_id2 == NULL) ||
840 (len < session_id2_len) ||
841 (memcmp(p, session_id2, session_id2_len) != 0))
842 fail++;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000843 buffer_consume(&b, session_id2_len);
844 } else {
Ben Lindstromf67e0772002-06-06 20:58:19 +0000845 p = buffer_get_string(&b, &len);
846 if ((session_id2 == NULL) ||
847 (len != session_id2_len) ||
848 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000849 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000850 xfree(p);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000851 }
852 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
853 fail++;
854 p = buffer_get_string(&b, NULL);
855 if (strcmp(authctxt->user, p) != 0) {
856 log("wrong user name passed to monitor: expected %s != %.100s",
857 authctxt->user, p);
858 fail++;
859 }
860 xfree(p);
861 buffer_skip_string(&b);
862 if (datafellows & SSH_BUG_PKAUTH) {
863 if (!buffer_get_char(&b))
864 fail++;
865 } else {
866 p = buffer_get_string(&b, NULL);
867 if (strcmp("publickey", p) != 0)
868 fail++;
869 xfree(p);
870 if (!buffer_get_char(&b))
871 fail++;
872 buffer_skip_string(&b);
873 }
874 buffer_skip_string(&b);
875 if (buffer_len(&b) != 0)
876 fail++;
877 buffer_free(&b);
878 return (fail == 0);
879}
880
881static int
882monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
883 u_char *chost)
884{
885 Buffer b;
886 u_char *p;
887 u_int len;
888 int fail = 0;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000889
890 buffer_init(&b);
891 buffer_append(&b, data, datalen);
892
Ben Lindstromf67e0772002-06-06 20:58:19 +0000893 p = buffer_get_string(&b, &len);
894 if ((session_id2 == NULL) ||
895 (len != session_id2_len) ||
896 (memcmp(p, session_id2, session_id2_len) != 0))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000897 fail++;
Ben Lindstromf67e0772002-06-06 20:58:19 +0000898 xfree(p);
899
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000900 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
901 fail++;
902 p = buffer_get_string(&b, NULL);
903 if (strcmp(authctxt->user, p) != 0) {
904 log("wrong user name passed to monitor: expected %s != %.100s",
905 authctxt->user, p);
906 fail++;
907 }
908 xfree(p);
909 buffer_skip_string(&b); /* service */
910 p = buffer_get_string(&b, NULL);
911 if (strcmp(p, "hostbased") != 0)
912 fail++;
913 xfree(p);
914 buffer_skip_string(&b); /* pkalg */
915 buffer_skip_string(&b); /* pkblob */
916
917 /* verify client host, strip trailing dot if necessary */
918 p = buffer_get_string(&b, NULL);
919 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
920 p[len - 1] = '\0';
921 if (strcmp(p, chost) != 0)
922 fail++;
923 xfree(p);
924
925 /* verify client user */
926 p = buffer_get_string(&b, NULL);
927 if (strcmp(p, cuser) != 0)
928 fail++;
929 xfree(p);
930
931 if (buffer_len(&b) != 0)
932 fail++;
933 buffer_free(&b);
934 return (fail == 0);
935}
936
937int
938mm_answer_keyverify(int socket, Buffer *m)
939{
940 Key *key;
941 u_char *signature, *data, *blob;
942 u_int signaturelen, datalen, bloblen;
943 int verified = 0;
944 int valid_data = 0;
945
946 blob = buffer_get_string(m, &bloblen);
947 signature = buffer_get_string(m, &signaturelen);
948 data = buffer_get_string(m, &datalen);
949
950 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Ben Lindstromb57a4bf2002-03-27 18:00:59 +0000951 !monitor_allowed_key(blob, bloblen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000952 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000953
954 key = key_from_blob(blob, bloblen);
955 if (key == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000956 fatal("%s: bad public key blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000957
958 switch (key_blobtype) {
959 case MM_USERKEY:
960 valid_data = monitor_valid_userblob(data, datalen);
961 break;
962 case MM_HOSTKEY:
963 valid_data = monitor_valid_hostbasedblob(data, datalen,
964 hostbased_cuser, hostbased_chost);
965 break;
966 default:
967 valid_data = 0;
968 break;
969 }
970 if (!valid_data)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000971 fatal("%s: bad signature data blob", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000972
973 verified = key_verify(key, signature, signaturelen, data, datalen);
974 debug3("%s: key %p signature %s",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000975 __func__, key, verified ? "verified" : "unverified");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000976
977 key_free(key);
978 xfree(blob);
979 xfree(signature);
980 xfree(data);
981
Ben Lindstrome1c09122002-06-23 00:38:24 +0000982 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
983
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000984 monitor_reset_key_state();
985
986 buffer_clear(m);
987 buffer_put_int(m, verified);
988 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
989
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000990 return (verified);
991}
992
993static void
994mm_record_login(Session *s, struct passwd *pw)
995{
996 socklen_t fromlen;
997 struct sockaddr_storage from;
998
999 /*
1000 * Get IP address of client. If the connection is not a socket, let
1001 * the address be 0.0.0.0.
1002 */
1003 memset(&from, 0, sizeof(from));
1004 if (packet_connection_is_on_socket()) {
1005 fromlen = sizeof(from);
1006 if (getpeername(packet_get_connection_in(),
1007 (struct sockaddr *) & from, &fromlen) < 0) {
1008 debug("getpeername: %.100s", strerror(errno));
1009 fatal_cleanup();
1010 }
1011 }
1012 /* Record that there was a login on that tty from the remote host. */
1013 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1014 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1015 (struct sockaddr *)&from);
1016}
1017
1018static void
1019mm_session_close(Session *s)
1020{
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001021 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001022 if (s->ttyfd != -1) {
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001023 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001024 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1025 session_pty_cleanup2(s);
1026 }
1027 s->used = 0;
1028}
1029
1030int
1031mm_answer_pty(int socket, Buffer *m)
1032{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001033 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001034 Session *s;
1035 int res, fd0;
1036
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001037 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001038
1039 buffer_clear(m);
1040 s = session_new();
1041 if (s == NULL)
1042 goto error;
1043 s->authctxt = authctxt;
1044 s->pw = authctxt->pw;
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001045 s->pid = pmonitor->m_pid;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001046 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1047 if (res == 0)
1048 goto error;
1049 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1050 pty_setowner(authctxt->pw, s->tty);
1051
1052 buffer_put_int(m, 1);
1053 buffer_put_cstring(m, s->tty);
1054 mm_request_send(socket, MONITOR_ANS_PTY, m);
1055
1056 mm_send_fd(socket, s->ptyfd);
1057 mm_send_fd(socket, s->ttyfd);
1058
1059 /* We need to trick ttyslot */
1060 if (dup2(s->ttyfd, 0) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001061 fatal("%s: dup2", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001062
1063 mm_record_login(s, authctxt->pw);
1064
1065 /* Now we can close the file descriptor again */
1066 close(0);
1067
1068 /* make sure nothing uses fd 0 */
1069 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001070 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001071 if (fd0 != 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001072 error("%s: fd0 %d != 0", __func__, fd0);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001073
1074 /* slave is not needed */
1075 close(s->ttyfd);
1076 s->ttyfd = s->ptyfd;
1077 /* no need to dup() because nobody closes ptyfd */
1078 s->ptymaster = s->ptyfd;
1079
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001080 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001081
1082 return (0);
1083
1084 error:
1085 if (s != NULL)
1086 mm_session_close(s);
1087 buffer_put_int(m, 0);
1088 mm_request_send(socket, MONITOR_ANS_PTY, m);
1089 return (0);
1090}
1091
1092int
1093mm_answer_pty_cleanup(int socket, Buffer *m)
1094{
1095 Session *s;
1096 char *tty;
1097
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001098 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001099
1100 tty = buffer_get_string(m, NULL);
1101 if ((s = session_by_tty(tty)) != NULL)
1102 mm_session_close(s);
1103 buffer_clear(m);
1104 xfree(tty);
1105 return (0);
1106}
1107
1108int
1109mm_answer_sesskey(int socket, Buffer *m)
1110{
1111 BIGNUM *p;
1112 int rsafail;
1113
1114 /* Turn off permissions */
1115 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1116
1117 if ((p = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001118 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001119
1120 buffer_get_bignum2(m, p);
1121
1122 rsafail = ssh1_session_key(p);
1123
1124 buffer_clear(m);
1125 buffer_put_int(m, rsafail);
1126 buffer_put_bignum2(m, p);
1127
1128 BN_clear_free(p);
1129
1130 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1131
1132 /* Turn on permissions for sessid passing */
1133 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1134
1135 return (0);
1136}
1137
1138int
1139mm_answer_sessid(int socket, Buffer *m)
1140{
1141 int i;
1142
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001143 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001144
1145 if (buffer_len(m) != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001146 fatal("%s: bad ssh1 session id", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001147 for (i = 0; i < 16; i++)
1148 session_id[i] = buffer_get_char(m);
1149
1150 /* Turn on permissions for getpwnam */
1151 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1152
1153 return (0);
1154}
1155
1156int
1157mm_answer_rsa_keyallowed(int socket, Buffer *m)
1158{
1159 BIGNUM *client_n;
1160 Key *key = NULL;
1161 u_char *blob = NULL;
1162 u_int blen = 0;
1163 int allowed = 0;
1164
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001165 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001166
Ben Lindstromdcf6bfb2002-06-06 20:57:17 +00001167 if (options.rsa_authentication && authctxt->valid) {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001168 if ((client_n = BN_new()) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001169 fatal("%s: BN_new", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001170 buffer_get_bignum2(m, client_n);
1171 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1172 BN_clear_free(client_n);
1173 }
1174 buffer_clear(m);
1175 buffer_put_int(m, allowed);
1176
1177 /* clear temporarily storage (used by generate challenge) */
1178 monitor_reset_key_state();
1179
1180 if (allowed && key != NULL) {
1181 key->type = KEY_RSA; /* cheat for key_to_blob */
1182 if (key_to_blob(key, &blob, &blen) == 0)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001183 fatal("%s: key_to_blob failed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001184 buffer_put_string(m, blob, blen);
1185
1186 /* Save temporarily for comparison in verify */
1187 key_blob = blob;
1188 key_bloblen = blen;
1189 key_blobtype = MM_RSAUSERKEY;
1190 key_free(key);
1191 }
1192
1193 mm_append_debug(m);
1194
1195 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1196
1197 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1198 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1199 return (0);
1200}
1201
1202int
1203mm_answer_rsa_challenge(int socket, Buffer *m)
1204{
1205 Key *key = NULL;
1206 u_char *blob;
1207 u_int blen;
1208
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001209 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001210
1211 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001212 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001213 blob = buffer_get_string(m, &blen);
1214 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001215 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001216 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001217 fatal("%s: key type mismatch", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001218 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001219 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001220
1221 if (ssh1_challenge)
1222 BN_clear_free(ssh1_challenge);
1223 ssh1_challenge = auth_rsa_generate_challenge(key);
1224
1225 buffer_clear(m);
1226 buffer_put_bignum2(m, ssh1_challenge);
1227
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001228 debug3("%s sending reply", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001229 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1230
1231 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1232 return (0);
1233}
1234
1235int
1236mm_answer_rsa_response(int socket, Buffer *m)
1237{
1238 Key *key = NULL;
1239 u_char *blob, *response;
1240 u_int blen, len;
1241 int success;
1242
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001243 debug3("%s entering", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001244
1245 if (!authctxt->valid)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001246 fatal("%s: authctxt not valid", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001247 if (ssh1_challenge == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001248 fatal("%s: no ssh1_challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001249
1250 blob = buffer_get_string(m, &blen);
1251 if (!monitor_allowed_key(blob, blen))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001252 fatal("%s: bad key, not previously allowed", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001253 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001254 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001255 if ((key = key_from_blob(blob, blen)) == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001256 fatal("%s: received bad key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001257 response = buffer_get_string(m, &len);
1258 if (len != 16)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001259 fatal("%s: received bad response to challenge", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001260 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1261
1262 key_free(key);
1263 xfree(response);
1264
1265 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1266
1267 /* reset state */
1268 BN_clear_free(ssh1_challenge);
1269 ssh1_challenge = NULL;
1270 monitor_reset_key_state();
1271
1272 buffer_clear(m);
1273 buffer_put_int(m, success);
1274 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1275
1276 return (success);
1277}
1278
1279int
1280mm_answer_term(int socket, Buffer *req)
1281{
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001282 extern struct monitor *pmonitor;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001283 int res, status;
1284
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001285 debug3("%s: tearing down sessions", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001286
1287 /* The child is terminating */
1288 session_destroy_all(&mm_session_close);
1289
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001290 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Ben Lindstrom47fd8112002-04-02 20:48:19 +00001291 if (errno != EINTR)
1292 exit(1);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001293
1294 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1295
1296 /* Terminate process */
1297 exit (res);
1298}
1299
1300void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001301monitor_apply_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001302{
1303 if (compat20) {
1304 set_newkeys(MODE_IN);
1305 set_newkeys(MODE_OUT);
1306 } else {
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001307 packet_set_protocol_flags(child_state.ssh1protoflags);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001308 packet_set_encryption_key(child_state.ssh1key,
1309 child_state.ssh1keylen, child_state.ssh1cipher);
1310 xfree(child_state.ssh1key);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001311 }
1312
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001313 /* for rc4 and other stateful ciphers */
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001314 packet_set_keycontext(MODE_OUT, child_state.keyout);
1315 xfree(child_state.keyout);
1316 packet_set_keycontext(MODE_IN, child_state.keyin);
1317 xfree(child_state.keyin);
1318
1319 if (!compat20) {
1320 packet_set_iv(MODE_OUT, child_state.ivout);
1321 xfree(child_state.ivout);
1322 packet_set_iv(MODE_IN, child_state.ivin);
1323 xfree(child_state.ivin);
1324 }
1325
1326 memcpy(&incoming_stream, &child_state.incoming,
1327 sizeof(incoming_stream));
1328 memcpy(&outgoing_stream, &child_state.outgoing,
1329 sizeof(outgoing_stream));
1330
1331 /* Update with new address */
Damien Miller2d6b8332002-06-21 15:59:49 +10001332 if (options.compression)
1333 mm_init_compression(pmonitor->m_zlib);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001334
1335 /* Network I/O buffers */
1336 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1337 buffer_clear(&input);
1338 buffer_append(&input, child_state.input, child_state.ilen);
1339 memset(child_state.input, 0, child_state.ilen);
1340 xfree(child_state.input);
1341
1342 buffer_clear(&output);
1343 buffer_append(&output, child_state.output, child_state.olen);
1344 memset(child_state.output, 0, child_state.olen);
1345 xfree(child_state.output);
1346}
1347
1348static Kex *
1349mm_get_kex(Buffer *m)
1350{
1351 Kex *kex;
1352 void *blob;
1353 u_int bloblen;
1354
1355 kex = xmalloc(sizeof(*kex));
1356 memset(kex, 0, sizeof(*kex));
1357 kex->session_id = buffer_get_string(m, &kex->session_id_len);
Ben Lindstromf67e0772002-06-06 20:58:19 +00001358 if ((session_id2 == NULL) ||
1359 (kex->session_id_len != session_id2_len) ||
1360 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1361 fatal("mm_get_get: internal error: bad session id");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001362 kex->we_need = buffer_get_int(m);
1363 kex->server = 1;
1364 kex->hostkey_type = buffer_get_int(m);
1365 kex->kex_type = buffer_get_int(m);
1366 blob = buffer_get_string(m, &bloblen);
1367 buffer_init(&kex->my);
1368 buffer_append(&kex->my, blob, bloblen);
1369 xfree(blob);
1370 blob = buffer_get_string(m, &bloblen);
1371 buffer_init(&kex->peer);
1372 buffer_append(&kex->peer, blob, bloblen);
1373 xfree(blob);
1374 kex->done = 1;
1375 kex->flags = buffer_get_int(m);
1376 kex->client_version_string = buffer_get_string(m, NULL);
1377 kex->server_version_string = buffer_get_string(m, NULL);
1378 kex->load_host_key=&get_hostkey_by_type;
1379 kex->host_key_index=&get_hostkey_index;
1380
1381 return (kex);
1382}
1383
1384/* This function requries careful sanity checking */
1385
1386void
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001387mm_get_keystate(struct monitor *pmonitor)
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001388{
1389 Buffer m;
1390 u_char *blob, *p;
1391 u_int bloblen, plen;
1392
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001393 debug3("%s: Waiting for new keys", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001394
1395 buffer_init(&m);
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001396 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001397 if (!compat20) {
1398 child_state.ssh1protoflags = buffer_get_int(&m);
1399 child_state.ssh1cipher = buffer_get_int(&m);
Ben Lindstrom402c6cc2002-06-21 00:43:42 +00001400 child_state.ssh1key = buffer_get_string(&m,
1401 &child_state.ssh1keylen);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001402 child_state.ivout = buffer_get_string(&m,
1403 &child_state.ivoutlen);
1404 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1405 goto skip;
1406 } else {
1407 /* Get the Kex for rekeying */
Ben Lindstrom7339b2a2002-05-15 16:25:01 +00001408 *pmonitor->m_pkex = mm_get_kex(&m);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001409 }
1410
1411 blob = buffer_get_string(&m, &bloblen);
1412 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1413 xfree(blob);
1414
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001415 debug3("%s: Waiting for second key", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001416 blob = buffer_get_string(&m, &bloblen);
1417 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1418 xfree(blob);
1419
1420 /* Now get sequence numbers for the packets */
1421 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1422 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1423
1424 skip:
1425 /* Get the key context */
1426 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1427 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1428
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001429 debug3("%s: Getting compression state", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001430 /* Get compression state */
1431 p = buffer_get_string(&m, &plen);
1432 if (plen != sizeof(child_state.outgoing))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001433 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001434 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1435 xfree(p);
1436
1437 p = buffer_get_string(&m, &plen);
1438 if (plen != sizeof(child_state.incoming))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001439 fatal("%s: bad request size", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001440 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1441 xfree(p);
1442
1443 /* Network I/O buffers */
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001444 debug3("%s: Getting Network I/O buffers", __func__);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001445 child_state.input = buffer_get_string(&m, &child_state.ilen);
1446 child_state.output = buffer_get_string(&m, &child_state.olen);
1447
1448 buffer_free(&m);
1449}
1450
1451
1452/* Allocation functions for zlib */
1453void *
1454mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1455{
Damien Miller530a7542002-06-26 23:27:11 +10001456 int len = size * ncount;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001457 void *address;
1458
Ben Lindstrom35a2cb92002-07-04 00:05:06 +00001459 if (len <= 0 || size > 65535 || ncount > 65535)
Damien Miller530a7542002-06-26 23:27:11 +10001460 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1461
1462 address = mm_malloc(mm, len);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001463
1464 return (address);
1465}
1466
1467void
1468mm_zfree(struct mm_master *mm, void *address)
1469{
1470 mm_free(mm, address);
1471}
1472
1473void
1474mm_init_compression(struct mm_master *mm)
1475{
1476 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1477 outgoing_stream.zfree = (free_func)mm_zfree;
1478 outgoing_stream.opaque = mm;
1479
1480 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1481 incoming_stream.zfree = (free_func)mm_zfree;
1482 incoming_stream.opaque = mm;
1483}
1484
1485/* XXX */
1486
1487#define FD_CLOSEONEXEC(x) do { \
1488 if (fcntl(x, F_SETFD, 1) == -1) \
1489 fatal("fcntl(%d, F_SETFD)", x); \
1490} while (0)
1491
1492static void
1493monitor_socketpair(int *pair)
1494{
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001495#ifdef HAVE_SOCKETPAIR
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001496 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001497 fatal("%s: socketpair", __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001498#else
1499 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +00001500 __func__);
Kevin Stevesfe6ca542002-04-10 22:04:54 +00001501#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001502 FD_CLOSEONEXEC(pair[0]);
1503 FD_CLOSEONEXEC(pair[1]);
1504}
1505
1506#define MM_MEMSIZE 65536
1507
1508struct monitor *
1509monitor_init(void)
1510{
1511 struct monitor *mon;
1512 int pair[2];
1513
1514 mon = xmalloc(sizeof(*mon));
1515
1516 monitor_socketpair(pair);
1517
1518 mon->m_recvfd = pair[0];
1519 mon->m_sendfd = pair[1];
1520
1521 /* Used to share zlib space across processes */
Damien Miller2d6b8332002-06-21 15:59:49 +10001522 if (options.compression) {
1523 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1524 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001525
Damien Miller2d6b8332002-06-21 15:59:49 +10001526 /* Compression needs to share state across borders */
1527 mm_init_compression(mon->m_zlib);
1528 }
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001529
1530 return mon;
1531}
1532
1533void
1534monitor_reinit(struct monitor *mon)
1535{
1536 int pair[2];
1537
1538 monitor_socketpair(pair);
1539
1540 mon->m_recvfd = pair[0];
1541 mon->m_sendfd = pair[1];
1542}