1 /* $OpenBSD: monitor.c,v 1.99 2008/07/10 18:08:11 markus 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>
54 #include "openbsd-compat/sys-queue.h"
64 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
67 #define TARGET_OS_MAC 1
72 #include "auth-options.h"
81 #include "monitor_mm.h"
85 #include "monitor_wrap.h"
86 #include "monitor_fdpass.h"
92 static Gssctxt *gsscontext = NULL;
96 extern ServerOptions options;
97 extern u_int utmp_len;
98 extern Newkeys *current_keys[];
99 extern z_stream incoming_stream;
100 extern z_stream outgoing_stream;
101 extern u_char session_id[];
102 extern Buffer input, output;
103 extern Buffer auth_debug;
104 extern int auth_debug_init;
105 extern Buffer loginmsg;
107 /* State exported from the child */
130 /* Functions on the monitor that answer unprivileged requests */
132 int mm_answer_moduli(int, Buffer *);
133 int mm_answer_sign(int, Buffer *);
134 int mm_answer_pwnamallow(int, Buffer *);
135 int mm_answer_auth2_read_banner(int, Buffer *);
136 int mm_answer_authserv(int, Buffer *);
137 int mm_answer_authpassword(int, Buffer *);
138 int mm_answer_bsdauthquery(int, Buffer *);
139 int mm_answer_bsdauthrespond(int, Buffer *);
140 int mm_answer_skeyquery(int, Buffer *);
141 int mm_answer_skeyrespond(int, Buffer *);
142 int mm_answer_keyallowed(int, Buffer *);
143 int mm_answer_keyverify(int, Buffer *);
144 int mm_answer_pty(int, Buffer *);
145 int mm_answer_pty_cleanup(int, Buffer *);
146 int mm_answer_term(int, Buffer *);
147 int mm_answer_rsa_keyallowed(int, Buffer *);
148 int mm_answer_rsa_challenge(int, Buffer *);
149 int mm_answer_rsa_response(int, Buffer *);
150 int mm_answer_sesskey(int, Buffer *);
151 int mm_answer_sessid(int, Buffer *);
154 int mm_answer_pam_start(int, Buffer *);
155 int mm_answer_pam_account(int, Buffer *);
156 int mm_answer_pam_init_ctx(int, Buffer *);
157 int mm_answer_pam_query(int, Buffer *);
158 int mm_answer_pam_respond(int, Buffer *);
159 int mm_answer_pam_free_ctx(int, Buffer *);
163 int mm_answer_gss_setup_ctx(int, Buffer *);
164 int mm_answer_gss_accept_ctx(int, Buffer *);
165 int mm_answer_gss_userok(int, Buffer *);
166 int mm_answer_gss_checkmic(int, Buffer *);
167 int mm_answer_gss_sign(int, Buffer *);
168 int mm_answer_gss_error(int, Buffer *);
169 int mm_answer_gss_indicate_mechs(int, Buffer *);
170 int mm_answer_gss_localname(int, Buffer *);
173 #ifdef SSH_AUDIT_EVENTS
174 int mm_answer_audit_event(int, Buffer *);
175 int mm_answer_audit_command(int, Buffer *);
178 static Authctxt *authctxt;
179 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
181 /* local state for key verify */
182 static u_char *key_blob = NULL;
183 static u_int key_bloblen = 0;
184 static int key_blobtype = MM_NOKEY;
185 static char *hostbased_cuser = NULL;
186 static char *hostbased_chost = NULL;
187 static char *auth_method = "unknown";
188 static u_int session_id2_len = 0;
189 static u_char *session_id2 = NULL;
190 static pid_t monitor_child_pid;
193 enum monitor_reqtype type;
195 int (*f)(int, Buffer *);
198 #define MON_ISAUTH 0x0004 /* Required for Authentication */
199 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
200 #define MON_ONCE 0x0010 /* Disable after calling */
201 #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
203 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
205 #define MON_PERMIT 0x1000 /* Request is permitted */
207 struct mon_table mon_dispatch_proto20[] = {
208 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
209 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
210 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
211 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
212 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
213 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
215 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
216 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
217 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
218 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
219 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
220 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
222 #ifdef SSH_AUDIT_EVENTS
223 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
226 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
227 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
230 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
231 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
233 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
234 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
236 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
237 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
238 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
239 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
240 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
241 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
242 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
243 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
248 struct mon_table mon_dispatch_postauth20[] = {
250 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
251 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
252 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
253 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
254 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
256 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
257 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
258 {MONITOR_REQ_PTY, 0, mm_answer_pty},
259 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
260 {MONITOR_REQ_TERM, 0, mm_answer_term},
261 #ifdef SSH_AUDIT_EVENTS
262 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
263 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
268 struct mon_table mon_dispatch_proto15[] = {
269 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
270 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
271 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
272 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
273 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
274 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
275 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
276 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
278 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
279 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
282 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
283 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
286 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
287 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
288 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
289 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
290 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
293 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
294 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
295 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
296 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
297 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
298 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
300 #ifdef SSH_AUDIT_EVENTS
301 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
306 struct mon_table mon_dispatch_postauth15[] = {
307 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
308 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
309 {MONITOR_REQ_TERM, 0, mm_answer_term},
310 #ifdef SSH_AUDIT_EVENTS
311 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
312 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
317 struct mon_table *mon_dispatch;
319 /* Specifies if a certain message is allowed at the moment */
322 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
324 while (ent->f != NULL) {
325 if (ent->type == type) {
326 ent->flags &= ~MON_PERMIT;
327 ent->flags |= permit ? MON_PERMIT : 0;
335 monitor_permit_authentications(int permit)
337 struct mon_table *ent = mon_dispatch;
339 while (ent->f != NULL) {
340 if (ent->flags & MON_AUTH) {
341 ent->flags &= ~MON_PERMIT;
342 ent->flags |= permit ? MON_PERMIT : 0;
349 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
351 struct mon_table *ent;
352 int authenticated = 0;
354 debug3("preauth child monitor started");
356 authctxt = _authctxt;
357 memset(authctxt, 0, sizeof(*authctxt));
359 authctxt->loginmsg = &loginmsg;
362 mon_dispatch = mon_dispatch_proto20;
364 /* Permit requests for moduli and signatures */
365 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
366 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
368 /* and for the GSSAPI key exchange */
369 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
370 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
371 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
374 mon_dispatch = mon_dispatch_proto15;
376 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
379 /* The first few requests do not require asynchronous access */
380 while (!authenticated) {
381 auth_method = "unknown";
382 authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1);
384 if (!(ent->flags & MON_AUTHDECIDE))
385 fatal("%s: unexpected authentication from %d",
386 __func__, ent->type);
387 if (authctxt->pw->pw_uid == 0 &&
388 !auth_root_allowed(auth_method))
391 /* PAM needs to perform account checks after auth */
392 if (options.use_pam && authenticated) {
396 mm_request_receive_expect(pmonitor->m_sendfd,
397 MONITOR_REQ_PAM_ACCOUNT, &m);
398 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
404 if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
405 auth_log(authctxt, authenticated, auth_method,
406 compat20 ? " ssh2" : "");
408 authctxt->failures++;
412 if (!authctxt->valid)
413 fatal("%s: authenticated invalid user", __func__);
414 if (strcmp(auth_method, "unknown") == 0)
415 fatal("%s: authentication method name unknown", __func__);
417 debug("%s: %s has been authenticated by privileged process",
418 __func__, authctxt->user);
420 mm_get_keystate(pmonitor);
424 monitor_set_child_handler(pid_t pid)
426 monitor_child_pid = pid;
430 monitor_child_handler(int sig)
432 kill(monitor_child_pid, sig);
436 monitor_child_postauth(struct monitor *pmonitor)
438 monitor_set_child_handler(pmonitor->m_pid);
439 signal(SIGHUP, &monitor_child_handler);
440 signal(SIGTERM, &monitor_child_handler);
441 signal(SIGINT, &monitor_child_handler);
444 mon_dispatch = mon_dispatch_postauth20;
446 /* Permit requests for moduli and signatures */
447 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
448 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
449 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
452 /* and for the GSSAPI key exchange */
453 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
454 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
455 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
459 mon_dispatch = mon_dispatch_postauth15;
460 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
463 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
466 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
467 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
471 monitor_read(pmonitor, mon_dispatch, NULL);
475 monitor_sync(struct monitor *pmonitor)
477 if (options.compression) {
478 /* The member allocation is not visible, so sync it */
479 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
484 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
485 struct mon_table **pent)
493 mm_request_receive(pmonitor->m_sendfd, &m);
494 type = buffer_get_char(&m);
496 debug3("%s: checking request %d", __func__, type);
498 while (ent->f != NULL) {
499 if (ent->type == type)
504 if (ent->f != NULL) {
505 if (!(ent->flags & MON_PERMIT))
506 fatal("%s: unpermitted request %d", __func__,
508 ret = (*ent->f)(pmonitor->m_sendfd, &m);
511 /* The child may use this request only once, disable it */
512 if (ent->flags & MON_ONCE) {
513 debug2("%s: %d used once, disabling now", __func__,
515 ent->flags &= ~MON_PERMIT;
524 fatal("%s: unsupported request: %d", __func__, type);
530 /* allowed key state */
532 monitor_allowed_key(u_char *blob, u_int bloblen)
534 /* make sure key is allowed */
535 if (key_blob == NULL || key_bloblen != bloblen ||
536 memcmp(key_blob, blob, key_bloblen))
542 monitor_reset_key_state(void)
545 if (key_blob != NULL)
547 if (hostbased_cuser != NULL)
548 xfree(hostbased_cuser);
549 if (hostbased_chost != NULL)
550 xfree(hostbased_chost);
553 key_blobtype = MM_NOKEY;
554 hostbased_cuser = NULL;
555 hostbased_chost = NULL;
559 mm_answer_moduli(int sock, Buffer *m)
564 min = buffer_get_int(m);
565 want = buffer_get_int(m);
566 max = buffer_get_int(m);
568 debug3("%s: got parameters: %d %d %d",
569 __func__, min, want, max);
570 /* We need to check here, too, in case the child got corrupted */
571 if (max < min || want < min || max < want)
572 fatal("%s: bad parameters: %d %d %d",
573 __func__, min, want, max);
577 dh = choose_dh(min, want, max);
579 buffer_put_char(m, 0);
582 /* Send first bignum */
583 buffer_put_char(m, 1);
584 buffer_put_bignum2(m, dh->p);
585 buffer_put_bignum2(m, dh->g);
589 mm_request_send(sock, MONITOR_ANS_MODULI, m);
594 mm_answer_sign(int sock, Buffer *m)
599 u_int siglen, datlen;
602 debug3("%s", __func__);
604 keyid = buffer_get_int(m);
605 p = buffer_get_string(m, &datlen);
608 * Supported KEX types will only return SHA1 (20 byte) or
609 * SHA256 (32 byte) hashes
611 if (datlen != 20 && datlen != 32)
612 fatal("%s: data length incorrect: %u", __func__, datlen);
614 /* save session id, it will be passed on the first call */
615 if (session_id2_len == 0) {
616 session_id2_len = datlen;
617 session_id2 = xmalloc(session_id2_len);
618 memcpy(session_id2, p, session_id2_len);
621 if ((key = get_hostkey_by_index(keyid)) == NULL)
622 fatal("%s: no hostkey from index %d", __func__, keyid);
623 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
624 fatal("%s: key_sign failed", __func__);
626 debug3("%s: signature %p(%u)", __func__, signature, siglen);
629 buffer_put_string(m, signature, siglen);
634 mm_request_send(sock, MONITOR_ANS_SIGN, m);
636 /* Turn on permissions for getpwnam */
637 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
642 /* Retrieves the password entry and also checks if the user is permitted */
645 mm_answer_pwnamallow(int sock, Buffer *m)
648 struct passwd *pwent;
651 debug3("%s", __func__);
653 username = buffer_get_string(m, NULL);
655 pwent = getpwnamallow(username);
657 if (authctxt->user) xfree(authctxt->user);
658 authctxt->user = xstrdup(username);
659 setproctitle("%s [priv]", pwent ? username : "unknown");
665 buffer_put_char(m, 0);
666 authctxt->pw = fakepw();
671 authctxt->pw = pwent;
674 buffer_put_char(m, 1);
675 buffer_put_string(m, pwent, sizeof(struct passwd));
676 buffer_put_cstring(m, pwent->pw_name);
677 buffer_put_cstring(m, "*");
678 buffer_put_cstring(m, pwent->pw_gecos);
679 #ifdef HAVE_PW_CLASS_IN_PASSWD
680 buffer_put_cstring(m, pwent->pw_class);
682 buffer_put_cstring(m, pwent->pw_dir);
683 buffer_put_cstring(m, pwent->pw_shell);
686 buffer_put_string(m, &options, sizeof(options));
687 if (options.banner != NULL)
688 buffer_put_cstring(m, options.banner);
689 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
690 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
692 /* For SSHv1 allow authentication now */
694 monitor_permit_authentications(1);
696 /* Allow service/style information on the auth context */
697 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
698 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
703 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
709 int mm_answer_auth2_read_banner(int sock, Buffer *m)
714 banner = auth2_read_banner();
715 buffer_put_cstring(m, banner != NULL ? banner : "");
716 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
725 mm_answer_authserv(int sock, Buffer *m)
727 monitor_permit_authentications(1);
729 authctxt->service = buffer_get_string(m, NULL);
730 authctxt->style = buffer_get_string(m, NULL);
731 debug3("%s: service=%s, style=%s",
732 __func__, authctxt->service, authctxt->style);
734 if (strlen(authctxt->style) == 0) {
735 xfree(authctxt->style);
736 authctxt->style = NULL;
743 mm_answer_authpassword(int sock, Buffer *m)
745 static int call_count;
750 passwd = buffer_get_string(m, &plen);
751 /* Only authenticate if the context is valid */
752 authenticated = options.password_authentication &&
753 auth_password(authctxt, passwd);
754 memset(passwd, 0, strlen(passwd));
758 buffer_put_int(m, authenticated);
760 debug3("%s: sending result %d", __func__, authenticated);
761 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
764 if (plen == 0 && call_count == 1)
765 auth_method = "none";
767 auth_method = "password";
769 /* Causes monitor loop to terminate if authenticated */
770 return (authenticated);
775 mm_answer_bsdauthquery(int sock, Buffer *m)
777 char *name, *infotxt;
783 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
784 &prompts, &echo_on) < 0 ? 0 : 1;
787 buffer_put_int(m, success);
789 buffer_put_cstring(m, prompts[0]);
791 debug3("%s: sending challenge success: %u", __func__, success);
792 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
805 mm_answer_bsdauthrespond(int sock, Buffer *m)
810 if (authctxt->as == 0)
811 fatal("%s: no bsd auth session", __func__);
813 response = buffer_get_string(m, NULL);
814 authok = options.challenge_response_authentication &&
815 auth_userresponse(authctxt->as, response, 0);
817 debug3("%s: <%s> = <%d>", __func__, response, authok);
821 buffer_put_int(m, authok);
823 debug3("%s: sending authenticated: %d", __func__, authok);
824 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
826 auth_method = "bsdauth";
828 return (authok != 0);
834 mm_answer_skeyquery(int sock, Buffer *m)
837 char challenge[1024];
840 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
841 sizeof(challenge)) < 0 ? 0 : 1;
844 buffer_put_int(m, success);
846 buffer_put_cstring(m, challenge);
848 debug3("%s: sending challenge success: %u", __func__, success);
849 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
855 mm_answer_skeyrespond(int sock, Buffer *m)
860 response = buffer_get_string(m, NULL);
862 authok = (options.challenge_response_authentication &&
864 skey_haskey(authctxt->pw->pw_name) == 0 &&
865 skey_passcheck(authctxt->pw->pw_name, response) != -1);
870 buffer_put_int(m, authok);
872 debug3("%s: sending authenticated: %d", __func__, authok);
873 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
875 auth_method = "skey";
877 return (authok != 0);
883 mm_answer_pam_start(int sock, Buffer *m)
885 if (!options.use_pam)
886 fatal("UsePAM not set, but ended up in %s anyway", __func__);
890 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
896 mm_answer_pam_account(int sock, Buffer *m)
900 if (!options.use_pam)
901 fatal("UsePAM not set, but ended up in %s anyway", __func__);
903 ret = do_pam_account();
905 buffer_put_int(m, ret);
906 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
908 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
913 static void *sshpam_ctxt, *sshpam_authok;
914 extern KbdintDevice sshpam_device;
917 mm_answer_pam_init_ctx(int sock, Buffer *m)
920 debug3("%s", __func__);
921 authctxt->user = buffer_get_string(m, NULL);
922 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
923 sshpam_authok = NULL;
925 if (sshpam_ctxt != NULL) {
926 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
927 buffer_put_int(m, 1);
929 buffer_put_int(m, 0);
931 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
936 mm_answer_pam_query(int sock, Buffer *m)
938 char *name, *info, **prompts;
939 u_int i, num, *echo_on;
942 debug3("%s", __func__);
943 sshpam_authok = NULL;
944 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
945 if (ret == 0 && num == 0)
946 sshpam_authok = sshpam_ctxt;
947 if (num > 1 || name == NULL || info == NULL)
950 buffer_put_int(m, ret);
951 buffer_put_cstring(m, name);
953 buffer_put_cstring(m, info);
955 buffer_put_int(m, num);
956 for (i = 0; i < num; ++i) {
957 buffer_put_cstring(m, prompts[i]);
959 buffer_put_int(m, echo_on[i]);
965 auth_method = "keyboard-interactive/pam";
966 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
971 mm_answer_pam_respond(int sock, Buffer *m)
977 debug3("%s", __func__);
978 sshpam_authok = NULL;
979 num = buffer_get_int(m);
981 resp = xcalloc(num, sizeof(char *));
982 for (i = 0; i < num; ++i)
983 resp[i] = buffer_get_string(m, NULL);
984 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
985 for (i = 0; i < num; ++i)
989 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
992 buffer_put_int(m, ret);
993 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
994 auth_method = "keyboard-interactive/pam";
996 sshpam_authok = sshpam_ctxt;
1001 mm_answer_pam_free_ctx(int sock, Buffer *m)
1004 debug3("%s", __func__);
1005 (sshpam_device.free_ctx)(sshpam_ctxt);
1007 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
1008 auth_method = "keyboard-interactive/pam";
1009 return (sshpam_authok == sshpam_ctxt);
1014 mm_append_debug(Buffer *m)
1016 if (auth_debug_init && buffer_len(&auth_debug)) {
1017 debug3("%s: Appending debug messages for child", __func__);
1018 buffer_append(m, buffer_ptr(&auth_debug),
1019 buffer_len(&auth_debug));
1020 buffer_clear(&auth_debug);
1025 mm_answer_keyallowed(int sock, Buffer *m)
1028 char *cuser, *chost;
1031 enum mm_keytype type = 0;
1034 debug3("%s entering", __func__);
1036 type = buffer_get_int(m);
1037 cuser = buffer_get_string(m, NULL);
1038 chost = buffer_get_string(m, NULL);
1039 blob = buffer_get_string(m, &bloblen);
1041 key = key_from_blob(blob, bloblen);
1043 if ((compat20 && type == MM_RSAHOSTKEY) ||
1044 (!compat20 && type != MM_RSAHOSTKEY))
1045 fatal("%s: key type and protocol mismatch", __func__);
1047 debug3("%s: key_from_blob: %p", __func__, key);
1049 if (key != NULL && authctxt->valid) {
1052 allowed = options.pubkey_authentication &&
1053 user_key_allowed(authctxt->pw, key);
1054 auth_method = "publickey";
1055 if (options.pubkey_authentication && allowed != 1)
1056 auth_clear_options();
1059 allowed = options.hostbased_authentication &&
1060 hostbased_key_allowed(authctxt->pw,
1062 auth_method = "hostbased";
1065 key->type = KEY_RSA1; /* XXX */
1066 allowed = options.rhosts_rsa_authentication &&
1067 auth_rhosts_rsa_key_allowed(authctxt->pw,
1069 if (options.rhosts_rsa_authentication && allowed != 1)
1070 auth_clear_options();
1071 auth_method = "rsa";
1074 fatal("%s: unknown key type %d", __func__, type);
1081 /* clear temporarily storage (used by verify) */
1082 monitor_reset_key_state();
1085 /* Save temporarily for comparison in verify */
1087 key_bloblen = bloblen;
1088 key_blobtype = type;
1089 hostbased_cuser = cuser;
1090 hostbased_chost = chost;
1092 /* Log failed attempt */
1093 auth_log(authctxt, 0, auth_method, compat20 ? " ssh2" : "");
1099 debug3("%s: key %p is %s",
1100 __func__, key, allowed ? "allowed" : "not allowed");
1103 buffer_put_int(m, allowed);
1104 buffer_put_int(m, forced_command != NULL);
1108 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1110 if (type == MM_RSAHOSTKEY)
1111 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1117 monitor_valid_userblob(u_char *data, u_int datalen)
1125 buffer_append(&b, data, datalen);
1127 if (datafellows & SSH_OLD_SESSIONID) {
1129 len = buffer_len(&b);
1130 if ((session_id2 == NULL) ||
1131 (len < session_id2_len) ||
1132 (memcmp(p, session_id2, session_id2_len) != 0))
1134 buffer_consume(&b, session_id2_len);
1136 p = buffer_get_string(&b, &len);
1137 if ((session_id2 == NULL) ||
1138 (len != session_id2_len) ||
1139 (memcmp(p, session_id2, session_id2_len) != 0))
1143 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1145 p = buffer_get_string(&b, NULL);
1146 if (strcmp(authctxt->user, p) != 0) {
1147 logit("wrong user name passed to monitor: expected %s != %.100s",
1152 buffer_skip_string(&b);
1153 if (datafellows & SSH_BUG_PKAUTH) {
1154 if (!buffer_get_char(&b))
1157 p = buffer_get_string(&b, NULL);
1158 if (strcmp("publickey", p) != 0)
1161 if (!buffer_get_char(&b))
1163 buffer_skip_string(&b);
1165 buffer_skip_string(&b);
1166 if (buffer_len(&b) != 0)
1173 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1182 buffer_append(&b, data, datalen);
1184 p = buffer_get_string(&b, &len);
1185 if ((session_id2 == NULL) ||
1186 (len != session_id2_len) ||
1187 (memcmp(p, session_id2, session_id2_len) != 0))
1191 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1193 p = buffer_get_string(&b, NULL);
1194 if (strcmp(authctxt->user, p) != 0) {
1195 logit("wrong user name passed to monitor: expected %s != %.100s",
1200 buffer_skip_string(&b); /* service */
1201 p = buffer_get_string(&b, NULL);
1202 if (strcmp(p, "hostbased") != 0)
1205 buffer_skip_string(&b); /* pkalg */
1206 buffer_skip_string(&b); /* pkblob */
1208 /* verify client host, strip trailing dot if necessary */
1209 p = buffer_get_string(&b, NULL);
1210 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1212 if (strcmp(p, chost) != 0)
1216 /* verify client user */
1217 p = buffer_get_string(&b, NULL);
1218 if (strcmp(p, cuser) != 0)
1222 if (buffer_len(&b) != 0)
1229 mm_answer_keyverify(int sock, Buffer *m)
1232 u_char *signature, *data, *blob;
1233 u_int signaturelen, datalen, bloblen;
1237 blob = buffer_get_string(m, &bloblen);
1238 signature = buffer_get_string(m, &signaturelen);
1239 data = buffer_get_string(m, &datalen);
1241 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1242 !monitor_allowed_key(blob, bloblen))
1243 fatal("%s: bad key, not previously allowed", __func__);
1245 key = key_from_blob(blob, bloblen);
1247 fatal("%s: bad public key blob", __func__);
1249 switch (key_blobtype) {
1251 valid_data = monitor_valid_userblob(data, datalen);
1254 valid_data = monitor_valid_hostbasedblob(data, datalen,
1255 hostbased_cuser, hostbased_chost);
1262 fatal("%s: bad signature data blob", __func__);
1264 verified = key_verify(key, signature, signaturelen, data, datalen);
1265 debug3("%s: key %p signature %s",
1266 __func__, key, (verified == 1) ? "verified" : "unverified");
1273 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1275 monitor_reset_key_state();
1278 buffer_put_int(m, verified);
1279 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1281 return (verified == 1);
1285 mm_record_login(Session *s, struct passwd *pw)
1288 struct sockaddr_storage from;
1291 * Get IP address of client. If the connection is not a socket, let
1292 * the address be 0.0.0.0.
1294 memset(&from, 0, sizeof(from));
1295 fromlen = sizeof(from);
1296 if (packet_connection_is_on_socket()) {
1297 if (getpeername(packet_get_connection_in(),
1298 (struct sockaddr *)&from, &fromlen) < 0) {
1299 debug("getpeername: %.100s", strerror(errno));
1303 /* Record that there was a login on that tty from the remote host. */
1304 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1305 get_remote_name_or_ip(utmp_len, options.use_dns),
1306 (struct sockaddr *)&from, fromlen);
1310 mm_session_close(Session *s)
1312 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1313 if (s->ttyfd != -1) {
1314 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1315 session_pty_cleanup2(s);
1317 session_unused(s->self);
1321 mm_answer_pty(int sock, Buffer *m)
1323 extern struct monitor *pmonitor;
1327 debug3("%s entering", __func__);
1333 s->authctxt = authctxt;
1334 s->pw = authctxt->pw;
1335 s->pid = pmonitor->m_pid;
1336 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1339 pty_setowner(authctxt->pw, s->tty);
1341 buffer_put_int(m, 1);
1342 buffer_put_cstring(m, s->tty);
1344 /* We need to trick ttyslot */
1345 if (dup2(s->ttyfd, 0) == -1)
1346 fatal("%s: dup2", __func__);
1348 mm_record_login(s, authctxt->pw);
1350 /* Now we can close the file descriptor again */
1353 /* send messages generated by record_login */
1354 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1355 buffer_clear(&loginmsg);
1357 mm_request_send(sock, MONITOR_ANS_PTY, m);
1359 if (mm_send_fd(sock, s->ptyfd) == -1 ||
1360 mm_send_fd(sock, s->ttyfd) == -1)
1361 fatal("%s: send fds failed", __func__);
1363 /* make sure nothing uses fd 0 */
1364 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1365 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1367 error("%s: fd0 %d != 0", __func__, fd0);
1369 /* slave is not needed */
1371 s->ttyfd = s->ptyfd;
1372 /* no need to dup() because nobody closes ptyfd */
1373 s->ptymaster = s->ptyfd;
1375 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1381 mm_session_close(s);
1382 buffer_put_int(m, 0);
1383 mm_request_send(sock, MONITOR_ANS_PTY, m);
1388 mm_answer_pty_cleanup(int sock, Buffer *m)
1393 debug3("%s entering", __func__);
1395 tty = buffer_get_string(m, NULL);
1396 if ((s = session_by_tty(tty)) != NULL)
1397 mm_session_close(s);
1404 mm_answer_sesskey(int sock, Buffer *m)
1409 /* Turn off permissions */
1410 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1412 if ((p = BN_new()) == NULL)
1413 fatal("%s: BN_new", __func__);
1415 buffer_get_bignum2(m, p);
1417 rsafail = ssh1_session_key(p);
1420 buffer_put_int(m, rsafail);
1421 buffer_put_bignum2(m, p);
1425 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1427 /* Turn on permissions for sessid passing */
1428 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1434 mm_answer_sessid(int sock, Buffer *m)
1438 debug3("%s entering", __func__);
1440 if (buffer_len(m) != 16)
1441 fatal("%s: bad ssh1 session id", __func__);
1442 for (i = 0; i < 16; i++)
1443 session_id[i] = buffer_get_char(m);
1445 /* Turn on permissions for getpwnam */
1446 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1452 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1456 u_char *blob = NULL;
1460 debug3("%s entering", __func__);
1462 auth_method = "rsa";
1463 if (options.rsa_authentication && authctxt->valid) {
1464 if ((client_n = BN_new()) == NULL)
1465 fatal("%s: BN_new", __func__);
1466 buffer_get_bignum2(m, client_n);
1467 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1468 BN_clear_free(client_n);
1471 buffer_put_int(m, allowed);
1472 buffer_put_int(m, forced_command != NULL);
1474 /* clear temporarily storage (used by generate challenge) */
1475 monitor_reset_key_state();
1477 if (allowed && key != NULL) {
1478 key->type = KEY_RSA; /* cheat for key_to_blob */
1479 if (key_to_blob(key, &blob, &blen) == 0)
1480 fatal("%s: key_to_blob failed", __func__);
1481 buffer_put_string(m, blob, blen);
1483 /* Save temporarily for comparison in verify */
1486 key_blobtype = MM_RSAUSERKEY;
1493 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1495 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1496 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1501 mm_answer_rsa_challenge(int sock, Buffer *m)
1507 debug3("%s entering", __func__);
1509 if (!authctxt->valid)
1510 fatal("%s: authctxt not valid", __func__);
1511 blob = buffer_get_string(m, &blen);
1512 if (!monitor_allowed_key(blob, blen))
1513 fatal("%s: bad key, not previously allowed", __func__);
1514 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1515 fatal("%s: key type mismatch", __func__);
1516 if ((key = key_from_blob(blob, blen)) == NULL)
1517 fatal("%s: received bad key", __func__);
1520 BN_clear_free(ssh1_challenge);
1521 ssh1_challenge = auth_rsa_generate_challenge(key);
1524 buffer_put_bignum2(m, ssh1_challenge);
1526 debug3("%s sending reply", __func__);
1527 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1529 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1537 mm_answer_rsa_response(int sock, Buffer *m)
1540 u_char *blob, *response;
1544 debug3("%s entering", __func__);
1546 if (!authctxt->valid)
1547 fatal("%s: authctxt not valid", __func__);
1548 if (ssh1_challenge == NULL)
1549 fatal("%s: no ssh1_challenge", __func__);
1551 blob = buffer_get_string(m, &blen);
1552 if (!monitor_allowed_key(blob, blen))
1553 fatal("%s: bad key, not previously allowed", __func__);
1554 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1555 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1556 if ((key = key_from_blob(blob, blen)) == NULL)
1557 fatal("%s: received bad key", __func__);
1558 response = buffer_get_string(m, &len);
1560 fatal("%s: received bad response to challenge", __func__);
1561 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1567 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1570 BN_clear_free(ssh1_challenge);
1571 ssh1_challenge = NULL;
1572 monitor_reset_key_state();
1575 buffer_put_int(m, success);
1576 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1582 mm_answer_term(int sock, Buffer *req)
1584 extern struct monitor *pmonitor;
1587 debug3("%s: tearing down sessions", __func__);
1589 /* The child is terminating */
1590 session_destroy_all(&mm_session_close);
1593 if (options.use_pam)
1597 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1601 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1603 /* Terminate process */
1607 #ifdef SSH_AUDIT_EVENTS
1608 /* Report that an audit event occurred */
1610 mm_answer_audit_event(int socket, Buffer *m)
1612 ssh_audit_event_t event;
1614 debug3("%s entering", __func__);
1616 event = buffer_get_int(m);
1618 case SSH_AUTH_FAIL_PUBKEY:
1619 case SSH_AUTH_FAIL_HOSTBASED:
1620 case SSH_AUTH_FAIL_GSSAPI:
1621 case SSH_LOGIN_EXCEED_MAXTRIES:
1622 case SSH_LOGIN_ROOT_DENIED:
1623 case SSH_CONNECTION_CLOSE:
1624 case SSH_INVALID_USER:
1628 fatal("Audit event type %d not permitted", event);
1635 mm_answer_audit_command(int socket, Buffer *m)
1640 debug3("%s entering", __func__);
1641 cmd = buffer_get_string(m, &len);
1642 /* sanity check command, if so how? */
1643 audit_run_command(cmd);
1647 #endif /* SSH_AUDIT_EVENTS */
1650 monitor_apply_keystate(struct monitor *pmonitor)
1653 set_newkeys(MODE_IN);
1654 set_newkeys(MODE_OUT);
1656 packet_set_protocol_flags(child_state.ssh1protoflags);
1657 packet_set_encryption_key(child_state.ssh1key,
1658 child_state.ssh1keylen, child_state.ssh1cipher);
1659 xfree(child_state.ssh1key);
1662 /* for rc4 and other stateful ciphers */
1663 packet_set_keycontext(MODE_OUT, child_state.keyout);
1664 xfree(child_state.keyout);
1665 packet_set_keycontext(MODE_IN, child_state.keyin);
1666 xfree(child_state.keyin);
1669 packet_set_iv(MODE_OUT, child_state.ivout);
1670 xfree(child_state.ivout);
1671 packet_set_iv(MODE_IN, child_state.ivin);
1672 xfree(child_state.ivin);
1675 memcpy(&incoming_stream, &child_state.incoming,
1676 sizeof(incoming_stream));
1677 memcpy(&outgoing_stream, &child_state.outgoing,
1678 sizeof(outgoing_stream));
1680 /* Update with new address */
1681 if (options.compression)
1682 mm_init_compression(pmonitor->m_zlib);
1684 /* Network I/O buffers */
1685 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1686 buffer_clear(&input);
1687 buffer_append(&input, child_state.input, child_state.ilen);
1688 memset(child_state.input, 0, child_state.ilen);
1689 xfree(child_state.input);
1691 buffer_clear(&output);
1692 buffer_append(&output, child_state.output, child_state.olen);
1693 memset(child_state.output, 0, child_state.olen);
1694 xfree(child_state.output);
1698 mm_get_kex(Buffer *m)
1704 kex = xcalloc(1, sizeof(*kex));
1705 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1706 if ((session_id2 == NULL) ||
1707 (kex->session_id_len != session_id2_len) ||
1708 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1709 fatal("mm_get_get: internal error: bad session id");
1710 kex->we_need = buffer_get_int(m);
1711 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1712 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1713 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1714 kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
1716 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1717 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_server;
1718 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1721 kex->hostkey_type = buffer_get_int(m);
1722 kex->kex_type = buffer_get_int(m);
1723 blob = buffer_get_string(m, &bloblen);
1724 buffer_init(&kex->my);
1725 buffer_append(&kex->my, blob, bloblen);
1727 blob = buffer_get_string(m, &bloblen);
1728 buffer_init(&kex->peer);
1729 buffer_append(&kex->peer, blob, bloblen);
1732 kex->flags = buffer_get_int(m);
1733 kex->client_version_string = buffer_get_string(m, NULL);
1734 kex->server_version_string = buffer_get_string(m, NULL);
1735 kex->load_host_key=&get_hostkey_by_type;
1736 kex->host_key_index=&get_hostkey_index;
1741 /* This function requries careful sanity checking */
1744 mm_get_keystate(struct monitor *pmonitor)
1748 u_int bloblen, plen;
1749 u_int32_t seqnr, packets;
1750 u_int64_t blocks, bytes;
1752 debug3("%s: Waiting for new keys", __func__);
1755 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1757 child_state.ssh1protoflags = buffer_get_int(&m);
1758 child_state.ssh1cipher = buffer_get_int(&m);
1759 child_state.ssh1key = buffer_get_string(&m,
1760 &child_state.ssh1keylen);
1761 child_state.ivout = buffer_get_string(&m,
1762 &child_state.ivoutlen);
1763 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1766 /* Get the Kex for rekeying */
1767 *pmonitor->m_pkex = mm_get_kex(&m);
1770 blob = buffer_get_string(&m, &bloblen);
1771 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1774 debug3("%s: Waiting for second key", __func__);
1775 blob = buffer_get_string(&m, &bloblen);
1776 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1779 /* Now get sequence numbers for the packets */
1780 seqnr = buffer_get_int(&m);
1781 blocks = buffer_get_int64(&m);
1782 packets = buffer_get_int(&m);
1783 bytes = buffer_get_int64(&m);
1784 packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes);
1785 seqnr = buffer_get_int(&m);
1786 blocks = buffer_get_int64(&m);
1787 packets = buffer_get_int(&m);
1788 bytes = buffer_get_int64(&m);
1789 packet_set_state(MODE_IN, seqnr, blocks, packets, bytes);
1792 /* Get the key context */
1793 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1794 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1796 debug3("%s: Getting compression state", __func__);
1797 /* Get compression state */
1798 p = buffer_get_string(&m, &plen);
1799 if (plen != sizeof(child_state.outgoing))
1800 fatal("%s: bad request size", __func__);
1801 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1804 p = buffer_get_string(&m, &plen);
1805 if (plen != sizeof(child_state.incoming))
1806 fatal("%s: bad request size", __func__);
1807 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1810 /* Network I/O buffers */
1811 debug3("%s: Getting Network I/O buffers", __func__);
1812 child_state.input = buffer_get_string(&m, &child_state.ilen);
1813 child_state.output = buffer_get_string(&m, &child_state.olen);
1819 /* Allocation functions for zlib */
1821 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1823 size_t len = (size_t) size * ncount;
1826 if (len == 0 || ncount > SIZE_T_MAX / size)
1827 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1829 address = mm_malloc(mm, len);
1835 mm_zfree(struct mm_master *mm, void *address)
1837 mm_free(mm, address);
1841 mm_init_compression(struct mm_master *mm)
1843 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1844 outgoing_stream.zfree = (free_func)mm_zfree;
1845 outgoing_stream.opaque = mm;
1847 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1848 incoming_stream.zfree = (free_func)mm_zfree;
1849 incoming_stream.opaque = mm;
1854 #define FD_CLOSEONEXEC(x) do { \
1855 if (fcntl(x, F_SETFD, 1) == -1) \
1856 fatal("fcntl(%d, F_SETFD)", x); \
1860 monitor_socketpair(int *pair)
1862 #ifdef HAVE_SOCKETPAIR
1863 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1864 fatal("%s: socketpair", __func__);
1866 fatal("%s: UsePrivilegeSeparation=yes not supported",
1869 FD_CLOSEONEXEC(pair[0]);
1870 FD_CLOSEONEXEC(pair[1]);
1873 #define MM_MEMSIZE 65536
1878 struct monitor *mon;
1881 mon = xcalloc(1, sizeof(*mon));
1883 monitor_socketpair(pair);
1885 mon->m_recvfd = pair[0];
1886 mon->m_sendfd = pair[1];
1888 /* Used to share zlib space across processes */
1889 if (options.compression) {
1890 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1891 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1893 /* Compression needs to share state across borders */
1894 mm_init_compression(mon->m_zlib);
1901 monitor_reinit(struct monitor *mon)
1905 monitor_socketpair(pair);
1907 mon->m_recvfd = pair[0];
1908 mon->m_sendfd = pair[1];
1913 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1919 goid.elements = buffer_get_string(m, &len);
1922 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1924 xfree(goid.elements);
1927 buffer_put_int(m, major);
1929 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1931 /* Now we have a context, enable the step */
1932 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1938 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1941 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1942 OM_uint32 major, minor;
1943 OM_uint32 flags = 0; /* GSI needs this */
1946 in.value = buffer_get_string(m, &len);
1948 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1952 buffer_put_int(m, major);
1953 buffer_put_string(m, out.value, out.length);
1954 buffer_put_int(m, flags);
1955 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1957 gss_release_buffer(&minor, &out);
1959 if (major == GSS_S_COMPLETE) {
1960 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1961 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1962 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1963 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1964 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1970 mm_answer_gss_checkmic(int sock, Buffer *m)
1972 gss_buffer_desc gssbuf, mic;
1976 gssbuf.value = buffer_get_string(m, &len);
1977 gssbuf.length = len;
1978 mic.value = buffer_get_string(m, &len);
1981 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1983 xfree(gssbuf.value);
1987 buffer_put_int(m, ret);
1989 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1991 if (!GSS_ERROR(ret))
1992 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1998 mm_answer_gss_userok(int sock, Buffer *m)
2002 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
2005 buffer_put_int(m, authenticated);
2007 debug3("%s: sending result %d", __func__, authenticated);
2008 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
2010 auth_method = "gssapi-with-mic";
2012 /* Monitor loop will terminate if authenticated */
2013 return (authenticated);
2017 mm_answer_gss_sign(int socket, Buffer *m)
2019 gss_buffer_desc data;
2020 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
2021 OM_uint32 major, minor;
2024 data.value = buffer_get_string(m, &len);
2026 if (data.length != 20)
2027 fatal("%s: data length incorrect: %d", __func__, (int)data.length);
2029 /* Save the session ID on the first time around */
2030 if (session_id2_len == 0) {
2031 session_id2_len = data.length;
2032 session_id2 = xmalloc(session_id2_len);
2033 memcpy(session_id2, data.value, session_id2_len);
2035 major = ssh_gssapi_sign(gsscontext, &data, &hash);
2040 buffer_put_int(m, major);
2041 buffer_put_string(m, hash.value, hash.length);
2043 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
2045 gss_release_buffer(&minor, &hash);
2047 /* Turn on getpwnam permissions */
2048 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
2054 mm_answer_gss_error(int socket, Buffer *m) {
2055 OM_uint32 major,minor;
2058 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2060 buffer_put_int(m,major);
2061 buffer_put_int(m,minor);
2062 buffer_put_cstring(m,msg);
2064 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2072 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2073 OM_uint32 major,minor;
2074 gss_OID_set mech_set;
2077 major=gss_indicate_mechs(&minor, &mech_set);
2080 buffer_put_int(m, major);
2081 buffer_put_int(m, mech_set->count);
2082 for (i=0; i < mech_set->count; i++) {
2083 buffer_put_string(m, mech_set->elements[i].elements,
2084 mech_set->elements[i].length);
2087 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2088 gss_release_oid_set(&minor,&mech_set);
2091 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2097 mm_answer_gss_localname(int socket, Buffer *m) {
2100 ssh_gssapi_localname(&name);
2104 buffer_put_cstring(m, name);
2105 debug3("%s: sending result %s", __func__, name);
2108 buffer_put_cstring(m, "");
2109 debug3("%s: sending result \"\"", __func__);
2112 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);