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"
41 #define SCHEMA_UNKNOWN 0
44 #define SCHEMA_STRUCTURAL 3
45 #define SCHEMA_ABSTRACT 4
46 #define SCHEMA_AUXILIARY 5
49 #define SCHEMA_SINGLE_VALUE 8
50 #define SCHEMA_EQUALITY 9
51 #define SCHEMA_ORDERING 10
52 #define SCHEMA_SUBSTR 11
53 #define SCHEMA_SYNTAX 12
54 #define SCHEMA_DESC 13
66 struct ldb_context *ldb_ctx;
67 struct ldb_dn *basedn;
69 static int check_braces(const char *string)
75 if ((c = strchr(string, '(')) == NULL) {
82 if (c == NULL) return 1;
90 static char *skip_spaces(char *string) {
91 return (string + strspn(string, " \t\n"));
94 static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
100 c = skip_spaces(values);
102 n = strcspn(c, " \t$");
103 s = talloc_strndup(msg, c, n);
104 if (ldb_msg_add_string(msg, attr, s) != 0) {
108 c += strspn(c, " \t$");
114 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
115 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
117 static char *get_def_value(TALLOC_CTX *ctx, char **string)
125 n = strcspn(c, "\'");
126 value = talloc_strndup(ctx, c, n);
128 c++; /* skip closing \' */
130 n = strcspn(c, " \t\n");
131 value = talloc_strndup(ctx, c, n);
139 static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
141 char *c = skip_spaces(*string);
143 struct schema_token *token;
146 token = talloc(ctx, struct schema_token);
148 n = strcspn(c, " \t\n");
149 type = talloc_strndup(token, c, n);
153 if (strcasecmp("NAME", type) == 0) {
155 token->type = SCHEMA_NAME;
156 /* we do not support aliases so we get only the first name given and skip others */
158 char *s = strchr(c, ')');
159 if (s == NULL) return NULL;
167 token->value = get_def_value(ctx, &c);
169 if (*string < c) { /* single name */
175 if (strcasecmp("SUP", type) == 0) {
177 token->type = SCHEMA_SUP;
182 token->value = talloc_strndup(ctx, c, n);
186 token->value = get_def_value(ctx, &c);
194 if (strcasecmp("STRUCTURAL", type) == 0) {
196 token->type = SCHEMA_STRUCTURAL;
201 if (strcasecmp("ABSTRACT", type) == 0) {
203 token->type = SCHEMA_ABSTRACT;
208 if (strcasecmp("AUXILIARY", type) == 0) {
210 token->type = SCHEMA_AUXILIARY;
215 if (strcasecmp("MUST", type) == 0) {
217 token->type = SCHEMA_MUST;
222 token->value = talloc_strndup(ctx, c, n);
226 token->value = get_def_value(ctx, &c);
234 if (strcasecmp("MAY", type) == 0) {
236 token->type = SCHEMA_MAY;
241 token->value = talloc_strndup(ctx, c, n);
245 token->value = get_def_value(ctx, &c);
253 if (strcasecmp("SINGLE-VALUE", type) == 0) {
255 token->type = SCHEMA_SINGLE_VALUE;
260 if (strcasecmp("EQUALITY", type) == 0) {
262 token->type = SCHEMA_EQUALITY;
264 token->value = get_def_value(ctx, &c);
271 if (strcasecmp("ORDERING", type) == 0) {
273 token->type = SCHEMA_ORDERING;
275 token->value = get_def_value(ctx, &c);
282 if (strcasecmp("SUBSTR", type) == 0) {
284 token->type = SCHEMA_SUBSTR;
286 token->value = get_def_value(ctx, &c);
293 if (strcasecmp("SYNTAX", type) == 0) {
295 token->type = SCHEMA_SYNTAX;
297 token->value = get_def_value(ctx, &c);
304 if (strcasecmp("DESC", type) == 0) {
306 token->type = SCHEMA_DESC;
308 token->value = get_def_value(ctx, &c);
315 token->type = SCHEMA_UNKNOWN;
322 c = strchr(++c, '\'');
325 c += strcspn(c, " \t\n");
333 static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
336 struct ldb_message *msg;
337 struct schema_token *token;
341 SHA256_CTX sha256_context;
342 uint8_t digest[SHA256_DIGEST_LENGTH];
346 bool isAttribute = false;
347 bool single_valued = false;
349 ctx = talloc_new(mem_ctx);
350 msg = ldb_msg_new(ctx);
352 ldb_msg_add_string(msg, "objectClass", "top");
354 c = talloc_strdup(ctx, entry);
361 if (strncmp(c, "attributetype", 13) == 0) {
363 MSG_ADD_STRING("objectClass", "attributeSchema");
369 if (strncmp(c, "objectclass", 11) == 0) {
371 MSG_ADD_STRING("objectClass", "classSchema");
380 if (c == NULL) goto failed;
385 /* get attributeID */
386 n = strcspn(c, " \t");
387 s = talloc_strndup(msg, c, n);
389 MSG_ADD_STRING("attributeID", s);
391 MSG_ADD_STRING("governsID", s);
394 SHA256_Init(&sha256_context);
395 SHA256_Update(&sha256_context, (uint8_t*)s, strlen(s));
396 SHA256_Final(digest, &sha256_context);
398 memcpy(&guid, digest, sizeof(struct GUID));
400 if (dsdb_msg_add_guid(msg, &guid, "schemaIdGuid") != 0) {
408 token = get_next_schema_token(msg, &c);
409 if (!token) goto failed;
411 switch (token->type) {
413 MSG_ADD_STRING("cn", token->value);
414 MSG_ADD_STRING("name", token->value);
415 MSG_ADD_STRING("lDAPDisplayName", token->value);
416 msg->dn = ldb_dn_copy(msg, basedn);
417 ldb_dn_add_child_fmt(msg->dn, "CN=%s,CN=Schema,CN=Configuration", token->value);
421 MSG_ADD_M_STRING("subClassOf", token->value);
424 case SCHEMA_STRUCTURAL:
425 MSG_ADD_STRING("objectClassCategory", "1");
428 case SCHEMA_ABSTRACT:
429 MSG_ADD_STRING("objectClassCategory", "2");
432 case SCHEMA_AUXILIARY:
433 MSG_ADD_STRING("objectClassCategory", "3");
437 MSG_ADD_M_STRING("mustContain", token->value);
441 MSG_ADD_M_STRING("mayContain", token->value);
444 case SCHEMA_SINGLE_VALUE:
445 single_valued = true;
448 case SCHEMA_EQUALITY:
452 case SCHEMA_ORDERING:
463 const struct dsdb_syntax *map;
466 n = strcspn(token->value, "{");
467 syntax_oid = talloc_strndup(ctx, token->value, n);
469 map = find_syntax_map_by_standard_oid(syntax_oid);
474 MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
476 oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
477 MSG_ADD_STRING("oMSyntax", oMSyntax);
482 MSG_ADD_STRING("description", token->value);
486 fprintf(stderr, "Unknown Definition: %s\n", token->value);
491 MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
493 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
496 talloc_steal(mem_ctx, msg);
505 static struct schema_conv process_file(FILE *in, FILE *out)
508 struct schema_conv ret;
511 struct ldb_ldif ldif;
513 ldif.changetype = LDB_CHANGETYPE_NONE;
515 ctx = talloc_new(NULL);
521 while ((c = fgetc(in)) != EOF) {
523 /* fprintf(stderr, "Parsing line %d\n", line); */
527 } while (c != EOF && c != '\n');
535 entry = talloc_array(ctx, char, 1024);
536 if (entry == NULL) exit(-1);
541 if (check_braces(entry) == 0) {
543 ldif.msg = process_entry(ctx, entry);
544 if (ldif.msg == NULL) {
546 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
549 ldb_ldif_write_file(ldb_ctx, out, &ldif);
557 if ((t % 1023) == 0) {
558 entry = talloc_realloc(ctx, entry, char, t + 1024);
559 if (entry == NULL) exit(-1);
561 } while ((c = fgetc(in)) != EOF);
565 if (check_braces(entry) == 0) {
567 ldif.msg = process_entry(ctx, entry);
568 if (ldif.msg == NULL) {
570 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
573 ldb_ldif_write_file(ldb_ctx, out, &ldif);
575 fprintf(stderr, "malformed entry on line %d\n", line);
586 static void usage(void)
588 printf("Usage: oLschema2ldif -H NONE <options>\n");
589 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
590 printf("Options:\n");
591 printf(" -I inputfile inputfile of OpenLDAP style schema otherwise STDIN\n");
592 printf(" -O outputfile outputfile otherwise STDOUT\n");
593 printf(" -o options pass options like modules to activate\n");
594 printf(" e.g: -o modules:timestamps\n");
596 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
600 int main(int argc, const char **argv)
603 struct schema_conv ret;
604 struct ldb_cmdline *options;
607 ctx = talloc_new(NULL);
608 ldb_ctx = ldb_init(ctx, NULL);
610 setenv("LDB_URL", "NONE", 1);
611 options = ldb_cmdline_process(ldb_ctx, argc, argv, usage);
613 if (options->basedn == NULL) {
614 perror("Base DN not specified");
617 basedn = ldb_dn_new(ctx, ldb_ctx, options->basedn);
618 if ( ! ldb_dn_validate(basedn)) {
619 perror("Malformed Base DN");
624 if (options->input) {
625 in = fopen(options->input, "r");
627 perror(options->input);
631 if (options->output) {
632 out = fopen(options->output, "w");
634 perror(options->output);
639 ret = process_file(in, out);
644 printf("Converted %d records with %d failures\n", ret.count, ret.failures);