1 /* $OpenBSD: monitor.c,v 1.106 2010/03/07 11:57:13 dtucker 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/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
52 #include <openssl/dh.h>
54 #include "openbsd-compat/sys-queue.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
67 #define TARGET_OS_MAC 1
72 #include "auth-options.h"
81 #include "monitor_mm.h"
85 #include "monitor_wrap.h"
86 #include "monitor_fdpass.h"
94 static Gssctxt
*gsscontext
= NULL
;
98 extern ServerOptions options
;
99 extern u_int utmp_len
;
100 extern Newkeys
*current_keys
[];
101 extern z_stream incoming_stream
;
102 extern z_stream outgoing_stream
;
103 extern u_char session_id
[];
104 extern Buffer auth_debug
;
105 extern int auth_debug_init
;
106 extern Buffer loginmsg
;
108 /* State exported from the child */
129 u_int64_t sent_bytes
;
130 u_int64_t recv_bytes
;
133 /* Functions on the monitor that answer unprivileged requests */
135 int mm_answer_moduli(int, Buffer
*);
136 int mm_answer_sign(int, Buffer
*);
137 int mm_answer_pwnamallow(int, Buffer
*);
138 int mm_answer_auth2_read_banner(int, Buffer
*);
139 int mm_answer_authserv(int, Buffer
*);
140 int mm_answer_authpassword(int, Buffer
*);
141 int mm_answer_bsdauthquery(int, Buffer
*);
142 int mm_answer_bsdauthrespond(int, Buffer
*);
143 int mm_answer_skeyquery(int, Buffer
*);
144 int mm_answer_skeyrespond(int, Buffer
*);
145 int mm_answer_keyallowed(int, Buffer
*);
146 int mm_answer_keyverify(int, Buffer
*);
147 int mm_answer_pty(int, Buffer
*);
148 int mm_answer_pty_cleanup(int, Buffer
*);
149 int mm_answer_term(int, Buffer
*);
150 int mm_answer_rsa_keyallowed(int, Buffer
*);
151 int mm_answer_rsa_challenge(int, Buffer
*);
152 int mm_answer_rsa_response(int, Buffer
*);
153 int mm_answer_sesskey(int, Buffer
*);
154 int mm_answer_sessid(int, Buffer
*);
155 int mm_answer_jpake_get_pwdata(int, Buffer
*);
156 int mm_answer_jpake_step1(int, Buffer
*);
157 int mm_answer_jpake_step2(int, Buffer
*);
158 int mm_answer_jpake_key_confirm(int, Buffer
*);
159 int mm_answer_jpake_check_confirm(int, Buffer
*);
162 int mm_answer_pam_start(int, Buffer
*);
163 int mm_answer_pam_account(int, Buffer
*);
164 int mm_answer_pam_init_ctx(int, Buffer
*);
165 int mm_answer_pam_query(int, Buffer
*);
166 int mm_answer_pam_respond(int, Buffer
*);
167 int mm_answer_pam_free_ctx(int, Buffer
*);
171 int mm_answer_gss_setup_ctx(int, Buffer
*);
172 int mm_answer_gss_accept_ctx(int, Buffer
*);
173 int mm_answer_gss_userok(int, Buffer
*);
174 int mm_answer_gss_checkmic(int, Buffer
*);
177 #ifdef SSH_AUDIT_EVENTS
178 int mm_answer_audit_event(int, Buffer
*);
179 int mm_answer_audit_command(int, Buffer
*);
182 static Authctxt
*authctxt
;
183 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
185 /* local state for key verify */
186 static u_char
*key_blob
= NULL
;
187 static u_int key_bloblen
= 0;
188 static int key_blobtype
= MM_NOKEY
;
189 static char *hostbased_cuser
= NULL
;
190 static char *hostbased_chost
= NULL
;
191 static char *auth_method
= "unknown";
192 static u_int session_id2_len
= 0;
193 static u_char
*session_id2
= NULL
;
194 static pid_t monitor_child_pid
;
197 enum monitor_reqtype type
;
199 int (*f
)(int, Buffer
*);
202 #define MON_ISAUTH 0x0004 /* Required for Authentication */
203 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
204 #define MON_ONCE 0x0010 /* Disable after calling */
205 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
207 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
209 #define MON_PERMIT 0x1000 /* Request is permitted */
211 struct mon_table mon_dispatch_proto20
[] = {
212 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
213 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
214 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
215 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
216 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
217 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
219 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
220 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
221 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
222 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
223 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
224 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
226 #ifdef SSH_AUDIT_EVENTS
227 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
230 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
231 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
234 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
235 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
237 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
238 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
240 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
241 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
242 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
243 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
246 {MONITOR_REQ_JPAKE_GET_PWDATA
, MON_ONCE
, mm_answer_jpake_get_pwdata
},
247 {MONITOR_REQ_JPAKE_STEP1
, MON_ISAUTH
, mm_answer_jpake_step1
},
248 {MONITOR_REQ_JPAKE_STEP2
, MON_ONCE
, mm_answer_jpake_step2
},
249 {MONITOR_REQ_JPAKE_KEY_CONFIRM
, MON_ONCE
, mm_answer_jpake_key_confirm
},
250 {MONITOR_REQ_JPAKE_CHECK_CONFIRM
, MON_AUTH
, mm_answer_jpake_check_confirm
},
255 struct mon_table mon_dispatch_postauth20
[] = {
256 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
257 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
258 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
259 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
260 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
261 #ifdef SSH_AUDIT_EVENTS
262 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
263 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
268 struct mon_table mon_dispatch_proto15
[] = {
269 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
270 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
271 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
272 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
273 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
274 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
275 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
276 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
278 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
279 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
282 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
283 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
286 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
287 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
288 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
289 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
290 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
291 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
293 #ifdef SSH_AUDIT_EVENTS
294 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
299 struct mon_table mon_dispatch_postauth15
[] = {
300 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
301 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
302 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
303 #ifdef SSH_AUDIT_EVENTS
304 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
305 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
|MON_ONCE
, mm_answer_audit_command
},
310 struct mon_table
*mon_dispatch
;
312 /* Specifies if a certain message is allowed at the moment */
315 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
317 while (ent
->f
!= NULL
) {
318 if (ent
->type
== type
) {
319 ent
->flags
&= ~MON_PERMIT
;
320 ent
->flags
|= permit
? MON_PERMIT
: 0;
328 monitor_permit_authentications(int permit
)
330 struct mon_table
*ent
= mon_dispatch
;
332 while (ent
->f
!= NULL
) {
333 if (ent
->flags
& MON_AUTH
) {
334 ent
->flags
&= ~MON_PERMIT
;
335 ent
->flags
|= permit
? MON_PERMIT
: 0;
342 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
344 struct mon_table
*ent
;
345 int authenticated
= 0;
347 debug3("preauth child monitor started");
349 authctxt
= _authctxt
;
350 memset(authctxt
, 0, sizeof(*authctxt
));
352 authctxt
->loginmsg
= &loginmsg
;
355 mon_dispatch
= mon_dispatch_proto20
;
357 /* Permit requests for moduli and signatures */
358 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
359 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
361 mon_dispatch
= mon_dispatch_proto15
;
363 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
366 /* The first few requests do not require asynchronous access */
367 while (!authenticated
) {
368 auth_method
= "unknown";
369 authenticated
= (monitor_read(pmonitor
, mon_dispatch
, &ent
) == 1);
371 if (!(ent
->flags
& MON_AUTHDECIDE
))
372 fatal("%s: unexpected authentication from %d",
373 __func__
, ent
->type
);
374 if (authctxt
->pw
->pw_uid
== 0 &&
375 !auth_root_allowed(auth_method
))
378 /* PAM needs to perform account checks after auth */
379 if (options
.use_pam
&& authenticated
) {
383 mm_request_receive_expect(pmonitor
->m_sendfd
,
384 MONITOR_REQ_PAM_ACCOUNT
, &m
);
385 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
391 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
392 auth_log(authctxt
, authenticated
, auth_method
,
393 compat20
? " ssh2" : "");
395 authctxt
->failures
++;
398 /* Cleanup JPAKE context after authentication */
399 if (ent
->flags
& MON_AUTHDECIDE
) {
400 if (authctxt
->jpake_ctx
!= NULL
) {
401 jpake_free(authctxt
->jpake_ctx
);
402 authctxt
->jpake_ctx
= NULL
;
408 if (!authctxt
->valid
)
409 fatal("%s: authenticated invalid user", __func__
);
410 if (strcmp(auth_method
, "unknown") == 0)
411 fatal("%s: authentication method name unknown", __func__
);
413 debug("%s: %s has been authenticated by privileged process",
414 __func__
, authctxt
->user
);
416 mm_get_keystate(pmonitor
);
420 monitor_set_child_handler(pid_t pid
)
422 monitor_child_pid
= pid
;
426 monitor_child_handler(int sig
)
428 kill(monitor_child_pid
, sig
);
432 monitor_child_postauth(struct monitor
*pmonitor
)
434 monitor_set_child_handler(pmonitor
->m_pid
);
435 signal(SIGHUP
, &monitor_child_handler
);
436 signal(SIGTERM
, &monitor_child_handler
);
437 signal(SIGINT
, &monitor_child_handler
);
440 mon_dispatch
= mon_dispatch_postauth20
;
442 /* Permit requests for moduli and signatures */
443 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
444 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
445 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
447 mon_dispatch
= mon_dispatch_postauth15
;
448 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
451 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
452 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
456 monitor_read(pmonitor
, mon_dispatch
, NULL
);
460 monitor_sync(struct monitor
*pmonitor
)
462 if (options
.compression
) {
463 /* The member allocation is not visible, so sync it */
464 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
469 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
470 struct mon_table
**pent
)
478 mm_request_receive(pmonitor
->m_sendfd
, &m
);
479 type
= buffer_get_char(&m
);
481 debug3("%s: checking request %d", __func__
, type
);
483 while (ent
->f
!= NULL
) {
484 if (ent
->type
== type
)
489 if (ent
->f
!= NULL
) {
490 if (!(ent
->flags
& MON_PERMIT
))
491 fatal("%s: unpermitted request %d", __func__
,
493 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
496 /* The child may use this request only once, disable it */
497 if (ent
->flags
& MON_ONCE
) {
498 debug2("%s: %d used once, disabling now", __func__
,
500 ent
->flags
&= ~MON_PERMIT
;
509 fatal("%s: unsupported request: %d", __func__
, type
);
515 /* allowed key state */
517 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
519 /* make sure key is allowed */
520 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
521 memcmp(key_blob
, blob
, key_bloblen
))
527 monitor_reset_key_state(void)
530 if (key_blob
!= NULL
)
532 if (hostbased_cuser
!= NULL
)
533 xfree(hostbased_cuser
);
534 if (hostbased_chost
!= NULL
)
535 xfree(hostbased_chost
);
538 key_blobtype
= MM_NOKEY
;
539 hostbased_cuser
= NULL
;
540 hostbased_chost
= NULL
;
544 mm_answer_moduli(int sock
, Buffer
*m
)
549 min
= buffer_get_int(m
);
550 want
= buffer_get_int(m
);
551 max
= buffer_get_int(m
);
553 debug3("%s: got parameters: %d %d %d",
554 __func__
, min
, want
, max
);
555 /* We need to check here, too, in case the child got corrupted */
556 if (max
< min
|| want
< min
|| max
< want
)
557 fatal("%s: bad parameters: %d %d %d",
558 __func__
, min
, want
, max
);
562 dh
= choose_dh(min
, want
, max
);
564 buffer_put_char(m
, 0);
567 /* Send first bignum */
568 buffer_put_char(m
, 1);
569 buffer_put_bignum2(m
, dh
->p
);
570 buffer_put_bignum2(m
, dh
->g
);
574 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
579 mm_answer_sign(int sock
, Buffer
*m
)
584 u_int siglen
, datlen
;
587 debug3("%s", __func__
);
589 keyid
= buffer_get_int(m
);
590 p
= buffer_get_string(m
, &datlen
);
593 * Supported KEX types will only return SHA1 (20 byte) or
594 * SHA256 (32 byte) hashes
596 if (datlen
!= 20 && datlen
!= 32)
597 fatal("%s: data length incorrect: %u", __func__
, datlen
);
599 /* save session id, it will be passed on the first call */
600 if (session_id2_len
== 0) {
601 session_id2_len
= datlen
;
602 session_id2
= xmalloc(session_id2_len
);
603 memcpy(session_id2
, p
, session_id2_len
);
606 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
607 fatal("%s: no hostkey from index %d", __func__
, keyid
);
608 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
609 fatal("%s: key_sign failed", __func__
);
611 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
614 buffer_put_string(m
, signature
, siglen
);
619 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
621 /* Turn on permissions for getpwnam */
622 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
627 /* Retrieves the password entry and also checks if the user is permitted */
630 mm_answer_pwnamallow(int sock
, Buffer
*m
)
633 struct passwd
*pwent
;
636 debug3("%s", __func__
);
638 if (authctxt
->attempt
++ != 0)
639 fatal("%s: multiple attempts for getpwnam", __func__
);
641 username
= buffer_get_string(m
, NULL
);
643 pwent
= getpwnamallow(username
);
645 authctxt
->user
= xstrdup(username
);
646 setproctitle("%s [priv]", pwent
? username
: "unknown");
652 buffer_put_char(m
, 0);
653 authctxt
->pw
= fakepw();
658 authctxt
->pw
= pwent
;
661 buffer_put_char(m
, 1);
662 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
663 buffer_put_cstring(m
, pwent
->pw_name
);
664 buffer_put_cstring(m
, "*");
665 buffer_put_cstring(m
, pwent
->pw_gecos
);
666 #ifdef HAVE_PW_CLASS_IN_PASSWD
667 buffer_put_cstring(m
, pwent
->pw_class
);
669 buffer_put_cstring(m
, pwent
->pw_dir
);
670 buffer_put_cstring(m
, pwent
->pw_shell
);
673 buffer_put_string(m
, &options
, sizeof(options
));
674 if (options
.banner
!= NULL
)
675 buffer_put_cstring(m
, options
.banner
);
676 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
677 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
679 /* For SSHv1 allow authentication now */
681 monitor_permit_authentications(1);
683 /* Allow service/style information on the auth context */
684 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
685 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
690 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
696 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
701 banner
= auth2_read_banner();
702 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
703 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
712 mm_answer_authserv(int sock
, Buffer
*m
)
714 monitor_permit_authentications(1);
716 authctxt
->service
= buffer_get_string(m
, NULL
);
717 authctxt
->style
= buffer_get_string(m
, NULL
);
718 debug3("%s: service=%s, style=%s",
719 __func__
, authctxt
->service
, authctxt
->style
);
721 if (strlen(authctxt
->style
) == 0) {
722 xfree(authctxt
->style
);
723 authctxt
->style
= NULL
;
730 mm_answer_authpassword(int sock
, Buffer
*m
)
732 static int call_count
;
737 passwd
= buffer_get_string(m
, &plen
);
738 /* Only authenticate if the context is valid */
739 authenticated
= options
.password_authentication
&&
740 auth_password(authctxt
, passwd
);
741 memset(passwd
, 0, strlen(passwd
));
745 buffer_put_int(m
, authenticated
);
747 debug3("%s: sending result %d", __func__
, authenticated
);
748 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
751 if (plen
== 0 && call_count
== 1)
752 auth_method
= "none";
754 auth_method
= "password";
756 /* Causes monitor loop to terminate if authenticated */
757 return (authenticated
);
762 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
764 char *name
, *infotxt
;
770 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
771 &prompts
, &echo_on
) < 0 ? 0 : 1;
774 buffer_put_int(m
, success
);
776 buffer_put_cstring(m
, prompts
[0]);
778 debug3("%s: sending challenge success: %u", __func__
, success
);
779 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
792 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
797 if (authctxt
->as
== 0)
798 fatal("%s: no bsd auth session", __func__
);
800 response
= buffer_get_string(m
, NULL
);
801 authok
= options
.challenge_response_authentication
&&
802 auth_userresponse(authctxt
->as
, response
, 0);
804 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
808 buffer_put_int(m
, authok
);
810 debug3("%s: sending authenticated: %d", __func__
, authok
);
811 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
813 auth_method
= "bsdauth";
815 return (authok
!= 0);
821 mm_answer_skeyquery(int sock
, Buffer
*m
)
824 char challenge
[1024];
827 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
828 sizeof(challenge
)) < 0 ? 0 : 1;
831 buffer_put_int(m
, success
);
833 buffer_put_cstring(m
, challenge
);
835 debug3("%s: sending challenge success: %u", __func__
, success
);
836 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
842 mm_answer_skeyrespond(int sock
, Buffer
*m
)
847 response
= buffer_get_string(m
, NULL
);
849 authok
= (options
.challenge_response_authentication
&&
851 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
852 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
857 buffer_put_int(m
, authok
);
859 debug3("%s: sending authenticated: %d", __func__
, authok
);
860 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
862 auth_method
= "skey";
864 return (authok
!= 0);
870 mm_answer_pam_start(int sock
, Buffer
*m
)
872 if (!options
.use_pam
)
873 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
877 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
883 mm_answer_pam_account(int sock
, Buffer
*m
)
887 if (!options
.use_pam
)
888 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
890 ret
= do_pam_account();
892 buffer_put_int(m
, ret
);
893 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
895 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
900 static void *sshpam_ctxt
, *sshpam_authok
;
901 extern KbdintDevice sshpam_device
;
904 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
907 debug3("%s", __func__
);
908 authctxt
->user
= buffer_get_string(m
, NULL
);
909 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
910 sshpam_authok
= NULL
;
912 if (sshpam_ctxt
!= NULL
) {
913 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
914 buffer_put_int(m
, 1);
916 buffer_put_int(m
, 0);
918 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
923 mm_answer_pam_query(int sock
, Buffer
*m
)
925 char *name
, *info
, **prompts
;
926 u_int i
, num
, *echo_on
;
929 debug3("%s", __func__
);
930 sshpam_authok
= NULL
;
931 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
932 if (ret
== 0 && num
== 0)
933 sshpam_authok
= sshpam_ctxt
;
934 if (num
> 1 || name
== NULL
|| info
== NULL
)
937 buffer_put_int(m
, ret
);
938 buffer_put_cstring(m
, name
);
940 buffer_put_cstring(m
, info
);
942 buffer_put_int(m
, num
);
943 for (i
= 0; i
< num
; ++i
) {
944 buffer_put_cstring(m
, prompts
[i
]);
946 buffer_put_int(m
, echo_on
[i
]);
952 auth_method
= "keyboard-interactive/pam";
953 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
958 mm_answer_pam_respond(int sock
, Buffer
*m
)
964 debug3("%s", __func__
);
965 sshpam_authok
= NULL
;
966 num
= buffer_get_int(m
);
968 resp
= xcalloc(num
, sizeof(char *));
969 for (i
= 0; i
< num
; ++i
)
970 resp
[i
] = buffer_get_string(m
, NULL
);
971 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
972 for (i
= 0; i
< num
; ++i
)
976 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
979 buffer_put_int(m
, ret
);
980 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
981 auth_method
= "keyboard-interactive/pam";
983 sshpam_authok
= sshpam_ctxt
;
988 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
991 debug3("%s", __func__
);
992 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
994 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
995 auth_method
= "keyboard-interactive/pam";
996 return (sshpam_authok
== sshpam_ctxt
);
1001 mm_answer_keyallowed(int sock
, Buffer
*m
)
1004 char *cuser
, *chost
;
1007 enum mm_keytype type
= 0;
1010 debug3("%s entering", __func__
);
1012 type
= buffer_get_int(m
);
1013 cuser
= buffer_get_string(m
, NULL
);
1014 chost
= buffer_get_string(m
, NULL
);
1015 blob
= buffer_get_string(m
, &bloblen
);
1017 key
= key_from_blob(blob
, bloblen
);
1019 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
1020 (!compat20
&& type
!= MM_RSAHOSTKEY
))
1021 fatal("%s: key type and protocol mismatch", __func__
);
1023 debug3("%s: key_from_blob: %p", __func__
, key
);
1025 if (key
!= NULL
&& authctxt
->valid
) {
1028 allowed
= options
.pubkey_authentication
&&
1029 user_key_allowed(authctxt
->pw
, key
);
1030 auth_method
= "publickey";
1031 if (options
.pubkey_authentication
&& allowed
!= 1)
1032 auth_clear_options();
1035 allowed
= options
.hostbased_authentication
&&
1036 hostbased_key_allowed(authctxt
->pw
,
1038 auth_method
= "hostbased";
1041 key
->type
= KEY_RSA1
; /* XXX */
1042 allowed
= options
.rhosts_rsa_authentication
&&
1043 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1045 if (options
.rhosts_rsa_authentication
&& allowed
!= 1)
1046 auth_clear_options();
1047 auth_method
= "rsa";
1050 fatal("%s: unknown key type %d", __func__
, type
);
1057 /* clear temporarily storage (used by verify) */
1058 monitor_reset_key_state();
1061 /* Save temporarily for comparison in verify */
1063 key_bloblen
= bloblen
;
1064 key_blobtype
= type
;
1065 hostbased_cuser
= cuser
;
1066 hostbased_chost
= chost
;
1068 /* Log failed attempt */
1069 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1075 debug3("%s: key %p is %s",
1076 __func__
, key
, allowed
? "allowed" : "not allowed");
1079 buffer_put_int(m
, allowed
);
1080 buffer_put_int(m
, forced_command
!= NULL
);
1082 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1084 if (type
== MM_RSAHOSTKEY
)
1085 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1091 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1099 buffer_append(&b
, data
, datalen
);
1101 if (datafellows
& SSH_OLD_SESSIONID
) {
1103 len
= buffer_len(&b
);
1104 if ((session_id2
== NULL
) ||
1105 (len
< session_id2_len
) ||
1106 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1108 buffer_consume(&b
, session_id2_len
);
1110 p
= buffer_get_string(&b
, &len
);
1111 if ((session_id2
== NULL
) ||
1112 (len
!= session_id2_len
) ||
1113 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1117 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1119 p
= buffer_get_string(&b
, NULL
);
1120 if (strcmp(authctxt
->user
, p
) != 0) {
1121 logit("wrong user name passed to monitor: expected %s != %.100s",
1126 buffer_skip_string(&b
);
1127 if (datafellows
& SSH_BUG_PKAUTH
) {
1128 if (!buffer_get_char(&b
))
1131 p
= buffer_get_string(&b
, NULL
);
1132 if (strcmp("publickey", p
) != 0)
1135 if (!buffer_get_char(&b
))
1137 buffer_skip_string(&b
);
1139 buffer_skip_string(&b
);
1140 if (buffer_len(&b
) != 0)
1147 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1156 buffer_append(&b
, data
, datalen
);
1158 p
= buffer_get_string(&b
, &len
);
1159 if ((session_id2
== NULL
) ||
1160 (len
!= session_id2_len
) ||
1161 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1165 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1167 p
= buffer_get_string(&b
, NULL
);
1168 if (strcmp(authctxt
->user
, p
) != 0) {
1169 logit("wrong user name passed to monitor: expected %s != %.100s",
1174 buffer_skip_string(&b
); /* service */
1175 p
= buffer_get_string(&b
, NULL
);
1176 if (strcmp(p
, "hostbased") != 0)
1179 buffer_skip_string(&b
); /* pkalg */
1180 buffer_skip_string(&b
); /* pkblob */
1182 /* verify client host, strip trailing dot if necessary */
1183 p
= buffer_get_string(&b
, NULL
);
1184 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1186 if (strcmp(p
, chost
) != 0)
1190 /* verify client user */
1191 p
= buffer_get_string(&b
, NULL
);
1192 if (strcmp(p
, cuser
) != 0)
1196 if (buffer_len(&b
) != 0)
1203 mm_answer_keyverify(int sock
, Buffer
*m
)
1206 u_char
*signature
, *data
, *blob
;
1207 u_int signaturelen
, datalen
, bloblen
;
1211 blob
= buffer_get_string(m
, &bloblen
);
1212 signature
= buffer_get_string(m
, &signaturelen
);
1213 data
= buffer_get_string(m
, &datalen
);
1215 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1216 !monitor_allowed_key(blob
, bloblen
))
1217 fatal("%s: bad key, not previously allowed", __func__
);
1219 key
= key_from_blob(blob
, bloblen
);
1221 fatal("%s: bad public key blob", __func__
);
1223 switch (key_blobtype
) {
1225 valid_data
= monitor_valid_userblob(data
, datalen
);
1228 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1229 hostbased_cuser
, hostbased_chost
);
1236 fatal("%s: bad signature data blob", __func__
);
1238 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1239 debug3("%s: key %p signature %s",
1240 __func__
, key
, (verified
== 1) ? "verified" : "unverified");
1247 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1249 monitor_reset_key_state();
1252 buffer_put_int(m
, verified
);
1253 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1255 return (verified
== 1);
1259 mm_record_login(Session
*s
, struct passwd
*pw
)
1262 struct sockaddr_storage from
;
1265 * Get IP address of client. If the connection is not a socket, let
1266 * the address be 0.0.0.0.
1268 memset(&from
, 0, sizeof(from
));
1269 fromlen
= sizeof(from
);
1270 if (packet_connection_is_on_socket()) {
1271 if (getpeername(packet_get_connection_in(),
1272 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1273 debug("getpeername: %.100s", strerror(errno
));
1277 /* Record that there was a login on that tty from the remote host. */
1278 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1279 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1280 (struct sockaddr
*)&from
, fromlen
);
1284 mm_session_close(Session
*s
)
1286 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1287 if (s
->ttyfd
!= -1) {
1288 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1289 session_pty_cleanup2(s
);
1291 session_unused(s
->self
);
1295 mm_answer_pty(int sock
, Buffer
*m
)
1297 extern struct monitor
*pmonitor
;
1301 debug3("%s entering", __func__
);
1307 s
->authctxt
= authctxt
;
1308 s
->pw
= authctxt
->pw
;
1309 s
->pid
= pmonitor
->m_pid
;
1310 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1313 pty_setowner(authctxt
->pw
, s
->tty
);
1315 buffer_put_int(m
, 1);
1316 buffer_put_cstring(m
, s
->tty
);
1318 /* We need to trick ttyslot */
1319 if (dup2(s
->ttyfd
, 0) == -1)
1320 fatal("%s: dup2", __func__
);
1322 mm_record_login(s
, authctxt
->pw
);
1324 /* Now we can close the file descriptor again */
1327 /* send messages generated by record_login */
1328 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1329 buffer_clear(&loginmsg
);
1331 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1333 if (mm_send_fd(sock
, s
->ptyfd
) == -1 ||
1334 mm_send_fd(sock
, s
->ttyfd
) == -1)
1335 fatal("%s: send fds failed", __func__
);
1337 /* make sure nothing uses fd 0 */
1338 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1339 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1341 error("%s: fd0 %d != 0", __func__
, fd0
);
1343 /* slave is not needed */
1345 s
->ttyfd
= s
->ptyfd
;
1346 /* no need to dup() because nobody closes ptyfd */
1347 s
->ptymaster
= s
->ptyfd
;
1349 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1355 mm_session_close(s
);
1356 buffer_put_int(m
, 0);
1357 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1362 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1367 debug3("%s entering", __func__
);
1369 tty
= buffer_get_string(m
, NULL
);
1370 if ((s
= session_by_tty(tty
)) != NULL
)
1371 mm_session_close(s
);
1378 mm_answer_sesskey(int sock
, Buffer
*m
)
1383 /* Turn off permissions */
1384 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1386 if ((p
= BN_new()) == NULL
)
1387 fatal("%s: BN_new", __func__
);
1389 buffer_get_bignum2(m
, p
);
1391 rsafail
= ssh1_session_key(p
);
1394 buffer_put_int(m
, rsafail
);
1395 buffer_put_bignum2(m
, p
);
1399 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1401 /* Turn on permissions for sessid passing */
1402 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1408 mm_answer_sessid(int sock
, Buffer
*m
)
1412 debug3("%s entering", __func__
);
1414 if (buffer_len(m
) != 16)
1415 fatal("%s: bad ssh1 session id", __func__
);
1416 for (i
= 0; i
< 16; i
++)
1417 session_id
[i
] = buffer_get_char(m
);
1419 /* Turn on permissions for getpwnam */
1420 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1426 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1430 u_char
*blob
= NULL
;
1434 debug3("%s entering", __func__
);
1436 auth_method
= "rsa";
1437 if (options
.rsa_authentication
&& authctxt
->valid
) {
1438 if ((client_n
= BN_new()) == NULL
)
1439 fatal("%s: BN_new", __func__
);
1440 buffer_get_bignum2(m
, client_n
);
1441 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1442 BN_clear_free(client_n
);
1445 buffer_put_int(m
, allowed
);
1446 buffer_put_int(m
, forced_command
!= NULL
);
1448 /* clear temporarily storage (used by generate challenge) */
1449 monitor_reset_key_state();
1451 if (allowed
&& key
!= NULL
) {
1452 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1453 if (key_to_blob(key
, &blob
, &blen
) == 0)
1454 fatal("%s: key_to_blob failed", __func__
);
1455 buffer_put_string(m
, blob
, blen
);
1457 /* Save temporarily for comparison in verify */
1460 key_blobtype
= MM_RSAUSERKEY
;
1465 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1467 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1468 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1473 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1479 debug3("%s entering", __func__
);
1481 if (!authctxt
->valid
)
1482 fatal("%s: authctxt not valid", __func__
);
1483 blob
= buffer_get_string(m
, &blen
);
1484 if (!monitor_allowed_key(blob
, blen
))
1485 fatal("%s: bad key, not previously allowed", __func__
);
1486 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1487 fatal("%s: key type mismatch", __func__
);
1488 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1489 fatal("%s: received bad key", __func__
);
1490 if (key
->type
!= KEY_RSA
)
1491 fatal("%s: received bad key type %d", __func__
, key
->type
);
1492 key
->type
= KEY_RSA1
;
1494 BN_clear_free(ssh1_challenge
);
1495 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1498 buffer_put_bignum2(m
, ssh1_challenge
);
1500 debug3("%s sending reply", __func__
);
1501 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1503 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1511 mm_answer_rsa_response(int sock
, Buffer
*m
)
1514 u_char
*blob
, *response
;
1518 debug3("%s entering", __func__
);
1520 if (!authctxt
->valid
)
1521 fatal("%s: authctxt not valid", __func__
);
1522 if (ssh1_challenge
== NULL
)
1523 fatal("%s: no ssh1_challenge", __func__
);
1525 blob
= buffer_get_string(m
, &blen
);
1526 if (!monitor_allowed_key(blob
, blen
))
1527 fatal("%s: bad key, not previously allowed", __func__
);
1528 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1529 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1530 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1531 fatal("%s: received bad key", __func__
);
1532 response
= buffer_get_string(m
, &len
);
1534 fatal("%s: received bad response to challenge", __func__
);
1535 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1541 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1544 BN_clear_free(ssh1_challenge
);
1545 ssh1_challenge
= NULL
;
1546 monitor_reset_key_state();
1549 buffer_put_int(m
, success
);
1550 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1556 mm_answer_term(int sock
, Buffer
*req
)
1558 extern struct monitor
*pmonitor
;
1561 debug3("%s: tearing down sessions", __func__
);
1563 /* The child is terminating */
1564 session_destroy_all(&mm_session_close
);
1567 if (options
.use_pam
)
1571 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1575 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1577 /* Terminate process */
1581 #ifdef SSH_AUDIT_EVENTS
1582 /* Report that an audit event occurred */
1584 mm_answer_audit_event(int socket
, Buffer
*m
)
1586 ssh_audit_event_t event
;
1588 debug3("%s entering", __func__
);
1590 event
= buffer_get_int(m
);
1592 case SSH_AUTH_FAIL_PUBKEY
:
1593 case SSH_AUTH_FAIL_HOSTBASED
:
1594 case SSH_AUTH_FAIL_GSSAPI
:
1595 case SSH_LOGIN_EXCEED_MAXTRIES
:
1596 case SSH_LOGIN_ROOT_DENIED
:
1597 case SSH_CONNECTION_CLOSE
:
1598 case SSH_INVALID_USER
:
1602 fatal("Audit event type %d not permitted", event
);
1609 mm_answer_audit_command(int socket
, Buffer
*m
)
1614 debug3("%s entering", __func__
);
1615 cmd
= buffer_get_string(m
, &len
);
1616 /* sanity check command, if so how? */
1617 audit_run_command(cmd
);
1621 #endif /* SSH_AUDIT_EVENTS */
1624 monitor_apply_keystate(struct monitor
*pmonitor
)
1627 set_newkeys(MODE_IN
);
1628 set_newkeys(MODE_OUT
);
1630 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1631 packet_set_encryption_key(child_state
.ssh1key
,
1632 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1633 xfree(child_state
.ssh1key
);
1636 /* for rc4 and other stateful ciphers */
1637 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1638 xfree(child_state
.keyout
);
1639 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1640 xfree(child_state
.keyin
);
1643 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1644 xfree(child_state
.ivout
);
1645 packet_set_iv(MODE_IN
, child_state
.ivin
);
1646 xfree(child_state
.ivin
);
1649 memcpy(&incoming_stream
, &child_state
.incoming
,
1650 sizeof(incoming_stream
));
1651 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1652 sizeof(outgoing_stream
));
1654 /* Update with new address */
1655 if (options
.compression
)
1656 mm_init_compression(pmonitor
->m_zlib
);
1658 /* Network I/O buffers */
1659 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1660 buffer_clear(packet_get_input());
1661 buffer_append(packet_get_input(), child_state
.input
, child_state
.ilen
);
1662 memset(child_state
.input
, 0, child_state
.ilen
);
1663 xfree(child_state
.input
);
1665 buffer_clear(packet_get_output());
1666 buffer_append(packet_get_output(), child_state
.output
,
1668 memset(child_state
.output
, 0, child_state
.olen
);
1669 xfree(child_state
.output
);
1673 roam_set_bytes(child_state
.sent_bytes
, child_state
.recv_bytes
);
1677 mm_get_kex(Buffer
*m
)
1683 kex
= xcalloc(1, sizeof(*kex
));
1684 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1685 if ((session_id2
== NULL
) ||
1686 (kex
->session_id_len
!= session_id2_len
) ||
1687 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1688 fatal("mm_get_get: internal error: bad session id");
1689 kex
->we_need
= buffer_get_int(m
);
1690 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1691 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1692 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1693 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1695 kex
->hostkey_type
= buffer_get_int(m
);
1696 kex
->kex_type
= buffer_get_int(m
);
1697 blob
= buffer_get_string(m
, &bloblen
);
1698 buffer_init(&kex
->my
);
1699 buffer_append(&kex
->my
, blob
, bloblen
);
1701 blob
= buffer_get_string(m
, &bloblen
);
1702 buffer_init(&kex
->peer
);
1703 buffer_append(&kex
->peer
, blob
, bloblen
);
1706 kex
->flags
= buffer_get_int(m
);
1707 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1708 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1709 kex
->load_host_public_key
=&get_hostkey_public_by_type
;
1710 kex
->load_host_private_key
=&get_hostkey_private_by_type
;
1711 kex
->host_key_index
=&get_hostkey_index
;
1716 /* This function requries careful sanity checking */
1719 mm_get_keystate(struct monitor
*pmonitor
)
1723 u_int bloblen
, plen
;
1724 u_int32_t seqnr
, packets
;
1725 u_int64_t blocks
, bytes
;
1727 debug3("%s: Waiting for new keys", __func__
);
1730 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1732 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1733 child_state
.ssh1cipher
= buffer_get_int(&m
);
1734 child_state
.ssh1key
= buffer_get_string(&m
,
1735 &child_state
.ssh1keylen
);
1736 child_state
.ivout
= buffer_get_string(&m
,
1737 &child_state
.ivoutlen
);
1738 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1741 /* Get the Kex for rekeying */
1742 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1745 blob
= buffer_get_string(&m
, &bloblen
);
1746 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1749 debug3("%s: Waiting for second key", __func__
);
1750 blob
= buffer_get_string(&m
, &bloblen
);
1751 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1754 /* Now get sequence numbers for the packets */
1755 seqnr
= buffer_get_int(&m
);
1756 blocks
= buffer_get_int64(&m
);
1757 packets
= buffer_get_int(&m
);
1758 bytes
= buffer_get_int64(&m
);
1759 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
, bytes
);
1760 seqnr
= buffer_get_int(&m
);
1761 blocks
= buffer_get_int64(&m
);
1762 packets
= buffer_get_int(&m
);
1763 bytes
= buffer_get_int64(&m
);
1764 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
, bytes
);
1767 /* Get the key context */
1768 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1769 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1771 debug3("%s: Getting compression state", __func__
);
1772 /* Get compression state */
1773 p
= buffer_get_string(&m
, &plen
);
1774 if (plen
!= sizeof(child_state
.outgoing
))
1775 fatal("%s: bad request size", __func__
);
1776 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1779 p
= buffer_get_string(&m
, &plen
);
1780 if (plen
!= sizeof(child_state
.incoming
))
1781 fatal("%s: bad request size", __func__
);
1782 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1785 /* Network I/O buffers */
1786 debug3("%s: Getting Network I/O buffers", __func__
);
1787 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1788 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1792 child_state
.sent_bytes
= buffer_get_int64(&m
);
1793 child_state
.recv_bytes
= buffer_get_int64(&m
);
1800 /* Allocation functions for zlib */
1802 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1804 size_t len
= (size_t) size
* ncount
;
1807 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1808 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1810 address
= mm_malloc(mm
, len
);
1816 mm_zfree(struct mm_master
*mm
, void *address
)
1818 mm_free(mm
, address
);
1822 mm_init_compression(struct mm_master
*mm
)
1824 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1825 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1826 outgoing_stream
.opaque
= mm
;
1828 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1829 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1830 incoming_stream
.opaque
= mm
;
1835 #define FD_CLOSEONEXEC(x) do { \
1836 if (fcntl(x, F_SETFD, 1) == -1) \
1837 fatal("fcntl(%d, F_SETFD)", x); \
1841 monitor_socketpair(int *pair
)
1843 #ifdef HAVE_SOCKETPAIR
1844 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1845 fatal("%s: socketpair", __func__
);
1847 fatal("%s: UsePrivilegeSeparation=yes not supported",
1850 FD_CLOSEONEXEC(pair
[0]);
1851 FD_CLOSEONEXEC(pair
[1]);
1854 #define MM_MEMSIZE 65536
1859 struct monitor
*mon
;
1862 mon
= xcalloc(1, sizeof(*mon
));
1864 monitor_socketpair(pair
);
1866 mon
->m_recvfd
= pair
[0];
1867 mon
->m_sendfd
= pair
[1];
1869 /* Used to share zlib space across processes */
1870 if (options
.compression
) {
1871 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1872 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1874 /* Compression needs to share state across borders */
1875 mm_init_compression(mon
->m_zlib
);
1882 monitor_reinit(struct monitor
*mon
)
1886 monitor_socketpair(pair
);
1888 mon
->m_recvfd
= pair
[0];
1889 mon
->m_sendfd
= pair
[1];
1894 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1900 goid
.elements
= buffer_get_string(m
, &len
);
1903 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1905 xfree(goid
.elements
);
1908 buffer_put_int(m
, major
);
1910 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1912 /* Now we have a context, enable the step */
1913 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1919 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1922 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1923 OM_uint32 major
, minor
;
1924 OM_uint32 flags
= 0; /* GSI needs this */
1927 in
.value
= buffer_get_string(m
, &len
);
1929 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1933 buffer_put_int(m
, major
);
1934 buffer_put_string(m
, out
.value
, out
.length
);
1935 buffer_put_int(m
, flags
);
1936 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1938 gss_release_buffer(&minor
, &out
);
1940 if (major
== GSS_S_COMPLETE
) {
1941 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1942 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1943 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1949 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1951 gss_buffer_desc gssbuf
, mic
;
1955 gssbuf
.value
= buffer_get_string(m
, &len
);
1956 gssbuf
.length
= len
;
1957 mic
.value
= buffer_get_string(m
, &len
);
1960 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1962 xfree(gssbuf
.value
);
1966 buffer_put_int(m
, ret
);
1968 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1970 if (!GSS_ERROR(ret
))
1971 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1977 mm_answer_gss_userok(int sock
, Buffer
*m
)
1981 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1984 buffer_put_int(m
, authenticated
);
1986 debug3("%s: sending result %d", __func__
, authenticated
);
1987 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1989 auth_method
= "gssapi-with-mic";
1991 /* Monitor loop will terminate if authenticated */
1992 return (authenticated
);
1998 mm_answer_jpake_step1(int sock
, Buffer
*m
)
2000 struct jpake_ctx
*pctx
;
2001 u_char
*x3_proof
, *x4_proof
;
2002 u_int x3_proof_len
, x4_proof_len
;
2004 if (!options
.zero_knowledge_password_authentication
)
2005 fatal("zero_knowledge_password_authentication disabled");
2007 if (authctxt
->jpake_ctx
!= NULL
)
2008 fatal("%s: authctxt->jpake_ctx already set (%p)",
2009 __func__
, authctxt
->jpake_ctx
);
2010 authctxt
->jpake_ctx
= pctx
= jpake_new();
2012 jpake_step1(pctx
->grp
,
2013 &pctx
->server_id
, &pctx
->server_id_len
,
2014 &pctx
->x3
, &pctx
->x4
, &pctx
->g_x3
, &pctx
->g_x4
,
2015 &x3_proof
, &x3_proof_len
,
2016 &x4_proof
, &x4_proof_len
);
2018 JPAKE_DEBUG_CTX((pctx
, "step1 done in %s", __func__
));
2022 buffer_put_string(m
, pctx
->server_id
, pctx
->server_id_len
);
2023 buffer_put_bignum2(m
, pctx
->g_x3
);
2024 buffer_put_bignum2(m
, pctx
->g_x4
);
2025 buffer_put_string(m
, x3_proof
, x3_proof_len
);
2026 buffer_put_string(m
, x4_proof
, x4_proof_len
);
2028 debug3("%s: sending step1", __func__
);
2029 mm_request_send(sock
, MONITOR_ANS_JPAKE_STEP1
, m
);
2031 bzero(x3_proof
, x3_proof_len
);
2032 bzero(x4_proof
, x4_proof_len
);
2036 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_GET_PWDATA
, 1);
2037 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_STEP1
, 0);
2043 mm_answer_jpake_get_pwdata(int sock
, Buffer
*m
)
2045 struct jpake_ctx
*pctx
= authctxt
->jpake_ctx
;
2046 char *hash_scheme
, *salt
;
2049 fatal("%s: pctx == NULL", __func__
);
2051 auth2_jpake_get_pwdata(authctxt
, &pctx
->s
, &hash_scheme
, &salt
);
2054 /* pctx->s is sensitive, not returned to slave */
2055 buffer_put_cstring(m
, hash_scheme
);
2056 buffer_put_cstring(m
, salt
);
2058 debug3("%s: sending pwdata", __func__
);
2059 mm_request_send(sock
, MONITOR_ANS_JPAKE_GET_PWDATA
, m
);
2061 bzero(hash_scheme
, strlen(hash_scheme
));
2062 bzero(salt
, strlen(salt
));
2066 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_STEP2
, 1);
2072 mm_answer_jpake_step2(int sock
, Buffer
*m
)
2074 struct jpake_ctx
*pctx
= authctxt
->jpake_ctx
;
2075 u_char
*x1_proof
, *x2_proof
, *x4_s_proof
;
2076 u_int x1_proof_len
, x2_proof_len
, x4_s_proof_len
;
2079 fatal("%s: pctx == NULL", __func__
);
2081 if ((pctx
->g_x1
= BN_new()) == NULL
||
2082 (pctx
->g_x2
= BN_new()) == NULL
)
2083 fatal("%s: BN_new", __func__
);
2084 buffer_get_bignum2(m
, pctx
->g_x1
);
2085 buffer_get_bignum2(m
, pctx
->g_x2
);
2086 pctx
->client_id
= buffer_get_string(m
, &pctx
->client_id_len
);
2087 x1_proof
= buffer_get_string(m
, &x1_proof_len
);
2088 x2_proof
= buffer_get_string(m
, &x2_proof_len
);
2090 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x3
,
2091 pctx
->g_x1
, pctx
->g_x2
, pctx
->x4
,
2092 pctx
->client_id
, pctx
->client_id_len
,
2093 pctx
->server_id
, pctx
->server_id_len
,
2094 x1_proof
, x1_proof_len
,
2095 x2_proof
, x2_proof_len
,
2097 &x4_s_proof
, &x4_s_proof_len
);
2099 JPAKE_DEBUG_CTX((pctx
, "step2 done in %s", __func__
));
2101 bzero(x1_proof
, x1_proof_len
);
2102 bzero(x2_proof
, x2_proof_len
);
2108 buffer_put_bignum2(m
, pctx
->b
);
2109 buffer_put_string(m
, x4_s_proof
, x4_s_proof_len
);
2111 debug3("%s: sending step2", __func__
);
2112 mm_request_send(sock
, MONITOR_ANS_JPAKE_STEP2
, m
);
2114 bzero(x4_s_proof
, x4_s_proof_len
);
2117 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_KEY_CONFIRM
, 1);
2123 mm_answer_jpake_key_confirm(int sock
, Buffer
*m
)
2125 struct jpake_ctx
*pctx
= authctxt
->jpake_ctx
;
2127 u_int x2_s_proof_len
;
2130 fatal("%s: pctx == NULL", __func__
);
2132 if ((pctx
->a
= BN_new()) == NULL
)
2133 fatal("%s: BN_new", __func__
);
2134 buffer_get_bignum2(m
, pctx
->a
);
2135 x2_s_proof
= buffer_get_string(m
, &x2_s_proof_len
);
2137 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->a
,
2138 pctx
->x4
, pctx
->g_x3
, pctx
->g_x4
, pctx
->g_x1
, pctx
->g_x2
,
2139 pctx
->server_id
, pctx
->server_id_len
,
2140 pctx
->client_id
, pctx
->client_id_len
,
2141 session_id2
, session_id2_len
,
2142 x2_s_proof
, x2_s_proof_len
,
2144 &pctx
->h_k_sid_sessid
, &pctx
->h_k_sid_sessid_len
);
2146 JPAKE_DEBUG_CTX((pctx
, "key_confirm done in %s", __func__
));
2148 bzero(x2_s_proof
, x2_s_proof_len
);
2151 /* pctx->k is sensitive, not sent */
2152 buffer_put_string(m
, pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
);
2154 debug3("%s: sending confirmation hash", __func__
);
2155 mm_request_send(sock
, MONITOR_ANS_JPAKE_KEY_CONFIRM
, m
);
2157 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_CHECK_CONFIRM
, 1);
2163 mm_answer_jpake_check_confirm(int sock
, Buffer
*m
)
2165 int authenticated
= 0;
2166 u_char
*peer_confirm_hash
;
2167 u_int peer_confirm_hash_len
;
2168 struct jpake_ctx
*pctx
= authctxt
->jpake_ctx
;
2171 fatal("%s: pctx == NULL", __func__
);
2173 peer_confirm_hash
= buffer_get_string(m
, &peer_confirm_hash_len
);
2175 authenticated
= jpake_check_confirm(pctx
->k
,
2176 pctx
->client_id
, pctx
->client_id_len
,
2177 session_id2
, session_id2_len
,
2178 peer_confirm_hash
, peer_confirm_hash_len
) && authctxt
->valid
;
2180 JPAKE_DEBUG_CTX((pctx
, "check_confirm done in %s", __func__
));
2182 bzero(peer_confirm_hash
, peer_confirm_hash_len
);
2183 xfree(peer_confirm_hash
);
2186 buffer_put_int(m
, authenticated
);
2188 debug3("%s: sending result %d", __func__
, authenticated
);
2189 mm_request_send(sock
, MONITOR_ANS_JPAKE_CHECK_CONFIRM
, m
);
2191 monitor_permit(mon_dispatch
, MONITOR_REQ_JPAKE_STEP1
, 1);
2193 auth_method
= "jpake-01@openssh.com";
2194 return authenticated
;