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