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