r11382: Require number of required M4 macros
[samba.git] / source4 / kdc / hdb-ldb.c
index d4c281de821aaab62be7ddb8633d71b34c7ef1f6..8a43a192515d1d831d26d72dd48919472075e3f2 100644 (file)
 #include "lib/ldb/include/ldb.h"
 #include "system/iconv.h"
 
+enum hdb_ldb_ent_type 
+{ HDB_LDB_ENT_TYPE_CLIENT, HDB_LDB_ENT_TYPE_SERVER, 
+  HDB_LDB_ENT_TYPE_KRBTGT, HDB_LDB_ENT_TYPE_ANY };
 
 static const char * const krb5_attrs[] = {
        "objectClass",
        "cn",
-       "name",
        "sAMAccountName",
 
        "userPrincipalName",
@@ -77,6 +79,17 @@ static const char * const krb5_attrs[] = {
        NULL
 };
 
+const char *cross_ref_attrs[] = {
+       "nCName", 
+       NULL
+};
+
+const char *realm_attrs[] = {
+       "dnsDomain", 
+       "maxPwdAge",
+       NULL
+};
+
 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
 {
     const char *tmp;
@@ -95,7 +108,7 @@ static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, con
     return timegm(&tm);
 }
 
-static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ent_type ent_type) 
+static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type) 
 {
        HDBFlags flags = int2HDBFlags(0);
 
@@ -107,41 +120,47 @@ static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum h
        /* mark the principal as invalid to start with */
        flags.invalid = 1;
 
+       flags.renewable = 1;
+
        /* Account types - clear the invalid bit if it turns out to be valid */
        if (userAccountControl & UF_NORMAL_ACCOUNT) {
-               if (ent_type == HDB_ENT_TYPE_CLIENT || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.client = 1;
                }
+               if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
+                       flags.server = 1;
+               }
                flags.invalid = 0;
        }
        
        if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
-               if (ent_type == HDB_ENT_TYPE_CLIENT || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.client = 1;
                }
                flags.invalid = 0;
        }
        if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
-               if (ent_type == HDB_ENT_TYPE_CLIENT || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.client = 1;
                }
-               if (ent_type == HDB_ENT_TYPE_SERVER || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.server = 1;
                }
                flags.invalid = 0;
        }
        if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
-               if (ent_type == HDB_ENT_TYPE_CLIENT || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.client = 1;
                }
-               if (ent_type == HDB_ENT_TYPE_SERVER || ent_type == HDB_ENT_TYPE_ENUM) {
+               if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
                        flags.server = 1;
                }
                flags.invalid = 0;
        }
 
