libads: record session expiry for spnego sasl binds
[samba.git] / source3 / libads / sasl.c
index fa9afd7869946e34c4f4f37981f833593dcb306b..b8d4527a15f72caf8de0753f0597571e96674537 100644 (file)
 */
 
 #include "includes.h"
+#include "../libcli/auth/spnego.h"
+#include "auth/credentials/credentials.h"
+#include "auth/gensec/gensec.h"
+#include "auth_generic.h"
+#include "ads.h"
+#include "smb_krb5.h"
+#include "system/gssapi.h"
+#include "lib/param/loadparm.h"
 
 #ifdef HAVE_LDAP
 
-static ADS_STATUS ads_sasl_ntlmssp_wrap(ADS_STRUCT *ads, uint8 *buf, uint32 len)
+static ADS_STATUS ads_sasl_gensec_wrap(ADS_STRUCT *ads, uint8_t *buf, uint32_t len)
 {
-       struct ntlmssp_state *ntlmssp_state =
-               (struct ntlmssp_state *)ads->ldap.wrap_private_data;
-       ADS_STATUS status;
+       struct gensec_security *gensec_security =
+               talloc_get_type_abort(ads->ldap.wrap_private_data,
+               struct gensec_security);
        NTSTATUS nt_status;
-       DATA_BLOB sig;
-       uint8 *dptr = ads->ldap.out.buf + (4 + NTLMSSP_SIG_SIZE);
-
-       /* copy the data to the right location */
-       memcpy(dptr, buf, len);
-
-       /* create the signature and may encrypt the data */
-       if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
-               nt_status = ntlmssp_seal_packet(ntlmssp_state,
-                                               dptr, len,
-                                               dptr, len,
-                                               &sig);
-       } else {
-               nt_status = ntlmssp_sign_packet(ntlmssp_state,
-                                               dptr, len,
-                                               dptr, len,
-                                               &sig);
+       DATA_BLOB unwrapped, wrapped;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       unwrapped = data_blob_const(buf, len);
+
+       nt_status = gensec_wrap(gensec_security, frame, &unwrapped, &wrapped);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               TALLOC_FREE(frame);
+               return ADS_ERROR_NT(nt_status);
        }
-       status = ADS_ERROR_NT(nt_status);
-       if (!ADS_ERR_OK(status)) return status;
 
-       /* copy the signature to the right location */
-       memcpy(ads->ldap.out.buf + 4,
-              sig.data, NTLMSSP_SIG_SIZE);
+       if ((ads->ldap.out.size - 4) < wrapped.length) {
+               TALLOC_FREE(frame);
+               return ADS_ERROR_NT(NT_STATUS_INTERNAL_ERROR);
+       }
 
-       data_blob_free(&sig);
+       /* copy the wrapped blob to the right location */
+       memcpy(ads->ldap.out.buf + 4, wrapped.data, wrapped.length);
 
        /* set how many bytes must be written to the underlying socket */
-       ads->ldap.out.left = 4 + NTLMSSP_SIG_SIZE + len;
+       ads->ldap.out.left = 4 + wrapped.length;
+
+       TALLOC_FREE(frame);
 
        return ADS_SUCCESS;
 }
 
-static ADS_STATUS ads_sasl_ntlmssp_unwrap(ADS_STRUCT *ads)
+static ADS_STATUS ads_sasl_gensec_unwrap(ADS_STRUCT *ads)
 {
-       struct ntlmssp_state *ntlmssp_state =
-               (struct ntlmssp_state *)ads->ldap.wrap_private_data;
-       ADS_STATUS status;
+       struct gensec_security *gensec_security =
+               talloc_get_type_abort(ads->ldap.wrap_private_data,
+               struct gensec_security);
        NTSTATUS nt_status;
-       DATA_BLOB sig;
-       uint8 *dptr = ads->ldap.in.buf + (4 + NTLMSSP_SIG_SIZE);
-       uint32 dlen = ads->ldap.in.ofs - (4 + NTLMSSP_SIG_SIZE);
-
-       /* wrap the signature into a DATA_BLOB */
-       sig = data_blob_const(ads->ldap.in.buf + 4, NTLMSSP_SIG_SIZE);
-
-       /* verify the signature and maybe decrypt the data */
-       if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL) {
-               nt_status = ntlmssp_unseal_packet(ntlmssp_state,
-                                                 dptr, dlen,
-                                                 dptr, dlen,
-                                                 &sig);
-       } else {
-               nt_status = ntlmssp_check_packet(ntlmssp_state,
-                                                dptr, dlen,
-                                                dptr, dlen,
-                                                &sig);
+       DATA_BLOB unwrapped, wrapped;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       wrapped = data_blob_const(ads->ldap.in.buf + 4, ads->ldap.in.ofs - 4);
+
+       nt_status = gensec_unwrap(gensec_security, frame, &wrapped, &unwrapped);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               TALLOC_FREE(frame);
+               return ADS_ERROR_NT(nt_status);
        }
-       status = ADS_ERROR_NT(nt_status);
-       if (!ADS_ERR_OK(status)) return status;
 
-       /* set the amount of bytes for the upper layer and set the ofs to the data */
-       ads->ldap.in.left       = dlen;
-       ads->ldap.in.ofs        = 4 + NTLMSSP_SIG_SIZE;
+       if (wrapped.length < unwrapped.length) {
+               TALLOC_FREE(frame);
+               return ADS_ERROR_NT(NT_STATUS_INTERNAL_ERROR);
+       }
+
+       /* copy the wrapped blob to the right location */
+       memcpy(ads->ldap.in.buf + 4, unwrapped.data, unwrapped.length);
+
+       /* set how many bytes must be written to the underlying socket */
+       ads->ldap.in.left       = unwrapped.length;
+       ads->ldap.in.ofs        = 4;
+
+       TALLOC_FREE(frame);
 
        return ADS_SUCCESS;
 }
 
-static void ads_sasl_ntlmssp_disconnect(ADS_STRUCT *ads)
+static void ads_sasl_gensec_disconnect(ADS_STRUCT *ads)
 {
-       struct ntlmssp_state *ntlmssp_state =
-               (struct ntlmssp_state *)ads->ldap.wrap_private_data;
+       struct gensec_security *gensec_security =
+               talloc_get_type_abort(ads->ldap.wrap_private_data,
+               struct gensec_security);
 
-       ntlmssp_end(&ntlmssp_state);
+       TALLOC_FREE(gensec_security);
 
        ads->ldap.wrap_ops = NULL;
        ads->ldap.wrap_private_data = NULL;
 }
 
