r3760: mention -o switch in help message
[metze/samba/wip.git] / source / 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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbdel
29  *
30  *  Description: utility to delete records - modelled on ldapdelete
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36
37 static int ldb_delete_recursive(struct ldb_context *ldb, const char *dn)
38 {
39         int ret, i, total=0;
40         const char *attrs[] = { "dn", NULL };
41         struct ldb_message **res;
42         
43         ret = ldb_search(ldb, dn, LDB_SCOPE_SUBTREE, "dn=*", attrs, &res);
44         if (ret <= 0) return -1;
45
46         for (i=0;i<ret;i++) {
47                 if (ldb_delete(ldb, res[i]->dn) == 0) {
48                         total++;
49                 }
50         }
51
52         ldb_search_free(ldb, 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, char * const argv[])
75 {
76         struct ldb_context *ldb;
77         const char **options = NULL;
78         int ldbopts;
79         int ret, i;
80         const char *ldb_url;
81         int opt, recursive=0;
82
83         ldb_url = getenv("LDB_URL");
84
85         ldbopts = 0;
86         while ((opt = getopt(argc, argv, "hH:ro:")) != EOF) {
87                 switch (opt) {
88                 case 'H':
89                         ldb_url = optarg;
90                         break;
91
92                 case 'r':
93                         recursive=1;
94                         break;
95
96                 case 'o':
97                         ldbopts++;
98                         if (options == NULL) {
99                                 options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
100                         } else {
101                                 options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
102                                 if (options == NULL) {
103                                         fprintf(stderr, "Out of memory!\n");
104                                         exit(-1);
105                                 }
106                         }
107                         options[ldbopts - 1] = optarg;
108                         options[ldbopts] = NULL;
109                         break;
110
111                 case 'h':
112                 default:
113                         usage();
114                         break;
115                 }
116         }
117
118         if (!ldb_url) {
119                 fprintf(stderr, "You must specify a ldb URL\n\n");
120                 usage();
121         }
122
123         argc -= optind;
124         argv += optind;
125
126         if (argc < 1) {
127                 usage();
128                 exit(1);
129         }
130
131         ldb = ldb_connect(ldb_url, 0, options);
132         if (!ldb) {
133                 perror("ldb_connect");
134                 exit(1);
135         }
136
137         ldb_set_debug_stderr(ldb);
138
139         for (i=0;i<argc;i++) {
140                 if (recursive) {
141                         ret = ldb_delete_recursive(ldb, argv[i]);
142                 } else {
143                         ret = ldb_delete(ldb, argv[i]);
144                         if (ret == 0) {
145                                 printf("Deleted 1 record\n");
146                         }
147                 }
148                 if (ret != 0) {
149                         printf("delete of '%s' failed - %s\n", 
150                                argv[i], ldb_errstring(ldb));
151                 }
152         }
153
154         ldb_close(ldb);
155
156         return 0;
157 }