Fix Coverity ID 472
[Samba.git] / source / lib / ldb / tools / ldbedit.c
blob0e1fd38e4c08d0e21ae97a485b56a022104e7973
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
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
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.
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.
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/>.
25 * Name: ldb
27 * Component: ldbedit
29 * Description: utility for ldb database editing
31 * Author: Andrew Tridgell
34 #include "includes.h"
35 #include "ldb/include/includes.h"
36 #include "ldb/tools/cmdline.h"
38 static struct ldb_cmdline *options;
41 debug routine
43 static void ldif_write_msg(struct ldb_context *ldb,
44 FILE *f,
45 enum ldb_changetype changetype,
46 struct ldb_message *msg)
48 struct ldb_ldif ldif;
49 ldif.changetype = changetype;
50 ldif.msg = msg;
51 ldb_ldif_write_file(ldb, f, &ldif);
55 modify a database record so msg1 becomes msg2
56 returns the number of modified elements
58 static int modify_record(struct ldb_context *ldb,
59 struct ldb_message *msg1,
60 struct ldb_message *msg2)
62 struct ldb_message *mod;
64 mod = ldb_msg_diff(ldb, msg1, msg2);
65 if (mod == NULL) {
66 fprintf(stderr, "Failed to calculate message differences\n");
67 return -1;
70 if (mod->num_elements == 0) {
71 return 0;
74 if (options->verbose > 0) {
75 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
78 if (ldb_modify(ldb, mod) != 0) {
79 fprintf(stderr, "failed to modify %s - %s\n",
80 ldb_dn_linearize(ldb, msg1->dn), ldb_errstring(ldb));
81 return -1;
84 return mod->num_elements;
88 find dn in msgs[]
90 static struct ldb_message *msg_find(struct ldb_context *ldb,
91 struct ldb_message **msgs,
92 int count,
93 const struct ldb_dn *dn)
95 int i;
96 for (i=0;i<count;i++) {
97 if (ldb_dn_compare(ldb, dn, msgs[i]->dn) == 0) {
98 return msgs[i];
101 return NULL;
105 merge the changes in msgs2 into the messages from msgs1
107 static int merge_edits(struct ldb_context *ldb,
108 struct ldb_message **msgs1, int count1,
109 struct ldb_message **msgs2, int count2)
111 int i;
112 struct ldb_message *msg;
113 int ret = 0;
114 int adds=0, modifies=0, deletes=0;
116 /* do the adds and modifies */
117 for (i=0;i<count2;i++) {
118 msg = msg_find(ldb, msgs1, count1, msgs2[i]->dn);
119 if (!msg) {
120 if (options->verbose > 0) {
121 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
123 if (ldb_add(ldb, msgs2[i]) != 0) {
124 fprintf(stderr, "failed to add %s - %s\n",
125 ldb_dn_linearize(ldb, msgs2[i]->dn),
126 ldb_errstring(ldb));
127 return -1;
129 adds++;
130 } else {
131 if (modify_record(ldb, msg, msgs2[i]) > 0) {
132 modifies++;
137 /* do the deletes */
138 for (i=0;i<count1;i++) {
139 msg = msg_find(ldb, msgs2, count2, msgs1[i]->dn);
140 if (!msg) {
141 if (options->verbose > 0) {
142 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
144 if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
145 fprintf(stderr, "failed to delete %s - %s\n",
146 ldb_dn_linearize(ldb, msgs1[i]->dn),
147 ldb_errstring(ldb));
148 return -1;
150 deletes++;
154 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
156 return ret;
160 save a set of messages as ldif to a file
162 static int save_ldif(struct ldb_context *ldb,
163 FILE *f, struct ldb_message **msgs, int count)
165 int i;
167 fprintf(f, "# editing %d records\n", count);
169 for (i=0;i<count;i++) {
170 struct ldb_ldif ldif;
171 fprintf(f, "# record %d\n", i+1);
173 ldif.changetype = LDB_CHANGETYPE_NONE;
174 ldif.msg = msgs[i];
176 ldb_ldif_write_file(ldb, f, &ldif);
179 return 0;
184 edit the ldb search results in msgs using the user selected editor
186 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
187 const char *editor)
189 int fd, ret;
190 FILE *f;
191 char file_template[] = "/tmp/ldbedit.XXXXXX";
192 char *cmd;
193 struct ldb_ldif *ldif;
194 struct ldb_message **msgs2 = NULL;
195 int count2 = 0;
197 /* write out the original set of messages to a temporary
198 file */
199 fd = mkstemp(file_template);
201 if (fd == -1) {
202 perror(file_template);
203 return -1;
206 f = fdopen(fd, "r+");
208 if (!f) {
209 perror("fopen");
210 close(fd);
211 unlink(file_template);
212 return -1;
215 if (save_ldif(ldb, f, msgs1, count1) != 0) {
216 return -1;
219 fclose(f);
221 cmd = talloc_asprintf(ldb, "%s %s", editor, file_template);
223 if (!cmd) {
224 unlink(file_template);
225 fprintf(stderr, "out of memory\n");
226 return -1;
229 /* run the editor */
230 ret = system(cmd);
231 talloc_free(cmd);
233 if (ret != 0) {
234 unlink(file_template);
235 fprintf(stderr, "edit with %s failed\n", editor);
236 return -1;
239 /* read the resulting ldif into msgs2 */
240 f = fopen(file_template, "r");
241 if (!f) {
242 perror(file_template);
243 return -1;
246 while ((ldif = ldb_ldif_read_file(ldb, f))) {
247 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
248 if (!msgs2) {
249 fprintf(stderr, "out of memory");
250 return -1;
252 msgs2[count2++] = ldif->msg;
255 fclose(f);
256 unlink(file_template);
258 return merge_edits(ldb, msgs1, count1, msgs2, count2);
261 static void usage(void)
263 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
264 printf("Options:\n");
265 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
266 printf(" -s base|sub|one choose search scope\n");
267 printf(" -b basedn choose baseDN\n");
268 printf(" -a edit all records (expression 'objectclass=*')\n");
269 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
270 printf(" -v verbose mode\n");
271 exit(1);
274 int main(int argc, const char **argv)
276 struct ldb_context *ldb;
277 struct ldb_result *result = NULL;
278 struct ldb_dn *basedn = NULL;
279 int ret;
280 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
281 const char * const * attrs = NULL;
283 ldb_global_init();
285 ldb = ldb_init(NULL);
287 options = ldb_cmdline_process(ldb, argc, argv, usage);
289 /* the check for '=' is for compatibility with ldapsearch */
290 if (options->argc > 0 &&
291 strchr(options->argv[0], '=')) {
292 expression = options->argv[0];
293 options->argv++;
294 options->argc--;
297 if (options->argc > 0) {
298 attrs = (const char * const *)(options->argv);
301 if (options->basedn != NULL) {
302 basedn = ldb_dn_explode(ldb, options->basedn);
303 if (basedn == NULL) {
304 printf("Invalid Base DN format\n");
305 exit(1);
309 ret = ldb_search(ldb, basedn, options->scope, expression, attrs, &result);
310 if (ret != LDB_SUCCESS) {
311 printf("search failed - %s\n", ldb_errstring(ldb));
312 exit(1);
315 if (result->count == 0) {
316 printf("no matching records - cannot edit\n");
317 return 0;
320 do_edit(ldb, result->msgs, result->count, options->editor);
322 ret = talloc_free(result);
323 if (ret == -1) {
324 fprintf(stderr, "talloc_free failed\n");
325 exit(1);
328 talloc_free(ldb);
329 return 0;