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