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
38 modify a database record so msg1 becomes msg2
39 returns the number of modified elements
41 static int modify_record(struct ldb_context *ldb,
42 struct ldb_message *msg1,
43 struct ldb_message *msg2)
45 struct ldb_message mod;
46 struct ldb_message_element *el;
54 /* look in msg2 to find elements that need to be added
56 for (i=0;i<msg2->num_elements;i++) {
57 el = ldb_msg_find_element(msg1, msg2->elements[i].name);
59 if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
63 if (ldb_msg_add(ldb, &mod,
65 el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
71 /* look in msg1 to find elements that need to be deleted */
72 for (i=0;i<msg1->num_elements;i++) {
73 el = ldb_msg_find_element(msg2, msg1->elements[i].name);
75 if (ldb_msg_add_empty(ldb, &mod,
76 msg1->elements[i].name,
77 LDB_FLAG_MOD_DELETE) != 0) {
84 if (mod.num_elements == 0) {
88 if (ldb_modify(ldb, &mod) != 0) {
89 fprintf(stderr, "failed to modify %s - %s\n",
90 msg1->dn, ldb_errstring(ldb));
100 static struct ldb_message *msg_find(struct ldb_message **msgs, int count,
104 for (i=0;i<count;i++) {
105 if (ldb_dn_cmp(dn, msgs[i]->dn) == 0) {
113 merge the changes in msgs2 into the messages from msgs1
115 static int merge_edits(struct ldb_context *ldb,
116 struct ldb_message **msgs1, int count1,
117 struct ldb_message **msgs2, int count2)
120 struct ldb_message *msg;
122 int adds=0, modifies=0, deletes=0;
124 /* do the adds and modifies */
125 for (i=0;i<count2;i++) {
126 msg = msg_find(msgs1, count1, msgs2[i]->dn);
128 if (ldb_add(ldb, msgs2[i]) != 0) {
129 fprintf(stderr, "failed to add %s - %s\n",
130 msgs2[i]->dn, ldb_errstring(ldb));
135 if (modify_record(ldb, msg, msgs2[i]) > 0) {
142 for (i=0;i<count1;i++) {
143 msg = msg_find(msgs2, count2, msgs1[i]->dn);
145 if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
146 fprintf(stderr, "failed to delete %s - %s\n",
147 msgs1[i]->dn, ldb_errstring(ldb));
154 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
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)
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;
176 ldb_ldif_write_file(ldb, f, &ldif);
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,
191 char template[] = "/tmp/ldbedit.XXXXXX";
193 struct ldb_ldif *ldif;
194 struct ldb_message **msgs2 = NULL;
197 /* write out the original set of messages to a temporary
199 fd = mkstemp(template);
206 f = fdopen(fd, "r+");
215 if (save_ldif(ldb, f, msgs1, count1) != 0) {
221 asprintf(&cmd, "%s %s", editor, template);
225 fprintf(stderr, "out of memory\n");
235 fprintf(stderr, "edit with %s failed\n", editor);
239 /* read the resulting ldif into msgs2 */
240 f = fopen(template, "r");
246 while ((ldif = ldb_ldif_read_file(ldb, f))) {
247 msgs2 = ldb_realloc_p(ldb, msgs2, struct ldb_message *, count2+1);
249 fprintf(stderr, "out of memory");
252 msgs2[count2++] = &ldif->msg;
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");
273 int main(int argc, char * const argv[])
275 struct ldb_context *ldb;
276 struct ldb_message **msgs;
278 const char *expression = NULL;
280 const char *basedn = NULL;
282 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
284 const char * const * attrs = NULL;
286 ldb_url = getenv("LDB_URL");
288 /* build the editor command to run -
289 use the same editor priorities as vipw */
290 editor = getenv("VISUAL");
292 editor = getenv("EDITOR");
298 while ((opt = getopt(argc, argv, "hab:e:H:s:")) != EOF) {
309 if (strcmp(optarg, "base") == 0) {
310 scope = LDB_SCOPE_BASE;
311 } else if (strcmp(optarg, "sub") == 0) {
312 scope = LDB_SCOPE_SUBTREE;
313 } else if (strcmp(optarg, "one") == 0) {
314 scope = LDB_SCOPE_ONELEVEL;
323 expression = "(|(objectclass=*)(dn=*))";
334 fprintf(stderr, "You must specify a ldb URL\n\n");
345 expression = argv[0];
351 attrs = (const char * const *)argv;
354 ldb = ldb_connect(ldb_url, 0, NULL);
357 perror("ldb_connect");
361 ldb_set_debug_stderr(ldb);
363 ret = ldb_search(ldb, basedn, scope, expression, attrs, &msgs);
366 printf("search failed - %s\n", ldb_errstring(ldb));
371 printf("no matching records - cannot edit\n");
375 do_edit(ldb, msgs, ret, editor);
378 ret = ldb_search_free(ldb, msgs);
380 fprintf(stderr, "search_free failed - %s\n", ldb_errstring(ldb));