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 "tools/cmdline.h"
38 static int dn_cmp(const void *p1, const void *p2)
40 const struct ldb_message *msg1, *msg2;
41 msg1 = *(const struct ldb_message * const *)p1;
42 msg2 = *(const struct ldb_message * const *)p2;
43 return ldb_dn_compare(msg1->dn, msg2->dn);
46 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
49 const char *attrs[] = { NULL };
50 struct ldb_result *res;
52 ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
53 if (ret != LDB_SUCCESS) return -1;
55 /* sort the DNs, deepest first */
56 qsort(res->msgs, res->count, sizeof(res->msgs[0]), dn_cmp);
58 for (i = 0; i < res->count; i++) {
59 if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == 0) {
62 printf("Failed to delete '%s' - %s\n",
63 ldb_dn_get_linearized(res->msgs[i]->dn),
73 printf("Deleted %d records\n", total);
77 static void usage(void)
79 printf("Usage: ldbdel <options> <DN...>\n");
80 printf("Deletes records from a ldb\n\n");
81 ldb_cmdline_help("ldbdel", stdout);
85 int main(int argc, const char **argv)
87 struct ldb_context *ldb;
89 struct ldb_cmdline *options;
91 ldb = ldb_init(NULL, NULL);
93 options = ldb_cmdline_process(ldb, argc, argv, usage);
95 if (options->argc < 1) {
99 struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
100 if (options->controls != NULL && req_ctrls== NULL) {
101 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
105 for (i=0;i<options->argc;i++) {
108 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
109 if ( ! ldb_dn_validate(dn)) {
110 printf("Invalid DN format\n");
113 if (options->recursive) {
114 ret = ldb_delete_recursive(ldb, dn,req_ctrls);
116 ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
118 printf("Deleted 1 record\n");
122 printf("delete of '%s' failed - %s\n",
123 ldb_dn_get_linearized(dn),