-static const struct ads_saslwrap_ops ads_sasl_ntlmssp_ops = {
-       .name           = "ntlmssp",
-       .wrap           = ads_sasl_ntlmssp_wrap,
-       .unwrap         = ads_sasl_ntlmssp_unwrap,
-       .disconnect     = ads_sasl_ntlmssp_disconnect
+static const struct ads_saslwrap_ops ads_sasl_gensec_ops = {
+       .name           = "gensec",
+       .wrap           = ads_sasl_gensec_wrap,
+       .unwrap         = ads_sasl_gensec_unwrap,
+       .disconnect     = ads_sasl_gensec_disconnect
 };
 
 /* 
-   perform a LDAP/SASL/SPNEGO/NTLMSSP bind (just how many layers can
+   perform a LDAP/SASL/SPNEGO/{NTLMSSP,KRB5} bind (just how many layers can
    we fit on one socket??)
 */
-static ADS_STATUS ads_sasl_spnego_ntlmssp_bind(ADS_STRUCT *ads)
+static ADS_STATUS ads_sasl_spnego_gensec_bind(ADS_STRUCT *ads,
+                               const char *sasl,
+                               enum credentials_use_kerberos krb5_state,
+                               const char *target_service,
+                               const char *target_hostname,
+                               const DATA_BLOB server_blob)
 {
-       DATA_BLOB msg1 = data_blob_null;
-       DATA_BLOB blob = data_blob_null;
        DATA_BLOB blob_in = data_blob_null;
        DATA_BLOB blob_out = data_blob_null;
-       struct berval cred, *scred = NULL;
        int rc;
        NTSTATUS nt_status;
-       int turn = 1;
-       uint32 features = 0;
-
-       struct ntlmssp_state *ntlmssp_state;
+       ADS_STATUS status;
+       struct auth_generic_state *auth_generic_state;
+       bool use_spnego_principal = lp_client_use_spnego_principal();
+       const char *sasl_list[] = { sasl, NULL };
+       NTTIME end_nt_time;
 
-       if (!NT_STATUS_IS_OK(nt_status = ntlmssp_client_start(&ntlmssp_state))) {
+       nt_status = auth_generic_client_prepare(NULL, &auth_generic_state);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return ADS_ERROR_NT(nt_status);
        }
-       ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
 
-       if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, ads->auth.user_name))) {
+       if (!NT_STATUS_IS_OK(nt_status = auth_generic_set_username(auth_generic_state, ads->auth.user_name))) {
                return ADS_ERROR_NT(nt_status);
        }
-       if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, ads->auth.realm))) {
+       if (!NT_STATUS_IS_OK(nt_status = auth_generic_set_domain(auth_generic_state, ads->auth.realm))) {
                return ADS_ERROR_NT(nt_status);
        }
-       if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_password(ntlmssp_state, ads->auth.password))) {
+       if (!NT_STATUS_IS_OK(nt_status = auth_generic_set_password(auth_generic_state, ads->auth.password))) {
                return ADS_ERROR_NT(nt_status);
        }
 
+       if (server_blob.length == 0) {
+               use_spnego_principal = false;
+       }
+
+       if (krb5_state == CRED_DONT_USE_KERBEROS) {
+               use_spnego_principal = false;
+       }
+
+       cli_credentials_set_kerberos_state(auth_generic_state->credentials,
+                                          krb5_state);
+
+       if (target_service != NULL) {
+               nt_status = gensec_set_target_service(
+                                       auth_generic_state->gensec_security,
+                                       target_service);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       return ADS_ERROR_NT(nt_status);
+               }
+       }
+
+       if (target_hostname != NULL) {
+               nt_status = gensec_set_target_hostname(
+                                       auth_generic_state->gensec_security,
+                                       target_hostname);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       return ADS_ERROR_NT(nt_status);
+               }
+       }
+
+       if (target_service != NULL && target_hostname != NULL) {
+               use_spnego_principal = false;
+       }
+
        switch (ads->ldap.wrap_type) {
        case ADS_SASLWRAP_TYPE_SEAL:
-               features = NTLMSSP_FEATURE_SIGN | NTLMSSP_FEATURE_SEAL;
+               gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SIGN);
+               gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SEAL);
                break;
        case ADS_SASLWRAP_TYPE_SIGN:
                if (ads->auth.flags & ADS_AUTH_SASL_FORCE) {
-                       features = NTLMSSP_FEATURE_SIGN;
+                       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SIGN);
                } else {
                        /*
                         * windows servers are broken with sign only,
-                        * so we need to use seal here too
+                        * so we let the NTLMSSP backend to seal here,
+                        * via GENSEC_FEATURE_LDAP_STYLE.
                         */
-                       features = NTLMSSP_FEATURE_SIGN | NTLMSSP_FEATURE_SEAL;
-                       ads->ldap.wrap_type = ADS_SASLWRAP_TYPE_SEAL;
+                       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_SIGN);
+                       gensec_want_feature(auth_generic_state->gensec_security, GENSEC_FEATURE_LDAP_STYLE);
                }
                break;
        case ADS_SASLWRAP_TYPE_PLAIN:
                break;
        }
 
-       ntlmssp_want_feature(ntlmssp_state, features);
+       nt_status = auth_generic_client_start_by_sasl(auth_generic_state,
+                                                     sasl_list);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               return ADS_ERROR_NT(nt_status);
+       }
 
-       blob_in = data_blob_null;
+       rc = LDAP_SASL_BIND_IN_PROGRESS;
+       nt_status = NT_STATUS_MORE_PROCESSING_REQUIRED;
+       if (use_spnego_principal) {
+               blob_in = data_blob_dup_talloc(talloc_tos(), server_blob);
+               if (blob_in.length == 0) {
+                       TALLOC_FREE(auth_generic_state);
+                       return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
+               }
+       } else {
+               blob_in = data_blob_null;
+       }
+       blob_out = data_blob_null;
 
-       do {
-               nt_status = ntlmssp_update(ntlmssp_state, 
-                                          blob_in, &blob_out);
-               data_blob_free(&blob_in);
-               if ((NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) 
-                    || NT_STATUS_IS_OK(nt_status))
-                   && blob_out.length) {
-                       if (turn == 1) {
-                               /* and wrap it in a SPNEGO wrapper */
-                               msg1 = gen_negTokenInit(OID_NTLMSSP, blob_out);
-                       } else {
-                               /* wrap it in SPNEGO */
-                               msg1 = spnego_gen_auth(blob_out);
-                       }
+       while (true) {
+               struct berval cred, *scred = NULL;
 
+               nt_status = gensec_update(auth_generic_state->gensec_security,
+                                         talloc_tos(), blob_in, &blob_out);
+               data_blob_free(&blob_in);
+               if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)
+                   && !NT_STATUS_IS_OK(nt_status))
+               {
+                       TALLOC_FREE(auth_generic_state);
                        data_blob_free(&blob_out);
+                       return ADS_ERROR_NT(nt_status);
+               }
 
-                       cred.bv_val = (char *)msg1.data;
-                       cred.bv_len = msg1.length;
-                       scred = NULL;
-                       rc = ldap_sasl_bind_s(ads->ldap.ld, NULL, "GSS-SPNEGO", &cred, NULL, NULL, &scred);
-                       data_blob_free(&msg1);
-                       if ((rc != LDAP_SASL_BIND_IN_PROGRESS) && (rc != 0)) {
-                               if (scred) {
-                                       ber_bvfree(scred);
-                               }
+               if (NT_STATUS_IS_OK(nt_status) && rc == 0 && blob_out.length == 0) {
+                       break;
+               }
 
-                               ntlmssp_end(&ntlmssp_state);
-                               return ADS_ERROR(rc);
-                       }
+               cred.bv_val = (char *)blob_out.data;
+               cred.bv_len = blob_out.length;
+               scred = NULL;
+               rc = ldap_sasl_bind_s(ads->ldap.ld, NULL, sasl, &cred, NULL, NULL, &scred);
+               data_blob_free(&blob_out);
+               if ((rc != LDAP_SASL_BIND_IN_PROGRESS) && (rc != 0)) {
                        if (scred) {
-                               blob = data_blob(scred->bv_val, scred->bv_len);
                                ber_bvfree(scred);
-                       } else {
-                               blob = data_blob_null;
                        }
 
+                       TALLOC_FREE(auth_generic_state);
+                       return ADS_ERROR(rc);
+               }
+               if (scred) {
+                       blob_in = data_blob_talloc(talloc_tos(),
+                                                  scred->bv_val,
+                                                  scred->bv_len);
+                       if (blob_in.length != scred->bv_len) {
+                               ber_bvfree(scred);
+                               TALLOC_FREE(auth_generic_state);
+                               return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
+                       }
+                       ber_bvfree(scred);
                } else {
+                       blob_in = data_blob_null;
+               }
+               if (NT_STATUS_IS_OK(nt_status) && rc == 0 && blob_in.length == 0) {
+                       break;
+               }
+       }
 
