r11382: Require number of required M4 macros
[samba.git] / source4 / kdc / hdb-ldb.c
index f138373cc3ff3dc3a6cecf4fd23495bc10e36af7..8a43a192515d1d831d26d72dd48919472075e3f2 100644 (file)
@@ -79,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;
@@ -116,6 +127,9 @@ static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum h
                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;
        }
        
@@ -144,8 +158,9 @@ static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum h
                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;
@@ -454,11 +469,11 @@ static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_con
        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;
 
        char *realm_dn_str;
 
@@ -469,7 +484,7 @@ static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_con
 
        /* Allow host/dns.name/realm@REALM, just convert into host/dns.name@REALM */
        if (princ.name.name_string.len == 3
-           && StrCaseCmp(princ.name.name_string.val[2], princ.realm) == 0) { 
+           && strcasecmp_m(princ.name.name_string.val[2], princ.realm) == 0) { 
                princ.name.name_string.len = 2;
        }
 
@@ -481,20 +496,24 @@ 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_LDB_ENT_TYPE_KRBTGT:
                filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))", 
@@ -502,15 +521,15 @@ static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_con
                break;
        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_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_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;
        }
 
@@ -554,17 +573,6 @@ static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context
        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);
@@ -614,84 +622,6 @@ static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context
        return 0;
 }
 
-static krb5_error_code LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx, 
-                                           TALLOC_CTX *mem_ctx,
-                                           const struct ldb_dn *realm_dn,
-                                           const char *alias_from,
-                                           char **alias_to)
-{
-       int i;
-       int count;
-       struct ldb_message **msg;
-       struct ldb_message_element *spnmappings;
-       struct ldb_dn *service_dn = ldb_dn_string_compose(mem_ctx, realm_dn,
-                                               "CN=Directory Service,CN=Windows NT"
-                                               ",CN=Services,CN=Configuration");
-       char *service_dn_str = ldb_dn_linearize(mem_ctx, service_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_str, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", service_dn_str, count);
-               return HDB_ERR_NOENTRY;
-       } else if (count > 1) {
-               krb5_warnx(context, "ldb_search: dn: %s found %d times!", service_dn_str, count);
-               krb5_set_error_string(context, "ldb_search: dn: %s found %d times!", service_dn_str, 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_str);
-               krb5_set_error_string(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn_str);
-               return HDB_ERR_NOENTRY;
-       }
-
-       for (i = 0; i < spnmappings->num_values; i++) {
-               char *mapping, *p, *str;
-               mapping = talloc_strdup(mem_ctx, 
-                                       (const char *)spnmappings->values[i].data);
-               if (!mapping) {
-                       krb5_warnx(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn_str);
-                       krb5_set_error_string(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn_str);
-                       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_str, mapping);
-                       krb5_set_error_string(context, "ldb_search: dn: %s sPNMapping malformed: %s", 
-                                             service_dn_str, mapping);
-                       return HDB_ERR_NOENTRY;
-               }
-               p[0] = '\0';
-               p++;
-               do {
-                       str = p;
-                       p = strchr(p, ',');
-                       if (p) {
-                               p[0] = '\0';
-                               p++;
-                       }
-                       if (strcasecmp(str, alias_from) == 0) {
-                               *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)
 {
@@ -744,22 +674,114 @@ static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
                return ENOMEM;
        }
 
-       realm = krb5_principal_get_realm(context, principal);
+               /* Cludge, cludge cludge.  If the realm part of krbtgt/realm,
+        * is in our db, then direct the caller at our primary
+        * krgtgt */
 
-       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");
+       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);
+               
+               if (ret != 0) {
+                       talloc_free(mem_ctx);
+                       return ret;
+               }
+
+               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 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 HDB_ERR_NOENTRY;
+               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 */
+               }
        }
 
-       realm_dn = realm_msg[0]->dn;
-
-       /* Cludge, cludge cludge.  If the realm part of krbtgt/realm,
-        * is in our db, then direct the caller at our primary
-        * krgtgt */
-       
        switch (ent_type) {
        case HDB_ENT_TYPE_SERVER:
                if (principal->name.name_string.len == 2
@@ -783,13 +805,11 @@ static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
                                return ENOMEM;
                        }
                        ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
+                       break;
                } else {
                        ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
+                       break;
                }
-               break;
-       case HDB_ENT_TYPE_CLIENT:
-               ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
-               break;
        case HDB_ENT_TYPE_ANY:
                ldb_ent_type = HDB_LDB_ENT_TYPE_ANY;
                break;
@@ -799,71 +819,29 @@ static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
                return HDB_ERR_NOENTRY;
        }
 
-       ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
-                                  mem_ctx, 
-                                  principal, ldb_ent_type, realm_dn, &msg);
 
-       if (ret != 0) {
-               char *alias_from = principal->name.name_string.val[0];
-               char *alias_to;
-               Principal alias_principal;
-               
-               /* Try again with a servicePrincipal alias */
-               if (ent_type != HDB_LDB_ENT_TYPE_SERVER && ent_type != HDB_LDB_ENT_TYPE_ANY) {
-                       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");
-                       talloc_free(mem_ctx);
-                       return ret;
-               }
-
-               /* 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;
-               }
-
-               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;
-               }
+       realm = krb5_principal_get_realm(context, principal);
 
-               /* 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;
-                       talloc_free(mem_ctx);
-                       free_Principal(&alias_principal);
-                       return ret;
-               }
+       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;
+       }
 
-               ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
-                                          mem_ctx, 
-                                          &alias_principal, ent_type, realm_dn, &msg);
-               free_Principal(&alias_principal);
+       realm_dn = realm_msg[0]->dn;
 
-               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;
-               }
+       ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db, 
+                                  mem_ctx, 
+                                  principal, ldb_ent_type, realm_dn, &msg);
 
-       }
-       if (ret == 0) {
+       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, ldb_ent_type, 
                                        realm_msg[0], msg[0], entry);
@@ -1036,7 +1014,7 @@ krb5_error_code hdb_ldb_create(TALLOC_CTX *mem_ctx,
        (*db)->hdb_db = NULL;
 
        /* Setup the link to LDB */
-       (*db)->hdb_db = samdb_connect(db);
+       (*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!");