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
34 #include "ldb_includes.h"
35 #include "tools/cmdline.h"
36 #include "utils/schema_convert.h"
38 #define SCHEMA_UNKNOWN 0
41 #define SCHEMA_STRUCTURAL 3
42 #define SCHEMA_ABSTRACT 4
43 #define SCHEMA_AUXILIARY 5
46 #define SCHEMA_SINGLE_VALUE 8
47 #define SCHEMA_EQUALITY 9
48 #define SCHEMA_ORDERING 10
49 #define SCHEMA_SUBSTR 11
50 #define SCHEMA_SYNTAX 12
51 #define SCHEMA_DESC 13
63 struct ldb_context *ldb_ctx;
64 struct ldb_dn *basedn;
66 static int check_braces(const char *string)
72 if ((c = strchr(string, '(')) == NULL) {
79 if (c == NULL) return 1;
87 static char *skip_spaces(char *string) {
88 return (string + strspn(string, " \t\n"));
91 static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
97 c = skip_spaces(values);
99 n = strcspn(c, " \t$");
100 s = talloc_strndup(msg, c, n);
101 if (ldb_msg_add_string(msg, attr, s) != 0) {
105 c += strspn(c, " \t$");
111 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
112 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
114 static char *get_def_value(TALLOC_CTX *ctx, char **string)
122 n = strcspn(c, "\'");
123 value = talloc_strndup(ctx, c, n);
125 c++; /* skip closing \' */
127 n = strcspn(c, " \t\n");
128 value = talloc_strndup(ctx, c, n);
136 static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
138 char *c = skip_spaces(*string);
140 struct schema_token *token;
143 token = talloc(ctx, struct schema_token);
145 n = strcspn(c, " \t\n");
146 type = talloc_strndup(token, c, n);
150 if (strcasecmp("NAME", type) == 0) {
152 token->type = SCHEMA_NAME;
153 /* we do not support aliases so we get only the first name given and skip others */
155 char *s = strchr(c, ')');
156 if (s == NULL) return NULL;
164 token->value = get_def_value(ctx, &c);
166 if (*string < c) { /* single name */
172 if (strcasecmp("SUP", type) == 0) {
174 token->type = SCHEMA_SUP;
179 token->value = talloc_strndup(ctx, c, n);
183 token->value = get_def_value(ctx, &c);
191 if (strcasecmp("STRUCTURAL", type) == 0) {
193 token->type = SCHEMA_STRUCTURAL;
198 if (strcasecmp("ABSTRACT", type) == 0) {
200 token->type = SCHEMA_ABSTRACT;
205 if (strcasecmp("AUXILIARY", type) == 0) {
207 token->type = SCHEMA_AUXILIARY;
212 if (strcasecmp("MUST", type) == 0) {
214 token->type = SCHEMA_MUST;
219 token->value = talloc_strndup(ctx, c, n);
223 token->value = get_def_value(ctx, &c);
231 if (strcasecmp("MAY", type) == 0) {
233 token->type = SCHEMA_MAY;
238 token->value = talloc_strndup(ctx, c, n);
242 token->value = get_def_value(ctx, &c);
250 if (strcasecmp("SINGLE-VALUE", type) == 0) {
252 token->type = SCHEMA_SINGLE_VALUE;
257 if (strcasecmp("EQUALITY", type) == 0) {
259 token->type = SCHEMA_EQUALITY;
261 token->value = get_def_value(ctx, &c);
268 if (strcasecmp("ORDERING", type) == 0) {
270 token->type = SCHEMA_ORDERING;
272 token->value = get_def_value(ctx, &c);
279 if (strcasecmp("SUBSTR", type) == 0) {
281 token->type = SCHEMA_SUBSTR;
283 token->value = get_def_value(ctx, &c);
290 if (strcasecmp("SYNTAX", type) == 0) {
292 token->type = SCHEMA_SYNTAX;
294 token->value = get_def_value(ctx, &c);
301 if (strcasecmp("DESC", type) == 0) {
303 token->type = SCHEMA_DESC;
305 token->value = get_def_value(ctx, &c);
312 token->type = SCHEMA_UNKNOWN;
319 c = strchr(++c, '\'');
322 c += strcspn(c, " \t\n");
330 static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
333 struct ldb_message *msg;
334 struct schema_token *token;
338 ctx = talloc_new(mem_ctx);
339 msg = ldb_msg_new(ctx);
341 ldb_msg_add_string(msg, "objectClass", "top");
343 c = talloc_strdup(ctx, entry);
350 if (strncmp(c, "attributetype", 13) == 0) {
352 MSG_ADD_STRING("objectClass", "attributeSchema");
357 if (strncmp(c, "objectclass", 11) == 0) {
359 MSG_ADD_STRING("objectClass", "classSchema");
368 if (c == NULL) goto failed;
373 /* get attributeID */
374 n = strcspn(c, " \t");
375 s = talloc_strndup(msg, c, n);
376 MSG_ADD_STRING("attributeID", s);
381 token = get_next_schema_token(msg, &c);
382 if (!token) goto failed;
384 switch (token->type) {
386 MSG_ADD_STRING("cn", token->value);
387 MSG_ADD_STRING("name", token->value);
388 MSG_ADD_STRING("lDAPDisplayName", token->value);
389 msg->dn = ldb_dn_copy(msg, basedn);
390 ldb_dn_add_child_fmt(msg->dn, "CN=%s,CN=Schema,CN=Configuration", token->value);
394 MSG_ADD_M_STRING("subClassOf", token->value);
397 case SCHEMA_STRUCTURAL:
398 MSG_ADD_STRING("objectClassCategory", "1");
401 case SCHEMA_ABSTRACT:
402 MSG_ADD_STRING("objectClassCategory", "2");
405 case SCHEMA_AUXILIARY:
406 MSG_ADD_STRING("objectClassCategory", "3");
410 MSG_ADD_M_STRING("mustContain", token->value);
414 MSG_ADD_M_STRING("mayContain", token->value);
417 case SCHEMA_SINGLE_VALUE:
418 MSG_ADD_STRING("isSingleValued", "TRUE");
421 case SCHEMA_EQUALITY:
425 case SCHEMA_ORDERING:
435 const struct syntax_map *map =
436 find_syntax_map_by_standard_oid(token->value);
440 MSG_ADD_STRING("attributeSyntax", map->AD_OID);
444 MSG_ADD_STRING("description", token->value);
448 fprintf(stderr, "Unknown Definition: %s\n", token->value);
452 talloc_steal(mem_ctx, msg);
461 static struct schema_conv process_file(FILE *in, FILE *out)
464 struct schema_conv ret;
467 struct ldb_ldif ldif;
469 ldif.changetype = LDB_CHANGETYPE_NONE;
471 ctx = talloc_new(NULL);
477 while ((c = fgetc(in)) != EOF) {
479 /* fprintf(stderr, "Parsing line %d\n", line); */
483 } while (c != EOF && c != '\n');
491 entry = talloc_array(ctx, char, 1024);
492 if (entry == NULL) exit(-1);
497 if (check_braces(entry) == 0) {
499 ldif.msg = process_entry(ctx, entry);
500 if (ldif.msg == NULL) {
502 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
505 ldb_ldif_write_file(ldb_ctx, out, &ldif);
513 if ((t % 1023) == 0) {
514 entry = talloc_realloc(ctx, entry, char, t + 1024);
515 if (entry == NULL) exit(-1);
517 } while ((c = fgetc(in)) != EOF);
521 if (check_braces(entry) == 0) {
523 ldif.msg = process_entry(ctx, entry);
524 if (ldif.msg == NULL) {
526 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
529 ldb_ldif_write_file(ldb_ctx, out, &ldif);
531 fprintf(stderr, "malformed entry on line %d\n", line);
542 static void usage(void)
544 printf("Usage: oLschema2ldif -H NONE <options>\n");
545 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
546 printf("Options:\n");
547 printf(" -I inputfile inputfile of OpenLDAP style schema otherwise STDIN\n");
548 printf(" -O outputfile outputfile otherwise STDOUT\n");
549 printf(" -o options pass options like modules to activate\n");
550 printf(" e.g: -o modules:timestamps\n");
552 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
556 int main(int argc, const char **argv)
559 struct schema_conv ret;
560 struct ldb_cmdline *options;
563 ctx = talloc_new(NULL);
564 ldb_ctx = ldb_init(ctx, NULL);
566 setenv("LDB_URL", "NONE", 1);
567 options = ldb_cmdline_process(ldb_ctx, argc, argv, usage);
569 if (options->basedn == NULL) {
570 perror("Base DN not specified");
573 basedn = ldb_dn_new(ctx, ldb_ctx, options->basedn);
574 if ( ! ldb_dn_validate(basedn)) {
575 perror("Malformed Base DN");
580 if (options->input) {
581 in = fopen(options->input, "r");
583 perror(options->input);
587 if (options->output) {
588 out = fopen(options->output, "w");
590 perror(options->output);
595 ret = process_file(in, out);
600 printf("Converted %d records with %d failures\n", ret.count, ret.failures);