r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / ldb / tools / ldbedit.c
blob17ade152b780fed372235570321846aad3efd2a5
1 /*
2 ldb database library
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
8 ** under the LGPL
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
26 * Name: ldb
28 * Component: ldbedit
30 * Description: utility for ldb database editing
32 * Author: Andrew Tridgell
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
39 static struct ldb_cmdline *options;
42 debug routine
44 static void ldif_write_msg(struct ldb_context *ldb,
45 FILE *f,
46 enum ldb_changetype changetype,
47 struct ldb_message *msg)
49 struct ldb_ldif ldif;
50 ldif.changetype = changetype;
51 ldif.msg = msg;
52 ldb_ldif_write_file(ldb, f, &ldif);
56 modify a database record so msg1 becomes msg2
57 returns the number of modified elements
59 static int modify_record(struct ldb_context *ldb,
60 struct ldb_message *msg1,
61 struct ldb_message *msg2)
63 struct ldb_message *mod;
65 mod = ldb_msg_diff(ldb, msg1, msg2);
66 if (mod == NULL) {
67 fprintf(stderr, "Failed to calculate message differences\n");
68 return -1;
71 if (mod->num_elements == 0) {
72 return 0;
75 if (options->verbose > 0) {
76 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
79 if (ldb_modify(ldb, mod) != 0) {
80 fprintf(stderr, "failed to modify %s - %s\n",
81 ldb_dn_linearize(ldb, msg1->dn), ldb_errstring(ldb));
82 return -1;
85 return mod->num_elements;
89 find dn in msgs[]
91 static struct ldb_message *msg_find(struct ldb_context *ldb,
92 struct ldb_message **msgs,
93 int count,
94 const struct ldb_dn *dn)
96 int i;
97 for (i=0;i<count;i++) {
98 if (ldb_dn_compare(ldb, dn, msgs[i]->dn) == 0) {
99 return msgs[i];
102 return NULL;
106 merge the changes in msgs2 into the messages from msgs1
108 static int merge_edits(struct ldb_context *ldb,
109 struct ldb_message **msgs1, int count1,
110 struct ldb_message **msgs2, int count2)
112 int i;
113 struct ldb_message *msg;
114 int ret = 0;
115 int adds=0, modifies=0, deletes=0;
117 /* do the adds and modifies */
118 for (i=0;i<count2;i++) {
119 msg = msg_find(ldb, msgs1, count1, msgs2[i]->dn);
120 if (!msg) {
121 if (options->verbose > 0) {
122 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_ADD, msgs2[i]);
124 if (ldb_add(ldb, msgs2[i]) != 0) {
125 fprintf(stderr, "failed to add %s - %s\n",
126 ldb_dn_linearize(ldb, msgs2[i]->dn),
127 ldb_errstring(ldb));
128 return -1;
130 adds++;
131 } else {
132 if (modify_record(ldb, msg, msgs2[i]) > 0) {
133 modifies++;
138 /* do the deletes */
139 for (i=0;i<count1;i++) {
140 msg = msg_find(ldb, msgs2, count2, msgs1[i]->dn);
141 if (!msg) {
142 if (options->verbose > 0) {
143 ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_DELETE, msgs1[i]);
145 if (ldb_delete(ldb, msgs1[i]->dn) != 0) {
146 fprintf(stderr, "failed to delete %s - %s\n",
147 ldb_dn_linearize(ldb, msgs1[i]->dn),
148 ldb_errstring(ldb));
149 return -1;
151 deletes++;
155 printf("# %d adds %d modifies %d deletes\n", adds, modifies, deletes);
157 return ret;
161 save a set of messages as ldif to a file
163 static int save_ldif(struct ldb_context *ldb,
164 FILE *f, struct ldb_message **msgs, int count)
166 int i;
168 fprintf(f, "# editing %d records\n", count);
170 for (i=0;i<count;i++) {
171 struct ldb_ldif ldif;
172 fprintf(f, "# record %d\n", i+1);
174 ldif.changetype = LDB_CHANGETYPE_NONE;
175 ldif.msg = msgs[i];
177 ldb_ldif_write_file(ldb, f, &ldif);
180 return 0;
185 edit the ldb search results in msgs using the user selected editor
187 static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int count1,
188 const char *editor)
190 int fd, ret;
191 FILE *f;
192 char file_template[] = "/tmp/ldbedit.XXXXXX";
193 char *cmd;
194 struct ldb_ldif *ldif;
195 struct ldb_message **msgs2 = NULL;
196 int count2 = 0;
198 /* write out the original set of messages to a temporary
199 file */
200 fd = mkstemp(file_template);
202 if (fd == -1) {
203 perror(file_template);
204 return -1;
207 f = fdopen(fd, "r+");
209 if (!f) {
210 perror("fopen");
211 close(fd);
212 unlink(file_template);
213 return -1;
216 if (save_ldif(ldb, f, msgs1, count1) != 0) {
217 return -1;
220 fclose(f);
222 cmd = talloc_asprintf(ldb, "%s %s", editor, file_template);
224 if (!cmd) {
225 unlink(file_template);
226 fprintf(stderr, "out of memory\n");
227 return -1;
230 /* run the editor */
231 ret = system(cmd);
232 talloc_free(cmd);
234 if (ret != 0) {
235 unlink(file_template);
236 fprintf(stderr, "edit with %s failed\n", editor);
237 return -1;
240 /* read the resulting ldif into msgs2 */
241 f = fopen(file_template, "r");
242 if (!f) {
243 perror(file_template);
244 return -1;
247 while ((ldif = ldb_ldif_read_file(ldb, f))) {
248 msgs2 = talloc_realloc(ldb, msgs2, struct ldb_message *, count2+1);
249 if (!msgs2) {
250 fprintf(stderr, "out of memory");
251 return -1;
253 msgs2[count2++] = ldif->msg;
256 fclose(f);
257 unlink(file_template);
259 return merge_edits(ldb, msgs1, count1, msgs2, count2);
262 static void usage(void)
264 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
265 printf("Options:\n");
266 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
267 printf(" -s base|sub|one choose search scope\n");
268 printf(" -b basedn choose baseDN\n");
269 printf(" -a edit all records (expression 'objectclass=*')\n");
270 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
271 printf(" -v verbose mode\n");
272 exit(1);
275 int main(int argc, const char **argv)
277 struct ldb_context *ldb;
278 struct ldb_result *result = NULL;
279 struct ldb_dn *basedn = NULL;
280 int ret;
281 const char *expression = "(|(objectClass=*)(distinguishedName=*))";
282 const char * const * attrs = NULL;
284 ldb_global_init();
286 ldb = ldb_init(NULL);
288 options = ldb_cmdline_process(ldb, argc, argv, usage);
290 /* the check for '=' is for compatibility with ldapsearch */
291 if (options->argc > 0 &&
292 strchr(options->argv[0], '=')) {
293 expression = options->argv[0];
294 options->argv++;
295 options->argc--;
298 if (options->argc > 0) {
299 attrs = (const char * const *)(options->argv);
302 if (options->basedn != NULL) {
303 basedn = ldb_dn_explode(ldb, options->basedn);
304 if (basedn == NULL) {
305 printf("Invalid Base DN format\n");
306 exit(1);
310 ret = ldb_search(ldb, basedn, options->scope, expression, attrs, &result);
311 if (ret != LDB_SUCCESS) {
312 printf("search failed - %s\n", ldb_errstring(ldb));
313 exit(1);
316 if (result->count == 0) {
317 printf("no matching records - cannot edit\n");
318 return 0;
321 do_edit(ldb, result->msgs, result->count, options->editor);
323 if (result) {
324 ret = talloc_free(result);
325 if (ret == -1) {
326 fprintf(stderr, "talloc_free failed\n");
327 exit(1);
331 talloc_free(ldb);
332 return 0;