s4-dbchecker: fixed handling of DSDB DNs in dbcheck
[mat/samba.git] / source4 / scripting / python / samba / dbchecker.py
1 #!/usr/bin/env python
2 #
3 # Samba4 AD database checker
4 #
5 # Copyright (C) Andrew Tridgell 2011
6 # Copyright (C) Matthieu Patou <mat@matws.net> 2011
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 import ldb
23 from samba import dsdb
24 from samba import common
25 from samba.dcerpc import misc
26 from samba.ndr import ndr_unpack
27 from samba.dcerpc import drsblobs
28
29
30 class dsdb_DN(object):
31     '''a class to manipulate DN components'''
32
33     def __init__(self, samdb, dnstring, syntax_oid):
34         if syntax_oid in [ dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_STRING_DN ]:
35             colons = dnstring.split(':')
36             if len(colons) < 4:
37                 raise Exception("invalid DN prefix")
38             prefix_len = 4 + len(colons[1]) + int(colons[1])
39             self.prefix = dnstring[0:prefix_len]
40             self.dnstring = dnstring[prefix_len:]
41         else:
42             self.dnstring = dnstring
43             self.prefix = ''
44         try:
45             self.dn = ldb.Dn(samdb, self.dnstring)
46         except Exception, msg:
47             print("ERROR: bad DN string '%s'" % self.dnstring)
48             raise
49
50     def __str__(self):
51         return self.prefix + str(self.dn.extended_str(mode=1))
52
53 class dbcheck(object):
54     """check a SAM database for errors"""
55
56     def __init__(self, samdb, samdb_schema=None, verbose=False, fix=False, yes=False, quiet=False):
57         self.samdb = samdb
58         self.dict_oid_name = None
59         self.samdb_schema = (samdb_schema or samdb)
60         self.verbose = verbose
61         self.fix = fix
62         self.yes = yes
63         self.quiet = quiet
64         self.remove_all_unknown_attributes = False
65         self.remove_all_empty_attributes = False
66         self.fix_all_normalisation = False
67         self.fix_all_DN_GUIDs = False
68         self.remove_all_deleted_DN_links = False
69         self.fix_all_target_mismatch = False
70         self.fix_all_metadata = False
71         self.fix_all_missing_backlinks = False
72         self.fix_all_orphaned_backlinks = False
73
74     def check_database(self, DN=None, scope=ldb.SCOPE_SUBTREE, controls=[], attrs=['*']):
75         '''perform a database check, returning the number of errors found'''
76
77         res = self.samdb.search(base=DN, scope=scope, attrs=['dn'], controls=controls)
78         self.report('Checking %u objects' % len(res))
79         error_count = 0
80
81         for object in res:
82             error_count += self.check_object(object.dn, attrs=attrs)
83
84         if DN is None:
85             error_count += self.check_rootdse()
86
87         if error_count != 0 and not self.fix:
88             self.report("Please use --fix to fix these errors")
89
90
91         self.report('Checked %u objects (%u errors)' % (len(res), error_count))
92
93         return error_count
94
95
96     def report(self, msg):
97         '''print a message unless quiet is set'''
98         if not self.quiet:
99             print(msg)
100
101
102     ################################################################
103     # a local confirm function that obeys the --fix and --yes options
104     def confirm(self, msg, allow_all=False, forced=False):
105         '''confirm a change'''
106         if not self.fix:
107             return False
108         if self.quiet:
109             return self.yes
110         if self.yes:
111             forced = True
112         return common.confirm(msg, forced=forced, allow_all=allow_all)
113
114     ################################################################
115     # a local confirm function with support for 'all'
116     def confirm_all(self, msg, all_attr):
117         '''confirm a change with support for "all" '''
118         if not self.fix:
119             return False
120         if self.quiet:
121             return self.yes
122         if getattr(self, all_attr) == 'NONE':
123             return False
124         if getattr(self, all_attr) == 'ALL':
125             forced = True
126         else:
127             forced = self.yes
128         c = common.confirm(msg, forced=forced, allow_all=True)
129         if c == 'ALL':
130             setattr(self, all_attr, 'ALL')
131             return True
132         if c == 'NONE':
133             setattr(self, all_attr, 'NONE')
134             return True
135         return c
136
137
138     def do_modify(self, m, controls, msg, validate=True):
139         '''perform a modify with optional verbose output'''
140         if self.verbose:
141             self.report(self.samdb.write_ldif(m, ldb.CHANGETYPE_MODIFY))
142         try:
143             self.samdb.modify(m, controls=controls, validate=validate)
144         except Exception, err:
145             self.report("%s : %s" % (msg, err))
146             return False
147         return True
148
149
150     ################################################################
151     # handle empty attributes
152     def err_empty_attribute(self, dn, attrname):
153         '''fix empty attributes'''
154         self.report("ERROR: Empty attribute %s in %s" % (attrname, dn))
155         if not self.confirm_all('Remove empty attribute %s from %s?' % (attrname, dn), 'remove_all_empty_attributes'):
156             self.report("Not fixing empty attribute %s" % attrname)
157             return
158
159         m = ldb.Message()
160         m.dn = dn
161         m[attrname] = ldb.MessageElement('', ldb.FLAG_MOD_DELETE, attrname)
162         if self.do_modify(m, ["relax:0", "show_recycled:1"],
163                           "Failed to remove empty attribute %s" % attrname, validate=False):
164             self.report("Removed empty attribute %s" % attrname)
165
166
167     ################################################################
168     # handle normalisation mismatches
169     def err_normalise_mismatch(self, dn, attrname, values):
170         '''fix attribute normalisation errors'''
171         self.report("ERROR: Normalisation error for attribute %s in %s" % (attrname, dn))
172         mod_list = []
173         for val in values:
174             normalised = self.samdb.dsdb_normalise_attributes(self.samdb_schema, attrname, [val])
175             if len(normalised) != 1:
176                 self.report("Unable to normalise value '%s'" % val)
177                 mod_list.append((val, ''))
178             elif (normalised[0] != val):
179                 self.report("value '%s' should be '%s'" % (val, normalised[0]))
180                 mod_list.append((val, normalised[0]))
181         if not self.confirm_all('Fix normalisation for %s from %s?' % (attrname, dn), 'fix_all_normalisation'):
182             self.report("Not fixing attribute %s" % attrname)
183             return
184
185         m = ldb.Message()
186         m.dn = dn
187         for i in range(0, len(mod_list)):
188             (val, nval) = mod_list[i]
189             m['value_%u' % i] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
190             if nval != '':
191                 m['normv_%u' % i] = ldb.MessageElement(nval, ldb.FLAG_MOD_ADD, attrname)
192
193         if self.do_modify(m, ["relax:0", "show_recycled:1"],
194                           "Failed to normalise attribute %s" % attrname,
195                           validate=False):
196             self.report("Normalised attribute %s" % attrname)
197
198     def is_deleted_objects_dn(self, dsdb_dn):
199         '''see if a dsdb_DN is the special Deleted Objects DN'''
200         return dsdb_dn.prefix == "B:32:18E2EA80684F11D2B9AA00C04F79F805:"
201
202
203     ################################################################
204     # handle a missing GUID extended DN component
205     def err_incorrect_dn_GUID(self, dn, attrname, val, dsdb_dn, errstr):
206         self.report("ERROR: %s component for %s in object %s - %s" % (errstr, attrname, dn, val))
207         controls=["extended_dn:1:1", "show_recycled:1"]
208         try:
209             res = self.samdb.search(base=str(dsdb_dn.dn), scope=ldb.SCOPE_BASE,
210                                     attrs=[], controls=controls)
211         except ldb.LdbError, (enum, estr):
212             self.report("unable to find object for DN %s - cannot fix (%s)" % (dsdb_dn.dn, estr))
213             return
214         if len(res) == 0:
215             self.report("unable to find object for DN %s - cannot fix" % dsdb_dn.dn)
216             return
217         dsdb_dn.dn = res[0].dn
218
219         if not self.confirm_all('Change DN to %s?' % str(dsdb_dn), 'fix_all_DN_GUIDs'):
220             self.report("Not fixing %s" % errstr)
221             return
222         m = ldb.Message()
223         m.dn = dn
224         m['old_value'] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
225         m['new_value'] = ldb.MessageElement(str(dsdb_dn), ldb.FLAG_MOD_ADD, attrname)
226
227         if self.do_modify(m, ["show_recycled:1"],
228                           "Failed to fix %s on attribute %s" % (errstr, attrname)):
229             self.report("Fixed %s on attribute %s" % (errstr, attrname))
230
231
232     ################################################################
233     # handle a DN pointing to a deleted object
234     def err_deleted_dn(self, dn, attrname, val, dsdb_dn, correct_dn):
235         self.report("ERROR: target DN is deleted for %s in object %s - %s" % (attrname, dn, val))
236         self.report("Target GUID points at deleted DN %s" % correct_dn)
237         if not self.confirm_all('Remove DN?', 'remove_all_deleted_DN_links'):
238             self.report("Not removing")
239             return
240         m = ldb.Message()
241         m.dn = dn
242         m['old_value'] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
243         if self.do_modify(m, ["show_recycled:1"],
244                           "Failed to remove deleted DN attribute %s" % attrname):
245             self.report("Removed deleted DN on attribute %s" % attrname)
246
247
248     ################################################################
249     # handle a DN string being incorrect
250     def err_dn_target_mismatch(self, dn, attrname, val, dsdb_dn, correct_dn, errstr):
251         self.report("ERROR: incorrect DN string component for %s in object %s - %s" % (attrname, dn, val))
252         dsdb_dn.dn = correct_dn
253
254         if not self.confirm_all('Change DN to %s?' % str(dsdb_dn), 'fix_all_target_mismatch'):
255             self.report("Not fixing %s" % errstr)
256             return
257         m = ldb.Message()
258         m.dn = dn
259         m['old_value'] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
260         m['new_value'] = ldb.MessageElement(str(dsdb_dn), ldb.FLAG_MOD_ADD, attrname)
261         if self.do_modify(m, ["show_recycled:1"],
262                           "Failed to fix incorrect DN string on attribute %s" % attrname):
263             self.report("Fixed incorrect DN string on attribute %s" % (attrname))
264
265     ################################################################
266     # handle an unknown attribute error
267     def err_unknown_attribute(self, obj, attrname):
268         '''handle an unknown attribute error'''
269         self.report("ERROR: unknown attribute '%s' in %s" % (attrname, obj.dn))
270         if not self.confirm_all('Remove unknown attribute %s' % attrname, 'remove_all_unknown_attributes'):
271             self.report("Not removing %s" % attrname)
272             return
273         m = ldb.Message()
274         m.dn = obj.dn
275         m['old_value'] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, attrname)
276         if self.do_modify(m, ["relax:0", "show_recycled:1"],
277                           "Failed to remove unknown attribute %s" % attrname):
278             self.report("Removed unknown attribute %s" % (attrname))
279
280
281     ################################################################
282     # handle a missing backlink
283     def err_missing_backlink(self, obj, attrname, val, backlink_name, target_dn):
284         '''handle a missing backlink value'''
285         self.report("ERROR: missing backlink attribute '%s' in %s for link %s in %s" % (backlink_name, target_dn, attrname, obj.dn))
286         if not self.confirm_all('Fix missing backlink %s' % backlink_name, 'fix_all_missing_backlinks'):
287             self.report("Not fixing missing backlink %s" % backlink_name)
288             return
289         m = ldb.Message()
290         m.dn = obj.dn
291         m['old_value'] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
292         m['new_value'] = ldb.MessageElement(val, ldb.FLAG_MOD_ADD, attrname)
293         if self.do_modify(m, ["show_recycled:1"],
294                           "Failed to fix missing backlink %s" % backlink_name):
295             self.report("Fixed missing backlink %s" % (backlink_name))
296
297
298     ################################################################
299     # handle a orphaned backlink
300     def err_orphaned_backlink(self, obj, attrname, val, link_name, target_dn):
301         '''handle a orphaned backlink value'''
302         self.report("ERROR: orphaned backlink attribute '%s' in %s for link %s in %s" % (attrname, obj.dn, link_name, target_dn))
303         if not self.confirm_all('Remove orphaned backlink %s' % link_name, 'fix_all_orphaned_backlinks'):
304             self.report("Not removing orphaned backlink %s" % link_name)
305             return
306         m = ldb.Message()
307         m.dn = obj.dn
308         m['value'] = ldb.MessageElement(val, ldb.FLAG_MOD_DELETE, attrname)
309         if self.do_modify(m, ["show_recycled:1", "relax:0"],
310                           "Failed to fix orphaned backlink %s" % link_name):
311             self.report("Fixed orphaned backlink %s" % (link_name))
312
313
314     ################################################################
315     # specialised checking for a dn attribute
316     def check_dn(self, obj, attrname, syntax_oid):
317         '''check a DN attribute for correctness'''
318         error_count = 0
319         for val in obj[attrname]:
320             dsdb_dn = dsdb_DN(self.samdb, val, syntax_oid)
321
322             # all DNs should have a GUID component
323             guid = dsdb_dn.dn.get_extended_component("GUID")
324             if guid is None:
325                 error_count += 1
326                 self.err_incorrect_dn_GUID(obj.dn, attrname, val, dsdb_dn, "missing GUID")
327                 continue
328
329             guidstr = str(misc.GUID(guid))
330
331             attrs=['isDeleted']
332             linkkID = self.samdb_schema.get_linkId_from_lDAPDisplayName(attrname)
333             reverse_link_name = self.samdb_schema.get_backlink_from_lDAPDisplayName(attrname)
334             if reverse_link_name is not None:
335                 attrs.append(reverse_link_name)
336
337             # check its the right GUID
338             try:
339                 res = self.samdb.search(base="<GUID=%s>" % guidstr, scope=ldb.SCOPE_BASE,
340                                         attrs=attrs, controls=["extended_dn:1:1", "show_recycled:1"])
341             except ldb.LdbError, (enum, estr):
342                 error_count += 1
343                 self.err_incorrect_dn_GUID(obj.dn, attrname, val, dsdb_dn, "incorrect GUID")
344                 continue
345
346             # now we have two cases - the source object might or might not be deleted
347             is_deleted = 'isDeleted' in obj and obj['isDeleted'][0].upper() == 'TRUE'
348             target_is_deleted = 'isDeleted' in res[0] and res[0]['isDeleted'][0].upper() == 'TRUE'
349
350             # the target DN is not allowed to be deleted, unless the target DN is the
351             # special Deleted Objects container
352             if target_is_deleted and not is_deleted and not self.is_deleted_objects_dn(dsdb_dn):
353                 error_count += 1
354                 self.err_deleted_dn(obj.dn, attrname, val, dsdb_dn, res[0].dn)
355                 continue
356
357             # check the DN matches in string form
358             if res[0].dn.extended_str() != dsdb_dn.dn.extended_str():
359                 error_count += 1
360                 self.err_dn_target_mismatch(obj.dn, attrname, val, dsdb_dn,
361                                             res[0].dn, "incorrect string version of DN")
362                 continue
363
364             # check the reverse_link is correct if there should be one
365             if reverse_link_name is not None:
366                 match_count = 0
367                 if reverse_link_name in res[0]:
368                     for v in res[0][reverse_link_name]:
369                         if v == obj.dn.extended_str():
370                             match_count += 1
371                 if match_count != 1:
372                     error_count += 1
373                     if linkkID & 1:
374                         self.err_orphaned_backlink(obj, attrname, val, reverse_link_name, dsdb_dn.dn)
375                     else:
376                         self.err_missing_backlink(obj, attrname, val, reverse_link_name, dsdb_dn.dn)
377                     continue
378
379         return error_count
380
381
382     def process_metadata(self, val):
383         '''Read metadata properties and list attributes in it'''
384
385         list_att = []
386
387         repl = ndr_unpack(drsblobs.replPropertyMetaDataBlob, str(val))
388         obj = repl.ctr
389
390         for o in repl.ctr.array:
391             att = self.samdb_schema.get_lDAPDisplayName_by_attid(o.attid)
392             list_att.append(att.lower())
393
394         return list_att
395
396
397     def fix_metadata(self, dn, attr):
398         '''re-write replPropertyMetaData elements for a single attribute for a
399         object. This is used to fix missing replPropertyMetaData elements'''
400         res = self.samdb.search(base = dn, scope=ldb.SCOPE_BASE, attrs = [attr],
401                                 controls = ["search_options:1:2", "show_recycled:1"])
402         msg = res[0]
403         nmsg = ldb.Message()
404         nmsg.dn = dn
405         nmsg[attr] = ldb.MessageElement(msg[attr], ldb.FLAG_MOD_REPLACE, attr)
406         if self.do_modify(nmsg, ["relax:0", "provision:0", "show_recycled:1"],
407                           "Failed to fix metadata for attribute %s" % attr):
408             self.report("Fixed metadata for attribute %s" % attr)
409
410
411     ################################################################
412     # check one object - calls to individual error handlers above
413     def check_object(self, dn, attrs=['*']):
414         '''check one object'''
415         if self.verbose:
416             self.report("Checking object %s" % dn)
417         if '*' in attrs:
418             attrs.append("replPropertyMetaData")
419
420         res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE,
421                                 controls=["extended_dn:1:1", "show_recycled:1"],
422                                 attrs=attrs)
423         if len(res) != 1:
424             self.report("Object %s disappeared during check" % dn)
425             return 1
426         obj = res[0]
427         error_count = 0
428         list_attrs_from_md = []
429         list_attrs_seen = []
430         got_repl_property_meta_data = False
431
432         for attrname in obj:
433             if attrname == 'dn':
434                 continue
435
436             if str(attrname).lower() == 'replpropertymetadata':
437                 list_attrs_from_md = self.process_metadata(obj[attrname])
438                 got_repl_property_meta_data = True
439                 continue
440
441
442             # check for empty attributes
443             for val in obj[attrname]:
444                 if val == '':
445                     self.err_empty_attribute(dn, attrname)
446                     error_count += 1
447                     continue
448
449             # get the syntax oid for the attribute, so we can can have
450             # special handling for some specific attribute types
451             try:
452                 syntax_oid = self.samdb_schema.get_syntax_oid_from_lDAPDisplayName(attrname)
453             except Exception, msg:
454                 self.err_unknown_attribute(obj, attrname)
455                 error_count += 1
456                 continue
457
458             flag = self.samdb_schema.get_systemFlags_from_lDAPDisplayName(attrname)
459             if (not flag & dsdb.DS_FLAG_ATTR_NOT_REPLICATED
460                 and not flag & dsdb.DS_FLAG_ATTR_IS_CONSTRUCTED
461                 and not self.samdb_schema.get_linkId_from_lDAPDisplayName(attrname)):
462                 list_attrs_seen.append(str(attrname).lower())
463
464             if syntax_oid in [ dsdb.DSDB_SYNTAX_BINARY_DN, dsdb.DSDB_SYNTAX_OR_NAME,
465                                dsdb.DSDB_SYNTAX_STRING_DN, ldb.SYNTAX_DN ]:
466                 # it's some form of DN, do specialised checking on those
467                 error_count += self.check_dn(obj, attrname, syntax_oid)
468
469             # check for incorrectly normalised attributes
470             for val in obj[attrname]:
471                 normalised = self.samdb.dsdb_normalise_attributes(self.samdb_schema, attrname, [val])
472                 if len(normalised) != 1 or normalised[0] != val:
473                     self.err_normalise_mismatch(dn, attrname, obj[attrname])
474                     error_count += 1
475                     break
476
477         show_dn = True
478         if got_repl_property_meta_data:
479             for att in list_attrs_seen:
480                 if not att in list_attrs_from_md:
481                     if show_dn:
482                         self.report("On object %s" % dn)
483                         show_dn = False
484                     error_count += 1
485                     self.report("ERROR: Attribute %s not present in replication metadata" % att)
486                     if not self.confirm_all("Fix missing replPropertyMetaData element '%s'" % att, 'fix_all_metadata'):
487                         self.report("Not fixing missing replPropertyMetaData element '%s'" % att)
488                         continue
489                     self.fix_metadata(dn, att)
490
491         return error_count
492
493     ################################################################
494     # check special @ROOTDSE attributes
495     def check_rootdse(self):
496         '''check the @ROOTDSE special object'''
497         dn = ldb.Dn(self.samdb, '@ROOTDSE')
498         if self.verbose:
499             self.report("Checking object %s" % dn)
500         res = self.samdb.search(base=dn, scope=ldb.SCOPE_BASE)
501         if len(res) != 1:
502             self.report("Object %s disappeared during check" % dn)
503             return 1
504         obj = res[0]
505         error_count = 0
506
507         # check that the dsServiceName is in GUID form
508         if not 'dsServiceName' in obj:
509             self.report('ERROR: dsServiceName missing in @ROOTDSE')
510             return error_count+1
511
512         if not obj['dsServiceName'][0].startswith('<GUID='):
513             self.report('ERROR: dsServiceName not in GUID form in @ROOTDSE')
514             error_count += 1
515             if not self.confirm('Change dsServiceName to GUID form?'):
516                 return error_count
517             res = self.samdb.search(base=ldb.Dn(self.samdb, obj['dsServiceName'][0]),
518                                     scope=ldb.SCOPE_BASE, attrs=['objectGUID'])
519             guid_str = str(ndr_unpack(misc.GUID, res[0]['objectGUID'][0]))
520             m = ldb.Message()
521             m.dn = dn
522             m['dsServiceName'] = ldb.MessageElement("<GUID=%s>" % guid_str,
523                                                     ldb.FLAG_MOD_REPLACE, 'dsServiceName')
524             if self.do_modify(m, [], "Failed to change dsServiceName to GUID form", validate=False):
525                 self.report("Changed dsServiceName to GUID form")
526         return error_count
527
528
529     ###############################################
530     # re-index the database
531     def reindex_database(self):
532         '''re-index the whole database'''
533         m = ldb.Message()
534         m.dn = ldb.Dn(self.samdb, "@ATTRIBUTES")
535         m['add']    = ldb.MessageElement('NONE', ldb.FLAG_MOD_ADD, 'force_reindex')
536         m['delete'] = ldb.MessageElement('NONE', ldb.FLAG_MOD_DELETE, 'force_reindex')
537         return self.do_modify(m, [], 're-indexed database', validate=False)