+       /* Not permitted to act as a client if disabled */
        if (userAccountControl & UF_ACCOUNTDISABLE) {
-               flags.invalid = 1;
+               flags.client = 0;
        }
        if (userAccountControl & UF_LOCKOUT) {
                flags.invalid = 1;
@@ -208,7 +227,7 @@ static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum h
  */
 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db, 
                                         TALLOC_CTX *mem_ctx, krb5_const_principal principal,
-                                        enum hdb_ent_type ent_type, struct ldb_message *realm_msg,
+                                        enum hdb_ldb_ent_type ent_type, struct ldb_message *realm_msg,
                                         struct ldb_message *msg,
                                         hdb_entry *ent)
 {
@@ -217,18 +236,13 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
        int i;
        krb5_error_code ret = 0;
        const char *dnsdomain = ldb_msg_find_string(realm_msg, "dnsDomain", NULL);
-       char *realm = talloc_strdup(mem_ctx, dnsdomain);
+       char *realm = strupper_talloc(mem_ctx, dnsdomain);
 
        if (!realm) {
                krb5_set_error_string(context, "talloc_strdup: out of memory");
                ret = ENOMEM;
                goto out;
        }
-
-       /* TODO: Use Samba charset functions */
-       for (i=0; i< strlen(realm); i++) {
-               realm[i] = toupper(realm[i]);
-       }
                        
        krb5_warnx(context, "LDB_message2entry:\n");
 
@@ -237,7 +251,7 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
        userAccountControl = ldb_msg_find_int(msg, "userAccountControl", 0);
        
        ent->principal = malloc(sizeof(*(ent->principal)));
-       if (ent_type == HDB_ENT_TYPE_ENUM && principal == NULL) {
+       if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
                const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
                if (!samAccountName) {
                        krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
@@ -277,7 +291,7 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
 
        ent->flags = uf2HDBFlags(context, userAccountControl, ent_type);
 
-       if (ent_type == HDB_ENT_TYPE_KRBTGT) {
+       if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
                ent->flags.invalid = 0;
                ent->flags.server = 1;
        }
@@ -321,8 +335,8 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
                const char *user_principal_name = ldb_msg_find_string(msg, "userPrincipalName", NULL);
                struct ldb_message_element *objectclasses;
                struct ldb_val computer_val;
-               computer_val.data = "computer";
-               computer_val.length = strlen(computer_val.data);
+               computer_val.data = discard_const_p(uint8_t,"computer");
+               computer_val.length = strlen((const char *)computer_val.data);
                
                objectclasses = ldb_msg_find_element(msg, "objectClass");
 
@@ -360,12 +374,14 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
                }
 
                if (ret == 0) {
+                       size_t num_keys = ent->keys.len;
                        /*
                         * create keys from unicodePwd
                         */
                        ret = hdb_generate_key_set_password(context, salt_principal, 
-                                                   unicodePwd, 
-                                                           &ent->keys.val, &ent->keys.len);
+                                                           unicodePwd, 
+                                                           &ent->keys.val, &num_keys);
+                       ent->keys.len = num_keys;
                        krb5_free_principal(context, salt_principal);
                }
 
@@ -387,7 +403,8 @@ static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
                } else if (val->length < 16) {
                        ent->keys.val = NULL;
                        ent->keys.len = 0;
-                       krb5_warnx(context, "ntPwdHash has invalid length: %d\n",val->length);
+                       krb5_warnx(context, "ntPwdHash has invalid length: %d\n",
+                                  (int)val->length);
                } else {
                        ret = krb5_data_alloc (&keyvalue, 16);
                        if (ret) {
@@ -444,23 +461,34 @@ out:
 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,                                         
                                            TALLOC_CTX *mem_ctx,
                                            krb5_const_principal principal,
-                                           enum hdb_ent_type ent_type,
-                                           const char *realm_dn,
+                                           enum hdb_ldb_ent_type ent_type,
+                                           const struct ldb_dn *realm_dn,
                                            struct ldb_message ***pmsg)
 {
        krb5_error_code ret;
        int count;
        char *filter = NULL;
        const char * const *princ_attrs = krb5_attrs;
-       char *p;
 
        char *princ_str;
        char *princ_str_talloc;
        char *short_princ;
+       char *short_princ_talloc;
 
-       struct ldb_message **msg;
+       char *realm_dn_str;
+
+       struct ldb_message **msg = NULL;
+
+       /* Structure assignment, so we don't mess with the source parameter */
+       struct Principal princ = *principal;
 
-       ret = krb5_unparse_name(context, principal, &princ_str);
+       /* Allow host/dns.name/realm@REALM, just convert into host/dns.name@REALM */
+       if (princ.name.name_string.len == 3
+           && strcasecmp_m(princ.name.name_string.val[2], princ.realm) == 0) { 
+               princ.name.name_string.len = 2;
+       }
+
+       ret = krb5_unparse_name(context, &princ, &princ_str);
 
        if (ret != 0) {
                krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
@@ -468,36 +496,40 @@ static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_con
                return ret;
        }
 
+       ret = krb5_unparse_name_norealm(context, &princ, &short_princ);
+
+       if (ret != 0) {
+               free(princ_str);
+               krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
+               krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
+               return ret;
+       }
+
        princ_str_talloc = talloc_strdup(mem_ctx, princ_str);
-       short_princ = talloc_strdup(mem_ctx, princ_str);
+       short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
        free(princ_str);
+       free(short_princ);
        if (!short_princ || !princ_str_talloc) {
                krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
                return ENOMEM;
        }
 
-       p = strchr(short_princ, '@');
-       if (p) {
-               p[0] = '\0';
-       }
-
-       
        switch (ent_type) {
-       case HDB_ENT_TYPE_KRBTGT:
+       case HDB_LDB_ENT_TYPE_KRBTGT:
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))", 
                                         KRB5_TGS_NAME);
                break;
