r25446: Merge some changes I made on the way home from SFO:
[tprouty/samba.git] / source4 / torture / ldap / schema.c
index 068888bae9f183419bdb7c9d8d8856cbb30787fc..4da758cac465ae000e947ba4b438626abc3690a3 100644 (file)
@@ -6,7 +6,7 @@
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
    
 */
 
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
 #include "dsdb/samdb/samdb.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
 #include "dsdb/samdb/samdb.h"
-#include "dlinklist.h"
+#include "lib/util/dlinklist.h"
 
 #include "torture/torture.h"
 #include "torture/ldap/proto.h"
 
 
 #include "torture/torture.h"
 #include "torture/ldap/proto.h"
 
-struct dsdb_attribute {
-       struct dsdb_attribute *prev, *next;
-
-       const char *lDAPDisplayName;
-       const char *attributeID;
-       uint32_t attID;
-       struct GUID schemaIDGUID;
-
-       uint32_t searchFlags;
-       BOOL systemOnly;
-       uint32_t systemFlags;
-       BOOL isMemberOfPartialAttributeSet;
-
-       const char *attributeSyntax;
-       uint32_t oMSyntax;
-
-       BOOL isSingleValued;
-       uint32_t rangeLower;
-       uint32_t rangeUpper;
-
-       BOOL showInAdvancedViewOnly;
-       const char *adminDisplayName;
-       const char *adminDescription;
-};
-
-struct dsdb_objectClass {
-       struct dsdb_objectClass *prev, *next;
-
-       const char *subClassOf;
-
-       const char *governsID;
-       const char *rDNAttID;
-
-       BOOL showInAdvancedViewOnly;
-       const char *adminDisplayName;
-       const char *adminDescription;
-
-       uint32_t objectClassCategory;
-       const char *lDAPDisplayName;
-
-       struct GUID schemaIDGUID;
-
-       BOOL systemOnly;
-
-       const char **systemPossSuperiors;
-       const char **systemMayContain;
-
-       const char **possSuperiors;
-       const char **mayContain;
-
-       const char *defaultSecurityDescriptor;
-
-       uint32_t systemFlags;
-       BOOL defaultHidingValue;
-
-       const char *defaultObjectCategory;
-};
-
-struct dsdb_schema {
-       struct dsdb_attribute *attributes;
-       struct dsdb_objectClass *objectClasses;
-};
+#include "param/param.h"
 
 struct test_rootDSE {
        const char *defaultdn;
 
 struct test_rootDSE {
        const char *defaultdn;
@@ -119,23 +57,24 @@ static BOOL test_search_rootDSE(struct ldb_context *ldb, struct test_rootDSE *ro
 
        d_printf("Testing RootDSE Search\n");
 
 
        d_printf("Testing RootDSE Search\n");
 
-       ret = ldb_search(ldb, ldb_dn_new(ldb), LDB_SCOPE_BASE, 
+       ret = ldb_search(ldb, ldb_dn_new(ldb, ldb, NULL), LDB_SCOPE_BASE, 
                         NULL, NULL, &r);
        if (ret != LDB_SUCCESS) {
                return False;
        } else if (r->count != 1) {
                         NULL, NULL, &r);
        if (ret != LDB_SUCCESS) {
                return False;
        } else if (r->count != 1) {
+               talloc_free(r);
                return False;
        }
 
        msg = r->msgs[0];
 
                return False;
        }
 
        msg = r->msgs[0];
 
-       root->defaultdn = ldb_msg_find_string(msg, "defaultNamingContext", NULL);
+       root->defaultdn = ldb_msg_find_attr_as_string(msg, "defaultNamingContext", NULL);
        talloc_steal(ldb, root->defaultdn);
        talloc_steal(ldb, root->defaultdn);
-       root->rootdn    = ldb_msg_find_string(msg, "rootDomainNamingContext", NULL);
+       root->rootdn    = ldb_msg_find_attr_as_string(msg, "rootDomainNamingContext", NULL);
        talloc_steal(ldb, root->rootdn);
        talloc_steal(ldb, root->rootdn);
-       root->configdn  = ldb_msg_find_string(msg, "configurationNamingContext", NULL);
+       root->configdn  = ldb_msg_find_attr_as_string(msg, "configurationNamingContext", NULL);
        talloc_steal(ldb, root->configdn);
        talloc_steal(ldb, root->configdn);
-       root->schemadn  = ldb_msg_find_string(msg, "schemaNamingContext", NULL);
+       root->schemadn  = ldb_msg_find_attr_as_string(msg, "schemaNamingContext", NULL);
        talloc_steal(ldb, root->schemadn);
 
        talloc_free(r);
        talloc_steal(ldb, root->schemadn);
 
        talloc_free(r);
@@ -225,9 +164,9 @@ static BOOL test_create_schema_type(struct ldb_context *ldb, struct test_rootDSE
        ctrl[1] = NULL;
 
        req->operation = LDB_SEARCH;
        ctrl[1] = NULL;
 
        req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_explode(ldb, root->schemadn);
+       req->op.search.base = ldb_dn_new(req, ldb, root->schemadn);
        req->op.search.scope = LDB_SCOPE_SUBTREE;
        req->op.search.scope = LDB_SCOPE_SUBTREE;
-       req->op.search.tree = ldb_parse_tree(ldb, filter);
+       req->op.search.tree = ldb_parse_tree(req, filter);
        if (req->op.search.tree == NULL) return -1;
        req->op.search.attrs = NULL;
        req->controls = ctrl;
        if (req->op.search.tree == NULL) return -1;
        req->op.search.attrs = NULL;
        req->controls = ctrl;
@@ -261,114 +200,46 @@ again:
        return True;
 }
 
        return True;
 }
 
-#define GET_STRING(p, elem, strict) do { \
-       (p)->elem = samdb_result_string(msg, #elem, NULL);\
-       if (strict && (p)->elem == NULL) { \
-               d_printf("%s: %s == NULL\n", __location__, #elem); \
-               goto failed; \
-       } \
-       (void)talloc_steal(p, (p)->elem); \
-} while (0)
-
-#define GET_BOOL(p, elem, strict) do { \
-       const char *str; \
-       str = samdb_result_string(msg, #elem, NULL);\
-       if (str == NULL) { \
-               if (strict) { \
-                       d_printf("%s: %s == NULL\n", __location__, #elem); \
-                       goto failed; \
-               } else { \
-                       (p)->elem = False; \
-               } \
-       } else if (strcasecmp("TRUE", str) == 0) { \
-               (p)->elem = True; \
-       } else if (strcasecmp("FALSE", str) == 0) { \
-               (p)->elem = False; \
-       } else { \
-               d_printf("%s: %s == %s\n", __location__, #elem, str); \
-               goto failed; \
-       } \
-} while (0)
-
-#define GET_UINT32(p, elem) do { \
-       (p)->elem = samdb_result_uint(msg, #elem, 0);\
-} while (0)
-
-#define GET_GUID(p, elem) do { \
-       (p)->elem = samdb_result_guid(msg, #elem);\
-} while (0)
-
 static int test_add_attribute(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
 static int test_add_attribute(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
-       struct dsdb_attribute *attr;
+       struct dsdb_attribute *attr = NULL;
+       WERROR status;
 
        attr = talloc_zero(schema, struct dsdb_attribute);
 
        attr = talloc_zero(schema, struct dsdb_attribute);
+       if (!attr) {
+               goto failed;
+       }
 
 
-       GET_STRING(attr, lDAPDisplayName, True);
-       GET_STRING(attr, attributeID, True);
-       attr->attID = UINT32_MAX;
-       GET_GUID(attr, schemaIDGUID);
-
-       GET_UINT32(attr, searchFlags);
-       GET_BOOL(attr, systemOnly, False);
-       GET_UINT32(attr, systemFlags);
-       GET_BOOL(attr, isMemberOfPartialAttributeSet, False);
-
-       GET_STRING(attr, attributeSyntax, True);
-       GET_UINT32(attr, oMSyntax);
-
-       GET_BOOL(attr, isSingleValued, True);
-       GET_UINT32(attr, rangeLower);
-       GET_UINT32(attr, rangeUpper);
-
-       GET_BOOL(attr, showInAdvancedViewOnly, False);
-       GET_STRING(attr, adminDisplayName, True);
-       GET_STRING(attr, adminDescription, True);
+       status = dsdb_attribute_from_ldb(schema, msg, attr, attr);
+       if (!W_ERROR_IS_OK(status)) {
+               goto failed;
+       }
 
        DLIST_ADD_END(schema->attributes, attr, struct dsdb_attribute *);
        return LDB_SUCCESS;
 failed:
 
        DLIST_ADD_END(schema->attributes, attr, struct dsdb_attribute *);
        return LDB_SUCCESS;
 failed:
+       talloc_free(attr);
        return LDB_ERR_OTHER;
 }
 
 static int test_add_class(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
        return LDB_ERR_OTHER;
 }
 
 static int test_add_class(void *ptr, struct ldb_context *ldb, struct ldb_message *msg)
 {
        struct dsdb_schema *schema = talloc_get_type(ptr, struct dsdb_schema);
-       struct dsdb_objectClass *obj;
-
-       obj = talloc_zero(schema, struct dsdb_objectClass);
-
-       GET_STRING(obj, subClassOf, True);
-
-       GET_STRING(obj, governsID, True);
-       GET_STRING(obj, rDNAttID, True);
-
-       GET_BOOL(obj, showInAdvancedViewOnly, False);
-       GET_STRING(obj, adminDisplayName, True);
-       GET_STRING(obj, adminDescription, True);
-
-       GET_UINT32(obj, objectClassCategory);
-       GET_STRING(obj, lDAPDisplayName, True);
-
-       GET_GUID(obj, schemaIDGUID);
+       struct dsdb_class *obj;
+       WERROR status;
 
 
-       GET_BOOL(obj, systemOnly, False);
-
-       obj->systemPossSuperiors= NULL;
-       obj->systemMayContain   = NULL;
-
-       obj->possSuperiors      = NULL;
-       obj->mayContain         = NULL;
-
-       GET_STRING(obj, defaultSecurityDescriptor, False);
-
-       GET_UINT32(obj, systemFlags);
-       GET_BOOL(obj, defaultHidingValue, True);
+       obj = talloc_zero(schema, struct dsdb_class);
+       if (!obj) {
+               goto failed;
+       }
 
 
-       GET_STRING(obj, defaultObjectCategory, True);
+       status = dsdb_class_from_ldb(schema, msg, obj, obj);
+       if (!W_ERROR_IS_OK(status)) {
+               goto failed;
+       }
 
 
-       DLIST_ADD_END(schema->objectClasses, obj, struct dsdb_objectClass *);
+       DLIST_ADD_END(schema->classes, obj, struct dsdb_class *);
        return LDB_SUCCESS;
 failed:
        return LDB_ERR_OTHER;
        return LDB_SUCCESS;
 failed:
        return LDB_ERR_OTHER;
@@ -469,12 +340,23 @@ static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE
                "2.5.5.17"
        };
 
                "2.5.5.17"
        };
 
+       d_printf("Dumping attribute syntaxes\n");
+
        for (i=0; i < ARRAY_SIZE(syntaxes); i++) {
                for (a=schema->attributes; a; a = a->next) {
        for (i=0; i < ARRAY_SIZE(syntaxes); i++) {
                for (a=schema->attributes; a; a = a->next) {
-                       if (strcmp(syntaxes[i], a->attributeSyntax) != 0) continue;
-                       d_printf("attr[%4u]: %s %u '%s'\n", a_i++,
-                                a->attributeSyntax, a->oMSyntax,
-                                a->lDAPDisplayName);
+                       char *om_hex;
+
+                       if (strcmp(syntaxes[i], a->attributeSyntax_oid) != 0) continue;
+
+                       om_hex = data_blob_hex_string(ldb, &a->oMObjectClass);
+                       if (!om_hex) {
+                               return False;
+                       }
+
+                       d_printf("attr[%4u]: %s %u '%s' '%s'\n", a_i++,
+                                a->attributeSyntax_oid, a->oMSyntax,
+                                om_hex, a->lDAPDisplayName);
+                       talloc_free(om_hex);
                }
        }
 
                }
        }
 
@@ -484,19 +366,17 @@ static BOOL test_dump_sorted_syntax(struct ldb_context *ldb, struct test_rootDSE
 BOOL torture_ldap_schema(struct torture_context *torture)
 {
        struct ldb_context *ldb;
 BOOL torture_ldap_schema(struct torture_context *torture)
 {
        struct ldb_context *ldb;
-       TALLOC_CTX *mem_ctx;
        BOOL ret = True;
        BOOL ret = True;
-       const char *host = lp_parm_string(-1, "torture", "host");
+       const char *host = torture_setting_string(torture, "host", NULL);
        char *url;
        struct test_rootDSE rootDSE;
        struct dsdb_schema *schema = NULL;
 
        char *url;
        struct test_rootDSE rootDSE;
        struct dsdb_schema *schema = NULL;
 
-       mem_ctx = talloc_init("torture_ldap_basic");
        ZERO_STRUCT(rootDSE);
 
        ZERO_STRUCT(rootDSE);
 
-       url = talloc_asprintf(mem_ctx, "ldap://%s/", host);
+       url = talloc_asprintf(torture, "ldap://%s/", host);
 
 
-       ldb = ldb_wrap_connect(mem_ctx, url,
+       ldb = ldb_wrap_connect(torture, global_loadparm, url,
                               NULL,
                               cmdline_credentials,
                               0, NULL);
                               NULL,
                               cmdline_credentials,
                               0, NULL);
@@ -513,6 +393,5 @@ BOOL torture_ldap_schema(struct torture_context *torture)
        ret &= test_dump_sorted_syntax(ldb, &rootDSE, schema);
 
 failed:
        ret &= test_dump_sorted_syntax(ldb, &rootDSE, schema);
 
 failed:
-       talloc_free(mem_ctx);
        return ret;
 }
        return ret;
 }