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