ad2olschema.c
来自「samba最新软件」· C语言 代码 · 共 699 行 · 第 1/2 页
C
699 行
for (i=0; i < attrs_res->count; i++) { struct ldb_message *msg = attrs_res->msgs[i]; const char *name = ldb_msg_find_attr_as_string(msg, "lDAPDisplayName", NULL); const char *description = ldb_msg_find_attr_as_string(msg, "description", NULL); const char *oid = ldb_msg_find_attr_as_string(msg, "attributeID", NULL); const char *syntax = ldb_msg_find_attr_as_string(msg, "attributeSyntax", NULL); bool single_value = ldb_msg_find_attr_as_bool(msg, "isSingleValued", false); const struct syntax_map *map = find_syntax_map_by_ad_oid(syntax); char *schema_entry = NULL; int j; if (!name) { printf("Failed to find lDAPDisplayName for schema DN: %s\n", ldb_dn_get_linearized(msg->dn)); ret.failures++; continue; } /* We have been asked to skip some attributes/objectClasses */ if (attrs_skip && str_list_check_ci(attrs_skip, name)) { ret.skipped++; continue; } /* We might have been asked to remap this oid, due to a conflict */ for (j=0; oid && oid_map && oid_map[j].old_oid; j++) { if (strcasecmp(oid, oid_map[j].old_oid) == 0) { oid = oid_map[j].new_oid; break; } } switch (target) { case TARGET_OPENLDAP: schema_entry = talloc_asprintf(mem_ctx, "attributetype (\n" " %s\n", oid); break; case TARGET_FEDORA_DS: schema_entry = talloc_asprintf(mem_ctx, "attributeTypes: (\n" " %s\n", oid); break; } IF_NULL_FAIL_RET(schema_entry); /* We might have been asked to remap this name, due to a conflict */ for (j=0; name && attr_map && attr_map[j].old_attr; j++) { if (strcasecmp(name, attr_map[j].old_attr) == 0) { name = attr_map[j].new_attr; break; } } schema_entry = talloc_asprintf_append(schema_entry, " NAME '%s'\n", name); IF_NULL_FAIL_RET(schema_entry); if (description) { schema_entry = talloc_asprintf_append(schema_entry, " DESC %s\n", description); IF_NULL_FAIL_RET(schema_entry); } if (map) { const char *syntax_oid; if (map->equality) { schema_entry = talloc_asprintf_append(schema_entry, " EQUALITY %s\n", map->equality); IF_NULL_FAIL_RET(schema_entry); } if (map->substring) { schema_entry = talloc_asprintf_append(schema_entry, " SUBSTR %s\n", map->substring); IF_NULL_FAIL_RET(schema_entry); } syntax_oid = map->Standard_OID; /* We might have been asked to remap this oid, * due to a conflict, or lack of * implementation */ for (j=0; syntax_oid && oid_map && oid_map[j].old_oid; j++) { if (strcasecmp(syntax_oid, oid_map[j].old_oid) == 0) { syntax_oid = oid_map[j].new_oid; break; } } schema_entry = talloc_asprintf_append(schema_entry, " SYNTAX %s\n", syntax_oid); IF_NULL_FAIL_RET(schema_entry); } if (single_value) { schema_entry = talloc_asprintf_append(schema_entry, " SINGLE-VALUE\n"); IF_NULL_FAIL_RET(schema_entry); } schema_entry = talloc_asprintf_append(schema_entry, " )"); switch (target) { case TARGET_OPENLDAP: fprintf(out, "%s\n\n", schema_entry); break; case TARGET_FEDORA_DS: fprintf(out, "%s\n", schema_entry); break; } ret.count++; } ldb_ret = fetch_objectclass_schema(ldb, schemadn, mem_ctx, &objectclasses_res); if (ldb_ret != LDB_SUCCESS) { printf("Failed to fetch objectClass schema elements: %s\n", ldb_errstring(ldb)); ret.failures = 1; return ret; } for (i=0; i < objectclasses_res->count; i++) { struct ldb_message *msg = objectclasses_res->msgs[i]; const char *name = ldb_msg_find_attr_as_string(msg, "lDAPDisplayName", NULL); const char *description = ldb_msg_find_attr_as_string(msg, "description", NULL); const char *oid = ldb_msg_find_attr_as_string(msg, "governsID", NULL); const char *subClassOf = ldb_msg_find_attr_as_string(msg, "subClassOf", NULL); int objectClassCategory = ldb_msg_find_attr_as_int(msg, "objectClassCategory", 0); struct ldb_message_element *must = ldb_msg_find_element(msg, "mustContain"); struct ldb_message_element *sys_must = ldb_msg_find_element(msg, "systemMustContain"); struct ldb_message_element *may = ldb_msg_find_element(msg, "mayContain"); struct ldb_message_element *sys_may = ldb_msg_find_element(msg, "systemMayContain"); char *schema_entry = NULL; int j; if (!name) { printf("Failed to find lDAPDisplayName for schema DN: %s\n", ldb_dn_get_linearized(msg->dn)); ret.failures++; continue; } /* We have been asked to skip some attributes/objectClasses */ if (attrs_skip && str_list_check_ci(attrs_skip, name)) { ret.skipped++; continue; } /* We might have been asked to remap this oid, due to a conflict */ for (j=0; oid_map && oid_map[j].old_oid; j++) { if (strcasecmp(oid, oid_map[j].old_oid) == 0) { oid = oid_map[j].new_oid; break; } } switch (target) { case TARGET_OPENLDAP: schema_entry = talloc_asprintf(mem_ctx, "objectclass (\n" " %s\n", oid); break; case TARGET_FEDORA_DS: schema_entry = talloc_asprintf(mem_ctx, "objectClasses: (\n" " %s\n", oid); break; } IF_NULL_FAIL_RET(schema_entry); if (!schema_entry) { ret.failures++; break; } /* We might have been asked to remap this name, due to a conflict */ for (j=0; name && attr_map && attr_map[j].old_attr; j++) { if (strcasecmp(name, attr_map[j].old_attr) == 0) { name = attr_map[j].new_attr; break; } } schema_entry = talloc_asprintf_append(schema_entry, " NAME '%s'\n", name); IF_NULL_FAIL_RET(schema_entry); if (!schema_entry) return ret; if (description) { schema_entry = talloc_asprintf_append(schema_entry, " DESC %s\n", description); IF_NULL_FAIL_RET(schema_entry); } if (subClassOf) { schema_entry = talloc_asprintf_append(schema_entry, " SUP %s\n", subClassOf); IF_NULL_FAIL_RET(schema_entry); } switch (objectClassCategory) { case 1: schema_entry = talloc_asprintf_append(schema_entry, " STRUCTURAL\n"); IF_NULL_FAIL_RET(schema_entry); break; case 2: schema_entry = talloc_asprintf_append(schema_entry, " ABSTRACT\n"); IF_NULL_FAIL_RET(schema_entry); break; case 3: schema_entry = talloc_asprintf_append(schema_entry, " AUXILIARY\n"); IF_NULL_FAIL_RET(schema_entry); break; }#define APPEND_ATTRS(attributes) \ do { \ int k; \ for (k=0; attributes && k < attributes->num_values; k++) { \ int attr_idx; \ const char *attr_name = (const char *)attributes->values[k].data; \ /* We might have been asked to remap this name, due to a conflict */ \ for (attr_idx=0; attr_name && attr_map && attr_map[attr_idx].old_attr; attr_idx++) { \ if (strcasecmp(attr_name, attr_map[attr_idx].old_attr) == 0) { \ attr_name = attr_map[attr_idx].new_attr; \ break; \ } \ } \ \ schema_entry = talloc_asprintf_append(schema_entry, \ " %s", \ attr_name); \ IF_NULL_FAIL_RET(schema_entry); \ if (k != (attributes->num_values - 1)) { \ schema_entry = talloc_asprintf_append(schema_entry, \ " $"); \ IF_NULL_FAIL_RET(schema_entry); \ if (target == TARGET_OPENLDAP && ((k+1)%5 == 0)) { \ schema_entry = talloc_asprintf_append(schema_entry, \ "\n "); \ IF_NULL_FAIL_RET(schema_entry); \ } \ } \ } \ } while (0) if (must || sys_must) { schema_entry = talloc_asprintf_append(schema_entry, " MUST ("); IF_NULL_FAIL_RET(schema_entry); APPEND_ATTRS(must); if (must && sys_must) { schema_entry = talloc_asprintf_append(schema_entry, \ " $"); \ } APPEND_ATTRS(sys_must); schema_entry = talloc_asprintf_append(schema_entry, " )\n"); IF_NULL_FAIL_RET(schema_entry); } if (may || sys_may) { schema_entry = talloc_asprintf_append(schema_entry, " MAY ("); IF_NULL_FAIL_RET(schema_entry); APPEND_ATTRS(may); if (may && sys_may) { schema_entry = talloc_asprintf_append(schema_entry, \ " $"); \ } APPEND_ATTRS(sys_may); schema_entry = talloc_asprintf_append(schema_entry, " )\n"); IF_NULL_FAIL_RET(schema_entry); } schema_entry = talloc_asprintf_append(schema_entry, " )"); switch (target) { case TARGET_OPENLDAP: fprintf(out, "%s\n\n", schema_entry); break; case TARGET_FEDORA_DS: fprintf(out, "%s\n", schema_entry); break; } ret.count++; } return ret;} int main(int argc, const char **argv){ TALLOC_CTX *ctx; struct ldb_cmdline *options; FILE *in = stdin; FILE *out = stdout; struct ldb_context *ldb; struct schema_conv ret; const char *target_str; enum convert_target target; ctx = talloc_new(NULL); ldb = ldb_init(ctx); options = ldb_cmdline_process(ldb, argc, argv, usage); if (options->input) { in = fopen(options->input, "r"); if (!in) { perror(options->input); exit(1); } } if (options->output) { out = fopen(options->output, "w"); if (!out) { perror(options->output); exit(1); } } target_str = lp_parm_string(cmdline_lp_ctx, NULL, "convert", "target"); if (!target_str || strcasecmp(target_str, "openldap") == 0) { target = TARGET_OPENLDAP; } else if (strcasecmp(target_str, "fedora-ds") == 0) { target = TARGET_FEDORA_DS; } else { printf("Unsupported target: %s\n", target_str); exit(1); } ret = process_convert(ldb, target, in, out); fclose(in); fclose(out); printf("Converted %d records (skipped %d) with %d failures\n", ret.count, ret.skipped, ret.failures); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?