1 /* $OpenBSD: monitor.c,v 1.234 2022/06/15 16:08:25 djm 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"
101 static Gssctxt
*gsscontext
= NULL
;
105 extern ServerOptions options
;
106 extern u_int utmp_len
;
107 extern struct sshbuf
*loginmsg
;
108 extern struct sshauthopt
*auth_opts
; /* XXX move to permanent ssh->authctxt? */
110 /* State exported from the child */
111 static struct sshbuf
*child_state
;
113 /* Functions on the monitor that answer unprivileged requests */
115 int mm_answer_moduli(struct ssh
*, int, struct sshbuf
*);
116 int mm_answer_sign(struct ssh
*, int, struct sshbuf
*);
117 int mm_answer_pwnamallow(struct ssh
*, int, struct sshbuf
*);
118 int mm_answer_auth2_read_banner(struct ssh
*, int, struct sshbuf
*);
119 int mm_answer_authserv(struct ssh
*, int, struct sshbuf
*);
120 int mm_answer_authpassword(struct ssh
*, int, struct sshbuf
*);
121 int mm_answer_bsdauthquery(struct ssh
*, int, struct sshbuf
*);
122 int mm_answer_bsdauthrespond(struct ssh
*, int, struct sshbuf
*);
123 int mm_answer_keyallowed(struct ssh
*, int, struct sshbuf
*);
124 int mm_answer_keyverify(struct ssh
*, int, struct sshbuf
*);
125 int mm_answer_pty(struct ssh
*, int, struct sshbuf
*);
126 int mm_answer_pty_cleanup(struct ssh
*, int, struct sshbuf
*);
127 int mm_answer_term(struct ssh
*, int, struct sshbuf
*);
128 int mm_answer_rsa_keyallowed(struct ssh
*, int, struct sshbuf
*);
129 int mm_answer_rsa_challenge(struct ssh
*, int, struct sshbuf
*);
130 int mm_answer_rsa_response(struct ssh
*, int, struct sshbuf
*);
131 int mm_answer_sesskey(struct ssh
*, int, struct sshbuf
*);
132 int mm_answer_sessid(struct ssh
*, int, struct sshbuf
*);
135 int mm_answer_pam_start(struct ssh
*, int, struct sshbuf
*);
136 int mm_answer_pam_account(struct ssh
*, int, struct sshbuf
*);
137 int mm_answer_pam_init_ctx(struct ssh
*, int, struct sshbuf
*);
138 int mm_answer_pam_query(struct ssh
*, int, struct sshbuf
*);
139 int mm_answer_pam_respond(struct ssh
*, int, struct sshbuf
*);
140 int mm_answer_pam_free_ctx(struct ssh
*, int, struct sshbuf
*);
144 int mm_answer_gss_setup_ctx(struct ssh
*, int, struct sshbuf
*);
145 int mm_answer_gss_accept_ctx(struct ssh
*, int, struct sshbuf
*);
146 int mm_answer_gss_userok(struct ssh
*, int, struct sshbuf
*);
147 int mm_answer_gss_checkmic(struct ssh
*, int, struct sshbuf
*);
150 #ifdef SSH_AUDIT_EVENTS
151 int mm_answer_audit_event(struct ssh
*, int, struct sshbuf
*);
152 int mm_answer_audit_command(struct ssh
*, int, struct sshbuf
*);
155 static Authctxt
*authctxt
;
157 /* local state for key verify */
158 static u_char
*key_blob
= NULL
;
159 static size_t key_bloblen
= 0;
160 static u_int key_blobtype
= MM_NOKEY
;
161 static struct sshauthopt
*key_opts
= NULL
;
162 static char *hostbased_cuser
= NULL
;
163 static char *hostbased_chost
= NULL
;
164 static char *auth_method
= "unknown";
165 static char *auth_submethod
= NULL
;
166 static u_int session_id2_len
= 0;
167 static u_char
*session_id2
= NULL
;
168 static pid_t monitor_child_pid
;
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
[] = {
191 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
193 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
194 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
195 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
196 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
197 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
199 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
200 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
201 {MONITOR_REQ_PAM_INIT_CTX
, MON_ONCE
, mm_answer_pam_init_ctx
},
202 {MONITOR_REQ_PAM_QUERY
, 0, mm_answer_pam_query
},
203 {MONITOR_REQ_PAM_RESPOND
, MON_ONCE
, mm_answer_pam_respond
},
204 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
206 #ifdef SSH_AUDIT_EVENTS
207 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
210 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
211 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
213 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
214 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
216 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
217 {MONITOR_REQ_GSSSTEP
, 0, mm_answer_gss_accept_ctx
},
218 {MONITOR_REQ_GSSUSEROK
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_gss_userok
},
219 {MONITOR_REQ_GSSCHECKMIC
, MON_ONCE
, mm_answer_gss_checkmic
},
224 struct mon_table mon_dispatch_postauth20
[] = {
226 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
228 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
229 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
230 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
231 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
232 #ifdef SSH_AUDIT_EVENTS
233 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
234 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
239 struct mon_table
*mon_dispatch
;
241 /* Specifies if a certain message is allowed at the moment */
243 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
245 while (ent
->f
!= NULL
) {
246 if (ent
->type
== type
) {
247 ent
->flags
&= ~MON_PERMIT
;
248 ent
->flags
|= permit
? MON_PERMIT
: 0;
256 monitor_permit_authentications(int permit
)
258 struct mon_table
*ent
= mon_dispatch
;
260 while (ent
->f
!= NULL
) {
261 if (ent
->flags
& MON_AUTH
) {
262 ent
->flags
&= ~MON_PERMIT
;
263 ent
->flags
|= permit
? MON_PERMIT
: 0;
270 monitor_child_preauth(struct ssh
*ssh
, struct monitor
*pmonitor
)
272 struct mon_table
*ent
;
273 int authenticated
= 0, partial
= 0;
275 debug3("preauth child monitor started");
277 if (pmonitor
->m_recvfd
>= 0)
278 close(pmonitor
->m_recvfd
);
279 if (pmonitor
->m_log_sendfd
>= 0)
280 close(pmonitor
->m_log_sendfd
);
281 pmonitor
->m_log_sendfd
= pmonitor
->m_recvfd
= -1;
283 authctxt
= (Authctxt
*)ssh
->authctxt
;
284 memset(authctxt
, 0, sizeof(*authctxt
));
285 ssh
->authctxt
= authctxt
;
287 authctxt
->loginmsg
= loginmsg
;
289 mon_dispatch
= mon_dispatch_proto20
;
290 /* Permit requests for moduli and signatures */
291 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
292 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
294 /* The first few requests do not require asynchronous access */
295 while (!authenticated
) {
297 auth_method
= "unknown";
298 auth_submethod
= NULL
;
299 auth2_authctxt_reset_info(authctxt
);
301 authenticated
= (monitor_read(ssh
, pmonitor
,
302 mon_dispatch
, &ent
) == 1);
304 /* Special handling for multiple required authentications */
305 if (options
.num_auth_methods
!= 0) {
307 !auth2_update_methods_lists(authctxt
,
308 auth_method
, auth_submethod
)) {
309 debug3_f("method %s: partial", auth_method
);
316 if (!(ent
->flags
& MON_AUTHDECIDE
))
317 fatal_f("unexpected authentication from %d",
319 if (authctxt
->pw
->pw_uid
== 0 &&
320 !auth_root_allowed(ssh
, auth_method
))
323 /* PAM needs to perform account checks after auth */
324 if (options
.use_pam
&& authenticated
) {
327 if ((m
= sshbuf_new()) == NULL
)
328 fatal("%s: sshbuf_new failed",
330 mm_request_receive_expect(pmonitor
->m_sendfd
,
331 MONITOR_REQ_PAM_ACCOUNT
, m
);
332 authenticated
= mm_answer_pam_account(
333 ssh
, pmonitor
->m_sendfd
, m
);
338 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
339 auth_log(ssh
, authenticated
, partial
,
340 auth_method
, auth_submethod
);
341 if (!partial
&& !authenticated
)
342 authctxt
->failures
++;
343 if (authenticated
|| partial
) {
344 auth2_update_session_info(authctxt
,
345 auth_method
, auth_submethod
);
350 if (!authctxt
->valid
)
351 fatal_f("authenticated invalid user");
352 if (strcmp(auth_method
, "unknown") == 0)
353 fatal_f("authentication method name unknown");
355 debug_f("user %s authenticated by privileged process", authctxt
->user
);
356 ssh
->authctxt
= NULL
;
357 ssh_packet_set_log_preamble(ssh
, "user %s", authctxt
->user
);
359 mm_get_keystate(ssh
, pmonitor
);
361 /* Drain any buffered messages from the child */
362 while (pmonitor
->m_log_recvfd
!= -1 && monitor_read_log(pmonitor
) == 0)
365 if (pmonitor
->m_recvfd
>= 0)
366 close(pmonitor
->m_recvfd
);
367 if (pmonitor
->m_log_sendfd
>= 0)
368 close(pmonitor
->m_log_sendfd
);
369 pmonitor
->m_sendfd
= pmonitor
->m_log_recvfd
= -1;
373 monitor_set_child_handler(pid_t pid
)
375 monitor_child_pid
= pid
;
379 monitor_child_handler(int sig
)
381 kill(monitor_child_pid
, sig
);
385 monitor_child_postauth(struct ssh
*ssh
, struct monitor
*pmonitor
)
387 close(pmonitor
->m_recvfd
);
388 pmonitor
->m_recvfd
= -1;
390 monitor_set_child_handler(pmonitor
->m_pid
);
391 ssh_signal(SIGHUP
, &monitor_child_handler
);
392 ssh_signal(SIGTERM
, &monitor_child_handler
);
393 ssh_signal(SIGINT
, &monitor_child_handler
);
395 ssh_signal(SIGXFSZ
, SIG_IGN
);
398 mon_dispatch
= mon_dispatch_postauth20
;
400 /* Permit requests for moduli and signatures */
401 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
402 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
403 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
405 if (auth_opts
->permit_pty_flag
) {
406 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
407 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
411 monitor_read(ssh
, pmonitor
, mon_dispatch
, NULL
);
415 monitor_read_log(struct monitor
*pmonitor
)
417 struct sshbuf
*logmsg
;
418 u_int len
, level
, forced
;
423 if ((logmsg
= sshbuf_new()) == NULL
)
424 fatal_f("sshbuf_new");
427 if ((r
= sshbuf_reserve(logmsg
, 4, &p
)) != 0)
428 fatal_fr(r
, "reserve len");
429 if (atomicio(read
, pmonitor
->m_log_recvfd
, p
, 4) != 4) {
430 if (errno
== EPIPE
) {
432 debug_f("child log fd closed");
433 close(pmonitor
->m_log_recvfd
);
434 pmonitor
->m_log_recvfd
= -1;
437 fatal_f("log fd read: %s", strerror(errno
));
439 if ((r
= sshbuf_get_u32(logmsg
, &len
)) != 0)
440 fatal_fr(r
, "parse len");
441 if (len
<= 4 || len
> 8192)
442 fatal_f("invalid log message length %u", len
);
444 /* Read severity, message */
445 sshbuf_reset(logmsg
);
446 if ((r
= sshbuf_reserve(logmsg
, len
, &p
)) != 0)
447 fatal_fr(r
, "reserve msg");
448 if (atomicio(read
, pmonitor
->m_log_recvfd
, p
, len
) != len
)
449 fatal_f("log fd read: %s", strerror(errno
));
450 if ((r
= sshbuf_get_u32(logmsg
, &level
)) != 0 ||
451 (r
= sshbuf_get_u32(logmsg
, &forced
)) != 0 ||
452 (r
= sshbuf_get_cstring(logmsg
, &msg
, NULL
)) != 0)
453 fatal_fr(r
, "parse");
456 if (log_level_name(level
) == NULL
)
457 fatal_f("invalid log level %u (corrupted message?)", level
);
458 sshlogdirect(level
, forced
, "%s [preauth]", msg
);
467 monitor_read(struct ssh
*ssh
, struct monitor
*pmonitor
, struct mon_table
*ent
,
468 struct mon_table
**pent
)
473 struct pollfd pfd
[2];
476 memset(&pfd
, 0, sizeof(pfd
));
477 pfd
[0].fd
= pmonitor
->m_sendfd
;
478 pfd
[0].events
= POLLIN
;
479 pfd
[1].fd
= pmonitor
->m_log_recvfd
;
480 pfd
[1].events
= pfd
[1].fd
== -1 ? 0 : POLLIN
;
481 if (poll(pfd
, pfd
[1].fd
== -1 ? 1 : 2, -1) == -1) {
482 if (errno
== EINTR
|| errno
== EAGAIN
)
484 fatal_f("poll: %s", strerror(errno
));
486 if (pfd
[1].revents
) {
488 * Drain all log messages before processing next
491 monitor_read_log(pmonitor
);
495 break; /* Continues below */
498 if ((m
= sshbuf_new()) == NULL
)
499 fatal_f("sshbuf_new");
501 mm_request_receive(pmonitor
->m_sendfd
, m
);
502 if ((r
= sshbuf_get_u8(m
, &type
)) != 0)
503 fatal_fr(r
, "parse type");
505 debug3_f("checking request %d", type
);
507 while (ent
->f
!= NULL
) {
508 if (ent
->type
== type
)
513 if (ent
->f
!= NULL
) {
514 if (!(ent
->flags
& MON_PERMIT
))
515 fatal_f("unpermitted request %d", type
);
516 ret
= (*ent
->f
)(ssh
, pmonitor
->m_sendfd
, m
);
519 /* The child may use this request only once, disable it */
520 if (ent
->flags
& MON_ONCE
) {
521 debug2_f("%d used once, disabling now", type
);
522 ent
->flags
&= ~MON_PERMIT
;
531 fatal_f("unsupported request: %d", type
);
537 /* allowed key state */
539 monitor_allowed_key(const u_char
*blob
, u_int bloblen
)
541 /* make sure key is allowed */
542 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
543 timingsafe_bcmp(key_blob
, blob
, key_bloblen
))
549 monitor_reset_key_state(void)
553 free(hostbased_cuser
);
554 free(hostbased_chost
);
555 sshauthopt_free(key_opts
);
558 key_blobtype
= MM_NOKEY
;
560 hostbased_cuser
= NULL
;
561 hostbased_chost
= NULL
;
566 mm_answer_moduli(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
569 const BIGNUM
*dh_p
, *dh_g
;
571 u_int min
, want
, max
;
573 if ((r
= sshbuf_get_u32(m
, &min
)) != 0 ||
574 (r
= sshbuf_get_u32(m
, &want
)) != 0 ||
575 (r
= sshbuf_get_u32(m
, &max
)) != 0)
576 fatal_fr(r
, "parse");
578 debug3_f("got parameters: %d %d %d", min
, want
, max
);
579 /* We need to check here, too, in case the child got corrupted */
580 if (max
< min
|| want
< min
|| max
< want
)
581 fatal_f("bad parameters: %d %d %d", min
, want
, max
);
585 dh
= choose_dh(min
, want
, max
);
587 if ((r
= sshbuf_put_u8(m
, 0)) != 0)
588 fatal_fr(r
, "assemble empty");
591 /* Send first bignum */
592 DH_get0_pqg(dh
, &dh_p
, NULL
, &dh_g
);
593 if ((r
= sshbuf_put_u8(m
, 1)) != 0 ||
594 (r
= sshbuf_put_bignum2(m
, dh_p
)) != 0 ||
595 (r
= sshbuf_put_bignum2(m
, dh_g
)) != 0)
596 fatal_fr(r
, "assemble");
600 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
606 mm_answer_sign(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
608 extern int auth_sock
; /* XXX move to state struct? */
610 struct sshbuf
*sigbuf
= NULL
;
611 u_char
*p
= NULL
, *signature
= NULL
;
613 size_t datlen
, siglen
, alglen
;
616 const char proof_req
[] = "hostkeys-prove-00@openssh.com";
618 debug3_f("entering");
620 if ((r
= sshbuf_get_u32(m
, &keyid
)) != 0 ||
621 (r
= sshbuf_get_string(m
, &p
, &datlen
)) != 0 ||
622 (r
= sshbuf_get_cstring(m
, &alg
, &alglen
)) != 0 ||
623 (r
= sshbuf_get_u32(m
, &compat
)) != 0)
624 fatal_fr(r
, "parse");
626 fatal_f("invalid key ID");
629 * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
630 * SHA384 (48 bytes) and SHA512 (64 bytes).
632 * Otherwise, verify the signature request is for a hostkey
635 * XXX perform similar check for KEX signature requests too?
636 * it's not trivial, since what is signed is the hash, rather
637 * than the full kex structure...
639 if (datlen
!= 20 && datlen
!= 32 && datlen
!= 48 && datlen
!= 64) {
641 * Construct expected hostkey proof and compare it to what
642 * the client sent us.
644 if (session_id2_len
== 0) /* hostkeys is never first */
645 fatal_f("bad data length: %zu", datlen
);
646 if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) == NULL
)
647 fatal_f("no hostkey for index %d", keyid
);
648 if ((sigbuf
= sshbuf_new()) == NULL
)
649 fatal_f("sshbuf_new");
650 if ((r
= sshbuf_put_cstring(sigbuf
, proof_req
)) != 0 ||
651 (r
= sshbuf_put_string(sigbuf
, session_id2
,
652 session_id2_len
)) != 0 ||
653 (r
= sshkey_puts(key
, sigbuf
)) != 0)
654 fatal_fr(r
, "assemble private key proof");
655 if (datlen
!= sshbuf_len(sigbuf
) ||
656 memcmp(p
, sshbuf_ptr(sigbuf
), sshbuf_len(sigbuf
)) != 0)
657 fatal_f("bad data length: %zu, hostkey proof len %zu",
658 datlen
, sshbuf_len(sigbuf
));
663 /* save session id, it will be passed on the first call */
664 if (session_id2_len
== 0) {
665 session_id2_len
= datlen
;
666 session_id2
= xmalloc(session_id2_len
);
667 memcpy(session_id2
, p
, session_id2_len
);
670 if ((key
= get_hostkey_by_index(keyid
)) != NULL
) {
671 if ((r
= sshkey_sign(key
, &signature
, &siglen
, p
, datlen
, alg
,
672 options
.sk_provider
, NULL
, compat
)) != 0)
674 } else if ((key
= get_hostkey_public_by_index(keyid
, ssh
)) != NULL
&&
676 if ((r
= ssh_agent_sign(auth_sock
, key
, &signature
, &siglen
,
677 p
, datlen
, alg
, compat
)) != 0)
678 fatal_fr(r
, "agent sign");
680 fatal_f("no hostkey from index %d", keyid
);
682 debug3_f("%s %s signature len=%zu", alg
,
683 is_proof
? "hostkey proof" : "KEX", siglen
);
686 if ((r
= sshbuf_put_string(m
, signature
, siglen
)) != 0)
687 fatal_fr(r
, "assemble");
693 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
695 /* Turn on permissions for getpwnam */
696 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
701 #define PUTPW(b, id) \
703 if ((r = sshbuf_put_string(b, \
704 &pwent->id, sizeof(pwent->id))) != 0) \
705 fatal_fr(r, "assemble %s", #id); \
708 /* Retrieves the password entry and also checks if the user is permitted */
710 mm_answer_pwnamallow(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
712 struct passwd
*pwent
;
716 debug3_f("entering");
718 if (authctxt
->attempt
++ != 0)
719 fatal_f("multiple attempts for getpwnam");
721 if ((r
= sshbuf_get_cstring(m
, &authctxt
->user
, NULL
)) != 0)
722 fatal_fr(r
, "parse");
724 pwent
= getpwnamallow(ssh
, authctxt
->user
);
726 setproctitle("%s [priv]", pwent
? authctxt
->user
: "unknown");
731 if ((r
= sshbuf_put_u8(m
, 0)) != 0)
732 fatal_fr(r
, "assemble fakepw");
733 authctxt
->pw
= fakepw();
738 authctxt
->pw
= pwent
;
741 /* XXX send fake class/dir/shell, etc. */
742 if ((r
= sshbuf_put_u8(m
, 1)) != 0)
743 fatal_fr(r
, "assemble ok");
746 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
749 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
752 if ((r
= sshbuf_put_cstring(m
, pwent
->pw_name
)) != 0 ||
753 (r
= sshbuf_put_cstring(m
, "*")) != 0 ||
754 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
755 (r
= sshbuf_put_cstring(m
, pwent
->pw_gecos
)) != 0 ||
757 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
758 (r
= sshbuf_put_cstring(m
, pwent
->pw_class
)) != 0 ||
760 (r
= sshbuf_put_cstring(m
, pwent
->pw_dir
)) != 0 ||
761 (r
= sshbuf_put_cstring(m
, pwent
->pw_shell
)) != 0)
762 fatal_fr(r
, "assemble pw");
765 ssh_packet_set_log_preamble(ssh
, "%suser %s",
766 authctxt
->valid
? "authenticating" : "invalid ", authctxt
->user
);
767 if ((r
= sshbuf_put_string(m
, &options
, sizeof(options
))) != 0)
768 fatal_fr(r
, "assemble options");
770 #define M_CP_STROPT(x) do { \
771 if (options.x != NULL && \
772 (r = sshbuf_put_cstring(m, options.x)) != 0) \
773 fatal_fr(r, "assemble %s", #x); \
775 #define M_CP_STRARRAYOPT(x, nx) do { \
776 for (i = 0; i < options.nx; i++) { \
777 if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
778 fatal_fr(r, "assemble %s", #x); \
781 /* See comment in servconf.h */
782 COPY_MATCH_STRING_OPTS();
784 #undef M_CP_STRARRAYOPT
786 /* Create valid auth method lists */
787 if (auth2_setup_methods_lists(authctxt
) != 0) {
789 * The monitor will continue long enough to let the child
790 * run to its packet_disconnect(), but it must not allow any
791 * authentication to succeed.
793 debug_f("no valid authentication method lists");
796 debug3_f("sending MONITOR_ANS_PWNAM: %d", allowed
);
797 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
799 /* Allow service/style information on the auth context */
800 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
801 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
805 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
811 int mm_answer_auth2_read_banner(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
817 banner
= auth2_read_banner();
818 if ((r
= sshbuf_put_cstring(m
, banner
!= NULL
? banner
: "")) != 0)
819 fatal_fr(r
, "assemble");
820 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
827 mm_answer_authserv(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
831 monitor_permit_authentications(1);
833 if ((r
= sshbuf_get_cstring(m
, &authctxt
->service
, NULL
)) != 0 ||
834 (r
= sshbuf_get_cstring(m
, &authctxt
->style
, NULL
)) != 0)
835 fatal_fr(r
, "parse");
836 debug3_f("service=%s, style=%s", authctxt
->service
, authctxt
->style
);
838 if (strlen(authctxt
->style
) == 0) {
839 free(authctxt
->style
);
840 authctxt
->style
= NULL
;
847 * Check that the key type appears in the supplied pattern list, ignoring
848 * mismatches in the signature algorithm. (Signature algorithm checks are
849 * performed in the unprivileged authentication code).
850 * Returns 1 on success, 0 otherwise.
853 key_base_type_match(const char *method
, const struct sshkey
*key
,
856 char *s
, *l
, *ol
= xstrdup(list
);
860 for ((s
= strsep(&l
, ",")); s
&& *s
!= '\0'; (s
= strsep(&l
, ","))) {
861 if (sshkey_type_from_name(s
) == key
->type
) {
867 error("%s key type %s is not in permitted list %s", method
,
868 sshkey_ssh_name(key
), list
);
876 mm_answer_authpassword(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
878 static int call_count
;
880 int r
, authenticated
;
883 if (!options
.password_authentication
)
884 fatal_f("password authentication not enabled");
885 if ((r
= sshbuf_get_cstring(m
, &passwd
, &plen
)) != 0)
886 fatal_fr(r
, "parse");
887 /* Only authenticate if the context is valid */
888 authenticated
= options
.password_authentication
&&
889 auth_password(ssh
, passwd
);
890 freezero(passwd
, plen
);
893 if ((r
= sshbuf_put_u32(m
, authenticated
)) != 0)
894 fatal_fr(r
, "assemble");
896 if ((r
= sshbuf_put_u32(m
, sshpam_get_maxtries_reached())) != 0)
897 fatal_fr(r
, "assemble PAM");
900 debug3("%s: sending result %d", __func__
, authenticated
);
901 debug3_f("sending result %d", authenticated
);
902 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
905 if (plen
== 0 && call_count
== 1)
906 auth_method
= "none";
908 auth_method
= "password";
910 /* Causes monitor loop to terminate if authenticated */
911 return (authenticated
);
916 mm_answer_bsdauthquery(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
918 char *name
, *infotxt
;
919 u_int numprompts
, *echo_on
, success
;
923 if (!options
.kbd_interactive_authentication
)
924 fatal_f("kbd-int authentication not enabled");
925 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
926 &prompts
, &echo_on
) < 0 ? 0 : 1;
929 if ((r
= sshbuf_put_u32(m
, success
)) != 0)
930 fatal_fr(r
, "assemble");
932 if ((r
= sshbuf_put_cstring(m
, prompts
[0])) != 0)
933 fatal_fr(r
, "assemble prompt");
936 debug3_f("sending challenge success: %u", success
);
937 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
950 mm_answer_bsdauthrespond(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
955 if (!options
.kbd_interactive_authentication
)
956 fatal_f("kbd-int authentication not enabled");
957 if (authctxt
->as
== NULL
)
958 fatal_f("no bsd auth session");
960 if ((r
= sshbuf_get_cstring(m
, &response
, NULL
)) != 0)
961 fatal_fr(r
, "parse");
962 authok
= options
.kbd_interactive_authentication
&&
963 auth_userresponse(authctxt
->as
, response
, 0);
965 debug3_f("<%s> = <%d>", response
, authok
);
969 if ((r
= sshbuf_put_u32(m
, authok
)) != 0)
970 fatal_fr(r
, "assemble");
972 debug3_f("sending authenticated: %d", authok
);
973 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
975 auth_method
= "keyboard-interactive";
976 auth_submethod
= "bsdauth";
978 return (authok
!= 0);
984 mm_answer_pam_start(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
986 if (!options
.use_pam
)
987 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
991 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
992 if (options
.kbd_interactive_authentication
)
993 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_INIT_CTX
, 1);
999 mm_answer_pam_account(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1004 if (!options
.use_pam
)
1005 fatal("%s: PAM not enabled", __func__
);
1007 ret
= do_pam_account();
1009 if ((r
= sshbuf_put_u32(m
, ret
)) != 0 ||
1010 (r
= sshbuf_put_stringb(m
, loginmsg
)) != 0)
1011 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1013 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
1018 static void *sshpam_ctxt
, *sshpam_authok
;
1019 extern KbdintDevice sshpam_device
;
1022 mm_answer_pam_init_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1027 debug3("%s", __func__
);
1028 if (!options
.kbd_interactive_authentication
)
1029 fatal("%s: kbd-int authentication not enabled", __func__
);
1030 if (sshpam_ctxt
!= NULL
)
1031 fatal("%s: already called", __func__
);
1032 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
1033 sshpam_authok
= NULL
;
1035 if (sshpam_ctxt
!= NULL
) {
1036 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
1037 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_QUERY
, 1);
1040 if ((r
= sshbuf_put_u32(m
, ok
)) != 0)
1041 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1042 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
1047 mm_answer_pam_query(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1049 char *name
= NULL
, *info
= NULL
, **prompts
= NULL
;
1050 u_int i
, num
= 0, *echo_on
= 0;
1053 debug3("%s", __func__
);
1054 sshpam_authok
= NULL
;
1055 if (sshpam_ctxt
== NULL
)
1056 fatal("%s: no context", __func__
);
1057 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
,
1058 &num
, &prompts
, &echo_on
);
1059 if (ret
== 0 && num
== 0)
1060 sshpam_authok
= sshpam_ctxt
;
1061 if (num
> 1 || name
== NULL
|| info
== NULL
)
1062 fatal("sshpam_device.query failed");
1063 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_RESPOND
, 1);
1065 if ((r
= sshbuf_put_u32(m
, ret
)) != 0 ||
1066 (r
= sshbuf_put_cstring(m
, name
)) != 0 ||
1067 (r
= sshbuf_put_cstring(m
, info
)) != 0 ||
1068 (r
= sshbuf_put_u32(m
, sshpam_get_maxtries_reached())) != 0 ||
1069 (r
= sshbuf_put_u32(m
, num
)) != 0)
1070 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1073 for (i
= 0; i
< num
; ++i
) {
1074 if ((r
= sshbuf_put_cstring(m
, prompts
[i
])) != 0 ||
1075 (r
= sshbuf_put_u32(m
, echo_on
[i
])) != 0)
1076 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1081 auth_method
= "keyboard-interactive";
1082 auth_submethod
= "pam";
1083 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
1088 mm_answer_pam_respond(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1094 debug3("%s", __func__
);
1095 if (sshpam_ctxt
== NULL
)
1096 fatal("%s: no context", __func__
);
1097 sshpam_authok
= NULL
;
1098 if ((r
= sshbuf_get_u32(m
, &num
)) != 0)
1099 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1101 resp
= xcalloc(num
, sizeof(char *));
1102 for (i
= 0; i
< num
; ++i
) {
1103 if ((r
= sshbuf_get_cstring(m
, &(resp
[i
]), NULL
)) != 0)
1104 fatal("%s: buffer error: %s",
1105 __func__
, ssh_err(r
));
1107 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
1108 for (i
= 0; i
< num
; ++i
)
1112 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
1115 if ((r
= sshbuf_put_u32(m
, ret
)) != 0)
1116 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1117 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
1118 auth_method
= "keyboard-interactive";
1119 auth_submethod
= "pam";
1121 sshpam_authok
= sshpam_ctxt
;
1126 mm_answer_pam_free_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1128 int r
= sshpam_authok
!= NULL
&& sshpam_authok
== sshpam_ctxt
;
1130 debug3("%s", __func__
);
1131 if (sshpam_ctxt
== NULL
)
1132 fatal("%s: no context", __func__
);
1133 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
1134 sshpam_ctxt
= sshpam_authok
= NULL
;
1136 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
1137 /* Allow another attempt */
1138 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_INIT_CTX
, 1);
1139 auth_method
= "keyboard-interactive";
1140 auth_submethod
= "pam";
1146 mm_answer_keyallowed(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1148 struct sshkey
*key
= NULL
;
1149 char *cuser
, *chost
;
1150 u_int pubkey_auth_attempt
;
1153 struct sshauthopt
*opts
= NULL
;
1155 debug3_f("entering");
1156 if ((r
= sshbuf_get_u32(m
, &type
)) != 0 ||
1157 (r
= sshbuf_get_cstring(m
, &cuser
, NULL
)) != 0 ||
1158 (r
= sshbuf_get_cstring(m
, &chost
, NULL
)) != 0 ||
1159 (r
= sshkey_froms(m
, &key
)) != 0 ||
1160 (r
= sshbuf_get_u32(m
, &pubkey_auth_attempt
)) != 0)
1161 fatal_fr(r
, "parse");
1163 if (key
!= NULL
&& authctxt
->valid
) {
1164 /* These should not make it past the privsep child */
1165 if (sshkey_type_plain(key
->type
) == KEY_RSA
&&
1166 (ssh
->compat
& SSH_BUG_RSASIGMD5
) != 0)
1167 fatal_f("passed a SSH_BUG_RSASIGMD5 key");
1171 auth_method
= "publickey";
1172 if (!options
.pubkey_authentication
)
1174 if (auth2_key_already_used(authctxt
, key
))
1176 if (!key_base_type_match(auth_method
, key
,
1177 options
.pubkey_accepted_algos
))
1179 allowed
= user_key_allowed(ssh
, authctxt
->pw
, key
,
1180 pubkey_auth_attempt
, &opts
);
1183 auth_method
= "hostbased";
1184 if (!options
.hostbased_authentication
)
1186 if (auth2_key_already_used(authctxt
, key
))
1188 if (!key_base_type_match(auth_method
, key
,
1189 options
.hostbased_accepted_algos
))
1191 allowed
= hostbased_key_allowed(ssh
, authctxt
->pw
,
1193 auth2_record_info(authctxt
,
1194 "client user \"%.100s\", client host \"%.100s\"",
1198 fatal_f("unknown key type %u", type
);
1203 debug3_f("%s authentication%s: %s key is %s", auth_method
,
1204 pubkey_auth_attempt
? "" : " test",
1205 (key
== NULL
|| !authctxt
->valid
) ? "invalid" : sshkey_type(key
),
1206 allowed
? "allowed" : "not allowed");
1208 auth2_record_key(authctxt
, 0, key
);
1210 /* clear temporarily storage (used by verify) */
1211 monitor_reset_key_state();
1214 /* Save temporarily for comparison in verify */
1215 if ((r
= sshkey_to_blob(key
, &key_blob
, &key_bloblen
)) != 0)
1216 fatal_fr(r
, "sshkey_to_blob");
1217 key_blobtype
= type
;
1219 hostbased_cuser
= cuser
;
1220 hostbased_chost
= chost
;
1222 /* Log failed attempt */
1223 auth_log(ssh
, 0, 0, auth_method
, NULL
);
1230 if ((r
= sshbuf_put_u32(m
, allowed
)) != 0)
1231 fatal_fr(r
, "assemble");
1232 if (opts
!= NULL
&& (r
= sshauthopt_serialise(opts
, m
, 1)) != 0)
1233 fatal_fr(r
, "sshauthopt_serialise");
1234 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1237 sshauthopt_free(opts
);
1243 monitor_valid_userblob(struct ssh
*ssh
, const u_char
*data
, u_int datalen
)
1246 struct sshkey
*hostkey
= NULL
;
1248 char *userstyle
, *cp
;
1251 int hostbound
= 0, r
, fail
= 0;
1253 if ((b
= sshbuf_from(data
, datalen
)) == NULL
)
1254 fatal_f("sshbuf_from");
1256 if (ssh
->compat
& SSH_OLD_SESSIONID
) {
1258 len
= sshbuf_len(b
);
1259 if ((session_id2
== NULL
) ||
1260 (len
< session_id2_len
) ||
1261 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1263 if ((r
= sshbuf_consume(b
, session_id2_len
)) != 0)
1264 fatal_fr(r
, "consume");
1266 if ((r
= sshbuf_get_string_direct(b
, &p
, &len
)) != 0)
1267 fatal_fr(r
, "parse sessionid");
1268 if ((session_id2
== NULL
) ||
1269 (len
!= session_id2_len
) ||
1270 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1273 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1274 fatal_fr(r
, "parse type");
1275 if (type
!= SSH2_MSG_USERAUTH_REQUEST
)
1277 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1278 fatal_fr(r
, "parse userstyle");
1279 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1280 authctxt
->style
? ":" : "",
1281 authctxt
->style
? authctxt
->style
: "");
1282 if (strcmp(userstyle
, cp
) != 0) {
1283 logit("wrong user name passed to monitor: "
1284 "expected %s != %.100s", userstyle
, cp
);
1289 if ((r
= sshbuf_skip_string(b
)) != 0 || /* service */
1290 (r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1291 fatal_fr(r
, "parse method");
1292 if (strcmp("publickey", cp
) != 0) {
1293 if (strcmp("publickey-hostbound-v00@openssh.com", cp
) == 0)
1299 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1300 fatal_fr(r
, "parse pktype");
1303 if ((r
= sshbuf_skip_string(b
)) != 0 || /* pkalg */
1304 (r
= sshbuf_skip_string(b
)) != 0 || /* pkblob */
1305 (hostbound
&& (r
= sshkey_froms(b
, &hostkey
)) != 0))
1306 fatal_fr(r
, "parse pk");
1307 if (sshbuf_len(b
) != 0)
1310 if (hostkey
!= NULL
) {
1312 * Ensure this is actually one of our hostkeys; unfortunately
1313 * can't check ssh->kex->initial_hostkey directly at this point
1314 * as packet state has not yet been exported to monitor.
1316 if (get_hostkey_index(hostkey
, 1, ssh
) == -1)
1317 fatal_f("hostbound hostkey does not match");
1318 sshkey_free(hostkey
);
1324 monitor_valid_hostbasedblob(const u_char
*data
, u_int datalen
,
1325 const char *cuser
, const char *chost
)
1329 char *cp
, *userstyle
;
1334 if ((b
= sshbuf_from(data
, datalen
)) == NULL
)
1335 fatal_f("sshbuf_new");
1336 if ((r
= sshbuf_get_string_direct(b
, &p
, &len
)) != 0)
1337 fatal_fr(r
, "parse sessionid");
1339 if ((session_id2
== NULL
) ||
1340 (len
!= session_id2_len
) ||
1341 (timingsafe_bcmp(p
, session_id2
, session_id2_len
) != 0))
1344 if ((r
= sshbuf_get_u8(b
, &type
)) != 0)
1345 fatal_fr(r
, "parse type");
1346 if (type
!= SSH2_MSG_USERAUTH_REQUEST
)
1348 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1349 fatal_fr(r
, "parse userstyle");
1350 xasprintf(&userstyle
, "%s%s%s", authctxt
->user
,
1351 authctxt
->style
? ":" : "",
1352 authctxt
->style
? authctxt
->style
: "");
1353 if (strcmp(userstyle
, cp
) != 0) {
1354 logit("wrong user name passed to monitor: "
1355 "expected %s != %.100s", userstyle
, cp
);
1360 if ((r
= sshbuf_skip_string(b
)) != 0 || /* service */
1361 (r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1362 fatal_fr(r
, "parse method");
1363 if (strcmp(cp
, "hostbased") != 0)
1366 if ((r
= sshbuf_skip_string(b
)) != 0 || /* pkalg */
1367 (r
= sshbuf_skip_string(b
)) != 0) /* pkblob */
1368 fatal_fr(r
, "parse pk");
1370 /* verify client host, strip trailing dot if necessary */
1371 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1372 fatal_fr(r
, "parse host");
1373 if (((len
= strlen(cp
)) > 0) && cp
[len
- 1] == '.')
1375 if (strcmp(cp
, chost
) != 0)
1379 /* verify client user */
1380 if ((r
= sshbuf_get_cstring(b
, &cp
, NULL
)) != 0)
1381 fatal_fr(r
, "parse ruser");
1382 if (strcmp(cp
, cuser
) != 0)
1386 if (sshbuf_len(b
) != 0)
1393 mm_answer_keyverify(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1396 const u_char
*signature
, *data
, *blob
;
1397 char *sigalg
= NULL
, *fp
= NULL
;
1398 size_t signaturelen
, datalen
, bloblen
;
1399 int r
, ret
, req_presence
= 0, req_verify
= 0, valid_data
= 0;
1401 struct sshkey_sig_details
*sig_details
= NULL
;
1403 if ((r
= sshbuf_get_string_direct(m
, &blob
, &bloblen
)) != 0 ||
1404 (r
= sshbuf_get_string_direct(m
, &signature
, &signaturelen
)) != 0 ||
1405 (r
= sshbuf_get_string_direct(m
, &data
, &datalen
)) != 0 ||
1406 (r
= sshbuf_get_cstring(m
, &sigalg
, NULL
)) != 0)
1407 fatal_fr(r
, "parse");
1409 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1410 !monitor_allowed_key(blob
, bloblen
))
1411 fatal_f("bad key, not previously allowed");
1413 /* Empty signature algorithm means NULL. */
1414 if (*sigalg
== '\0') {
1419 /* XXX use sshkey_froms here; need to change key_blob, etc. */
1420 if ((r
= sshkey_from_blob(blob
, bloblen
, &key
)) != 0)
1421 fatal_fr(r
, "parse key");
1423 switch (key_blobtype
) {
1425 valid_data
= monitor_valid_userblob(ssh
, data
, datalen
);
1426 auth_method
= "publickey";
1429 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1430 hostbased_cuser
, hostbased_chost
);
1431 auth_method
= "hostbased";
1438 fatal_f("bad %s signature data blob",
1439 key_blobtype
== MM_USERKEY
? "userkey" :
1440 (key_blobtype
== MM_HOSTKEY
? "hostkey" : "unknown"));
1442 if ((fp
= sshkey_fingerprint(key
, options
.fingerprint_hash
,
1443 SSH_FP_DEFAULT
)) == NULL
)
1444 fatal_f("sshkey_fingerprint failed");
1446 ret
= sshkey_verify(key
, signature
, signaturelen
, data
, datalen
,
1447 sigalg
, ssh
->compat
, &sig_details
);
1448 debug3_f("%s %s signature using %s %s%s%s", auth_method
,
1449 sshkey_type(key
), sigalg
== NULL
? "default" : sigalg
,
1450 (ret
== 0) ? "verified" : "unverified",
1451 (ret
!= 0) ? ": " : "", (ret
!= 0) ? ssh_err(ret
) : "");
1453 if (ret
== 0 && key_blobtype
== MM_USERKEY
&& sig_details
!= NULL
) {
1454 req_presence
= (options
.pubkey_auth_options
&
1455 PUBKEYAUTH_TOUCH_REQUIRED
) ||
1456 !key_opts
->no_require_user_presence
;
1458 (sig_details
->sk_flags
& SSH_SK_USER_PRESENCE_REQD
) == 0) {
1459 error("public key %s %s signature for %s%s from %.128s "
1460 "port %d rejected: user presence "
1461 "(authenticator touch) requirement not met ",
1462 sshkey_type(key
), fp
,
1463 authctxt
->valid
? "" : "invalid user ",
1464 authctxt
->user
, ssh_remote_ipaddr(ssh
),
1465 ssh_remote_port(ssh
));
1466 ret
= SSH_ERR_SIGNATURE_INVALID
;
1468 req_verify
= (options
.pubkey_auth_options
&
1469 PUBKEYAUTH_VERIFY_REQUIRED
) || key_opts
->require_verify
;
1471 (sig_details
->sk_flags
& SSH_SK_USER_VERIFICATION_REQD
) == 0) {
1472 error("public key %s %s signature for %s%s from %.128s "
1473 "port %d rejected: user verification requirement "
1474 "not met ", sshkey_type(key
), fp
,
1475 authctxt
->valid
? "" : "invalid user ",
1476 authctxt
->user
, ssh_remote_ipaddr(ssh
),
1477 ssh_remote_port(ssh
));
1478 ret
= SSH_ERR_SIGNATURE_INVALID
;
1481 auth2_record_key(authctxt
, ret
== 0, key
);
1483 if (key_blobtype
== MM_USERKEY
)
1484 auth_activate_options(ssh
, key_opts
);
1485 monitor_reset_key_state();
1489 /* encode ret != 0 as positive integer, since we're sending u32 */
1490 encoded_ret
= (ret
!= 0);
1491 if ((r
= sshbuf_put_u32(m
, encoded_ret
)) != 0 ||
1492 (r
= sshbuf_put_u8(m
, sig_details
!= NULL
)) != 0)
1493 fatal_fr(r
, "assemble");
1494 if (sig_details
!= NULL
) {
1495 if ((r
= sshbuf_put_u32(m
, sig_details
->sk_counter
)) != 0 ||
1496 (r
= sshbuf_put_u8(m
, sig_details
->sk_flags
)) != 0)
1497 fatal_fr(r
, "assemble sk");
1499 sshkey_sig_details_free(sig_details
);
1500 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1510 mm_record_login(struct ssh
*ssh
, Session
*s
, struct passwd
*pw
)
1513 struct sockaddr_storage from
;
1516 * Get IP address of client. If the connection is not a socket, let
1517 * the address be 0.0.0.0.
1519 memset(&from
, 0, sizeof(from
));
1520 fromlen
= sizeof(from
);
1521 if (ssh_packet_connection_is_on_socket(ssh
)) {
1522 if (getpeername(ssh_packet_get_connection_in(ssh
),
1523 (struct sockaddr
*)&from
, &fromlen
) == -1) {
1524 debug("getpeername: %.100s", strerror(errno
));
1528 /* Record that there was a login on that tty from the remote host. */
1529 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1530 session_get_remote_name_or_ip(ssh
, utmp_len
, options
.use_dns
),
1531 (struct sockaddr
*)&from
, fromlen
);
1535 mm_session_close(Session
*s
)
1537 debug3_f("session %d pid %ld", s
->self
, (long)s
->pid
);
1538 if (s
->ttyfd
!= -1) {
1539 debug3_f("tty %s ptyfd %d", s
->tty
, s
->ptyfd
);
1540 session_pty_cleanup2(s
);
1542 session_unused(s
->self
);
1546 mm_answer_pty(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1548 extern struct monitor
*pmonitor
;
1552 debug3_f("entering");
1558 s
->authctxt
= authctxt
;
1559 s
->pw
= authctxt
->pw
;
1560 s
->pid
= pmonitor
->m_pid
;
1561 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1564 pty_setowner(authctxt
->pw
, s
->tty
);
1566 if ((r
= sshbuf_put_u32(m
, 1)) != 0 ||
1567 (r
= sshbuf_put_cstring(m
, s
->tty
)) != 0)
1568 fatal_fr(r
, "assemble");
1570 /* We need to trick ttyslot */
1571 if (dup2(s
->ttyfd
, 0) == -1)
1574 mm_record_login(ssh
, s
, authctxt
->pw
);
1576 /* Now we can close the file descriptor again */
1579 /* send messages generated by record_login */
1580 if ((r
= sshbuf_put_stringb(m
, loginmsg
)) != 0)
1581 fatal_fr(r
, "assemble loginmsg");
1582 sshbuf_reset(loginmsg
);
1584 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1586 if (mm_send_fd(sock
, s
->ptyfd
) == -1 ||
1587 mm_send_fd(sock
, s
->ttyfd
) == -1)
1588 fatal_f("send fds failed");
1590 /* make sure nothing uses fd 0 */
1591 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) == -1)
1592 fatal_f("open(/dev/null): %s", strerror(errno
));
1594 error_f("fd0 %d != 0", fd0
);
1596 /* slave side of pty is not needed */
1598 s
->ttyfd
= s
->ptyfd
;
1599 /* no need to dup() because nobody closes ptyfd */
1600 s
->ptymaster
= s
->ptyfd
;
1602 debug3_f("tty %s ptyfd %d", s
->tty
, s
->ttyfd
);
1608 mm_session_close(s
);
1609 if ((r
= sshbuf_put_u32(m
, 0)) != 0)
1610 fatal_fr(r
, "assemble 0");
1611 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1616 mm_answer_pty_cleanup(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1622 debug3_f("entering");
1624 if ((r
= sshbuf_get_cstring(m
, &tty
, NULL
)) != 0)
1625 fatal_fr(r
, "parse tty");
1626 if ((s
= session_by_tty(tty
)) != NULL
)
1627 mm_session_close(s
);
1634 mm_answer_term(struct ssh
*ssh
, int sock
, struct sshbuf
*req
)
1636 extern struct monitor
*pmonitor
;
1639 debug3_f("tearing down sessions");
1641 /* The child is terminating */
1642 session_destroy_all(ssh
, &mm_session_close
);
1645 if (options
.use_pam
)
1649 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1653 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1655 /* Terminate process */
1659 #ifdef SSH_AUDIT_EVENTS
1660 /* Report that an audit event occurred */
1662 mm_answer_audit_event(struct ssh
*ssh
, int socket
, struct sshbuf
*m
)
1665 ssh_audit_event_t event
;
1668 debug3("%s entering", __func__
);
1670 if ((r
= sshbuf_get_u32(m
, &n
)) != 0)
1671 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1672 event
= (ssh_audit_event_t
)n
;
1674 case SSH_AUTH_FAIL_PUBKEY
:
1675 case SSH_AUTH_FAIL_HOSTBASED
:
1676 case SSH_AUTH_FAIL_GSSAPI
:
1677 case SSH_LOGIN_EXCEED_MAXTRIES
:
1678 case SSH_LOGIN_ROOT_DENIED
:
1679 case SSH_CONNECTION_CLOSE
:
1680 case SSH_INVALID_USER
:
1681 audit_event(ssh
, event
);
1684 fatal("Audit event type %d not permitted", event
);
1691 mm_answer_audit_command(struct ssh
*ssh
, int socket
, struct sshbuf
*m
)
1696 debug3("%s entering", __func__
);
1697 if ((r
= sshbuf_get_cstring(m
, &cmd
, NULL
)) != 0)
1698 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1699 /* sanity check command, if so how? */
1700 audit_run_command(cmd
);
1704 #endif /* SSH_AUDIT_EVENTS */
1707 monitor_clear_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1709 ssh_clear_newkeys(ssh
, MODE_IN
);
1710 ssh_clear_newkeys(ssh
, MODE_OUT
);
1711 sshbuf_free(child_state
);
1716 monitor_apply_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1721 debug3_f("packet_set_state");
1722 if ((r
= ssh_packet_set_state(ssh
, child_state
)) != 0)
1723 fatal_fr(r
, "packet_set_state");
1724 sshbuf_free(child_state
);
1726 if ((kex
= ssh
->kex
) == NULL
)
1727 fatal_f("internal error: ssh->kex == NULL");
1728 if (session_id2_len
!= sshbuf_len(ssh
->kex
->session_id
)) {
1729 fatal_f("incorrect session id length %zu (expected %u)",
1730 sshbuf_len(ssh
->kex
->session_id
), session_id2_len
);
1732 if (memcmp(sshbuf_ptr(ssh
->kex
->session_id
), session_id2
,
1733 session_id2_len
) != 0)
1734 fatal_f("session ID mismatch");
1735 /* XXX set callbacks */
1737 kex
->kex
[KEX_DH_GRP1_SHA1
] = kex_gen_server
;
1738 kex
->kex
[KEX_DH_GRP14_SHA1
] = kex_gen_server
;
1739 kex
->kex
[KEX_DH_GRP14_SHA256
] = kex_gen_server
;
1740 kex
->kex
[KEX_DH_GRP16_SHA512
] = kex_gen_server
;
1741 kex
->kex
[KEX_DH_GRP18_SHA512
] = kex_gen_server
;
1742 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1743 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1744 # ifdef OPENSSL_HAS_ECC
1745 kex
->kex
[KEX_ECDH_SHA2
] = kex_gen_server
;
1747 #endif /* WITH_OPENSSL */
1748 kex
->kex
[KEX_C25519_SHA256
] = kex_gen_server
;
1749 kex
->kex
[KEX_KEM_SNTRUP761X25519_SHA512
] = kex_gen_server
;
1750 kex
->load_host_public_key
=&get_hostkey_public_by_type
;
1751 kex
->load_host_private_key
=&get_hostkey_private_by_type
;
1752 kex
->host_key_index
=&get_hostkey_index
;
1753 kex
->sign
= sshd_hostkey_sign
;
1756 /* This function requires careful sanity checking */
1759 mm_get_keystate(struct ssh
*ssh
, struct monitor
*pmonitor
)
1761 debug3_f("Waiting for new keys");
1763 if ((child_state
= sshbuf_new()) == NULL
)
1764 fatal_f("sshbuf_new failed");
1765 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
,
1767 debug3_f("GOT new keys");
1773 #define FD_CLOSEONEXEC(x) do { \
1774 if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
1775 fatal("fcntl(%d, F_SETFD)", x); \
1779 monitor_openfds(struct monitor
*mon
, int do_logfds
)
1786 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1787 fatal_f("socketpair: %s", strerror(errno
));
1789 if (setsockopt(pair
[0], SOL_SOCKET
, SO_ZEROIZE
, &on
, sizeof(on
)) == -1)
1790 error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno
));
1791 if (setsockopt(pair
[1], SOL_SOCKET
, SO_ZEROIZE
, &on
, sizeof(on
)) == -1)
1792 error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno
));
1794 FD_CLOSEONEXEC(pair
[0]);
1795 FD_CLOSEONEXEC(pair
[1]);
1796 mon
->m_recvfd
= pair
[0];
1797 mon
->m_sendfd
= pair
[1];
1800 if (pipe(pair
) == -1)
1801 fatal_f("pipe: %s", strerror(errno
));
1802 FD_CLOSEONEXEC(pair
[0]);
1803 FD_CLOSEONEXEC(pair
[1]);
1804 mon
->m_log_recvfd
= pair
[0];
1805 mon
->m_log_sendfd
= pair
[1];
1807 mon
->m_log_recvfd
= mon
->m_log_sendfd
= -1;
1810 #define MM_MEMSIZE 65536
1815 struct monitor
*mon
;
1817 mon
= xcalloc(1, sizeof(*mon
));
1818 monitor_openfds(mon
, 1);
1824 monitor_reinit(struct monitor
*mon
)
1826 monitor_openfds(mon
, 0);
1831 mm_answer_gss_setup_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1839 if (!options
.gss_authentication
)
1840 fatal_f("GSSAPI authentication not enabled");
1842 if ((r
= sshbuf_get_string(m
, &p
, &len
)) != 0)
1843 fatal_fr(r
, "parse");
1847 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1849 free(goid
.elements
);
1852 if ((r
= sshbuf_put_u32(m
, major
)) != 0)
1853 fatal_fr(r
, "assemble");
1855 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1857 /* Now we have a context, enable the step */
1858 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1864 mm_answer_gss_accept_ctx(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1867 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1868 OM_uint32 major
, minor
;
1869 OM_uint32 flags
= 0; /* GSI needs this */
1872 if (!options
.gss_authentication
)
1873 fatal_f("GSSAPI authentication not enabled");
1875 if ((r
= ssh_gssapi_get_buffer_desc(m
, &in
)) != 0)
1876 fatal_fr(r
, "ssh_gssapi_get_buffer_desc");
1877 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1881 if ((r
= sshbuf_put_u32(m
, major
)) != 0 ||
1882 (r
= sshbuf_put_string(m
, out
.value
, out
.length
)) != 0 ||
1883 (r
= sshbuf_put_u32(m
, flags
)) != 0)
1884 fatal_fr(r
, "assemble");
1885 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1887 gss_release_buffer(&minor
, &out
);
1889 if (major
== GSS_S_COMPLETE
) {
1890 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1891 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1892 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1898 mm_answer_gss_checkmic(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1900 gss_buffer_desc gssbuf
, mic
;
1904 if (!options
.gss_authentication
)
1905 fatal_f("GSSAPI authentication not enabled");
1907 if ((r
= ssh_gssapi_get_buffer_desc(m
, &gssbuf
)) != 0 ||
1908 (r
= ssh_gssapi_get_buffer_desc(m
, &mic
)) != 0)
1909 fatal_fr(r
, "ssh_gssapi_get_buffer_desc");
1911 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1917 if ((r
= sshbuf_put_u32(m
, ret
)) != 0)
1918 fatal_fr(r
, "assemble");
1920 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1922 if (!GSS_ERROR(ret
))
1923 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1929 mm_answer_gss_userok(struct ssh
*ssh
, int sock
, struct sshbuf
*m
)
1931 int r
, authenticated
;
1932 const char *displayname
;
1934 if (!options
.gss_authentication
)
1935 fatal_f("GSSAPI authentication not enabled");
1937 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1940 if ((r
= sshbuf_put_u32(m
, authenticated
)) != 0)
1941 fatal_fr(r
, "assemble");
1943 debug3_f("sending result %d", authenticated
);
1944 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1946 auth_method
= "gssapi-with-mic";
1948 if ((displayname
= ssh_gssapi_displayname()) != NULL
)
1949 auth2_record_info(authctxt
, "%s", displayname
);
1951 /* Monitor loop will terminate if authenticated */
1952 return (authenticated
);