dsdb:schema: use NUMERIC_CMP in place of uint32_cmp
[Samba.git] / source4 / scripting / bin / get-descriptors
blob6e6922282f6d467c6cb730383495899576a3a9ac
1 #!/usr/bin/env python3
3 # Unix SMB/CIFS implementation.
4 # A script to compare differences of security descriotors between
5 # a remote host and the local Ldb
6 # Needs the local domain, the remote domain, IP of the remote host
7 # Username and password for the remote domain, must be at least
8 # Domain Administrator
10 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
11 # Copyright (C) Nadezhda Ivanova <nadezhda.ivanova@postpath.com> 2009
13 # Based on the original in EJS:
14 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 3 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
30 import optparse
31 import sys
32 import base64
34 sys.path.insert(0, "bin/python")
36 import samba
37 from samba.auth import system_session
38 import samba.getopt as options
39 from samba.ndr import ndr_pack, ndr_unpack
40 from samba.dcerpc import security
41 from samba import Ldb
42 from samba.samdb import SamDB
43 from ldb import SCOPE_SUBTREE, SCOPE_BASE
45 parser = optparse.OptionParser("get-descriptors [options]")
46 sambaopts = options.SambaOptions(parser)
47 credopts = options.CredentialsOptions(parser)
48 parser.add_option_group(credopts)
50 parser.add_option("--local-domain", type="string", metavar="LOCALDOMAIN",
51                   help="set local domain")
52 parser.add_option("--remote-domain", type="string", metavar="REMOTEDOMAIN",
53                   help="set remote domain")
54 parser.add_option("--host", type="string", metavar="HOST",
55                   help="Ip of the remote host used for comparison")
56 parser.add_option("--as-ldif", help="Output in LDIF format", action="store_true")
58 lp = sambaopts.get_loadparm()
59 creds = credopts.get_credentials(lp)
61 opts = parser.parse_args()[0]
63 if not opts.host or not opts.localdomain or not opts.remote_domain:
64     parser.print_usage()
65     sys.exit(1)
67 class DescrGetter:
69     def __init__(self, localdomain, remotedomain):
70         self.samdb = SamDB(session_info=system_session(), lp=lp, options=["modules:paged_searches"])
71         self.remote_ldb= Ldb("ldap://" + opts.host + ":389", credentials=creds, lp=lp,
72                              options=["modules:paged_searches"])
73         self.local_domain = localdomain.replace(".", ",DC=")
74         self.local_domain = "DC=" + self.local_domain
75         self.remote_domain = remotedomain.replace(".", ",DC=")
76         self.remote_domain = "DC=" + self.remote_domain
77         self.local_map = {}
78         self.remote_map = {}
80     def get_domain_local_sid(self):
81         res = self.samdb.search(base=self.local_domain,expression="(objectClass=*)", scope=SCOPE_BASE)
82         self.local_sid = ndr_unpack( security.dom_sid,res[0]["objectSid"][0])
84     def get_domain_remote_sid(self):
85         res = self.remote_ldb.search(base=self.remote_domain, expression="(objectClass=*)", scope=SCOPE_BASE)
86         self.remote_sid = ndr_unpack( security.dom_sid,res[0]["objectSid"][0])
88     def add_to_ldif(self, dn, descr):
89         ldif_entry = ["dn: " + dn,
90                       "changetype: modify",
91                       "replace: nTSecurityDescriptor",
92                       "nTSecurityDescriptor::  " + base64.b64encode(ndr_pack(descr)).decode('utf8')]
94         for line in ldif_entry:
95             length = 79
96             if len(line) <= length + 1:
97                 print(line)
98             else:
99                 for i in range(len(line) / length + 1):
100                     if i == 0:
101                         l = line[i * length:((i + 1) * length)]
102                     else:
103                         l = " " + line[(i * length):((i + 1) * length)]
104                     print(l)
105         print("\n")
107     def write_as_sddl(self, dn, descr):
108         print(dn)
109         print(descr + "\n")
111     def read_descr_by_base(self, search_base):
112         res = self.samdb.search(base=search_base + self.local_domain, expression="(objectClass=*)", scope=SCOPE_SUBTREE, attrs=["nTSecurityDescriptor"])
113         for entry in res:
114             dn = entry["dn"].__str__().replace(self.local_domain, "")
116             if "nTSecurityDescriptor" in entry:
117                 desc_obj = ndr_unpack(security.descriptor, entry["nTSecurityDescriptor"][0])
118                 self.local_map[dn] = desc_obj
120         res = self.remote_ldb.search(base=search_base + self.remote_domain, expression="(objectClass=*)", scope=SCOPE_SUBTREE, attrs=["nTSecurityDescriptor"])
121         for entry in res:
122             dn = entry["dn"].__str__().replace(self.remote_domain, "")
124             if "nTSecurityDescriptor" in entry:
125                 desc_obj = ndr_unpack(security.descriptor, entry["nTSecurityDescriptor"][0])
126                 self.remote_map[dn] = desc_obj
128     def read_desc(self):
129         self.read_descr_by_base("CN=Schema,CN=Configuration,")
130         self.read_descr_by_base("CN=Configuration,")
131         self.read_descr_by_base("")
133     def write_desc_to_ldif(self):
134         key_list_local = self.local_map.keys()
135         key_list_remote = self.remote_map.keys()
136         for key in key_list_remote:
137             if key in key_list_local:
138                 sddl = self.remote_map[key].as_sddl(self.remote_sid)
139                 sddl_local = self.local_map[key].as_sddl(self.local_sid)
140                 if sddl != sddl_local:
141                     descr = security.descriptor.from_sddl(sddl, self.local_sid)
142                 if opts.as_ldif:
143                     self.add_to_ldif(key + self.local_domain, descr)
144                 else:
145                     self.write_as_sddl(key, descr.as_sddl(self.local_sid))
147     def run(self):
148         self.get_domain_local_sid()
149         self.get_domain_remote_sid()
150         self.read_desc()
151         self.write_desc_to_ldif()
153 desc = DescrGetter(opts.local_domain, opts.remote_domain)
154 desc.run()