summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-06-21 06:35:55 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:18:37 -0500
commiteb0a13025aa6d693c7e9fa213ef04fa58cf60e3e (patch)
tree5cebae639d75ba412cb4b5521274d148bc6b2525 /source4/lib/ldb/common
parentf29aa44d579a29af0d059ce680d7c6e28c884f2a (diff)
downloadsamba-eb0a13025aa6d693c7e9fa213ef04fa58cf60e3e.tar.gz
samba-eb0a13025aa6d693c7e9fa213ef04fa58cf60e3e.tar.bz2
samba-eb0a13025aa6d693c7e9fa213ef04fa58cf60e3e.zip
r7803: added support in ldb for callers to setup ldif read/write functions,
so that ldbedit, ldbsearch etc can display nice human readable ldif, while storing the data as binary blobs. This will be used for storing NDR encoded objectSid and similar attributes, while making the command line interface sane (This used to be commit 37e283089a846fc0608fef3981a3447300e33728)
Diffstat (limited to 'source4/lib/ldb/common')
-rw-r--r--source4/lib/ldb/common/ldb_ldif.c79
1 files changed, 69 insertions, 10 deletions
diff --git a/source4/lib/ldb/common/ldb_ldif.c b/source4/lib/ldb/common/ldb_ldif.c
index 9492aa3634..88ef9fae45 100644
--- a/source4/lib/ldb/common/ldb_ldif.c
+++ b/source4/lib/ldb/common/ldb_ldif.c
@@ -41,6 +41,44 @@
#include "ldb/include/ldb_private.h"
#include <ctype.h>
+/*
+ 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 (strcmp(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 (strcmp(attr, ldb->ldif_handlers[i].attr) == 0) {
+ return ldb->ldif_handlers[i].write_fn;
+ }
+ }
+ return ldb_ldif_default;
+}
/*
this base64 decoder was taken from jitterbug (written by tridge).
@@ -49,9 +87,9 @@
int ldb_base64_decode(char *s)
{
const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- int bit_offset, byte_offset, idx, i, n;
+ int bit_offset=0, byte_offset, idx, i, n;
uint8_t *d = (uint8_t *)s;
- char *p;
+ char *p=NULL;
n=i=0;
@@ -254,13 +292,17 @@ int ldb_ldif_write(struct ldb_context *ldb,
}
for (j=0;j<msg->elements[i].num_values;j++) {
- if (ldb_should_b64_encode(&msg->elements[i].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 = fprintf_fn(private_data, "%s:: ",
msg->elements[i].name);
CHECK_RET;
ret = base64_encode_f(ldb, fprintf_fn, private_data,
- msg->elements[i].values[j].data,
- msg->elements[i].values[j].length,
+ v.data, v.length,
strlen(msg->elements[i].name)+3);
CHECK_RET;
ret = fprintf_fn(private_data, "\n");
@@ -269,13 +311,15 @@ 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,
- msg->elements[i].values[j].data,
- msg->elements[i].values[j].length,
+ 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");
@@ -510,8 +554,9 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
msg->dn = value.data;
while (next_attr(&s, &attr, &value) == 0) {
+ ldb_ldif_handler_t read_fn;
struct ldb_message_element *el;
- int empty = 0;
+ int ret, empty = 0;
if (ldb_attr_cmp(attr, "changetype") == 0) {
int i;
@@ -555,6 +600,8 @@ 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);
+
if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
flags == el->flags) {
/* its a continuation */
@@ -564,7 +611,13 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
if (!el->values) {
goto failed;
}
- el->values[el->num_values] = value;
+ ret = read_fn(ldb, &value, &el->values[el->num_values]);
+ if (ret != 0) {
+ 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 */
@@ -582,7 +635,13 @@ struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
goto failed;
}
el->num_values = 1;
- el->values[0] = value;
+ ret = read_fn(ldb, &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++;
}
}