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) {
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(&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 (strcmp(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(FILE *f, struct ldb_message **msgs, int count)
166 fprintf(f, "# editing %d records\n", count);
168 for (i=0;i<count;i++) {
169 struct ldb_ldif ldif;
170 fprintf(f, "# record %d\n", i+1);
172 ldif.changetype = LDB_CHANGETYPE_NONE;
175 ldif_write_file(f, &ldif);
183 edit the ldb search results in msgs using the user selected editor
185 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
190 char template[] = "/tmp/ldbedit.XXXXXX";
192 struct ldb_ldif *ldif;
193 struct ldb_message **msgs2 = NULL;
196 /* write out the original set of messages to a temporary
198 fd = mkstemp(template);
205 f = fdopen(fd, "r+");
214 if (save_ldif(f, msgs1, count1) != 0) {
220 asprintf(&cmd, "%s %s", editor, template);
224 fprintf(stderr, "out of memory\n");
234 fprintf(stderr, "edit with %s failed\n", editor);
238 /* read the resulting ldif into msgs2 */
239 f = fopen(template, "r");
245 while ((ldif = ldif_read_file(f))) {
246 msgs2 = realloc_p(msgs2, struct ldb_message *, count2+1);
248 fprintf(stderr, "out of memory");
251 msgs2[count2++] = &ldif->msg;
257 return merge_edits(ldb, msgs1, count1, msgs2, count2);
260 static void usage(void)
262 printf("Usage: ldbedit <options> <expression>\n");
263 printf("Options:\n");
264 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
265 printf(" -s base|sub|one choose search scope\n");
266 printf(" -b basedn choose baseDN\n");
267 printf(" -a edit all records (expression 'objectclass=*')\n");
268 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
272 int main(int argc, char * const argv[])
274 struct ldb_context *ldb;
275 struct ldb_message **msgs;
277 const char *expression = NULL;
279 const char *basedn = NULL;
281 enum ldb_scope scope = LDB_SCOPE_SUBTREE;
284 ldb_url = getenv("LDB_URL");
286 /* build the editor command to run -
287 use the same editor priorities as vipw */
288 editor = getenv("VISUAL");
290 editor = getenv("EDITOR");
296 while ((opt = getopt(argc, argv, "hab:e:H:s:")) != EOF) {
307 if (strcmp(optarg, "base") == 0) {
308 scope = LDB_SCOPE_BASE;
309 } else if (strcmp(optarg, "sub") == 0) {
310 scope = LDB_SCOPE_SUBTREE;
311 } else if (strcmp(optarg, "one") == 0) {
312 scope = LDB_SCOPE_ONELEVEL;
321 expression = "(|(objectclass=*)(dn=*))";
332 fprintf(stderr, "You must specify a ldb URL\n\n");
343 expression = argv[0];
346 ldb = ldb_connect(ldb_url, 0, NULL);
349 perror("ldb_connect");
353 ret = ldb_search(ldb, basedn, scope, expression, NULL, &msgs);
356 printf("search failed - %s\n", ldb_errstring(ldb));
361 printf("no matching records - cannot edit\n");
365 do_edit(ldb, msgs, ret, editor);
368 ret = ldb_search_free(ldb, msgs);
370 fprintf(stderr, "search_free failed - %s\n", ldb_errstring(ldb));