summaryrefslogtreecommitdiffstats
path: root/source4/utils/oLschema2ldif/main.c
blob: c312298d623488931715094a7d9545636ed03315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
   ldb database library

   Copyright (C) Simo Sorce 2005

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Name: ldb
 *
 *  Component: oLschema2ldif
 *
 *  Description: utility to convert an OpenLDAP schema into AD LDIF
 *
 *  Author: Simo Sorce
 */

#include "includes.h"
#include "./lib.h"
#include "lib/cmdline/cmdline.h"

static struct options {
	const char *basedn;
	const char *input;
	const char *output;
} options;

static void usage(void)
{
	printf("Usage: oLschema2ldif [OPTIONS]\n");
	printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
	printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
	exit(1);
}


 int main(int argc, const char **argv)
{
	TALLOC_CTX *ctx;
	struct schema_conv ret;
	poptContext pc;
	struct conv_options copt;
	int opt;

	struct poptOption popt_options[] = {
		POPT_AUTOHELP
		{ "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
		{ "input", 'I', POPT_ARG_STRING, &options.input, 0,
		  "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
		{ "output", 'O', POPT_ARG_STRING, &options.output, 0,
		  "outputfile otherwise STDOUT", "outputfile"},
		POPT_COMMON_VERSION
		POPT_TABLEEND
	};

	ctx = talloc_new(NULL);
	if (ctx == NULL) {
		exit(ENOMEM);
	}

	setenv("LDB_URL", "NONE", 1);

	pc = samba_popt_get_context(getprogname(),
				    argc,
				    argv,
				    popt_options,
				    POPT_CONTEXT_KEEP_FIRST);
	if (pc == NULL) {
		DBG_ERR("Failed to setup popt context!\n");
		TALLOC_FREE(ctx);
		exit(1);
	}

	while((opt = poptGetNextOpt(pc)) != -1) {
		fprintf(stderr, "Invalid option %s: %s\n",
			poptBadOption(pc, 0), poptStrerror(opt));
		usage();
	}

	if (options.basedn == NULL) {
		printf("Base DN not specified\n");
		usage();
		exit(1);
	}

	copt.in = stdin;
	copt.out = stdout;
	copt.ldb_ctx = ldb_init(ctx, NULL);

	copt.basedn = ldb_dn_new(ctx, copt.ldb_ctx, options.basedn);
	if (!ldb_dn_validate(copt.basedn)) {
		printf("Malformed Base DN\n");
		usage();
		exit(1);
	}

	if (options.input) {
		copt.in = fopen(options.input, "r");
		if (!copt.in) {
			perror(options.input);
			usage();
			exit(1);
		}
	}
	if (options.output) {
		copt.out = fopen(options.output, "w");
		if (!copt.out) {
			perror(options.output);
			usage();
			exit(1);
		}
	}

	ret = process_file(ctx, &copt);

	fclose(copt.in);
	fclose(copt.out);

	printf("Converted %d records with %d failures\n", ret.count, ret.failures);

	poptFreeContext(pc);

	return 0;
}