monitor.c revision 1.1 1 /* $NetBSD: monitor.c,v 1.1 2009/06/07 22:19:12 christos Exp $ */
2 /* $OpenBSD: monitor.c,v 1.101 2009/02/12 03:26:22 djm Exp $ */
3 /*
4 * Copyright 2002 Niels Provos <provos (at) citi.umich.edu>
5 * Copyright 2002 Markus Friedl <markus (at) openbsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/socket.h>
32 #include <sys/tree.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35
36 #include <openssl/dh.h>
37
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46
47 #include "xmalloc.h"
48 #include "ssh.h"
49 #include "key.h"
50 #include "buffer.h"
51 #include "hostfile.h"
52 #include "auth.h"
53 #include "cipher.h"
54 #include "kex.h"
55 #include "dh.h"
56 #include <zlib.h>
57 #include "packet.h"
58 #include "auth-options.h"
59 #include "sshpty.h"
60 #include "channels.h"
61 #include "session.h"
62 #include "sshlogin.h"
63 #include "canohost.h"
64 #include "log.h"
65 #include "servconf.h"
66 #include "monitor.h"
67 #include "monitor_mm.h"
68 #ifdef GSSAPI
69 #include "ssh-gss.h"
70 #endif
71 #include "monitor_wrap.h"
72 #include "monitor_fdpass.h"
73 #include "misc.h"
74 #include "compat.h"
75 #include "ssh2.h"
76 #include "jpake.h"
77
78 #ifdef GSSAPI
79 static Gssctxt *gsscontext = NULL;
80 #endif
81
82 /* Imports */
83 extern ServerOptions options;
84 extern u_int utmp_len;
85 extern Newkeys *current_keys[];
86 extern z_stream incoming_stream;
87 extern z_stream outgoing_stream;
88 extern u_char session_id[];
89 extern Buffer input, output;
90 extern Buffer auth_debug;
91 extern int auth_debug_init;
92 extern Buffer loginmsg;
93
94 /* State exported from the child */
95
96 struct {
97 z_stream incoming;
98 z_stream outgoing;
99 u_char *keyin;
100 u_int keyinlen;
101 u_char *keyout;
102 u_int keyoutlen;
103 u_char *ivin;
104 u_int ivinlen;
105 u_char *ivout;
106 u_int ivoutlen;
107 u_char *ssh1key;
108 u_int ssh1keylen;
109 int ssh1cipher;
110 int ssh1protoflags;
111 u_char *input;
112 u_int ilen;
113 u_char *output;
114 u_int olen;
115 } child_state;
116
117 /* Functions on the monitor that answer unprivileged requests */
118
119 int mm_answer_moduli(int, Buffer *);
120 int mm_answer_sign(int, Buffer *);
121 int mm_answer_pwnamallow(int, Buffer *);
122 int mm_answer_auth2_read_banner(int, Buffer *);
123 int mm_answer_authserv(int, Buffer *);
124 int mm_answer_authpassword(int, Buffer *);
125 int mm_answer_bsdauthquery(int, Buffer *);
126 int mm_answer_bsdauthrespond(int, Buffer *);
127 int mm_answer_skeyquery(int, Buffer *);
128 int mm_answer_skeyrespond(int, Buffer *);
129 int mm_answer_keyallowed(int, Buffer *);
130 int mm_answer_keyverify(int, Buffer *);
131 int mm_answer_pty(int, Buffer *);
132 int mm_answer_pty_cleanup(int, Buffer *);
133 int mm_answer_term(int, Buffer *);
134 int mm_answer_rsa_keyallowed(int, Buffer *);
135 int mm_answer_rsa_challenge(int, Buffer *);
136 int mm_answer_rsa_response(int, Buffer *);
137 int mm_answer_sesskey(int, Buffer *);
138 int mm_answer_sessid(int, Buffer *);
139 int mm_answer_jpake_get_pwdata(int, Buffer *);
140 int mm_answer_jpake_step1(int, Buffer *);
141 int mm_answer_jpake_step2(int, Buffer *);
142 int mm_answer_jpake_key_confirm(int, Buffer *);
143 int mm_answer_jpake_check_confirm(int, Buffer *);
144
145 #ifdef GSSAPI
146 int mm_answer_gss_setup_ctx(int, Buffer *);
147 int mm_answer_gss_accept_ctx(int, Buffer *);
148 int mm_answer_gss_userok(int, Buffer *);
149 int mm_answer_gss_checkmic(int, Buffer *);
150 #endif
151
152 static Authctxt *authctxt;
153 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
154
155 /* local state for key verify */
156 static u_char *key_blob = NULL;
157 static u_int key_bloblen = 0;
158 static int key_blobtype = MM_NOKEY;
159 static char *hostbased_cuser = NULL;
160 static char *hostbased_chost = NULL;
161 static char *auth_method = "unknown";
162 static u_int session_id2_len = 0;
163 static u_char *session_id2 = NULL;
164 static pid_t monitor_child_pid;
165
166 struct mon_table {
167 enum monitor_reqtype type;
168 int flags;
169 int (*f)(int, Buffer *);
170 };
171
172 #define MON_ISAUTH 0x0004 /* Required for Authentication */
173 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
174 #define MON_ONCE 0x0010 /* Disable after calling */
175 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
176
177 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
178
179 #define MON_PERMIT 0x1000 /* Request is permitted */
180
181 struct mon_table mon_dispatch_proto20[] = {
182 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
183 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
184 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
185 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
186 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
187 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
188 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
189 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
190 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
191 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
192 #ifdef GSSAPI
193 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
194 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
195 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
196 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
197 #endif
198 #ifdef JPAKE
199 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
200 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
201 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
202 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
203 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
204 #endif
205 {0, 0, NULL}
206 };
207
208 struct mon_table mon_dispatch_postauth20[] = {
209 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
210 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
211 {MONITOR_REQ_PTY, 0, mm_answer_pty},
212 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
213 {MONITOR_REQ_TERM, 0, mm_answer_term},
214 {0, 0, NULL}
215 };
216
217 struct mon_table mon_dispatch_proto15[] = {
218 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
219 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
220 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
221 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
222 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
223 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
224 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
225 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
226 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
227 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
228 {0, 0, NULL}
229 };
230
231 struct 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
238 struct mon_table *mon_dispatch;
239
240 /* Specifies if a certain message is allowed at the moment */
241
242 static void
243 monitor_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
255 static void
256 monitor_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
269 void
270 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
271 {
272 struct mon_table *ent;
273 int authenticated = 0;
274
275 debug3("preauth child monitor started");
276
277 authctxt = _authctxt;
278 memset(authctxt, 0, sizeof(*authctxt));
279
280 if (compat20) {
281 mon_dispatch = mon_dispatch_proto20;
282
283 /* Permit requests for moduli and signatures */
284 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
285 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
286 } else {
287 mon_dispatch = mon_dispatch_proto15;
288
289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
290 }
291
292 /* The first few requests do not require asynchronous access */
293 while (!authenticated) {
294 auth_method = "unknown";
295 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
296 if (authenticated) {
297 if (!(ent->flags & MON_AUTHDECIDE))
298 fatal("%s: unexpected authentication from %d",
299 __func__, ent->type);
300 if (authctxt->pw->pw_uid == 0 &&
301 !auth_root_allowed(auth_method))
302 authenticated = 0;
303 }
304
305 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
306 auth_log(authctxt, authenticated, auth_method,
307 compat20 ? " ssh2" : "");
308 if (!authenticated)
309 authctxt->failures++;
310 }
311 #ifdef JPAKE
312 /* Cleanup JPAKE context after authentication */
313 if (ent->flags & MON_AUTHDECIDE) {
314 if (authctxt->jpake_ctx != NULL) {
315 jpake_free(authctxt->jpake_ctx);
316 authctxt->jpake_ctx = NULL;
317 }
318 }
319 #endif
320 }
321
322 if (!authctxt->valid)
323 fatal("%s: authenticated invalid user", __func__);
324 if (strcmp(auth_method, "unknown") == 0)
325 fatal("%s: authentication method name unknown", __func__);
326
327 debug("%s: %s has been authenticated by privileged process",
328 __func__, authctxt->user);
329
330 mm_get_keystate(pmonitor);
331 }
332
333 static void
334 monitor_set_child_handler(pid_t pid)
335 {
336 monitor_child_pid = pid;
337 }
338
339 static void
340 monitor_child_handler(int sig)
341 {
342 kill(monitor_child_pid, sig);
343 }
344
345 void
346 monitor_child_postauth(struct monitor *pmonitor)
347 {
348 monitor_set_child_handler(pmonitor->m_pid);
349 signal(SIGHUP, &monitor_child_handler);
350 signal(SIGTERM, &monitor_child_handler);
351 signal(SIGINT, &monitor_child_handler);
352
353 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 } else {
361 mon_dispatch = mon_dispatch_postauth15;
362 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
363 }
364 if (!no_pty_flag) {
365 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
366 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
367 }
368
369 for (;;)
370 monitor_read(pmonitor, mon_dispatch, NULL);
371 }
372
373 void
374 monitor_sync(struct monitor *pmonitor)
375 {
376 if (options.compression) {
377 /* The member allocation is not visible, so sync it */
378 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
379 }
380 }
381
382 int
383 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
384 struct mon_table **pent)
385 {
386 Buffer m;
387 int ret;
388 u_char type;
389
390 buffer_init(&m);
391
392 mm_request_receive(pmonitor->m_sendfd, &m);
393 type = buffer_get_char(&m);
394
395 debug3("%s: checking request %d", __func__, type);
396
397 while (ent->f != NULL) {
398 if (ent->type == type)
399 break;
400 ent++;
401 }
402
403 if (ent->f != NULL) {
404 if (!(ent->flags & MON_PERMIT))
405 fatal("%s: unpermitted request %d", __func__,
406 type);
407 ret = (*ent->f)(pmonitor->m_sendfd, &m);
408 buffer_free(&m);
409
410 /* The child may use this request only once, disable it */
411 if (ent->flags & MON_ONCE) {
412 debug2("%s: %d used once, disabling now", __func__,
413 type);
414 ent->flags &= ~MON_PERMIT;
415 }
416
417 if (pent != NULL)
418 *pent = ent;
419
420 return ret;
421 }
422
423 fatal("%s: unsupported request: %d", __func__, type);
424
425 /* NOTREACHED */
426 return (-1);
427 }
428
429 /* allowed key state */
430 static int
431 monitor_allowed_key(u_char *blob, u_int bloblen)
432 {
433 /* make sure key is allowed */
434 if (key_blob == NULL || key_bloblen != bloblen ||
435 memcmp(key_blob, blob, key_bloblen))
436 return (0);
437 return (1);
438 }
439
440 static void
441 monitor_reset_key_state(void)
442 {
443 /* reset state */
444 if (key_blob != NULL)
445 xfree(key_blob);
446 if (hostbased_cuser != NULL)
447 xfree(hostbased_cuser);
448 if (hostbased_chost != NULL)
449 xfree(hostbased_chost);
450 key_blob = NULL;
451 key_bloblen = 0;
452 key_blobtype = MM_NOKEY;
453 hostbased_cuser = NULL;
454 hostbased_chost = NULL;
455 }
456
457 int
458 mm_answer_moduli(int sock, Buffer *m)
459 {
460 DH *dh;
461 int min, want, max;
462
463 min = buffer_get_int(m);
464 want = buffer_get_int(m);
465 max = buffer_get_int(m);
466
467 debug3("%s: got parameters: %d %d %d",
468 __func__, min, want, max);
469 /* We need to check here, too, in case the child got corrupted */
470 if (max < min || want < min || max < want)
471 fatal("%s: bad parameters: %d %d %d",
472 __func__, min, want, max);
473
474 buffer_clear(m);
475
476 dh = choose_dh(min, want, max);
477 if (dh == NULL) {
478 buffer_put_char(m, 0);
479 return (0);
480 } else {
481 /* Send first bignum */
482 buffer_put_char(m, 1);
483 buffer_put_bignum2(m, dh->p);
484 buffer_put_bignum2(m, dh->g);
485
486 DH_free(dh);
487 }
488 mm_request_send(sock, MONITOR_ANS_MODULI, m);
489 return (0);
490 }
491
492 int
493 mm_answer_sign(int sock, Buffer *m)
494 {
495 Key *key;
496 u_char *p;
497 u_char *signature;
498 u_int siglen, datlen;
499 int keyid;
500
501 debug3("%s", __func__);
502
503 keyid = buffer_get_int(m);
504 p = buffer_get_string(m, &datlen);
505
506 /*
507 * Supported KEX types will only return SHA1 (20 byte) or
508 * SHA256 (32 byte) hashes
509 */
510 if (datlen != 20 && datlen != 32)
511 fatal("%s: data length incorrect: %u", __func__, datlen);
512
513 /* save session id, it will be passed on the first call */
514 if (session_id2_len == 0) {
515 session_id2_len = datlen;
516 session_id2 = xmalloc(session_id2_len);
517 memcpy(session_id2, p, session_id2_len);
518 }
519
520 if ((key = get_hostkey_by_index(keyid)) == NULL)
521 fatal("%s: no hostkey from index %d", __func__, keyid);
522 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
523 fatal("%s: key_sign failed", __func__);
524
525 debug3("%s: signature %p(%u)", __func__, signature, siglen);
526
527 buffer_clear(m);
528 buffer_put_string(m, signature, siglen);
529
530 xfree(p);
531 xfree(signature);
532
533 mm_request_send(sock, MONITOR_ANS_SIGN, m);
534
535 /* Turn on permissions for getpwnam */
536 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
537
538 return (0);
539 }
540
541 /* Retrieves the password entry and also checks if the user is permitted */
542
543 int
544 mm_answer_pwnamallow(int sock, Buffer *m)
545 {
546 char *username;
547 struct passwd *pwent;
548 int allowed = 0;
549
550 debug3("%s", __func__);
551
552 if (authctxt->attempt++ != 0)
553 fatal("%s: multiple attempts for getpwnam", __func__);
554
555 username = buffer_get_string(m, NULL);
556
557 pwent = getpwnamallow(username);
558
559 authctxt->user = xstrdup(username);
560 setproctitle("%s [priv]", pwent ? username : "unknown");
561 xfree(username);
562
563 buffer_clear(m);
564
565 if (pwent == NULL) {
566 buffer_put_char(m, 0);
567 authctxt->pw = fakepw();
568 goto out;
569 }
570
571 allowed = 1;
572 authctxt->pw = pwent;
573 authctxt->valid = 1;
574
575 buffer_put_char(m, 1);
576 buffer_put_string(m, pwent, sizeof(struct passwd));
577 buffer_put_cstring(m, pwent->pw_name);
578 buffer_put_cstring(m, "*");
579 buffer_put_cstring(m, pwent->pw_gecos);
580 buffer_put_cstring(m, pwent->pw_class);
581 buffer_put_cstring(m, pwent->pw_dir);
582 buffer_put_cstring(m, pwent->pw_shell);
583
584 out:
585 buffer_put_string(m, &options, sizeof(options));
586 if (options.banner != NULL)
587 buffer_put_cstring(m, options.banner);
588 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
589 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
590
591 /* For SSHv1 allow authentication now */
592 if (!compat20)
593 monitor_permit_authentications(1);
594 else {
595 /* Allow service/style information on the auth context */
596 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
597 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
598 }
599
600
601 return (0);
602 }
603
604 int mm_answer_auth2_read_banner(int sock, Buffer *m)
605 {
606 char *banner;
607
608 buffer_clear(m);
609 banner = auth2_read_banner();
610 buffer_put_cstring(m, banner != NULL ? banner : "");
611 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
612
613 if (banner != NULL)
614 xfree(banner);
615
616 return (0);
617 }
618
619 int
620 mm_answer_authserv(int sock, Buffer *m)
621 {
622 monitor_permit_authentications(1);
623
624 authctxt->service = buffer_get_string(m, NULL);
625 authctxt->style = buffer_get_string(m, NULL);
626 debug3("%s: service=%s, style=%s",
627 __func__, authctxt->service, authctxt->style);
628
629 if (strlen(authctxt->style) == 0) {
630 xfree(authctxt->style);
631 authctxt->style = NULL;
632 }
633
634 return (0);
635 }
636
637 int
638 mm_answer_authpassword(int sock, Buffer *m)
639 {
640 static int call_count;
641 char *passwd;
642 int authenticated;
643 u_int plen;
644
645 passwd = buffer_get_string(m, &plen);
646 /* Only authenticate if the context is valid */
647 authenticated = options.password_authentication &&
648 auth_password(authctxt, passwd);
649 memset(passwd, 0, strlen(passwd));
650 xfree(passwd);
651
652 buffer_clear(m);
653 buffer_put_int(m, authenticated);
654
655 debug3("%s: sending result %d", __func__, authenticated);
656 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
657
658 call_count++;
659 if (plen == 0 && call_count == 1)
660 auth_method = "none";
661 else
662 auth_method = "password";
663
664 /* Causes monitor loop to terminate if authenticated */
665 return (authenticated);
666 }
667
668 int
669 mm_answer_bsdauthquery(int sock, Buffer *m)
670 {
671 char *name, *infotxt;
672 u_int numprompts;
673 u_int *echo_on;
674 char **prompts;
675 u_int success;
676
677 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
678 &prompts, &echo_on) < 0 ? 0 : 1;
679
680 buffer_clear(m);
681 buffer_put_int(m, success);
682 if (success)
683 buffer_put_cstring(m, prompts[0]);
684
685 debug3("%s: sending challenge success: %u", __func__, success);
686 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
687
688 if (success) {
689 xfree(name);
690 xfree(infotxt);
691 xfree(prompts);
692 xfree(echo_on);
693 }
694
695 return (0);
696 }
697
698 int
699 mm_answer_bsdauthrespond(int sock, Buffer *m)
700 {
701 char *response;
702 int authok;
703
704 if (authctxt->as == 0)
705 fatal("%s: no bsd auth session", __func__);
706
707 response = buffer_get_string(m, NULL);
708 authok = options.challenge_response_authentication &&
709 auth_userresponse(authctxt->as, response, 0);
710 authctxt->as = NULL;
711 debug3("%s: <%s> = <%d>", __func__, response, authok);
712 xfree(response);
713
714 buffer_clear(m);
715 buffer_put_int(m, authok);
716
717 debug3("%s: sending authenticated: %d", __func__, authok);
718 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
719
720 auth_method = "bsdauth";
721
722 return (authok != 0);
723 }
724
725
726 static void
727 mm_append_debug(Buffer *m)
728 {
729 if (auth_debug_init && buffer_len(&auth_debug)) {
730 debug3("%s: Appending debug messages for child", __func__);
731 buffer_append(m, buffer_ptr(&auth_debug),
732 buffer_len(&auth_debug));
733 buffer_clear(&auth_debug);
734 }
735 }
736
737 int
738 mm_answer_keyallowed(int sock, Buffer *m)
739 {
740 Key *key;
741 char *cuser, *chost;
742 u_char *blob;
743 u_int bloblen;
744 enum mm_keytype type = 0;
745 int allowed = 0;
746
747 debug3("%s entering", __func__);
748
749 type = buffer_get_int(m);
750 cuser = buffer_get_string(m, NULL);
751 chost = buffer_get_string(m, NULL);
752 blob = buffer_get_string(m, &bloblen);
753
754 key = key_from_blob(blob, bloblen);
755
756 if ((compat20 && type == MM_RSAHOSTKEY) ||
757 (!compat20 && type != MM_RSAHOSTKEY))
758 fatal("%s: key type and protocol mismatch", __func__);
759
760 debug3("%s: key_from_blob: %p", __func__, key);
761
762 if (key != NULL && authctxt->valid) {
763 switch (type) {
764 case MM_USERKEY:
765 allowed = options.pubkey_authentication &&
766 user_key_allowed(authctxt->pw, key);
767 auth_method = "publickey";
768 if (options.pubkey_authentication && allowed != 1)
769 auth_clear_options();
770 break;
771 case MM_HOSTKEY:
772 allowed = options.hostbased_authentication &&
773 hostbased_key_allowed(authctxt->pw,
774 cuser, chost, key);
775 auth_method = "hostbased";
776 break;
777 case MM_RSAHOSTKEY:
778 key->type = KEY_RSA1; /* XXX */
779 allowed = options.rhosts_rsa_authentication &&
780 auth_rhosts_rsa_key_allowed(authctxt->pw,
781 cuser, chost, key);
782 if (options.rhosts_rsa_authentication && allowed != 1)
783 auth_clear_options();
784 auth_method = "rsa";
785 break;
786 default:
787 fatal("%s: unknown key type %d", __func__, type);
788 break;
789 }
790 }
791 if (key != NULL)
792 key_free(key);
793
794 /* clear temporarily storage (used by verify) */
795 monitor_reset_key_state();
796
797 if (allowed) {
798 /* Save temporarily for comparison in verify */
799 key_blob = blob;
800 key_bloblen = bloblen;
801 key_blobtype = type;
802 hostbased_cuser = cuser;
803 hostbased_chost = chost;
804 } else {
805 /* Log failed attempt */
806 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
807 xfree(blob);
808 xfree(cuser);
809 xfree(chost);
810 }
811
812 debug3("%s: key %p is %s",
813 __func__, key, allowed ? "allowed" : "not allowed");
814
815 buffer_clear(m);
816 buffer_put_int(m, allowed);
817 buffer_put_int(m, forced_command != NULL);
818
819 mm_append_debug(m);
820
821 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
822
823 if (type == MM_RSAHOSTKEY)
824 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
825
826 return (0);
827 }
828
829 static int
830 monitor_valid_userblob(u_char *data, u_int datalen)
831 {
832 Buffer b;
833 char *p;
834 u_int len;
835 int fail = 0;
836
837 buffer_init(&b);
838 buffer_append(&b, data, datalen);
839
840 if (datafellows & SSH_OLD_SESSIONID) {
841 p = buffer_ptr(&b);
842 len = buffer_len(&b);
843 if ((session_id2 == NULL) ||
844 (len < session_id2_len) ||
845 (memcmp(p, session_id2, session_id2_len) != 0))
846 fail++;
847 buffer_consume(&b, session_id2_len);
848 } else {
849 p = buffer_get_string(&b, &len);
850 if ((session_id2 == NULL) ||
851 (len != session_id2_len) ||
852 (memcmp(p, session_id2, session_id2_len) != 0))
853 fail++;
854 xfree(p);
855 }
856 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
857 fail++;
858 p = buffer_get_string(&b, NULL);
859 if (strcmp(authctxt->user, p) != 0) {
860 logit("wrong user name passed to monitor: expected %s != %.100s",
861 authctxt->user, p);
862 fail++;
863 }
864 xfree(p);
865 buffer_skip_string(&b);
866 if (datafellows & SSH_BUG_PKAUTH) {
867 if (!buffer_get_char(&b))
868 fail++;
869 } else {
870 p = buffer_get_string(&b, NULL);
871 if (strcmp("publickey", p) != 0)
872 fail++;
873 xfree(p);
874 if (!buffer_get_char(&b))
875 fail++;
876 buffer_skip_string(&b);
877 }
878 buffer_skip_string(&b);
879 if (buffer_len(&b) != 0)
880 fail++;
881 buffer_free(&b);
882 return (fail == 0);
883 }
884
885 static int
886 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
887 char *chost)
888 {
889 Buffer b;
890 char *p;
891 u_int len;
892 int fail = 0;
893
894 buffer_init(&b);
895 buffer_append(&b, data, datalen);
896
897 p = buffer_get_string(&b, &len);
898 if ((session_id2 == NULL) ||
899 (len != session_id2_len) ||
900 (memcmp(p, session_id2, session_id2_len) != 0))
901 fail++;
902 xfree(p);
903
904 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
905 fail++;
906 p = buffer_get_string(&b, NULL);
907 if (strcmp(authctxt->user, p) != 0) {
908 logit("wrong user name passed to monitor: expected %s != %.100s",
909 authctxt->user, p);
910 fail++;
911 }
912 xfree(p);
913 buffer_skip_string(&b); /* service */
914 p = buffer_get_string(&b, NULL);
915 if (strcmp(p, "hostbased") != 0)
916 fail++;
917 xfree(p);
918 buffer_skip_string(&b); /* pkalg */
919 buffer_skip_string(&b); /* pkblob */
920
921 /* verify client host, strip trailing dot if necessary */
922 p = buffer_get_string(&b, NULL);
923 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
924 p[len - 1] = '\0';
925 if (strcmp(p, chost) != 0)
926 fail++;
927 xfree(p);
928
929 /* verify client user */
930 p = buffer_get_string(&b, NULL);
931 if (strcmp(p, cuser) != 0)
932 fail++;
933 xfree(p);
934
935 if (buffer_len(&b) != 0)
936 fail++;
937 buffer_free(&b);
938 return (fail == 0);
939 }
940
941 int
942 mm_answer_keyverify(int sock, Buffer *m)
943 {
944 Key *key;
945 u_char *signature, *data, *blob;
946 u_int signaturelen, datalen, bloblen;
947 int verified = 0;
948 int valid_data = 0;
949
950 blob = buffer_get_string(m, &bloblen);
951 signature = buffer_get_string(m, &signaturelen);
952 data = buffer_get_string(m, &datalen);
953
954 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
955 !monitor_allowed_key(blob, bloblen))
956 fatal("%s: bad key, not previously allowed", __func__);
957
958 key = key_from_blob(blob, bloblen);
959 if (key == NULL)
960 fatal("%s: bad public key blob", __func__);
961
962 switch (key_blobtype) {
963 case MM_USERKEY:
964 valid_data = monitor_valid_userblob(data, datalen);
965 break;
966 case MM_HOSTKEY:
967 valid_data = monitor_valid_hostbasedblob(data, datalen,
968 hostbased_cuser, hostbased_chost);
969 break;
970 default:
971 valid_data = 0;
972 break;
973 }
974 if (!valid_data)
975 fatal("%s: bad signature data blob", __func__);
976
977 verified = key_verify(key, signature, signaturelen, data, datalen);
978 debug3("%s: key %p signature %s",
979 __func__, key, (verified == 1) ? "verified" : "unverified");
980
981 key_free(key);
982 xfree(blob);
983 xfree(signature);
984 xfree(data);
985
986 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
987
988 monitor_reset_key_state();
989
990 buffer_clear(m);
991 buffer_put_int(m, verified);
992 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
993
994 return (verified == 1);
995 }
996
997 static void
998 mm_record_login(Session *s, struct passwd *pw)
999 {
1000 socklen_t fromlen;
1001 struct sockaddr_storage from;
1002
1003 /*
1004 * Get IP address of client. If the connection is not a socket, let
1005 * the address be 0.0.0.0.
1006 */
1007 memset(&from, 0, sizeof(from));
1008 fromlen = sizeof(from);
1009 if (packet_connection_is_on_socket()) {
1010 if (getpeername(packet_get_connection_in(),
1011 (struct sockaddr *)&from, &fromlen) < 0) {
1012 debug("getpeername: %.100s", strerror(errno));
1013 cleanup_exit(255);
1014 }
1015 }
1016 /* Record that there was a login on that tty from the remote host. */
1017 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1018 get_remote_name_or_ip(utmp_len, options.use_dns),
1019 (struct sockaddr *)&from, fromlen);
1020 }
1021
1022 static void
1023 mm_session_close(Session *s)
1024 {
1025 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1026 if (s->ttyfd != -1) {
1027 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1028 session_pty_cleanup2(s);
1029 }
1030 session_unused(s->self);
1031 }
1032
1033 int
1034 mm_answer_pty(int sock, Buffer *m)
1035 {
1036 extern struct monitor *pmonitor;
1037 Session *s;
1038 int res, fd0;
1039
1040 debug3("%s entering", __func__);
1041
1042 buffer_clear(m);
1043 s = session_new();
1044 if (s == NULL)
1045 goto error;
1046 s->authctxt = authctxt;
1047 s->pw = authctxt->pw;
1048 s->pid = pmonitor->m_pid;
1049 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1050 if (res == 0)
1051 goto error;
1052 pty_setowner(authctxt->pw, s->tty);
1053
1054 buffer_put_int(m, 1);
1055 buffer_put_cstring(m, s->tty);
1056
1057 /* We need to trick ttyslot */
1058 if (dup2(s->ttyfd, 0) == -1)
1059 fatal("%s: dup2", __func__);
1060
1061 mm_record_login(s, authctxt->pw);
1062
1063 /* Now we can close the file descriptor again */
1064 close(0);
1065
1066 /* send messages generated by record_login */
1067 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1068 buffer_clear(&loginmsg);
1069
1070 mm_request_send(sock, MONITOR_ANS_PTY, m);
1071
1072 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1073 mm_send_fd(sock, s->ttyfd) == -1)
1074 fatal("%s: send fds failed", __func__);
1075
1076 /* make sure nothing uses fd 0 */
1077 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1078 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1079 if (fd0 != 0)
1080 error("%s: fd0 %d != 0", __func__, fd0);
1081
1082 /* slave is not needed */
1083 close(s->ttyfd);
1084 s->ttyfd = s->ptyfd;
1085 /* no need to dup() because nobody closes ptyfd */
1086 s->ptymaster = s->ptyfd;
1087
1088 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1089
1090 return (0);
1091
1092 error:
1093 if (s != NULL)
1094 mm_session_close(s);
1095 buffer_put_int(m, 0);
1096 mm_request_send(sock, MONITOR_ANS_PTY, m);
1097 return (0);
1098 }
1099
1100 int
1101 mm_answer_pty_cleanup(int sock, Buffer *m)
1102 {
1103 Session *s;
1104 char *tty;
1105
1106 debug3("%s entering", __func__);
1107
1108 tty = buffer_get_string(m, NULL);
1109 if ((s = session_by_tty(tty)) != NULL)
1110 mm_session_close(s);
1111 buffer_clear(m);
1112 xfree(tty);
1113 return (0);
1114 }
1115
1116 int
1117 mm_answer_sesskey(int sock, Buffer *m)
1118 {
1119 BIGNUM *p;
1120 int rsafail;
1121
1122 /* Turn off permissions */
1123 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1124
1125 if ((p = BN_new()) == NULL)
1126 fatal("%s: BN_new", __func__);
1127
1128 buffer_get_bignum2(m, p);
1129
1130 rsafail = ssh1_session_key(p);
1131
1132 buffer_clear(m);
1133 buffer_put_int(m, rsafail);
1134 buffer_put_bignum2(m, p);
1135
1136 BN_clear_free(p);
1137
1138 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1139
1140 /* Turn on permissions for sessid passing */
1141 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1142
1143 return (0);
1144 }
1145
1146 int
1147 mm_answer_sessid(int sock, Buffer *m)
1148 {
1149 int i;
1150
1151 debug3("%s entering", __func__);
1152
1153 if (buffer_len(m) != 16)
1154 fatal("%s: bad ssh1 session id", __func__);
1155 for (i = 0; i < 16; i++)
1156 session_id[i] = buffer_get_char(m);
1157
1158 /* Turn on permissions for getpwnam */
1159 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1160
1161 return (0);
1162 }
1163
1164 int
1165 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1166 {
1167 BIGNUM *client_n;
1168 Key *key = NULL;
1169 u_char *blob = NULL;
1170 u_int blen = 0;
1171 int allowed = 0;
1172
1173 debug3("%s entering", __func__);
1174
1175 auth_method = "rsa";
1176 if (options.rsa_authentication && authctxt->valid) {
1177 if ((client_n = BN_new()) == NULL)
1178 fatal("%s: BN_new", __func__);
1179 buffer_get_bignum2(m, client_n);
1180 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1181 BN_clear_free(client_n);
1182 }
1183 buffer_clear(m);
1184 buffer_put_int(m, allowed);
1185 buffer_put_int(m, forced_command != NULL);
1186
1187 /* clear temporarily storage (used by generate challenge) */
1188 monitor_reset_key_state();
1189
1190 if (allowed && key != NULL) {
1191 key->type = KEY_RSA; /* cheat for key_to_blob */
1192 if (key_to_blob(key, &blob, &blen) == 0)
1193 fatal("%s: key_to_blob failed", __func__);
1194 buffer_put_string(m, blob, blen);
1195
1196 /* Save temporarily for comparison in verify */
1197 key_blob = blob;
1198 key_bloblen = blen;
1199 key_blobtype = MM_RSAUSERKEY;
1200 }
1201 if (key != NULL)
1202 key_free(key);
1203
1204 mm_append_debug(m);
1205
1206 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1207
1208 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1209 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1210 return (0);
1211 }
1212
1213 int
1214 mm_answer_rsa_challenge(int sock, Buffer *m)
1215 {
1216 Key *key = NULL;
1217 u_char *blob;
1218 u_int blen;
1219
1220 debug3("%s entering", __func__);
1221
1222 if (!authctxt->valid)
1223 fatal("%s: authctxt not valid", __func__);
1224 blob = buffer_get_string(m, &blen);
1225 if (!monitor_allowed_key(blob, blen))
1226 fatal("%s: bad key, not previously allowed", __func__);
1227 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1228 fatal("%s: key type mismatch", __func__);
1229 if ((key = key_from_blob(blob, blen)) == NULL)
1230 fatal("%s: received bad key", __func__);
1231 if (key->type != KEY_RSA)
1232 fatal("%s: received bad key type %d", __func__, key->type);
1233 key->type = KEY_RSA1;
1234 if (ssh1_challenge)
1235 BN_clear_free(ssh1_challenge);
1236 ssh1_challenge = auth_rsa_generate_challenge(key);
1237
1238 buffer_clear(m);
1239 buffer_put_bignum2(m, ssh1_challenge);
1240
1241 debug3("%s sending reply", __func__);
1242 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1243
1244 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1245
1246 xfree(blob);
1247 key_free(key);
1248 return (0);
1249 }
1250
1251 int
1252 mm_answer_rsa_response(int sock, Buffer *m)
1253 {
1254 Key *key = NULL;
1255 u_char *blob, *response;
1256 u_int blen, len;
1257 int success;
1258
1259 debug3("%s entering", __func__);
1260
1261 if (!authctxt->valid)
1262 fatal("%s: authctxt not valid", __func__);
1263 if (ssh1_challenge == NULL)
1264 fatal("%s: no ssh1_challenge", __func__);
1265
1266 blob = buffer_get_string(m, &blen);
1267 if (!monitor_allowed_key(blob, blen))
1268 fatal("%s: bad key, not previously allowed", __func__);
1269 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1270 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1271 if ((key = key_from_blob(blob, blen)) == NULL)
1272 fatal("%s: received bad key", __func__);
1273 response = buffer_get_string(m, &len);
1274 if (len != 16)
1275 fatal("%s: received bad response to challenge", __func__);
1276 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1277
1278 xfree(blob);
1279 key_free(key);
1280 xfree(response);
1281
1282 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1283
1284 /* reset state */
1285 BN_clear_free(ssh1_challenge);
1286 ssh1_challenge = NULL;
1287 monitor_reset_key_state();
1288
1289 buffer_clear(m);
1290 buffer_put_int(m, success);
1291 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1292
1293 return (success);
1294 }
1295
1296 int
1297 mm_answer_term(int sock, Buffer *req)
1298 {
1299 extern struct monitor *pmonitor;
1300 int res, status;
1301
1302 debug3("%s: tearing down sessions", __func__);
1303
1304 /* The child is terminating */
1305 session_destroy_all(&mm_session_close);
1306
1307 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1308 if (errno != EINTR)
1309 exit(1);
1310
1311 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1312
1313 /* Terminate process */
1314 exit(res);
1315 }
1316
1317 void
1318 monitor_apply_keystate(struct monitor *pmonitor)
1319 {
1320 if (compat20) {
1321 set_newkeys(MODE_IN);
1322 set_newkeys(MODE_OUT);
1323 } else {
1324 packet_set_protocol_flags(child_state.ssh1protoflags);
1325 packet_set_encryption_key(child_state.ssh1key,
1326 child_state.ssh1keylen, child_state.ssh1cipher);
1327 xfree(child_state.ssh1key);
1328 }
1329
1330 /* for rc4 and other stateful ciphers */
1331 packet_set_keycontext(MODE_OUT, child_state.keyout);
1332 xfree(child_state.keyout);
1333 packet_set_keycontext(MODE_IN, child_state.keyin);
1334 xfree(child_state.keyin);
1335
1336 if (!compat20) {
1337 packet_set_iv(MODE_OUT, child_state.ivout);
1338 xfree(child_state.ivout);
1339 packet_set_iv(MODE_IN, child_state.ivin);
1340 xfree(child_state.ivin);
1341 }
1342
1343 memcpy(&incoming_stream, &child_state.incoming,
1344 sizeof(incoming_stream));
1345 memcpy(&outgoing_stream, &child_state.outgoing,
1346 sizeof(outgoing_stream));
1347
1348 /* Update with new address */
1349 if (options.compression)
1350 mm_init_compression(pmonitor->m_zlib);
1351
1352 /* Network I/O buffers */
1353 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1354 buffer_clear(&input);
1355 buffer_append(&input, child_state.input, child_state.ilen);
1356 memset(child_state.input, 0, child_state.ilen);
1357 xfree(child_state.input);
1358
1359 buffer_clear(&output);
1360 buffer_append(&output, child_state.output, child_state.olen);
1361 memset(child_state.output, 0, child_state.olen);
1362 xfree(child_state.output);
1363 }
1364
1365 static Kex *
1366 mm_get_kex(Buffer *m)
1367 {
1368 Kex *kex;
1369 void *blob;
1370 u_int bloblen;
1371
1372 kex = xcalloc(1, sizeof(*kex));
1373 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1374 if ((session_id2 == NULL) ||
1375 (kex->session_id_len != session_id2_len) ||
1376 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1377 fatal("mm_get_get: internal error: bad session id");
1378 kex->we_need = buffer_get_int(m);
1379 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1380 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1381 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1382 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1383 kex->server = 1;
1384 kex->hostkey_type = buffer_get_int(m);
1385 kex->kex_type = buffer_get_int(m);
1386 blob = buffer_get_string(m, &bloblen);
1387 buffer_init(&kex->my);
1388 buffer_append(&kex->my, blob, bloblen);
1389 xfree(blob);
1390 blob = buffer_get_string(m, &bloblen);
1391 buffer_init(&kex->peer);
1392 buffer_append(&kex->peer, blob, bloblen);
1393 xfree(blob);
1394 kex->done = 1;
1395 kex->flags = buffer_get_int(m);
1396 kex->client_version_string = buffer_get_string(m, NULL);
1397 kex->server_version_string = buffer_get_string(m, NULL);
1398 kex->load_host_key=&get_hostkey_by_type;
1399 kex->host_key_index=&get_hostkey_index;
1400
1401 return (kex);
1402 }
1403
1404 /* This function requries careful sanity checking */
1405
1406 void
1407 mm_get_keystate(struct monitor *pmonitor)
1408 {
1409 Buffer m;
1410 u_char *blob, *p;
1411 u_int bloblen, plen;
1412 u_int32_t seqnr, packets;
1413 u_int64_t blocks, bytes;
1414
1415 debug3("%s: Waiting for new keys", __func__);
1416
1417 buffer_init(&m);
1418 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1419 if (!compat20) {
1420 child_state.ssh1protoflags = buffer_get_int(&m);
1421 child_state.ssh1cipher = buffer_get_int(&m);
1422 child_state.ssh1key = buffer_get_string(&m,
1423 &child_state.ssh1keylen);
1424 child_state.ivout = buffer_get_string(&m,
1425 &child_state.ivoutlen);
1426 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1427 goto skip;
1428 } else {
1429 /* Get the Kex for rekeying */
1430 *pmonitor->m_pkex = mm_get_kex(&m);
1431 }
1432
1433 blob = buffer_get_string(&m, &bloblen);
1434 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1435 xfree(blob);
1436
1437 debug3("%s: Waiting for second key", __func__);
1438 blob = buffer_get_string(&m, &bloblen);
1439 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1440 xfree(blob);
1441
1442 /* Now get sequence numbers for the packets */
1443 seqnr = buffer_get_int(&m);
1444 blocks = buffer_get_int64(&m);
1445 packets = buffer_get_int(&m);
1446 bytes = buffer_get_int64(&m);
1447 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1448 seqnr = buffer_get_int(&m);
1449 blocks = buffer_get_int64(&m);
1450 packets = buffer_get_int(&m);
1451 bytes = buffer_get_int64(&m);
1452 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1453
1454 skip:
1455 /* Get the key context */
1456 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1457 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1458
1459 debug3("%s: Getting compression state", __func__);
1460 /* Get compression state */
1461 p = buffer_get_string(&m, &plen);
1462 if (plen != sizeof(child_state.outgoing))
1463 fatal("%s: bad request size", __func__);
1464 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1465 xfree(p);
1466
1467 p = buffer_get_string(&m, &plen);
1468 if (plen != sizeof(child_state.incoming))
1469 fatal("%s: bad request size", __func__);
1470 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1471 xfree(p);
1472
1473 /* Network I/O buffers */
1474 debug3("%s: Getting Network I/O buffers", __func__);
1475 child_state.input = buffer_get_string(&m, &child_state.ilen);
1476 child_state.output = buffer_get_string(&m, &child_state.olen);
1477
1478 buffer_free(&m);
1479 }
1480
1481
1482 /* Allocation functions for zlib */
1483 void *
1484 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1485 {
1486 size_t len = (size_t) size * ncount;
1487 void *address;
1488
1489 if (len == 0 || ncount > SIZE_T_MAX / size)
1490 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1491
1492 address = mm_malloc(mm, len);
1493
1494 return (address);
1495 }
1496
1497 void
1498 mm_zfree(struct mm_master *mm, void *address)
1499 {
1500 mm_free(mm, address);
1501 }
1502
1503 void
1504 mm_init_compression(struct mm_master *mm)
1505 {
1506 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1507 outgoing_stream.zfree = (free_func)mm_zfree;
1508 outgoing_stream.opaque = mm;
1509
1510 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1511 incoming_stream.zfree = (free_func)mm_zfree;
1512 incoming_stream.opaque = mm;
1513 }
1514
1515 /* XXX */
1516
1517 #define FD_CLOSEONEXEC(x) do { \
1518 if (fcntl(x, F_SETFD, 1) == -1) \
1519 fatal("fcntl(%d, F_SETFD)", x); \
1520 } while (0)
1521
1522 static void
1523 monitor_socketpair(int *pair)
1524 {
1525 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1526 fatal("%s: socketpair", __func__);
1527 FD_CLOSEONEXEC(pair[0]);
1528 FD_CLOSEONEXEC(pair[1]);
1529 }
1530
1531 #define MM_MEMSIZE 65536
1532
1533 struct monitor *
1534 monitor_init(void)
1535 {
1536 struct monitor *mon;
1537 int pair[2];
1538
1539 mon = xcalloc(1, sizeof(*mon));
1540
1541 monitor_socketpair(pair);
1542
1543 mon->m_recvfd = pair[0];
1544 mon->m_sendfd = pair[1];
1545
1546 /* Used to share zlib space across processes */
1547 if (options.compression) {
1548 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1549 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1550
1551 /* Compression needs to share state across borders */
1552 mm_init_compression(mon->m_zlib);
1553 }
1554
1555 return mon;
1556 }
1557
1558 void
1559 monitor_reinit(struct monitor *mon)
1560 {
1561 int pair[2];
1562
1563 monitor_socketpair(pair);
1564
1565 mon->m_recvfd = pair[0];
1566 mon->m_sendfd = pair[1];
1567 }
1568
1569 #ifdef GSSAPI
1570 int
1571 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1572 {
1573 gss_OID_desc goid;
1574 OM_uint32 major;
1575 u_int len;
1576
1577 goid.elements = buffer_get_string(m, &len);
1578 goid.length = len;
1579
1580 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1581
1582 xfree(goid.elements);
1583
1584 buffer_clear(m);
1585 buffer_put_int(m, major);
1586
1587 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1588
1589 /* Now we have a context, enable the step */
1590 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1591
1592 return (0);
1593 }
1594
1595 int
1596 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1597 {
1598 gss_buffer_desc in;
1599 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1600 OM_uint32 major, minor;
1601 OM_uint32 flags = 0; /* GSI needs this */
1602 u_int len;
1603
1604 in.value = buffer_get_string(m, &len);
1605 in.length = len;
1606 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1607 xfree(in.value);
1608
1609 buffer_clear(m);
1610 buffer_put_int(m, major);
1611 buffer_put_string(m, out.value, out.length);
1612 buffer_put_int(m, flags);
1613 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1614
1615 gss_release_buffer(&minor, &out);
1616
1617 if (major == GSS_S_COMPLETE) {
1618 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1619 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1620 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1621 }
1622 return (0);
1623 }
1624
1625 int
1626 mm_answer_gss_checkmic(int sock, Buffer *m)
1627 {
1628 gss_buffer_desc gssbuf, mic;
1629 OM_uint32 ret;
1630 u_int len;
1631
1632 gssbuf.value = buffer_get_string(m, &len);
1633 gssbuf.length = len;
1634 mic.value = buffer_get_string(m, &len);
1635 mic.length = len;
1636
1637 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1638
1639 xfree(gssbuf.value);
1640 xfree(mic.value);
1641
1642 buffer_clear(m);
1643 buffer_put_int(m, ret);
1644
1645 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1646
1647 if (!GSS_ERROR(ret))
1648 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1649
1650 return (0);
1651 }
1652
1653 int
1654 mm_answer_gss_userok(int sock, Buffer *m)
1655 {
1656 int authenticated;
1657
1658 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1659
1660 buffer_clear(m);
1661 buffer_put_int(m, authenticated);
1662
1663 debug3("%s: sending result %d", __func__, authenticated);
1664 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1665
1666 auth_method = "gssapi-with-mic";
1667
1668 /* Monitor loop will terminate if authenticated */
1669 return (authenticated);
1670 }
1671 #endif /* GSSAPI */
1672
1673 #ifdef JPAKE
1674 int
1675 mm_answer_jpake_step1(int sock, Buffer *m)
1676 {
1677 struct jpake_ctx *pctx;
1678 u_char *x3_proof, *x4_proof;
1679 u_int x3_proof_len, x4_proof_len;
1680
1681 if (!options.zero_knowledge_password_authentication)
1682 fatal("zero_knowledge_password_authentication disabled");
1683
1684 if (authctxt->jpake_ctx != NULL)
1685 fatal("%s: authctxt->jpake_ctx already set (%p)",
1686 __func__, authctxt->jpake_ctx);
1687 authctxt->jpake_ctx = pctx = jpake_new();
1688
1689 jpake_step1(pctx->grp,
1690 &pctx->server_id, &pctx->server_id_len,
1691 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
1692 &x3_proof, &x3_proof_len,
1693 &x4_proof, &x4_proof_len);
1694
1695 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
1696
1697 buffer_clear(m);
1698
1699 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
1700 buffer_put_bignum2(m, pctx->g_x3);
1701 buffer_put_bignum2(m, pctx->g_x4);
1702 buffer_put_string(m, x3_proof, x3_proof_len);
1703 buffer_put_string(m, x4_proof, x4_proof_len);
1704
1705 debug3("%s: sending step1", __func__);
1706 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
1707
1708 bzero(x3_proof, x3_proof_len);
1709 bzero(x4_proof, x4_proof_len);
1710 xfree(x3_proof);
1711 xfree(x4_proof);
1712
1713 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
1714 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
1715
1716 return 0;
1717 }
1718
1719 int
1720 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
1721 {
1722 struct jpake_ctx *pctx = authctxt->jpake_ctx;
1723 char *hash_scheme, *salt;
1724
1725 if (pctx == NULL)
1726 fatal("%s: pctx == NULL", __func__);
1727
1728 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
1729
1730 buffer_clear(m);
1731 /* pctx->s is sensitive, not returned to slave */
1732 buffer_put_cstring(m, hash_scheme);
1733 buffer_put_cstring(m, salt);
1734
1735 debug3("%s: sending pwdata", __func__);
1736 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
1737
1738 bzero(hash_scheme, strlen(hash_scheme));
1739 bzero(salt, strlen(salt));
1740 xfree(hash_scheme);
1741 xfree(salt);
1742
1743 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
1744
1745 return 0;
1746 }
1747
1748 int
1749 mm_answer_jpake_step2(int sock, Buffer *m)
1750 {
1751 struct jpake_ctx *pctx = authctxt->jpake_ctx;
1752 u_char *x1_proof, *x2_proof, *x4_s_proof;
1753 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
1754
1755 if (pctx == NULL)
1756 fatal("%s: pctx == NULL", __func__);
1757
1758 if ((pctx->g_x1 = BN_new()) == NULL ||
1759 (pctx->g_x2 = BN_new()) == NULL)
1760 fatal("%s: BN_new", __func__);
1761 buffer_get_bignum2(m, pctx->g_x1);
1762 buffer_get_bignum2(m, pctx->g_x2);
1763 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
1764 x1_proof = buffer_get_string(m, &x1_proof_len);
1765 x2_proof = buffer_get_string(m, &x2_proof_len);
1766
1767 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
1768 pctx->g_x1, pctx->g_x2, pctx->x4,
1769 pctx->client_id, pctx->client_id_len,
1770 pctx->server_id, pctx->server_id_len,
1771 x1_proof, x1_proof_len,
1772 x2_proof, x2_proof_len,
1773 &pctx->b,
1774 &x4_s_proof, &x4_s_proof_len);
1775
1776 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
1777
1778 bzero(x1_proof, x1_proof_len);
1779 bzero(x2_proof, x2_proof_len);
1780 xfree(x1_proof);
1781 xfree(x2_proof);
1782
1783 buffer_clear(m);
1784
1785 buffer_put_bignum2(m, pctx->b);
1786 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
1787
1788 debug3("%s: sending step2", __func__);
1789 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
1790
1791 bzero(x4_s_proof, x4_s_proof_len);
1792 xfree(x4_s_proof);
1793
1794 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
1795
1796 return 0;
1797 }
1798
1799 int
1800 mm_answer_jpake_key_confirm(int sock, Buffer *m)
1801 {
1802 struct jpake_ctx *pctx = authctxt->jpake_ctx;
1803 u_char *x2_s_proof;
1804 u_int x2_s_proof_len;
1805
1806 if (pctx == NULL)
1807 fatal("%s: pctx == NULL", __func__);
1808
1809 if ((pctx->a = BN_new()) == NULL)
1810 fatal("%s: BN_new", __func__);
1811 buffer_get_bignum2(m, pctx->a);
1812 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
1813
1814 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
1815 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
1816 pctx->server_id, pctx->server_id_len,
1817 pctx->client_id, pctx->client_id_len,
1818 session_id2, session_id2_len,
1819 x2_s_proof, x2_s_proof_len,
1820 &pctx->k,
1821 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
1822
1823 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
1824
1825 bzero(x2_s_proof, x2_s_proof_len);
1826 buffer_clear(m);
1827
1828 /* pctx->k is sensitive, not sent */
1829 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
1830
1831 debug3("%s: sending confirmation hash", __func__);
1832 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
1833
1834 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
1835
1836 return 0;
1837 }
1838
1839 int
1840 mm_answer_jpake_check_confirm(int sock, Buffer *m)
1841 {
1842 int authenticated = 0;
1843 u_char *peer_confirm_hash;
1844 u_int peer_confirm_hash_len;
1845 struct jpake_ctx *pctx = authctxt->jpake_ctx;
1846
1847 if (pctx == NULL)
1848 fatal("%s: pctx == NULL", __func__);
1849
1850 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
1851
1852 authenticated = jpake_check_confirm(pctx->k,
1853 pctx->client_id, pctx->client_id_len,
1854 session_id2, session_id2_len,
1855 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
1856
1857 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
1858
1859 bzero(peer_confirm_hash, peer_confirm_hash_len);
1860 xfree(peer_confirm_hash);
1861
1862 buffer_clear(m);
1863 buffer_put_int(m, authenticated);
1864
1865 debug3("%s: sending result %d", __func__, authenticated);
1866 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
1867
1868 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
1869
1870 auth_method = "jpake-01 (at) openssh.com";
1871 return authenticated;
1872 }
1873
1874 #endif /* JPAKE */
1875