s4:ldb: use try to print the extended dn in the ldif output
[kai/samba.git] / source4 / lib / ldb / common / ldb_ldif.c
index 50e9f5e5904d2aa03bffe7b4b35c6928587e6ae9..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/>.
 */
 
 /*
@@ -36,8 +35,7 @@
   see RFC2849 for the LDIF format definition
 */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_includes.h"
 #include "system/locale.h"
 
 /*
@@ -280,13 +278,15 @@ int ldb_ldif_write(struct ldb_context *ldb,
        TALLOC_CTX *mem_ctx;
        unsigned int i, j;
        int total=0, ret;
+       char *p;
        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));
+       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) {
@@ -306,9 +306,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) {
@@ -329,9 +329,11 @@ 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, mem_ctx, &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(&v)) {
                                ret = fprintf_fn(private_data, "%s:: ", 
                                                 msg->elements[i].name);
                                CHECK_RET;
@@ -544,7 +546,6 @@ 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) {
@@ -552,7 +553,6 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
        }
        talloc_steal(ldif, chunk);
 
-       msg->private_data = chunk;
        s = chunk;
 
        if (next_attr(ldif, &s, &attr, &value) != 0) {
@@ -566,16 +566,16 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
                goto failed;
        }
 
-       msg->dn = ldb_dn_new(msg, ldb, (char *)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", 
-                                 value.data);
+                         (char *)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;
 
@@ -621,7 +621,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) {
@@ -632,7 +632,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;
                        }
@@ -661,7 +661,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;
                        }