1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-03 13:47:25 +03:00

s4: make ldbadd/ldbmodify/ldbdelete really use the --controls switch

This commit is contained in:
Matthieu Patou 2009-12-22 20:44:19 +03:00 committed by Simo Sorce
parent 5aa0d97464
commit 3bd4f6792c
6 changed files with 230 additions and 9 deletions

View File

@ -1,3 +1,13 @@
################################################
# Start SUBSYSTEM LIBLDB_UTIL
[SUBSYSTEM::LIBLDB_UTIL]
CFLAGS = -I$(ldbsrcdir) -I$(ldbsrcdir)/include
PUBLIC_DEPENDENCIES = LIBLDB
# End SUBSYSTEM LIBLDB_UTIL
################################################
LIBLDB_UTIL_OBJ_FILES = $(ldbsrcdir)/tools/ldbutil.o
################################################
# Start SUBSYSTEM LIBLDB_CMDLINE
[SUBSYSTEM::LIBLDB_CMDLINE]
@ -14,6 +24,7 @@ LIBLDB_CMDLINE_OBJ_FILES = $(ldbsrcdir)/tools/cmdline.o
[BINARY::ldbadd]
INSTALLDIR = BINDIR
PRIVATE_DEPENDENCIES = \
LIBLDB_UTIL \
LIBLDB_CMDLINE LIBCLI_RESOLVE
# End BINARY ldbadd
################################################
@ -28,6 +39,7 @@ MANPAGES += $(ldbsrcdir)/man/ldbadd.1
[BINARY::ldbdel]
INSTALLDIR = BINDIR
PRIVATE_DEPENDENCIES = \
LIBLDB_UTIL \
LIBLDB_CMDLINE
# End BINARY ldbdel
################################################
@ -41,6 +53,7 @@ MANPAGES += $(ldbsrcdir)/man/ldbdel.1
[BINARY::ldbmodify]
INSTALLDIR = BINDIR
PRIVATE_DEPENDENCIES = \
LIBLDB_UTIL \
LIBLDB_CMDLINE
# End BINARY ldbmodify
################################################

View File

