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