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