5740f22503afecb1bab536e5947a9b26729afa27
[ira/wip.git] / source4 / lib / ldb / tools / ldbdel.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbdel
28  *
29  *  Description: utility to delete records - modelled on ldapdelete
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb.h"
35 #include "tools/cmdline.h"
36
37 static int dn_cmp(const void *p1, const void *p2)
38 {
39         const struct ldb_message *msg1, *msg2;
40         msg1 = *(const struct ldb_message * const *)p1;
41         msg2 = *(const struct ldb_message * const *)p2;
42         return ldb_dn_compare(msg1->dn, msg2->dn);
43 }
44
45 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
46 {
47         int ret, i, total=0;
48         const char *attrs[] = { NULL };
49         struct ldb_result *res;
50         
51         ret = ldb_search(ldb, ldb, &res, dn, LDB_SCOPE_SUBTREE, attrs, "distinguishedName=*");
52         if (ret != LDB_SUCCESS) return -1;
53
54         /* sort the DNs, deepest first */
55         qsort(res->msgs, res->count, sizeof(res->msgs[0]), dn_cmp);
56
57         for (i = 0; i < res->count; i++) {
58                 if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
59                         total++;
60                 } else {
61                         printf("Failed to delete '%s' - %s\n",
62                                ldb_dn_get_linearized(res->msgs[i]->dn),
63                                ldb_errstring(ldb));
64                 }
65         }
66
67         talloc_free(res);
68
69         if (total == 0) {
70                 return -1;
71         }
72         printf("Deleted %d records\n", total);
73         return 0;
74 }
75
76 static void usage(void)
77 {
78         printf("Usage: ldbdel <options> <DN...>\n");
79         printf("Deletes records from a ldb\n\n");
80         ldb_cmdline_help("ldbdel", stdout);
81         exit(1);
82 }
83
84 int main(int argc, const char **argv)
85 {
86         struct ldb_context *ldb;
87         int ret = 0, i;
88         struct ldb_cmdline *options;
89
90         ldb = ldb_init(NULL, NULL);
91
92         options = ldb_cmdline_process(ldb, argc, argv, usage);
93
94         if (options->argc < 1) {
95                 usage();
96                 exit(1);
97         }
98
99         for (i=0;i<options->argc;i++) {
100                 struct ldb_dn *dn;
101
102                 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
103                 if ( ! ldb_dn_validate(dn)) {
104                         printf("Invalid DN format\n");
105                         exit(1);
106                 }
107                 if (options->recursive) {
108                         ret = ldb_delete_recursive(ldb, dn);
109                 } else {
110                         ret = ldb_delete(ldb, dn);
111                         if (ret == 0) {
112                                 printf("Deleted 1 record\n");
113                         }
114                 }
115                 if (ret != 0) {
116                         printf("delete of '%s' failed - %s\n",
117                                 ldb_dn_get_linearized(dn),
118                                 ldb_errstring(ldb));
119                 }
120         }
121
122         talloc_free(ldb);
123
124         return ret;
125 }