]> andersk Git - openssh.git/blobdiff - sshconnect2.c
- (tim) [configure.ac] Bug 1078. Fix --without-kerberos5. Reported by
[openssh.git] / sshconnect2.c
index 1b85730fea4364252ef7a5046a523a9eaf98ad3f..ee7932d6814fe59e89dfca33919c7d392d0922b2 100644 (file)
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshconnect2.c,v 1.119 2003/05/15 00:28:28 markus Exp $");
-
-#ifdef KRB5
-#include <krb5.h>
-#ifndef HEIMDAL
-#define krb5_get_err_text(context,code) error_message(code)
-#endif /* !HEIMDAL */
-#endif
+RCSID("$OpenBSD: sshconnect2.c,v 1.142 2005/08/30 22:08:05 djm Exp $");
 
 #include "openbsd-compat/sys-queue.h"
 
@@ -50,13 +43,17 @@ RCSID("$OpenBSD: sshconnect2.c,v 1.119 2003/05/15 00:28:28 markus Exp $");
 #include "authfd.h"
 #include "log.h"
 #include "readconf.h"
-#include "readpass.h"
+#include "misc.h"
 #include "match.h"
 #include "dispatch.h"
 #include "canohost.h"
 #include "msg.h"
 #include "pathnames.h"
 
+#ifdef GSSAPI
+#include "ssh-gss.h"
+#endif
+
 /* import */
 extern char *client_version_string;
 extern char *server_version_string;
@@ -67,7 +64,7 @@ extern Options options;
  */
 
 u_char *session_id2 = NULL;
-int session_id2_len = 0;
+u_int session_id2_len = 0;
 
 char *xxx_host;
 struct sockaddr *xxx_hostaddr;
