source: vendor/current/source4/utils/oLschema2ldif.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 13.6 KB
Line 
1/*
2 ldb database library
3
4 Copyright (C) Simo Sorce 2005
5
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
9
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.
14
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.
19
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/>.
22*/
23
24/*
25 * Name: ldb
26 *
27 * Component: oLschema2ldif
28 *
29 * Description: utility to convert an OpenLDAP schema into AD LDIF
30 *
31 * Author: Simo Sorce
32 */
33
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"
40
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
55
56struct schema_conv {
57 int count;
58 int failures;
59};
60
61struct schema_token {
62 int type;
63 char *value;
64};
65
66struct ldb_context *ldb_ctx;
67struct ldb_dn *basedn;
68
69static int check_braces(const char *string)
70{
71 int b;
72 char *c;
73
74 b = 0;
75 if ((c = strchr(string, '(')) == NULL) {
76 return -1;
77 }
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;
88 }
89 }
90 c++;
91 }
92 return 0;
93}
94
95static char *skip_spaces(char *string) {
96 return (string + strspn(string, " \t\n"));
97}
98
99static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
100{
101 char *c;
102 char *s;
103 int n;
104
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;
111 }
112 c += n;
113 c += strspn(c, " \t$");
114 }
115
116 return 0;
117}
118
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)
121
122static char *get_def_value(TALLOC_CTX *ctx, char **string)
123{
124 char *c = *string;
125 char *value;
126 int n;
127
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;
138 }
139 *string = c;
140
141 return value;
142}
143
144static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
145{
146 char *c = skip_spaces(*string);
147 char *type;
148 struct schema_token *token;
149 int n;
150
151 token = talloc(ctx, struct schema_token);
152
153 n = strcspn(c, " \t\n");
154 type = talloc_strndup(token, c, n);
155 c += n;
156 c = skip_spaces(c);
157
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;
167
168 c++;
169 c = skip_spaces(c);
170 }
171
172 token->value = get_def_value(ctx, &c);
173
174 if (*string < c) { /* single name */
175 c = skip_spaces(c);
176 *string = c;
177 }
178 return token;
179 }
180 if (strcasecmp("SUP", type) == 0) {
181 talloc_free(type);
182 token->type = SCHEMA_SUP;
183
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);
192 }
193
194 c = skip_spaces(c);
195 *string = c;
196 return token;
197 }
198
199 if (strcasecmp("STRUCTURAL", type) == 0) {
200 talloc_free(type);
201 token->type = SCHEMA_STRUCTURAL;
202 *string = c;
203 return token;
204 }
205
206 if (strcasecmp("ABSTRACT", type) == 0) {
207 talloc_free(type);
208 token->type = SCHEMA_ABSTRACT;
209 *string = c;
210 return token;
211 }
212
213 if (strcasecmp("AUXILIARY", type) == 0) {
214 talloc_free(type);
215 token->type = SCHEMA_AUXILIARY;
216 *string = c;
217 return token;
218 }
219
220 if (strcasecmp("MUST", type) == 0) {
221 talloc_free(type);
222 token->type = SCHEMA_MUST;
223
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);
232 }
233
234 c = skip_spaces(c);
235 *string = c;
236 return token;
237 }
238
239 if (strcasecmp("MAY", type) == 0) {
240 talloc_free(type);
241 token->type = SCHEMA_MAY;
242
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);
251 }
252
253 c = skip_spaces(c);
254 *string = c;
255 return token;
256 }
257
258 if (strcasecmp("SINGLE-VALUE", type) == 0) {
259 talloc_free(type);
260 token->type = SCHEMA_SINGLE_VALUE;
261 *string = c;
262 return token;
263 }
264
265 if (strcasecmp("EQUALITY", type) == 0) {
266 talloc_free(type);
267 token->type = SCHEMA_EQUALITY;
268
269 token->value = get_def_value(ctx, &c);
270
271 c = skip_spaces(c);
272 *string = c;
273 return token;
274 }
275
276 if (strcasecmp("ORDERING", type) == 0) {
277 talloc_free(type);
278 token->type = SCHEMA_ORDERING;
279
280 token->value = get_def_value(ctx, &c);
281
282 c = skip_spaces(c);
283 *string = c;
284 return token;
285 }
286
287 if (strcasecmp("SUBSTR", type) == 0) {
288 talloc_free(type);
289 token->type = SCHEMA_SUBSTR;
290
291 token->value = get_def_value(ctx, &c);
292
293 c = skip_spaces(c);
294 *string = c;
295 return token;
296 }
297
298 if (strcasecmp("SYNTAX", type) == 0) {
299 talloc_free(type);
300 token->type = SCHEMA_SYNTAX;
301
302 token->value = get_def_value(ctx, &c);
303
304 c = skip_spaces(c);
305 *string = c;
306 return token;
307 }
308
309 if (strcasecmp("DESC", type) == 0) {
310 talloc_free(type);
311 token->type = SCHEMA_DESC;
312
313 token->value = get_def_value(ctx, &c);
314
315 c = skip_spaces(c);
316 *string = c;
317 return token;
318 }
319
320 token->type = SCHEMA_UNKNOWN;
321 token->value = type;
322 if (*c == ')') {
323 *string = c;
324 return token;
325 }
326 if (*c == '\'') {
327 c = strchr(++c, '\'');
328 c++;
329 } else {
330 c += strcspn(c, " \t\n");
331 }
332 c = skip_spaces(c);
333 *string = c;
334
335 return token;
336}
337
338static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
339{
340 TALLOC_CTX *ctx;
341 struct ldb_message *msg;
342 struct schema_token *token;
343 char *c, *s;
344 int n;
345
346 SHA256_CTX sha256_context;
347 uint8_t digest[SHA256_DIGEST_LENGTH];
348
349 struct GUID guid;
350
351 bool isAttribute = false;
352 bool single_valued = false;
353
354 ctx = talloc_new(mem_ctx);
355 if (ctx == NULL) {
356 return NULL;
357 }
358 msg = ldb_msg_new(ctx);
359 if (msg == NULL) {
360 goto failed;
361 }
362
363 ldb_msg_add_string(msg, "objectClass", "top");
364
365 c = talloc_strdup(ctx, entry);
366 if (!c) return NULL;
367
368 c = skip_spaces(c);
369
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;
377 }
378 goto failed;
379 case 'o':
380 if (strncmp(c, "objectclass", 11) == 0) {
381 c += 11;
382 MSG_ADD_STRING("objectClass", "classSchema");
383 break;
384 }
385 goto failed;
386 default:
387 goto failed;
388 }
389
390 c = strchr(c, '(');
391 if (c == NULL) goto failed;
392 c++;
393
394 c = skip_spaces(c);
395
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);
403 }
404
405 samba_SHA256_Init(&sha256_context);
406 samba_SHA256_Update(&sha256_context, (uint8_t*)s, strlen(s));
407 samba_SHA256_Final(digest, &sha256_context);
408
409 memcpy(&guid, digest, sizeof(struct GUID));
410
411 if (dsdb_msg_add_guid(msg, &guid, "schemaIdGuid") != 0) {
412 goto failed;
413 }
414
415 c += n;
416 c = skip_spaces(c);
417
418 while (*c != ')') {
419 token = get_next_schema_token(msg, &c);
420 if (!token) goto failed;
421
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;
430
431 case SCHEMA_SUP:
432 MSG_ADD_M_STRING("subClassOf", token->value);
433 break;
434
435 case SCHEMA_STRUCTURAL:
436 MSG_ADD_STRING("objectClassCategory", "1");
437 break;
438
439 case SCHEMA_ABSTRACT:
440 MSG_ADD_STRING("objectClassCategory", "2");
441 break;
442
443 case SCHEMA_AUXILIARY:
444 MSG_ADD_STRING("objectClassCategory", "3");
445 break;
446
447 case SCHEMA_MUST:
448 MSG_ADD_M_STRING("mustContain", token->value);
449 break;
450
451 case SCHEMA_MAY:
452 MSG_ADD_M_STRING("mayContain", token->value);
453 break;
454
455 case SCHEMA_SINGLE_VALUE:
456 single_valued = true;
457 break;
458
459 case SCHEMA_EQUALITY:
460 /* TODO */
461 break;
462
463 case SCHEMA_ORDERING:
464 /* TODO */
465 break;
466
467 case SCHEMA_SUBSTR:
468 /* TODO */
469 break;
470
471 case SCHEMA_SYNTAX:
472 {
473 char *syntax_oid;
474 const struct dsdb_syntax *map;
475 char *oMSyntax;
476
477 n = strcspn(token->value, "{");
478 syntax_oid = talloc_strndup(ctx, token->value, n);
479
480 map = find_syntax_map_by_standard_oid(syntax_oid);
481 if (!map) {
482 break;
483 }
484
485 MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
486
487 oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
488 MSG_ADD_STRING("oMSyntax", oMSyntax);
489
490 break;
491 }
492 case SCHEMA_DESC:
493 MSG_ADD_STRING("description", token->value);
494 break;
495
496 default:
497 fprintf(stderr, "Unknown Definition: %s\n", token->value);
498 }
499 }
500
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));
505 }
506
507 talloc_steal(mem_ctx, msg);
508 talloc_free(ctx);
509 return msg;
510
511failed:
512 talloc_free(ctx);
513 return NULL;
514}
515
516static struct schema_conv process_file(FILE *in, FILE *out)
517{
518 TALLOC_CTX *ctx;
519 struct schema_conv ret;
520 char *entry;
521 int c, t, line;
522 struct ldb_ldif ldif;
523
524 ldif.changetype = LDB_CHANGETYPE_NONE;
525
526 ctx = talloc_new(NULL);
527
528 ret.count = 0;
529 ret.failures = 0;
530 line = 0;
531
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;
540 }
541 if (c == '\n') {
542 continue;
543 }
544
545 t = 0;
546 entry = talloc_array(ctx, char, 1024);
547 if (entry == NULL) exit(-1);
548
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;
561 }
562 ldb_ldif_write_file(ldb_ctx, out, &ldif);
563 break;
564 }
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;
569 }
570 line++;
571 } else {
572 entry[t] = c;
573 t++;
574 }
575 if ((t % 1023) == 0) {
576 entry = talloc_realloc(ctx, entry, char, t + 1024);
577 if (entry == NULL) exit(-1);
578 }
579 } while ((c = fgetc(in)) != EOF);
580
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;
590 }
591 ldb_ldif_write_file(ldb_ctx, out, &ldif);
592 } else {
593 fprintf(stderr, "malformed entry on line %d\n", line);
594 ret.failures++;
595 }
596 }
597
598 if (c == EOF) break;
599 }
600
601 return ret;
602}
603
604static struct options {
605 const char *basedn;
606 const char *input;
607 const char *output;
608} options;
609
610static 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 }
619};
620
621
622static void usage(void)
623{
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);
632}
633
634
635 int main(int argc, const char **argv)
636{
637 TALLOC_CTX *ctx;
638 struct schema_conv ret;
639 FILE *in = stdin;
640 FILE *out = stdout;
641 poptContext pc;
642 int opt;
643
644 ctx = talloc_new(NULL);
645 ldb_ctx = ldb_init(ctx, NULL);
646
647 setenv("LDB_URL", "NONE", 1);
648
649 pc = poptGetContext(argv[0], argc, argv, popt_options,
650 POPT_CONTEXT_KEEP_FIRST);
651
652 while((opt = poptGetNextOpt(pc)) != -1) {
653 fprintf(stderr, "Invalid option %s: %s\n",
654 poptBadOption(pc, 0), poptStrerror(opt));
655 usage();
656 }
657
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);
668 }
669 }
670
671 if (options.input) {
672 in = fopen(options.input, "r");
673 if (!in) {
674 perror(options.input);
675 usage();
676 exit(1);
677 }
678 }
679 if (options.output) {
680 out = fopen(options.output, "w");
681 if (!out) {
682 perror(options.output);
683 usage();
684 exit(1);
685 }
686 }
687
688 ret = process_file(in, out);
689
690 fclose(in);
691 fclose(out);
692
693 printf("Converted %d records with %d failures\n", ret.count, ret.failures);
694
695 return 0;
696}
Note: See TracBrowser for help on using the repository browser.