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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: oLschema2ldif
30 * Description: utility to convert an OpenLDAP schema into AD LDIF
36 #include "ldb/include/includes.h"
37 #include "ldb/tools/cmdline.h"
38 #include "ldb/tools/convert.h"
40 #define SCHEMA_UNKNOWN 0
43 #define SCHEMA_STRUCTURAL 3
44 #define SCHEMA_ABSTRACT 4
45 #define SCHEMA_AUXILIARY 5
48 #define SCHEMA_SINGLE_VALUE 8
49 #define SCHEMA_EQUALITY 9
50 #define SCHEMA_ORDERING 10
51 #define SCHEMA_SUBSTR 11
52 #define SCHEMA_SYNTAX 12
53 #define SCHEMA_DESC 13
65 struct ldb_context *ldb_ctx;
66 struct ldb_dn *basedn;
68 static int check_braces(const char *string)
74 if ((c = strchr(string, '(')) == NULL) {
81 if (c == NULL) return 1;
89 static char *skip_spaces(char *string) {
90 return (string + strspn(string, " \t\n"));
93 static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
99 c = skip_spaces(values);
101 n = strcspn(c, " \t$");
102 s = talloc_strndup(msg, c, n);
103 if (ldb_msg_add_string(msg, attr, s) != 0) {
107 c += strspn(c, " \t$");
113 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
114 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
116 static char *get_def_value(TALLOC_CTX *ctx, char **string)
124 n = strcspn(c, "\'");
125 value = talloc_strndup(ctx, c, n);
127 c++; /* skip closing \' */
129 n = strcspn(c, " \t\n");
130 value = talloc_strndup(ctx, c, n);
138 static struct token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
140 char *c = skip_spaces(*string);
145 token = talloc(ctx, struct token);
147 n = strcspn(c, " \t\n");
148 type = talloc_strndup(token, c, n);
152 if (strcasecmp("NAME", type) == 0) {
154 token->type = SCHEMA_NAME;
155 /* we do not support aliases so we get only the first name given and skip others */
157 char *s = strchr(c, ')');
158 if (s == NULL) return NULL;
166 token->value = get_def_value(ctx, &c);
168 if (*string < c) { /* single name */
174 if (strcasecmp("SUP", type) == 0) {
176 token->type = SCHEMA_SUP;
181 token->value = talloc_strndup(ctx, c, n);
185 token->value = get_def_value(ctx, &c);
193 if (strcasecmp("STRUCTURAL", type) == 0) {
195 token->type = SCHEMA_STRUCTURAL;
200 if (strcasecmp("ABSTRACT", type) == 0) {
202 token->type = SCHEMA_ABSTRACT;
207 if (strcasecmp("AUXILIARY", type) == 0) {
209 token->type = SCHEMA_AUXILIARY;
214 if (strcasecmp("MUST", type) == 0) {
216 token->type = SCHEMA_MUST;
221 token->value = talloc_strndup(ctx, c, n);
225 token->value = get_def_value(ctx, &c);
233 if (strcasecmp("MAY", type) == 0) {
235 token->type = SCHEMA_MAY;
240 token->value = talloc_strndup(ctx, c, n);
244 token->value = get_def_value(ctx, &c);
252 if (strcasecmp("SINGLE-VALUE", type) == 0) {
254 token->type = SCHEMA_SINGLE_VALUE;
259 if (strcasecmp("EQUALITY", type) == 0) {
261 token->type = SCHEMA_EQUALITY;
263 token->value = get_def_value(ctx, &c);
270 if (strcasecmp("ORDERING", type) == 0) {
272 token->type = SCHEMA_ORDERING;
274 token->value = get_def_value(ctx, &c);
281 if (strcasecmp("SUBSTR", type) == 0) {
283 token->type = SCHEMA_SUBSTR;
285 token->value = get_def_value(ctx, &c);
292 if (strcasecmp("SYNTAX", type) == 0) {
294 token->type = SCHEMA_SYNTAX;
296 token->value = get_def_value(ctx, &c);
303 if (strcasecmp("DESC", type) == 0) {
305 token->type = SCHEMA_DESC;
307 token->value = get_def_value(ctx, &c);
314 token->type = SCHEMA_UNKNOWN;
321 c = strchr(++c, '\'');
324 c += strcspn(c, " \t\n");
332 static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
335 struct ldb_message *msg;
340 ctx = talloc_new(mem_ctx);
341 msg = ldb_msg_new(ctx);
343 ldb_msg_add_string(msg, "objectClass", "top");
345 c = talloc_strdup(ctx, entry);
352 if (strncmp(c, "attributetype", 13) == 0) {
354 MSG_ADD_STRING("objectClass", "attributeSchema");
359 if (strncmp(c, "objectclass", 11) == 0) {
361 MSG_ADD_STRING("objectClass", "classSchema");
370 if (c == NULL) goto failed;
375 /* get attributeID */
376 n = strcspn(c, " \t");
377 s = talloc_strndup(msg, c, n);
378 MSG_ADD_STRING("attributeID", s);
383 token = get_next_schema_token(msg, &c);
384 if (!token) goto failed;
386 switch (token->type) {
388 MSG_ADD_STRING("cn", token->value);
389 MSG_ADD_STRING("name", token->value);
390 MSG_ADD_STRING("lDAPDisplayName", token->value);
391 msg->dn = ldb_dn_string_compose(msg, basedn,
392 "CN=%s,CN=Schema,CN=Configuration",
397 MSG_ADD_M_STRING("subClassOf", token->value);
400 case SCHEMA_STRUCTURAL:
401 MSG_ADD_STRING("objectClassCategory", "1");
404 case SCHEMA_ABSTRACT:
405 MSG_ADD_STRING("objectClassCategory", "2");
408 case SCHEMA_AUXILIARY:
409 MSG_ADD_STRING("objectClassCategory", "3");
413 MSG_ADD_M_STRING("mustContain", token->value);
417 MSG_ADD_M_STRING("mayContain", token->value);
420 case SCHEMA_SINGLE_VALUE:
421 MSG_ADD_STRING("isSingleValued", "TRUE");
424 case SCHEMA_EQUALITY:
428 case SCHEMA_ORDERING:
438 const struct syntax_map *map =
439 find_syntax_map_by_standard_oid(token->value);
443 MSG_ADD_STRING("attributeSyntax", map->AD_OID);
447 MSG_ADD_STRING("description", token->value);
451 fprintf(stderr, "Unknown Definition: %s\n", token->value);
455 talloc_steal(mem_ctx, msg);
464 static struct schema_conv process_file(FILE *in, FILE *out)
467 struct schema_conv ret;
470 struct ldb_ldif ldif;
472 ldif.changetype = LDB_CHANGETYPE_NONE;
474 ctx = talloc_new(NULL);
480 while ((c = fgetc(in)) != EOF) {
482 /* fprintf(stderr, "Parsing line %d\n", line); */
486 } while (c != EOF && c != '\n');
494 entry = talloc_array(ctx, char, 1024);
495 if (entry == NULL) exit(-1);
500 if (check_braces(entry) == 0) {
502 ldif.msg = process_entry(ctx, entry);
503 if (ldif.msg == NULL) {
505 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
508 ldb_ldif_write_file(ldb_ctx, out, &ldif);
516 if ((t % 1023) == 0) {
517 entry = talloc_realloc(ctx, entry, char, t + 1024);
518 if (entry == NULL) exit(-1);
520 } while ((c = fgetc(in)) != EOF);
524 if (check_braces(entry) == 0) {
526 ldif.msg = process_entry(ctx, entry);
527 if (ldif.msg == NULL) {
529 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
532 ldb_ldif_write_file(ldb_ctx, out, &ldif);
534 fprintf(stderr, "malformed entry on line %d\n", line);
545 static void usage(void)
547 printf("Usage: oLschema2ldif -H NONE <options>\n");
548 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
549 printf("Options:\n");
550 printf(" -I inputfile inputfile of OpenLDAP style schema otherwise STDIN\n");
551 printf(" -O outputfile outputfile otherwise STDOUT\n");
552 printf(" -o options pass options like modules to activate\n");
553 printf(" e.g: -o modules:timestamps\n");
555 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
559 int main(int argc, const char **argv)
562 struct schema_conv ret;
563 struct ldb_cmdline *options;
568 ctx = talloc_new(NULL);
569 ldb_ctx = ldb_init(ctx);
571 setenv("LDB_URL", "NONE", 1);
572 options = ldb_cmdline_process(ldb_ctx, argc, argv, usage);
574 if (options->basedn == NULL) {
575 perror("Base DN not specified");
578 basedn = ldb_dn_explode(ctx, options->basedn);
579 if (basedn == NULL) {
580 perror("Malformed Base DN");
585 if (options->input) {
586 in = fopen(options->input, "r");
588 perror(options->input);
592 if (options->output) {
593 out = fopen(options->output, "w");
595 perror(options->output);
600 ret = process_file(in, out);
605 printf("Converted %d records with %d failures\n", ret.count, ret.failures);