@ -33,6 +33,7 @@
#include "ldb.h"
#include "tools/cmdline.h"
#include "ldbutil.h"
static int failures;
static struct ldb_cmdline *options;
@ -53,6 +54,12 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
{
struct ldb_ldif *ldif;
int ret = LDB_SUCCESS;
struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
if (options->controls != NULL && req_ctrls== NULL) {
printf("parsing controls failed: %s\n", ldb_errstring(ldb));
return -1;
}
while ((ldif = ldb_ldif_read_file(ldb, f))) {
if (ldif->changetype != LDB_CHANGETYPE_ADD &&
@ -63,7 +70,7 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);
ret = ldb_add(ldb, ldif->msg);
ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
if (ret != LDB_SUCCESS) {
fprintf(stderr, "ERR: \"%s\" on DN %s\n",
ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));

View File

@ -33,6 +33,7 @@
#include "ldb.h"
#include "tools/cmdline.h"
#include "ldbutil.h"
static int dn_cmp(const void *p1, const void *p2)
{
@ -42,7 +43,7 @@ static int dn_cmp(const void *p1, const void *p2)
return ldb_dn_compare(msg1->dn, msg2->dn);
}
static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
{
int ret, i, total=0;
const char *attrs[] = { NULL };
@ -55,7 +56,7 @@ static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
qsort(res->msgs, res->count, sizeof(res->msgs[0]), dn_cmp);
for (i = 0; i < res->count; i++) {
if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == 0) {
total++;
} else {
printf("Failed to delete '%s' - %s\n",
@ -95,6 +96,11 @@ int main(int argc, const char **argv)
usage();
exit(1);
}
struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
if (options->controls != NULL && req_ctrls== NULL) {
printf("parsing controls failed: %s\n", ldb_errstring(ldb));
return -1;
}
for (i=0;i<options->argc;i++) {
struct ldb_dn *dn;
@ -105,9 +111,9 @@ int main(int argc, const char **argv)
exit(1);
}
if (options->recursive) {
ret = ldb_delete_recursive(ldb, dn);
ret = ldb_delete_recursive(ldb, dn,req_ctrls);
} else {
ret = ldb_delete(ldb, dn);
ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
if (ret == 0) {
printf("Deleted 1 record\n");
}

View File

@ -33,6 +33,7 @@
#include "ldb.h"
#include "tools/cmdline.h"
#include "ldbutil.h"
static int failures;
static struct ldb_cmdline *options;
@ -52,18 +53,23 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
{
struct ldb_ldif *ldif;
int ret = LDB_SUCCESS;
struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
if (options->controls != NULL && req_ctrls== NULL) {
printf("parsing controls failed: %s\n", ldb_errstring(ldb));
return -1;
}
while ((ldif = ldb_ldif_read_file(ldb, f))) {
switch (ldif->changetype) {
case LDB_CHANGETYPE_NONE:
case LDB_CHANGETYPE_ADD:
ret = ldb_add(ldb, ldif->msg);
ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
break;
case LDB_CHANGETYPE_DELETE:
ret = ldb_delete(ldb, ldif->msg->dn);
ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls);
break;
case LDB_CHANGETYPE_MODIFY:
ret = ldb_modify(ldb, ldif->msg);
ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls);
break;
}
if (ret != LDB_SUCCESS) {

View File

@ -0,0 +1,148 @@
/*
ldb database library utility
Copyright (C) Matthieu Patou 2009
** 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 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
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, see <http://www.gnu.org/licenses/>.
*/
/*
* Name: ldb
*
* Description: Common function used by ldb_add/ldb_modify/ldb_delete
*
* Author: Matthieu Patou
*/
#include "ldb.h"
/* autostarts a transacion if none active */
static int ldb_do_autotransaction(struct ldb_context *ldb,
struct ldb_request *req)
{
int ret;
ret = ldb_transaction_start(ldb);
if (ret != LDB_SUCCESS) {
return ret;
}
ret = ldb_request(ldb, req);
if (ret == LDB_SUCCESS) {
ret = ldb_wait(req->handle, LDB_WAIT_ALL);
}
if (ret == LDB_SUCCESS) {
return ldb_transaction_commit(ldb);
}
ldb_transaction_cancel(ldb);
if (ldb_errstring(ldb) == NULL) {
/* no error string was setup by the backend */
ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
}
return ret;
}
/*
Same as ldb_add but accept control
*/
int ldb_add_ctrl(struct ldb_context *ldb,
const struct ldb_message *message,
struct ldb_control **controls)
{
struct ldb_request *req;
int ret;
ret = ldb_msg_sanity_check(ldb, message);
if (ret != LDB_SUCCESS) {
return ret;
}
ret = ldb_build_add_req(&req, ldb, ldb,
message,
controls,
NULL,
ldb_op_default_callback,
NULL);
if (ret != LDB_SUCCESS) return ret;
/* do request and autostart a transaction */
ret = ldb_do_autotransaction(ldb, req);
talloc_free(req);
return ret;
}
/*
same as ldb_delete but accept control
*/
int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
struct ldb_control **controls)
{
struct ldb_request *req;
int ret;
ret = ldb_build_del_req(&req, ldb, ldb,
dn,
controls,
NULL,
ldb_op_default_callback,
NULL);
if (ret != LDB_SUCCESS) return ret;
/* do request and autostart a transaction */
ret = ldb_do_autotransaction(ldb, req);
talloc_free(req);
return ret;
}
/*
same as ldb_modify, but accepts controls
*/
int ldb_modify_ctrl(struct ldb_context *ldb,
const struct ldb_message *message,
struct ldb_control **controls)
{
struct ldb_request *req;
int ret;
ret = ldb_msg_sanity_check(ldb, message);
if (ret != LDB_SUCCESS) {
return ret;
}
ret = ldb_build_mod_req(&req, ldb, ldb,
message,
controls,
NULL,
ldb_op_default_callback,
NULL);
if (ret != LDB_SUCCESS) return ret;
/* do request and autostart a transaction */
ret = ldb_do_autotransaction(ldb, req);
talloc_free(req);
return ret;
}

View File

@ -0,0 +1,41 @@
/*
ldb database library utility header file
Copyright (C) Matthieu Patou 2009
** 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 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
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, see <http://www.gnu.org/licenses/>.
*/
/*
* Name: ldb
*
* Description: Common function used by ldb_add/ldb_modify/ldb_delete
*
* Author: Matthieu Patou
*/
#include "ldb.h"
int ldb_add_ctrl(struct ldb_context *ldb,
const struct ldb_message *message,
struct ldb_control **controls);
int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
struct ldb_control **controls);
int ldb_modify_ctrl(struct ldb_context *ldb,
const struct ldb_message *message,
struct ldb_control **controls);