r23798: updated old Temple Place FSF addresses to new URL
[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_includes.h"
35 #include "tools/cmdline.h"
36
37 static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
38 {
39         int ret, i, total=0;
40         const char *attrs[] = { NULL };
41         struct ldb_result *res;
42         
43         ret = ldb_search(ldb, dn, LDB_SCOPE_SUBTREE, "distinguishedName=*", attrs, &res);
44         if (ret != LDB_SUCCESS) return -1;
45
46         for (i = 0; i < res->count; i++) {
47                 if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
48                         total++;
49                 }
50         }
51
52         talloc_free(res);
53
54         if (total == 0) {
55                 return -1;
56         }
57         printf("Deleted %d records\n", total);
58         return 0;
59 }
60
61 static void usage(void)
62 {
63         printf("Usage: ldbdel <options> <DN...>\n");
64         printf("Options:\n");
65         printf("  -r               recursively delete the given subtree\n");
66         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
67         printf("  -o options       pass options like modules to activate\n");
68         printf("              e.g: -o modules:timestamps\n");
69         printf("\n");
70         printf("Deletes records from a ldb\n\n");
71         exit(1);
72 }
73
74 int main(int argc, const char **argv)
75 {
76         struct ldb_context *ldb;
77         int ret = 0, i;
78         struct ldb_cmdline *options;
79
80         ldb_global_init();
81
82         ldb = ldb_init(NULL);
83
84         options = ldb_cmdline_process(ldb, argc, argv, usage);
85
86         if (options->argc < 1) {
87                 usage();
88                 exit(1);
89         }
90
91         for (i=0;i<options->argc;i++) {
92                 struct ldb_dn *dn;
93
94                 dn = ldb_dn_new(ldb, ldb, options->argv[i]);
95                 if ( ! ldb_dn_validate(dn)) {
96                         printf("Invalid DN format\n");
97                         exit(1);
98                 }
99                 if (options->recursive) {
100                         ret = ldb_delete_recursive(ldb, dn);
101                 } else {
102                         ret = ldb_delete(ldb, dn);
103                         if (ret == 0) {
104                                 printf("Deleted 1 record\n");
105                         }
106                 }
107                 if (ret != 0) {
108                         printf("delete of '%s' failed - %s\n",
109                                 ldb_dn_get_linearized(dn),
110                                 ldb_errstring(ldb));
111                 }
112         }
113
114         talloc_free(ldb);
115
116         return ret;
117 }