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
);
358 msg
= ldb_msg_new(ctx
);
363 ldb_msg_add_string(msg
, "objectClass", "top");
365 c
= talloc_strdup(ctx
, entry
);
372 if (strncmp(c
, "attributetype", 13) == 0) {
374 MSG_ADD_STRING("objectClass", "attributeSchema");
380 if (strncmp(c
, "objectclass", 11) == 0) {
382 MSG_ADD_STRING("objectClass", "classSchema");
391 if (c
== NULL
) goto failed
;
396 /* get attributeID */
397 n
= strcspn(c
, " \t");
398 s
= talloc_strndup(msg
, c
, n
);
400 MSG_ADD_STRING("attributeID", s
);
402 MSG_ADD_STRING("governsID", s
);
405 samba_SHA256_Init(&sha256_context
);
406 samba_SHA256_Update(&sha256_context
, (uint8_t*)s
, strlen(s
));
407 samba_SHA256_Final(digest
, &sha256_context
);
409 memcpy(&guid
, digest
, sizeof(struct GUID
));
411 if (dsdb_msg_add_guid(msg
, &guid
, "schemaIdGuid") != 0) {
419 token
= get_next_schema_token(msg
, &c
);
420 if (!token
) goto failed
;
422 switch (token
->type
) {
424 MSG_ADD_STRING("cn", token
->value
);
425 MSG_ADD_STRING("name", token
->value
);
426 MSG_ADD_STRING("lDAPDisplayName", token
->value
);
427 msg
->dn
= ldb_dn_copy(msg
, basedn
);
428 ldb_dn_add_child_fmt(msg
->dn
, "CN=%s,CN=Schema,CN=Configuration", token
->value
);
432 MSG_ADD_M_STRING("subClassOf", token
->value
);
435 case SCHEMA_STRUCTURAL
:
436 MSG_ADD_STRING("objectClassCategory", "1");
439 case SCHEMA_ABSTRACT
:
440 MSG_ADD_STRING("objectClassCategory", "2");
443 case SCHEMA_AUXILIARY
:
444 MSG_ADD_STRING("objectClassCategory", "3");
448 MSG_ADD_M_STRING("mustContain", token
->value
);
452 MSG_ADD_M_STRING("mayContain", token
->value
);
455 case SCHEMA_SINGLE_VALUE
:
456 single_valued
= true;
459 case SCHEMA_EQUALITY
:
463 case SCHEMA_ORDERING
:
474 const struct dsdb_syntax
*map
;
477 n
= strcspn(token
->value
, "{");
478 syntax_oid
= talloc_strndup(ctx
, token
->value
, n
);
480 map
= find_syntax_map_by_standard_oid(syntax_oid
);
485 MSG_ADD_STRING("attributeSyntax", map
->attributeSyntax_oid
);
487 oMSyntax
= talloc_asprintf(msg
, "%d", map
->oMSyntax
);
488 MSG_ADD_STRING("oMSyntax", oMSyntax
);
493 MSG_ADD_STRING("description", token
->value
);
497 fprintf(stderr
, "Unknown Definition: %s\n", token
->value
);
502 MSG_ADD_STRING("isSingleValued", single_valued
? "TRUE" : "FALSE");
504 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg
->dn
));
507 talloc_steal(mem_ctx
, msg
);
516 static struct schema_conv
process_file(FILE *in
, FILE *out
)
519 struct schema_conv ret
;
522 struct ldb_ldif ldif
;
524 ldif
.changetype
= LDB_CHANGETYPE_NONE
;
526 ctx
= talloc_new(NULL
);
532 while ((c
= fgetc(in
)) != EOF
) {
534 /* fprintf(stderr, "Parsing line %d\n", line); */
538 } while (c
!= EOF
&& c
!= '\n');
546 entry
= talloc_array(ctx
, char, 1024);
547 if (entry
== NULL
) exit(-1);
553 ret2
= check_braces(entry
);
556 ldif
.msg
= process_entry(ctx
, entry
);
557 if (ldif
.msg
== NULL
) {
559 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
562 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
566 fprintf(stderr
, "Invalid entry %s, closing braces need to be preceded by a space\n", entry
);
575 if ((t
% 1023) == 0) {
576 entry
= talloc_realloc(ctx
, entry
, char, t
+ 1024);
577 if (entry
== NULL
) exit(-1);
579 } while ((c
= fgetc(in
)) != EOF
);
583 if (check_braces(entry
) == 0) {
585 ldif
.msg
= process_entry(ctx
, entry
);
586 if (ldif
.msg
== NULL
) {
588 fprintf(stderr
, "No valid msg from entry \n[%s]\n at line %d\n", entry
, line
);
591 ldb_ldif_write_file(ldb_ctx
, out
, &ldif
);
593 fprintf(stderr
, "malformed entry on line %d\n", line
);
604 static struct options
{
610 static struct poptOption popt_options
[] = {
612 { "basedn", 'b', POPT_ARG_STRING
, &options
.basedn
, 0, "base DN", "DN" },
613 { "input", 'I', POPT_ARG_STRING
, &options
.input
, 0,
614 "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
615 { "output", 'O', POPT_ARG_STRING
, &options
.output
, 0,
616 "outputfile otherwise STDOUT", "outputfile"},
622 static void usage(void)
625 printf("Usage: oLschema2ldif <options>\n");
626 printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
627 printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
628 pc
= poptGetContext("oLschema2ldif", 0, NULL
, popt_options
,
629 POPT_CONTEXT_KEEP_FIRST
);
630 poptPrintHelp(pc
, stdout
, 0);
635 int main(int argc
, const char **argv
)
638 struct schema_conv ret
;
644 ctx
= talloc_new(NULL
);
645 ldb_ctx
= ldb_init(ctx
, NULL
);
647 setenv("LDB_URL", "NONE", 1);
649 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
,
650 POPT_CONTEXT_KEEP_FIRST
);
652 while((opt
= poptGetNextOpt(pc
)) != -1) {
653 fprintf(stderr
, "Invalid option %s: %s\n",
654 poptBadOption(pc
, 0), poptStrerror(opt
));
658 if (options
.basedn
== NULL
) {
659 printf("Base DN not specified\n");
663 basedn
= ldb_dn_new(ctx
, ldb_ctx
, options
.basedn
);
664 if ( ! ldb_dn_validate(basedn
)) {
665 printf("Malformed Base DN\n");
672 in
= fopen(options
.input
, "r");
674 perror(options
.input
);
679 if (options
.output
) {
680 out
= fopen(options
.output
, "w");
682 perror(options
.output
);
688 ret
= process_file(in
, out
);
693 printf("Converted %d records with %d failures\n", ret
.count
, ret
.failures
);