4 Copyright (C) Andrew Bartlett 2006
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: ad2oLschema
29 * Description: utility to convert an AD schema into the format required by OpenLDAP
31 * Author: Andrew Bartlett
35 #include "ldb_includes.h"
36 #include "system/locale.h"
37 #include "tools/cmdline.h"
38 #include "tools/convert.h"
39 #include "param/param.h"
40 #include "lib/cmdline/popt_common.h"
54 static void usage(void)
56 printf("Usage: ad2oLschema <options>\n");
57 printf("\nConvert AD-like LDIF to OpenLDAP schema format\n\n");
59 printf(" -I inputfile inputfile of mapped OIDs and skipped attributes/ObjectClasses");
60 printf(" -H url LDB or LDAP server to read schmea from\n");
61 printf(" -O outputfile outputfile otherwise STDOUT\n");
62 printf(" -o options pass options like modules to activate\n");
63 printf(" e.g: -o modules:timestamps\n");
65 printf("Converts records from an AD-like LDIF schema into an openLdap formatted schema\n\n");
69 static int fetch_attrs_schema(struct ldb_context *ldb, struct ldb_dn *schemadn,
71 struct ldb_result **attrs_res)
73 TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
75 const char *attrs[] = {
85 return LDB_ERR_OPERATIONS_ERROR;
89 ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE,
90 "objectClass=attributeSchema",
92 if (ret != LDB_SUCCESS) {
93 printf("Search failed: %s\n", ldb_errstring(ldb));
94 return LDB_ERR_OPERATIONS_ERROR;
100 static const char *oc_attrs[] = {
106 "objectClassCategory",
113 static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn,
115 struct ldb_result *search_from,
116 struct ldb_result *res_list)
120 for (i=0; i < search_from->count; i++) {
121 struct ldb_result *res;
122 const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i],
123 "lDAPDisplayname", NULL);
125 ret = ldb_search_exp_fmt(ldb, mem_ctx, &res,
126 schemadn, LDB_SCOPE_SUBTREE, oc_attrs,
127 "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))",
129 if (ret != LDB_SUCCESS) {
130 printf("Search failed: %s\n", ldb_errstring(ldb));
134 res_list->msgs = talloc_realloc(res_list, res_list->msgs,
135 struct ldb_message *, res_list->count + 2);
136 if (!res_list->msgs) {
137 return LDB_ERR_OPERATIONS_ERROR;
139 res_list->msgs[res_list->count] = talloc_move(res_list,
140 &search_from->msgs[i]);
142 res_list->msgs[res_list->count] = NULL;
144 if (res->count > 0) {
145 ret = fetch_oc_recursive(ldb, schemadn, mem_ctx, res, res_list);
147 if (ret != LDB_SUCCESS) {
154 static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *schemadn,
156 struct ldb_result **objectclasses_res)
158 TALLOC_CTX *local_ctx = talloc_new(mem_ctx);
159 struct ldb_result *top_res, *ret_res;
162 return LDB_ERR_OPERATIONS_ERROR;
166 ret = ldb_search(ldb, schemadn, LDB_SCOPE_SUBTREE,
167 "(&(objectClass=classSchema)(lDAPDisplayName=top))",
169 if (ret != LDB_SUCCESS) {
170 printf("Search failed: %s\n", ldb_errstring(ldb));
171 return LDB_ERR_OPERATIONS_ERROR;
174 talloc_steal(local_ctx, top_res);
176 if (top_res->count != 1) {
177 return LDB_ERR_OPERATIONS_ERROR;
180 ret_res = talloc_zero(local_ctx, struct ldb_result);
182 return LDB_ERR_OPERATIONS_ERROR;
185 ret = fetch_oc_recursive(ldb, schemadn, local_ctx, top_res, ret_res);
187 if (ret != LDB_SUCCESS) {
188 printf("Search failed: %s\n", ldb_errstring(ldb));
189 return LDB_ERR_OPERATIONS_ERROR;
192 *objectclasses_res = talloc_move(mem_ctx, &ret_res);
196 static struct ldb_dn *find_schema_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
198 const char *rootdse_attrs[] = {"schemaNamingContext", NULL};
199 const char *no_attrs[] = { NULL };
200 struct ldb_dn *schemadn;
201 struct ldb_dn *basedn = ldb_dn_new(mem_ctx, ldb, NULL);
202 struct ldb_result *rootdse_res;
203 struct ldb_result *schema_res;
209 /* Search for rootdse */
210 ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_BASE, NULL, rootdse_attrs, &rootdse_res);
211 if (ldb_ret != LDB_SUCCESS) {
212 ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE,
213 "(&(objectClass=dMD)(cn=Schema))",
214 no_attrs, &schema_res);
216 printf("cn=Schema Search failed: %s\n", ldb_errstring(ldb));
220 talloc_steal(mem_ctx, schema_res);
222 if (schema_res->count != 1) {
223 printf("Failed to find rootDSE");
227 schemadn = talloc_steal(mem_ctx, schema_res->msgs[0]->dn);
228 talloc_free(schema_res);
233 talloc_steal(mem_ctx, rootdse_res);
235 if (rootdse_res->count != 1) {
236 printf("Failed to find rootDSE");
241 schemadn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, rootdse_res->msgs[0], "schemaNamingContext");
246 talloc_free(rootdse_res);
250 #define IF_NULL_FAIL_RET(x) do { \
258 static struct schema_conv process_convert(struct ldb_context *ldb, enum convert_target target, FILE *in, FILE *out)
260 /* Read list of attributes to skip, OIDs to map */
261 TALLOC_CTX *mem_ctx = talloc_new(ldb);
263 const char **attrs_skip = NULL;
269 int num_oid_maps = 0;
274 int num_attr_maps = 0;
275 struct ldb_result *attrs_res, *objectclasses_res;
276 struct ldb_dn *schemadn;
277 struct schema_conv ret;
285 while ((line = afdgets(fileno(in), mem_ctx, 0))) {
287 if (line[0] == '\0') {
291 if (line[0] == '#') {
294 if (isdigit(line[0])) {
295 char *p = strchr(line, ':');
299 oid_map = talloc_realloc(mem_ctx, oid_map, struct oid_map, num_oid_maps + 2);
300 trim_string(line, " ", " ");
301 oid_map[num_oid_maps].old_oid = talloc_move(oid_map, &line);
302 trim_string(p, " ", " ");
303 oid_map[num_oid_maps].new_oid = p;
305 oid_map[num_oid_maps].old_oid = NULL;
307 char *p = strchr(line, ':');
309 /* remap attribute/objectClass */
312 attr_map = talloc_realloc(mem_ctx, attr_map, struct attr_map, num_attr_maps + 2);
313 trim_string(line, " ", " ");
314 attr_map[num_attr_maps].old_attr = talloc_move(attr_map, &line);
315 trim_string(p, " ", " ");
316 attr_map[num_attr_maps].new_attr = p;
318 attr_map[num_attr_maps].old_attr = NULL;
320 /* skip attribute/objectClass */
321 attrs_skip = talloc_realloc(mem_ctx, attrs_skip, const char *, num_skip + 2);
322 trim_string(line, " ", " ");
323 attrs_skip[num_skip] = talloc_move(attrs_skip, &line);
325 attrs_skip[num_skip] = NULL;
330 schemadn = find_schema_dn(ldb, mem_ctx);
332 printf("Failed to find schema DN: %s\n", ldb_errstring(ldb));
337 ldb_ret = fetch_attrs_schema(ldb, schemadn, mem_ctx, &attrs_res);
338 if (ldb_ret != LDB_SUCCESS) {
339 printf("Failed to fetch attribute schema: %s\n", ldb_errstring(ldb));
345 case TARGET_OPENLDAP:
347 case TARGET_FEDORA_DS:
348 fprintf(out, "dn: cn=schema\n");
352 for (i=0; i < attrs_res->count; i++) {
353 struct ldb_message *msg = attrs_res->msgs[i];
355 const char *name = ldb_msg_find_attr_as_string(msg, "lDAPDisplayName", NULL);
356 const char *description = ldb_msg_find_attr_as_string(msg, "description", NULL);
357 const char *oid = ldb_msg_find_attr_as_string(msg, "attributeID", NULL);
358 const char *syntax = ldb_msg_find_attr_as_string(msg, "attributeSyntax", NULL);
359 bool single_value = ldb_msg_find_attr_as_bool(msg, "isSingleValued", false);
360 const struct syntax_map *map = find_syntax_map_by_ad_oid(syntax);
361 char *schema_entry = NULL;
365 printf("Failed to find lDAPDisplayName for schema DN: %s\n", ldb_dn_get_linearized(msg->dn));
370 /* We have been asked to skip some attributes/objectClasses */
371 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
376 /* We might have been asked to remap this oid, due to a conflict */
377 for (j=0; oid && oid_map && oid_map[j].old_oid; j++) {
378 if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
379 oid = oid_map[j].new_oid;
385 case TARGET_OPENLDAP:
386 schema_entry = talloc_asprintf(mem_ctx,
390 case TARGET_FEDORA_DS:
391 schema_entry = talloc_asprintf(mem_ctx,
392 "attributeTypes: (\n"
396 IF_NULL_FAIL_RET(schema_entry);
398 /* We might have been asked to remap this name, due to a conflict */
399 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
400 if (strcasecmp(name, attr_map[j].old_attr) == 0) {
401 name = attr_map[j].new_attr;
406 schema_entry = talloc_asprintf_append(schema_entry,
407 " NAME '%s'\n", name);
408 IF_NULL_FAIL_RET(schema_entry);
411 schema_entry = talloc_asprintf_append(schema_entry,
412 " DESC %s\n", description);
413 IF_NULL_FAIL_RET(schema_entry);
417 const char *syntax_oid;
419 schema_entry = talloc_asprintf_append(schema_entry,
420 " EQUALITY %s\n", map->equality);
421 IF_NULL_FAIL_RET(schema_entry);
423 if (map->substring) {
424 schema_entry = talloc_asprintf_append(schema_entry,
425 " SUBSTR %s\n", map->substring);
426 IF_NULL_FAIL_RET(schema_entry);
428 syntax_oid = map->Standard_OID;
429 /* We might have been asked to remap this oid,
430 * due to a conflict, or lack of
432 for (j=0; syntax_oid && oid_map[j].old_oid; j++) {
433 if (strcasecmp(syntax_oid, oid_map[j].old_oid) == 0) {
434 syntax_oid = oid_map[j].new_oid;
438 schema_entry = talloc_asprintf_append(schema_entry,
439 " SYNTAX %s\n", syntax_oid);
440 IF_NULL_FAIL_RET(schema_entry);
444 schema_entry = talloc_asprintf_append(schema_entry,
446 IF_NULL_FAIL_RET(schema_entry);
449 schema_entry = talloc_asprintf_append(schema_entry,
453 case TARGET_OPENLDAP:
454 fprintf(out, "%s\n\n", schema_entry);
456 case TARGET_FEDORA_DS:
457 fprintf(out, "%s\n", schema_entry);
463 ldb_ret = fetch_objectclass_schema(ldb, schemadn, mem_ctx, &objectclasses_res);
464 if (ldb_ret != LDB_SUCCESS) {
465 printf("Failed to fetch objectClass schema elements: %s\n", ldb_errstring(ldb));
470 for (i=0; i < objectclasses_res->count; i++) {
471 struct ldb_message *msg = objectclasses_res->msgs[i];
472 const char *name = ldb_msg_find_attr_as_string(msg, "lDAPDisplayName", NULL);
473 const char *description = ldb_msg_find_attr_as_string(msg, "description", NULL);
474 const char *oid = ldb_msg_find_attr_as_string(msg, "governsID", NULL);
475 const char *subClassOf = ldb_msg_find_attr_as_string(msg, "subClassOf", NULL);
476 int objectClassCategory = ldb_msg_find_attr_as_int(msg, "objectClassCategory", 0);
477 struct ldb_message_element *must = ldb_msg_find_element(msg, "mustContain");
478 struct ldb_message_element *sys_must = ldb_msg_find_element(msg, "systemMustContain");
479 struct ldb_message_element *may = ldb_msg_find_element(msg, "mayContain");
480 struct ldb_message_element *sys_may = ldb_msg_find_element(msg, "systemMayContain");
481 char *schema_entry = NULL;
485 printf("Failed to find lDAPDisplayName for schema DN: %s\n", ldb_dn_get_linearized(msg->dn));
490 /* We have been asked to skip some attributes/objectClasses */
491 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
496 /* We might have been asked to remap this oid, due to a conflict */
497 for (j=0; oid_map[j].old_oid; j++) {
498 if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
499 oid = oid_map[j].new_oid;
505 case TARGET_OPENLDAP:
506 schema_entry = talloc_asprintf(mem_ctx,
510 case TARGET_FEDORA_DS:
511 schema_entry = talloc_asprintf(mem_ctx,
516 IF_NULL_FAIL_RET(schema_entry);
522 /* We might have been asked to remap this name, due to a conflict */
523 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
524 if (strcasecmp(name, attr_map[j].old_attr) == 0) {
525 name = attr_map[j].new_attr;
530 schema_entry = talloc_asprintf_append(schema_entry,
531 " NAME '%s'\n", name);
532 IF_NULL_FAIL_RET(schema_entry);
534 if (!schema_entry) return ret;
537 schema_entry = talloc_asprintf_append(schema_entry,
538 " DESC %s\n", description);
539 IF_NULL_FAIL_RET(schema_entry);
543 schema_entry = talloc_asprintf_append(schema_entry,
544 " SUP %s\n", subClassOf);
545 IF_NULL_FAIL_RET(schema_entry);
548 switch (objectClassCategory) {
550 schema_entry = talloc_asprintf_append(schema_entry,
552 IF_NULL_FAIL_RET(schema_entry);
555 schema_entry = talloc_asprintf_append(schema_entry,
557 IF_NULL_FAIL_RET(schema_entry);
560 schema_entry = talloc_asprintf_append(schema_entry,
562 IF_NULL_FAIL_RET(schema_entry);
566 #define APPEND_ATTRS(attributes) \
569 for (k=0; attributes && k < attributes->num_values; k++) { \
571 const char *attr_name = (const char *)attributes->values[k].data; \
572 /* We might have been asked to remap this name, due to a conflict */ \
573 for (attr_idx=0; attr_name && attr_map && attr_map[attr_idx].old_attr; attr_idx++) { \
574 if (strcasecmp(attr_name, attr_map[attr_idx].old_attr) == 0) { \
575 attr_name = attr_map[attr_idx].new_attr; \
580 schema_entry = talloc_asprintf_append(schema_entry, \
583 IF_NULL_FAIL_RET(schema_entry); \
584 if (k != (attributes->num_values - 1)) { \
585 schema_entry = talloc_asprintf_append(schema_entry, \
587 IF_NULL_FAIL_RET(schema_entry); \
588 if (target == TARGET_OPENLDAP && ((k+1)%5 == 0)) { \
589 schema_entry = talloc_asprintf_append(schema_entry, \
591 IF_NULL_FAIL_RET(schema_entry); \
597 if (must || sys_must) {
598 schema_entry = talloc_asprintf_append(schema_entry,
600 IF_NULL_FAIL_RET(schema_entry);
603 if (must && sys_must) {
604 schema_entry = talloc_asprintf_append(schema_entry, \
607 APPEND_ATTRS(sys_must);
609 schema_entry = talloc_asprintf_append(schema_entry,
611 IF_NULL_FAIL_RET(schema_entry);
614 if (may || sys_may) {
615 schema_entry = talloc_asprintf_append(schema_entry,
617 IF_NULL_FAIL_RET(schema_entry);
620 if (may && sys_may) {
621 schema_entry = talloc_asprintf_append(schema_entry, \
624 APPEND_ATTRS(sys_may);
626 schema_entry = talloc_asprintf_append(schema_entry,
628 IF_NULL_FAIL_RET(schema_entry);
631 schema_entry = talloc_asprintf_append(schema_entry,
635 case TARGET_OPENLDAP:
636 fprintf(out, "%s\n\n", schema_entry);
638 case TARGET_FEDORA_DS:
639 fprintf(out, "%s\n", schema_entry);
648 int main(int argc, const char **argv)
651 struct ldb_cmdline *options;
654 struct ldb_context *ldb;
655 struct schema_conv ret;
656 const char *target_str;
657 enum convert_target target;
661 ctx = talloc_new(NULL);
664 options = ldb_cmdline_process(ldb, argc, argv, usage);
666 if (options->input) {
667 in = fopen(options->input, "r");
669 perror(options->input);
673 if (options->output) {
674 out = fopen(options->output, "w");
676 perror(options->output);
681 target_str = lp_parm_string(cmdline_lp_ctx, NULL, "convert", "target");
683 if (!target_str || strcasecmp(target_str, "openldap") == 0) {
684 target = TARGET_OPENLDAP;
685 } else if (strcasecmp(target_str, "fedora-ds") == 0) {
686 target = TARGET_FEDORA_DS;
688 printf("Unsupported target: %s\n", target_str);
692 ret = process_convert(ldb, target, in, out);
697 printf("Converted %d records (skipped %d) with %d failures\n", ret.count, ret.skipped, ret.failures);