s4-ldif: Fix memory leek in ldb_ldif_write()
[ira/wip.git] / source4 / lib / ldb / common / ldb_ldif.c
index f3bc5c6207fe1c3221c6036dd1f29488b2aea724..888761e67bdf7fbf5c5bf72bfb7fa23e626013eb 100644 (file)
@@ -10,7 +10,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,8 +18,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
   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
-
-/*
-  add to the list of ldif handlers for this ldb context
-*/
-int ldb_ldif_add_handlers(struct ldb_context *ldb, 
-                         const struct ldb_ldif_handler *handlers, 
-                         unsigned num_handlers)
-{
-       struct ldb_ldif_handler *h;
-       h = talloc_realloc(ldb, ldb->ldif_handlers,
-                          struct ldb_ldif_handler,
-                          ldb->ldif_num_handlers + num_handlers);
-       if (h == NULL) {
-               ldb_oom(ldb);
-               return -1;
-       }
-       ldb->ldif_handlers = h;
-       memcpy(h + ldb->ldif_num_handlers, 
-              handlers, sizeof(*h) * num_handlers);
-       ldb->ldif_num_handlers += num_handlers;
-       return 0;
-}
-                         
-
-/*
-  default function for ldif read/write
-*/
-static int ldb_ldif_default(struct ldb_context *ldb, const struct ldb_val *in, 
-                           struct ldb_val *out)
-{
-       *out = *in;
-       return 0;
-}
-
-
-/*
-  return a function for reading an ldif encoded attributes into a ldb_val
-*/
-static ldb_ldif_handler_t ldb_ldif_read_fn(struct ldb_context *ldb, const char *attr)
-{
-       int i;
-       for (i=0;i<ldb->ldif_num_handlers;i++) {
-               if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
-                       return ldb->ldif_handlers[i].read_fn;
-               }
-       }
-       return ldb_ldif_default;
-}
-
-/*
-  return a function for writing an ldif encoded attribute from a ldb_val
-*/
-static ldb_ldif_handler_t ldb_ldif_write_fn(struct ldb_context *ldb, const char *attr)
-{
-       int i;
-       for (i=0;i<ldb->ldif_num_handlers;i++) {
-               if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
-                       return ldb->ldif_handlers[i].write_fn;
-               }
-       }
-       return ldb_ldif_default;
-}
+#include "ldb_private.h"
+#include "system/locale.h"
 
 /*
   
@@ -116,8 +48,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;
        }
@@ -132,7 +70,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;
@@ -141,7 +79,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) {
@@ -217,10 +155,10 @@ char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
        const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        int bit_offset, byte_offset, idx, i;
        const uint8_t *d = (const uint8_t *)buf;
-       int bytes = (len*8 + 5)/6;
+       int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
        char *out;
 
-       out = talloc_array(mem_ctx, char, bytes+2);
+       out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
        if (!out) return NULL;
 
        for (i=0;i<bytes;i++) {
@@ -237,7 +175,8 @@ char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
                out[i] = b64[idx];
        }
 
-       out[i++] = '=';
+       for (;i<bytes+pad_bytes;i++)
+               out[i] = '=';
        out[i] = 0;
 
        return out;
@@ -246,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;
        }
@@ -291,6 +234,8 @@ static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *priva
        return total;
 }
 
+#undef CHECK_RET
+
 /*
   encode as base64 to a file
 */
@@ -323,6 +268,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
 */
@@ -331,13 +279,18 @@ 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;
+       char *p;
        const struct ldb_message *msg;
 
-       msg = ldif->msg;
+       mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
 
-       ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
+       msg = ldif->msg;
+       p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
+       ret = fprintf_fn(private_data, "dn: %s\n", p);
+       talloc_free(p);
        CHECK_RET;
 
        if (ldif->changetype != LDB_CHANGETYPE_NONE) {
@@ -347,8 +300,9 @@ 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;
                }
                ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
@@ -356,6 +310,10 @@ int ldb_ldif_write(struct ldb_context *ldb,
        }
 
        for (i=0;i<msg->num_elements;i++) {
+               const struct ldb_schema_attribute *a;
+
+               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) {
                        case LDB_FLAG_MOD_ADD:
@@ -374,17 +332,17 @@ int ldb_ldif_write(struct ldb_context *ldb,
                }
 
                for (j=0;j<msg->elements[i].num_values;j++) {
-                       ldb_ldif_handler_t write_fn = ldb_ldif_write_fn(ldb, 
-                                                                     msg->elements[i].name);
                        struct ldb_val v;
-                       ret = write_fn(ldb, &msg->elements[i].values[j], &v);
-                       CHECK_RET;
-                       if (ldb_should_b64_encode(&v)) {
+                       ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
+                       if (ret != LDB_SUCCESS) {
+                               v = msg->elements[i].values[j];
+                       }
+                       if (ret != LDB_SUCCESS || ldb_should_b64_encode(ldb, &v)) {
                                ret = fprintf_fn(private_data, "%s:: ", 
                                                 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");
@@ -392,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,
-                                                 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;
@@ -410,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;
 }
 
@@ -520,7 +485,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');
 
@@ -534,7 +499,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;
@@ -562,40 +527,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
 */
@@ -626,14 +557,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) {
@@ -642,15 +572,21 @@ 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;
        }
 
-       msg->dn = value.data;
+       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'",
+                         (char *)value.data);
+               goto failed;
+       }
 
        while (next_attr(ldif, &s, &attr, &value) == 0) {
-               ldb_ldif_handler_t read_fn;
+               const struct ldb_schema_attribute *a;
                struct ldb_message_element *el;
                int ret, empty = 0;
 
@@ -663,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;
@@ -688,7 +624,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;
@@ -696,7 +632,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                
                el = &msg->elements[msg->num_elements-1];
 
-               read_fn = ldb_ldif_read_fn(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) {
@@ -707,17 +643,22 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                        if (!el->values) {
                                goto failed;
                        }
-                       ret = read_fn(ldb, &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'", el->name);
+                               goto failed;
+                       }
                        if (value.data != el->values[el->num_values].data) {
                                talloc_steal(el->values, el->values[el->num_values].data);
                        }
                        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) {
@@ -731,7 +672,7 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                                goto failed;
                        }
                        el->num_values = 1;
-                       ret = read_fn(ldb, &value, &el->values[0]);
+                       ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
                        if (ret != 0) {
                                goto failed;
                        }
@@ -760,7 +701,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);
 }
 
@@ -781,18 +723,22 @@ 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++;
        }
        return EOF;
 }
 
-struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
+struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
 {
        struct ldif_read_string_state state;
-       state.s = s;
-       return ldb_ldif_read(ldb, fgetc_string, &state);
+       struct ldb_ldif *ldif;
+       state.s = *s;
+       ldif = ldb_ldif_read(ldb, fgetc_string, &state);
+       *s = state.s;
+       return ldif;
 }
 
 
@@ -807,7 +753,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;
 
@@ -823,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);
+}