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.36 2003/04/01 10:22:21 markus 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 montior 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 *);
129 int mm_answer_krb4(int, Buffer *);
132 int mm_answer_krb5(int, Buffer *);
136 int mm_answer_gss_setup_ctx(int, Buffer *);
137 int mm_answer_gss_accept_ctx(int, Buffer *);
138 int mm_answer_gss_userok(int, Buffer *);
139 int mm_answer_gss_sign(int, Buffer *);
140 int mm_answer_gss_error(int, Buffer *);
141 int mm_answer_gss_indicate_mechs(int, Buffer *);
142 int mm_answer_gss_localname(int, Buffer *);
146 int mm_answer_gsi_gridmap(int, Buffer *);
149 static Authctxt *authctxt;
150 static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */
152 /* local state for key verify */
153 static u_char *key_blob = NULL;
154 static u_int key_bloblen = 0;
155 static int key_blobtype = MM_NOKEY;
156 static char *hostbased_cuser = NULL;
157 static char *hostbased_chost = NULL;
158 static char *auth_method = "unknown";
159 static int session_id2_len = 0;
160 static u_char *session_id2 = NULL;
163 enum monitor_reqtype type;
165 int (*f)(int, Buffer *);
168 #define MON_ISAUTH 0x0004 /* Required for Authentication */
169 #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
170 #define MON_ONCE 0x0010 /* Disable after calling */
172 #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
174 #define MON_PERMIT 0x1000 /* Request is permitted */
176 struct mon_table mon_dispatch_proto20[] = {
177 {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
178 {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
179 {MONITOR_REQ_PWNAM, MON_AUTH, mm_answer_pwnamallow},
180 {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
181 {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
182 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
184 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
187 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
188 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
191 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
192 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
195 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
196 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
197 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
198 {MONITOR_REQ_GSSERR, MON_ISAUTH | MON_ONCE, mm_answer_gss_error},
199 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
200 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
201 {MONITOR_REQ_GSSLOCALNAME, MON_ISAUTH, mm_answer_gss_localname},
203 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
204 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
208 struct mon_table mon_dispatch_postauth20[] = {
210 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
211 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
212 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
213 {MONITOR_REQ_GSSERR, 0, mm_answer_gss_error},
214 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
216 {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
217 {MONITOR_REQ_SIGN, 0, mm_answer_sign},
218 {MONITOR_REQ_PTY, 0, mm_answer_pty},
219 {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
220 {MONITOR_REQ_TERM, 0, mm_answer_term},
224 struct mon_table mon_dispatch_proto15[] = {
225 {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
226 {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
227 {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
228 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
229 {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
230 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
231 {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
232 {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
234 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
235 {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
238 {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
239 {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
242 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
243 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
244 {MONITOR_REQ_GSSSIGN, MON_ONCE, mm_answer_gss_sign},
245 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
246 {MONITOR_REQ_GSSMECHS, MON_ISAUTH, mm_answer_gss_indicate_mechs},
249 {MONITOR_REQ_GSIGRIDMAP, MON_PERMIT, mm_answer_gsi_gridmap},
252 {MONITOR_REQ_PAM_START, MON_ISAUTH, mm_answer_pam_start},
255 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
258 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
263 struct mon_table mon_dispatch_postauth15[] = {
265 {MONITOR_REQ_GSSSETUP, 0, mm_answer_gss_setup_ctx},
266 {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
267 {MONITOR_REQ_GSSSIGN, 0, mm_answer_gss_sign},
268 {MONITOR_REQ_GSSMECHS, 0, mm_answer_gss_indicate_mechs},
270 {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
271 {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
272 {MONITOR_REQ_TERM, 0, mm_answer_term},
276 struct mon_table *mon_dispatch;
278 /* Specifies if a certain message is allowed at the moment */
281 monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
283 while (ent->f != NULL) {
284 if (ent->type == type) {
285 ent->flags &= ~MON_PERMIT;
286 ent->flags |= permit ? MON_PERMIT : 0;
294 monitor_permit_authentications(int permit)
296 struct mon_table *ent = mon_dispatch;
298 while (ent->f != NULL) {
299 if (ent->flags & MON_AUTH) {
300 ent->flags &= ~MON_PERMIT;
301 ent->flags |= permit ? MON_PERMIT : 0;
308 monitor_child_preauth(struct monitor *pmonitor)
310 struct mon_table *ent;
311 int authenticated = 0;
313 debug3("preauth child monitor started");
316 mon_dispatch = mon_dispatch_proto20;
318 /* Permit requests for moduli and signatures */
319 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
320 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
322 /* and for the GSSAPI key exchange */
323 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP, 1);
324 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
325 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
328 mon_dispatch = mon_dispatch_proto15;
330 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
332 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS, 1);
333 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
336 monitor_permit(mon_dispatch, MONITOR_REQ_GSIGRIDMAP, 1);
340 authctxt = authctxt_new();
342 /* The first few requests do not require asynchronous access */
343 while (!authenticated) {
344 authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
346 if (!(ent->flags & MON_AUTHDECIDE))
347 fatal("%s: unexpected authentication from %d",
348 __func__, ent->type);
349 if (authctxt->pw->pw_uid == 0 &&
350 !auth_root_allowed(auth_method))
353 if (!do_pam_account(authctxt->pw->pw_name, NULL))
358 if (ent->flags & MON_AUTHDECIDE) {
359 auth_log(authctxt, authenticated, auth_method,
360 compat20 ? " ssh2" : "");
362 authctxt->failures++;
366 if (!authctxt->valid)
367 fatal("%s: authenticated invalid user", __func__);
369 debug("%s: %s has been authenticated by privileged process",
370 __func__, authctxt->user);
372 mm_get_keystate(pmonitor);
378 monitor_child_postauth(struct monitor *pmonitor)
381 mon_dispatch = mon_dispatch_postauth20;
383 /* Permit requests for moduli and signatures */
384 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
385 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
386 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
389 /* and for the GSSAPI key exchange */
390 monitor_permit(mon_dispatch, MONITOR_REQ_GSSMECHS,1);
391 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSETUP,1);
392 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR,1);
396 mon_dispatch = mon_dispatch_postauth15;
397 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
400 monitor_permit(mon_dispatch, MONITOR_REQ_GSSERR, 1);
403 monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
404 monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
408 monitor_read(pmonitor, mon_dispatch, NULL);
412 monitor_sync(struct monitor *pmonitor)
414 if (options.compression) {
415 /* The member allocation is not visible, so sync it */
416 mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
421 monitor_read(struct monitor *pmonitor, struct mon_table *ent,
422 struct mon_table **pent)
430 mm_request_receive(pmonitor->m_sendfd, &m);
431 type = buffer_get_char(&m);
433 debug3("%s: checking request %d", __func__, type);
435 while (ent->f != NULL) {
436 if (ent->type == type)
441 if (ent->f != NULL) {
442 if (!(ent->flags & MON_PERMIT))
443 fatal("%s: unpermitted request %d", __func__,
445 ret = (*ent->f)(pmonitor->m_sendfd, &m);
448 /* The child may use this request only once, disable it */
449 if (ent->flags & MON_ONCE) {
450 debug2("%s: %d used once, disabling now", __func__,
452 ent->flags &= ~MON_PERMIT;
461 fatal("%s: unsupported request: %d", __func__, type);
467 /* allowed key state */
469 monitor_allowed_key(u_char *blob, u_int bloblen)
471 /* make sure key is allowed */
472 if (key_blob == NULL || key_bloblen != bloblen ||
473 memcmp(key_blob, blob, key_bloblen))
479 monitor_reset_key_state(void)
482 if (key_blob != NULL)
484 if (hostbased_cuser != NULL)
485 xfree(hostbased_cuser);
486 if (hostbased_chost != NULL)
487 xfree(hostbased_chost);
490 key_blobtype = MM_NOKEY;
491 hostbased_cuser = NULL;
492 hostbased_chost = NULL;
496 mm_answer_moduli(int socket, Buffer *m)
501 min = buffer_get_int(m);
502 want = buffer_get_int(m);
503 max = buffer_get_int(m);
505 debug3("%s: got parameters: %d %d %d",
506 __func__, min, want, max);
507 /* We need to check here, too, in case the child got corrupted */
508 if (max < min || want < min || max < want)
509 fatal("%s: bad parameters: %d %d %d",
510 __func__, min, want, max);
514 dh = choose_dh(min, want, max);
516 buffer_put_char(m, 0);
519 /* Send first bignum */
520 buffer_put_char(m, 1);
521 buffer_put_bignum2(m, dh->p);
522 buffer_put_bignum2(m, dh->g);
526 mm_request_send(socket, MONITOR_ANS_MODULI, m);
531 mm_answer_sign(int socket, Buffer *m)
536 u_int siglen, datlen;
539 debug3("%s", __func__);
541 keyid = buffer_get_int(m);
542 p = buffer_get_string(m, &datlen);
545 fatal("%s: data length incorrect: %u", __func__, datlen);
547 /* save session id, it will be passed on the first call */
548 if (session_id2_len == 0) {
549 session_id2_len = datlen;
550 session_id2 = xmalloc(session_id2_len);
551 memcpy(session_id2, p, session_id2_len);
554 if ((key = get_hostkey_by_index(keyid)) == NULL)
555 fatal("%s: no hostkey from index %d", __func__, keyid);
556 if (key_sign(key, &signature, &siglen, p, datlen) < 0)
557 fatal("%s: key_sign failed", __func__);
559 debug3("%s: signature %p(%u)", __func__, signature, siglen);
562 buffer_put_string(m, signature, siglen);
567 mm_request_send(socket, MONITOR_ANS_SIGN, m);
569 /* Turn on permissions for getpwnam */
570 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
575 /* Retrieves the password entry and also checks if the user is permitted */
578 mm_answer_pwnamallow(int socket, Buffer *m)
581 struct passwd *pwent;
584 debug3("%s", __func__);
586 login = buffer_get_string(m, NULL);
588 pwent = getpwnamallow(login);
590 if (authctxt->user) xfree(authctxt->user);
591 authctxt->user = xstrdup(login);
592 setproctitle("%s [priv]", pwent ? login : "unknown");
598 buffer_put_char(m, 0);
603 authctxt->pw = pwent;
606 buffer_put_char(m, 1);
607 buffer_put_string(m, pwent, sizeof(struct passwd));
608 buffer_put_cstring(m, pwent->pw_name);
609 buffer_put_cstring(m, "*");
610 buffer_put_cstring(m, pwent->pw_gecos);
611 #ifdef HAVE_PW_CLASS_IN_PASSWD
612 buffer_put_cstring(m, pwent->pw_class);
614 buffer_put_cstring(m, pwent->pw_dir);
615 buffer_put_cstring(m, pwent->pw_shell);
618 debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
619 mm_request_send(socket, MONITOR_ANS_PWNAM, m);
621 /* For SSHv1 allow authentication now */
623 monitor_permit_authentications(1);
625 /* Allow service/style information on the auth context */
626 monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
627 monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
631 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
637 int mm_answer_auth2_read_banner(int socket, Buffer *m)
642 banner = auth2_read_banner();
643 buffer_put_cstring(m, banner != NULL ? banner : "");
644 mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
653 mm_answer_authserv(int socket, Buffer *m)
655 monitor_permit_authentications(1);
657 authctxt->service = buffer_get_string(m, NULL);
658 authctxt->style = buffer_get_string(m, NULL);
659 debug3("%s: service=%s, style=%s",
660 __func__, authctxt->service, authctxt->style);
662 if (strlen(authctxt->style) == 0) {
663 xfree(authctxt->style);
664 authctxt->style = NULL;
671 mm_answer_authpassword(int socket, Buffer *m)
673 static int call_count;
678 passwd = buffer_get_string(m, &plen);
679 /* Only authenticate if the context is valid */
680 authenticated = options.password_authentication &&
681 auth_password(authctxt, passwd) && authctxt->valid;
682 memset(passwd, 0, strlen(passwd));
686 buffer_put_int(m, authenticated);
688 debug3("%s: sending result %d", __func__, authenticated);
689 mm_request_send(socket, MONITOR_ANS_AUTHPASSWORD, m);
692 if (plen == 0 && call_count == 1)
693 auth_method = "none";
695 auth_method = "password";
697 /* Causes monitor loop to terminate if authenticated */
698 return (authenticated);
703 mm_answer_bsdauthquery(int socket, Buffer *m)
705 char *name, *infotxt;
711 success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
712 &prompts, &echo_on) < 0 ? 0 : 1;
715 buffer_put_int(m, success);
717 buffer_put_cstring(m, prompts[0]);
719 debug3("%s: sending challenge success: %u", __func__, success);
720 mm_request_send(socket, MONITOR_ANS_BSDAUTHQUERY, m);
733 mm_answer_bsdauthrespond(int socket, Buffer *m)
738 if (authctxt->as == 0)
739 fatal("%s: no bsd auth session", __func__);
741 response = buffer_get_string(m, NULL);
742 authok = options.challenge_response_authentication &&
743 auth_userresponse(authctxt->as, response, 0);
745 debug3("%s: <%s> = <%d>", __func__, response, authok);
749 buffer_put_int(m, authok);
751 debug3("%s: sending authenticated: %d", __func__, authok);
752 mm_request_send(socket, MONITOR_ANS_BSDAUTHRESPOND, m);
754 auth_method = "bsdauth";
756 return (authok != 0);
762 mm_answer_skeyquery(int socket, Buffer *m)
765 char challenge[1024];
768 success = skeychallenge(&skey, authctxt->user, challenge) < 0 ? 0 : 1;
771 buffer_put_int(m, success);
773 buffer_put_cstring(m, challenge);
775 debug3("%s: sending challenge success: %u", __func__, success);
776 mm_request_send(socket, MONITOR_ANS_SKEYQUERY, m);
782 mm_answer_skeyrespond(int socket, Buffer *m)
787 response = buffer_get_string(m, NULL);
789 authok = (options.challenge_response_authentication &&
791 skey_haskey(authctxt->pw->pw_name) == 0 &&
792 skey_passcheck(authctxt->pw->pw_name, response) != -1);
797 buffer_put_int(m, authok);
799 debug3("%s: sending authenticated: %d", __func__, authok);
800 mm_request_send(socket, MONITOR_ANS_SKEYRESPOND, m);
802 auth_method = "skey";
804 return (authok != 0);
810 mm_answer_pam_start(int socket, Buffer *m)
814 user = buffer_get_string(m, NULL);
825 mm_append_debug(Buffer *m)
827 if (auth_debug_init && buffer_len(&auth_debug)) {
828 debug3("%s: Appending debug messages for child", __func__);
829 buffer_append(m, buffer_ptr(&auth_debug),
830 buffer_len(&auth_debug));
831 buffer_clear(&auth_debug);
836 mm_answer_keyallowed(int socket, Buffer *m)
842 enum mm_keytype type = 0;
845 debug3("%s entering", __func__);
847 type = buffer_get_int(m);
848 cuser = buffer_get_string(m, NULL);
849 chost = buffer_get_string(m, NULL);
850 blob = buffer_get_string(m, &bloblen);
852 key = key_from_blob(blob, bloblen);
854 if ((compat20 && type == MM_RSAHOSTKEY) ||
855 (!compat20 && type != MM_RSAHOSTKEY))
856 fatal("%s: key type and protocol mismatch", __func__);
858 debug3("%s: key_from_blob: %p", __func__, key);
860 if (key != NULL && authctxt->pw != NULL) {
863 allowed = options.pubkey_authentication &&
864 user_key_allowed(authctxt->pw, key);
867 allowed = options.hostbased_authentication &&
868 hostbased_key_allowed(authctxt->pw,
872 key->type = KEY_RSA1; /* XXX */
873 allowed = options.rhosts_rsa_authentication &&
874 auth_rhosts_rsa_key_allowed(authctxt->pw,
878 fatal("%s: unknown key type %d", __func__, type);
885 /* clear temporarily storage (used by verify) */
886 monitor_reset_key_state();
889 /* Save temporarily for comparison in verify */
891 key_bloblen = bloblen;
893 hostbased_cuser = cuser;
894 hostbased_chost = chost;
897 debug3("%s: key %p is %s",
898 __func__, key, allowed ? "allowed" : "disallowed");
901 buffer_put_int(m, allowed);
902 buffer_put_int(m, forced_command != NULL);
906 mm_request_send(socket, MONITOR_ANS_KEYALLOWED, m);
908 if (type == MM_RSAHOSTKEY)
909 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
915 monitor_valid_userblob(u_char *data, u_int datalen)
923 buffer_append(&b, data, datalen);
925 if (datafellows & SSH_OLD_SESSIONID) {
927 len = buffer_len(&b);
928 if ((session_id2 == NULL) ||
929 (len < session_id2_len) ||
930 (memcmp(p, session_id2, session_id2_len) != 0))
932 buffer_consume(&b, session_id2_len);
934 p = buffer_get_string(&b, &len);
935 if ((session_id2 == NULL) ||
936 (len != session_id2_len) ||
937 (memcmp(p, session_id2, session_id2_len) != 0))
941 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
943 p = buffer_get_string(&b, NULL);
944 if (strcmp(authctxt->user, p) != 0) {
945 log("wrong user name passed to monitor: expected %s != %.100s",
950 buffer_skip_string(&b);
951 if (datafellows & SSH_BUG_PKAUTH) {
952 if (!buffer_get_char(&b))
955 p = buffer_get_string(&b, NULL);
956 if (strcmp("publickey", p) != 0)
959 if (!buffer_get_char(&b))
961 buffer_skip_string(&b);
963 buffer_skip_string(&b);
964 if (buffer_len(&b) != 0)
971 monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
980 buffer_append(&b, data, datalen);
982 p = buffer_get_string(&b, &len);
983 if ((session_id2 == NULL) ||
984 (len != session_id2_len) ||
985 (memcmp(p, session_id2, session_id2_len) != 0))
989 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
991 p = buffer_get_string(&b, NULL);
992 if (strcmp(authctxt->user, p) != 0) {
993 log("wrong user name passed to monitor: expected %s != %.100s",
998 buffer_skip_string(&b); /* service */
999 p = buffer_get_string(&b, NULL);
1000 if (strcmp(p, "hostbased") != 0)
1003 buffer_skip_string(&b); /* pkalg */
1004 buffer_skip_string(&b); /* pkblob */
1006 /* verify client host, strip trailing dot if necessary */
1007 p = buffer_get_string(&b, NULL);
1008 if (((len = strlen(p)) > 0) && p[len - 1] == '.')
1010 if (strcmp(p, chost) != 0)
1014 /* verify client user */
1015 p = buffer_get_string(&b, NULL);
1016 if (strcmp(p, cuser) != 0)
1020 if (buffer_len(&b) != 0)
1027 mm_answer_keyverify(int socket, Buffer *m)
1030 u_char *signature, *data, *blob;
1031 u_int signaturelen, datalen, bloblen;
1035 blob = buffer_get_string(m, &bloblen);
1036 signature = buffer_get_string(m, &signaturelen);
1037 data = buffer_get_string(m, &datalen);
1039 if (hostbased_cuser == NULL || hostbased_chost == NULL ||
1040 !monitor_allowed_key(blob, bloblen))
1041 fatal("%s: bad key, not previously allowed", __func__);
1043 key = key_from_blob(blob, bloblen);
1045 fatal("%s: bad public key blob", __func__);
1047 switch (key_blobtype) {
1049 valid_data = monitor_valid_userblob(data, datalen);
1052 valid_data = monitor_valid_hostbasedblob(data, datalen,
1053 hostbased_cuser, hostbased_chost);
1060 fatal("%s: bad signature data blob", __func__);
1062 verified = key_verify(key, signature, signaturelen, data, datalen);
1063 debug3("%s: key %p signature %s",
1064 __func__, key, verified ? "verified" : "unverified");
1071 auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
1073 monitor_reset_key_state();
1076 buffer_put_int(m, verified);
1077 mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
1083 mm_record_login(Session *s, struct passwd *pw)
1086 struct sockaddr_storage from;
1089 * Get IP address of client. If the connection is not a socket, let
1090 * the address be 0.0.0.0.
1092 memset(&from, 0, sizeof(from));
1093 fromlen = sizeof(from);
1094 if (packet_connection_is_on_socket()) {
1095 if (getpeername(packet_get_connection_in(),
1096 (struct sockaddr *) & from, &fromlen) < 0) {
1097 debug("getpeername: %.100s", strerror(errno));
1101 /* Record that there was a login on that tty from the remote host. */
1102 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1103 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1104 (struct sockaddr *)&from, fromlen);
1108 mm_session_close(Session *s)
1110 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1111 if (s->ttyfd != -1) {
1112 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1113 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
1114 session_pty_cleanup2(s);
1120 mm_answer_pty(int socket, Buffer *m)
1122 extern struct monitor *pmonitor;
1126 debug3("%s entering", __func__);
1132 s->authctxt = authctxt;
1133 s->pw = authctxt->pw;
1134 s->pid = pmonitor->m_pid;
1135 res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
1138 fatal_add_cleanup(session_pty_cleanup2, (void *)s);
1139 pty_setowner(authctxt->pw, s->tty);
1141 buffer_put_int(m, 1);
1142 buffer_put_cstring(m, s->tty);
1143 mm_request_send(socket, MONITOR_ANS_PTY, m);
1145 mm_send_fd(socket, s->ptyfd);
1146 mm_send_fd(socket, s->ttyfd);
1148 /* We need to trick ttyslot */
1149 if (dup2(s->ttyfd, 0) == -1)
1150 fatal("%s: dup2", __func__);
1152 mm_record_login(s, authctxt->pw);
1154 /* Now we can close the file descriptor again */
1157 /* make sure nothing uses fd 0 */
1158 if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0)
1159 fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
1161 error("%s: fd0 %d != 0", __func__, fd0);
1163 /* slave is not needed */
1165 s->ttyfd = s->ptyfd;
1166 /* no need to dup() because nobody closes ptyfd */
1167 s->ptymaster = s->ptyfd;
1169 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
1175 mm_session_close(s);
1176 buffer_put_int(m, 0);
1177 mm_request_send(socket, MONITOR_ANS_PTY, m);
1182 mm_answer_pty_cleanup(int socket, Buffer *m)
1187 debug3("%s entering", __func__);
1189 tty = buffer_get_string(m, NULL);
1190 if ((s = session_by_tty(tty)) != NULL)
1191 mm_session_close(s);
1198 mm_answer_sesskey(int socket, Buffer *m)
1203 /* Turn off permissions */
1204 monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
1206 if ((p = BN_new()) == NULL)
1207 fatal("%s: BN_new", __func__);
1209 buffer_get_bignum2(m, p);
1211 rsafail = ssh1_session_key(p);
1214 buffer_put_int(m, rsafail);
1215 buffer_put_bignum2(m, p);
1219 mm_request_send(socket, MONITOR_ANS_SESSKEY, m);
1221 /* Turn on permissions for sessid passing */
1222 monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
1228 mm_answer_sessid(int socket, Buffer *m)
1232 debug3("%s entering", __func__);
1234 if (buffer_len(m) != 16)
1235 fatal("%s: bad ssh1 session id", __func__);
1236 for (i = 0; i < 16; i++)
1237 session_id[i] = buffer_get_char(m);
1239 /* Turn on permissions for getpwnam */
1240 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1246 mm_answer_rsa_keyallowed(int socket, Buffer *m)
1250 u_char *blob = NULL;
1254 debug3("%s entering", __func__);
1256 if (options.rsa_authentication && authctxt->valid) {
1257 if ((client_n = BN_new()) == NULL)
1258 fatal("%s: BN_new", __func__);
1259 buffer_get_bignum2(m, client_n);
1260 allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
1261 BN_clear_free(client_n);
1264 buffer_put_int(m, allowed);
1265 buffer_put_int(m, forced_command != NULL);
1267 /* clear temporarily storage (used by generate challenge) */
1268 monitor_reset_key_state();
1270 if (allowed && key != NULL) {
1271 key->type = KEY_RSA; /* cheat for key_to_blob */
1272 if (key_to_blob(key, &blob, &blen) == 0)
1273 fatal("%s: key_to_blob failed", __func__);
1274 buffer_put_string(m, blob, blen);
1276 /* Save temporarily for comparison in verify */
1279 key_blobtype = MM_RSAUSERKEY;
1286 mm_request_send(socket, MONITOR_ANS_RSAKEYALLOWED, m);
1288 monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
1289 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
1294 mm_answer_rsa_challenge(int socket, Buffer *m)
1300 debug3("%s entering", __func__);
1302 if (!authctxt->valid)
1303 fatal("%s: authctxt not valid", __func__);
1304 blob = buffer_get_string(m, &blen);
1305 if (!monitor_allowed_key(blob, blen))
1306 fatal("%s: bad key, not previously allowed", __func__);
1307 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1308 fatal("%s: key type mismatch", __func__);
1309 if ((key = key_from_blob(blob, blen)) == NULL)
1310 fatal("%s: received bad key", __func__);
1313 BN_clear_free(ssh1_challenge);
1314 ssh1_challenge = auth_rsa_generate_challenge(key);
1317 buffer_put_bignum2(m, ssh1_challenge);
1319 debug3("%s sending reply", __func__);
1320 mm_request_send(socket, MONITOR_ANS_RSACHALLENGE, m);
1322 monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
1330 mm_answer_rsa_response(int socket, Buffer *m)
1333 u_char *blob, *response;
1337 debug3("%s entering", __func__);
1339 if (!authctxt->valid)
1340 fatal("%s: authctxt not valid", __func__);
1341 if (ssh1_challenge == NULL)
1342 fatal("%s: no ssh1_challenge", __func__);
1344 blob = buffer_get_string(m, &blen);
1345 if (!monitor_allowed_key(blob, blen))
1346 fatal("%s: bad key, not previously allowed", __func__);
1347 if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
1348 fatal("%s: key type mismatch: %d", __func__, key_blobtype);
1349 if ((key = key_from_blob(blob, blen)) == NULL)
1350 fatal("%s: received bad key", __func__);
1351 response = buffer_get_string(m, &len);
1353 fatal("%s: received bad response to challenge", __func__);
1354 success = auth_rsa_verify_response(key, ssh1_challenge, response);
1360 auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
1363 BN_clear_free(ssh1_challenge);
1364 ssh1_challenge = NULL;
1365 monitor_reset_key_state();
1368 buffer_put_int(m, success);
1369 mm_request_send(socket, MONITOR_ANS_RSARESPONSE, m);
1376 mm_answer_krb4(int socket, Buffer *m)
1378 KTEXT_ST auth, reply;
1383 reply.length = auth.length = 0;
1385 p = buffer_get_string(m, &alen);
1386 if (alen >= MAX_KTXT_LEN)
1387 fatal("%s: auth too large", __func__);
1388 memcpy(auth.dat, p, alen);
1393 success = options.kerberos_authentication &&
1395 auth_krb4(authctxt, &auth, &client, &reply);
1397 memset(auth.dat, 0, alen);
1399 buffer_put_int(m, success);
1402 buffer_put_cstring(m, client);
1403 buffer_put_string(m, reply.dat, reply.length);
1407 memset(reply.dat, 0, reply.length);
1410 debug3("%s: sending result %d", __func__, success);
1411 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1413 auth_method = "kerberos";
1415 /* Causes monitor loop to terminate if authenticated */
1422 mm_answer_krb5(int socket, Buffer *m)
1424 krb5_data tkt, reply;
1429 /* use temporary var to avoid size issues on 64bit arch */
1430 tkt.data = buffer_get_string(m, &len);
1433 success = options.kerberos_authentication &&
1435 auth_krb5(authctxt, &tkt, &client_user, &reply);
1441 buffer_put_int(m, success);
1444 buffer_put_cstring(m, client_user);
1445 buffer_put_string(m, reply.data, reply.length);
1451 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1458 mm_answer_term(int socket, Buffer *req)
1460 extern struct monitor *pmonitor;
1463 debug3("%s: tearing down sessions", __func__);
1465 /* The child is terminating */
1466 session_destroy_all(&mm_session_close);
1468 while (waitpid(pmonitor->m_pid, &status, 0) == -1)
1472 res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
1474 /* Terminate process */
1479 monitor_apply_keystate(struct monitor *pmonitor)
1482 set_newkeys(MODE_IN);
1483 set_newkeys(MODE_OUT);
1485 packet_set_protocol_flags(child_state.ssh1protoflags);
1486 packet_set_encryption_key(child_state.ssh1key,
1487 child_state.ssh1keylen, child_state.ssh1cipher);
1488 xfree(child_state.ssh1key);
1491 /* for rc4 and other stateful ciphers */
1492 packet_set_keycontext(MODE_OUT, child_state.keyout);
1493 xfree(child_state.keyout);
1494 packet_set_keycontext(MODE_IN, child_state.keyin);
1495 xfree(child_state.keyin);
1498 packet_set_iv(MODE_OUT, child_state.ivout);
1499 xfree(child_state.ivout);
1500 packet_set_iv(MODE_IN, child_state.ivin);
1501 xfree(child_state.ivin);
1504 memcpy(&incoming_stream, &child_state.incoming,
1505 sizeof(incoming_stream));
1506 memcpy(&outgoing_stream, &child_state.outgoing,
1507 sizeof(outgoing_stream));
1509 /* Update with new address */
1510 if (options.compression)
1511 mm_init_compression(pmonitor->m_zlib);
1513 /* Network I/O buffers */
1514 /* XXX inefficient for large buffers, need: buffer_init_from_string */
1515 buffer_clear(&input);
1516 buffer_append(&input, child_state.input, child_state.ilen);
1517 memset(child_state.input, 0, child_state.ilen);
1518 xfree(child_state.input);
1520 buffer_clear(&output);
1521 buffer_append(&output, child_state.output, child_state.olen);
1522 memset(child_state.output, 0, child_state.olen);
1523 xfree(child_state.output);
1527 mm_get_kex(Buffer *m)
1533 kex = xmalloc(sizeof(*kex));
1534 memset(kex, 0, sizeof(*kex));
1535 kex->session_id = buffer_get_string(m, &kex->session_id_len);
1536 if ((session_id2 == NULL) ||
1537 (kex->session_id_len != session_id2_len) ||
1538 (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
1539 fatal("mm_get_get: internal error: bad session id");
1540 kex->we_need = buffer_get_int(m);
1541 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
1542 kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
1544 kex->kex[KEX_GSS_GRP1_SHA1] =kexgss_server;
1547 kex->hostkey_type = buffer_get_int(m);
1548 kex->kex_type = buffer_get_int(m);
1549 blob = buffer_get_string(m, &bloblen);
1550 buffer_init(&kex->my);
1551 buffer_append(&kex->my, blob, bloblen);
1553 blob = buffer_get_string(m, &bloblen);
1554 buffer_init(&kex->peer);
1555 buffer_append(&kex->peer, blob, bloblen);
1558 kex->flags = buffer_get_int(m);
1559 kex->client_version_string = buffer_get_string(m, NULL);
1560 kex->server_version_string = buffer_get_string(m, NULL);
1561 kex->load_host_key=&get_hostkey_by_type;
1562 kex->host_key_index=&get_hostkey_index;
1567 /* This function requries careful sanity checking */
1570 mm_get_keystate(struct monitor *pmonitor)
1574 u_int bloblen, plen;
1576 debug3("%s: Waiting for new keys", __func__);
1579 mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
1581 child_state.ssh1protoflags = buffer_get_int(&m);
1582 child_state.ssh1cipher = buffer_get_int(&m);
1583 child_state.ssh1key = buffer_get_string(&m,
1584 &child_state.ssh1keylen);
1585 child_state.ivout = buffer_get_string(&m,
1586 &child_state.ivoutlen);
1587 child_state.ivin = buffer_get_string(&m, &child_state.ivinlen);
1590 /* Get the Kex for rekeying */
1591 *pmonitor->m_pkex = mm_get_kex(&m);
1594 blob = buffer_get_string(&m, &bloblen);
1595 current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
1598 debug3("%s: Waiting for second key", __func__);
1599 blob = buffer_get_string(&m, &bloblen);
1600 current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
1603 /* Now get sequence numbers for the packets */
1604 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1605 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1608 /* Get the key context */
1609 child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen);
1610 child_state.keyin = buffer_get_string(&m, &child_state.keyinlen);
1612 debug3("%s: Getting compression state", __func__);
1613 /* Get compression state */
1614 p = buffer_get_string(&m, &plen);
1615 if (plen != sizeof(child_state.outgoing))
1616 fatal("%s: bad request size", __func__);
1617 memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing));
1620 p = buffer_get_string(&m, &plen);
1621 if (plen != sizeof(child_state.incoming))
1622 fatal("%s: bad request size", __func__);
1623 memcpy(&child_state.incoming, p, sizeof(child_state.incoming));
1626 /* Network I/O buffers */
1627 debug3("%s: Getting Network I/O buffers", __func__);
1628 child_state.input = buffer_get_string(&m, &child_state.ilen);
1629 child_state.output = buffer_get_string(&m, &child_state.olen);
1635 /* Allocation functions for zlib */
1637 mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
1639 size_t len = (size_t) size * ncount;
1642 if (len == 0 || ncount > SIZE_T_MAX / size)
1643 fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
1645 address = mm_malloc(mm, len);
1651 mm_zfree(struct mm_master *mm, void *address)
1653 mm_free(mm, address);
1657 mm_init_compression(struct mm_master *mm)
1659 outgoing_stream.zalloc = (alloc_func)mm_zalloc;
1660 outgoing_stream.zfree = (free_func)mm_zfree;
1661 outgoing_stream.opaque = mm;
1663 incoming_stream.zalloc = (alloc_func)mm_zalloc;
1664 incoming_stream.zfree = (free_func)mm_zfree;
1665 incoming_stream.opaque = mm;
1670 #define FD_CLOSEONEXEC(x) do { \
1671 if (fcntl(x, F_SETFD, 1) == -1) \
1672 fatal("fcntl(%d, F_SETFD)", x); \
1676 monitor_socketpair(int *pair)
1678 #ifdef HAVE_SOCKETPAIR
1679 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
1680 fatal("%s: socketpair", __func__);
1682 fatal("%s: UsePrivilegeSeparation=yes not supported",
1685 FD_CLOSEONEXEC(pair[0]);
1686 FD_CLOSEONEXEC(pair[1]);
1689 #define MM_MEMSIZE 65536
1694 struct monitor *mon;
1697 mon = xmalloc(sizeof(*mon));
1699 monitor_socketpair(pair);
1701 mon->m_recvfd = pair[0];
1702 mon->m_sendfd = pair[1];
1704 /* Used to share zlib space across processes */
1705 if (options.compression) {
1706 mon->m_zback = mm_create(NULL, MM_MEMSIZE);
1707 mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
1709 /* Compression needs to share state across borders */
1710 mm_init_compression(mon->m_zlib);
1717 monitor_reinit(struct monitor *mon)
1721 monitor_socketpair(pair);
1723 mon->m_recvfd = pair[0];
1724 mon->m_sendfd = pair[1];
1730 mm_answer_gss_setup_ctx(int socket, Buffer *m) {
1735 oid.elements=buffer_get_string(m,&len);
1738 major=ssh_gssapi_server_ctx(&gsscontext,&oid);
1740 xfree(oid.elements);
1743 buffer_put_int(m,major);
1745 mm_request_send(socket,MONITOR_ANS_GSSSETUP,m);
1747 /* Now we have a context, enable the step and sign */
1748 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP,1);
1754 mm_answer_gss_accept_ctx(int socket, Buffer *m) {
1755 gss_buffer_desc in,out;
1756 OM_uint32 major,minor;
1757 OM_uint32 flags = 0; /* GSI needs this */
1759 in.value = buffer_get_string(m,&in.length);
1760 major=ssh_gssapi_accept_ctx(gsscontext,&in,&out,&flags);
1764 buffer_put_int(m, major);
1765 buffer_put_string(m, out.value, out.length);
1766 buffer_put_int(m, flags);
1767 mm_request_send(socket,MONITOR_ANS_GSSSTEP,m);
1769 gss_release_buffer(&minor, &out);
1771 /* Complete - now we can do signing */
1772 if (major==GSS_S_COMPLETE) {
1773 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP,0);
1774 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSIGN,1);
1780 mm_answer_gss_userok(int socket, Buffer *m) {
1783 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1786 buffer_put_int(m, authenticated);
1788 debug3("%s: sending result %d", __func__, authenticated);
1789 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1791 /* XXX - auth method could also be 'external' */
1792 auth_method="gssapi";
1794 /* Monitor loop will terminate if authenticated */
1795 return(authenticated);
1799 mm_answer_gss_sign(int socket, Buffer *m) {
1800 gss_buffer_desc data,hash;
1801 OM_uint32 major,minor;
1803 data.value = buffer_get_string(m,&data.length);
1804 if (data.length != 16) { /* HACK - i.e. we are using SSHv1 */
1805 if (data.length != 20)
1806 fatal("%s: data length incorrect: %d", __func__, data.length);
1808 /* Save the session ID - only first time round */
1809 if (session_id2_len == 0) {
1810 session_id2_len=data.length;
1811 session_id2 = xmalloc(session_id2_len);
1812 memcpy(session_id2, data.value, session_id2_len);
1815 major=ssh_gssapi_sign(gsscontext, &data, &hash);
1820 buffer_put_int(m, major);
1821 buffer_put_string(m, hash.value, hash.length);
1823 mm_request_send(socket,MONITOR_ANS_GSSSIGN,m);
1825 gss_release_buffer(&minor,&hash);
1827 /* Turn on permissions for getpwnam */
1828 monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
1834 mm_answer_gss_error(int socket, Buffer *m) {
1835 OM_uint32 major,minor;
1838 msg=ssh_gssapi_last_error(gsscontext,&major,&minor);
1840 buffer_put_int(m,major);
1841 buffer_put_int(m,minor);
1842 buffer_put_cstring(m,msg);
1844 mm_request_send(socket,MONITOR_ANS_GSSERR,m);
1852 mm_answer_gss_indicate_mechs(int socket, Buffer *m) {
1853 OM_uint32 major,minor;
1854 gss_OID_set mech_set;
1857 major=gss_indicate_mechs(&minor, &mech_set);
1860 buffer_put_int(m, major);
1861 buffer_put_int(m, mech_set->count);
1862 for (i=0; i < mech_set->count; i++) {
1863 buffer_put_string(m, mech_set->elements[i].elements,
1864 mech_set->elements[i].length);
1867 #if !defined(MECHGLUE) /* mechglue memory management bug ??? */
1868 gss_release_oid_set(&minor,&mech_set);
1871 mm_request_send(socket,MONITOR_ANS_GSSMECHS,m);
1877 mm_answer_gss_localname(int socket, Buffer *m) {
1880 ssh_gssapi_localname(&name);
1884 buffer_put_cstring(m, name);
1885 debug3("%s: sending result %s", __func__, name);
1888 buffer_put_cstring(m, "");
1889 debug3("%s: sending result \"\"", __func__);
1892 mm_request_send(socket, MONITOR_ANS_GSSLOCALNAME, m);
1901 mm_answer_gsi_gridmap(int socket, Buffer *m) {
1902 char *subject, *name;
1904 subject = buffer_get_string(m, NULL);
1906 gsi_gridmap(subject, &name);
1910 buffer_put_cstring(m, name);
1911 debug3("%s: sending result %s", __func__, name);
1914 buffer_put_cstring(m, "");
1915 debug3("%s: sending result \"\"", __func__);
1918 mm_request_send(socket, MONITOR_ANS_GSIGRIDMAP, m);