1 /* $OpenBSD: monitor.c,v 1.247 2024/12/03 22:30:03 jsg Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
31 #include <sys/socket.h>
53 # ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
59 #include <openssl/dh.h>
62 #include "openbsd-compat/sys-tree.h"
63 #include "openbsd-compat/sys-queue.h"
64 #include "openbsd-compat/openssl-compat.h"
78 #include "auth-options.h"
91 #include "monitor_wrap.h"
92 #include "monitor_fdpass.h"
102 static Gssctxt
*gsscontext
= NULL
;
106 extern ServerOptions options
;
107 extern u_int utmp_len
;
108 extern struct sshbuf
*cfg
;
109 extern struct sshbuf
*loginmsg
;
110 extern struct include_list includes
;
111 extern struct sshauthopt
*auth_opts
; /* XXX move to permanent ssh->authctxt? */
113 /* State exported from the child */
114 static struct sshbuf
*child_state
;
116 /* Functions on the monitor that answer unprivileged requests */
118 int mm_answer_moduli(struct ssh
*, int, struct sshbuf
*);
119 int mm_answer_sign(struct ssh
*, int, struct sshbuf
*);
120 int mm_answer_pwnamallow(struct ssh
*, int, struct sshbuf
*);
121 int mm_answer_auth2_read_banner(struct ssh
*, int, struct sshbuf
*);
122 int mm_answer_authserv(struct ssh
*, int, struct sshbuf
*);
123 int mm_answer_authpassword(struct ssh
*, int, struct sshbuf
*);
124 int mm_answer_bsdauthquery(struct ssh
*, int, struct sshbuf
*);
125 int mm_answer_bsdauthrespond(struct ssh
*, int, struct sshbuf
*);
126 int mm_answer_keyallowed(struct ssh
*, int, struct sshbuf
*);
127 int mm_answer_keyverify(struct ssh
*, int, struct sshbuf
*);
128 int mm_answer_pty(struct ssh
*, int, struct sshbuf
*);
129 int mm_answer_pty_cleanup(struct ssh
*, int, struct sshbuf
*);
130 int mm_answer_term(struct ssh
*, int, struct sshbuf
*);
131 int mm_answer_state(struct ssh
*, int, struct sshbuf
*);
134 int mm_answer_pam_start(struct ssh
*, int, struct sshbuf
*);
135 int mm_answer_pam_account(struct ssh
*, int, struct sshbuf
*);
136 int mm_answer_pam_init_ctx(struct ssh
*, int, struct sshbuf
*);
137 int mm_answer_pam_query(struct ssh
*, int, struct sshbuf
*);
138 int mm_answer_pam_respond(struct ssh
*, int, struct sshbuf
*);
139 int mm_answer_pam_free_ctx(struct ssh
*, int, struct sshbuf
*);
143 int mm_answer_gss_setup_ctx(struct ssh
*, int, struct sshbuf
*);
144 int mm_answer_gss_accept_ctx(struct ssh
*, int, struct sshbuf
*);
145 int mm_answer_gss_userok(struct ssh
*, int, struct sshbuf
*);
146 int mm_answer_gss_checkmic(struct ssh
*, int, struct sshbuf
*);
149 #ifdef SSH_AUDIT_EVENTS
150 int mm_answer_audit_event(struct ssh
*, int, struct sshbuf
*);
151 int mm_answer_audit_command(struct ssh
*, int, struct sshbuf
*);
154 static Authctxt
*authctxt
;
156 /* local state for key verify */
157 static u_char
*key_blob
= NULL
;
158 static size_t key_bloblen
= 0;
159 static u_int key_blobtype
= MM_NOKEY
;
160 static struct sshauthopt
*key_opts
= NULL
;
161 static char *hostbased_cuser
= NULL
;
162 static char *hostbased_chost
= NULL
;
163 static char *auth_method
= "unknown";
164 static char *auth_submethod
= NULL
;
165 static u_int session_id2_len
= 0;
166 static u_char
*session_id2
= NULL
;
167 static pid_t monitor_child_pid
;
168 int auth_attempted
= 0;
171 enum monitor_reqtype type
;
173 int (*f
)(struct ssh
*, int, struct sshbuf
*);
176 #define MON_ISAUTH 0x0004 /* Required for Authentication */
177 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
178 #define MON_ONCE 0x0010 /* Disable after calling */
179 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
181 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
183 #define MON_PERMIT 0x1000 /* Request is permitted */
185 static int monitor_read(struct ssh
*, struct monitor
*, struct mon_table
*,
186 struct mon_table
**);
187 static int monitor_read_log(struct monitor
*);
189 struct mon_table mon_dispatch_proto20
[] = {
190 {MONITOR_REQ_STATE
, MON_ONCE
, mm_answer_state
},
192 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
194 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
195 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
196 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
197 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
198 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
200 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
201 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
202 {MONITOR_REQ_PAM_INIT_CTX
, MON_ONCE
, mm_answer_pam_init_ctx
},
203 {MONITOR_REQ_PAM_QUERY
, 0, mm_answer_pam_query
},
204 {MONITOR_REQ_PAM_RESPOND
, MON_ONCE
, mm_answer_pam_respond
},
205 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
207 #ifdef SSH_AUDIT_EVENTS
208 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
211 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
212 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
214 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
215 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
217 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
218 {MONITOR_REQ_GSSSTEP
, 0, mm_answer_gss_accept_ctx
},
219 {MONITOR_REQ_GSSUSEROK
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_gss_userok
},
220 {MONITOR_REQ_GSSCHECKMIC
, MON_ONCE
, mm_answer_gss_checkmic
},
225 struct mon_table mon_dispatch_postauth20
[] = {
226 {MONITOR_REQ_STATE
, MON_ONCE
, mm_answer_state
},
228 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
230 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
231 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
232 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
233 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
234 #ifdef SSH_AUDIT_EVENTS
235 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
236 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
241 struct mon_table
*mon_dispatch
;
243 /* Specifies if a certain message is allowed at the moment */
245 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
247 while (ent
->f
!= NULL
) {
248 if (ent
->type
== type
) {
249 ent
->flags
&= ~MON_PERMIT
;
250 ent
->flags
|= permit
? MON_PERMIT
: 0;
258 monitor_permit_authentications(int permit
)
260 struct mon_table
*ent
= mon_dispatch
;
262 while (ent
->f
!= NULL
) {
263 if (ent
->flags
& MON_AUTH
) {
264 ent
->flags
&= ~MON_PERMIT
;
265 ent
->flags
|= permit
? MON_PERMIT
: 0;
272 monitor_child_preauth(struct ssh
*ssh
, struct monitor
*pmonitor
)
274 struct mon_table
*ent
;
275 int authenticated
= 0, partial
= 0;
277 debug3("preauth child monitor started");
279 if (pmonitor
->m_recvfd
>= 0)
280 close(pmonitor
->m_recvfd
);
281 if (pmonitor
->m_log_sendfd
>= 0)
282 close(pmonitor
->m_log_sendfd
);
283 pmonitor
->m_log_sendfd
= pmonitor
->m_recvfd
= -1;
285 authctxt
= (Authctxt
*)ssh
->authctxt
;
286 memset(authctxt
, 0, sizeof(*authctxt
));
287 ssh
->authctxt
= authctxt
;
289 authctxt
->loginmsg
= loginmsg
;
291 mon_dispatch
= mon_dispatch_proto20
;
292 /* Permit requests for state, moduli and signatures */
293 monitor_permit(mon_dispatch
, MONITOR_REQ_STATE
, 1);
294 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
295 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
297 /* The first few requests do not require asynchronous access */
298 while (!authenticated
) {
300 auth_method
= "unknown";
301 auth_submethod
= NULL
;
302 auth2_authctxt_reset_info(authctxt
);
304 authenticated
= (monitor_read(ssh
, pmonitor
,
305 mon_dispatch
, &ent
) == 1);
307 /* Record that auth was attempted to set exit status later */
308 if ((ent
->flags
& MON_AUTH
) != 0)
311 /* Special handling for multiple required authentications */
312 if (options
.num_auth_methods
!= 0) {
314 !auth2_update_methods_lists(authctxt
,
315 auth_method
, auth_submethod
)) {
316 debug3_f("method %s: partial", auth_method
);
323 if (!(ent
->flags
& MON_AUTHDECIDE
))
324 fatal_f("unexpected authentication from %d",
326 if (authctxt
->pw
->pw_uid
== 0 &&
327 !auth_root_allowed(ssh
, auth_method
))
330 /* PAM needs to perform account checks after auth */
331 if (options
.use_pam
&& authenticated
) {
334 if ((m
= sshbuf_new()) == NULL
)
335 fatal("%s: sshbuf_new failed",
337 mm_request_receive_expect(pmonitor
->m_sendfd
,
338 MONITOR_REQ_PAM_ACCOUNT
, m
);
339 authenticated
= mm_answer_pam_account(
340 ssh
, pmonitor
->m_sendfd
, m
);
345 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
346 auth_log(ssh
, authenticated
, partial
,
347 auth_method
, auth_submethod
);
348 if (!partial
&& !authenticated
)
349 authctxt
->failures
++;
350 if (authenticated
|| partial
) {
351 auth2_update_session_info(authctxt
,
352 auth_method
, auth_submethod
);
355 if (authctxt
->failures
> options
.max_authtries
) {
356 /* Shouldn't happen */
357 fatal_f("privsep child made too many authentication "
362 if (!authctxt
->valid
)
363 fatal_f("authenticated invalid user");
364 if (strcmp(auth_method
, "unknown") == 0)
365 fatal_f("authentication method name unknown");
367 debug_f("user %s authenticated by privileged process", authctxt
->user
);
369 ssh
->authctxt
= NULL
;
370 ssh_packet_set_log_preamble(ssh
, "user %s", authctxt
->user
);
372 mm_get_keystate(ssh
, pmonitor
);
374 /* Drain any buffered messages from the child */
375 while (pmonitor
->m_log_recvfd
!= -1 && monitor_read_log(pmonitor
) == 0)
378 if (pmonitor
->m_recvfd
>= 0)
379 close(pmonitor
->m_recvfd
);
380 if (pmonitor
->m_log_sendfd
>= 0)
381 close(pmonitor
->m_log_sendfd
);
382 pmonitor
->m_sendfd
= pmonitor
->m_log_recvfd
= -1;
386 monitor_set_child_handler(pid_t pid
)
388 monitor_child_pid
= pid
;
392 monitor_child_handler(int sig
)
394 kill(monitor_child_pid
, sig
);
398 monitor_child_postauth(struct ssh
*ssh
, struct monitor
*pmonitor
)
400 close(pmonitor
->m_recvfd
);
401 pmonitor
->m_recvfd
= -1;
403 monitor_set_child_handler(pmonitor
->m_pid
);
404 ssh_signal(SIGHUP
, &monitor_child_handler
);
405 ssh_signal(SIGTERM
, &monitor_child_handler
);
406 ssh_signal(SIGINT
, &monitor_child_handler
);
408 ssh_signal(SIGXFSZ
, SIG_IGN
);
411 mon_dispatch
= mon_dispatch_postauth20
;
413 /* Permit requests for moduli and signatures */
414 monitor_permit(mon_dispatch
, MONITOR_REQ_STATE
, 1);
415 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
416 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
417 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
419 if (auth_opts
->permit_pty_flag
) {
420 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
421 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
425 monitor_read(ssh
, pmonitor
, mon_dispatch
, NULL
);
429 monitor_read_log(struct monitor
*pmonitor
)
431 struct sshbuf
*logmsg
;
432 u_int len
, level
, forced
;
437 if ((logmsg
= sshbuf_new()) == NULL
)
438 fatal_f("sshbuf_new");
441 if ((r
= sshbuf_reserve(logmsg
, 4, &p
)) != 0)
442 fatal_fr(r
, "reserve len");
443 if (atomicio(read
, pmonitor
->m_log_recvfd
, p
, 4) != 4) {
444 if (errno
== EPIPE
) {
446 debug_f("child log fd closed");
447 close(pmonitor
->m_log_recvfd
);
448 pmonitor
->m_log_recvfd
= -1;
451 fatal_f("log fd read: %s", strerror(errno
));
453 if ((r
= sshbuf_get_u32(logmsg
, &len
)) != 0)
454 fatal_fr(r
, "parse len");
455 if (len
<= 4 || len
> 8192)
456 fatal_f("invalid log message length %u", len
);
458 /* Read severity, message */
459 sshbuf_reset(logmsg
);
460 if ((r
= sshbuf_reserve(logmsg
, len
, &p
)) != 0)
461 fatal_fr(r
, "reserve msg");
462 if (atomicio(read
, pmonitor
->m_log_recvfd
, p
, len
) != len
)
463 fatal_f("log fd read: %s", strerror(errno
));
464 if ((r
= sshbuf_get_u32(logmsg
, &level
)) != 0 ||
465 (r
= sshbuf_get_u32(logmsg
, &forced
)) != 0 ||
466 (r
= sshbuf_get_cstring(logmsg
, &msg
, NULL
)) != 0)
467 fatal_fr(r
, "parse");
470 if (log_level_name(level
) == NULL
)
471 fatal_f("invalid log level %u (corrupted message?)", level
);
472 sshlogdirect(level
, forced
, "%s [%s]", msg
,
473 mon_dispatch
== mon_dispatch_postauth20
? "postauth" : "preauth");
482 monitor_read(struct ssh
*ssh
, struct monitor
*pmonitor
, struct mon_table
*ent
,
483 struct mon_table
**pent
)
488 struct pollfd pfd
[2];
491 memset(&pfd
, 0, sizeof(pfd
));
492 pfd
[0].fd
= pmonitor
->m_sendfd
;
493 pfd
[0].events
= POLLIN
;
494 pfd
[1].fd
= pmonitor
->m_log_recvfd
;
495 pfd
[1].events
= pfd
[1].fd
== -1 ? 0 : POLLIN
;
496 if (poll(pfd
, pfd
[1].fd
== -1 ? 1 : 2, -1) == -1) {
497 if (errno
== EINTR
|| errno
== EAGAIN
)
499 fatal_f("poll: %s", strerror(errno
));
501 if (pfd
[1].revents
) {
503 * Drain all log messages before processing next
506 monitor_read_log(pmonitor
);
510 break; /* Continues below */
513 if ((m
= sshbuf_new()) == NULL
)
514 fatal_f("sshbuf_new");
516 mm_request_receive(pmonitor
->m_sendfd
, m
);
517 if ((r
= sshbuf_get_u8(m
, &type
)) != 0)
518 fatal_fr(r
, "parse type");
520 debug3_f("checking request %d", type
);
522 while (ent
->f
!= NULL
) {
523 if (ent
->type
== type
)
528 if (ent
->f
!= NULL
) {
529 if (!(ent
->flags
& MON_PERMIT
))
530 fatal_f("unpermitted request %d", type
);
531 ret
= (*ent
->f
)(ssh
, pmonitor
->m_sendfd
, m
);
534 /* The child may use this request only once, disable it */
535 if (ent
->flags
& MON_ONCE
) {
536 debug2_f("%d used once, disabling now", type
);
537 ent
->flags
&= ~MON_PERMIT
;
546 fatal_f("unsupported request: %d", type
);
552 /* allowed key state */
554 monitor_allowed_key(const u_char
*blob
, u_int bloblen
)
556 /* make sure key is allowed */
557 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
558 timingsafe_bcmp(key_blob
, blob
, key_bloblen
))
564 monitor_reset_key_state(void)
568 free(hostbased_cuser
);
569 free(hostbased_chost
);
570 sshauthopt_free(key_opts
);
573 key_blobtype
= MM_NOKEY
;
575 hostbased_cuser
= NULL
;
576 hostbased_chost
= NULL
;
580 mm_answer_state(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
582 struct sshbuf
*inc
= NULL
, *hostkeys
= NULL
;
583 struct sshbuf
*opts
= NULL
, *confdata
= NULL
;
584 struct include_item
*item
= NULL
;
590 debug_f("config len %zu", sshbuf_len(cfg
));
592 if ((m
= sshbuf_new()) == NULL
||
593 (inc
= sshbuf_new()) == NULL
||
594 (opts
= sshbuf_new()) == NULL
||
595 (confdata
= sshbuf_new()) == NULL
)
596 fatal_f("sshbuf_new failed");
598 /* XXX unnecessary? */
599 /* pack includes into a string */
600 TAILQ_FOREACH(item
, &includes
, entry
) {
601 if ((r
= sshbuf_put_cstring(inc
, item
->selector
)) != 0 ||
602 (r
= sshbuf_put_cstring(inc
, item
->filename
)) != 0 ||
603 (r
= sshbuf_put_stringb(inc
, item
->contents
)) != 0)
604 fatal_fr(r
, "compose includes");
607 hostkeys
= pack_hostkeys();
610 * Protocol from monitor to unpriv privsep process:
611 * string configuration
612 * uint64 timing_secret XXX move delays to monitor and remove
613 * string host_keys[] {
617 * string server_banner
618 * string client_banner
619 * string included_files[] {
624 * string configuration_data (postauth)
625 * string keystate (postauth)
626 * string authenticated_user (postauth)
627 * string session_info (postauth)
628 * string authopts (postauth)
630 if ((r
= sshbuf_put_stringb(m
, cfg
)) != 0 ||
631 (r
= sshbuf_put_u64(m
, options
.timing_secret
)) != 0 ||
632 (r
= sshbuf_put_stringb(m
, hostkeys
)) != 0 ||
633 (r
= sshbuf_put_stringb(m
, ssh
->kex
->server_version
)) != 0 ||
634 (r
= sshbuf_put_stringb(m
, ssh
->kex
->client_version
)) != 0 ||
635 (r
= sshbuf_put_stringb(m
, inc
)) != 0)
636 fatal_fr(r
, "compose config");
638 postauth
= (authctxt
&& authctxt
->pw
&& authctxt
->authenticated
);
640 /* XXX shouldn't be reachable */
641 fatal_f("internal error: called in postauth");
646 sshbuf_free(confdata
);
648 mm_request_send(sock
, MONITOR_ANS_STATE
, m
);
657 mm_answer_moduli(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
660 const BIGNUM
*dh_p
, *dh_g
;
662 u_int min
, want
, max
;
664 if ((r
= sshbuf_get_u32(m
, &min
)) != 0 ||
665 (r
= sshbuf_get_u32(m
, &want
)) != 0 ||
666 (r
= sshbuf_get_u32(m
, &max
)) != 0)
667 fatal_fr(r
, "parse");
669 debug3_f("got parameters: %d %d %d", min
, want
, max
);
670 /* We need to check here, too, in case the child got corrupted */
671 if (max
< min
|| want
< min
|| max
< want
)
672 fatal_f("bad parameters: %d %d %d", min
, want
, max
);
676 dh
= choose_dh(min
, want
, max
);
678 if ((r
= sshbuf_put_u8(m
, 0)) != 0)
679 fatal_fr(r
, "assemble empty");
682 /* Send first bignum */
683 DH_get0_pqg(dh
, &dh_p
, NULL
, &dh_g
);
684 if ((r
= sshbuf_put_u8(m
, 1)) != 0 ||
685 (r
= sshbuf_put_bignum2(m
, dh_p
)) != 0 ||
686 (r
= sshbuf_put_bignum2(m
, dh_g
)) != 0)
687 fatal_fr(r
, "assemble");
691 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
697 mm_answer_sign(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
699 extern int auth_sock
; /* XXX move to state struct? */
700 struct sshkey
*pubkey
, *key
;
701 struct sshbuf
*sigbuf
= NULL
;
702 u_char
*p
= NULL
, *signature
= NULL
;
704 size_t datlen
, siglen
;
705 int r
, is_proof
= 0, keyid
;
707 const char proof_req
[] = "hostkeys-prove-00@openssh.com";
709 debug3_f("entering");
711 if ((r
= sshkey_froms(m
, &pubkey
)) != 0 ||
712 (r
= sshbuf_get_string(m
, &p
, &datlen
)) != 0 ||
713 (r
= sshbuf_get_cstring(m
, &alg
, NULL
)) != 0 ||
714 (r
= sshbuf_get_u32(m
, &compat
)) != 0)
715 fatal_fr(r
, "parse");
717 if ((keyid
= get_hostkey_index(pubkey
, 1, ssh
)) == -1)
718 fatal_f("unknown hostkey");
719 debug_f("hostkey %s index %d", sshkey_ssh_name(pubkey
), keyid
);
723 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
724 * SHA384 (48 bytes) and SHA512 (64 bytes).
726 * Otherwise, verify the signature request is for a hostkey
729 * XXX perform similar check for KEX signature requests too?
730 * it's not trivial, since what is signed is the hash, rather
731 * than the full kex structure...
733 if (datlen
!= 20 && datlen
!= 32 && datlen
!= 48 && datlen
!= 64) {
735 * Construct expected hostkey proof and compare it to what
736 * the client sent us.
738 if (session_id2_len
== 0) /* hostkeys is never first */
739 fatal_f("bad data length: %zu", datlen
);
740 if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) == NULL
)
741 fatal_f("no hostkey for index %d", keyid
);
742 if ((sigbuf
= sshbuf_new()) == NULL
)
743 fatal_f("sshbuf_new");
744 if ((r
= sshbuf_put_cstring(sigbuf
, proof_req
)) != 0 ||
745 (r
= sshbuf_put_string(sigbuf
, session_id2
,
746 session_id2_len
)) != 0 ||
747 (r
= sshkey_puts(key
, sigbuf
)) != 0)
748 fatal_fr(r
, "assemble private key proof");
749 if (datlen
!= sshbuf_len(sigbuf
) ||
750 memcmp(p
, sshbuf_ptr(sigbuf
), sshbuf_len(sigbuf
)) != 0)
751 fatal_f("bad data length: %zu, hostkey proof len %zu",
752 datlen
, sshbuf_len(sigbuf
));
757 /* save session id, it will be passed on the first call */
758 if (session_id2_len
== 0) {
759 session_id2_len
= datlen
;
760 session_id2
= xmalloc(session_id2_len
);
761 memcpy(session_id2
, p
, session_id2_len
);
764 if ((key
= get_hostkey_by_index(keyid
)) != NULL
) {
765 if ((r
= sshkey_sign(key
, &signature
, &siglen
, p
, datlen
, alg
,
766 options
.sk_provider
, NULL
, compat
)) != 0)
768 } else if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) != NULL
&&
770 if ((r
= ssh_agent_sign(auth_sock
, key
, &signature
, &siglen
,
771 p
, datlen
, alg
, compat
)) != 0)
772 fatal_fr(r
, "agent sign");
774 fatal_f("no hostkey from index %d", keyid
);
776 debug3_f("%s %s signature len=%zu", alg
,
777 is_proof
? "hostkey proof" : "KEX", siglen
);
780 if ((r
= sshbuf_put_string(m
, signature
, siglen
)) != 0)
781 fatal_fr(r
, "assemble");
787 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
789 /* Turn on permissions for getpwnam */
790 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
795 #define PUTPW(b, id) \
797 if ((r = sshbuf_put_string(b, \
798 &pwent->id, sizeof(pwent->id))) != 0) \
799 fatal_fr(r, "assemble %s", #id); \
803 mm_encode_server_options(struct sshbuf
*m
)
808 /* XXX this leaks raw pointers to the unpriv child processes */
809 if ((r
= sshbuf_put_string(m
, &options
, sizeof(options
))) != 0)
810 fatal_fr(r
, "assemble options");
812 #define M_CP_STROPT(x) do { \
813 if (options.x != NULL && \
814 (r = sshbuf_put_cstring(m, options.x)) != 0) \
815 fatal_fr(r, "assemble %s", #x); \
817 #define M_CP_STRARRAYOPT(x, nx) do { \
818 for (i = 0; i < options.nx; i++) { \
819 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
820 fatal_fr(r, "assemble %s", #x); \
823 /* See comment in servconf.h */
824 COPY_MATCH_STRING_OPTS();
826 #undef M_CP_STRARRAYOPT
829 /* Retrieves the password entry and also checks if the user is permitted */
831 mm_answer_pwnamallow(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
833 struct passwd
*pwent
;
836 debug3_f("entering");
838 if (authctxt
->attempt
++ != 0)
839 fatal_f("multiple attempts for getpwnam");
841 if ((r
= sshbuf_get_cstring(m
, &authctxt
->user
, NULL
)) != 0)
842 fatal_fr(r
, "parse");
844 pwent
= getpwnamallow(ssh
, authctxt
->user
);
846 setproctitle("%s [priv]", pwent
? authctxt
->user
: "unknown");
851 if ((r
= sshbuf_put_u8(m
, 0)) != 0)
852 fatal_fr(r
, "assemble fakepw");
853 authctxt
->pw
= fakepw();
858 authctxt
->pw
= pwent
;
861 /* XXX send fake class/dir/shell, etc. */
862 if ((r
= sshbuf_put_u8(m
, 1)) != 0)
863 fatal_fr(r
, "assemble ok");
866 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
869 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
872 if ((r
= sshbuf_put_cstring(m
, pwent
->pw_name
)) != 0 ||
873 (r
= sshbuf_put_cstring(m
, "*")) != 0 ||
874 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
875 (r
= sshbuf_put_cstring(m
, pwent
->pw_gecos
)) != 0 ||
877 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
878 (r
= sshbuf_put_cstring(m
, pwent
->pw_class
)) != 0 ||
880 (r
= sshbuf_put_cstring(m
, pwent
->pw_dir
)) != 0 ||
881 (r
= sshbuf_put_cstring(m
, pwent
->pw_shell
)) != 0)
882 fatal_fr(r
, "assemble pw");
885 ssh_packet_set_log_preamble(ssh
, "%suser %s",
886 authctxt
->valid
? "authenticating" : "invalid ", authctxt
->user
);
888 if (options
.refuse_connection
) {
889 logit("administratively prohibited connection for "
890 "%s%s from %.128s port %d",
891 authctxt
->valid
? "" : "invalid user ",
892 authctxt
->user
, ssh_remote_ipaddr(ssh
),
893 ssh_remote_port(ssh
));
894 cleanup_exit(EXIT_CONFIG_REFUSED
);
897 /* Send active options to unpriv */
898 mm_encode_server_options(m
);
900 /* Create valid auth method lists */
901 if (auth2_setup_methods_lists(authctxt
) != 0) {
903 * The monitor will continue long enough to let the child
904 * run to its packet_disconnect(), but it must not allow any
905 * authentication to succeed.
907 debug_f("no valid authentication method lists");
910 debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed
);
911 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
913 /* Allow service/style information on the auth context */
914 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
915 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
919 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
925 int mm_answer_auth2_read_banner(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
931 banner
= auth2_read_banner();
932 if ((r
= sshbuf_put_cstring(m
, banner
!= NULL
? banner
: "")) != 0)
933 fatal_fr(r
, "assemble");
934 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
941 mm_answer_authserv(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
945 monitor_permit_authentications(1);
947 if ((r
= sshbuf_get_cstring(m
, &authctxt
->service
, NULL
)) != 0 ||
948 (r
= sshbuf_get_cstring(m
, &authctxt
->style
, NULL
)) != 0)
949 fatal_fr(r
, "parse");
950 debug3_f("service=%s, style=%s", authctxt
->service
, authctxt
->style
);
952 if (strlen(authctxt
->style
) == 0) {
953 free(authctxt
->style
);
954 authctxt
->style
= NULL
;
961 * Check that the key type appears in the supplied pattern list, ignoring
962 * mismatches in the signature algorithm. (Signature algorithm checks are
963 * performed in the unprivileged authentication code).
964 * Returns 1 on success, 0 otherwise.
967 key_base_type_match(const char *method
, const struct sshkey
*key
,
970 char *s
, *l
, *ol
= xstrdup(list
);
974 for ((s
= strsep(&l
, ",")); s
&& *s
!= '\0'; (s
= strsep(&l
, ","))) {
975 if (sshkey_type_from_name(s
) == key
->type
) {
981 error("%s key type %s is not in permitted list %s", method
,
982 sshkey_ssh_name(key
), list
);
990 mm_answer_authpassword(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
992 static int call_count
;
994 int r
, authenticated
;
997 if (!options
.password_authentication
)
998 fatal_f("password authentication not enabled");
999 if ((r
= sshbuf_get_cstring(m
, &passwd
, &plen
)) != 0)
1000 fatal_fr(r
, "parse");
1001 /* Only authenticate if the context is valid */
1002 authenticated
= options
.password_authentication
&&
1003 auth_password(ssh
, passwd
);
1004 freezero(passwd
, plen
);
1007 if ((r
= sshbuf_put_u32(m
, authenticated
)) != 0)
1008 fatal_fr(r
, "assemble");
1010 if ((r
= sshbuf_put_u32(m
, sshpam_get_maxtries_reached())) != 0)
1011 fatal_fr(r
, "assemble PAM");
1014 debug3("%s: sending result %d", __func__
, authenticated
);
1015 debug3_f("sending result %d", authenticated
);
1016 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
1019 if (plen
== 0 && call_count
== 1)
1020 auth_method
= "none";
1022 auth_method
= "password";
1024 /* Causes monitor loop to terminate if authenticated */
1025 return (authenticated
);
1030 mm_answer_bsdauthquery(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1032 char *name
, *infotxt
;
1033 u_int numprompts
, *echo_on
, success
;
1037 if (!options
.kbd_interactive_authentication
)
1038 fatal_f("kbd-int authentication not enabled");
1039 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
1040 &prompts
, &echo_on
) < 0 ? 0 : 1;
1043 if ((r
= sshbuf_put_u32(m
, success
)) != 0)
1044 fatal_fr(r
, "assemble");
1046 if ((r
= sshbuf_put_cstring(m
, prompts
[0])) != 0)
1047 fatal_fr(r
, "assemble prompt");
1050 debug3_f("sending challenge success: %u", success
);
1051 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
1064 mm_answer_bsdauthrespond(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1069 if (!options
.kbd_interactive_authentication
)
1070 fatal_f("kbd-int authentication not enabled");
1071 if (authctxt
->as
== NULL
)
1072 fatal_f("no bsd auth session");
1074 if ((r
= sshbuf_get_cstring(m
, &response
, NULL
)) != 0)
1075 fatal_fr(r
, "parse");
1076 authok
= options
.kbd_interactive_authentication
&&
1077 auth_userresponse(authctxt
->as
, response
, 0);
1078 authctxt
->as
= NULL
;
1079 debug3_f("<%s> = <%d>", response
, authok
);
1083 if ((r
= sshbuf_put_u32(m
, authok
)) != 0)
1084 fatal_fr(r
, "assemble");
1086 debug3_f("sending authenticated: %d", authok
);
1087 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
1089 auth_method
= "keyboard-interactive";
1090 auth_submethod
= "bsdauth";
1092 return (authok
!= 0);
1098 mm_answer_pam_start(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1100 if (!options
.use_pam
)
1101 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
1105 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
1106 if (options
.kbd_interactive_authentication
)
1107 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_INIT_CTX
, 1);
1113 mm_answer_pam_account(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1118 if (!options
.use_pam
)
1119 fatal("%s: PAM not enabled", __func__
);
1121 ret
= do_pam_account();
1123 if ((r
= sshbuf_put_u32(m
, ret
)) != 0 ||
1124 (r
= sshbuf_put_stringb(m
, loginmsg
)) != 0)
1125 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1127 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
1132 static void *sshpam_ctxt
, *sshpam_authok
;
1133 extern KbdintDevice sshpam_device
;
1136 mm_answer_pam_init_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1141 debug3("%s", __func__
);
1142 if (!options
.kbd_interactive_authentication
)
1143 fatal("%s: kbd-int authentication not enabled", __func__
);
1144 if (sshpam_ctxt
!= NULL
)
1145 fatal("%s: already called", __func__
);
1146 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
1147 sshpam_authok
= NULL
;
1149 if (sshpam_ctxt
!= NULL
) {
1150 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
1151 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_QUERY
, 1);
1154 if ((r
= sshbuf_put_u32(m
, ok
)) != 0)
1155 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1156 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
1161 mm_answer_pam_query(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1163 char *name
= NULL
, *info
= NULL
, **prompts
= NULL
;
1164 u_int i
, num
= 0, *echo_on
= 0;
1167 debug3("%s", __func__
);
1168 sshpam_authok
= NULL
;
1169 if (sshpam_ctxt
== NULL
)
1170 fatal("%s: no context", __func__
);
1171 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
,
1172 &num
, &prompts
, &echo_on
);
1173 if (ret
== 0 && num
== 0)
1174 sshpam_authok
= sshpam_ctxt
;
1175 if (num
> 1 || name
== NULL
|| info
== NULL
)
1176 fatal("sshpam_device.query failed");
1177 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_RESPOND
, 1);
1179 if ((r
= sshbuf_put_u32(m
, ret
)) != 0 ||
1180 (r
= sshbuf_put_cstring(m
, name
)) != 0 ||
1181 (r
= sshbuf_put_cstring(m
, info
)) != 0 ||
1182 (r
= sshbuf_put_u32(m
, sshpam_get_maxtries_reached())) != 0 ||
1183 (r
= sshbuf_put_u32(m
, num
)) != 0)
1184 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1187 for (i
= 0; i
< num
; ++i
) {
1188 if ((r
= sshbuf_put_cstring(m
, prompts
[i
])) != 0 ||
1189 (r
= sshbuf_put_u32(m
, echo_on
[i
])) != 0)
1190 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1195 auth_method
= "keyboard-interactive";
1196 auth_submethod
= "pam";
1197 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
1202 mm_answer_pam_respond(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1208 debug3("%s", __func__
);
1209 if (sshpam_ctxt
== NULL
)
1210 fatal("%s: no context", __func__
);
1211 sshpam_authok
= NULL
;
1212 if ((r
= sshbuf_get_u32(m
, &num
)) != 0)
1213 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1214 if (num
> PAM_MAX_NUM_MSG
) {
1215 fatal_f("Too many PAM messages, got %u, expected <= %u",
1216 num
, (unsigned)PAM_MAX_NUM_MSG
);
1219 resp
= xcalloc(num
, sizeof(char *));
1220 for (i
= 0; i
< num
; ++i
) {
1221 if ((r
= sshbuf_get_cstring(m
, &(resp
[i
]), NULL
)) != 0)
1222 fatal("%s: buffer error: %s",
1223 __func__
, ssh_err(r
));
1225 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
1226 for (i
= 0; i
< num
; ++i
)
1230 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
1233 if ((r
= sshbuf_put_u32(m
, ret
)) != 0)
1234 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1235 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
1236 auth_method
= "keyboard-interactive";
1237 auth_submethod
= "pam";
1239 sshpam_authok
= sshpam_ctxt
;
1244 mm_answer_pam_free_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1246 int r
= sshpam_authok
!= NULL
&& sshpam_authok
== sshpam_ctxt
;
1248 debug3("%s", __func__
);
1249 if (sshpam_ctxt
== NULL
)
1250 fatal("%s: no context", __func__
);
1251 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
1252 sshpam_ctxt
= sshpam_authok
= NULL
;
1254 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
1255 /* Allow another attempt */
1256 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_INIT_CTX
, 1);
1257 auth_method
= "keyboard-interactive";
1258 auth_submethod
= "pam";
1264 mm_answer_keyallowed(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1266 struct sshkey
*key
= NULL
;
1267 char *cuser
, *chost
;
1268 u_int pubkey_auth_attempt
;
1271 struct sshauthopt
*opts
= NULL
;
1273 debug3_f("entering");
1274 if ((r
= sshbuf_get_u32(m
, &type
)) != 0 ||
1275 (r
= sshbuf_get_cstring(m
, &cuser
, NULL
)) != 0 ||
1276 (r
= sshbuf_get_cstring(m
, &chost
, NULL
)) != 0 ||
1277 (r
= sshkey_froms(m
, &key
)) != 0 ||
1278 (r
= sshbuf_get_u32(m
, &pubkey_auth_attempt
)) != 0)
1279 fatal_fr(r
, "parse");
1281 if (key
!= NULL
&& authctxt
->valid
) {
1284 auth_method
= "publickey";
1285 if (!options
.pubkey_authentication
)
1287 if (auth2_key_already_used(authctxt
, key
))
1289 if (!key_base_type_match(auth_method
, key
,
1290 options
.pubkey_accepted_algos
))
1292 allowed
= user_key_allowed(ssh
, authctxt
->pw
, key
,
1293 pubkey_auth_attempt
, &opts
);
1296 auth_method
= "hostbased";
1297 if (!options
.hostbased_authentication
)
1299 if (auth2_key_already_used(authctxt
, key
))
1301 if (!key_base_type_match(auth_method
, key
,
1302 options
.hostbased_accepted_algos
))
1304 allowed
= hostbased_key_allowed(ssh
, authctxt
->pw
,
1306 auth2_record_info(authctxt
,
1307 "client user \"%.100s\", client host \"%.100s\"",
1311 fatal_f("unknown key type %u", type
);
1316 debug3_f("%s authentication%s: %s key is %s", auth_method
,
1317 pubkey_auth_attempt
? "" : " test",
1318 (key
== NULL
|| !authctxt
->valid
) ? "invalid" : sshkey_type(key
),
1319 allowed
? "allowed" : "not allowed");
1321 auth2_record_key(authctxt
, 0, key
);
1323 /* clear temporarily storage (used by verify) */
1324 monitor_reset_key_state();
1327 /* Save temporarily for comparison in verify */
1328 if ((r
= sshkey_to_blob(key
, &key_blob
, &key_bloblen
)) != 0)
1329 fatal_fr(r
, "sshkey_to_blob");
1330 key_blobtype
= type
;
1332 hostbased_cuser
= cuser
;
1333 hostbased_chost
= chost
;
1335 /* Log failed attempt */
1336 auth_log(ssh
, 0, 0, auth_method
, NULL
);
1343 if ((r
= sshbuf_put_u32(m
, allowed
)) != 0)
1344 fatal_fr(r
, "assemble");
1345 if (opts
!= NULL
&& (r
= sshauthopt_serialise(opts
, m
, 1)) != 0)
1346 fatal_fr(r
, "sshauthopt_serialise");
1347 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1350 sshauthopt_free(opts
);
1356 monitor_valid_userblob(struct ssh
*ssh
, const u_char
*data
, u_int datalen
)
1359 struct sshkey
*hostkey
= NULL
;
1361 char *userstyle
, *cp
;
1364 int hostbound
= 0, r
, fail
= 0;
1366 if ((b
= sshbuf_from(data
, datalen
)) == NULL
)
1367 fatal_f("sshbuf_from");
1369 if (ssh
->compat
& SSH_OLD_SESSIONID
) {
1371 len
= sshbuf_len(b
);
1372 if ((session_id2
== NULL
) ||
1373 (len
< session_id2_len
) ||
1374 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1376 if ((r
= sshbuf_consume(b
, session_id2_len
)) != 0)
1377 fatal_fr(r
, "consume");
1379 if ((r
= sshbuf_get_string_direct(b
, &p
, &len
)) != 0)
1380 fatal_fr(r
, "parse sessionid");
1381 if ((session_id2
== NULL
) ||
1382 (len
!= session_id2_len
) ||
1383 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1386 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1387 fatal_fr(r
, "parse type");
1388 if (type
!= SSH2_MSG_USERAUTH_REQUEST
)
1390 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1391 fatal_fr(r
, "parse userstyle");
1392 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1393 authctxt
->style
? ":" : "",
1394 authctxt
->style
? authctxt
->style
: "");
1395 if (strcmp(userstyle
, cp
) != 0) {
1396 logit("wrong user name passed to monitor: "
1397 "expected %s != %.100s", userstyle
, cp
);
1402 if ((r
= sshbuf_skip_string(b
)) != 0 || /* service */
1403 (r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1404 fatal_fr(r
, "parse method");
1405 if (strcmp("publickey", cp
) != 0) {
1406 if (strcmp("publickey-hostbound-v00@openssh.com", cp
) == 0)
1412 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1413 fatal_fr(r
, "parse pktype");
1416 if ((r
= sshbuf_skip_string(b
)) != 0 || /* pkalg */
1417 (r
= sshbuf_skip_string(b
)) != 0 || /* pkblob */
1418 (hostbound
&& (r
= sshkey_froms(b
, &hostkey
)) != 0))
1419 fatal_fr(r
, "parse pk");
1420 if (sshbuf_len(b
) != 0)
1423 if (hostkey
!= NULL
) {
1425 * Ensure this is actually one of our hostkeys; unfortunately
1426 * can't check ssh->kex->initial_hostkey directly at this point
1427 * as packet state has not yet been exported to monitor.
1429 if (get_hostkey_index(hostkey
, 1, ssh
) == -1)
1430 fatal_f("hostbound hostkey does not match");
1431 sshkey_free(hostkey
);
1437 monitor_valid_hostbasedblob(const u_char
*data
, u_int datalen
,
1438 const char *cuser
, const char *chost
)
1442 char *cp
, *userstyle
;
1447 if ((b
= sshbuf_from(data
, datalen
)) == NULL
)
1448 fatal_f("sshbuf_new");
1449 if ((r
= sshbuf_get_string_direct(b
, &p
, &len
)) != 0)
1450 fatal_fr(r
, "parse sessionid");
1452 if ((session_id2
== NULL
) ||
1453 (len
!= session_id2_len
) ||
1454 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1457 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1458 fatal_fr(r
, "parse type");
1459 if (type
!= SSH2_MSG_USERAUTH_REQUEST
)
1461 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1462 fatal_fr(r
, "parse userstyle");
1463 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1464 authctxt
->style
? ":" : "",
1465 authctxt
->style
? authctxt
->style
: "");
1466 if (strcmp(userstyle
, cp
) != 0) {
1467 logit("wrong user name passed to monitor: "
1468 "expected %s != %.100s", userstyle
, cp
);
1473 if ((r
= sshbuf_skip_string(b
)) != 0 || /* service */
1474 (r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1475 fatal_fr(r
, "parse method");
1476 if (strcmp(cp
, "hostbased") != 0)
1479 if ((r
= sshbuf_skip_string(b
)) != 0 || /* pkalg */
1480 (r
= sshbuf_skip_string(b
)) != 0) /* pkblob */
1481 fatal_fr(r
, "parse pk");
1483 /* verify client host, strip trailing dot if necessary */
1484 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1485 fatal_fr(r
, "parse host");
1486 if (((len
= strlen(cp
)) > 0) && cp
[len
- 1] == '.')
1488 if (strcmp(cp
, chost
) != 0)
1492 /* verify client user */
1493 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1494 fatal_fr(r
, "parse ruser");
1495 if (strcmp(cp
, cuser
) != 0)
1499 if (sshbuf_len(b
) != 0)
1506 mm_answer_keyverify(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1509 const u_char
*signature
, *data
, *blob
;
1510 char *sigalg
= NULL
, *fp
= NULL
;
1511 size_t signaturelen
, datalen
, bloblen
;
1512 int r
, ret
, req_presence
= 0, req_verify
= 0, valid_data
= 0;
1514 struct sshkey_sig_details
*sig_details
= NULL
;
1516 if ((r
= sshbuf_get_string_direct(m
, &blob
, &bloblen
)) != 0 ||
1517 (r
= sshbuf_get_string_direct(m
, &signature
, &signaturelen
)) != 0 ||
1518 (r
= sshbuf_get_string_direct(m
, &data
, &datalen
)) != 0 ||
1519 (r
= sshbuf_get_cstring(m
, &sigalg
, NULL
)) != 0)
1520 fatal_fr(r
, "parse");
1522 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1523 !monitor_allowed_key(blob
, bloblen
))
1524 fatal_f("bad key, not previously allowed");
1526 /* Empty signature algorithm means NULL. */
1527 if (*sigalg
== '\0') {
1532 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1533 if ((r
= sshkey_from_blob(blob
, bloblen
, &key
)) != 0)
1534 fatal_fr(r
, "parse key");
1536 switch (key_blobtype
) {
1538 valid_data
= monitor_valid_userblob(ssh
, data
, datalen
);
1539 auth_method
= "publickey";
1542 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1543 hostbased_cuser
, hostbased_chost
);
1544 auth_method
= "hostbased";
1551 fatal_f("bad %s signature data blob",
1552 key_blobtype
== MM_USERKEY
? "userkey" :
1553 (key_blobtype
== MM_HOSTKEY
? "hostkey" : "unknown"));
1555 if ((fp
= sshkey_fingerprint(key
, options
.fingerprint_hash
,
1556 SSH_FP_DEFAULT
)) == NULL
)
1557 fatal_f("sshkey_fingerprint failed");
1559 ret
= sshkey_verify(key
, signature
, signaturelen
, data
, datalen
,
1560 sigalg
, ssh
->compat
, &sig_details
);
1561 debug3_f("%s %s signature using %s %s%s%s", auth_method
,
1562 sshkey_type(key
), sigalg
== NULL
? "default" : sigalg
,
1563 (ret
== 0) ? "verified" : "unverified",
1564 (ret
!= 0) ? ": " : "", (ret
!= 0) ? ssh_err(ret
) : "");
1566 if (ret
== 0 && key_blobtype
== MM_USERKEY
&& sig_details
!= NULL
) {
1567 req_presence
= (options
.pubkey_auth_options
&
1568 PUBKEYAUTH_TOUCH_REQUIRED
) ||
1569 !key_opts
->no_require_user_presence
;
1571 (sig_details
->sk_flags
& SSH_SK_USER_PRESENCE_REQD
) == 0) {
1572 error("public key %s %s signature for %s%s from %.128s "
1573 "port %d rejected: user presence "
1574 "(authenticator touch) requirement not met ",
1575 sshkey_type(key
), fp
,
1576 authctxt
->valid
? "" : "invalid user ",
1577 authctxt
->user
, ssh_remote_ipaddr(ssh
),
1578 ssh_remote_port(ssh
));
1579 ret
= SSH_ERR_SIGNATURE_INVALID
;
1581 req_verify
= (options
.pubkey_auth_options
&
1582 PUBKEYAUTH_VERIFY_REQUIRED
) || key_opts
->require_verify
;
1584 (sig_details
->sk_flags
& SSH_SK_USER_VERIFICATION_REQD
) == 0) {
1585 error("public key %s %s signature for %s%s from %.128s "
1586 "port %d rejected: user verification requirement "
1587 "not met ", sshkey_type(key
), fp
,
1588 authctxt
->valid
? "" : "invalid user ",
1589 authctxt
->user
, ssh_remote_ipaddr(ssh
),
1590 ssh_remote_port(ssh
));
1591 ret
= SSH_ERR_SIGNATURE_INVALID
;
1594 auth2_record_key(authctxt
, ret
== 0, key
);
1596 if (key_blobtype
== MM_USERKEY
&& ret
== 0)
1597 auth_activate_options(ssh
, key_opts
);
1598 monitor_reset_key_state();
1602 /* encode ret != 0 as positive integer, since we're sending u32 */
1603 encoded_ret
= (ret
!= 0);
1604 if ((r
= sshbuf_put_u32(m
, encoded_ret
)) != 0 ||
1605 (r
= sshbuf_put_u8(m
, sig_details
!= NULL
)) != 0)
1606 fatal_fr(r
, "assemble");
1607 if (sig_details
!= NULL
) {
1608 if ((r
= sshbuf_put_u32(m
, sig_details
->sk_counter
)) != 0 ||
1609 (r
= sshbuf_put_u8(m
, sig_details
->sk_flags
)) != 0)
1610 fatal_fr(r
, "assemble sk");
1612 sshkey_sig_details_free(sig_details
);
1613 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1623 mm_record_login(struct ssh
*ssh
, Session
*s
, struct passwd
*pw
)
1626 struct sockaddr_storage from
;
1629 * Get IP address of client. If the connection is not a socket, let
1630 * the address be 0.0.0.0.
1632 memset(&from
, 0, sizeof(from
));
1633 fromlen
= sizeof(from
);
1634 if (ssh_packet_connection_is_on_socket(ssh
)) {
1635 if (getpeername(ssh_packet_get_connection_in(ssh
),
1636 (struct sockaddr
*)&from
, &fromlen
) == -1) {
1637 debug("getpeername: %.100s", strerror(errno
));
1641 /* Record that there was a login on that tty from the remote host. */
1642 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1643 session_get_remote_name_or_ip(ssh
, utmp_len
, options
.use_dns
),
1644 (struct sockaddr
*)&from
, fromlen
);
1648 mm_session_close(Session
*s
)
1650 debug3_f("session %d pid %ld", s
->self
, (long)s
->pid
);
1651 if (s
->ttyfd
!= -1) {
1652 debug3_f("tty %s ptyfd %d", s
->tty
, s
->ptyfd
);
1653 session_pty_cleanup2(s
);
1655 session_unused(s
->self
);
1659 mm_answer_pty(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1661 extern struct monitor
*pmonitor
;
1665 debug3_f("entering");
1671 s
->authctxt
= authctxt
;
1672 s
->pw
= authctxt
->pw
;
1673 s
->pid
= pmonitor
->m_pid
;
1674 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1677 pty_setowner(authctxt
->pw
, s
->tty
);
1679 if ((r
= sshbuf_put_u32(m
, 1)) != 0 ||
1680 (r
= sshbuf_put_cstring(m
, s
->tty
)) != 0)
1681 fatal_fr(r
, "assemble");
1683 /* We need to trick ttyslot */
1684 if (dup2(s
->ttyfd
, 0) == -1)
1687 mm_record_login(ssh
, s
, authctxt
->pw
);
1689 /* Now we can close the file descriptor again */
1692 /* send messages generated by record_login */
1693 if ((r
= sshbuf_put_stringb(m
, loginmsg
)) != 0)
1694 fatal_fr(r
, "assemble loginmsg");
1695 sshbuf_reset(loginmsg
);
1697 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1699 if (mm_send_fd(sock
, s
->ptyfd
) == -1 ||
1700 mm_send_fd(sock
, s
->ttyfd
) == -1)
1701 fatal_f("send fds failed");
1703 /* make sure nothing uses fd 0 */
1704 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) == -1)
1705 fatal_f("open(/dev/null): %s", strerror(errno
));
1707 error_f("fd0 %d != 0", fd0
);
1709 /* slave side of pty is not needed */
1711 s
->ttyfd
= s
->ptyfd
;
1712 /* no need to dup() because nobody closes ptyfd */
1713 s
->ptymaster
= s
->ptyfd
;
1715 debug3_f("tty %s ptyfd %d", s
->tty
, s
->ttyfd
);
1721 mm_session_close(s
);
1722 if ((r
= sshbuf_put_u32(m
, 0)) != 0)
1723 fatal_fr(r
, "assemble 0");
1724 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1729 mm_answer_pty_cleanup(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1735 debug3_f("entering");
1737 if ((r
= sshbuf_get_cstring(m
, &tty
, NULL
)) != 0)
1738 fatal_fr(r
, "parse tty");
1739 if ((s
= session_by_tty(tty
)) != NULL
)
1740 mm_session_close(s
);
1747 mm_answer_term(struct ssh
*ssh
, int sock
, struct sshbuf
*req
)
1749 extern struct monitor
*pmonitor
;
1752 debug3_f("tearing down sessions");
1754 /* The child is terminating */
1755 session_destroy_all(ssh
, &mm_session_close
);
1758 if (options
.use_pam
)
1762 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1766 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1768 /* Terminate process */
1772 #ifdef SSH_AUDIT_EVENTS
1773 /* Report that an audit event occurred */
1775 mm_answer_audit_event(struct ssh
*ssh
, int socket
, struct sshbuf
*m
)
1778 ssh_audit_event_t event
;
1781 debug3("%s entering", __func__
);
1783 if ((r
= sshbuf_get_u32(m
, &n
)) != 0)
1784 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1785 event
= (ssh_audit_event_t
)n
;
1787 case SSH_AUTH_FAIL_PUBKEY
:
1788 case SSH_AUTH_FAIL_HOSTBASED
:
1789 case SSH_AUTH_FAIL_GSSAPI
:
1790 case SSH_LOGIN_EXCEED_MAXTRIES
:
1791 case SSH_LOGIN_ROOT_DENIED
:
1792 case SSH_CONNECTION_CLOSE
:
1793 case SSH_INVALID_USER
:
1794 audit_event(ssh
, event
);
1797 fatal("Audit event type %d not permitted", event
);
1804 mm_answer_audit_command(struct ssh
*ssh
, int socket
, struct sshbuf
*m
)
1809 debug3("%s entering", __func__
);
1810 if ((r
= sshbuf_get_cstring(m
, &cmd
, NULL
)) != 0)
1811 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1812 /* sanity check command, if so how? */
1813 audit_run_command(cmd
);
1817 #endif /* SSH_AUDIT_EVENTS */
1820 monitor_clear_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1822 ssh_clear_newkeys(ssh
, MODE_IN
);
1823 ssh_clear_newkeys(ssh
, MODE_OUT
);
1824 sshbuf_free(child_state
);
1829 monitor_apply_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1834 debug3_f("packet_set_state");
1835 if ((r
= ssh_packet_set_state(ssh
, child_state
)) != 0)
1836 fatal_fr(r
, "packet_set_state");
1837 sshbuf_free(child_state
);
1839 if ((kex
= ssh
->kex
) == NULL
)
1840 fatal_f("internal error: ssh->kex == NULL");
1841 if (session_id2_len
!= sshbuf_len(ssh
->kex
->session_id
)) {
1842 fatal_f("incorrect session id length %zu (expected %u)",
1843 sshbuf_len(ssh
->kex
->session_id
), session_id2_len
);
1845 if (memcmp(sshbuf_ptr(ssh
->kex
->session_id
), session_id2
,
1846 session_id2_len
) != 0)
1847 fatal_f("session ID mismatch");
1848 /* XXX set callbacks */
1850 kex
->kex
[KEX_DH_GRP1_SHA1
] = kex_gen_server
;
1851 kex
->kex
[KEX_DH_GRP14_SHA1
] = kex_gen_server
;
1852 kex
->kex
[KEX_DH_GRP14_SHA256
] = kex_gen_server
;
1853 kex
->kex
[KEX_DH_GRP16_SHA512
] = kex_gen_server
;
1854 kex
->kex
[KEX_DH_GRP18_SHA512
] = kex_gen_server
;
1855 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1856 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1857 # ifdef OPENSSL_HAS_ECC
1858 kex
->kex
[KEX_ECDH_SHA2
] = kex_gen_server
;
1860 #endif /* WITH_OPENSSL */
1861 kex
->kex
[KEX_C25519_SHA256
] = kex_gen_server
;
1862 kex
->kex
[KEX_KEM_SNTRUP761X25519_SHA512
] = kex_gen_server
;
1863 kex
->kex
[KEX_KEM_MLKEM768X25519_SHA256
] = kex_gen_server
;
1864 kex
->load_host_public_key
=&get_hostkey_public_by_type
;
1865 kex
->load_host_private_key
=&get_hostkey_private_by_type
;
1866 kex
->host_key_index
=&get_hostkey_index
;
1867 kex
->sign
= sshd_hostkey_sign
;
1870 /* This function requires careful sanity checking */
1873 mm_get_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1875 debug3_f("Waiting for new keys");
1877 if ((child_state
= sshbuf_new()) == NULL
)
1878 fatal_f("sshbuf_new failed");
1879 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
,
1881 debug3_f("GOT new keys");
1887 #define FD_CLOSEONEXEC(x) do { \
1888 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1889 fatal("fcntl(%d, F_SETFD)", x); \
1893 monitor_openfds(struct monitor
*mon
, int do_logfds
)
1900 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1901 fatal_f("socketpair: %s", strerror(errno
));
1903 if (setsockopt(pair
[0], SOL_SOCKET
, SO_ZEROIZE
, &on
, sizeof(on
)) == -1)
1904 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno
));
1905 if (setsockopt(pair
[1], SOL_SOCKET
, SO_ZEROIZE
, &on
, sizeof(on
)) == -1)
1906 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno
));
1908 FD_CLOSEONEXEC(pair
[0]);
1909 FD_CLOSEONEXEC(pair
[1]);
1910 mon
->m_recvfd
= pair
[0];
1911 mon
->m_sendfd
= pair
[1];
1914 if (pipe(pair
) == -1)
1915 fatal_f("pipe: %s", strerror(errno
));
1916 FD_CLOSEONEXEC(pair
[0]);
1917 FD_CLOSEONEXEC(pair
[1]);
1918 mon
->m_log_recvfd
= pair
[0];
1919 mon
->m_log_sendfd
= pair
[1];
1921 mon
->m_log_recvfd
= mon
->m_log_sendfd
= -1;
1927 struct monitor
*mon
;
1929 mon
= xcalloc(1, sizeof(*mon
));
1930 monitor_openfds(mon
, 1);
1936 monitor_reinit(struct monitor
*mon
)
1938 monitor_openfds(mon
, 0);
1943 mm_answer_gss_setup_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1951 if (!options
.gss_authentication
)
1952 fatal_f("GSSAPI authentication not enabled");
1954 if ((r
= sshbuf_get_string(m
, &p
, &len
)) != 0)
1955 fatal_fr(r
, "parse");
1959 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1961 free(goid
.elements
);
1964 if ((r
= sshbuf_put_u32(m
, major
)) != 0)
1965 fatal_fr(r
, "assemble");
1967 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1969 /* Now we have a context, enable the step */
1970 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1976 mm_answer_gss_accept_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1979 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1980 OM_uint32 major
, minor
;
1981 OM_uint32 flags
= 0; /* GSI needs this */
1984 if (!options
.gss_authentication
)
1985 fatal_f("GSSAPI authentication not enabled");
1987 if ((r
= ssh_gssapi_get_buffer_desc(m
, &in
)) != 0)
1988 fatal_fr(r
, "ssh_gssapi_get_buffer_desc");
1989 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1993 if ((r
= sshbuf_put_u32(m
, major
)) != 0 ||
1994 (r
= sshbuf_put_string(m
, out
.value
, out
.length
)) != 0 ||
1995 (r
= sshbuf_put_u32(m
, flags
)) != 0)
1996 fatal_fr(r
, "assemble");
1997 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1999 gss_release_buffer(&minor
, &out
);
2001 if (major
== GSS_S_COMPLETE
) {
2002 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
2003 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2004 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
2010 mm_answer_gss_checkmic(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
2012 gss_buffer_desc gssbuf
, mic
;
2016 if (!options
.gss_authentication
)
2017 fatal_f("GSSAPI authentication not enabled");
2019 if ((r
= ssh_gssapi_get_buffer_desc(m
, &gssbuf
)) != 0 ||
2020 (r
= ssh_gssapi_get_buffer_desc(m
, &mic
)) != 0)
2021 fatal_fr(r
, "ssh_gssapi_get_buffer_desc");
2023 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
2029 if ((r
= sshbuf_put_u32(m
, ret
)) != 0)
2030 fatal_fr(r
, "assemble");
2032 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
2034 if (!GSS_ERROR(ret
))
2035 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
2041 mm_answer_gss_userok(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
2043 int r
, authenticated
;
2044 const char *displayname
;
2046 if (!options
.gss_authentication
)
2047 fatal_f("GSSAPI authentication not enabled");
2049 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
2052 if ((r
= sshbuf_put_u32(m
, authenticated
)) != 0)
2053 fatal_fr(r
, "assemble");
2055 debug3_f("sending result %d", authenticated
);
2056 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
2058 auth_method
= "gssapi-with-mic";
2060 if ((displayname
= ssh_gssapi_displayname()) != NULL
)
2061 auth2_record_info(authctxt
, "%s", displayname
);
2063 /* Monitor loop will terminate if authenticated */
2064 return (authenticated
);