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