@@ -104,10 +101,10 @@ ssh_kex2(char *host, struct sockaddr *hostaddr)
            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
        if (options.compression) {
                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
-               myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
+               myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
        } else {
                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
-               myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
+               myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
        }
        if (options.macs != NULL) {
                myproposal[PROPOSAL_MAC_ALGS_CTOS] =
@@ -123,6 +120,7 @@ ssh_kex2(char *host, struct sockaddr *hostaddr)
        /* start key exchange */
        kex = kex_setup(myproposal);
        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
+       kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
        kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
        kex->client_version_string=client_version_string;
        kex->server_version_string=server_version_string;
@@ -178,6 +176,8 @@ struct Authctxt {
        Sensitive *sensitive;
        /* kbd-interactive */
        int info_req_seen;
+       /* generic */
+       void *methoddata;
 };
 struct Authmethod {
        char    *name;          /* string to compare against server's list */
@@ -201,6 +201,15 @@ int        userauth_kbdint(Authctxt *);
 int    userauth_hostbased(Authctxt *);
 int    userauth_kerberos(Authctxt *);
 
+#ifdef GSSAPI
+int    userauth_gssapi(Authctxt *authctxt);
+void   input_gssapi_response(int type, u_int32_t, void *);
+void   input_gssapi_token(int type, u_int32_t, void *);
+void   input_gssapi_hash(int type, u_int32_t, void *);
+void   input_gssapi_error(int, u_int32_t, void *);
+void   input_gssapi_errtok(int, u_int32_t, void *);
+#endif
+
 void   userauth(Authctxt *, char *);
 
 static int sign_and_send_pubkey(Authctxt *, Identity *);
@@ -213,16 +222,16 @@ static Authmethod *authmethod_lookup(const char *name);
 static char *authmethods_get(void);
 
 Authmethod authmethods[] = {
+#ifdef GSSAPI
+       {"gssapi-with-mic",
+               userauth_gssapi,
+               &options.gss_authentication,
+               NULL},
+#endif
        {"hostbased",
                userauth_hostbased,
                &options.hostbased_authentication,
                NULL},
-#if KRB5
-       {"kerberos-2@ssh.com",
-               userauth_kerberos,
-               &options.kerberos_authentication,
-               NULL},
-#endif
        {"publickey",
                userauth_pubkey,
                &options.pubkey_authentication,
@@ -283,6 +292,7 @@ ssh_userauth2(const char *local_user, const char *server_user, char *host,
        authctxt.success = 0;
        authctxt.method = authmethod_lookup("none");
        authctxt.authlist = NULL;
+       authctxt.methoddata = NULL;
        authctxt.sensitive = sensitive;
        authctxt.info_req_seen = 0;
        if (authctxt.method == NULL)
@@ -306,6 +316,10 @@ ssh_userauth2(const char *local_user, const char *server_user, char *host,
 void
 userauth(Authctxt *authctxt, char *authlist)
 {
+       if (authctxt->methoddata) {
+               xfree(authctxt->methoddata);
+               authctxt->methoddata = NULL;
+       }
        if (authlist == NULL) {
                authlist = authctxt->authlist;
        } else {
@@ -338,17 +352,19 @@ void
 input_userauth_error(int type, u_int32_t seq, void *ctxt)
 {
        fatal("input_userauth_error: bad message during authentication: "
-          "type %d", type);
+           "type %d", type);
 }
 
 void
 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
 {
        char *msg, *lang;
+
        debug3("input_userauth_banner");
        msg = packet_get_string(NULL);
        lang = packet_get_string(NULL);
-       fprintf(stderr, "%s", msg);
+       if (options.log_level > SYSLOG_LEVEL_QUIET)
+               fprintf(stderr, "%s", msg);
        xfree(msg);
        xfree(lang);
 }
@@ -359,8 +375,14 @@ input_userauth_success(int type, u_int32_t seq, void *ctxt)
        Authctxt *authctxt = ctxt;
        if (authctxt == NULL)
                fatal("input_userauth_success: no authentication context");
-       if (authctxt->authlist)
+       if (authctxt->authlist) {
                xfree(authctxt->authlist);
+               authctxt->authlist = NULL;
+       }
+       if (authctxt->methoddata) {
+               xfree(authctxt->methoddata);
+               authctxt->methoddata = NULL;
+       }
        authctxt->success = 1;                  /* break out */
 }
 
@@ -432,7 +454,12 @@ input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
        debug2("input_userauth_pk_ok: fp %s", fp);
        xfree(fp);
 
-       TAILQ_FOREACH(id, &authctxt->keys, next) {
+       /*
+        * search keys in the reverse order, because last candidate has been
+        * moved to the end of the queue.  this also avoids confusion by
+        * duplicate keys
+        */
+       TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
                if (key_equal(key, id->key)) {
                        sent = sign_and_send_pubkey(authctxt, id);
                        break;
@@ -449,6 +476,238 @@ done:
                userauth(authctxt, NULL);
 }
 
+#ifdef GSSAPI
+int
+userauth_gssapi(Authctxt *authctxt)
+{
+       Gssctxt *gssctxt = NULL;
+       static gss_OID_set gss_supported = NULL;
+       static u_int mech = 0;
+       OM_uint32 min;
+       int ok = 0;
+
+       /* Try one GSSAPI method at a time, rather than sending them all at
+        * once. */
+
+       if (gss_supported == NULL)
+               gss_indicate_mechs(&min, &gss_supported);
+
+       /* Check to see if the mechanism is usable before we offer it */
+       while (mech < gss_supported->count && !ok) {
+               if (gssctxt)
+                       ssh_gssapi_delete_ctx(&gssctxt);
+               ssh_gssapi_build_ctx(&gssctxt);
+               ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
+
+               /* My DER encoding requires length<128 */
+               if (gss_supported->elements[mech].length < 128 &&
+                   !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
+                   authctxt->host))) {
+                       ok = 1; /* Mechanism works */
+               } else {
+                       mech++;
+               }
+       }
+
+       if (!ok)
+               return 0;
+
+       authctxt->methoddata=(void *)gssctxt;
+
+       packet_start(SSH2_MSG_USERAUTH_REQUEST);
+       packet_put_cstring(authctxt->server_user);
+       packet_put_cstring(authctxt->service);
+       packet_put_cstring(authctxt->method->name);
+
+       packet_put_int(1);
+
+       packet_put_int((gss_supported->elements[mech].length) + 2);
+       packet_put_char(SSH_GSS_OIDTYPE);
+       packet_put_char(gss_supported->elements[mech].length);
+       packet_put_raw(gss_supported->elements[mech].elements,
+           gss_supported->elements[mech].length);
+
+       packet_send();
+
+       dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
+       dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
+       dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
+       dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
+
+       mech++; /* Move along to next candidate */
+
+       return 1;
+}
+
+static OM_uint32
+process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
+{
+       Authctxt *authctxt = ctxt;
+       Gssctxt *gssctxt = authctxt->methoddata;
+       gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
+       gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
+       gss_buffer_desc gssbuf;
+       OM_uint32 status, ms, flags;
+       Buffer b;
+
+       status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
+           recv_tok, &send_tok, &flags);
+
+       if (send_tok.length > 0) {
+               if (GSS_ERROR(status))
+                       packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
+               else
+                       packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
+
+               packet_put_string(send_tok.value, send_tok.length);
+               packet_send();
+               gss_release_buffer(&ms, &send_tok);
+       }
+
+       if (status == GSS_S_COMPLETE) {
+               /* send either complete or MIC, depending on mechanism */
+               if (!(flags & GSS_C_INTEG_FLAG)) {
+                       packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
+                       packet_send();
+               } else {
+                       ssh_gssapi_buildmic(&b, authctxt->server_user,
+                           authctxt->service, "gssapi-with-mic");
+
+                       gssbuf.value = buffer_ptr(&b);
+                       gssbuf.length = buffer_len(&b);
+
+                       status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
+
+                       if (!GSS_ERROR(status)) {
+                               packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
+                               packet_put_string(mic.value, mic.length);
+
+                               packet_send();
+                       }
+
+                       buffer_free(&b);
+                       gss_release_buffer(&ms, &mic);
+               }
+       }
+
+       return status;
+}
+
+void
+input_gssapi_response(int type, u_int32_t plen, void *ctxt)
+{
+       Authctxt *authctxt = ctxt;
+       Gssctxt *gssctxt;
+       int oidlen;
+       char *oidv;
+
+       if (authctxt == NULL)
+               fatal("input_gssapi_response: no authentication context");
+       gssctxt = authctxt->methoddata;
+
+       /* Setup our OID */
+       oidv = packet_get_string(&oidlen);
+
+       if (oidlen <= 2 ||
+           oidv[0] != SSH_GSS_OIDTYPE ||
+           oidv[1] != oidlen - 2) {
+               xfree(oidv);
+               debug("Badly encoded mechanism OID received");
+               userauth(authctxt, NULL);
+               return;
+       }
+
+       if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
+               fatal("Server returned different OID than expected");
+
+       packet_check_eom();
+
+       xfree(oidv);
+
+       if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
+               /* Start again with next method on list */
+               debug("Trying to start again");
+               userauth(authctxt, NULL);
+               return;
+       }
+}
+
+void
+input_gssapi_token(int type, u_int32_t plen, void *ctxt)
+{
+       Authctxt *authctxt = ctxt;
+       gss_buffer_desc recv_tok;
+       OM_uint32 status;
+       u_int slen;
+
+       if (authctxt == NULL)
+               fatal("input_gssapi_response: no authentication context");
+
+       recv_tok.value = packet_get_string(&slen);
+       recv_tok.length = slen; /* safe typecast */
+
+       packet_check_eom();
+
+       status = process_gssapi_token(ctxt, &recv_tok);
+
+       xfree(recv_tok.value);
+
+       if (GSS_ERROR(status)) {
+               /* Start again with the next method in the list */
+               userauth(authctxt, NULL);
+               return;
+       }
+}
+
+void
+input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
+{
+       Authctxt *authctxt = ctxt;
+       Gssctxt *gssctxt;
+       gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
+       gss_buffer_desc recv_tok;
+       OM_uint32 status, ms;
+       u_int len;
+
+       if (authctxt == NULL)
+               fatal("input_gssapi_response: no authentication context");
+       gssctxt = authctxt->methoddata;
+
+       recv_tok.value = packet_get_string(&len);
+       recv_tok.length = len;
+
+       packet_check_eom();
+
+       /* Stick it into GSSAPI and see what it says */
+       status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
+           &recv_tok, &send_tok, NULL);
+
+       xfree(recv_tok.value);
+       gss_release_buffer(&ms, &send_tok);
+
+       /* Server will be returning a failed packet after this one */
+}
+
+void
+input_gssapi_error(int type, u_int32_t plen, void *ctxt)
+{
+       OM_uint32 maj, min;
+       char *msg;
+       char *lang;
+
+       maj=packet_get_int();
+       min=packet_get_int();
+       msg=packet_get_string(NULL);
+       lang=packet_get_string(NULL);
+
+       packet_check_eom();
+
+       debug("Server GSSAPI Error:\n%s\n", msg);
+       xfree(msg);
+       xfree(lang);
+}
+#endif /* GSSAPI */
+
 int
 userauth_none(Authctxt *authctxt)
 {
@@ -591,7 +850,7 @@ sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
        Buffer b;
        u_char *blob, *signature;
        u_int bloblen, slen;
-       int skip = 0;
+       u_int skip = 0;
        int ret = -1;
        int have_sig = 1;
 
@@ -777,7 +1036,7 @@ pubkey_prepare(Authctxt *authctxt)
                    key = ssh_get_next_identity(ac, &comment, 2)) {
                        found = 0;
                        TAILQ_FOREACH(id, &files, next) {
-                               /* agent keys from the config file are preferred */ 
+                               /* agent keys from the config file are preferred */
                                if (key_equal(key, id->key)) {
                                        key_free(key);
                                        xfree(comment);
@@ -788,7 +1047,7 @@ pubkey_prepare(Authctxt *authctxt)
                                        break;
                                }
                        }
-                       if (!found) {
+                       if (!found && !options.identities_only) {
                                id = xmalloc(sizeof(*id));
                                memset(id, 0, sizeof(*id));
                                id->key = key;
@@ -841,6 +1100,7 @@ userauth_pubkey(Authctxt *authctxt)
        while ((id = TAILQ_FIRST(&authctxt->keys))) {
                if (id->tried++)
                        return (0);
+               /* move key to the end of the queue */
                TAILQ_REMOVE(&authctxt->keys, id, next);
                TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
                /*
@@ -1005,11 +1265,12 @@ ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
        buffer_init(&b);
        buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
        buffer_put_string(&b, data, datalen);
-       ssh_msg_send(to[1], version, &b);
+       if (ssh_msg_send(to[1], version, &b) == -1)
+               fatal("ssh_keysign: couldn't send request");
 
        if (ssh_msg_recv(from[0], &b) < 0) {
                error("ssh_keysign: no reply");
-               buffer_clear(&b);
+               buffer_free(&b);
                return -1;
        }
        close(from[0]);
@@ -1021,11 +1282,11 @@ ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
 
        if (buffer_get_char(&b) != version) {
                error("ssh_keysign: bad version");
-               buffer_clear(&b);
+               buffer_free(&b);
                return -1;
        }
        *sigp = buffer_get_string(&b, lenp);
-       buffer_clear(&b);
+       buffer_free(&b);
 
        return 0;
 }
@@ -1123,101 +1384,6 @@ userauth_hostbased(Authctxt *authctxt)
        return 1;
 }
 
-#if KRB5
-static int
-ssh_krb5_helper(krb5_data *ap, krb5_context *context)
-{
-       krb5_context xcontext = NULL;   /* XXX share with ssh1 */
-       krb5_auth_context xauth_context = NULL;
-       krb5_auth_context *auth_context;
-       krb5_error_code problem;
-       const char *tkfile;
-       struct stat buf;
-       krb5_ccache ccache = NULL;
-       const char *remotehost;
-       int ret;
-
-       memset(ap, 0, sizeof(*ap));
-
-       context = &xcontext;
-       auth_context = &xauth_context;
-
-       problem = krb5_init_context(context);
-       if (problem) {
-               debug("Kerberos v5: krb5_init_context failed");
-               ret = 0;
-               goto out;
-       }
-
-       tkfile = krb5_cc_default_name(*context);
-       if (strncmp(tkfile, "FILE:", 5) == 0)
-               tkfile += 5;
-
-       if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
-               debug("Kerberos v5: could not get default ccache (permission denied).");
-               ret = 0;
-               goto out;
-       }
-
-       problem = krb5_cc_default(*context, &ccache);
-       if (problem) {
-               debug("Kerberos v5: krb5_cc_default failed: %s",
-                   krb5_get_err_text(*context, problem));
-               ret = 0;
-               goto out;
-       }
-
-       remotehost = get_canonical_hostname(1);
-
-       problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
-           "host", remotehost, NULL, ccache, ap);
-       if (problem) {
-               debug("Kerberos v5: krb5_mk_req failed: %s",
-                   krb5_get_err_text(*context, problem));
-               ret = 0;
-               goto out;
-       }
-       ret = 1;
-
- out:
-       if (ccache != NULL)
-               krb5_cc_close(*context, ccache);
-       if (*auth_context)
-               krb5_auth_con_free(*context, *auth_context);
-       return (ret);
-}
-
-int
-userauth_kerberos(Authctxt *authctxt)
-{
-       krb5_data ap;
-       krb5_context *context;
-       int ret = 0;
-
-       if (ssh_krb5_helper(&ap, context) == 0)
-               goto out;
-
-       packet_start(SSH2_MSG_USERAUTH_REQUEST);
-       packet_put_cstring(authctxt->server_user);
-       packet_put_cstring(authctxt->service);
-       packet_put_cstring(authctxt->method->name);
-       packet_put_string(ap.data, ap.length);
-       packet_send();
-
-#ifdef HEIMDAL
-       krb5_data_free(&ap);
-#else
-       krb5_free_data_contents(*context, &ap);
-#endif
-       ret = 1;
-
-out:
-       if (*context)
-               krb5_free_context(*context);
-       return ret;
-}
-#endif
-
 /* find auth method */
 
 /*
This page took 0.070034 seconds and 4 git commands to generate.