s4-ldb: use TALLOC_CTX type instead of 'void'
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_ldif.c
index 400fb352ff4aba05ef06d976f4034b81cf8e9a10..2628d4d5dae39fa0ef5e60a008c7c4b019289630 100644 (file)
@@ -41,7 +41,7 @@
 /*
   
 */
-static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
+static int ldb_read_data_file(TALLOC_CTX *mem_ctx, struct ldb_val *value)
 {
        struct stat statbuf;
        char *buf;
@@ -150,7 +150,7 @@ int ldb_base64_decode(char *s)
   encode as base64
   caller frees
 */
-char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
+char *ldb_base64_encode(TALLOC_CTX *mem_ctx, const char *buf, int len)
 {
        const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        int bit_offset, byte_offset, idx, i;
@@ -185,11 +185,15 @@ char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
 /*
   see if a buffer should be base64 encoded
 */
-int ldb_should_b64_encode(const struct ldb_val *val)
+int ldb_should_b64_encode(struct ldb_context *ldb, const struct ldb_val *val)
 {
        unsigned int i;
        uint8_t *p = val->data;
 
+       if (ldb->flags & LDB_FLG_SHOW_BINARY) {
+               return 0;
+       }
+
        if (val->length == 0) {
                return 0;
        }
@@ -215,7 +219,7 @@ int ldb_should_b64_encode(const struct ldb_val *val)
 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
                        const char *buf, size_t length, int start_pos)
 {
-       unsigned int i;
+       size_t i;
        int total=0, ret;
 
        for (i=0;i<length;i++) {
@@ -296,7 +300,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",
+                       ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
                                  ldif->changetype);
                        talloc_free(mem_ctx);
                        return -1;
@@ -333,7 +337,7 @@ int ldb_ldif_write(struct ldb_context *ldb,
                        if (ret != LDB_SUCCESS) {
                                v = msg->elements[i].values[j];
                        }
-                       if (ret != LDB_SUCCESS || ldb_should_b64_encode(&v)) {
+                       if (ret != LDB_SUCCESS || ldb_should_b64_encode(ldb, &v)) {
                                ret = fprintf_fn(private_data, "%s:: ", 
                                                 msg->elements[i].name);
                                CHECK_RET;
@@ -346,9 +350,14 @@ int ldb_ldif_write(struct ldb_context *ldb,
                        } else {
                                ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
                                CHECK_RET;
-                               ret = fold_string(fprintf_fn, private_data,
-                                                 (char *)v.data, v.length,
-                                                 strlen(msg->elements[i].name)+2);
+                               if (ldb->flags & LDB_FLG_SHOW_BINARY) {
+                                       ret = fprintf_fn(private_data, "%*.*s", 
+                                                        v.length, v.length, (char *)v.data);
+                               } else {
+                                       ret = fold_string(fprintf_fn, private_data,
+                                                         (char *)v.data, v.length,
+                                                         strlen(msg->elements[i].name)+2);
+                               }
                                CHECK_RET;
                                ret = fprintf_fn(private_data, "\n");
                                CHECK_RET;
@@ -364,6 +373,8 @@ int ldb_ldif_write(struct ldb_context *ldb,
        ret = fprintf_fn(private_data,"\n");
        CHECK_RET;
 
+       talloc_free(mem_ctx);
+
        return total;
 }
 
@@ -438,7 +449,7 @@ static char *next_chunk(struct ldb_context *ldb,
 
 
 /* simple ldif attribute parser */
-static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
+static int next_attr(TALLOC_CTX *mem_ctx, char **s, const char **attr, struct ldb_val *value)
 {
        char *p;
        int base64_encoded = 0;
@@ -499,7 +510,7 @@ static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val
        if (binary_file) {
                int len = ldb_read_data_file(mem_ctx, value);
                if (len == -1) {
-                       /* an error occured hile trying to retrieve the file */
+                       /* an error occurred while trying to retrieve the file */
                        return -1;
                }
        }
@@ -561,7 +572,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
        
        /* first line must be a dn */
        if (ldb_attr_cmp(attr, "dn") != 0) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n", 
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
                          attr);
                goto failed;
        }
@@ -569,7 +580,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
        msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
 
        if ( ! ldb_dn_validate(msg->dn)) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n", 
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'",
                          (char *)value.data);
                goto failed;
        }
@@ -588,8 +599,8 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                                }
                        }
                        if (!ldb_changetypes[i].name) {
-                               ldb_debug(ldb, LDB_DEBUG_ERROR, 
-                                         "Error: Bad ldif changetype '%s'\n",(char *)value.data);
+                               ldb_debug(ldb, LDB_DEBUG_ERROR,
+                                         "Error: Bad ldif changetype '%s'",(char *)value.data);
                        }
                        flags = 0;
                        continue;
@@ -632,13 +643,13 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                        if (!el->values) {
                                goto failed;
                        }
-                       ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
+                       ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
                        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);
+                                         "Error: Attribute value cannot be empty for attribute '%s'", el->name);
                                goto failed;
                        }
                        if (value.data != el->values[el->num_values].data) {
@@ -647,7 +658,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                        el->num_values++;
                } else {
                        /* its a new attribute */
-                       msg->elements = talloc_realloc(ldif, msg->elements, 
+                       msg->elements = talloc_realloc(msg, msg->elements, 
                                                         struct ldb_message_element, 
                                                         msg->num_elements+1);
                        if (!msg->elements) {
@@ -661,7 +672,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                                goto failed;
                        }
                        el->num_values = 1;
-                       ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
+                       ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
                        if (ret != 0) {
                                goto failed;
                        }
@@ -759,3 +770,59 @@ int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif
        state.f = f;
        return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
 }
+
+/*
+  wrapper around ldif_write() for a string
+*/
+struct ldif_write_string_state {
+       char *string;
+};
+
+static int ldif_printf_string(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
+
+static int ldif_printf_string(void *private_data, const char *fmt, ...)
+{
+       struct ldif_write_string_state *state =
+               (struct ldif_write_string_state *)private_data;
+       va_list ap;
+       size_t oldlen = talloc_get_size(state->string);
+       va_start(ap, fmt);
+       
+       state->string = talloc_vasprintf_append(state->string, fmt, ap);
+       va_end(ap);
+       if (!state->string) {
+               return -1;
+       }
+
+       return talloc_get_size(state->string) - oldlen;
+}
+
+char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
+                           const struct ldb_ldif *ldif)
+{
+       struct ldif_write_string_state state;
+       state.string = talloc_strdup(mem_ctx, "");
+       if (!state.string) {
+               return NULL;
+       }
+       if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
+               return NULL;
+       }
+       return state.string;
+}
+
+/*
+  convenient function to turn a ldb_message into a string. Useful for
+  debugging
+ */
+char *ldb_ldif_message_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
+                             enum ldb_changetype changetype,
+                             const struct ldb_message *msg)
+{
+       struct ldb_ldif ldif;
+
+       ldif.changetype = changetype;
+       ldif.msg = discard_const_p(struct ldb_message, msg);
+
+       return ldb_ldif_write_string(ldb, mem_ctx, &ldif);
+}