monitor.c revision 1.11 1 /* $NetBSD: monitor.c,v 1.11 2013/11/08 19:18:25 christos Exp $ */
2 /* $OpenBSD: monitor.c,v 1.127 2013/07/19 07:37:48 markus 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 "includes.h"
30 __RCSID("$NetBSD: monitor.c,v 1.11 2013/11/08 19:18:25 christos Exp $");
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <sys/tree.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37
38 #include <openssl/dh.h>
39
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <paths.h>
43 #include <poll.h>
44 #include <pwd.h>
45 #include <signal.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #ifdef SKEY
50 #include <skey.h>
51 #endif
52
53 #include "atomicio.h"
54 #include "xmalloc.h"
55 #include "ssh.h"
56 #include "key.h"
57 #include "buffer.h"
58 #include "hostfile.h"
59 #include "auth.h"
60 #include "cipher.h"
61 #include "kex.h"
62 #include "dh.h"
63 #include <zlib.h>
64 #include "packet.h"
65 #include "auth-options.h"
66 #include "sshpty.h"
67 #include "channels.h"
68 #include "session.h"
69 #include "sshlogin.h"
70 #include "canohost.h"
71 #include "log.h"
72 #include "servconf.h"
73 #include "monitor.h"
74 #include "monitor_mm.h"
75 #ifdef GSSAPI
76 #include "ssh-gss.h"
77 #endif
78 #include "monitor_wrap.h"
79 #include "monitor_fdpass.h"
80 #include "misc.h"
81 #include "compat.h"
82 #include "ssh2.h"
83 #include "jpake.h"
84 #include "roaming.h"
85 #include "authfd.h"
86
87 #ifdef GSSAPI
88 static Gssctxt *gsscontext = NULL;
89 #endif
90
91 /* Imports */
92 extern ServerOptions options;
93 extern u_int utmp_len;
94 extern Newkeys *current_keys[];
95 extern z_stream incoming_stream;
96 extern z_stream outgoing_stream;
97 extern u_char session_id[];
98 extern Buffer auth_debug;
99 extern int auth_debug_init;
100 extern Buffer loginmsg;
101
102 /* State exported from the child */
103
104 struct {
105 z_stream incoming;
106 z_stream outgoing;
107 u_char *keyin;
108 u_int keyinlen;
109 u_char *keyout;
110 u_int keyoutlen;
111 u_char *ivin;
112 u_int ivinlen;
113 u_char *ivout;
114 u_int ivoutlen;
115 u_char *ssh1key;
116 u_int ssh1keylen;
117 int ssh1cipher;
118 int ssh1protoflags;
119 u_char *input;
120 u_int ilen;
121 u_char *output;
122 u_int olen;
123 u_int64_t sent_bytes;
124 u_int64_t recv_bytes;
125 } child_state;
126
127 /* Functions on the monitor that answer unprivileged requests */
128
129 int mm_answer_moduli(int, Buffer *);
130 int mm_answer_sign(int, Buffer *);
131 int mm_answer_pwnamallow(int, Buffer *);
132 int mm_answer_auth2_read_banner(int, Buffer *);
133 int mm_answer_authserv(int, Buffer *);
134 int mm_answer_authpassword(int, Buffer *);
135 int mm_answer_bsdauthquery(int, Buffer *);
136 int mm_answer_bsdauthrespond(int, Buffer *);
137 int mm_answer_skeyquery(int, Buffer *);
138 int mm_answer_skeyrespond(int, Buffer *);
139 int mm_answer_keyallowed(int, Buffer *);
140 int mm_answer_keyverify(int, Buffer *);
141 int mm_answer_pty(int, Buffer *);
142 int mm_answer_pty_cleanup(int, Buffer *);
143 int mm_answer_term(int, Buffer *);
144 int mm_answer_rsa_keyallowed(int, Buffer *);
145 int mm_answer_rsa_challenge(int, Buffer *);
146 int mm_answer_rsa_response(int, Buffer *);
147 int mm_answer_sesskey(int, Buffer *);
148 int mm_answer_sessid(int, Buffer *);
149 int mm_answer_jpake_get_pwdata(int, Buffer *);
150 int mm_answer_jpake_step1(int, Buffer *);
151 int mm_answer_jpake_step2(int, Buffer *);
152 int mm_answer_jpake_key_confirm(int, Buffer *);
153 int mm_answer_jpake_check_confirm(int, Buffer *);
154
155 #ifdef USE_PAM
156 int mm_answer_pam_start(int, Buffer *);
157 int mm_answer_pam_account(int, Buffer *);
158 int mm_answer_pam_init_ctx(int, Buffer *);
159 int mm_answer_pam_query(int, Buffer *);
160 int mm_answer_pam_respond(int, Buffer *);
161 int mm_answer_pam_free_ctx(int, Buffer *);
162 #endif
163
164 #ifdef KRB4
165 int mm_answer_krb4(int, Buffer *);
166 #endif
167 #ifdef KRB5
168 int mm_answer_krb5(int, Buffer *);
169 #endif
170
171 #ifdef GSSAPI
172 int mm_answer_gss_setup_ctx(int, Buffer *);
173 int mm_answer_gss_accept_ctx(int, Buffer *);
174 int mm_answer_gss_userok(int, Buffer *);
175 int mm_answer_gss_checkmic(int, Buffer *);
176 #endif
177
178 static int monitor_read_log(struct monitor *);
179
180 static Authctxt *authctxt;
181 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
182
183 /* local state for key verify */
184 static u_char *key_blob = NULL;
185 static u_int key_bloblen = 0;
186 static int key_blobtype = MM_NOKEY;
187 static char *hostbased_cuser = NULL;
188 static char *hostbased_chost = NULL;
189 static const char *auth_method = "unknown";
190 static const char *auth_submethod = NULL;
191 static u_int session_id2_len = 0;
192 static u_char *session_id2 = NULL;
193 static pid_t monitor_child_pid;
194
195 struct mon_table {
196 enum monitor_reqtype type;
197 int flags;
198 int (*f)(int, Buffer *);
199 };
200
201 #define MON_ISAUTH 0x0004 /* Required for Authentication */
202 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
203 #define MON_ONCE 0x0010 /* Disable after calling */
204 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
205
206 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
207
208 #define MON_PERMIT 0x1000 /* Request is permitted */
209
210 struct mon_table mon_dispatch_proto20[] = {
211 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
212 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
213 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
214 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
215 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
216 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
217 #ifdef USE_PAM
218 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
219 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
220 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
221 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
222 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
223 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
224 #endif
225 #ifdef BSD_AUTH
226 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
227 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
228 #endif
229 #ifdef SKEY
230 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
231 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
232 #endif
233 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
234 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
235 #ifdef KRB4
236 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
237 #endif
238 #ifdef KRB5
239 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
240 #endif
241 #ifdef GSSAPI
242 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
243 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
244 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
245 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
246 #endif
247 #ifdef JPAKE
248 {MONITOR_REQ_JPAKE_GET_PWDATA, MON_ONCE, mm_answer_jpake_get_pwdata},
249 {MONITOR_REQ_JPAKE_STEP1, MON_ISAUTH, mm_answer_jpake_step1},
250 {MONITOR_REQ_JPAKE_STEP2, MON_ONCE, mm_answer_jpake_step2},
251 {MONITOR_REQ_JPAKE_KEY_CONFIRM, MON_ONCE, mm_answer_jpake_key_confirm},
252 {MONITOR_REQ_JPAKE_CHECK_CONFIRM, MON_AUTH, mm_answer_jpake_check_confirm},
253 #endif
254 {0, 0, NULL}
255 };
256
257 struct mon_table mon_dispatch_postauth20[] = {
258 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
259 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
260 {MONITOR_REQ_PTY, 0, mm_answer_pty},
261 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
262 {MONITOR_REQ_TERM, 0, mm_answer_term},
263 {0, 0, NULL}
264 };
265
266 struct mon_table mon_dispatch_proto15[] = {
267 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
268 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
269 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
270 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
271 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
272 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
273 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
274 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
275 #ifdef BSD_AUTH
276 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
277 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
278 #endif
279 #ifdef SKEY
280 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
281 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
282 #endif
283 #ifdef USE_PAM
284 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
285 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
286 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
287 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
288 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
289 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
290 #endif
291 #ifdef KRB4
292 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
293 #endif
294 #ifdef KRB5
295 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
296 #endif
297 {0, 0, NULL}
298 };
299
300 struct mon_table mon_dispatch_postauth15[] = {
301 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
302 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
303 {MONITOR_REQ_TERM, 0, mm_answer_term},
304 {0, 0, NULL}
305 };
306
307 struct mon_table *mon_dispatch;
308
309 /* Specifies if a certain message is allowed at the moment */
310
311 static void
312 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
313 {
314 while (ent->f != NULL) {
315 if (ent->type == type) {
316 ent->flags &= ~MON_PERMIT;
317 ent->flags |= permit ? MON_PERMIT : 0;
318 return;
319 }
320 ent++;
321 }
322 }
323
324 static void
325 monitor_permit_authentications(int permit)
326 {
327 struct mon_table *ent = mon_dispatch;
328
329 while (ent->f != NULL) {
330 if (ent->flags & MON_AUTH) {
331 ent->flags &= ~MON_PERMIT;
332 ent->flags |= permit ? MON_PERMIT : 0;
333 }
334 ent++;
335 }
336 }
337
338 void
339 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
340 {
341 struct mon_table *ent;
342 int authenticated = 0, partial = 0;
343
344 debug3("preauth child monitor started");
345
346 close(pmonitor->m_recvfd);
347 close(pmonitor->m_log_sendfd);
348 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
349
350 authctxt = _authctxt;
351 memset(authctxt, 0, sizeof(*authctxt));
352
353 if (compat20) {
354 mon_dispatch = mon_dispatch_proto20;
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 } else {
360 mon_dispatch = mon_dispatch_proto15;
361
362 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
363 }
364
365 /* The first few requests do not require asynchronous access */
366 while (!authenticated) {
367 partial = 0;
368 auth_method = "unknown";
369 auth_submethod = NULL;
370 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
371
372 /* Special handling for multiple required authentications */
373 if (options.num_auth_methods != 0) {
374 if (!compat20)
375 fatal("AuthenticationMethods is not supported"
376 "with SSH protocol 1");
377 if (authenticated &&
378 !auth2_update_methods_lists(authctxt,
379 auth_method, auth_submethod)) {
380 debug3("%s: method %s: partial", __func__,
381 auth_method);
382 authenticated = 0;
383 partial = 1;
384 }
385 }
386
387 if (authenticated) {
388 if (!(ent->flags & MON_AUTHDECIDE))
389 fatal("%s: unexpected authentication from %d",
390 __func__, ent->type);
391 if (authctxt->pw->pw_uid == 0 &&
392 !auth_root_allowed(auth_method))
393 authenticated = 0;
394 #ifdef USE_PAM
395 /* PAM needs to perform account checks after auth */
396 if (options.use_pam && authenticated) {
397 Buffer m;
398
399 buffer_init(&m);
400 mm_request_receive_expect(pmonitor->m_sendfd,
401 MONITOR_REQ_PAM_ACCOUNT, &m);
402 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
403 buffer_free(&m);
404 }
405 #endif
406 }
407 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
408 auth_log(authctxt, authenticated, partial,
409 auth_method, auth_submethod);
410 if (!authenticated)
411 authctxt->failures++;
412 }
413 #ifdef JPAKE
414 /* Cleanup JPAKE context after authentication */
415 if (ent->flags & MON_AUTHDECIDE) {
416 if (authctxt->jpake_ctx != NULL) {
417 jpake_free(authctxt->jpake_ctx);
418 authctxt->jpake_ctx = NULL;
419 }
420 }
421 #endif
422 }
423
424 if (!authctxt->valid)
425 fatal("%s: authenticated invalid user", __func__);
426 if (strcmp(auth_method, "unknown") == 0)
427 fatal("%s: authentication method name unknown", __func__);
428
429 debug("%s: %s has been authenticated by privileged process",
430 __func__, authctxt->user);
431
432 mm_get_keystate(pmonitor);
433
434 /* Drain any buffered messages from the child */
435 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
436 ;
437
438 close(pmonitor->m_sendfd);
439 close(pmonitor->m_log_recvfd);
440 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
441 }
442
443 static void
444 monitor_set_child_handler(pid_t pid)
445 {
446 monitor_child_pid = pid;
447 }
448
449 static void
450 monitor_child_handler(int sig)
451 {
452 kill(monitor_child_pid, sig);
453 }
454
455 void
456 monitor_child_postauth(struct monitor *pmonitor)
457 {
458 close(pmonitor->m_recvfd);
459 pmonitor->m_recvfd = -1;
460
461 monitor_set_child_handler(pmonitor->m_pid);
462 signal(SIGHUP, &monitor_child_handler);
463 signal(SIGTERM, &monitor_child_handler);
464 signal(SIGINT, &monitor_child_handler);
465
466 if (compat20) {
467 mon_dispatch = mon_dispatch_postauth20;
468
469 /* Permit requests for moduli and signatures */
470 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
471 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
472 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
473 } else {
474 mon_dispatch = mon_dispatch_postauth15;
475 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
476 }
477 if (!no_pty_flag) {
478 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
479 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
480 }
481
482 for (;;)
483 monitor_read(pmonitor, mon_dispatch, NULL);
484 }
485
486 void
487 monitor_sync(struct monitor *pmonitor)
488 {
489 if (options.compression) {
490 /* The member allocation is not visible, so sync it */
491 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
492 }
493 }
494
495 static int
496 monitor_read_log(struct monitor *pmonitor)
497 {
498 Buffer logmsg;
499 u_int len, level;
500 char *msg;
501
502 buffer_init(&logmsg);
503
504 /* Read length */
505 buffer_append_space(&logmsg, 4);
506 if (atomicio(read, pmonitor->m_log_recvfd,
507 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
508 if (errno == EPIPE) {
509 buffer_free(&logmsg);
510 debug("%s: child log fd closed", __func__);
511 close(pmonitor->m_log_recvfd);
512 pmonitor->m_log_recvfd = -1;
513 return -1;
514 }
515 fatal("%s: log fd read: %s", __func__, strerror(errno));
516 }
517 len = buffer_get_int(&logmsg);
518 if (len <= 4 || len > 8192)
519 fatal("%s: invalid log message length %u", __func__, len);
520
521 /* Read severity, message */
522 buffer_clear(&logmsg);
523 buffer_append_space(&logmsg, len);
524 if (atomicio(read, pmonitor->m_log_recvfd,
525 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
526 fatal("%s: log fd read: %s", __func__, strerror(errno));
527
528 /* Log it */
529 level = buffer_get_int(&logmsg);
530 msg = buffer_get_string(&logmsg, NULL);
531 if (log_level_name(level) == NULL)
532 fatal("%s: invalid log level %u (corrupted message?)",
533 __func__, level);
534 do_log2(level, "%s [preauth]", msg);
535
536 buffer_free(&logmsg);
537 free(msg);
538
539 return 0;
540 }
541
542 int
543 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
544 struct mon_table **pent)
545 {
546 Buffer m;
547 int ret;
548 u_char type;
549 struct pollfd pfd[2];
550
551 for (;;) {
552 bzero(&pfd, sizeof(pfd));
553 pfd[0].fd = pmonitor->m_sendfd;
554 pfd[0].events = POLLIN;
555 pfd[1].fd = pmonitor->m_log_recvfd;
556 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
557 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
558 if (errno == EINTR || errno == EAGAIN)
559 continue;
560 fatal("%s: poll: %s", __func__, strerror(errno));
561 }
562 if (pfd[1].revents) {
563 /*
564 * Drain all log messages before processing next
565 * monitor request.
566 */
567 monitor_read_log(pmonitor);
568 continue;
569 }
570 if (pfd[0].revents)
571 break; /* Continues below */
572 }
573
574 buffer_init(&m);
575
576 mm_request_receive(pmonitor->m_sendfd, &m);
577 type = buffer_get_char(&m);
578
579 debug3("%s: checking request %d", __func__, type);
580
581 while (ent->f != NULL) {
582 if (ent->type == type)
583 break;
584 ent++;
585 }
586
587 if (ent->f != NULL) {
588 if (!(ent->flags & MON_PERMIT))
589 fatal("%s: unpermitted request %d", __func__,
590 type);
591 ret = (*ent->f)(pmonitor->m_sendfd, &m);
592 buffer_free(&m);
593
594 /* The child may use this request only once, disable it */
595 if (ent->flags & MON_ONCE) {
596 debug2("%s: %d used once, disabling now", __func__,
597 type);
598 ent->flags &= ~MON_PERMIT;
599 }
600
601 if (pent != NULL)
602 *pent = ent;
603
604 return ret;
605 }
606
607 fatal("%s: unsupported request: %d", __func__, type);
608
609 /* NOTREACHED */
610 return (-1);
611 }
612
613 /* allowed key state */
614 static int
615 monitor_allowed_key(u_char *blob, u_int bloblen)
616 {
617 /* make sure key is allowed */
618 if (key_blob == NULL || key_bloblen != bloblen ||
619 timingsafe_bcmp(key_blob, blob, key_bloblen))
620 return (0);
621 return (1);
622 }
623
624 static void
625 monitor_reset_key_state(void)
626 {
627 /* reset state */
628 free(key_blob);
629 free(hostbased_cuser);
630 free(hostbased_chost);
631 key_blob = NULL;
632 key_bloblen = 0;
633 key_blobtype = MM_NOKEY;
634 hostbased_cuser = NULL;
635 hostbased_chost = NULL;
636 }
637
638 int
639 mm_answer_moduli(int sock, Buffer *m)
640 {
641 DH *dh;
642 int min, want, max;
643
644 min = buffer_get_int(m);
645 want = buffer_get_int(m);
646 max = buffer_get_int(m);
647
648 debug3("%s: got parameters: %d %d %d",
649 __func__, min, want, max);
650 /* We need to check here, too, in case the child got corrupted */
651 if (max < min || want < min || max < want)
652 fatal("%s: bad parameters: %d %d %d",
653 __func__, min, want, max);
654
655 buffer_clear(m);
656
657 dh = choose_dh(min, want, max);
658 if (dh == NULL) {
659 buffer_put_char(m, 0);
660 return (0);
661 } else {
662 /* Send first bignum */
663 buffer_put_char(m, 1);
664 buffer_put_bignum2(m, dh->p);
665 buffer_put_bignum2(m, dh->g);
666
667 DH_free(dh);
668 }
669 mm_request_send(sock, MONITOR_ANS_MODULI, m);
670 return (0);
671 }
672
673 extern AuthenticationConnection *auth_conn;
674
675 int
676 mm_answer_sign(int sock, Buffer *m)
677 {
678 Key *key;
679 u_char *p;
680 u_char *signature;
681 u_int siglen, datlen;
682 int keyid;
683
684 debug3("%s", __func__);
685
686 keyid = buffer_get_int(m);
687 p = buffer_get_string(m, &datlen);
688
689 /*
690 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
691 * SHA384 (48 bytes) and SHA512 (64 bytes).
692 */
693 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64)
694 fatal("%s: data length incorrect: %u", __func__, datlen);
695
696 /* save session id, it will be passed on the first call */
697 if (session_id2_len == 0) {
698 session_id2_len = datlen;
699 session_id2 = xmalloc(session_id2_len);
700 memcpy(session_id2, p, session_id2_len);
701 }
702
703 if ((key = get_hostkey_by_index(keyid)) != NULL) {
704 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
705 fatal("%s: key_sign failed", __func__);
706 } else if ((key = get_hostkey_public_by_index(keyid)) != NULL &&
707 auth_conn != NULL) {
708 if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p,
709 datlen) < 0)
710 fatal("%s: ssh_agent_sign failed", __func__);
711 } else
712 fatal("%s: no hostkey from index %d", __func__, keyid);
713
714 debug3("%s: signature %p(%u)", __func__, signature, siglen);
715
716 buffer_clear(m);
717 buffer_put_string(m, signature, siglen);
718
719 free(p);
720 free(signature);
721
722 mm_request_send(sock, MONITOR_ANS_SIGN, m);
723
724 /* Turn on permissions for getpwnam */
725 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
726
727 return (0);
728 }
729
730 /* Retrieves the password entry and also checks if the user is permitted */
731
732 int
733 mm_answer_pwnamallow(int sock, Buffer *m)
734 {
735 char *username;
736 struct passwd *pwent;
737 int allowed = 0;
738 u_int i;
739
740 debug3("%s", __func__);
741
742 if (authctxt->attempt++ != 0)
743 fatal("%s: multiple attempts for getpwnam", __func__);
744
745 username = buffer_get_string(m, NULL);
746
747 pwent = getpwnamallow(username);
748
749 authctxt->user = xstrdup(username);
750 setproctitle("%s [priv]", pwent ? username : "unknown");
751 free(username);
752
753 buffer_clear(m);
754
755 if (pwent == NULL) {
756 buffer_put_char(m, 0);
757 authctxt->pw = fakepw();
758 goto out;
759 }
760
761 allowed = 1;
762 authctxt->pw = pwent;
763 authctxt->valid = 1;
764
765 buffer_put_char(m, 1);
766 buffer_put_string(m, pwent, sizeof(struct passwd));
767 buffer_put_cstring(m, pwent->pw_name);
768 buffer_put_cstring(m, "*");
769 buffer_put_cstring(m, pwent->pw_gecos);
770 buffer_put_cstring(m, pwent->pw_class);
771 buffer_put_cstring(m, pwent->pw_dir);
772 buffer_put_cstring(m, pwent->pw_shell);
773
774 out:
775 buffer_put_string(m, &options, sizeof(options));
776
777 #define M_CP_STROPT(x) do { \
778 if (options.x != NULL) \
779 buffer_put_cstring(m, options.x); \
780 } while (0)
781 #define M_CP_STRARRAYOPT(x, nx) do { \
782 for (i = 0; i < options.nx; i++) \
783 buffer_put_cstring(m, options.x[i]); \
784 } while (0)
785 /* See comment in servconf.h */
786 COPY_MATCH_STRING_OPTS();
787 #undef M_CP_STROPT
788 #undef M_CP_STRARRAYOPT
789
790 /* Create valid auth method lists */
791 if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
792 /*
793 * The monitor will continue long enough to let the child
794 * run to it's packet_disconnect(), but it must not allow any
795 * authentication to succeed.
796 */
797 debug("%s: no valid authentication method lists", __func__);
798 }
799
800 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
801 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
802
803 /* For SSHv1 allow authentication now */
804 if (!compat20)
805 monitor_permit_authentications(1);
806 else {
807 /* Allow service/style information on the auth context */
808 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
809 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
810 }
811
812 #ifdef USE_PAM
813 if (options.use_pam)
814 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
815 #endif
816
817 return (0);
818 }
819
820 int mm_answer_auth2_read_banner(int sock, Buffer *m)
821 {
822 char *banner;
823
824 buffer_clear(m);
825 banner = auth2_read_banner();
826 buffer_put_cstring(m, banner != NULL ? banner : "");
827 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
828 free(banner);
829
830 return (0);
831 }
832
833 int
834 mm_answer_authserv(int sock, Buffer *m)
835 {
836 monitor_permit_authentications(1);
837
838 authctxt->service = buffer_get_string(m, NULL);
839 authctxt->style = buffer_get_string(m, NULL);
840 debug3("%s: service=%s, style=%s",
841 __func__, authctxt->service, authctxt->style);
842
843 if (strlen(authctxt->style) == 0) {
844 free(authctxt->style);
845 authctxt->style = NULL;
846 }
847
848 return (0);
849 }
850
851 int
852 mm_answer_authpassword(int sock, Buffer *m)
853 {
854 static int call_count;
855 char *passwd;
856 int authenticated;
857 u_int plen;
858
859 passwd = buffer_get_string(m, &plen);
860 /* Only authenticate if the context is valid */
861 authenticated = options.password_authentication &&
862 auth_password(authctxt, passwd);
863 memset(passwd, 0, strlen(passwd));
864 free(passwd);
865
866 buffer_clear(m);
867 buffer_put_int(m, authenticated);
868
869 debug3("%s: sending result %d", __func__, authenticated);
870 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
871
872 call_count++;
873 if (plen == 0 && call_count == 1)
874 auth_method = "none";
875 else
876 auth_method = "password";
877
878 /* Causes monitor loop to terminate if authenticated */
879 return (authenticated);
880 }
881
882 #ifdef BSD_AUTH
883 int
884 mm_answer_bsdauthquery(int sock, Buffer *m)
885 {
886 char *name, *infotxt;
887 u_int numprompts;
888 u_int *echo_on;
889 char **prompts;
890 u_int success;
891
892 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
893 &prompts, &echo_on) < 0 ? 0 : 1;
894
895 buffer_clear(m);
896 buffer_put_int(m, success);
897 if (success)
898 buffer_put_cstring(m, prompts[0]);
899
900 debug3("%s: sending challenge success: %u", __func__, success);
901 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
902
903 if (success) {
904 free(name);
905 free(infotxt);
906 free(prompts);
907 free(echo_on);
908 }
909
910 return (0);
911 }
912
913 int
914 mm_answer_bsdauthrespond(int sock, Buffer *m)
915 {
916 char *response;
917 int authok;
918
919 if (authctxt->as == 0)
920 fatal("%s: no bsd auth session", __func__);
921
922 response = buffer_get_string(m, NULL);
923 authok = options.challenge_response_authentication &&
924 auth_userresponse(authctxt->as, response, 0);
925 authctxt->as = NULL;
926 debug3("%s: <%s> = <%d>", __func__, response, authok);
927 free(response);
928
929 buffer_clear(m);
930 buffer_put_int(m, authok);
931
932 debug3("%s: sending authenticated: %d", __func__, authok);
933 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
934
935 if (compat20) {
936 auth_method = "keyboard-interactive";
937 auth_submethod = "bsdauth";
938 } else
939 auth_method = "bsdauth";
940
941 return (authok != 0);
942 }
943 #endif
944
945 #ifdef SKEY
946 int
947 mm_answer_skeyquery(int sock, Buffer *m)
948 {
949 struct skey skey;
950 char challenge[1024];
951 u_int success;
952
953 success = skeychallenge(&skey, authctxt->user, challenge,
954 sizeof(challenge)) < 0 ? 0 : 1;
955
956 buffer_clear(m);
957 buffer_put_int(m, success);
958 if (success)
959 buffer_put_cstring(m, challenge);
960
961 debug3("%s: sending challenge success: %u", __func__, success);
962 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
963
964 return (0);
965 }
966
967 int
968 mm_answer_skeyrespond(int sock, Buffer *m)
969 {
970 char *response;
971 int authok;
972
973 response = buffer_get_string(m, NULL);
974
975 authok = (options.challenge_response_authentication &&
976 authctxt->valid &&
977 skey_haskey(authctxt->pw->pw_name) == 0 &&
978 skey_passcheck(authctxt->pw->pw_name, response) != -1);
979
980 free(response);
981
982 buffer_clear(m);
983 buffer_put_int(m, authok);
984
985 debug3("%s: sending authenticated: %d", __func__, authok);
986 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
987
988 auth_method = "skey";
989
990 return (authok != 0);
991 }
992 #endif
993
994 #ifdef USE_PAM
995 int
996 mm_answer_pam_start(int sock, Buffer *m)
997 {
998 if (!options.use_pam)
999 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1000
1001 start_pam(authctxt);
1002
1003 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
1004
1005 return (0);
1006 }
1007
1008 int
1009 mm_answer_pam_account(int sock, Buffer *m)
1010 {
1011 u_int ret;
1012
1013 if (!options.use_pam)
1014 fatal("UsePAM not set, but ended up in %s anyway", __func__);
1015
1016 ret = do_pam_account();
1017
1018 buffer_put_int(m, ret);
1019 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1020
1021 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
1022
1023 return (ret);
1024 }
1025
1026 static void *sshpam_ctxt, *sshpam_authok;
1027 extern KbdintDevice sshpam_device;
1028
1029 int
1030 mm_answer_pam_init_ctx(int sock, Buffer *m)
1031 {
1032
1033 debug3("%s", __func__);
1034 authctxt->user = buffer_get_string(m, NULL);
1035 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
1036 sshpam_authok = NULL;
1037 buffer_clear(m);
1038 if (sshpam_ctxt != NULL) {
1039 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
1040 buffer_put_int(m, 1);
1041 } else {
1042 buffer_put_int(m, 0);
1043 }
1044 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
1045 return (0);
1046 }
1047
1048 int
1049 mm_answer_pam_query(int sock, Buffer *m)
1050 {
1051 char *name, *info, **prompts;
1052 u_int i, num, *echo_on;
1053 int ret;
1054
1055 debug3("%s", __func__);
1056 sshpam_authok = NULL;
1057 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
1058 if (ret == 0 && num == 0)
1059 sshpam_authok = sshpam_ctxt;
1060 if (num > 1 || name == NULL || info == NULL)
1061 ret = -1;
1062 buffer_clear(m);
1063 buffer_put_int(m, ret);
1064 buffer_put_cstring(m, name);
1065 free(name);
1066 buffer_put_cstring(m, info);
1067 free(info);
1068 buffer_put_int(m, num);
1069 for (i = 0; i < num; ++i) {
1070 buffer_put_cstring(m, prompts[i]);
1071 free(prompts[i]);
1072 buffer_put_int(m, echo_on[i]);
1073 }
1074 if (prompts != NULL)
1075 free(prompts);
1076 if (echo_on != NULL)
1077 free(echo_on);
1078 auth_method = "keyboard-interactive/pam";
1079 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1080 return (0);
1081 }
1082
1083 int
1084 mm_answer_pam_respond(int sock, Buffer *m)
1085 {
1086 char **resp;
1087 u_int i, num;
1088 int ret;
1089
1090 debug3("%s", __func__);
1091 sshpam_authok = NULL;
1092 num = buffer_get_int(m);
1093 if (num > 0) {
1094 resp = xmalloc(num * sizeof(char *));
1095 for (i = 0; i < num; ++i)
1096 resp[i] = buffer_get_string(m, NULL);
1097 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1098 for (i = 0; i < num; ++i)
1099 free(resp[i]);
1100 free(resp);
1101 } else {
1102 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1103 }
1104 buffer_clear(m);
1105 buffer_put_int(m, ret);
1106 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1107 auth_method = "keyboard-interactive/pam";
1108 if (ret == 0)
1109 sshpam_authok = sshpam_ctxt;
1110 return (0);
1111 }
1112
1113 int
1114 mm_answer_pam_free_ctx(int sock, Buffer *m)
1115 {
1116
1117 debug3("%s", __func__);
1118 (sshpam_device.free_ctx)(sshpam_ctxt);
1119 buffer_clear(m);
1120 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1121 auth_method = "keyboard-interactive/pam";
1122 return (sshpam_authok == sshpam_ctxt);
1123 }
1124 #endif
1125
1126 int
1127 mm_answer_keyallowed(int sock, Buffer *m)
1128 {
1129 Key *key;
1130 char *cuser, *chost;
1131 u_char *blob;
1132 u_int bloblen;
1133 enum mm_keytype type = 0;
1134 int allowed = 0;
1135
1136 debug3("%s entering", __func__);
1137
1138 type = buffer_get_int(m);
1139 cuser = buffer_get_string(m, NULL);
1140 chost = buffer_get_string(m, NULL);
1141 blob = buffer_get_string(m, &bloblen);
1142
1143 key = key_from_blob(blob, bloblen);
1144
1145 if ((compat20 && type == MM_RSAHOSTKEY) ||
1146 (!compat20 && type != MM_RSAHOSTKEY))
1147 fatal("%s: key type and protocol mismatch", __func__);
1148
1149 debug3("%s: key_from_blob: %p", __func__, key);
1150
1151 if (key != NULL && authctxt->valid) {
1152 switch (type) {
1153 case MM_USERKEY:
1154 allowed = options.pubkey_authentication &&
1155 user_key_allowed(authctxt->pw, key);
1156 pubkey_auth_info(authctxt, key, NULL);
1157 auth_method = "publickey";
1158 if (options.pubkey_authentication && allowed != 1)
1159 auth_clear_options();
1160 break;
1161 case MM_HOSTKEY:
1162 allowed = options.hostbased_authentication &&
1163 hostbased_key_allowed(authctxt->pw,
1164 cuser, chost, key);
1165 pubkey_auth_info(authctxt, key,
1166 "client user \"%.100s\", client host \"%.100s\"",
1167 cuser, chost);
1168 auth_method = "hostbased";
1169 break;
1170 case MM_RSAHOSTKEY:
1171 key->type = KEY_RSA1; /* XXX */
1172 allowed = options.rhosts_rsa_authentication &&
1173 auth_rhosts_rsa_key_allowed(authctxt->pw,
1174 cuser, chost, key);
1175 if (options.rhosts_rsa_authentication && allowed != 1)
1176 auth_clear_options();
1177 auth_method = "rsa";
1178 break;
1179 default:
1180 fatal("%s: unknown key type %d", __func__, type);
1181 break;
1182 }
1183 }
1184 if (key != NULL)
1185 key_free(key);
1186
1187 /* clear temporarily storage (used by verify) */
1188 monitor_reset_key_state();
1189
1190 if (allowed) {
1191 /* Save temporarily for comparison in verify */
1192 key_blob = blob;
1193 key_bloblen = bloblen;
1194 key_blobtype = type;
1195 hostbased_cuser = cuser;
1196 hostbased_chost = chost;
1197 } else {
1198 /* Log failed attempt */
1199 auth_log(authctxt, 0, 0, auth_method, NULL);
1200 free(blob);
1201 free(cuser);
1202 free(chost);
1203 }
1204
1205 debug3("%s: key %p is %s",
1206 __func__, key, allowed ? "allowed" : "not allowed");
1207
1208 buffer_clear(m);
1209 buffer_put_int(m, allowed);
1210 buffer_put_int(m, forced_command != NULL);
1211
1212 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1213
1214 if (type == MM_RSAHOSTKEY)
1215 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1216
1217 return (0);
1218 }
1219
1220 static int
1221 monitor_valid_userblob(u_char *data, u_int datalen)
1222 {
1223 Buffer b;
1224 char *p, *userstyle;
1225 u_int len;
1226 int fail = 0;
1227
1228 buffer_init(&b);
1229 buffer_append(&b, data, datalen);
1230
1231 if (datafellows & SSH_OLD_SESSIONID) {
1232 p = buffer_ptr(&b);
1233 len = buffer_len(&b);
1234 if ((session_id2 == NULL) ||
1235 (len < session_id2_len) ||
1236 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1237 fail++;
1238 buffer_consume(&b, session_id2_len);
1239 } else {
1240 p = buffer_get_string(&b, &len);
1241 if ((session_id2 == NULL) ||
1242 (len != session_id2_len) ||
1243 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1244 fail++;
1245 free(p);
1246 }
1247 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1248 fail++;
1249 p = buffer_get_cstring(&b, NULL);
1250 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1251 authctxt->style ? ":" : "",
1252 authctxt->style ? authctxt->style : "");
1253 if (strcmp(userstyle, p) != 0) {
1254 logit("wrong user name passed to monitor: expected %s != %.100s",
1255 userstyle, p);
1256 fail++;
1257 }
1258 free(userstyle);
1259 free(p);
1260 buffer_skip_string(&b);
1261 if (datafellows & SSH_BUG_PKAUTH) {
1262 if (!buffer_get_char(&b))
1263 fail++;
1264 } else {
1265 p = buffer_get_cstring(&b, NULL);
1266 if (strcmp("publickey", p) != 0)
1267 fail++;
1268 free(p);
1269 if (!buffer_get_char(&b))
1270 fail++;
1271 buffer_skip_string(&b);
1272 }
1273 buffer_skip_string(&b);
1274 if (buffer_len(&b) != 0)
1275 fail++;
1276 buffer_free(&b);
1277 return (fail == 0);
1278 }
1279
1280 static int
1281 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1282 char *chost)
1283 {
1284 Buffer b;
1285 char *p, *userstyle;
1286 u_int len;
1287 int fail = 0;
1288
1289 buffer_init(&b);
1290 buffer_append(&b, data, datalen);
1291
1292 p = buffer_get_string(&b, &len);
1293 if ((session_id2 == NULL) ||
1294 (len != session_id2_len) ||
1295 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1296 fail++;
1297 free(p);
1298
1299 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1300 fail++;
1301 p = buffer_get_cstring(&b, NULL);
1302 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1303 authctxt->style ? ":" : "",
1304 authctxt->style ? authctxt->style : "");
1305 if (strcmp(userstyle, p) != 0) {
1306 logit("wrong user name passed to monitor: expected %s != %.100s",
1307 userstyle, p);
1308 fail++;
1309 }
1310 free(userstyle);
1311 free(p);
1312 buffer_skip_string(&b); /* service */
1313 p = buffer_get_cstring(&b, NULL);
1314 if (strcmp(p, "hostbased") != 0)
1315 fail++;
1316 free(p);
1317 buffer_skip_string(&b); /* pkalg */
1318 buffer_skip_string(&b); /* pkblob */
1319
1320 /* verify client host, strip trailing dot if necessary */
1321 p = buffer_get_string(&b, NULL);
1322 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1323 p[len - 1] = '\0';
1324 if (strcmp(p, chost) != 0)
1325 fail++;
1326 free(p);
1327
1328 /* verify client user */
1329 p = buffer_get_string(&b, NULL);
1330 if (strcmp(p, cuser) != 0)
1331 fail++;
1332 free(p);
1333
1334 if (buffer_len(&b) != 0)
1335 fail++;
1336 buffer_free(&b);
1337 return (fail == 0);
1338 }
1339
1340 int
1341 mm_answer_keyverify(int sock, Buffer *m)
1342 {
1343 Key *key;
1344 u_char *signature, *data, *blob;
1345 u_int signaturelen, datalen, bloblen;
1346 int verified = 0;
1347 int valid_data = 0;
1348
1349 blob = buffer_get_string(m, &bloblen);
1350 signature = buffer_get_string(m, &signaturelen);
1351 data = buffer_get_string(m, &datalen);
1352
1353 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1354 !monitor_allowed_key(blob, bloblen))
1355 fatal("%s: bad key, not previously allowed", __func__);
1356
1357 key = key_from_blob(blob, bloblen);
1358 if (key == NULL)
1359 fatal("%s: bad public key blob", __func__);
1360
1361 switch (key_blobtype) {
1362 case MM_USERKEY:
1363 valid_data = monitor_valid_userblob(data, datalen);
1364 break;
1365 case MM_HOSTKEY:
1366 valid_data = monitor_valid_hostbasedblob(data, datalen,
1367 hostbased_cuser, hostbased_chost);
1368 break;
1369 default:
1370 valid_data = 0;
1371 break;
1372 }
1373 if (!valid_data)
1374 fatal("%s: bad signature data blob", __func__);
1375
1376 verified = key_verify(key, signature, signaturelen, data, datalen);
1377 debug3("%s: key %p signature %s",
1378 __func__, key, (verified == 1) ? "verified" : "unverified");
1379
1380 key_free(key);
1381 free(blob);
1382 free(signature);
1383 free(data);
1384
1385 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1386
1387 monitor_reset_key_state();
1388
1389 buffer_clear(m);
1390 buffer_put_int(m, verified);
1391 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1392
1393 return (verified == 1);
1394 }
1395
1396 static void
1397 mm_record_login(Session *s, struct passwd *pw)
1398 {
1399 socklen_t fromlen;
1400 struct sockaddr_storage from;
1401
1402 /*
1403 * Get IP address of client. If the connection is not a socket, let
1404 * the address be 0.0.0.0.
1405 */
1406 memset(&from, 0, sizeof(from));
1407 fromlen = sizeof(from);
1408 if (packet_connection_is_on_socket()) {
1409 if (getpeername(packet_get_connection_in(),
1410 (struct sockaddr *)&from, &fromlen) < 0) {
1411 debug("getpeername: %.100s", strerror(errno));
1412 cleanup_exit(255);
1413 }
1414 }
1415 /* Record that there was a login on that tty from the remote host. */
1416 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1417 get_remote_name_or_ip(utmp_len, options.use_dns),
1418 (struct sockaddr *)&from, fromlen);
1419 }
1420
1421 static void
1422 mm_session_close(Session *s)
1423 {
1424 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1425 if (s->ttyfd != -1) {
1426 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1427 session_pty_cleanup2(s);
1428 }
1429 session_unused(s->self);
1430 }
1431
1432 int
1433 mm_answer_pty(int sock, Buffer *m)
1434 {
1435 extern struct monitor *pmonitor;
1436 Session *s;
1437 int res, fd0;
1438
1439 debug3("%s entering", __func__);
1440
1441 buffer_clear(m);
1442 s = session_new();
1443 if (s == NULL)
1444 goto error;
1445 s->authctxt = authctxt;
1446 s->pw = authctxt->pw;
1447 s->pid = pmonitor->m_pid;
1448 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1449 if (res == 0)
1450 goto error;
1451 pty_setowner(authctxt->pw, s->tty);
1452
1453 buffer_put_int(m, 1);
1454 buffer_put_cstring(m, s->tty);
1455
1456 /* We need to trick ttyslot */
1457 if (dup2(s->ttyfd, 0) == -1)
1458 fatal("%s: dup2", __func__);
1459
1460 mm_record_login(s, authctxt->pw);
1461
1462 /* Now we can close the file descriptor again */
1463 close(0);
1464
1465 /* send messages generated by record_login */
1466 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1467 buffer_clear(&loginmsg);
1468
1469 mm_request_send(sock, MONITOR_ANS_PTY, m);
1470
1471 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1472 mm_send_fd(sock, s->ttyfd) == -1)
1473 fatal("%s: send fds failed", __func__);
1474
1475 /* make sure nothing uses fd 0 */
1476 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1477 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1478 if (fd0 != 0)
1479 error("%s: fd0 %d != 0", __func__, fd0);
1480
1481 /* slave is not needed */
1482 close(s->ttyfd);
1483 s->ttyfd = s->ptyfd;
1484 /* no need to dup() because nobody closes ptyfd */
1485 s->ptymaster = s->ptyfd;
1486
1487 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1488
1489 return (0);
1490
1491 error:
1492 if (s != NULL)
1493 mm_session_close(s);
1494 buffer_put_int(m, 0);
1495 mm_request_send(sock, MONITOR_ANS_PTY, m);
1496 return (0);
1497 }
1498
1499 int
1500 mm_answer_pty_cleanup(int sock, Buffer *m)
1501 {
1502 Session *s;
1503 char *tty;
1504
1505 debug3("%s entering", __func__);
1506
1507 tty = buffer_get_string(m, NULL);
1508 if ((s = session_by_tty(tty)) != NULL)
1509 mm_session_close(s);
1510 buffer_clear(m);
1511 free(tty);
1512 return (0);
1513 }
1514
1515 int
1516 mm_answer_sesskey(int sock, Buffer *m)
1517 {
1518 BIGNUM *p;
1519 int rsafail;
1520
1521 /* Turn off permissions */
1522 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1523
1524 if ((p = BN_new()) == NULL)
1525 fatal("%s: BN_new", __func__);
1526
1527 buffer_get_bignum2(m, p);
1528
1529 rsafail = ssh1_session_key(p);
1530
1531 buffer_clear(m);
1532 buffer_put_int(m, rsafail);
1533 buffer_put_bignum2(m, p);
1534
1535 BN_clear_free(p);
1536
1537 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1538
1539 /* Turn on permissions for sessid passing */
1540 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1541
1542 return (0);
1543 }
1544
1545 int
1546 mm_answer_sessid(int sock, Buffer *m)
1547 {
1548 int i;
1549
1550 debug3("%s entering", __func__);
1551
1552 if (buffer_len(m) != 16)
1553 fatal("%s: bad ssh1 session id", __func__);
1554 for (i = 0; i < 16; i++)
1555 session_id[i] = buffer_get_char(m);
1556
1557 /* Turn on permissions for getpwnam */
1558 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1559
1560 return (0);
1561 }
1562
1563 int
1564 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1565 {
1566 BIGNUM *client_n;
1567 Key *key = NULL;
1568 u_char *blob = NULL;
1569 u_int blen = 0;
1570 int allowed = 0;
1571
1572 debug3("%s entering", __func__);
1573
1574 auth_method = "rsa";
1575 if (options.rsa_authentication && authctxt->valid) {
1576 if ((client_n = BN_new()) == NULL)
1577 fatal("%s: BN_new", __func__);
1578 buffer_get_bignum2(m, client_n);
1579 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1580 BN_clear_free(client_n);
1581 }
1582 buffer_clear(m);
1583 buffer_put_int(m, allowed);
1584 buffer_put_int(m, forced_command != NULL);
1585
1586 /* clear temporarily storage (used by generate challenge) */
1587 monitor_reset_key_state();
1588
1589 if (allowed && key != NULL) {
1590 key->type = KEY_RSA; /* cheat for key_to_blob */
1591 if (key_to_blob(key, &blob, &blen) == 0)
1592 fatal("%s: key_to_blob failed", __func__);
1593 buffer_put_string(m, blob, blen);
1594
1595 /* Save temporarily for comparison in verify */
1596 key_blob = blob;
1597 key_bloblen = blen;
1598 key_blobtype = MM_RSAUSERKEY;
1599 }
1600 if (key != NULL)
1601 key_free(key);
1602
1603 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1604
1605 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1606 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1607 return (0);
1608 }
1609
1610 int
1611 mm_answer_rsa_challenge(int sock, Buffer *m)
1612 {
1613 Key *key = NULL;
1614 u_char *blob;
1615 u_int blen;
1616
1617 debug3("%s entering", __func__);
1618
1619 if (!authctxt->valid)
1620 fatal("%s: authctxt not valid", __func__);
1621 blob = buffer_get_string(m, &blen);
1622 if (!monitor_allowed_key(blob, blen))
1623 fatal("%s: bad key, not previously allowed", __func__);
1624 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1625 fatal("%s: key type mismatch", __func__);
1626 if ((key = key_from_blob(blob, blen)) == NULL)
1627 fatal("%s: received bad key", __func__);
1628 if (key->type != KEY_RSA)
1629 fatal("%s: received bad key type %d", __func__, key->type);
1630 key->type = KEY_RSA1;
1631 if (ssh1_challenge)
1632 BN_clear_free(ssh1_challenge);
1633 ssh1_challenge = auth_rsa_generate_challenge(key);
1634
1635 buffer_clear(m);
1636 buffer_put_bignum2(m, ssh1_challenge);
1637
1638 debug3("%s sending reply", __func__);
1639 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1640
1641 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1642
1643 free(blob);
1644 key_free(key);
1645 return (0);
1646 }
1647
1648 int
1649 mm_answer_rsa_response(int sock, Buffer *m)
1650 {
1651 Key *key = NULL;
1652 u_char *blob, *response;
1653 u_int blen, len;
1654 int success;
1655
1656 debug3("%s entering", __func__);
1657
1658 if (!authctxt->valid)
1659 fatal("%s: authctxt not valid", __func__);
1660 if (ssh1_challenge == NULL)
1661 fatal("%s: no ssh1_challenge", __func__);
1662
1663 blob = buffer_get_string(m, &blen);
1664 if (!monitor_allowed_key(blob, blen))
1665 fatal("%s: bad key, not previously allowed", __func__);
1666 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1667 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1668 if ((key = key_from_blob(blob, blen)) == NULL)
1669 fatal("%s: received bad key", __func__);
1670 response = buffer_get_string(m, &len);
1671 if (len != 16)
1672 fatal("%s: received bad response to challenge", __func__);
1673 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1674
1675 free(blob);
1676 key_free(key);
1677 free(response);
1678
1679 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1680
1681 /* reset state */
1682 BN_clear_free(ssh1_challenge);
1683 ssh1_challenge = NULL;
1684 monitor_reset_key_state();
1685
1686 buffer_clear(m);
1687 buffer_put_int(m, success);
1688 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1689
1690 return (success);
1691 }
1692
1693 #ifdef KRB4
1694 int
1695 mm_answer_krb4(int socket, Buffer *m)
1696 {
1697 KTEXT_ST auth, reply;
1698 char *client, *p;
1699 int success;
1700 u_int alen;
1701
1702 reply.length = auth.length = 0;
1703
1704 p = buffer_get_string(m, &alen);
1705 if (alen >= MAX_KTXT_LEN)
1706 fatal("%s: auth too large", __func__);
1707 memcpy(auth.dat, p, alen);
1708 auth.length = alen;
1709 memset(p, 0, alen);
1710 free(p);
1711
1712 success = options.kerberos_authentication &&
1713 authctxt->valid &&
1714 auth_krb4(authctxt, &auth, &client, &reply);
1715
1716 memset(auth.dat, 0, alen);
1717 buffer_clear(m);
1718 buffer_put_int(m, success);
1719
1720 if (success) {
1721 buffer_put_cstring(m, client);
1722 buffer_put_string(m, reply.dat, reply.length);
1723 if (client)
1724 free(client);
1725 if (reply.length)
1726 memset(reply.dat, 0, reply.length);
1727 }
1728
1729 debug3("%s: sending result %d", __func__, success);
1730 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1731
1732 auth_method = "kerberos";
1733
1734 /* Causes monitor loop to terminate if authenticated */
1735 return (success);
1736 }
1737 #endif
1738
1739 #ifdef KRB5
1740 int
1741 mm_answer_krb5(int xsocket, Buffer *m)
1742 {
1743 krb5_data tkt, reply;
1744 char *client_user;
1745 u_int len;
1746 int success;
1747
1748 /* use temporary var to avoid size issues on 64bit arch */
1749 tkt.data = buffer_get_string(m, &len);
1750 tkt.length = len;
1751
1752 success = options.kerberos_authentication &&
1753 authctxt->valid &&
1754 auth_krb5(authctxt, &tkt, &client_user, &reply);
1755
1756 if (tkt.length)
1757 free(tkt.data);
1758
1759 buffer_clear(m);
1760 buffer_put_int(m, success);
1761
1762 if (success) {
1763 buffer_put_cstring(m, client_user);
1764 buffer_put_string(m, reply.data, reply.length);
1765 if (client_user)
1766 free(client_user);
1767 if (reply.length)
1768 free(reply.data);
1769 }
1770 mm_request_send(xsocket, MONITOR_ANS_KRB5, m);
1771
1772 auth_method = "kerberos";
1773
1774 return success;
1775 }
1776 #endif
1777
1778 int
1779 mm_answer_term(int sock, Buffer *req)
1780 {
1781 extern struct monitor *pmonitor;
1782 int res, status;
1783
1784 debug3("%s: tearing down sessions", __func__);
1785
1786 /* The child is terminating */
1787 session_destroy_all(&mm_session_close);
1788
1789 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1790 if (errno != EINTR)
1791 exit(1);
1792
1793 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1794
1795 /* Terminate process */
1796 exit(res);
1797 }
1798
1799 void
1800 monitor_apply_keystate(struct monitor *pmonitor)
1801 {
1802 if (compat20) {
1803 set_newkeys(MODE_IN);
1804 set_newkeys(MODE_OUT);
1805 } else {
1806 packet_set_protocol_flags(child_state.ssh1protoflags);
1807 packet_set_encryption_key(child_state.ssh1key,
1808 child_state.ssh1keylen, child_state.ssh1cipher);
1809 free(child_state.ssh1key);
1810 }
1811
1812 /* for rc4 and other stateful ciphers */
1813 packet_set_keycontext(MODE_OUT, child_state.keyout);
1814 free(child_state.keyout);
1815 packet_set_keycontext(MODE_IN, child_state.keyin);
1816 free(child_state.keyin);
1817
1818 if (!compat20) {
1819 packet_set_iv(MODE_OUT, child_state.ivout);
1820 free(child_state.ivout);
1821 packet_set_iv(MODE_IN, child_state.ivin);
1822 free(child_state.ivin);
1823 }
1824
1825 memcpy(&incoming_stream, &child_state.incoming,
1826 sizeof(incoming_stream));
1827 memcpy(&outgoing_stream, &child_state.outgoing,
1828 sizeof(outgoing_stream));
1829
1830 /* Update with new address */
1831 if (options.compression)
1832 mm_init_compression(pmonitor->m_zlib);
1833
1834 if (options.rekey_limit || options.rekey_interval)
1835 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
1836 (time_t)options.rekey_interval);
1837
1838 /* Network I/O buffers */
1839 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1840 buffer_clear(packet_get_input());
1841 buffer_append(packet_get_input(), child_state.input, child_state.ilen);
1842 memset(child_state.input, 0, child_state.ilen);
1843 free(child_state.input);
1844
1845 buffer_clear(packet_get_output());
1846 buffer_append(packet_get_output(), child_state.output,
1847 child_state.olen);
1848 memset(child_state.output, 0, child_state.olen);
1849 free(child_state.output);
1850
1851 /* Roaming */
1852 if (compat20)
1853 roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes);
1854 }
1855
1856 static Kex *
1857 mm_get_kex(Buffer *m)
1858 {
1859 Kex *kex;
1860 void *blob;
1861 u_int bloblen;
1862
1863 kex = xcalloc(1, sizeof(*kex));
1864 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1865 if (session_id2 == NULL ||
1866 kex->session_id_len != session_id2_len ||
1867 timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0)
1868 fatal("mm_get_get: internal error: bad session id");
1869 kex->we_need = buffer_get_int(m);
1870 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1871 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1872 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1873 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1874 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1875 kex->server = 1;
1876 kex->hostkey_type = buffer_get_int(m);
1877 kex->kex_type = buffer_get_int(m);
1878 blob = buffer_get_string(m, &bloblen);
1879 buffer_init(&kex->my);
1880 buffer_append(&kex->my, blob, bloblen);
1881 free(blob);
1882 blob = buffer_get_string(m, &bloblen);
1883 buffer_init(&kex->peer);
1884 buffer_append(&kex->peer, blob, bloblen);
1885 free(blob);
1886 kex->done = 1;
1887 kex->flags = buffer_get_int(m);
1888 kex->client_version_string = buffer_get_string(m, NULL);
1889 kex->server_version_string = buffer_get_string(m, NULL);
1890 kex->load_host_public_key=&get_hostkey_public_by_type;
1891 kex->load_host_private_key=&get_hostkey_private_by_type;
1892 kex->host_key_index=&get_hostkey_index;
1893 kex->sign = sshd_hostkey_sign;
1894
1895 return (kex);
1896 }
1897
1898 /* This function requries careful sanity checking */
1899
1900 void
1901 mm_get_keystate(struct monitor *pmonitor)
1902 {
1903 Buffer m;
1904 u_char *blob, *p;
1905 u_int bloblen, plen;
1906 u_int32_t seqnr, packets;
1907 u_int64_t blocks, bytes;
1908
1909 debug3("%s: Waiting for new keys", __func__);
1910
1911 buffer_init(&m);
1912 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1913 if (!compat20) {
1914 child_state.ssh1protoflags = buffer_get_int(&m);
1915 child_state.ssh1cipher = buffer_get_int(&m);
1916 child_state.ssh1key = buffer_get_string(&m,
1917 &child_state.ssh1keylen);
1918 child_state.ivout = buffer_get_string(&m,
1919 &child_state.ivoutlen);
1920 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1921 goto skip;
1922 } else {
1923 /* Get the Kex for rekeying */
1924 *pmonitor->m_pkex = mm_get_kex(&m);
1925 }
1926
1927 blob = buffer_get_string(&m, &bloblen);
1928 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1929 free(blob);
1930
1931 debug3("%s: Waiting for second key", __func__);
1932 blob = buffer_get_string(&m, &bloblen);
1933 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1934 free(blob);
1935
1936 /* Now get sequence numbers for the packets */
1937 seqnr = buffer_get_int(&m);
1938 blocks = buffer_get_int64(&m);
1939 packets = buffer_get_int(&m);
1940 bytes = buffer_get_int64(&m);
1941 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1942 seqnr = buffer_get_int(&m);
1943 blocks = buffer_get_int64(&m);
1944 packets = buffer_get_int(&m);
1945 bytes = buffer_get_int64(&m);
1946 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1947
1948 skip:
1949 /* Get the key context */
1950 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1951 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1952
1953 debug3("%s: Getting compression state", __func__);
1954 /* Get compression state */
1955 p = buffer_get_string(&m, &plen);
1956 if (plen != sizeof(child_state.outgoing))
1957 fatal("%s: bad request size", __func__);
1958 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1959 free(p);
1960
1961 p = buffer_get_string(&m, &plen);
1962 if (plen != sizeof(child_state.incoming))
1963 fatal("%s: bad request size", __func__);
1964 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1965 free(p);
1966
1967 /* Network I/O buffers */
1968 debug3("%s: Getting Network I/O buffers", __func__);
1969 child_state.input = buffer_get_string(&m, &child_state.ilen);
1970 child_state.output = buffer_get_string(&m, &child_state.olen);
1971
1972 /* Roaming */
1973 if (compat20) {
1974 child_state.sent_bytes = buffer_get_int64(&m);
1975 child_state.recv_bytes = buffer_get_int64(&m);
1976 }
1977
1978 buffer_free(&m);
1979 }
1980
1981
1982 /* Allocation functions for zlib */
1983 void *
1984 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1985 {
1986 size_t len = (size_t) size * ncount;
1987 void *address;
1988
1989 if (len == 0 || ncount > SIZE_T_MAX / size)
1990 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1991
1992 address = mm_malloc(mm, len);
1993
1994 return (address);
1995 }
1996
1997 void
1998 mm_zfree(struct mm_master *mm, void *address)
1999 {
2000 mm_free(mm, address);
2001 }
2002
2003 void
2004 mm_init_compression(struct mm_master *mm)
2005 {
2006 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
2007 outgoing_stream.zfree = (free_func)mm_zfree;
2008 outgoing_stream.opaque = mm;
2009
2010 incoming_stream.zalloc = (alloc_func)mm_zalloc;
2011 incoming_stream.zfree = (free_func)mm_zfree;
2012 incoming_stream.opaque = mm;
2013 }
2014
2015 /* XXX */
2016
2017 #define FD_CLOSEONEXEC(x) do { \
2018 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
2019 fatal("fcntl(%d, F_SETFD)", x); \
2020 } while (0)
2021
2022 static void
2023 monitor_openfds(struct monitor *mon, int do_logfds)
2024 {
2025 int pair[2];
2026
2027 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
2028 fatal("%s: socketpair: %s", __func__, strerror(errno));
2029 FD_CLOSEONEXEC(pair[0]);
2030 FD_CLOSEONEXEC(pair[1]);
2031 mon->m_recvfd = pair[0];
2032 mon->m_sendfd = pair[1];
2033
2034 if (do_logfds) {
2035 if (pipe(pair) == -1)
2036 fatal("%s: pipe: %s", __func__, strerror(errno));
2037 FD_CLOSEONEXEC(pair[0]);
2038 FD_CLOSEONEXEC(pair[1]);
2039 mon->m_log_recvfd = pair[0];
2040 mon->m_log_sendfd = pair[1];
2041 } else
2042 mon->m_log_recvfd = mon->m_log_sendfd = -1;
2043 }
2044
2045 #define MM_MEMSIZE 65536
2046
2047 struct monitor *
2048 monitor_init(void)
2049 {
2050 struct monitor *mon;
2051
2052 mon = xcalloc(1, sizeof(*mon));
2053
2054 monitor_openfds(mon, 1);
2055
2056 /* Used to share zlib space across processes */
2057 if (options.compression) {
2058 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
2059 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
2060
2061 /* Compression needs to share state across borders */
2062 mm_init_compression(mon->m_zlib);
2063 }
2064
2065 return mon;
2066 }
2067
2068 void
2069 monitor_reinit(struct monitor *mon)
2070 {
2071 monitor_openfds(mon, 0);
2072 }
2073
2074 #ifdef GSSAPI
2075 int
2076 mm_answer_gss_setup_ctx(int sock, Buffer *m)
2077 {
2078 gss_OID_desc goid;
2079 OM_uint32 major;
2080 u_int len;
2081
2082 goid.elements = buffer_get_string(m, &len);
2083 goid.length = len;
2084
2085 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
2086
2087 free(goid.elements);
2088
2089 buffer_clear(m);
2090 buffer_put_int(m, major);
2091
2092 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
2093
2094 /* Now we have a context, enable the step */
2095 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
2096
2097 return (0);
2098 }
2099
2100 int
2101 mm_answer_gss_accept_ctx(int sock, Buffer *m)
2102 {
2103 gss_buffer_desc in;
2104 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
2105 OM_uint32 major, minor;
2106 OM_uint32 flags = 0; /* GSI needs this */
2107 u_int len;
2108
2109 in.value = buffer_get_string(m, &len);
2110 in.length = len;
2111 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
2112 free(in.value);
2113
2114 buffer_clear(m);
2115 buffer_put_int(m, major);
2116 buffer_put_string(m, out.value, out.length);
2117 buffer_put_int(m, flags);
2118 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
2119
2120 gss_release_buffer(&minor, &out);
2121
2122 if (major == GSS_S_COMPLETE) {
2123 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
2124 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2125 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
2126 }
2127 return (0);
2128 }
2129
2130 int
2131 mm_answer_gss_checkmic(int sock, Buffer *m)
2132 {
2133 gss_buffer_desc gssbuf, mic;
2134 OM_uint32 ret;
2135 u_int len;
2136
2137 gssbuf.value = buffer_get_string(m, &len);
2138 gssbuf.length = len;
2139 mic.value = buffer_get_string(m, &len);
2140 mic.length = len;
2141
2142 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
2143
2144 free(gssbuf.value);
2145 free(mic.value);
2146
2147 buffer_clear(m);
2148 buffer_put_int(m, ret);
2149
2150 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
2151
2152 if (!GSS_ERROR(ret))
2153 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
2154
2155 return (0);
2156 }
2157
2158 int
2159 mm_answer_gss_userok(int sock, Buffer *m)
2160 {
2161 int authenticated;
2162
2163 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2164
2165 buffer_clear(m);
2166 buffer_put_int(m, authenticated);
2167
2168 debug3("%s: sending result %d", __func__, authenticated);
2169 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2170
2171 auth_method = "gssapi-with-mic";
2172
2173 /* Monitor loop will terminate if authenticated */
2174 return (authenticated);
2175 }
2176 #endif /* GSSAPI */
2177
2178 #ifdef JPAKE
2179 int
2180 mm_answer_jpake_step1(int sock, Buffer *m)
2181 {
2182 struct jpake_ctx *pctx;
2183 u_char *x3_proof, *x4_proof;
2184 u_int x3_proof_len, x4_proof_len;
2185
2186 if (!options.zero_knowledge_password_authentication)
2187 fatal("zero_knowledge_password_authentication disabled");
2188
2189 if (authctxt->jpake_ctx != NULL)
2190 fatal("%s: authctxt->jpake_ctx already set (%p)",
2191 __func__, authctxt->jpake_ctx);
2192 authctxt->jpake_ctx = pctx = jpake_new();
2193
2194 jpake_step1(pctx->grp,
2195 &pctx->server_id, &pctx->server_id_len,
2196 &pctx->x3, &pctx->x4, &pctx->g_x3, &pctx->g_x4,
2197 &x3_proof, &x3_proof_len,
2198 &x4_proof, &x4_proof_len);
2199
2200 JPAKE_DEBUG_CTX((pctx, "step1 done in %s", __func__));
2201
2202 buffer_clear(m);
2203
2204 buffer_put_string(m, pctx->server_id, pctx->server_id_len);
2205 buffer_put_bignum2(m, pctx->g_x3);
2206 buffer_put_bignum2(m, pctx->g_x4);
2207 buffer_put_string(m, x3_proof, x3_proof_len);
2208 buffer_put_string(m, x4_proof, x4_proof_len);
2209
2210 debug3("%s: sending step1", __func__);
2211 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP1, m);
2212
2213 bzero(x3_proof, x3_proof_len);
2214 bzero(x4_proof, x4_proof_len);
2215 free(x3_proof);
2216 free(x4_proof);
2217
2218 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_GET_PWDATA, 1);
2219 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 0);
2220
2221 return 0;
2222 }
2223
2224 int
2225 mm_answer_jpake_get_pwdata(int sock, Buffer *m)
2226 {
2227 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2228 char *hash_scheme, *salt;
2229
2230 if (pctx == NULL)
2231 fatal("%s: pctx == NULL", __func__);
2232
2233 auth2_jpake_get_pwdata(authctxt, &pctx->s, &hash_scheme, &salt);
2234
2235 buffer_clear(m);
2236 /* pctx->s is sensitive, not returned to slave */
2237 buffer_put_cstring(m, hash_scheme);
2238 buffer_put_cstring(m, salt);
2239
2240 debug3("%s: sending pwdata", __func__);
2241 mm_request_send(sock, MONITOR_ANS_JPAKE_GET_PWDATA, m);
2242
2243 bzero(hash_scheme, strlen(hash_scheme));
2244 bzero(salt, strlen(salt));
2245 free(hash_scheme);
2246 free(salt);
2247
2248 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP2, 1);
2249
2250 return 0;
2251 }
2252
2253 int
2254 mm_answer_jpake_step2(int sock, Buffer *m)
2255 {
2256 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2257 u_char *x1_proof, *x2_proof, *x4_s_proof;
2258 u_int x1_proof_len, x2_proof_len, x4_s_proof_len;
2259
2260 if (pctx == NULL)
2261 fatal("%s: pctx == NULL", __func__);
2262
2263 if ((pctx->g_x1 = BN_new()) == NULL ||
2264 (pctx->g_x2 = BN_new()) == NULL)
2265 fatal("%s: BN_new", __func__);
2266 buffer_get_bignum2(m, pctx->g_x1);
2267 buffer_get_bignum2(m, pctx->g_x2);
2268 pctx->client_id = buffer_get_string(m, &pctx->client_id_len);
2269 x1_proof = buffer_get_string(m, &x1_proof_len);
2270 x2_proof = buffer_get_string(m, &x2_proof_len);
2271
2272 jpake_step2(pctx->grp, pctx->s, pctx->g_x3,
2273 pctx->g_x1, pctx->g_x2, pctx->x4,
2274 pctx->client_id, pctx->client_id_len,
2275 pctx->server_id, pctx->server_id_len,
2276 x1_proof, x1_proof_len,
2277 x2_proof, x2_proof_len,
2278 &pctx->b,
2279 &x4_s_proof, &x4_s_proof_len);
2280
2281 JPAKE_DEBUG_CTX((pctx, "step2 done in %s", __func__));
2282
2283 bzero(x1_proof, x1_proof_len);
2284 bzero(x2_proof, x2_proof_len);
2285 free(x1_proof);
2286 free(x2_proof);
2287
2288 buffer_clear(m);
2289
2290 buffer_put_bignum2(m, pctx->b);
2291 buffer_put_string(m, x4_s_proof, x4_s_proof_len);
2292
2293 debug3("%s: sending step2", __func__);
2294 mm_request_send(sock, MONITOR_ANS_JPAKE_STEP2, m);
2295
2296 bzero(x4_s_proof, x4_s_proof_len);
2297 free(x4_s_proof);
2298
2299 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_KEY_CONFIRM, 1);
2300
2301 return 0;
2302 }
2303
2304 int
2305 mm_answer_jpake_key_confirm(int sock, Buffer *m)
2306 {
2307 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2308 u_char *x2_s_proof;
2309 u_int x2_s_proof_len;
2310
2311 if (pctx == NULL)
2312 fatal("%s: pctx == NULL", __func__);
2313
2314 if ((pctx->a = BN_new()) == NULL)
2315 fatal("%s: BN_new", __func__);
2316 buffer_get_bignum2(m, pctx->a);
2317 x2_s_proof = buffer_get_string(m, &x2_s_proof_len);
2318
2319 jpake_key_confirm(pctx->grp, pctx->s, pctx->a,
2320 pctx->x4, pctx->g_x3, pctx->g_x4, pctx->g_x1, pctx->g_x2,
2321 pctx->server_id, pctx->server_id_len,
2322 pctx->client_id, pctx->client_id_len,
2323 session_id2, session_id2_len,
2324 x2_s_proof, x2_s_proof_len,
2325 &pctx->k,
2326 &pctx->h_k_sid_sessid, &pctx->h_k_sid_sessid_len);
2327
2328 JPAKE_DEBUG_CTX((pctx, "key_confirm done in %s", __func__));
2329
2330 bzero(x2_s_proof, x2_s_proof_len);
2331 buffer_clear(m);
2332
2333 /* pctx->k is sensitive, not sent */
2334 buffer_put_string(m, pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len);
2335
2336 debug3("%s: sending confirmation hash", __func__);
2337 mm_request_send(sock, MONITOR_ANS_JPAKE_KEY_CONFIRM, m);
2338
2339 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_CHECK_CONFIRM, 1);
2340
2341 return 0;
2342 }
2343
2344 int
2345 mm_answer_jpake_check_confirm(int sock, Buffer *m)
2346 {
2347 int authenticated = 0;
2348 u_char *peer_confirm_hash;
2349 u_int peer_confirm_hash_len;
2350 struct jpake_ctx *pctx = authctxt->jpake_ctx;
2351
2352 if (pctx == NULL)
2353 fatal("%s: pctx == NULL", __func__);
2354
2355 peer_confirm_hash = buffer_get_string(m, &peer_confirm_hash_len);
2356
2357 authenticated = jpake_check_confirm(pctx->k,
2358 pctx->client_id, pctx->client_id_len,
2359 session_id2, session_id2_len,
2360 peer_confirm_hash, peer_confirm_hash_len) && authctxt->valid;
2361
2362 JPAKE_DEBUG_CTX((pctx, "check_confirm done in %s", __func__));
2363
2364 bzero(peer_confirm_hash, peer_confirm_hash_len);
2365 free(peer_confirm_hash);
2366
2367 buffer_clear(m);
2368 buffer_put_int(m, authenticated);
2369
2370 debug3("%s: sending result %d", __func__, authenticated);
2371 mm_request_send(sock, MONITOR_ANS_JPAKE_CHECK_CONFIRM, m);
2372
2373 monitor_permit(mon_dispatch, MONITOR_REQ_JPAKE_STEP1, 1);
2374
2375 auth_method = "jpake-01 (at) openssh.com";
2376 return authenticated;
2377 }
2378
2379 #endif /* JPAKE */
2380