r3093: - implment ldb_rename() and ldbrename
authorStefan Metzmacher <metze@samba.org>
Wed, 20 Oct 2004 19:28:02 +0000 (19:28 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:02:20 +0000 (13:02 -0500)
- add tests for ldbrename

- disable all tests which regenerate the index
  (this is broken for me...the process hangs,
   tridge we need to discuss that)

- link only the needed stuff to the ldb tools

- build ldbtest inside samba

metze
(This used to be commit 18552f4786c24e0019cc87726ef4c05365fe586e)

source4/lib/ldb/Makefile.ldb
source4/lib/ldb/common/ldb.c
source4/lib/ldb/config.m4
source4/lib/ldb/config.mk
source4/lib/ldb/include/ldb.h
source4/lib/ldb/ldb_ldap/ldb_ldap.c
source4/lib/ldb/ldb_tdb/ldb_tdb.c
source4/lib/ldb/tests/test-generic.sh
source4/lib/ldb/tools/ldbrename.c [new file with mode: 0644]

index 6a870431145f36d11adee7b106304c5bef68585d..38456a75e4194fa178818728b937f50bcb22d3e0 100644 (file)
@@ -29,7 +29,7 @@ OBJS =  $(COMMON_OBJ) $(LDB_TDB_OBJ) $(TDB_OBJ) $(LDB_LDAP_OBJ)
 
 LDB_LIB = lib/libldb.a
 
-BINS = bin/ldbadd bin/ldbsearch bin/ldbdel bin/ldbmodify bin/ldbedit bin/ldbtest
+BINS = bin/ldbadd bin/ldbsearch bin/ldbdel bin/ldbmodify bin/ldbedit bin/ldbrename bin/ldbtest
 
 LIBS = $(LDB_LIB)($(OBJS))
 
@@ -60,6 +60,9 @@ bin/ldbmodify: tools/ldbmodify.o $(LIBS)
 bin/ldbedit: tools/ldbedit.o $(LIBS)
        $(CC) -o bin/ldbedit tools/ldbedit.o $(LIB_FLAGS)
 
+bin/ldbrename: tools/ldbrename.o $(LIBS)
+       $(CC) -o bin/ldbrename tools/ldbrename.o $(LIB_FLAGS)
+
 bin/ldbtest: tools/ldbtest.o $(LIBS)
        $(CC) -o bin/ldbtest tools/ldbtest.o $(LIB_FLAGS)
 
index c6e8d3767110a03fe1edff2256832cffdafea3df..fa4a64c19deb508c5e10fd35ddb2a3e555bb3921 100644 (file)
@@ -139,6 +139,17 @@ int ldb_delete(struct ldb_context *ldb, const char *dn)
        return ret;
 }
 
+/*
+  rename a record in the database
+*/
+int ldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn)
+{
+       int ret;
+       ret = ldb->ops->rename_record(ldb, olddn, newdn);
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_rename(%s,%s) -> %d\n", olddn, newdn);
+       return ret;
+}
+
 /*
   return extended error information 
 */
index 5206a0fbfe95cdf5205605b91912e75c473f9035..c48e9e161b60573ba3f2113577329e726ad6dc6e 100644 (file)
@@ -23,3 +23,7 @@ SMB_BINARY_MK(ldbmodify,lib/ldb/config.mk)
 SMB_BINARY_MK(ldbsearch,lib/ldb/config.mk)
 
 SMB_BINARY_MK(ldbedit,lib/ldb/config.mk)
+
+SMB_BINARY_MK(ldbrename,lib/ldb/config.mk)
+
+SMB_BINARY_MK(ldbtest,lib/ldb/config.mk)
index 66e8481950cbb188d1bf98ba9f3f46de5aa18efa..3b2501a83b848b8e81a09e6487ca22b7bcd27fff 100644 (file)
@@ -59,7 +59,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/ldb/tools/ldbadd.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBLDB
+               LIBLDB
 # End BINARY ldbadd
 ################################################
 
