s4:schema - Change also here counters to "unsigned" where needed
[Samba/cd1.git] / source4 / dsdb / schema / schema_convert_to_ol.c
blobff2595cb72519a8acaed7660b45028f8e3183329
1 /*
2 schema conversion routines
4 Copyright (C) Andrew Bartlett 2006-2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "ldb.h"
23 #include "dsdb/samdb/samdb.h"
24 #include "system/locale.h"
26 #define SEPERATOR "\n "
28 struct attr_map {
29 char *old_attr;
30 char *new_attr;
33 struct oid_map {
34 char *old_oid;
35 char *new_oid;
38 static char *print_schema_recursive(char *append_to_string, struct dsdb_schema *schema, const char *print_class,
39 enum dsdb_schema_convert_target target,
40 const char **attrs_skip, const struct attr_map *attr_map, const struct oid_map *oid_map)
42 char *out = append_to_string;
43 const struct dsdb_class *objectclass;
44 objectclass = dsdb_class_by_lDAPDisplayName(schema, print_class);
45 if (!objectclass) {
46 DEBUG(0, ("Cannot find class %s in schema\n", print_class));
47 return NULL;
50 do {
51 TALLOC_CTX *mem_ctx = talloc_new(append_to_string);
52 const char *name = objectclass->lDAPDisplayName;
53 const char *oid = objectclass->governsID_oid;
54 const char *subClassOf = objectclass->subClassOf;
55 int objectClassCategory = objectclass->objectClassCategory;
56 const char **must;
57 const char **may;
58 char *schema_entry = NULL;
59 struct ldb_val objectclass_name_as_ldb_val = data_blob_string_const(objectclass->lDAPDisplayName);
60 struct ldb_message_element objectclass_name_as_el = {
61 .name = "objectClass",
62 .num_values = 1,
63 .values = &objectclass_name_as_ldb_val
65 unsigned int j;
66 unsigned int attr_idx;
68 if (!mem_ctx) {
69 DEBUG(0, ("Failed to create new talloc context\n"));
70 return NULL;
73 /* We have been asked to skip some attributes/objectClasses */
74 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
75 continue;
78 /* We might have been asked to remap this oid, due to a conflict */
79 for (j=0; oid_map && oid_map[j].old_oid; j++) {
80 if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
81 oid = oid_map[j].new_oid;
82 break;
86 /* We might have been asked to remap this name, due to a conflict */
87 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
88 if (strcasecmp(name, attr_map[j].old_attr) == 0) {
89 name = attr_map[j].new_attr;
90 break;
94 /* We might have been asked to remap this subClassOf, due to a conflict */
95 for (j=0; subClassOf && attr_map && attr_map[j].old_attr; j++) {
96 if (strcasecmp(subClassOf, attr_map[j].old_attr) == 0) {
97 subClassOf = attr_map[j].new_attr;
98 break;
102 may = dsdb_full_attribute_list(mem_ctx, schema, &objectclass_name_as_el, DSDB_SCHEMA_ALL_MAY);
104 for (j=0; may && may[j]; j++) {
105 /* We might have been asked to remap this name, due to a conflict */
106 for (attr_idx=0; attr_map && attr_map[attr_idx].old_attr; attr_idx++) {
107 if (strcasecmp(may[j], attr_map[attr_idx].old_attr) == 0) {
108 may[j] = attr_map[attr_idx].new_attr;
109 break;
114 must = dsdb_full_attribute_list(mem_ctx, schema, &objectclass_name_as_el, DSDB_SCHEMA_ALL_MUST);
116 for (j=0; must && must[j]; j++) {
117 /* We might have been asked to remap this name, due to a conflict */
118 for (attr_idx=0; attr_map && attr_map[attr_idx].old_attr; attr_idx++) {
119 if (strcasecmp(must[j], attr_map[attr_idx].old_attr) == 0) {
120 must[j] = attr_map[attr_idx].new_attr;
121 break;
126 schema_entry = schema_class_description(mem_ctx, target,
127 SEPERATOR,
128 oid,
129 name,
130 NULL,
131 subClassOf,
132 objectClassCategory,
133 must,
134 may,
135 NULL);
136 if (schema_entry == NULL) {
137 DEBUG(0, ("failed to generate schema description for %s\n", name));
138 return NULL;
141 switch (target) {
142 case TARGET_OPENLDAP:
143 out = talloc_asprintf_append(out, "objectclass %s\n\n", schema_entry);
144 break;
145 case TARGET_FEDORA_DS:
146 out = talloc_asprintf_append(out, "objectClasses: %s\n", schema_entry);
147 break;
149 talloc_free(mem_ctx);
150 } while (0);
153 for (objectclass=schema->classes; objectclass; objectclass = objectclass->next) {
154 if (ldb_attr_cmp(objectclass->subClassOf, print_class) == 0
155 && ldb_attr_cmp(objectclass->lDAPDisplayName, print_class) != 0) {
156 out = print_schema_recursive(out, schema, objectclass->lDAPDisplayName,
157 target, attrs_skip, attr_map, oid_map);
160 return out;
163 /* Routine to linearise our internal schema into the format that
164 OpenLDAP and Fedora DS use for their backend.
166 The 'mappings' are of a format like:
168 #Standard OpenLDAP attributes
169 labeledURI
170 #The memberOf plugin provides this attribute
171 memberOf
172 #These conflict with OpenLDAP builtins
173 attributeTypes:samba4AttributeTypes
174 2.5.21.5:1.3.6.1.4.1.7165.4.255.7
179 char *dsdb_convert_schema_to_openldap(struct ldb_context *ldb, char *target_str, const char *mappings)
181 /* Read list of attributes to skip, OIDs to map */
182 TALLOC_CTX *mem_ctx = talloc_new(ldb);
183 char *line;
184 char *out;
185 const char **attrs_skip = NULL;
186 unsigned int num_skip = 0;
187 struct oid_map *oid_map = NULL;
188 unsigned int num_oid_maps = 0;
189 struct attr_map *attr_map = NULL;
190 unsigned int num_attr_maps = 0;
191 struct dsdb_attribute *attribute;
192 struct dsdb_schema *schema;
193 enum dsdb_schema_convert_target target;
195 char *next_line = talloc_strdup(mem_ctx, mappings);
197 if (!target_str || strcasecmp(target_str, "openldap") == 0) {
198 target = TARGET_OPENLDAP;
199 } else if (strcasecmp(target_str, "fedora-ds") == 0) {
200 target = TARGET_FEDORA_DS;
201 } else {
202 DEBUG(0, ("Invalid target type for schema conversion %s\n", target_str));
203 return NULL;
206 /* The mappings are line-seperated, and specify details such as OIDs to skip etc */
207 while (1) {
208 line = next_line;
209 next_line = strchr(line, '\n');
210 if (!next_line) {
211 break;
213 next_line[0] = '\0';
214 next_line++;
216 /* Blank Line */
217 if (line[0] == '\0') {
218 continue;
220 /* Comment */
221 if (line[0] == '#') {
222 continue;
225 if (isdigit(line[0])) {
226 char *p = strchr(line, ':');
227 if (!p) {
228 DEBUG(0, ("schema mapping file line has OID but no OID to map to: %s\n", line));
229 return NULL;
231 p[0] = '\0';
232 p++;
233 oid_map = talloc_realloc(mem_ctx, oid_map, struct oid_map, num_oid_maps + 2);
234 trim_string(line, " ", " ");
235 oid_map[num_oid_maps].old_oid = talloc_strdup(oid_map, line);
236 trim_string(p, " ", " ");
237 oid_map[num_oid_maps].new_oid = p;
238 num_oid_maps++;
239 oid_map[num_oid_maps].old_oid = NULL;
240 } else {
241 char *p = strchr(line, ':');
242 if (p) {
243 /* remap attribute/objectClass */
244 p[0] = '\0';
245 p++;
246 attr_map = talloc_realloc(mem_ctx, attr_map, struct attr_map, num_attr_maps + 2);
247 trim_string(line, " ", " ");
248 attr_map[num_attr_maps].old_attr = talloc_strdup(attr_map, line);
249 trim_string(p, " ", " ");
250 attr_map[num_attr_maps].new_attr = p;
251 num_attr_maps++;
252 attr_map[num_attr_maps].old_attr = NULL;
253 } else {
254 /* skip attribute/objectClass */
255 attrs_skip = talloc_realloc(mem_ctx, attrs_skip, const char *, num_skip + 2);
256 trim_string(line, " ", " ");
257 attrs_skip[num_skip] = talloc_strdup(attrs_skip, line);
258 num_skip++;
259 attrs_skip[num_skip] = NULL;
264 schema = dsdb_get_schema(ldb);
265 if (!schema) {
266 DEBUG(0, ("No schema on ldb to convert!\n"));
267 return NULL;
270 switch (target) {
271 case TARGET_OPENLDAP:
272 out = talloc_strdup(mem_ctx, "");
273 break;
274 case TARGET_FEDORA_DS:
275 out = talloc_strdup(mem_ctx, "dn: cn=schema\n");
276 break;
279 for (attribute=schema->attributes; attribute; attribute = attribute->next) {
280 const char *name = attribute->lDAPDisplayName;
281 const char *oid = attribute->attributeID_oid;
282 const char *syntax = attribute->attributeSyntax_oid;
283 const char *equality = NULL, *substring = NULL;
284 bool single_value = attribute->isSingleValued;
286 char *schema_entry = NULL;
287 unsigned int j;
289 /* We have been asked to skip some attributes/objectClasses */
290 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
291 continue;
294 /* We might have been asked to remap this oid, due to a conflict */
295 for (j=0; oid && oid_map && oid_map[j].old_oid; j++) {
296 if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
297 oid = oid_map[j].new_oid;
298 break;
302 if (attribute->syntax) {
303 /* We might have been asked to remap this oid,
304 * due to a conflict, or lack of
305 * implementation */
306 syntax = attribute->syntax->ldap_oid;
307 /* We might have been asked to remap this oid, due to a conflict */
308 for (j=0; syntax && oid_map && oid_map[j].old_oid; j++) {
309 if (strcasecmp(syntax, oid_map[j].old_oid) == 0) {
310 syntax = oid_map[j].new_oid;
311 break;
315 equality = attribute->syntax->equality;
316 substring = attribute->syntax->substring;
319 /* We might have been asked to remap this name, due to a conflict */
320 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
321 if (strcasecmp(name, attr_map[j].old_attr) == 0) {
322 name = attr_map[j].new_attr;
323 break;
327 schema_entry = schema_attribute_description(mem_ctx,
328 target,
329 SEPERATOR,
330 oid,
331 name,
332 equality,
333 substring,
334 syntax,
335 single_value,
336 false,
337 NULL, NULL,
338 NULL, NULL,
339 false, false);
341 if (schema_entry == NULL) {
342 DEBUG(0, ("failed to generate attribute description for %s\n", name));
343 return NULL;
346 switch (target) {
347 case TARGET_OPENLDAP:
348 out = talloc_asprintf_append(out, "attributetype %s\n\n", schema_entry);
349 break;
350 case TARGET_FEDORA_DS:
351 out = talloc_asprintf_append(out, "attributeTypes: %s\n", schema_entry);
352 break;
356 out = print_schema_recursive(out, schema, "top", target, attrs_skip, attr_map, oid_map);
358 return out;