Merge the two attribute syntax tables.
[tprouty/samba.git] / source4 / utils / ad2oLschema.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett 2006-2008
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: ad2oLschema
28  *
29  *  Description: utility to convert an AD schema into the format required by OpenLDAP
30  *
31  *  Author: Andrew Bartlett
32  */
33
34 #include "includes.h"
35 #include "ldb_includes.h"
36 #include "system/locale.h"
37 #include "lib/ldb/tools/cmdline.h"
38 #include "param/param.h"
39 #include "lib/cmdline/popt_common.h"
40 #include "dsdb/samdb/samdb.h"
41
42 struct schema_conv {
43         int count;
44         int skipped;
45         int failures;
46 };
47         
48
49 static void usage(void)
50 {
51         printf("Usage: ad2oLschema <options>\n");
52         printf("\nConvert AD-like LDIF to OpenLDAP schema format\n\n");
53         printf("Options:\n");
54         printf("  -I inputfile     inputfile of mapped OIDs and skipped attributes/ObjectClasses");
55         printf("  -H url           LDB or LDAP server to read schmea from\n");
56         printf("  -O outputfile    outputfile otherwise STDOUT\n");
57         printf("  -o options       pass options like modules to activate\n");
58         printf("              e.g: -o modules:timestamps\n");
59         printf("\n");
60         printf("Converts records from an AD-like LDIF schema into an openLdap formatted schema\n\n");
61         exit(1);
62 }
63
64 static struct ldb_dn *find_schema_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx) 
65 {
66         const char *rootdse_attrs[] = {"schemaNamingContext", NULL};
67         struct ldb_dn *schemadn;
68         struct ldb_dn *basedn = ldb_dn_new(mem_ctx, ldb, NULL);
69         struct ldb_result *rootdse_res;
70         struct ldb_result *schema_res;
71         int ldb_ret;
72         
73         if (!basedn) {
74                 return NULL;
75         }
76         
77         /* Search for rootdse */
78         ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_BASE, NULL, rootdse_attrs, &rootdse_res);
79         if (ldb_ret != LDB_SUCCESS) {
80                 ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, 
81                                  "(&(objectClass=dMD)(cn=Schema))", 
82                                  NULL, &schema_res);
83                 if (ldb_ret) {
84                         printf("cn=Schema Search failed: %s\n", ldb_errstring(ldb));
85                         return NULL;
86                 }
87
88                 talloc_steal(mem_ctx, schema_res);
89
90                 if (schema_res->count != 1) {
91                         talloc_free(schema_res);
92                         printf("Failed to find rootDSE");
93                         return NULL;
94                 }
95                 
96                 schemadn = talloc_steal(mem_ctx, schema_res->msgs[0]->dn);
97                 talloc_free(schema_res);
98                 return schemadn;
99         }
100         
101         if (rootdse_res->count != 1) {
102                 printf("Failed to find rootDSE");
103                 talloc_free(rootdse_res);
104                 return NULL;
105         }
106         
107         /* Locate schema */
108         schemadn = ldb_msg_find_attr_as_dn(ldb, mem_ctx, rootdse_res->msgs[0], "schemaNamingContext");
109         talloc_free(rootdse_res);
110
111         if (!schemadn) {
112                 return NULL;
113         }
114
115         return schemadn;
116 }
117
118
119 static struct schema_conv process_convert(struct ldb_context *ldb, enum dsdb_schema_convert_target target, FILE *in, FILE *out) 
120 {
121         /* Read list of attributes to skip, OIDs to map */
122         TALLOC_CTX *mem_ctx = talloc_new(ldb);
123         char *line;
124         const char **attrs_skip = NULL;
125         int num_skip = 0;
126         struct oid_map {
127                 char *old_oid;
128                 char *new_oid;
129         } *oid_map = NULL;
130         int num_oid_maps = 0;
131         struct attr_map {
132                 char *old_attr;
133                 char *new_attr;
134         } *attr_map = NULL;
135         int num_attr_maps = 0;  
136         struct dsdb_class *objectclass;
137         struct dsdb_attribute *attribute;
138         struct ldb_dn *schemadn;
139         struct schema_conv ret;
140         struct dsdb_schema *schema;
141         const char *seperator;
142         char *error_string;
143
144         int ldb_ret;
145
146         ret.count = 0;
147         ret.skipped = 0;
148         ret.failures = 0;
149
150         while ((line = afdgets(fileno(in), mem_ctx, 0))) {
151                 /* Blank Line */
152                 if (line[0] == '\0') {
153                         continue;
154                 }
155                 /* Comment */
156                 if (line[0] == '#') {
157                         continue;
158                 }
159                 if (isdigit(line[0])) {
160                         char *p = strchr(line, ':');
161                         if (!p) {
162                                 ret.failures++;
163                                 return ret;
164                         }
165                         p[0] = '\0';
166                         p++;
167                         oid_map = talloc_realloc(mem_ctx, oid_map, struct oid_map, num_oid_maps + 2);
168                         trim_string(line, " ", " ");
169                         oid_map[num_oid_maps].old_oid = talloc_move(oid_map, &line);
170                         trim_string(p, " ", " ");
171                         oid_map[num_oid_maps].new_oid = p;
172                         num_oid_maps++;
173                         oid_map[num_oid_maps].old_oid = NULL;
174                 } else {
175                         char *p = strchr(line, ':');
176                         if (p) {
177                                 /* remap attribute/objectClass */
178                                 p[0] = '\0';
179                                 p++;
180                                 attr_map = talloc_realloc(mem_ctx, attr_map, struct attr_map, num_attr_maps + 2);
181                                 trim_string(line, " ", " ");
182                                 attr_map[num_attr_maps].old_attr = talloc_move(attr_map, &line);
183                                 trim_string(p, " ", " ");
184                                 attr_map[num_attr_maps].new_attr = p;
185                                 num_attr_maps++;
186                                 attr_map[num_attr_maps].old_attr = NULL;
187                         } else {
188                                 /* skip attribute/objectClass */
189                                 attrs_skip = talloc_realloc(mem_ctx, attrs_skip, const char *, num_skip + 2);
190                                 trim_string(line, " ", " ");
191                                 attrs_skip[num_skip] = talloc_move(attrs_skip, &line);
192                                 num_skip++;
193                                 attrs_skip[num_skip] = NULL;
194                         }
195                 }
196         }
197
198         schemadn = find_schema_dn(ldb, mem_ctx);
199         if (!schemadn) {
200                 printf("Failed to find schema DN: %s\n", ldb_errstring(ldb));
201                 ret.failures = 1;
202                 return ret;
203         }
204         
205         ldb_ret = dsdb_schema_from_schema_dn(mem_ctx, ldb,
206                                              lp_iconv_convenience(cmdline_lp_ctx),
207                                              schemadn, &schema, &error_string);
208         if (ldb_ret != LDB_SUCCESS) {
209                 printf("Failed to load schema: %s\n", error_string);
210                 ret.failures = 1;
211                 return ret;
212         }
213
214         switch (target) {
215         case TARGET_OPENLDAP:
216                 seperator = "\n  ";
217                 break;
218         case TARGET_FEDORA_DS:
219                 seperator = "\n  ";
220                 fprintf(out, "dn: cn=schema\n");
221                 break;
222         }
223
224         for (attribute=schema->attributes; attribute; attribute = attribute->next) {
225                 const char *name = attribute->lDAPDisplayName;
226                 const char *description = attribute->adminDescription;
227                 const char *oid = attribute->attributeID_oid;
228                 const char *syntax = attribute->attributeSyntax_oid;
229                 const char *equality = NULL, *substring = NULL;
230                 bool single_value = attribute->isSingleValued;
231
232                 const struct dsdb_syntax *map = find_syntax_map_by_ad_syntax(attribute->oMSyntax);
233                 char *schema_entry = NULL;
234                 int j;
235
236                 /* We have been asked to skip some attributes/objectClasses */
237                 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
238                         ret.skipped++;
239                         continue;
240                 }
241
242                 /* We might have been asked to remap this oid, due to a conflict */
243                 for (j=0; oid && oid_map && oid_map[j].old_oid; j++) {
244                         if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
245                                 oid =  oid_map[j].new_oid;
246                                 break;
247                         }
248                 }
249                 
250                 if (map) {
251                         /* We might have been asked to remap this oid,
252                          * due to a conflict, or lack of
253                          * implementation */
254                         syntax = map->ldap_oid;
255                         /* We might have been asked to remap this oid, due to a conflict */
256                         for (j=0; syntax && oid_map && oid_map[j].old_oid; j++) {
257                                 if (strcasecmp(syntax, oid_map[j].old_oid) == 0) {
258                                         syntax =  oid_map[j].new_oid;
259                                         break;
260                                 }
261                         }
262                         
263                         equality = map->equality;
264                         substring = map->substring;
265                 }
266
267                 /* We might have been asked to remap this name, due to a conflict */
268                 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
269                         if (strcasecmp(name, attr_map[j].old_attr) == 0) {
270                                 name =  attr_map[j].new_attr;
271                                 break;
272                         }
273                 }
274                 
275                 schema_entry = schema_attribute_description(mem_ctx, target, seperator, oid, name, description, equality, substring, syntax, single_value, false);
276
277                 if (schema_entry == NULL) {
278                         ret.failures++;
279                         return ret;
280                 }
281
282                 switch (target) {
283                 case TARGET_OPENLDAP:
284                         fprintf(out, "attributetype %s\n\n", schema_entry);
285                         break;
286                 case TARGET_FEDORA_DS:
287                         fprintf(out, "attributeTypes: %s\n", schema_entry);
288                         break;
289                 }
290                 ret.count++;
291         }
292
293         /* This is already sorted to have 'top' and similar classes first */
294         for (objectclass=schema->classes; objectclass; objectclass = objectclass->next) {
295                 const char *name = objectclass->lDAPDisplayName;
296                 const char *description = objectclass->adminDescription;
297                 const char *oid = objectclass->governsID_oid;
298                 const char *subClassOf = objectclass->subClassOf;
299                 int objectClassCategory = objectclass->objectClassCategory;
300                 char **must;
301                 char **may;
302                 char *schema_entry = NULL;
303                 const char *objectclass_name_as_list[] = {
304                         objectclass->lDAPDisplayName,
305                         NULL
306                 };
307                 int j;
308                 int attr_idx;
309                 
310                 /* We have been asked to skip some attributes/objectClasses */
311                 if (attrs_skip && str_list_check_ci(attrs_skip, name)) {
312                         ret.skipped++;
313                         continue;
314                 }
315
316                 /* We might have been asked to remap this oid, due to a conflict */
317                 for (j=0; oid_map && oid_map[j].old_oid; j++) {
318                         if (strcasecmp(oid, oid_map[j].old_oid) == 0) {
319                                 oid =  oid_map[j].new_oid;
320                                 break;
321                         }
322                 }
323                 
324                 /* We might have been asked to remap this name, due to a conflict */
325                 for (j=0; name && attr_map && attr_map[j].old_attr; j++) {
326                         if (strcasecmp(name, attr_map[j].old_attr) == 0) {
327                                 name =  attr_map[j].new_attr;
328                                 break;
329                         }
330                 }
331                 
332                 may = dsdb_full_attribute_list(mem_ctx, schema, objectclass_name_as_list, DSDB_SCHEMA_ALL_MAY);
333
334                 for (j=0; may && may[j]; j++) {
335                         /* We might have been asked to remap this name, due to a conflict */ 
336                         for (attr_idx=0; attr_map && attr_map[attr_idx].old_attr; attr_idx++) { 
337                                 if (strcasecmp(may[j], attr_map[attr_idx].old_attr) == 0) { 
338                                         may[j] =  attr_map[attr_idx].new_attr; 
339                                         break;                          
340                                 }                                       
341                         }                                               
342                 }
343
344                 must = dsdb_full_attribute_list(mem_ctx, schema, objectclass_name_as_list, DSDB_SCHEMA_ALL_MUST);
345
346                 for (j=0; must && must[j]; j++) {
347                         /* We might have been asked to remap this name, due to a conflict */ 
348                         for (attr_idx=0; attr_map && attr_map[attr_idx].old_attr; attr_idx++) { 
349                                 if (strcasecmp(must[j], attr_map[attr_idx].old_attr) == 0) { 
350                                         must[j] =  attr_map[attr_idx].new_attr; 
351                                         break;                          
352                                 }                                       
353                         }                                               
354                 }
355
356                 schema_entry = schema_class_description(mem_ctx, target, 
357                                                         seperator,
358                                                         oid, 
359                                                         name,
360                                                         NULL, 
361                                                         description,
362                                                         subClassOf,
363                                                         objectClassCategory,
364                                                         must,
365                                                         may);
366                 if (schema_entry == NULL) {
367                         ret.failures++;
368                         return ret;
369                 }
370
371                 switch (target) {
372                 case TARGET_OPENLDAP:
373                         fprintf(out, "objectclass %s\n\n", schema_entry);
374                         break;
375                 case TARGET_FEDORA_DS:
376                         fprintf(out, "objectClasses: %s\n", schema_entry);
377                         break;
378                 }
379                 ret.count++;
380         }
381
382         return ret;
383 }
384
385  int main(int argc, const char **argv)
386 {
387         TALLOC_CTX *ctx;
388         struct ldb_cmdline *options;
389         FILE *in = stdin;
390         FILE *out = stdout;
391         struct ldb_context *ldb;
392         struct schema_conv ret;
393         const char *target_str;
394         enum dsdb_schema_convert_target target;
395
396         ctx = talloc_new(NULL);
397         ldb = ldb_init(ctx, NULL);
398
399         options = ldb_cmdline_process(ldb, argc, argv, usage);
400
401         if (options->input) {
402                 in = fopen(options->input, "r");
403                 if (!in) {
404                         perror(options->input);
405                         exit(1);
406                 }
407         }
408         if (options->output) {
409                 out = fopen(options->output, "w");
410                 if (!out) {
411                         perror(options->output);
412                         exit(1);
413                 }
414         }
415
416         target_str = lp_parm_string(cmdline_lp_ctx, NULL, "convert", "target");
417
418         if (!target_str || strcasecmp(target_str, "openldap") == 0) {
419                 target = TARGET_OPENLDAP;
420         } else if (strcasecmp(target_str, "fedora-ds") == 0) {
421                 target = TARGET_FEDORA_DS;
422         } else {
423                 printf("Unsupported target: %s\n", target_str);
424                 exit(1);
425         }
426
427         ret = process_convert(ldb, target, in, out);
428
429         fclose(in);
430         fclose(out);
431
432         printf("Converted %d records (skipped %d) with %d failures\n", ret.count, ret.skipped, ret.failures);
433
434         return 0;
435 }