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