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
28 * Component: ldbmodify
30 * Description: utility to modify records - modelled on ldapmodify
32 * Author: Andrew Tridgell
39 static void usage(void)
41 printf("Usage: ldbmodify <options> <ldif...>\n");
43 printf(" -H ldb_url choose the database (or $LDB_URL)\n");
45 printf("Modifies a ldb based upon ldif change records\n\n");
50 process modifies for one file
52 static int process_file(struct ldb_context *ldb, FILE *f)
54 struct ldb_ldif *ldif;
55 int ret = -1, count = 0;
57 while ((ldif = ldb_ldif_read_file(ldb, f))) {
58 switch (ldif->changetype) {
59 case LDB_CHANGETYPE_NONE:
60 case LDB_CHANGETYPE_ADD:
61 ret = ldb_add(ldb, &ldif->msg);
63 case LDB_CHANGETYPE_DELETE:
64 ret = ldb_delete(ldb, ldif->msg.dn);
66 case LDB_CHANGETYPE_MODIFY:
67 ret = ldb_modify(ldb, &ldif->msg);
71 fprintf(stderr, "ERR: \"%s\" on DN %s\n",
72 ldb_errstring(ldb), ldif->msg.dn);
77 ldb_ldif_read_free(ldb, ldif);
83 int main(int argc, char * const argv[])
85 struct ldb_context *ldb;
88 const char **options = NULL;
92 ldb_url = getenv("LDB_URL");
95 while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
103 if (options == NULL) {
104 options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
106 options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
107 if (options == NULL) {
108 fprintf(stderr, "Out of memory!\n");
112 options[ldbopts - 1] = optarg;
113 options[ldbopts] = NULL;
124 fprintf(stderr, "You must specify a ldb URL\n\n");
131 ldb = ldb_connect(ldb_url, 0, options);
134 perror("ldb_connect");
138 ldb_set_debug_stderr(ldb);
145 for (i=0;i<argc;i++) {
147 if (strcmp(argv[i],"-") == 0) {
150 f = fopen(argv[i], "r");
156 count += process_file(ldb, f);
164 printf("Modified %d records with %d failures\n", count, failures);