mirror of
https://github.com/samba-team/samba.git
synced 2025-03-10 12:58:35 +03:00
r3093: - implment ldb_rename() and ldbrename
- 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)
This commit is contained in:
parent
8d48ca63db
commit
a9bd405497
@ -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)
|
||||
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
################################################
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
};
|
||||
|
@ -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 "Adding index"
|
||||
$VALGRIND bin/ldbadd tests/test-index.ldif || exit 1
|
||||
echo "Showing renamed record"
|
||||
$VALGRIND bin/ldbsearch '(uid=uham)' || exit 1
|
||||
|
||||
echo "Starting ldbtest indexed"
|
||||
time $VALGRIND bin/ldbtest -r 1000 -s 5000 || 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 "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`
|
||||
|
103
source4/lib/ldb/tools/ldbrename.c
Normal file
103
source4/lib/ldb/tools/ldbrename.c
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user