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 $");
31 #include <openssl/dh.h>
37 #define skeychallenge(k, u, c) opiechallenge((k), (u), (c))
38 #define skey_haskey(u) opie_haskey((u))
39 #define skey_passcheck(u, r) opie_passverify((u), (r))
49 #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */
52 #define TARGET_OS_MAC 1
57 #include "auth-options.h"
66 #include "monitor_mm.h"
67 #include "monitor_wrap.h"
68 #include "monitor_fdpass.h"
78 static Gssctxt *gsscontext = NULL;
82 extern ServerOptions options;
83 extern u_int utmp_len;
84 extern Newkeys *current_keys[];
85 extern z_stream incoming_stream;
86 extern z_stream outgoing_stream;
87 extern u_char session_id[];
88 extern Buffer input, output;
89 extern Buffer auth_debug;
90 extern int auth_debug_init;
91 extern Buffer loginmsg;
93 /* State exported from the child */
116 /* Functions on the monitor that answer unprivileged requests */
118 int mm_answer_moduli(int, Buffer *);
119 int mm_answer_sign(int, Buffer *);
120 int mm_answer_pwnamallow(int, Buffer *);
121 int mm_answer_auth2_read_banner(int, Buffer *);
122 int mm_answer_authserv(int, Buffer *);
123 int mm_answer_authpassword(int, Buffer *);
124 int mm_answer_bsdauthquery(int, Buffer *);
125 int mm_answer_bsdauthrespond(int, Buffer *);
126 int mm_answer_skeyquery(int, Buffer *);
127 int mm_answer_skeyrespond(int, Buffer *);
128 int mm_answer_keyallowed(int, Buffer *);
129 int mm_answer_keyverify(int, Buffer *);
130 int mm_answer_pty(int, Buffer *);
131 int mm_answer_pty_cleanup(int, Buffer *);
132 int mm_answer_term(int, Buffer *);
133 int mm_answer_rsa_keyallowed(int, Buffer *);
134 int mm_answer_rsa_challenge(int, Buffer *);
135 int mm_answer_rsa_response(int, Buffer *);
136 int mm_answer_sesskey(int, Buffer *);
137 int mm_answer_sessid(int, Buffer *);
140 int mm_answer_pam_start(int, Buffer *);
141 int mm_answer_pam_account(int, Buffer *);
142 int mm_answer_pam_init_ctx(int, Buffer *);
143 int mm_answer_pam_query(int, Buffer *);
144 int mm_answer_pam_respond(int, Buffer *);
145 int mm_answer_pam_free_ctx(int, Buffer *);
149 int mm_answer_gss_setup_ctx(int, Buffer *);
150 int mm_answer_gss_accept_ctx(int, Buffer *);
151 int mm_answer_gss_userok(int, Buffer *);
152 int mm_answer_gss_checkmic(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_ONCE, 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_ONCE, 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},
199 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
200 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
203 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
204 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
206 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
207 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
209 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
210 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
211 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
212 {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
217 struct mon_table mon_dispatch_postauth20[] = {
218 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
219 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
220 {MONITOR_REQ_PTY, 0, mm_answer_pty},
221 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
222 {MONITOR_REQ_TERM, 0, mm_answer_term},
226 struct mon_table mon_dispatch_proto15[] = {
227 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
228 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
229 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
230 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
231 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
232 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
233 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
234 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
236 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
237 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
240 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
241 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
244 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
245 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
246 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
247 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
248 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
249 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
254 struct mon_table mon_dispatch_postauth15[] = {
255 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
256 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
257 {MONITOR_REQ_TERM, 0, mm_answer_term},
261 struct mon_table *mon_dispatch;
263 /* Specifies if a certain message is allowed at the moment */
266 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
268 while (ent->f != NULL) {
269 if (ent->type == type) {
270 ent->flags &= ~MON_PERMIT;
271 ent->flags |= permit ? MON_PERMIT : 0;
279 monitor_permit_authentications(int permit)
281 struct mon_table *ent = mon_dispatch;
283 while (ent->f != NULL) {
284 if (ent->flags & MON_AUTH) {
285 ent->flags &= ~MON_PERMIT;
286 ent->flags |= permit ? MON_PERMIT : 0;
293 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
295 struct mon_table *ent;
296 int authenticated = 0;
298 debug3("preauth child monitor started");
300 authctxt = _authctxt;
301 memset(authctxt, 0, sizeof(*authctxt));
304 mon_dispatch = mon_dispatch_proto20;
306 /* Permit requests for moduli and signatures */
307 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
308 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
310 mon_dispatch = mon_dispatch_proto15;
312 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
315 /* The first few requests do not require asynchronous access */
316 while (!authenticated) {
317 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
319 if (!(ent->flags & MON_AUTHDECIDE))
320 fatal("%s: unexpected authentication from %d",
321 __func__, ent->type);
322 if (authctxt->pw->pw_uid == 0 &&
323 !auth_root_allowed(auth_method))
326 /* PAM needs to perform account checks after auth */
327 if (options.use_pam && authenticated) {
331 mm_request_receive_expect(pmonitor->m_sendfd,
332 MONITOR_REQ_PAM_ACCOUNT, &m);
333 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
339 if (ent->flags & MON_AUTHDECIDE) {
340 auth_log(authctxt, authenticated, auth_method,
341 compat20 ? " ssh2" : "");
343 authctxt->failures++;
347 if (!authctxt->valid)
348 fatal("%s: authenticated invalid user", __func__);
350 debug("%s: %s has been authenticated by privileged process",
351 __func__, authctxt->user);
353 mm_get_keystate(pmonitor);
357 monitor_set_child_handler(pid_t pid)
359 monitor_child_pid = pid;
363 monitor_child_handler(int sig)
365 kill(monitor_child_pid, sig);
369 monitor_child_postauth(struct monitor *pmonitor)
371 monitor_set_child_handler(pmonitor->m_pid);
372 signal(SIGHUP, &monitor_child_handler);
373 signal(SIGTERM, &monitor_child_handler);
376 mon_dispatch = mon_dispatch_postauth20;
378 /* Permit requests for moduli and signatures */
379 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
380 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
381 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
383 mon_dispatch = mon_dispatch_postauth15;
384 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
387 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
388 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
392 monitor_read(pmonitor, mon_dispatch, NULL);
396 monitor_sync(struct monitor *pmonitor)
398 if (options.compression) {
399 /* The member allocation is not visible, so sync it */
400 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
405 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
406 struct mon_table **pent)
414 mm_request_receive(pmonitor->m_sendfd, &m);
415 type = buffer_get_char(&m);
417 debug3("%s: checking request %d", __func__, type);
419 while (ent->f != NULL) {
420 if (ent->type == type)
425 if (ent->f != NULL) {
426 if (!(ent->flags & MON_PERMIT))
427 fatal("%s: unpermitted request %d", __func__,
429 ret = (*ent->f)(pmonitor->m_sendfd, &m);
432 /* The child may use this request only once, disable it */
433 if (ent->flags & MON_ONCE) {
434 debug2("%s: %d used once, disabling now", __func__,
436 ent->flags &= ~MON_PERMIT;
445 fatal("%s: unsupported request: %d", __func__, type);
451 /* allowed key state */
453 monitor_allowed_key(u_char *blob, u_int bloblen)
455 /* make sure key is allowed */
456 if (key_blob == NULL || key_bloblen != bloblen ||
457 memcmp(key_blob, blob, key_bloblen))
463 monitor_reset_key_state(void)
466 if (key_blob != NULL)
468 if (hostbased_cuser != NULL)
469 xfree(hostbased_cuser);
470 if (hostbased_chost != NULL)
471 xfree(hostbased_chost);
474 key_blobtype = MM_NOKEY;
475 hostbased_cuser = NULL;
476 hostbased_chost = NULL;
480 mm_answer_moduli(int sock, Buffer *m)
485 min = buffer_get_int(m);
486 want = buffer_get_int(m);
487 max = buffer_get_int(m);
489 debug3("%s: got parameters: %d %d %d",
490 __func__, min, want, max);
491 /* We need to check here, too, in case the child got corrupted */
492 if (max < min || want < min || max < want)
493 fatal("%s: bad parameters: %d %d %d",
494 __func__, min, want, max);
498 dh = choose_dh(min, want, max);
500 buffer_put_char(m, 0);
503 /* Send first bignum */
504 buffer_put_char(m, 1);
505 buffer_put_bignum2(m, dh->p);
506 buffer_put_bignum2(m, dh->g);
510 mm_request_send(sock, MONITOR_ANS_MODULI, m);
515 mm_answer_sign(int sock, Buffer *m)
520 u_int siglen, datlen;
523 debug3("%s", __func__);
525 keyid = buffer_get_int(m);
526 p = buffer_get_string(m, &datlen);
529 fatal("%s: data length incorrect: %u", __func__, datlen);
531 /* save session id, it will be passed on the first call */
532 if (session_id2_len == 0) {
533 session_id2_len = datlen;
534 session_id2 = xmalloc(session_id2_len);
535 memcpy(session_id2, p, session_id2_len);
538 if ((key = get_hostkey_by_index(keyid)) == NULL)
539 fatal("%s: no hostkey from index %d", __func__, keyid);
540 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
541 fatal("%s: key_sign failed", __func__);
543 debug3("%s: signature %p(%u)", __func__, signature, siglen);
546 buffer_put_string(m, signature, siglen);
551 mm_request_send(sock, MONITOR_ANS_SIGN, m);
553 /* Turn on permissions for getpwnam */
554 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
559 /* Retrieves the password entry and also checks if the user is permitted */
562 mm_answer_pwnamallow(int sock, Buffer *m)
565 struct passwd *pwent;
568 debug3("%s", __func__);
570 if (authctxt->attempt++ != 0)
571 fatal("%s: multiple attempts for getpwnam", __func__);
573 username = buffer_get_string(m, NULL);
575 pwent = getpwnamallow(username);
577 authctxt->user = xstrdup(username);
578 setproctitle("%s [priv]", pwent ? username : "unknown");
584 buffer_put_char(m, 0);
585 authctxt->pw = fakepw();
590 authctxt->pw = pwent;
593 buffer_put_char(m, 1);
594 buffer_put_string(m, pwent, sizeof(struct passwd));
595 buffer_put_cstring(m, pwent->pw_name);
596 buffer_put_cstring(m, "*");
597 buffer_put_cstring(m, pwent->pw_gecos);
598 #ifdef HAVE_PW_CLASS_IN_PASSWD
599 buffer_put_cstring(m, pwent->pw_class);
601 buffer_put_cstring(m, pwent->pw_dir);
602 buffer_put_cstring(m, pwent->pw_shell);
605 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
606 mm_request_send(sock, MONITOR_ANS_PWNAM, m);
608 /* For SSHv1 allow authentication now */
610 monitor_permit_authentications(1);
612 /* Allow service/style information on the auth context */
613 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
614 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
619 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
625 int mm_answer_auth2_read_banner(int sock, Buffer *m)
630 banner = auth2_read_banner();
631 buffer_put_cstring(m, banner != NULL ? banner : "");
632 mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
641 mm_answer_authserv(int sock, Buffer *m)
643 monitor_permit_authentications(1);
645 authctxt->service = buffer_get_string(m, NULL);
646 authctxt->style = buffer_get_string(m, NULL);
647 debug3("%s: service=%s, style=%s",
648 __func__, authctxt->service, authctxt->style);
650 if (strlen(authctxt->style) == 0) {
651 xfree(authctxt->style);
652 authctxt->style = NULL;
659 mm_answer_authpassword(int sock, Buffer *m)
661 static int call_count;
666 passwd = buffer_get_string(m, &plen);
667 /* Only authenticate if the context is valid */
668 authenticated = options.password_authentication &&
669 auth_password(authctxt, passwd);
670 memset(passwd, 0, strlen(passwd));
674 buffer_put_int(m, authenticated);
676 debug3("%s: sending result %d", __func__, authenticated);
677 mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
680 if (plen == 0 && call_count == 1)
681 auth_method = "none";
683 auth_method = "password";
685 /* Causes monitor loop to terminate if authenticated */
686 return (authenticated);
691 mm_answer_bsdauthquery(int sock, Buffer *m)
693 char *name, *infotxt;
699 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
700 &prompts, &echo_on) < 0 ? 0 : 1;
703 buffer_put_int(m, success);
705 buffer_put_cstring(m, prompts[0]);
707 debug3("%s: sending challenge success: %u", __func__, success);
708 mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
721 mm_answer_bsdauthrespond(int sock, Buffer *m)
726 if (authctxt->as == 0)
727 fatal("%s: no bsd auth session", __func__);
729 response = buffer_get_string(m, NULL);
730 authok = options.challenge_response_authentication &&
731 auth_userresponse(authctxt->as, response, 0);
733 debug3("%s: <%s> = <%d>", __func__, response, authok);
737 buffer_put_int(m, authok);
739 debug3("%s: sending authenticated: %d", __func__, authok);
740 mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
742 auth_method = "bsdauth";
744 return (authok != 0);
750 mm_answer_skeyquery(int sock, Buffer *m)
753 char challenge[1024];
756 success = _compat_skeychallenge(&skey, authctxt->user, challenge,
757 sizeof(challenge)) < 0 ? 0 : 1;
760 buffer_put_int(m, success);
762 buffer_put_cstring(m, challenge);
764 debug3("%s: sending challenge success: %u", __func__, success);
765 mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m);
771 mm_answer_skeyrespond(int sock, Buffer *m)
776 response = buffer_get_string(m, NULL);
778 authok = (options.challenge_response_authentication &&
780 skey_haskey(authctxt->pw->pw_name) == 0 &&
781 skey_passcheck(authctxt->pw->pw_name, response) != -1);
786 buffer_put_int(m, authok);
788 debug3("%s: sending authenticated: %d", __func__, authok);
789 mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
791 auth_method = "skey";
793 return (authok != 0);
799 mm_answer_pam_start(int sock, Buffer *m)
801 if (!options.use_pam)
802 fatal("UsePAM not set, but ended up in %s anyway", __func__);
806 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
812 mm_answer_pam_account(int sock, Buffer *m)
816 if (!options.use_pam)
817 fatal("UsePAM not set, but ended up in %s anyway", __func__);
819 ret = do_pam_account();
821 buffer_put_int(m, ret);
823 mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
828 static void *sshpam_ctxt, *sshpam_authok;
829 extern KbdintDevice sshpam_device;
832 mm_answer_pam_init_ctx(int sock, Buffer *m)
835 debug3("%s", __func__);
836 authctxt->user = buffer_get_string(m, NULL);
837 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
838 sshpam_authok = NULL;
840 if (sshpam_ctxt != NULL) {
841 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
842 buffer_put_int(m, 1);
844 buffer_put_int(m, 0);
846 mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
851 mm_answer_pam_query(int sock, Buffer *m)
853 char *name, *info, **prompts;
857 debug3("%s", __func__);
858 sshpam_authok = NULL;
859 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
860 if (ret == 0 && num == 0)
861 sshpam_authok = sshpam_ctxt;
862 if (num > 1 || name == NULL || info == NULL)
865 buffer_put_int(m, ret);
866 buffer_put_cstring(m, name);
868 buffer_put_cstring(m, info);
870 buffer_put_int(m, num);
871 for (i = 0; i < num; ++i) {
872 buffer_put_cstring(m, prompts[i]);
874 buffer_put_int(m, echo_on[i]);
880 mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
885 mm_answer_pam_respond(int sock, Buffer *m)
891 debug3("%s", __func__);
892 sshpam_authok = NULL;
893 num = buffer_get_int(m);
895 resp = xmalloc(num * sizeof(char *));
896 for (i = 0; i < num; ++i)
897 resp[i] = buffer_get_string(m, NULL);
898 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
899 for (i = 0; i < num; ++i)
903 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
906 buffer_put_int(m, ret);
907 mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
908 auth_method = "keyboard-interactive/pam";
910 sshpam_authok = sshpam_ctxt;
915 mm_answer_pam_free_ctx(int sock, Buffer *m)
918 debug3("%s", __func__);
919 (sshpam_device.free_ctx)(sshpam_ctxt);
921 mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
922 return (sshpam_authok == sshpam_ctxt);
927 mm_append_debug(Buffer *m)
929 if (auth_debug_init && buffer_len(&auth_debug)) {
930 debug3("%s: Appending debug messages for child", __func__);
931 buffer_append(m, buffer_ptr(&auth_debug),
932 buffer_len(&auth_debug));
933 buffer_clear(&auth_debug);
938 mm_answer_keyallowed(int sock, Buffer *m)
944 enum mm_keytype type = 0;
947 debug3("%s entering", __func__);
949 type = buffer_get_int(m);
950 cuser = buffer_get_string(m, NULL);
951 chost = buffer_get_string(m, NULL);
952 blob = buffer_get_string(m, &bloblen);
954 key = key_from_blob(blob, bloblen);
956 if ((compat20 && type == MM_RSAHOSTKEY) ||
957 (!compat20 && type != MM_RSAHOSTKEY))
958 fatal("%s: key type and protocol mismatch", __func__);
960 debug3("%s: key_from_blob: %p", __func__, key);
962 if (key != NULL && authctxt->valid) {
965 allowed = options.pubkey_authentication &&
966 user_key_allowed(authctxt->pw, key);
969 allowed = options.hostbased_authentication &&
970 hostbased_key_allowed(authctxt->pw,
974 key->type = KEY_RSA1; /* XXX */
975 allowed = options.rhosts_rsa_authentication &&
976 auth_rhosts_rsa_key_allowed(authctxt->pw,
980 fatal("%s: unknown key type %d", __func__, type);
987 /* clear temporarily storage (used by verify) */
988 monitor_reset_key_state();
991 /* Save temporarily for comparison in verify */
993 key_bloblen = bloblen;
995 hostbased_cuser = cuser;
996 hostbased_chost = chost;
999 debug3("%s: key %p is %s",
1000 __func__, key, allowed ? "allowed" : "disallowed");
1003 buffer_put_int(m, allowed);
1004 buffer_put_int(m, forced_command != NULL);
1008 mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
1010 if (type == MM_RSAHOSTKEY)
1011 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1017 monitor_valid_userblob(u_char *data, u_int datalen)
1025 buffer_append(&b, data, datalen);
1027 if (datafellows & SSH_OLD_SESSIONID) {
1029 len = buffer_len(&b);
1030 if ((session_id2 == NULL) ||
1031 (len < session_id2_len) ||
1032 (memcmp(p, session_id2, session_id2_len) != 0))
1034 buffer_consume(&b, session_id2_len);
1036 p = buffer_get_string(&b, &len);
1037 if ((session_id2 == NULL) ||
1038 (len != session_id2_len) ||
1039 (memcmp(p, session_id2, session_id2_len) != 0))
1043 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1045 p = buffer_get_string(&b, NULL);
1046 if (strcmp(authctxt->user, p) != 0) {
1047 logit("wrong user name passed to monitor: expected %s != %.100s",
1052 buffer_skip_string(&b);
1053 if (datafellows & SSH_BUG_PKAUTH) {
1054 if (!buffer_get_char(&b))
1057 p = buffer_get_string(&b, NULL);
1058 if (strcmp("publickey", p) != 0)
1061 if (!buffer_get_char(&b))
1063 buffer_skip_string(&b);
1065 buffer_skip_string(&b);
1066 if (buffer_len(&b) != 0)
1073 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1082 buffer_append(&b, data, datalen);
1084 p = buffer_get_string(&b, &len);
1085 if ((session_id2 == NULL) ||
1086 (len != session_id2_len) ||
1087 (memcmp(p, session_id2, session_id2_len) != 0))
1091 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1093 p = buffer_get_string(&b, NULL);
1094 if (strcmp(authctxt->user, p) != 0) {
1095 logit("wrong user name passed to monitor: expected %s != %.100s",
1100 buffer_skip_string(&b); /* service */
1101 p = buffer_get_string(&b, NULL);
1102 if (strcmp(p, "hostbased") != 0)
1105 buffer_skip_string(&b); /* pkalg */
1106 buffer_skip_string(&b); /* pkblob */
1108 /* verify client host, strip trailing dot if necessary */
1109 p = buffer_get_string(&b, NULL);
1110 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1112 if (strcmp(p, chost) != 0)
1116 /* verify client user */
1117 p = buffer_get_string(&b, NULL);
1118 if (strcmp(p, cuser) != 0)
1122 if (buffer_len(&b) != 0)
1129 mm_answer_keyverify(int sock, Buffer *m)
1132 u_char *signature, *data, *blob;
1133 u_int signaturelen, datalen, bloblen;
1137 blob = buffer_get_string(m, &bloblen);
1138 signature = buffer_get_string(m, &signaturelen);
1139 data = buffer_get_string(m, &datalen);
1141 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1142 !monitor_allowed_key(blob, bloblen))
1143 fatal("%s: bad key, not previously allowed", __func__);
1145 key = key_from_blob(blob, bloblen);
1147 fatal("%s: bad public key blob", __func__);
1149 switch (key_blobtype) {
1151 valid_data = monitor_valid_userblob(data, datalen);
1154 valid_data = monitor_valid_hostbasedblob(data, datalen,
1155 hostbased_cuser, hostbased_chost);
1162 fatal("%s: bad signature data blob", __func__);
1164 verified = key_verify(key, signature, signaturelen, data, datalen);
1165 debug3("%s: key %p signature %s",
1166 __func__, key, verified ? "verified" : "unverified");
1173 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1175 monitor_reset_key_state();
1178 buffer_put_int(m, verified);
1179 mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
1185 mm_record_login(Session *s, struct passwd *pw)
1188 struct sockaddr_storage from;
1191 * Get IP address of client. If the connection is not a socket, let
1192 * the address be 0.0.0.0.
1194 memset(&from, 0, sizeof(from));
1195 fromlen = sizeof(from);
1196 if (packet_connection_is_on_socket()) {
1197 if (getpeername(packet_get_connection_in(),
1198 (struct sockaddr *) & from, &fromlen) < 0) {
1199 debug("getpeername: %.100s", strerror(errno));
1203 /* Record that there was a login on that tty from the remote host. */
1204 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1205 get_remote_name_or_ip(utmp_len, options.use_dns),
1206 (struct sockaddr *)&from, fromlen);
1210 mm_session_close(Session *s)
1212 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1213 if (s->ttyfd != -1) {
1214 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1215 session_pty_cleanup2(s);
1221 mm_answer_pty(int sock, Buffer *m)
1223 extern struct monitor *pmonitor;
1227 debug3("%s entering", __func__);
1233 s->authctxt = authctxt;
1234 s->pw = authctxt->pw;
1235 s->pid = pmonitor->m_pid;
1236 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1239 pty_setowner(authctxt->pw, s->tty);
1241 buffer_put_int(m, 1);
1242 buffer_put_cstring(m, s->tty);
1244 /* We need to trick ttyslot */
1245 if (dup2(s->ttyfd, 0) == -1)
1246 fatal("%s: dup2", __func__);
1248 mm_record_login(s, authctxt->pw);
1250 /* Now we can close the file descriptor again */
1253 /* send messages generated by record_login */
1254 buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg));
1255 buffer_clear(&loginmsg);
1257 mm_request_send(sock, MONITOR_ANS_PTY, m);
1259 mm_send_fd(sock, s->ptyfd);
1260 mm_send_fd(sock, s->ttyfd);
1262 /* make sure nothing uses fd 0 */
1263 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1264 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1266 error("%s: fd0 %d != 0", __func__, fd0);
1268 /* slave is not needed */
1270 s->ttyfd = s->ptyfd;
1271 /* no need to dup() because nobody closes ptyfd */
1272 s->ptymaster = s->ptyfd;
1274 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1280 mm_session_close(s);
1281 buffer_put_int(m, 0);
1282 mm_request_send(sock, MONITOR_ANS_PTY, m);
1287 mm_answer_pty_cleanup(int sock, Buffer *m)
1292 debug3("%s entering", __func__);
1294 tty = buffer_get_string(m, NULL);
1295 if ((s = session_by_tty(tty)) != NULL)
1296 mm_session_close(s);
1303 mm_answer_sesskey(int sock, Buffer *m)
1308 /* Turn off permissions */
1309 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1311 if ((p = BN_new()) == NULL)
1312 fatal("%s: BN_new", __func__);
1314 buffer_get_bignum2(m, p);
1316 rsafail = ssh1_session_key(p);
1319 buffer_put_int(m, rsafail);
1320 buffer_put_bignum2(m, p);
1324 mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
1326 /* Turn on permissions for sessid passing */
1327 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1333 mm_answer_sessid(int sock, Buffer *m)
1337 debug3("%s entering", __func__);
1339 if (buffer_len(m) != 16)
1340 fatal("%s: bad ssh1 session id", __func__);
1341 for (i = 0; i < 16; i++)
1342 session_id[i] = buffer_get_char(m);
1344 /* Turn on permissions for getpwnam */
1345 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1351 mm_answer_rsa_keyallowed(int sock, Buffer *m)
1355 u_char *blob = NULL;
1359 debug3("%s entering", __func__);
1361 if (options.rsa_authentication && authctxt->valid) {
1362 if ((client_n = BN_new()) == NULL)
1363 fatal("%s: BN_new", __func__);
1364 buffer_get_bignum2(m, client_n);
1365 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1366 BN_clear_free(client_n);
1369 buffer_put_int(m, allowed);
1370 buffer_put_int(m, forced_command != NULL);
1372 /* clear temporarily storage (used by generate challenge) */
1373 monitor_reset_key_state();
1375 if (allowed && key != NULL) {
1376 key->type = KEY_RSA; /* cheat for key_to_blob */
1377 if (key_to_blob(key, &blob, &blen) == 0)
1378 fatal("%s: key_to_blob failed", __func__);
1379 buffer_put_string(m, blob, blen);
1381 /* Save temporarily for comparison in verify */
1384 key_blobtype = MM_RSAUSERKEY;
1391 mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
1393 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1394 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1399 mm_answer_rsa_challenge(int sock, Buffer *m)
1405 debug3("%s entering", __func__);
1407 if (!authctxt->valid)
1408 fatal("%s: authctxt not valid", __func__);
1409 blob = buffer_get_string(m, &blen);
1410 if (!monitor_allowed_key(blob, blen))
1411 fatal("%s: bad key, not previously allowed", __func__);
1412 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1413 fatal("%s: key type mismatch", __func__);
1414 if ((key = key_from_blob(blob, blen)) == NULL)
1415 fatal("%s: received bad key", __func__);
1418 BN_clear_free(ssh1_challenge);
1419 ssh1_challenge = auth_rsa_generate_challenge(key);
1422 buffer_put_bignum2(m, ssh1_challenge);
1424 debug3("%s sending reply", __func__);
1425 mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
1427 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1435 mm_answer_rsa_response(int sock, Buffer *m)
1438 u_char *blob, *response;
1442 debug3("%s entering", __func__);
1444 if (!authctxt->valid)
1445 fatal("%s: authctxt not valid", __func__);
1446 if (ssh1_challenge == NULL)
1447 fatal("%s: no ssh1_challenge", __func__);
1449 blob = buffer_get_string(m, &blen);
1450 if (!monitor_allowed_key(blob, blen))
1451 fatal("%s: bad key, not previously allowed", __func__);
1452 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1453 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1454 if ((key = key_from_blob(blob, blen)) == NULL)
1455 fatal("%s: received bad key", __func__);
1456 response = buffer_get_string(m, &len);
1458 fatal("%s: received bad response to challenge", __func__);
1459 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1465 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1468 BN_clear_free(ssh1_challenge);
1469 ssh1_challenge = NULL;
1470 monitor_reset_key_state();
1473 buffer_put_int(m, success);
1474 mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
1480 mm_answer_term(int sock, Buffer *req)
1482 extern struct monitor *pmonitor;
1485 debug3("%s: tearing down sessions", __func__);
1487 /* The child is terminating */
1488 session_destroy_all(&mm_session_close);
1490 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1494 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1496 /* Terminate process */
1501 monitor_apply_keystate(struct monitor *pmonitor)
1504 set_newkeys(MODE_IN);
1505 set_newkeys(MODE_OUT);
1507 packet_set_protocol_flags(child_state.ssh1protoflags);
1508 packet_set_encryption_key(child_state.ssh1key,
1509 child_state.ssh1keylen, child_state.ssh1cipher);
1510 xfree(child_state.ssh1key);
1513 /* for rc4 and other stateful ciphers */
1514 packet_set_keycontext(MODE_OUT, child_state.keyout);
1515 xfree(child_state.keyout);
1516 packet_set_keycontext(MODE_IN, child_state.keyin);
1517 xfree(child_state.keyin);
1520 packet_set_iv(MODE_OUT, child_state.ivout);
1521 xfree(child_state.ivout);
1522 packet_set_iv(MODE_IN, child_state.ivin);
1523 xfree(child_state.ivin);
1526 memcpy(&incoming_stream, &child_state.incoming,
1527 sizeof(incoming_stream));
1528 memcpy(&outgoing_stream, &child_state.outgoing,
1529 sizeof(outgoing_stream));
1531 /* Update with new address */
1532 if (options.compression)
1533 mm_init_compression(pmonitor->m_zlib);
1535 /* Network I/O buffers */
1536 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1537 buffer_clear(&input);
1538 buffer_append(&input, child_state.input, child_state.ilen);
1539 memset(child_state.input, 0, child_state.ilen);
1540 xfree(child_state.input);
1542 buffer_clear(&output);
1543 buffer_append(&output, child_state.output, child_state.olen);
1544 memset(child_state.output, 0, child_state.olen);
1545 xfree(child_state.output);
1549 mm_get_kex(Buffer *m)
1555 kex = xmalloc(sizeof(*kex));
1556 memset(kex, 0, sizeof(*kex));
1557 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1558 if ((session_id2 == NULL) ||
1559 (kex->session_id_len != session_id2_len) ||
1560 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1561 fatal("mm_get_get: internal error: bad session id");
1562 kex->we_need = buffer_get_int(m);
1563 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1564 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
1565 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1567 kex->hostkey_type = buffer_get_int(m);
1568 kex->kex_type = buffer_get_int(m);
1569 blob = buffer_get_string(m, &bloblen);
1570 buffer_init(&kex->my);
1571 buffer_append(&kex->my, blob, bloblen);
1573 blob = buffer_get_string(m, &bloblen);
1574 buffer_init(&kex->peer);
1575 buffer_append(&kex->peer, blob, bloblen);
1578 kex->flags = buffer_get_int(m);
1579 kex->client_version_string = buffer_get_string(m, NULL);
1580 kex->server_version_string = buffer_get_string(m, NULL);
1581 kex->load_host_key=&get_hostkey_by_type;
1582 kex->host_key_index=&get_hostkey_index;
1587 /* This function requries careful sanity checking */
1590 mm_get_keystate(struct monitor *pmonitor)
1594 u_int bloblen, plen;
1595 u_int32_t seqnr, packets;
1598 debug3("%s: Waiting for new keys", __func__);
1601 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1603 child_state.ssh1protoflags = buffer_get_int(&m);
1604 child_state.ssh1cipher = buffer_get_int(&m);
1605 child_state.ssh1key = buffer_get_string(&m,
1606 &child_state.ssh1keylen);
1607 child_state.ivout = buffer_get_string(&m,
1608 &child_state.ivoutlen);
1609 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1612 /* Get the Kex for rekeying */
1613 *pmonitor->m_pkex = mm_get_kex(&m);
1616 blob = buffer_get_string(&m, &bloblen);
1617 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1620 debug3("%s: Waiting for second key", __func__);
1621 blob = buffer_get_string(&m, &bloblen);
1622 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1625 /* Now get sequence numbers for the packets */
1626 seqnr = buffer_get_int(&m);
1627 blocks = buffer_get_int64(&m);
1628 packets = buffer_get_int(&m);
1629 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1630 seqnr = buffer_get_int(&m);
1631 blocks = buffer_get_int64(&m);
1632 packets = buffer_get_int(&m);
1633 packet_set_state(MODE_IN, seqnr, blocks, packets);
1636 /* Get the key context */
1637 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1638 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1640 debug3("%s: Getting compression state", __func__);
1641 /* Get compression state */
1642 p = buffer_get_string(&m, &plen);
1643 if (plen != sizeof(child_state.outgoing))
1644 fatal("%s: bad request size", __func__);
1645 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1648 p = buffer_get_string(&m, &plen);
1649 if (plen != sizeof(child_state.incoming))
1650 fatal("%s: bad request size", __func__);
1651 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1654 /* Network I/O buffers */
1655 debug3("%s: Getting Network I/O buffers", __func__);
1656 child_state.input = buffer_get_string(&m, &child_state.ilen);
1657 child_state.output = buffer_get_string(&m, &child_state.olen);
1663 /* Allocation functions for zlib */
1665 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1667 size_t len = (size_t) size * ncount;
1670 if (len == 0 || ncount > SIZE_T_MAX / size)
1671 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1673 address = mm_malloc(mm, len);
1679 mm_zfree(struct mm_master *mm, void *address)
1681 mm_free(mm, address);
1685 mm_init_compression(struct mm_master *mm)
1687 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1688 outgoing_stream.zfree = (free_func)mm_zfree;
1689 outgoing_stream.opaque = mm;
1691 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1692 incoming_stream.zfree = (free_func)mm_zfree;
1693 incoming_stream.opaque = mm;
1698 #define FD_CLOSEONEXEC(x) do { \
1699 if (fcntl(x, F_SETFD, 1) == -1) \
1700 fatal("fcntl(%d, F_SETFD)", x); \
1704 monitor_socketpair(int *pair)
1706 #ifdef HAVE_SOCKETPAIR
1707 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1708 fatal("%s: socketpair", __func__);
1710 fatal("%s: UsePrivilegeSeparation=yes not supported",
1713 FD_CLOSEONEXEC(pair[0]);
1714 FD_CLOSEONEXEC(pair[1]);
1717 #define MM_MEMSIZE 65536
1722 struct monitor *mon;
1725 mon = xmalloc(sizeof(*mon));
1728 monitor_socketpair(pair);
1730 mon->m_recvfd = pair[0];
1731 mon->m_sendfd = pair[1];
1733 /* Used to share zlib space across processes */
1734 if (options.compression) {
1735 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1736 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1738 /* Compression needs to share state across borders */
1739 mm_init_compression(mon->m_zlib);
1746 monitor_reinit(struct monitor *mon)
1750 monitor_socketpair(pair);
1752 mon->m_recvfd = pair[0];
1753 mon->m_sendfd = pair[1];
1758 mm_answer_gss_setup_ctx(int sock, Buffer *m)
1764 goid.elements = buffer_get_string(m, &len);
1767 major = ssh_gssapi_server_ctx(&gsscontext, &goid);
1769 xfree(goid.elements);
1772 buffer_put_int(m, major);
1774 mm_request_send(sock,MONITOR_ANS_GSSSETUP, m);
1776 /* Now we have a context, enable the step */
1777 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1783 mm_answer_gss_accept_ctx(int sock, Buffer *m)
1786 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1787 OM_uint32 major,minor;
1788 OM_uint32 flags = 0; /* GSI needs this */
1791 in.value = buffer_get_string(m, &len);
1793 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1797 buffer_put_int(m, major);
1798 buffer_put_string(m, out.value, out.length);
1799 buffer_put_int(m, flags);
1800 mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
1802 gss_release_buffer(&minor, &out);
1804 if (major==GSS_S_COMPLETE) {
1805 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1806 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1807 monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
1813 mm_answer_gss_checkmic(int sock, Buffer *m)
1815 gss_buffer_desc gssbuf, mic;
1819 gssbuf.value = buffer_get_string(m, &len);
1820 gssbuf.length = len;
1821 mic.value = buffer_get_string(m, &len);
1824 ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
1826 xfree(gssbuf.value);
1830 buffer_put_int(m, ret);
1832 mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
1834 if (!GSS_ERROR(ret))
1835 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1841 mm_answer_gss_userok(int sock, Buffer *m)
1845 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1848 buffer_put_int(m, authenticated);
1850 debug3("%s: sending result %d", __func__, authenticated);
1851 mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
1853 auth_method="gssapi-with-mic";
1855 /* Monitor loop will terminate if authenticated */
1856 return (authenticated);