-       case HDB_ENT_TYPE_CLIENT:
+       case HDB_LDB_ENT_TYPE_CLIENT:
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(userPrincipalName=%s)))", 
-                                        short_princ, princ_str_talloc);
+                                        short_princ_talloc, princ_str_talloc);
                break;
-       case HDB_ENT_TYPE_SERVER:
+       case HDB_LDB_ENT_TYPE_SERVER:
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(servicePrincipalName=%s)))", 
-                                        short_princ, short_princ);
+                                        short_princ_talloc, short_princ_talloc);
                break;
-       case HDB_ENT_TYPE_ENUM:
+       case HDB_LDB_ENT_TYPE_ANY:
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(|(samAccountName=%s)(servicePrincipalName=%s))(userPrincipalName=%s)))", 
-                                        short_princ, short_princ, princ_str_talloc);
+                                        short_princ_talloc, short_princ_talloc, princ_str_talloc);
                break;
        }
 
@@ -509,20 +541,23 @@ static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_con
        count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter, 
                           princ_attrs, &msg);
 
-       *pmsg = talloc_steal(mem_ctx, msg);
+       realm_dn_str = ldb_dn_linearize(mem_ctx, realm_dn);
+
        if (count < 1) {
                krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d", 
-                          realm_dn, filter, count);
+                          realm_dn_str, filter, count);
                krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d", 
-                                     realm_dn, filter, count);
+                                     realm_dn_str, filter, count);
                return HDB_ERR_NOENTRY;
        } else if (count > 1) {
+               talloc_free(msg);
                krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d", 
-                          realm_dn, filter, count);
+                          realm_dn_str, filter, count);
                krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d", 
-                                     realm_dn, filter, count);
+                                     realm_dn_str, filter, count);
                return HDB_ERR_NOENTRY;
        }
+       *pmsg = talloc_steal(mem_ctx, msg);
        return 0;
 }
 
@@ -532,22 +567,12 @@ static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context
                                        struct ldb_message ***pmsg)
 {
        int count;
-       const char *realm_dn;
+       struct ldb_dn *realm_dn;
+       const char *realm_dn_str;
        char *cross_ref_filter;
        struct ldb_message **cross_ref_msg;
        struct ldb_message **msg;
 
-       const char *cross_ref_attrs[] = {
-               "nCName", 
-               NULL
-       };
-
-       const char *realm_attrs[] = {
-               "dnsDomain", 
-               "maxPwdAge",
-               NULL
-       };
-
        cross_ref_filter = talloc_asprintf(mem_ctx, 
                                           "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
                                           realm, realm);
@@ -573,7 +598,8 @@ static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context
                return HDB_ERR_NOENTRY;
        }
 
-       realm_dn = ldb_msg_find_string(cross_ref_msg[0], "nCName", NULL);
+       realm_dn_str = ldb_msg_find_string(cross_ref_msg[0], "nCName", NULL);
+       realm_dn = ldb_dn_explode(mem_ctx, realm_dn_str);
 
        count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_BASE, "(objectClass=domain)",
                           realm_attrs, &msg);
