1 /* $OpenBSD: monitor.c,v 1.81 2006/07/11 20:07:25 stevesk 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>
46 #include <openssl/dh.h>
52 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
55 #define TARGET_OS_MAC 1
60 #include "auth-options.h"
69 #include "monitor_mm.h"
70 #include "monitor_wrap.h"
71 #include "monitor_fdpass.h"
81 static Gssctxt
*gsscontext
= NULL
;
85 extern ServerOptions options
;
86 extern u_int utmp_len
;
87 extern Newkeys
*current_keys
[];
88 extern z_stream incoming_stream
;
89 extern z_stream outgoing_stream
;
90 extern u_char session_id
[];
91 extern Buffer input
, output
;
92 extern Buffer auth_debug
;
93 extern int auth_debug_init
;
94 extern Buffer loginmsg
;
96 /* State exported from the child */
119 /* Functions on the monitor that answer unprivileged requests */
121 int mm_answer_moduli(int, Buffer
*);
122 int mm_answer_sign(int, Buffer
*);
123 int mm_answer_pwnamallow(int, Buffer
*);
124 int mm_answer_auth2_read_banner(int, Buffer
*);
125 int mm_answer_authserv(int, Buffer
*);
126 int mm_answer_authpassword(int, Buffer
*);
127 int mm_answer_bsdauthquery(int, Buffer
*);
128 int mm_answer_bsdauthrespond(int, Buffer
*);
129 int mm_answer_skeyquery(int, Buffer
*);
130 int mm_answer_skeyrespond(int, Buffer
*);
131 int mm_answer_keyallowed(int, Buffer
*);
132 int mm_answer_keyverify(int, Buffer
*);
133 int mm_answer_pty(int, Buffer
*);
134 int mm_answer_pty_cleanup(int, Buffer
*);
135 int mm_answer_term(int, Buffer
*);
136 int mm_answer_rsa_keyallowed(int, Buffer
*);
137 int mm_answer_rsa_challenge(int, Buffer
*);
138 int mm_answer_rsa_response(int, Buffer
*);
139 int mm_answer_sesskey(int, Buffer
*);
140 int mm_answer_sessid(int, Buffer
*);
143 int mm_answer_pam_start(int, Buffer
*);
144 int mm_answer_pam_account(int, Buffer
*);
145 int mm_answer_pam_init_ctx(int, Buffer
*);
146 int mm_answer_pam_query(int, Buffer
*);
147 int mm_answer_pam_respond(int, Buffer
*);
148 int mm_answer_pam_free_ctx(int, Buffer
*);
152 int mm_answer_gss_setup_ctx(int, Buffer
*);
153 int mm_answer_gss_accept_ctx(int, Buffer
*);
154 int mm_answer_gss_userok(int, Buffer
*);
155 int mm_answer_gss_checkmic(int, Buffer
*);
158 #ifdef SSH_AUDIT_EVENTS
159 int mm_answer_audit_event(int, Buffer
*);
160 int mm_answer_audit_command(int, Buffer
*);
163 static Authctxt
*authctxt
;
164 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
166 /* local state for key verify */
167 static u_char
*key_blob
= NULL
;
168 static u_int key_bloblen
= 0;
169 static int key_blobtype
= MM_NOKEY
;
170 static char *hostbased_cuser
= NULL
;
171 static char *hostbased_chost
= NULL
;
172 static char *auth_method
= "unknown";
173 static u_int session_id2_len
= 0;
174 static u_char
*session_id2
= NULL
;
175 static pid_t monitor_child_pid
;
178 enum monitor_reqtype type
;
180 int (*f
)(int, Buffer
*);
183 #define MON_ISAUTH 0x0004 /* Required for Authentication */
184 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
185 #define MON_ONCE 0x0010 /* Disable after calling */
186 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
188 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
190 #define MON_PERMIT 0x1000 /* Request is permitted */
192 struct mon_table mon_dispatch_proto20
[] = {
193 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
194 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
195 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
196 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
197 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
198 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
200 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
201 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
202 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
203 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
204 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
205 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
207 #ifdef SSH_AUDIT_EVENTS
208 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
211 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
212 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
215 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
216 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
218 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
219 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
221 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
222 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
223 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
224 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
229 struct mon_table mon_dispatch_postauth20
[] = {
230 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
231 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
232 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
233 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
234 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
235 #ifdef SSH_AUDIT_EVENTS
236 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
237 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
242 struct mon_table mon_dispatch_proto15
[] = {
243 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
244 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
245 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
246 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
247 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
248 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
249 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
250 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
252 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
253 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
256 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
257 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
260 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
261 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
262 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
263 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
264 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
265 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
267 #ifdef SSH_AUDIT_EVENTS
268 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
273 struct mon_table mon_dispatch_postauth15
[] = {
274 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
275 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
276 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
277 #ifdef SSH_AUDIT_EVENTS
278 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
279 {MONITOR_REQ_AUDIT_COMMAND
, MON_ONCE
, mm_answer_audit_command
},
284 struct mon_table
*mon_dispatch
;
286 /* Specifies if a certain message is allowed at the moment */
289 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
291 while (ent
->f
!= NULL
) {
292 if (ent
->type
== type
) {
293 ent
->flags
&= ~MON_PERMIT
;
294 ent
->flags
|= permit
? MON_PERMIT
: 0;
302 monitor_permit_authentications(int permit
)
304 struct mon_table
*ent
= mon_dispatch
;
306 while (ent
->f
!= NULL
) {
307 if (ent
->flags
& MON_AUTH
) {
308 ent
->flags
&= ~MON_PERMIT
;
309 ent
->flags
|= permit
? MON_PERMIT
: 0;
316 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
318 struct mon_table
*ent
;
319 int authenticated
= 0;
321 debug3("preauth child monitor started");
323 authctxt
= _authctxt
;
324 memset(authctxt
, 0, sizeof(*authctxt
));
326 authctxt
->loginmsg
= &loginmsg
;
329 mon_dispatch
= mon_dispatch_proto20
;
331 /* Permit requests for moduli and signatures */
332 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
333 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
335 mon_dispatch
= mon_dispatch_proto15
;
337 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
340 /* The first few requests do not require asynchronous access */
341 while (!authenticated
) {
342 auth_method
= "unknown";
343 authenticated
= monitor_read(pmonitor
, mon_dispatch
, &ent
);
345 if (!(ent
->flags
& MON_AUTHDECIDE
))
346 fatal("%s: unexpected authentication from %d",
347 __func__
, ent
->type
);
348 if (authctxt
->pw
->pw_uid
== 0 &&
349 !auth_root_allowed(auth_method
))
352 /* PAM needs to perform account checks after auth */
353 if (options
.use_pam
&& authenticated
) {
357 mm_request_receive_expect(pmonitor
->m_sendfd
,
358 MONITOR_REQ_PAM_ACCOUNT
, &m
);
359 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
365 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
366 auth_log(authctxt
, authenticated
, auth_method
,
367 compat20
? " ssh2" : "");
369 authctxt
->failures
++;
373 if (!authctxt
->valid
)
374 fatal("%s: authenticated invalid user", __func__
);
375 if (strcmp(auth_method
, "unknown") == 0)
376 fatal("%s: authentication method name unknown", __func__
);
378 debug("%s: %s has been authenticated by privileged process",
379 __func__
, authctxt
->user
);
381 mm_get_keystate(pmonitor
);
385 monitor_set_child_handler(pid_t pid
)
387 monitor_child_pid
= pid
;
391 monitor_child_handler(int sig
)
393 kill(monitor_child_pid
, sig
);
397 monitor_child_postauth(struct monitor
*pmonitor
)
399 monitor_set_child_handler(pmonitor
->m_pid
);
400 signal(SIGHUP
, &monitor_child_handler
);
401 signal(SIGTERM
, &monitor_child_handler
);
404 mon_dispatch
= mon_dispatch_postauth20
;
406 /* Permit requests for moduli and signatures */
407 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
408 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
409 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
411 mon_dispatch
= mon_dispatch_postauth15
;
412 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
415 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
416 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
420 monitor_read(pmonitor
, mon_dispatch
, NULL
);
424 monitor_sync(struct monitor
*pmonitor
)
426 if (options
.compression
) {
427 /* The member allocation is not visible, so sync it */
428 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
433 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
434 struct mon_table
**pent
)
442 mm_request_receive(pmonitor
->m_sendfd
, &m
);
443 type
= buffer_get_char(&m
);
445 debug3("%s: checking request %d", __func__
, type
);
447 while (ent
->f
!= NULL
) {
448 if (ent
->type
== type
)
453 if (ent
->f
!= NULL
) {
454 if (!(ent
->flags
& MON_PERMIT
))
455 fatal("%s: unpermitted request %d", __func__
,
457 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
460 /* The child may use this request only once, disable it */
461 if (ent
->flags
& MON_ONCE
) {
462 debug2("%s: %d used once, disabling now", __func__
,
464 ent
->flags
&= ~MON_PERMIT
;
473 fatal("%s: unsupported request: %d", __func__
, type
);
479 /* allowed key state */
481 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
483 /* make sure key is allowed */
484 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
485 memcmp(key_blob
, blob
, key_bloblen
))
491 monitor_reset_key_state(void)
494 if (key_blob
!= NULL
)
496 if (hostbased_cuser
!= NULL
)
497 xfree(hostbased_cuser
);
498 if (hostbased_chost
!= NULL
)
499 xfree(hostbased_chost
);
502 key_blobtype
= MM_NOKEY
;
503 hostbased_cuser
= NULL
;
504 hostbased_chost
= NULL
;
508 mm_answer_moduli(int sock
, Buffer
*m
)
513 min
= buffer_get_int(m
);
514 want
= buffer_get_int(m
);
515 max
= buffer_get_int(m
);
517 debug3("%s: got parameters: %d %d %d",
518 __func__
, min
, want
, max
);
519 /* We need to check here, too, in case the child got corrupted */
520 if (max
< min
|| want
< min
|| max
< want
)
521 fatal("%s: bad parameters: %d %d %d",
522 __func__
, min
, want
, max
);
526 dh
= choose_dh(min
, want
, max
);
528 buffer_put_char(m
, 0);
531 /* Send first bignum */
532 buffer_put_char(m
, 1);
533 buffer_put_bignum2(m
, dh
->p
);
534 buffer_put_bignum2(m
, dh
->g
);
538 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
543 mm_answer_sign(int sock
, Buffer
*m
)
548 u_int siglen
, datlen
;
551 debug3("%s", __func__
);
553 keyid
= buffer_get_int(m
);
554 p
= buffer_get_string(m
, &datlen
);
557 * Supported KEX types will only return SHA1 (20 byte) or
558 * SHA256 (32 byte) hashes
560 if (datlen
!= 20 && datlen
!= 32)
561 fatal("%s: data length incorrect: %u", __func__
, datlen
);
563 /* save session id, it will be passed on the first call */
564 if (session_id2_len
== 0) {
565 session_id2_len
= datlen
;
566 session_id2
= xmalloc(session_id2_len
);
567 memcpy(session_id2
, p
, session_id2_len
);
570 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
571 fatal("%s: no hostkey from index %d", __func__
, keyid
);
572 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
573 fatal("%s: key_sign failed", __func__
);
575 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
578 buffer_put_string(m
, signature
, siglen
);
583 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
585 /* Turn on permissions for getpwnam */
586 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
591 /* Retrieves the password entry and also checks if the user is permitted */
594 mm_answer_pwnamallow(int sock
, Buffer
*m
)
597 struct passwd
*pwent
;
600 debug3("%s", __func__
);
602 if (authctxt
->attempt
++ != 0)
603 fatal("%s: multiple attempts for getpwnam", __func__
);
605 username
= buffer_get_string(m
, NULL
);
607 pwent
= getpwnamallow(username
);
609 authctxt
->user
= xstrdup(username
);
610 setproctitle("%s [priv]", pwent
? username
: "unknown");
616 buffer_put_char(m
, 0);
617 authctxt
->pw
= fakepw();
622 authctxt
->pw
= pwent
;
625 buffer_put_char(m
, 1);
626 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
627 buffer_put_cstring(m
, pwent
->pw_name
);
628 buffer_put_cstring(m
, "*");
629 buffer_put_cstring(m
, pwent
->pw_gecos
);
630 #ifdef HAVE_PW_CLASS_IN_PASSWD
631 buffer_put_cstring(m
, pwent
->pw_class
);
633 buffer_put_cstring(m
, pwent
->pw_dir
);
634 buffer_put_cstring(m
, pwent
->pw_shell
);
637 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
638 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
640 /* For SSHv1 allow authentication now */
642 monitor_permit_authentications(1);
644 /* Allow service/style information on the auth context */
645 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
646 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
651 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
653 #ifdef SSH_AUDIT_EVENTS
654 monitor_permit(mon_dispatch
, MONITOR_REQ_AUDIT_COMMAND
, 1);
660 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
665 banner
= auth2_read_banner();
666 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
667 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
676 mm_answer_authserv(int sock
, Buffer
*m
)
678 monitor_permit_authentications(1);
680 authctxt
->service
= buffer_get_string(m
, NULL
);
681 authctxt
->style
= buffer_get_string(m
, NULL
);
682 debug3("%s: service=%s, style=%s",
683 __func__
, authctxt
->service
, authctxt
->style
);
685 if (strlen(authctxt
->style
) == 0) {
686 xfree(authctxt
->style
);
687 authctxt
->style
= NULL
;
694 mm_answer_authpassword(int sock
, Buffer
*m
)
696 static int call_count
;
701 passwd
= buffer_get_string(m
, &plen
);
702 /* Only authenticate if the context is valid */
703 authenticated
= options
.password_authentication
&&
704 auth_password(authctxt
, passwd
);
705 memset(passwd
, 0, strlen(passwd
));
709 buffer_put_int(m
, authenticated
);
711 debug3("%s: sending result %d", __func__
, authenticated
);
712 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
715 if (plen
== 0 && call_count
== 1)
716 auth_method
= "none";
718 auth_method
= "password";
720 /* Causes monitor loop to terminate if authenticated */
721 return (authenticated
);
726 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
728 char *name
, *infotxt
;
734 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
735 &prompts
, &echo_on
) < 0 ? 0 : 1;
738 buffer_put_int(m
, success
);
740 buffer_put_cstring(m
, prompts
[0]);
742 debug3("%s: sending challenge success: %u", __func__
, success
);
743 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
756 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
761 if (authctxt
->as
== 0)
762 fatal("%s: no bsd auth session", __func__
);
764 response
= buffer_get_string(m
, NULL
);
765 authok
= options
.challenge_response_authentication
&&
766 auth_userresponse(authctxt
->as
, response
, 0);
768 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
772 buffer_put_int(m
, authok
);
774 debug3("%s: sending authenticated: %d", __func__
, authok
);
775 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
777 auth_method
= "bsdauth";
779 return (authok
!= 0);
785 mm_answer_skeyquery(int sock
, Buffer
*m
)
788 char challenge
[1024];
791 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
792 sizeof(challenge
)) < 0 ? 0 : 1;
795 buffer_put_int(m
, success
);
797 buffer_put_cstring(m
, challenge
);
799 debug3("%s: sending challenge success: %u", __func__
, success
);
800 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
806 mm_answer_skeyrespond(int sock
, Buffer
*m
)
811 response
= buffer_get_string(m
, NULL
);
813 authok
= (options
.challenge_response_authentication
&&
815 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
816 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
821 buffer_put_int(m
, authok
);
823 debug3("%s: sending authenticated: %d", __func__
, authok
);
824 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
826 auth_method
= "skey";
828 return (authok
!= 0);
834 mm_answer_pam_start(int sock
, Buffer
*m
)
836 if (!options
.use_pam
)
837 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
841 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
847 mm_answer_pam_account(int sock
, Buffer
*m
)
851 if (!options
.use_pam
)
852 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
854 ret
= do_pam_account();
856 buffer_put_int(m
, ret
);
857 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
859 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
864 static void *sshpam_ctxt
, *sshpam_authok
;
865 extern KbdintDevice sshpam_device
;
868 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
871 debug3("%s", __func__
);
872 authctxt
->user
= buffer_get_string(m
, NULL
);
873 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
874 sshpam_authok
= NULL
;
876 if (sshpam_ctxt
!= NULL
) {
877 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
878 buffer_put_int(m
, 1);
880 buffer_put_int(m
, 0);
882 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
887 mm_answer_pam_query(int sock
, Buffer
*m
)
889 char *name
, *info
, **prompts
;
890 u_int i
, num
, *echo_on
;
893 debug3("%s", __func__
);
894 sshpam_authok
= NULL
;
895 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
896 if (ret
== 0 && num
== 0)
897 sshpam_authok
= sshpam_ctxt
;
898 if (num
> 1 || name
== NULL
|| info
== NULL
)
901 buffer_put_int(m
, ret
);
902 buffer_put_cstring(m
, name
);
904 buffer_put_cstring(m
, info
);
906 buffer_put_int(m
, num
);
907 for (i
= 0; i
< num
; ++i
) {
908 buffer_put_cstring(m
, prompts
[i
]);
910 buffer_put_int(m
, echo_on
[i
]);
916 auth_method
= "keyboard-interactive/pam";
917 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
922 mm_answer_pam_respond(int sock
, Buffer
*m
)
928 debug3("%s", __func__
);
929 sshpam_authok
= NULL
;
930 num
= buffer_get_int(m
);
932 resp
= xcalloc(num
, sizeof(char *));
933 for (i
= 0; i
< num
; ++i
)
934 resp
[i
] = buffer_get_string(m
, NULL
);
935 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
936 for (i
= 0; i
< num
; ++i
)
940 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
943 buffer_put_int(m
, ret
);
944 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
945 auth_method
= "keyboard-interactive/pam";
947 sshpam_authok
= sshpam_ctxt
;
952 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
955 debug3("%s", __func__
);
956 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
958 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
959 auth_method
= "keyboard-interactive/pam";
960 return (sshpam_authok
== sshpam_ctxt
);
965 mm_append_debug(Buffer
*m
)
967 if (auth_debug_init
&& buffer_len(&auth_debug
)) {
968 debug3("%s: Appending debug messages for child", __func__
);
969 buffer_append(m
, buffer_ptr(&auth_debug
),
970 buffer_len(&auth_debug
));
971 buffer_clear(&auth_debug
);
976 mm_answer_keyallowed(int sock
, Buffer
*m
)
982 enum mm_keytype type
= 0;
985 debug3("%s entering", __func__
);
987 type
= buffer_get_int(m
);
988 cuser
= buffer_get_string(m
, NULL
);
989 chost
= buffer_get_string(m
, NULL
);
990 blob
= buffer_get_string(m
, &bloblen
);
992 key
= key_from_blob(blob
, bloblen
);
994 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
995 (!compat20
&& type
!= MM_RSAHOSTKEY
))
996 fatal("%s: key type and protocol mismatch", __func__
);
998 debug3("%s: key_from_blob: %p", __func__
, key
);
1000 if (key
!= NULL
&& authctxt
->valid
) {
1003 allowed
= options
.pubkey_authentication
&&
1004 user_key_allowed(authctxt
->pw
, key
);
1005 auth_method
= "publickey";
1008 allowed
= options
.hostbased_authentication
&&
1009 hostbased_key_allowed(authctxt
->pw
,
1011 auth_method
= "hostbased";
1014 key
->type
= KEY_RSA1
; /* XXX */
1015 allowed
= options
.rhosts_rsa_authentication
&&
1016 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1018 auth_method
= "rsa";
1021 fatal("%s: unknown key type %d", __func__
, type
);
1028 /* clear temporarily storage (used by verify) */
1029 monitor_reset_key_state();
1032 /* Save temporarily for comparison in verify */
1034 key_bloblen
= bloblen
;
1035 key_blobtype
= type
;
1036 hostbased_cuser
= cuser
;
1037 hostbased_chost
= chost
;
1039 /* Log failed attempt */
1040 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1046 debug3("%s: key %p is %s",
1047 __func__
, key
, allowed
? "allowed" : "disallowed");
1050 buffer_put_int(m
, allowed
);
1051 buffer_put_int(m
, forced_command
!= NULL
);
1055 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1057 if (type
== MM_RSAHOSTKEY
)
1058 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1064 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1072 buffer_append(&b
, data
, datalen
);
1074 if (datafellows
& SSH_OLD_SESSIONID
) {
1076 len
= buffer_len(&b
);
1077 if ((session_id2
== NULL
) ||
1078 (len
< session_id2_len
) ||
1079 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1081 buffer_consume(&b
, session_id2_len
);
1083 p
= buffer_get_string(&b
, &len
);
1084 if ((session_id2
== NULL
) ||
1085 (len
!= session_id2_len
) ||
1086 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1090 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1092 p
= buffer_get_string(&b
, NULL
);
1093 if (strcmp(authctxt
->user
, p
) != 0) {
1094 logit("wrong user name passed to monitor: expected %s != %.100s",
1099 buffer_skip_string(&b
);
1100 if (datafellows
& SSH_BUG_PKAUTH
) {
1101 if (!buffer_get_char(&b
))
1104 p
= buffer_get_string(&b
, NULL
);
1105 if (strcmp("publickey", p
) != 0)
1108 if (!buffer_get_char(&b
))
1110 buffer_skip_string(&b
);
1112 buffer_skip_string(&b
);
1113 if (buffer_len(&b
) != 0)
1120 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1129 buffer_append(&b
, data
, datalen
);
1131 p
= buffer_get_string(&b
, &len
);
1132 if ((session_id2
== NULL
) ||
1133 (len
!= session_id2_len
) ||
1134 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1138 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1140 p
= buffer_get_string(&b
, NULL
);
1141 if (strcmp(authctxt
->user
, p
) != 0) {
1142 logit("wrong user name passed to monitor: expected %s != %.100s",
1147 buffer_skip_string(&b
); /* service */
1148 p
= buffer_get_string(&b
, NULL
);
1149 if (strcmp(p
, "hostbased") != 0)
1152 buffer_skip_string(&b
); /* pkalg */
1153 buffer_skip_string(&b
); /* pkblob */
1155 /* verify client host, strip trailing dot if necessary */
1156 p
= buffer_get_string(&b
, NULL
);
1157 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1159 if (strcmp(p
, chost
) != 0)
1163 /* verify client user */
1164 p
= buffer_get_string(&b
, NULL
);
1165 if (strcmp(p
, cuser
) != 0)
1169 if (buffer_len(&b
) != 0)
1176 mm_answer_keyverify(int sock
, Buffer
*m
)
1179 u_char
*signature
, *data
, *blob
;
1180 u_int signaturelen
, datalen
, bloblen
;
1184 blob
= buffer_get_string(m
, &bloblen
);
1185 signature
= buffer_get_string(m
, &signaturelen
);
1186 data
= buffer_get_string(m
, &datalen
);
1188 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1189 !monitor_allowed_key(blob
, bloblen
))
1190 fatal("%s: bad key, not previously allowed", __func__
);
1192 key
= key_from_blob(blob
, bloblen
);
1194 fatal("%s: bad public key blob", __func__
);
1196 switch (key_blobtype
) {
1198 valid_data
= monitor_valid_userblob(data
, datalen
);
1201 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1202 hostbased_cuser
, hostbased_chost
);
1209 fatal("%s: bad signature data blob", __func__
);
1211 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1212 debug3("%s: key %p signature %s",
1213 __func__
, key
, verified
? "verified" : "unverified");
1220 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1222 monitor_reset_key_state();
1225 buffer_put_int(m
, verified
);
1226 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1232 mm_record_login(Session
*s
, struct passwd
*pw
)
1235 struct sockaddr_storage from
;
1238 * Get IP address of client. If the connection is not a socket, let
1239 * the address be 0.0.0.0.
1241 memset(&from
, 0, sizeof(from
));
1242 fromlen
= sizeof(from
);
1243 if (packet_connection_is_on_socket()) {
1244 if (getpeername(packet_get_connection_in(),
1245 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1246 debug("getpeername: %.100s", strerror(errno
));
1250 /* Record that there was a login on that tty from the remote host. */
1251 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1252 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1253 (struct sockaddr
*)&from
, fromlen
);
1257 mm_session_close(Session
*s
)
1259 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1260 if (s
->ttyfd
!= -1) {
1261 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1262 session_pty_cleanup2(s
);
1268 mm_answer_pty(int sock
, Buffer
*m
)
1270 extern struct monitor
*pmonitor
;
1274 debug3("%s entering", __func__
);
1280 s
->authctxt
= authctxt
;
1281 s
->pw
= authctxt
->pw
;
1282 s
->pid
= pmonitor
->m_pid
;
1283 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1286 pty_setowner(authctxt
->pw
, s
->tty
);
1288 buffer_put_int(m
, 1);
1289 buffer_put_cstring(m
, s
->tty
);
1291 /* We need to trick ttyslot */
1292 if (dup2(s
->ttyfd
, 0) == -1)
1293 fatal("%s: dup2", __func__
);
1295 mm_record_login(s
, authctxt
->pw
);
1297 /* Now we can close the file descriptor again */
1300 /* send messages generated by record_login */
1301 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1302 buffer_clear(&loginmsg
);
1304 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1306 mm_send_fd(sock
, s
->ptyfd
);
1307 mm_send_fd(sock
, s
->ttyfd
);
1309 /* make sure nothing uses fd 0 */
1310 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1311 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1313 error("%s: fd0 %d != 0", __func__
, fd0
);
1315 /* slave is not needed */
1317 s
->ttyfd
= s
->ptyfd
;
1318 /* no need to dup() because nobody closes ptyfd */
1319 s
->ptymaster
= s
->ptyfd
;
1321 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1327 mm_session_close(s
);
1328 buffer_put_int(m
, 0);
1329 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1334 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1339 debug3("%s entering", __func__
);
1341 tty
= buffer_get_string(m
, NULL
);
1342 if ((s
= session_by_tty(tty
)) != NULL
)
1343 mm_session_close(s
);
1350 mm_answer_sesskey(int sock
, Buffer
*m
)
1355 /* Turn off permissions */
1356 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1358 if ((p
= BN_new()) == NULL
)
1359 fatal("%s: BN_new", __func__
);
1361 buffer_get_bignum2(m
, p
);
1363 rsafail
= ssh1_session_key(p
);
1366 buffer_put_int(m
, rsafail
);
1367 buffer_put_bignum2(m
, p
);
1371 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1373 /* Turn on permissions for sessid passing */
1374 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1380 mm_answer_sessid(int sock
, Buffer
*m
)
1384 debug3("%s entering", __func__
);
1386 if (buffer_len(m
) != 16)
1387 fatal("%s: bad ssh1 session id", __func__
);
1388 for (i
= 0; i
< 16; i
++)
1389 session_id
[i
] = buffer_get_char(m
);
1391 /* Turn on permissions for getpwnam */
1392 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1398 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1402 u_char
*blob
= NULL
;
1406 debug3("%s entering", __func__
);
1408 auth_method
= "rsa";
1409 if (options
.rsa_authentication
&& authctxt
->valid
) {
1410 if ((client_n
= BN_new()) == NULL
)
1411 fatal("%s: BN_new", __func__
);
1412 buffer_get_bignum2(m
, client_n
);
1413 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1414 BN_clear_free(client_n
);
1417 buffer_put_int(m
, allowed
);
1418 buffer_put_int(m
, forced_command
!= NULL
);
1420 /* clear temporarily storage (used by generate challenge) */
1421 monitor_reset_key_state();
1423 if (allowed
&& key
!= NULL
) {
1424 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1425 if (key_to_blob(key
, &blob
, &blen
) == 0)
1426 fatal("%s: key_to_blob failed", __func__
);
1427 buffer_put_string(m
, blob
, blen
);
1429 /* Save temporarily for comparison in verify */
1432 key_blobtype
= MM_RSAUSERKEY
;
1439 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1441 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1442 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1447 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1453 debug3("%s entering", __func__
);
1455 if (!authctxt
->valid
)
1456 fatal("%s: authctxt not valid", __func__
);
1457 blob
= buffer_get_string(m
, &blen
);
1458 if (!monitor_allowed_key(blob
, blen
))
1459 fatal("%s: bad key, not previously allowed", __func__
);
1460 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1461 fatal("%s: key type mismatch", __func__
);
1462 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1463 fatal("%s: received bad key", __func__
);
1466 BN_clear_free(ssh1_challenge
);
1467 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1470 buffer_put_bignum2(m
, ssh1_challenge
);
1472 debug3("%s sending reply", __func__
);
1473 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1475 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1483 mm_answer_rsa_response(int sock
, Buffer
*m
)
1486 u_char
*blob
, *response
;
1490 debug3("%s entering", __func__
);
1492 if (!authctxt
->valid
)
1493 fatal("%s: authctxt not valid", __func__
);
1494 if (ssh1_challenge
== NULL
)
1495 fatal("%s: no ssh1_challenge", __func__
);
1497 blob
= buffer_get_string(m
, &blen
);
1498 if (!monitor_allowed_key(blob
, blen
))
1499 fatal("%s: bad key, not previously allowed", __func__
);
1500 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1501 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1502 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1503 fatal("%s: received bad key", __func__
);
1504 response
= buffer_get_string(m
, &len
);
1506 fatal("%s: received bad response to challenge", __func__
);
1507 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1513 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1516 BN_clear_free(ssh1_challenge
);
1517 ssh1_challenge
= NULL
;
1518 monitor_reset_key_state();
1521 buffer_put_int(m
, success
);
1522 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1528 mm_answer_term(int sock
, Buffer
*req
)
1530 extern struct monitor
*pmonitor
;
1533 debug3("%s: tearing down sessions", __func__
);
1535 /* The child is terminating */
1536 session_destroy_all(&mm_session_close
);
1538 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1542 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1544 /* Terminate process */
1548 #ifdef SSH_AUDIT_EVENTS
1549 /* Report that an audit event occurred */
1551 mm_answer_audit_event(int socket
, Buffer
*m
)
1553 ssh_audit_event_t event
;
1555 debug3("%s entering", __func__
);
1557 event
= buffer_get_int(m
);
1559 case SSH_AUTH_FAIL_PUBKEY
:
1560 case SSH_AUTH_FAIL_HOSTBASED
:
1561 case SSH_AUTH_FAIL_GSSAPI
:
1562 case SSH_LOGIN_EXCEED_MAXTRIES
:
1563 case SSH_LOGIN_ROOT_DENIED
:
1564 case SSH_CONNECTION_CLOSE
:
1565 case SSH_INVALID_USER
:
1569 fatal("Audit event type %d not permitted", event
);
1576 mm_answer_audit_command(int socket
, Buffer
*m
)
1581 debug3("%s entering", __func__
);
1582 cmd
= buffer_get_string(m
, &len
);
1583 /* sanity check command, if so how? */
1584 audit_run_command(cmd
);
1588 #endif /* SSH_AUDIT_EVENTS */
1591 monitor_apply_keystate(struct monitor
*pmonitor
)
1594 set_newkeys(MODE_IN
);
1595 set_newkeys(MODE_OUT
);
1597 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1598 packet_set_encryption_key(child_state
.ssh1key
,
1599 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1600 xfree(child_state
.ssh1key
);
1603 /* for rc4 and other stateful ciphers */
1604 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1605 xfree(child_state
.keyout
);
1606 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1607 xfree(child_state
.keyin
);
1610 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1611 xfree(child_state
.ivout
);
1612 packet_set_iv(MODE_IN
, child_state
.ivin
);
1613 xfree(child_state
.ivin
);
1616 memcpy(&incoming_stream
, &child_state
.incoming
,
1617 sizeof(incoming_stream
));
1618 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1619 sizeof(outgoing_stream
));
1621 /* Update with new address */
1622 if (options
.compression
)
1623 mm_init_compression(pmonitor
->m_zlib
);
1625 /* Network I/O buffers */
1626 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1627 buffer_clear(&input
);
1628 buffer_append(&input
, child_state
.input
, child_state
.ilen
);
1629 memset(child_state
.input
, 0, child_state
.ilen
);
1630 xfree(child_state
.input
);
1632 buffer_clear(&output
);
1633 buffer_append(&output
, child_state
.output
, child_state
.olen
);
1634 memset(child_state
.output
, 0, child_state
.olen
);
1635 xfree(child_state
.output
);
1639 mm_get_kex(Buffer
*m
)
1645 kex
= xcalloc(1, sizeof(*kex
));
1646 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1647 if ((session_id2
== NULL
) ||
1648 (kex
->session_id_len
!= session_id2_len
) ||
1649 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1650 fatal("mm_get_get: internal error: bad session id");
1651 kex
->we_need
= buffer_get_int(m
);
1652 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1653 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1654 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1655 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1657 kex
->hostkey_type
= buffer_get_int(m
);
1658 kex
->kex_type
= buffer_get_int(m
);
1659 blob
= buffer_get_string(m
, &bloblen
);
1660 buffer_init(&kex
->my
);
1661 buffer_append(&kex
->my
, blob
, bloblen
);
1663 blob
= buffer_get_string(m
, &bloblen
);
1664 buffer_init(&kex
->peer
);
1665 buffer_append(&kex
->peer
, blob
, bloblen
);
1668 kex
->flags
= buffer_get_int(m
);
1669 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1670 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1671 kex
->load_host_key
=&get_hostkey_by_type
;
1672 kex
->host_key_index
=&get_hostkey_index
;
1677 /* This function requries careful sanity checking */
1680 mm_get_keystate(struct monitor
*pmonitor
)
1684 u_int bloblen
, plen
;
1685 u_int32_t seqnr
, packets
;
1688 debug3("%s: Waiting for new keys", __func__
);
1691 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1693 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1694 child_state
.ssh1cipher
= buffer_get_int(&m
);
1695 child_state
.ssh1key
= buffer_get_string(&m
,
1696 &child_state
.ssh1keylen
);
1697 child_state
.ivout
= buffer_get_string(&m
,
1698 &child_state
.ivoutlen
);
1699 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1702 /* Get the Kex for rekeying */
1703 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1706 blob
= buffer_get_string(&m
, &bloblen
);
1707 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1710 debug3("%s: Waiting for second key", __func__
);
1711 blob
= buffer_get_string(&m
, &bloblen
);
1712 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1715 /* Now get sequence numbers for the packets */
1716 seqnr
= buffer_get_int(&m
);
1717 blocks
= buffer_get_int64(&m
);
1718 packets
= buffer_get_int(&m
);
1719 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
);
1720 seqnr
= buffer_get_int(&m
);
1721 blocks
= buffer_get_int64(&m
);
1722 packets
= buffer_get_int(&m
);
1723 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
);
1726 /* Get the key context */
1727 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1728 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1730 debug3("%s: Getting compression state", __func__
);
1731 /* Get compression state */
1732 p
= buffer_get_string(&m
, &plen
);
1733 if (plen
!= sizeof(child_state
.outgoing
))
1734 fatal("%s: bad request size", __func__
);
1735 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1738 p
= buffer_get_string(&m
, &plen
);
1739 if (plen
!= sizeof(child_state
.incoming
))
1740 fatal("%s: bad request size", __func__
);
1741 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1744 /* Network I/O buffers */
1745 debug3("%s: Getting Network I/O buffers", __func__
);
1746 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1747 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1753 /* Allocation functions for zlib */
1755 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1757 size_t len
= (size_t) size
* ncount
;
1760 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1761 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1763 address
= mm_malloc(mm
, len
);
1769 mm_zfree(struct mm_master
*mm
, void *address
)
1771 mm_free(mm
, address
);
1775 mm_init_compression(struct mm_master
*mm
)
1777 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1778 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1779 outgoing_stream
.opaque
= mm
;
1781 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1782 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1783 incoming_stream
.opaque
= mm
;
1788 #define FD_CLOSEONEXEC(x) do { \
1789 if (fcntl(x, F_SETFD, 1) == -1) \
1790 fatal("fcntl(%d, F_SETFD)", x); \
1794 monitor_socketpair(int *pair
)
1796 #ifdef HAVE_SOCKETPAIR
1797 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1798 fatal("%s: socketpair", __func__
);
1800 fatal("%s: UsePrivilegeSeparation=yes not supported",
1803 FD_CLOSEONEXEC(pair
[0]);
1804 FD_CLOSEONEXEC(pair
[1]);
1807 #define MM_MEMSIZE 65536
1812 struct monitor
*mon
;
1815 mon
= xcalloc(1, sizeof(*mon
));
1817 monitor_socketpair(pair
);
1819 mon
->m_recvfd
= pair
[0];
1820 mon
->m_sendfd
= pair
[1];
1822 /* Used to share zlib space across processes */
1823 if (options
.compression
) {
1824 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1825 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1827 /* Compression needs to share state across borders */
1828 mm_init_compression(mon
->m_zlib
);
1835 monitor_reinit(struct monitor
*mon
)
1839 monitor_socketpair(pair
);
1841 mon
->m_recvfd
= pair
[0];
1842 mon
->m_sendfd
= pair
[1];
1847 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1853 goid
.elements
= buffer_get_string(m
, &len
);
1856 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1858 xfree(goid
.elements
);
1861 buffer_put_int(m
, major
);
1863 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1865 /* Now we have a context, enable the step */
1866 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1872 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1875 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1876 OM_uint32 major
, minor
;
1877 OM_uint32 flags
= 0; /* GSI needs this */
1880 in
.value
= buffer_get_string(m
, &len
);
1882 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1886 buffer_put_int(m
, major
);
1887 buffer_put_string(m
, out
.value
, out
.length
);
1888 buffer_put_int(m
, flags
);
1889 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1891 gss_release_buffer(&minor
, &out
);
1893 if (major
== GSS_S_COMPLETE
) {
1894 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1895 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1896 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1902 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1904 gss_buffer_desc gssbuf
, mic
;
1908 gssbuf
.value
= buffer_get_string(m
, &len
);
1909 gssbuf
.length
= len
;
1910 mic
.value
= buffer_get_string(m
, &len
);
1913 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1915 xfree(gssbuf
.value
);
1919 buffer_put_int(m
, ret
);
1921 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1923 if (!GSS_ERROR(ret
))
1924 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1930 mm_answer_gss_userok(int sock
, Buffer
*m
)
1934 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1937 buffer_put_int(m
, authenticated
);
1939 debug3("%s: sending result %d", __func__
, authenticated
);
1940 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1942 auth_method
= "gssapi-with-mic";
1944 /* Monitor loop will terminate if authenticated */
1945 return (authenticated
);