1 /* $OpenBSD: monitor.c,v 1.86 2006/08/04 20:46:05 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/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
50 #include <openssl/dh.h>
61 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
64 #define TARGET_OS_MAC 1
69 #include "auth-options.h"
78 #include "monitor_mm.h"
82 #include "monitor_wrap.h"
83 #include "monitor_fdpass.h"
89 static Gssctxt
*gsscontext
= NULL
;
93 extern ServerOptions options
;
94 extern u_int utmp_len
;
95 extern Newkeys
*current_keys
[];
96 extern z_stream incoming_stream
;
97 extern z_stream outgoing_stream
;
98 extern u_char session_id
[];
99 extern Buffer input
, output
;
100 extern Buffer auth_debug
;
101 extern int auth_debug_init
;
102 extern Buffer loginmsg
;
104 /* State exported from the child */
127 /* Functions on the monitor that answer unprivileged requests */
129 int mm_answer_moduli(int, Buffer
*);
130 int mm_answer_sign(int, Buffer
*);
131 int mm_answer_pwnamallow(int, Buffer
*);
132 int mm_answer_auth2_read_banner(int, Buffer
*);
133 int mm_answer_authserv(int, Buffer
*);
134 int mm_answer_authpassword(int, Buffer
*);
135 int mm_answer_bsdauthquery(int, Buffer
*);
136 int mm_answer_bsdauthrespond(int, Buffer
*);
137 int mm_answer_skeyquery(int, Buffer
*);
138 int mm_answer_skeyrespond(int, Buffer
*);
139 int mm_answer_keyallowed(int, Buffer
*);
140 int mm_answer_keyverify(int, Buffer
*);
141 int mm_answer_pty(int, Buffer
*);
142 int mm_answer_pty_cleanup(int, Buffer
*);
143 int mm_answer_term(int, Buffer
*);
144 int mm_answer_rsa_keyallowed(int, Buffer
*);
145 int mm_answer_rsa_challenge(int, Buffer
*);
146 int mm_answer_rsa_response(int, Buffer
*);
147 int mm_answer_sesskey(int, Buffer
*);
148 int mm_answer_sessid(int, Buffer
*);
151 int mm_answer_pam_start(int, Buffer
*);
152 int mm_answer_pam_account(int, Buffer
*);
153 int mm_answer_pam_init_ctx(int, Buffer
*);
154 int mm_answer_pam_query(int, Buffer
*);
155 int mm_answer_pam_respond(int, Buffer
*);
156 int mm_answer_pam_free_ctx(int, Buffer
*);
160 int mm_answer_gss_setup_ctx(int, Buffer
*);
161 int mm_answer_gss_accept_ctx(int, Buffer
*);
162 int mm_answer_gss_userok(int, Buffer
*);
163 int mm_answer_gss_checkmic(int, Buffer
*);
166 #ifdef SSH_AUDIT_EVENTS
167 int mm_answer_audit_event(int, Buffer
*);
168 int mm_answer_audit_command(int, Buffer
*);
171 static Authctxt
*authctxt
;
172 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
174 /* local state for key verify */
175 static u_char
*key_blob
= NULL
;
176 static u_int key_bloblen
= 0;
177 static int key_blobtype
= MM_NOKEY
;
178 static char *hostbased_cuser
= NULL
;
179 static char *hostbased_chost
= NULL
;
180 static char *auth_method
= "unknown";
181 static u_int session_id2_len
= 0;
182 static u_char
*session_id2
= NULL
;
183 static pid_t monitor_child_pid
;
186 enum monitor_reqtype type
;
188 int (*f
)(int, Buffer
*);
191 #define MON_ISAUTH 0x0004 /* Required for Authentication */
192 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
193 #define MON_ONCE 0x0010 /* Disable after calling */
194 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
196 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
198 #define MON_PERMIT 0x1000 /* Request is permitted */
200 struct mon_table mon_dispatch_proto20
[] = {
201 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
202 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
203 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
204 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
205 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
206 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
208 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
209 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
210 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
211 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
212 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
213 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
215 #ifdef SSH_AUDIT_EVENTS
216 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
219 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
220 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
223 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
224 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
226 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
227 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
229 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
230 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
231 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
232 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
237 struct mon_table mon_dispatch_postauth20
[] = {
238 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
239 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
240 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
241 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
242 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
243 #ifdef SSH_AUDIT_EVENTS
244 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
245 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
250 struct mon_table mon_dispatch_proto15
[] = {
251 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
252 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
253 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
254 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
255 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
256 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
257 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
258 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
260 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
261 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
264 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
265 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
268 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
269 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
270 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
271 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
272 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
273 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
275 #ifdef SSH_AUDIT_EVENTS
276 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
281 struct mon_table mon_dispatch_postauth15
[] = {
282 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
283 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
284 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
285 #ifdef SSH_AUDIT_EVENTS
286 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
287 {MONITOR_REQ_AUDIT_COMMAND
, MON_ONCE
, mm_answer_audit_command
},
292 struct mon_table
*mon_dispatch
;
294 /* Specifies if a certain message is allowed at the moment */
297 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
299 while (ent
->f
!= NULL
) {
300 if (ent
->type
== type
) {
301 ent
->flags
&= ~MON_PERMIT
;
302 ent
->flags
|= permit
? MON_PERMIT
: 0;
310 monitor_permit_authentications(int permit
)
312 struct mon_table
*ent
= mon_dispatch
;
314 while (ent
->f
!= NULL
) {
315 if (ent
->flags
& MON_AUTH
) {
316 ent
->flags
&= ~MON_PERMIT
;
317 ent
->flags
|= permit
? MON_PERMIT
: 0;
324 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
326 struct mon_table
*ent
;
327 int authenticated
= 0;
329 debug3("preauth child monitor started");
331 authctxt
= _authctxt
;
332 memset(authctxt
, 0, sizeof(*authctxt
));
334 authctxt
->loginmsg
= &loginmsg
;
337 mon_dispatch
= mon_dispatch_proto20
;
339 /* Permit requests for moduli and signatures */
340 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
341 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
343 mon_dispatch
= mon_dispatch_proto15
;
345 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
348 /* The first few requests do not require asynchronous access */
349 while (!authenticated
) {
350 auth_method
= "unknown";
351 authenticated
= monitor_read(pmonitor
, mon_dispatch
, &ent
);
353 if (!(ent
->flags
& MON_AUTHDECIDE
))
354 fatal("%s: unexpected authentication from %d",
355 __func__
, ent
->type
);
356 if (authctxt
->pw
->pw_uid
== 0 &&
357 !auth_root_allowed(auth_method
))
360 /* PAM needs to perform account checks after auth */
361 if (options
.use_pam
&& authenticated
) {
365 mm_request_receive_expect(pmonitor
->m_sendfd
,
366 MONITOR_REQ_PAM_ACCOUNT
, &m
);
367 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
373 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
374 auth_log(authctxt
, authenticated
, auth_method
,
375 compat20
? " ssh2" : "");
377 authctxt
->failures
++;
381 if (!authctxt
->valid
)
382 fatal("%s: authenticated invalid user", __func__
);
383 if (strcmp(auth_method
, "unknown") == 0)
384 fatal("%s: authentication method name unknown", __func__
);
386 debug("%s: %s has been authenticated by privileged process",
387 __func__
, authctxt
->user
);
389 mm_get_keystate(pmonitor
);
393 monitor_set_child_handler(pid_t pid
)
395 monitor_child_pid
= pid
;
399 monitor_child_handler(int sig
)
401 kill(monitor_child_pid
, sig
);
405 monitor_child_postauth(struct monitor
*pmonitor
)
407 monitor_set_child_handler(pmonitor
->m_pid
);
408 signal(SIGHUP
, &monitor_child_handler
);
409 signal(SIGTERM
, &monitor_child_handler
);
412 mon_dispatch
= mon_dispatch_postauth20
;
414 /* Permit requests for moduli and signatures */
415 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
416 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
417 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
419 mon_dispatch
= mon_dispatch_postauth15
;
420 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
423 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
424 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
428 monitor_read(pmonitor
, mon_dispatch
, NULL
);
432 monitor_sync(struct monitor
*pmonitor
)
434 if (options
.compression
) {
435 /* The member allocation is not visible, so sync it */
436 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
441 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
442 struct mon_table
**pent
)
450 mm_request_receive(pmonitor
->m_sendfd
, &m
);
451 type
= buffer_get_char(&m
);
453 debug3("%s: checking request %d", __func__
, type
);
455 while (ent
->f
!= NULL
) {
456 if (ent
->type
== type
)
461 if (ent
->f
!= NULL
) {
462 if (!(ent
->flags
& MON_PERMIT
))
463 fatal("%s: unpermitted request %d", __func__
,
465 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
468 /* The child may use this request only once, disable it */
469 if (ent
->flags
& MON_ONCE
) {
470 debug2("%s: %d used once, disabling now", __func__
,
472 ent
->flags
&= ~MON_PERMIT
;
481 fatal("%s: unsupported request: %d", __func__
, type
);
487 /* allowed key state */
489 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
491 /* make sure key is allowed */
492 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
493 memcmp(key_blob
, blob
, key_bloblen
))
499 monitor_reset_key_state(void)
502 if (key_blob
!= NULL
)
504 if (hostbased_cuser
!= NULL
)
505 xfree(hostbased_cuser
);
506 if (hostbased_chost
!= NULL
)
507 xfree(hostbased_chost
);
510 key_blobtype
= MM_NOKEY
;
511 hostbased_cuser
= NULL
;
512 hostbased_chost
= NULL
;
516 mm_answer_moduli(int sock
, Buffer
*m
)
521 min
= buffer_get_int(m
);
522 want
= buffer_get_int(m
);
523 max
= buffer_get_int(m
);
525 debug3("%s: got parameters: %d %d %d",
526 __func__
, min
, want
, max
);
527 /* We need to check here, too, in case the child got corrupted */
528 if (max
< min
|| want
< min
|| max
< want
)
529 fatal("%s: bad parameters: %d %d %d",
530 __func__
, min
, want
, max
);
534 dh
= choose_dh(min
, want
, max
);
536 buffer_put_char(m
, 0);
539 /* Send first bignum */
540 buffer_put_char(m
, 1);
541 buffer_put_bignum2(m
, dh
->p
);
542 buffer_put_bignum2(m
, dh
->g
);
546 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
551 mm_answer_sign(int sock
, Buffer
*m
)
556 u_int siglen
, datlen
;
559 debug3("%s", __func__
);
561 keyid
= buffer_get_int(m
);
562 p
= buffer_get_string(m
, &datlen
);
565 * Supported KEX types will only return SHA1 (20 byte) or
566 * SHA256 (32 byte) hashes
568 if (datlen
!= 20 && datlen
!= 32)
569 fatal("%s: data length incorrect: %u", __func__
, datlen
);
571 /* save session id, it will be passed on the first call */
572 if (session_id2_len
== 0) {
573 session_id2_len
= datlen
;
574 session_id2
= xmalloc(session_id2_len
);
575 memcpy(session_id2
, p
, session_id2_len
);
578 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
579 fatal("%s: no hostkey from index %d", __func__
, keyid
);
580 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
581 fatal("%s: key_sign failed", __func__
);
583 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
586 buffer_put_string(m
, signature
, siglen
);
591 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
593 /* Turn on permissions for getpwnam */
594 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
599 /* Retrieves the password entry and also checks if the user is permitted */
602 mm_answer_pwnamallow(int sock
, Buffer
*m
)
605 struct passwd
*pwent
;
608 debug3("%s", __func__
);
610 if (authctxt
->attempt
++ != 0)
611 fatal("%s: multiple attempts for getpwnam", __func__
);
613 username
= buffer_get_string(m
, NULL
);
615 pwent
= getpwnamallow(username
);
617 authctxt
->user
= xstrdup(username
);
618 setproctitle("%s [priv]", pwent
? username
: "unknown");
624 buffer_put_char(m
, 0);
625 authctxt
->pw
= fakepw();
630 authctxt
->pw
= pwent
;
633 buffer_put_char(m
, 1);
634 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
635 buffer_put_cstring(m
, pwent
->pw_name
);
636 buffer_put_cstring(m
, "*");
637 buffer_put_cstring(m
, pwent
->pw_gecos
);
638 #ifdef HAVE_PW_CLASS_IN_PASSWD
639 buffer_put_cstring(m
, pwent
->pw_class
);
641 buffer_put_cstring(m
, pwent
->pw_dir
);
642 buffer_put_cstring(m
, pwent
->pw_shell
);
645 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
646 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
648 /* For SSHv1 allow authentication now */
650 monitor_permit_authentications(1);
652 /* Allow service/style information on the auth context */
653 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
654 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
659 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
661 #ifdef SSH_AUDIT_EVENTS
662 monitor_permit(mon_dispatch
, MONITOR_REQ_AUDIT_COMMAND
, 1);
668 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
673 banner
= auth2_read_banner();
674 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
675 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
684 mm_answer_authserv(int sock
, Buffer
*m
)
686 monitor_permit_authentications(1);
688 authctxt
->service
= buffer_get_string(m
, NULL
);
689 authctxt
->style
= buffer_get_string(m
, NULL
);
690 debug3("%s: service=%s, style=%s",
691 __func__
, authctxt
->service
, authctxt
->style
);
693 if (strlen(authctxt
->style
) == 0) {
694 xfree(authctxt
->style
);
695 authctxt
->style
= NULL
;
702 mm_answer_authpassword(int sock
, Buffer
*m
)
704 static int call_count
;
709 passwd
= buffer_get_string(m
, &plen
);
710 /* Only authenticate if the context is valid */
711 authenticated
= options
.password_authentication
&&
712 auth_password(authctxt
, passwd
);
713 memset(passwd
, 0, strlen(passwd
));
717 buffer_put_int(m
, authenticated
);
719 debug3("%s: sending result %d", __func__
, authenticated
);
720 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
723 if (plen
== 0 && call_count
== 1)
724 auth_method
= "none";
726 auth_method
= "password";
728 /* Causes monitor loop to terminate if authenticated */
729 return (authenticated
);
734 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
736 char *name
, *infotxt
;
742 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
743 &prompts
, &echo_on
) < 0 ? 0 : 1;
746 buffer_put_int(m
, success
);
748 buffer_put_cstring(m
, prompts
[0]);
750 debug3("%s: sending challenge success: %u", __func__
, success
);
751 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
764 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
769 if (authctxt
->as
== 0)
770 fatal("%s: no bsd auth session", __func__
);
772 response
= buffer_get_string(m
, NULL
);
773 authok
= options
.challenge_response_authentication
&&
774 auth_userresponse(authctxt
->as
, response
, 0);
776 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
780 buffer_put_int(m
, authok
);
782 debug3("%s: sending authenticated: %d", __func__
, authok
);
783 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
785 auth_method
= "bsdauth";
787 return (authok
!= 0);
793 mm_answer_skeyquery(int sock
, Buffer
*m
)
796 char challenge
[1024];
799 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
800 sizeof(challenge
)) < 0 ? 0 : 1;
803 buffer_put_int(m
, success
);
805 buffer_put_cstring(m
, challenge
);
807 debug3("%s: sending challenge success: %u", __func__
, success
);
808 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
814 mm_answer_skeyrespond(int sock
, Buffer
*m
)
819 response
= buffer_get_string(m
, NULL
);
821 authok
= (options
.challenge_response_authentication
&&
823 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
824 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
829 buffer_put_int(m
, authok
);
831 debug3("%s: sending authenticated: %d", __func__
, authok
);
832 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
834 auth_method
= "skey";
836 return (authok
!= 0);
842 mm_answer_pam_start(int sock
, Buffer
*m
)
844 if (!options
.use_pam
)
845 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
849 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
855 mm_answer_pam_account(int sock
, Buffer
*m
)
859 if (!options
.use_pam
)
860 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
862 ret
= do_pam_account();
864 buffer_put_int(m
, ret
);
865 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
867 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
872 static void *sshpam_ctxt
, *sshpam_authok
;
873 extern KbdintDevice sshpam_device
;
876 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
879 debug3("%s", __func__
);
880 authctxt
->user
= buffer_get_string(m
, NULL
);
881 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
882 sshpam_authok
= NULL
;
884 if (sshpam_ctxt
!= NULL
) {
885 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
886 buffer_put_int(m
, 1);
888 buffer_put_int(m
, 0);
890 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
895 mm_answer_pam_query(int sock
, Buffer
*m
)
897 char *name
, *info
, **prompts
;
898 u_int i
, num
, *echo_on
;
901 debug3("%s", __func__
);
902 sshpam_authok
= NULL
;
903 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
904 if (ret
== 0 && num
== 0)
905 sshpam_authok
= sshpam_ctxt
;
906 if (num
> 1 || name
== NULL
|| info
== NULL
)
909 buffer_put_int(m
, ret
);
910 buffer_put_cstring(m
, name
);
912 buffer_put_cstring(m
, info
);
914 buffer_put_int(m
, num
);
915 for (i
= 0; i
< num
; ++i
) {
916 buffer_put_cstring(m
, prompts
[i
]);
918 buffer_put_int(m
, echo_on
[i
]);
924 auth_method
= "keyboard-interactive/pam";
925 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
930 mm_answer_pam_respond(int sock
, Buffer
*m
)
936 debug3("%s", __func__
);
937 sshpam_authok
= NULL
;
938 num
= buffer_get_int(m
);
940 resp
= xcalloc(num
, sizeof(char *));
941 for (i
= 0; i
< num
; ++i
)
942 resp
[i
] = buffer_get_string(m
, NULL
);
943 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
944 for (i
= 0; i
< num
; ++i
)
948 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
951 buffer_put_int(m
, ret
);
952 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
953 auth_method
= "keyboard-interactive/pam";
955 sshpam_authok
= sshpam_ctxt
;
960 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
963 debug3("%s", __func__
);
964 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
966 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
967 auth_method
= "keyboard-interactive/pam";
968 return (sshpam_authok
== sshpam_ctxt
);
973 mm_append_debug(Buffer
*m
)
975 if (auth_debug_init
&& buffer_len(&auth_debug
)) {
976 debug3("%s: Appending debug messages for child", __func__
);
977 buffer_append(m
, buffer_ptr(&auth_debug
),
978 buffer_len(&auth_debug
));
979 buffer_clear(&auth_debug
);
984 mm_answer_keyallowed(int sock
, Buffer
*m
)
990 enum mm_keytype type
= 0;
993 debug3("%s entering", __func__
);
995 type
= buffer_get_int(m
);
996 cuser
= buffer_get_string(m
, NULL
);
997 chost
= buffer_get_string(m
, NULL
);
998 blob
= buffer_get_string(m
, &bloblen
);
1000 key
= key_from_blob(blob
, bloblen
);
1002 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
1003 (!compat20
&& type
!= MM_RSAHOSTKEY
))
1004 fatal("%s: key type and protocol mismatch", __func__
);
1006 debug3("%s: key_from_blob: %p", __func__
, key
);
1008 if (key
!= NULL
&& authctxt
->valid
) {
1011 allowed
= options
.pubkey_authentication
&&
1012 user_key_allowed(authctxt
->pw
, key
);
1013 auth_method
= "publickey";
1016 allowed
= options
.hostbased_authentication
&&
1017 hostbased_key_allowed(authctxt
->pw
,
1019 auth_method
= "hostbased";
1022 key
->type
= KEY_RSA1
; /* XXX */
1023 allowed
= options
.rhosts_rsa_authentication
&&
1024 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1026 auth_method
= "rsa";
1029 fatal("%s: unknown key type %d", __func__
, type
);
1036 /* clear temporarily storage (used by verify) */
1037 monitor_reset_key_state();
1040 /* Save temporarily for comparison in verify */
1042 key_bloblen
= bloblen
;
1043 key_blobtype
= type
;
1044 hostbased_cuser
= cuser
;
1045 hostbased_chost
= chost
;
1047 /* Log failed attempt */
1048 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1054 debug3("%s: key %p is %s",
1055 __func__
, key
, allowed
? "allowed" : "disallowed");
1058 buffer_put_int(m
, allowed
);
1059 buffer_put_int(m
, forced_command
!= NULL
);
1063 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1065 if (type
== MM_RSAHOSTKEY
)
1066 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1072 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1080 buffer_append(&b
, data
, datalen
);
1082 if (datafellows
& SSH_OLD_SESSIONID
) {
1084 len
= buffer_len(&b
);
1085 if ((session_id2
== NULL
) ||
1086 (len
< session_id2_len
) ||
1087 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1089 buffer_consume(&b
, session_id2_len
);
1091 p
= buffer_get_string(&b
, &len
);
1092 if ((session_id2
== NULL
) ||
1093 (len
!= session_id2_len
) ||
1094 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1098 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1100 p
= buffer_get_string(&b
, NULL
);
1101 if (strcmp(authctxt
->user
, p
) != 0) {
1102 logit("wrong user name passed to monitor: expected %s != %.100s",
1107 buffer_skip_string(&b
);
1108 if (datafellows
& SSH_BUG_PKAUTH
) {
1109 if (!buffer_get_char(&b
))
1112 p
= buffer_get_string(&b
, NULL
);
1113 if (strcmp("publickey", p
) != 0)
1116 if (!buffer_get_char(&b
))
1118 buffer_skip_string(&b
);
1120 buffer_skip_string(&b
);
1121 if (buffer_len(&b
) != 0)
1128 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1137 buffer_append(&b
, data
, datalen
);
1139 p
= buffer_get_string(&b
, &len
);
1140 if ((session_id2
== NULL
) ||
1141 (len
!= session_id2_len
) ||
1142 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1146 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1148 p
= buffer_get_string(&b
, NULL
);
1149 if (strcmp(authctxt
->user
, p
) != 0) {
1150 logit("wrong user name passed to monitor: expected %s != %.100s",
1155 buffer_skip_string(&b
); /* service */
1156 p
= buffer_get_string(&b
, NULL
);
1157 if (strcmp(p
, "hostbased") != 0)
1160 buffer_skip_string(&b
); /* pkalg */
1161 buffer_skip_string(&b
); /* pkblob */
1163 /* verify client host, strip trailing dot if necessary */
1164 p
= buffer_get_string(&b
, NULL
);
1165 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1167 if (strcmp(p
, chost
) != 0)
1171 /* verify client user */
1172 p
= buffer_get_string(&b
, NULL
);
1173 if (strcmp(p
, cuser
) != 0)
1177 if (buffer_len(&b
) != 0)
1184 mm_answer_keyverify(int sock
, Buffer
*m
)
1187 u_char
*signature
, *data
, *blob
;
1188 u_int signaturelen
, datalen
, bloblen
;
1192 blob
= buffer_get_string(m
, &bloblen
);
1193 signature
= buffer_get_string(m
, &signaturelen
);
1194 data
= buffer_get_string(m
, &datalen
);
1196 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1197 !monitor_allowed_key(blob
, bloblen
))
1198 fatal("%s: bad key, not previously allowed", __func__
);
1200 key
= key_from_blob(blob
, bloblen
);
1202 fatal("%s: bad public key blob", __func__
);
1204 switch (key_blobtype
) {
1206 valid_data
= monitor_valid_userblob(data
, datalen
);
1209 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1210 hostbased_cuser
, hostbased_chost
);
1217 fatal("%s: bad signature data blob", __func__
);
1219 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1220 debug3("%s: key %p signature %s",
1221 __func__
, key
, verified
? "verified" : "unverified");
1228 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1230 monitor_reset_key_state();
1233 buffer_put_int(m
, verified
);
1234 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1240 mm_record_login(Session
*s
, struct passwd
*pw
)
1243 struct sockaddr_storage from
;
1246 * Get IP address of client. If the connection is not a socket, let
1247 * the address be 0.0.0.0.
1249 memset(&from
, 0, sizeof(from
));
1250 fromlen
= sizeof(from
);
1251 if (packet_connection_is_on_socket()) {
1252 if (getpeername(packet_get_connection_in(),
1253 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1254 debug("getpeername: %.100s", strerror(errno
));
1258 /* Record that there was a login on that tty from the remote host. */
1259 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1260 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1261 (struct sockaddr
*)&from
, fromlen
);
1265 mm_session_close(Session
*s
)
1267 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1268 if (s
->ttyfd
!= -1) {
1269 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1270 session_pty_cleanup2(s
);
1276 mm_answer_pty(int sock
, Buffer
*m
)
1278 extern struct monitor
*pmonitor
;
1282 debug3("%s entering", __func__
);
1288 s
->authctxt
= authctxt
;
1289 s
->pw
= authctxt
->pw
;
1290 s
->pid
= pmonitor
->m_pid
;
1291 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1294 pty_setowner(authctxt
->pw
, s
->tty
);
1296 buffer_put_int(m
, 1);
1297 buffer_put_cstring(m
, s
->tty
);
1299 /* We need to trick ttyslot */
1300 if (dup2(s
->ttyfd
, 0) == -1)
1301 fatal("%s: dup2", __func__
);
1303 mm_record_login(s
, authctxt
->pw
);
1305 /* Now we can close the file descriptor again */
1308 /* send messages generated by record_login */
1309 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1310 buffer_clear(&loginmsg
);
1312 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1314 mm_send_fd(sock
, s
->ptyfd
);
1315 mm_send_fd(sock
, s
->ttyfd
);
1317 /* make sure nothing uses fd 0 */
1318 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1319 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1321 error("%s: fd0 %d != 0", __func__
, fd0
);
1323 /* slave is not needed */
1325 s
->ttyfd
= s
->ptyfd
;
1326 /* no need to dup() because nobody closes ptyfd */
1327 s
->ptymaster
= s
->ptyfd
;
1329 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1335 mm_session_close(s
);
1336 buffer_put_int(m
, 0);
1337 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1342 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1347 debug3("%s entering", __func__
);
1349 tty
= buffer_get_string(m
, NULL
);
1350 if ((s
= session_by_tty(tty
)) != NULL
)
1351 mm_session_close(s
);
1358 mm_answer_sesskey(int sock
, Buffer
*m
)
1363 /* Turn off permissions */
1364 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1366 if ((p
= BN_new()) == NULL
)
1367 fatal("%s: BN_new", __func__
);
1369 buffer_get_bignum2(m
, p
);
1371 rsafail
= ssh1_session_key(p
);
1374 buffer_put_int(m
, rsafail
);
1375 buffer_put_bignum2(m
, p
);
1379 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1381 /* Turn on permissions for sessid passing */
1382 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1388 mm_answer_sessid(int sock
, Buffer
*m
)
1392 debug3("%s entering", __func__
);
1394 if (buffer_len(m
) != 16)
1395 fatal("%s: bad ssh1 session id", __func__
);
1396 for (i
= 0; i
< 16; i
++)
1397 session_id
[i
] = buffer_get_char(m
);
1399 /* Turn on permissions for getpwnam */
1400 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1406 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1410 u_char
*blob
= NULL
;
1414 debug3("%s entering", __func__
);
1416 auth_method
= "rsa";
1417 if (options
.rsa_authentication
&& authctxt
->valid
) {
1418 if ((client_n
= BN_new()) == NULL
)
1419 fatal("%s: BN_new", __func__
);
1420 buffer_get_bignum2(m
, client_n
);
1421 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1422 BN_clear_free(client_n
);
1425 buffer_put_int(m
, allowed
);
1426 buffer_put_int(m
, forced_command
!= NULL
);
1428 /* clear temporarily storage (used by generate challenge) */
1429 monitor_reset_key_state();
1431 if (allowed
&& key
!= NULL
) {
1432 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1433 if (key_to_blob(key
, &blob
, &blen
) == 0)
1434 fatal("%s: key_to_blob failed", __func__
);
1435 buffer_put_string(m
, blob
, blen
);
1437 /* Save temporarily for comparison in verify */
1440 key_blobtype
= MM_RSAUSERKEY
;
1447 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1449 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1450 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1455 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1461 debug3("%s entering", __func__
);
1463 if (!authctxt
->valid
)
1464 fatal("%s: authctxt not valid", __func__
);
1465 blob
= buffer_get_string(m
, &blen
);
1466 if (!monitor_allowed_key(blob
, blen
))
1467 fatal("%s: bad key, not previously allowed", __func__
);
1468 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1469 fatal("%s: key type mismatch", __func__
);
1470 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1471 fatal("%s: received bad key", __func__
);
1474 BN_clear_free(ssh1_challenge
);
1475 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1478 buffer_put_bignum2(m
, ssh1_challenge
);
1480 debug3("%s sending reply", __func__
);
1481 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1483 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1491 mm_answer_rsa_response(int sock
, Buffer
*m
)
1494 u_char
*blob
, *response
;
1498 debug3("%s entering", __func__
);
1500 if (!authctxt
->valid
)
1501 fatal("%s: authctxt not valid", __func__
);
1502 if (ssh1_challenge
== NULL
)
1503 fatal("%s: no ssh1_challenge", __func__
);
1505 blob
= buffer_get_string(m
, &blen
);
1506 if (!monitor_allowed_key(blob
, blen
))
1507 fatal("%s: bad key, not previously allowed", __func__
);
1508 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1509 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1510 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1511 fatal("%s: received bad key", __func__
);
1512 response
= buffer_get_string(m
, &len
);
1514 fatal("%s: received bad response to challenge", __func__
);
1515 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1521 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1524 BN_clear_free(ssh1_challenge
);
1525 ssh1_challenge
= NULL
;
1526 monitor_reset_key_state();
1529 buffer_put_int(m
, success
);
1530 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1536 mm_answer_term(int sock
, Buffer
*req
)
1538 extern struct monitor
*pmonitor
;
1541 debug3("%s: tearing down sessions", __func__
);
1543 /* The child is terminating */
1544 session_destroy_all(&mm_session_close
);
1546 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1550 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1552 /* Terminate process */
1556 #ifdef SSH_AUDIT_EVENTS
1557 /* Report that an audit event occurred */
1559 mm_answer_audit_event(int socket
, Buffer
*m
)
1561 ssh_audit_event_t event
;
1563 debug3("%s entering", __func__
);
1565 event
= buffer_get_int(m
);
1567 case SSH_AUTH_FAIL_PUBKEY
:
1568 case SSH_AUTH_FAIL_HOSTBASED
:
1569 case SSH_AUTH_FAIL_GSSAPI
:
1570 case SSH_LOGIN_EXCEED_MAXTRIES
:
1571 case SSH_LOGIN_ROOT_DENIED
:
1572 case SSH_CONNECTION_CLOSE
:
1573 case SSH_INVALID_USER
:
1577 fatal("Audit event type %d not permitted", event
);
1584 mm_answer_audit_command(int socket
, Buffer
*m
)
1589 debug3("%s entering", __func__
);
1590 cmd
= buffer_get_string(m
, &len
);
1591 /* sanity check command, if so how? */
1592 audit_run_command(cmd
);
1596 #endif /* SSH_AUDIT_EVENTS */
1599 monitor_apply_keystate(struct monitor
*pmonitor
)
1602 set_newkeys(MODE_IN
);
1603 set_newkeys(MODE_OUT
);
1605 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1606 packet_set_encryption_key(child_state
.ssh1key
,
1607 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1608 xfree(child_state
.ssh1key
);
1611 /* for rc4 and other stateful ciphers */
1612 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1613 xfree(child_state
.keyout
);
1614 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1615 xfree(child_state
.keyin
);
1618 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1619 xfree(child_state
.ivout
);
1620 packet_set_iv(MODE_IN
, child_state
.ivin
);
1621 xfree(child_state
.ivin
);
1624 memcpy(&incoming_stream
, &child_state
.incoming
,
1625 sizeof(incoming_stream
));
1626 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1627 sizeof(outgoing_stream
));
1629 /* Update with new address */
1630 if (options
.compression
)
1631 mm_init_compression(pmonitor
->m_zlib
);
1633 /* Network I/O buffers */
1634 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1635 buffer_clear(&input
);
1636 buffer_append(&input
, child_state
.input
, child_state
.ilen
);
1637 memset(child_state
.input
, 0, child_state
.ilen
);
1638 xfree(child_state
.input
);
1640 buffer_clear(&output
);
1641 buffer_append(&output
, child_state
.output
, child_state
.olen
);
1642 memset(child_state
.output
, 0, child_state
.olen
);
1643 xfree(child_state
.output
);
1647 mm_get_kex(Buffer
*m
)
1653 kex
= xcalloc(1, sizeof(*kex
));
1654 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1655 if ((session_id2
== NULL
) ||
1656 (kex
->session_id_len
!= session_id2_len
) ||
1657 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1658 fatal("mm_get_get: internal error: bad session id");
1659 kex
->we_need
= buffer_get_int(m
);
1660 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1661 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1662 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1663 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1665 kex
->hostkey_type
= buffer_get_int(m
);
1666 kex
->kex_type
= buffer_get_int(m
);
1667 blob
= buffer_get_string(m
, &bloblen
);
1668 buffer_init(&kex
->my
);
1669 buffer_append(&kex
->my
, blob
, bloblen
);
1671 blob
= buffer_get_string(m
, &bloblen
);
1672 buffer_init(&kex
->peer
);
1673 buffer_append(&kex
->peer
, blob
, bloblen
);
1676 kex
->flags
= buffer_get_int(m
);
1677 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1678 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1679 kex
->load_host_key
=&get_hostkey_by_type
;
1680 kex
->host_key_index
=&get_hostkey_index
;
1685 /* This function requries careful sanity checking */
1688 mm_get_keystate(struct monitor
*pmonitor
)
1692 u_int bloblen
, plen
;
1693 u_int32_t seqnr
, packets
;
1696 debug3("%s: Waiting for new keys", __func__
);
1699 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1701 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1702 child_state
.ssh1cipher
= buffer_get_int(&m
);
1703 child_state
.ssh1key
= buffer_get_string(&m
,
1704 &child_state
.ssh1keylen
);
1705 child_state
.ivout
= buffer_get_string(&m
,
1706 &child_state
.ivoutlen
);
1707 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1710 /* Get the Kex for rekeying */
1711 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1714 blob
= buffer_get_string(&m
, &bloblen
);
1715 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1718 debug3("%s: Waiting for second key", __func__
);
1719 blob
= buffer_get_string(&m
, &bloblen
);
1720 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1723 /* Now get sequence numbers for the packets */
1724 seqnr
= buffer_get_int(&m
);
1725 blocks
= buffer_get_int64(&m
);
1726 packets
= buffer_get_int(&m
);
1727 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
);
1728 seqnr
= buffer_get_int(&m
);
1729 blocks
= buffer_get_int64(&m
);
1730 packets
= buffer_get_int(&m
);
1731 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
);
1734 /* Get the key context */
1735 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1736 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1738 debug3("%s: Getting compression state", __func__
);
1739 /* Get compression state */
1740 p
= buffer_get_string(&m
, &plen
);
1741 if (plen
!= sizeof(child_state
.outgoing
))
1742 fatal("%s: bad request size", __func__
);
1743 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1746 p
= buffer_get_string(&m
, &plen
);
1747 if (plen
!= sizeof(child_state
.incoming
))
1748 fatal("%s: bad request size", __func__
);
1749 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1752 /* Network I/O buffers */
1753 debug3("%s: Getting Network I/O buffers", __func__
);
1754 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1755 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1761 /* Allocation functions for zlib */
1763 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1765 size_t len
= (size_t) size
* ncount
;
1768 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1769 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1771 address
= mm_malloc(mm
, len
);
1777 mm_zfree(struct mm_master
*mm
, void *address
)
1779 mm_free(mm
, address
);
1783 mm_init_compression(struct mm_master
*mm
)
1785 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1786 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1787 outgoing_stream
.opaque
= mm
;
1789 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1790 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1791 incoming_stream
.opaque
= mm
;
1796 #define FD_CLOSEONEXEC(x) do { \
1797 if (fcntl(x, F_SETFD, 1) == -1) \
1798 fatal("fcntl(%d, F_SETFD)", x); \
1802 monitor_socketpair(int *pair
)
1804 #ifdef HAVE_SOCKETPAIR
1805 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1806 fatal("%s: socketpair", __func__
);
1808 fatal("%s: UsePrivilegeSeparation=yes not supported",
1811 FD_CLOSEONEXEC(pair
[0]);
1812 FD_CLOSEONEXEC(pair
[1]);
1815 #define MM_MEMSIZE 65536
1820 struct monitor
*mon
;
1823 mon
= xcalloc(1, sizeof(*mon
));
1825 monitor_socketpair(pair
);
1827 mon
->m_recvfd
= pair
[0];
1828 mon
->m_sendfd
= pair
[1];
1830 /* Used to share zlib space across processes */
1831 if (options
.compression
) {
1832 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1833 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1835 /* Compression needs to share state across borders */
1836 mm_init_compression(mon
->m_zlib
);
1843 monitor_reinit(struct monitor
*mon
)
1847 monitor_socketpair(pair
);
1849 mon
->m_recvfd
= pair
[0];
1850 mon
->m_sendfd
= pair
[1];
1855 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1861 goid
.elements
= buffer_get_string(m
, &len
);
1864 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1866 xfree(goid
.elements
);
1869 buffer_put_int(m
, major
);
1871 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1873 /* Now we have a context, enable the step */
1874 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1880 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1883 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1884 OM_uint32 major
, minor
;
1885 OM_uint32 flags
= 0; /* GSI needs this */
1888 in
.value
= buffer_get_string(m
, &len
);
1890 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1894 buffer_put_int(m
, major
);
1895 buffer_put_string(m
, out
.value
, out
.length
);
1896 buffer_put_int(m
, flags
);
1897 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1899 gss_release_buffer(&minor
, &out
);
1901 if (major
== GSS_S_COMPLETE
) {
1902 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1903 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1904 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1910 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1912 gss_buffer_desc gssbuf
, mic
;
1916 gssbuf
.value
= buffer_get_string(m
, &len
);
1917 gssbuf
.length
= len
;
1918 mic
.value
= buffer_get_string(m
, &len
);
1921 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1923 xfree(gssbuf
.value
);
1927 buffer_put_int(m
, ret
);
1929 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1931 if (!GSS_ERROR(ret
))
1932 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1938 mm_answer_gss_userok(int sock
, Buffer
*m
)
1942 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1945 buffer_put_int(m
, authenticated
);
1947 debug3("%s: sending result %d", __func__
, authenticated
);
1948 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1950 auth_method
= "gssapi-with-mic";
1952 /* Monitor loop will terminate if authenticated */
1953 return (authenticated
);