b18aea1b10ca3e02db4a9ff1bcfbf4bb92375d5e
[kai/samba.git] / source4 / lib / ldb / tools / ldbedit.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: ldbedit
28  *
29  *  Description: utility for ldb database editing
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb_includes.h"
35 #include "tools/cmdline.h"
36
37 static struct ldb_cmdline *options;
38
39 /*
40   debug routine 
41 */
42 static void ldif_write_msg(struct ldb_context *ldb, 
43                            FILE *f, 
44                            enum ldb_changetype changetype,
45                            struct ldb_message *msg)
46 {
47         struct ldb_ldif ldif;
48         ldif.changetype = changetype;
49         ldif.msg = msg;
50         ldb_ldif_write_file(ldb, f, &ldif);
51 }
52
53 /*
54   modify a database record so msg1 becomes msg2
55   returns the number of modified elements
56 */
57 static int modify_record(struct ldb_context *ldb, 
58                          struct ldb_message *msg1,
59                          struct ldb_message *msg2)
60 {
61         struct ldb_message *mod;
62
63         mod = ldb_msg_diff(ldb, msg1, msg2);
64         if (mod == NULL) {
65                 fprintf(stderr, "Failed to calculate message differences\n");
66                 return -1;
67         }
68
69         if (mod->num_elements == 0) {
70                 return 0;
71         }
72
73         if (options->verbose > 0) {
74                 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
75         }
76
77         if (ldb_modify(ldb, mod) != 0) {
78                 fprintf(stderr, "failed to modify %s - %s\n", 
79                         ldb_dn_get_linearized(msg1->dn), ldb_errstring(ldb));
80                 return -1;
81         }
82
83         return mod->num_elements;
84 }
85
86 /*
87   find dn in msgs[]
88 */
89 static struct ldb_message *msg_find(struct ldb_context *ldb,
90                                     struct ldb_message **msgs,
91                                     int count,
92                                     struct ldb_dn *dn)
93 {
94         int i;
95         for (i=0;i<count;i++) {
96                 if (ldb_dn_compare(dn, msgs[i]->dn) == 0) {
97                         return msgs[i];
98                 }
99         }
100         return NULL;
101 }
102
103 /*
104   merge the changes in msgs2 into the messages from msgs1
105 */
106 static int merge_edits(struct ldb_context *ldb,
107                        struct ldb_message **msgs1, int count1,
108                        struct ldb_message **msgs2, int count2)
109 {
110         int i;
111         struct ldb_message *msg;
112         int ret = 0;
113         int adds=0, modifies=0, deletes=0;
114
115         if (ldb_transaction_start(ldb) != 0) {
116                 fprintf(stderr, "Failed to start transaction\n");
117                 return -1;
118         }
119
120         /* do the adds and modifies */
121         for (i=0;i<count2;i++) {
122                 msg = msg_find(ldb, msgs1, count1, msgs2[i]->dn);
123                 if (!msg) {
124                         if (options->verbose > 0) {
125                                 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
126                         }
127                         if (ldb_add(ldb, msgs2[i]) != 0) {
128                                 fprintf(stderr, "failed to add %s - %s\n",
129                                         ldb_dn_get_linearized(msgs2[i]->dn),
130                                         ldb_errstring(ldb));
131                                 return -1;
132                         }
133                         adds++;
134                 } else {
135                         if (modify_record(ldb, msg, msgs2[i]) > 0) {
136                                 modifies++;
137                         }
138                 }
139         }
140
141         /* do the deletes */
142         for (i=0;i<count1;i++) {
143                 msg = msg_find(ldb, msgs2, count2, msgs1[i]->dn);
144                 if (!msg) {
145                         if (options->verbose > 0) {
146                                 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
147                         }
148                         if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
149                                 fprintf(stderr, "failed to delete %s - %s\n",
150                                         ldb_dn_get_linearized(msgs1[i]->dn),
151                                         ldb_errstring(ldb));
152                                 return -1;
153                         }
154                         deletes++;
155                 }
156         }
157
158         if (ldb_transaction_commit(ldb) != 0) {
159                 fprintf(stderr, "Failed to commit transaction\n");
160                 return -1;
161         }
162
163         printf("# %d adds  %d modifies  %d deletes\n", adds, modifies, deletes);
164
165         return ret;
166 }
167
168 /*
169   save a set of messages as ldif to a file
170 */
171 static int save_ldif(struct ldb_context *ldb, 
172                      FILE *f, struct ldb_message **msgs, int count)
173 {
174         int i;
175
176         fprintf(f, "# editing %d records\n", count);
177
178         for (i=0;i<count;i++) {
179                 struct ldb_ldif ldif;
180                 fprintf(f, "# record %d\n", i+1);
181
182                 ldif.changetype = LDB_CHANGETYPE_NONE;
183                 ldif.msg = msgs[i];
184
185                 ldb_ldif_write_file(ldb, f, &ldif);
186         }
187
188         return 0;
189 }
190
191
192 /*
193   edit the ldb search results in msgs using the user selected editor
194 */
195 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
196                    const char *editor)
197 {
198         int fd, ret;
199         FILE *f;
200         char file_template[] = "/tmp/ldbedit.XXXXXX";
201         char *cmd;
202         struct ldb_ldif *ldif;
203         struct ldb_message **msgs2 = NULL;
204         int count2 = 0;
205
206         /* write out the original set of messages to a temporary
207            file */
208         fd = mkstemp(file_template);
209
210         if (fd == -1) {
211                 perror(file_template);
212                 return -1;
213         }
214
215         f = fdopen(fd, "r+");
216
217         if (!f) {
218                 perror("fopen");
219                 close(fd);
220                 unlink(file_template);
221                 return -1;
222         }
223
224         if (save_ldif(ldb, f, msgs1, count1) != 0) {
225                 return -1;
226         }
227
228         fclose(f);
229
230         cmd = talloc_asprintf(ldb, "%s %s", editor, file_template);
231
232         if (!cmd) {
233                 unlink(file_template);
234                 fprintf(stderr, "out of memory\n");
235                 return -1;
236         }
237
238         /* run the editor */
239         ret = system(cmd);
240         talloc_free(cmd);
241
242         if (ret != 0) {
243                 unlink(file_template);
244                 fprintf(stderr, "edit with %s failed\n", editor);
245                 return -1;
246         }
247
248         /* read the resulting ldif into msgs2 */
249         f = fopen(file_template, "r");
250         if (!f) {
251                 perror(file_template);
252                 return -1;
253         }
254
255         while ((ldif = ldb_ldif_read_file(ldb, f))) {
256                 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
257                 if (!msgs2) {
258                         fprintf(stderr, "out of memory");
259                         return -1;
260                 }
261                 msgs2[count2++] = ldif->msg;
262         }
263
264         fclose(f);
265         unlink(file_template);
266
267         return merge_edits(ldb, msgs1, count1, msgs2, count2);
268 }
269
270 static void usage(void)
271 {
272         printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
273         printf("Options:\n");
274         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
275         printf("  -s base|sub|one  choose search scope\n");
276         printf("  -b basedn        choose baseDN\n");
277         printf("  -a               edit all records (expression 'objectclass=*')\n");
278         printf("  -e editor        choose editor (or $VISUAL or $EDITOR)\n");
279         printf("  -v               verbose mode\n");
280         exit(1);
281 }
282
283 int main(int argc, const char **argv)
284 {
285         struct ldb_context *ldb;
286         struct ldb_result *result = NULL;
287         struct ldb_dn *basedn = NULL;
288         int ret;
289         const char *expression = "(|(objectClass=*)(distinguishedName=*))";
290         const char * const * attrs = NULL;
291
292         ldb = ldb_init(NULL, NULL);
293
294         options = ldb_cmdline_process(ldb, argc, argv, usage);
295
296         /* the check for '=' is for compatibility with ldapsearch */
297         if (options->argc > 0 && 
298             strchr(options->argv[0], '=')) {
299                 expression = options->argv[0];
300                 options->argv++;
301                 options->argc--;
302         }
303
304         if (options->argc > 0) {
305                 attrs = (const char * const *)(options->argv);
306         }
307
308         if (options->basedn != NULL) {
309                 basedn = ldb_dn_new(ldb, ldb, options->basedn);
310                 if ( ! ldb_dn_validate(basedn)) {
311                         printf("Invalid Base DN format\n");
312                         exit(1);
313                 }
314         }
315
316         ret = ldb_search(ldb, ldb, &result, basedn, options->scope, attrs, "%s", expression);
317         if (ret != LDB_SUCCESS) {
318                 printf("search failed - %s\n", ldb_errstring(ldb));
319                 exit(1);
320         }
321
322         if (result->count == 0) {
323                 printf("no matching records - cannot edit\n");
324                 return 0;
325         }
326
327         do_edit(ldb, result->msgs, result->count, options->editor);
328
329         if (result) {
330                 ret = talloc_free(result);
331                 if (ret == -1) {
332                         fprintf(stderr, "talloc_free failed\n");
333                         exit(1);
334                 }
335         }
336
337         talloc_free(ldb);
338         return 0;
339 }