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 "ldb/include/includes.h"
36 #include "ldb/tools/cmdline.h"
38 static struct ldb_cmdline
*options
;
43 static void ldif_write_msg(struct ldb_context
*ldb
,
45 enum ldb_changetype changetype
,
46 struct ldb_message
*msg
)
49 ldif
.changetype
= changetype
;
51 ldb_ldif_write_file(ldb
, f
, &ldif
);
55 modify a database record so msg1 becomes msg2
56 returns the number of modified elements
58 static int modify_record(struct ldb_context
*ldb
,
59 struct ldb_message
*msg1
,
60 struct ldb_message
*msg2
)
62 struct ldb_message
*mod
;
64 mod
= ldb_msg_diff(ldb
, msg1
, msg2
);
66 fprintf(stderr
, "Failed to calculate message differences\n");
70 if (mod
->num_elements
== 0) {
74 if (options
->verbose
> 0) {
75 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_MODIFY
, mod
);
78 if (ldb_modify(ldb
, mod
) != 0) {
79 fprintf(stderr
, "failed to modify %s - %s\n",
80 ldb_dn_linearize(ldb
, msg1
->dn
), ldb_errstring(ldb
));
84 return mod
->num_elements
;
90 static struct ldb_message
*msg_find(struct ldb_context
*ldb
,
91 struct ldb_message
**msgs
,
93 const struct ldb_dn
*dn
)
96 for (i
=0;i
<count
;i
++) {
97 if (ldb_dn_compare(ldb
, dn
, msgs
[i
]->dn
) == 0) {
105 merge the changes in msgs2 into the messages from msgs1
107 static int merge_edits(struct ldb_context
*ldb
,
108 struct ldb_message
**msgs1
, int count1
,
109 struct ldb_message
**msgs2
, int count2
)
112 struct ldb_message
*msg
;
114 int adds
=0, modifies
=0, deletes
=0;
116 /* do the adds and modifies */
117 for (i
=0;i
<count2
;i
++) {
118 msg
= msg_find(ldb
, msgs1
, count1
, msgs2
[i
]->dn
);
120 if (options
->verbose
> 0) {
121 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_ADD
, msgs2
[i
]);
123 if (ldb_add(ldb
, msgs2
[i
]) != 0) {
124 fprintf(stderr
, "failed to add %s - %s\n",
125 ldb_dn_linearize(ldb
, msgs2
[i
]->dn
),
131 if (modify_record(ldb
, msg
, msgs2
[i
]) > 0) {
138 for (i
=0;i
<count1
;i
++) {
139 msg
= msg_find(ldb
, msgs2
, count2
, msgs1
[i
]->dn
);
141 if (options
->verbose
> 0) {
142 ldif_write_msg(ldb
, stdout
, LDB_CHANGETYPE_DELETE
, msgs1
[i
]);
144 if (ldb_delete(ldb
, msgs1
[i
]->dn
) != 0) {
145 fprintf(stderr
, "failed to delete %s - %s\n",
146 ldb_dn_linearize(ldb
, msgs1
[i
]->dn
),
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(struct ldb_context
*ldb
,
163 FILE *f
, struct ldb_message
**msgs
, int count
)
167 fprintf(f
, "# editing %d records\n", count
);
169 for (i
=0;i
<count
;i
++) {
170 struct ldb_ldif ldif
;
171 fprintf(f
, "# record %d\n", i
+1);
173 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
176 ldb_ldif_write_file(ldb
, f
, &ldif
);
184 edit the ldb search results in msgs using the user selected editor
186 static int do_edit(struct ldb_context
*ldb
, struct ldb_message
**msgs1
, int count1
,
191 char file_template
[] = "/tmp/ldbedit.XXXXXX";
193 struct ldb_ldif
*ldif
;
194 struct ldb_message
**msgs2
= NULL
;
197 /* write out the original set of messages to a temporary
199 fd
= mkstemp(file_template
);
202 perror(file_template
);
206 f
= fdopen(fd
, "r+");
211 unlink(file_template
);
215 if (save_ldif(ldb
, f
, msgs1
, count1
) != 0) {
221 cmd
= talloc_asprintf(ldb
, "%s %s", editor
, file_template
);
224 unlink(file_template
);
225 fprintf(stderr
, "out of memory\n");
234 unlink(file_template
);
235 fprintf(stderr
, "edit with %s failed\n", editor
);
239 /* read the resulting ldif into msgs2 */
240 f
= fopen(file_template
, "r");
242 perror(file_template
);
246 while ((ldif
= ldb_ldif_read_file(ldb
, f
))) {
247 msgs2
= talloc_realloc(ldb
, msgs2
, struct ldb_message
*, count2
+1);
249 fprintf(stderr
, "out of memory");
252 msgs2
[count2
++] = ldif
->msg
;
256 unlink(file_template
);
258 return merge_edits(ldb
, msgs1
, count1
, msgs2
, count2
);
261 static void usage(void)
263 printf("Usage: ldbedit <options> <expression> <attributes ...>\n");
264 printf("Options:\n");
265 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
266 printf(" -s base|sub|one choose search scope\n");
267 printf(" -b basedn choose baseDN\n");
268 printf(" -a edit all records (expression 'objectclass=*')\n");
269 printf(" -e editor choose editor (or $VISUAL or $EDITOR)\n");
270 printf(" -v verbose mode\n");
274 int main(int argc
, const char **argv
)
276 struct ldb_context
*ldb
;
277 struct ldb_result
*result
= NULL
;
278 struct ldb_dn
*basedn
= NULL
;
280 const char *expression
= "(|(objectClass=*)(distinguishedName=*))";
281 const char * const * attrs
= NULL
;
285 ldb
= ldb_init(NULL
);
287 options
= ldb_cmdline_process(ldb
, argc
, argv
, usage
);
289 /* the check for '=' is for compatibility with ldapsearch */
290 if (options
->argc
> 0 &&
291 strchr(options
->argv
[0], '=')) {
292 expression
= options
->argv
[0];
297 if (options
->argc
> 0) {
298 attrs
= (const char * const *)(options
->argv
);
301 if (options
->basedn
!= NULL
) {
302 basedn
= ldb_dn_explode(ldb
, options
->basedn
);
303 if (basedn
== NULL
) {
304 printf("Invalid Base DN format\n");
309 ret
= ldb_search(ldb
, basedn
, options
->scope
, expression
, attrs
, &result
);
310 if (ret
!= LDB_SUCCESS
) {
311 printf("search failed - %s\n", ldb_errstring(ldb
));
315 if (result
->count
== 0) {
316 printf("no matching records - cannot edit\n");
320 do_edit(ldb
, result
->msgs
, result
->count
, options
->editor
);
323 ret
= talloc_free(result
);
325 fprintf(stderr
, "talloc_free failed\n");