-                       ntlmssp_end(&ntlmssp_state);
-                       data_blob_free(&blob_out);
-                       return ADS_ERROR_NT(nt_status);
+       data_blob_free(&blob_in);
+       data_blob_free(&blob_out);
+
+       if (ads->ldap.wrap_type >= ADS_SASLWRAP_TYPE_SEAL) {
+               bool ok;
+
+               ok = gensec_have_feature(auth_generic_state->gensec_security,
+                                        GENSEC_FEATURE_SEAL);
+               if (!ok) {
+                       DEBUG(0,("The gensec feature sealing request, but unavailable\n"));
+                       TALLOC_FREE(auth_generic_state);
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_NETWORK_RESPONSE);
                }
-               
-               if ((turn == 1) && 
-                   (rc == LDAP_SASL_BIND_IN_PROGRESS)) {
-                       DATA_BLOB tmp_blob = data_blob_null;
-                       /* the server might give us back two challenges */
-                       if (!spnego_parse_challenge(blob, &blob_in, 
-                                                   &tmp_blob)) {
-
-                               ntlmssp_end(&ntlmssp_state);
-                               data_blob_free(&blob);
-                               DEBUG(3,("Failed to parse challenges\n"));
-                               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
-                       }
-                       data_blob_free(&tmp_blob);
-               } else if (rc == LDAP_SASL_BIND_IN_PROGRESS) {
-                       if (!spnego_parse_auth_response(blob, nt_status, OID_NTLMSSP, 
-                                                       &blob_in)) {
-
-                               ntlmssp_end(&ntlmssp_state);
-                               data_blob_free(&blob);
-                               DEBUG(3,("Failed to parse auth response\n"));
-                               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
-                       }
+
+               ok = gensec_have_feature(auth_generic_state->gensec_security,
+                                        GENSEC_FEATURE_SIGN);
+               if (!ok) {
+                       DEBUG(0,("The gensec feature signing request, but unavailable\n"));
+                       TALLOC_FREE(auth_generic_state);
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_NETWORK_RESPONSE);
                }
-               data_blob_free(&blob);
-               data_blob_free(&blob_out);
-               turn++;
-       } while (rc == LDAP_SASL_BIND_IN_PROGRESS && !NT_STATUS_IS_OK(nt_status));
-       
-       /* we have a reference conter on ntlmssp_state, if we are signing
-          then the state will be kept by the signing engine */
+
+       } else if (ads->ldap.wrap_type >= ADS_SASLWRAP_TYPE_SIGN) {
+               bool ok;
+
+               ok = gensec_have_feature(auth_generic_state->gensec_security,
+                                        GENSEC_FEATURE_SIGN);
+               if (!ok) {
+                       DEBUG(0,("The gensec feature signing request, but unavailable\n"));
+                       TALLOC_FREE(auth_generic_state);
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_NETWORK_RESPONSE);
+               }
+       }
+
+       ads->auth.tgs_expire = LONG_MAX;
+       end_nt_time = gensec_expire_time(auth_generic_state->gensec_security);
+       if (end_nt_time != GENSEC_EXPIRE_TIME_INFINITY) {
+               struct timeval tv;
+               nttime_to_timeval(&tv, end_nt_time);
+               ads->auth.tgs_expire = tv.tv_sec;
+       }
 
        if (ads->ldap.wrap_type > ADS_SASLWRAP_TYPE_PLAIN) {
-               ads->ldap.out.min = 4;
-               ads->ldap.out.max = 0x0FFFFFFF - NTLMSSP_SIG_SIZE;
-               ads->ldap.out.sig_size = NTLMSSP_SIG_SIZE;
-               ads->ldap.in.min = 4;
-               ads->ldap.in.max = 0x0FFFFFFF;
-               status = ads_setup_sasl_wrapping(ads, &ads_sasl_ntlmssp_ops, ntlmssp_state);
+               size_t max_wrapped = gensec_max_wrapped_size(auth_generic_state->gensec_security);
+               ads->ldap.out.max_unwrapped = gensec_max_input_size(auth_generic_state->gensec_security);
+
+               ads->ldap.out.sig_size = max_wrapped - ads->ldap.out.max_unwrapped;
+               /*
+                * Note that we have to truncate this to 0x2C
+                * (taken from a capture with LDAP unbind), as the
+                * signature size is not constant for Kerberos with
+                * arcfour-hmac-md5.
+                */
+               ads->ldap.in.min_wrapped = MIN(ads->ldap.out.sig_size, 0x2C);
+               ads->ldap.in.max_wrapped = max_wrapped;
+               status = ads_setup_sasl_wrapping(ads, &ads_sasl_gensec_ops, auth_generic_state->gensec_security);
                if (!ADS_ERR_OK(status)) {
-                       DEBUG(0, "ads_setup_sasl_wrapping() failed: %s\n",
+                       DEBUG(0, ("ads_setup_sasl_wrapping() failed: %s\n",
                                ads_errstr(status)));
-                       ntlmssp_end(&ntlmssp_state);
+                       TALLOC_FREE(auth_generic_state);
                        return status;
                }
-       } else {
-               ntlmssp_end(&ntlmssp_state);
+               /* Only keep the gensec_security element around long-term */
+               talloc_steal(NULL, auth_generic_state->gensec_security);
        }
+       TALLOC_FREE(auth_generic_state);
 
        return ADS_ERROR(rc);
 }
 
