r3783: - don't use make proto for ldb anymore
[kamenim/samba.git] / source4 / lib / ldb / tools / ldbadd.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: ldbadd
29  *
30  *  Description: utility to add records - modelled on ldapadd
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/ldb.h"
37
38 static int failures;
39
40 static void usage(void)
41 {
42         printf("Usage: ldbadd <options> <ldif...>\n");
43         printf("Options:\n");
44         printf("  -H ldb_url       choose the database (or $LDB_URL)\n");
45         printf("  -o options       pass options like modules to activate\n");
46         printf("              e.g: -o modules:timestamps\n");
47         printf("\n");
48         printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
49         exit(1);
50 }
51
52
53 /*
54   add records from an opened file
55 */
56 static int process_file(struct ldb_context *ldb, FILE *f)
57 {
58         struct ldb_ldif *ldif;
59         int ret, count=0;
60
61         while ((ldif = ldb_ldif_read_file(ldb, f))) {
62                 if (ldif->changetype != LDB_CHANGETYPE_ADD &&
63                     ldif->changetype != LDB_CHANGETYPE_NONE) {
64                         fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n");
65                         break;
66                 }
67
68                 ret = ldb_add(ldb, &ldif->msg);
69                 if (ret != 0) {
70                         fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
71                                 ldb_errstring(ldb), ldif->msg.dn);
72                         failures++;
73                 } else {
74                         count++;
75                 }
76                 ldb_ldif_read_free(ldb, ldif);
77         }
78
79         return count;
80 }
81
82
83
84  int main(int argc, char * const argv[])
85 {
86         struct ldb_context *ldb;
87         int count=0;
88         const char *ldb_url;
89         const char **options = NULL;
90         int ldbopts;
91         int opt, i;
92
93         ldb_url = getenv("LDB_URL");
94
95         ldbopts = 0;
96         while ((opt = getopt(argc, argv, "hH:o:")) != EOF) {
97                 switch (opt) {
98                 case 'H':
99                         ldb_url = optarg;
100                         break;
101
102                 case 'o':
103                         ldbopts++;
104                         if (options == NULL) {
105                                 options = (const char **)malloc(sizeof(char *) * (ldbopts + 1));
106                         } else {
107                                 options = (const char **)realloc(options, sizeof(char *) * (ldbopts + 1));
108                                 if (options == NULL) {
109                                         fprintf(stderr, "Out of memory!\n");
110                                         exit(-1);
111                                 }
112                         }
113                         options[ldbopts - 1] = optarg;
114                         options[ldbopts] = NULL;
115                         break;
116
117                 case 'h':
118                 default:
119                         usage();
120                         break;
121                 }
122         }
123
124         if (!ldb_url) {
125                 fprintf(stderr, "You must specify a ldb URL\n\n");
126                 usage();
127         }
128
129         argc -= optind;
130         argv += optind;
131
132         ldb = ldb_connect(ldb_url, 0, options);
133
134         if (!ldb) {
135                 perror("ldb_connect");
136                 exit(1);
137         }
138
139         ldb_set_debug_stderr(ldb);
140
141         if (argc == 0) {
142                 usage();
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("Added %d records with %d failures\n", count, failures);
165         
166         return 0;
167 }