4
Copyright (C) Simo Sorce 2005
6
** NOTE! The following LGPL license applies to the ldb
7
** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
27
* Component: oLschema2ldif
29
* Description: utility to convert an OpenLDAP schema into AD LDIF
35
#include "ldb/include/includes.h"
36
#include "ldb/tools/cmdline.h"
37
#include "ldb/tools/convert.h"
39
#define SCHEMA_UNKNOWN 0
42
#define SCHEMA_STRUCTURAL 3
43
#define SCHEMA_ABSTRACT 4
44
#define SCHEMA_AUXILIARY 5
47
#define SCHEMA_SINGLE_VALUE 8
48
#define SCHEMA_EQUALITY 9
49
#define SCHEMA_ORDERING 10
50
#define SCHEMA_SUBSTR 11
51
#define SCHEMA_SYNTAX 12
52
#define SCHEMA_DESC 13
64
struct ldb_context *ldb_ctx;
65
struct ldb_dn *basedn;
67
static int check_braces(const char *string)
73
if ((c = strchr(string, '(')) == NULL) {
80
if (c == NULL) return 1;
88
static char *skip_spaces(char *string) {
89
return (string + strspn(string, " \t\n"));
92
static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
98
c = skip_spaces(values);
100
n = strcspn(c, " \t$");
101
s = talloc_strndup(msg, c, n);
102
if (ldb_msg_add_string(msg, attr, s) != 0) {
106
c += strspn(c, " \t$");
112
#define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
113
#define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
115
static char *get_def_value(TALLOC_CTX *ctx, char **string)
123
n = strcspn(c, "\'");
124
value = talloc_strndup(ctx, c, n);
126
c++; /* skip closing \' */
128
n = strcspn(c, " \t\n");
129
value = talloc_strndup(ctx, c, n);
137
static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
139
char *c = skip_spaces(*string);
141
struct schema_token *token;
144
token = talloc(ctx, struct schema_token);
146
n = strcspn(c, " \t\n");
147
type = talloc_strndup(token, c, n);
151
if (strcasecmp("NAME", type) == 0) {
153
token->type = SCHEMA_NAME;
154
/* we do not support aliases so we get only the first name given and skip others */
156
char *s = strchr(c, ')');
157
if (s == NULL) return NULL;
165
token->value = get_def_value(ctx, &c);
167
if (*string < c) { /* single name */
173
if (strcasecmp("SUP", type) == 0) {
175
token->type = SCHEMA_SUP;
180
token->value = talloc_strndup(ctx, c, n);
184
token->value = get_def_value(ctx, &c);
192
if (strcasecmp("STRUCTURAL", type) == 0) {
194
token->type = SCHEMA_STRUCTURAL;
199
if (strcasecmp("ABSTRACT", type) == 0) {
201
token->type = SCHEMA_ABSTRACT;
206
if (strcasecmp("AUXILIARY", type) == 0) {
208
token->type = SCHEMA_AUXILIARY;
213
if (strcasecmp("MUST", type) == 0) {
215
token->type = SCHEMA_MUST;
220
token->value = talloc_strndup(ctx, c, n);
224
token->value = get_def_value(ctx, &c);
232
if (strcasecmp("MAY", type) == 0) {
234
token->type = SCHEMA_MAY;
239
token->value = talloc_strndup(ctx, c, n);
243
token->value = get_def_value(ctx, &c);
251
if (strcasecmp("SINGLE-VALUE", type) == 0) {
253
token->type = SCHEMA_SINGLE_VALUE;
258
if (strcasecmp("EQUALITY", type) == 0) {
260
token->type = SCHEMA_EQUALITY;
262
token->value = get_def_value(ctx, &c);
269
if (strcasecmp("ORDERING", type) == 0) {
271
token->type = SCHEMA_ORDERING;
273
token->value = get_def_value(ctx, &c);
280
if (strcasecmp("SUBSTR", type) == 0) {
282
token->type = SCHEMA_SUBSTR;
284
token->value = get_def_value(ctx, &c);
291
if (strcasecmp("SYNTAX", type) == 0) {
293
token->type = SCHEMA_SYNTAX;
295
token->value = get_def_value(ctx, &c);
302
if (strcasecmp("DESC", type) == 0) {
304
token->type = SCHEMA_DESC;
306
token->value = get_def_value(ctx, &c);
313
token->type = SCHEMA_UNKNOWN;
320
c = strchr(++c, '\'');
323
c += strcspn(c, " \t\n");
331
static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
334
struct ldb_message *msg;
335
struct schema_token *token;
339
ctx = talloc_new(mem_ctx);
340
msg = ldb_msg_new(ctx);
342
ldb_msg_add_string(msg, "objectClass", "top");
344
c = talloc_strdup(ctx, entry);
351
if (strncmp(c, "attributetype", 13) == 0) {
353
MSG_ADD_STRING("objectClass", "attributeSchema");
358
if (strncmp(c, "objectclass", 11) == 0) {
360
MSG_ADD_STRING("objectClass", "classSchema");
369
if (c == NULL) goto failed;
374
/* get attributeID */
375
n = strcspn(c, " \t");
376
s = talloc_strndup(msg, c, n);
377
MSG_ADD_STRING("attributeID", s);
382
token = get_next_schema_token(msg, &c);
383
if (!token) goto failed;
385
switch (token->type) {
387
MSG_ADD_STRING("cn", token->value);
388
MSG_ADD_STRING("name", token->value);
389
MSG_ADD_STRING("lDAPDisplayName", token->value);
390
msg->dn = ldb_dn_string_compose(msg, basedn,
391
"CN=%s,CN=Schema,CN=Configuration",
396
MSG_ADD_M_STRING("subClassOf", token->value);
399
case SCHEMA_STRUCTURAL:
400
MSG_ADD_STRING("objectClassCategory", "1");
403
case SCHEMA_ABSTRACT:
404
MSG_ADD_STRING("objectClassCategory", "2");
407
case SCHEMA_AUXILIARY:
408
MSG_ADD_STRING("objectClassCategory", "3");
412
MSG_ADD_M_STRING("mustContain", token->value);
416
MSG_ADD_M_STRING("mayContain", token->value);
419
case SCHEMA_SINGLE_VALUE:
420
MSG_ADD_STRING("isSingleValued", "TRUE");
423
case SCHEMA_EQUALITY:
427
case SCHEMA_ORDERING:
437
const struct syntax_map *map =
438
find_syntax_map_by_standard_oid(token->value);
442
MSG_ADD_STRING("attributeSyntax", map->AD_OID);
446
MSG_ADD_STRING("description", token->value);
450
fprintf(stderr, "Unknown Definition: %s\n", token->value);
454
talloc_steal(mem_ctx, msg);
463
static struct schema_conv process_file(FILE *in, FILE *out)
466
struct schema_conv ret;
469
struct ldb_ldif ldif;
471
ldif.changetype = LDB_CHANGETYPE_NONE;
473
ctx = talloc_new(NULL);
479
while ((c = fgetc(in)) != EOF) {
481
/* fprintf(stderr, "Parsing line %d\n", line); */
485
} while (c != EOF && c != '\n');
493
entry = talloc_array(ctx, char, 1024);
494
if (entry == NULL) exit(-1);
499
if (check_braces(entry) == 0) {
501
ldif.msg = process_entry(ctx, entry);
502
if (ldif.msg == NULL) {
504
fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
507
ldb_ldif_write_file(ldb_ctx, out, &ldif);
515
if ((t % 1023) == 0) {
516
entry = talloc_realloc(ctx, entry, char, t + 1024);
517
if (entry == NULL) exit(-1);
519
} while ((c = fgetc(in)) != EOF);
523
if (check_braces(entry) == 0) {
525
ldif.msg = process_entry(ctx, entry);
526
if (ldif.msg == NULL) {
528
fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
531
ldb_ldif_write_file(ldb_ctx, out, &ldif);
533
fprintf(stderr, "malformed entry on line %d\n", line);
544
static void usage(void)
546
printf("Usage: oLschema2ldif -H NONE <options>\n");
547
printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
548
printf("Options:\n");
549
printf(" -I inputfile inputfile of OpenLDAP style schema otherwise STDIN\n");
550
printf(" -O outputfile outputfile otherwise STDOUT\n");
551
printf(" -o options pass options like modules to activate\n");
552
printf(" e.g: -o modules:timestamps\n");
554
printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
558
int main(int argc, const char **argv)
561
struct schema_conv ret;
562
struct ldb_cmdline *options;
567
ctx = talloc_new(NULL);
568
ldb_ctx = ldb_init(ctx);
570
setenv("LDB_URL", "NONE", 1);
571
options = ldb_cmdline_process(ldb_ctx, argc, argv, usage);
573
if (options->basedn == NULL) {
574
perror("Base DN not specified");
577
basedn = ldb_dn_explode(ctx, options->basedn);
578
if (basedn == NULL) {
579
perror("Malformed Base DN");
584
if (options->input) {
585
in = fopen(options->input, "r");
587
perror(options->input);
591
if (options->output) {
592
out = fopen(options->output, "w");
594
perror(options->output);
599
ret = process_file(in, out);
604
printf("Converted %d records with %d failures\n", ret.count, ret.failures);