monitor.c revision 1.23 1 /* $NetBSD: monitor.c,v 1.23 2017/10/07 19:39:19 christos Exp $ */
2 /* $OpenBSD: monitor.c,v 1.174 2017/10/02 19:33:20 djm Exp $ */
3 /*
4 * Copyright 2002 Niels Provos <provos (at) citi.umich.edu>
5 * Copyright 2002 Markus Friedl <markus (at) openbsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "includes.h"
30 __RCSID("$NetBSD: monitor.c,v 1.23 2017/10/07 19:39:19 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/queue.h>
36
37 #ifdef WITH_OPENSSL
38 #include <openssl/dh.h>
39 #endif
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <paths.h>
45 #include <poll.h>
46 #include <pwd.h>
47 #include <signal.h>
48 #include <stdarg.h>
49 #include <unistd.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "atomicio.h"
56 #include "xmalloc.h"
57 #include "ssh.h"
58 #include "key.h"
59 #include "buffer.h"
60 #include "hostfile.h"
61 #include "auth.h"
62 #include "cipher.h"
63 #include "kex.h"
64 #include "dh.h"
65 #include <zlib.h>
66 #include "packet.h"
67 #include "auth-options.h"
68 #include "sshpty.h"
69 #include "channels.h"
70 #include "session.h"
71 #include "sshlogin.h"
72 #include "canohost.h"
73 #include "log.h"
74 #include "misc.h"
75 #include "servconf.h"
76 #include "monitor.h"
77 #ifdef GSSAPI
78 #include "ssh-gss.h"
79 #endif
80 #include "monitor_wrap.h"
81 #include "monitor_fdpass.h"
82 #include "compat.h"
83 #include "ssh2.h"
84 #include "authfd.h"
85 #include "match.h"
86 #include "ssherr.h"
87
88 #ifdef GSSAPI
89 static Gssctxt *gsscontext = NULL;
90 #endif
91
92 /* Imports */
93 extern ServerOptions options;
94 extern u_int utmp_len;
95 extern u_char session_id[];
96 extern Buffer auth_debug;
97 extern int auth_debug_init;
98 extern Buffer loginmsg;
99
100 /* State exported from the child */
101 static struct sshbuf *child_state;
102
103 /* Functions on the monitor that answer unprivileged requests */
104
105 int mm_answer_moduli(int, Buffer *);
106 int mm_answer_sign(int, Buffer *);
107 int mm_answer_pwnamallow(int, Buffer *);
108 int mm_answer_auth2_read_banner(int, Buffer *);
109 int mm_answer_authserv(int, Buffer *);
110 int mm_answer_authpassword(int, Buffer *);
111 int mm_answer_bsdauthquery(int, Buffer *);
112 int mm_answer_bsdauthrespond(int, Buffer *);
113 int mm_answer_skeyquery(int, Buffer *);
114 int mm_answer_skeyrespond(int, Buffer *);
115 int mm_answer_keyallowed(int, Buffer *);
116 int mm_answer_keyverify(int, Buffer *);
117 int mm_answer_pty(int, Buffer *);
118 int mm_answer_pty_cleanup(int, Buffer *);
119 int mm_answer_term(int, Buffer *);
120 int mm_answer_rsa_keyallowed(int, Buffer *);
121 int mm_answer_rsa_challenge(int, Buffer *);
122 int mm_answer_rsa_response(int, Buffer *);
123 int mm_answer_sesskey(int, Buffer *);
124 int mm_answer_sessid(int, Buffer *);
125
126 #ifdef USE_PAM
127 int mm_answer_pam_start(int, Buffer *);
128 int mm_answer_pam_account(int, Buffer *);
129 int mm_answer_pam_init_ctx(int, Buffer *);
130 int mm_answer_pam_query(int, Buffer *);
131 int mm_answer_pam_respond(int, Buffer *);
132 int mm_answer_pam_free_ctx(int, Buffer *);
133 #endif
134
135 #ifdef KRB5
136 int mm_answer_krb5(int, Buffer *);
137 #endif
138
139 #ifdef GSSAPI
140 int mm_answer_gss_setup_ctx(int, Buffer *);
141 int mm_answer_gss_accept_ctx(int, Buffer *);
142 int mm_answer_gss_userok(int, Buffer *);
143 int mm_answer_gss_checkmic(int, Buffer *);
144 #endif
145
146 static int monitor_read_log(struct monitor *);
147
148 static Authctxt *authctxt;
149
150 /* local state for key verify */
151 static u_char *key_blob = NULL;
152 static u_int key_bloblen = 0;
153 static int key_blobtype = MM_NOKEY;
154 static char *hostbased_cuser = NULL;
155 static char *hostbased_chost = NULL;
156 static const char *auth_method = "unknown";
157 static const char *auth_submethod = NULL;
158 static u_int session_id2_len = 0;
159 static u_char *session_id2 = NULL;
160 static pid_t monitor_child_pid;
161
162 struct mon_table {
163 enum monitor_reqtype type;
164 int flags;
165 int (*f)(int, Buffer *);
166 };
167
168 #define MON_ISAUTH 0x0004 /* Required for Authentication */
169 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
170 #define MON_ONCE 0x0010 /* Disable after calling */
171 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
172
173 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
174
175 #define MON_PERMIT 0x1000 /* Request is permitted */
176
177 struct mon_table mon_dispatch_proto20[] = {
178 #ifdef WITH_OPENSSL
179 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
180 #endif
181 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
182 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
183 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
184 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
185 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
186 #ifdef USE_PAM
187 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
188 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
189 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
190 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
191 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
192 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
193 #endif
194 #ifdef BSD_AUTH
195 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
196 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
197 #endif
198 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
199 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
200 #ifdef KRB5
201 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
202 #endif
203 #ifdef GSSAPI
204 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
205 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
206 {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
207 {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
208 #endif
209 {0, 0, NULL}
210 };
211
212 struct mon_table mon_dispatch_postauth20[] = {
213 #ifdef WITH_OPENSSL
214 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
215 #endif
216 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
217 {MONITOR_REQ_PTY, 0, mm_answer_pty},
218 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
219 {MONITOR_REQ_TERM, 0, mm_answer_term},
220 {0, 0, NULL}
221 };
222
223 struct mon_table *mon_dispatch;
224
225 /* Specifies if a certain message is allowed at the moment */
226
227 static void
228 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
229 {
230 while (ent->f != NULL) {
231 if (ent->type == type) {
232 ent->flags &= ~MON_PERMIT;
233 ent->flags |= permit ? MON_PERMIT : 0;
234 return;
235 }
236 ent++;
237 }
238 }
239
240 static void
241 monitor_permit_authentications(int permit)
242 {
243 struct mon_table *ent = mon_dispatch;
244
245 while (ent->f != NULL) {
246 if (ent->flags & MON_AUTH) {
247 ent->flags &= ~MON_PERMIT;
248 ent->flags |= permit ? MON_PERMIT : 0;
249 }
250 ent++;
251 }
252 }
253
254 void
255 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
256 {
257 struct ssh *ssh = active_state; /* XXX */
258 struct mon_table *ent;
259 int authenticated = 0, partial = 0;
260
261 debug3("preauth child monitor started");
262
263 close(pmonitor->m_recvfd);
264 close(pmonitor->m_log_sendfd);
265 pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
266
267 authctxt = _authctxt;
268 memset(authctxt, 0, sizeof(*authctxt));
269
270 mon_dispatch = mon_dispatch_proto20;
271 /* Permit requests for moduli and signatures */
272 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
273 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
274
275 /* The first few requests do not require asynchronous access */
276 while (!authenticated) {
277 partial = 0;
278 auth_method = "unknown";
279 auth_submethod = NULL;
280 auth2_authctxt_reset_info(authctxt);
281
282 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
283
284 /* Special handling for multiple required authentications */
285 if (options.num_auth_methods != 0) {
286 if (authenticated &&
287 !auth2_update_methods_lists(authctxt,
288 auth_method, auth_submethod)) {
289 debug3("%s: method %s: partial", __func__,
290 auth_method);
291 authenticated = 0;
292 partial = 1;
293 }
294 }
295
296 if (authenticated) {
297 if (!(ent->flags & MON_AUTHDECIDE))
298 fatal("%s: unexpected authentication from %d",
299 __func__, ent->type);
300 if (authctxt->pw->pw_uid == 0 &&
301 !auth_root_allowed(auth_method))
302 authenticated = 0;
303 #ifdef USE_PAM
304 /* PAM needs to perform account checks after auth */
305 if (options.use_pam && authenticated) {
306 Buffer m;
307
308 buffer_init(&m);
309 mm_request_receive_expect(pmonitor->m_sendfd,
310 MONITOR_REQ_PAM_ACCOUNT, &m);
311 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
312 buffer_free(&m);
313 }
314 #endif
315 }
316 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
317 auth_log(authctxt, authenticated, partial,
318 auth_method, auth_submethod);
319 if (!partial && !authenticated)
320 authctxt->failures++;
321 if (authenticated || partial) {
322 auth2_update_session_info(authctxt,
323 auth_method, auth_submethod);
324 }
325 }
326 }
327
328 if (!authctxt->valid)
329 fatal("%s: authenticated invalid user", __func__);
330 if (strcmp(auth_method, "unknown") == 0)
331 fatal("%s: authentication method name unknown", __func__);
332
333 debug("%s: %s has been authenticated by privileged process",
334 __func__, authctxt->user);
335 ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
336
337 mm_get_keystate(pmonitor);
338
339 /* Drain any buffered messages from the child */
340 while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0)
341 ;
342
343 close(pmonitor->m_sendfd);
344 close(pmonitor->m_log_recvfd);
345 pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
346 }
347
348 static void
349 monitor_set_child_handler(pid_t pid)
350 {
351 monitor_child_pid = pid;
352 }
353
354 static void
355 monitor_child_handler(int sig)
356 {
357 kill(monitor_child_pid, sig);
358 }
359
360 void
361 monitor_child_postauth(struct monitor *pmonitor)
362 {
363 close(pmonitor->m_recvfd);
364 pmonitor->m_recvfd = -1;
365
366 monitor_set_child_handler(pmonitor->m_pid);
367 signal(SIGHUP, &monitor_child_handler);
368 signal(SIGTERM, &monitor_child_handler);
369 signal(SIGINT, &monitor_child_handler);
370
371 mon_dispatch = mon_dispatch_postauth20;
372
373 /* Permit requests for moduli and signatures */
374 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
375 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
376 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
377
378 if (!no_pty_flag) {
379 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
380 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
381 }
382
383 for (;;)
384 monitor_read(pmonitor, mon_dispatch, NULL);
385 }
386
387 static int
388 monitor_read_log(struct monitor *pmonitor)
389 {
390 Buffer logmsg;
391 u_int len, level;
392 char *msg;
393
394 buffer_init(&logmsg);
395
396 /* Read length */
397 buffer_append_space(&logmsg, 4);
398 if (atomicio(read, pmonitor->m_log_recvfd,
399 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) {
400 if (errno == EPIPE) {
401 buffer_free(&logmsg);
402 debug("%s: child log fd closed", __func__);
403 close(pmonitor->m_log_recvfd);
404 pmonitor->m_log_recvfd = -1;
405 return -1;
406 }
407 fatal("%s: log fd read: %s", __func__, strerror(errno));
408 }
409 len = buffer_get_int(&logmsg);
410 if (len <= 4 || len > 8192)
411 fatal("%s: invalid log message length %u", __func__, len);
412
413 /* Read severity, message */
414 buffer_clear(&logmsg);
415 buffer_append_space(&logmsg, len);
416 if (atomicio(read, pmonitor->m_log_recvfd,
417 buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg))
418 fatal("%s: log fd read: %s", __func__, strerror(errno));
419
420 /* Log it */
421 level = buffer_get_int(&logmsg);
422 msg = buffer_get_string(&logmsg, NULL);
423 if (log_level_name(level) == NULL)
424 fatal("%s: invalid log level %u (corrupted message?)",
425 __func__, level);
426 do_log2(level, "%s [preauth]", msg);
427
428 buffer_free(&logmsg);
429 free(msg);
430
431 return 0;
432 }
433
434 int
435 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
436 struct mon_table **pent)
437 {
438 Buffer m;
439 int ret;
440 u_char type;
441 struct pollfd pfd[2];
442
443 for (;;) {
444 memset(&pfd, 0, sizeof(pfd));
445 pfd[0].fd = pmonitor->m_sendfd;
446 pfd[0].events = POLLIN;
447 pfd[1].fd = pmonitor->m_log_recvfd;
448 pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
449 if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
450 if (errno == EINTR || errno == EAGAIN)
451 continue;
452 fatal("%s: poll: %s", __func__, strerror(errno));
453 }
454 if (pfd[1].revents) {
455 /*
456 * Drain all log messages before processing next
457 * monitor request.
458 */
459 monitor_read_log(pmonitor);
460 continue;
461 }
462 if (pfd[0].revents)
463 break; /* Continues below */
464 }
465
466 buffer_init(&m);
467
468 mm_request_receive(pmonitor->m_sendfd, &m);
469 type = buffer_get_char(&m);
470
471 debug3("%s: checking request %d", __func__, type);
472
473 while (ent->f != NULL) {
474 if (ent->type == type)
475 break;
476 ent++;
477 }
478
479 if (ent->f != NULL) {
480 if (!(ent->flags & MON_PERMIT))
481 fatal("%s: unpermitted request %d", __func__,
482 type);
483 ret = (*ent->f)(pmonitor->m_sendfd, &m);
484 buffer_free(&m);
485
486 /* The child may use this request only once, disable it */
487 if (ent->flags & MON_ONCE) {
488 debug2("%s: %d used once, disabling now", __func__,
489 type);
490 ent->flags &= ~MON_PERMIT;
491 }
492
493 if (pent != NULL)
494 *pent = ent;
495
496 return ret;
497 }
498
499 fatal("%s: unsupported request: %d", __func__, type);
500
501 /* NOTREACHED */
502 return (-1);
503 }
504
505 /* allowed key state */
506 static int
507 monitor_allowed_key(u_char *blob, u_int bloblen)
508 {
509 /* make sure key is allowed */
510 if (key_blob == NULL || key_bloblen != bloblen ||
511 timingsafe_bcmp(key_blob, blob, key_bloblen))
512 return (0);
513 return (1);
514 }
515
516 static void
517 monitor_reset_key_state(void)
518 {
519 /* reset state */
520 free(key_blob);
521 free(hostbased_cuser);
522 free(hostbased_chost);
523 key_blob = NULL;
524 key_bloblen = 0;
525 key_blobtype = MM_NOKEY;
526 hostbased_cuser = NULL;
527 hostbased_chost = NULL;
528 }
529
530 #ifdef WITH_OPENSSL
531 int
532 mm_answer_moduli(int sock, Buffer *m)
533 {
534 DH *dh;
535 int min, want, max;
536
537 min = buffer_get_int(m);
538 want = buffer_get_int(m);
539 max = buffer_get_int(m);
540
541 debug3("%s: got parameters: %d %d %d",
542 __func__, min, want, max);
543 /* We need to check here, too, in case the child got corrupted */
544 if (max < min || want < min || max < want)
545 fatal("%s: bad parameters: %d %d %d",
546 __func__, min, want, max);
547
548 buffer_clear(m);
549
550 dh = choose_dh(min, want, max);
551 if (dh == NULL) {
552 buffer_put_char(m, 0);
553 return (0);
554 } else {
555 /* Send first bignum */
556 buffer_put_char(m, 1);
557 buffer_put_bignum2(m, dh->p);
558 buffer_put_bignum2(m, dh->g);
559
560 DH_free(dh);
561 }
562 mm_request_send(sock, MONITOR_ANS_MODULI, m);
563 return (0);
564 }
565 #endif
566
567 int
568 mm_answer_sign(int sock, Buffer *m)
569 {
570 struct ssh *ssh = active_state; /* XXX */
571 extern int auth_sock; /* XXX move to state struct? */
572 struct sshkey *key;
573 struct sshbuf *sigbuf = NULL;
574 u_char *p = NULL, *signature = NULL;
575 char *alg = NULL;
576 size_t datlen, siglen, alglen;
577 int r, is_proof = 0;
578 u_int keyid;
579 const char proof_req[] = "hostkeys-prove-00 (at) openssh.com";
580
581 debug3("%s", __func__);
582
583 if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
584 (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
585 (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
586 fatal("%s: buffer error: %s", __func__, ssh_err(r));
587 if (keyid > INT_MAX)
588 fatal("%s: invalid key ID", __func__);
589
590 /*
591 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
592 * SHA384 (48 bytes) and SHA512 (64 bytes).
593 *
594 * Otherwise, verify the signature request is for a hostkey
595 * proof.
596 *
597 * XXX perform similar check for KEX signature requests too?
598 * it's not trivial, since what is signed is the hash, rather
599 * than the full kex structure...
600 */
601 if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
602 /*
603 * Construct expected hostkey proof and compare it to what
604 * the client sent us.
605 */
606 if (session_id2_len == 0) /* hostkeys is never first */
607 fatal("%s: bad data length: %zu", __func__, datlen);
608 if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
609 fatal("%s: no hostkey for index %d", __func__, keyid);
610 if ((sigbuf = sshbuf_new()) == NULL)
611 fatal("%s: sshbuf_new", __func__);
612 if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
613 (r = sshbuf_put_string(sigbuf, session_id2,
614 session_id2_len)) != 0 ||
615 (r = sshkey_puts(key, sigbuf)) != 0)
616 fatal("%s: couldn't prepare private key "
617 "proof buffer: %s", __func__, ssh_err(r));
618 if (datlen != sshbuf_len(sigbuf) ||
619 memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
620 fatal("%s: bad data length: %zu, hostkey proof len %zu",
621 __func__, datlen, sshbuf_len(sigbuf));
622 sshbuf_free(sigbuf);
623 is_proof = 1;
624 }
625
626 /* save session id, it will be passed on the first call */
627 if (session_id2_len == 0) {
628 session_id2_len = datlen;
629 session_id2 = xmalloc(session_id2_len);
630 memcpy(session_id2, p, session_id2_len);
631 }
632
633 if ((key = get_hostkey_by_index(keyid)) != NULL) {
634 if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
635 datafellows)) != 0)
636 fatal("%s: sshkey_sign failed: %s",
637 __func__, ssh_err(r));
638 } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
639 auth_sock > 0) {
640 if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
641 p, datlen, alg, datafellows)) != 0) {
642 fatal("%s: ssh_agent_sign failed: %s",
643 __func__, ssh_err(r));
644 }
645 } else
646 fatal("%s: no hostkey from index %d", __func__, keyid);
647
648 debug3("%s: %s signature %p(%zu)", __func__,
649 is_proof ? "KEX" : "hostkey proof", signature, siglen);
650
651 sshbuf_reset(m);
652 if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
653 fatal("%s: buffer error: %s", __func__, ssh_err(r));
654
655 free(alg);
656 free(p);
657 free(signature);
658
659 mm_request_send(sock, MONITOR_ANS_SIGN, m);
660
661 /* Turn on permissions for getpwnam */
662 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
663
664 return (0);
665 }
666
667 /* Retrieves the password entry and also checks if the user is permitted */
668
669 int
670 mm_answer_pwnamallow(int sock, Buffer *m)
671 {
672 struct ssh *ssh = active_state; /* XXX */
673 char *username;
674 struct passwd *pwent;
675 int allowed = 0;
676 u_int i;
677
678 debug3("%s", __func__);
679
680 if (authctxt->attempt++ != 0)
681 fatal("%s: multiple attempts for getpwnam", __func__);
682
683 username = buffer_get_string(m, NULL);
684
685 pwent = getpwnamallow(username);
686
687 authctxt->user = xstrdup(username);
688 setproctitle("%s [priv]", pwent ? username : "unknown");
689 free(username);
690
691 buffer_clear(m);
692
693 if (pwent == NULL) {
694 buffer_put_char(m, 0);
695 authctxt->pw = fakepw();
696 goto out;
697 }
698
699 allowed = 1;
700 authctxt->pw = pwent;
701 authctxt->valid = 1;
702
703 buffer_put_char(m, 1);
704 buffer_put_string(m, pwent, sizeof(struct passwd));
705 buffer_put_cstring(m, pwent->pw_name);
706 buffer_put_cstring(m, "*");
707 buffer_put_cstring(m, pwent->pw_gecos);
708 buffer_put_cstring(m, pwent->pw_class);
709 buffer_put_cstring(m, pwent->pw_dir);
710 buffer_put_cstring(m, pwent->pw_shell);
711
712 out:
713 ssh_packet_set_log_preamble(ssh, "%suser %s",
714 authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
715 buffer_put_string(m, &options, sizeof(options));
716
717 #define M_CP_STROPT(x) do { \
718 if (options.x != NULL) \
719 buffer_put_cstring(m, options.x); \
720 } while (0)
721 #define M_CP_STRARRAYOPT(x, nx) do { \
722 for (i = 0; i < options.nx; i++) \
723 buffer_put_cstring(m, options.x[i]); \
724 } while (0)
725 #define M_CP_STRARRAYOPT_ALLOC(x, nx) M_CP_STRARRAYOPT(x, nx)
726 /* See comment in servconf.h */
727 COPY_MATCH_STRING_OPTS();
728 #undef M_CP_STROPT
729 #undef M_CP_STRARRAYOPT
730 #undef M_CP_STRARRAYOPT_ALLOC
731
732 /* Create valid auth method lists */
733 if (auth2_setup_methods_lists(authctxt) != 0) {
734 /*
735 * The monitor will continue long enough to let the child
736 * run to it's packet_disconnect(), but it must not allow any
737 * authentication to succeed.
738 */
739 debug("%s: no valid authentication method lists", __func__);
740 }
741
742 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
743 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
744
745 /* Allow service/style information on the auth context */
746 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
747 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
748
749 #ifdef USE_PAM
750 if (options.use_pam)
751 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
752 #endif
753
754 return (0);
755 }
756
757 int mm_answer_auth2_read_banner(int sock, Buffer *m)
758 {
759 char *banner;
760
761 buffer_clear(m);
762 banner = auth2_read_banner();
763 buffer_put_cstring(m, banner != NULL ? banner : "");
764 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
765 free(banner);
766
767 return (0);
768 }
769
770 int
771 mm_answer_authserv(int sock, Buffer *m)
772 {
773 monitor_permit_authentications(1);
774
775 authctxt->service = buffer_get_string(m, NULL);
776 authctxt->style = buffer_get_string(m, NULL);
777 debug3("%s: service=%s, style=%s",
778 __func__, authctxt->service, authctxt->style);
779
780 if (strlen(authctxt->style) == 0) {
781 free(authctxt->style);
782 authctxt->style = NULL;
783 }
784
785 return (0);
786 }
787
788 int
789 mm_answer_authpassword(int sock, Buffer *m)
790 {
791 static int call_count;
792 char *passwd;
793 int authenticated;
794 u_int plen;
795
796 if (!options.password_authentication)
797 fatal("%s: password authentication not enabled", __func__);
798 passwd = buffer_get_string(m, &plen);
799 /* Only authenticate if the context is valid */
800 authenticated = options.password_authentication &&
801 auth_password(authctxt, passwd);
802 explicit_bzero(passwd, strlen(passwd));
803 free(passwd);
804
805 buffer_clear(m);
806 buffer_put_int(m, authenticated);
807
808 debug3("%s: sending result %d", __func__, authenticated);
809 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
810
811 call_count++;
812 if (plen == 0 && call_count == 1)
813 auth_method = "none";
814 else
815 auth_method = "password";
816
817 /* Causes monitor loop to terminate if authenticated */
818 return (authenticated);
819 }
820
821 #ifdef BSD_AUTH
822 int
823 mm_answer_bsdauthquery(int sock, Buffer *m)
824 {
825 char *name, *infotxt;
826 u_int numprompts;
827 u_int *echo_on;
828 char **prompts;
829 u_int success;
830
831 if (!options.kbd_interactive_authentication)
832 fatal("%s: kbd-int authentication not enabled", __func__);
833 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
834 &prompts, &echo_on) < 0 ? 0 : 1;
835
836 buffer_clear(m);
837 buffer_put_int(m, success);
838 if (success)
839 buffer_put_cstring(m, prompts[0]);
840
841 debug3("%s: sending challenge success: %u", __func__, success);
842 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
843
844 if (success) {
845 free(name);
846 free(infotxt);
847 free(prompts);
848 free(echo_on);
849 }
850
851 return (0);
852 }
853
854 int
855 mm_answer_bsdauthrespond(int sock, Buffer *m)
856 {
857 char *response;
858 int authok;
859
860 if (!options.kbd_interactive_authentication)
861 fatal("%s: kbd-int authentication not enabled", __func__);
862 if (authctxt->as == NULL)
863 fatal("%s: no bsd auth session", __func__);
864
865 response = buffer_get_string(m, NULL);
866 authok = options.challenge_response_authentication &&
867 auth_userresponse(authctxt->as, response, 0);
868 authctxt->as = NULL;
869 debug3("%s: <%s> = <%d>", __func__, response, authok);
870 free(response);
871
872 buffer_clear(m);
873 buffer_put_int(m, authok);
874
875 debug3("%s: sending authenticated: %d", __func__, authok);
876 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
877
878 auth_method = "keyboard-interactive";
879 auth_submethod = "bsdauth";
880
881 return (authok != 0);
882 }
883 #endif
884
885 #ifdef SKEY
886 int
887 mm_answer_skeyquery(int sock, Buffer *m)
888 {
889 struct skey skey;
890 char challenge[1024];
891 u_int success;
892
893 success = skeychallenge(&skey, authctxt->user, challenge,
894 sizeof(challenge)) < 0 ? 0 : 1;
895
896 buffer_clear(m);
897 buffer_put_int(m, success);
898 if (success)
899 buffer_put_cstring(m, challenge);
900
901 debug3("%s: sending challenge success: %u", __func__, success);
902 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
903
904 return (0);
905 }
906
907 int
908 mm_answer_skeyrespond(int sock, Buffer *m)
909 {
910 char *response;
911 int authok;
912
913 response = buffer_get_string(m, NULL);
914
915 authok = (options.challenge_response_authentication &&
916 authctxt->valid &&
917 skey_haskey(authctxt->pw->pw_name) == 0 &&
918 skey_passcheck(authctxt->pw->pw_name, response) != -1);
919
920 free(response);
921
922 buffer_clear(m);
923 buffer_put_int(m, authok);
924
925 debug3("%s: sending authenticated: %d", __func__, authok);
926 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
927
928 auth_method = "skey";
929 auth_submethod = "bsdauth";
930
931 return (authok != 0);
932 }
933 #endif
934
935 #ifdef USE_PAM
936 int
937 mm_answer_pam_start(int sock, Buffer *m)
938 {
939 if (!options.use_pam)
940 fatal("UsePAM not set, but ended up in %s anyway", __func__);
941
942 start_pam(authctxt);
943
944 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
945
946 return (0);
947 }
948
949 int
950 mm_answer_pam_account(int sock, Buffer *m)
951 {
952 u_int ret;
953
954 if (!options.use_pam)
955 fatal("UsePAM not set, but ended up in %s anyway", __func__);
956
957 ret = do_pam_account();
958
959 buffer_put_int(m, ret);
960 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
961
962 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
963
964 return (ret);
965 }
966
967 static void *sshpam_ctxt, *sshpam_authok;
968 extern KbdintDevice sshpam_device;
969
970 int
971 mm_answer_pam_init_ctx(int sock, Buffer *m)
972 {
973 debug3("%s", __func__);
974 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
975 sshpam_authok = NULL;
976 buffer_clear(m);
977 if (sshpam_ctxt != NULL) {
978 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
979 buffer_put_int(m, 1);
980 } else {
981 buffer_put_int(m, 0);
982 }
983 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
984 return (0);
985 }
986
987 int
988 mm_answer_pam_query(int sock, Buffer *m)
989 {
990 char *name, *info, **prompts;
991 u_int i, num, *echo_on;
992 int ret;
993
994 debug3("%s", __func__);
995 sshpam_authok = NULL;
996 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
997 if (ret == 0 && num == 0)
998 sshpam_authok = sshpam_ctxt;
999 if (num > 1 || name == NULL || info == NULL)
1000 ret = -1;
1001 buffer_clear(m);
1002 buffer_put_int(m, ret);
1003 buffer_put_cstring(m, name);
1004 free(name);
1005 buffer_put_cstring(m, info);
1006 free(info);
1007 buffer_put_int(m, num);
1008 for (i = 0; i < num; ++i) {
1009 buffer_put_cstring(m, prompts[i]);
1010 free(prompts[i]);
1011 buffer_put_int(m, echo_on[i]);
1012 }
1013 if (prompts != NULL)
1014 free(prompts);
1015 if (echo_on != NULL)
1016 free(echo_on);
1017 auth_method = "keyboard-interactive/pam";
1018 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
1019 return (0);
1020 }
1021
1022 int
1023 mm_answer_pam_respond(int sock, Buffer *m)
1024 {
1025 char **resp;
1026 u_int i, num;
1027 int ret;
1028
1029 debug3("%s", __func__);
1030 sshpam_authok = NULL;
1031 num = buffer_get_int(m);
1032 if (num > 0) {
1033 resp = xmalloc(num * sizeof(char *));
1034 for (i = 0; i < num; ++i)
1035 resp[i] = buffer_get_string(m, NULL);
1036 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
1037 for (i = 0; i < num; ++i)
1038 free(resp[i]);
1039 free(resp);
1040 } else {
1041 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
1042 }
1043 buffer_clear(m);
1044 buffer_put_int(m, ret);
1045 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
1046 auth_method = "keyboard-interactive/pam";
1047 if (ret == 0)
1048 sshpam_authok = sshpam_ctxt;
1049 return (0);
1050 }
1051
1052 int
1053 mm_answer_pam_free_ctx(int sock, Buffer *m)
1054 {
1055 int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
1056
1057 debug3("%s", __func__);
1058 (sshpam_device.free_ctx)(sshpam_ctxt);
1059 sshpam_ctxt = sshpam_authok = NULL;
1060 buffer_clear(m);
1061 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1062 auth_method = "keyboard-interactive/pam";
1063 return r;
1064 }
1065 #endif
1066
1067 int
1068 mm_answer_keyallowed(int sock, Buffer *m)
1069 {
1070 struct sshkey *key;
1071 char *cuser, *chost;
1072 u_char *blob;
1073 u_int bloblen, pubkey_auth_attempt;
1074 enum mm_keytype type = 0;
1075 int allowed = 0;
1076
1077 debug3("%s entering", __func__);
1078
1079 type = buffer_get_int(m);
1080 cuser = buffer_get_string(m, NULL);
1081 chost = buffer_get_string(m, NULL);
1082 blob = buffer_get_string(m, &bloblen);
1083 pubkey_auth_attempt = buffer_get_int(m);
1084
1085 key = key_from_blob(blob, bloblen);
1086
1087 debug3("%s: key_from_blob: %p", __func__, key);
1088
1089 if (key != NULL && authctxt->valid) {
1090 /* These should not make it past the privsep child */
1091 if (key_type_plain(key->type) == KEY_RSA &&
1092 (datafellows & SSH_BUG_RSASIGMD5) != 0)
1093 fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
1094
1095 switch (type) {
1096 case MM_USERKEY:
1097 allowed = options.pubkey_authentication &&
1098 !auth2_key_already_used(authctxt, key) &&
1099 match_pattern_list(sshkey_ssh_name(key),
1100 options.pubkey_key_types, 0) == 1 &&
1101 user_key_allowed(authctxt->pw, key,
1102 pubkey_auth_attempt);
1103 auth_method = "publickey";
1104 if (options.pubkey_authentication &&
1105 (!pubkey_auth_attempt || allowed != 1))
1106 auth_clear_options();
1107 break;
1108 case MM_HOSTKEY:
1109 allowed = options.hostbased_authentication &&
1110 !auth2_key_already_used(authctxt, key) &&
1111 match_pattern_list(sshkey_ssh_name(key),
1112 options.hostbased_key_types, 0) == 1 &&
1113 hostbased_key_allowed(authctxt->pw,
1114 cuser, chost, key);
1115 auth2_record_info(authctxt,
1116 "client user \"%.100s\", client host \"%.100s\"",
1117 cuser, chost);
1118 auth_method = "hostbased";
1119 break;
1120 default:
1121 fatal("%s: unknown key type %d", __func__, type);
1122 break;
1123 }
1124 }
1125
1126 debug3("%s: key is %s", __func__, allowed ? "allowed" : "not allowed");
1127
1128 auth2_record_key(authctxt, 0, key);
1129 sshkey_free(key);
1130
1131 /* clear temporarily storage (used by verify) */
1132 monitor_reset_key_state();
1133
1134 if (allowed) {
1135 /* Save temporarily for comparison in verify */
1136 key_blob = blob;
1137 key_bloblen = bloblen;
1138 key_blobtype = type;
1139 hostbased_cuser = cuser;
1140 hostbased_chost = chost;
1141 } else {
1142 /* Log failed attempt */
1143 auth_log(authctxt, 0, 0, auth_method, NULL);
1144 free(blob);
1145 free(cuser);
1146 free(chost);
1147 }
1148
1149 buffer_clear(m);
1150 buffer_put_int(m, allowed);
1151 buffer_put_int(m, forced_command != NULL);
1152
1153 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1154
1155 return (0);
1156 }
1157
1158 static int
1159 monitor_valid_userblob(u_char *data, u_int datalen)
1160 {
1161 Buffer b;
1162 u_char *p;
1163 char *userstyle, *cp;
1164 u_int len;
1165 int fail = 0;
1166
1167 buffer_init(&b);
1168 buffer_append(&b, data, datalen);
1169
1170 if (datafellows & SSH_OLD_SESSIONID) {
1171 p = buffer_ptr(&b);
1172 len = buffer_len(&b);
1173 if ((session_id2 == NULL) ||
1174 (len < session_id2_len) ||
1175 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1176 fail++;
1177 buffer_consume(&b, session_id2_len);
1178 } else {
1179 p = buffer_get_string(&b, &len);
1180 if ((session_id2 == NULL) ||
1181 (len != session_id2_len) ||
1182 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1183 fail++;
1184 free(p);
1185 }
1186 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1187 fail++;
1188 cp = buffer_get_cstring(&b, NULL);
1189 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1190 authctxt->style ? ":" : "",
1191 authctxt->style ? authctxt->style : "");
1192 if (strcmp(userstyle, cp) != 0) {
1193 logit("wrong user name passed to monitor: "
1194 "expected %s != %.100s", userstyle, cp);
1195 fail++;
1196 }
1197 free(userstyle);
1198 free(cp);
1199 buffer_skip_string(&b);
1200 if (datafellows & SSH_BUG_PKAUTH) {
1201 if (!buffer_get_char(&b))
1202 fail++;
1203 } else {
1204 cp = buffer_get_cstring(&b, NULL);
1205 if (strcmp("publickey", cp) != 0)
1206 fail++;
1207 free(cp);
1208 if (!buffer_get_char(&b))
1209 fail++;
1210 buffer_skip_string(&b);
1211 }
1212 buffer_skip_string(&b);
1213 if (buffer_len(&b) != 0)
1214 fail++;
1215 buffer_free(&b);
1216 return (fail == 0);
1217 }
1218
1219 static int
1220 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1221 char *chost)
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 p = buffer_get_string(&b, &len);
1232 if ((session_id2 == NULL) ||
1233 (len != session_id2_len) ||
1234 (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
1235 fail++;
1236 free(p);
1237
1238 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1239 fail++;
1240 p = buffer_get_cstring(&b, NULL);
1241 xasprintf(&userstyle, "%s%s%s", authctxt->user,
1242 authctxt->style ? ":" : "",
1243 authctxt->style ? authctxt->style : "");
1244 if (strcmp(userstyle, p) != 0) {
1245 logit("wrong user name passed to monitor: expected %s != %.100s",
1246 userstyle, p);
1247 fail++;
1248 }
1249 free(userstyle);
1250 free(p);
1251 buffer_skip_string(&b); /* service */
1252 p = buffer_get_cstring(&b, NULL);
1253 if (strcmp(p, "hostbased") != 0)
1254 fail++;
1255 free(p);
1256 buffer_skip_string(&b); /* pkalg */
1257 buffer_skip_string(&b); /* pkblob */
1258
1259 /* verify client host, strip trailing dot if necessary */
1260 p = buffer_get_string(&b, NULL);
1261 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1262 p[len - 1] = '\0';
1263 if (strcmp(p, chost) != 0)
1264 fail++;
1265 free(p);
1266
1267 /* verify client user */
1268 p = buffer_get_string(&b, NULL);
1269 if (strcmp(p, cuser) != 0)
1270 fail++;
1271 free(p);
1272
1273 if (buffer_len(&b) != 0)
1274 fail++;
1275 buffer_free(&b);
1276 return (fail == 0);
1277 }
1278
1279 int
1280 mm_answer_keyverify(int sock, struct sshbuf *m)
1281 {
1282 struct sshkey *key;
1283 u_char *signature, *data, *blob;
1284 size_t signaturelen, datalen, bloblen;
1285 int r, ret, valid_data = 0, encoded_ret;
1286
1287 if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
1288 (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
1289 (r = sshbuf_get_string(m, &data, &datalen)) != 0)
1290 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1291
1292 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1293 !monitor_allowed_key(blob, bloblen))
1294 fatal("%s: bad key, not previously allowed", __func__);
1295
1296 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1297 if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
1298 fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
1299
1300 switch (key_blobtype) {
1301 case MM_USERKEY:
1302 valid_data = monitor_valid_userblob(data, datalen);
1303 auth_method = "publickey";
1304 break;
1305 case MM_HOSTKEY:
1306 valid_data = monitor_valid_hostbasedblob(data, datalen,
1307 hostbased_cuser, hostbased_chost);
1308 auth_method = "hostbased";
1309 break;
1310 default:
1311 valid_data = 0;
1312 break;
1313 }
1314 if (!valid_data)
1315 fatal("%s: bad signature data blob", __func__);
1316
1317 ret = sshkey_verify(key, signature, signaturelen, data, datalen,
1318 active_state->compat);
1319 debug3("%s: %s %p signature %s", __func__, auth_method, key,
1320 (ret == 0) ? "verified" : "unverified");
1321 auth2_record_key(authctxt, ret == 0, key);
1322
1323 free(blob);
1324 free(signature);
1325 free(data);
1326
1327 monitor_reset_key_state();
1328
1329 sshkey_free(key);
1330 sshbuf_reset(m);
1331
1332 /* encode ret != 0 as positive integer, since we're sending u32 */
1333 encoded_ret = (ret != 0);
1334 if ((r = sshbuf_put_u32(m, encoded_ret)) != 0)
1335 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1336 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1337
1338 return ret == 0;
1339 }
1340
1341 static void
1342 mm_record_login(Session *s, struct passwd *pw)
1343 {
1344 struct ssh *ssh = active_state; /* XXX */
1345 socklen_t fromlen;
1346 struct sockaddr_storage from;
1347
1348 /*
1349 * Get IP address of client. If the connection is not a socket, let
1350 * the address be 0.0.0.0.
1351 */
1352 memset(&from, 0, sizeof(from));
1353 fromlen = sizeof(from);
1354 if (packet_connection_is_on_socket()) {
1355 if (getpeername(packet_get_connection_in(),
1356 (struct sockaddr *)&from, &fromlen) < 0) {
1357 debug("getpeername: %.100s", strerror(errno));
1358 cleanup_exit(255);
1359 }
1360 }
1361 /* Record that there was a login on that tty from the remote host. */
1362 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1363 session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
1364 (struct sockaddr *)&from, fromlen);
1365 }
1366
1367 static void
1368 mm_session_close(Session *s)
1369 {
1370 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1371 if (s->ttyfd != -1) {
1372 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1373 session_pty_cleanup2(s);
1374 }
1375 session_unused(s->self);
1376 }
1377
1378 int
1379 mm_answer_pty(int sock, Buffer *m)
1380 {
1381 extern struct monitor *pmonitor;
1382 Session *s;
1383 int res, fd0;
1384
1385 debug3("%s entering", __func__);
1386
1387 buffer_clear(m);
1388 s = session_new();
1389 if (s == NULL)
1390 goto error;
1391 s->authctxt = authctxt;
1392 s->pw = authctxt->pw;
1393 s->pid = pmonitor->m_pid;
1394 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1395 if (res == 0)
1396 goto error;
1397 pty_setowner(authctxt->pw, s->tty);
1398
1399 buffer_put_int(m, 1);
1400 buffer_put_cstring(m, s->tty);
1401
1402 /* We need to trick ttyslot */
1403 if (dup2(s->ttyfd, 0) == -1)
1404 fatal("%s: dup2", __func__);
1405
1406 mm_record_login(s, authctxt->pw);
1407
1408 /* Now we can close the file descriptor again */
1409 close(0);
1410
1411 /* send messages generated by record_login */
1412 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1413 buffer_clear(&loginmsg);
1414
1415 mm_request_send(sock, MONITOR_ANS_PTY, m);
1416
1417 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1418 mm_send_fd(sock, s->ttyfd) == -1)
1419 fatal("%s: send fds failed", __func__);
1420
1421 /* make sure nothing uses fd 0 */
1422 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1423 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1424 if (fd0 != 0)
1425 error("%s: fd0 %d != 0", __func__, fd0);
1426
1427 /* slave is not needed */
1428 close(s->ttyfd);
1429 s->ttyfd = s->ptyfd;
1430 /* no need to dup() because nobody closes ptyfd */
1431 s->ptymaster = s->ptyfd;
1432
1433 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1434
1435 return (0);
1436
1437 error:
1438 if (s != NULL)
1439 mm_session_close(s);
1440 buffer_put_int(m, 0);
1441 mm_request_send(sock, MONITOR_ANS_PTY, m);
1442 return (0);
1443 }
1444
1445 int
1446 mm_answer_pty_cleanup(int sock, Buffer *m)
1447 {
1448 Session *s;
1449 char *tty;
1450
1451 debug3("%s entering", __func__);
1452
1453 tty = buffer_get_string(m, NULL);
1454 if ((s = session_by_tty(tty)) != NULL)
1455 mm_session_close(s);
1456 buffer_clear(m);
1457 free(tty);
1458 return (0);
1459 }
1460
1461 #ifdef KRB5
1462 int
1463 mm_answer_krb5(int xsocket, Buffer *m)
1464 {
1465 krb5_data tkt, reply;
1466 char *client_user;
1467 u_int len;
1468 int success;
1469
1470 /* use temporary var to avoid size issues on 64bit arch */
1471 tkt.data = buffer_get_string(m, &len);
1472 tkt.length = len;
1473
1474 success = options.kerberos_authentication &&
1475 authctxt->valid &&
1476 auth_krb5(authctxt, &tkt, &client_user, &reply);
1477
1478 if (tkt.length)
1479 free(tkt.data);
1480
1481 buffer_clear(m);
1482 buffer_put_int(m, success);
1483
1484 if (success) {
1485 buffer_put_cstring(m, client_user);
1486 buffer_put_string(m, reply.data, reply.length);
1487 if (client_user)
1488 free(client_user);
1489 if (reply.length)
1490 free(reply.data);
1491 }
1492 mm_request_send(xsocket, MONITOR_ANS_KRB5, m);
1493
1494 auth_method = "kerberos";
1495
1496 return success;
1497 }
1498 #endif
1499
1500 int
1501 mm_answer_term(int sock, Buffer *req)
1502 {
1503 struct ssh *ssh = active_state; /* XXX */
1504 extern struct monitor *pmonitor;
1505 int res, status;
1506
1507 debug3("%s: tearing down sessions", __func__);
1508
1509 /* The child is terminating */
1510 session_destroy_all(ssh, &mm_session_close);
1511
1512 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1513 if (errno != EINTR)
1514 exit(1);
1515
1516 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1517
1518 /* Terminate process */
1519 exit(res);
1520 }
1521
1522 void
1523 monitor_clear_keystate(struct monitor *pmonitor)
1524 {
1525 struct ssh *ssh = active_state; /* XXX */
1526
1527 ssh_clear_newkeys(ssh, MODE_IN);
1528 ssh_clear_newkeys(ssh, MODE_OUT);
1529 sshbuf_free(child_state);
1530 child_state = NULL;
1531 }
1532
1533 void
1534 monitor_apply_keystate(struct monitor *pmonitor)
1535 {
1536 struct ssh *ssh = active_state; /* XXX */
1537 struct kex *kex;
1538 int r;
1539
1540 debug3("%s: packet_set_state", __func__);
1541 if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
1542 fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
1543 sshbuf_free(child_state);
1544 child_state = NULL;
1545
1546 if ((kex = ssh->kex) != NULL) {
1547 /* XXX set callbacks */
1548 #ifdef WITH_OPENSSL
1549 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1550 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1551 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
1552 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
1553 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
1554 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1555 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1556 kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
1557 #endif
1558 kex->kex[KEX_C25519_SHA256] = kexc25519_server;
1559 kex->load_host_public_key=&get_hostkey_public_by_type;
1560 kex->load_host_private_key=&get_hostkey_private_by_type;
1561 kex->host_key_index=&get_hostkey_index;
1562 kex->sign = sshd_hostkey_sign;
1563 }
1564 }
1565
1566 /* This function requries careful sanity checking */
1567
1568 void
1569 mm_get_keystate(struct monitor *pmonitor)
1570 {
1571 debug3("%s: Waiting for new keys", __func__);
1572
1573 if ((child_state = sshbuf_new()) == NULL)
1574 fatal("%s: sshbuf_new failed", __func__);
1575 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
1576 child_state);
1577 debug3("%s: GOT new keys", __func__);
1578 }
1579
1580
1581 /* XXX */
1582
1583 #define FD_CLOSEONEXEC(x) do { \
1584 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1585 fatal("fcntl(%d, F_SETFD)", x); \
1586 } while (0)
1587
1588 static void
1589 monitor_openfds(struct monitor *mon, int do_logfds)
1590 {
1591 int pair[2];
1592 #ifdef SO_ZEROIZE
1593 int on = 1;
1594 #endif
1595
1596 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1597 fatal("%s: socketpair: %s", __func__, strerror(errno));
1598 #ifdef SO_ZEROIZE
1599 if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1600 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
1601 if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) < 0)
1602 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
1603 #endif
1604 FD_CLOSEONEXEC(pair[0]);
1605 FD_CLOSEONEXEC(pair[1]);
1606 mon->m_recvfd = pair[0];
1607 mon->m_sendfd = pair[1];
1608
1609 if (do_logfds) {
1610 if (pipe(pair) == -1)
1611 fatal("%s: pipe: %s", __func__, strerror(errno));
1612 FD_CLOSEONEXEC(pair[0]);
1613 FD_CLOSEONEXEC(pair[1]);
1614 mon->m_log_recvfd = pair[0];
1615 mon->m_log_sendfd = pair[1];
1616 } else
1617 mon->m_log_recvfd = mon->m_log_sendfd = -1;
1618 }
1619
1620 #define MM_MEMSIZE 65536
1621
1622 struct monitor *
1623 monitor_init(void)
1624 {
1625 struct monitor *mon;
1626
1627 mon = xcalloc(1, sizeof(*mon));
1628 monitor_openfds(mon, 1);
1629
1630 return mon;
1631 }
1632
1633 void
1634 monitor_reinit(struct monitor *mon)
1635 {
1636 monitor_openfds(mon, 0);
1637 }
1638
1639 #ifdef GSSAPI
1640 int
1641 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1642 {
1643 gss_OID_desc goid;
1644 OM_uint32 major;
1645 u_int len;
1646
1647 if (!options.gss_authentication)
1648 fatal("%s: GSSAPI authentication not enabled", __func__);
1649
1650 goid.elements = buffer_get_string(m, &len);
1651 goid.length = len;
1652
1653 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1654
1655 free(goid.elements);
1656
1657 buffer_clear(m);
1658 buffer_put_int(m, major);
1659
1660 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1661
1662 /* Now we have a context, enable the step */
1663 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1664
1665 return (0);
1666 }
1667
1668 int
1669 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1670 {
1671 gss_buffer_desc in;
1672 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1673 OM_uint32 major, minor;
1674 OM_uint32 flags = 0; /* GSI needs this */
1675 u_int len;
1676
1677 if (!options.gss_authentication)
1678 fatal("%s: GSSAPI authentication not enabled", __func__);
1679
1680 in.value = buffer_get_string(m, &len);
1681 in.length = len;
1682 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1683 free(in.value);
1684
1685 buffer_clear(m);
1686 buffer_put_int(m, major);
1687 buffer_put_string(m, out.value, out.length);
1688 buffer_put_int(m, flags);
1689 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1690
1691 gss_release_buffer(&minor, &out);
1692
1693 if (major == GSS_S_COMPLETE) {
1694 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1695 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1696 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1697 }
1698 return (0);
1699 }
1700
1701 int
1702 mm_answer_gss_checkmic(int sock, Buffer *m)
1703 {
1704 gss_buffer_desc gssbuf, mic;
1705 OM_uint32 ret;
1706 u_int len;
1707
1708 if (!options.gss_authentication)
1709 fatal("%s: GSSAPI authentication not enabled", __func__);
1710
1711 gssbuf.value = buffer_get_string(m, &len);
1712 gssbuf.length = len;
1713 mic.value = buffer_get_string(m, &len);
1714 mic.length = len;
1715
1716 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1717
1718 free(gssbuf.value);
1719 free(mic.value);
1720
1721 buffer_clear(m);
1722 buffer_put_int(m, ret);
1723
1724 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1725
1726 if (!GSS_ERROR(ret))
1727 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1728
1729 return (0);
1730 }
1731
1732 int
1733 mm_answer_gss_userok(int sock, Buffer *m)
1734 {
1735 int authenticated;
1736 const char *displayname;
1737
1738 if (!options.gss_authentication)
1739 fatal("%s: GSSAPI authentication not enabled", __func__);
1740
1741 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1742
1743 buffer_clear(m);
1744 buffer_put_int(m, authenticated);
1745
1746 debug3("%s: sending result %d", __func__, authenticated);
1747 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1748
1749 auth_method = "gssapi-with-mic";
1750
1751 if ((displayname = ssh_gssapi_displayname()) != NULL)
1752 auth2_record_info(authctxt, "%s", displayname);
1753
1754 /* Monitor loop will terminate if authenticated */
1755 return (authenticated);
1756 }
1757 #endif /* GSSAPI */
1758
1759