s4:ldb: use try to print the extended dn in the ldif output
[kai/samba.git] / source4 / lib / ldb / common / ldb_ldif.c
index ef782e90e3127cc7847a47b54591566c6202cfee..619c10e11e8ab689a85a446b2d8f532bae34fa00 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_includes.h"
+#include "system/locale.h"
 
+/*
+  
+*/
+static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
+{
+       struct stat statbuf;
+       char *buf;
+       int count, size, bytes;
+       int ret;
+       int f;
+       const char *fname = (const char *)value->data;
+
+       if (strncmp(fname, "file://", 7) != 0) {
+               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       }
+       fname += 7;
+
+       f = open(fname, O_RDONLY);
+       if (f == -1) {
+               return -1;
+       }
+
+       if (fstat(f, &statbuf) != 0) {
+               ret = -1;
+               goto done;
+       }
+
+       if (statbuf.st_size == 0) {
+               ret = -1;
+               goto done;
+       }
+
+       value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
+       if (value->data == NULL) {
+               ret = -1;
+               goto done;
+       }
+       value->data[statbuf.st_size] = 0;
+
+       count = 0;
+       size = statbuf.st_size;
+       buf = (char *)value->data;
+       while (count < statbuf.st_size) {
+               bytes = read(f, buf, size);
+               if (bytes == -1) {
+                       talloc_free(value->data);
+                       ret = -1;
+                       goto done;
+               }
+               count += bytes;
+               buf += bytes;
+               size -= bytes;
+       }
+
+       value->length = statbuf.st_size;
+       ret = statbuf.st_size;
+
+done:
+       close(f);
+       return ret;
+}
 
 /*
   this base64 decoder was taken from jitterbug (written by tridge).
   we might need to replace it with a new version
 */
-static int base64_decode(char *s)
+int ldb_base64_decode(char *s)
 {
        const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-       int bit_offset, byte_offset, idx, i, n;
-       unsigned char *d = (unsigned char *)s;
-       char *p;
+       int bit_offset=0, byte_offset, idx, i, n;
+       uint8_t *d = (uint8_t *)s;
+       char *p=NULL;
 
        n=i=0;
 
@@ -68,6 +129,9 @@ static int base64_decode(char *s)
                }
                s++; i++;
        }
