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 "dsdb/samdb/samdb.h"
37 #include "../lib/crypto/sha256.h"
38 #include "../librpc/gen_ndr/ndr_misc.h"
39 #include "lib/cmdline/popt_common.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;
86 if (*(c
- 1) != ' ' && c
&& (*(c
+ 1) == '\0')) {
95 static char *skip_spaces(char *string
) {
96 return (string
+ strspn(string
, " \t\n"));
99 static int add_multi_string(struct ldb_message
*msg
, const char *attr
, char *values
)
105 c
= skip_spaces(values
);
107 n
= strcspn(c
, " \t$");
108 s
= talloc_strndup(msg
, c
, n
);
109 if (ldb_msg_add_string(msg
, attr
, s
) != 0) {
113 c
+= strspn(c
, " \t$");
119 #define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
120 #define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
122 static char *get_def_value(TALLOC_CTX
*ctx
, char **string
)
130 n
= strcspn(c
, "\'");
131 value
= talloc_strndup(ctx
, c
, n
);
133 c
++; /* skip closing \' */
135 n
= strcspn(c
, " \t\n");
136 value
= talloc_strndup(ctx
, c
, n
);
144 static struct schema_token
*get_next_schema_token(TALLOC_CTX
*ctx
, char **string
)
146 char *c
= skip_spaces(*string
);
148 struct schema_token
*token
;
151 token
= talloc(ctx
, struct schema_token
);
153 n
= strcspn(c
, " \t\n");
154 type
= talloc_strndup(token
, c
, n
);
158 if (strcasecmp("NAME", type
) == 0) {
160 token
->type
= SCHEMA_NAME
;
161 /* we do not support aliases so we get only the first name given and skip others */
163 char *s
= strchr(c
, ')');
164 if (s
== NULL
) return NULL
;
172 token
->value
= get_def_value(ctx
, &c
);
174 if (*string
< c
) { /* single name */
180 if (strcasecmp("SUP", type
) == 0) {
182 token
->type
= SCHEMA_SUP
;
187 token
->value
= talloc_strndup(ctx
, c
, n
);
191 token
->value
= get_def_value(ctx
, &c
);
199 if (strcasecmp("STRUCTURAL", type
) == 0) {
201 token
->type
= SCHEMA_STRUCTURAL
;
206 if (strcasecmp("ABSTRACT", type
) == 0) {
208 token
->type
= SCHEMA_ABSTRACT
;
213 if (strcasecmp("AUXILIARY", type
) == 0) {
215 token
->type
= SCHEMA_AUXILIARY
;
220 if (strcasecmp("MUST", type
) == 0) {
222 token
->type
= SCHEMA_MUST
;
227 token
->value
= talloc_strndup(ctx
, c
, n
);
231 token
->value
= get_def_value(ctx
, &c
);
239 if (strcasecmp("MAY", type
) == 0) {
241 token
->type
= SCHEMA_MAY
;
246 token
->value
= talloc_strndup(ctx
, c
, n
);
250 token
->value
= get_def_value(ctx
, &c
);
258 if (strcasecmp("SINGLE-VALUE", type
) == 0) {
260 token
->type
= SCHEMA_SINGLE_VALUE
;
265 if (strcasecmp("EQUALITY", type
) == 0) {
267 token
->type
= SCHEMA_EQUALITY
;
269 token
->value
= get_def_value(ctx
, &c
);
276 if (strcasecmp("ORDERING", type
) == 0) {
278 token
->type
= SCHEMA_ORDERING
;
280 token
->value
= get_def_value(ctx
, &c
);
287 if (strcasecmp("SUBSTR", type
) == 0) {
289 token
->type
= SCHEMA_SUBSTR
;
291 token
->value
= get_def_value(ctx
, &c
);
298 if (strcasecmp("SYNTAX", type
) == 0) {
300 token
->type
= SCHEMA_SYNTAX
;
302 token
->value
= get_def_value(ctx
, &c
);
309 if (strcasecmp("DESC", type
) == 0) {
311 token
->type
= SCHEMA_DESC
;
313 token
->value
= get_def_value(ctx
, &c
);
320 token
->type
= SCHEMA_UNKNOWN
;
327 c
= strchr(++c
, '\'');
330 c
+= strcspn(c
, " \t\n");
338 static struct ldb_message
*process_entry(TALLOC_CTX
*mem_ctx
, const char *entry
)
341 struct ldb_message
*msg
;
342 struct schema_token
*token
;
346 SHA256_CTX sha256_context
;
347 uint8_t digest
[SHA256_DIGEST_LENGTH
];
351 bool isAttribute
= false;
352 bool single_valued
= false;
354 ctx
= talloc_new(mem_ctx
);
355 msg
= ldb_msg_new(ctx
);
357 ldb_msg_add_string(msg
, "objectClass", "top");
359 c
= talloc_strdup(ctx
, entry
);
366 if (strncmp(c
, "attributetype", 13) == 0) {
368 MSG_ADD_STRING("objectClass", "attributeSchema");
374 if (strncmp(c
, "objectclass", 11) == 0) {
376 MSG_ADD_STRING("objectClass", "classSchema");
385 if (c
== NULL
) goto failed
;
390 /* get attributeID */
391 n
= strcspn(c
, " \t");
392 s
= talloc_strndup(msg
, c
, n
);
394 MSG_ADD_STRING("attributeID", s
);
396 MSG_ADD_STRING("governsID", s
);
399 samba_SHA256_Init(&sha256_context
);
400 samba_SHA256_Update(&sha256_context
, (uint8_t*)s
, strlen(s
));
401 samba_SHA256_Final(digest
, &sha256_context
);
403 memcpy(&guid
, digest
, sizeof(struct GUID
));
405 if (dsdb_msg_add_guid(msg
, &guid
, "schemaIdGuid") != 0) {
413 token
= get_next_schema_token(msg
, &c
);
414 if (!token
) goto failed
;
416 switch (token
->type
) {
418 MSG_ADD_STRING("cn", token
->value
);
419 MSG_ADD_STRING("name", token
->value
);
420 MSG_ADD_STRING("lDAPDisplayName", token
->value
);
421 msg
->dn
= ldb_dn_copy(msg
, basedn
);
422 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=Schema,CN=Configuration", token
->value
);
426 MSG_ADD_M_STRING("subClassOf", token
->value
);
429 case SCHEMA_STRUCTURAL
:
430 MSG_ADD_STRING("objectClassCategory", "1");
433 case SCHEMA_ABSTRACT
:
434 MSG_ADD_STRING("objectClassCategory", "2");
437 case SCHEMA_AUXILIARY
:
438 MSG_ADD_STRING("objectClassCategory", "3");
442 MSG_ADD_M_STRING("mustContain", token
->value
);
446 MSG_ADD_M_STRING("mayContain", token
->value
);
449 case SCHEMA_SINGLE_VALUE
:
450 single_valued
= true;
453 case SCHEMA_EQUALITY
:
457 case SCHEMA_ORDERING
:
468 const struct dsdb_syntax
*map
;
471 n
= strcspn(token
->value
, "{");
472 syntax_oid
= talloc_strndup(ctx
, token
->value
, n
);
474 map
= find_syntax_map_by_standard_oid(syntax_oid
);
479 MSG_ADD_STRING("attributeSyntax", map
->attributeSyntax_oid
);
481 oMSyntax
= talloc_asprintf(msg
, "%d", map
->oMSyntax
);
482 MSG_ADD_STRING("oMSyntax", oMSyntax
);
487 MSG_ADD_STRING("description", token
->value
);
491 fprintf(stderr
, "Unknown Definition: %s\n", token
->value
);
496 MSG_ADD_STRING("isSingleValued", single_valued
? "TRUE" : "FALSE");
498 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg
->dn
));
501 talloc_steal(mem_ctx
, msg
);
510 static struct schema_conv
process_file(FILE *in
, FILE *out
)
513 struct schema_conv ret
;
516 struct ldb_ldif ldif
;
518 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
520 ctx
= talloc_new(NULL
);
526 while ((c
= fgetc(in
)) != EOF
) {
528 /* fprintf(stderr, "Parsing line %d\n", line); */
532 } while (c
!= EOF
&& c
!= '\n');
540 entry
= talloc_array(ctx
, char, 1024);
541 if (entry
== NULL
) exit(-1);
547 ret2
= check_braces(entry
);
550 ldif
.msg
= process_entry(ctx
, entry
);
551 if (ldif
.msg
== NULL
) {
553 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
556 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
560 fprintf(stderr
, "Invalid entry %s, closing braces needs to be preceeded by a space\n", entry
);
569 if ((t
% 1023) == 0) {
570 entry
= talloc_realloc(ctx
, entry
, char, t
+ 1024);
571 if (entry
== NULL
) exit(-1);
573 } while ((c
= fgetc(in
)) != EOF
);
577 if (check_braces(entry
) == 0) {
579 ldif
.msg
= process_entry(ctx
, entry
);
580 if (ldif
.msg
== NULL
) {
582 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
585 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
587 fprintf(stderr
, "malformed entry on line %d\n", line
);
598 static struct options
{
604 static struct poptOption popt_options
[] = {
606 { "basedn", 'b', POPT_ARG_STRING
, &options
.basedn
, 0, "base DN", "DN" },
607 { "input", 'I', POPT_ARG_STRING
, &options
.input
, 0,
608 "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
609 { "output", 'O', POPT_ARG_STRING
, &options
.output
, 0,
610 "outputfile otherwise STDOUT", "outputfile"},
616 static void usage(void)
619 printf("Usage: oLschema2ldif <options>\n");
620 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
621 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
622 pc
= poptGetContext("oLschema2ldif", 0, NULL
, popt_options
,
623 POPT_CONTEXT_KEEP_FIRST
);
624 poptPrintHelp(pc
, stdout
, 0);
629 int main(int argc
, const char **argv
)
632 struct schema_conv ret
;
638 ctx
= talloc_new(NULL
);
639 ldb_ctx
= ldb_init(ctx
, NULL
);
641 setenv("LDB_URL", "NONE", 1);
643 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
,
644 POPT_CONTEXT_KEEP_FIRST
);
646 while((opt
= poptGetNextOpt(pc
)) != -1) {
647 fprintf(stderr
, "Invalid option %s: %s\n",
648 poptBadOption(pc
, 0), poptStrerror(opt
));
652 if (options
.basedn
== NULL
) {
653 printf("Base DN not specified\n");
657 basedn
= ldb_dn_new(ctx
, ldb_ctx
, options
.basedn
);
658 if ( ! ldb_dn_validate(basedn
)) {
659 printf("Malformed Base DN\n");
666 in
= fopen(options
.input
, "r");
668 perror(options
.input
);
673 if (options
.output
) {
674 out
= fopen(options
.output
, "w");
676 perror(options
.output
);
682 ret
= process_file(in
, out
);
687 printf("Converted %d records with %d failures\n", ret
.count
, ret
.failures
);