@@ -584,126 +610,32 @@ static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context
        }
 
        if (count < 1) {
-               krb5_warnx(context, "ldb_search: dn: %s not found: %d", realm_dn, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", realm_dn, count);
+               krb5_warnx(context, "ldb_search: dn: %s not found: %d", realm_dn_str, count);
+               krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", realm_dn_str, count);
                return HDB_ERR_NOENTRY;
        } else if (count > 1) {
-               krb5_warnx(context, "ldb_search: dn: '%s' more than 1 entry: %d", realm_dn, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s more than 1 entry: %d", realm_dn, count);
+               krb5_warnx(context, "ldb_search: dn: '%s' more than 1 entry: %d", realm_dn_str, count);
+               krb5_set_error_string(context, "ldb_search: dn: %s more than 1 entry: %d", realm_dn_str, count);
                return HDB_ERR_NOENTRY;
        }
 
        return 0;
 }
 
-static krb5_error_code LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx, 
-                                           TALLOC_CTX *mem_ctx,
-                                           const char *realm_dn,
-                                           const char *alias_from,
-                                           char **alias_to)
-{
-       int i;
-       int count;
-       struct ldb_message **msg;
-       struct ldb_message_element *spnmappings;
-       char *service_dn = talloc_asprintf(mem_ctx, 
-                                          "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,%s", 
-                                          realm_dn);
-       const char *directory_attrs[] = {
-               "sPNMappings", 
-               NULL
-       };
-
-       count = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
-                          directory_attrs, &msg);
-       talloc_steal(mem_ctx, msg);
-
-       if (count < 1) {
-               krb5_warnx(context, "ldb_search: dn: %s not found: %d", service_dn, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", service_dn, count);
-               return HDB_ERR_NOENTRY;
-       } else if (count > 1) {
-               krb5_warnx(context, "ldb_search: dn: %s found %d times!", service_dn, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s found %d times!", service_dn, count);
-               return HDB_ERR_NOENTRY;
-       }
-       
-       spnmappings = ldb_msg_find_element(msg[0], "sPNMappings");
-       if (!spnmappings || spnmappings->num_values == 0) {
-               krb5_warnx(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn);
-               krb5_set_error_string(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn);
-       }
-
-       for (i = 0; i < spnmappings->num_values; i++) {
-               char *mapping, *p, *str;
-               mapping = talloc_strdup(mem_ctx, 
-                                       spnmappings->values[i].data);
-               if (!mapping) {
-                       krb5_warnx(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn);
-                       krb5_set_error_string(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn);
-                       return HDB_ERR_NOENTRY;
-               }
-               
-               /* C string manipulation sucks */
-               
-               p = strchr(mapping, '=');
-               if (!p) {
-                       krb5_warnx(context, "ldb_search: dn: %s sPNMapping malformed: %s", 
-                                  service_dn, mapping);
-                       krb5_set_error_string(context, "ldb_search: dn: %s sPNMapping malformed: %s", 
-                                             service_dn, mapping);
-               }
-               p[0] = '\0';
-               p++;
-               do {
-                       str = p;
-                       p = strchr(p, ',');
-                       if (p) {
-                               p[0] = '\0';
-                               p++;
-                       }
-                       if (strcasecmp(str, alias_from) == 0) {
-                               krb5_warnx(context, "LDB_lookup_spn_alias: got alias %s for service %s", 
-                                          mapping, alias_from);
-                               *alias_to = mapping;
-                               return 0;
-                       }
-               } while (p);
-       }
-       krb5_warnx(context, "LDB_lookup_spn_alias: no alias for service %s applicable", alias_from);
-       return HDB_ERR_NOENTRY;
-}
 
 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
 {
-       struct ldb_context *sam_db;
-
        if (db->hdb_master_key_set) {
                krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
                krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
                return HDB_ERR_NOENTRY;
        }               
 
-       /* in future, we could cache the connect here, but for now KISS */
-
-       sam_db = ldb_connect(db->hdb_name, 0, NULL);
-       if (sam_db == NULL) {
-               krb5_warnx(context, "LDB_open: hdb_name '%s' failed\n",db->hdb_name);
-               krb5_set_error_string(context, "ldb_connect(%s, 0, NULL) failed!", db->hdb_name);
-               return HDB_ERR_NOENTRY;
-       }
-
-       db->hdb_db = talloc_steal(db, sam_db);
-
-       krb5_warnx(context, "LDB_open: hdb_name '%s' ok\n",db->hdb_name);
-
        return 0;
 }
 
 static krb5_error_code LDB_close(krb5_context context, HDB *db)
 {
-       talloc_free(db->hdb_db);
-       db->hdb_db = NULL;
        return 0;
 }
 
@@ -729,113 +661,192 @@ static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
 {
        struct ldb_message **msg = NULL;
        struct ldb_message **realm_msg = NULL;
+       struct ldb_message **realm_fixed_msg = NULL;
+       enum hdb_ldb_ent_type ldb_ent_type;
        krb5_error_code ret;
 
        const char *realm;
-       const char *realm_dn;
-       TALLOC_CTX *mem_ctx = talloc_named(NULL, 0, "LDB_fetch context\n");
+       const struct ldb_dn *realm_dn;
+       TALLOC_CTX *mem_ctx = talloc_named(NULL, 0, "LDB_fetch context");
 
        if (!mem_ctx) {
                krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
                return ENOMEM;
        }
 
-       realm = krb5_principal_get_realm(context, principal);
-               
-       ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db, 
-                              mem_ctx, realm, &realm_msg);
-       if (ret != 0) {
-               krb5_warnx(context, "LDB_fetch: could not find realm\n");
-               talloc_free(mem_ctx);
-               return HDB_ERR_NOENTRY;
-       }
-
-       realm_dn = realm_msg[0]->dn;
-
-       /* Cludge, cludge cludge.  If the realm part of krbtgt/realm,
+               /* Cludge, cludge cludge.  If the realm part of krbtgt/realm,
         * is in our db, then direct the caller at our primary
         * krgtgt */
-       if (ent_type != HDB_ENT_TYPE_KRBTGT 
-           && principal->name.name_string.len == 2
-           && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)
-           && (LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
-                                mem_ctx, principal->name.name_string.val[1], NULL) == 0)) {
-               ent_type = HDB_ENT_TYPE_KRBTGT;
-       }
-
-       ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
-                                  mem_ctx, 
-                                  principal, ent_type, realm_dn, &msg);
 
