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.55 2004/02/05 05:37:17 dtucker Exp $");
30 #include <openssl/dh.h>
36 #include "zlib.h" /* XXX Moved due to conflict on MacOS/X */
43 #include "auth-options.h"
52 #include "monitor_mm.h"
53 #include "monitor_wrap.h"
54 #include "monitor_fdpass.h"
65 static Gssctxt *gsscontext = NULL;
69 extern ServerOptions options;
70 extern u_int utmp_len;
71 extern Newkeys *current_keys[];
72 extern z_stream incoming_stream;
73 extern z_stream outgoing_stream;
74 extern u_char session_id[];
75 extern Buffer input, output;
76 extern Buffer auth_debug;
77 extern int auth_debug_init;
79 /* State exported from the child */
102 /* Functions on the monitor that answer unprivileged requests */
104 int mm_answer_moduli(int, Buffer *);
105 int mm_answer_sign(int, Buffer *);
106 int mm_answer_pwnamallow(int, Buffer *);
107 int mm_answer_auth2_read_banner(int, Buffer *);
108 int mm_answer_authserv(int, Buffer *);
109 int mm_answer_authpassword(int, Buffer *);
110 int mm_answer_bsdauthquery(int, Buffer *);
111 int mm_answer_bsdauthrespond(int, Buffer *);
112 int mm_answer_skeyquery(int, Buffer *);
113 int mm_answer_skeyrespond(int, Buffer *);
114 int mm_answer_keyallowed(int, Buffer *);
115 int mm_answer_keyverify(int, Buffer *);
116 int mm_answer_pty(int, Buffer *);
117 int mm_answer_pty_cleanup(int, Buffer *);
118 int mm_answer_term(int, Buffer *);
119 int mm_answer_rsa_keyallowed(int, Buffer *);
120 int mm_answer_rsa_challenge(int, Buffer *);
121 int mm_answer_rsa_response(int, Buffer *);
122 int mm_answer_sesskey(int, Buffer *);
123 int mm_answer_sessid(int, Buffer *);
126 int mm_answer_pam_start(int, Buffer *);
127 int mm_answer_pam_account(int, Buffer *);
128 int mm_answer_pam_init_ctx(int, Buffer *);
129 int mm_answer_pam_query(int, Buffer *);
130 int mm_answer_pam_respond(int, Buffer *);
131 int mm_answer_pam_free_ctx(int, Buffer *);
135 int mm_answer_gss_setup_ctx(int, Buffer *);
136 int mm_answer_gss_accept_ctx(int, Buffer *);
137 int mm_answer_gss_userok(int, Buffer *);
138 int mm_answer_gss_checkmic(int, Buffer *);
141 static Authctxt *authctxt;
142 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
144 /* local state for key verify */
145 static u_char *key_blob = NULL;
146 static u_int key_bloblen = 0;
147 static int key_blobtype = MM_NOKEY;
148 static char *hostbased_cuser = NULL;
149 static char *hostbased_chost = NULL;
150 static char *auth_method = "unknown";
151 static u_int session_id2_len = 0;
152 static u_char *session_id2 = NULL;
153 static pid_t monitor_child_pid;
156 enum monitor_reqtype type;
158 int (*f)(int, Buffer *);
161 #define MON_ISAUTH 0x0004 /* Required for Authentication */
162 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
163 #define MON_ONCE 0x0010 /* Disable after calling */
165 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
167 #define MON_PERMIT 0x1000 /* Request is permitted */
169 struct mon_table mon_dispatch_proto20[] = {
170 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
171 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
172 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
173 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
174 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
175 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
177 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
178 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
179 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
180 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
181 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
182 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
185 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
186 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
189 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
190 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
192 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
193 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
195 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
196 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
197 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
198 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
203 struct mon_table mon_dispatch_postauth20[] = {
204 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
205 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
206 {MONITOR_REQ_PTY, 0, mm_answer_pty},
207 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
208 {MONITOR_REQ_TERM, 0, mm_answer_term},
212 struct mon_table mon_dispatch_proto15[] = {
213 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
214 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
215 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
216 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
217 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
218 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
219 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
220 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
222 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
223 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
226 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
227 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
230 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
231 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
232 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
233 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
234 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
235 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
240 struct mon_table mon_dispatch_postauth15[] = {
241 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
242 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
243 {MONITOR_REQ_TERM, 0, mm_answer_term},
247 struct mon_table *mon_dispatch;
249 /* Specifies if a certain message is allowed at the moment */
252 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
254 while (ent->f != NULL) {
255 if (ent->type == type) {
256 ent->flags &= ~MON_PERMIT;
257 ent->flags |= permit ? MON_PERMIT : 0;
265 monitor_permit_authentications(int permit)
267 struct mon_table *ent = mon_dispatch;
269 while (ent->f != NULL) {
270 if (ent->flags & MON_AUTH) {
271 ent->flags &= ~MON_PERMIT;
272 ent->flags |= permit ? MON_PERMIT : 0;
279 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
281 struct mon_table *ent;
282 int authenticated = 0;
284 debug3("preauth child monitor started");
286 authctxt = _authctxt;
287 memset(authctxt, 0, sizeof(*authctxt));
290 mon_dispatch = mon_dispatch_proto20;
292 /* Permit requests for moduli and signatures */
293 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
294 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
296 mon_dispatch = mon_dispatch_proto15;
298 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
301 /* The first few requests do not require asynchronous access */
302 while (!authenticated) {
303 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
305 if (!(ent->flags & MON_AUTHDECIDE))
306 fatal("%s: unexpected authentication from %d",
307 __func__, ent->type);
308 if (authctxt->pw->pw_uid == 0 &&
309 !auth_root_allowed(auth_method))
312 /* PAM needs to perform account checks after auth */
313 if (options.use_pam && authenticated) {
317 mm_request_receive_expect(pmonitor->m_sendfd,
318 MONITOR_REQ_PAM_ACCOUNT, &m);
319 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
325 if (ent->flags & MON_AUTHDECIDE) {
326 auth_log(authctxt, authenticated, auth_method,
327 compat20 ? " ssh2" : "");
329 authctxt->failures++;
333 if (!authctxt->valid)
334 fatal("%s: authenticated invalid user", __func__);
336 debug("%s: %s has been authenticated by privileged process",
337 __func__, authctxt->user);
339 mm_get_keystate(pmonitor);
343 monitor_set_child_handler(pid_t pid)
345 monitor_child_pid = pid;
349 monitor_child_handler(int signal)
351 kill(monitor_child_pid, signal);
355 monitor_child_postauth(struct monitor *pmonitor)
357 monitor_set_child_handler(pmonitor->m_pid);
358 signal(SIGHUP, &monitor_child_handler);
359 signal(SIGTERM, &monitor_child_handler);
362 mon_dispatch = mon_dispatch_postauth20;
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);
367 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
369 mon_dispatch = mon_dispatch_postauth15;
370 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
373 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
374 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
378 monitor_read(pmonitor, mon_dispatch, NULL);
382 monitor_sync(struct monitor *pmonitor)
384 if (options.compression) {
385 /* The member allocation is not visible, so sync it */
386 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
391 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
392 struct mon_table **pent)
400 mm_request_receive(pmonitor->m_sendfd, &m);
401 type = buffer_get_char(&m);
403 debug3("%s: checking request %d", __func__, type);
405 while (ent->f != NULL) {
406 if (ent->type == type)
411 if (ent->f != NULL) {
412 if (!(ent->flags & MON_PERMIT))
413 fatal("%s: unpermitted request %d", __func__,
415 ret = (*ent->f)(pmonitor->m_sendfd, &m);
418 /* The child may use this request only once, disable it */
419 if (ent->flags & MON_ONCE) {
420 debug2("%s: %d used once, disabling now", __func__,
422 ent->flags &= ~MON_PERMIT;
431 fatal("%s: unsupported request: %d", __func__, type);
437 /* allowed key state */
439 monitor_allowed_key(u_char *blob, u_int bloblen)
441 /* make sure key is allowed */
442 if (key_blob == NULL || key_bloblen != bloblen ||
443 memcmp(key_blob, blob, key_bloblen))
449 monitor_reset_key_state(void)
452 if (key_blob != NULL)
454 if (hostbased_cuser != NULL)
455 xfree(hostbased_cuser);
456 if (hostbased_chost != NULL)
457 xfree(hostbased_chost);
460 key_blobtype = MM_NOKEY;
461 hostbased_cuser = NULL;
462 hostbased_chost = NULL;
466 mm_answer_moduli(int socket, Buffer *m)
471 min = buffer_get_int(m);
472 want = buffer_get_int(m);
473 max = buffer_get_int(m);
475 debug3("%s: got parameters: %d %d %d",
476 __func__, min, want, max);
477 /* We need to check here, too, in case the child got corrupted */
478 if (max < min || want < min || max < want)
479 fatal("%s: bad parameters: %d %d %d",
480 __func__, min, want, max);
484 dh = choose_dh(min, want, max);
486 buffer_put_char(m, 0);
489 /* Send first bignum */
490 buffer_put_char(m, 1);
491 buffer_put_bignum2(m, dh->p);
492 buffer_put_bignum2(m, dh->g);
496 mm_request_send(socket, MONITOR_ANS_MODULI, m);
501 mm_answer_sign(int socket, Buffer *m)
506 u_int siglen, datlen;
509 debug3("%s", __func__);
511 keyid = buffer_get_int(m);
512 p = buffer_get_string(m, &datlen);
515 fatal("%s: data length incorrect: %u", __func__, datlen);
517 /* save session id, it will be passed on the first call */
518 if (session_id2_len == 0) {
519 session_id2_len = datlen;
520 session_id2 = xmalloc(session_id2_len);
521 memcpy(session_id2, p, session_id2_len);
524 if ((key = get_hostkey_by_index(keyid)) == NULL)
525 fatal("%s: no hostkey from index %d", __func__, keyid);
526 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
527 fatal("%s: key_sign failed", __func__);
529 debug3("%s: signature %p(%u)", __func__, signature, siglen);
532 buffer_put_string(m, signature, siglen);
537 mm_request_send(socket, MONITOR_ANS_SIGN, m);
539 /* Turn on permissions for getpwnam */
540 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
545 /* Retrieves the password entry and also checks if the user is permitted */
548 mm_answer_pwnamallow(int socket, Buffer *m)
551 struct passwd *pwent;
554 debug3("%s", __func__);
556 if (authctxt->attempt++ != 0)
557 fatal("%s: multiple attempts for getpwnam", __func__);
559 login = buffer_get_string(m, NULL);
561 pwent = getpwnamallow(login);
563 authctxt->user = xstrdup(login);
564 setproctitle("%s [priv]", pwent ? login : "unknown");
570 buffer_put_char(m, 0);
571 authctxt->pw = fakepw();
576 authctxt->pw = pwent;
579 buffer_put_char(m, 1);
580 buffer_put_string(m, pwent, sizeof(struct passwd));
581 buffer_put_cstring(m, pwent->pw_name);
582 buffer_put_cstring(m, "*");
583 buffer_put_cstring(m, pwent->pw_gecos);
584 #ifdef HAVE_PW_CLASS_IN_PASSWD
585 buffer_put_cstring(m, pwent->pw_class);
587 buffer_put_cstring(m, pwent->pw_dir);
588 buffer_put_cstring(m, pwent->pw_shell);
591 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
592 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
594 /* For SSHv1 allow authentication now */
596 monitor_permit_authentications(1);
598 /* Allow service/style information on the auth context */
599 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
600 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
605 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
611 int mm_answer_auth2_read_banner(int socket, Buffer *m)
616 banner = auth2_read_banner();
617 buffer_put_cstring(m, banner != NULL ? banner : "");
618 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
627 mm_answer_authserv(int socket, Buffer *m)
629 monitor_permit_authentications(1);
631 authctxt->service = buffer_get_string(m, NULL);
632 authctxt->style = buffer_get_string(m, NULL);
633 debug3("%s: service=%s, style=%s",
634 __func__, authctxt->service, authctxt->style);
636 if (strlen(authctxt->style) == 0) {
637 xfree(authctxt->style);
638 authctxt->style = NULL;
645 mm_answer_authpassword(int socket, Buffer *m)
647 static int call_count;
652 passwd = buffer_get_string(m, &plen);
653 /* Only authenticate if the context is valid */
654 authenticated = options.password_authentication &&
655 auth_password(authctxt, passwd);
656 memset(passwd, 0, strlen(passwd));
660 buffer_put_int(m, authenticated);
662 debug3("%s: sending result %d", __func__, authenticated);
663 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
666 if (plen == 0 && call_count == 1)
667 auth_method = "none";
669 auth_method = "password";
671 /* Causes monitor loop to terminate if authenticated */
672 return (authenticated);
677 mm_answer_bsdauthquery(int socket, Buffer *m)
679 char *name, *infotxt;
685 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
686 &prompts, &echo_on) < 0 ? 0 : 1;
689 buffer_put_int(m, success);
691 buffer_put_cstring(m, prompts[0]);
693 debug3("%s: sending challenge success: %u", __func__, success);
694 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
707 mm_answer_bsdauthrespond(int socket, Buffer *m)
712 if (authctxt->as == 0)
713 fatal("%s: no bsd auth session", __func__);
715 response = buffer_get_string(m, NULL);
716 authok = options.challenge_response_authentication &&
717 auth_userresponse(authctxt->as, response, 0);
719 debug3("%s: <%s> = <%d>", __func__, response, authok);
723 buffer_put_int(m, authok);
725 debug3("%s: sending authenticated: %d", __func__, authok);
726 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
728 auth_method = "bsdauth";
730 return (authok != 0);
736 mm_answer_skeyquery(int socket, Buffer *m)
739 char challenge[1024];
742 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
745 buffer_put_int(m, success);
747 buffer_put_cstring(m, challenge);
749 debug3("%s: sending challenge success: %u", __func__, success);
750 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
756 mm_answer_skeyrespond(int socket, Buffer *m)
761 response = buffer_get_string(m, NULL);
763 authok = (options.challenge_response_authentication &&
765 skey_haskey(authctxt->pw->pw_name) == 0 &&
766 skey_passcheck(authctxt->pw->pw_name, response) != -1);
771 buffer_put_int(m, authok);
773 debug3("%s: sending authenticated: %d", __func__, authok);
774 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
776 auth_method = "skey";
778 return (authok != 0);
784 mm_answer_pam_start(int socket, Buffer *m)
786 if (!options.use_pam)
787 fatal("UsePAM not set, but ended up in %s anyway", __func__);
791 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
797 mm_answer_pam_account(int socket, Buffer *m)
801 if (!options.use_pam)
802 fatal("UsePAM not set, but ended up in %s anyway", __func__);
804 ret = do_pam_account();
806 buffer_put_int(m, ret);
808 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
813 static void *sshpam_ctxt, *sshpam_authok;
814 extern KbdintDevice sshpam_device;
817 mm_answer_pam_init_ctx(int socket, Buffer *m)
820 debug3("%s", __func__);
821 authctxt->user = buffer_get_string(m, NULL);
822 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
823 sshpam_authok = NULL;
825 if (sshpam_ctxt != NULL) {
826 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
827 buffer_put_int(m, 1);
829 buffer_put_int(m, 0);
831 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
836 mm_answer_pam_query(int socket, Buffer *m)
838 char *name, *info, **prompts;
842 debug3("%s", __func__);
843 sshpam_authok = NULL;
844 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
845 if (ret == 0 && num == 0)
846 sshpam_authok = sshpam_ctxt;
847 if (num > 1 || name == NULL || info == NULL)
850 buffer_put_int(m, ret);
851 buffer_put_cstring(m, name);
853 buffer_put_cstring(m, info);
855 buffer_put_int(m, num);
856 for (i = 0; i < num; ++i) {
857 buffer_put_cstring(m, prompts[i]);
859 buffer_put_int(m, echo_on[i]);
865 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
870 mm_answer_pam_respond(int socket, Buffer *m)
876 debug3("%s", __func__);
877 sshpam_authok = NULL;
878 num = buffer_get_int(m);
880 resp = xmalloc(num * sizeof(char *));
881 for (i = 0; i < num; ++i)
882 resp[i] = buffer_get_string(m, NULL);
883 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
884 for (i = 0; i < num; ++i)
888 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
891 buffer_put_int(m, ret);
892 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
893 auth_method = "keyboard-interactive/pam";
895 sshpam_authok = sshpam_ctxt;
900 mm_answer_pam_free_ctx(int socket, Buffer *m)
903 debug3("%s", __func__);
904 (sshpam_device.free_ctx)(sshpam_ctxt);
906 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
907 return (sshpam_authok == sshpam_ctxt);
912 mm_append_debug(Buffer *m)
914 if (auth_debug_init && buffer_len(&auth_debug)) {
915 debug3("%s: Appending debug messages for child", __func__);
916 buffer_append(m, buffer_ptr(&auth_debug),
917 buffer_len(&auth_debug));
918 buffer_clear(&auth_debug);
923 mm_answer_keyallowed(int socket, Buffer *m)
929 enum mm_keytype type = 0;
932 debug3("%s entering", __func__);
934 type = buffer_get_int(m);
935 cuser = buffer_get_string(m, NULL);
936 chost = buffer_get_string(m, NULL);
937 blob = buffer_get_string(m, &bloblen);
939 key = key_from_blob(blob, bloblen);
941 if ((compat20 && type == MM_RSAHOSTKEY) ||
942 (!compat20 && type != MM_RSAHOSTKEY))
943 fatal("%s: key type and protocol mismatch", __func__);
945 debug3("%s: key_from_blob: %p", __func__, key);
947 if (key != NULL && authctxt->valid) {
950 allowed = options.pubkey_authentication &&
951 user_key_allowed(authctxt->pw, key);
954 allowed = options.hostbased_authentication &&
955 hostbased_key_allowed(authctxt->pw,
959 key->type = KEY_RSA1; /* XXX */
960 allowed = options.rhosts_rsa_authentication &&
961 auth_rhosts_rsa_key_allowed(authctxt->pw,
965 fatal("%s: unknown key type %d", __func__, type);
972 /* clear temporarily storage (used by verify) */
973 monitor_reset_key_state();
976 /* Save temporarily for comparison in verify */
978 key_bloblen = bloblen;
980 hostbased_cuser = cuser;
981 hostbased_chost = chost;
984 debug3("%s: key %p is %s",
985 __func__, key, allowed ? "allowed" : "disallowed");
988 buffer_put_int(m, allowed);
989 buffer_put_int(m, forced_command != NULL);
993 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
995 if (type == MM_RSAHOSTKEY)
996 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1002 monitor_valid_userblob(u_char *data, u_int datalen)
1010 buffer_append(&b, data, datalen);
1012 if (datafellows & SSH_OLD_SESSIONID) {
1014 len = buffer_len(&b);
1015 if ((session_id2 == NULL) ||
1016 (len < session_id2_len) ||
1017 (memcmp(p, session_id2, session_id2_len) != 0))
1019 buffer_consume(&b, session_id2_len);
1021 p = buffer_get_string(&b, &len);
1022 if ((session_id2 == NULL) ||
1023 (len != session_id2_len) ||
1024 (memcmp(p, session_id2, session_id2_len) != 0))
1028 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1030 p = buffer_get_string(&b, NULL);
1031 if (strcmp(authctxt->user, p) != 0) {
1032 logit("wrong user name passed to monitor: expected %s != %.100s",
1037 buffer_skip_string(&b);
1038 if (datafellows & SSH_BUG_PKAUTH) {
1039 if (!buffer_get_char(&b))
1042 p = buffer_get_string(&b, NULL);
1043 if (strcmp("publickey", p) != 0)
1046 if (!buffer_get_char(&b))
1048 buffer_skip_string(&b);
1050 buffer_skip_string(&b);
1051 if (buffer_len(&b) != 0)
1058 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1067 buffer_append(&b, data, datalen);
1069 p = buffer_get_string(&b, &len);
1070 if ((session_id2 == NULL) ||
1071 (len != session_id2_len) ||
1072 (memcmp(p, session_id2, session_id2_len) != 0))
1076 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1078 p = buffer_get_string(&b, NULL);
1079 if (strcmp(authctxt->user, p) != 0) {
1080 logit("wrong user name passed to monitor: expected %s != %.100s",
1085 buffer_skip_string(&b); /* service */
1086 p = buffer_get_string(&b, NULL);
1087 if (strcmp(p, "hostbased") != 0)
1090 buffer_skip_string(&b); /* pkalg */
1091 buffer_skip_string(&b); /* pkblob */
1093 /* verify client host, strip trailing dot if necessary */
1094 p = buffer_get_string(&b, NULL);
1095 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1097 if (strcmp(p, chost) != 0)
1101 /* verify client user */
1102 p = buffer_get_string(&b, NULL);
1103 if (strcmp(p, cuser) != 0)
1107 if (buffer_len(&b) != 0)
1114 mm_answer_keyverify(int socket, Buffer *m)
1117 u_char *signature, *data, *blob;
1118 u_int signaturelen, datalen, bloblen;
1122 blob = buffer_get_string(m, &bloblen);
1123 signature = buffer_get_string(m, &signaturelen);
1124 data = buffer_get_string(m, &datalen);
1126 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1127 !monitor_allowed_key(blob, bloblen))
1128 fatal("%s: bad key, not previously allowed", __func__);
1130 key = key_from_blob(blob, bloblen);
1132 fatal("%s: bad public key blob", __func__);
1134 switch (key_blobtype) {
1136 valid_data = monitor_valid_userblob(data, datalen);
1139 valid_data = monitor_valid_hostbasedblob(data, datalen,
1140 hostbased_cuser, hostbased_chost);
1147 fatal("%s: bad signature data blob", __func__);
1149 verified = key_verify(key, signature, signaturelen, data, datalen);
1150 debug3("%s: key %p signature %s",
1151 __func__, key, verified ? "verified" : "unverified");
1158 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1160 monitor_reset_key_state();
1163 buffer_put_int(m, verified);
1164 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1170 mm_record_login(Session *s, struct passwd *pw)
1173 struct sockaddr_storage from;
1176 * Get IP address of client. If the connection is not a socket, let
1177 * the address be 0.0.0.0.
1179 memset(&from, 0, sizeof(from));
1180 fromlen = sizeof(from);
1181 if (packet_connection_is_on_socket()) {
1182 if (getpeername(packet_get_connection_in(),
1183 (struct sockaddr *) & from, &fromlen) < 0) {
1184 debug("getpeername: %.100s", strerror(errno));
1188 /* Record that there was a login on that tty from the remote host. */
1189 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1190 get_remote_name_or_ip(utmp_len, options.use_dns),
1191 (struct sockaddr *)&from, fromlen);
1195 mm_session_close(Session *s)
1197 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1198 if (s->ttyfd != -1) {
1199 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1200 session_pty_cleanup2(s);
1206 mm_answer_pty(int socket, Buffer *m)
1208 extern struct monitor *pmonitor;
1212 debug3("%s entering", __func__);
1218 s->authctxt = authctxt;
1219 s->pw = authctxt->pw;
1220 s->pid = pmonitor->m_pid;
1221 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1224 pty_setowner(authctxt->pw, s->tty);
1226 buffer_put_int(m, 1);
1227 buffer_put_cstring(m, s->tty);
1228 mm_request_send(socket, MONITOR_ANS_PTY, m);
1230 mm_send_fd(socket, s->ptyfd);
1231 mm_send_fd(socket, s->ttyfd);
1233 /* We need to trick ttyslot */
1234 if (dup2(s->ttyfd, 0) == -1)
1235 fatal("%s: dup2", __func__);
1237 mm_record_login(s, authctxt->pw);
1239 /* Now we can close the file descriptor again */
1242 /* make sure nothing uses fd 0 */
1243 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1244 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1246 error("%s: fd0 %d != 0", __func__, fd0);
1248 /* slave is not needed */
1250 s->ttyfd = s->ptyfd;
1251 /* no need to dup() because nobody closes ptyfd */
1252 s->ptymaster = s->ptyfd;
1254 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1260 mm_session_close(s);
1261 buffer_put_int(m, 0);
1262 mm_request_send(socket, MONITOR_ANS_PTY, m);
1267 mm_answer_pty_cleanup(int socket, Buffer *m)
1272 debug3("%s entering", __func__);
1274 tty = buffer_get_string(m, NULL);
1275 if ((s = session_by_tty(tty)) != NULL)
1276 mm_session_close(s);
1283 mm_answer_sesskey(int socket, Buffer *m)
1288 /* Turn off permissions */
1289 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1291 if ((p = BN_new()) == NULL)
1292 fatal("%s: BN_new", __func__);
1294 buffer_get_bignum2(m, p);
1296 rsafail = ssh1_session_key(p);
1299 buffer_put_int(m, rsafail);
1300 buffer_put_bignum2(m, p);
1304 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1306 /* Turn on permissions for sessid passing */
1307 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1313 mm_answer_sessid(int socket, Buffer *m)
1317 debug3("%s entering", __func__);
1319 if (buffer_len(m) != 16)
1320 fatal("%s: bad ssh1 session id", __func__);
1321 for (i = 0; i < 16; i++)
1322 session_id[i] = buffer_get_char(m);
1324 /* Turn on permissions for getpwnam */
1325 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1331 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1335 u_char *blob = NULL;
1339 debug3("%s entering", __func__);
1341 if (options.rsa_authentication && authctxt->valid) {
1342 if ((client_n = BN_new()) == NULL)
1343 fatal("%s: BN_new", __func__);
1344 buffer_get_bignum2(m, client_n);
1345 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1346 BN_clear_free(client_n);
1349 buffer_put_int(m, allowed);
1350 buffer_put_int(m, forced_command != NULL);
1352 /* clear temporarily storage (used by generate challenge) */
1353 monitor_reset_key_state();
1355 if (allowed && key != NULL) {
1356 key->type = KEY_RSA; /* cheat for key_to_blob */
1357 if (key_to_blob(key, &blob, &blen) == 0)
1358 fatal("%s: key_to_blob failed", __func__);
1359 buffer_put_string(m, blob, blen);
1361 /* Save temporarily for comparison in verify */
1364 key_blobtype = MM_RSAUSERKEY;
1371 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1373 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1374 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1379 mm_answer_rsa_challenge(int socket, Buffer *m)
1385 debug3("%s entering", __func__);
1387 if (!authctxt->valid)
1388 fatal("%s: authctxt not valid", __func__);
1389 blob = buffer_get_string(m, &blen);
1390 if (!monitor_allowed_key(blob, blen))
1391 fatal("%s: bad key, not previously allowed", __func__);
1392 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1393 fatal("%s: key type mismatch", __func__);
1394 if ((key = key_from_blob(blob, blen)) == NULL)
1395 fatal("%s: received bad key", __func__);
1398 BN_clear_free(ssh1_challenge);
1399 ssh1_challenge = auth_rsa_generate_challenge(key);
1402 buffer_put_bignum2(m, ssh1_challenge);
1404 debug3("%s sending reply", __func__);
1405 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1407 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1415 mm_answer_rsa_response(int socket, Buffer *m)
1418 u_char *blob, *response;
1422 debug3("%s entering", __func__);
1424 if (!authctxt->valid)
1425 fatal("%s: authctxt not valid", __func__);
1426 if (ssh1_challenge == NULL)
1427 fatal("%s: no ssh1_challenge", __func__);
1429 blob = buffer_get_string(m, &blen);
1430 if (!monitor_allowed_key(blob, blen))
1431 fatal("%s: bad key, not previously allowed", __func__);
1432 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1433 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1434 if ((key = key_from_blob(blob, blen)) == NULL)
1435 fatal("%s: received bad key", __func__);
1436 response = buffer_get_string(m, &len);
1438 fatal("%s: received bad response to challenge", __func__);
1439 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1445 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1448 BN_clear_free(ssh1_challenge);
1449 ssh1_challenge = NULL;
1450 monitor_reset_key_state();
1453 buffer_put_int(m, success);
1454 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1460 mm_answer_term(int socket, Buffer *req)
1462 extern struct monitor *pmonitor;
1465 debug3("%s: tearing down sessions", __func__);
1467 /* The child is terminating */
1468 session_destroy_all(&mm_session_close);
1470 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1474 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1476 /* Terminate process */
1481 monitor_apply_keystate(struct monitor *pmonitor)
1484 set_newkeys(MODE_IN);
1485 set_newkeys(MODE_OUT);
1487 packet_set_protocol_flags(child_state.ssh1protoflags);
1488 packet_set_encryption_key(child_state.ssh1key,
1489 child_state.ssh1keylen, child_state.ssh1cipher);
1490 xfree(child_state.ssh1key);
1493 /* for rc4 and other stateful ciphers */
1494 packet_set_keycontext(MODE_OUT, child_state.keyout);
1495 xfree(child_state.keyout);
1496 packet_set_keycontext(MODE_IN, child_state.keyin);
1497 xfree(child_state.keyin);
1500 packet_set_iv(MODE_OUT, child_state.ivout);
1501 xfree(child_state.ivout);
1502 packet_set_iv(MODE_IN, child_state.ivin);
1503 xfree(child_state.ivin);
1506 memcpy(&incoming_stream, &child_state.incoming,
1507 sizeof(incoming_stream));
1508 memcpy(&outgoing_stream, &child_state.outgoing,
1509 sizeof(outgoing_stream));
1511 /* Update with new address */
1512 if (options.compression)
1513 mm_init_compression(pmonitor->m_zlib);
1515 /* Network I/O buffers */
1516 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1517 buffer_clear(&input);
1518 buffer_append(&input, child_state.input, child_state.ilen);
1519 memset(child_state.input, 0, child_state.ilen);
1520 xfree(child_state.input);
1522 buffer_clear(&output);
1523 buffer_append(&output, child_state.output, child_state.olen);
1524 memset(child_state.output, 0, child_state.olen);
1525 xfree(child_state.output);
1529 mm_get_kex(Buffer *m)
1535 kex = xmalloc(sizeof(*kex));
1536 memset(kex, 0, sizeof(*kex));
1537 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1538 if ((session_id2 == NULL) ||
1539 (kex->session_id_len != session_id2_len) ||
1540 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1541 fatal("mm_get_get: internal error: bad session id");
1542 kex->we_need = buffer_get_int(m);
1543 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1544 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1546 kex->hostkey_type = buffer_get_int(m);
1547 kex->kex_type = buffer_get_int(m);
1548 blob = buffer_get_string(m, &bloblen);
1549 buffer_init(&kex->my);
1550 buffer_append(&kex->my, blob, bloblen);
1552 blob = buffer_get_string(m, &bloblen);
1553 buffer_init(&kex->peer);
1554 buffer_append(&kex->peer, blob, bloblen);
1557 kex->flags = buffer_get_int(m);
1558 kex->client_version_string = buffer_get_string(m, NULL);
1559 kex->server_version_string = buffer_get_string(m, NULL);
1560 kex->load_host_key=&get_hostkey_by_type;
1561 kex->host_key_index=&get_hostkey_index;
1566 /* This function requries careful sanity checking */
1569 mm_get_keystate(struct monitor *pmonitor)
1573 u_int bloblen, plen;
1574 u_int32_t seqnr, packets;
1577 debug3("%s: Waiting for new keys", __func__);
1580 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1582 child_state.ssh1protoflags = buffer_get_int(&m);
1583 child_state.ssh1cipher = buffer_get_int(&m);
1584 child_state.ssh1key = buffer_get_string(&m,
1585 &child_state.ssh1keylen);
1586 child_state.ivout = buffer_get_string(&m,
1587 &child_state.ivoutlen);
1588 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1591 /* Get the Kex for rekeying */
1592 *pmonitor->m_pkex = mm_get_kex(&m);
1595 blob = buffer_get_string(&m, &bloblen);
1596 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1599 debug3("%s: Waiting for second key", __func__);
1600 blob = buffer_get_string(&m, &bloblen);
1601 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1604 /* Now get sequence numbers for the packets */
1605 seqnr = buffer_get_int(&m);
1606 blocks = buffer_get_int64(&m);
1607 packets = buffer_get_int(&m);
1608 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1609 seqnr = buffer_get_int(&m);
1610 blocks = buffer_get_int64(&m);
1611 packets = buffer_get_int(&m);
1612 packet_set_state(MODE_IN, seqnr, blocks, packets);
1615 /* Get the key context */
1616 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1617 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1619 debug3("%s: Getting compression state", __func__);
1620 /* Get compression state */
1621 p = buffer_get_string(&m, &plen);
1622 if (plen != sizeof(child_state.outgoing))
1623 fatal("%s: bad request size", __func__);
1624 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1627 p = buffer_get_string(&m, &plen);
1628 if (plen != sizeof(child_state.incoming))
1629 fatal("%s: bad request size", __func__);
1630 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1633 /* Network I/O buffers */
1634 debug3("%s: Getting Network I/O buffers", __func__);
1635 child_state.input = buffer_get_string(&m, &child_state.ilen);
1636 child_state.output = buffer_get_string(&m, &child_state.olen);
1642 /* Allocation functions for zlib */
1644 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1646 size_t len = (size_t) size * ncount;
1649 if (len == 0 || ncount > SIZE_T_MAX / size)
1650 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1652 address = mm_malloc(mm, len);
1658 mm_zfree(struct mm_master *mm, void *address)
1660 mm_free(mm, address);
1664 mm_init_compression(struct mm_master *mm)
1666 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1667 outgoing_stream.zfree = (free_func)mm_zfree;
1668 outgoing_stream.opaque = mm;
1670 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1671 incoming_stream.zfree = (free_func)mm_zfree;
1672 incoming_stream.opaque = mm;
1677 #define FD_CLOSEONEXEC(x) do { \
1678 if (fcntl(x, F_SETFD, 1) == -1) \
1679 fatal("fcntl(%d, F_SETFD)", x); \
1683 monitor_socketpair(int *pair)
1685 #ifdef HAVE_SOCKETPAIR
1686 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1687 fatal("%s: socketpair", __func__);
1689 fatal("%s: UsePrivilegeSeparation=yes not supported",
1692 FD_CLOSEONEXEC(pair[0]);
1693 FD_CLOSEONEXEC(pair[1]);
1696 #define MM_MEMSIZE 65536
1701 struct monitor *mon;
1704 mon = xmalloc(sizeof(*mon));
1707 monitor_socketpair(pair);
1709 mon->m_recvfd = pair[0];
1710 mon->m_sendfd = pair[1];
1712 /* Used to share zlib space across processes */
1713 if (options.compression) {
1714 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1715 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1717 /* Compression needs to share state across borders */
1718 mm_init_compression(mon->m_zlib);
1725 monitor_reinit(struct monitor *mon)
1729 monitor_socketpair(pair);
1731 mon->m_recvfd = pair[0];
1732 mon->m_sendfd = pair[1];
1737 mm_answer_gss_setup_ctx(int socket, Buffer *m)
1743 oid.elements = buffer_get_string(m, &len);
1746 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1748 xfree(oid.elements);
1751 buffer_put_int(m, major);
1753 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1755 /* Now we have a context, enable the step */
1756 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1762 mm_answer_gss_accept_ctx(int socket, Buffer *m)
1765 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1766 OM_uint32 major,minor;
1767 OM_uint32 flags = 0; /* GSI needs this */
1770 in.value = buffer_get_string(m, &len);
1772 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1776 buffer_put_int(m, major);
1777 buffer_put_string(m, out.value, out.length);
1778 buffer_put_int(m, flags);
1779 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1781 gss_release_buffer(&minor, &out);
1783 if (major==GSS_S_COMPLETE) {
1784 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1785 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1786 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1792 mm_answer_gss_checkmic(int socket, Buffer *m)
1794 gss_buffer_desc gssbuf, mic;
1798 gssbuf.value = buffer_get_string(m, &len);
1799 gssbuf.length = len;
1800 mic.value = buffer_get_string(m, &len);
1803 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1805 xfree(gssbuf.value);
1809 buffer_put_int(m, ret);
1811 mm_request_send(socket, MONITOR_ANS_GSSCHECKMIC, m);
1813 if (!GSS_ERROR(ret))
1814 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1820 mm_answer_gss_userok(int socket, Buffer *m)
1824 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1827 buffer_put_int(m, authenticated);
1829 debug3("%s: sending result %d", __func__, authenticated);
1830 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1832 auth_method="gssapi-with-mic";
1834 /* Monitor loop will terminate if authenticated */
1835 return (authenticated);