2 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
3 * Copyright 2002 Markus Friedl <markus@openbsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 RCSID("$OpenBSD: monitor.c,v 1.64 2005/10/13 22:24:31 stevesk Exp $");
30 #include <openssl/dh.h>
40 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
43 #define TARGET_OS_MAC 1
48 #include "auth-options.h"
57 #include "monitor_mm.h"
58 #include "monitor_wrap.h"
59 #include "monitor_fdpass.h"
69 static Gssctxt *gsscontext = NULL;
73 extern ServerOptions options;
74 extern u_int utmp_len;
75 extern Newkeys *current_keys[];
76 extern z_stream incoming_stream;
77 extern z_stream outgoing_stream;
78 extern u_char session_id[];
79 extern Buffer input, output;
80 extern Buffer auth_debug;
81 extern int auth_debug_init;
82 extern Buffer loginmsg;
84 /* State exported from the child */
107 /* Functions on the monitor that answer unprivileged requests */
109 int mm_answer_moduli(int, Buffer *);
110 int mm_answer_sign(int, Buffer *);
111 int mm_answer_pwnamallow(int, Buffer *);
112 int mm_answer_auth2_read_banner(int, Buffer *);
113 int mm_answer_authserv(int, Buffer *);
114 int mm_answer_authpassword(int, Buffer *);
115 int mm_answer_bsdauthquery(int, Buffer *);
116 int mm_answer_bsdauthrespond(int, Buffer *);
117 int mm_answer_skeyquery(int, Buffer *);
118 int mm_answer_skeyrespond(int, Buffer *);
119 int mm_answer_keyallowed(int, Buffer *);
120 int mm_answer_keyverify(int, Buffer *);
121 int mm_answer_pty(int, Buffer *);
122 int mm_answer_pty_cleanup(int, Buffer *);
123 int mm_answer_term(int, Buffer *);
124 int mm_answer_rsa_keyallowed(int, Buffer *);
125 int mm_answer_rsa_challenge(int, Buffer *);
126 int mm_answer_rsa_response(int, Buffer *);
127 int mm_answer_sesskey(int, Buffer *);
128 int mm_answer_sessid(int, Buffer *);
131 int mm_answer_pam_start(int, Buffer *);
132 int mm_answer_pam_account(int, Buffer *);
133 int mm_answer_pam_init_ctx(int, Buffer *);
134 int mm_answer_pam_query(int, Buffer *);
135 int mm_answer_pam_respond(int, Buffer *);
136 int mm_answer_pam_free_ctx(int, Buffer *);
140 int mm_answer_gss_setup_ctx(int, Buffer *);
141 int mm_answer_gss_accept_ctx(int, Buffer *);
142 int mm_answer_gss_userok(int, Buffer *);
143 int mm_answer_gss_checkmic(int, Buffer *);
144 int mm_answer_gss_sign(int, Buffer *);
145 int mm_answer_gss_error(int, Buffer *);
146 int mm_answer_gss_indicate_mechs(int, Buffer *);
147 int mm_answer_gss_localname(int, Buffer *);
150 #ifdef SSH_AUDIT_EVENTS
151 int mm_answer_audit_event(int, Buffer *);
152 int mm_answer_audit_command(int, Buffer *);
155 static Authctxt *authctxt;
156 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
158 /* local state for key verify */
159 static u_char *key_blob = NULL;
160 static u_int key_bloblen = 0;
161 static int key_blobtype = MM_NOKEY;
162 static char *hostbased_cuser = NULL;
163 static char *hostbased_chost = NULL;
164 static char *auth_method = "unknown";
165 static u_int session_id2_len = 0;
166 static u_char *session_id2 = NULL;
167 static pid_t monitor_child_pid;
170 enum monitor_reqtype type;
172 int (*f)(int, Buffer *);
175 #define MON_ISAUTH 0x0004 /* Required for Authentication */
176 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
177 #define MON_ONCE 0x0010 /* Disable after calling */
179 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
181 #define MON_PERMIT 0x1000 /* Request is permitted */
183 struct mon_table mon_dispatch_proto20[] = {
184 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
185 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
186 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
187 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
188 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
189 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
191 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
192 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
193 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
194 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
195 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
196 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
198 #ifdef SSH_AUDIT_EVENTS
199 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
202 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
203 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
206 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
207 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
209 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
210 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
212 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
213 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
214 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
215 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
216 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
217 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
218 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
219 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
224 struct mon_table mon_dispatch_postauth20[] = {
226 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
227 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
228 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
229 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
230 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
232 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
233 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
234 {MONITOR_REQ_PTY, 0, mm_answer_pty},
235 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
236 {MONITOR_REQ_TERM, 0, mm_answer_term},
237 #ifdef SSH_AUDIT_EVENTS
238 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
239 {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
244 struct mon_table mon_dispatch_proto15[] = {
245 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
246 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
247 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
248 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
249 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
250 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
251 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
252 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
254 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
255 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
258 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
259 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
262 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
263 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
264 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
265 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
266 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
269 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
270 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
271 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
272 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
273 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
274 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
276 #ifdef SSH_AUDIT_EVENTS
277 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
282 struct mon_table mon_dispatch_postauth15[] = {
283 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
284 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
285 {MONITOR_REQ_TERM, 0, mm_answer_term},
286 #ifdef SSH_AUDIT_EVENTS
287 {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
288 {MONITOR_REQ_AUDIT_COMMAND, MON_ONCE, mm_answer_audit_command},
293 struct mon_table *mon_dispatch;
295 /* Specifies if a certain message is allowed at the moment */
298 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
300 while (ent->f != NULL) {
301 if (ent->type == type) {
302 ent->flags &= ~MON_PERMIT;
303 ent->flags |= permit ? MON_PERMIT : 0;
311 monitor_permit_authentications(int permit)
313 struct mon_table *ent = mon_dispatch;
315 while (ent->f != NULL) {
316 if (ent->flags & MON_AUTH) {
317 ent->flags &= ~MON_PERMIT;
318 ent->flags |= permit ? MON_PERMIT : 0;
325 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
327 struct mon_table *ent;
328 int authenticated = 0;
330 debug3("preauth child monitor started");
332 authctxt = _authctxt;
333 memset(authctxt, 0, sizeof(*authctxt));
335 authctxt->loginmsg = &loginmsg;
338 mon_dispatch = mon_dispatch_proto20;
340 /* Permit requests for moduli and signatures */
341 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
342 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
344 /* and for the GSSAPI key exchange */
345 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
346 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
347 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
350 mon_dispatch = mon_dispatch_proto15;
352 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
355 /* The first few requests do not require asynchronous access */
356 while (!authenticated) {
357 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
359 if (!(ent->flags & MON_AUTHDECIDE))
360 fatal("%s: unexpected authentication from %d",
361 __func__, ent->type);
362 if (authctxt->pw->pw_uid == 0 &&
363 !auth_root_allowed(auth_method))
366 /* PAM needs to perform account checks after auth */
367 if (options.use_pam && authenticated) {
371 mm_request_receive_expect(pmonitor->m_sendfd,
372 MONITOR_REQ_PAM_ACCOUNT, &m);
373 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
379 if (ent->flags & MON_AUTHDECIDE) {
380 auth_log(authctxt, authenticated, auth_method,
381 compat20 ? " ssh2" : "");
383 authctxt->failures++;
387 if (!authctxt->valid)
388 fatal("%s: authenticated invalid user", __func__);
390 debug("%s: %s has been authenticated by privileged process",
391 __func__, authctxt->user);
393 mm_get_keystate(pmonitor);
397 monitor_set_child_handler(pid_t pid)
399 monitor_child_pid = pid;
403 monitor_child_handler(int sig)
405 kill(monitor_child_pid, sig);
409 monitor_child_postauth(struct monitor *pmonitor)
411 monitor_set_child_handler(pmonitor->m_pid);
412 signal(SIGHUP, &monitor_child_handler);
413 signal(SIGTERM, &monitor_child_handler);
416 mon_dispatch = mon_dispatch_postauth20;
418 /* Permit requests for moduli and signatures */
419 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
420 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
421 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
424 /* and for the GSSAPI key exchange */
425 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
426 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
427 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
431 mon_dispatch = mon_dispatch_postauth15;
432 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
435 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
438 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
439 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
443 monitor_read(pmonitor, mon_dispatch, NULL);
447 monitor_sync(struct monitor *pmonitor)
449 if (options.compression) {
450 /* The member allocation is not visible, so sync it */
451 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
456 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
457 struct mon_table **pent)
465 mm_request_receive(pmonitor->m_sendfd, &m);
466 type = buffer_get_char(&m);
468 debug3("%s: checking request %d", __func__, type);
470 while (ent->f != NULL) {
471 if (ent->type == type)
476 if (ent->f != NULL) {
477 if (!(ent->flags & MON_PERMIT))
478 fatal("%s: unpermitted request %d", __func__,
480 ret = (*ent->f)(pmonitor->m_sendfd, &m);
483 /* The child may use this request only once, disable it */
484 if (ent->flags & MON_ONCE) {
485 debug2("%s: %d used once, disabling now", __func__,
487 ent->flags &= ~MON_PERMIT;
496 fatal("%s: unsupported request: %d", __func__, type);
502 /* allowed key state */
504 monitor_allowed_key(u_char *blob, u_int bloblen)
506 /* make sure key is allowed */
507 if (key_blob == NULL || key_bloblen != bloblen ||
508 memcmp(key_blob, blob, key_bloblen))
514 monitor_reset_key_state(void)
517 if (key_blob != NULL)
519 if (hostbased_cuser != NULL)
520 xfree(hostbased_cuser);
521 if (hostbased_chost != NULL)
522 xfree(hostbased_chost);
525 key_blobtype = MM_NOKEY;
526 hostbased_cuser = NULL;
527 hostbased_chost = NULL;
531 mm_answer_moduli(int sock, Buffer *m)
536 min = buffer_get_int(m);
537 want = buffer_get_int(m);
538 max = buffer_get_int(m);
540 debug3("%s: got parameters: %d %d %d",
541 __func__, min, want, max);
542 /* We need to check here, too, in case the child got corrupted */
543 if (max < min || want < min || max < want)
544 fatal("%s: bad parameters: %d %d %d",
545 __func__, min, want, max);
549 dh = choose_dh(min, want, max);
551 buffer_put_char(m, 0);
554 /* Send first bignum */
555 buffer_put_char(m, 1);
556 buffer_put_bignum2(m, dh->p);
557 buffer_put_bignum2(m, dh->g);
561 mm_request_send(sock, MONITOR_ANS_MODULI, m);
566 mm_answer_sign(int sock, Buffer *m)
571 u_int siglen, datlen;
574 debug3("%s", __func__);
576 keyid = buffer_get_int(m);
577 p = buffer_get_string(m, &datlen);
580 fatal("%s: data length incorrect: %u", __func__, datlen);
582 /* save session id, it will be passed on the first call */
583 if (session_id2_len == 0) {
584 session_id2_len = datlen;
585 session_id2 = xmalloc(session_id2_len);
586 memcpy(session_id2, p, session_id2_len);
589 if ((key = get_hostkey_by_index(keyid)) == NULL)
590 fatal("%s: no hostkey from index %d", __func__, keyid);
591 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
592 fatal("%s: key_sign failed", __func__);
594 debug3("%s: signature %p(%u)", __func__, signature, siglen);
597 buffer_put_string(m, signature, siglen);
602 mm_request_send(sock, MONITOR_ANS_SIGN, m);
604 /* Turn on permissions for getpwnam */
605 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
610 /* Retrieves the password entry and also checks if the user is permitted */
613 mm_answer_pwnamallow(int sock, Buffer *m)
616 struct passwd *pwent;
619 debug3("%s", __func__);
621 username = buffer_get_string(m, NULL);
623 pwent = getpwnamallow(username);
625 if (authctxt->user) xfree(authctxt->user);
626 authctxt->user = xstrdup(username);
627 setproctitle("%s [priv]", pwent ? username : "unknown");
633 buffer_put_char(m, 0);
634 authctxt->pw = fakepw();
639 authctxt->pw = pwent;
642 buffer_put_char(m, 1);
643 buffer_put_string(m, pwent, sizeof(struct passwd));
644 buffer_put_cstring(m, pwent->pw_name);
645 buffer_put_cstring(m, "*");
646 buffer_put_cstring(m, pwent->pw_gecos);
647 #ifdef HAVE_PW_CLASS_IN_PASSWD
648 buffer_put_cstring(m, pwent->pw_class);
650 buffer_put_cstring(m, pwent->pw_dir);
651 buffer_put_cstring(m, pwent->pw_shell);
654 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
655 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
657 /* For SSHv1 allow authentication now */
659 monitor_permit_authentications(1);
661 /* Allow service/style information on the auth context */
662 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
663 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
668 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
670 #ifdef SSH_AUDIT_EVENTS
671 monitor_permit(mon_dispatch, MONITOR_REQ_AUDIT_COMMAND, 1);
677 int mm_answer_auth2_read_banner(int sock, Buffer *m)
682 banner = auth2_read_banner();
683 buffer_put_cstring(m, banner != NULL ? banner : "");
684 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
693 mm_answer_authserv(int sock, Buffer *m)
695 monitor_permit_authentications(1);
697 authctxt->service = buffer_get_string(m, NULL);
698 authctxt->style = buffer_get_string(m, NULL);
699 debug3("%s: service=%s, style=%s",
700 __func__, authctxt->service, authctxt->style);
702 if (strlen(authctxt->style) == 0) {
703 xfree(authctxt->style);
704 authctxt->style = NULL;
711 mm_answer_authpassword(int sock, Buffer *m)
713 static int call_count;
718 passwd = buffer_get_string(m, &plen);
719 /* Only authenticate if the context is valid */
720 authenticated = options.password_authentication &&
721 auth_password(authctxt, passwd);
722 memset(passwd, 0, strlen(passwd));
726 buffer_put_int(m, authenticated);
728 debug3("%s: sending result %d", __func__, authenticated);
729 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
732 if (plen == 0 && call_count == 1)
733 auth_method = "none";
735 auth_method = "password";
737 /* Causes monitor loop to terminate if authenticated */
738 return (authenticated);
743 mm_answer_bsdauthquery(int sock, Buffer *m)
745 char *name, *infotxt;
751 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
752 &prompts, &echo_on) < 0 ? 0 : 1;
755 buffer_put_int(m, success);
757 buffer_put_cstring(m, prompts[0]);
759 debug3("%s: sending challenge success: %u", __func__, success);
760 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
773 mm_answer_bsdauthrespond(int sock, Buffer *m)
778 if (authctxt->as == 0)
779 fatal("%s: no bsd auth session", __func__);
781 response = buffer_get_string(m, NULL);
782 authok = options.challenge_response_authentication &&
783 auth_userresponse(authctxt->as, response, 0);
785 debug3("%s: <%s> = <%d>", __func__, response, authok);
789 buffer_put_int(m, authok);
791 debug3("%s: sending authenticated: %d", __func__, authok);
792 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
794 auth_method = "bsdauth";
796 return (authok != 0);
802 mm_answer_skeyquery(int sock, Buffer *m)
805 char challenge[1024];
808 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
809 sizeof(challenge)) < 0 ? 0 : 1;
812 buffer_put_int(m, success);
814 buffer_put_cstring(m, challenge);
816 debug3("%s: sending challenge success: %u", __func__, success);
817 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
823 mm_answer_skeyrespond(int sock, Buffer *m)
828 response = buffer_get_string(m, NULL);
830 authok = (options.challenge_response_authentication &&
832 skey_haskey(authctxt->pw->pw_name) == 0 &&
833 skey_passcheck(authctxt->pw->pw_name, response) != -1);
838 buffer_put_int(m, authok);
840 debug3("%s: sending authenticated: %d", __func__, authok);
841 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
843 auth_method = "skey";
845 return (authok != 0);
851 mm_answer_pam_start(int sock, Buffer *m)
853 if (!options.use_pam)
854 fatal("UsePAM not set, but ended up in %s anyway", __func__);
858 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
864 mm_answer_pam_account(int sock, Buffer *m)
868 if (!options.use_pam)
869 fatal("UsePAM not set, but ended up in %s anyway", __func__);
871 ret = do_pam_account();
873 buffer_put_int(m, ret);
874 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
876 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
881 static void *sshpam_ctxt, *sshpam_authok;
882 extern KbdintDevice sshpam_device;
885 mm_answer_pam_init_ctx(int sock, Buffer *m)
888 debug3("%s", __func__);
889 authctxt->user = buffer_get_string(m, NULL);
890 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
891 sshpam_authok = NULL;
893 if (sshpam_ctxt != NULL) {
894 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
895 buffer_put_int(m, 1);
897 buffer_put_int(m, 0);
899 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
904 mm_answer_pam_query(int sock, Buffer *m)
906 char *name, *info, **prompts;
907 u_int i, num, *echo_on;
910 debug3("%s", __func__);
911 sshpam_authok = NULL;
912 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
913 if (ret == 0 && num == 0)
914 sshpam_authok = sshpam_ctxt;
915 if (num > 1 || name == NULL || info == NULL)
918 buffer_put_int(m, ret);
919 buffer_put_cstring(m, name);
921 buffer_put_cstring(m, info);
923 buffer_put_int(m, num);
924 for (i = 0; i < num; ++i) {
925 buffer_put_cstring(m, prompts[i]);
927 buffer_put_int(m, echo_on[i]);
933 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
938 mm_answer_pam_respond(int sock, Buffer *m)
944 debug3("%s", __func__);
945 sshpam_authok = NULL;
946 num = buffer_get_int(m);
948 resp = xmalloc(num * sizeof(char *));
949 for (i = 0; i < num; ++i)
950 resp[i] = buffer_get_string(m, NULL);
951 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
952 for (i = 0; i < num; ++i)
956 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
959 buffer_put_int(m, ret);
960 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
961 auth_method = "keyboard-interactive/pam";
963 sshpam_authok = sshpam_ctxt;
968 mm_answer_pam_free_ctx(int sock, Buffer *m)
971 debug3("%s", __func__);
972 (sshpam_device.free_ctx)(sshpam_ctxt);
974 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
975 return (sshpam_authok == sshpam_ctxt);
980 mm_append_debug(Buffer *m)
982 if (auth_debug_init && buffer_len(&auth_debug)) {
983 debug3("%s: Appending debug messages for child", __func__);
984 buffer_append(m, buffer_ptr(&auth_debug),
985 buffer_len(&auth_debug));
986 buffer_clear(&auth_debug);
991 mm_answer_keyallowed(int sock, Buffer *m)
997 enum mm_keytype type = 0;
1000 debug3("%s entering", __func__);
1002 type = buffer_get_int(m);
1003 cuser = buffer_get_string(m, NULL);
1004 chost = buffer_get_string(m, NULL);
1005 blob = buffer_get_string(m, &bloblen);
1007 key = key_from_blob(blob, bloblen);
1009 if ((compat20 && type == MM_RSAHOSTKEY) ||
1010 (!compat20 && type != MM_RSAHOSTKEY))
1011 fatal("%s: key type and protocol mismatch", __func__);
1013 debug3("%s: key_from_blob: %p", __func__, key);
1015 if (key != NULL && authctxt->valid) {
1018 allowed = options.pubkey_authentication &&
1019 user_key_allowed(authctxt->pw, key);
1022 allowed = options.hostbased_authentication &&
1023 hostbased_key_allowed(authctxt->pw,
1027 key->type = KEY_RSA1; /* XXX */
1028 allowed = options.rhosts_rsa_authentication &&
1029 auth_rhosts_rsa_key_allowed(authctxt->pw,
1033 fatal("%s: unknown key type %d", __func__, type);
1040 /* clear temporarily storage (used by verify) */
1041 monitor_reset_key_state();
1044 /* Save temporarily for comparison in verify */
1046 key_bloblen = bloblen;
1047 key_blobtype = type;
1048 hostbased_cuser = cuser;
1049 hostbased_chost = chost;
1052 debug3("%s: key %p is %s",
1053 __func__, key, allowed ? "allowed" : "disallowed");
1056 buffer_put_int(m, allowed);
1057 buffer_put_int(m, forced_command != NULL);
1061 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1063 if (type == MM_RSAHOSTKEY)
1064 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1070 monitor_valid_userblob(u_char *data, u_int datalen)
1078 buffer_append(&b, data, datalen);
1080 if (datafellows & SSH_OLD_SESSIONID) {
1082 len = buffer_len(&b);
1083 if ((session_id2 == NULL) ||
1084 (len < session_id2_len) ||
1085 (memcmp(p, session_id2, session_id2_len) != 0))
1087 buffer_consume(&b, session_id2_len);
1089 p = buffer_get_string(&b, &len);
1090 if ((session_id2 == NULL) ||
1091 (len != session_id2_len) ||
1092 (memcmp(p, session_id2, session_id2_len) != 0))
1096 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1098 p = buffer_get_string(&b, NULL);
1099 if (strcmp(authctxt->user, p) != 0) {
1100 logit("wrong user name passed to monitor: expected %s != %.100s",
1105 buffer_skip_string(&b);
1106 if (datafellows & SSH_BUG_PKAUTH) {
1107 if (!buffer_get_char(&b))
1110 p = buffer_get_string(&b, NULL);
1111 if (strcmp("publickey", p) != 0)
1114 if (!buffer_get_char(&b))
1116 buffer_skip_string(&b);
1118 buffer_skip_string(&b);
1119 if (buffer_len(&b) != 0)
1126 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1135 buffer_append(&b, data, datalen);
1137 p = buffer_get_string(&b, &len);
1138 if ((session_id2 == NULL) ||
1139 (len != session_id2_len) ||
1140 (memcmp(p, session_id2, session_id2_len) != 0))
1144 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1146 p = buffer_get_string(&b, NULL);
1147 if (strcmp(authctxt->user, p) != 0) {
1148 logit("wrong user name passed to monitor: expected %s != %.100s",
1153 buffer_skip_string(&b); /* service */
1154 p = buffer_get_string(&b, NULL);
1155 if (strcmp(p, "hostbased") != 0)
1158 buffer_skip_string(&b); /* pkalg */
1159 buffer_skip_string(&b); /* pkblob */
1161 /* verify client host, strip trailing dot if necessary */
1162 p = buffer_get_string(&b, NULL);
1163 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1165 if (strcmp(p, chost) != 0)
1169 /* verify client user */
1170 p = buffer_get_string(&b, NULL);
1171 if (strcmp(p, cuser) != 0)
1175 if (buffer_len(&b) != 0)
1182 mm_answer_keyverify(int sock, Buffer *m)
1185 u_char *signature, *data, *blob;
1186 u_int signaturelen, datalen, bloblen;
1190 blob = buffer_get_string(m, &bloblen);
1191 signature = buffer_get_string(m, &signaturelen);
1192 data = buffer_get_string(m, &datalen);
1194 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1195 !monitor_allowed_key(blob, bloblen))
1196 fatal("%s: bad key, not previously allowed", __func__);
1198 key = key_from_blob(blob, bloblen);
1200 fatal("%s: bad public key blob", __func__);
1202 switch (key_blobtype) {
1204 valid_data = monitor_valid_userblob(data, datalen);
1207 valid_data = monitor_valid_hostbasedblob(data, datalen,
1208 hostbased_cuser, hostbased_chost);
1215 fatal("%s: bad signature data blob", __func__);
1217 verified = key_verify(key, signature, signaturelen, data, datalen);
1218 debug3("%s: key %p signature %s",
1219 __func__, key, verified ? "verified" : "unverified");
1226 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1228 monitor_reset_key_state();
1231 buffer_put_int(m, verified);
1232 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1238 mm_record_login(Session *s, struct passwd *pw)
1241 struct sockaddr_storage from;
1244 * Get IP address of client. If the connection is not a socket, let
1245 * the address be 0.0.0.0.
1247 memset(&from, 0, sizeof(from));
1248 fromlen = sizeof(from);
1249 if (packet_connection_is_on_socket()) {
1250 if (getpeername(packet_get_connection_in(),
1251 (struct sockaddr *) & from, &fromlen) < 0) {
1252 debug("getpeername: %.100s", strerror(errno));
1256 /* Record that there was a login on that tty from the remote host. */
1257 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1258 get_remote_name_or_ip(utmp_len, options.use_dns),
1259 (struct sockaddr *)&from, fromlen);
1263 mm_session_close(Session *s)
1265 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1266 if (s->ttyfd != -1) {
1267 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1268 session_pty_cleanup2(s);
1274 mm_answer_pty(int sock, Buffer *m)
1276 extern struct monitor *pmonitor;
1280 debug3("%s entering", __func__);
1286 s->authctxt = authctxt;
1287 s->pw = authctxt->pw;
1288 s->pid = pmonitor->m_pid;
1289 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1292 pty_setowner(authctxt->pw, s->tty);
1294 buffer_put_int(m, 1);
1295 buffer_put_cstring(m, s->tty);
1297 /* We need to trick ttyslot */
1298 if (dup2(s->ttyfd, 0) == -1)
1299 fatal("%s: dup2", __func__);
1301 mm_record_login(s, authctxt->pw);
1303 /* Now we can close the file descriptor again */
1306 /* send messages generated by record_login */
1307 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1308 buffer_clear(&loginmsg);
1310 mm_request_send(sock, MONITOR_ANS_PTY, m);
1312 mm_send_fd(sock, s->ptyfd);
1313 mm_send_fd(sock, s->ttyfd);
1315 /* make sure nothing uses fd 0 */
1316 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1317 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1319 error("%s: fd0 %d != 0", __func__, fd0);
1321 /* slave is not needed */
1323 s->ttyfd = s->ptyfd;
1324 /* no need to dup() because nobody closes ptyfd */
1325 s->ptymaster = s->ptyfd;
1327 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1333 mm_session_close(s);
1334 buffer_put_int(m, 0);
1335 mm_request_send(sock, MONITOR_ANS_PTY, m);
1340 mm_answer_pty_cleanup(int sock, Buffer *m)
1345 debug3("%s entering", __func__);
1347 tty = buffer_get_string(m, NULL);
1348 if ((s = session_by_tty(tty)) != NULL)
1349 mm_session_close(s);
1356 mm_answer_sesskey(int sock, Buffer *m)
1361 /* Turn off permissions */
1362 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
1364 if ((p = BN_new()) == NULL)
1365 fatal("%s: BN_new", __func__);
1367 buffer_get_bignum2(m, p);
1369 rsafail = ssh1_session_key(p);
1372 buffer_put_int(m, rsafail);
1373 buffer_put_bignum2(m, p);
1377 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1379 /* Turn on permissions for sessid passing */
1380 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1386 mm_answer_sessid(int sock, Buffer *m)
1390 debug3("%s entering", __func__);
1392 if (buffer_len(m) != 16)
1393 fatal("%s: bad ssh1 session id", __func__);
1394 for (i = 0; i < 16; i++)
1395 session_id[i] = buffer_get_char(m);
1397 /* Turn on permissions for getpwnam */
1398 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1404 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1408 u_char *blob = NULL;
1412 debug3("%s entering", __func__);
1414 if (options.rsa_authentication && authctxt->valid) {
1415 if ((client_n = BN_new()) == NULL)
1416 fatal("%s: BN_new", __func__);
1417 buffer_get_bignum2(m, client_n);
1418 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1419 BN_clear_free(client_n);
1422 buffer_put_int(m, allowed);
1423 buffer_put_int(m, forced_command != NULL);
1425 /* clear temporarily storage (used by generate challenge) */
1426 monitor_reset_key_state();
1428 if (allowed && key != NULL) {
1429 key->type = KEY_RSA; /* cheat for key_to_blob */
1430 if (key_to_blob(key, &blob, &blen) == 0)
1431 fatal("%s: key_to_blob failed", __func__);
1432 buffer_put_string(m, blob, blen);
1434 /* Save temporarily for comparison in verify */
1437 key_blobtype = MM_RSAUSERKEY;
1444 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1446 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1447 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1452 mm_answer_rsa_challenge(int sock, Buffer *m)
1458 debug3("%s entering", __func__);
1460 if (!authctxt->valid)
1461 fatal("%s: authctxt not valid", __func__);
1462 blob = buffer_get_string(m, &blen);
1463 if (!monitor_allowed_key(blob, blen))
1464 fatal("%s: bad key, not previously allowed", __func__);
1465 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1466 fatal("%s: key type mismatch", __func__);
1467 if ((key = key_from_blob(blob, blen)) == NULL)
1468 fatal("%s: received bad key", __func__);
1471 BN_clear_free(ssh1_challenge);
1472 ssh1_challenge = auth_rsa_generate_challenge(key);
1475 buffer_put_bignum2(m, ssh1_challenge);
1477 debug3("%s sending reply", __func__);
1478 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1480 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1488 mm_answer_rsa_response(int sock, Buffer *m)
1491 u_char *blob, *response;
1495 debug3("%s entering", __func__);
1497 if (!authctxt->valid)
1498 fatal("%s: authctxt not valid", __func__);
1499 if (ssh1_challenge == NULL)
1500 fatal("%s: no ssh1_challenge", __func__);
1502 blob = buffer_get_string(m, &blen);
1503 if (!monitor_allowed_key(blob, blen))
1504 fatal("%s: bad key, not previously allowed", __func__);
1505 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1506 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1507 if ((key = key_from_blob(blob, blen)) == NULL)
1508 fatal("%s: received bad key", __func__);
1509 response = buffer_get_string(m, &len);
1511 fatal("%s: received bad response to challenge", __func__);
1512 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1518 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1521 BN_clear_free(ssh1_challenge);
1522 ssh1_challenge = NULL;
1523 monitor_reset_key_state();
1526 buffer_put_int(m, success);
1527 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1533 mm_answer_term(int sock, Buffer *req)
1535 extern struct monitor *pmonitor;
1538 debug3("%s: tearing down sessions", __func__);
1540 /* The child is terminating */
1541 session_destroy_all(&mm_session_close);
1543 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1547 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1549 /* Terminate process */
1553 #ifdef SSH_AUDIT_EVENTS
1554 /* Report that an audit event occurred */
1556 mm_answer_audit_event(int socket, Buffer *m)
1558 ssh_audit_event_t event;
1560 debug3("%s entering", __func__);
1562 event = buffer_get_int(m);
1564 case SSH_AUTH_FAIL_PUBKEY:
1565 case SSH_AUTH_FAIL_HOSTBASED:
1566 case SSH_AUTH_FAIL_GSSAPI:
1567 case SSH_LOGIN_EXCEED_MAXTRIES:
1568 case SSH_LOGIN_ROOT_DENIED:
1569 case SSH_CONNECTION_CLOSE:
1570 case SSH_INVALID_USER:
1574 fatal("Audit event type %d not permitted", event);
1581 mm_answer_audit_command(int socket, Buffer *m)
1586 debug3("%s entering", __func__);
1587 cmd = buffer_get_string(m, &len);
1588 /* sanity check command, if so how? */
1589 audit_run_command(cmd);
1593 #endif /* SSH_AUDIT_EVENTS */
1596 monitor_apply_keystate(struct monitor *pmonitor)
1599 set_newkeys(MODE_IN);
1600 set_newkeys(MODE_OUT);
1602 packet_set_protocol_flags(child_state.ssh1protoflags);
1603 packet_set_encryption_key(child_state.ssh1key,
1604 child_state.ssh1keylen, child_state.ssh1cipher);
1605 xfree(child_state.ssh1key);
1608 /* for rc4 and other stateful ciphers */
1609 packet_set_keycontext(MODE_OUT, child_state.keyout);
1610 xfree(child_state.keyout);
1611 packet_set_keycontext(MODE_IN, child_state.keyin);
1612 xfree(child_state.keyin);
1615 packet_set_iv(MODE_OUT, child_state.ivout);
1616 xfree(child_state.ivout);
1617 packet_set_iv(MODE_IN, child_state.ivin);
1618 xfree(child_state.ivin);
1621 memcpy(&incoming_stream, &child_state.incoming,
1622 sizeof(incoming_stream));
1623 memcpy(&outgoing_stream, &child_state.outgoing,
1624 sizeof(outgoing_stream));
1626 /* Update with new address */
1627 if (options.compression)
1628 mm_init_compression(pmonitor->m_zlib);
1630 /* Network I/O buffers */
1631 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1632 buffer_clear(&input);
1633 buffer_append(&input, child_state.input, child_state.ilen);
1634 memset(child_state.input, 0, child_state.ilen);
1635 xfree(child_state.input);
1637 buffer_clear(&output);
1638 buffer_append(&output, child_state.output, child_state.olen);
1639 memset(child_state.output, 0, child_state.olen);
1640 xfree(child_state.output);
1644 mm_get_kex(Buffer *m)
1650 kex = xmalloc(sizeof(*kex));
1651 memset(kex, 0, sizeof(*kex));
1652 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1653 if ((session_id2 == NULL) ||
1654 (kex->session_id_len != session_id2_len) ||
1655 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1656 fatal("mm_get_get: internal error: bad session id");
1657 kex->we_need = buffer_get_int(m);
1658 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1659 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1660 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1662 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_server;
1663 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_server;
1666 kex->hostkey_type = buffer_get_int(m);
1667 kex->kex_type = buffer_get_int(m);
1668 blob = buffer_get_string(m, &bloblen);
1669 buffer_init(&kex->my);
1670 buffer_append(&kex->my, blob, bloblen);
1672 blob = buffer_get_string(m, &bloblen);
1673 buffer_init(&kex->peer);
1674 buffer_append(&kex->peer, blob, bloblen);
1677 kex->flags = buffer_get_int(m);
1678 kex->client_version_string = buffer_get_string(m, NULL);
1679 kex->server_version_string = buffer_get_string(m, NULL);
1680 kex->load_host_key=&get_hostkey_by_type;
1681 kex->host_key_index=&get_hostkey_index;
1686 /* This function requries careful sanity checking */
1689 mm_get_keystate(struct monitor *pmonitor)
1693 u_int bloblen, plen;
1694 u_int32_t seqnr, packets;
1697 debug3("%s: Waiting for new keys", __func__);
1700 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1702 child_state.ssh1protoflags = buffer_get_int(&m);
1703 child_state.ssh1cipher = buffer_get_int(&m);
1704 child_state.ssh1key = buffer_get_string(&m,
1705 &child_state.ssh1keylen);
1706 child_state.ivout = buffer_get_string(&m,
1707 &child_state.ivoutlen);
1708 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1711 /* Get the Kex for rekeying */
1712 *pmonitor->m_pkex = mm_get_kex(&m);
1715 blob = buffer_get_string(&m, &bloblen);
1716 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1719 debug3("%s: Waiting for second key", __func__);
1720 blob = buffer_get_string(&m, &bloblen);
1721 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1724 /* Now get sequence numbers for the packets */
1725 seqnr = buffer_get_int(&m);
1726 blocks = buffer_get_int64(&m);
1727 packets = buffer_get_int(&m);
1728 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1729 seqnr = buffer_get_int(&m);
1730 blocks = buffer_get_int64(&m);
1731 packets = buffer_get_int(&m);
1732 packet_set_state(MODE_IN, seqnr, blocks, packets);
1735 /* Get the key context */
1736 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1737 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1739 debug3("%s: Getting compression state", __func__);
1740 /* Get compression state */
1741 p = buffer_get_string(&m, &plen);
1742 if (plen != sizeof(child_state.outgoing))
1743 fatal("%s: bad request size", __func__);
1744 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1747 p = buffer_get_string(&m, &plen);
1748 if (plen != sizeof(child_state.incoming))
1749 fatal("%s: bad request size", __func__);
1750 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1753 /* Network I/O buffers */
1754 debug3("%s: Getting Network I/O buffers", __func__);
1755 child_state.input = buffer_get_string(&m, &child_state.ilen);
1756 child_state.output = buffer_get_string(&m, &child_state.olen);
1762 /* Allocation functions for zlib */
1764 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1766 size_t len = (size_t) size * ncount;
1769 if (len == 0 || ncount > SIZE_T_MAX / size)
1770 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1772 address = mm_malloc(mm, len);
1778 mm_zfree(struct mm_master *mm, void *address)
1780 mm_free(mm, address);
1784 mm_init_compression(struct mm_master *mm)
1786 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1787 outgoing_stream.zfree = (free_func)mm_zfree;
1788 outgoing_stream.opaque = mm;
1790 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1791 incoming_stream.zfree = (free_func)mm_zfree;
1792 incoming_stream.opaque = mm;
1797 #define FD_CLOSEONEXEC(x) do { \
1798 if (fcntl(x, F_SETFD, 1) == -1) \
1799 fatal("fcntl(%d, F_SETFD)", x); \
1803 monitor_socketpair(int *pair)
1805 #ifdef HAVE_SOCKETPAIR
1806 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1807 fatal("%s: socketpair", __func__);
1809 fatal("%s: UsePrivilegeSeparation=yes not supported",
1812 FD_CLOSEONEXEC(pair[0]);
1813 FD_CLOSEONEXEC(pair[1]);
1816 #define MM_MEMSIZE 65536
1821 struct monitor *mon;
1824 mon = xmalloc(sizeof(*mon));
1827 monitor_socketpair(pair);
1829 mon->m_recvfd = pair[0];
1830 mon->m_sendfd = pair[1];
1832 /* Used to share zlib space across processes */
1833 if (options.compression) {
1834 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1835 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1837 /* Compression needs to share state across borders */
1838 mm_init_compression(mon->m_zlib);
1845 monitor_reinit(struct monitor *mon)
1849 monitor_socketpair(pair);
1851 mon->m_recvfd = pair[0];
1852 mon->m_sendfd = pair[1];
1857 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1863 goid.elements = buffer_get_string(m, &len);
1866 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1868 xfree(goid.elements);
1871 buffer_put_int(m, major);
1873 mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
1875 /* Now we have a context, enable the step */
1876 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1882 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1885 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1886 OM_uint32 major, minor;
1887 OM_uint32 flags = 0; /* GSI needs this */
1890 in.value = buffer_get_string(m, &len);
1892 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1896 buffer_put_int(m, major);
1897 buffer_put_string(m, out.value, out.length);
1898 buffer_put_int(m, flags);
1899 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1901 gss_release_buffer(&minor, &out);
1903 if (major == GSS_S_COMPLETE) {
1904 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1905 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1906 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1907 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1908 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN, 1);
1914 mm_answer_gss_checkmic(int sock, Buffer *m)
1916 gss_buffer_desc gssbuf, mic;
1920 gssbuf.value = buffer_get_string(m, &len);
1921 gssbuf.length = len;
1922 mic.value = buffer_get_string(m, &len);
1925 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1927 xfree(gssbuf.value);
1931 buffer_put_int(m, ret);
1933 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1935 if (!GSS_ERROR(ret))
1936 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1942 mm_answer_gss_userok(int sock, Buffer *m)
1946 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1949 buffer_put_int(m, authenticated);
1951 debug3("%s: sending result %d", __func__, authenticated);
1952 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1954 auth_method = "gssapi-with-mic";
1956 /* Monitor loop will terminate if authenticated */
1957 return (authenticated);
1961 mm_answer_gss_sign(int socket, Buffer *m)
1963 gss_buffer_desc data;
1964 gss_buffer_desc hash = GSS_C_EMPTY_BUFFER;
1965 OM_uint32 major, minor;
1968 data.value = buffer_get_string(m, &len);
1970 if (data.length != 20)
1971 fatal("%s: data length incorrect: %d", __func__, data.length);
1973 /* Save the session ID on the first time around */
1974 if (session_id2_len == 0) {
1975 session_id2_len = data.length;
1976 session_id2 = xmalloc(session_id2_len);
1977 memcpy(session_id2, data.value, session_id2_len);
1979 major = ssh_gssapi_sign(gsscontext, &data, &hash);
1984 buffer_put_int(m, major);
1985 buffer_put_string(m, hash.value, hash.length);
1987 mm_request_send(socket, MONITOR_ANS_GSSSIGN, m);
1989 gss_release_buffer(&minor, &hash);
1991 /* Turn on getpwnam permissions */
1992 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1998 mm_answer_gss_error(int socket, Buffer *m) {
1999 OM_uint32 major,minor;
2002 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
2004 buffer_put_int(m,major);
2005 buffer_put_int(m,minor);
2006 buffer_put_cstring(m,msg);
2008 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
2016 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
2017 OM_uint32 major,minor;
2018 gss_OID_set mech_set;
2021 major=gss_indicate_mechs(&minor, &mech_set);
2024 buffer_put_int(m, major);
2025 buffer_put_int(m, mech_set->count);
2026 for (i=0; i < mech_set->count; i++) {
2027 buffer_put_string(m, mech_set->elements[i].elements,
2028 mech_set->elements[i].length);
2031 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
2032 gss_release_oid_set(&minor,&mech_set);
2035 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
2041 mm_answer_gss_localname(int socket, Buffer *m) {
2044 ssh_gssapi_localname(&name);
2048 buffer_put_cstring(m, name);
2049 debug3("%s: sending result %s", __func__, name);
2052 buffer_put_cstring(m, "");
2053 debug3("%s: sending result \"\"", __func__);
2056 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);