libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.git] / source4 / utils / oLschema2ldif / main.c
1 /*
2    ldb database library
3
4    Copyright (C) Simo Sorce 2005
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: oLschema2ldif
28  *
29  *  Description: utility to convert an OpenLDAP schema into AD LDIF
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "includes.h"
35 #include "./lib.h"
36 #include "lib/cmdline/popt_common.h"
37
38 static struct options {
39         const char *basedn;
40         const char *input;
41         const char *output;
42 } options;
43
44 static struct poptOption popt_options[] = {
45         POPT_AUTOHELP
46         { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
47         { "input", 'I', POPT_ARG_STRING, &options.input, 0,
48           "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
49         { "output", 'O', POPT_ARG_STRING, &options.output, 0,
50           "outputfile otherwise STDOUT", "outputfile"},
51         POPT_COMMON_VERSION
52         {0}
53 };
54
55
56 static void usage(void)
57 {
58         poptContext pc;
59         printf("Usage: oLschema2ldif <options>\n");
60         printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
61         printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
62         pc = poptGetContext("oLschema2ldif", 0, NULL, popt_options,
63                             POPT_CONTEXT_KEEP_FIRST);
64         poptPrintHelp(pc, stdout, 0);
65         exit(1);
66 }
67
68
69  int main(int argc, const char **argv)
70 {
71         TALLOC_CTX *ctx;
72         struct schema_conv ret;
73         poptContext pc;
74         struct conv_options copt;
75         int opt;
76
77         ctx = talloc_new(NULL);
78
79         setenv("LDB_URL", "NONE", 1);
80
81         pc = poptGetContext(argv[0], argc, argv, popt_options,
82                             POPT_CONTEXT_KEEP_FIRST);
83
84         while((opt = poptGetNextOpt(pc)) != -1) {
85                 fprintf(stderr, "Invalid option %s: %s\n",
86                         poptBadOption(pc, 0), poptStrerror(opt));
87                 usage();
88         }
89
90         if (options.basedn == NULL) {
91                 printf("Base DN not specified\n");
92                 usage();
93                 exit(1);
94         }
95
96         copt.in = stdin;
97         copt.out = stdout;
98         copt.ldb_ctx = ldb_init(ctx, NULL);
99
100         copt.basedn = ldb_dn_new(ctx, copt.ldb_ctx, options.basedn);
101         if (!ldb_dn_validate(copt.basedn)) {
102                 printf("Malformed Base DN\n");
103                 usage();
104                 exit(1);
105         }
106
107         if (options.input) {
108                 copt.in = fopen(options.input, "r");
109                 if (!copt.in) {
110                         perror(options.input);
111                         usage();
112                         exit(1);
113                 }
114         }
115         if (options.output) {
116                 copt.out = fopen(options.output, "w");
117                 if (!copt.out) {
118                         perror(options.output);
119                         usage();
120                         exit(1);
121                 }
122         }
123
124         ret = process_file(ctx, &copt);
125
126         fclose(copt.in);
127         fclose(copt.out);
128
129         printf("Converted %d records with %d failures\n", ret.count, ret.failures);
130
131         poptFreeContext(pc);
132
133         return 0;
134 }