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