+       if (bit_offset >= 3) {
+               n--;
+       }
 
        if (*s && !p) {
                /* the only termination allowed */
@@ -86,15 +150,15 @@ static int base64_decode(char *s)
   encode as base64
   caller frees
 */
-char *ldb_base64_encode(const char *buf, int len)
+char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
 {
        const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        int bit_offset, byte_offset, idx, i;
-       const unsigned char *d = (const unsigned char *)buf;
-       int bytes = (len*8 + 5)/6;
+       const uint8_t *d = (const uint8_t *)buf;
+       int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
        char *out;
 
-       out = malloc(bytes+2);
+       out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
        if (!out) return NULL;
 
        for (i=0;i<bytes;i++) {
@@ -111,7 +175,8 @@ char *ldb_base64_encode(const char *buf, int len)
                out[i] = b64[idx];
        }
 
-       out[i++] = '=';
+       for (;i<bytes+pad_bytes;i++)
+               out[i] = '=';
        out[i] = 0;
 
        return out;
@@ -122,10 +187,14 @@ char *ldb_base64_encode(const char *buf, int len)
 */
 int ldb_should_b64_encode(const struct ldb_val *val)
 {
-       int i;
-       unsigned char *p = val->data;
+       unsigned int i;
+       uint8_t *p = val->data;
+
+       if (val->length == 0) {
+               return 0;
+       }
 
-       if (val->length == 0 || p[0] == ' ' || p[0] == ':') {
+       if (p[0] == ' ' || p[0] == ':') {
                return 1;
        }
 
@@ -146,7 +215,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)
 {
-       int i;
+       unsigned int i;
        int total=0, ret;
 
        for (i=0;i<length;i++) {
@@ -161,13 +230,17 @@ static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *priva
        return total;
 }
 
+#undef CHECK_RET
+
 /*
   encode as base64 to a file
 */
-static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
+static int base64_encode_f(struct ldb_context *ldb,
+                          int (*fprintf_fn)(void *, const char *, ...), 
+                          void *private_data,
                           const char *buf, int len, int start_pos)
 {
-       char *b = ldb_base64_encode(buf, len);
+       char *b = ldb_base64_encode(ldb, buf, len);
        int ret;
 
        if (!b) {
@@ -176,7 +249,7 @@ static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *p
 
        ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
 
-       free(b);
+       talloc_free(b);
        return ret;
 }
 
@@ -191,20 +264,29 @@ 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
 */
-int ldif_write(int (*fprintf_fn)(void *, const char *, ...), 
-              void *private_data,
-              const struct ldb_ldif *ldif)
+int ldb_ldif_write(struct ldb_context *ldb,
+                  int (*fprintf_fn)(void *, const char *, ...), 
+                  void *private_data,
+                  const struct ldb_ldif *ldif)
 {
-       int i, j;
+       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) {
@@ -214,7 +296,9 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
                        }
                }
                if (!ldb_changetypes[i].name) {
-                       fprintf(stderr,"Invalid changetype\n");
+                       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);
@@ -222,6 +306,10 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
        }
 
        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:
@@ -240,13 +328,17 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
                }
 
                for (j=0;j<msg->elements[i].num_values;j++) {
-                       if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
+                       struct ldb_val 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(&v)) {
                                ret = fprintf_fn(private_data, "%s:: ", 
                                                 msg->elements[i].name);
                                CHECK_RET;
-                               ret = base64_encode_f(fprintf_fn, private_data, 
-                                                     msg->elements[i].values[j].data, 
-                                                     msg->elements[i].values[j].length,
+                               ret = base64_encode_f(ldb, fprintf_fn, private_data, 
+                                                     (char *)v.data, v.length,
                                                      strlen(msg->elements[i].name)+3);
                                CHECK_RET;
                                ret = fprintf_fn(private_data, "\n");
@@ -255,13 +347,15 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
                                ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
                                CHECK_RET;
                                ret = fold_string(fprintf_fn, private_data,
-                                                 msg->elements[i].values[j].data,
-                                                 msg->elements[i].values[j].length,
+                                                 (char *)v.data, v.length,
                                                  strlen(msg->elements[i].name)+2);
                                CHECK_RET;
                                ret = fprintf_fn(private_data, "\n");
                                CHECK_RET;
                        }
+                       if (v.data != msg->elements[i].values[j].data) {
+                               talloc_free(v.data);
+                       }
                }
                if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
                        fprintf_fn(private_data, "-\n");
@@ -282,7 +376,8 @@ int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
 
   caller frees
 */
-static char *next_chunk(int (*fgetc_fn)(void *), void *private_data)
+static char *next_chunk(struct ldb_context *ldb, 
+                       int (*fgetc_fn)(void *), void *private_data)
 {
        size_t alloc_size=0, chunk_size = 0;
        char *chunk = NULL;
@@ -293,9 +388,9 @@ static char *next_chunk(int (*fgetc_fn)(void *), void *private_data)
                if (chunk_size+1 >= alloc_size) {
                        char *c2;
                        alloc_size += 1024;
-                       c2 = realloc_p(chunk, char, alloc_size);
+                       c2 = talloc_realloc(ldb, chunk, char, alloc_size);
                        if (!c2) {
-                               free(chunk);
+                               talloc_free(chunk);
                                errno = ENOMEM;
                                return NULL;
                        }
@@ -343,10 +438,11 @@ static char *next_chunk(int (*fgetc_fn)(void *), void *private_data)
 
 
 /* simple ldif attribute parser */
-static int next_attr(char **s, const char **attr, struct ldb_val *value)
+static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
 {
        char *p;
        int base64_encoded = 0;
+       int binary_file = 0;
 
        if (strncmp(*s, "-\n", 2) == 0) {
                value->length = 0;
@@ -367,13 +463,18 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
                p++;
        }
 
+       if (*p == '<') {
+               binary_file = 1;
+               p++;
+       }
+
        *attr = *s;
 
-       while (isspace(*p)) {
+       while (*p == ' ' || *p == '\t') {
                p++;
        }
 
-       value->data = p;
+       value->data = (uint8_t *)p;
 
        p = strchr(p, '\n');
 
@@ -387,7 +488,7 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
        }
 
        if (base64_encoded) {
-               int len = base64_decode(value->data);
+               int len = ldb_base64_decode((char *)value->data);
                if (len == -1) {
                        /* it wasn't valid base64 data */
                        return -1;
@@ -395,6 +496,14 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
                value->length = len;
        }
 
+       if (binary_file) {
+               int len = ldb_read_data_file(mem_ctx, value);
+               if (len == -1) {
+                       /* an error occured hile trying to retrieve the file */
+                       return -1;
+               }
+       }
+
        return 0;
 }
 
@@ -402,55 +511,16 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value)
 /*
   free a message from a ldif_read
 */
-void ldif_read_free(struct ldb_ldif *ldif)
+void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
 {
-       struct ldb_message *msg = &ldif->msg;
-       int i;
-       for (i=0;i<msg->num_elements;i++) {
-               if (msg->elements[i].name) free(msg->elements[i].name);
-               if (msg->elements[i].values) free(msg->elements[i].values);
-       }
-       if (msg->elements) free(msg->elements);
-       if (msg->private_data) free(msg->private_data);
-       free(ldif);
-}
-
-/*
-  add an empty element
-*/
-static int msg_add_empty(struct ldb_message *msg, const char *name, unsigned flags)
-{
-       struct ldb_message_element *el2, *el;
-
-       el2 = realloc_p(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 = strdup(name);
-       el->num_values = 0;
-       el->values = NULL;
-       el->flags = flags;
-
-       if (!el->name) {
-               errno = ENOMEM;
-               return -1;
-       }
-
-       msg->num_elements++;
-
-       return 0;
+       talloc_free(ldif);
 }
 
 /*
  read from a LDIF source, creating a ldb_message
 */
-struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
+struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
+                              int (*fgetc_fn)(void *), void *private_data)
 {
        struct ldb_ldif *ldif;
        struct ldb_message *msg;
@@ -461,40 +531,53 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
 
        value.data = NULL;
 
-       ldif = malloc_p(struct ldb_ldif);
+       ldif = talloc(ldb, struct ldb_ldif);
        if (!ldif) return NULL;
 
+       ldif->msg = talloc(ldif, struct ldb_message);
+       if (ldif->msg == NULL) {
+               talloc_free(ldif);
+               return NULL;
+       }
+
        ldif->changetype = LDB_CHANGETYPE_NONE;
-       msg = &ldif->msg;
+       msg = ldif->msg;
 
        msg->dn = NULL;
        msg->elements = NULL;
        msg->num_elements = 0;
-       msg->private_data = NULL;
 
-       chunk = next_chunk(fgetc_fn, private_data);
+       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(&s, &attr, &value) != 0) {
+       if (next_attr(ldif, &s, &attr, &value) != 0) {
                goto failed;
        }
        
        /* first line must be a dn */
        if (ldb_attr_cmp(attr, "dn") != 0) {
-               fprintf(stderr, "First line must be a dn not '%s'\n", attr);
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n", 
+                         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'\n", 
+                         (char *)value.data);
+               goto failed;
+       }
 
-       while (next_attr(&s, &attr, &value) == 0) {
+       while (next_attr(ldif, &s, &attr, &value) == 0) {
+               const struct ldb_schema_attribute *a;
                struct ldb_message_element *el;
-               int empty = 0;
+               int ret, empty = 0;
 
                if (ldb_attr_cmp(attr, "changetype") == 0) {
                        int i;
@@ -505,8 +588,8 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
                                }
                        }
                        if (!ldb_changetypes[i].name) {
-                               fprintf(stderr,"Bad changetype '%s'\n",
-                                       (char *)value.data);
+                               ldb_debug(ldb, LDB_DEBUG_ERROR, 
+                                         "Error: Bad ldif changetype '%s'\n",(char *)value.data);
                        }
                        flags = 0;
                        continue;
@@ -530,7 +613,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
                }
 
                if (empty) {
-                       if (msg_add_empty(msg, (char *)value.data, flags) != 0) {
+                       if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
                                goto failed;
                        }
                        continue;
@@ -538,33 +621,53 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
                
                el = &msg->elements[msg->num_elements-1];
 
+               a = ldb_schema_attribute_by_name(ldb, attr);
+
                if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
                    flags == el->flags) {
                        /* its a continuation */
                        el->values = 
-                               realloc_p(el->values, struct ldb_val, el->num_values+1);
+                               talloc_realloc(msg->elements, el->values, 
+                                                struct ldb_val, el->num_values+1);
                        if (!el->values) {
                                goto failed;
                        }
-                       el->values[el->num_values] = value;
+                       ret = a->syntax->ldif_read_fn(ldb, ldif, &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);
+                               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 = realloc_p(msg->elements, 
-                                                 struct ldb_message_element, 
-                                                 msg->num_elements+1);
+                       msg->elements = talloc_realloc(ldif, msg->elements, 
+                                                        struct ldb_message_element, 
+                                                        msg->num_elements+1);
                        if (!msg->elements) {
                                goto failed;
                        }
                        el = &msg->elements[msg->num_elements];
                        el->flags = flags;
-                       el->name = strdup(attr);
-                       el->values = malloc_p(struct ldb_val);
+                       el->name = talloc_strdup(msg->elements, attr);
+                       el->values = talloc(msg->elements, struct ldb_val);
                        if (!el->values || !el->name) {
                                goto failed;
                        }
                        el->num_values = 1;
-                       el->values[0] = value;
+                       ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
+                       if (ret != 0) {
+                               goto failed;
+                       }
+                       if (value.data != el->values[0].data) {
+                               talloc_steal(el->values, el->values[0].data);
+                       }
                        msg->num_elements++;
                }
        }
@@ -572,7 +675,7 @@ struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
        return ldif;
 
 failed:
-       if (ldif) ldif_read_free(ldif);
+       talloc_free(ldif);
        return NULL;
 }
 
@@ -587,15 +690,16 @@ 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);
 }
 
-struct ldb_ldif *ldif_read_file(FILE *f)
+struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
 {
        struct ldif_read_file_state state;
        state.f = f;
-       return ldif_read(fgetc_file, &state);
+       return ldb_ldif_read(ldb, fgetc_file, &state);
 }
 
 
@@ -608,18 +712,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 *ldif_read_string(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 ldif_read(fgetc_string, &state);
+       struct ldb_ldif *ldif;
+       state.s = *s;
+       ldif = ldb_ldif_read(ldb, fgetc_string, &state);
+       *s = state.s;
+       return ldif;
 }
 
 
@@ -630,9 +738,12 @@ struct ldif_write_file_state {
        FILE *f;
 };
 
+static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
+
 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;
 
@@ -642,9 +753,9 @@ static int fprintf_file(void *private_data, const char *fmt, ...)
        return ret;
 }
 
-int ldif_write_file(FILE *f, const struct ldb_ldif *ldif)
+int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
 {
        struct ldif_write_file_state state;
        state.f = f;
-       return ldif_write(fprintf_file, &state, ldif);
+       return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
 }