r22681: Fix standalone ldb build when parent directory name != ldb.
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_ldif.c
index 7ba6a00147194ae35d017efea926736b4e1fb3c8..6daeff7a90079254f5eb96019e4cec919574ee1a 100644 (file)
   see RFC2849 for the LDIF format definition
 */
 
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include "ldb/include/ldb_private.h"
-#include <ctype.h>
-#ifdef _SAMBA_BUILD_
-#include "system/filesys.h"
-#endif
+#include "ldb_includes.h"
+#include "system/locale.h"
 
 /*
   
@@ -54,8 +49,14 @@ static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
        int count, size, bytes;
        int ret;
        int f;
+       const char *fname = (const char *)value->data;
 
-       f = open((const char *)value->data, O_RDONLY);
+       if (strncmp(fname, "file://", 7) != 0) {
+               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       }
+       fname += 7;
+
+       f = open(fname, O_RDONLY);
        if (f == -1) {
                return -1;
        }
@@ -70,7 +71,7 @@ static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
                goto done;
        }
 
-       value->data = talloc_size(mem_ctx, statbuf.st_size + 1);
+       value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
        if (value->data == NULL) {
                ret = -1;
                goto done;
@@ -230,6 +231,8 @@ static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *priva
        return total;
 }
 
+#undef CHECK_RET
+
 /*
   encode as base64 to a file
 */
@@ -262,6 +265,9 @@ static const struct {
        {NULL, 0}
 };
 
+/* this macro is used to handle the return checking on fprintf_fn() */
+#define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
+
 /*
   write to ldif, using a caller supplied write method
 */
@@ -270,13 +276,16 @@ int ldb_ldif_write(struct ldb_context *ldb,
                   void *private_data,
                   const struct ldb_ldif *ldif)
 {
+       TALLOC_CTX *mem_ctx;
        unsigned int i, j;
        int total=0, ret;
        const struct ldb_message *msg;
 
+       mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
+
        msg = ldif->msg;
 
-       ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_linearize(msg->dn, msg->dn));
+       ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_get_linearized(msg->dn));
        CHECK_RET;
 
        if (ldif->changetype != LDB_CHANGETYPE_NONE) {
@@ -288,6 +297,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
                if (!ldb_changetypes[i].name) {
                        ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
                                  ldif->changetype);
+                       talloc_free(mem_ctx);
                        return -1;
                }
                ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
@@ -295,9 +305,9 @@ int ldb_ldif_write(struct ldb_context *ldb,
        }
 
        for (i=0;i<msg->num_elements;i++) {
-               const struct ldb_attrib_handler *h;
+               const struct ldb_schema_attribute *a;
 
-               h = ldb_attrib_handler(ldb, msg->elements[i].name);
+               a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
 
                if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
                        switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
@@ -318,7 +328,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
 
                for (j=0;j<msg->elements[i].num_values;j++) {
                        struct ldb_val v;
-                       ret = h->ldif_write_fn(ldb, ldb, &msg->elements[i].values[j], &v);
+                       ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
                        CHECK_RET;
                        if (ldb_should_b64_encode(&v)) {
                                ret = fprintf_fn(private_data, "%s:: ", 
@@ -503,40 +513,6 @@ void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
        talloc_free(ldif);
 }
 
-/*
-  add an empty element
-*/
-static int msg_add_empty(struct ldb_context *ldb,
-                        struct ldb_message *msg, const char *name, unsigned flags)
-{
-       struct ldb_message_element *el2, *el;
-
-       el2 = talloc_realloc(msg, msg->elements, 
-                              struct ldb_message_element, msg->num_elements+1);
-       if (!el2) {
-               errno = ENOMEM;
-               return -1;
-       }
-       
-       msg->elements = el2;
-
-       el = &msg->elements[msg->num_elements];
-       
-       el->name = talloc_strdup(msg->elements, name);
-       el->num_values = 0;
-       el->values = NULL;
-       el->flags = flags;
-
-       if (!el->name) {
-               errno = ENOMEM;
-               return -1;
-       }
-
-       msg->num_elements++;
-
-       return 0;
-}
-
 /*
  read from a LDIF source, creating a ldb_message
 */
@@ -567,14 +543,13 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
        msg->dn = NULL;
        msg->elements = NULL;
        msg->num_elements = 0;
-       msg->private_data = NULL;
 
        chunk = next_chunk(ldb, fgetc_fn, private_data);
        if (!chunk) {
                goto failed;
        }
+       talloc_steal(ldif, chunk);
 
-       msg->private_data = chunk;
        s = chunk;
 
        if (next_attr(ldif, &s, &attr, &value) != 0) {
@@ -588,16 +563,16 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                goto failed;
        }
 
-       msg->dn = ldb_dn_explode(msg, (char *)value.data);
+       msg->dn = ldb_dn_new(msg, ldb, (char *)value.data);
 
-       if (msg->dn == NULL) {
+       if ( ! ldb_dn_validate(msg->dn)) {
                ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n", 
                                  value.data);
                goto failed;
        }
 
        while (next_attr(ldif, &s, &attr, &value) == 0) {
-               const struct ldb_attrib_handler *h;             
+               const struct ldb_schema_attribute *a;
                struct ldb_message_element *el;
                int ret, empty = 0;
 
@@ -635,7 +610,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                }
 
                if (empty) {
-                       if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
+                       if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
                                goto failed;
                        }
                        continue;
@@ -643,7 +618,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                
                el = &msg->elements[msg->num_elements-1];
 
-               h = ldb_attrib_handler(ldb, attr);
+               a = ldb_schema_attribute_by_name(ldb, attr);
 
                if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
                    flags == el->flags) {
@@ -654,7 +629,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                        if (!el->values) {
                                goto failed;
                        }
-                       ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
+                       ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
                        if (ret != 0) {
                                goto failed;
                        }
@@ -683,7 +658,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                                goto failed;
                        }
                        el->num_values = 1;
-                       ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
+                       ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
                        if (ret != 0) {
                                goto failed;
                        }
@@ -712,7 +687,8 @@ struct ldif_read_file_state {
 
 static int fgetc_file(void *private_data)
 {
-       struct ldif_read_file_state *state = private_data;
+       struct ldif_read_file_state *state =
+               (struct ldif_read_file_state *)private_data;
        return fgetc(state->f);
 }
 
@@ -733,7 +709,8 @@ struct ldif_read_string_state {
 
 static int fgetc_string(void *private_data)
 {
-       struct ldif_read_string_state *state = private_data;
+       struct ldif_read_string_state *state =
+               (struct ldif_read_string_state *)private_data;
        if (state->s[0] != 0) {
                return *state->s++;
        }
@@ -762,7 +739,8 @@ static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBU
 
 static int fprintf_file(void *private_data, const char *fmt, ...)
 {
-       struct ldif_write_file_state *state = private_data;
+       struct ldif_write_file_state *state =
+               (struct ldif_write_file_state *)private_data;
        int ret;
        va_list ap;