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