r792: - changed the ldb ldif_* functions to be in the ldb_ namespace
[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         int opt, i;
89
90         ldb_url = getenv("LDB_URL");
91
92         while ((opt = getopt(argc, argv, "hH:")) != EOF) {
93                 switch (opt) {
94                 case 'H':
95                         ldb_url = optarg;
96                         break;
97
98                 case 'h':
99                 default:
100                         usage();
101                         break;
102                 }
103         }
104
105         if (!ldb_url) {
106                 fprintf(stderr, "You must specify a ldb URL\n\n");
107                 usage();
108         }
109
110         argc -= optind;
111         argv += optind;
112
113         ldb = ldb_connect(ldb_url, 0, NULL);
114
115         if (!ldb) {
116                 perror("ldb_connect");
117                 exit(1);
118         }
119
120         ldb_set_debug_stderr(ldb);
121
122         if (argc == 0) {
123                 usage();
124                 exit(1);
125         }
126
127         for (i=0;i<argc;i++) {
128                 FILE *f;
129                 if (strcmp(argv[i],"-") == 0) {
130                         f = stdin;
131                 } else {
132                         f = fopen(argv[i], "r");
133                 }
134                 if (!f) {
135                         perror(argv[i]);
136                         exit(1);
137                 }
138                 count += process_file(ldb, f);
139                 if (f != stdin) {
140                         fclose(f);
141                 }
142         }
143
144         ldb_close(ldb);
145
146         printf("Modified %d records with %d failures\n", count, failures);
147
148         if (failures != 0) {
149                 return -1;
150         }
151         
152         return 0;
153 }