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",
95 "defaultObjectCategory",
97 /* this attributes are not used by w2k3 */
100 "msDs-Schema-Extensions",
105 attrib_attrs = new Array("objectClass",
113 "showInAdvancedViewOnly",
119 "extendedCharsAllowed",
122 "attributeSecurityGUID",
125 "isMemberOfPartialAttributeSet",
127 /* this attributes are not used by w2k3 */
130 "msDs-Schema-Extensions",
146 print only if verbose is set
149 if (verbose != undefined) {
150 print(vsprintf(arguments));
154 function get_object_cn(ldb, name) {
155 var attrs = new Array("cn");
157 var res = ldb.search(sprintf("(ldapDisplayName=%s)", name), rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
158 assert(res != undefined);
159 assert(res.msgs.length == 1);
161 var cn = res.msgs[0]["cn"];
162 assert(cn != undefined);
163 if (typeof(cn) == "string") {
169 create an objectclass object
171 function obj_objectClass(ldb, name) {
172 var o = new Object();
174 o.cn = get_object_cn(ldb, name);
179 create an attribute object
181 function obj_attribute(ldb, name) {
182 var o = new Object();
184 o.cn = get_object_cn(ldb, name);
189 syntaxmap = new Object();
191 syntaxmap['2.5.5.1'] = '1.3.6.1.4.1.1466.115.121.1.12';
192 syntaxmap['2.5.5.2'] = '1.3.6.1.4.1.1466.115.121.1.38';
193 syntaxmap['2.5.5.3'] = '1.2.840.113556.1.4.1362';
194 syntaxmap['2.5.5.4'] = '1.2.840.113556.1.4.905';
195 syntaxmap['2.5.5.5'] = '1.3.6.1.4.1.1466.115.121.1.26';
196 syntaxmap['2.5.5.6'] = '1.3.6.1.4.1.1466.115.121.1.36';
197 syntaxmap['2.5.5.7'] = '1.2.840.113556.1.4.903';
198 syntaxmap['2.5.5.8'] = '1.3.6.1.4.1.1466.115.121.1.7';
199 syntaxmap['2.5.5.9'] = '1.3.6.1.4.1.1466.115.121.1.27';
200 syntaxmap['2.5.5.10'] = '1.3.6.1.4.1.1466.115.121.1.40';
201 syntaxmap['2.5.5.11'] = '1.3.6.1.4.1.1466.115.121.1.24';
202 syntaxmap['2.5.5.12'] = '1.3.6.1.4.1.1466.115.121.1.15';
203 syntaxmap['2.5.5.13'] = '1.3.6.1.4.1.1466.115.121.1.43';
204 syntaxmap['2.5.5.14'] = '1.2.840.113556.1.4.904';
205 syntaxmap['2.5.5.15'] = '1.2.840.113556.1.4.907';
206 syntaxmap['2.5.5.16'] = '1.2.840.113556.1.4.906';
207 syntaxmap['2.5.5.17'] = '1.3.6.1.4.1.1466.115.121.1.40';
210 map some attribute syntaxes from some apparently MS specific
211 syntaxes to the standard syntaxes
213 function map_attribute_syntax(s) {
214 if (syntaxmap[s] != undefined) {
222 fix a string DN to use ${SCHEMADN}
224 function fix_dn(dn) {
225 var s = strstr(dn, rootDse.schemaNamingContext);
229 return substr(dn, 0, strlen(dn) - strlen(s)) + "${SCHEMADN}";
233 dump an object as ldif
235 function write_ldif_one(o, attrs) {
237 printf("dn: CN=%s,${SCHEMADN}\n", o.cn);
238 for (i=0;i<attrs.length;i++) {
240 if (o[a] == undefined) {
243 /* special case for oMObjectClass, which is a binary object */
244 if (a == "oMObjectClass") {
245 printf("%s:: %s\n", a, o[a]);
249 if (typeof(v) == "string") {
253 for (j=0;j<v.length;j++) {
254 printf("%s: %s\n", a, fix_dn(v[j]));
261 dump an array of objects as ldif
263 function write_ldif(o, attrs) {
266 write_ldif_one(o[i], attrs);
272 create a testDN based an an example DN
273 the idea is to ensure we obey any structural rules
275 function create_testdn(exampleDN) {
276 var a = split(",", exampleDN);
282 find the properties of an objectclass
284 function find_objectclass_properties(ldb, o) {
285 var res = ldb.search(
286 sprintf("(ldapDisplayName=%s)", o.name),
287 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, class_attrs);
288 assert(res != undefined);
289 assert(res.msgs.length == 1);
290 var msg = res.msgs[0];
298 find the properties of an attribute
300 function find_attribute_properties(ldb, o) {
301 var res = ldb.search(
302 sprintf("(ldapDisplayName=%s)", o.name),
303 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrib_attrs);
304 assert(res != undefined);
305 assert(res.msgs.length == 1);
306 var msg = res.msgs[0];
309 /* special case for oMObjectClass, which is a binary object */
310 if (a == "oMObjectClass") {
311 o[a] = ldb.encode(msg[a]);
319 find the auto-created properties of an objectclass. Only works for classes
320 that can be created using just a DN and the objectclass
322 function find_objectclass_auto(ldb, o) {
323 if (o["exampleDN"] == undefined) {
326 var testdn = create_testdn(o.exampleDN);
329 dprintf("testdn is '%s'\n", testdn);
331 var ldif = "dn: " + testdn;
332 ldif = ldif + "\nobjectClass: " + o.name;
335 dprintf("error adding %s: %s\n", o.name, ok.errstr);
336 dprintf("%s\n", ldif);
340 var res = ldb.search("", testdn, ldb.SCOPE_BASE);
341 ok = ldb.del(testdn);
342 assert(ok.error == 0);
345 for (a in res.msgs[0]) {
346 attributes[a].autocreate = true;
352 look at auxiliary information from a class to intuit the existance of more
353 classes needed for a minimal schema
355 function expand_objectclass(ldb, o) {
356 var attrs = new Array("auxiliaryClass", "systemAuxiliaryClass",
357 "possSuperiors", "systemPossSuperiors",
359 var res = ldb.search(
360 sprintf("(&(objectClass=classSchema)(ldapDisplayName=%s))", o.name),
361 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
363 dprintf("Expanding class %s\n", o.name);
364 assert(res != undefined);
365 assert(res.msgs.length == 1);
366 var msg = res.msgs[0];
367 for (a=0;a<attrs.length;a++) {
368 var aname = attrs[a];
369 if (msg[aname] == undefined) {
372 var list = msg[aname];
373 if (typeof(list) == "string") {
374 list = new Array(msg[aname]);
377 for (i=0;i<list.length;i++) {
379 if (objectclasses[name] == undefined) {
380 dprintf("Found new objectclass '%s'\n", name);
381 objectclasses[name] = obj_objectClass(ldb, name);
389 add the must and may attributes from an objectclass to the full list
392 function add_objectclass_attributes(ldb, class) {
393 var attrs = new Array("mustContain", "systemMustContain",
394 "mayContain", "systemMayContain");
396 for (i=0;i<attrs.length;i++) {
397 var aname = attrs[i];
398 if (class[aname] == undefined) {
401 var alist = class[aname];
402 if (typeof(alist) == "string") {
403 alist = new Array(alist);
406 var len = alist.length;
407 for (j=0;j<len;j++) {
409 if (attributes[a] == undefined) {
410 attributes[a] = obj_attribute(ldb, a);
418 process an individual record, working out what attributes it has
420 function walk_dn(ldb, dn) {
421 /* get a list of all possible attributes for this object */
422 var attrs = new Array("allowedAttributes");
423 var res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, attrs);
424 if (res.error != 0) {
425 dprintf("Unable to fetch allowedAttributes for '%s' - %s\n",
429 var allattrs = res.msgs[0].allowedAttributes;
430 res = ldb.search("objectClass=*", dn, ldb.SCOPE_BASE, allattrs);
431 if (res.error != 0) {
432 dprintf("Unable to fetch all attributes for '%s' - %s\n",
437 var msg = res.msgs[0];
439 if (attributes[a] == undefined) {
440 attributes[a] = obj_attribute(ldb, a);
446 walk a naming context, looking for all records
448 function walk_naming_context(ldb, namingContext) {
449 var attrs = new Array("objectClass");
450 var res = ldb.search("objectClass=*", namingContext, ldb.SCOPE_DEFAULT, attrs);
451 if (res.error != 0) {
452 dprintf("Unable to fetch objectClasses for '%s' - %s\n",
453 namingContext, res.errstr);
457 for (r=0;r<res.msgs.length;r++) {
458 var msg = res.msgs[r].objectClass;
460 for (c=0;c<msg.length;c++) {
461 var objectClass = msg[c];
462 if (objectclasses[objectClass] == undefined) {
463 objectclasses[objectClass] = obj_objectClass(ldb, objectClass);
464 objectclasses[objectClass].exampleDN = res.msgs[r].dn;
467 walk_dn(ldb, res.msgs[r].dn);
472 trim the may attributes for an objectClass
474 function trim_objectclass_attributes(ldb, class) {
477 /* trim possibleInferiors,
478 * include only the classes we extracted */
479 var possinf = class["possibleInferiors"];
480 if (possinf != undefined) {
481 var newpossinf = new Array();
482 if (typeof(possinf) == "string") {
483 possinf = new Array(possinf);
486 for (j = 0;j < possinf.length; j++) {
488 if (objectclasses[x] != undefined) {
493 class["possibleInferiors"] = newpossinf;
496 /* trim systemMayContain,
497 * remove duplicates */
498 var sysmay = class["systemMayContain"];
499 if (sysmay != undefined) {
500 var newsysmay = new Array();
501 if (typeof(sysmay) == "string") {
502 sysmay = new Array(sysmay);
504 for (j = 0;j < sysmay.length; j++) {
507 if (newsysmay[0] == undefined) {
510 for (n = 0; n < newsysmay.length; n++) {
511 if (newsysmay[n] == x) {
520 class["systemMayContain"] = newsysmay;
524 * remove duplicates */
525 var may = class["mayContain"];
526 if (may != undefined) {
527 var newmay = new Array();
528 if (typeof(may) == "string") {
529 may = new Array(may);
531 for (j = 0;j < may.length; j++) {
534 if (newmay[0] == undefined) {
537 for (n = 0; n < newmay.length; n++) {
538 if (newmay[n] == x) {
547 class["mayContain"] = newmay;
552 load the basic attributes of an objectClass
554 function build_objectclass(ldb, name) {
555 var attrs = new Array("name");
556 var res = ldb.search(
557 sprintf("(&(objectClass=classSchema)(ldapDisplayName=%s))", name),
558 rootDse.schemaNamingContext, ldb.SCOPE_SUBTREE, attrs);
559 if (res.error != 0) {
560 dprintf("unknown class '%s'\n", name);
563 if (res.msgs.length == 0) {
564 dprintf("unknown class '%s'\n", name);
567 return obj_objectClass(ldb, name);
573 function list_append(a1, a2) {
575 if (a1 == undefined) {
578 if (a2 == undefined) {
581 for (i=0;i<a2.length;i++) {
582 a1[a1.length] = a2[i];
588 form a coalesced attribute list
590 function attribute_list(class, attr1, attr2) {
591 var a1 = class[attr1];
592 var a2 = class[attr2];
593 if (typeof(a1) == "string") {
596 if (typeof(a2) == "string") {
599 return list_append(a1, a2);
603 write out a list in aggregate form
605 function aggregate_list(name, list) {
606 if (list == undefined) {
610 printf("%s ( ", name);
611 for (i=0;i<list.length;i++) {
612 printf("%s ", list[i]);
613 if (i < (list.length - 1)) {
621 write the aggregate record for an objectclass
623 function write_aggregate_objectclass(class) {
624 printf("objectClasses: ( %s NAME '%s' ", class.governsID, class.name);
625 if (class['subClassOf'] != undefined) {
626 printf("SUP %s ", class['subClassOf']);
628 if (class.objectClassCategory == 1) {
629 printf("STRUCTURAL ");
630 } else if (class.objectClassCategory == 2) {
632 } else if (class.objectClassCategory == 3) {
633 printf("AUXILIARY ");
638 list = attribute_list(class, "systemMustContain", "mustContain");
639 aggregate_list("MUST", list);
641 list = attribute_list(class, "systemMayContain", "mayContain");
642 aggregate_list("MAY", list);
649 write the aggregate record for an ditcontentrule
651 function write_aggregate_ditcontentrule(class) {
652 var list = attribute_list(class, "auxiliaryClass", "systemAuxiliaryClass");
654 if (list == undefined) {
658 printf("dITContentRules: ( %s NAME '%s' ", class.governsID, class.name);
660 aggregate_list("AUX", list);
662 var may_list = undefined;
663 var must_list = undefined;
665 for (i=0;i<list.length;i++) {
668 list2 = attribute_list(objectclasses[c],
669 "mayContain", "systemMayContain");
670 may_list = list_append(may_list, list2);
671 list2 = attribute_list(objectclasses[c],
672 "mustContain", "systemMustContain");
673 must_list = list_append(must_list, list2);
676 aggregate_list("MUST", must_list);
677 aggregate_list("MAY", may_list);
683 write the aggregate record for an attribute
685 function write_aggregate_attribute(attrib) {
686 printf("attributeTypes: ( %s NAME '%s' SYNTAX '%s' ",
687 attrib.attributeID, attrib.name,
688 map_attribute_syntax(attrib.attributeSyntax));
689 if (attrib['isSingleValued'] == "TRUE") {
690 printf("SINGLE-VALUE ");
692 if (attrib['systemOnly'] == "TRUE") {
693 printf("NO-USER-MODIFICATION ");
701 write the aggregate record
703 function write_aggregate() {
704 printf("dn: CN=Aggregate,${SCHEMADN}\n");
705 print("objectClass: top
706 objectClass: subSchema
708 if (dump_subschema_auto == undefined) {
712 for (i in objectclasses) {
713 write_aggregate_objectclass(objectclasses[i]);
715 for (i in attributes) {
716 write_aggregate_attribute(attributes[i]);
718 for (i in objectclasses) {
719 write_aggregate_ditcontentrule(objectclasses[i]);
724 load a list from a file
726 function load_list(file) {
727 var sys = sys_init();
728 var s = sys.file_load(file);
729 var a = split("\n", s);
733 /* get the rootDSE */
734 var res = ldb.search("", "", ldb.SCOPE_BASE);
735 rootDse = res.msgs[0];
737 /* load the list of classes we are interested in */
738 var classes = load_list(classfile);
740 for (i=0;i<classes.length;i++) {
741 var classname = classes[i];
742 var class = build_objectclass(ldb, classname);
743 if (class != undefined) {
744 objectclasses[classname] = class;
750 expand the objectclass list as needed
754 /* calculate the actual number of classes */
755 for (i in objectclasses) {
758 /* so EJS do not have while nor the break statement
759 cannot find any other way than doing more loops
760 than necessary to recursively expand all classes
763 for (inf = 0;inf < 500; inf++) {
764 if (expanded < num_classes) {
765 for (i in objectclasses) {
766 var n = objectclasses[i];
767 if (objectclasses_expanded[i] != "DONE") {
768 expand_objectclass(ldb, objectclasses[i]);
769 objectclasses_expanded[i] = "DONE";
773 /* recalculate the actual number of classes */
775 for (i in objectclasses) {
782 find objectclass properties
784 for (i in objectclasses) {
785 find_objectclass_properties(ldb, objectclasses[i]);
789 form the full list of attributes
791 for (i in objectclasses) {
792 add_objectclass_attributes(ldb, objectclasses[i]);
795 /* and attribute properties */
796 for (i in attributes) {
797 find_attribute_properties(ldb, attributes[i]);
801 trim the 'may' attribute lists to those really needed
803 for (i in objectclasses) {
804 trim_objectclass_attributes(ldb, objectclasses[i]);
808 dump an ldif form of the attributes and objectclasses
810 if (dump_attributes != undefined) {
811 write_ldif(attributes, attrib_attrs);
813 if (dump_classes != undefined) {
814 write_ldif(objectclasses, class_attrs);
816 if (dump_subschema != undefined) {
820 if (verbose == undefined) {
825 dump list of objectclasses
827 printf("objectClasses:\n")
828 for (i in objectclasses) {
831 printf("attributes:\n")
832 for (i in attributes) {
836 printf("autocreated attributes:\n");
837 for (i in attributes) {
838 if (attributes[i].autocreate == true) {