-       if (ret != 0) {
-               char *alias_from = principal->name.name_string.val[0];
-               char *alias_to;
-               Principal alias_principal;
+       switch (ent_type) {
+       case HDB_ENT_TYPE_CLIENT:
+       {
+               int ldb_ret;
+               NTSTATUS nt_status;
+               struct ldb_dn *user_dn, *domain_dn;
+               char *principal_string;
+               ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
+
+               ret = krb5_unparse_name(context, principal, &principal_string);
                
-               /* Try again with a servicePrincipal alias */
-               if (ent_type != HDB_ENT_TYPE_SERVER && ent_type != HDB_ENT_TYPE_ENUM) {
+               if (ret != 0) {
                        talloc_free(mem_ctx);
                        return ret;
                }
-               if (principal->name.name_string.len < 2) {
-                       krb5_warnx(context, "LDB_fetch: could not find principal in DB, alias not applicable");
-                       krb5_set_error_string(context, "LDB_fetch: could not find principal in DB, alias not applicable");
+
+               nt_status = crack_user_principal_name((struct ldb_context *)db->hdb_db,
+                                                     mem_ctx, principal_string, 
+                                                     &user_dn, &domain_dn);
+               free(principal_string);
+
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        talloc_free(mem_ctx);
-                       return ret;
+                       return HDB_ERR_NOENTRY;
                }
 
-               /* Look for the list of aliases */
-               ret = LDB_lookup_spn_alias(context, 
-                                          (struct ldb_context *)db->hdb_db, mem_ctx, 
-                                          realm_dn, alias_from, 
-                                          &alias_to);
-               if (ret != 0) {
-                       talloc_free(mem_ctx);
-                       return ret;
+               ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
+                                         mem_ctx, user_dn, &msg, krb5_attrs);
+
+               if (ldb_ret != 1) {
+                       return HDB_ERR_NOENTRY;
                }
 
-               ret = copy_Principal(principal, &alias_principal);
-               if (ret != 0) {
-                       krb5_warnx(context, "LDB_fetch: could not copy principal");
-                       krb5_set_error_string(context, "LDB_fetch: could not copy principal");
-                       talloc_free(mem_ctx);
-                       return ret;
+               ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
+                                         mem_ctx, domain_dn, &realm_msg, realm_attrs);
+
+               if (ldb_ret != 1) {
+                       return HDB_ERR_NOENTRY;
                }
 
-               /* ooh, very nasty playing around in the Principal... */
-               free(alias_principal.name.name_string.val[0]);
-               alias_principal.name.name_string.val[0] = strdup(alias_to);
-               if (!alias_principal.name.name_string.val[0]) {
-                       krb5_warnx(context, "LDB_fetch: strdup() failed");
-                       krb5_set_error_string(context, "LDB_fetch: strdup() failed");
-                       ret = ENOMEM;
+               ret = LDB_message2entry(context, db, mem_ctx, 
+                                       principal, ldb_ent_type, 
+                                       realm_msg[0], msg[0], entry);
+               talloc_free(mem_ctx);
+               return ret;
+       }
+       case HDB_ENT_TYPE_SERVER:
+               if ((principal->name.name_string.len == 2)
+                       && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)) {
+                       /* krbtgt case.  Either us or a trusted realm */
+                       
+               } else if (principal->name.name_string.len >= 2) {
+                       /* 'normal server' case */
+                       int ldb_ret;
+                       NTSTATUS nt_status;
+                       struct ldb_dn *user_dn, *domain_dn;
+                       char *principal_string;
+                       ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
+                       
+                       ret = krb5_unparse_name_norealm(context, principal, &principal_string);
+                       
+                       if (ret != 0) {
+                               talloc_free(mem_ctx);
+                               return ret;
+                       }
+                       
+                       /* At this point we may find the host is known to be
+                        * in a different realm, so we should generate a
+                        * referral instead */
+                       nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
+                                                                mem_ctx, principal_string, 
+                                                                &user_dn, &domain_dn);
+                       free(principal_string);
+                       
+                       if (!NT_STATUS_IS_OK(nt_status)) {
+                               talloc_free(mem_ctx);
+                               return HDB_ERR_NOENTRY;
+                       }
+                       
+                       ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
+                                                 mem_ctx, user_dn, &msg, krb5_attrs);
+                       
+                       if (ldb_ret != 1) {
+                       return HDB_ERR_NOENTRY;
+                       }
+                       
+                       ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
+                                                 mem_ctx, domain_dn, &realm_msg, realm_attrs);
+                       
+                       if (ldb_ret != 1) {
+                               return HDB_ERR_NOENTRY;
+                       }
+                       
+                       ret = LDB_message2entry(context, db, mem_ctx, 
+                                               principal, ldb_ent_type, 
+                                               realm_msg[0], msg[0], entry);
                        talloc_free(mem_ctx);
                        return ret;
+                       
+               } else {
+                       /* server as client principal case, but we must not lookup userPrincipalNames */
                }
