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.61 2004/07/17 05:31:41 dtucker 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 *);
146 static Authctxt *authctxt;
147 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
149 /* local state for key verify */
150 static u_char *key_blob = NULL;
151 static u_int key_bloblen = 0;
152 static int key_blobtype = MM_NOKEY;
153 static char *hostbased_cuser = NULL;
154 static char *hostbased_chost = NULL;
155 static char *auth_method = "unknown";
156 static u_int session_id2_len = 0;
157 static u_char *session_id2 = NULL;
158 static pid_t monitor_child_pid;
161 enum monitor_reqtype type;
163 int (*f)(int, Buffer *);
166 #define MON_ISAUTH 0x0004 /* Required for Authentication */
167 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
168 #define MON_ONCE 0x0010 /* Disable after calling */
170 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
172 #define MON_PERMIT 0x1000 /* Request is permitted */
174 struct mon_table mon_dispatch_proto20[] = {
175 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
176 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
177 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
178 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
179 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
180 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
182 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
183 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
184 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
185 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
186 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
187 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
190 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
191 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
194 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
195 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
197 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
198 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
200 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
201 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
202 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
203 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
208 struct mon_table mon_dispatch_postauth20[] = {
209 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
210 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
211 {MONITOR_REQ_PTY, 0, mm_answer_pty},
212 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
213 {MONITOR_REQ_TERM, 0, mm_answer_term},
217 struct mon_table mon_dispatch_proto15[] = {
218 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
219 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
220 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
221 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
222 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
223 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
224 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
225 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
227 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
228 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
231 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
232 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
235 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
236 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
237 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
238 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
239 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
240 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
245 struct mon_table mon_dispatch_postauth15[] = {
246 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
247 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
248 {MONITOR_REQ_TERM, 0, mm_answer_term},
252 struct mon_table *mon_dispatch;
254 /* Specifies if a certain message is allowed at the moment */
257 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
259 while (ent->f != NULL) {
260 if (ent->type == type) {
261 ent->flags &= ~MON_PERMIT;
262 ent->flags |= permit ? MON_PERMIT : 0;
270 monitor_permit_authentications(int permit)
272 struct mon_table *ent = mon_dispatch;
274 while (ent->f != NULL) {
275 if (ent->flags & MON_AUTH) {
276 ent->flags &= ~MON_PERMIT;
277 ent->flags |= permit ? MON_PERMIT : 0;
284 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
286 struct mon_table *ent;
287 int authenticated = 0;
289 debug3("preauth child monitor started");
291 authctxt = _authctxt;
292 memset(authctxt, 0, sizeof(*authctxt));
295 mon_dispatch = mon_dispatch_proto20;
297 /* Permit requests for moduli and signatures */
298 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
299 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
301 mon_dispatch = mon_dispatch_proto15;
303 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
306 /* The first few requests do not require asynchronous access */
307 while (!authenticated) {
308 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
310 if (!(ent->flags & MON_AUTHDECIDE))
311 fatal("%s: unexpected authentication from %d",
312 __func__, ent->type);
313 if (authctxt->pw->pw_uid == 0 &&
314 !auth_root_allowed(auth_method))
317 /* PAM needs to perform account checks after auth */
318 if (options.use_pam && authenticated) {
322 mm_request_receive_expect(pmonitor->m_sendfd,
323 MONITOR_REQ_PAM_ACCOUNT, &m);
324 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
330 if (ent->flags & MON_AUTHDECIDE) {
331 auth_log(authctxt, authenticated, auth_method,
332 compat20 ? " ssh2" : "");
334 authctxt->failures++;
338 if (!authctxt->valid)
339 fatal("%s: authenticated invalid user", __func__);
341 debug("%s: %s has been authenticated by privileged process",
342 __func__, authctxt->user);
344 mm_get_keystate(pmonitor);
348 monitor_set_child_handler(pid_t pid)
350 monitor_child_pid = pid;
354 monitor_child_handler(int sig)
356 kill(monitor_child_pid, sig);
360 monitor_child_postauth(struct monitor *pmonitor)
362 monitor_set_child_handler(pmonitor->m_pid);
363 signal(SIGHUP, &monitor_child_handler);
364 signal(SIGTERM, &monitor_child_handler);
367 mon_dispatch = mon_dispatch_postauth20;
369 /* Permit requests for moduli and signatures */
370 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
371 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
372 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
374 mon_dispatch = mon_dispatch_postauth15;
375 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
378 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
379 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
383 monitor_read(pmonitor, mon_dispatch, NULL);
387 monitor_sync(struct monitor *pmonitor)
389 if (options.compression) {
390 /* The member allocation is not visible, so sync it */
391 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
396 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
397 struct mon_table **pent)
405 mm_request_receive(pmonitor->m_sendfd, &m);
406 type = buffer_get_char(&m);
408 debug3("%s: checking request %d", __func__, type);
410 while (ent->f != NULL) {
411 if (ent->type == type)
416 if (ent->f != NULL) {
417 if (!(ent->flags & MON_PERMIT))
418 fatal("%s: unpermitted request %d", __func__,
420 ret = (*ent->f)(pmonitor->m_sendfd, &m);
423 /* The child may use this request only once, disable it */
424 if (ent->flags & MON_ONCE) {
425 debug2("%s: %d used once, disabling now", __func__,
427 ent->flags &= ~MON_PERMIT;
436 fatal("%s: unsupported request: %d", __func__, type);
442 /* allowed key state */
444 monitor_allowed_key(u_char *blob, u_int bloblen)
446 /* make sure key is allowed */
447 if (key_blob == NULL || key_bloblen != bloblen ||
448 memcmp(key_blob, blob, key_bloblen))
454 monitor_reset_key_state(void)
457 if (key_blob != NULL)
459 if (hostbased_cuser != NULL)
460 xfree(hostbased_cuser);
461 if (hostbased_chost != NULL)
462 xfree(hostbased_chost);
465 key_blobtype = MM_NOKEY;
466 hostbased_cuser = NULL;
467 hostbased_chost = NULL;
471 mm_answer_moduli(int sock, Buffer *m)
476 min = buffer_get_int(m);
477 want = buffer_get_int(m);
478 max = buffer_get_int(m);
480 debug3("%s: got parameters: %d %d %d",
481 __func__, min, want, max);
482 /* We need to check here, too, in case the child got corrupted */
483 if (max < min || want < min || max < want)
484 fatal("%s: bad parameters: %d %d %d",
485 __func__, min, want, max);
489 dh = choose_dh(min, want, max);
491 buffer_put_char(m, 0);
494 /* Send first bignum */
495 buffer_put_char(m, 1);
496 buffer_put_bignum2(m, dh->p);
497 buffer_put_bignum2(m, dh->g);
501 mm_request_send(sock, MONITOR_ANS_MODULI, m);
506 mm_answer_sign(int sock, Buffer *m)
511 u_int siglen, datlen;
514 debug3("%s", __func__);
516 keyid = buffer_get_int(m);
517 p = buffer_get_string(m, &datlen);
520 fatal("%s: data length incorrect: %u", __func__, datlen);
522 /* save session id, it will be passed on the first call */
523 if (session_id2_len == 0) {
524 session_id2_len = datlen;
525 session_id2 = xmalloc(session_id2_len);
526 memcpy(session_id2, p, session_id2_len);
529 if ((key = get_hostkey_by_index(keyid)) == NULL)
530 fatal("%s: no hostkey from index %d", __func__, keyid);
531 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
532 fatal("%s: key_sign failed", __func__);
534 debug3("%s: signature %p(%u)", __func__, signature, siglen);
537 buffer_put_string(m, signature, siglen);
542 mm_request_send(sock, MONITOR_ANS_SIGN, m);
544 /* Turn on permissions for getpwnam */
545 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
550 /* Retrieves the password entry and also checks if the user is permitted */
553 mm_answer_pwnamallow(int sock, Buffer *m)
556 struct passwd *pwent;
559 debug3("%s", __func__);
561 if (authctxt->attempt++ != 0)
562 fatal("%s: multiple attempts for getpwnam", __func__);
564 username = buffer_get_string(m, NULL);
566 pwent = getpwnamallow(username);
568 authctxt->user = xstrdup(username);
569 setproctitle("%s [priv]", pwent ? username : "unknown");
575 buffer_put_char(m, 0);
576 authctxt->pw = fakepw();
581 authctxt->pw = pwent;
584 buffer_put_char(m, 1);
585 buffer_put_string(m, pwent, sizeof(struct passwd));
586 buffer_put_cstring(m, pwent->pw_name);
587 buffer_put_cstring(m, "*");
588 buffer_put_cstring(m, pwent->pw_gecos);
589 #ifdef HAVE_PW_CLASS_IN_PASSWD
590 buffer_put_cstring(m, pwent->pw_class);
592 buffer_put_cstring(m, pwent->pw_dir);
593 buffer_put_cstring(m, pwent->pw_shell);
596 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
597 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
599 /* For SSHv1 allow authentication now */
601 monitor_permit_authentications(1);
603 /* Allow service/style information on the auth context */
604 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
605 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
610 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
616 int mm_answer_auth2_read_banner(int sock, Buffer *m)
621 banner = auth2_read_banner();
622 buffer_put_cstring(m, banner != NULL ? banner : "");
623 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
632 mm_answer_authserv(int sock, Buffer *m)
634 monitor_permit_authentications(1);
636 authctxt->service = buffer_get_string(m, NULL);
637 authctxt->style = buffer_get_string(m, NULL);
638 debug3("%s: service=%s, style=%s",
639 __func__, authctxt->service, authctxt->style);
641 if (strlen(authctxt->style) == 0) {
642 xfree(authctxt->style);
643 authctxt->style = NULL;
650 mm_answer_authpassword(int sock, Buffer *m)
652 static int call_count;
657 passwd = buffer_get_string(m, &plen);
658 /* Only authenticate if the context is valid */
659 authenticated = options.password_authentication &&
660 auth_password(authctxt, passwd);
661 memset(passwd, 0, strlen(passwd));
665 buffer_put_int(m, authenticated);
667 debug3("%s: sending result %d", __func__, authenticated);
668 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
671 if (plen == 0 && call_count == 1)
672 auth_method = "none";
674 auth_method = "password";
676 /* Causes monitor loop to terminate if authenticated */
677 return (authenticated);
682 mm_answer_bsdauthquery(int sock, Buffer *m)
684 char *name, *infotxt;
690 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
691 &prompts, &echo_on) < 0 ? 0 : 1;
694 buffer_put_int(m, success);
696 buffer_put_cstring(m, prompts[0]);
698 debug3("%s: sending challenge success: %u", __func__, success);
699 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
712 mm_answer_bsdauthrespond(int sock, Buffer *m)
717 if (authctxt->as == 0)
718 fatal("%s: no bsd auth session", __func__);
720 response = buffer_get_string(m, NULL);
721 authok = options.challenge_response_authentication &&
722 auth_userresponse(authctxt->as, response, 0);
724 debug3("%s: <%s> = <%d>", __func__, response, authok);
728 buffer_put_int(m, authok);
730 debug3("%s: sending authenticated: %d", __func__, authok);
731 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
733 auth_method = "bsdauth";
735 return (authok != 0);
741 mm_answer_skeyquery(int sock, Buffer *m)
744 char challenge[1024];
747 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
748 sizeof(challenge)) < 0 ? 0 : 1;
751 buffer_put_int(m, success);
753 buffer_put_cstring(m, challenge);
755 debug3("%s: sending challenge success: %u", __func__, success);
756 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
762 mm_answer_skeyrespond(int sock, Buffer *m)
767 response = buffer_get_string(m, NULL);
769 authok = (options.challenge_response_authentication &&
771 skey_haskey(authctxt->pw->pw_name) == 0 &&
772 skey_passcheck(authctxt->pw->pw_name, response) != -1);
777 buffer_put_int(m, authok);
779 debug3("%s: sending authenticated: %d", __func__, authok);
780 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
782 auth_method = "skey";
784 return (authok != 0);
790 mm_answer_pam_start(int sock, Buffer *m)
792 if (!options.use_pam)
793 fatal("UsePAM not set, but ended up in %s anyway", __func__);
797 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
803 mm_answer_pam_account(int sock, Buffer *m)
807 if (!options.use_pam)
808 fatal("UsePAM not set, but ended up in %s anyway", __func__);
810 ret = do_pam_account();
812 buffer_put_int(m, ret);
814 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
819 static void *sshpam_ctxt, *sshpam_authok;
820 extern KbdintDevice sshpam_device;
823 mm_answer_pam_init_ctx(int sock, Buffer *m)
826 debug3("%s", __func__);
827 authctxt->user = buffer_get_string(m, NULL);
828 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
829 sshpam_authok = NULL;
831 if (sshpam_ctxt != NULL) {
832 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
833 buffer_put_int(m, 1);
835 buffer_put_int(m, 0);
837 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
842 mm_answer_pam_query(int sock, Buffer *m)
844 char *name, *info, **prompts;
848 debug3("%s", __func__);
849 sshpam_authok = NULL;
850 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
851 if (ret == 0 && num == 0)
852 sshpam_authok = sshpam_ctxt;
853 if (num > 1 || name == NULL || info == NULL)
856 buffer_put_int(m, ret);
857 buffer_put_cstring(m, name);
859 buffer_put_cstring(m, info);
861 buffer_put_int(m, num);
862 for (i = 0; i < num; ++i) {
863 buffer_put_cstring(m, prompts[i]);
865 buffer_put_int(m, echo_on[i]);
871 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
876 mm_answer_pam_respond(int sock, Buffer *m)
882 debug3("%s", __func__);
883 sshpam_authok = NULL;
884 num = buffer_get_int(m);
886 resp = xmalloc(num * sizeof(char *));
887 for (i = 0; i < num; ++i)
888 resp[i] = buffer_get_string(m, NULL);
889 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
890 for (i = 0; i < num; ++i)
894 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
897 buffer_put_int(m, ret);
898 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
899 auth_method = "keyboard-interactive/pam";
901 sshpam_authok = sshpam_ctxt;
906 mm_answer_pam_free_ctx(int sock, Buffer *m)
909 debug3("%s", __func__);
910 (sshpam_device.free_ctx)(sshpam_ctxt);
912 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
913 return (sshpam_authok == sshpam_ctxt);
918 mm_append_debug(Buffer *m)
920 if (auth_debug_init && buffer_len(&auth_debug)) {
921 debug3("%s: Appending debug messages for child", __func__);
922 buffer_append(m, buffer_ptr(&auth_debug),
923 buffer_len(&auth_debug));
924 buffer_clear(&auth_debug);
929 mm_answer_keyallowed(int sock, Buffer *m)
935 enum mm_keytype type = 0;
938 debug3("%s entering", __func__);
940 type = buffer_get_int(m);
941 cuser = buffer_get_string(m, NULL);
942 chost = buffer_get_string(m, NULL);
943 blob = buffer_get_string(m, &bloblen);
945 key = key_from_blob(blob, bloblen);
947 if ((compat20 && type == MM_RSAHOSTKEY) ||
948 (!compat20 && type != MM_RSAHOSTKEY))
949 fatal("%s: key type and protocol mismatch", __func__);
951 debug3("%s: key_from_blob: %p", __func__, key);
953 if (key != NULL && authctxt->valid) {
956 allowed = options.pubkey_authentication &&
957 user_key_allowed(authctxt->pw, key);
960 allowed = options.hostbased_authentication &&
961 hostbased_key_allowed(authctxt->pw,
965 key->type = KEY_RSA1; /* XXX */
966 allowed = options.rhosts_rsa_authentication &&
967 auth_rhosts_rsa_key_allowed(authctxt->pw,
971 fatal("%s: unknown key type %d", __func__, type);
978 /* clear temporarily storage (used by verify) */
979 monitor_reset_key_state();
982 /* Save temporarily for comparison in verify */
984 key_bloblen = bloblen;
986 hostbased_cuser = cuser;
987 hostbased_chost = chost;
990 debug3("%s: key %p is %s",
991 __func__, key, allowed ? "allowed" : "disallowed");
994 buffer_put_int(m, allowed);
995 buffer_put_int(m, forced_command != NULL);
999 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1001 if (type == MM_RSAHOSTKEY)
1002 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1008 monitor_valid_userblob(u_char *data, u_int datalen)
1016 buffer_append(&b, data, datalen);
1018 if (datafellows & SSH_OLD_SESSIONID) {
1020 len = buffer_len(&b);
1021 if ((session_id2 == NULL) ||
1022 (len < session_id2_len) ||
1023 (memcmp(p, session_id2, session_id2_len) != 0))
1025 buffer_consume(&b, session_id2_len);
1027 p = buffer_get_string(&b, &len);
1028 if ((session_id2 == NULL) ||
1029 (len != session_id2_len) ||
1030 (memcmp(p, session_id2, session_id2_len) != 0))
1034 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1036 p = buffer_get_string(&b, NULL);
1037 if (strcmp(authctxt->user, p) != 0) {
1038 logit("wrong user name passed to monitor: expected %s != %.100s",
1043 buffer_skip_string(&b);
1044 if (datafellows & SSH_BUG_PKAUTH) {
1045 if (!buffer_get_char(&b))
1048 p = buffer_get_string(&b, NULL);
1049 if (strcmp("publickey", p) != 0)
1052 if (!buffer_get_char(&b))
1054 buffer_skip_string(&b);
1056 buffer_skip_string(&b);
1057 if (buffer_len(&b) != 0)
1064 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1073 buffer_append(&b, data, datalen);
1075 p = buffer_get_string(&b, &len);
1076 if ((session_id2 == NULL) ||
1077 (len != session_id2_len) ||
1078 (memcmp(p, session_id2, session_id2_len) != 0))
1082 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1084 p = buffer_get_string(&b, NULL);
1085 if (strcmp(authctxt->user, p) != 0) {
1086 logit("wrong user name passed to monitor: expected %s != %.100s",
1091 buffer_skip_string(&b); /* service */
1092 p = buffer_get_string(&b, NULL);
1093 if (strcmp(p, "hostbased") != 0)
1096 buffer_skip_string(&b); /* pkalg */
1097 buffer_skip_string(&b); /* pkblob */
1099 /* verify client host, strip trailing dot if necessary */
1100 p = buffer_get_string(&b, NULL);
1101 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1103 if (strcmp(p, chost) != 0)
1107 /* verify client user */
1108 p = buffer_get_string(&b, NULL);
1109 if (strcmp(p, cuser) != 0)
1113 if (buffer_len(&b) != 0)
1120 mm_answer_keyverify(int sock, Buffer *m)
1123 u_char *signature, *data, *blob;
1124 u_int signaturelen, datalen, bloblen;
1128 blob = buffer_get_string(m, &bloblen);
1129 signature = buffer_get_string(m, &signaturelen);
1130 data = buffer_get_string(m, &datalen);
1132 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1133 !monitor_allowed_key(blob, bloblen))
1134 fatal("%s: bad key, not previously allowed", __func__);
1136 key = key_from_blob(blob, bloblen);
1138 fatal("%s: bad public key blob", __func__);
1140 switch (key_blobtype) {
1142 valid_data = monitor_valid_userblob(data, datalen);
1145 valid_data = monitor_valid_hostbasedblob(data, datalen,
1146 hostbased_cuser, hostbased_chost);
1153 fatal("%s: bad signature data blob", __func__);
1155 verified = key_verify(key, signature, signaturelen, data, datalen);
1156 debug3("%s: key %p signature %s",
1157 __func__, key, verified ? "verified" : "unverified");
1164 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1166 monitor_reset_key_state();
1169 buffer_put_int(m, verified);
1170 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1176 mm_record_login(Session *s, struct passwd *pw)
1179 struct sockaddr_storage from;
1182 * Get IP address of client. If the connection is not a socket, let
1183 * the address be 0.0.0.0.
1185 memset(&from, 0, sizeof(from));
1186 fromlen = sizeof(from);
1187 if (packet_connection_is_on_socket()) {
1188 if (getpeername(packet_get_connection_in(),
1189 (struct sockaddr *) & from, &fromlen) < 0) {
1190 debug("getpeername: %.100s", strerror(errno));
1194 /* Record that there was a login on that tty from the remote host. */
1195 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1196 get_remote_name_or_ip(utmp_len, options.use_dns),
1197 (struct sockaddr *)&from, fromlen);
1201 mm_session_close(Session *s)
1203 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1204 if (s->ttyfd != -1) {
1205 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1206 session_pty_cleanup2(s);
1212 mm_answer_pty(int sock, Buffer *m)
1214 extern struct monitor *pmonitor;
1218 debug3("%s entering", __func__);
1224 s->authctxt = authctxt;
1225 s->pw = authctxt->pw;
1226 s->pid = pmonitor->m_pid;
1227 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1230 pty_setowner(authctxt->pw, s->tty);
1232 buffer_put_int(m, 1);
1233 buffer_put_cstring(m, s->tty);
1235 /* We need to trick ttyslot */
1236 if (dup2(s->ttyfd, 0) == -1)
1237 fatal("%s: dup2", __func__);
1239 mm_record_login(s, authctxt->pw);
1241 /* Now we can close the file descriptor again */
1244 /* send messages generated by record_login */
1245 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1246 buffer_clear(&loginmsg);
1248 mm_request_send(sock, MONITOR_ANS_PTY, m);
1250 mm_send_fd(sock, s->ptyfd);
1251 mm_send_fd(sock, s->ttyfd);
1253 /* make sure nothing uses fd 0 */
1254 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1255 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1257 error("%s: fd0 %d != 0", __func__, fd0);
1259 /* slave is not needed */
1261 s->ttyfd = s->ptyfd;
1262 /* no need to dup() because nobody closes ptyfd */
1263 s->ptymaster = s->ptyfd;
1265 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1271 mm_session_close(s);
1272 buffer_put_int(m, 0);
1273 mm_request_send(sock, MONITOR_ANS_PTY, m);
1278 mm_answer_pty_cleanup(int sock, Buffer *m)
1283 debug3("%s entering", __func__);
1285 tty = buffer_get_string(m, NULL);
1286 if ((s = session_by_tty(tty)) != NULL)
1287 mm_session_close(s);
1294 mm_answer_sesskey(int sock, Buffer *m)
1299 /* Turn off permissions */
1300 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1302 if ((p = BN_new()) == NULL)
1303 fatal("%s: BN_new", __func__);
1305 buffer_get_bignum2(m, p);
1307 rsafail = ssh1_session_key(p);
1310 buffer_put_int(m, rsafail);
1311 buffer_put_bignum2(m, p);
1315 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1317 /* Turn on permissions for sessid passing */
1318 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1324 mm_answer_sessid(int sock, Buffer *m)
1328 debug3("%s entering", __func__);
1330 if (buffer_len(m) != 16)
1331 fatal("%s: bad ssh1 session id", __func__);
1332 for (i = 0; i < 16; i++)
1333 session_id[i] = buffer_get_char(m);
1335 /* Turn on permissions for getpwnam */
1336 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1342 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1346 u_char *blob = NULL;
1350 debug3("%s entering", __func__);
1352 if (options.rsa_authentication && authctxt->valid) {
1353 if ((client_n = BN_new()) == NULL)
1354 fatal("%s: BN_new", __func__);
1355 buffer_get_bignum2(m, client_n);
1356 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1357 BN_clear_free(client_n);
1360 buffer_put_int(m, allowed);
1361 buffer_put_int(m, forced_command != NULL);
1363 /* clear temporarily storage (used by generate challenge) */
1364 monitor_reset_key_state();
1366 if (allowed && key != NULL) {
1367 key->type = KEY_RSA; /* cheat for key_to_blob */
1368 if (key_to_blob(key, &blob, &blen) == 0)
1369 fatal("%s: key_to_blob failed", __func__);
1370 buffer_put_string(m, blob, blen);
1372 /* Save temporarily for comparison in verify */
1375 key_blobtype = MM_RSAUSERKEY;
1382 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1384 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1385 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1390 mm_answer_rsa_challenge(int sock, Buffer *m)
1396 debug3("%s entering", __func__);
1398 if (!authctxt->valid)
1399 fatal("%s: authctxt not valid", __func__);
1400 blob = buffer_get_string(m, &blen);
1401 if (!monitor_allowed_key(blob, blen))
1402 fatal("%s: bad key, not previously allowed", __func__);
1403 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1404 fatal("%s: key type mismatch", __func__);
1405 if ((key = key_from_blob(blob, blen)) == NULL)
1406 fatal("%s: received bad key", __func__);
1409 BN_clear_free(ssh1_challenge);
1410 ssh1_challenge = auth_rsa_generate_challenge(key);
1413 buffer_put_bignum2(m, ssh1_challenge);
1415 debug3("%s sending reply", __func__);
1416 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1418 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1426 mm_answer_rsa_response(int sock, Buffer *m)
1429 u_char *blob, *response;
1433 debug3("%s entering", __func__);
1435 if (!authctxt->valid)
1436 fatal("%s: authctxt not valid", __func__);
1437 if (ssh1_challenge == NULL)
1438 fatal("%s: no ssh1_challenge", __func__);
1440 blob = buffer_get_string(m, &blen);
1441 if (!monitor_allowed_key(blob, blen))
1442 fatal("%s: bad key, not previously allowed", __func__);
1443 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1444 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1445 if ((key = key_from_blob(blob, blen)) == NULL)
1446 fatal("%s: received bad key", __func__);
1447 response = buffer_get_string(m, &len);
1449 fatal("%s: received bad response to challenge", __func__);
1450 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1456 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1459 BN_clear_free(ssh1_challenge);
1460 ssh1_challenge = NULL;
1461 monitor_reset_key_state();
1464 buffer_put_int(m, success);
1465 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1471 mm_answer_term(int sock, Buffer *req)
1473 extern struct monitor *pmonitor;
1476 debug3("%s: tearing down sessions", __func__);
1478 /* The child is terminating */
1479 session_destroy_all(&mm_session_close);
1481 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1485 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1487 /* Terminate process */
1492 monitor_apply_keystate(struct monitor *pmonitor)
1495 set_newkeys(MODE_IN);
1496 set_newkeys(MODE_OUT);
1498 packet_set_protocol_flags(child_state.ssh1protoflags);
1499 packet_set_encryption_key(child_state.ssh1key,
1500 child_state.ssh1keylen, child_state.ssh1cipher);
1501 xfree(child_state.ssh1key);
1504 /* for rc4 and other stateful ciphers */
1505 packet_set_keycontext(MODE_OUT, child_state.keyout);
1506 xfree(child_state.keyout);
1507 packet_set_keycontext(MODE_IN, child_state.keyin);
1508 xfree(child_state.keyin);
1511 packet_set_iv(MODE_OUT, child_state.ivout);
1512 xfree(child_state.ivout);
1513 packet_set_iv(MODE_IN, child_state.ivin);
1514 xfree(child_state.ivin);
1517 memcpy(&incoming_stream, &child_state.incoming,
1518 sizeof(incoming_stream));
1519 memcpy(&outgoing_stream, &child_state.outgoing,
1520 sizeof(outgoing_stream));
1522 /* Update with new address */
1523 if (options.compression)
1524 mm_init_compression(pmonitor->m_zlib);
1526 /* Network I/O buffers */
1527 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1528 buffer_clear(&input);
1529 buffer_append(&input, child_state.input, child_state.ilen);
1530 memset(child_state.input, 0, child_state.ilen);
1531 xfree(child_state.input);
1533 buffer_clear(&output);
1534 buffer_append(&output, child_state.output, child_state.olen);
1535 memset(child_state.output, 0, child_state.olen);
1536 xfree(child_state.output);
1540 mm_get_kex(Buffer *m)
1546 kex = xmalloc(sizeof(*kex));
1547 memset(kex, 0, sizeof(*kex));
1548 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1549 if ((session_id2 == NULL) ||
1550 (kex->session_id_len != session_id2_len) ||
1551 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1552 fatal("mm_get_get: internal error: bad session id");
1553 kex->we_need = buffer_get_int(m);
1554 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1555 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1556 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1558 kex->hostkey_type = buffer_get_int(m);
1559 kex->kex_type = buffer_get_int(m);
1560 blob = buffer_get_string(m, &bloblen);
1561 buffer_init(&kex->my);
1562 buffer_append(&kex->my, blob, bloblen);
1564 blob = buffer_get_string(m, &bloblen);
1565 buffer_init(&kex->peer);
1566 buffer_append(&kex->peer, blob, bloblen);
1569 kex->flags = buffer_get_int(m);
1570 kex->client_version_string = buffer_get_string(m, NULL);
1571 kex->server_version_string = buffer_get_string(m, NULL);
1572 kex->load_host_key=&get_hostkey_by_type;
1573 kex->host_key_index=&get_hostkey_index;
1578 /* This function requries careful sanity checking */
1581 mm_get_keystate(struct monitor *pmonitor)
1585 u_int bloblen, plen;
1586 u_int32_t seqnr, packets;
1589 debug3("%s: Waiting for new keys", __func__);
1592 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1594 child_state.ssh1protoflags = buffer_get_int(&m);
1595 child_state.ssh1cipher = buffer_get_int(&m);
1596 child_state.ssh1key = buffer_get_string(&m,
1597 &child_state.ssh1keylen);
1598 child_state.ivout = buffer_get_string(&m,
1599 &child_state.ivoutlen);
1600 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1603 /* Get the Kex for rekeying */
1604 *pmonitor->m_pkex = mm_get_kex(&m);
1607 blob = buffer_get_string(&m, &bloblen);
1608 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1611 debug3("%s: Waiting for second key", __func__);
1612 blob = buffer_get_string(&m, &bloblen);
1613 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1616 /* Now get sequence numbers for the packets */
1617 seqnr = buffer_get_int(&m);
1618 blocks = buffer_get_int64(&m);
1619 packets = buffer_get_int(&m);
1620 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1621 seqnr = buffer_get_int(&m);
1622 blocks = buffer_get_int64(&m);
1623 packets = buffer_get_int(&m);
1624 packet_set_state(MODE_IN, seqnr, blocks, packets);
1627 /* Get the key context */
1628 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1629 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1631 debug3("%s: Getting compression state", __func__);
1632 /* Get compression state */
1633 p = buffer_get_string(&m, &plen);
1634 if (plen != sizeof(child_state.outgoing))
1635 fatal("%s: bad request size", __func__);
1636 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1639 p = buffer_get_string(&m, &plen);
1640 if (plen != sizeof(child_state.incoming))
1641 fatal("%s: bad request size", __func__);
1642 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1645 /* Network I/O buffers */
1646 debug3("%s: Getting Network I/O buffers", __func__);
1647 child_state.input = buffer_get_string(&m, &child_state.ilen);
1648 child_state.output = buffer_get_string(&m, &child_state.olen);
1654 /* Allocation functions for zlib */
1656 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1658 size_t len = (size_t) size * ncount;
1661 if (len == 0 || ncount > SIZE_T_MAX / size)
1662 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1664 address = mm_malloc(mm, len);
1670 mm_zfree(struct mm_master *mm, void *address)
1672 mm_free(mm, address);
1676 mm_init_compression(struct mm_master *mm)
1678 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1679 outgoing_stream.zfree = (free_func)mm_zfree;
1680 outgoing_stream.opaque = mm;
1682 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1683 incoming_stream.zfree = (free_func)mm_zfree;
1684 incoming_stream.opaque = mm;
1689 #define FD_CLOSEONEXEC(x) do { \
1690 if (fcntl(x, F_SETFD, 1) == -1) \
1691 fatal("fcntl(%d, F_SETFD)", x); \
1695 monitor_socketpair(int *pair)
1697 #ifdef HAVE_SOCKETPAIR
1698 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1699 fatal("%s: socketpair", __func__);
1701 fatal("%s: UsePrivilegeSeparation=yes not supported",
1704 FD_CLOSEONEXEC(pair[0]);
1705 FD_CLOSEONEXEC(pair[1]);
1708 #define MM_MEMSIZE 65536
1713 struct monitor *mon;
1716 mon = xmalloc(sizeof(*mon));
1719 monitor_socketpair(pair);
1721 mon->m_recvfd = pair[0];
1722 mon->m_sendfd = pair[1];
1724 /* Used to share zlib space across processes */
1725 if (options.compression) {
1726 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1727 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1729 /* Compression needs to share state across borders */
1730 mm_init_compression(mon->m_zlib);
1737 monitor_reinit(struct monitor *mon)
1741 monitor_socketpair(pair);
1743 mon->m_recvfd = pair[0];
1744 mon->m_sendfd = pair[1];
1749 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1755 goid.elements = buffer_get_string(m, &len);
1758 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1760 xfree(goid.elements);
1763 buffer_put_int(m, major);
1765 mm_request_send(sock,MONITOR_ANS_GSSSETUP, m);
1767 /* Now we have a context, enable the step */
1768 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1774 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1777 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1778 OM_uint32 major,minor;
1779 OM_uint32 flags = 0; /* GSI needs this */
1782 in.value = buffer_get_string(m, &len);
1784 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1788 buffer_put_int(m, major);
1789 buffer_put_string(m, out.value, out.length);
1790 buffer_put_int(m, flags);
1791 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1793 gss_release_buffer(&minor, &out);
1795 if (major==GSS_S_COMPLETE) {
1796 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1797 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1798 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1804 mm_answer_gss_checkmic(int sock, Buffer *m)
1806 gss_buffer_desc gssbuf, mic;
1810 gssbuf.value = buffer_get_string(m, &len);
1811 gssbuf.length = len;
1812 mic.value = buffer_get_string(m, &len);
1815 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1817 xfree(gssbuf.value);
1821 buffer_put_int(m, ret);
1823 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1825 if (!GSS_ERROR(ret))
1826 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1832 mm_answer_gss_userok(int sock, Buffer *m)
1836 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1839 buffer_put_int(m, authenticated);
1841 debug3("%s: sending result %d", __func__, authenticated);
1842 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1844 auth_method="gssapi-with-mic";
1846 /* Monitor loop will terminate if authenticated */
1847 return (authenticated);