3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Andrew Tridgell 2009
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/>.
20 """Tests the possibleInferiors generation in the schema_fsmo ldb module"""
26 # Find right directory when running from source tree
27 sys.path.insert(0, "bin/python")
30 from samba import getopt as options, Ldb
33 parser = optparse.OptionParser("possibleinferiors.py <URL> [<CLASS>]")
34 sambaopts = options.SambaOptions(parser)
35 parser.add_option_group(sambaopts)
36 credopts = options.CredentialsOptions(parser)
37 parser.add_option_group(credopts)
38 parser.add_option_group(options.VersionOptions(parser))
39 parser.add_option("--wspp", action="store_true")
41 opts, args = parser.parse_args()
54 """return a unique list"""
56 return [set.setdefault(e,e) for e in alist if e not in set]
59 lp_ctx = sambaopts.get_loadparm()
61 creds = credopts.get_credentials(lp_ctx)
62 db = Ldb(url, credentials=creds, lp=lp_ctx, options=["modules:paged_searches"])
65 res = db.search(base="", expression="",
67 attrs=["schemaNamingContext"])
70 schema_base = rootDse["schemaNamingContext"][0]
72 def possible_inferiors_search(db, oc):
73 """return the possible inferiors via a search for the possibleInferiors attribute"""
74 res = db.search(base=schema_base,
75 expression=("ldapDisplayName=%s" % oc),
76 attrs=["possibleInferiors"])
79 if len(res) == 0 or res[0].get("possibleInferiors") is None:
81 for item in res[0]["possibleInferiors"]:
82 poss.append(str(item))
83 poss = uniq_list(poss)
89 # see [MS-ADTS] section 3.1.1.4.5.21
90 # and section 3.1.1.4.2 for this algorithm
93 # !objectClassCategory=2
94 # !objectClassCategory=3
96 def SUPCLASSES(classinfo, oc):
100 if classinfo[oc].get("SUPCLASSES") is not None:
101 return classinfo[oc]["SUPCLASSES"]
102 res = classinfo[oc]["subClassOf"];
105 list.extend(SUPCLASSES(classinfo,r))
106 classinfo[oc]["SUPCLASSES"] = list
109 def AUXCLASSES(classinfo, oclist):
114 if classinfo[oc].get("AUXCLASSES") is not None:
115 list.extend(classinfo[oc]["AUXCLASSES"])
118 list2.extend(classinfo[oc]["systemAuxiliaryClass"])
119 list2.extend(AUXCLASSES(classinfo, classinfo[oc]["systemAuxiliaryClass"]))
120 list2.extend(classinfo[oc]["auxiliaryClass"])
121 list2.extend(AUXCLASSES(classinfo, classinfo[oc]["auxiliaryClass"]))
122 list2.extend(AUXCLASSES(classinfo, SUPCLASSES(classinfo, oc)))
123 classinfo[oc]["AUXCLASSES"] = list2
127 def SUBCLASSES(classinfo, oclist):
130 list.extend(classinfo[oc]["SUBCLASSES"])
133 def POSSSUPERIORS(classinfo, oclist):
136 if classinfo[oc].get("POSSSUPERIORS") is not None:
137 list.extend(classinfo[oc]["POSSSUPERIORS"])
140 list2.extend(classinfo[oc]["systemPossSuperiors"])
141 list2.extend(classinfo[oc]["possSuperiors"])
142 list2.extend(POSSSUPERIORS(classinfo, SUPCLASSES(classinfo, oc)))
144 # the WSPP docs suggest we should do this:
145 list2.extend(POSSSUPERIORS(classinfo, AUXCLASSES(classinfo, [oc])))
147 # but testing against w2k3 and w2k8 shows that we need to do this instead
148 list2.extend(SUBCLASSES(classinfo, list2))
149 classinfo[oc]["POSSSUPERIORS"] = list2
153 def pull_classinfo(db):
154 """At startup we build a classinfo[] dictionary that holds all the information needed to construct the possible inferiors"""
156 res = db.search(base=schema_base,
157 expression="objectclass=classSchema",
158 attrs=["ldapDisplayName", "systemOnly", "objectClassCategory",
159 "possSuperiors", "systemPossSuperiors",
160 "auxiliaryClass", "systemAuxiliaryClass", "subClassOf"])
162 name = str(r["ldapDisplayName"][0])
164 if str(r["systemOnly"]) == "TRUE":
165 classinfo[name]["systemOnly"] = True
167 classinfo[name]["systemOnly"] = False
168 if r.get("objectClassCategory"):
169 classinfo[name]["objectClassCategory"] = int(r["objectClassCategory"][0])
171 classinfo[name]["objectClassCategory"] = 0
172 for a in [ "possSuperiors", "systemPossSuperiors",
173 "auxiliaryClass", "systemAuxiliaryClass",
175 classinfo[name][a] = []
178 classinfo[name][a].append(str(i))
180 # build a list of subclasses for each class
181 def subclasses_recurse(subclasses, oc):
182 list = subclasses[oc]
184 list.extend(subclasses_recurse(subclasses, c))
191 for c in classinfo[oc]["subClassOf"]:
193 subclasses[c].append(oc)
195 classinfo[oc]["SUBCLASSES"] = uniq_list(subclasses_recurse(subclasses, oc))
199 def is_in_list(list, c):
205 def possible_inferiors_constructed(db, classinfo, c):
208 superiors = POSSSUPERIORS(classinfo, [oc])
209 if (is_in_list(superiors, c) and
210 classinfo[oc]["systemOnly"] == False and
211 classinfo[oc]["objectClassCategory"] != 2 and
212 classinfo[oc]["objectClassCategory"] != 3):
214 list = uniq_list(list)
218 def test_class(db, classinfo, oc):
219 """test to see if one objectclass returns the correct possibleInferiors"""
220 print "Testing objectClass %s" % oc
221 poss1 = possible_inferiors_search(db, oc)
222 poss2 = possible_inferiors_constructed(db, classinfo, oc)
224 print "Returned incorrect list for objectclass %s" % oc
225 print "search: %s" % poss1
226 print "constructed: %s" % poss2
227 for i in range(0,min(len(poss1),len(poss2))):
228 print "%30s %30s" % (poss1[i], poss2[i])
231 def get_object_classes(db):
232 """return a list of all object classes"""
234 for item in classinfo:
238 classinfo = pull_classinfo(db)
240 if objectclass is None:
241 for oc in get_object_classes(db):
242 test_class(db,classinfo,oc)
244 test_class(db,classinfo,objectclass)
246 print "Lists match OK"