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
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.
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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Description: utility for ldb database editing
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
40 #include "system/filesys.h"
48 static void ldif_write_msg(struct ldb_context *ldb,
50 enum ldb_changetype changetype,
51 struct ldb_message *msg)
54 ldif.changetype = changetype;
56 ldb_ldif_write_file(ldb, f, &ldif);
60 modify a database record so msg1 becomes msg2
61 returns the number of modified elements
63 static int modify_record(struct ldb_context *ldb,
64 struct ldb_message *msg1,
65 struct ldb_message *msg2)
67 struct ldb_message *mod;
68 struct ldb_message_element *el;
72 mod = ldb_msg_new(ldb);
75 mod->num_elements = 0;
78 msg2 = ldb_msg_canonicalize(ldb, msg2);
80 fprintf(stderr, "Failed to canonicalise msg2\n");
84 /* look in msg2 to find elements that need to be added
86 for (i=0;i<msg2->num_elements;i++) {
87 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
89 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
93 if (ldb_msg_add(ldb, mod,
95 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
101 /* look in msg1 to find elements that need to be deleted */
102 for (i=0;i<msg1->num_elements;i++) {
103 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
105 if (ldb_msg_add_empty(ldb, mod,
106 msg1->elements[i].name,
107 LDB_FLAG_MOD_DELETE) != 0) {
114 if (mod->num_elements == 0) {
118 if (ldb_modify(ldb, mod) != 0) {
119 fprintf(stderr, "failed to modify %s - %s\n",
120 msg1->dn, ldb_errstring(ldb));
125 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
134 static struct ldb_message *msg_find(struct ldb_message **msgs, int count,
138 for (i=0;i<count;i++) {
139 if (ldb_dn_cmp(dn, msgs[i]->dn) == 0) {
147 merge the changes in msgs2 into the messages from msgs1
149 static int merge_edits(struct ldb_context *ldb,
150 struct ldb_message **msgs1, int count1,
151 struct ldb_message **msgs2, int count2)
154 struct ldb_message *msg;
156 int adds=0, modifies=0, deletes=0;
158 /* do the adds and modifies */
159 for (i=0;i<count2;i++) {
160 msg = msg_find(msgs1, count1, msgs2[i]->dn);
162 if (ldb_add(ldb, msgs2[i]) != 0) {
163 fprintf(stderr, "failed to add %s - %s\n",
164 msgs2[i]->dn, ldb_errstring(ldb));
168 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
172 if (modify_record(ldb, msg, msgs2[i]) > 0) {
179 for (i=0;i<count1;i++) {
180 msg = msg_find(msgs2, count2, msgs1[i]->dn);
182 if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
183 fprintf(stderr, "failed to delete %s - %s\n",
184 msgs1[i]->dn, ldb_errstring(ldb));
188 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
194 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
200 save a set of messages as ldif to a file
202 static int save_ldif(struct ldb_context *ldb,
203 FILE *f, struct ldb_message **msgs, int count)
207 fprintf(f, "# editing %d records\n", count);
209 for (i=0;i<count;i++) {
210 struct ldb_ldif ldif;
211 fprintf(f, "# record %d\n", i+1);
213 ldif.changetype = LDB_CHANGETYPE_NONE;
216 ldb_ldif_write_file(ldb, f, &ldif);
224 edit the ldb search results in msgs using the user selected editor
226 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
231 char template[] = "/tmp/ldbedit.XXXXXX";
233 struct ldb_ldif *ldif;
234 struct ldb_message **msgs2 = NULL;
237 /* write out the original set of messages to a temporary
239 fd = mkstemp(template);
246 f = fdopen(fd, "r+");
255 if (save_ldif(ldb, f, msgs1, count1) != 0) {
261 asprintf(&cmd, "%s %s", editor, template);
265 fprintf(stderr, "out of memory\n");
275 fprintf(stderr, "edit with %s failed\n", editor);
279 /* read the resulting ldif into msgs2 */
280 f = fopen(template, "r");
286 while ((ldif = ldb_ldif_read_file(ldb, f))) {
287 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
289 fprintf(stderr, "out of memory");
292 msgs2[count2++] = ldif->msg;
298 return merge_edits(ldb, msgs1, count1, msgs2, count2);
301 static void usage(void)
303 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
304 printf("Options:\n");
305 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
306 printf(" -s base|sub|one choose search scope\n");
307 printf(" -b basedn choose baseDN\n");
308 printf(" -a edit all records (expression 'objectclass=*')\n");
309 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
310 printf(" -v verbose mode)\n");
314 int main(int argc, char * const argv[])
316 struct ldb_context *ldb;
317 struct ldb_message **msgs;
319 const char *expression = NULL;
321 const char *basedn = NULL;
322 const char **options = NULL;
325 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
327 const char * const * attrs = NULL;
329 ldb_url = getenv("LDB_URL");
331 /* build the editor command to run -
332 use the same editor priorities as vipw */
333 editor = getenv("VISUAL");
335 editor = getenv("EDITOR");
342 while ((opt = getopt(argc, argv, "hab:e:H:s:vo:")) != EOF) {
353 if (strcmp(optarg, "base") == 0) {
354 scope = LDB_SCOPE_BASE;
355 } else if (strcmp(optarg, "sub") == 0) {
356 scope = LDB_SCOPE_SUBTREE;
357 } else if (strcmp(optarg, "one") == 0) {
358 scope = LDB_SCOPE_ONELEVEL;
367 expression = "(|(objectclass=*)(dn=*))";
375 options = ldb_options_parse(options, &ldbopts, optarg);
386 fprintf(stderr, "You must specify a ldb URL\n\n");
397 expression = argv[0];
403 attrs = (const char * const *)argv;
406 ldb = ldb_connect(ldb_url, 0, options);
409 perror("ldb_connect");
413 ldb_set_debug_stderr(ldb);
415 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
418 printf("search failed - %s\n", ldb_errstring(ldb));
423 printf("no matching records - cannot edit\n");
427 do_edit(ldb, msgs, ret, editor);
430 ret = ldb_search_free(ldb, msgs);
432 fprintf(stderr, "search_free failed - %s\n", ldb_errstring(ldb));