+       }
 
-               ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
-                                          mem_ctx, 
-                                          &alias_principal, ent_type, realm_dn, &msg);
-               if (ret != 0) {
-                       krb5_warnx(context, "LDB_fetch: could not find alias principal in DB");
-                       krb5_set_error_string(context, "LDB_fetch: could not find alias principal in DB");
-                       talloc_free(mem_ctx);
-                       return ret;
+       switch (ent_type) {
+       case HDB_ENT_TYPE_SERVER:
+               if (principal->name.name_string.len == 2
+                   && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)
+                   && (LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
+                                        mem_ctx, principal->name.name_string.val[1], &realm_fixed_msg) == 0)) {
+                       const char *dnsdomain = ldb_msg_find_string(realm_fixed_msg[0], "dnsDomain", NULL);
+                       char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
+                       if (!realm_fixed) {
+                               krb5_set_error_string(context, "strupper_talloc: out of memory");
+                               talloc_free(mem_ctx);
+                               return ENOMEM;
+                       }
+
+                       free(principal->name.name_string.val[1]);
+                       principal->name.name_string.val[1] = strdup(realm_fixed);
+                       talloc_free(realm_fixed);
+                       if (!principal->name.name_string.val[1]) {
+                               krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
+                               talloc_free(mem_ctx);
+                               return ENOMEM;
+                       }
+                       ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
+                       break;
+               } else {
+                       ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
+                       break;
                }
+       case HDB_ENT_TYPE_ANY:
+               ldb_ent_type = HDB_LDB_ENT_TYPE_ANY;
+               break;
+       default:
+               krb5_warnx(context, "LDB_fetch: invalid ent_type specified!");
+               talloc_free(mem_ctx);
+               return HDB_ERR_NOENTRY;
+       }
+
 
+       realm = krb5_principal_get_realm(context, principal);
+
+       ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db, 
+                              mem_ctx, realm, &realm_msg);
+       if (ret != 0) {
+               krb5_warnx(context, "LDB_fetch: could not find realm");
+               talloc_free(mem_ctx);
+               return HDB_ERR_NOENTRY;
        }
-       if (ret == 0) {
+
+       realm_dn = realm_msg[0]->dn;
+
+       ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
+                                  mem_ctx, 
+                                  principal, ldb_ent_type, realm_dn, &msg);
+
+       if (ret != 0) {
+               krb5_warnx(context, "LDB_fetch: could not find principal in DB");
+               krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
+               talloc_free(mem_ctx);
+               return ret;
+       } else {
                ret = LDB_message2entry(context, db, mem_ctx, 
-                                       principal, ent_type, 
+                                       principal, ldb_ent_type, 
                                        realm_msg[0], msg[0], entry);
                if (ret != 0) {
                        krb5_warnx(context, "LDB_fetch: message2entry failed\n");       
-#if 0 /* master key support removed */
-               } else {
-                       if (db->hdb_master_key_set && (!(flags & HDB_F_DECRYPT))) {
-                               ret = hdb_seal_keys(context, db, entry);
-                       }
-#endif
                }
        }
 
