r5585: LDB interfaces change:
[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 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38
39 #ifdef _SAMBA_BUILD_
40 #include "system/filesys.h"
41 #endif
42
43 static int ldb_delete_recursive(struct ldb_context *ldb, const char *dn)
44 {
45         int ret, i, total=0;
46         const char *attrs[] = { "dn", NULL };
47         struct ldb_message **res;
48         
49         ret = ldb_search(ldb, dn, LDB_SCOPE_SUBTREE, "dn=*", attrs, &res);
50         if (ret <= 0) return -1;
51
52         for (i=0;i<ret;i++) {
53                 if (ldb_delete(ldb, res[i]->dn) == 0) {
54                         total++;
55                 }
56         }
57
58         ldb_search_free(ldb, res);
59
60         if (total == 0) {
61                 return -1;
62         }
63         printf("Deleted %d records\n", total);
64         return 0;
65 }
66
67 static void usage(void)
68 {
69         printf("Usage: ldbdel <options> <DN...>\n");
70         printf("Options:\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");
75         printf("\n");
76         printf("Deletes records from a ldb\n\n");
77         exit(1);
78 }
79
80  int main(int argc, char * const argv[])
81 {
82         struct ldb_context *ldb;
83         const char **options = NULL;
84         int ldbopts;
85         int ret, i;
86         const char *ldb_url;
87         int opt, recursive=0;
88
89         ldb_url = getenv("LDB_URL");
90
91         ldbopts = 0;
92         while ((opt = getopt(argc, argv, "hH:ro:")) != EOF) {
93                 switch (opt) {
94                 case 'H':
95                         ldb_url = optarg;
96                         break;
97
98                 case 'r':
99                         recursive=1;
100                         break;
101
102                 case 'o':
103                         options = ldb_options_parse(options, &ldbopts, optarg);
104                         break;
105
106                 case 'h':
107                 default:
108                         usage();
109                         break;
110                 }
111         }
112
113         if (!ldb_url) {
114                 fprintf(stderr, "You must specify a ldb URL\n\n");
115                 usage();
116         }
117
118         argc -= optind;
119         argv += optind;
120
121         if (argc < 1) {
122                 usage();
123                 exit(1);
124         }
125
126         ldb = ldb_connect(ldb_url, 0, options);
127         if (!ldb) {
128                 perror("ldb_connect");
129                 exit(1);
130         }
131
132         ldb_set_debug_stderr(ldb);
133
134         for (i=0;i<argc;i++) {
135                 if (recursive) {
136                         ret = ldb_delete_recursive(ldb, argv[i]);
137                 } else {
138                         ret = ldb_delete(ldb, argv[i]);
139                         if (ret == 0) {
140                                 printf("Deleted 1 record\n");
141                         }
142                 }
143                 if (ret != 0) {
144                         printf("delete of '%s' failed - %s\n", 
145                                argv[i], ldb_errstring(ldb));
146                 }
147         }
148
149         talloc_free(ldb);
150
151         return 0;
152 }