python/samba: let get_domain_descriptor() include adprep 2016 ACEs
[Samba.git] / python / samba / descriptor.py
blob5b911685db81e785af6ca14153c45c46f125b6ee
2 # Unix SMB/CIFS implementation.
3 # backend code for provisioning a Samba4 server
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
6 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008-2009
7 # Copyright (C) Oliver Liebel <oliver@itc.li> 2008-2009
8 # Copyright (C) Amitay Isaacs <amitay@samba.org> 2011
10 # Based on the original in EJS:
11 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 """Functions for setting up a Samba configuration (security descriptors)."""
29 from samba.dcerpc import security
30 from samba.ndr import ndr_pack
31 from samba.schema import get_schema_descriptor
32 import ldb
33 import re
35 # Descriptors of naming contexts and other important objects
38 def sddl2binary(sddl_in, domain_sid, name_map):
39 sddl = "%s" % sddl_in
41 for [name, sid] in name_map.items():
42 sddl = sddl.replace(name, sid)
44 sec = security.descriptor.from_sddl(sddl, domain_sid)
45 return ndr_pack(sec)
48 def get_empty_descriptor(domain_sid, name_map=None):
49 if name_map is None:
50 name_map = {}
52 sddl = ""
53 return sddl2binary(sddl, domain_sid, name_map)
55 # "get_schema_descriptor" is located in "schema.py"
58 def get_config_descriptor(domain_sid, name_map=None):
59 if name_map is None:
60 name_map = {}
62 sddl = "O:EAG:EAD:(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
63 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
64 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
65 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
66 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
67 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
68 "(A;;RPLCLORC;;;AU)(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
69 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)(A;CIIO;RPWPCRCCLCLORCWOWDSDSW;;;DA)" \
70 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
71 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
72 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
73 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
74 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
75 "S:(AU;SA;WPWOWD;;;WD)(AU;SA;CR;;;BA)(AU;SA;CR;;;DU)" \
76 "(OU;SA;CR;45ec5156-db7e-47bb-b53f-dbeb2d03c40f;;WD)"
77 return sddl2binary(sddl, domain_sid, name_map)
80 def get_config_partitions_descriptor(domain_sid, name_map=None):
81 if name_map is None:
82 name_map = {}
84 sddl = "D:" \
85 "(A;;LCLORC;;;AU)" \
86 "(OA;;RP;e48d0154-bcf8-11d1-8702-00c04fb96050;;AU)" \
87 "(OA;;RP;d31a8757-2447-4545-8081-3bb610cacbf2;;AU)" \
88 "(OA;;RP;66171887-8f3c-11d0-afda-00c04fd930c9;;AU)" \
89 "(OA;;RP;032160bf-9824-11d1-aec0-0000f80367c1;;AU)" \
90 "(OA;;RP;789ee1eb-8c8e-4e4c-8cec-79b31b7617b5;;AU)" \
91 "(OA;;RP;5706aeaf-b940-4fb2-bcfc-5268683ad9fe;;AU)" \
92 "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
93 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
94 "(A;;CC;;;ED)" \
95 "(OA;CIIO;WP;3df793df-9858-4417-a701-735a1ecebf74;bf967a8d-0de6-11d0-a285-00aa003049e2;BA)" \
96 "S:" \
97 "(AU;CISA;WPCRCCDCWOWDSDDT;;;WD)"
98 return sddl2binary(sddl, domain_sid, name_map)
101 def get_config_sites_descriptor(domain_sid, name_map=None):
102 if name_map is None:
103 name_map = {}
105 sddl = "D:" \
106 "(A;;RPLCLORC;;;AU)" \
107 "(OA;CIIO;SW;d31a8757-2447-4545-8081-3bb610cacbf2;f0f8ffab-1191-11d0-a060-00aa006c33ed;RO)" \
108 "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
109 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
110 "S:" \
111 "(AU;CISA;CCDCSDDT;;;WD)" \
112 "(OU;CIIOSA;CR;;f0f8ffab-1191-11d0-a060-00aa006c33ed;WD)" \
113 "(OU;CIIOSA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967ab3-0de6-11d0-a285-00aa003049e2;WD)" \
114 "(OU;CIIOSA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967ab3-0de6-11d0-a285-00aa003049e2;WD)" \
115 "(OU;CIIOSA;WP;3e10944c-c354-11d0-aff8-0000f80367c1;b7b13124-b82e-11d0-afee-0000f80367c1;WD)"
116 return sddl2binary(sddl, domain_sid, name_map)
119 def get_config_ntds_quotas_descriptor(domain_sid, name_map=None):
120 if name_map is None:
121 name_map = {}
123 sddl = "D:" \
124 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
125 "(A;;RPLCLORC;;;BA)" \
126 "(OA;;CR;4ecc03fe-ffc0-4947-b630-eb672a8a9dbc;;WD)"
127 return sddl2binary(sddl, domain_sid, name_map)
130 def get_config_delete_protected1_descriptor(domain_sid, name_map=None):
131 if name_map is None:
132 name_map = {}
134 sddl = "D:AI" \
135 "(A;;RPLCLORC;;;AU)" \
136 "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
137 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
138 return sddl2binary(sddl, domain_sid, name_map)
141 def get_config_delete_protected1wd_descriptor(domain_sid, name_map=None):
142 if name_map is None:
143 name_map = {}
145 sddl = "D:AI" \
146 "(A;;RPLCLORC;;;WD)" \
147 "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
148 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
149 return sddl2binary(sddl, domain_sid, name_map)
152 def get_config_delete_protected2_descriptor(domain_sid, name_map=None):
153 if name_map is None:
154 name_map = {}
156 sddl = "D:AI" \
157 "(A;;RPLCLORC;;;AU)" \
158 "(A;;RPWPCRCCDCLCLORCWOWDSW;;;EA)" \
159 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
160 return sddl2binary(sddl, domain_sid, name_map)
163 def get_domain_descriptor(domain_sid, name_map=None):
164 if name_map is None:
165 name_map = {}
167 sddl = "O:BAG:BAD:AI(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
168 "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
169 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
170 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
171 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
172 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
173 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
174 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
175 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
176 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
177 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
178 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;DD)" \
179 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
180 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
181 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
182 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
183 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
184 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
185 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
186 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
187 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
188 "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
189 "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
190 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
191 "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
192 "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
193 "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
194 "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
195 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
196 "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
197 "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
198 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
199 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
200 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
201 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
202 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
203 "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
204 "(OA;CIIO;WP;ea1b7b93-5e48-46d5-bc6c-4df4fda78a35;bf967a86-0de6-11d0-a285-00aa003049e2;PS)" \
205 "(OA;;CR;3e0f7e18-2c7a-4c10-ba82-4d926db99a3e;;CN)" \
206 "(OA;OICI;RPWP;3f78c3e5-f79a-46bd-a0b8-9d18116ddc79;;PS)" \
207 "(OA;CI;RPWP;5b47d60f-6090-40b2-9f37-2a4de88f3063;;KA)" \
208 "(OA;CI;RPWP;5b47d60f-6090-40b2-9f37-2a4de88f3063;;EK)" \
209 "(OA;CIIO;SW;9b026da6-0d3c-465c-8bee-5199d7165cba;bf967a86-0de6-11d0-a285-00aa003049e2;PS)" \
210 "(OA;CIIO;SW;9b026da6-0d3c-465c-8bee-5199d7165cba;bf967a86-0de6-11d0-a285-00aa003049e2;CO)" \
211 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
212 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
213 "(A;;RPRC;;;RU)" \
214 "(A;CI;LC;;;RU)" \
215 "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
216 "(A;;RP;;;WD)" \
217 "(A;;RPLCLORC;;;ED)" \
218 "(A;;RPLCLORC;;;AU)" \
219 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
220 "S:AI(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
221 "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
222 "(AU;SA;CR;;;DU)(AU;SA;CR;;;BA)(AU;SA;WPWOWD;;;WD)"
223 return sddl2binary(sddl, domain_sid, name_map)
226 def get_domain_infrastructure_descriptor(domain_sid, name_map=None):
227 if name_map is None:
228 name_map = {}
230 sddl = "D:" \
231 "(A;;RPLCLORC;;;AU)" \
232 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
233 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
234 "S:" \
235 "(AU;SA;WPCR;;;WD)"
236 return sddl2binary(sddl, domain_sid, name_map)
239 def get_domain_builtin_descriptor(domain_sid, name_map=None):
240 if name_map is None:
241 name_map = {}
243 sddl = "D:" \
244 "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
245 "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
246 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
247 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
248 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
249 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
250 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
251 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
252 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
253 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
254 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
255 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;DD)" \
256 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
257 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
258 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
259 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
260 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
261 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
262 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
263 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
264 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
265 "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
266 "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
267 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
268 "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
269 "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
270 "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
271 "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
272 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
273 "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
274 "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
275 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
276 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
277 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
278 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
279 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
280 "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
281 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
282 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
283 "(A;;RPRC;;;RU)" \
284 "(A;CI;LC;;;RU)" \
285 "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
286 "(A;;RP;;;WD)" \
287 "(A;;RPLCLORC;;;ED)" \
288 "(A;;RPLCLORC;;;AU)" \
289 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
290 "S:" \
291 "(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
292 "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
293 "(AU;SA;CR;;;DU)" \
294 "(AU;SA;CR;;;BA)" \
295 "(AU;SA;WPWOWD;;;WD)"
296 return sddl2binary(sddl, domain_sid, name_map)
299 def get_domain_computers_descriptor(domain_sid, name_map=None):
300 if name_map is None:
301 name_map = {}
303 sddl = "D:" \
304 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
305 "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
306 "(OA;;CCDC;bf967a86-0de6-11d0-a285-00aa003049e2;;AO)" \
307 "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
308 "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
309 "(OA;;CCDC;bf967aa8-0de6-11d0-a285-00aa003049e2;;PO)" \
310 "(A;;RPLCLORC;;;AU)" \
311 "(OA;;CCDC;4828cc14-1437-45bc-9b07-ad6f015e5f28;;AO)" \
312 "S:"
313 return sddl2binary(sddl, domain_sid, name_map)
316 def get_domain_users_descriptor(domain_sid, name_map=None):
317 if name_map is None:
318 name_map = {}
320 sddl = "D:" \
321 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
322 "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
323 "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
324 "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
325 "(OA;;CCDC;bf967aa8-0de6-11d0-a285-00aa003049e2;;PO)" \
326 "(A;;RPLCLORC;;;AU)" \
327 "(OA;;CCDC;4828cc14-1437-45bc-9b07-ad6f015e5f28;;AO)" \
328 "S:"
329 return sddl2binary(sddl, domain_sid, name_map)
332 def get_managed_service_accounts_descriptor(domain_sid, name_map=None):
333 if name_map is None:
334 name_map = {}
336 sddl = "D:" \
337 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
338 "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
339 "(OA;;CCDC;ce206244-5827-4a86-ba1c-1c0c386c1b64;;AO)" \
340 "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
341 "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
342 "(A;;RPLCLORC;;;AU)" \
343 "S:"
344 return sddl2binary(sddl, domain_sid, name_map)
347 def get_domain_controllers_descriptor(domain_sid, name_map=None):
348 if name_map is None:
349 name_map = {}
351 sddl = "D:" \
352 "(A;;RPLCLORC;;;AU)" \
353 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
354 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
355 "(A;;RPLCLORC;;;ED)" \
356 "S:" \
357 "(AU;SA;CCDCWOWDSDDT;;;WD)" \
358 "(AU;CISA;WP;;;WD)"
359 return sddl2binary(sddl, domain_sid, name_map)
362 def get_domain_delete_protected1_descriptor(domain_sid, name_map=None):
363 if name_map is None:
364 name_map = {}
366 sddl = "D:AI" \
367 "(A;;RPLCLORC;;;AU)" \
368 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
369 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
370 return sddl2binary(sddl, domain_sid, name_map)
373 def get_domain_delete_protected2_descriptor(domain_sid, name_map=None):
374 if name_map is None:
375 name_map = {}
377 sddl = "D:AI" \
378 "(A;;RPLCLORC;;;AU)" \
379 "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
380 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
381 return sddl2binary(sddl, domain_sid, name_map)
384 def get_dns_partition_descriptor(domain_sid, name_map=None):
385 if name_map is None:
386 name_map = {}
388 sddl = "O:SYG:BAD:AI" \
389 "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
390 "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
391 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
392 "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
393 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
394 "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
395 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
396 "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
397 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
398 "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
399 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
400 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
401 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
402 "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
403 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
404 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
405 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
406 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
407 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
408 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
409 "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
410 "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
411 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
412 "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
413 "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
414 "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
415 "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
416 "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
417 "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
418 "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
419 "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
420 "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
421 "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
422 "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
423 "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
424 "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
425 "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
426 "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
427 "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
428 "(A;;RPRC;;;RU)" \
429 "(A;CI;LC;;;RU)" \
430 "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
431 "(A;;RP;;;WD)" \
432 "(A;;RPLCLORC;;;ED)" \
433 "(A;;RPLCLORC;;;AU)" \
434 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
435 "S:AI" \
436 "(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
437 "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
438 "(AU;SA;CR;;;DU)(AU;SA;CR;;;BA)(AU;SA;WPWOWD;;;WD)"
439 return sddl2binary(sddl, domain_sid, name_map)
442 def get_dns_forest_microsoft_dns_descriptor(domain_sid, name_map=None):
443 if name_map is None:
444 name_map = {}
446 sddl = "O:SYG:SYD:AI" \
447 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
448 "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
449 return sddl2binary(sddl, domain_sid, name_map)
452 def get_dns_domain_microsoft_dns_descriptor(domain_sid, name_map=None):
453 if name_map is None:
454 name_map = {}
456 sddl = "O:SYG:SYD:AI" \
457 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;DA)" \
458 "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;DnsAdmins)" \
459 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
460 "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
461 return sddl2binary(sddl, domain_sid, name_map)
464 def get_paritions_crossref_subdomain_descriptor(domain_sid, name_map=None):
465 if name_map is None:
466 name_map = {}
468 sddl = "O:SubdomainAdminsG:SubdomainAdminsD:AI" \
469 "(A;;RPWPCRCCLCLORCWOWDSW;;;SubdomainAdmins)" \
470 "(A;;RPLCLORC;;;AU)" \
471 "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
472 return sddl2binary(sddl, domain_sid, name_map)
475 def get_wellknown_sds(samdb):
477 # Then subcontainers
478 subcontainers = [
479 (ldb.Dn(samdb, "%s" % str(samdb.domain_dn())), get_domain_descriptor),
480 (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(samdb.domain_dn())), get_domain_delete_protected2_descriptor),
481 (ldb.Dn(samdb, "CN=System,%s" % str(samdb.domain_dn())), get_domain_delete_protected1_descriptor),
482 (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(samdb.domain_dn())), get_domain_infrastructure_descriptor),
483 (ldb.Dn(samdb, "CN=Builtin,%s" % str(samdb.domain_dn())), get_domain_builtin_descriptor),
484 (ldb.Dn(samdb, "CN=Computers,%s" % str(samdb.domain_dn())), get_domain_computers_descriptor),
485 (ldb.Dn(samdb, "CN=Users,%s" % str(samdb.domain_dn())), get_domain_users_descriptor),
486 (ldb.Dn(samdb, "OU=Domain Controllers,%s" % str(samdb.domain_dn())), get_domain_controllers_descriptor),
487 (ldb.Dn(samdb, "CN=MicrosoftDNS,CN=System,%s" % str(samdb.domain_dn())), get_dns_domain_microsoft_dns_descriptor),
489 (ldb.Dn(samdb, "%s" % str(samdb.get_config_basedn())), get_config_descriptor),
490 (ldb.Dn(samdb, "CN=NTDS Quotas,%s" % str(samdb.get_config_basedn())), get_config_ntds_quotas_descriptor),
491 (ldb.Dn(samdb, "CN=LostAndFoundConfig,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
492 (ldb.Dn(samdb, "CN=Services,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1_descriptor),
493 (ldb.Dn(samdb, "CN=Physical Locations,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
494 (ldb.Dn(samdb, "CN=WellKnown Security Principals,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
495 (ldb.Dn(samdb, "CN=ForestUpdates,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
496 (ldb.Dn(samdb, "CN=DisplaySpecifiers,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
497 (ldb.Dn(samdb, "CN=Extended-Rights,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
498 (ldb.Dn(samdb, "CN=Partitions,%s" % str(samdb.get_config_basedn())), get_config_partitions_descriptor),
499 (ldb.Dn(samdb, "CN=Sites,%s" % str(samdb.get_config_basedn())), get_config_sites_descriptor),
501 (ldb.Dn(samdb, "%s" % str(samdb.get_schema_basedn())), get_schema_descriptor),
504 current = samdb.search(expression="(objectClass=*)",
505 base="", scope=ldb.SCOPE_BASE,
506 attrs=["namingContexts"])
508 for nc in current[0]["namingContexts"]:
510 dnsforestdn = ldb.Dn(samdb, "DC=ForestDnsZones,%s" % (str(samdb.get_root_basedn())))
511 if ldb.Dn(samdb, nc.decode('utf8')) == dnsforestdn:
512 c = (ldb.Dn(samdb, "%s" % str(dnsforestdn)), get_dns_partition_descriptor)
513 subcontainers.append(c)
514 c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsforestdn)),
515 get_domain_delete_protected1_descriptor)
516 subcontainers.append(c)
517 c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsforestdn)),
518 get_domain_delete_protected2_descriptor)
519 subcontainers.append(c)
520 c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsforestdn)),
521 get_dns_forest_microsoft_dns_descriptor)
522 subcontainers.append(c)
523 continue
525 dnsdomaindn = ldb.Dn(samdb, "DC=DomainDnsZones,%s" % (str(samdb.domain_dn())))
526 if ldb.Dn(samdb, nc.decode('utf8')) == dnsdomaindn:
527 c = (ldb.Dn(samdb, "%s" % str(dnsdomaindn)), get_dns_partition_descriptor)
528 subcontainers.append(c)
529 c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsdomaindn)),
530 get_domain_delete_protected1_descriptor)
531 subcontainers.append(c)
532 c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsdomaindn)),
533 get_domain_delete_protected2_descriptor)
534 subcontainers.append(c)
535 c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsdomaindn)),
536 get_dns_domain_microsoft_dns_descriptor)
537 subcontainers.append(c)
539 return subcontainers
542 def chunck_acl(acl):
543 """Return separate ACE of an ACL
545 :param acl: A string representing the ACL
546 :return: A hash with different parts
549 p = re.compile(r'(\w+)?(\(.*?\))')
550 tab = p.findall(acl)
552 hash = {}
553 hash["aces"] = []
554 for e in tab:
555 if len(e[0]) > 0:
556 hash["flags"] = e[0]
557 hash["aces"].append(e[1])
559 return hash
562 def chunck_sddl(sddl):
563 """ Return separate parts of the SDDL (owner, group, ...)
565 :param sddl: An string containing the SDDL to chunk
566 :return: A hash with the different chunk
569 p = re.compile(r'([OGDS]:)(.*?)(?=(?:[GDS]:|$))')
570 tab = p.findall(sddl)
572 hash = {}
573 for e in tab:
574 if e[0] == "O:":
575 hash["owner"] = e[1]
576 if e[0] == "G:":
577 hash["group"] = e[1]
578 if e[0] == "D:":
579 hash["dacl"] = e[1]
580 if e[0] == "S:":
581 hash["sacl"] = e[1]
583 return hash
586 def get_clean_sd(sd):
587 """Get the SD without any inherited ACEs
589 :param sd: SD to strip
590 :return: An SD with inherited ACEs stripped
593 sd_clean = security.descriptor()
594 sd_clean.owner_sid = sd.owner_sid
595 sd_clean.group_sid = sd.group_sid
596 sd_clean.type = sd.type
597 sd_clean.revision = sd.revision
599 aces = []
600 if sd.sacl is not None:
601 aces = sd.sacl.aces
602 for i in range(0, len(aces)):
603 ace = aces[i]
605 if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
606 sd_clean.sacl_add(ace)
607 continue
609 aces = []
610 if sd.dacl is not None:
611 aces = sd.dacl.aces
612 for i in range(0, len(aces)):
613 ace = aces[i]
615 if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
616 sd_clean.dacl_add(ace)
617 continue
618 return sd_clean
621 def get_diff_sds(refsd, cursd, domainsid, checkSacl=True):
622 """Get the difference between 2 sd
624 This function split the textual representation of ACL into smaller
625 chunck in order to not to report a simple permutation as a difference
627 :param refsddl: First sddl to compare
628 :param cursddl: Second sddl to compare
629 :param checkSacl: If false we skip the sacl checks
630 :return: A string that explain difference between sddls
633 cursddl = get_clean_sd(cursd).as_sddl(domainsid)
634 refsddl = get_clean_sd(refsd).as_sddl(domainsid)
636 txt = ""
637 hash_cur = chunck_sddl(cursddl)
638 hash_ref = chunck_sddl(refsddl)
640 if "owner" not in hash_cur:
641 txt = "\tNo owner in current SD"
642 elif "owner" in hash_ref and hash_cur["owner"] != hash_ref["owner"]:
643 txt = "\tOwner mismatch: %s (in ref) %s" \
644 "(in current)\n" % (hash_ref["owner"], hash_cur["owner"])
646 if "group" not in hash_cur:
647 txt = "%s\tNo group in current SD" % txt
648 elif "group" in hash_ref and hash_cur["group"] != hash_ref["group"]:
649 txt = "%s\tGroup mismatch: %s (in ref) %s" \
650 "(in current)\n" % (txt, hash_ref["group"], hash_cur["group"])
652 parts = ["dacl"]
653 if checkSacl:
654 parts.append("sacl")
655 for part in parts:
656 if part in hash_cur and part in hash_ref:
658 # both are present, check if they contain the same ACE
659 h_cur = set()
660 h_ref = set()
661 c_cur = chunck_acl(hash_cur[part])
662 c_ref = chunck_acl(hash_ref[part])
664 for elem in c_cur["aces"]:
665 h_cur.add(elem)
667 for elem in c_ref["aces"]:
668 h_ref.add(elem)
670 for k in set(h_ref):
671 if k in h_cur:
672 h_cur.remove(k)
673 h_ref.remove(k)
675 if len(h_cur) + len(h_ref) > 0:
676 txt = "%s\tPart %s is different between reference" \
677 " and current here is the detail:\n" % (txt, part)
679 for item in h_cur:
680 txt = "%s\t\t%s ACE is not present in the" \
681 " reference\n" % (txt, item)
683 for item in h_ref:
684 txt = "%s\t\t%s ACE is not present in the" \
685 " current\n" % (txt, item)
687 elif part in hash_cur and part not in hash_ref:
688 txt = "%s\tReference ACL hasn't a %s part\n" % (txt, part)
689 elif part not in hash_cur and part in hash_ref:
690 txt = "%s\tCurrent ACL hasn't a %s part\n" % (txt, part)
692 return txt