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
36 #include "tools/cmdline.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "../lib/crypto/sha256.h"
39 #include "../librpc/gen_ndr/ndr_misc.h"
40 #include "lib/cmdline/popt_common.h"
42 #define SCHEMA_UNKNOWN 0
45 #define SCHEMA_STRUCTURAL 3
46 #define SCHEMA_ABSTRACT 4
47 #define SCHEMA_AUXILIARY 5
50 #define SCHEMA_SINGLE_VALUE 8
51 #define SCHEMA_EQUALITY 9
52 #define SCHEMA_ORDERING 10
53 #define SCHEMA_SUBSTR 11
54 #define SCHEMA_SYNTAX 12
55 #define SCHEMA_DESC 13
67 struct ldb_context *ldb_ctx;
68 struct ldb_dn *basedn;
70 static int check_braces(const char *string)
76 if ((c = strchr(string, '(')) == NULL) {
83 if (c == NULL) return 1;
87 if (*(c - 1) != ' ' && c && (*(c + 1) == '\0')) {
96 static char *skip_spaces(char *string) {
97 return (string + strspn(string, " \t\n"));
100 static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
106 c = skip_spaces(values);
108 n = strcspn(c, " \t$");
109 s = talloc_strndup(msg, c, n);
110 if (ldb_msg_add_string(msg, attr, s) != 0) {
114 c += strspn(c, " \t$");
120 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
121 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
123 static char *get_def_value(TALLOC_CTX *ctx, char **string)
131 n = strcspn(c, "\'");
132 value = talloc_strndup(ctx, c, n);
134 c++; /* skip closing \' */
136 n = strcspn(c, " \t\n");
137 value = talloc_strndup(ctx, c, n);
145 static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
147 char *c = skip_spaces(*string);
149 struct schema_token *token;
152 token = talloc(ctx, struct schema_token);
154 n = strcspn(c, " \t\n");
155 type = talloc_strndup(token, c, n);
159 if (strcasecmp("NAME", type) == 0) {
161 token->type = SCHEMA_NAME;
162 /* we do not support aliases so we get only the first name given and skip others */
164 char *s = strchr(c, ')');
165 if (s == NULL) return NULL;
173 token->value = get_def_value(ctx, &c);
175 if (*string < c) { /* single name */
181 if (strcasecmp("SUP", type) == 0) {
183 token->type = SCHEMA_SUP;
188 token->value = talloc_strndup(ctx, c, n);
192 token->value = get_def_value(ctx, &c);
200 if (strcasecmp("STRUCTURAL", type) == 0) {
202 token->type = SCHEMA_STRUCTURAL;
207 if (strcasecmp("ABSTRACT", type) == 0) {
209 token->type = SCHEMA_ABSTRACT;
214 if (strcasecmp("AUXILIARY", type) == 0) {
216 token->type = SCHEMA_AUXILIARY;
221 if (strcasecmp("MUST", type) == 0) {
223 token->type = SCHEMA_MUST;
228 token->value = talloc_strndup(ctx, c, n);
232 token->value = get_def_value(ctx, &c);
240 if (strcasecmp("MAY", type) == 0) {
242 token->type = SCHEMA_MAY;
247 token->value = talloc_strndup(ctx, c, n);
251 token->value = get_def_value(ctx, &c);
259 if (strcasecmp("SINGLE-VALUE", type) == 0) {
261 token->type = SCHEMA_SINGLE_VALUE;
266 if (strcasecmp("EQUALITY", type) == 0) {
268 token->type = SCHEMA_EQUALITY;
270 token->value = get_def_value(ctx, &c);
277 if (strcasecmp("ORDERING", type) == 0) {
279 token->type = SCHEMA_ORDERING;
281 token->value = get_def_value(ctx, &c);
288 if (strcasecmp("SUBSTR", type) == 0) {
290 token->type = SCHEMA_SUBSTR;
292 token->value = get_def_value(ctx, &c);
299 if (strcasecmp("SYNTAX", type) == 0) {
301 token->type = SCHEMA_SYNTAX;
303 token->value = get_def_value(ctx, &c);
310 if (strcasecmp("DESC", type) == 0) {
312 token->type = SCHEMA_DESC;
314 token->value = get_def_value(ctx, &c);
321 token->type = SCHEMA_UNKNOWN;
328 c = strchr(++c, '\'');
331 c += strcspn(c, " \t\n");
339 static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
342 struct ldb_message *msg;
343 struct schema_token *token;
347 SHA256_CTX sha256_context;
348 uint8_t digest[SHA256_DIGEST_LENGTH];
352 bool isAttribute = false;
353 bool single_valued = false;
355 ctx = talloc_new(mem_ctx);
356 msg = ldb_msg_new(ctx);
358 ldb_msg_add_string(msg, "objectClass", "top");
360 c = talloc_strdup(ctx, entry);
367 if (strncmp(c, "attributetype", 13) == 0) {
369 MSG_ADD_STRING("objectClass", "attributeSchema");
375 if (strncmp(c, "objectclass", 11) == 0) {
377 MSG_ADD_STRING("objectClass", "classSchema");
386 if (c == NULL) goto failed;
391 /* get attributeID */
392 n = strcspn(c, " \t");
393 s = talloc_strndup(msg, c, n);
395 MSG_ADD_STRING("attributeID", s);
397 MSG_ADD_STRING("governsID", s);
400 samba_SHA256_Init(&sha256_context);
401 samba_SHA256_Update(&sha256_context, (uint8_t*)s, strlen(s));
402 samba_SHA256_Final(digest, &sha256_context);
404 memcpy(&guid, digest, sizeof(struct GUID));
406 if (dsdb_msg_add_guid(msg, &guid, "schemaIdGuid") != 0) {
414 token = get_next_schema_token(msg, &c);
415 if (!token) goto failed;
417 switch (token->type) {
419 MSG_ADD_STRING("cn", token->value);
420 MSG_ADD_STRING("name", token->value);
421 MSG_ADD_STRING("lDAPDisplayName", token->value);
422 msg->dn = ldb_dn_copy(msg, basedn);
423 ldb_dn_add_child_fmt(msg->dn, "CN=%s,CN=Schema,CN=Configuration", token->value);
427 MSG_ADD_M_STRING("subClassOf", token->value);
430 case SCHEMA_STRUCTURAL:
431 MSG_ADD_STRING("objectClassCategory", "1");
434 case SCHEMA_ABSTRACT:
435 MSG_ADD_STRING("objectClassCategory", "2");
438 case SCHEMA_AUXILIARY:
439 MSG_ADD_STRING("objectClassCategory", "3");
443 MSG_ADD_M_STRING("mustContain", token->value);
447 MSG_ADD_M_STRING("mayContain", token->value);
450 case SCHEMA_SINGLE_VALUE:
451 single_valued = true;
454 case SCHEMA_EQUALITY:
458 case SCHEMA_ORDERING:
469 const struct dsdb_syntax *map;
472 n = strcspn(token->value, "{");
473 syntax_oid = talloc_strndup(ctx, token->value, n);
475 map = find_syntax_map_by_standard_oid(syntax_oid);
480 MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
482 oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
483 MSG_ADD_STRING("oMSyntax", oMSyntax);
488 MSG_ADD_STRING("description", token->value);
492 fprintf(stderr, "Unknown Definition: %s\n", token->value);
497 MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
499 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
502 talloc_steal(mem_ctx, msg);
511 static struct schema_conv process_file(FILE *in, FILE *out)
514 struct schema_conv ret;
517 struct ldb_ldif ldif;
519 ldif.changetype = LDB_CHANGETYPE_NONE;
521 ctx = talloc_new(NULL);
527 while ((c = fgetc(in)) != EOF) {
529 /* fprintf(stderr, "Parsing line %d\n", line); */
533 } while (c != EOF && c != '\n');
541 entry = talloc_array(ctx, char, 1024);
542 if (entry == NULL) exit(-1);
548 ret2 = check_braces(entry);
551 ldif.msg = process_entry(ctx, entry);
552 if (ldif.msg == NULL) {
554 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
557 ldb_ldif_write_file(ldb_ctx, out, &ldif);
561 fprintf(stderr, "Invalid entry %s, closing braces needs to be preceeded by a space\n", entry);
570 if ((t % 1023) == 0) {
571 entry = talloc_realloc(ctx, entry, char, t + 1024);
572 if (entry == NULL) exit(-1);
574 } while ((c = fgetc(in)) != EOF);
578 if (check_braces(entry) == 0) {
580 ldif.msg = process_entry(ctx, entry);
581 if (ldif.msg == NULL) {
583 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
586 ldb_ldif_write_file(ldb_ctx, out, &ldif);
588 fprintf(stderr, "malformed entry on line %d\n", line);
599 static struct options {
605 static struct poptOption popt_options[] = {
607 { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
608 { "input", 'I', POPT_ARG_STRING, &options.input, 0,
609 "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
610 { "output", 'O', POPT_ARG_STRING, &options.output, 0,
611 "outputfile otherwise STDOUT", "outputfile"},
617 static void usage(void)
620 printf("Usage: oLschema2ldif <options>\n");
621 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
622 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
623 pc = poptGetContext("oLschema2ldif", 0, NULL, popt_options,
624 POPT_CONTEXT_KEEP_FIRST);
625 poptPrintHelp(pc, stdout, 0);
630 int main(int argc, const char **argv)
633 struct schema_conv ret;
639 ctx = talloc_new(NULL);
640 ldb_ctx = ldb_init(ctx, NULL);
642 setenv("LDB_URL", "NONE", 1);
644 pc = poptGetContext(argv[0], argc, argv, popt_options,
645 POPT_CONTEXT_KEEP_FIRST);
647 while((opt = poptGetNextOpt(pc)) != -1) {
648 fprintf(stderr, "Invalid option %s: %s\n",
649 poptBadOption(pc, 0), poptStrerror(opt));
653 if (options.basedn == NULL) {
654 printf("Base DN not specified\n");
658 basedn = ldb_dn_new(ctx, ldb_ctx, options.basedn);
659 if ( ! ldb_dn_validate(basedn)) {
660 printf("Malformed Base DN\n");
667 in = fopen(options.input, "r");
669 perror(options.input);
674 if (options.output) {
675 out = fopen(options.output, "w");
677 perror(options.output);
683 ret = process_file(in, out);
688 printf("Converted %d records with %d failures\n", ret.count, ret.failures);