1 /* $OpenBSD: monitor.c,v 1.91 2007/05/17 20:52:13 djm Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2002 Markus Friedl <markus@openbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include "openbsd-compat/sys-tree.h"
52 #include <openssl/dh.h>
63 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
66 #define TARGET_OS_MAC 1
71 #include "auth-options.h"
80 #include "monitor_mm.h"
84 #include "monitor_wrap.h"
85 #include "monitor_fdpass.h"
91 static Gssctxt
*gsscontext
= NULL
;
95 extern ServerOptions options
;
96 extern u_int utmp_len
;
97 extern Newkeys
*current_keys
[];
98 extern z_stream incoming_stream
;
99 extern z_stream outgoing_stream
;
100 extern u_char session_id
[];
101 extern Buffer input
, output
;
102 extern Buffer auth_debug
;
103 extern int auth_debug_init
;
104 extern Buffer loginmsg
;
106 /* State exported from the child */
129 /* Functions on the monitor that answer unprivileged requests */
131 int mm_answer_moduli(int, Buffer
*);
132 int mm_answer_sign(int, Buffer
*);
133 int mm_answer_pwnamallow(int, Buffer
*);
134 int mm_answer_auth2_read_banner(int, Buffer
*);
135 int mm_answer_authserv(int, Buffer
*);
136 int mm_answer_authpassword(int, Buffer
*);
137 int mm_answer_bsdauthquery(int, Buffer
*);
138 int mm_answer_bsdauthrespond(int, Buffer
*);
139 int mm_answer_skeyquery(int, Buffer
*);
140 int mm_answer_skeyrespond(int, Buffer
*);
141 int mm_answer_keyallowed(int, Buffer
*);
142 int mm_answer_keyverify(int, Buffer
*);
143 int mm_answer_pty(int, Buffer
*);
144 int mm_answer_pty_cleanup(int, Buffer
*);
145 int mm_answer_term(int, Buffer
*);
146 int mm_answer_rsa_keyallowed(int, Buffer
*);
147 int mm_answer_rsa_challenge(int, Buffer
*);
148 int mm_answer_rsa_response(int, Buffer
*);
149 int mm_answer_sesskey(int, Buffer
*);
150 int mm_answer_sessid(int, Buffer
*);
153 int mm_answer_pam_start(int, Buffer
*);
154 int mm_answer_pam_account(int, Buffer
*);
155 int mm_answer_pam_init_ctx(int, Buffer
*);
156 int mm_answer_pam_query(int, Buffer
*);
157 int mm_answer_pam_respond(int, Buffer
*);
158 int mm_answer_pam_free_ctx(int, Buffer
*);
162 int mm_answer_gss_setup_ctx(int, Buffer
*);
163 int mm_answer_gss_accept_ctx(int, Buffer
*);
164 int mm_answer_gss_userok(int, Buffer
*);
165 int mm_answer_gss_checkmic(int, Buffer
*);
168 #ifdef SSH_AUDIT_EVENTS
169 int mm_answer_audit_event(int, Buffer
*);
170 int mm_answer_audit_command(int, Buffer
*);
173 static Authctxt
*authctxt
;
174 static BIGNUM
*ssh1_challenge
= NULL
; /* used for ssh1 rsa auth */
176 /* local state for key verify */
177 static u_char
*key_blob
= NULL
;
178 static u_int key_bloblen
= 0;
179 static int key_blobtype
= MM_NOKEY
;
180 static char *hostbased_cuser
= NULL
;
181 static char *hostbased_chost
= NULL
;
182 static char *auth_method
= "unknown";
183 static u_int session_id2_len
= 0;
184 static u_char
*session_id2
= NULL
;
185 static pid_t monitor_child_pid
;
188 enum monitor_reqtype type
;
190 int (*f
)(int, Buffer
*);
193 #define MON_ISAUTH 0x0004 /* Required for Authentication */
194 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
195 #define MON_ONCE 0x0010 /* Disable after calling */
196 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
198 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
200 #define MON_PERMIT 0x1000 /* Request is permitted */
202 struct mon_table mon_dispatch_proto20
[] = {
203 {MONITOR_REQ_MODULI
, MON_ONCE
, mm_answer_moduli
},
204 {MONITOR_REQ_SIGN
, MON_ONCE
, mm_answer_sign
},
205 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
206 {MONITOR_REQ_AUTHSERV
, MON_ONCE
, mm_answer_authserv
},
207 {MONITOR_REQ_AUTH2_READ_BANNER
, MON_ONCE
, mm_answer_auth2_read_banner
},
208 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
210 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
211 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
212 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
213 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
214 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
215 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
217 #ifdef SSH_AUDIT_EVENTS
218 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
221 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
222 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
225 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
226 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
228 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
, mm_answer_keyallowed
},
229 {MONITOR_REQ_KEYVERIFY
, MON_AUTH
, mm_answer_keyverify
},
231 {MONITOR_REQ_GSSSETUP
, MON_ISAUTH
, mm_answer_gss_setup_ctx
},
232 {MONITOR_REQ_GSSSTEP
, MON_ISAUTH
, mm_answer_gss_accept_ctx
},
233 {MONITOR_REQ_GSSUSEROK
, MON_AUTH
, mm_answer_gss_userok
},
234 {MONITOR_REQ_GSSCHECKMIC
, MON_ISAUTH
, mm_answer_gss_checkmic
},
239 struct mon_table mon_dispatch_postauth20
[] = {
240 {MONITOR_REQ_MODULI
, 0, mm_answer_moduli
},
241 {MONITOR_REQ_SIGN
, 0, mm_answer_sign
},
242 {MONITOR_REQ_PTY
, 0, mm_answer_pty
},
243 {MONITOR_REQ_PTYCLEANUP
, 0, mm_answer_pty_cleanup
},
244 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
245 #ifdef SSH_AUDIT_EVENTS
246 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
247 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
, mm_answer_audit_command
},
252 struct mon_table mon_dispatch_proto15
[] = {
253 {MONITOR_REQ_PWNAM
, MON_ONCE
, mm_answer_pwnamallow
},
254 {MONITOR_REQ_SESSKEY
, MON_ONCE
, mm_answer_sesskey
},
255 {MONITOR_REQ_SESSID
, MON_ONCE
, mm_answer_sessid
},
256 {MONITOR_REQ_AUTHPASSWORD
, MON_AUTH
, mm_answer_authpassword
},
257 {MONITOR_REQ_RSAKEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_rsa_keyallowed
},
258 {MONITOR_REQ_KEYALLOWED
, MON_ISAUTH
|MON_ALOG
, mm_answer_keyallowed
},
259 {MONITOR_REQ_RSACHALLENGE
, MON_ONCE
, mm_answer_rsa_challenge
},
260 {MONITOR_REQ_RSARESPONSE
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_rsa_response
},
262 {MONITOR_REQ_BSDAUTHQUERY
, MON_ISAUTH
, mm_answer_bsdauthquery
},
263 {MONITOR_REQ_BSDAUTHRESPOND
, MON_AUTH
, mm_answer_bsdauthrespond
},
266 {MONITOR_REQ_SKEYQUERY
, MON_ISAUTH
, mm_answer_skeyquery
},
267 {MONITOR_REQ_SKEYRESPOND
, MON_AUTH
, mm_answer_skeyrespond
},
270 {MONITOR_REQ_PAM_START
, MON_ONCE
, mm_answer_pam_start
},
271 {MONITOR_REQ_PAM_ACCOUNT
, 0, mm_answer_pam_account
},
272 {MONITOR_REQ_PAM_INIT_CTX
, MON_ISAUTH
, mm_answer_pam_init_ctx
},
273 {MONITOR_REQ_PAM_QUERY
, MON_ISAUTH
, mm_answer_pam_query
},
274 {MONITOR_REQ_PAM_RESPOND
, MON_ISAUTH
, mm_answer_pam_respond
},
275 {MONITOR_REQ_PAM_FREE_CTX
, MON_ONCE
|MON_AUTHDECIDE
, mm_answer_pam_free_ctx
},
277 #ifdef SSH_AUDIT_EVENTS
278 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
283 struct mon_table mon_dispatch_postauth15
[] = {
284 {MONITOR_REQ_PTY
, MON_ONCE
, mm_answer_pty
},
285 {MONITOR_REQ_PTYCLEANUP
, MON_ONCE
, mm_answer_pty_cleanup
},
286 {MONITOR_REQ_TERM
, 0, mm_answer_term
},
287 #ifdef SSH_AUDIT_EVENTS
288 {MONITOR_REQ_AUDIT_EVENT
, MON_PERMIT
, mm_answer_audit_event
},
289 {MONITOR_REQ_AUDIT_COMMAND
, MON_PERMIT
|MON_ONCE
, mm_answer_audit_command
},
294 struct mon_table
*mon_dispatch
;
296 /* Specifies if a certain message is allowed at the moment */
299 monitor_permit(struct mon_table
*ent
, enum monitor_reqtype type
, int permit
)
301 while (ent
->f
!= NULL
) {
302 if (ent
->type
== type
) {
303 ent
->flags
&= ~MON_PERMIT
;
304 ent
->flags
|= permit
? MON_PERMIT
: 0;
312 monitor_permit_authentications(int permit
)
314 struct mon_table
*ent
= mon_dispatch
;
316 while (ent
->f
!= NULL
) {
317 if (ent
->flags
& MON_AUTH
) {
318 ent
->flags
&= ~MON_PERMIT
;
319 ent
->flags
|= permit
? MON_PERMIT
: 0;
326 monitor_child_preauth(Authctxt
*_authctxt
, struct monitor
*pmonitor
)
328 struct mon_table
*ent
;
329 int authenticated
= 0;
331 debug3("preauth child monitor started");
333 authctxt
= _authctxt
;
334 memset(authctxt
, 0, sizeof(*authctxt
));
336 authctxt
->loginmsg
= &loginmsg
;
339 mon_dispatch
= mon_dispatch_proto20
;
341 /* Permit requests for moduli and signatures */
342 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
343 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
345 mon_dispatch
= mon_dispatch_proto15
;
347 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 1);
350 /* The first few requests do not require asynchronous access */
351 while (!authenticated
) {
352 auth_method
= "unknown";
353 authenticated
= (monitor_read(pmonitor
, mon_dispatch
, &ent
) == 1);
355 if (!(ent
->flags
& MON_AUTHDECIDE
))
356 fatal("%s: unexpected authentication from %d",
357 __func__
, ent
->type
);
358 if (authctxt
->pw
->pw_uid
== 0 &&
359 !auth_root_allowed(auth_method
))
362 /* PAM needs to perform account checks after auth */
363 if (options
.use_pam
&& authenticated
) {
367 mm_request_receive_expect(pmonitor
->m_sendfd
,
368 MONITOR_REQ_PAM_ACCOUNT
, &m
);
369 authenticated
= mm_answer_pam_account(pmonitor
->m_sendfd
, &m
);
375 if (ent
->flags
& (MON_AUTHDECIDE
|MON_ALOG
)) {
376 auth_log(authctxt
, authenticated
, auth_method
,
377 compat20
? " ssh2" : "");
379 authctxt
->failures
++;
383 if (!authctxt
->valid
)
384 fatal("%s: authenticated invalid user", __func__
);
385 if (strcmp(auth_method
, "unknown") == 0)
386 fatal("%s: authentication method name unknown", __func__
);
388 debug("%s: %s has been authenticated by privileged process",
389 __func__
, authctxt
->user
);
391 mm_get_keystate(pmonitor
);
395 monitor_set_child_handler(pid_t pid
)
397 monitor_child_pid
= pid
;
401 monitor_child_handler(int sig
)
403 kill(monitor_child_pid
, sig
);
407 monitor_child_postauth(struct monitor
*pmonitor
)
409 monitor_set_child_handler(pmonitor
->m_pid
);
410 signal(SIGHUP
, &monitor_child_handler
);
411 signal(SIGTERM
, &monitor_child_handler
);
412 signal(SIGINT
, &monitor_child_handler
);
415 mon_dispatch
= mon_dispatch_postauth20
;
417 /* Permit requests for moduli and signatures */
418 monitor_permit(mon_dispatch
, MONITOR_REQ_MODULI
, 1);
419 monitor_permit(mon_dispatch
, MONITOR_REQ_SIGN
, 1);
420 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
422 mon_dispatch
= mon_dispatch_postauth15
;
423 monitor_permit(mon_dispatch
, MONITOR_REQ_TERM
, 1);
426 monitor_permit(mon_dispatch
, MONITOR_REQ_PTY
, 1);
427 monitor_permit(mon_dispatch
, MONITOR_REQ_PTYCLEANUP
, 1);
431 monitor_read(pmonitor
, mon_dispatch
, NULL
);
435 monitor_sync(struct monitor
*pmonitor
)
437 if (options
.compression
) {
438 /* The member allocation is not visible, so sync it */
439 mm_share_sync(&pmonitor
->m_zlib
, &pmonitor
->m_zback
);
444 monitor_read(struct monitor
*pmonitor
, struct mon_table
*ent
,
445 struct mon_table
**pent
)
453 mm_request_receive(pmonitor
->m_sendfd
, &m
);
454 type
= buffer_get_char(&m
);
456 debug3("%s: checking request %d", __func__
, type
);
458 while (ent
->f
!= NULL
) {
459 if (ent
->type
== type
)
464 if (ent
->f
!= NULL
) {
465 if (!(ent
->flags
& MON_PERMIT
))
466 fatal("%s: unpermitted request %d", __func__
,
468 ret
= (*ent
->f
)(pmonitor
->m_sendfd
, &m
);
471 /* The child may use this request only once, disable it */
472 if (ent
->flags
& MON_ONCE
) {
473 debug2("%s: %d used once, disabling now", __func__
,
475 ent
->flags
&= ~MON_PERMIT
;
484 fatal("%s: unsupported request: %d", __func__
, type
);
490 /* allowed key state */
492 monitor_allowed_key(u_char
*blob
, u_int bloblen
)
494 /* make sure key is allowed */
495 if (key_blob
== NULL
|| key_bloblen
!= bloblen
||
496 memcmp(key_blob
, blob
, key_bloblen
))
502 monitor_reset_key_state(void)
505 if (key_blob
!= NULL
)
507 if (hostbased_cuser
!= NULL
)
508 xfree(hostbased_cuser
);
509 if (hostbased_chost
!= NULL
)
510 xfree(hostbased_chost
);
513 key_blobtype
= MM_NOKEY
;
514 hostbased_cuser
= NULL
;
515 hostbased_chost
= NULL
;
519 mm_answer_moduli(int sock
, Buffer
*m
)
524 min
= buffer_get_int(m
);
525 want
= buffer_get_int(m
);
526 max
= buffer_get_int(m
);
528 debug3("%s: got parameters: %d %d %d",
529 __func__
, min
, want
, max
);
530 /* We need to check here, too, in case the child got corrupted */
531 if (max
< min
|| want
< min
|| max
< want
)
532 fatal("%s: bad parameters: %d %d %d",
533 __func__
, min
, want
, max
);
537 dh
= choose_dh(min
, want
, max
);
539 buffer_put_char(m
, 0);
542 /* Send first bignum */
543 buffer_put_char(m
, 1);
544 buffer_put_bignum2(m
, dh
->p
);
545 buffer_put_bignum2(m
, dh
->g
);
549 mm_request_send(sock
, MONITOR_ANS_MODULI
, m
);
554 mm_answer_sign(int sock
, Buffer
*m
)
559 u_int siglen
, datlen
;
562 debug3("%s", __func__
);
564 keyid
= buffer_get_int(m
);
565 p
= buffer_get_string(m
, &datlen
);
568 * Supported KEX types will only return SHA1 (20 byte) or
569 * SHA256 (32 byte) hashes
571 if (datlen
!= 20 && datlen
!= 32)
572 fatal("%s: data length incorrect: %u", __func__
, datlen
);
574 /* save session id, it will be passed on the first call */
575 if (session_id2_len
== 0) {
576 session_id2_len
= datlen
;
577 session_id2
= xmalloc(session_id2_len
);
578 memcpy(session_id2
, p
, session_id2_len
);
581 if ((key
= get_hostkey_by_index(keyid
)) == NULL
)
582 fatal("%s: no hostkey from index %d", __func__
, keyid
);
583 if (key_sign(key
, &signature
, &siglen
, p
, datlen
) < 0)
584 fatal("%s: key_sign failed", __func__
);
586 debug3("%s: signature %p(%u)", __func__
, signature
, siglen
);
589 buffer_put_string(m
, signature
, siglen
);
594 mm_request_send(sock
, MONITOR_ANS_SIGN
, m
);
596 /* Turn on permissions for getpwnam */
597 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
602 /* Retrieves the password entry and also checks if the user is permitted */
605 mm_answer_pwnamallow(int sock
, Buffer
*m
)
608 struct passwd
*pwent
;
611 debug3("%s", __func__
);
613 if (authctxt
->attempt
++ != 0)
614 fatal("%s: multiple attempts for getpwnam", __func__
);
616 username
= buffer_get_string(m
, NULL
);
618 pwent
= getpwnamallow(username
);
620 authctxt
->user
= xstrdup(username
);
621 setproctitle("%s [priv]", pwent
? username
: "unknown");
627 buffer_put_char(m
, 0);
628 authctxt
->pw
= fakepw();
633 authctxt
->pw
= pwent
;
636 buffer_put_char(m
, 1);
637 buffer_put_string(m
, pwent
, sizeof(struct passwd
));
638 buffer_put_cstring(m
, pwent
->pw_name
);
639 buffer_put_cstring(m
, "*");
640 buffer_put_cstring(m
, pwent
->pw_gecos
);
641 #ifdef HAVE_PW_CLASS_IN_PASSWD
642 buffer_put_cstring(m
, pwent
->pw_class
);
644 buffer_put_cstring(m
, pwent
->pw_dir
);
645 buffer_put_cstring(m
, pwent
->pw_shell
);
646 buffer_put_string(m
, &options
, sizeof(options
));
647 if (options
.banner
!= NULL
)
648 buffer_put_cstring(m
, options
.banner
);
651 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__
, allowed
);
652 mm_request_send(sock
, MONITOR_ANS_PWNAM
, m
);
654 /* For SSHv1 allow authentication now */
656 monitor_permit_authentications(1);
658 /* Allow service/style information on the auth context */
659 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTHSERV
, 1);
660 monitor_permit(mon_dispatch
, MONITOR_REQ_AUTH2_READ_BANNER
, 1);
665 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_START
, 1);
671 int mm_answer_auth2_read_banner(int sock
, Buffer
*m
)
676 banner
= auth2_read_banner();
677 buffer_put_cstring(m
, banner
!= NULL
? banner
: "");
678 mm_request_send(sock
, MONITOR_ANS_AUTH2_READ_BANNER
, m
);
687 mm_answer_authserv(int sock
, Buffer
*m
)
689 monitor_permit_authentications(1);
691 authctxt
->service
= buffer_get_string(m
, NULL
);
692 authctxt
->style
= buffer_get_string(m
, NULL
);
693 debug3("%s: service=%s, style=%s",
694 __func__
, authctxt
->service
, authctxt
->style
);
696 if (strlen(authctxt
->style
) == 0) {
697 xfree(authctxt
->style
);
698 authctxt
->style
= NULL
;
705 mm_answer_authpassword(int sock
, Buffer
*m
)
707 static int call_count
;
712 passwd
= buffer_get_string(m
, &plen
);
713 /* Only authenticate if the context is valid */
714 authenticated
= options
.password_authentication
&&
715 auth_password(authctxt
, passwd
);
716 memset(passwd
, 0, strlen(passwd
));
720 buffer_put_int(m
, authenticated
);
722 debug3("%s: sending result %d", __func__
, authenticated
);
723 mm_request_send(sock
, MONITOR_ANS_AUTHPASSWORD
, m
);
726 if (plen
== 0 && call_count
== 1)
727 auth_method
= "none";
729 auth_method
= "password";
731 /* Causes monitor loop to terminate if authenticated */
732 return (authenticated
);
737 mm_answer_bsdauthquery(int sock
, Buffer
*m
)
739 char *name
, *infotxt
;
745 success
= bsdauth_query(authctxt
, &name
, &infotxt
, &numprompts
,
746 &prompts
, &echo_on
) < 0 ? 0 : 1;
749 buffer_put_int(m
, success
);
751 buffer_put_cstring(m
, prompts
[0]);
753 debug3("%s: sending challenge success: %u", __func__
, success
);
754 mm_request_send(sock
, MONITOR_ANS_BSDAUTHQUERY
, m
);
767 mm_answer_bsdauthrespond(int sock
, Buffer
*m
)
772 if (authctxt
->as
== 0)
773 fatal("%s: no bsd auth session", __func__
);
775 response
= buffer_get_string(m
, NULL
);
776 authok
= options
.challenge_response_authentication
&&
777 auth_userresponse(authctxt
->as
, response
, 0);
779 debug3("%s: <%s> = <%d>", __func__
, response
, authok
);
783 buffer_put_int(m
, authok
);
785 debug3("%s: sending authenticated: %d", __func__
, authok
);
786 mm_request_send(sock
, MONITOR_ANS_BSDAUTHRESPOND
, m
);
788 auth_method
= "bsdauth";
790 return (authok
!= 0);
796 mm_answer_skeyquery(int sock
, Buffer
*m
)
799 char challenge
[1024];
802 success
= _compat_skeychallenge(&skey
, authctxt
->user
, challenge
,
803 sizeof(challenge
)) < 0 ? 0 : 1;
806 buffer_put_int(m
, success
);
808 buffer_put_cstring(m
, challenge
);
810 debug3("%s: sending challenge success: %u", __func__
, success
);
811 mm_request_send(sock
, MONITOR_ANS_SKEYQUERY
, m
);
817 mm_answer_skeyrespond(int sock
, Buffer
*m
)
822 response
= buffer_get_string(m
, NULL
);
824 authok
= (options
.challenge_response_authentication
&&
826 skey_haskey(authctxt
->pw
->pw_name
) == 0 &&
827 skey_passcheck(authctxt
->pw
->pw_name
, response
) != -1);
832 buffer_put_int(m
, authok
);
834 debug3("%s: sending authenticated: %d", __func__
, authok
);
835 mm_request_send(sock
, MONITOR_ANS_SKEYRESPOND
, m
);
837 auth_method
= "skey";
839 return (authok
!= 0);
845 mm_answer_pam_start(int sock
, Buffer
*m
)
847 if (!options
.use_pam
)
848 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
852 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_ACCOUNT
, 1);
858 mm_answer_pam_account(int sock
, Buffer
*m
)
862 if (!options
.use_pam
)
863 fatal("UsePAM not set, but ended up in %s anyway", __func__
);
865 ret
= do_pam_account();
867 buffer_put_int(m
, ret
);
868 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
870 mm_request_send(sock
, MONITOR_ANS_PAM_ACCOUNT
, m
);
875 static void *sshpam_ctxt
, *sshpam_authok
;
876 extern KbdintDevice sshpam_device
;
879 mm_answer_pam_init_ctx(int sock
, Buffer
*m
)
882 debug3("%s", __func__
);
883 authctxt
->user
= buffer_get_string(m
, NULL
);
884 sshpam_ctxt
= (sshpam_device
.init_ctx
)(authctxt
);
885 sshpam_authok
= NULL
;
887 if (sshpam_ctxt
!= NULL
) {
888 monitor_permit(mon_dispatch
, MONITOR_REQ_PAM_FREE_CTX
, 1);
889 buffer_put_int(m
, 1);
891 buffer_put_int(m
, 0);
893 mm_request_send(sock
, MONITOR_ANS_PAM_INIT_CTX
, m
);
898 mm_answer_pam_query(int sock
, Buffer
*m
)
900 char *name
, *info
, **prompts
;
901 u_int i
, num
, *echo_on
;
904 debug3("%s", __func__
);
905 sshpam_authok
= NULL
;
906 ret
= (sshpam_device
.query
)(sshpam_ctxt
, &name
, &info
, &num
, &prompts
, &echo_on
);
907 if (ret
== 0 && num
== 0)
908 sshpam_authok
= sshpam_ctxt
;
909 if (num
> 1 || name
== NULL
|| info
== NULL
)
912 buffer_put_int(m
, ret
);
913 buffer_put_cstring(m
, name
);
915 buffer_put_cstring(m
, info
);
917 buffer_put_int(m
, num
);
918 for (i
= 0; i
< num
; ++i
) {
919 buffer_put_cstring(m
, prompts
[i
]);
921 buffer_put_int(m
, echo_on
[i
]);
927 auth_method
= "keyboard-interactive/pam";
928 mm_request_send(sock
, MONITOR_ANS_PAM_QUERY
, m
);
933 mm_answer_pam_respond(int sock
, Buffer
*m
)
939 debug3("%s", __func__
);
940 sshpam_authok
= NULL
;
941 num
= buffer_get_int(m
);
943 resp
= xcalloc(num
, sizeof(char *));
944 for (i
= 0; i
< num
; ++i
)
945 resp
[i
] = buffer_get_string(m
, NULL
);
946 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, resp
);
947 for (i
= 0; i
< num
; ++i
)
951 ret
= (sshpam_device
.respond
)(sshpam_ctxt
, num
, NULL
);
954 buffer_put_int(m
, ret
);
955 mm_request_send(sock
, MONITOR_ANS_PAM_RESPOND
, m
);
956 auth_method
= "keyboard-interactive/pam";
958 sshpam_authok
= sshpam_ctxt
;
963 mm_answer_pam_free_ctx(int sock
, Buffer
*m
)
966 debug3("%s", __func__
);
967 (sshpam_device
.free_ctx
)(sshpam_ctxt
);
969 mm_request_send(sock
, MONITOR_ANS_PAM_FREE_CTX
, m
);
970 auth_method
= "keyboard-interactive/pam";
971 return (sshpam_authok
== sshpam_ctxt
);
976 mm_append_debug(Buffer
*m
)
978 if (auth_debug_init
&& buffer_len(&auth_debug
)) {
979 debug3("%s: Appending debug messages for child", __func__
);
980 buffer_append(m
, buffer_ptr(&auth_debug
),
981 buffer_len(&auth_debug
));
982 buffer_clear(&auth_debug
);
987 mm_answer_keyallowed(int sock
, Buffer
*m
)
993 enum mm_keytype type
= 0;
996 debug3("%s entering", __func__
);
998 type
= buffer_get_int(m
);
999 cuser
= buffer_get_string(m
, NULL
);
1000 chost
= buffer_get_string(m
, NULL
);
1001 blob
= buffer_get_string(m
, &bloblen
);
1003 key
= key_from_blob(blob
, bloblen
);
1005 if ((compat20
&& type
== MM_RSAHOSTKEY
) ||
1006 (!compat20
&& type
!= MM_RSAHOSTKEY
))
1007 fatal("%s: key type and protocol mismatch", __func__
);
1009 debug3("%s: key_from_blob: %p", __func__
, key
);
1011 if (key
!= NULL
&& authctxt
->valid
) {
1014 allowed
= options
.pubkey_authentication
&&
1015 user_key_allowed(authctxt
->pw
, key
);
1016 auth_method
= "publickey";
1019 allowed
= options
.hostbased_authentication
&&
1020 hostbased_key_allowed(authctxt
->pw
,
1022 auth_method
= "hostbased";
1025 key
->type
= KEY_RSA1
; /* XXX */
1026 allowed
= options
.rhosts_rsa_authentication
&&
1027 auth_rhosts_rsa_key_allowed(authctxt
->pw
,
1029 auth_method
= "rsa";
1032 fatal("%s: unknown key type %d", __func__
, type
);
1039 /* clear temporarily storage (used by verify) */
1040 monitor_reset_key_state();
1043 /* Save temporarily for comparison in verify */
1045 key_bloblen
= bloblen
;
1046 key_blobtype
= type
;
1047 hostbased_cuser
= cuser
;
1048 hostbased_chost
= chost
;
1050 /* Log failed attempt */
1051 auth_log(authctxt
, 0, auth_method
, compat20
? " ssh2" : "");
1057 debug3("%s: key %p is %s",
1058 __func__
, key
, allowed
? "allowed" : "disallowed");
1061 buffer_put_int(m
, allowed
);
1062 buffer_put_int(m
, forced_command
!= NULL
);
1066 mm_request_send(sock
, MONITOR_ANS_KEYALLOWED
, m
);
1068 if (type
== MM_RSAHOSTKEY
)
1069 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1075 monitor_valid_userblob(u_char
*data
, u_int datalen
)
1083 buffer_append(&b
, data
, datalen
);
1085 if (datafellows
& SSH_OLD_SESSIONID
) {
1087 len
= buffer_len(&b
);
1088 if ((session_id2
== NULL
) ||
1089 (len
< session_id2_len
) ||
1090 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1092 buffer_consume(&b
, session_id2_len
);
1094 p
= buffer_get_string(&b
, &len
);
1095 if ((session_id2
== NULL
) ||
1096 (len
!= session_id2_len
) ||
1097 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1101 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1103 p
= buffer_get_string(&b
, NULL
);
1104 if (strcmp(authctxt
->user
, p
) != 0) {
1105 logit("wrong user name passed to monitor: expected %s != %.100s",
1110 buffer_skip_string(&b
);
1111 if (datafellows
& SSH_BUG_PKAUTH
) {
1112 if (!buffer_get_char(&b
))
1115 p
= buffer_get_string(&b
, NULL
);
1116 if (strcmp("publickey", p
) != 0)
1119 if (!buffer_get_char(&b
))
1121 buffer_skip_string(&b
);
1123 buffer_skip_string(&b
);
1124 if (buffer_len(&b
) != 0)
1131 monitor_valid_hostbasedblob(u_char
*data
, u_int datalen
, char *cuser
,
1140 buffer_append(&b
, data
, datalen
);
1142 p
= buffer_get_string(&b
, &len
);
1143 if ((session_id2
== NULL
) ||
1144 (len
!= session_id2_len
) ||
1145 (memcmp(p
, session_id2
, session_id2_len
) != 0))
1149 if (buffer_get_char(&b
) != SSH2_MSG_USERAUTH_REQUEST
)
1151 p
= buffer_get_string(&b
, NULL
);
1152 if (strcmp(authctxt
->user
, p
) != 0) {
1153 logit("wrong user name passed to monitor: expected %s != %.100s",
1158 buffer_skip_string(&b
); /* service */
1159 p
= buffer_get_string(&b
, NULL
);
1160 if (strcmp(p
, "hostbased") != 0)
1163 buffer_skip_string(&b
); /* pkalg */
1164 buffer_skip_string(&b
); /* pkblob */
1166 /* verify client host, strip trailing dot if necessary */
1167 p
= buffer_get_string(&b
, NULL
);
1168 if (((len
= strlen(p
)) > 0) && p
[len
- 1] == '.')
1170 if (strcmp(p
, chost
) != 0)
1174 /* verify client user */
1175 p
= buffer_get_string(&b
, NULL
);
1176 if (strcmp(p
, cuser
) != 0)
1180 if (buffer_len(&b
) != 0)
1187 mm_answer_keyverify(int sock
, Buffer
*m
)
1190 u_char
*signature
, *data
, *blob
;
1191 u_int signaturelen
, datalen
, bloblen
;
1195 blob
= buffer_get_string(m
, &bloblen
);
1196 signature
= buffer_get_string(m
, &signaturelen
);
1197 data
= buffer_get_string(m
, &datalen
);
1199 if (hostbased_cuser
== NULL
|| hostbased_chost
== NULL
||
1200 !monitor_allowed_key(blob
, bloblen
))
1201 fatal("%s: bad key, not previously allowed", __func__
);
1203 key
= key_from_blob(blob
, bloblen
);
1205 fatal("%s: bad public key blob", __func__
);
1207 switch (key_blobtype
) {
1209 valid_data
= monitor_valid_userblob(data
, datalen
);
1212 valid_data
= monitor_valid_hostbasedblob(data
, datalen
,
1213 hostbased_cuser
, hostbased_chost
);
1220 fatal("%s: bad signature data blob", __func__
);
1222 verified
= key_verify(key
, signature
, signaturelen
, data
, datalen
);
1223 debug3("%s: key %p signature %s",
1224 __func__
, key
, (verified
== 1) ? "verified" : "unverified");
1231 auth_method
= key_blobtype
== MM_USERKEY
? "publickey" : "hostbased";
1233 monitor_reset_key_state();
1236 buffer_put_int(m
, verified
);
1237 mm_request_send(sock
, MONITOR_ANS_KEYVERIFY
, m
);
1239 return (verified
== 1);
1243 mm_record_login(Session
*s
, struct passwd
*pw
)
1246 struct sockaddr_storage from
;
1249 * Get IP address of client. If the connection is not a socket, let
1250 * the address be 0.0.0.0.
1252 memset(&from
, 0, sizeof(from
));
1253 fromlen
= sizeof(from
);
1254 if (packet_connection_is_on_socket()) {
1255 if (getpeername(packet_get_connection_in(),
1256 (struct sockaddr
*)&from
, &fromlen
) < 0) {
1257 debug("getpeername: %.100s", strerror(errno
));
1261 /* Record that there was a login on that tty from the remote host. */
1262 record_login(s
->pid
, s
->tty
, pw
->pw_name
, pw
->pw_uid
,
1263 get_remote_name_or_ip(utmp_len
, options
.use_dns
),
1264 (struct sockaddr
*)&from
, fromlen
);
1268 mm_session_close(Session
*s
)
1270 debug3("%s: session %d pid %ld", __func__
, s
->self
, (long)s
->pid
);
1271 if (s
->ttyfd
!= -1) {
1272 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ptyfd
);
1273 session_pty_cleanup2(s
);
1279 mm_answer_pty(int sock
, Buffer
*m
)
1281 extern struct monitor
*pmonitor
;
1285 debug3("%s entering", __func__
);
1291 s
->authctxt
= authctxt
;
1292 s
->pw
= authctxt
->pw
;
1293 s
->pid
= pmonitor
->m_pid
;
1294 res
= pty_allocate(&s
->ptyfd
, &s
->ttyfd
, s
->tty
, sizeof(s
->tty
));
1297 pty_setowner(authctxt
->pw
, s
->tty
);
1299 buffer_put_int(m
, 1);
1300 buffer_put_cstring(m
, s
->tty
);
1302 /* We need to trick ttyslot */
1303 if (dup2(s
->ttyfd
, 0) == -1)
1304 fatal("%s: dup2", __func__
);
1306 mm_record_login(s
, authctxt
->pw
);
1308 /* Now we can close the file descriptor again */
1311 /* send messages generated by record_login */
1312 buffer_put_string(m
, buffer_ptr(&loginmsg
), buffer_len(&loginmsg
));
1313 buffer_clear(&loginmsg
);
1315 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1317 mm_send_fd(sock
, s
->ptyfd
);
1318 mm_send_fd(sock
, s
->ttyfd
);
1320 /* make sure nothing uses fd 0 */
1321 if ((fd0
= open(_PATH_DEVNULL
, O_RDONLY
)) < 0)
1322 fatal("%s: open(/dev/null): %s", __func__
, strerror(errno
));
1324 error("%s: fd0 %d != 0", __func__
, fd0
);
1326 /* slave is not needed */
1328 s
->ttyfd
= s
->ptyfd
;
1329 /* no need to dup() because nobody closes ptyfd */
1330 s
->ptymaster
= s
->ptyfd
;
1332 debug3("%s: tty %s ptyfd %d", __func__
, s
->tty
, s
->ttyfd
);
1338 mm_session_close(s
);
1339 buffer_put_int(m
, 0);
1340 mm_request_send(sock
, MONITOR_ANS_PTY
, m
);
1345 mm_answer_pty_cleanup(int sock
, Buffer
*m
)
1350 debug3("%s entering", __func__
);
1352 tty
= buffer_get_string(m
, NULL
);
1353 if ((s
= session_by_tty(tty
)) != NULL
)
1354 mm_session_close(s
);
1361 mm_answer_sesskey(int sock
, Buffer
*m
)
1366 /* Turn off permissions */
1367 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSKEY
, 0);
1369 if ((p
= BN_new()) == NULL
)
1370 fatal("%s: BN_new", __func__
);
1372 buffer_get_bignum2(m
, p
);
1374 rsafail
= ssh1_session_key(p
);
1377 buffer_put_int(m
, rsafail
);
1378 buffer_put_bignum2(m
, p
);
1382 mm_request_send(sock
, MONITOR_ANS_SESSKEY
, m
);
1384 /* Turn on permissions for sessid passing */
1385 monitor_permit(mon_dispatch
, MONITOR_REQ_SESSID
, 1);
1391 mm_answer_sessid(int sock
, Buffer
*m
)
1395 debug3("%s entering", __func__
);
1397 if (buffer_len(m
) != 16)
1398 fatal("%s: bad ssh1 session id", __func__
);
1399 for (i
= 0; i
< 16; i
++)
1400 session_id
[i
] = buffer_get_char(m
);
1402 /* Turn on permissions for getpwnam */
1403 monitor_permit(mon_dispatch
, MONITOR_REQ_PWNAM
, 1);
1409 mm_answer_rsa_keyallowed(int sock
, Buffer
*m
)
1413 u_char
*blob
= NULL
;
1417 debug3("%s entering", __func__
);
1419 auth_method
= "rsa";
1420 if (options
.rsa_authentication
&& authctxt
->valid
) {
1421 if ((client_n
= BN_new()) == NULL
)
1422 fatal("%s: BN_new", __func__
);
1423 buffer_get_bignum2(m
, client_n
);
1424 allowed
= auth_rsa_key_allowed(authctxt
->pw
, client_n
, &key
);
1425 BN_clear_free(client_n
);
1428 buffer_put_int(m
, allowed
);
1429 buffer_put_int(m
, forced_command
!= NULL
);
1431 /* clear temporarily storage (used by generate challenge) */
1432 monitor_reset_key_state();
1434 if (allowed
&& key
!= NULL
) {
1435 key
->type
= KEY_RSA
; /* cheat for key_to_blob */
1436 if (key_to_blob(key
, &blob
, &blen
) == 0)
1437 fatal("%s: key_to_blob failed", __func__
);
1438 buffer_put_string(m
, blob
, blen
);
1440 /* Save temporarily for comparison in verify */
1443 key_blobtype
= MM_RSAUSERKEY
;
1450 mm_request_send(sock
, MONITOR_ANS_RSAKEYALLOWED
, m
);
1452 monitor_permit(mon_dispatch
, MONITOR_REQ_RSACHALLENGE
, allowed
);
1453 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 0);
1458 mm_answer_rsa_challenge(int sock
, Buffer
*m
)
1464 debug3("%s entering", __func__
);
1466 if (!authctxt
->valid
)
1467 fatal("%s: authctxt not valid", __func__
);
1468 blob
= buffer_get_string(m
, &blen
);
1469 if (!monitor_allowed_key(blob
, blen
))
1470 fatal("%s: bad key, not previously allowed", __func__
);
1471 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1472 fatal("%s: key type mismatch", __func__
);
1473 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1474 fatal("%s: received bad key", __func__
);
1477 BN_clear_free(ssh1_challenge
);
1478 ssh1_challenge
= auth_rsa_generate_challenge(key
);
1481 buffer_put_bignum2(m
, ssh1_challenge
);
1483 debug3("%s sending reply", __func__
);
1484 mm_request_send(sock
, MONITOR_ANS_RSACHALLENGE
, m
);
1486 monitor_permit(mon_dispatch
, MONITOR_REQ_RSARESPONSE
, 1);
1494 mm_answer_rsa_response(int sock
, Buffer
*m
)
1497 u_char
*blob
, *response
;
1501 debug3("%s entering", __func__
);
1503 if (!authctxt
->valid
)
1504 fatal("%s: authctxt not valid", __func__
);
1505 if (ssh1_challenge
== NULL
)
1506 fatal("%s: no ssh1_challenge", __func__
);
1508 blob
= buffer_get_string(m
, &blen
);
1509 if (!monitor_allowed_key(blob
, blen
))
1510 fatal("%s: bad key, not previously allowed", __func__
);
1511 if (key_blobtype
!= MM_RSAUSERKEY
&& key_blobtype
!= MM_RSAHOSTKEY
)
1512 fatal("%s: key type mismatch: %d", __func__
, key_blobtype
);
1513 if ((key
= key_from_blob(blob
, blen
)) == NULL
)
1514 fatal("%s: received bad key", __func__
);
1515 response
= buffer_get_string(m
, &len
);
1517 fatal("%s: received bad response to challenge", __func__
);
1518 success
= auth_rsa_verify_response(key
, ssh1_challenge
, response
);
1524 auth_method
= key_blobtype
== MM_RSAUSERKEY
? "rsa" : "rhosts-rsa";
1527 BN_clear_free(ssh1_challenge
);
1528 ssh1_challenge
= NULL
;
1529 monitor_reset_key_state();
1532 buffer_put_int(m
, success
);
1533 mm_request_send(sock
, MONITOR_ANS_RSARESPONSE
, m
);
1539 mm_answer_term(int sock
, Buffer
*req
)
1541 extern struct monitor
*pmonitor
;
1544 debug3("%s: tearing down sessions", __func__
);
1546 /* The child is terminating */
1547 session_destroy_all(&mm_session_close
);
1549 while (waitpid(pmonitor
->m_pid
, &status
, 0) == -1)
1553 res
= WIFEXITED(status
) ? WEXITSTATUS(status
) : 1;
1555 /* Terminate process */
1559 #ifdef SSH_AUDIT_EVENTS
1560 /* Report that an audit event occurred */
1562 mm_answer_audit_event(int socket
, Buffer
*m
)
1564 ssh_audit_event_t event
;
1566 debug3("%s entering", __func__
);
1568 event
= buffer_get_int(m
);
1570 case SSH_AUTH_FAIL_PUBKEY
:
1571 case SSH_AUTH_FAIL_HOSTBASED
:
1572 case SSH_AUTH_FAIL_GSSAPI
:
1573 case SSH_LOGIN_EXCEED_MAXTRIES
:
1574 case SSH_LOGIN_ROOT_DENIED
:
1575 case SSH_CONNECTION_CLOSE
:
1576 case SSH_INVALID_USER
:
1580 fatal("Audit event type %d not permitted", event
);
1587 mm_answer_audit_command(int socket
, Buffer
*m
)
1592 debug3("%s entering", __func__
);
1593 cmd
= buffer_get_string(m
, &len
);
1594 /* sanity check command, if so how? */
1595 audit_run_command(cmd
);
1599 #endif /* SSH_AUDIT_EVENTS */
1602 monitor_apply_keystate(struct monitor
*pmonitor
)
1605 set_newkeys(MODE_IN
);
1606 set_newkeys(MODE_OUT
);
1608 packet_set_protocol_flags(child_state
.ssh1protoflags
);
1609 packet_set_encryption_key(child_state
.ssh1key
,
1610 child_state
.ssh1keylen
, child_state
.ssh1cipher
);
1611 xfree(child_state
.ssh1key
);
1614 /* for rc4 and other stateful ciphers */
1615 packet_set_keycontext(MODE_OUT
, child_state
.keyout
);
1616 xfree(child_state
.keyout
);
1617 packet_set_keycontext(MODE_IN
, child_state
.keyin
);
1618 xfree(child_state
.keyin
);
1621 packet_set_iv(MODE_OUT
, child_state
.ivout
);
1622 xfree(child_state
.ivout
);
1623 packet_set_iv(MODE_IN
, child_state
.ivin
);
1624 xfree(child_state
.ivin
);
1627 memcpy(&incoming_stream
, &child_state
.incoming
,
1628 sizeof(incoming_stream
));
1629 memcpy(&outgoing_stream
, &child_state
.outgoing
,
1630 sizeof(outgoing_stream
));
1632 /* Update with new address */
1633 if (options
.compression
)
1634 mm_init_compression(pmonitor
->m_zlib
);
1636 /* Network I/O buffers */
1637 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1638 buffer_clear(&input
);
1639 buffer_append(&input
, child_state
.input
, child_state
.ilen
);
1640 memset(child_state
.input
, 0, child_state
.ilen
);
1641 xfree(child_state
.input
);
1643 buffer_clear(&output
);
1644 buffer_append(&output
, child_state
.output
, child_state
.olen
);
1645 memset(child_state
.output
, 0, child_state
.olen
);
1646 xfree(child_state
.output
);
1650 mm_get_kex(Buffer
*m
)
1656 kex
= xcalloc(1, sizeof(*kex
));
1657 kex
->session_id
= buffer_get_string(m
, &kex
->session_id_len
);
1658 if ((session_id2
== NULL
) ||
1659 (kex
->session_id_len
!= session_id2_len
) ||
1660 (memcmp(kex
->session_id
, session_id2
, session_id2_len
) != 0))
1661 fatal("mm_get_get: internal error: bad session id");
1662 kex
->we_need
= buffer_get_int(m
);
1663 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_server
;
1664 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_server
;
1665 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_server
;
1666 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_server
;
1668 kex
->hostkey_type
= buffer_get_int(m
);
1669 kex
->kex_type
= buffer_get_int(m
);
1670 blob
= buffer_get_string(m
, &bloblen
);
1671 buffer_init(&kex
->my
);
1672 buffer_append(&kex
->my
, blob
, bloblen
);
1674 blob
= buffer_get_string(m
, &bloblen
);
1675 buffer_init(&kex
->peer
);
1676 buffer_append(&kex
->peer
, blob
, bloblen
);
1679 kex
->flags
= buffer_get_int(m
);
1680 kex
->client_version_string
= buffer_get_string(m
, NULL
);
1681 kex
->server_version_string
= buffer_get_string(m
, NULL
);
1682 kex
->load_host_key
=&get_hostkey_by_type
;
1683 kex
->host_key_index
=&get_hostkey_index
;
1688 /* This function requries careful sanity checking */
1691 mm_get_keystate(struct monitor
*pmonitor
)
1695 u_int bloblen
, plen
;
1696 u_int32_t seqnr
, packets
;
1699 debug3("%s: Waiting for new keys", __func__
);
1702 mm_request_receive_expect(pmonitor
->m_sendfd
, MONITOR_REQ_KEYEXPORT
, &m
);
1704 child_state
.ssh1protoflags
= buffer_get_int(&m
);
1705 child_state
.ssh1cipher
= buffer_get_int(&m
);
1706 child_state
.ssh1key
= buffer_get_string(&m
,
1707 &child_state
.ssh1keylen
);
1708 child_state
.ivout
= buffer_get_string(&m
,
1709 &child_state
.ivoutlen
);
1710 child_state
.ivin
= buffer_get_string(&m
, &child_state
.ivinlen
);
1713 /* Get the Kex for rekeying */
1714 *pmonitor
->m_pkex
= mm_get_kex(&m
);
1717 blob
= buffer_get_string(&m
, &bloblen
);
1718 current_keys
[MODE_OUT
] = mm_newkeys_from_blob(blob
, bloblen
);
1721 debug3("%s: Waiting for second key", __func__
);
1722 blob
= buffer_get_string(&m
, &bloblen
);
1723 current_keys
[MODE_IN
] = mm_newkeys_from_blob(blob
, bloblen
);
1726 /* Now get sequence numbers for the packets */
1727 seqnr
= buffer_get_int(&m
);
1728 blocks
= buffer_get_int64(&m
);
1729 packets
= buffer_get_int(&m
);
1730 packet_set_state(MODE_OUT
, seqnr
, blocks
, packets
);
1731 seqnr
= buffer_get_int(&m
);
1732 blocks
= buffer_get_int64(&m
);
1733 packets
= buffer_get_int(&m
);
1734 packet_set_state(MODE_IN
, seqnr
, blocks
, packets
);
1737 /* Get the key context */
1738 child_state
.keyout
= buffer_get_string(&m
, &child_state
.keyoutlen
);
1739 child_state
.keyin
= buffer_get_string(&m
, &child_state
.keyinlen
);
1741 debug3("%s: Getting compression state", __func__
);
1742 /* Get compression state */
1743 p
= buffer_get_string(&m
, &plen
);
1744 if (plen
!= sizeof(child_state
.outgoing
))
1745 fatal("%s: bad request size", __func__
);
1746 memcpy(&child_state
.outgoing
, p
, sizeof(child_state
.outgoing
));
1749 p
= buffer_get_string(&m
, &plen
);
1750 if (plen
!= sizeof(child_state
.incoming
))
1751 fatal("%s: bad request size", __func__
);
1752 memcpy(&child_state
.incoming
, p
, sizeof(child_state
.incoming
));
1755 /* Network I/O buffers */
1756 debug3("%s: Getting Network I/O buffers", __func__
);
1757 child_state
.input
= buffer_get_string(&m
, &child_state
.ilen
);
1758 child_state
.output
= buffer_get_string(&m
, &child_state
.olen
);
1764 /* Allocation functions for zlib */
1766 mm_zalloc(struct mm_master
*mm
, u_int ncount
, u_int size
)
1768 size_t len
= (size_t) size
* ncount
;
1771 if (len
== 0 || ncount
> SIZE_T_MAX
/ size
)
1772 fatal("%s: mm_zalloc(%u, %u)", __func__
, ncount
, size
);
1774 address
= mm_malloc(mm
, len
);
1780 mm_zfree(struct mm_master
*mm
, void *address
)
1782 mm_free(mm
, address
);
1786 mm_init_compression(struct mm_master
*mm
)
1788 outgoing_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1789 outgoing_stream
.zfree
= (free_func
)mm_zfree
;
1790 outgoing_stream
.opaque
= mm
;
1792 incoming_stream
.zalloc
= (alloc_func
)mm_zalloc
;
1793 incoming_stream
.zfree
= (free_func
)mm_zfree
;
1794 incoming_stream
.opaque
= mm
;
1799 #define FD_CLOSEONEXEC(x) do { \
1800 if (fcntl(x, F_SETFD, 1) == -1) \
1801 fatal("fcntl(%d, F_SETFD)", x); \
1805 monitor_socketpair(int *pair
)
1807 #ifdef HAVE_SOCKETPAIR
1808 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1)
1809 fatal("%s: socketpair", __func__
);
1811 fatal("%s: UsePrivilegeSeparation=yes not supported",
1814 FD_CLOSEONEXEC(pair
[0]);
1815 FD_CLOSEONEXEC(pair
[1]);
1818 #define MM_MEMSIZE 65536
1823 struct monitor
*mon
;
1826 mon
= xcalloc(1, sizeof(*mon
));
1828 monitor_socketpair(pair
);
1830 mon
->m_recvfd
= pair
[0];
1831 mon
->m_sendfd
= pair
[1];
1833 /* Used to share zlib space across processes */
1834 if (options
.compression
) {
1835 mon
->m_zback
= mm_create(NULL
, MM_MEMSIZE
);
1836 mon
->m_zlib
= mm_create(mon
->m_zback
, 20 * MM_MEMSIZE
);
1838 /* Compression needs to share state across borders */
1839 mm_init_compression(mon
->m_zlib
);
1846 monitor_reinit(struct monitor
*mon
)
1850 monitor_socketpair(pair
);
1852 mon
->m_recvfd
= pair
[0];
1853 mon
->m_sendfd
= pair
[1];
1858 mm_answer_gss_setup_ctx(int sock
, Buffer
*m
)
1864 goid
.elements
= buffer_get_string(m
, &len
);
1867 major
= ssh_gssapi_server_ctx(&gsscontext
, &goid
);
1869 xfree(goid
.elements
);
1872 buffer_put_int(m
, major
);
1874 mm_request_send(sock
, MONITOR_ANS_GSSSETUP
, m
);
1876 /* Now we have a context, enable the step */
1877 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 1);
1883 mm_answer_gss_accept_ctx(int sock
, Buffer
*m
)
1886 gss_buffer_desc out
= GSS_C_EMPTY_BUFFER
;
1887 OM_uint32 major
, minor
;
1888 OM_uint32 flags
= 0; /* GSI needs this */
1891 in
.value
= buffer_get_string(m
, &len
);
1893 major
= ssh_gssapi_accept_ctx(gsscontext
, &in
, &out
, &flags
);
1897 buffer_put_int(m
, major
);
1898 buffer_put_string(m
, out
.value
, out
.length
);
1899 buffer_put_int(m
, flags
);
1900 mm_request_send(sock
, MONITOR_ANS_GSSSTEP
, m
);
1902 gss_release_buffer(&minor
, &out
);
1904 if (major
== GSS_S_COMPLETE
) {
1905 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSSTEP
, 0);
1906 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1907 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSCHECKMIC
, 1);
1913 mm_answer_gss_checkmic(int sock
, Buffer
*m
)
1915 gss_buffer_desc gssbuf
, mic
;
1919 gssbuf
.value
= buffer_get_string(m
, &len
);
1920 gssbuf
.length
= len
;
1921 mic
.value
= buffer_get_string(m
, &len
);
1924 ret
= ssh_gssapi_checkmic(gsscontext
, &gssbuf
, &mic
);
1926 xfree(gssbuf
.value
);
1930 buffer_put_int(m
, ret
);
1932 mm_request_send(sock
, MONITOR_ANS_GSSCHECKMIC
, m
);
1934 if (!GSS_ERROR(ret
))
1935 monitor_permit(mon_dispatch
, MONITOR_REQ_GSSUSEROK
, 1);
1941 mm_answer_gss_userok(int sock
, Buffer
*m
)
1945 authenticated
= authctxt
->valid
&& ssh_gssapi_userok(authctxt
->user
);
1948 buffer_put_int(m
, authenticated
);
1950 debug3("%s: sending result %d", __func__
, authenticated
);
1951 mm_request_send(sock
, MONITOR_ANS_GSSUSEROK
, m
);
1953 auth_method
= "gssapi-with-mic";
1955 /* Monitor loop will terminate if authenticated */
1956 return (authenticated
);