ldb: detect eof on ldif files
[amitay/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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldbmodify
28  *
29  *  Description: utility to modify records - modelled on ldapmodify
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "ldb.h"
35 #include "tools/cmdline.h"
36 #include "ldbutil.h"
37
38 static unsigned int failures;
39 static struct ldb_cmdline *options;
40
41 static void usage(struct ldb_context *ldb)
42 {
43         printf("Usage: ldbmodify <options> <ldif...>\n");
44         printf("Modifies a ldb based upon ldif change records\n\n");
45         ldb_cmdline_help(ldb, "ldbmodify", stdout);
46         exit(LDB_ERR_OPERATIONS_ERROR);
47 }
48
49 /*
50   process modifies for one file
51 */
52 static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
53 {
54         struct ldb_ldif *ldif;
55         int ret = LDB_SUCCESS;
56         struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
57
58         if (options->controls != NULL &&  req_ctrls== NULL) {
59                 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
60                 exit(LDB_ERR_OPERATIONS_ERROR);
61         }
62
63         while ((ldif = ldb_ldif_read_file(ldb, f))) {
64                 switch (ldif->changetype) {
65                 case LDB_CHANGETYPE_NONE:
66                 case LDB_CHANGETYPE_ADD:
67                         ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
68                         break;
69                 case LDB_CHANGETYPE_DELETE:
70                         ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls);
71                         break;
72                 case LDB_CHANGETYPE_MODIFY:
73                         ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls);
74                         break;
75                 }
76                 if (ret != LDB_SUCCESS) {
77                         fprintf(stderr, "ERR: (%s) \"%s\" on DN %s\n",
78                                 ldb_strerror(ret),
79                                 ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
80                         failures++;
81                 } else {
82                         (*count)++;
83                         if (options->verbose) {
84                                 printf("Modified %s\n", ldb_dn_get_linearized(ldif->msg->dn));
85                         }
86                 }
87                 ldb_ldif_read_free(ldb, ldif);
88         }
89
90         if (!feof(f)) {
91                 fprintf(stderr, "Failed to parse ldif\n");
92                 return -1;
93         }
94
95         return ret;
96 }
97
98 int main(int argc, const char **argv)
99 {
100         struct ldb_context *ldb;
101         unsigned int i, count = 0;
102         int ret = LDB_SUCCESS;
103         TALLOC_CTX *mem_ctx = talloc_new(NULL);
104
105         ldb = ldb_init(mem_ctx, NULL);
106         if (ldb == NULL) {
107                 return LDB_ERR_OPERATIONS_ERROR;
108         }
109
110         options = ldb_cmdline_process(ldb, argc, argv, usage);
111
112         if (options->argc == 0) {
113                 ret = process_file(ldb, stdin, &count);
114         } else {
115                 for (i=0;i<options->argc;i++) {
116                         const char *fname = options->argv[i];
117                         FILE *f;
118                         f = fopen(fname, "r");
119                         if (!f) {
120                                 perror(fname);
121                                 return LDB_ERR_OPERATIONS_ERROR;
122                         }
123                         ret = process_file(ldb, f, &count);
124                         fclose(f);
125                 }
126         }
127
128         talloc_free(mem_ctx);
129
130         printf("Modified %u records with %u failures\n", count, failures);
131
132         return ret;
133 }