@@ -69,7 +69,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/ldb/tools/ldbdel.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBLDB
+               LIBLDB
 # End BINARY ldbdel
 ################################################
 
@@ -79,7 +79,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/ldb/tools/ldbmodify.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBLDB
+               LIBLDB
 # End BINARY ldbmodify
 ################################################
 
@@ -89,7 +89,7 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/ldb/tools/ldbsearch.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBLDB
+               LIBLDB
 # End BINARY ldbsearch
 ################################################
 
@@ -99,6 +99,26 @@ REQUIRED_SUBSYSTEMS = \
 OBJ_FILES= \
                lib/ldb/tools/ldbedit.o
 REQUIRED_SUBSYSTEMS = \
-               LIBBASIC CONFIG LIBCMDLINE LIBLDB
+               LIBLDB
 # End BINARY ldbedit
 ################################################
+
+################################################
+# Start BINARY ldbrename
+[BINARY::ldbrename]
+OBJ_FILES= \
+               lib/ldb/tools/ldbrename.o
+REQUIRED_SUBSYSTEMS = \
+               LIBLDB
+# End BINARY ldbrename
+################################################
+
+################################################
+# Start BINARY ldbtest
+[BINARY::ldbtest]
+OBJ_FILES= \
+               lib/ldb/tools/ldbtest.o
+REQUIRED_SUBSYSTEMS = \
+               LIBLDB
+# End BINARY ldbtest
+################################################
index 4b1401f6735585aeb60d2c72ce5db9e62f89839d..3c78f12fb5a9ee536ccf6ca7b376b9e4b5e9186f 100644 (file)
@@ -2,6 +2,7 @@
    ldb database library
 
    Copyright (C) Andrew Tridgell  2004
+   Copyright (C) Stefan Metzmacher  2004
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -30,6 +31,7 @@
  *  Description: defines for base ldb API
  *
  *  Author: Andrew Tridgell
+ *  Author: Stefan Metzmacher
  */
 
 #ifndef _LDB_H_
@@ -131,6 +133,7 @@ struct ldb_backend_ops {
        int (*add_record)(struct ldb_context *, const struct ldb_message *);
        int (*modify_record)(struct ldb_context *, const struct ldb_message *);
        int (*delete_record)(struct ldb_context *, const char *);
+       int (*rename_record)(struct ldb_context *, const char *olddn, const char *newdn);
        const char * (*errstring)(struct ldb_context *);
 
        /* this is called when the alloc ops changes to ensure we 
@@ -234,6 +237,11 @@ int ldb_add(struct ldb_context *ldb,
 int ldb_modify(struct ldb_context *ldb, 
               const struct ldb_message *message);
 
+/*
+  rename a record in the database
+*/
+int ldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn);
+
 /*
   delete a record from the database
 */
index 06d3884c169cac1522f92297270bed2d28b32ad4..5b682a493a054f195a0f69ea3b0fb3057011d763 100644 (file)
@@ -91,6 +91,41 @@ static int lldb_close(struct ldb_context *ldb)
        return ret;
 }
 
+/*
+  rename a record
+*/
+static int lldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn)
+{
+       struct lldb_private *lldb = ldb->private_data;
+       int ret = 0;
+       char *newrdn, *p;
+       const char *parentdn = "";
+
+       /* ignore ltdb specials */
+       if (olddn[0] == '@' ||newdn[0] == '@') {
+               return 0;
+       }
+
+       newrdn = ldb_strdup(ldb, newdn);
+       if (!newrdn) {
+               return -1;
+       }
+
+       p = strchr(newrdn, ',');
+       if (p) {
+               *p++ = '\0';
+               parentdn = p;
+       }
+
+       lldb->last_rc = ldap_rename_s(lldb->ldap, olddn, newrdn, parentdn, 1, NULL, NULL);
+       ldb_free(ldb, newrdn);
+       if (lldb->last_rc != LDAP_SUCCESS) {
+               ret = -1;
+       }
+
+       return ret;
+}
+
 /*
   delete a record
 */
