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