1 /* $OpenBSD: monitor.c,v 1.77 2006/03/30 11:40:21 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>
42 #include <openssl/dh.h>
48 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
51 #define TARGET_OS_MAC 1
56 #include "auth-options.h"
65 #include "monitor_mm.h"
66 #include "monitor_wrap.h"
67 #include "monitor_fdpass.h"
77 static Gssctxt
*gsscontext
= NULL
;
81 extern ServerOptions options
;
82 extern u_int utmp_len
;
83 extern Newkeys
*current_keys
[];
84 extern z_stream incoming_stream
;
85 extern z_stream outgoing_stream
;
86 extern u_char session_id
[];
87 extern Buffer input
, output
;
88 extern Buffer auth_debug
;
89 extern int auth_debug_init
;
90 extern Buffer loginmsg
;
92 /* State exported from the child */
115 /* Functions on the monitor that answer unprivileged requests */
117 int mm_answer_moduli(int, Buffer
*);
118 int mm_answer_sign(int, Buffer
*);
119 int mm_answer_pwnamallow(int, Buffer
*);
120 int mm_answer_auth2_read_banner(int, Buffer
*);
121 int mm_answer_authserv(int, Buffer
*);
122 int mm_answer_authpassword(int, Buffer
*);
123 int mm_answer_bsdauthquery(int, Buffer
*);
124 int mm_answer_bsdauthrespond(int, Buffer
*);
125 int mm_answer_skeyquery(int, Buffer
*);
126 int mm_answer_skeyrespond(int, Buffer
*);
127 int mm_answer_keyallowed(int, Buffer
*);
128 int mm_answer_keyverify(int, Buffer
*);
129 int mm_answer_pty(int, Buffer
*);
130 int mm_answer_pty_cleanup(int, Buffer
*);
131 int mm_answer_term(int, Buffer
*);
132 int mm_answer_rsa_keyallowed(int, Buffer
*);
133 int mm_answer_rsa_challenge(int, Buffer
*);
134 int mm_answer_rsa_response(int, Buffer
*);
135 int mm_answer_sesskey(int, Buffer
*);
136 int mm_answer_sessid(int, Buffer
*);
139 int mm_answer_pam_start(int, Buffer
*);
140 int mm_answer_pam_account(int, Buffer
*);
141 int mm_answer_pam_init_ctx(int, Buffer
*);
142 int mm_answer_pam_query(int, Buffer
*);
143 int mm_answer_pam_respond(int, Buffer
*);
144 int mm_answer_pam_free_ctx(int, Buffer
*);
148 int mm_answer_gss_setup_ctx(int, Buffer
*);
149 int mm_answer_gss_accept_ctx(int, Buffer
*);
150 int mm_answer_gss_userok(int, Buffer
*);
151 int mm_answer_gss_checkmic(int, Buffer
*);
154 #ifdef SSH_AUDIT_EVENTS
155 int mm_answer_audit_event(int, Buffer
*);
156 int mm_answer_audit_command(int, Buffer
*);
159 static Authctxt
*authctxt
;
160 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
162 /* local state for key verify */
163 static u_char
*key_blob
= NULL
;
164 static u_int key_bloblen
= 0;
165 static int key_blobtype
= MM_NOKEY
;
166 static char *hostbased_cuser
= NULL
;
167 static char *hostbased_chost
= NULL
;
168 static char *auth_method
= "unknown";
169 static u_int session_id2_len
= 0;
170 static u_char
*session_id2
= NULL
;
171 static pid_t monitor_child_pid
;
174 enum monitor_reqtype type
;
176 int (*f
)(int, Buffer
*);
179 #define MON_ISAUTH 0x0004 /* Required for Authentication */
180 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
181 #define MON_ONCE 0x0010 /* Disable after calling */
182 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
184 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
186 #define MON_PERMIT 0x1000 /* Request is permitted */
188 struct mon_table mon_dispatch_proto20
[] = {
189 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
190 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
191 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
192 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
193 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
194 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
196 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
197 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
198 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
199 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
200 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
201 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
203 #ifdef SSH_AUDIT_EVENTS
204 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
207 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
208 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
211 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
212 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
214 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
215 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
217 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
218 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
219 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
220 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
225 struct mon_table mon_dispatch_postauth20
[] = {
226 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
227 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
228 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
229 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
230 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
231 #ifdef SSH_AUDIT_EVENTS
232 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
233 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
238 struct mon_table mon_dispatch_proto15
[] = {
239 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
240 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
241 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
242 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
243 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
244 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
245 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
246 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
248 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
249 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
252 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
253 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
256 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
257 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
258 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
259 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
260 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
261 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
263 #ifdef SSH_AUDIT_EVENTS
264 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
269 struct mon_table mon_dispatch_postauth15
[] = {
270 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
271 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
272 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
273 #ifdef SSH_AUDIT_EVENTS
274 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
275 {MONITOR_REQ_AUDIT_COMMAND
, MON_ONCE
, mm_answer_audit_command
},
280 struct mon_table
*mon_dispatch
;
282 /* Specifies if a certain message is allowed at the moment */
285 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
287 while (ent
->f
!= NULL
) {
288 if (ent
->type
== type
) {
289 ent
->flags
&= ~MON_PERMIT
;
290 ent
->flags
|= permit
? MON_PERMIT
: 0;
298 monitor_permit_authentications(int permit
)
300 struct mon_table
*ent
= mon_dispatch
;
302 while (ent
->f
!= NULL
) {
303 if (ent
->flags
& MON_AUTH
) {
304 ent
->flags
&= ~MON_PERMIT
;
305 ent
->flags
|= permit
? MON_PERMIT
: 0;
312 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
314 struct mon_table
*ent
;
315 int authenticated
= 0;
317 debug3("preauth child monitor started");
319 authctxt
= _authctxt
;
320 memset(authctxt
, 0, sizeof(*authctxt
));
322 authctxt
->loginmsg
= &loginmsg
;
325 mon_dispatch
= mon_dispatch_proto20
;
327 /* Permit requests for moduli and signatures */
328 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
329 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
331 mon_dispatch
= mon_dispatch_proto15
;
333 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
336 /* The first few requests do not require asynchronous access */
337 while (!authenticated
) {
338 auth_method
= "unknown";
339 authenticated
= monitor_read(pmonitor
, mon_dispatch
, &ent
);
341 if (!(ent
->flags
& MON_AUTHDECIDE
))
342 fatal("%s: unexpected authentication from %d",
343 __func__
, ent
->type
);
344 if (authctxt
->pw
->pw_uid
== 0 &&
345 !auth_root_allowed(auth_method
))
348 /* PAM needs to perform account checks after auth */
349 if (options
.use_pam
&& authenticated
) {
353 mm_request_receive_expect(pmonitor
->m_sendfd
,
354 MONITOR_REQ_PAM_ACCOUNT
, &m
);
355 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
361 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
362 auth_log(authctxt
, authenticated
, auth_method
,
363 compat20
? " ssh2" : "");
365 authctxt
->failures
++;
369 if (!authctxt
->valid
)
370 fatal("%s: authenticated invalid user", __func__
);
371 if (strcmp(auth_method
, "unknown") == 0)
372 fatal("%s: authentication method name unknown", __func__
);
374 debug("%s: %s has been authenticated by privileged process",
375 __func__
, authctxt
->user
);
377 mm_get_keystate(pmonitor
);
381 monitor_set_child_handler(pid_t pid
)
383 monitor_child_pid
= pid
;
387 monitor_child_handler(int sig
)
389 kill(monitor_child_pid
, sig
);
393 monitor_child_postauth(struct monitor
*pmonitor
)
395 monitor_set_child_handler(pmonitor
->m_pid
);
396 signal(SIGHUP
, &monitor_child_handler
);
397 signal(SIGTERM
, &monitor_child_handler
);
400 mon_dispatch
= mon_dispatch_postauth20
;
402 /* Permit requests for moduli and signatures */
403 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
404 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
405 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
407 mon_dispatch
= mon_dispatch_postauth15
;
408 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
411 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
412 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
416 monitor_read(pmonitor
, mon_dispatch
, NULL
);
420 monitor_sync(struct monitor
*pmonitor
)
422 if (options
.compression
) {
423 /* The member allocation is not visible, so sync it */
424 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
429 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
430 struct mon_table
**pent
)
438 mm_request_receive(pmonitor
->m_sendfd
, &m
);
439 type
= buffer_get_char(&m
);
441 debug3("%s: checking request %d", __func__
, type
);
443 while (ent
->f
!= NULL
) {
444 if (ent
->type
== type
)
449 if (ent
->f
!= NULL
) {
450 if (!(ent
->flags
& MON_PERMIT
))
451 fatal("%s: unpermitted request %d", __func__
,
453 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
456 /* The child may use this request only once, disable it */
457 if (ent
->flags
& MON_ONCE
) {
458 debug2("%s: %d used once, disabling now", __func__
,
460 ent
->flags
&= ~MON_PERMIT
;
469 fatal("%s: unsupported request: %d", __func__
, type
);
475 /* allowed key state */
477 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
479 /* make sure key is allowed */
480 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
481 memcmp(key_blob
, blob
, key_bloblen
))
487 monitor_reset_key_state(void)
490 if (key_blob
!= NULL
)
492 if (hostbased_cuser
!= NULL
)
493 xfree(hostbased_cuser
);
494 if (hostbased_chost
!= NULL
)
495 xfree(hostbased_chost
);
498 key_blobtype
= MM_NOKEY
;
499 hostbased_cuser
= NULL
;
500 hostbased_chost
= NULL
;
504 mm_answer_moduli(int sock
, Buffer
*m
)
509 min
= buffer_get_int(m
);
510 want
= buffer_get_int(m
);
511 max
= buffer_get_int(m
);
513 debug3("%s: got parameters: %d %d %d",
514 __func__
, min
, want
, max
);
515 /* We need to check here, too, in case the child got corrupted */
516 if (max
< min
|| want
< min
|| max
< want
)
517 fatal("%s: bad parameters: %d %d %d",
518 __func__
, min
, want
, max
);
522 dh
= choose_dh(min
, want
, max
);
524 buffer_put_char(m
, 0);
527 /* Send first bignum */
528 buffer_put_char(m
, 1);
529 buffer_put_bignum2(m
, dh
->p
);
530 buffer_put_bignum2(m
, dh
->g
);
534 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
539 mm_answer_sign(int sock
, Buffer
*m
)
544 u_int siglen
, datlen
;
547 debug3("%s", __func__
);
549 keyid
= buffer_get_int(m
);
550 p
= buffer_get_string(m
, &datlen
);
553 * Supported KEX types will only return SHA1 (20 byte) or
554 * SHA256 (32 byte) hashes
556 if (datlen
!= 20 && datlen
!= 32)
557 fatal("%s: data length incorrect: %u", __func__
, datlen
);
559 /* save session id, it will be passed on the first call */
560 if (session_id2_len
== 0) {
561 session_id2_len
= datlen
;
562 session_id2
= xmalloc(session_id2_len
);
563 memcpy(session_id2
, p
, session_id2_len
);
566 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
567 fatal("%s: no hostkey from index %d", __func__
, keyid
);
568 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
569 fatal("%s: key_sign failed", __func__
);
571 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
574 buffer_put_string(m
, signature
, siglen
);
579 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
581 /* Turn on permissions for getpwnam */
582 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
587 /* Retrieves the password entry and also checks if the user is permitted */
590 mm_answer_pwnamallow(int sock
, Buffer
*m
)
593 struct passwd
*pwent
;
596 debug3("%s", __func__
);
598 if (authctxt
->attempt
++ != 0)
599 fatal("%s: multiple attempts for getpwnam", __func__
);
601 username
= buffer_get_string(m
, NULL
);
603 pwent
= getpwnamallow(username
);
605 authctxt
->user
= xstrdup(username
);
606 setproctitle("%s [priv]", pwent
? username
: "unknown");
612 buffer_put_char(m
, 0);
613 authctxt
->pw
= fakepw();
618 authctxt
->pw
= pwent
;
621 buffer_put_char(m
, 1);
622 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
623 buffer_put_cstring(m
, pwent
->pw_name
);
624 buffer_put_cstring(m
, "*");
625 buffer_put_cstring(m
, pwent
->pw_gecos
);
626 #ifdef HAVE_PW_CLASS_IN_PASSWD
627 buffer_put_cstring(m
, pwent
->pw_class
);
629 buffer_put_cstring(m
, pwent
->pw_dir
);
630 buffer_put_cstring(m
, pwent
->pw_shell
);
633 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
634 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
636 /* For SSHv1 allow authentication now */
638 monitor_permit_authentications(1);
640 /* Allow service/style information on the auth context */
641 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
642 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
647 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
649 #ifdef SSH_AUDIT_EVENTS
650 monitor_permit(mon_dispatch
, MONITOR_REQ_AUDIT_COMMAND
, 1);
656 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
661 banner
= auth2_read_banner();
662 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
663 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
672 mm_answer_authserv(int sock
, Buffer
*m
)
674 monitor_permit_authentications(1);
676 authctxt
->service
= buffer_get_string(m
, NULL
);
677 authctxt
->style
= buffer_get_string(m
, NULL
);
678 debug3("%s: service=%s, style=%s",
679 __func__
, authctxt
->service
, authctxt
->style
);
681 if (strlen(authctxt
->style
) == 0) {
682 xfree(authctxt
->style
);
683 authctxt
->style
= NULL
;
690 mm_answer_authpassword(int sock
, Buffer
*m
)
692 static int call_count
;
697 passwd
= buffer_get_string(m
, &plen
);
698 /* Only authenticate if the context is valid */
699 authenticated
= options
.password_authentication
&&
700 auth_password(authctxt
, passwd
);
701 memset(passwd
, 0, strlen(passwd
));
705 buffer_put_int(m
, authenticated
);
707 debug3("%s: sending result %d", __func__
, authenticated
);
708 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
711 if (plen
== 0 && call_count
== 1)
712 auth_method
= "none";
714 auth_method
= "password";
716 /* Causes monitor loop to terminate if authenticated */
717 return (authenticated
);
722 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
724 char *name
, *infotxt
;
730 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
731 &prompts
, &echo_on
) < 0 ? 0 : 1;
734 buffer_put_int(m
, success
);
736 buffer_put_cstring(m
, prompts
[0]);
738 debug3("%s: sending challenge success: %u", __func__
, success
);
739 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
752 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
757 if (authctxt
->as
== 0)
758 fatal("%s: no bsd auth session", __func__
);
760 response
= buffer_get_string(m
, NULL
);
761 authok
= options
.challenge_response_authentication
&&
762 auth_userresponse(authctxt
->as
, response
, 0);
764 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
768 buffer_put_int(m
, authok
);
770 debug3("%s: sending authenticated: %d", __func__
, authok
);
771 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
773 auth_method
= "bsdauth";
775 return (authok
!= 0);
781 mm_answer_skeyquery(int sock
, Buffer
*m
)
784 char challenge
[1024];
787 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
788 sizeof(challenge
)) < 0 ? 0 : 1;
791 buffer_put_int(m
, success
);
793 buffer_put_cstring(m
, challenge
);
795 debug3("%s: sending challenge success: %u", __func__
, success
);
796 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
802 mm_answer_skeyrespond(int sock
, Buffer
*m
)
807 response
= buffer_get_string(m
, NULL
);
809 authok
= (options
.challenge_response_authentication
&&
811 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
812 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
817 buffer_put_int(m
, authok
);
819 debug3("%s: sending authenticated: %d", __func__
, authok
);
820 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
822 auth_method
= "skey";
824 return (authok
!= 0);
830 mm_answer_pam_start(int sock
, Buffer
*m
)
832 if (!options
.use_pam
)
833 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
837 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
843 mm_answer_pam_account(int sock
, Buffer
*m
)
847 if (!options
.use_pam
)
848 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
850 ret
= do_pam_account();
852 buffer_put_int(m
, ret
);
853 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
855 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
860 static void *sshpam_ctxt
, *sshpam_authok
;
861 extern KbdintDevice sshpam_device
;
864 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
867 debug3("%s", __func__
);
868 authctxt
->user
= buffer_get_string(m
, NULL
);
869 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
870 sshpam_authok
= NULL
;
872 if (sshpam_ctxt
!= NULL
) {
873 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
874 buffer_put_int(m
, 1);
876 buffer_put_int(m
, 0);
878 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
883 mm_answer_pam_query(int sock
, Buffer
*m
)
885 char *name
, *info
, **prompts
;
886 u_int i
, num
, *echo_on
;
889 debug3("%s", __func__
);
890 sshpam_authok
= NULL
;
891 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
892 if (ret
== 0 && num
== 0)
893 sshpam_authok
= sshpam_ctxt
;
894 if (num
> 1 || name
== NULL
|| info
== NULL
)
897 buffer_put_int(m
, ret
);
898 buffer_put_cstring(m
, name
);
900 buffer_put_cstring(m
, info
);
902 buffer_put_int(m
, num
);
903 for (i
= 0; i
< num
; ++i
) {
904 buffer_put_cstring(m
, prompts
[i
]);
906 buffer_put_int(m
, echo_on
[i
]);
912 auth_method
= "keyboard-interactive/pam";
913 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
918 mm_answer_pam_respond(int sock
, Buffer
*m
)
924 debug3("%s", __func__
);
925 sshpam_authok
= NULL
;
926 num
= buffer_get_int(m
);
928 resp
= xcalloc(num
, sizeof(char *));
929 for (i
= 0; i
< num
; ++i
)
930 resp
[i
] = buffer_get_string(m
, NULL
);
931 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
932 for (i
= 0; i
< num
; ++i
)
936 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
939 buffer_put_int(m
, ret
);
940 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
941 auth_method
= "keyboard-interactive/pam";
943 sshpam_authok
= sshpam_ctxt
;
948 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
951 debug3("%s", __func__
);
952 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
954 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
955 auth_method
= "keyboard-interactive/pam";
956 return (sshpam_authok
== sshpam_ctxt
);
961 mm_append_debug(Buffer
*m
)
963 if (auth_debug_init
&& buffer_len(&auth_debug
)) {
964 debug3("%s: Appending debug messages for child", __func__
);
965 buffer_append(m
, buffer_ptr(&auth_debug
),
966 buffer_len(&auth_debug
));
967 buffer_clear(&auth_debug
);
972 mm_answer_keyallowed(int sock
, Buffer
*m
)
978 enum mm_keytype type
= 0;
981 debug3("%s entering", __func__
);
983 type
= buffer_get_int(m
);
984 cuser
= buffer_get_string(m
, NULL
);
985 chost
= buffer_get_string(m
, NULL
);
986 blob
= buffer_get_string(m
, &bloblen
);
988 key
= key_from_blob(blob
, bloblen
);
990 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
991 (!compat20
&& type
!= MM_RSAHOSTKEY
))
992 fatal("%s: key type and protocol mismatch", __func__
);
994 debug3("%s: key_from_blob: %p", __func__
, key
);
996 if (key
!= NULL
&& authctxt
->valid
) {
999 allowed
= options
.pubkey_authentication
&&
1000 user_key_allowed(authctxt
->pw
, key
);
1001 auth_method
= "publickey";
1004 allowed
= options
.hostbased_authentication
&&
1005 hostbased_key_allowed(authctxt
->pw
,
1007 auth_method
= "hostbased";
1010 key
->type
= KEY_RSA1
; /* XXX */
1011 allowed
= options
.rhosts_rsa_authentication
&&
1012 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1014 auth_method
= "rsa";
1017 fatal("%s: unknown key type %d", __func__
, type
);
1024 /* clear temporarily storage (used by verify) */
1025 monitor_reset_key_state();
1028 /* Save temporarily for comparison in verify */
1030 key_bloblen
= bloblen
;
1031 key_blobtype
= type
;
1032 hostbased_cuser
= cuser
;
1033 hostbased_chost
= chost
;
1035 /* Log failed attempt */
1036 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1042 debug3("%s: key %p is %s",
1043 __func__
, key
, allowed
? "allowed" : "disallowed");
1046 buffer_put_int(m
, allowed
);
1047 buffer_put_int(m
, forced_command
!= NULL
);
1051 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1053 if (type
== MM_RSAHOSTKEY
)
1054 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1060 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1068 buffer_append(&b
, data
, datalen
);
1070 if (datafellows
& SSH_OLD_SESSIONID
) {
1072 len
= buffer_len(&b
);
1073 if ((session_id2
== NULL
) ||
1074 (len
< session_id2_len
) ||
1075 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1077 buffer_consume(&b
, session_id2_len
);
1079 p
= buffer_get_string(&b
, &len
);
1080 if ((session_id2
== NULL
) ||
1081 (len
!= session_id2_len
) ||
1082 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1086 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1088 p
= buffer_get_string(&b
, NULL
);
1089 if (strcmp(authctxt
->user
, p
) != 0) {
1090 logit("wrong user name passed to monitor: expected %s != %.100s",
1095 buffer_skip_string(&b
);
1096 if (datafellows
& SSH_BUG_PKAUTH
) {
1097 if (!buffer_get_char(&b
))
1100 p
= buffer_get_string(&b
, NULL
);
1101 if (strcmp("publickey", p
) != 0)
1104 if (!buffer_get_char(&b
))
1106 buffer_skip_string(&b
);
1108 buffer_skip_string(&b
);
1109 if (buffer_len(&b
) != 0)
1116 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1125 buffer_append(&b
, data
, datalen
);
1127 p
= buffer_get_string(&b
, &len
);
1128 if ((session_id2
== NULL
) ||
1129 (len
!= session_id2_len
) ||
1130 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1134 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1136 p
= buffer_get_string(&b
, NULL
);
1137 if (strcmp(authctxt
->user
, p
) != 0) {
1138 logit("wrong user name passed to monitor: expected %s != %.100s",
1143 buffer_skip_string(&b
); /* service */
1144 p
= buffer_get_string(&b
, NULL
);
1145 if (strcmp(p
, "hostbased") != 0)
1148 buffer_skip_string(&b
); /* pkalg */
1149 buffer_skip_string(&b
); /* pkblob */
1151 /* verify client host, strip trailing dot if necessary */
1152 p
= buffer_get_string(&b
, NULL
);
1153 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1155 if (strcmp(p
, chost
) != 0)
1159 /* verify client user */
1160 p
= buffer_get_string(&b
, NULL
);
1161 if (strcmp(p
, cuser
) != 0)
1165 if (buffer_len(&b
) != 0)
1172 mm_answer_keyverify(int sock
, Buffer
*m
)
1175 u_char
*signature
, *data
, *blob
;
1176 u_int signaturelen
, datalen
, bloblen
;
1180 blob
= buffer_get_string(m
, &bloblen
);
1181 signature
= buffer_get_string(m
, &signaturelen
);
1182 data
= buffer_get_string(m
, &datalen
);
1184 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1185 !monitor_allowed_key(blob
, bloblen
))
1186 fatal("%s: bad key, not previously allowed", __func__
);
1188 key
= key_from_blob(blob
, bloblen
);
1190 fatal("%s: bad public key blob", __func__
);
1192 switch (key_blobtype
) {
1194 valid_data
= monitor_valid_userblob(data
, datalen
);
1197 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1198 hostbased_cuser
, hostbased_chost
);
1205 fatal("%s: bad signature data blob", __func__
);
1207 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1208 debug3("%s: key %p signature %s",
1209 __func__
, key
, verified
? "verified" : "unverified");
1216 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1218 monitor_reset_key_state();
1221 buffer_put_int(m
, verified
);
1222 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1228 mm_record_login(Session
*s
, struct passwd
*pw
)
1231 struct sockaddr_storage from
;
1234 * Get IP address of client. If the connection is not a socket, let
1235 * the address be 0.0.0.0.
1237 memset(&from
, 0, sizeof(from
));
1238 fromlen
= sizeof(from
);
1239 if (packet_connection_is_on_socket()) {
1240 if (getpeername(packet_get_connection_in(),
1241 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1242 debug("getpeername: %.100s", strerror(errno
));
1246 /* Record that there was a login on that tty from the remote host. */
1247 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1248 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1249 (struct sockaddr
*)&from
, fromlen
);
1253 mm_session_close(Session
*s
)
1255 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1256 if (s
->ttyfd
!= -1) {
1257 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1258 session_pty_cleanup2(s
);
1264 mm_answer_pty(int sock
, Buffer
*m
)
1266 extern struct monitor
*pmonitor
;
1270 debug3("%s entering", __func__
);
1276 s
->authctxt
= authctxt
;
1277 s
->pw
= authctxt
->pw
;
1278 s
->pid
= pmonitor
->m_pid
;
1279 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1282 pty_setowner(authctxt
->pw
, s
->tty
);
1284 buffer_put_int(m
, 1);
1285 buffer_put_cstring(m
, s
->tty
);
1287 /* We need to trick ttyslot */
1288 if (dup2(s
->ttyfd
, 0) == -1)
1289 fatal("%s: dup2", __func__
);
1291 mm_record_login(s
, authctxt
->pw
);
1293 /* Now we can close the file descriptor again */
1296 /* send messages generated by record_login */
1297 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1298 buffer_clear(&loginmsg
);
1300 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1302 mm_send_fd(sock
, s
->ptyfd
);
1303 mm_send_fd(sock
, s
->ttyfd
);
1305 /* make sure nothing uses fd 0 */
1306 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1307 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1309 error("%s: fd0 %d != 0", __func__
, fd0
);
1311 /* slave is not needed */
1313 s
->ttyfd
= s
->ptyfd
;
1314 /* no need to dup() because nobody closes ptyfd */
1315 s
->ptymaster
= s
->ptyfd
;
1317 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1323 mm_session_close(s
);
1324 buffer_put_int(m
, 0);
1325 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1330 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1335 debug3("%s entering", __func__
);
1337 tty
= buffer_get_string(m
, NULL
);
1338 if ((s
= session_by_tty(tty
)) != NULL
)
1339 mm_session_close(s
);
1346 mm_answer_sesskey(int sock
, Buffer
*m
)
1351 /* Turn off permissions */
1352 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1354 if ((p
= BN_new()) == NULL
)
1355 fatal("%s: BN_new", __func__
);
1357 buffer_get_bignum2(m
, p
);
1359 rsafail
= ssh1_session_key(p
);
1362 buffer_put_int(m
, rsafail
);
1363 buffer_put_bignum2(m
, p
);
1367 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1369 /* Turn on permissions for sessid passing */
1370 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1376 mm_answer_sessid(int sock
, Buffer
*m
)
1380 debug3("%s entering", __func__
);
1382 if (buffer_len(m
) != 16)
1383 fatal("%s: bad ssh1 session id", __func__
);
1384 for (i
= 0; i
< 16; i
++)
1385 session_id
[i
] = buffer_get_char(m
);
1387 /* Turn on permissions for getpwnam */
1388 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1394 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1398 u_char
*blob
= NULL
;
1402 debug3("%s entering", __func__
);
1404 auth_method
= "rsa";
1405 if (options
.rsa_authentication
&& authctxt
->valid
) {
1406 if ((client_n
= BN_new()) == NULL
)
1407 fatal("%s: BN_new", __func__
);
1408 buffer_get_bignum2(m
, client_n
);
1409 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1410 BN_clear_free(client_n
);
1413 buffer_put_int(m
, allowed
);
1414 buffer_put_int(m
, forced_command
!= NULL
);
1416 /* clear temporarily storage (used by generate challenge) */
1417 monitor_reset_key_state();
1419 if (allowed
&& key
!= NULL
) {
1420 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1421 if (key_to_blob(key
, &blob
, &blen
) == 0)
1422 fatal("%s: key_to_blob failed", __func__
);
1423 buffer_put_string(m
, blob
, blen
);
1425 /* Save temporarily for comparison in verify */
1428 key_blobtype
= MM_RSAUSERKEY
;
1435 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1437 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1438 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1443 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1449 debug3("%s entering", __func__
);
1451 if (!authctxt
->valid
)
1452 fatal("%s: authctxt not valid", __func__
);
1453 blob
= buffer_get_string(m
, &blen
);
1454 if (!monitor_allowed_key(blob
, blen
))
1455 fatal("%s: bad key, not previously allowed", __func__
);
1456 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1457 fatal("%s: key type mismatch", __func__
);
1458 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1459 fatal("%s: received bad key", __func__
);
1462 BN_clear_free(ssh1_challenge
);
1463 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1466 buffer_put_bignum2(m
, ssh1_challenge
);
1468 debug3("%s sending reply", __func__
);
1469 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1471 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1479 mm_answer_rsa_response(int sock
, Buffer
*m
)
1482 u_char
*blob
, *response
;
1486 debug3("%s entering", __func__
);
1488 if (!authctxt
->valid
)
1489 fatal("%s: authctxt not valid", __func__
);
1490 if (ssh1_challenge
== NULL
)
1491 fatal("%s: no ssh1_challenge", __func__
);
1493 blob
= buffer_get_string(m
, &blen
);
1494 if (!monitor_allowed_key(blob
, blen
))
1495 fatal("%s: bad key, not previously allowed", __func__
);
1496 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1497 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1498 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1499 fatal("%s: received bad key", __func__
);
1500 response
= buffer_get_string(m
, &len
);
1502 fatal("%s: received bad response to challenge", __func__
);
1503 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1509 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1512 BN_clear_free(ssh1_challenge
);
1513 ssh1_challenge
= NULL
;
1514 monitor_reset_key_state();
1517 buffer_put_int(m
, success
);
1518 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1524 mm_answer_term(int sock
, Buffer
*req
)
1526 extern struct monitor
*pmonitor
;
1529 debug3("%s: tearing down sessions", __func__
);
1531 /* The child is terminating */
1532 session_destroy_all(&mm_session_close
);
1534 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1538 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1540 /* Terminate process */
1544 #ifdef SSH_AUDIT_EVENTS
1545 /* Report that an audit event occurred */
1547 mm_answer_audit_event(int socket
, Buffer
*m
)
1549 ssh_audit_event_t event
;
1551 debug3("%s entering", __func__
);
1553 event
= buffer_get_int(m
);
1555 case SSH_AUTH_FAIL_PUBKEY
:
1556 case SSH_AUTH_FAIL_HOSTBASED
:
1557 case SSH_AUTH_FAIL_GSSAPI
:
1558 case SSH_LOGIN_EXCEED_MAXTRIES
:
1559 case SSH_LOGIN_ROOT_DENIED
:
1560 case SSH_CONNECTION_CLOSE
:
1561 case SSH_INVALID_USER
:
1565 fatal("Audit event type %d not permitted", event
);
1572 mm_answer_audit_command(int socket
, Buffer
*m
)
1577 debug3("%s entering", __func__
);
1578 cmd
= buffer_get_string(m
, &len
);
1579 /* sanity check command, if so how? */
1580 audit_run_command(cmd
);
1584 #endif /* SSH_AUDIT_EVENTS */
1587 monitor_apply_keystate(struct monitor
*pmonitor
)
1590 set_newkeys(MODE_IN
);
1591 set_newkeys(MODE_OUT
);
1593 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1594 packet_set_encryption_key(child_state
.ssh1key
,
1595 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1596 xfree(child_state
.ssh1key
);
1599 /* for rc4 and other stateful ciphers */
1600 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1601 xfree(child_state
.keyout
);
1602 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1603 xfree(child_state
.keyin
);
1606 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1607 xfree(child_state
.ivout
);
1608 packet_set_iv(MODE_IN
, child_state
.ivin
);
1609 xfree(child_state
.ivin
);
1612 memcpy(&incoming_stream
, &child_state
.incoming
,
1613 sizeof(incoming_stream
));
1614 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1615 sizeof(outgoing_stream
));
1617 /* Update with new address */
1618 if (options
.compression
)
1619 mm_init_compression(pmonitor
->m_zlib
);
1621 /* Network I/O buffers */
1622 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1623 buffer_clear(&input
);
1624 buffer_append(&input
, child_state
.input
, child_state
.ilen
);
1625 memset(child_state
.input
, 0, child_state
.ilen
);
1626 xfree(child_state
.input
);
1628 buffer_clear(&output
);
1629 buffer_append(&output
, child_state
.output
, child_state
.olen
);
1630 memset(child_state
.output
, 0, child_state
.olen
);
1631 xfree(child_state
.output
);
1635 mm_get_kex(Buffer
*m
)
1641 kex
= xcalloc(1, sizeof(*kex
));
1642 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1643 if ((session_id2
== NULL
) ||
1644 (kex
->session_id_len
!= session_id2_len
) ||
1645 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1646 fatal("mm_get_get: internal error: bad session id");
1647 kex
->we_need
= buffer_get_int(m
);
1648 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1649 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1650 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1651 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1653 kex
->hostkey_type
= buffer_get_int(m
);
1654 kex
->kex_type
= buffer_get_int(m
);
1655 blob
= buffer_get_string(m
, &bloblen
);
1656 buffer_init(&kex
->my
);
1657 buffer_append(&kex
->my
, blob
, bloblen
);
1659 blob
= buffer_get_string(m
, &bloblen
);
1660 buffer_init(&kex
->peer
);
1661 buffer_append(&kex
->peer
, blob
, bloblen
);
1664 kex
->flags
= buffer_get_int(m
);
1665 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1666 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1667 kex
->load_host_key
=&get_hostkey_by_type
;
1668 kex
->host_key_index
=&get_hostkey_index
;
1673 /* This function requries careful sanity checking */
1676 mm_get_keystate(struct monitor
*pmonitor
)
1680 u_int bloblen
, plen
;
1681 u_int32_t seqnr
, packets
;
1684 debug3("%s: Waiting for new keys", __func__
);
1687 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1689 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1690 child_state
.ssh1cipher
= buffer_get_int(&m
);
1691 child_state
.ssh1key
= buffer_get_string(&m
,
1692 &child_state
.ssh1keylen
);
1693 child_state
.ivout
= buffer_get_string(&m
,
1694 &child_state
.ivoutlen
);
1695 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1698 /* Get the Kex for rekeying */
1699 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1702 blob
= buffer_get_string(&m
, &bloblen
);
1703 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1706 debug3("%s: Waiting for second key", __func__
);
1707 blob
= buffer_get_string(&m
, &bloblen
);
1708 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1711 /* Now get sequence numbers for the packets */
1712 seqnr
= buffer_get_int(&m
);
1713 blocks
= buffer_get_int64(&m
);
1714 packets
= buffer_get_int(&m
);
1715 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
);
1716 seqnr
= buffer_get_int(&m
);
1717 blocks
= buffer_get_int64(&m
);
1718 packets
= buffer_get_int(&m
);
1719 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
);
1722 /* Get the key context */
1723 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1724 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1726 debug3("%s: Getting compression state", __func__
);
1727 /* Get compression state */
1728 p
= buffer_get_string(&m
, &plen
);
1729 if (plen
!= sizeof(child_state
.outgoing
))
1730 fatal("%s: bad request size", __func__
);
1731 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1734 p
= buffer_get_string(&m
, &plen
);
1735 if (plen
!= sizeof(child_state
.incoming
))
1736 fatal("%s: bad request size", __func__
);
1737 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1740 /* Network I/O buffers */
1741 debug3("%s: Getting Network I/O buffers", __func__
);
1742 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1743 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1749 /* Allocation functions for zlib */
1751 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1753 size_t len
= (size_t) size
* ncount
;
1756 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1757 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1759 address
= mm_malloc(mm
, len
);
1765 mm_zfree(struct mm_master
*mm
, void *address
)
1767 mm_free(mm
, address
);
1771 mm_init_compression(struct mm_master
*mm
)
1773 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1774 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1775 outgoing_stream
.opaque
= mm
;
1777 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1778 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1779 incoming_stream
.opaque
= mm
;
1784 #define FD_CLOSEONEXEC(x) do { \
1785 if (fcntl(x, F_SETFD, 1) == -1) \
1786 fatal("fcntl(%d, F_SETFD)", x); \
1790 monitor_socketpair(int *pair
)
1792 #ifdef HAVE_SOCKETPAIR
1793 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1794 fatal("%s: socketpair", __func__
);
1796 fatal("%s: UsePrivilegeSeparation=yes not supported",
1799 FD_CLOSEONEXEC(pair
[0]);
1800 FD_CLOSEONEXEC(pair
[1]);
1803 #define MM_MEMSIZE 65536
1808 struct monitor
*mon
;
1811 mon
= xcalloc(1, sizeof(*mon
));
1813 monitor_socketpair(pair
);
1815 mon
->m_recvfd
= pair
[0];
1816 mon
->m_sendfd
= pair
[1];
1818 /* Used to share zlib space across processes */
1819 if (options
.compression
) {
1820 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1821 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1823 /* Compression needs to share state across borders */
1824 mm_init_compression(mon
->m_zlib
);
1831 monitor_reinit(struct monitor
*mon
)
1835 monitor_socketpair(pair
);
1837 mon
->m_recvfd
= pair
[0];
1838 mon
->m_sendfd
= pair
[1];
1843 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1849 goid
.elements
= buffer_get_string(m
, &len
);
1852 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1854 xfree(goid
.elements
);
1857 buffer_put_int(m
, major
);
1859 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1861 /* Now we have a context, enable the step */
1862 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1868 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1871 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1872 OM_uint32 major
, minor
;
1873 OM_uint32 flags
= 0; /* GSI needs this */
1876 in
.value
= buffer_get_string(m
, &len
);
1878 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1882 buffer_put_int(m
, major
);
1883 buffer_put_string(m
, out
.value
, out
.length
);
1884 buffer_put_int(m
, flags
);
1885 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1887 gss_release_buffer(&minor
, &out
);
1889 if (major
== GSS_S_COMPLETE
) {
1890 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1891 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1892 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1898 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1900 gss_buffer_desc gssbuf
, mic
;
1904 gssbuf
.value
= buffer_get_string(m
, &len
);
1905 gssbuf
.length
= len
;
1906 mic
.value
= buffer_get_string(m
, &len
);
1909 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1911 xfree(gssbuf
.value
);
1915 buffer_put_int(m
, ret
);
1917 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1919 if (!GSS_ERROR(ret
))
1920 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1926 mm_answer_gss_userok(int sock
, Buffer
*m
)
1930 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1933 buffer_put_int(m
, authenticated
);
1935 debug3("%s: sending result %d", __func__
, authenticated
);
1936 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1938 auth_method
= "gssapi-with-mic";
1940 /* Monitor loop will terminate if authenticated */
1941 return (authenticated
);