-#ifdef HAVE_GSSAPI
-static ADS_STATUS ads_sasl_gssapi_wrap(ADS_STRUCT *ads, uint8 *buf, uint32 len)
+#ifdef HAVE_KRB5
+static ADS_STATUS ads_init_gssapi_cred(ADS_STRUCT *ads, gss_cred_id_t *cred)
 {
-       gss_ctx_id_t context_handle = ads->ldap.wrap_private_data;
+       ADS_STATUS status;
+       krb5_context kctx;
+       krb5_error_code kerr;
+       krb5_ccache kccache = NULL;
+       uint32_t maj, min;
+
+       *cred = GSS_C_NO_CREDENTIAL;
+
+       if (!ads->auth.ccache_name) {
+               return ADS_SUCCESS;
+       }
+
+       kerr = krb5_init_context(&kctx);
+       if (kerr) {
+               return ADS_ERROR_KRB5(kerr);
+       }
+
+#ifdef HAVE_GSS_KRB5_IMPORT_CRED
+       kerr = krb5_cc_resolve(kctx, ads->auth.ccache_name, &kccache);
+       if (kerr) {
+               status = ADS_ERROR_KRB5(kerr);
+               goto done;
+       }
+
+       maj = gss_krb5_import_cred(&min, kccache, NULL, NULL, cred);
+       if (maj != GSS_S_COMPLETE) {
+               status = ADS_ERROR_GSS(maj, min);
+               goto done;
+       }
+#else
+       /* We need to fallback to overriding the default creds.
+        * This operation is not thread safe as it changes the process
+        * environment variable, but we do not have any better option
+        * with older kerberos libraries */
+       {
+               const char *oldccname = NULL;
+
+               oldccname = getenv("KRB5CCNAME");
+               setenv("KRB5CCNAME", ads->auth.ccache_name, 1);
+
+               maj = gss_acquire_cred(&min, GSS_C_NO_NAME, GSS_C_INDEFINITE,
+                                      NULL, GSS_C_INITIATE, cred, NULL, NULL);
+
+               if (oldccname) {
+                       setenv("KRB5CCNAME", oldccname, 1);
+               } else {
+                       unsetenv("KRB5CCNAME");
+               }
+
+               if (maj != GSS_S_COMPLETE) {
+                       status = ADS_ERROR_GSS(maj, min);
+                       goto done;
+               }
+       }
+#endif
+
+       status = ADS_SUCCESS;
+
+done:
+       if (!ADS_ERR_OK(status) && kccache != NULL) {
+               krb5_cc_close(kctx, kccache);
+       }
+       krb5_free_context(kctx);
+       return status;
+}
+
+static ADS_STATUS ads_sasl_gssapi_wrap(ADS_STRUCT *ads, uint8_t *buf, uint32_t len)
+{
+       gss_ctx_id_t context_handle = (gss_ctx_id_t)ads->ldap.wrap_private_data;
        ADS_STATUS status;
        int gss_rc;
-       uint32 minor_status;
+       uint32_t minor_status;
        gss_buffer_desc unwrapped, wrapped;
        int conf_req_flag, conf_state;
 
@@ -309,10 +456,10 @@ static ADS_STATUS ads_sasl_gssapi_wrap(ADS_STRUCT *ads, uint8 *buf, uint32 len)
 
 static ADS_STATUS ads_sasl_gssapi_unwrap(ADS_STRUCT *ads)
 {
-       gss_ctx_id_t context_handle = ads->ldap.wrap_private_data;
+       gss_ctx_id_t context_handle = (gss_ctx_id_t)ads->ldap.wrap_private_data;
        ADS_STATUS status;
        int gss_rc;
-       uint32 minor_status;
+       uint32_t minor_status;
        gss_buffer_desc unwrapped, wrapped;
        int conf_state;
 
@@ -329,7 +476,7 @@ static ADS_STATUS ads_sasl_gssapi_unwrap(ADS_STRUCT *ads)
                return ADS_ERROR_NT(NT_STATUS_ACCESS_DENIED);
        }
 
-       if (wrapped.length < wrapped.length) {
+       if (wrapped.length < unwrapped.length) {
                return ADS_ERROR_NT(NT_STATUS_INTERNAL_ERROR);
        }
 
@@ -347,8 +494,8 @@ static ADS_STATUS ads_sasl_gssapi_unwrap(ADS_STRUCT *ads)
 
 static void ads_sasl_gssapi_disconnect(ADS_STRUCT *ads)
 {
-       gss_ctx_id_t context_handle = ads->ldap.wrap_private_data;
-       uint32 minor_status;
+       gss_ctx_id_t context_handle = (gss_ctx_id_t)ads->ldap.wrap_private_data;
+       uint32_t minor_status;
 
        gss_delete_sec_context(&minor_status, &context_handle, GSS_C_NO_BUFFER);
 
@@ -363,381 +510,169 @@ static const struct ads_saslwrap_ops ads_sasl_gssapi_ops = {
        .disconnect     = ads_sasl_gssapi_disconnect
 };
 
-/* 
-   perform a LDAP/SASL/SPNEGO/GSSKRB5 bind
-*/
-static ADS_STATUS ads_sasl_spnego_gsskrb5_bind(ADS_STRUCT *ads, const gss_name_t serv_name)
-{
-       ADS_STATUS status;
-       BOOL ok;
-       uint32 minor_status;
-       int gss_rc, rc;
-       gss_OID_desc krb5_mech_type =
-       {9, CONST_DISCARD(char *, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02") };
-       gss_OID mech_type = &krb5_mech_type;
-       gss_OID actual_mech_type = GSS_C_NULL_OID;
-       const char *spnego_mechs[] = {OID_KERBEROS5_OLD, OID_KERBEROS5, OID_NTLMSSP, NULL};
-       gss_ctx_id_t context_handle = GSS_C_NO_CONTEXT;
-       gss_buffer_desc input_token, output_token;
-       uint32 req_flags, ret_flags;
-       uint32 req_tmp, ret_tmp;
-       DATA_BLOB unwrapped;
-       DATA_BLOB wrapped;
-       struct berval cred, *scred = NULL;
+#endif /* HAVE_KRB5 */
 
-       input_token.value = NULL;
-       input_token.length = 0;
+#ifdef HAVE_KRB5
+struct ads_service_principal {
+       char *service;
+       char *hostname;
+       char *string;
+#ifdef HAVE_KRB5
+       gss_name_t name;
+#endif
+};
 
-       req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG;
-       switch (ads->ldap.wrap_type) {
-       case ADS_SASLWRAP_TYPE_SEAL:
-               req_flags |= GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG;
-               break;
-       case ADS_SASLWRAP_TYPE_SIGN:
-               req_flags |= GSS_C_INTEG_FLAG;
-               break;
-       case ADS_SASLWRAP_TYPE_PLAIN:
-               break;
+static void ads_free_service_principal(struct ads_service_principal *p)
+{
+       SAFE_FREE(p->service);
+       SAFE_FREE(p->hostname);
+       SAFE_FREE(p->string);
+
+#ifdef HAVE_KRB5
+       if (p->name) {
+               uint32_t minor_status;
+               gss_release_name(&minor_status, &p->name);
        }
+#endif
+       ZERO_STRUCTP(p);
+}
 
-       /* Note: here we explicit ask for the krb5 mech_type */
-       gss_rc = gss_init_sec_context(&minor_status,
-                                     GSS_C_NO_CREDENTIAL,
-                                     &context_handle,
-                                     serv_name,
-                                     mech_type,
-                                     req_flags,
-                                     0,
-                                     NULL,
-                                     &input_token,
-                                     &actual_mech_type,
-                                     &output_token,
-                                     &ret_flags,
-                                     NULL);
-       if (gss_rc && gss_rc != GSS_S_CONTINUE_NEEDED) {
-               status = ADS_ERROR_GSS(gss_rc, minor_status);
-               goto failed;
+static ADS_STATUS ads_guess_target(ADS_STRUCT *ads,
+                                  char **service,
+                                  char **hostname,
+                                  char **principal)
+{
+       ADS_STATUS status = ADS_ERROR(LDAP_NO_MEMORY);
+       char *princ = NULL;
+       TALLOC_CTX *frame;
+       char *server = NULL;
+       char *realm = NULL;
+       int rc;
+
+       frame = talloc_stackframe();
+       if (frame == NULL) {
+               return ADS_ERROR(LDAP_NO_MEMORY);
        }
 
-       /*
-        * As some gssapi krb5 mech implementations
-        * automaticly add GSS_C_INTEG_FLAG and GSS_C_CONF_FLAG
-        * to req_flags internaly, it's not possible to
-        * use plain or signing only connection via
-        * the gssapi interface.
-        *
-        * Because of this we need to check it the ret_flags
-        * has more flags as req_flags and correct the value
-        * of ads->ldap.wrap_type.
-        *
-        * I ads->auth.flags has ADS_AUTH_SASL_FORCE
-        * we need to give an error.
-        */
-       req_tmp = req_flags & (GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
-       ret_tmp = ret_flags & (GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
+       if (ads->server.realm && ads->server.ldap_server) {
+               server = strlower_talloc(frame, ads->server.ldap_server);
+               if (server == NULL) {
+                       goto out;
+               }
 
-       if (req_tmp == ret_tmp) {
-               /* everythings fine... */
+               realm = strupper_talloc(frame, ads->server.realm);
+               if (realm == NULL) {
+                       goto out;
+               }
 
-       } else if (req_flags & GSS_C_CONF_FLAG) {
                /*
-                * here we wanted sealing but didn't got it
-                * from the gssapi library
+                * If we got a name which is bigger than a NetBIOS name,
+                * but isn't a FQDN, create one.
                 */
-               status = ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
-               goto failed;
+               if (strlen(server) > 15 && strstr(server, ".") == NULL) {
+                       char *dnsdomain;
 
-       } else if ((req_flags & GSS_C_INTEG_FLAG) &&
-                  !(ret_flags & GSS_C_INTEG_FLAG)) {
-               /*
-                * here we wanted siging but didn't got it
-                * from the gssapi library
-                */
-               status = ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
-               goto failed;
+                       dnsdomain = strlower_talloc(frame, ads->server.realm);
+                       if (dnsdomain == NULL) {
+                               goto out;
+                       }
 
-       } else if (ret_flags & GSS_C_CONF_FLAG) {
-               /*
-                * here we didn't want sealing
-                * but the gssapi library forces it
-                * so correct the needed wrap_type if
-                * the caller didn't forced siging only
-                */
-               if (ads->auth.flags & ADS_AUTH_SASL_FORCE) {
-                       status = ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
-                       goto failed;
+                       server = talloc_asprintf(frame,
+                                                "%s.%s",
+                                                server, dnsdomain);
+                       if (server == NULL) {
+                               goto out;
+                       }
+               }
+       } else if (ads->config.realm && ads->config.ldap_server_name) {
+               server = strlower_talloc(frame, ads->config.ldap_server_name);
+               if (server == NULL) {
+                       goto out;
                }
 
-               ads->ldap.wrap_type = ADS_SASLWRAP_TYPE_SEAL;
-               req_flags = ret_flags;
-
-       } else if (ret_flags & GSS_C_INTEG_FLAG) {
-               /*
-                * here we didn't want signing
-                * but the gssapi library forces it
-                * so correct the needed wrap_type if
-                * the caller didn't forced plain
-                */
-               if (ads->auth.flags & ADS_AUTH_SASL_FORCE) {
-                       status = ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
-                       goto failed;
+               realm = strupper_talloc(frame, ads->config.realm);
+               if (realm == NULL) {
+                       goto out;
                }
 
-               ads->ldap.wrap_type = ADS_SASLWRAP_TYPE_SIGN;
-               req_flags = ret_flags;
-       } else {
                /*
-                * This could (should?) not happen
+                * If we got a name which is bigger than a NetBIOS name,
+                * but isn't a FQDN, create one.
                 */
-               status = ADS_ERROR_NT(NT_STATUS_INTERNAL_ERROR);
-               goto failed;
-       
-       }
+               if (strlen(server) > 15 && strstr(server, ".") == NULL) {
+                       char *dnsdomain;
 
-       /* and wrap that in a shiny SPNEGO wrapper */
-       unwrapped = data_blob_const(output_token.value, output_token.length);
-       wrapped = gen_negTokenTarg(spnego_mechs, unwrapped);
-       gss_release_buffer(&minor_status, &output_token);
-       if (unwrapped.length > wrapped.length) {
-               status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
-               goto failed;
-       }
-
-       cred.bv_val = (char *)wrapped.data;
-       cred.bv_len = wrapped.length;
+                       dnsdomain = strlower_talloc(frame, ads->server.realm);
+                       if (dnsdomain == NULL) {
+                               goto out;
+                       }
 
-       rc = ldap_sasl_bind_s(ads->ldap.ld, NULL, "GSS-SPNEGO", &cred, NULL, NULL, 
-                             &scred);
-       data_blob_free(&wrapped);
-       if (rc != LDAP_SUCCESS) {
-               status = ADS_ERROR(rc);
-               goto failed;
+                       server = talloc_asprintf(frame,
+                                                "%s.%s",
+                                                server, dnsdomain);
+                       if (server == NULL) {
+                               goto out;
+                       }
+               }
        }
 
-       if (scred) {
-               wrapped = data_blob_const(scred->bv_val, scred->bv_len);
-       } else {
-               wrapped = data_blob_null;
+       if (server == NULL || realm == NULL) {
+               goto out;
        }
 
-       ok = spnego_parse_auth_response(wrapped, NT_STATUS_OK,
-                                       OID_KERBEROS5_OLD,
-                                       &unwrapped);
-       if (scred) ber_bvfree(scred);
-       if (!ok) {
-               status = ADS_ERROR_NT(NT_STATUS_INVALID_NETWORK_RESPONSE);
-               goto failed;
+       *service = SMB_STRDUP("ldap");
+       if (*service == NULL) {
+               status = ADS_ERROR(LDAP_PARAM_ERROR);
+               goto out;
        }
-
-       input_token.value       = unwrapped.data;
-       input_token.length      = unwrapped.length;
-
-       /* 
-        * As we asked for mutal authentication
-        * we need to pass the servers response
-        * to gssapi
-        */
-       gss_rc = gss_init_sec_context(&minor_status,
-                                     GSS_C_NO_CREDENTIAL,
-                                     &context_handle,
-                                     serv_name,
-                                     mech_type,
-                                     req_flags,
-                                     0,
-                                     NULL,
-                                     &input_token,
-                                     &actual_mech_type,
-                                     &output_token,
-                                     &ret_flags,
-                                     NULL);
-       data_blob_free(&unwrapped);
-       if (gss_rc) {
-               status = ADS_ERROR_GSS(gss_rc, minor_status);
-               goto failed;
+       *hostname = SMB_STRDUP(server);
+       if (*hostname == NULL) {
+               SAFE_FREE(*service);
+               status = ADS_ERROR(LDAP_PARAM_ERROR);
+               goto out;
        }
-
-       gss_release_buffer(&minor_status, &output_token);
-
-       /*
-        * If we the sign and seal options
-        * doesn't match after getting the response
-        * from the server, we don't want to use the connection
-        */
-       req_tmp = req_flags & (GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
-       ret_tmp = ret_flags & (GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG);
-
-       if (req_tmp != ret_tmp) {
-               /* everythings fine... */
-               status = ADS_ERROR_NT(NT_STATUS_INVALID_NETWORK_RESPONSE);
-               goto failed;
+       rc = asprintf(&princ, "ldap/%s@%s", server, realm);
+       if (rc == -1 || princ == NULL) {
+               SAFE_FREE(*service);
+               SAFE_FREE(*hostname);
+               status = ADS_ERROR(LDAP_PARAM_ERROR);
+               goto out;
        }
 
-       if (ads->ldap.wrap_type > ADS_SASLWRAP_TYPE_PLAIN) {
-               uint32 max_msg_size = 0x0A000000;
-
-               gss_rc = gss_wrap_size_limit(&minor_status, context_handle,
-                                            (ads->ldap.wrap_type == ADS_SASLWRAP_TYPE_SEAL),
-                                            GSS_C_QOP_DEFAULT,
-                                            max_msg_size, &ads->ldap.out.max);
-               if (gss_rc) {
-                       status = ADS_ERROR_GSS(gss_rc, minor_status);
-                       goto failed;
-               }
-
-               ads->ldap.out.min = 4;
-               ads->ldap.out.sig_size = max_msg_size - ads->ldap.out.max;
-               ads->ldap.in.min = 4;
-               ads->ldap.in.max = max_msg_size;
-               status = ads_setup_sasl_wrapping(ads, &ads_sasl_gssapi_ops, context_handle);
-               if (!ADS_ERR_OK(status)) {
-                       DEBUG(0, "ads_setup_sasl_wrapping() failed: %s\n",
-                               ads_errstr(status)));
-                       goto failed;
-               }
-               /* make sure we don't free context_handle */
-               context_handle = GSS_C_NO_CONTEXT;
-       }
+       *principal = princ;
 
-failed:
-       if (context_handle != GSS_C_NO_CONTEXT)
-               gss_delete_sec_context(&minor_status, &context_handle, GSS_C_NO_BUFFER);
+       status = ADS_SUCCESS;
+out:
+       TALLOC_FREE(frame);
        return status;
 }
 
-#endif
-
-#ifdef HAVE_KRB5
-struct ads_service_principal {
-        krb5_context ctx;
-        char *string;
-        krb5_principal principal;
-#ifdef HAVE_GSSAPI
-        gss_name_t name;
-#endif
-};
-
-static void ads_free_service_principal(struct ads_service_principal *p)
-{
-       SAFE_FREE(p->string);
-
-#ifdef HAVE_GSSAPI
-       if (p->name) {
-               uint32 minor_status;
-               gss_release_name(&minor_status, &p->name);
-       }
-#endif
-       if (p->principal) {
-               krb5_free_principal(p->ctx, p->principal);
-       }
-
-       if (p->ctx) {
-               krb5_free_context(p->ctx);
-       }
-
-       ZERO_STRUCTP(p);
-}
-
 static ADS_STATUS ads_generate_service_principal(ADS_STRUCT *ads,
-                                                const char *given_principal,
                                                 struct ads_service_principal *p)
 {
        ADS_STATUS status;
-       krb5_enctype enc_types[] = {
-#ifdef ENCTYPE_ARCFOUR_HMAC
-                       ENCTYPE_ARCFOUR_HMAC,
-#endif
-                       ENCTYPE_DES_CBC_MD5,
-                       ENCTYPE_NULL};
-#ifdef HAVE_GSSAPI
+#ifdef HAVE_KRB5
        gss_buffer_desc input_name;
-       gss_OID_desc nt_principal = 
-       {10, CONST_DISCARD(char *, "\052\206\110\206\367\022\001\002\002\002")};
-       uint32 minor_status;
+       /* GSS_KRB5_NT_PRINCIPAL_NAME */
+       gss_OID_desc nt_principal =
+       {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x01")};
+       uint32_t minor_status;
        int gss_rc;
 #endif
 
        ZERO_STRUCTP(p);
 
-       /* I've seen a child Windows 2000 domain not send 
-          the principal name back in the first round of 
-          the SASL bind reply.  So we guess based on server
-          name and realm.  --jerry  */
-       if (given_principal) {
-               p->string = SMB_STRDUP(given_principal);
-               if (!p->string) {
-                       return ADS_ERROR(LDAP_NO_MEMORY);
-               }
-       } else if (ads->server.realm && ads->server.ldap_server) {
-               char *server, *server_realm;
-
-               server = SMB_STRDUP(ads->server.ldap_server);
-               server_realm = SMB_STRDUP(ads->server.realm);
-
-               if (!server || !server_realm) {
-                       return ADS_ERROR(LDAP_NO_MEMORY);
-               }
-
-               strlower_m(server);
-               strupper_m(server_realm);
-               asprintf(&p->string, "ldap/%s@%s", server, server_realm);
-
-               SAFE_FREE(server);
-               SAFE_FREE(server_realm);
-
-               if (!p->string) {
-                       return ADS_ERROR(LDAP_NO_MEMORY);
-               }
-       } else if (ads->config.realm && ads->config.ldap_server_name) {
-               char *server, *server_realm;
-
-               server = SMB_STRDUP(ads->config.ldap_server_name);
-               server_realm = SMB_STRDUP(ads->config.realm);
-
-               if (!server || !server_realm) {
-                       return ADS_ERROR(LDAP_NO_MEMORY);
-               }
-
-               strlower_m(server);
-               strupper_m(server_realm);
-               asprintf(&p->string, "ldap/%s@%s", server, server_realm);
-
-               SAFE_FREE(server);
-               SAFE_FREE(server_realm);
-
-               if (!p->string) {
-                       return ADS_ERROR(LDAP_NO_MEMORY);
-               }
-       }
-
-       initialize_krb5_error_table();
-       status = ADS_ERROR_KRB5(krb5_init_context(&p->ctx));
-       if (!ADS_ERR_OK(status)) {
-               ads_free_service_principal(p);
-               return status;
-       }
-       status = ADS_ERROR_KRB5(krb5_set_default_tgs_ktypes(p->ctx, enc_types));
+       status = ads_guess_target(ads,
+                                 &p->service,
+                                 &p->hostname,
+                                 &p->string);
        if (!ADS_ERR_OK(status)) {
-               ads_free_service_principal(p);
-               return status;
-       }
-       status = ADS_ERROR_KRB5(smb_krb5_parse_name(p->ctx, p->string, &p->principal));
-       if (!ADS_ERR_OK(status)) {
-               ads_free_service_principal(p);
                return status;
        }
 
-#ifdef HAVE_GSSAPI
-       /*
-        * The MIT libraries have a *HORRIBLE* bug - input_value.value needs
-        * to point to the *address* of the krb5_principal, and the gss libraries
-        * to a shallow copy of the krb5_principal pointer - so we need to keep
-        * the krb5_principal around until we do the gss_release_name. MIT *SUCKS* !
-        * Just one more way in which MIT engineers screwed me over.... JRA.
-        *
-        * That's the reason for principal not beeing a local var in this function
-        */
-       input_name.value = &p->principal;
-       input_name.length = sizeof(p->principal);
+#ifdef HAVE_KRB5
+       input_name.value = p->string;
+       input_name.length = strlen(p->string);
 
        gss_rc = gss_import_name(&minor_status, &input_name, &nt_principal, &p->name);
        if (gss_rc) {
@@ -746,71 +681,18 @@ static ADS_STATUS ads_generate_service_principal(ADS_STRUCT *ads,
        }
 #endif
 
-       return status;
-}
-
-/* 
-   perform a LDAP/SASL/SPNEGO/KRB5 bind
-*/
-static ADS_STATUS ads_sasl_spnego_rawkrb5_bind(ADS_STRUCT *ads, const char *principal)
-{
-       DATA_BLOB blob = data_blob_null;
-       struct berval cred, *scred = NULL;
-       DATA_BLOB session_key = data_blob_null;
-       int rc;
-
-       if (ads->ldap.wrap_type > ADS_SASLWRAP_TYPE_PLAIN) {
-               return ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
-       }
-
-       rc = spnego_gen_negTokenTarg(principal, ads->auth.time_offset, &blob, &session_key, 0,
-                                    &ads->auth.tgs_expire);
-
-       if (rc) {
-               return ADS_ERROR_KRB5(rc);
-       }
-
-       /* now send the auth packet and we should be done */
-       cred.bv_val = (char *)blob.data;
-       cred.bv_len = blob.length;
-
-       rc = ldap_sasl_bind_s(ads->ldap.ld, NULL, "GSS-SPNEGO", &cred, NULL, NULL, &scred);
-
-       data_blob_free(&blob);
-       data_blob_free(&session_key);
-       if(scred)
-               ber_bvfree(scred);
-
-       return ADS_ERROR(rc);
+       return ADS_SUCCESS;
 }
 
-static ADS_STATUS ads_sasl_spnego_krb5_bind(ADS_STRUCT *ads,
-                                           struct ads_service_principal *p)
-{
-#ifdef HAVE_GSSAPI
-       /*
-        * we only use the gsskrb5 based implementation
-        * when sasl sign or seal is requested.
-        *
-        * This has the following reasons:
-        * - it's likely that the gssapi krb5 mech implementation
-        *   doesn't support to negotiate plain connections
-        * - the ads_sasl_spnego_rawkrb5_bind is more robust
-        *   against clock skew errors
-        */
-       if (ads->ldap.wrap_type > ADS_SASLWRAP_TYPE_PLAIN) {
-               return ads_sasl_spnego_gsskrb5_bind(ads, p->name);
-       }
-#endif
-       return ads_sasl_spnego_rawkrb5_bind(ads, p->string);
-}
-#endif
+#endif /* HAVE_KRB5 */
 
 /* 
    this performs a SASL/SPNEGO bind
 */
 static ADS_STATUS ads_sasl_spnego_bind(ADS_STRUCT *ads)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct ads_service_principal p = {0};
        struct berval *scred=NULL;
        int rc, i;
        ADS_STATUS status;
@@ -818,14 +700,14 @@ static ADS_STATUS ads_sasl_spnego_bind(ADS_STRUCT *ads)
        char *given_principal = NULL;
        char *OIDs[ASN1_MAX_OIDS];
 #ifdef HAVE_KRB5
-       BOOL got_kerberos_mechanism = False;
+       bool got_kerberos_mechanism = False;
 #endif
 
        rc = ldap_sasl_bind_s(ads->ldap.ld, NULL, "GSS-SPNEGO", NULL, NULL, NULL, &scred);
 
        if (rc != LDAP_SASL_BIND_IN_PROGRESS) {
                status = ADS_ERROR(rc);
-               goto failed;
+               goto done;
        }
 
        blob = data_blob(scred->bv_val, scred->bv_len);
@@ -838,12 +720,12 @@ static ADS_STATUS ads_sasl_spnego_bind(ADS_STRUCT *ads)
 
        /* the server sent us the first part of the SPNEGO exchange in the negprot 
           reply */
-       if (!spnego_parse_negTokenInit(blob, OIDs, &given_principal)) {
-               data_blob_free(&blob);
+       if (!spnego_parse_negTokenInit(talloc_tos(), blob, OIDs, &given_principal, NULL) ||
+                       OIDs[0] == NULL) {
                status = ADS_ERROR(LDAP_OPERATIONS_ERROR);
-               goto failed;
+               goto done;
        }
-       data_blob_free(&blob);
+       TALLOC_FREE(given_principal);
 
        /* make sure the server understands kerberos */
        for (i=0;OIDs[i];i++) {
@@ -854,60 +736,66 @@ static ADS_STATUS ads_sasl_spnego_bind(ADS_STRUCT *ads)
                        got_kerberos_mechanism = True;
                }
 #endif
-               free(OIDs[i]);
+               talloc_free(OIDs[i]);
+       }
+
+       status = ads_generate_service_principal(ads, &p);
+       if (!ADS_ERR_OK(status)) {
+               goto done;
        }
-       DEBUG(3,("ads_sasl_spnego_bind: got server principal name = %s\n", given_principal));
 
 #ifdef HAVE_KRB5
        if (!(ads->auth.flags & ADS_AUTH_DISABLE_KERBEROS) &&
            got_kerberos_mechanism) 
        {
-               struct ads_service_principal p;
-
-               status = ads_generate_service_principal(ads, given_principal, &p);
-               SAFE_FREE(given_principal);
-               if (!ADS_ERR_OK(status)) {
-                       return status;
-               }
-
-               status = ads_sasl_spnego_krb5_bind(ads, &p);
+               status = ads_sasl_spnego_gensec_bind(ads, "GSS-SPNEGO",
+                                                    CRED_MUST_USE_KERBEROS,
+                                                    p.service, p.hostname,
+                                                    blob);
                if (ADS_ERR_OK(status)) {
                        ads_free_service_principal(&p);
-                       return status;
+                       goto done;
                }
 
-               DEBUG(10,("ads_sasl_spnego_krb5_bind failed with: %s, "
+               DEBUG(10,("ads_sasl_spnego_gensec_bind(KRB5) failed with: %s, "
                          "calling kinit\n", ads_errstr(status)));
 
                status = ADS_ERROR_KRB5(ads_kinit_password(ads)); 
 
                if (ADS_ERR_OK(status)) {
-                       status = ads_sasl_spnego_krb5_bind(ads, &p);
+                       status = ads_sasl_spnego_gensec_bind(ads, "GSS-SPNEGO",
+                                                       CRED_MUST_USE_KERBEROS,
+                                                       p.service, p.hostname,
+                                                       blob);
+                       if (!ADS_ERR_OK(status)) {
+                               DEBUG(0,("kinit succeeded but "
+                                       "ads_sasl_spnego_gensec_bind(KRB5) failed: %s\n",
+                                       ads_errstr(status)));
+                       }
                }
 
-               ads_free_service_principal(&p);
-
                /* only fallback to NTLMSSP if allowed */
                if (ADS_ERR_OK(status) || 
                    !(ads->auth.flags & ADS_AUTH_ALLOW_NTLMSSP)) {
-                       return status;
+                       goto done;
                }
-       } else
-#endif
-       {
-               SAFE_FREE(given_principal);
        }
+#endif
 
        /* lets do NTLMSSP ... this has the big advantage that we don't need
           to sync clocks, and we don't rely on special versions of the krb5 
           library for HMAC_MD4 encryption */
-       return ads_sasl_spnego_ntlmssp_bind(ads);
-
-failed:
+       status = ads_sasl_spnego_gensec_bind(ads, "GSS-SPNEGO",
+                                            CRED_DONT_USE_KERBEROS,
+                                            p.service, p.hostname,
+                                            data_blob_null);
+done:
+       ads_free_service_principal(&p);
+       TALLOC_FREE(frame);
        return status;
 }
 
-#ifdef HAVE_GSSAPI
+#ifdef HAVE_KRB5
 #define MAX_GSS_PASSES 3
 
 /* this performs a SASL/gssapi bind
@@ -918,38 +806,40 @@ failed:
 */
 static ADS_STATUS ads_sasl_gssapi_do_bind(ADS_STRUCT *ads, const gss_name_t serv_name)
 {
-       uint32 minor_status;
+       uint32_t minor_status;
+       gss_cred_id_t gss_cred = GSS_C_NO_CREDENTIAL;
        gss_ctx_id_t context_handle = GSS_C_NO_CONTEXT;
        gss_OID mech_type = GSS_C_NULL_OID;
        gss_buffer_desc output_token, input_token;
-       uint32 req_flags, ret_flags;
+       uint32_t req_flags, ret_flags;
        int conf_state;
        struct berval cred;
        struct berval *scred = NULL;
        int i=0;
        int gss_rc, rc;
-       uint8 *p;
-       uint32 max_msg_size = 0;
+       uint8_t *p;
+       uint32_t max_msg_size = ADS_SASL_WRAPPING_OUT_MAX_WRAPPED;
+       uint8_t wrap_type = ADS_SASLWRAP_TYPE_PLAIN;
        ADS_STATUS status;
 
        input_token.value = NULL;
        input_token.length = 0;
 
-       req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG;
-       switch (ads->ldap.wrap_type) {
-       case ADS_SASLWRAP_TYPE_SEAL:
-               req_flags |= GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG;
-               break;
-       case ADS_SASLWRAP_TYPE_SIGN:
-               req_flags |= GSS_C_INTEG_FLAG;
-               break;
-       case ADS_SASLWRAP_TYPE_PLAIN:
-               break;
+       status = ads_init_gssapi_cred(ads, &gss_cred);
+       if (!ADS_ERR_OK(status)) {
+               goto failed;
        }
 
+       /*
+        * Note: here we always ask the gssapi for sign and seal
+        *       as this is negotiated later after the mutal
+        *       authentication
+        */
+       req_flags = GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_INTEG_FLAG | GSS_C_CONF_FLAG;
+
        for (i=0; i < MAX_GSS_PASSES; i++) {
                gss_rc = gss_init_sec_context(&minor_status,
-                                         GSS_C_NO_CREDENTIAL,
+                                         gss_cred,
                                          &context_handle,
                                          serv_name,
                                          mech_type,
@@ -961,11 +851,10 @@ static ADS_STATUS ads_sasl_gssapi_do_bind(ADS_STRUCT *ads, const gss_name_t serv
                                          &output_token,
                                          &ret_flags,
                                          NULL);
-
-               if (input_token.value) {
-                       gss_release_buffer(&minor_status, &input_token);
+               if (scred) {
+                       ber_bvfree(scred);
+                       scred = NULL;
                }
-
                if (gss_rc && gss_rc != GSS_S_CONTINUE_NEEDED) {
                        status = ADS_ERROR_GSS(gss_rc, minor_status);
                        goto failed;
@@ -998,34 +887,58 @@ static ADS_STATUS ads_sasl_gssapi_do_bind(ADS_STRUCT *ads, const gss_name_t serv
 
        gss_rc = gss_unwrap(&minor_status,context_handle,&input_token,&output_token,
                            &conf_state,NULL);
+       if (scred) {
+               ber_bvfree(scred);
+               scred = NULL;
+       }
        if (gss_rc) {
                status = ADS_ERROR_GSS(gss_rc, minor_status);
                goto failed;
        }
 
-       gss_release_buffer(&minor_status, &input_token);
-
-       p = (uint8 *)output_token.value;
+       p = (uint8_t *)output_token.value;
 
 #if 0
        file_save("sasl_gssapi.dat", output_token.value, output_token.length);
 #endif
 
        if (p) {
-               max_msg_size = (p[1]<<16) | (p[2]<<8) | p[3];
+               wrap_type = CVAL(p,0);
+               SCVAL(p,0,0);
+               max_msg_size = RIVAL(p,0);
        }
 
        gss_release_buffer(&minor_status, &output_token);
 
+       if (!(wrap_type & ads->ldap.wrap_type)) {
+               /*
+                * the server doesn't supports the wrap
+                * type we want :-(
+                */
+               DEBUG(0,("The ldap sasl wrap type doesn't match wanted[%d] server[%d]\n",
+                       ads->ldap.wrap_type, wrap_type));
+               DEBUGADD(0,("You may want to set the 'client ldap sasl wrapping' option\n"));
+               status = ADS_ERROR_NT(NT_STATUS_NOT_SUPPORTED);
+               goto failed;
+       }
+
+       /* 0x58 is the minimum windows accepts */
+       if (max_msg_size < 0x58) {
+               max_msg_size = 0x58;
+       }
+
        output_token.length = 4;
        output_token.value = SMB_MALLOC(output_token.length);
-       p = (uint8 *)output_token.value;
+       if (!output_token.value) {
+               output_token.length = 0;
+               status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
+               goto failed;
+       }
+       p = (uint8_t *)output_token.value;
+
+       RSIVAL(p,0,max_msg_size);
+       SCVAL(p,0,ads->ldap.wrap_type);
 
-       *p++ = ads->ldap.wrap_type;
-       /* choose the same size as the server gave us */
-       *p++ = max_msg_size>>16;
-       *p++ = max_msg_size>>8;
-       *p++ = max_msg_size;
        /*
         * we used to add sprintf("dn:%s", ads->config.bind_path) here.
         * but using ads->config.bind_path is the wrong! It should be
@@ -1036,14 +949,19 @@ static ADS_STATUS ads_sasl_gssapi_do_bind(ADS_STRUCT *ads, const gss_name_t serv
         */
 
        gss_rc = gss_wrap(&minor_status, context_handle,0,GSS_C_QOP_DEFAULT,
-                         &output_token, &conf_state,
-                         &input_token);
+                       &output_token, /* used as *input* here. */
+                       &conf_state,
+                       &input_token); /* Used as *output* here. */
        if (gss_rc) {
                status = ADS_ERROR_GSS(gss_rc, minor_status);
+               output_token.length = 0;
+               SAFE_FREE(output_token.value);
                goto failed;
        }
 
-       free(output_token.value);
+       /* We've finished with output_token. */
+       SAFE_FREE(output_token.value);
+       output_token.length = 0;
 
        cred.bv_val = (char *)input_token.value;
        cred.bv_len = input_token.length;
@@ -1060,27 +978,28 @@ static ADS_STATUS ads_sasl_gssapi_do_bind(ADS_STRUCT *ads, const gss_name_t serv
                gss_rc = gss_wrap_size_limit(&minor_status, context_handle,
                                             (ads->ldap.wrap_type == ADS_SASLWRAP_TYPE_SEAL),
                                             GSS_C_QOP_DEFAULT,
-                                            max_msg_size, &ads->ldap.out.max);
+                                            max_msg_size, &ads->ldap.out.max_unwrapped);
                if (gss_rc) {
                        status = ADS_ERROR_GSS(gss_rc, minor_status);
                        goto failed;
                }
 
-               ads->ldap.out.min = 4;
-               ads->ldap.out.sig_size = max_msg_size - ads->ldap.out.max;
-               ads->ldap.in.min = 4;
-               ads->ldap.in.max = max_msg_size;
+               ads->ldap.out.sig_size = max_msg_size - ads->ldap.out.max_unwrapped;
+               ads->ldap.in.min_wrapped = 0x2C; /* taken from a capture with LDAP unbind */
+               ads->ldap.in.max_wrapped = max_msg_size;
                status = ads_setup_sasl_wrapping(ads, &ads_sasl_gssapi_ops, context_handle);
                if (!ADS_ERR_OK(status)) {
-                       DEBUG(0, "ads_setup_sasl_wrapping() failed: %s\n",
+                       DEBUG(0, ("ads_setup_sasl_wrapping() failed: %s\n",
                                ads_errstr(status)));
                        goto failed;
                }
                /* make sure we don't free context_handle */
                context_handle = GSS_C_NO_CONTEXT;
        }
-failed:
 
+failed:
+       if (gss_cred != GSS_C_NO_CREDENTIAL)
+               gss_release_cred(&minor_status, &gss_cred);
        if (context_handle != GSS_C_NO_CONTEXT)
                gss_delete_sec_context(&minor_status, &context_handle, GSS_C_NO_BUFFER);
 
@@ -1094,7 +1013,7 @@ static ADS_STATUS ads_sasl_gssapi_bind(ADS_STRUCT *ads)
        ADS_STATUS status;
        struct ads_service_principal p;
 
-       status = ads_generate_service_principal(ads, NULL, &p);
+       status = ads_generate_service_principal(ads, &p);
        if (!ADS_ERR_OK(status)) {
                return status;
        }
@@ -1119,7 +1038,7 @@ static ADS_STATUS ads_sasl_gssapi_bind(ADS_STRUCT *ads)
        return status;
 }
 
-#endif /* HAVE_GGSAPI */
+#endif /* HAVE_KRB5 */
 
 /* mapping between SASL mechanisms and functions */
 static struct {
@@ -1127,7 +1046,7 @@ static struct {
        ADS_STATUS (*fn)(ADS_STRUCT *);
 } sasl_mechanisms[] = {
        {"GSS-SPNEGO", ads_sasl_spnego_bind},
-#ifdef HAVE_GSSAPI
+#ifdef HAVE_KRB5
        {"GSSAPI", ads_sasl_gssapi_bind}, /* doesn't work with .NET RC1. No idea why */
 #endif
        {NULL, NULL}
@@ -1161,7 +1080,17 @@ ADS_STATUS ads_sasl_bind(ADS_STRUCT *ads)
                for (j=0;values && values[j];j++) {
                        if (strcmp(values[j], sasl_mechanisms[i].name) == 0) {
                                DEBUG(4,("Found SASL mechanism %s\n", values[j]));
+retry:
                                status = sasl_mechanisms[i].fn(ads);
+                               if (status.error_type == ENUM_ADS_ERROR_LDAP &&
+                                   status.err.rc == LDAP_STRONG_AUTH_REQUIRED &&
+                                   ads->ldap.wrap_type == ADS_SASLWRAP_TYPE_PLAIN)
+                               {
+                                       DEBUG(3,("SASL bin got LDAP_STRONG_AUTH_REQUIRED "
+                                                "retrying with signing enabled\n"));
+                                       ads->ldap.wrap_type = ADS_SASLWRAP_TYPE_SIGN;
+                                       goto retry;
+                               }
                                ldap_value_free(values);
                                ldap_msgfree(res);
                                return status;