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;
91 static char *skip_spaces(char *string
) {
92 return (string
+ strspn(string
, " \t\n"));
95 static int add_multi_string(struct ldb_message
*msg
, const char *attr
, char *values
)
101 c
= skip_spaces(values
);
103 n
= strcspn(c
, " \t$");
104 s
= talloc_strndup(msg
, c
, n
);
105 if (ldb_msg_add_string(msg
, attr
, s
) != 0) {
109 c
+= strspn(c
, " \t$");
115 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
116 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
118 static char *get_def_value(TALLOC_CTX
*ctx
, char **string
)
126 n
= strcspn(c
, "\'");
127 value
= talloc_strndup(ctx
, c
, n
);
129 c
++; /* skip closing \' */
131 n
= strcspn(c
, " \t\n");
132 value
= talloc_strndup(ctx
, c
, n
);
140 static struct schema_token
*get_next_schema_token(TALLOC_CTX
*ctx
, char **string
)
142 char *c
= skip_spaces(*string
);
144 struct schema_token
*token
;
147 token
= talloc(ctx
, struct schema_token
);
149 n
= strcspn(c
, " \t\n");
150 type
= talloc_strndup(token
, c
, n
);
154 if (strcasecmp("NAME", type
) == 0) {
156 token
->type
= SCHEMA_NAME
;
157 /* we do not support aliases so we get only the first name given and skip others */
159 char *s
= strchr(c
, ')');
160 if (s
== NULL
) return NULL
;
168 token
->value
= get_def_value(ctx
, &c
);
170 if (*string
< c
) { /* single name */
176 if (strcasecmp("SUP", type
) == 0) {
178 token
->type
= SCHEMA_SUP
;
183 token
->value
= talloc_strndup(ctx
, c
, n
);
187 token
->value
= get_def_value(ctx
, &c
);
195 if (strcasecmp("STRUCTURAL", type
) == 0) {
197 token
->type
= SCHEMA_STRUCTURAL
;
202 if (strcasecmp("ABSTRACT", type
) == 0) {
204 token
->type
= SCHEMA_ABSTRACT
;
209 if (strcasecmp("AUXILIARY", type
) == 0) {
211 token
->type
= SCHEMA_AUXILIARY
;
216 if (strcasecmp("MUST", type
) == 0) {
218 token
->type
= SCHEMA_MUST
;
223 token
->value
= talloc_strndup(ctx
, c
, n
);
227 token
->value
= get_def_value(ctx
, &c
);
235 if (strcasecmp("MAY", type
) == 0) {
237 token
->type
= SCHEMA_MAY
;
242 token
->value
= talloc_strndup(ctx
, c
, n
);
246 token
->value
= get_def_value(ctx
, &c
);
254 if (strcasecmp("SINGLE-VALUE", type
) == 0) {
256 token
->type
= SCHEMA_SINGLE_VALUE
;
261 if (strcasecmp("EQUALITY", type
) == 0) {
263 token
->type
= SCHEMA_EQUALITY
;
265 token
->value
= get_def_value(ctx
, &c
);
272 if (strcasecmp("ORDERING", type
) == 0) {
274 token
->type
= SCHEMA_ORDERING
;
276 token
->value
= get_def_value(ctx
, &c
);
283 if (strcasecmp("SUBSTR", type
) == 0) {
285 token
->type
= SCHEMA_SUBSTR
;
287 token
->value
= get_def_value(ctx
, &c
);
294 if (strcasecmp("SYNTAX", type
) == 0) {
296 token
->type
= SCHEMA_SYNTAX
;
298 token
->value
= get_def_value(ctx
, &c
);
305 if (strcasecmp("DESC", type
) == 0) {
307 token
->type
= SCHEMA_DESC
;
309 token
->value
= get_def_value(ctx
, &c
);
316 token
->type
= SCHEMA_UNKNOWN
;
323 c
= strchr(++c
, '\'');
326 c
+= strcspn(c
, " \t\n");
334 static struct ldb_message
*process_entry(TALLOC_CTX
*mem_ctx
, const char *entry
)
337 struct ldb_message
*msg
;
338 struct schema_token
*token
;
342 SHA256_CTX sha256_context
;
343 uint8_t digest
[SHA256_DIGEST_LENGTH
];
347 bool isAttribute
= false;
348 bool single_valued
= false;
350 ctx
= talloc_new(mem_ctx
);
351 msg
= ldb_msg_new(ctx
);
353 ldb_msg_add_string(msg
, "objectClass", "top");
355 c
= talloc_strdup(ctx
, entry
);
362 if (strncmp(c
, "attributetype", 13) == 0) {
364 MSG_ADD_STRING("objectClass", "attributeSchema");
370 if (strncmp(c
, "objectclass", 11) == 0) {
372 MSG_ADD_STRING("objectClass", "classSchema");
381 if (c
== NULL
) goto failed
;
386 /* get attributeID */
387 n
= strcspn(c
, " \t");
388 s
= talloc_strndup(msg
, c
, n
);
390 MSG_ADD_STRING("attributeID", s
);
392 MSG_ADD_STRING("governsID", s
);
395 SHA256_Init(&sha256_context
);
396 SHA256_Update(&sha256_context
, (uint8_t*)s
, strlen(s
));
397 SHA256_Final(digest
, &sha256_context
);
399 memcpy(&guid
, digest
, sizeof(struct GUID
));
401 if (dsdb_msg_add_guid(msg
, &guid
, "schemaIdGuid") != 0) {
409 token
= get_next_schema_token(msg
, &c
);
410 if (!token
) goto failed
;
412 switch (token
->type
) {
414 MSG_ADD_STRING("cn", token
->value
);
415 MSG_ADD_STRING("name", token
->value
);
416 MSG_ADD_STRING("lDAPDisplayName", token
->value
);
417 msg
->dn
= ldb_dn_copy(msg
, basedn
);
418 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=Schema,CN=Configuration", token
->value
);
422 MSG_ADD_M_STRING("subClassOf", token
->value
);
425 case SCHEMA_STRUCTURAL
:
426 MSG_ADD_STRING("objectClassCategory", "1");
429 case SCHEMA_ABSTRACT
:
430 MSG_ADD_STRING("objectClassCategory", "2");
433 case SCHEMA_AUXILIARY
:
434 MSG_ADD_STRING("objectClassCategory", "3");
438 MSG_ADD_M_STRING("mustContain", token
->value
);
442 MSG_ADD_M_STRING("mayContain", token
->value
);
445 case SCHEMA_SINGLE_VALUE
:
446 single_valued
= true;
449 case SCHEMA_EQUALITY
:
453 case SCHEMA_ORDERING
:
464 const struct dsdb_syntax
*map
;
467 n
= strcspn(token
->value
, "{");
468 syntax_oid
= talloc_strndup(ctx
, token
->value
, n
);
470 map
= find_syntax_map_by_standard_oid(syntax_oid
);
475 MSG_ADD_STRING("attributeSyntax", map
->attributeSyntax_oid
);
477 oMSyntax
= talloc_asprintf(msg
, "%d", map
->oMSyntax
);
478 MSG_ADD_STRING("oMSyntax", oMSyntax
);
483 MSG_ADD_STRING("description", token
->value
);
487 fprintf(stderr
, "Unknown Definition: %s\n", token
->value
);
492 MSG_ADD_STRING("isSingleValued", single_valued
? "TRUE" : "FALSE");
494 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg
->dn
));
497 talloc_steal(mem_ctx
, msg
);
506 static struct schema_conv
process_file(FILE *in
, FILE *out
)
509 struct schema_conv ret
;
512 struct ldb_ldif ldif
;
514 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
516 ctx
= talloc_new(NULL
);
522 while ((c
= fgetc(in
)) != EOF
) {
524 /* fprintf(stderr, "Parsing line %d\n", line); */
528 } while (c
!= EOF
&& c
!= '\n');
536 entry
= talloc_array(ctx
, char, 1024);
537 if (entry
== NULL
) exit(-1);
542 if (check_braces(entry
) == 0) {
544 ldif
.msg
= process_entry(ctx
, entry
);
545 if (ldif
.msg
== NULL
) {
547 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
550 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
558 if ((t
% 1023) == 0) {
559 entry
= talloc_realloc(ctx
, entry
, char, t
+ 1024);
560 if (entry
== NULL
) exit(-1);
562 } while ((c
= fgetc(in
)) != EOF
);
566 if (check_braces(entry
) == 0) {
568 ldif
.msg
= process_entry(ctx
, entry
);
569 if (ldif
.msg
== NULL
) {
571 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
574 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
576 fprintf(stderr
, "malformed entry on line %d\n", line
);
587 static struct options
{
593 static struct poptOption popt_options
[] = {
595 { "basedn", 'b', POPT_ARG_STRING
, &options
.basedn
, 0, "base DN", "DN" },
596 { "input", 'I', POPT_ARG_STRING
, &options
.input
, 0,
597 "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
598 { "output", 'O', POPT_ARG_STRING
, &options
.output
, 0,
599 "outputfile otherwise STDOUT", "outputfile"},
605 static void usage(void)
608 printf("Usage: oLschema2ldif <options>\n");
609 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
610 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
611 pc
= poptGetContext("oLschema2ldif", 0, NULL
, popt_options
,
612 POPT_CONTEXT_KEEP_FIRST
);
613 poptPrintHelp(pc
, stdout
, 0);
618 int main(int argc
, const char **argv
)
621 struct schema_conv ret
;
627 ctx
= talloc_new(NULL
);
628 ldb_ctx
= ldb_init(ctx
, NULL
);
630 setenv("LDB_URL", "NONE", 1);
632 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
,
633 POPT_CONTEXT_KEEP_FIRST
);
635 while((opt
= poptGetNextOpt(pc
)) != -1) {
636 fprintf(stderr
, "Invalid option %s: %s\n",
637 poptBadOption(pc
, 0), poptStrerror(opt
));
641 if (options
.basedn
== NULL
) {
642 printf("Base DN not specified\n");
646 basedn
= ldb_dn_new(ctx
, ldb_ctx
, options
.basedn
);
647 if ( ! ldb_dn_validate(basedn
)) {
648 printf("Malformed Base DN\n");
655 in
= fopen(options
.input
, "r");
657 perror(options
.input
);
662 if (options
.output
) {
663 out
= fopen(options
.output
, "w");
665 perror(options
.output
);
671 ret
= process_file(in
, out
);
676 printf("Converted %d records with %d failures\n", ret
.count
, ret
.failures
);