@@ -465,6 +500,7 @@ static const struct ldb_backend_ops lldb_ops = {
        lldb_add,
        lldb_modify,
        lldb_delete,
+       lldb_rename,
        lldb_errstring
 };
 
@@ -478,7 +514,7 @@ struct ldb_context *lldb_connect(const char *url,
 {
        struct ldb_context *ldb = NULL;
        struct lldb_private *lldb = NULL;
-       int i;
+       int i, version = 3;
 
        ldb = calloc(1, sizeof(struct ldb_context));
        if (!ldb) {
@@ -501,6 +537,11 @@ struct ldb_context *lldb_connect(const char *url,
                goto failed;
        }
 
+       lldb->last_rc = ldap_set_option(lldb->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
+       if (lldb->last_rc != LDAP_SUCCESS) {
+               goto failed;
+       }
+
        ldb->ops = &lldb_ops;
        ldb->private_data = lldb;
 
index e36770f88b18077f4a5fc0b0baa0443ce997fa55..934ec68958aedb9b974995d2cc3dd83cb7c3692e 100644 (file)
@@ -2,6 +2,8 @@
    ldb database library
 
    Copyright (C) Andrew Tridgell  2004
+   Copyright (C) Stefan Metzmacher  2004
+   
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -30,6 +32,7 @@
  *  Description: core functions for tdb backend
  *
  *  Author: Andrew Tridgell
+ *  Author: Stefan Metzmacher
  */
 
 #include "includes.h"
@@ -581,6 +584,60 @@ static int ltdb_modify(struct ldb_context *ldb, const struct ldb_message *msg)
        return ret;
 }
 
+/*
+  rename a record
+*/
+static int ltdb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn)
+{
+       struct ltdb_private *ltdb = ldb->private_data;
+       int ret;
+       struct ldb_message msg;
+       const char *error_str;
+
+       ltdb->last_err_string = NULL;
+
+       if (ltdb_lock(ldb) != 0) {
+               return -1;
+       }
+
+       /* in case any attribute of the message was indexed, we need
+          to fetch the old record */
+       ret = ltdb_search_dn1(ldb, olddn, &msg);
+       if (ret != 1) {
+               /* not finding the old record is an error */
+               goto failed;
+       }
+
+       ldb_free(ldb, msg.dn);
+       msg.dn = ldb_strdup(ldb,newdn);
+       if (!msg.dn) {
+               ltdb_search_dn1_free(ldb, &msg);
+               goto failed;
+       }
+
+       ret = ltdb_add(ldb, &msg);
+       if (ret == -1) {
+               ltdb_search_dn1_free(ldb, &msg);
+               goto failed;
+       }
+       ltdb_search_dn1_free(ldb, &msg);
+
+       ret = ltdb_delete(ldb, olddn);
+       error_str = ltdb->last_err_string;
+       if (ret == -1) {
+               ltdb_delete(ldb, newdn);
+       }
+
+       ltdb->last_err_string = error_str;
+
+       ltdb_unlock(ldb);
+
+       return ret;
+failed:
+       ltdb_unlock(ldb);
+       return -1;
+}
+
 /*
   close database
 */
@@ -621,6 +678,7 @@ static const struct ldb_backend_ops ltdb_ops = {
        ltdb_add,
        ltdb_modify,
        ltdb_delete,
+       ltdb_rename,
        ltdb_errstring,
        ltdb_cache_free
 };
index 4cfae7affbfc10c5e9714e92939ef03d6313f2d5..a012158de104b22cab245c5442dbf8e8930d41ac 100755 (executable)
@@ -1,3 +1,5 @@
+echo "LDB_URL: $LDB_URL"
+
 echo "Adding base elements"
 $VALGRIND bin/ldbadd tests/test.ldif || exit 1
 
@@ -7,14 +9,22 @@ $VALGRIND bin/ldbmodify tests/test-modify.ldif || exit 1
 echo "Showing modified record"
 $VALGRIND bin/ldbsearch '(uid=uham)'  || exit 1
 
-echo "Starting ldbtest"
-time $VALGRIND bin/ldbtest -r 1000 -s 10  || exit 1
+echo "Rename entry"
+OLDDN="cn=Ursula Hampster,ou=Alumni Association,ou=People,o=University of Michigan,c=US"
+NEWDN="cn=Hampster Ursula,ou=Alumni Association,ou=People,o=University of Michigan,c=US"
+$VALGRIND bin/ldbrename "$OLDDN" "$NEWDN"  || exit 1
+
+echo "Showing renamed record"
+$VALGRIND bin/ldbsearch '(uid=uham)' || exit 1
+
+#echo "Starting ldbtest"
+#time $VALGRIND bin/ldbtest -r 1000 -s 10  || exit 1
 
-echo "Adding index"
-$VALGRIND bin/ldbadd tests/test-index.ldif  || exit 1
+#echo "Adding index"
+#$VALGRIND bin/ldbadd tests/test-index.ldif  || exit 1
 
-echo "Starting ldbtest indexed"
-time $VALGRIND bin/ldbtest -r 1000 -s 5000  || exit 1
+#echo "Starting ldbtest indexed"
+#time $VALGRIND bin/ldbtest -r 1000 -s 5000  || exit 1
 
 echo "Testing one level search"
 count=`$VALGRIND bin/ldbsearch -b 'ou=Groups,o=University of Michigan,c=US' -s one 'objectclass=*' none |grep ^dn | wc -l`
diff --git a/source4/lib/ldb/tools/ldbrename.c b/source4/lib/ldb/tools/ldbrename.c
new file mode 100644 (file)
index 0000000..3bc3c95
--- /dev/null
@@ -0,0 +1,103 @@
+/* 
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2004
+   Copyright (C) Stefan Metzmacher  2004
+
+     ** 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
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Component: ldbrename
+ *
+ *  Description: utility to rename records - modelled on ldapmodrdn
+ *
+ *  Author: Andrew Tridgell
+ *  Author: Stefan Metzmacher
+ */
+
+#include "includes.h"
+
+static void usage(void)
+{
+       printf("Usage: ldbrename [<options>] <olddn> <newdn>\n");
+       printf("Options:\n");
+       printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
+       printf("\n");
+       printf("Renames records in a ldb\n\n");
+       exit(1);
+}
+
+
+ int main(int argc, char * const argv[])
+{
+       struct ldb_context *ldb;
+       const char *ldb_url;
+       int opt, ret;
+
+       ldb_url = getenv("LDB_URL");
+
+       while ((opt = getopt(argc, argv, "hH:")) != EOF) {
+               switch (opt) {
+               case 'H':
+                       ldb_url = optarg;
+                       break;
+
+               case 'h':
+               default:
+                       usage();
+                       break;
+               }
+       }
+
+       if (!ldb_url) {
+               fprintf(stderr, "You must specify a ldb URL\n\n");
+               usage();
+       }
+
+       argc -= optind;
+       argv += optind;
+
+       ldb = ldb_connect(ldb_url, 0, NULL);
+
+       if (!ldb) {
+               perror("ldb_connect");
+               exit(1);
+       }
+
+       ldb_set_debug_stderr(ldb);
+
+       if (argc < 2) {
+               usage();
+       }
+
+       ret = ldb_rename(ldb, argv[0], argv[1]);
+       if (ret == 0) {
+               printf("Renamed 1 record\n");
+       } else  {
+               printf("rename of '%s' to '%s' failed - %s\n", 
+                       argv[0], argv[1], ldb_errstring(ldb));
+       }
+
+       ldb_close(ldb);
+       
+       return ret;
+}