r19188: merge from samba3:
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_ldif.c
index 38866d703132af4b404dc53694650fcf56926048..0c31f25cc793b5cf1e0be0d637f1a13c536d4e64 100644 (file)
 */
 
 #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/include/includes.h"
+#include "system/locale.h"
 
 /*
   
@@ -54,8 +50,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(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 +72,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;
@@ -79,7 +81,7 @@ static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
 
        count = 0;
        size = statbuf.st_size;
-       buf = value->data;
+       buf = (char *)value->data;
        while (count < statbuf.st_size) {
                bytes = read(f, buf, size);
                if (bytes == -1) {
@@ -325,7 +327,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
                                                 msg->elements[i].name);
                                CHECK_RET;
                                ret = base64_encode_f(ldb, fprintf_fn, private_data, 
-                                                     v.data, v.length,
+                                                     (char *)v.data, v.length,
                                                      strlen(msg->elements[i].name)+3);
                                CHECK_RET;
                                ret = fprintf_fn(private_data, "\n");
@@ -334,7 +336,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
                                ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
                                CHECK_RET;
                                ret = fold_string(fprintf_fn, private_data,
-                                                 v.data, v.length,
+                                                 (char *)v.data, v.length,
                                                  strlen(msg->elements[i].name)+2);
                                CHECK_RET;
                                ret = fprintf_fn(private_data, "\n");
@@ -461,7 +463,7 @@ static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val
                p++;
        }
 
-       value->data = p;
+       value->data = (uint8_t *)p;
 
        p = strchr(p, '\n');
 
@@ -475,7 +477,7 @@ static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val
        }
 
        if (base64_encoded) {
-               int len = ldb_base64_decode(value->data);
+               int len = ldb_base64_decode((char *)value->data);
                if (len == -1) {
                        /* it wasn't valid base64 data */
                        return -1;
@@ -503,40 +505,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
 */
@@ -588,7 +556,13 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                goto failed;
        }
 
-       msg->dn = ldb_dn_explode(msg, value.data);
+       msg->dn = ldb_dn_explode(msg, (char *)value.data);
+
+       if (msg->dn == NULL) {
+               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;             
@@ -629,7 +603,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) != 0) {
                                goto failed;
                        }
                        continue;
@@ -652,6 +626,11 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                        if (ret != 0) {
                                goto failed;
                        }
+                       if (value.length == 0) {
+                               ldb_debug(ldb, LDB_DEBUG_ERROR,
+                                         "Error: Attribute value cannot be empty for attribute '%s'\n", el->name);
+                               goto failed;
+                       }
                        if (value.data != el->values[el->num_values].data) {
                                talloc_steal(el->values, el->values[el->num_values].data);
                        }
@@ -701,7 +680,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);
 }
 
@@ -722,7 +702,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++;
        }
@@ -751,7 +732,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;