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"
38 #include "ldb/tools/cmdline.h"
41 #include "system/filesys.h"
44 static struct ldb_cmdline *options;
49 static void ldif_write_msg(struct ldb_context *ldb,
51 enum ldb_changetype changetype,
52 struct ldb_message *msg)
55 ldif.changetype = changetype;
57 ldb_ldif_write_file(ldb, f, &ldif);
61 modify a database record so msg1 becomes msg2
62 returns the number of modified elements
64 static int modify_record(struct ldb_context *ldb,
65 struct ldb_message *msg1,
66 struct ldb_message *msg2)
68 struct ldb_message *mod;
70 mod = ldb_msg_diff(ldb, msg1, msg2);
72 fprintf(stderr, "Failed to calculate message differences\n");
76 if (mod->num_elements == 0) {
80 if (options->verbose > 0) {
81 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
84 if (ldb_modify(ldb, mod) != 0) {
85 fprintf(stderr, "failed to modify %s - %s\n",
86 msg1->dn, ldb_errstring(ldb));
90 return mod->num_elements;
96 static struct ldb_message *msg_find(struct ldb_message **msgs, int count,
100 for (i=0;i<count;i++) {
101 if (ldb_dn_cmp(dn, msgs[i]->dn) == 0) {
109 merge the changes in msgs2 into the messages from msgs1
111 static int merge_edits(struct ldb_context *ldb,
112 struct ldb_message **msgs1, int count1,
113 struct ldb_message **msgs2, int count2)
116 struct ldb_message *msg;
118 int adds=0, modifies=0, deletes=0;
120 /* do the adds and modifies */
121 for (i=0;i<count2;i++) {
122 msg = msg_find(msgs1, count1, msgs2[i]->dn);
124 if (options->verbose > 0) {
125 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
127 if (ldb_add(ldb, msgs2[i]) != 0) {
128 fprintf(stderr, "failed to add %s - %s\n",
129 msgs2[i]->dn, ldb_errstring(ldb));
134 if (modify_record(ldb, msg, msgs2[i]) > 0) {
141 for (i=0;i<count1;i++) {
142 msg = msg_find(msgs2, count2, msgs1[i]->dn);
144 if (options->verbose > 0) {
145 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
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));
156 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
162 save a set of messages as ldif to a file
164 static int save_ldif(struct ldb_context *ldb,
165 FILE *f, struct ldb_message **msgs, int count)
169 fprintf(f, "# editing %d records\n", count);
171 for (i=0;i<count;i++) {
172 struct ldb_ldif ldif;
173 fprintf(f, "# record %d\n", i+1);
175 ldif.changetype = LDB_CHANGETYPE_NONE;
178 ldb_ldif_write_file(ldb, f, &ldif);
186 edit the ldb search results in msgs using the user selected editor
188 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
193 char template[] = "/tmp/ldbedit.XXXXXX";
195 struct ldb_ldif *ldif;
196 struct ldb_message **msgs2 = NULL;
199 /* write out the original set of messages to a temporary
201 fd = mkstemp(template);
208 f = fdopen(fd, "r+");
217 if (save_ldif(ldb, f, msgs1, count1) != 0) {
223 asprintf(&cmd, "%s %s", editor, template);
227 fprintf(stderr, "out of memory\n");
237 fprintf(stderr, "edit with %s failed\n", editor);
241 /* read the resulting ldif into msgs2 */
242 f = fopen(template, "r");
248 while ((ldif = ldb_ldif_read_file(ldb, f))) {
249 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
251 fprintf(stderr, "out of memory");
254 msgs2[count2++] = ldif->msg;
260 return merge_edits(ldb, msgs1, count1, msgs2, count2);
263 static void usage(void)
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");
276 int main(int argc, const char **argv)
278 struct ldb_context *ldb;
279 struct ldb_message **msgs;
281 const char *expression = "(|(objectclass=*)(dn=*))";
282 const char * const * attrs = NULL;
284 ldb = ldb_init(NULL);
286 options = ldb_cmdline_process(ldb, argc, argv, usage);
288 /* the check for '=' is for compatibility with ldapsearch */
289 if (options->argc > 0 &&
290 strchr(options->argv[0], '=')) {
291 expression = options->argv[0];
296 if (options->argc > 0) {
297 attrs = (const char * const *)(options->argv);
300 ret = ldb_search(ldb, options->basedn, options->scope, expression, attrs, &msgs);
302 printf("search failed - %s\n", ldb_errstring(ldb));
307 printf("no matching records - cannot edit\n");
311 do_edit(ldb, msgs, ret, options->editor);
314 ret = talloc_free(msgs);
316 fprintf(stderr, "talloc_free failed\n");