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_errors.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 ldb_dn_linearize(ldb, msg1->dn), ldb_errstring(ldb));
90 return mod->num_elements;
96 static struct ldb_message *msg_find(struct ldb_context *ldb,
97 struct ldb_message **msgs,
99 const struct ldb_dn *dn)
102 for (i=0;i<count;i++) {
103 if (ldb_dn_compare(ldb, dn, msgs[i]->dn) == 0) {
111 merge the changes in msgs2 into the messages from msgs1
113 static int merge_edits(struct ldb_context *ldb,
114 struct ldb_message **msgs1, int count1,
115 struct ldb_message **msgs2, int count2)
118 struct ldb_message *msg;
120 int adds=0, modifies=0, deletes=0;
122 /* do the adds and modifies */
123 for (i=0;i<count2;i++) {
124 msg = msg_find(ldb, msgs1, count1, msgs2[i]->dn);
126 if (options->verbose > 0) {
127 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
129 if (ldb_add(ldb, msgs2[i]) != 0) {
130 fprintf(stderr, "failed to add %s - %s\n",
131 ldb_dn_linearize(ldb, msgs2[i]->dn),
137 if (modify_record(ldb, msg, msgs2[i]) > 0) {
144 for (i=0;i<count1;i++) {
145 msg = msg_find(ldb, msgs2, count2, msgs1[i]->dn);
147 if (options->verbose > 0) {
148 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
150 if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
151 fprintf(stderr, "failed to delete %s - %s\n",
152 ldb_dn_linearize(ldb, msgs1[i]->dn),
160 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
166 save a set of messages as ldif to a file
168 static int save_ldif(struct ldb_context *ldb,
169 FILE *f, struct ldb_message **msgs, int count)
173 fprintf(f, "# editing %d records\n", count);
175 for (i=0;i<count;i++) {
176 struct ldb_ldif ldif;
177 fprintf(f, "# record %d\n", i+1);
179 ldif.changetype = LDB_CHANGETYPE_NONE;
182 ldb_ldif_write_file(ldb, f, &ldif);
190 edit the ldb search results in msgs using the user selected editor
192 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
197 char template[] = "/tmp/ldbedit.XXXXXX";
199 struct ldb_ldif *ldif;
200 struct ldb_message **msgs2 = NULL;
203 /* write out the original set of messages to a temporary
205 fd = mkstemp(template);
212 f = fdopen(fd, "r+");
221 if (save_ldif(ldb, f, msgs1, count1) != 0) {
227 asprintf(&cmd, "%s %s", editor, template);
231 fprintf(stderr, "out of memory\n");
241 fprintf(stderr, "edit with %s failed\n", editor);
245 /* read the resulting ldif into msgs2 */
246 f = fopen(template, "r");
252 while ((ldif = ldb_ldif_read_file(ldb, f))) {
253 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
255 fprintf(stderr, "out of memory");
258 msgs2[count2++] = ldif->msg;
264 return merge_edits(ldb, msgs1, count1, msgs2, count2);
267 static void usage(void)
269 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
270 printf("Options:\n");
271 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
272 printf(" -s base|sub|one choose search scope\n");
273 printf(" -b basedn choose baseDN\n");
274 printf(" -a edit all records (expression 'objectclass=*')\n");
275 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
276 printf(" -v verbose mode\n");
280 int main(int argc, const char **argv)
282 struct ldb_context *ldb;
283 struct ldb_result *result = NULL;
284 struct ldb_dn *basedn = NULL;
286 const char *expression = "(|(objectclass=*)(distinguishedName=*))";
287 const char * const * attrs = NULL;
289 ldb = ldb_init(NULL);
291 options = ldb_cmdline_process(ldb, argc, argv, usage);
293 /* the check for '=' is for compatibility with ldapsearch */
294 if (options->argc > 0 &&
295 strchr(options->argv[0], '=')) {
296 expression = options->argv[0];
301 if (options->argc > 0) {
302 attrs = (const char * const *)(options->argv);
305 if (options->basedn != NULL) {
306 basedn = ldb_dn_explode(ldb, options->basedn);
307 if (basedn == NULL) {
308 printf("Invalid Base DN format\n");
313 ret = ldb_search(ldb, basedn, options->scope, expression, attrs, &result);
314 if (ret != LDB_SUCCESS) {
315 printf("search failed - %s\n", ldb_errstring(ldb));
319 if (result->count == 0) {
320 printf("no matching records - cannot edit\n");
324 do_edit(ldb, result->msgs, result->count, options->editor);
327 ret = talloc_free(result);
329 fprintf(stderr, "talloc_free failed\n");