r15933: remove the last sync call to ldb_request
[jelmer/samba4-debian.git] / source / ldap_server / ldap_parse.c
index b28242783e8a13c32a94610b2d3940ed943c18dd..d411b26c1a58738892087c9230d42b0860007f41 100644 (file)
@@ -21,7 +21,7 @@
 #include "includes.h"
 #include "ldap_parse.h"
 
-char char_from_hex(char a, char b) {
+static char char_from_hex(char a, char b) {
        char m, l;
 
        if ('0' <= a  && a <= '9') {
@@ -47,7 +47,7 @@ char char_from_hex(char a, char b) {
        return ((m << 4) + l);
 }
 
-char *parse_slash(char *p, char *end) {
+static char *parse_slash(char *p, char *end) {
        switch (*(p + 1)) {
        case ',':
        case '=':
@@ -68,26 +68,111 @@ char *parse_slash(char *p, char *end) {
        }
 }
 
+#define LDAP_PARSE_DN_INVALID(x) do {\
+       if (x) { \
+               dn->comp_num = -1; \
+               return dn; \
+       } \
+} while(0)
 
-struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
+#if 0
+static void ldap_parse_attributetypedescription(struct ldap_schema *schema, DATA_BLOB *data)
+{
+       char *desc;
+
+       desc = talloc_array(schema, char, data->lenght + 1);
+       memcpy(desc, data->data, data->lenght);
+       desc[data->lenght] = '\0';
+
+}
+
+static void ldap_parse_objectclassdescription(struct ldap_schema *schema, DATA_BLOB *data)
+{
+       char *desc;
+
+       desc = talloc_array(schema, char, data->lenght + 1);
+       memcpy(desc, data->data, data->lenght);
+       desc[data->lenght] = '\0';
+
+}
+
+static struct ldap_schema *ldap_get_schema(void *mem_ctx, struct ldap_schema *schema, struct ldb_context *ldb)
+{
+       NTSTATUS status;
+       struct ldap_schema *local_schema;
+       struct ldb_message **res;
+       const char *errstr;
+       const char *schema_dn = "cn=schema";
+       const char *attr_filter = "attributeTypeDescription=*";
+       const char *class_filter = "objectClassDescription=*";
+       const char *attrs = "attributeTypeDescription";
+       const char *classes = "objectClassDescription";
+       enum ldb_scope scope = LDAP_SCOPE_SUBTREE;
+       int count, i, j, k;
+
+       local_schema = schema;
+       if (local_schema == NULL) {
+               local_schema = talloc(mem_ctx, struct ldap_schema);
+               ALLOC_CHECK(local_schema);
+       }
+
+       count = ldb_search(ldb, schema_dn, scope, attr_filter, attrs, &res);
+
+       for (i = 0; i < count; i++) {
+               if (res[i]->num_elements == 0) {
+                       goto attr_done;
+               }
+               for (j = 0; j < res[i]->num_elements; j++) {
+                       for (k = 0; res[i]->elements[j].num_values; k++) {
+                               ldap_parse_attributetypedescription(local_schema, &(res[i]->elements[j].values[k]));
+                       }
+               }
+attr_done:
+       }
+
+       count = ldb_search(ldb, schema_dn, scope, class_filter, classes, &res);
+
+       for (i = 0; i < count; i++) {
+               if (res[i]->num_elements == 0) {
+                       goto class_done;
+               }
+               for (j = 0; j < res[i]->num_elements; j++) {
+                       for (k = 0; res[i]->elements[j].num_values; k++) {
+                               ldap_parse_objectclassdescription(local_schema, &(res[i]->elements[j].values[k]));
+                       }
+               }
+class_done:
+       }
+
+       return local_schema;
+}
+#endif
+
+struct ldap_dn *ldap_parse_dn(void *mem_ctx, const char *orig_dn)
 {
        struct ldap_dn *dn;
        struct dn_component *component;
        struct dn_attribute *attribute;
        char *p, *start, *separator, *src, *dest, *dn_copy, *dn_end;
-       int i, size;
+       int i, size, orig_len;
 
-       dn = talloc_p(mem_ctx, struct ldap_dn);
+       dn = talloc(mem_ctx, struct ldap_dn);
        dn->comp_num = 0;
-       dn->components = talloc_array_p(dn, struct dn_component *, 1);
-       component = talloc_p(dn, struct dn_component);
+       dn->components = talloc_array(dn, struct dn_component *, 1);
+       component = talloc(dn, struct dn_component);
        component->attr_num = 0;
 
+       orig_len = strlen(orig_dn);
+       if (orig_len == 0) {
+               dn->dn = talloc_strdup(dn, orig_dn);
+               return dn;
+       }
+
        dn_copy = p = talloc_strdup(mem_ctx, orig_dn);
-       dn_end = dn_copy + strlen(orig_dn) + 1;
+       dn_end = dn_copy + orig_len + 1;
        do {
-               component->attributes = talloc_array_p(component, struct dn_attribute *, 1);
-               attribute = talloc_p(component, struct dn_attribute);
+               component->attributes = talloc_array(component, struct dn_attribute *, 1);
+               attribute = talloc(component, struct dn_attribute);
 
                /* skip "spaces" */
                while (*p == ' ' || *p == '\n') {
@@ -113,6 +198,7 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
                        }
 
                        /* save key name */
+                       LDAP_PARSE_DN_INVALID((p - start) < 1);
                        attribute->name = talloc_strndup(attribute, start, p - start);
                        DEBUG(10, ("attribute name: [%s]\n", attribute->name));
 
@@ -157,6 +243,7 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
                        }
 
                        /* save the value */
+                       LDAP_PARSE_DN_INVALID((p - start) < 1);
                        attribute->value = talloc_strndup(attribute, start, p - start);
                        DEBUG(10, ("attribute value: [%s]\n", attribute->value));
 
@@ -168,10 +255,10 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
                        component->attr_num++;
 
                        if (*separator == '+') { /* expect other attributes in this component */
-                               component->attributes = talloc_realloc_p(component, component->attributes, struct dn_attribute *, component->attr_num + 1);
+                               component->attributes = talloc_realloc(component, component->attributes, struct dn_attribute *, component->attr_num + 1);
 
                                /* allocate new attribute structure */
-                               attribute = talloc_p(component, struct dn_attribute);
+                               attribute = talloc(component, struct dn_attribute);
 
                                /* skip spaces past the separator */
                                p = separator + strspn(p, " \n");
@@ -185,7 +272,7 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
                }
 
                /* rebuild the normlaized component and put it here */
-               component->component = dest = talloc(component, size);
+               component->component = dest = talloc_size(component, size);
                for (i = 0; i < component->attr_num; i++) {
                        if (i != 0) {
                                *dest = '+';
@@ -203,8 +290,8 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
                dn->comp_num++;
 
                if (*separator == ',' || *separator == ';') {
-                       dn->components = talloc_realloc_p(dn, dn->components, struct dn_component *, dn->comp_num + 1);
-                       component = talloc_p(dn, struct dn_component);
+                       dn->components = talloc_realloc(dn, dn->components, struct dn_component *, dn->comp_num + 1);
+                       component = talloc(dn, struct dn_component);
                        component->attr_num = 0;
                }
                p = separator + 1;
@@ -216,7 +303,7 @@ struct ldap_dn *ldap_parse_dn(TALLOC_CTX *mem_ctx, const char *orig_dn)
        }
 
        /* rebuild the normlaized dn and put it here */
-       dn->dn = dest = talloc(dn, size);
+       dn->dn = dest = talloc_size(dn, size);
        for (i = 0; i < dn->comp_num; i++) {
                if (i != 0) {
                        *dest = ',';