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