r3754: merge in ldb modules support from the tmp branch ldbPlugins
[kai/samba.git] / source4 / lib / ldb / tools / ldbmodify.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
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
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldbmodify
29  *
30  *  Description: utility to modify records - modelled on ldapmodify
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36
37 static int failures;
38
39 static void usage(void)
40 {
41         printf("Usage: ldbmodify <options> <ldif...>\n");
42         printf("Options:\n");
43         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
44         printf("\n");
45         printf("Modifies a ldb based upon ldif change records\n\n");
46         exit(1);
47 }
48
49 /*
50   process modifies for one file
51 */
52 static int process_file(struct ldb_context *ldb, FILE *f)
53 {
54         struct ldb_ldif *ldif;
55         int ret = -1, count = 0;
56         
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);
62                         break;
63                 case LDB_CHANGETYPE_DELETE:
64                         ret = ldb_delete(ldb, ldif->msg.dn);
65                         break;
66                 case LDB_CHANGETYPE_MODIFY:
67                         ret = ldb_modify(ldb, &ldif->msg);
68                         break;
69                 }
70                 if (ret != 0) {
71                         fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
72                                 ldb_errstring(ldb), ldif->msg.dn);
73                         failures++;
74                 } else {
75                         count++;
76                 }
77                 ldb_ldif_read_free(ldb, ldif);
78         }
79
80         return count;
81 }
82
83  int main(int argc, char * const argv[])
84 {
85         struct ldb_context *ldb;
86         int count=0;
87         const char *ldb_url;
88         const char **options = NULL;
89         int ldbopts;
90         int opt, i;
91
92         ldb_url = getenv("LDB_URL");
93
94         ldbopts = 0;
95         while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
96                 switch (opt) {
97                 case 'H':
98                         ldb_url = optarg;
99                         break;
100
101                 case 'o':
102                         ldbopts++;
103                         if (options == NULL) {
104                                 options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
105                         } else {
106                                 options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
107                                 if (options == NULL) {
108                                         fprintf(stderr, "Out of memory!\n");
109                                         exit(-1);
110                                 }
111                         }
112                         options[ldbopts - 1] = optarg;
113                         options[ldbopts] = NULL;
114                         break;
115
116                 case 'h':
117                 default:
118                         usage();
119                         break;
120                 }
121         }
122
123         if (!ldb_url) {
124                 fprintf(stderr, "You must specify a ldb URL\n\n");
125                 usage();
126         }
127
128         argc -= optind;
129         argv += optind;
130
131         ldb = ldb_connect(ldb_url, 0, options);
132
133         if (!ldb) {
134                 perror("ldb_connect");
135                 exit(1);
136         }
137
138         ldb_set_debug_stderr(ldb);
139
140         if (argc == 0) {
141                 usage();
142                 exit(1);
143         }
144
145         for (i=0;i<argc;i++) {
146                 FILE *f;
147                 if (strcmp(argv[i],"-") == 0) {
148                         f = stdin;
149                 } else {
150                         f = fopen(argv[i], "r");
151                 }
152                 if (!f) {
153                         perror(argv[i]);
154                         exit(1);
155                 }
156                 count += process_file(ldb, f);
157                 if (f != stdin) {
158                         fclose(f);
159                 }
160         }
161
162         ldb_close(ldb);
163
164         printf("Modified %d records with %d failures\n", count, failures);
165
166         if (failures != 0) {
167                 return -1;
168         }
169         
170         return 0;
171 }