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