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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Description: utility to delete records - modelled on ldapdelete
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
40 #include "system/filesys.h"
43 static int ldb_delete_recursive(struct ldb_context *ldb, const char *dn)
46 const char *attrs[] = { "dn", NULL };
47 struct ldb_message **res;
49 ret = ldb_search(ldb, dn, LDB_SCOPE_SUBTREE, "dn=*", attrs, &res);
50 if (ret <= 0) return -1;
53 if (ldb_delete(ldb, res[i]->dn) == 0) {
58 ldb_search_free(ldb, res);
63 printf("Deleted %d records\n", total);
67 static void usage(void)
69 printf("Usage: ldbdel <options> <DN...>\n");
71 printf(" -r recursively delete the given subtree\n");
72 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
73 printf(" -o options pass options like modules to activate\n");
74 printf(" e.g: -o modules:timestamps\n");
76 printf("Deletes records from a ldb\n\n");
80 int main(int argc, char * const argv[])
82 struct ldb_context *ldb;
83 const char **options = NULL;
89 ldb_url = getenv("LDB_URL");
92 while ((opt = getopt(argc, argv, "hH:ro:")) != EOF) {
103 options = ldb_options_parse(options, &ldbopts, optarg);
114 fprintf(stderr, "You must specify a ldb URL\n\n");
126 ldb = ldb_connect(ldb_url, 0, options);
128 perror("ldb_connect");
132 ldb_set_debug_stderr(ldb);
134 for (i=0;i<argc;i++) {
136 ret = ldb_delete_recursive(ldb, argv[i]);
138 ret = ldb_delete(ldb, argv[i]);
140 printf("Deleted 1 record\n");
144 printf("delete of '%s' failed - %s\n",
145 argv[i], ldb_errstring(ldb));