@@ -879,7 +890,7 @@ static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hd
 
        if (priv->index < priv->count) {
                ret = LDB_message2entry(context, db, mem_ctx, 
-                                       NULL, HDB_ENT_TYPE_ENUM
+                                       NULL, HDB_LDB_ENT_TYPE_ANY
                                        priv->realm_msgs[0], priv->msgs[priv->index++], entry);
        } else {
                ret = HDB_ERR_NOENTRY;
@@ -888,15 +899,6 @@ static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hd
        if (ret != 0) {
                talloc_free(priv);
                db->hdb_openp = NULL;
-#if 0 /* master key support removed */
-       } else {
-               if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
-                       ret = hdb_unseal_keys(context, db, entry);
-                       if (ret != 0) {
-                               hdb_free_entry(context,entry);
-                       }
-               }
-#endif
        } else {
                talloc_free(mem_ctx);
        }
@@ -910,7 +912,7 @@ static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flag
        struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
        struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
        char *realm;
-       char *realm_dn = NULL;
+       struct ldb_dn *realm_dn = NULL;
        struct ldb_message **msgs = NULL;
        struct ldb_message **realm_msgs = NULL;
        krb5_error_code ret;
@@ -953,7 +955,7 @@ static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flag
 
        if (ret != 0) {
                talloc_free(priv);
-               krb5_warnx(context, "LDB_fetch: could not find realm\n");
+               krb5_warnx(context, "LDB_firstkey: could not find realm\n");
                return HDB_ERR_NOENTRY;
        }
 
@@ -961,7 +963,7 @@ static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flag
 
        priv->realm_msgs = talloc_steal(priv, realm_msgs);
 
-       krb5_warnx(context, "LDB_lookup_principal: realm ok\n");
+       krb5_warnx(context, "LDB_firstkey: realm ok\n");
 
        priv->count = ldb_search(ldb_ctx, realm_dn,
                                 LDB_SCOPE_SUBTREE, "(objectClass=user)",
@@ -993,24 +995,16 @@ static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags
        return LDB_seq(context, db, flags, entry);
 }
 
-#if 0 /* no way to easily get context here, and we don't want to use master keys anyway */
-static int LDB_db_destructor(void *ptr) 
-{
-       HDB *db = talloc_get_type(ptr, HDB);
-       hdb_clear_master_key(context, db);
-       return 0;
-}
-#endif
-
 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
 {
        talloc_free(db);
        return 0;
 }
 
-krb5_error_code hdb_ldb_create(krb5_context context, struct HDB **db, const char *arg)
+krb5_error_code hdb_ldb_create(TALLOC_CTX *mem_ctx, 
+                              krb5_context context, struct HDB **db, const char *arg)
 {
-       *db = talloc(NULL, HDB);
+       *db = talloc(mem_ctx, HDB);
        if (!*db) {
                krb5_set_error_string(context, "malloc: out of memory");
                return ENOMEM;
@@ -1018,21 +1012,14 @@ krb5_error_code hdb_ldb_create(krb5_context context, struct HDB **db, const char
 
        (*db)->hdb_master_key_set = 0;
        (*db)->hdb_db = NULL;
-#if 0
-       talloc_set_destructor(*db, LDB_db_destructor);
-#endif
-       if (!arg || arg[0] == '\0') {
+
+       /* Setup the link to LDB */
+       (*db)->hdb_db = samdb_connect(db, system_session(db));
+       if ((*db)->hdb_db == NULL) {
+               krb5_warnx(context, "hdb_ldb_create: samdb_connect failed!");
+               krb5_set_error_string(context, "samdb_connect failed!");
                talloc_free(*db);
-               krb5_set_error_string(context, "hdb_ldb_create: no db name specified");
-               return EINVAL;
-       } else {
-               (*db)->hdb_name = talloc_strdup(*db, arg); 
-               if ((*db)->hdb_name == NULL) {
-                       krb5_set_error_string(context, "strdup: out of memory");
-                       talloc_free(*db);
-                       *db = NULL;
-                       return ENOMEM;
-               }
+               return HDB_ERR_NOENTRY;
        }
 
        (*db)->hdb_openp = 0;