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.47 2003/08/24 17:36:52 deraadt Exp $");
30 #include <openssl/dh.h>
42 #include "auth-options.h"
51 #include "monitor_mm.h"
52 #include "monitor_wrap.h"
53 #include "monitor_fdpass.h"
64 static Gssctxt *gsscontext = NULL;
68 extern ServerOptions options;
69 extern u_int utmp_len;
70 extern Newkeys *current_keys[];
71 extern z_stream incoming_stream;
72 extern z_stream outgoing_stream;
73 extern u_char session_id[];
74 extern Buffer input, output;
75 extern Buffer auth_debug;
76 extern int auth_debug_init;
78 /* State exported from the child */
101 /* Functions on the monitor that answer unprivileged requests */
103 int mm_answer_moduli(int, Buffer *);
104 int mm_answer_sign(int, Buffer *);
105 int mm_answer_pwnamallow(int, Buffer *);
106 int mm_answer_auth2_read_banner(int, Buffer *);
107 int mm_answer_authserv(int, Buffer *);
108 int mm_answer_authpassword(int, Buffer *);
109 int mm_answer_bsdauthquery(int, Buffer *);
110 int mm_answer_bsdauthrespond(int, Buffer *);
111 int mm_answer_skeyquery(int, Buffer *);
112 int mm_answer_skeyrespond(int, Buffer *);
113 int mm_answer_keyallowed(int, Buffer *);
114 int mm_answer_keyverify(int, Buffer *);
115 int mm_answer_pty(int, Buffer *);
116 int mm_answer_pty_cleanup(int, Buffer *);
117 int mm_answer_term(int, Buffer *);
118 int mm_answer_rsa_keyallowed(int, Buffer *);
119 int mm_answer_rsa_challenge(int, Buffer *);
120 int mm_answer_rsa_response(int, Buffer *);
121 int mm_answer_sesskey(int, Buffer *);
122 int mm_answer_sessid(int, Buffer *);
125 int mm_answer_pam_start(int, Buffer *);
126 int mm_answer_pam_account(int, Buffer *);
127 int mm_answer_pam_init_ctx(int, Buffer *);
128 int mm_answer_pam_query(int, Buffer *);
129 int mm_answer_pam_respond(int, Buffer *);
130 int mm_answer_pam_free_ctx(int, Buffer *);
134 int mm_answer_krb5(int, Buffer *);
137 int mm_answer_gss_setup_ctx(int, Buffer *);
138 int mm_answer_gss_accept_ctx(int, Buffer *);
139 int mm_answer_gss_userok(int, Buffer *);
142 static Authctxt *authctxt;
143 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
145 /* local state for key verify */
146 static u_char *key_blob = NULL;
147 static u_int key_bloblen = 0;
148 static int key_blobtype = MM_NOKEY;
149 static char *hostbased_cuser = NULL;
150 static char *hostbased_chost = NULL;
151 static char *auth_method = "unknown";
152 static u_int session_id2_len = 0;
153 static u_char *session_id2 = NULL;
154 static pid_t monitor_child_pid;
157 enum monitor_reqtype type;
159 int (*f)(int, Buffer *);
162 #define MON_ISAUTH 0x0004 /* Required for Authentication */
163 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
164 #define MON_ONCE 0x0010 /* Disable after calling */
166 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
168 #define MON_PERMIT 0x1000 /* Request is permitted */
170 struct mon_table mon_dispatch_proto20[] = {
171 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
172 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
173 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
174 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
175 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
176 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
178 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
179 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
180 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
181 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
182 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
183 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
186 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
187 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
190 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
191 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
193 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
194 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
196 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
199 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
200 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
201 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
206 struct mon_table mon_dispatch_postauth20[] = {
207 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
208 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
209 {MONITOR_REQ_PTY, 0, mm_answer_pty},
210 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
211 {MONITOR_REQ_TERM, 0, mm_answer_term},
215 struct mon_table mon_dispatch_proto15[] = {
216 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
217 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
218 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
219 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
220 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
221 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
222 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
223 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
225 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
226 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
229 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
230 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
233 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
234 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
235 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
236 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
237 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
238 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
241 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
246 struct mon_table mon_dispatch_postauth15[] = {
247 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
248 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
249 {MONITOR_REQ_TERM, 0, mm_answer_term},
253 struct mon_table *mon_dispatch;
255 /* Specifies if a certain message is allowed at the moment */
258 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
260 while (ent->f != NULL) {
261 if (ent->type == type) {
262 ent->flags &= ~MON_PERMIT;
263 ent->flags |= permit ? MON_PERMIT : 0;
271 monitor_permit_authentications(int permit)
273 struct mon_table *ent = mon_dispatch;
275 while (ent->f != NULL) {
276 if (ent->flags & MON_AUTH) {
277 ent->flags &= ~MON_PERMIT;
278 ent->flags |= permit ? MON_PERMIT : 0;
285 monitor_child_preauth(struct monitor *pmonitor)
287 struct mon_table *ent;
288 int authenticated = 0;
290 debug3("preauth child monitor started");
293 mon_dispatch = mon_dispatch_proto20;
295 /* Permit requests for moduli and signatures */
296 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
297 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
299 mon_dispatch = mon_dispatch_proto15;
301 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
304 authctxt = authctxt_new();
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) {
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);
350 monitor_set_child_handler(pid_t pid)
352 monitor_child_pid = pid;
356 monitor_child_handler(int signal)
358 kill(monitor_child_pid, signal);
362 monitor_child_postauth(struct monitor *pmonitor)
364 monitor_set_child_handler(pmonitor->m_pid);
365 signal(SIGHUP, &monitor_child_handler);
366 signal(SIGTERM, &monitor_child_handler);
369 mon_dispatch = mon_dispatch_postauth20;
371 /* Permit requests for moduli and signatures */
372 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
373 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
374 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
376 mon_dispatch = mon_dispatch_postauth15;
377 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
380 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
381 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
385 monitor_read(pmonitor, mon_dispatch, NULL);
389 monitor_sync(struct monitor *pmonitor)
391 if (options.compression) {
392 /* The member allocation is not visible, so sync it */
393 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
398 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
399 struct mon_table **pent)
407 mm_request_receive(pmonitor->m_sendfd, &m);
408 type = buffer_get_char(&m);
410 debug3("%s: checking request %d", __func__, type);
412 while (ent->f != NULL) {
413 if (ent->type == type)
418 if (ent->f != NULL) {
419 if (!(ent->flags & MON_PERMIT))
420 fatal("%s: unpermitted request %d", __func__,
422 ret = (*ent->f)(pmonitor->m_sendfd, &m);
425 /* The child may use this request only once, disable it */
426 if (ent->flags & MON_ONCE) {
427 debug2("%s: %d used once, disabling now", __func__,
429 ent->flags &= ~MON_PERMIT;
438 fatal("%s: unsupported request: %d", __func__, type);
444 /* allowed key state */
446 monitor_allowed_key(u_char *blob, u_int bloblen)
448 /* make sure key is allowed */
449 if (key_blob == NULL || key_bloblen != bloblen ||
450 memcmp(key_blob, blob, key_bloblen))
456 monitor_reset_key_state(void)
459 if (key_blob != NULL)
461 if (hostbased_cuser != NULL)
462 xfree(hostbased_cuser);
463 if (hostbased_chost != NULL)
464 xfree(hostbased_chost);
467 key_blobtype = MM_NOKEY;
468 hostbased_cuser = NULL;
469 hostbased_chost = NULL;
473 mm_answer_moduli(int socket, Buffer *m)
478 min = buffer_get_int(m);
479 want = buffer_get_int(m);
480 max = buffer_get_int(m);
482 debug3("%s: got parameters: %d %d %d",
483 __func__, min, want, max);
484 /* We need to check here, too, in case the child got corrupted */
485 if (max < min || want < min || max < want)
486 fatal("%s: bad parameters: %d %d %d",
487 __func__, min, want, max);
491 dh = choose_dh(min, want, max);
493 buffer_put_char(m, 0);
496 /* Send first bignum */
497 buffer_put_char(m, 1);
498 buffer_put_bignum2(m, dh->p);
499 buffer_put_bignum2(m, dh->g);
503 mm_request_send(socket, MONITOR_ANS_MODULI, m);
508 mm_answer_sign(int socket, Buffer *m)
513 u_int siglen, datlen;
516 debug3("%s", __func__);
518 keyid = buffer_get_int(m);
519 p = buffer_get_string(m, &datlen);
522 fatal("%s: data length incorrect: %u", __func__, datlen);
524 /* save session id, it will be passed on the first call */
525 if (session_id2_len == 0) {
526 session_id2_len = datlen;
527 session_id2 = xmalloc(session_id2_len);
528 memcpy(session_id2, p, session_id2_len);
531 if ((key = get_hostkey_by_index(keyid)) == NULL)
532 fatal("%s: no hostkey from index %d", __func__, keyid);
533 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
534 fatal("%s: key_sign failed", __func__);
536 debug3("%s: signature %p(%u)", __func__, signature, siglen);
539 buffer_put_string(m, signature, siglen);
544 mm_request_send(socket, MONITOR_ANS_SIGN, m);
546 /* Turn on permissions for getpwnam */
547 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
552 /* Retrieves the password entry and also checks if the user is permitted */
555 mm_answer_pwnamallow(int socket, Buffer *m)
558 struct passwd *pwent;
561 debug3("%s", __func__);
563 if (authctxt->attempt++ != 0)
564 fatal("%s: multiple attempts for getpwnam", __func__);
566 login = buffer_get_string(m, NULL);
568 pwent = getpwnamallow(login);
570 authctxt->user = xstrdup(login);
571 setproctitle("%s [priv]", pwent ? login : "unknown");
577 buffer_put_char(m, 0);
582 authctxt->pw = pwent;
585 buffer_put_char(m, 1);
586 buffer_put_string(m, pwent, sizeof(struct passwd));
587 buffer_put_cstring(m, pwent->pw_name);
588 buffer_put_cstring(m, "*");
589 buffer_put_cstring(m, pwent->pw_gecos);
590 #ifdef HAVE_PW_CLASS_IN_PASSWD
591 buffer_put_cstring(m, pwent->pw_class);
593 buffer_put_cstring(m, pwent->pw_dir);
594 buffer_put_cstring(m, pwent->pw_shell);
597 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
598 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
600 /* For SSHv1 allow authentication now */
602 monitor_permit_authentications(1);
604 /* Allow service/style information on the auth context */
605 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
606 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
611 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
617 int mm_answer_auth2_read_banner(int socket, Buffer *m)
622 banner = auth2_read_banner();
623 buffer_put_cstring(m, banner != NULL ? banner : "");
624 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
633 mm_answer_authserv(int socket, Buffer *m)
635 monitor_permit_authentications(1);
637 authctxt->service = buffer_get_string(m, NULL);
638 authctxt->style = buffer_get_string(m, NULL);
639 debug3("%s: service=%s, style=%s",
640 __func__, authctxt->service, authctxt->style);
642 if (strlen(authctxt->style) == 0) {
643 xfree(authctxt->style);
644 authctxt->style = NULL;
651 mm_answer_authpassword(int socket, Buffer *m)
653 static int call_count;
658 passwd = buffer_get_string(m, &plen);
659 /* Only authenticate if the context is valid */
660 authenticated = options.password_authentication &&
661 auth_password(authctxt, passwd) && authctxt->valid;
662 memset(passwd, 0, strlen(passwd));
666 buffer_put_int(m, authenticated);
668 debug3("%s: sending result %d", __func__, authenticated);
669 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
672 if (plen == 0 && call_count == 1)
673 auth_method = "none";
675 auth_method = "password";
677 /* Causes monitor loop to terminate if authenticated */
678 return (authenticated);
683 mm_answer_bsdauthquery(int socket, Buffer *m)
685 char *name, *infotxt;
691 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
692 &prompts, &echo_on) < 0 ? 0 : 1;
695 buffer_put_int(m, success);
697 buffer_put_cstring(m, prompts[0]);
699 debug3("%s: sending challenge success: %u", __func__, success);
700 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
713 mm_answer_bsdauthrespond(int socket, Buffer *m)
718 if (authctxt->as == 0)
719 fatal("%s: no bsd auth session", __func__);
721 response = buffer_get_string(m, NULL);
722 authok = options.challenge_response_authentication &&
723 auth_userresponse(authctxt->as, response, 0);
725 debug3("%s: <%s> = <%d>", __func__, response, authok);
729 buffer_put_int(m, authok);
731 debug3("%s: sending authenticated: %d", __func__, authok);
732 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
734 auth_method = "bsdauth";
736 return (authok != 0);
742 mm_answer_skeyquery(int socket, Buffer *m)
745 char challenge[1024];
748 success = skeychallenge(&skey, authctxt->user, 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(socket, MONITOR_ANS_SKEYQUERY, m);
762 mm_answer_skeyrespond(int socket, 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(socket, MONITOR_ANS_SKEYRESPOND, m);
782 auth_method = "skey";
784 return (authok != 0);
790 mm_answer_pam_start(int socket, Buffer *m)
794 if (!options.use_pam)
795 fatal("UsePAM not set, but ended up in %s anyway", __func__);
797 user = buffer_get_string(m, NULL);
803 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
809 mm_answer_pam_account(int socket, Buffer *m)
813 if (!options.use_pam)
814 fatal("UsePAM not set, but ended up in %s anyway", __func__);
816 ret = do_pam_account();
818 buffer_put_int(m, ret);
820 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
825 static void *sshpam_ctxt, *sshpam_authok;
826 extern KbdintDevice sshpam_device;
829 mm_answer_pam_init_ctx(int socket, Buffer *m)
832 debug3("%s", __func__);
833 authctxt->user = buffer_get_string(m, NULL);
834 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
835 sshpam_authok = NULL;
837 if (sshpam_ctxt != NULL) {
838 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
839 buffer_put_int(m, 1);
841 buffer_put_int(m, 0);
843 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
848 mm_answer_pam_query(int socket, Buffer *m)
850 char *name, *info, **prompts;
854 debug3("%s", __func__);
855 sshpam_authok = NULL;
856 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
857 if (ret == 0 && num == 0)
858 sshpam_authok = sshpam_ctxt;
859 if (num > 1 || name == NULL || info == NULL)
862 buffer_put_int(m, ret);
863 buffer_put_cstring(m, name);
865 buffer_put_cstring(m, info);
867 buffer_put_int(m, num);
868 for (i = 0; i < num; ++i) {
869 buffer_put_cstring(m, prompts[i]);
871 buffer_put_int(m, echo_on[i]);
877 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
882 mm_answer_pam_respond(int socket, Buffer *m)
888 debug3("%s", __func__);
889 sshpam_authok = NULL;
890 num = buffer_get_int(m);
892 resp = xmalloc(num * sizeof(char *));
893 for (i = 0; i < num; ++i)
894 resp[i] = buffer_get_string(m, NULL);
895 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
896 for (i = 0; i < num; ++i)
900 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
903 buffer_put_int(m, ret);
904 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
905 auth_method = "keyboard-interactive/pam";
907 sshpam_authok = sshpam_ctxt;
912 mm_answer_pam_free_ctx(int socket, Buffer *m)
915 debug3("%s", __func__);
916 (sshpam_device.free_ctx)(sshpam_ctxt);
918 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
919 return (sshpam_authok == sshpam_ctxt);
924 mm_append_debug(Buffer *m)
926 if (auth_debug_init && buffer_len(&auth_debug)) {
927 debug3("%s: Appending debug messages for child", __func__);
928 buffer_append(m, buffer_ptr(&auth_debug),
929 buffer_len(&auth_debug));
930 buffer_clear(&auth_debug);
935 mm_answer_keyallowed(int socket, Buffer *m)
941 enum mm_keytype type = 0;
944 debug3("%s entering", __func__);
946 type = buffer_get_int(m);
947 cuser = buffer_get_string(m, NULL);
948 chost = buffer_get_string(m, NULL);
949 blob = buffer_get_string(m, &bloblen);
951 key = key_from_blob(blob, bloblen);
953 if ((compat20 && type == MM_RSAHOSTKEY) ||
954 (!compat20 && type != MM_RSAHOSTKEY))
955 fatal("%s: key type and protocol mismatch", __func__);
957 debug3("%s: key_from_blob: %p", __func__, key);
959 if (key != NULL && authctxt->pw != NULL) {
962 allowed = options.pubkey_authentication &&
963 user_key_allowed(authctxt->pw, key);
966 allowed = options.hostbased_authentication &&
967 hostbased_key_allowed(authctxt->pw,
971 key->type = KEY_RSA1; /* XXX */
972 allowed = options.rhosts_rsa_authentication &&
973 auth_rhosts_rsa_key_allowed(authctxt->pw,
977 fatal("%s: unknown key type %d", __func__, type);
984 /* clear temporarily storage (used by verify) */
985 monitor_reset_key_state();
988 /* Save temporarily for comparison in verify */
990 key_bloblen = bloblen;
992 hostbased_cuser = cuser;
993 hostbased_chost = chost;
996 debug3("%s: key %p is %s",
997 __func__, key, allowed ? "allowed" : "disallowed");
1000 buffer_put_int(m, allowed);
1001 buffer_put_int(m, forced_command != NULL);
1005 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
1007 if (type == MM_RSAHOSTKEY)
1008 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1014 monitor_valid_userblob(u_char *data, u_int datalen)
1022 buffer_append(&b, data, datalen);
1024 if (datafellows & SSH_OLD_SESSIONID) {
1026 len = buffer_len(&b);
1027 if ((session_id2 == NULL) ||
1028 (len < session_id2_len) ||
1029 (memcmp(p, session_id2, session_id2_len) != 0))
1031 buffer_consume(&b, session_id2_len);
1033 p = buffer_get_string(&b, &len);
1034 if ((session_id2 == NULL) ||
1035 (len != session_id2_len) ||
1036 (memcmp(p, session_id2, session_id2_len) != 0))
1040 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1042 p = buffer_get_string(&b, NULL);
1043 if (strcmp(authctxt->user, p) != 0) {
1044 logit("wrong user name passed to monitor: expected %s != %.100s",
1049 buffer_skip_string(&b);
1050 if (datafellows & SSH_BUG_PKAUTH) {
1051 if (!buffer_get_char(&b))
1054 p = buffer_get_string(&b, NULL);
1055 if (strcmp("publickey", p) != 0)
1058 if (!buffer_get_char(&b))
1060 buffer_skip_string(&b);
1062 buffer_skip_string(&b);
1063 if (buffer_len(&b) != 0)
1070 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1079 buffer_append(&b, data, datalen);
1081 p = buffer_get_string(&b, &len);
1082 if ((session_id2 == NULL) ||
1083 (len != session_id2_len) ||
1084 (memcmp(p, session_id2, session_id2_len) != 0))
1088 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
1090 p = buffer_get_string(&b, NULL);
1091 if (strcmp(authctxt->user, p) != 0) {
1092 logit("wrong user name passed to monitor: expected %s != %.100s",
1097 buffer_skip_string(&b); /* service */
1098 p = buffer_get_string(&b, NULL);
1099 if (strcmp(p, "hostbased") != 0)
1102 buffer_skip_string(&b); /* pkalg */
1103 buffer_skip_string(&b); /* pkblob */
1105 /* verify client host, strip trailing dot if necessary */
1106 p = buffer_get_string(&b, NULL);
1107 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1109 if (strcmp(p, chost) != 0)
1113 /* verify client user */
1114 p = buffer_get_string(&b, NULL);
1115 if (strcmp(p, cuser) != 0)
1119 if (buffer_len(&b) != 0)
1126 mm_answer_keyverify(int socket, Buffer *m)
1129 u_char *signature, *data, *blob;
1130 u_int signaturelen, datalen, bloblen;
1134 blob = buffer_get_string(m, &bloblen);
1135 signature = buffer_get_string(m, &signaturelen);
1136 data = buffer_get_string(m, &datalen);
1138 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1139 !monitor_allowed_key(blob, bloblen))
1140 fatal("%s: bad key, not previously allowed", __func__);
1142 key = key_from_blob(blob, bloblen);
1144 fatal("%s: bad public key blob", __func__);
1146 switch (key_blobtype) {
1148 valid_data = monitor_valid_userblob(data, datalen);
1151 valid_data = monitor_valid_hostbasedblob(data, datalen,
1152 hostbased_cuser, hostbased_chost);
1159 fatal("%s: bad signature data blob", __func__);
1161 verified = key_verify(key, signature, signaturelen, data, datalen);
1162 debug3("%s: key %p signature %s",
1163 __func__, key, verified ? "verified" : "unverified");
1170 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1172 monitor_reset_key_state();
1175 buffer_put_int(m, verified);
1176 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1182 mm_record_login(Session *s, struct passwd *pw)
1185 struct sockaddr_storage from;
1188 * Get IP address of client. If the connection is not a socket, let
1189 * the address be 0.0.0.0.
1191 memset(&from, 0, sizeof(from));
1192 fromlen = sizeof(from);
1193 if (packet_connection_is_on_socket()) {
1194 if (getpeername(packet_get_connection_in(),
1195 (struct sockaddr *) & from, &fromlen) < 0) {
1196 debug("getpeername: %.100s", strerror(errno));
1200 /* Record that there was a login on that tty from the remote host. */
1201 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1202 get_remote_name_or_ip(utmp_len, options.use_dns),
1203 (struct sockaddr *)&from, fromlen);
1207 mm_session_close(Session *s)
1209 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
1210 if (s->ttyfd != -1) {
1211 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1212 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1213 session_pty_cleanup2(s);
1219 mm_answer_pty(int socket, Buffer *m)
1221 extern struct monitor *pmonitor;
1225 debug3("%s entering", __func__);
1231 s->authctxt = authctxt;
1232 s->pw = authctxt->pw;
1233 s->pid = pmonitor->m_pid;
1234 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1237 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1238 pty_setowner(authctxt->pw, s->tty);
1240 buffer_put_int(m, 1);
1241 buffer_put_cstring(m, s->tty);
1242 mm_request_send(socket, MONITOR_ANS_PTY, m);
1244 mm_send_fd(socket, s->ptyfd);
1245 mm_send_fd(socket, s->ttyfd);
1247 /* We need to trick ttyslot */
1248 if (dup2(s->ttyfd, 0) == -1)
1249 fatal("%s: dup2", __func__);
1251 mm_record_login(s, authctxt->pw);
1253 /* Now we can close the file descriptor again */
1256 /* make sure nothing uses fd 0 */
1257 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1258 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1260 error("%s: fd0 %d != 0", __func__, fd0);
1262 /* slave is not needed */
1264 s->ttyfd = s->ptyfd;
1265 /* no need to dup() because nobody closes ptyfd */
1266 s->ptymaster = s->ptyfd;
1268 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1274 mm_session_close(s);
1275 buffer_put_int(m, 0);
1276 mm_request_send(socket, MONITOR_ANS_PTY, m);
1281 mm_answer_pty_cleanup(int socket, Buffer *m)
1286 debug3("%s entering", __func__);
1288 tty = buffer_get_string(m, NULL);
1289 if ((s = session_by_tty(tty)) != NULL)
1290 mm_session_close(s);
1297 mm_answer_sesskey(int socket, Buffer *m)
1302 /* Turn off permissions */
1303 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1305 if ((p = BN_new()) == NULL)
1306 fatal("%s: BN_new", __func__);
1308 buffer_get_bignum2(m, p);
1310 rsafail = ssh1_session_key(p);
1313 buffer_put_int(m, rsafail);
1314 buffer_put_bignum2(m, p);
1318 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1320 /* Turn on permissions for sessid passing */
1321 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1327 mm_answer_sessid(int socket, Buffer *m)
1331 debug3("%s entering", __func__);
1333 if (buffer_len(m) != 16)
1334 fatal("%s: bad ssh1 session id", __func__);
1335 for (i = 0; i < 16; i++)
1336 session_id[i] = buffer_get_char(m);
1338 /* Turn on permissions for getpwnam */
1339 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1345 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1349 u_char *blob = NULL;
1353 debug3("%s entering", __func__);
1355 if (options.rsa_authentication && authctxt->valid) {
1356 if ((client_n = BN_new()) == NULL)
1357 fatal("%s: BN_new", __func__);
1358 buffer_get_bignum2(m, client_n);
1359 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1360 BN_clear_free(client_n);
1363 buffer_put_int(m, allowed);
1364 buffer_put_int(m, forced_command != NULL);
1366 /* clear temporarily storage (used by generate challenge) */
1367 monitor_reset_key_state();
1369 if (allowed && key != NULL) {
1370 key->type = KEY_RSA; /* cheat for key_to_blob */
1371 if (key_to_blob(key, &blob, &blen) == 0)
1372 fatal("%s: key_to_blob failed", __func__);
1373 buffer_put_string(m, blob, blen);
1375 /* Save temporarily for comparison in verify */
1378 key_blobtype = MM_RSAUSERKEY;
1385 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1387 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1388 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1393 mm_answer_rsa_challenge(int socket, Buffer *m)
1399 debug3("%s entering", __func__);
1401 if (!authctxt->valid)
1402 fatal("%s: authctxt not valid", __func__);
1403 blob = buffer_get_string(m, &blen);
1404 if (!monitor_allowed_key(blob, blen))
1405 fatal("%s: bad key, not previously allowed", __func__);
1406 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1407 fatal("%s: key type mismatch", __func__);
1408 if ((key = key_from_blob(blob, blen)) == NULL)
1409 fatal("%s: received bad key", __func__);
1412 BN_clear_free(ssh1_challenge);
1413 ssh1_challenge = auth_rsa_generate_challenge(key);
1416 buffer_put_bignum2(m, ssh1_challenge);
1418 debug3("%s sending reply", __func__);
1419 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1421 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1429 mm_answer_rsa_response(int socket, Buffer *m)
1432 u_char *blob, *response;
1436 debug3("%s entering", __func__);
1438 if (!authctxt->valid)
1439 fatal("%s: authctxt not valid", __func__);
1440 if (ssh1_challenge == NULL)
1441 fatal("%s: no ssh1_challenge", __func__);
1443 blob = buffer_get_string(m, &blen);
1444 if (!monitor_allowed_key(blob, blen))
1445 fatal("%s: bad key, not previously allowed", __func__);
1446 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1447 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1448 if ((key = key_from_blob(blob, blen)) == NULL)
1449 fatal("%s: received bad key", __func__);
1450 response = buffer_get_string(m, &len);
1452 fatal("%s: received bad response to challenge", __func__);
1453 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1459 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1462 BN_clear_free(ssh1_challenge);
1463 ssh1_challenge = NULL;
1464 monitor_reset_key_state();
1467 buffer_put_int(m, success);
1468 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1475 mm_answer_krb5(int socket, Buffer *m)
1477 krb5_data tkt, reply;
1482 /* use temporary var to avoid size issues on 64bit arch */
1483 tkt.data = buffer_get_string(m, &len);
1486 success = options.kerberos_authentication &&
1488 auth_krb5(authctxt, &tkt, &client_user, &reply);
1494 buffer_put_int(m, success);
1497 buffer_put_cstring(m, client_user);
1498 buffer_put_string(m, reply.data, reply.length);
1504 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1506 auth_method = "kerberos";
1513 mm_answer_term(int socket, Buffer *req)
1515 extern struct monitor *pmonitor;
1518 debug3("%s: tearing down sessions", __func__);
1520 /* The child is terminating */
1521 session_destroy_all(&mm_session_close);
1523 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1527 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1529 /* Terminate process */
1534 monitor_apply_keystate(struct monitor *pmonitor)
1537 set_newkeys(MODE_IN);
1538 set_newkeys(MODE_OUT);
1540 packet_set_protocol_flags(child_state.ssh1protoflags);
1541 packet_set_encryption_key(child_state.ssh1key,
1542 child_state.ssh1keylen, child_state.ssh1cipher);
1543 xfree(child_state.ssh1key);
1546 /* for rc4 and other stateful ciphers */
1547 packet_set_keycontext(MODE_OUT, child_state.keyout);
1548 xfree(child_state.keyout);
1549 packet_set_keycontext(MODE_IN, child_state.keyin);
1550 xfree(child_state.keyin);
1553 packet_set_iv(MODE_OUT, child_state.ivout);
1554 xfree(child_state.ivout);
1555 packet_set_iv(MODE_IN, child_state.ivin);
1556 xfree(child_state.ivin);
1559 memcpy(&incoming_stream, &child_state.incoming,
1560 sizeof(incoming_stream));
1561 memcpy(&outgoing_stream, &child_state.outgoing,
1562 sizeof(outgoing_stream));
1564 /* Update with new address */
1565 if (options.compression)
1566 mm_init_compression(pmonitor->m_zlib);
1568 /* Network I/O buffers */
1569 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1570 buffer_clear(&input);
1571 buffer_append(&input, child_state.input, child_state.ilen);
1572 memset(child_state.input, 0, child_state.ilen);
1573 xfree(child_state.input);
1575 buffer_clear(&output);
1576 buffer_append(&output, child_state.output, child_state.olen);
1577 memset(child_state.output, 0, child_state.olen);
1578 xfree(child_state.output);
1582 mm_get_kex(Buffer *m)
1588 kex = xmalloc(sizeof(*kex));
1589 memset(kex, 0, sizeof(*kex));
1590 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1591 if ((session_id2 == NULL) ||
1592 (kex->session_id_len != session_id2_len) ||
1593 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1594 fatal("mm_get_get: internal error: bad session id");
1595 kex->we_need = buffer_get_int(m);
1596 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1597 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1599 kex->hostkey_type = buffer_get_int(m);
1600 kex->kex_type = buffer_get_int(m);
1601 blob = buffer_get_string(m, &bloblen);
1602 buffer_init(&kex->my);
1603 buffer_append(&kex->my, blob, bloblen);
1605 blob = buffer_get_string(m, &bloblen);
1606 buffer_init(&kex->peer);
1607 buffer_append(&kex->peer, blob, bloblen);
1610 kex->flags = buffer_get_int(m);
1611 kex->client_version_string = buffer_get_string(m, NULL);
1612 kex->server_version_string = buffer_get_string(m, NULL);
1613 kex->load_host_key=&get_hostkey_by_type;
1614 kex->host_key_index=&get_hostkey_index;
1619 /* This function requries careful sanity checking */
1622 mm_get_keystate(struct monitor *pmonitor)
1626 u_int bloblen, plen;
1627 u_int32_t seqnr, packets;
1630 debug3("%s: Waiting for new keys", __func__);
1633 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1635 child_state.ssh1protoflags = buffer_get_int(&m);
1636 child_state.ssh1cipher = buffer_get_int(&m);
1637 child_state.ssh1key = buffer_get_string(&m,
1638 &child_state.ssh1keylen);
1639 child_state.ivout = buffer_get_string(&m,
1640 &child_state.ivoutlen);
1641 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1644 /* Get the Kex for rekeying */
1645 *pmonitor->m_pkex = mm_get_kex(&m);
1648 blob = buffer_get_string(&m, &bloblen);
1649 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1652 debug3("%s: Waiting for second key", __func__);
1653 blob = buffer_get_string(&m, &bloblen);
1654 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1657 /* Now get sequence numbers for the packets */
1658 seqnr = buffer_get_int(&m);
1659 blocks = buffer_get_int64(&m);
1660 packets = buffer_get_int(&m);
1661 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1662 seqnr = buffer_get_int(&m);
1663 blocks = buffer_get_int64(&m);
1664 packets = buffer_get_int(&m);
1665 packet_set_state(MODE_IN, seqnr, blocks, packets);
1668 /* Get the key context */
1669 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1670 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1672 debug3("%s: Getting compression state", __func__);
1673 /* Get compression state */
1674 p = buffer_get_string(&m, &plen);
1675 if (plen != sizeof(child_state.outgoing))
1676 fatal("%s: bad request size", __func__);
1677 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1680 p = buffer_get_string(&m, &plen);
1681 if (plen != sizeof(child_state.incoming))
1682 fatal("%s: bad request size", __func__);
1683 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1686 /* Network I/O buffers */
1687 debug3("%s: Getting Network I/O buffers", __func__);
1688 child_state.input = buffer_get_string(&m, &child_state.ilen);
1689 child_state.output = buffer_get_string(&m, &child_state.olen);
1695 /* Allocation functions for zlib */
1697 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1699 size_t len = (size_t) size * ncount;
1702 if (len == 0 || ncount > SIZE_T_MAX / size)
1703 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1705 address = mm_malloc(mm, len);
1711 mm_zfree(struct mm_master *mm, void *address)
1713 mm_free(mm, address);
1717 mm_init_compression(struct mm_master *mm)
1719 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1720 outgoing_stream.zfree = (free_func)mm_zfree;
1721 outgoing_stream.opaque = mm;
1723 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1724 incoming_stream.zfree = (free_func)mm_zfree;
1725 incoming_stream.opaque = mm;
1730 #define FD_CLOSEONEXEC(x) do { \
1731 if (fcntl(x, F_SETFD, 1) == -1) \
1732 fatal("fcntl(%d, F_SETFD)", x); \
1736 monitor_socketpair(int *pair)
1738 #ifdef HAVE_SOCKETPAIR
1739 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1740 fatal("%s: socketpair", __func__);
1742 fatal("%s: UsePrivilegeSeparation=yes not supported",
1745 FD_CLOSEONEXEC(pair[0]);
1746 FD_CLOSEONEXEC(pair[1]);
1749 #define MM_MEMSIZE 65536
1754 struct monitor *mon;
1757 mon = xmalloc(sizeof(*mon));
1759 monitor_socketpair(pair);
1761 mon->m_recvfd = pair[0];
1762 mon->m_sendfd = pair[1];
1764 /* Used to share zlib space across processes */
1765 if (options.compression) {
1766 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1767 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1769 /* Compression needs to share state across borders */
1770 mm_init_compression(mon->m_zlib);
1777 monitor_reinit(struct monitor *mon)
1781 monitor_socketpair(pair);
1783 mon->m_recvfd = pair[0];
1784 mon->m_sendfd = pair[1];
1789 mm_answer_gss_setup_ctx(int socket, Buffer *m)
1795 oid.elements = buffer_get_string(m, &len);
1798 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1800 xfree(oid.elements);
1803 buffer_put_int(m, major);
1805 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1807 /* Now we have a context, enable the step */
1808 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1814 mm_answer_gss_accept_ctx(int socket, Buffer *m)
1817 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1818 OM_uint32 major,minor;
1819 OM_uint32 flags = 0; /* GSI needs this */
1822 in.value = buffer_get_string(m, &len);
1824 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1828 buffer_put_int(m, major);
1829 buffer_put_string(m, out.value, out.length);
1830 buffer_put_int(m, flags);
1831 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1833 gss_release_buffer(&minor, &out);
1835 /* Complete - now we can do signing */
1836 if (major==GSS_S_COMPLETE) {
1837 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1838 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1844 mm_answer_gss_userok(int socket, Buffer *m)
1848 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1851 buffer_put_int(m, authenticated);
1853 debug3("%s: sending result %d", __func__, authenticated);
1854 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1856 auth_method="gssapi";
1858 /* Monitor loop will terminate if authenticated */
1859 return (authenticated);