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