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