Revert "Revert "try to guard against multiple shibboleth library initializations""
[moonshot.git] / mech_eap / util_krb.c
index 836fef1..88ad6dd 100644 (file)
@@ -54,6 +54,40 @@ createKrbContextKey(void)
     GSSEAP_KEY_CREATE(&krbContextKey, destroyKrbContext);
 }
 
+static krb5_error_code
+initKrbContext(krb5_context *pKrbContext)
+{
+    krb5_context krbContext;
+    krb5_error_code code;
+    char *defaultRealm = NULL;
+
+    *pKrbContext = NULL;
+
+    code = krb5_init_context(&krbContext);
+    if (code != 0)
+        goto cleanup;
+
+    krb5_appdefault_string(krbContext, "eap_gss",
+                           NULL, "default_realm", "", &defaultRealm);
+
+    if (defaultRealm != NULL && defaultRealm[0] != '\0') {
+        code = krb5_set_default_realm(krbContext, defaultRealm);
+        if (code != 0)
+            goto cleanup;
+    }
+
+    *pKrbContext = krbContext;
+
+cleanup:
+    if (code != 0 && krbContext != NULL)
+        krb5_free_context(krbContext);
+
+    if (defaultRealm != NULL)
+        GSSEAP_FREE(defaultRealm);
+
+    return code;
+}
+
 OM_uint32
 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context)
 {
@@ -63,7 +97,7 @@ gssEapKerberosInit(OM_uint32 *minor, krb5_context *context)
 
     *context = GSSEAP_GETSPECIFIC(krbContextKey);
     if (*context == NULL) {
-        *minor = krb5_init_context(context);
+        *minor = initKrbContext(context);
         if (*minor == 0) {
             if (GSSEAP_SETSPECIFIC(krbContextKey, *context) != 0) {
                 *minor = errno;
@@ -266,26 +300,6 @@ rfc3961ChecksumTypeForKey(OM_uint32 *minor,
     return GSS_S_COMPLETE;
 }
 
-#ifdef HAVE_HEIMDAL_VERSION
-static heim_general_string krbAnonymousPrincipalComponents[] =
-    { KRB5_WELLKNOWN_NAME, KRB5_ANON_NAME };
-
-static const Principal krbAnonymousPrincipalData = {
-    { KRB5_NT_WELLKNOWN, { 2, krbAnonymousPrincipalComponents } },
-    "WELLKNOWN:ANONYMOUS"
-};
-#endif
-
-krb5_const_principal
-krbAnonymousPrincipal(void)
-{
-#ifdef HAVE_HEIMDAL_VERSION
-    return &krbAnonymousPrincipalData;
-#else
-    return krb5_anonymous_principal();
-#endif
-}
-
 krb5_error_code
 krbCryptoLength(krb5_context krbContext,
 #ifdef HAVE_HEIMDAL_VERSION