lib/util: Build iov_buf library only when building samba
[Samba.git] / source4 / utils / oLschema2ldif.c
blob108c2c8ab20b59c5825a338f688429de1a12e512
1 /*
2 ldb database library
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
8 ** under the LGPL
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/>.
25 * Name: ldb
27 * Component: oLschema2ldif
29 * Description: utility to convert an OpenLDAP schema into AD LDIF
31 * Author: Simo Sorce
34 #include "includes.h"
35 #include "ldb.h"
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
42 #define SCHEMA_NAME 1
43 #define SCHEMA_SUP 2
44 #define SCHEMA_STRUCTURAL 3
45 #define SCHEMA_ABSTRACT 4
46 #define SCHEMA_AUXILIARY 5
47 #define SCHEMA_MUST 6
48 #define SCHEMA_MAY 7
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
56 struct schema_conv {
57 int count;
58 int failures;
61 struct schema_token {
62 int type;
63 char *value;
66 struct ldb_context *ldb_ctx;
67 struct ldb_dn *basedn;
69 static int check_braces(const char *string)
71 int b;
72 char *c;
74 b = 0;
75 if ((c = strchr(string, '(')) == NULL) {
76 return -1;
78 b++;
79 c++;
80 while (b) {
81 c = strpbrk(c, "()");
82 if (c == NULL) return 1;
83 if (*c == '(') b++;
84 if (*c == ')') {
85 b--;
86 if (*(c - 1) != ' ' && c && (*(c + 1) == '\0')) {
87 return 2;
90 c++;
92 return 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)
101 char *c;
102 char *s;
103 int n;
105 c = skip_spaces(values);
106 while (*c) {
107 n = strcspn(c, " \t$");
108 s = talloc_strndup(msg, c, n);
109 if (ldb_msg_add_string(msg, attr, s) != 0) {
110 return -1;
112 c += n;
113 c += strspn(c, " \t$");
116 return 0;
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)
124 char *c = *string;
125 char *value;
126 int n;
128 if (*c == '\'') {
129 c++;
130 n = strcspn(c, "\'");
131 value = talloc_strndup(ctx, c, n);
132 c += n;
133 c++; /* skip closing \' */
134 } else {
135 n = strcspn(c, " \t\n");
136 value = talloc_strndup(ctx, c, n);
137 c += n;
139 *string = c;
141 return value;
144 static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
146 char *c = skip_spaces(*string);
147 char *type;
148 struct schema_token *token;
149 int n;
151 token = talloc(ctx, struct schema_token);
153 n = strcspn(c, " \t\n");
154 type = talloc_strndup(token, c, n);
155 c += n;
156 c = skip_spaces(c);
158 if (strcasecmp("NAME", type) == 0) {
159 talloc_free(type);
160 token->type = SCHEMA_NAME;
161 /* we do not support aliases so we get only the first name given and skip others */
162 if (*c == '(') {
163 char *s = strchr(c, ')');
164 if (s == NULL) return NULL;
165 s = skip_spaces(s);
166 *string = s;
168 c++;
169 c = skip_spaces(c);
172 token->value = get_def_value(ctx, &c);
174 if (*string < c) { /* single name */
175 c = skip_spaces(c);
176 *string = c;
178 return token;
180 if (strcasecmp("SUP", type) == 0) {
181 talloc_free(type);
182 token->type = SCHEMA_SUP;
184 if (*c == '(') {
185 c++;
186 n = strcspn(c, ")");
187 token->value = talloc_strndup(ctx, c, n);
188 c += n;
189 c++;
190 } else {
191 token->value = get_def_value(ctx, &c);
194 c = skip_spaces(c);
195 *string = c;
196 return token;
199 if (strcasecmp("STRUCTURAL", type) == 0) {
200 talloc_free(type);
201 token->type = SCHEMA_STRUCTURAL;
202 *string = c;
203 return token;
206 if (strcasecmp("ABSTRACT", type) == 0) {
207 talloc_free(type);
208 token->type = SCHEMA_ABSTRACT;
209 *string = c;
210 return token;
213 if (strcasecmp("AUXILIARY", type) == 0) {
214 talloc_free(type);
215 token->type = SCHEMA_AUXILIARY;
216 *string = c;
217 return token;
220 if (strcasecmp("MUST", type) == 0) {
221 talloc_free(type);
222 token->type = SCHEMA_MUST;
224 if (*c == '(') {
225 c++;
226 n = strcspn(c, ")");
227 token->value = talloc_strndup(ctx, c, n);
228 c += n;
229 c++;
230 } else {
231 token->value = get_def_value(ctx, &c);
234 c = skip_spaces(c);
235 *string = c;
236 return token;
239 if (strcasecmp("MAY", type) == 0) {
240 talloc_free(type);
241 token->type = SCHEMA_MAY;
243 if (*c == '(') {
244 c++;
245 n = strcspn(c, ")");
246 token->value = talloc_strndup(ctx, c, n);
247 c += n;
248 c++;
249 } else {
250 token->value = get_def_value(ctx, &c);
253 c = skip_spaces(c);
254 *string = c;
255 return token;
258 if (strcasecmp("SINGLE-VALUE", type) == 0) {
259 talloc_free(type);
260 token->type = SCHEMA_SINGLE_VALUE;
261 *string = c;
262 return token;
265 if (strcasecmp("EQUALITY", type) == 0) {
266 talloc_free(type);
267 token->type = SCHEMA_EQUALITY;
269 token->value = get_def_value(ctx, &c);
271 c = skip_spaces(c);
272 *string = c;
273 return token;
276 if (strcasecmp("ORDERING", type) == 0) {
277 talloc_free(type);
278 token->type = SCHEMA_ORDERING;
280 token->value = get_def_value(ctx, &c);
282 c = skip_spaces(c);
283 *string = c;
284 return token;
287 if (strcasecmp("SUBSTR", type) == 0) {
288 talloc_free(type);
289 token->type = SCHEMA_SUBSTR;
291 token->value = get_def_value(ctx, &c);
293 c = skip_spaces(c);
294 *string = c;
295 return token;
298 if (strcasecmp("SYNTAX", type) == 0) {
299 talloc_free(type);
300 token->type = SCHEMA_SYNTAX;
302 token->value = get_def_value(ctx, &c);
304 c = skip_spaces(c);
305 *string = c;
306 return token;
309 if (strcasecmp("DESC", type) == 0) {
310 talloc_free(type);
311 token->type = SCHEMA_DESC;
313 token->value = get_def_value(ctx, &c);
315 c = skip_spaces(c);
316 *string = c;
317 return token;
320 token->type = SCHEMA_UNKNOWN;
321 token->value = type;
322 if (*c == ')') {
323 *string = c;
324 return token;
326 if (*c == '\'') {
327 c = strchr(++c, '\'');
328 c++;
329 } else {
330 c += strcspn(c, " \t\n");
332 c = skip_spaces(c);
333 *string = c;
335 return token;
338 static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
340 TALLOC_CTX *ctx;
341 struct ldb_message *msg;
342 struct schema_token *token;
343 char *c, *s;
344 int n;
346 SHA256_CTX sha256_context;
347 uint8_t digest[SHA256_DIGEST_LENGTH];
349 struct GUID guid;
351 bool isAttribute = false;
352 bool single_valued = false;
354 ctx = talloc_new(mem_ctx);
355 if (ctx == NULL) {
356 return NULL;
358 msg = ldb_msg_new(ctx);
359 if (msg == NULL) {
360 goto failed;
363 ldb_msg_add_string(msg, "objectClass", "top");
365 c = talloc_strdup(ctx, entry);
366 if (!c) return NULL;
368 c = skip_spaces(c);
370 switch (*c) {
371 case 'a':
372 if (strncmp(c, "attributetype", 13) == 0) {
373 c += 13;
374 MSG_ADD_STRING("objectClass", "attributeSchema");
375 isAttribute = true;
376 break;
378 goto failed;
379 case 'o':
380 if (strncmp(c, "objectclass", 11) == 0) {
381 c += 11;
382 MSG_ADD_STRING("objectClass", "classSchema");
383 break;
385 goto failed;
386 default:
387 goto failed;
390 c = strchr(c, '(');
391 if (c == NULL) goto failed;
392 c++;
394 c = skip_spaces(c);
396 /* get attributeID */
397 n = strcspn(c, " \t");
398 s = talloc_strndup(msg, c, n);
399 if (isAttribute) {
400 MSG_ADD_STRING("attributeID", s);
401 } else {
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) {
412 goto failed;
415 c += n;
416 c = skip_spaces(c);
418 while (*c != ')') {
419 token = get_next_schema_token(msg, &c);
420 if (!token) goto failed;
422 switch (token->type) {
423 case SCHEMA_NAME:
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);
429 break;
431 case SCHEMA_SUP:
432 MSG_ADD_M_STRING("subClassOf", token->value);
433 break;
435 case SCHEMA_STRUCTURAL:
436 MSG_ADD_STRING("objectClassCategory", "1");
437 break;
439 case SCHEMA_ABSTRACT:
440 MSG_ADD_STRING("objectClassCategory", "2");
441 break;
443 case SCHEMA_AUXILIARY:
444 MSG_ADD_STRING("objectClassCategory", "3");
445 break;
447 case SCHEMA_MUST:
448 MSG_ADD_M_STRING("mustContain", token->value);
449 break;
451 case SCHEMA_MAY:
452 MSG_ADD_M_STRING("mayContain", token->value);
453 break;
455 case SCHEMA_SINGLE_VALUE:
456 single_valued = true;
457 break;
459 case SCHEMA_EQUALITY:
460 /* TODO */
461 break;
463 case SCHEMA_ORDERING:
464 /* TODO */
465 break;
467 case SCHEMA_SUBSTR:
468 /* TODO */
469 break;
471 case SCHEMA_SYNTAX:
473 char *syntax_oid;
474 const struct dsdb_syntax *map;
475 char *oMSyntax;
477 n = strcspn(token->value, "{");
478 syntax_oid = talloc_strndup(ctx, token->value, n);
480 map = find_syntax_map_by_standard_oid(syntax_oid);
481 if (!map) {
482 break;
485 MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
487 oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
488 MSG_ADD_STRING("oMSyntax", oMSyntax);
490 break;
492 case SCHEMA_DESC:
493 MSG_ADD_STRING("description", token->value);
494 break;
496 default:
497 fprintf(stderr, "Unknown Definition: %s\n", token->value);
501 if (isAttribute) {
502 MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
503 } else {
504 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
507 talloc_steal(mem_ctx, msg);
508 talloc_free(ctx);
509 return msg;
511 failed:
512 talloc_free(ctx);
513 return NULL;
516 static struct schema_conv process_file(FILE *in, FILE *out)
518 TALLOC_CTX *ctx;
519 struct schema_conv ret;
520 char *entry;
521 int c, t, line;
522 struct ldb_ldif ldif;
524 ldif.changetype = LDB_CHANGETYPE_NONE;
526 ctx = talloc_new(NULL);
528 ret.count = 0;
529 ret.failures = 0;
530 line = 0;
532 while ((c = fgetc(in)) != EOF) {
533 line++;
534 /* fprintf(stderr, "Parsing line %d\n", line); */
535 if (c == '#') {
536 do {
537 c = fgetc(in);
538 } while (c != EOF && c != '\n');
539 continue;
541 if (c == '\n') {
542 continue;
545 t = 0;
546 entry = talloc_array(ctx, char, 1024);
547 if (entry == NULL) exit(-1);
549 do {
550 if (c == '\n') {
551 int ret2 = 0;
552 entry[t] = '\0';
553 ret2 = check_braces(entry);
554 if (ret2 == 0) {
555 ret.count++;
556 ldif.msg = process_entry(ctx, entry);
557 if (ldif.msg == NULL) {
558 ret.failures++;
559 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
560 break;
562 ldb_ldif_write_file(ldb_ctx, out, &ldif);
563 break;
565 if (ret2 == 2) {
566 fprintf(stderr, "Invalid entry %s, closing braces need to be preceded by a space\n", entry);
567 ret.failures++;
568 break;
570 line++;
571 } else {
572 entry[t] = c;
573 t++;
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);
581 if (c != '\n') {
582 entry[t] = '\0';
583 if (check_braces(entry) == 0) {
584 ret.count++;
585 ldif.msg = process_entry(ctx, entry);
586 if (ldif.msg == NULL) {
587 ret.failures++;
588 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
589 break;
591 ldb_ldif_write_file(ldb_ctx, out, &ldif);
592 } else {
593 fprintf(stderr, "malformed entry on line %d\n", line);
594 ret.failures++;
598 if (c == EOF) break;
601 return ret;
604 static struct options {
605 const char *basedn;
606 const char *input;
607 const char *output;
608 } options;
610 static struct poptOption popt_options[] = {
611 POPT_AUTOHELP
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"},
617 POPT_COMMON_VERSION
618 { NULL }
622 static void usage(void)
624 poptContext pc;
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);
631 exit(1);
635 int main(int argc, const char **argv)
637 TALLOC_CTX *ctx;
638 struct schema_conv ret;
639 FILE *in = stdin;
640 FILE *out = stdout;
641 poptContext pc;
642 int opt;
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));
655 usage();
658 if (options.basedn == NULL) {
659 printf("Base DN not specified\n");
660 usage();
661 exit(1);
662 } else {
663 basedn = ldb_dn_new(ctx, ldb_ctx, options.basedn);
664 if ( ! ldb_dn_validate(basedn)) {
665 printf("Malformed Base DN\n");
666 usage();
667 exit(1);
671 if (options.input) {
672 in = fopen(options.input, "r");
673 if (!in) {
674 perror(options.input);
675 usage();
676 exit(1);
679 if (options.output) {
680 out = fopen(options.output, "w");
681 if (!out) {
682 perror(options.output);
683 usage();
684 exit(1);
688 ret = process_file(in, out);
690 fclose(in);
691 fclose(out);
693 printf("Converted %d records with %d failures\n", ret.count, ret.failures);
695 return 0;