4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 * Description: utility to delete records - modelled on ldapdelete
31 * Author: Andrew Tridgell
35 #include "ldb/include/includes.h"
36 #include "ldb/tools/cmdline.h"
38 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
41 const char *attrs[] = { NULL };
42 struct ldb_result *res;
44 ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
45 if (ret != LDB_SUCCESS) return -1;
47 for (i = 0; i < res->count; i++) {
48 if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
58 printf("Deleted %d records\n", total);
62 static void usage(void)
64 printf("Usage: ldbdel <options> <DN...>\n");
66 printf(" -r recursively delete the given subtree\n");
67 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
68 printf(" -o options pass options like modules to activate\n");
69 printf(" e.g: -o modules:timestamps\n");
71 printf("Deletes records from a ldb\n\n");
75 int main(int argc, const char **argv)
77 struct ldb_context *ldb;
79 struct ldb_cmdline *options;
83 ldb = ldb_init(NULL, NULL);
85 options = ldb_cmdline_process(ldb, argc, argv, usage);
87 if (options->argc < 1) {
92 for (i=0;i<options->argc;i++) {
95 dn = ldb_dn_explode(ldb, options->argv[i]);
97 printf("Invalid DN format\n");
100 if (options->recursive) {
101 ret = ldb_delete_recursive(ldb, dn);
103 ret = ldb_delete(ldb, dn);
105 printf("Deleted 1 record\n");
109 printf("delete of '%s' failed - %s\n",
110 ldb_dn_linearize(ldb, dn),