r7804: added the samba specific ldif handlers into the tree, but don't enable
authorAndrew Tridgell <tridge@samba.org>
Tue, 21 Jun 2005 07:52:00 +0000 (07:52 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:37 +0000 (13:18 -0500)
them just yet. I have tested them, and they work fine, but enabling
them will break code in rpc_server/ and samdb, so we need to fix that
first

source/lib/ldb/common/ldb_ldif.c
source/lib/ldb/config.mk
source/lib/ldb/include/ldb.h
source/lib/ldb/samba/README [new file with mode: 0644]
source/lib/ldb/samba/ldif_handlers.c [new file with mode: 0644]
source/lib/ldb/tools/cmdline.c

index 88ef9fae45e305a27532007fc1abbaa006e21575..94109ce2245d25da341fc9cc446798dfead02dae 100644 (file)
 #include "ldb/include/ldb_private.h"
 #include <ctype.h>
 
+
+/*
+  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
 */
@@ -59,7 +83,7 @@ static ldb_ldif_handler_t ldb_ldif_read_fn(struct ldb_context *ldb, const char *
 {
        int i;
        for (i=0;i<ldb->ldif_num_handlers;i++) {
-               if (strcmp(attr, ldb->ldif_handlers[i].attr) == 0) {
+               if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
                        return ldb->ldif_handlers[i].read_fn;
                }
        }
@@ -73,7 +97,7 @@ static ldb_ldif_handler_t ldb_ldif_write_fn(struct ldb_context *ldb, const char
 {
        int i;
        for (i=0;i<ldb->ldif_num_handlers;i++) {
-               if (strcmp(attr, ldb->ldif_handlers[i].attr) == 0) {
+               if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
                        return ldb->ldif_handlers[i].write_fn;
                }
        }
index 9206ac108113d775a8d9eba78b7f31541ae76fb8..00568aeda8f8d2fa4d130e0c3ee35dd57800f9f2 100644 (file)
@@ -90,12 +90,20 @@ REQUIRED_SUBSYSTEMS = \
 # End LIBRARY LIBLDB
 ################################################
 
+################################################
+# Start SUBSYSTEM LDBSAMBA
+[SUBSYSTEM::LDBSAMBA]
+OBJ_FILES = \
+               lib/ldb/samba/ldif_handlers.o
+# End SUBSYSTEM LDBSAMBA
+################################################
+
 ################################################
 # Start SUBSYSTEM LIBLDB_CMDLINE
 [SUBSYSTEM::LIBLDB_CMDLINE]
 OBJ_FILES= \
                lib/ldb/tools/cmdline.o
-REQUIRED_SUBSYSTEMS = LIBLDB LIBCMDLINE LIBBASIC
+REQUIRED_SUBSYSTEMS = LIBLDB LIBCMDLINE LIBBASIC LDBSAMBA
 # End SUBSYSTEM LIBLDB_CMDLINE
 ################################################
 
index 48290beb9284316537005ffe9677e0f900ea09b6..31026763277b94af0e696568c93320dc1b85c64e 100644 (file)
@@ -285,6 +285,9 @@ struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s);
 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len);
 int ldb_base64_decode(char *s);
+int ldb_ldif_add_handlers(struct ldb_context *ldb, 
+                         const struct ldb_ldif_handler *handlers, 
+                         unsigned num_handlers);
 
 
 /* useful functions for ldb_message structure manipulation */
diff --git a/source/lib/ldb/samba/README b/source/lib/ldb/samba/README
new file mode 100644 (file)
index 0000000..3fa4715
--- /dev/null
@@ -0,0 +1,7 @@
+This directory contains Samba specific extensions to ldb. It also
+serves as example code on how to extend ldb for your own application.
+
+The main extension Samba uses is to provide ldif encode/decode
+routines for specific attributes, so users can get nice pretty
+printing of attributes in ldbedit, while the attributes are stored in
+the standard NDR format in the database.
diff --git a/source/lib/ldb/samba/ldif_handlers.c b/source/lib/ldb/samba/ldif_handlers.c
new file mode 100644 (file)
index 0000000..7252d08
--- /dev/null
@@ -0,0 +1,95 @@
+/* 
+   ldb database library - ldif handlers for Samba
+
+   Copyright (C) Andrew Tridgell  2005
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+   
+   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.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   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
+*/
+
+#include "includes.h"
+#include "ldb/include/ldb.h"
+#include "ldb/include/ldb_private.h"
+#include "librpc/gen_ndr/ndr_security.h"
+
+/*
+  convert a ldif formatted objectSid to a NDR formatted blob
+*/
+static int ldif_read_objectSid(struct ldb_context *ldb, const struct ldb_val *in,
+                              struct ldb_val *out)
+{
+       struct dom_sid *sid;
+       NTSTATUS status;
+       sid = dom_sid_parse_talloc(ldb, in->data);
+       if (sid == NULL) {
+               return -1;
+       }
+       status = ndr_push_struct_blob(out, ldb, sid, 
+                                     (ndr_push_flags_fn_t)ndr_push_dom_sid);
+       talloc_free(sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               return -1;
+       }
+       return 0;
+}
+
+/*
+  convert a NDR formatted blob to a ldif formatted objectSid
+*/
+static int ldif_write_objectSid(struct ldb_context *ldb, const struct ldb_val *in,
+                              struct ldb_val *out)
+{
+       struct dom_sid *sid;
+       NTSTATUS status;
+       sid = talloc(ldb, struct dom_sid);
+       if (sid == NULL) {
+               return -1;
+       }
+       status = ndr_pull_struct_blob(in, sid, sid, 
+                                     (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(sid);
+               return -1;
+       }
+       out->data = dom_sid_string(ldb, sid);
+       talloc_free(sid);
+       if (out->data == NULL) {
+               return -1;
+       }
+       out->length = strlen(out->data);
+       return 0;
+}
+
+
+static const struct ldb_ldif_handler samba_handlers[] = {
+       { "objectSid", ldif_read_objectSid, ldif_write_objectSid }
+};
+
+/*
+  register the samba ldif handlers
+*/
+int ldb_register_samba_handlers(struct ldb_context *ldb)
+{
+#if 0
+       /* we can't enable this until we fix the sam code to handle
+          non-string elements */
+       return ldb_ldif_add_handlers(ldb, samba_handlers, ARRAY_SIZE(samba_handlers));
+#else
+       return 0;
+#endif
+}
index 1f4a7544a5e1fc651e519394163390b175e5b042..31d3f2662a54adeea979d30e2db2dae680af56a3 100644 (file)
@@ -36,9 +36,9 @@
 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
                                        void (*usage)(void))
 {
-       struct ldb_cmdline options, *ret;
+       struct ldb_cmdline options, *ret=NULL;
        poptContext pc;
-       int num_options = 0;
+       int r, num_options = 0;
        char opt;
        struct poptOption popt_options[] = {
                POPT_AUTOHELP
@@ -65,6 +65,10 @@ struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const
 
 #ifdef _SAMBA_BUILD_
        ldbsearch_init_subsystems;
+       r = ldb_register_samba_handlers(ldb);
+       if (r != 0) {
+               goto failed;
+       }
 #endif
 
        ret = talloc_zero(ldb, struct ldb_cmdline);