1 /* $OpenBSD: monitor.c,v 1.82 2006/07/22 20:48:23 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>
47 #include <openssl/dh.h>
53 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
56 #define TARGET_OS_MAC 1
61 #include "auth-options.h"
70 #include "monitor_mm.h"
71 #include "monitor_wrap.h"
72 #include "monitor_fdpass.h"
82 static Gssctxt
*gsscontext
= NULL
;
86 extern ServerOptions options
;
87 extern u_int utmp_len
;
88 extern Newkeys
*current_keys
[];
89 extern z_stream incoming_stream
;
90 extern z_stream outgoing_stream
;
91 extern u_char session_id
[];
92 extern Buffer input
, output
;
93 extern Buffer auth_debug
;
94 extern int auth_debug_init
;
95 extern Buffer loginmsg
;
97 /* State exported from the child */
120 /* Functions on the monitor that answer unprivileged requests */
122 int mm_answer_moduli(int, Buffer
*);
123 int mm_answer_sign(int, Buffer
*);
124 int mm_answer_pwnamallow(int, Buffer
*);
125 int mm_answer_auth2_read_banner(int, Buffer
*);
126 int mm_answer_authserv(int, Buffer
*);
127 int mm_answer_authpassword(int, Buffer
*);
128 int mm_answer_bsdauthquery(int, Buffer
*);
129 int mm_answer_bsdauthrespond(int, Buffer
*);
130 int mm_answer_skeyquery(int, Buffer
*);
131 int mm_answer_skeyrespond(int, Buffer
*);
132 int mm_answer_keyallowed(int, Buffer
*);
133 int mm_answer_keyverify(int, Buffer
*);
134 int mm_answer_pty(int, Buffer
*);
135 int mm_answer_pty_cleanup(int, Buffer
*);
136 int mm_answer_term(int, Buffer
*);
137 int mm_answer_rsa_keyallowed(int, Buffer
*);
138 int mm_answer_rsa_challenge(int, Buffer
*);
139 int mm_answer_rsa_response(int, Buffer
*);
140 int mm_answer_sesskey(int, Buffer
*);
141 int mm_answer_sessid(int, Buffer
*);
144 int mm_answer_pam_start(int, Buffer
*);
145 int mm_answer_pam_account(int, Buffer
*);
146 int mm_answer_pam_init_ctx(int, Buffer
*);
147 int mm_answer_pam_query(int, Buffer
*);
148 int mm_answer_pam_respond(int, Buffer
*);
149 int mm_answer_pam_free_ctx(int, Buffer
*);
153 int mm_answer_gss_setup_ctx(int, Buffer
*);
154 int mm_answer_gss_accept_ctx(int, Buffer
*);
155 int mm_answer_gss_userok(int, Buffer
*);
156 int mm_answer_gss_checkmic(int, Buffer
*);
159 #ifdef SSH_AUDIT_EVENTS
160 int mm_answer_audit_event(int, Buffer
*);
161 int mm_answer_audit_command(int, Buffer
*);
164 static Authctxt
*authctxt
;
165 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
167 /* local state for key verify */
168 static u_char
*key_blob
= NULL
;
169 static u_int key_bloblen
= 0;
170 static int key_blobtype
= MM_NOKEY
;
171 static char *hostbased_cuser
= NULL
;
172 static char *hostbased_chost
= NULL
;
173 static char *auth_method
= "unknown";
174 static u_int session_id2_len
= 0;
175 static u_char
*session_id2
= NULL
;
176 static pid_t monitor_child_pid
;
179 enum monitor_reqtype type
;
181 int (*f
)(int, Buffer
*);
184 #define MON_ISAUTH 0x0004 /* Required for Authentication */
185 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
186 #define MON_ONCE 0x0010 /* Disable after calling */
187 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
189 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
191 #define MON_PERMIT 0x1000 /* Request is permitted */
193 struct mon_table mon_dispatch_proto20
[] = {
194 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
195 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
196 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
197 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
198 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
199 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
201 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
202 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
203 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
204 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
205 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
206 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
208 #ifdef SSH_AUDIT_EVENTS
209 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
212 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
213 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
216 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
217 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
219 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
220 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
222 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
223 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
224 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
225 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
230 struct mon_table mon_dispatch_postauth20
[] = {
231 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
232 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
233 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
234 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
235 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
236 #ifdef SSH_AUDIT_EVENTS
237 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
238 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
243 struct mon_table mon_dispatch_proto15
[] = {
244 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
245 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
246 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
247 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
248 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
249 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
250 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
251 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
253 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
254 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
257 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
258 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
261 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
262 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
263 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
264 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
265 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
266 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
268 #ifdef SSH_AUDIT_EVENTS
269 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
274 struct mon_table mon_dispatch_postauth15
[] = {
275 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
276 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
277 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
278 #ifdef SSH_AUDIT_EVENTS
279 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
280 {MONITOR_REQ_AUDIT_COMMAND
, MON_ONCE
, mm_answer_audit_command
},
285 struct mon_table
*mon_dispatch
;
287 /* Specifies if a certain message is allowed at the moment */
290 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
292 while (ent
->f
!= NULL
) {
293 if (ent
->type
== type
) {
294 ent
->flags
&= ~MON_PERMIT
;
295 ent
->flags
|= permit
? MON_PERMIT
: 0;
303 monitor_permit_authentications(int permit
)
305 struct mon_table
*ent
= mon_dispatch
;
307 while (ent
->f
!= NULL
) {
308 if (ent
->flags
& MON_AUTH
) {
309 ent
->flags
&= ~MON_PERMIT
;
310 ent
->flags
|= permit
? MON_PERMIT
: 0;
317 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
319 struct mon_table
*ent
;
320 int authenticated
= 0;
322 debug3("preauth child monitor started");
324 authctxt
= _authctxt
;
325 memset(authctxt
, 0, sizeof(*authctxt
));
327 authctxt
->loginmsg
= &loginmsg
;
330 mon_dispatch
= mon_dispatch_proto20
;
332 /* Permit requests for moduli and signatures */
333 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
334 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
336 mon_dispatch
= mon_dispatch_proto15
;
338 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
341 /* The first few requests do not require asynchronous access */
342 while (!authenticated
) {
343 auth_method
= "unknown";
344 authenticated
= monitor_read(pmonitor
, mon_dispatch
, &ent
);
346 if (!(ent
->flags
& MON_AUTHDECIDE
))
347 fatal("%s: unexpected authentication from %d",
348 __func__
, ent
->type
);
349 if (authctxt
->pw
->pw_uid
== 0 &&
350 !auth_root_allowed(auth_method
))
353 /* PAM needs to perform account checks after auth */
354 if (options
.use_pam
&& authenticated
) {
358 mm_request_receive_expect(pmonitor
->m_sendfd
,
359 MONITOR_REQ_PAM_ACCOUNT
, &m
);
360 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
366 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
367 auth_log(authctxt
, authenticated
, auth_method
,
368 compat20
? " ssh2" : "");
370 authctxt
->failures
++;
374 if (!authctxt
->valid
)
375 fatal("%s: authenticated invalid user", __func__
);
376 if (strcmp(auth_method
, "unknown") == 0)
377 fatal("%s: authentication method name unknown", __func__
);
379 debug("%s: %s has been authenticated by privileged process",
380 __func__
, authctxt
->user
);
382 mm_get_keystate(pmonitor
);
386 monitor_set_child_handler(pid_t pid
)
388 monitor_child_pid
= pid
;
392 monitor_child_handler(int sig
)
394 kill(monitor_child_pid
, sig
);
398 monitor_child_postauth(struct monitor
*pmonitor
)
400 monitor_set_child_handler(pmonitor
->m_pid
);
401 signal(SIGHUP
, &monitor_child_handler
);
402 signal(SIGTERM
, &monitor_child_handler
);
405 mon_dispatch
= mon_dispatch_postauth20
;
407 /* Permit requests for moduli and signatures */
408 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
409 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
410 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
412 mon_dispatch
= mon_dispatch_postauth15
;
413 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
416 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
417 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
421 monitor_read(pmonitor
, mon_dispatch
, NULL
);
425 monitor_sync(struct monitor
*pmonitor
)
427 if (options
.compression
) {
428 /* The member allocation is not visible, so sync it */
429 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
434 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
435 struct mon_table
**pent
)
443 mm_request_receive(pmonitor
->m_sendfd
, &m
);
444 type
= buffer_get_char(&m
);
446 debug3("%s: checking request %d", __func__
, type
);
448 while (ent
->f
!= NULL
) {
449 if (ent
->type
== type
)
454 if (ent
->f
!= NULL
) {
455 if (!(ent
->flags
& MON_PERMIT
))
456 fatal("%s: unpermitted request %d", __func__
,
458 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
461 /* The child may use this request only once, disable it */
462 if (ent
->flags
& MON_ONCE
) {
463 debug2("%s: %d used once, disabling now", __func__
,
465 ent
->flags
&= ~MON_PERMIT
;
474 fatal("%s: unsupported request: %d", __func__
, type
);
480 /* allowed key state */
482 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
484 /* make sure key is allowed */
485 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
486 memcmp(key_blob
, blob
, key_bloblen
))
492 monitor_reset_key_state(void)
495 if (key_blob
!= NULL
)
497 if (hostbased_cuser
!= NULL
)
498 xfree(hostbased_cuser
);
499 if (hostbased_chost
!= NULL
)
500 xfree(hostbased_chost
);
503 key_blobtype
= MM_NOKEY
;
504 hostbased_cuser
= NULL
;
505 hostbased_chost
= NULL
;
509 mm_answer_moduli(int sock
, Buffer
*m
)
514 min
= buffer_get_int(m
);
515 want
= buffer_get_int(m
);
516 max
= buffer_get_int(m
);
518 debug3("%s: got parameters: %d %d %d",
519 __func__
, min
, want
, max
);
520 /* We need to check here, too, in case the child got corrupted */
521 if (max
< min
|| want
< min
|| max
< want
)
522 fatal("%s: bad parameters: %d %d %d",
523 __func__
, min
, want
, max
);
527 dh
= choose_dh(min
, want
, max
);
529 buffer_put_char(m
, 0);
532 /* Send first bignum */
533 buffer_put_char(m
, 1);
534 buffer_put_bignum2(m
, dh
->p
);
535 buffer_put_bignum2(m
, dh
->g
);
539 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
544 mm_answer_sign(int sock
, Buffer
*m
)
549 u_int siglen
, datlen
;
552 debug3("%s", __func__
);
554 keyid
= buffer_get_int(m
);
555 p
= buffer_get_string(m
, &datlen
);
558 * Supported KEX types will only return SHA1 (20 byte) or
559 * SHA256 (32 byte) hashes
561 if (datlen
!= 20 && datlen
!= 32)
562 fatal("%s: data length incorrect: %u", __func__
, datlen
);
564 /* save session id, it will be passed on the first call */
565 if (session_id2_len
== 0) {
566 session_id2_len
= datlen
;
567 session_id2
= xmalloc(session_id2_len
);
568 memcpy(session_id2
, p
, session_id2_len
);
571 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
572 fatal("%s: no hostkey from index %d", __func__
, keyid
);
573 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
574 fatal("%s: key_sign failed", __func__
);
576 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
579 buffer_put_string(m
, signature
, siglen
);
584 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
586 /* Turn on permissions for getpwnam */
587 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
592 /* Retrieves the password entry and also checks if the user is permitted */
595 mm_answer_pwnamallow(int sock
, Buffer
*m
)
598 struct passwd
*pwent
;
601 debug3("%s", __func__
);
603 if (authctxt
->attempt
++ != 0)
604 fatal("%s: multiple attempts for getpwnam", __func__
);
606 username
= buffer_get_string(m
, NULL
);
608 pwent
= getpwnamallow(username
);
610 authctxt
->user
= xstrdup(username
);
611 setproctitle("%s [priv]", pwent
? username
: "unknown");
617 buffer_put_char(m
, 0);
618 authctxt
->pw
= fakepw();
623 authctxt
->pw
= pwent
;
626 buffer_put_char(m
, 1);
627 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
628 buffer_put_cstring(m
, pwent
->pw_name
);
629 buffer_put_cstring(m
, "*");
630 buffer_put_cstring(m
, pwent
->pw_gecos
);
631 #ifdef HAVE_PW_CLASS_IN_PASSWD
632 buffer_put_cstring(m
, pwent
->pw_class
);
634 buffer_put_cstring(m
, pwent
->pw_dir
);
635 buffer_put_cstring(m
, pwent
->pw_shell
);
638 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
639 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
641 /* For SSHv1 allow authentication now */
643 monitor_permit_authentications(1);
645 /* Allow service/style information on the auth context */
646 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
647 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
652 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
654 #ifdef SSH_AUDIT_EVENTS
655 monitor_permit(mon_dispatch
, MONITOR_REQ_AUDIT_COMMAND
, 1);
661 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
666 banner
= auth2_read_banner();
667 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
668 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
677 mm_answer_authserv(int sock
, Buffer
*m
)
679 monitor_permit_authentications(1);
681 authctxt
->service
= buffer_get_string(m
, NULL
);
682 authctxt
->style
= buffer_get_string(m
, NULL
);
683 debug3("%s: service=%s, style=%s",
684 __func__
, authctxt
->service
, authctxt
->style
);
686 if (strlen(authctxt
->style
) == 0) {
687 xfree(authctxt
->style
);
688 authctxt
->style
= NULL
;
695 mm_answer_authpassword(int sock
, Buffer
*m
)
697 static int call_count
;
702 passwd
= buffer_get_string(m
, &plen
);
703 /* Only authenticate if the context is valid */
704 authenticated
= options
.password_authentication
&&
705 auth_password(authctxt
, passwd
);
706 memset(passwd
, 0, strlen(passwd
));
710 buffer_put_int(m
, authenticated
);
712 debug3("%s: sending result %d", __func__
, authenticated
);
713 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
716 if (plen
== 0 && call_count
== 1)
717 auth_method
= "none";
719 auth_method
= "password";
721 /* Causes monitor loop to terminate if authenticated */
722 return (authenticated
);
727 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
729 char *name
, *infotxt
;
735 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
736 &prompts
, &echo_on
) < 0 ? 0 : 1;
739 buffer_put_int(m
, success
);
741 buffer_put_cstring(m
, prompts
[0]);
743 debug3("%s: sending challenge success: %u", __func__
, success
);
744 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
757 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
762 if (authctxt
->as
== 0)
763 fatal("%s: no bsd auth session", __func__
);
765 response
= buffer_get_string(m
, NULL
);
766 authok
= options
.challenge_response_authentication
&&
767 auth_userresponse(authctxt
->as
, response
, 0);
769 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
773 buffer_put_int(m
, authok
);
775 debug3("%s: sending authenticated: %d", __func__
, authok
);
776 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
778 auth_method
= "bsdauth";
780 return (authok
!= 0);
786 mm_answer_skeyquery(int sock
, Buffer
*m
)
789 char challenge
[1024];
792 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
793 sizeof(challenge
)) < 0 ? 0 : 1;
796 buffer_put_int(m
, success
);
798 buffer_put_cstring(m
, challenge
);
800 debug3("%s: sending challenge success: %u", __func__
, success
);
801 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
807 mm_answer_skeyrespond(int sock
, Buffer
*m
)
812 response
= buffer_get_string(m
, NULL
);
814 authok
= (options
.challenge_response_authentication
&&
816 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
817 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
822 buffer_put_int(m
, authok
);
824 debug3("%s: sending authenticated: %d", __func__
, authok
);
825 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
827 auth_method
= "skey";
829 return (authok
!= 0);
835 mm_answer_pam_start(int sock
, Buffer
*m
)
837 if (!options
.use_pam
)
838 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
842 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
848 mm_answer_pam_account(int sock
, Buffer
*m
)
852 if (!options
.use_pam
)
853 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
855 ret
= do_pam_account();
857 buffer_put_int(m
, ret
);
858 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
860 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
865 static void *sshpam_ctxt
, *sshpam_authok
;
866 extern KbdintDevice sshpam_device
;
869 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
872 debug3("%s", __func__
);
873 authctxt
->user
= buffer_get_string(m
, NULL
);
874 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
875 sshpam_authok
= NULL
;
877 if (sshpam_ctxt
!= NULL
) {
878 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
879 buffer_put_int(m
, 1);
881 buffer_put_int(m
, 0);
883 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
888 mm_answer_pam_query(int sock
, Buffer
*m
)
890 char *name
, *info
, **prompts
;
891 u_int i
, num
, *echo_on
;
894 debug3("%s", __func__
);
895 sshpam_authok
= NULL
;
896 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
897 if (ret
== 0 && num
== 0)
898 sshpam_authok
= sshpam_ctxt
;
899 if (num
> 1 || name
== NULL
|| info
== NULL
)
902 buffer_put_int(m
, ret
);
903 buffer_put_cstring(m
, name
);
905 buffer_put_cstring(m
, info
);
907 buffer_put_int(m
, num
);
908 for (i
= 0; i
< num
; ++i
) {
909 buffer_put_cstring(m
, prompts
[i
]);
911 buffer_put_int(m
, echo_on
[i
]);
917 auth_method
= "keyboard-interactive/pam";
918 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
923 mm_answer_pam_respond(int sock
, Buffer
*m
)
929 debug3("%s", __func__
);
930 sshpam_authok
= NULL
;
931 num
= buffer_get_int(m
);
933 resp
= xcalloc(num
, sizeof(char *));
934 for (i
= 0; i
< num
; ++i
)
935 resp
[i
] = buffer_get_string(m
, NULL
);
936 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
937 for (i
= 0; i
< num
; ++i
)
941 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
944 buffer_put_int(m
, ret
);
945 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
946 auth_method
= "keyboard-interactive/pam";
948 sshpam_authok
= sshpam_ctxt
;
953 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
956 debug3("%s", __func__
);
957 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
959 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
960 auth_method
= "keyboard-interactive/pam";
961 return (sshpam_authok
== sshpam_ctxt
);
966 mm_append_debug(Buffer
*m
)
968 if (auth_debug_init
&& buffer_len(&auth_debug
)) {
969 debug3("%s: Appending debug messages for child", __func__
);
970 buffer_append(m
, buffer_ptr(&auth_debug
),
971 buffer_len(&auth_debug
));
972 buffer_clear(&auth_debug
);
977 mm_answer_keyallowed(int sock
, Buffer
*m
)
983 enum mm_keytype type
= 0;
986 debug3("%s entering", __func__
);
988 type
= buffer_get_int(m
);
989 cuser
= buffer_get_string(m
, NULL
);
990 chost
= buffer_get_string(m
, NULL
);
991 blob
= buffer_get_string(m
, &bloblen
);
993 key
= key_from_blob(blob
, bloblen
);
995 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
996 (!compat20
&& type
!= MM_RSAHOSTKEY
))
997 fatal("%s: key type and protocol mismatch", __func__
);
999 debug3("%s: key_from_blob: %p", __func__
, key
);
1001 if (key
!= NULL
&& authctxt
->valid
) {
1004 allowed
= options
.pubkey_authentication
&&
1005 user_key_allowed(authctxt
->pw
, key
);
1006 auth_method
= "publickey";
1009 allowed
= options
.hostbased_authentication
&&
1010 hostbased_key_allowed(authctxt
->pw
,
1012 auth_method
= "hostbased";
1015 key
->type
= KEY_RSA1
; /* XXX */
1016 allowed
= options
.rhosts_rsa_authentication
&&
1017 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1019 auth_method
= "rsa";
1022 fatal("%s: unknown key type %d", __func__
, type
);
1029 /* clear temporarily storage (used by verify) */
1030 monitor_reset_key_state();
1033 /* Save temporarily for comparison in verify */
1035 key_bloblen
= bloblen
;
1036 key_blobtype
= type
;
1037 hostbased_cuser
= cuser
;
1038 hostbased_chost
= chost
;
1040 /* Log failed attempt */
1041 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1047 debug3("%s: key %p is %s",
1048 __func__
, key
, allowed
? "allowed" : "disallowed");
1051 buffer_put_int(m
, allowed
);
1052 buffer_put_int(m
, forced_command
!= NULL
);
1056 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1058 if (type
== MM_RSAHOSTKEY
)
1059 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1065 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1073 buffer_append(&b
, data
, datalen
);
1075 if (datafellows
& SSH_OLD_SESSIONID
) {
1077 len
= buffer_len(&b
);
1078 if ((session_id2
== NULL
) ||
1079 (len
< session_id2_len
) ||
1080 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1082 buffer_consume(&b
, session_id2_len
);
1084 p
= buffer_get_string(&b
, &len
);
1085 if ((session_id2
== NULL
) ||
1086 (len
!= session_id2_len
) ||
1087 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1091 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1093 p
= buffer_get_string(&b
, NULL
);
1094 if (strcmp(authctxt
->user
, p
) != 0) {
1095 logit("wrong user name passed to monitor: expected %s != %.100s",
1100 buffer_skip_string(&b
);
1101 if (datafellows
& SSH_BUG_PKAUTH
) {
1102 if (!buffer_get_char(&b
))
1105 p
= buffer_get_string(&b
, NULL
);
1106 if (strcmp("publickey", p
) != 0)
1109 if (!buffer_get_char(&b
))
1111 buffer_skip_string(&b
);
1113 buffer_skip_string(&b
);
1114 if (buffer_len(&b
) != 0)
1121 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1130 buffer_append(&b
, data
, datalen
);
1132 p
= buffer_get_string(&b
, &len
);
1133 if ((session_id2
== NULL
) ||
1134 (len
!= session_id2_len
) ||
1135 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1139 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1141 p
= buffer_get_string(&b
, NULL
);
1142 if (strcmp(authctxt
->user
, p
) != 0) {
1143 logit("wrong user name passed to monitor: expected %s != %.100s",
1148 buffer_skip_string(&b
); /* service */
1149 p
= buffer_get_string(&b
, NULL
);
1150 if (strcmp(p
, "hostbased") != 0)
1153 buffer_skip_string(&b
); /* pkalg */
1154 buffer_skip_string(&b
); /* pkblob */
1156 /* verify client host, strip trailing dot if necessary */
1157 p
= buffer_get_string(&b
, NULL
);
1158 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1160 if (strcmp(p
, chost
) != 0)
1164 /* verify client user */
1165 p
= buffer_get_string(&b
, NULL
);
1166 if (strcmp(p
, cuser
) != 0)
1170 if (buffer_len(&b
) != 0)
1177 mm_answer_keyverify(int sock
, Buffer
*m
)
1180 u_char
*signature
, *data
, *blob
;
1181 u_int signaturelen
, datalen
, bloblen
;
1185 blob
= buffer_get_string(m
, &bloblen
);
1186 signature
= buffer_get_string(m
, &signaturelen
);
1187 data
= buffer_get_string(m
, &datalen
);
1189 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1190 !monitor_allowed_key(blob
, bloblen
))
1191 fatal("%s: bad key, not previously allowed", __func__
);
1193 key
= key_from_blob(blob
, bloblen
);
1195 fatal("%s: bad public key blob", __func__
);
1197 switch (key_blobtype
) {
1199 valid_data
= monitor_valid_userblob(data
, datalen
);
1202 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1203 hostbased_cuser
, hostbased_chost
);
1210 fatal("%s: bad signature data blob", __func__
);
1212 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1213 debug3("%s: key %p signature %s",
1214 __func__
, key
, verified
? "verified" : "unverified");
1221 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1223 monitor_reset_key_state();
1226 buffer_put_int(m
, verified
);
1227 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1233 mm_record_login(Session
*s
, struct passwd
*pw
)
1236 struct sockaddr_storage from
;
1239 * Get IP address of client. If the connection is not a socket, let
1240 * the address be 0.0.0.0.
1242 memset(&from
, 0, sizeof(from
));
1243 fromlen
= sizeof(from
);
1244 if (packet_connection_is_on_socket()) {
1245 if (getpeername(packet_get_connection_in(),
1246 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1247 debug("getpeername: %.100s", strerror(errno
));
1251 /* Record that there was a login on that tty from the remote host. */
1252 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1253 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1254 (struct sockaddr
*)&from
, fromlen
);
1258 mm_session_close(Session
*s
)
1260 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1261 if (s
->ttyfd
!= -1) {
1262 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1263 session_pty_cleanup2(s
);
1269 mm_answer_pty(int sock
, Buffer
*m
)
1271 extern struct monitor
*pmonitor
;
1275 debug3("%s entering", __func__
);
1281 s
->authctxt
= authctxt
;
1282 s
->pw
= authctxt
->pw
;
1283 s
->pid
= pmonitor
->m_pid
;
1284 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1287 pty_setowner(authctxt
->pw
, s
->tty
);
1289 buffer_put_int(m
, 1);
1290 buffer_put_cstring(m
, s
->tty
);
1292 /* We need to trick ttyslot */
1293 if (dup2(s
->ttyfd
, 0) == -1)
1294 fatal("%s: dup2", __func__
);
1296 mm_record_login(s
, authctxt
->pw
);
1298 /* Now we can close the file descriptor again */
1301 /* send messages generated by record_login */
1302 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1303 buffer_clear(&loginmsg
);
1305 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1307 mm_send_fd(sock
, s
->ptyfd
);
1308 mm_send_fd(sock
, s
->ttyfd
);
1310 /* make sure nothing uses fd 0 */
1311 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1312 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1314 error("%s: fd0 %d != 0", __func__
, fd0
);
1316 /* slave is not needed */
1318 s
->ttyfd
= s
->ptyfd
;
1319 /* no need to dup() because nobody closes ptyfd */
1320 s
->ptymaster
= s
->ptyfd
;
1322 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1328 mm_session_close(s
);
1329 buffer_put_int(m
, 0);
1330 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1335 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1340 debug3("%s entering", __func__
);
1342 tty
= buffer_get_string(m
, NULL
);
1343 if ((s
= session_by_tty(tty
)) != NULL
)
1344 mm_session_close(s
);
1351 mm_answer_sesskey(int sock
, Buffer
*m
)
1356 /* Turn off permissions */
1357 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1359 if ((p
= BN_new()) == NULL
)
1360 fatal("%s: BN_new", __func__
);
1362 buffer_get_bignum2(m
, p
);
1364 rsafail
= ssh1_session_key(p
);
1367 buffer_put_int(m
, rsafail
);
1368 buffer_put_bignum2(m
, p
);
1372 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1374 /* Turn on permissions for sessid passing */
1375 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1381 mm_answer_sessid(int sock
, Buffer
*m
)
1385 debug3("%s entering", __func__
);
1387 if (buffer_len(m
) != 16)
1388 fatal("%s: bad ssh1 session id", __func__
);
1389 for (i
= 0; i
< 16; i
++)
1390 session_id
[i
] = buffer_get_char(m
);
1392 /* Turn on permissions for getpwnam */
1393 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1399 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1403 u_char
*blob
= NULL
;
1407 debug3("%s entering", __func__
);
1409 auth_method
= "rsa";
1410 if (options
.rsa_authentication
&& authctxt
->valid
) {
1411 if ((client_n
= BN_new()) == NULL
)
1412 fatal("%s: BN_new", __func__
);
1413 buffer_get_bignum2(m
, client_n
);
1414 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1415 BN_clear_free(client_n
);
1418 buffer_put_int(m
, allowed
);
1419 buffer_put_int(m
, forced_command
!= NULL
);
1421 /* clear temporarily storage (used by generate challenge) */
1422 monitor_reset_key_state();
1424 if (allowed
&& key
!= NULL
) {
1425 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1426 if (key_to_blob(key
, &blob
, &blen
) == 0)
1427 fatal("%s: key_to_blob failed", __func__
);
1428 buffer_put_string(m
, blob
, blen
);
1430 /* Save temporarily for comparison in verify */
1433 key_blobtype
= MM_RSAUSERKEY
;
1440 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1442 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1443 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1448 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1454 debug3("%s entering", __func__
);
1456 if (!authctxt
->valid
)
1457 fatal("%s: authctxt not valid", __func__
);
1458 blob
= buffer_get_string(m
, &blen
);
1459 if (!monitor_allowed_key(blob
, blen
))
1460 fatal("%s: bad key, not previously allowed", __func__
);
1461 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1462 fatal("%s: key type mismatch", __func__
);
1463 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1464 fatal("%s: received bad key", __func__
);
1467 BN_clear_free(ssh1_challenge
);
1468 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1471 buffer_put_bignum2(m
, ssh1_challenge
);
1473 debug3("%s sending reply", __func__
);
1474 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1476 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1484 mm_answer_rsa_response(int sock
, Buffer
*m
)
1487 u_char
*blob
, *response
;
1491 debug3("%s entering", __func__
);
1493 if (!authctxt
->valid
)
1494 fatal("%s: authctxt not valid", __func__
);
1495 if (ssh1_challenge
== NULL
)
1496 fatal("%s: no ssh1_challenge", __func__
);
1498 blob
= buffer_get_string(m
, &blen
);
1499 if (!monitor_allowed_key(blob
, blen
))
1500 fatal("%s: bad key, not previously allowed", __func__
);
1501 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1502 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1503 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1504 fatal("%s: received bad key", __func__
);
1505 response
= buffer_get_string(m
, &len
);
1507 fatal("%s: received bad response to challenge", __func__
);
1508 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1514 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1517 BN_clear_free(ssh1_challenge
);
1518 ssh1_challenge
= NULL
;
1519 monitor_reset_key_state();
1522 buffer_put_int(m
, success
);
1523 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1529 mm_answer_term(int sock
, Buffer
*req
)
1531 extern struct monitor
*pmonitor
;
1534 debug3("%s: tearing down sessions", __func__
);
1536 /* The child is terminating */
1537 session_destroy_all(&mm_session_close
);
1539 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1543 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1545 /* Terminate process */
1549 #ifdef SSH_AUDIT_EVENTS
1550 /* Report that an audit event occurred */
1552 mm_answer_audit_event(int socket
, Buffer
*m
)
1554 ssh_audit_event_t event
;
1556 debug3("%s entering", __func__
);
1558 event
= buffer_get_int(m
);
1560 case SSH_AUTH_FAIL_PUBKEY
:
1561 case SSH_AUTH_FAIL_HOSTBASED
:
1562 case SSH_AUTH_FAIL_GSSAPI
:
1563 case SSH_LOGIN_EXCEED_MAXTRIES
:
1564 case SSH_LOGIN_ROOT_DENIED
:
1565 case SSH_CONNECTION_CLOSE
:
1566 case SSH_INVALID_USER
:
1570 fatal("Audit event type %d not permitted", event
);
1577 mm_answer_audit_command(int socket
, Buffer
*m
)
1582 debug3("%s entering", __func__
);
1583 cmd
= buffer_get_string(m
, &len
);
1584 /* sanity check command, if so how? */
1585 audit_run_command(cmd
);
1589 #endif /* SSH_AUDIT_EVENTS */
1592 monitor_apply_keystate(struct monitor
*pmonitor
)
1595 set_newkeys(MODE_IN
);
1596 set_newkeys(MODE_OUT
);
1598 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1599 packet_set_encryption_key(child_state
.ssh1key
,
1600 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1601 xfree(child_state
.ssh1key
);
1604 /* for rc4 and other stateful ciphers */
1605 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1606 xfree(child_state
.keyout
);
1607 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1608 xfree(child_state
.keyin
);
1611 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1612 xfree(child_state
.ivout
);
1613 packet_set_iv(MODE_IN
, child_state
.ivin
);
1614 xfree(child_state
.ivin
);
1617 memcpy(&incoming_stream
, &child_state
.incoming
,
1618 sizeof(incoming_stream
));
1619 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1620 sizeof(outgoing_stream
));
1622 /* Update with new address */
1623 if (options
.compression
)
1624 mm_init_compression(pmonitor
->m_zlib
);
1626 /* Network I/O buffers */
1627 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1628 buffer_clear(&input
);
1629 buffer_append(&input
, child_state
.input
, child_state
.ilen
);
1630 memset(child_state
.input
, 0, child_state
.ilen
);
1631 xfree(child_state
.input
);
1633 buffer_clear(&output
);
1634 buffer_append(&output
, child_state
.output
, child_state
.olen
);
1635 memset(child_state
.output
, 0, child_state
.olen
);
1636 xfree(child_state
.output
);
1640 mm_get_kex(Buffer
*m
)
1646 kex
= xcalloc(1, sizeof(*kex
));
1647 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1648 if ((session_id2
== NULL
) ||
1649 (kex
->session_id_len
!= session_id2_len
) ||
1650 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1651 fatal("mm_get_get: internal error: bad session id");
1652 kex
->we_need
= buffer_get_int(m
);
1653 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1654 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1655 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1656 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1658 kex
->hostkey_type
= buffer_get_int(m
);
1659 kex
->kex_type
= buffer_get_int(m
);
1660 blob
= buffer_get_string(m
, &bloblen
);
1661 buffer_init(&kex
->my
);
1662 buffer_append(&kex
->my
, blob
, bloblen
);
1664 blob
= buffer_get_string(m
, &bloblen
);
1665 buffer_init(&kex
->peer
);
1666 buffer_append(&kex
->peer
, blob
, bloblen
);
1669 kex
->flags
= buffer_get_int(m
);
1670 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1671 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1672 kex
->load_host_key
=&get_hostkey_by_type
;
1673 kex
->host_key_index
=&get_hostkey_index
;
1678 /* This function requries careful sanity checking */
1681 mm_get_keystate(struct monitor
*pmonitor
)
1685 u_int bloblen
, plen
;
1686 u_int32_t seqnr
, packets
;
1689 debug3("%s: Waiting for new keys", __func__
);
1692 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1694 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1695 child_state
.ssh1cipher
= buffer_get_int(&m
);
1696 child_state
.ssh1key
= buffer_get_string(&m
,
1697 &child_state
.ssh1keylen
);
1698 child_state
.ivout
= buffer_get_string(&m
,
1699 &child_state
.ivoutlen
);
1700 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1703 /* Get the Kex for rekeying */
1704 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1707 blob
= buffer_get_string(&m
, &bloblen
);
1708 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1711 debug3("%s: Waiting for second key", __func__
);
1712 blob
= buffer_get_string(&m
, &bloblen
);
1713 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1716 /* Now get sequence numbers for the packets */
1717 seqnr
= buffer_get_int(&m
);
1718 blocks
= buffer_get_int64(&m
);
1719 packets
= buffer_get_int(&m
);
1720 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
);
1721 seqnr
= buffer_get_int(&m
);
1722 blocks
= buffer_get_int64(&m
);
1723 packets
= buffer_get_int(&m
);
1724 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
);
1727 /* Get the key context */
1728 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1729 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1731 debug3("%s: Getting compression state", __func__
);
1732 /* Get compression state */
1733 p
= buffer_get_string(&m
, &plen
);
1734 if (plen
!= sizeof(child_state
.outgoing
))
1735 fatal("%s: bad request size", __func__
);
1736 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1739 p
= buffer_get_string(&m
, &plen
);
1740 if (plen
!= sizeof(child_state
.incoming
))
1741 fatal("%s: bad request size", __func__
);
1742 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1745 /* Network I/O buffers */
1746 debug3("%s: Getting Network I/O buffers", __func__
);
1747 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1748 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1754 /* Allocation functions for zlib */
1756 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1758 size_t len
= (size_t) size
* ncount
;
1761 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1762 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1764 address
= mm_malloc(mm
, len
);
1770 mm_zfree(struct mm_master
*mm
, void *address
)
1772 mm_free(mm
, address
);
1776 mm_init_compression(struct mm_master
*mm
)
1778 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1779 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1780 outgoing_stream
.opaque
= mm
;
1782 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1783 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1784 incoming_stream
.opaque
= mm
;
1789 #define FD_CLOSEONEXEC(x) do { \
1790 if (fcntl(x, F_SETFD, 1) == -1) \
1791 fatal("fcntl(%d, F_SETFD)", x); \
1795 monitor_socketpair(int *pair
)
1797 #ifdef HAVE_SOCKETPAIR
1798 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1799 fatal("%s: socketpair", __func__
);
1801 fatal("%s: UsePrivilegeSeparation=yes not supported",
1804 FD_CLOSEONEXEC(pair
[0]);
1805 FD_CLOSEONEXEC(pair
[1]);
1808 #define MM_MEMSIZE 65536
1813 struct monitor
*mon
;
1816 mon
= xcalloc(1, sizeof(*mon
));
1818 monitor_socketpair(pair
);
1820 mon
->m_recvfd
= pair
[0];
1821 mon
->m_sendfd
= pair
[1];
1823 /* Used to share zlib space across processes */
1824 if (options
.compression
) {
1825 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1826 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1828 /* Compression needs to share state across borders */
1829 mm_init_compression(mon
->m_zlib
);
1836 monitor_reinit(struct monitor
*mon
)
1840 monitor_socketpair(pair
);
1842 mon
->m_recvfd
= pair
[0];
1843 mon
->m_sendfd
= pair
[1];
1848 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1854 goid
.elements
= buffer_get_string(m
, &len
);
1857 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1859 xfree(goid
.elements
);
1862 buffer_put_int(m
, major
);
1864 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1866 /* Now we have a context, enable the step */
1867 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1873 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1876 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1877 OM_uint32 major
, minor
;
1878 OM_uint32 flags
= 0; /* GSI needs this */
1881 in
.value
= buffer_get_string(m
, &len
);
1883 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1887 buffer_put_int(m
, major
);
1888 buffer_put_string(m
, out
.value
, out
.length
);
1889 buffer_put_int(m
, flags
);
1890 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1892 gss_release_buffer(&minor
, &out
);
1894 if (major
== GSS_S_COMPLETE
) {
1895 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1896 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1897 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1903 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1905 gss_buffer_desc gssbuf
, mic
;
1909 gssbuf
.value
= buffer_get_string(m
, &len
);
1910 gssbuf
.length
= len
;
1911 mic
.value
= buffer_get_string(m
, &len
);
1914 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1916 xfree(gssbuf
.value
);
1920 buffer_put_int(m
, ret
);
1922 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1924 if (!GSS_ERROR(ret
))
1925 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1931 mm_answer_gss_userok(int sock
, Buffer
*m
)
1935 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1938 buffer_put_int(m
, authenticated
);
1940 debug3("%s: sending result %d", __func__
, authenticated
);
1941 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1943 auth_method
= "gssapi-with-mic";
1945 /* Monitor loop will terminate if authenticated */
1946 return (authenticated
);