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 3 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, see <http://www.gnu.org/licenses/>.
29 * Description: utility for ldb database editing
31 * Author: Andrew Tridgell
35 #include "system/filesys.h"
36 #include "system/time.h"
37 #include "system/filesys.h"
39 #include "tools/cmdline.h"
40 #include "tools/ldbutil.h"
42 static struct ldb_cmdline
*options
;
47 static void ldif_write_msg(struct ldb_context
*ldb
,
49 enum ldb_changetype changetype
,
50 struct ldb_message
*msg
)
53 ldif
.changetype
= changetype
;
55 ldb_ldif_write_file(ldb
, f
, &ldif
);
59 modify a database record so msg1 becomes msg2
60 returns the number of modified elements
62 static int modify_record(struct ldb_context
*ldb
,
63 struct ldb_message
*msg1
,
64 struct ldb_message
*msg2
,
65 struct ldb_control
**req_ctrls
)
68 struct ldb_message
*mod
;
70 if (ldb_msg_difference(ldb
, ldb
, msg1
, msg2
, &mod
) != LDB_SUCCESS
) {
71 fprintf(stderr
, "Failed to calculate message differences\n");
75 ret
= mod
->num_elements
;
80 if (options
->verbose
> 0) {
81 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_MODIFY
, mod
);
84 if (ldb_modify_ctrl(ldb
, mod
, req_ctrls
) != LDB_SUCCESS
) {
85 fprintf(stderr
, "failed to modify %s - %s\n",
86 ldb_dn_get_linearized(msg1
->dn
), ldb_errstring(ldb
));
99 static struct ldb_message
*msg_find(struct ldb_context
*ldb
,
100 struct ldb_message
**msgs
,
105 for (i
=0;i
<count
;i
++) {
106 if (ldb_dn_compare(dn
, msgs
[i
]->dn
) == 0) {
114 merge the changes in msgs2 into the messages from msgs1
116 static int merge_edits(struct ldb_context
*ldb
,
117 struct ldb_message
**msgs1
, unsigned int count1
,
118 struct ldb_message
**msgs2
, unsigned int count2
)
121 struct ldb_message
*msg
;
123 unsigned int adds
=0, modifies
=0, deletes
=0;
124 struct ldb_control
**req_ctrls
= ldb_parse_control_strings(ldb
, ldb
, (const char **)options
->controls
);
125 if (options
->controls
!= NULL
&& req_ctrls
== NULL
) {
126 fprintf(stderr
, "parsing controls failed: %s\n", ldb_errstring(ldb
));
130 if (ldb_transaction_start(ldb
) != LDB_SUCCESS
) {
131 fprintf(stderr
, "Failed to start transaction: %s\n", ldb_errstring(ldb
));
135 /* do the adds and modifies */
136 for (i
=0;i
<count2
;i
++) {
137 msg
= msg_find(ldb
, msgs1
, count1
, msgs2
[i
]->dn
);
139 if (options
->verbose
> 0) {
140 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_ADD
, msgs2
[i
]);
142 if (ldb_add_ctrl(ldb
, msgs2
[i
], req_ctrls
) != LDB_SUCCESS
) {
143 fprintf(stderr
, "failed to add %s - %s\n",
144 ldb_dn_get_linearized(msgs2
[i
]->dn
),
146 ldb_transaction_cancel(ldb
);
151 ret
= modify_record(ldb
, msg
, msgs2
[i
], req_ctrls
);
153 modifies
+= (unsigned int) ret
;
155 ldb_transaction_cancel(ldb
);
162 for (i
=0;i
<count1
;i
++) {
163 msg
= msg_find(ldb
, msgs2
, count2
, msgs1
[i
]->dn
);
165 if (options
->verbose
> 0) {
166 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_DELETE
, msgs1
[i
]);
168 if (ldb_delete_ctrl(ldb
, msgs1
[i
]->dn
, req_ctrls
) != LDB_SUCCESS
) {
169 fprintf(stderr
, "failed to delete %s - %s\n",
170 ldb_dn_get_linearized(msgs1
[i
]->dn
),
172 ldb_transaction_cancel(ldb
);
179 if (ldb_transaction_commit(ldb
) != LDB_SUCCESS
) {
180 fprintf(stderr
, "Failed to commit transaction: %s\n", ldb_errstring(ldb
));
184 printf("# %u adds %u modifies %u deletes\n", adds
, modifies
, deletes
);
190 save a set of messages as ldif to a file
192 static int save_ldif(struct ldb_context
*ldb
,
193 FILE *f
, struct ldb_message
**msgs
, unsigned int count
)
197 fprintf(f
, "# editing %u records\n", count
);
199 for (i
=0;i
<count
;i
++) {
200 struct ldb_ldif ldif
;
201 fprintf(f
, "# record %u\n", i
+1);
203 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
206 ldb_ldif_write_file(ldb
, f
, &ldif
);
214 edit the ldb search results in msgs using the user selected editor
216 static int do_edit(struct ldb_context
*ldb
, struct ldb_message
**msgs1
,
217 unsigned int count1
, const char *editor
)
221 char file_template
[] = "/tmp/ldbedit.XXXXXX";
223 struct ldb_ldif
*ldif
;
224 struct ldb_message
**msgs2
= NULL
;
225 unsigned int count2
= 0;
227 /* write out the original set of messages to a temporary
229 fd
= mkstemp(file_template
);
232 perror(file_template
);
236 f
= fdopen(fd
, "r+");
241 unlink(file_template
);
245 if (save_ldif(ldb
, f
, msgs1
, count1
) != 0) {
251 cmd
= talloc_asprintf(ldb
, "%s %s", editor
, file_template
);
254 unlink(file_template
);
255 fprintf(stderr
, "out of memory\n");
264 unlink(file_template
);
265 fprintf(stderr
, "edit with %s failed\n", editor
);
269 /* read the resulting ldif into msgs2 */
270 f
= fopen(file_template
, "r");
272 perror(file_template
);
276 while ((ldif
= ldb_ldif_read_file(ldb
, f
))) {
277 msgs2
= talloc_realloc(ldb
, msgs2
, struct ldb_message
*, count2
+1);
279 fprintf(stderr
, "out of memory");
282 msgs2
[count2
++] = ldif
->msg
;
285 /* the feof() test works here, even for the last line of the
286 * file, as we parse ldif files character by character, and
287 * feof() is only true if we have failed to read a character
288 * from the file. So if the last line is bad, we don't get
289 * feof() set, so we know the record was bad. Only if we
290 * attempt to go to the next record will we get feof() and
291 * thus consider that the ldif has ended without errors
294 fprintf(stderr
, "Error parsing ldif - aborting\n");
296 unlink(file_template
);
301 unlink(file_template
);
303 return merge_edits(ldb
, msgs1
, count1
, msgs2
, count2
);
306 static void usage(struct ldb_context
*ldb
)
308 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
309 ldb_cmdline_help(ldb
, "ldbedit", stdout
);
310 exit(LDB_ERR_OPERATIONS_ERROR
);
313 int main(int argc
, const char **argv
)
315 struct ldb_context
*ldb
;
316 struct ldb_result
*result
= NULL
;
317 struct ldb_dn
*basedn
= NULL
;
319 const char *expression
= "(|(objectClass=*)(distinguishedName=*))";
320 const char * const * attrs
= NULL
;
321 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
322 struct ldb_control
**req_ctrls
;
324 ldb
= ldb_init(mem_ctx
, NULL
);
326 return LDB_ERR_OPERATIONS_ERROR
;
329 options
= ldb_cmdline_process(ldb
, argc
, argv
, usage
);
331 /* the check for '=' is for compatibility with ldapsearch */
332 if (options
->argc
> 0 &&
333 strchr(options
->argv
[0], '=')) {
334 expression
= options
->argv
[0];
339 if (options
->argc
> 0) {
340 attrs
= (const char * const *)(options
->argv
);
343 if (options
->basedn
!= NULL
) {
344 basedn
= ldb_dn_new(ldb
, ldb
, options
->basedn
);
345 if (basedn
== NULL
) {
346 return LDB_ERR_OPERATIONS_ERROR
;
350 req_ctrls
= ldb_parse_control_strings(ldb
, ldb
, (const char **)options
->controls
);
351 if (options
->controls
!= NULL
&& req_ctrls
== NULL
) {
352 printf("parsing controls failed: %s\n", ldb_errstring(ldb
));
353 return LDB_ERR_OPERATIONS_ERROR
;
356 ret
= ldb_search_ctrl(ldb
, ldb
, &result
, basedn
, options
->scope
, attrs
, req_ctrls
, "%s", expression
);
357 if (ret
!= LDB_SUCCESS
) {
358 printf("search failed - %s\n", ldb_errstring(ldb
));
362 if (result
->count
== 0) {
363 printf("no matching records - cannot edit\n");
364 talloc_free(mem_ctx
);
368 ret
= do_edit(ldb
, result
->msgs
, result
->count
, options
->editor
);
370 talloc_free(mem_ctx
);
372 return ret
== 0 ? LDB_SUCCESS
: LDB_ERR_OPERATIONS_ERROR
;