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