2 exec smbscript "$0" ${1+"$@"}
4 work out the minimal schema for a set of objectclasses
11 var options = GetOptions(ARGV,
14 "POPT_COMMON_CREDENTIALS",
20 if (options == undefined) {
21 println("Failed to parse options");
24 verbose = options["verbose"];
26 dump_classes = options["classes"];
27 dump_attributes = options["attributes"];
28 dump_subschema = options["subschema"];
29 dump_subschema_auto = options["subschema-auto"];
31 if (dump_classes != undefined) {
34 if (dump_attributes != undefined) {
37 if (dump_subschema != undefined) {
40 if (dump_subschema_auto != undefined) {
42 dump_subschema = "yes";
44 if (dump_all != undefined) {
46 dump_attributes = "yes";
47 dump_subschema = "yes";
48 dump_subschema_auto = "yes";
51 if (options.ARGV.length != 2) {
52 println("Usage: minschema.js <URL> <classfile>");
56 var url = options.ARGV[0];
57 var classfile = options.ARGV[1];
59 /* use command line creds if available */
60 ldb.credentials = options.get_credentials();
62 var ok = ldb.connect(url);
65 objectclasses = new Object();
66 attributes = new Object();
67 rootDse = new Object();
69 objectclasses_expanded = new Object();
71 /* the attributes we need for objectclasses */
72 class_attrs = new Array("objectClass",
81 "showInAdvancedViewOnly",
84 "objectClassCategory",
88 "systemPossSuperiors",
91 "systemAuxiliaryClass",
92 "defaultSecurityDescriptor",
96 "defaultObjectCategory",
98 /* this attributes are not used by w2k3 */
101 "msDs-Schema-Extensions",
106 attrib_attrs = new Array("objectClass",
114 "showInAdvancedViewOnly",
120 "extendedCharsAllowed",
123 "attributeSecurityGUID",
126 "isMemberOfPartialAttributeSet",
129 /* this attributes are not used by w2k3 */
132 "msDs-Schema-Extensions",
148 print only if verbose is set
151 if (verbose != undefined) {
152 print(vsprintf(arguments));
156 function get_object_cn(ldb, name) {
157 var attrs = new Array("cn");
159 var res = ldb.search(sprintf("(ldapDisplayName=%s)", name), rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
160 assert(res != undefined);
161 assert(res.msgs.length == 1);
163 var cn = res.msgs[0]["cn"];
164 assert(cn != undefined);
165 if (typeof(cn) == "string") {
171 create an objectclass object
173 function obj_objectClass(ldb, name) {
174 var o = new Object();
176 o.cn = get_object_cn(ldb, name);
181 create an attribute object
183 function obj_attribute(ldb, name) {
184 var o = new Object();
186 o.cn = get_object_cn(ldb, name);
191 syntaxmap = new Object();
193 syntaxmap['2.5.5.1'] = '1.3.6.1.4.1.1466.115.121.1.12';
194 syntaxmap['2.5.5.2'] = '1.3.6.1.4.1.1466.115.121.1.38';
195 syntaxmap['2.5.5.3'] = '1.2.840.113556.1.4.1362';
196 syntaxmap['2.5.5.4'] = '1.2.840.113556.1.4.905';
197 syntaxmap['2.5.5.5'] = '1.3.6.1.4.1.1466.115.121.1.26';
198 syntaxmap['2.5.5.6'] = '1.3.6.1.4.1.1466.115.121.1.36';
199 syntaxmap['2.5.5.7'] = '1.2.840.113556.1.4.903';
200 syntaxmap['2.5.5.8'] = '1.3.6.1.4.1.1466.115.121.1.7';
201 syntaxmap['2.5.5.9'] = '1.3.6.1.4.1.1466.115.121.1.27';
202 syntaxmap['2.5.5.10'] = '1.3.6.1.4.1.1466.115.121.1.40';
203 syntaxmap['2.5.5.11'] = '1.3.6.1.4.1.1466.115.121.1.24';
204 syntaxmap['2.5.5.12'] = '1.3.6.1.4.1.1466.115.121.1.15';
205 syntaxmap['2.5.5.13'] = '1.3.6.1.4.1.1466.115.121.1.43';
206 syntaxmap['2.5.5.14'] = '1.2.840.113556.1.4.904';
207 syntaxmap['2.5.5.15'] = '1.2.840.113556.1.4.907';
208 syntaxmap['2.5.5.16'] = '1.2.840.113556.1.4.906';
209 syntaxmap['2.5.5.17'] = '1.3.6.1.4.1.1466.115.121.1.40';
212 map some attribute syntaxes from some apparently MS specific
213 syntaxes to the standard syntaxes
215 function map_attribute_syntax(s) {
216 if (syntaxmap[s] != undefined) {
224 fix a string DN to use ${SCHEMADN}
226 function fix_dn(dn) {
227 var s = strstr(dn, rootDse.schemaNamingContext);
231 return substr(dn, 0, strlen(dn) - strlen(s)) + "${SCHEMADN}";
235 dump an object as ldif
237 function write_ldif_one(o, attrs) {
239 printf("dn: CN=%s,${SCHEMADN}\n", o.cn);
240 for (i=0;i<attrs.length;i++) {
242 if (o[a] == undefined) {
245 /* special case for oMObjectClass, which is a binary object */
246 if (a == "oMObjectClass") {
247 printf("%s:: %s\n", a, o[a]);
251 if (typeof(v) == "string") {
255 for (j=0;j<v.length;j++) {
256 printf("%s: %s\n", a, fix_dn(v[j]));
263 dump an array of objects as ldif
265 function write_ldif(o, attrs) {
268 write_ldif_one(o[i], attrs);
274 create a testDN based an an example DN
275 the idea is to ensure we obey any structural rules
277 function create_testdn(exampleDN) {
278 var a = split(",", exampleDN);
284 find the properties of an objectclass
286 function find_objectclass_properties(ldb, o) {
287 var res = ldb.search(
288 sprintf("(ldapDisplayName=%s)", o.name),
289 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, class_attrs);
290 assert(res != undefined);
291 assert(res.msgs.length == 1);
292 var msg = res.msgs[0];
300 find the properties of an attribute
302 function find_attribute_properties(ldb, o) {
303 var res = ldb.search(
304 sprintf("(ldapDisplayName=%s)", o.name),
305 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrib_attrs);
306 assert(res != undefined);
307 assert(res.msgs.length == 1);
308 var msg = res.msgs[0];
311 /* special case for oMObjectClass, which is a binary object */
312 if (a == "oMObjectClass") {
313 o[a] = ldb.encode(msg[a]);
321 find the auto-created properties of an objectclass. Only works for classes
322 that can be created using just a DN and the objectclass
324 function find_objectclass_auto(ldb, o) {
325 if (o["exampleDN"] == undefined) {
328 var testdn = create_testdn(o.exampleDN);
331 dprintf("testdn is '%s'\n", testdn);
333 var ldif = "dn: " + testdn;
334 ldif = ldif + "\nobjectClass: " + o.name;
337 dprintf("error adding %s: %s\n", o.name, ok.errstr);
338 dprintf("%s\n", ldif);
342 var res = ldb.search("", testdn, ldb.SCOPE_BASE);
343 ok = ldb.del(testdn);
344 assert(ok.error == 0);
347 for (a in res.msgs[0]) {
348 attributes[a].autocreate = true;
354 look at auxiliary information from a class to intuit the existance of more
355 classes needed for a minimal schema
357 function expand_objectclass(ldb, o) {
358 var attrs = new Array("auxiliaryClass", "systemAuxiliaryClass",
359 "possSuperiors", "systemPossSuperiors",
361 var res = ldb.search(
362 sprintf("(&(objectClass=classSchema)(ldapDisplayName=%s))", o.name),
363 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
365 dprintf("Expanding class %s\n", o.name);
366 assert(res != undefined);
367 assert(res.msgs.length == 1);
368 var msg = res.msgs[0];
369 for (a=0;a<attrs.length;a++) {
370 var aname = attrs[a];
371 if (msg[aname] == undefined) {
374 var list = msg[aname];
375 if (typeof(list) == "string") {
376 list = new Array(msg[aname]);
379 for (i=0;i<list.length;i++) {
381 if (objectclasses[name] == undefined) {
382 dprintf("Found new objectclass '%s'\n", name);
383 objectclasses[name] = obj_objectClass(ldb, name);
391 add the must and may attributes from an objectclass to the full list
394 function add_objectclass_attributes(ldb, class) {
395 var attrs = new Array("mustContain", "systemMustContain",
396 "mayContain", "systemMayContain");
398 for (i=0;i<attrs.length;i++) {
399 var aname = attrs[i];
400 if (class[aname] == undefined) {
403 var alist = class[aname];
404 if (typeof(alist) == "string") {
405 alist = new Array(alist);
408 var len = alist.length;
409 for (j=0;j<len;j++) {
411 if (attributes[a] == undefined) {
412 attributes[a] = obj_attribute(ldb, a);
420 process an individual record, working out what attributes it has
422 function walk_dn(ldb, dn) {
423 /* get a list of all possible attributes for this object */
424 var attrs = new Array("allowedAttributes");
425 var res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, attrs);
426 if (res.error != 0) {
427 dprintf("Unable to fetch allowedAttributes for '%s' - %s\n",
431 var allattrs = res.msgs[0].allowedAttributes;
432 res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, allattrs);
433 if (res.error != 0) {
434 dprintf("Unable to fetch all attributes for '%s' - %s\n",
439 var msg = res.msgs[0];
441 if (attributes[a] == undefined) {
442 attributes[a] = obj_attribute(ldb, a);
448 walk a naming context, looking for all records
450 function walk_naming_context(ldb, namingContext) {
451 var attrs = new Array("objectClass");
452 var res = ldb.search("objectClass=*", namingContext, ldb.SCOPE_DEFAULT, attrs);
453 if (res.error != 0) {
454 dprintf("Unable to fetch objectClasses for '%s' - %s\n",
455 namingContext, res.errstr);
459 for (r=0;r<res.msgs.length;r++) {
460 var msg = res.msgs[r].objectClass;
462 for (c=0;c<msg.length;c++) {
463 var objectClass = msg[c];
464 if (objectclasses[objectClass] == undefined) {
465 objectclasses[objectClass] = obj_objectClass(ldb, objectClass);
466 objectclasses[objectClass].exampleDN = res.msgs[r].dn;
469 walk_dn(ldb, res.msgs[r].dn);
474 trim the may attributes for an objectClass
476 function trim_objectclass_attributes(ldb, class) {
479 /* trim possibleInferiors,
480 * include only the classes we extracted */
481 var possinf = class["possibleInferiors"];
482 if (possinf != undefined) {
483 var newpossinf = new Array();
484 if (typeof(possinf) == "string") {
485 possinf = new Array(possinf);
488 for (j = 0;j < possinf.length; j++) {
490 if (objectclasses[x] != undefined) {
495 class["possibleInferiors"] = newpossinf;
498 /* trim systemMayContain,
499 * remove duplicates */
500 var sysmay = class["systemMayContain"];
501 if (sysmay != undefined) {
502 var newsysmay = new Array();
503 if (typeof(sysmay) == "string") {
504 sysmay = new Array(sysmay);
506 for (j = 0;j < sysmay.length; j++) {
509 if (newsysmay[0] == undefined) {
512 for (n = 0; n < newsysmay.length; n++) {
513 if (newsysmay[n] == x) {
522 class["systemMayContain"] = newsysmay;
526 * remove duplicates */
527 var may = class["mayContain"];
528 if (may != undefined) {
529 var newmay = new Array();
530 if (typeof(may) == "string") {
531 may = new Array(may);
533 for (j = 0;j < may.length; j++) {
536 if (newmay[0] == undefined) {
539 for (n = 0; n < newmay.length; n++) {
540 if (newmay[n] == x) {
549 class["mayContain"] = newmay;
554 load the basic attributes of an objectClass
556 function build_objectclass(ldb, name) {
557 var attrs = new Array("name");
558 var res = ldb.search(
559 sprintf("(&(objectClass=classSchema)(ldapDisplayName=%s))", name),
560 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
561 if (res.error != 0) {
562 dprintf("unknown class '%s'\n", name);
565 if (res.msgs.length == 0) {
566 dprintf("unknown class '%s'\n", name);
569 return obj_objectClass(ldb, name);
575 function list_append(a1, a2) {
577 if (a1 == undefined) {
580 if (a2 == undefined) {
583 for (i=0;i<a2.length;i++) {
584 a1[a1.length] = a2[i];
590 form a coalesced attribute list
592 function attribute_list(class, attr1, attr2) {
593 var a1 = class[attr1];
594 var a2 = class[attr2];
595 if (typeof(a1) == "string") {
598 if (typeof(a2) == "string") {
601 return list_append(a1, a2);
605 write out a list in aggregate form
607 function aggregate_list(name, list) {
608 if (list == undefined) {
612 printf("%s ( ", name);
613 for (i=0;i<list.length;i++) {
614 printf("%s ", list[i]);
615 if (i < (list.length - 1)) {
623 write the aggregate record for an objectclass
625 function write_aggregate_objectclass(class) {
626 printf("objectClasses: ( %s NAME '%s' ", class.governsID, class.name);
627 if (class['subClassOf'] != undefined) {
628 printf("SUP %s ", class['subClassOf']);
630 if (class.objectClassCategory == 1) {
631 printf("STRUCTURAL ");
632 } else if (class.objectClassCategory == 2) {
634 } else if (class.objectClassCategory == 3) {
635 printf("AUXILIARY ");
640 list = attribute_list(class, "systemMustContain", "mustContain");
641 aggregate_list("MUST", list);
643 list = attribute_list(class, "systemMayContain", "mayContain");
644 aggregate_list("MAY", list);
651 write the aggregate record for an ditcontentrule
653 function write_aggregate_ditcontentrule(class) {
654 var list = attribute_list(class, "auxiliaryClass", "systemAuxiliaryClass");
656 if (list == undefined) {
660 printf("dITContentRules: ( %s NAME '%s' ", class.governsID, class.name);
662 aggregate_list("AUX", list);
664 var may_list = undefined;
665 var must_list = undefined;
667 for (i=0;i<list.length;i++) {
670 list2 = attribute_list(objectclasses[c],
671 "mayContain", "systemMayContain");
672 may_list = list_append(may_list, list2);
673 list2 = attribute_list(objectclasses[c],
674 "mustContain", "systemMustContain");
675 must_list = list_append(must_list, list2);
678 aggregate_list("MUST", must_list);
679 aggregate_list("MAY", may_list);
685 write the aggregate record for an attribute
687 function write_aggregate_attribute(attrib) {
688 printf("attributeTypes: ( %s NAME '%s' SYNTAX '%s' ",
689 attrib.attributeID, attrib.name,
690 map_attribute_syntax(attrib.attributeSyntax));
691 if (attrib['isSingleValued'] == "TRUE") {
692 printf("SINGLE-VALUE ");
694 if (attrib['systemOnly'] == "TRUE") {
695 printf("NO-USER-MODIFICATION ");
703 write the aggregate record
705 function write_aggregate() {
706 printf("dn: CN=Aggregate,${SCHEMADN}\n");
707 print("objectClass: top
708 objectClass: subSchema
709 objectCategory: CN=SubSchema,${SCHEMADN}
711 if (dump_subschema_auto == undefined) {
715 for (i in objectclasses) {
716 write_aggregate_objectclass(objectclasses[i]);
718 for (i in attributes) {
719 write_aggregate_attribute(attributes[i]);
721 for (i in objectclasses) {
722 write_aggregate_ditcontentrule(objectclasses[i]);
727 load a list from a file
729 function load_list(file) {
730 var sys = sys_init();
731 var s = sys.file_load(file);
732 var a = split("\n", s);
736 /* get the rootDSE */
737 var res = ldb.search("", "", ldb.SCOPE_BASE);
738 rootDse = res.msgs[0];
740 /* load the list of classes we are interested in */
741 var classes = load_list(classfile);
743 for (i=0;i<classes.length;i++) {
744 var classname = classes[i];
745 var class = build_objectclass(ldb, classname);
746 if (class != undefined) {
747 objectclasses[classname] = class;
753 expand the objectclass list as needed
757 /* calculate the actual number of classes */
758 for (i in objectclasses) {
761 /* so EJS do not have while nor the break statement
762 cannot find any other way than doing more loops
763 than necessary to recursively expand all classes
766 for (inf = 0;inf < 500; inf++) {
767 if (expanded < num_classes) {
768 for (i in objectclasses) {
769 var n = objectclasses[i];
770 if (objectclasses_expanded[i] != "DONE") {
771 expand_objectclass(ldb, objectclasses[i]);
772 objectclasses_expanded[i] = "DONE";
776 /* recalculate the actual number of classes */
778 for (i in objectclasses) {
785 find objectclass properties
787 for (i in objectclasses) {
788 find_objectclass_properties(ldb, objectclasses[i]);
792 form the full list of attributes
794 for (i in objectclasses) {
795 add_objectclass_attributes(ldb, objectclasses[i]);
798 /* and attribute properties */
799 for (i in attributes) {
800 find_attribute_properties(ldb, attributes[i]);
804 trim the 'may' attribute lists to those really needed
806 for (i in objectclasses) {
807 trim_objectclass_attributes(ldb, objectclasses[i]);
811 dump an ldif form of the attributes and objectclasses
813 if (dump_attributes != undefined) {
814 write_ldif(attributes, attrib_attrs);
816 if (dump_classes != undefined) {
817 write_ldif(objectclasses, class_attrs);
819 if (dump_subschema != undefined) {
823 if (verbose == undefined) {
828 dump list of objectclasses
830 printf("objectClasses:\n")
831 for (i in objectclasses) {
834 printf("attributes:\n")
835 for (i in attributes) {
839 printf("autocreated attributes:\n");
840 for (i in attributes) {
841 if (attributes[i].autocreate == true) {