smbd:smb2: fix error code when the header says the request is signed but we don't...
[Samba.git] / source4 / utils / oLschema2ldif.c
blobbcdf5709775b194c9098a95e2b8af6a997f496ee
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 msg = ldb_msg_new(ctx);
357 ldb_msg_add_string(msg, "objectClass", "top");
359 c = talloc_strdup(ctx, entry);
360 if (!c) return NULL;
362 c = skip_spaces(c);
364 switch (*c) {
365 case 'a':
366 if (strncmp(c, "attributetype", 13) == 0) {
367 c += 13;
368 MSG_ADD_STRING("objectClass", "attributeSchema");
369 isAttribute = true;
370 break;
372 goto failed;
373 case 'o':
374 if (strncmp(c, "objectclass", 11) == 0) {
375 c += 11;
376 MSG_ADD_STRING("objectClass", "classSchema");
377 break;
379 goto failed;
380 default:
381 goto failed;
384 c = strchr(c, '(');
385 if (c == NULL) goto failed;
386 c++;
388 c = skip_spaces(c);
390 /* get attributeID */
391 n = strcspn(c, " \t");
392 s = talloc_strndup(msg, c, n);
393 if (isAttribute) {
394 MSG_ADD_STRING("attributeID", s);
395 } else {
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) {
406 goto failed;
409 c += n;
410 c = skip_spaces(c);
412 while (*c != ')') {
413 token = get_next_schema_token(msg, &c);
414 if (!token) goto failed;
416 switch (token->type) {
417 case SCHEMA_NAME:
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);
423 break;
425 case SCHEMA_SUP:
426 MSG_ADD_M_STRING("subClassOf", token->value);
427 break;
429 case SCHEMA_STRUCTURAL:
430 MSG_ADD_STRING("objectClassCategory", "1");
431 break;
433 case SCHEMA_ABSTRACT:
434 MSG_ADD_STRING("objectClassCategory", "2");
435 break;
437 case SCHEMA_AUXILIARY:
438 MSG_ADD_STRING("objectClassCategory", "3");
439 break;
441 case SCHEMA_MUST:
442 MSG_ADD_M_STRING("mustContain", token->value);
443 break;
445 case SCHEMA_MAY:
446 MSG_ADD_M_STRING("mayContain", token->value);
447 break;
449 case SCHEMA_SINGLE_VALUE:
450 single_valued = true;
451 break;
453 case SCHEMA_EQUALITY:
454 /* TODO */
455 break;
457 case SCHEMA_ORDERING:
458 /* TODO */
459 break;
461 case SCHEMA_SUBSTR:
462 /* TODO */
463 break;
465 case SCHEMA_SYNTAX:
467 char *syntax_oid;
468 const struct dsdb_syntax *map;
469 char *oMSyntax;
471 n = strcspn(token->value, "{");
472 syntax_oid = talloc_strndup(ctx, token->value, n);
474 map = find_syntax_map_by_standard_oid(syntax_oid);
475 if (!map) {
476 break;
479 MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
481 oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
482 MSG_ADD_STRING("oMSyntax", oMSyntax);
484 break;
486 case SCHEMA_DESC:
487 MSG_ADD_STRING("description", token->value);
488 break;
490 default:
491 fprintf(stderr, "Unknown Definition: %s\n", token->value);
495 if (isAttribute) {
496 MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
497 } else {
498 MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
501 talloc_steal(mem_ctx, msg);
502 talloc_free(ctx);
503 return msg;
505 failed:
506 talloc_free(ctx);
507 return NULL;
510 static struct schema_conv process_file(FILE *in, FILE *out)
512 TALLOC_CTX *ctx;
513 struct schema_conv ret;
514 char *entry;
515 int c, t, line;
516 struct ldb_ldif ldif;
518 ldif.changetype = LDB_CHANGETYPE_NONE;
520 ctx = talloc_new(NULL);
522 ret.count = 0;
523 ret.failures = 0;
524 line = 0;
526 while ((c = fgetc(in)) != EOF) {
527 line++;
528 /* fprintf(stderr, "Parsing line %d\n", line); */
529 if (c == '#') {
530 do {
531 c = fgetc(in);
532 } while (c != EOF && c != '\n');
533 continue;
535 if (c == '\n') {
536 continue;
539 t = 0;
540 entry = talloc_array(ctx, char, 1024);
541 if (entry == NULL) exit(-1);
543 do {
544 if (c == '\n') {
545 int ret2 = 0;
546 entry[t] = '\0';
547 ret2 = check_braces(entry);
548 if (ret2 == 0) {
549 ret.count++;
550 ldif.msg = process_entry(ctx, entry);
551 if (ldif.msg == NULL) {
552 ret.failures++;
553 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
554 break;
556 ldb_ldif_write_file(ldb_ctx, out, &ldif);
557 break;
559 if (ret2 == 2) {
560 fprintf(stderr, "Invalid entry %s, closing braces needs to be preceeded by a space\n", entry);
561 ret.failures++;
562 break;
564 line++;
565 } else {
566 entry[t] = c;
567 t++;
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);
575 if (c != '\n') {
576 entry[t] = '\0';
577 if (check_braces(entry) == 0) {
578 ret.count++;
579 ldif.msg = process_entry(ctx, entry);
580 if (ldif.msg == NULL) {
581 ret.failures++;
582 fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
583 break;
585 ldb_ldif_write_file(ldb_ctx, out, &ldif);
586 } else {
587 fprintf(stderr, "malformed entry on line %d\n", line);
588 ret.failures++;
592 if (c == EOF) break;
595 return ret;
598 static struct options {
599 const char *basedn;
600 const char *input;
601 const char *output;
602 } options;
604 static struct poptOption popt_options[] = {
605 POPT_AUTOHELP
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"},
611 POPT_COMMON_VERSION
612 { NULL }
616 static void usage(void)
618 poptContext pc;
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);
625 exit(1);
629 int main(int argc, const char **argv)
631 TALLOC_CTX *ctx;
632 struct schema_conv ret;
633 FILE *in = stdin;
634 FILE *out = stdout;
635 poptContext pc;
636 int opt;
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));
649 usage();
652 if (options.basedn == NULL) {
653 printf("Base DN not specified\n");
654 usage();
655 exit(1);
656 } else {
657 basedn = ldb_dn_new(ctx, ldb_ctx, options.basedn);
658 if ( ! ldb_dn_validate(basedn)) {
659 printf("Malformed Base DN\n");
660 usage();
661 exit(1);
665 if (options.input) {
666 in = fopen(options.input, "r");
667 if (!in) {
668 perror(options.input);
669 usage();
670 exit(1);
673 if (options.output) {
674 out = fopen(options.output, "w");
675 if (!out) {
676 perror(options.output);
677 usage();
678 exit(1);
682 ret = process_file(in, out);
684 fclose(in);
685 fclose(out);
687 printf("Converted %d records with %d failures\n", ret.count, ret.failures);
689 return 0;