python: Restore SDDL abbreviations for SIDs
[samba.git] / python / samba / descriptor.py
1
2 # Unix SMB/CIFS implementation.
3 # backend code for provisioning a Samba4 server
4
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
9 #
10 # Based on the original in EJS:
11 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
12 #
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.
17 #
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.
22 #
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/>.
25 #
26
27 """Functions for setting up a Samba configuration (security descriptors)."""
28
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
34
35 # Descriptors of naming contexts and other important objects
36
37
38 def sddl2binary(sddl_in, domain_sid, name_map):
39     sddl = "%s" % sddl_in
40
41     for [name, sid] in name_map.items():
42         sddl = sddl.replace(name, sid)
43
44     sec = security.descriptor.from_sddl(sddl, domain_sid)
45     return ndr_pack(sec)
46
47
48 def get_empty_descriptor(domain_sid, name_map={}):
49     sddl = ""
50     return sddl2binary(sddl, domain_sid, name_map)
51
52 # "get_schema_descriptor" is located in "schema.py"
53
54
55 def get_config_descriptor(domain_sid, name_map={}):
56     sddl = "O:EAG:EAD:(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
57            "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
58            "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
59            "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
60            "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
61            "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
62            "(A;;RPLCLORC;;;AU)(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
63            "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)(A;CIIO;RPWPCRCCLCLORCWOWDSDSW;;;DA)" \
64            "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
65            "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
66            "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
67            "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
68            "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
69            "S:(AU;SA;WPWOWD;;;WD)(AU;SA;CR;;;BA)(AU;SA;CR;;;DU)" \
70            "(OU;SA;CR;45ec5156-db7e-47bb-b53f-dbeb2d03c40f;;WD)"
71     return sddl2binary(sddl, domain_sid, name_map)
72
73
74 def get_config_partitions_descriptor(domain_sid, name_map={}):
75     sddl = "D:" \
76         "(A;;LCLORC;;;AU)" \
77         "(OA;;RP;e48d0154-bcf8-11d1-8702-00c04fb96050;;AU)" \
78         "(OA;;RP;d31a8757-2447-4545-8081-3bb610cacbf2;;AU)" \
79         "(OA;;RP;66171887-8f3c-11d0-afda-00c04fd930c9;;AU)" \
80         "(OA;;RP;032160bf-9824-11d1-aec0-0000f80367c1;;AU)" \
81         "(OA;;RP;789ee1eb-8c8e-4e4c-8cec-79b31b7617b5;;AU)" \
82         "(OA;;RP;5706aeaf-b940-4fb2-bcfc-5268683ad9fe;;AU)" \
83         "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
84         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
85         "(A;;CC;;;ED)" \
86         "(OA;CIIO;WP;3df793df-9858-4417-a701-735a1ecebf74;bf967a8d-0de6-11d0-a285-00aa003049e2;BA)" \
87         "S:" \
88         "(AU;CISA;WPCRCCDCWOWDSDDT;;;WD)"
89     return sddl2binary(sddl, domain_sid, name_map)
90
91
92 def get_config_sites_descriptor(domain_sid, name_map={}):
93     sddl = "D:" \
94         "(A;;RPLCLORC;;;AU)" \
95         "(OA;CIIO;SW;d31a8757-2447-4545-8081-3bb610cacbf2;f0f8ffab-1191-11d0-a060-00aa006c33ed;RO)" \
96         "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
97         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
98         "S:" \
99         "(AU;CISA;CCDCSDDT;;;WD)" \
100         "(OU;CIIOSA;CR;;f0f8ffab-1191-11d0-a060-00aa006c33ed;WD)" \
101         "(OU;CIIOSA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967ab3-0de6-11d0-a285-00aa003049e2;WD)" \
102         "(OU;CIIOSA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967ab3-0de6-11d0-a285-00aa003049e2;WD)" \
103         "(OU;CIIOSA;WP;3e10944c-c354-11d0-aff8-0000f80367c1;b7b13124-b82e-11d0-afee-0000f80367c1;WD)"
104     return sddl2binary(sddl, domain_sid, name_map)
105
106
107 def get_config_ntds_quotas_descriptor(domain_sid, name_map={}):
108     sddl = "D:" \
109         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
110         "(A;;RPLCLORC;;;BA)" \
111         "(OA;;CR;4ecc03fe-ffc0-4947-b630-eb672a8a9dbc;;WD)"
112     return sddl2binary(sddl, domain_sid, name_map)
113
114
115 def get_config_delete_protected1_descriptor(domain_sid, name_map={}):
116     sddl = "D:AI" \
117         "(A;;RPLCLORC;;;AU)" \
118         "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
119         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
120     return sddl2binary(sddl, domain_sid, name_map)
121
122
123 def get_config_delete_protected1wd_descriptor(domain_sid, name_map={}):
124     sddl = "D:AI" \
125         "(A;;RPLCLORC;;;WD)" \
126         "(A;;RPWPCRCCLCLORCWOWDSW;;;EA)" \
127         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
128     return sddl2binary(sddl, domain_sid, name_map)
129
130
131 def get_config_delete_protected2_descriptor(domain_sid, name_map={}):
132     sddl = "D:AI" \
133         "(A;;RPLCLORC;;;AU)" \
134         "(A;;RPWPCRCCDCLCLORCWOWDSW;;;EA)" \
135         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
136     return sddl2binary(sddl, domain_sid, name_map)
137
138
139 def get_domain_descriptor(domain_sid, name_map={}):
140     sddl = "O:BAG:BAD:AI(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
141         "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
142         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
143         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
144         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
145         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
146         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
147         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
148         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
149         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
150         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
151         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;DD)" \
152         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
153         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
154         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
155         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
156         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
157         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
158         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
159         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
160         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
161         "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
162         "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
163         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
164         "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
165         "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
166         "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
167         "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
168         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
169         "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
170         "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
171         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
172         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
173         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
174         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
175         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
176         "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
177         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
178         "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
179         "(A;;RPRC;;;RU)" \
180         "(A;CI;LC;;;RU)" \
181         "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
182         "(A;;RP;;;WD)" \
183         "(A;;RPLCLORC;;;ED)" \
184         "(A;;RPLCLORC;;;AU)" \
185         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
186         "S:AI(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
187         "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
188         "(AU;SA;CR;;;DU)(AU;SA;CR;;;BA)(AU;SA;WPWOWD;;;WD)"
189     return sddl2binary(sddl, domain_sid, name_map)
190
191
192 def get_domain_infrastructure_descriptor(domain_sid, name_map={}):
193     sddl = "D:" \
194         "(A;;RPLCLORC;;;AU)" \
195         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
196         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
197         "S:" \
198         "(AU;SA;WPCR;;;WD)"
199     return sddl2binary(sddl, domain_sid, name_map)
200
201
202 def get_domain_builtin_descriptor(domain_sid, name_map={}):
203     sddl = "D:" \
204         "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
205         "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
206         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
207         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
208         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
209         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
210         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
211         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
212         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
213         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
214         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
215         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;DD)" \
216         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
217         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
218         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
219         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
220         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
221         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
222         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
223         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
224         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
225         "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
226         "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
227         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
228         "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
229         "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
230         "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
231         "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
232         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
233         "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
234         "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
235         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
236         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
237         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
238         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
239         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
240         "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
241         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
242         "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
243         "(A;;RPRC;;;RU)" \
244         "(A;CI;LC;;;RU)" \
245         "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
246         "(A;;RP;;;WD)" \
247         "(A;;RPLCLORC;;;ED)" \
248         "(A;;RPLCLORC;;;AU)" \
249         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
250         "S:" \
251         "(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
252         "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
253         "(AU;SA;CR;;;DU)" \
254         "(AU;SA;CR;;;BA)" \
255         "(AU;SA;WPWOWD;;;WD)"
256     return sddl2binary(sddl, domain_sid, name_map)
257
258
259 def get_domain_computers_descriptor(domain_sid, name_map={}):
260     sddl = "D:" \
261         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
262         "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
263         "(OA;;CCDC;bf967a86-0de6-11d0-a285-00aa003049e2;;AO)" \
264         "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
265         "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
266         "(OA;;CCDC;bf967aa8-0de6-11d0-a285-00aa003049e2;;PO)" \
267         "(A;;RPLCLORC;;;AU)" \
268         "(OA;;CCDC;4828cc14-1437-45bc-9b07-ad6f015e5f28;;AO)" \
269         "S:"
270     return sddl2binary(sddl, domain_sid, name_map)
271
272
273 def get_domain_users_descriptor(domain_sid, name_map={}):
274     sddl = "D:" \
275         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
276         "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
277         "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
278         "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
279         "(OA;;CCDC;bf967aa8-0de6-11d0-a285-00aa003049e2;;PO)" \
280         "(A;;RPLCLORC;;;AU)" \
281         "(OA;;CCDC;4828cc14-1437-45bc-9b07-ad6f015e5f28;;AO)" \
282         "S:"
283     return sddl2binary(sddl, domain_sid, name_map)
284
285
286 def get_managed_service_accounts_descriptor(domain_sid, name_map={}):
287     sddl = "D:" \
288         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
289         "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
290         "(OA;;CCDC;ce206244-5827-4a86-ba1c-1c0c386c1b64;;AO)" \
291         "(OA;;CCDC;bf967aba-0de6-11d0-a285-00aa003049e2;;AO)" \
292         "(OA;;CCDC;bf967a9c-0de6-11d0-a285-00aa003049e2;;AO)" \
293         "(A;;RPLCLORC;;;AU)" \
294         "S:"
295     return sddl2binary(sddl, domain_sid, name_map)
296
297
298 def get_domain_controllers_descriptor(domain_sid, name_map={}):
299     sddl = "D:" \
300         "(A;;RPLCLORC;;;AU)" \
301         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
302         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
303         "(A;;RPLCLORC;;;ED)" \
304         "S:" \
305         "(AU;SA;CCDCWOWDSDDT;;;WD)" \
306         "(AU;CISA;WP;;;WD)"
307     return sddl2binary(sddl, domain_sid, name_map)
308
309
310 def get_domain_delete_protected1_descriptor(domain_sid, name_map={}):
311     sddl = "D:AI" \
312         "(A;;RPLCLORC;;;AU)" \
313         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
314         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
315     return sddl2binary(sddl, domain_sid, name_map)
316
317
318 def get_domain_delete_protected2_descriptor(domain_sid, name_map={}):
319     sddl = "D:AI" \
320         "(A;;RPLCLORC;;;AU)" \
321         "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
322         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
323     return sddl2binary(sddl, domain_sid, name_map)
324
325
326 def get_dns_partition_descriptor(domain_sid, name_map={}):
327     sddl = "O:SYG:BAD:AI" \
328         "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
329         "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
330         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
331         "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
332         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
333         "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
334         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
335         "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
336         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
337         "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
338         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;RO)" \
339         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
340         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
341         "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
342         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
343         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
344         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
345         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
346         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
347         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
348         "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;S-1-5-32-557)" \
349         "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
350         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
351         "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
352         "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
353         "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
354         "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
355         "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
356         "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
357         "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
358         "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
359         "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
360         "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
361         "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
362         "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
363         "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
364         "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
365         "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
366         "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
367         "(A;;RPRC;;;RU)" \
368         "(A;CI;LC;;;RU)" \
369         "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
370         "(A;;RP;;;WD)" \
371         "(A;;RPLCLORC;;;ED)" \
372         "(A;;RPLCLORC;;;AU)" \
373         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
374         "S:AI" \
375         "(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
376         "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
377         "(AU;SA;CR;;;DU)(AU;SA;CR;;;BA)(AU;SA;WPWOWD;;;WD)"
378     return sddl2binary(sddl, domain_sid, name_map)
379
380
381 def get_dns_forest_microsoft_dns_descriptor(domain_sid, name_map={}):
382     sddl = "O:SYG:SYD:AI" \
383         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
384         "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
385     return sddl2binary(sddl, domain_sid, name_map)
386
387
388 def get_dns_domain_microsoft_dns_descriptor(domain_sid, name_map={}):
389     sddl = "O:SYG:SYD:AI" \
390         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;DA)" \
391         "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;DnsAdmins)" \
392         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
393         "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
394     return sddl2binary(sddl, domain_sid, name_map)
395
396
397 def get_paritions_crossref_subdomain_descriptor(domain_sid, name_map={}):
398     sddl = "O:SubdomainAdminsG:SubdomainAdminsD:AI" \
399         "(A;;RPWPCRCCLCLORCWOWDSW;;;SubdomainAdmins)" \
400         "(A;;RPLCLORC;;;AU)" \
401         "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
402     return sddl2binary(sddl, domain_sid, name_map)
403
404
405 def get_wellknown_sds(samdb):
406
407     # Then subcontainers
408     subcontainers = [
409         (ldb.Dn(samdb, "%s" % str(samdb.domain_dn())), get_domain_descriptor),
410         (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(samdb.domain_dn())), get_domain_delete_protected2_descriptor),
411         (ldb.Dn(samdb, "CN=System,%s" % str(samdb.domain_dn())), get_domain_delete_protected1_descriptor),
412         (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(samdb.domain_dn())), get_domain_infrastructure_descriptor),
413         (ldb.Dn(samdb, "CN=Builtin,%s" % str(samdb.domain_dn())), get_domain_builtin_descriptor),
414         (ldb.Dn(samdb, "CN=Computers,%s" % str(samdb.domain_dn())), get_domain_computers_descriptor),
415         (ldb.Dn(samdb, "CN=Users,%s" % str(samdb.domain_dn())), get_domain_users_descriptor),
416         (ldb.Dn(samdb, "OU=Domain Controllers,%s" % str(samdb.domain_dn())), get_domain_controllers_descriptor),
417         (ldb.Dn(samdb, "CN=MicrosoftDNS,CN=System,%s" % str(samdb.domain_dn())), get_dns_domain_microsoft_dns_descriptor),
418
419         (ldb.Dn(samdb, "%s" % str(samdb.get_config_basedn())), get_config_descriptor),
420         (ldb.Dn(samdb, "CN=NTDS Quotas,%s" % str(samdb.get_config_basedn())), get_config_ntds_quotas_descriptor),
421         (ldb.Dn(samdb, "CN=LostAndFoundConfig,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
422         (ldb.Dn(samdb, "CN=Services,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1_descriptor),
423         (ldb.Dn(samdb, "CN=Physical Locations,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
424         (ldb.Dn(samdb, "CN=WellKnown Security Principals,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
425         (ldb.Dn(samdb, "CN=ForestUpdates,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
426         (ldb.Dn(samdb, "CN=DisplaySpecifiers,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
427         (ldb.Dn(samdb, "CN=Extended-Rights,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
428         (ldb.Dn(samdb, "CN=Partitions,%s" % str(samdb.get_config_basedn())), get_config_partitions_descriptor),
429         (ldb.Dn(samdb, "CN=Sites,%s" % str(samdb.get_config_basedn())), get_config_sites_descriptor),
430
431         (ldb.Dn(samdb, "%s" % str(samdb.get_schema_basedn())), get_schema_descriptor),
432     ]
433
434     current = samdb.search(expression="(objectClass=*)",
435                            base="", scope=ldb.SCOPE_BASE,
436                            attrs=["namingContexts"])
437
438     for nc in current[0]["namingContexts"]:
439
440         dnsforestdn = ldb.Dn(samdb, "DC=ForestDnsZones,%s" % (str(samdb.get_root_basedn())))
441         if ldb.Dn(samdb, nc.decode('utf8')) == dnsforestdn:
442             c = (ldb.Dn(samdb, "%s" % str(dnsforestdn)), get_dns_partition_descriptor)
443             subcontainers.append(c)
444             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsforestdn)),
445                  get_domain_delete_protected1_descriptor)
446             subcontainers.append(c)
447             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsforestdn)),
448                  get_domain_delete_protected2_descriptor)
449             subcontainers.append(c)
450             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsforestdn)),
451                  get_dns_forest_microsoft_dns_descriptor)
452             subcontainers.append(c)
453             continue
454
455         dnsdomaindn = ldb.Dn(samdb, "DC=DomainDnsZones,%s" % (str(samdb.domain_dn())))
456         if ldb.Dn(samdb, nc.decode('utf8')) == dnsdomaindn:
457             c = (ldb.Dn(samdb, "%s" % str(dnsdomaindn)), get_dns_partition_descriptor)
458             subcontainers.append(c)
459             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsdomaindn)),
460                  get_domain_delete_protected1_descriptor)
461             subcontainers.append(c)
462             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsdomaindn)),
463                  get_domain_delete_protected2_descriptor)
464             subcontainers.append(c)
465             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsdomaindn)),
466                  get_dns_domain_microsoft_dns_descriptor)
467             subcontainers.append(c)
468
469     return subcontainers
470
471
472 def chunck_acl(acl):
473     """Return separate ACE of an ACL
474
475     :param acl: A string representing the ACL
476     :return: A hash with different parts
477     """
478
479     p = re.compile(r'(\w+)?(\(.*?\))')
480     tab = p.findall(acl)
481
482     hash = {}
483     hash["aces"] = []
484     for e in tab:
485         if len(e[0]) > 0:
486             hash["flags"] = e[0]
487         hash["aces"].append(e[1])
488
489     return hash
490
491
492 def chunck_sddl(sddl):
493     """ Return separate parts of the SDDL (owner, group, ...)
494
495     :param sddl: An string containing the SDDL to chunk
496     :return: A hash with the different chunk
497     """
498
499     p = re.compile(r'([OGDS]:)(.*?)(?=(?:[GDS]:|$))')
500     tab = p.findall(sddl)
501
502     hash = {}
503     for e in tab:
504         if e[0] == "O:":
505             hash["owner"] = e[1]
506         if e[0] == "G:":
507             hash["group"] = e[1]
508         if e[0] == "D:":
509             hash["dacl"] = e[1]
510         if e[0] == "S:":
511             hash["sacl"] = e[1]
512
513     return hash
514
515
516 def get_clean_sd(sd):
517     """Get the SD without any inherited ACEs
518
519     :param sd: SD to strip
520     :return: An SD with inherited ACEs stripped
521     """
522
523     sd_clean = security.descriptor()
524     sd_clean.owner_sid = sd.owner_sid
525     sd_clean.group_sid = sd.group_sid
526     sd_clean.type = sd.type
527     sd_clean.revision = sd.revision
528
529     aces = []
530     if sd.sacl is not None:
531         aces = sd.sacl.aces
532     for i in range(0, len(aces)):
533         ace = aces[i]
534
535         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
536             sd_clean.sacl_add(ace)
537             continue
538
539     aces = []
540     if sd.dacl is not None:
541         aces = sd.dacl.aces
542     for i in range(0, len(aces)):
543         ace = aces[i]
544
545         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
546             sd_clean.dacl_add(ace)
547             continue
548     return sd_clean
549
550
551 def get_diff_sds(refsd, cursd, domainsid, checkSacl=True):
552     """Get the difference between 2 sd
553
554     This function split the textual representation of ACL into smaller
555     chunck in order to not to report a simple permutation as a difference
556
557     :param refsddl: First sddl to compare
558     :param cursddl: Second sddl to compare
559     :param checkSacl: If false we skip the sacl checks
560     :return: A string that explain difference between sddls
561     """
562
563     cursddl = get_clean_sd(cursd).as_sddl(domainsid)
564     refsddl = get_clean_sd(refsd).as_sddl(domainsid)
565
566     txt = ""
567     hash_cur = chunck_sddl(cursddl)
568     hash_ref = chunck_sddl(refsddl)
569
570     if "owner" not in hash_cur:
571         txt = "\tNo owner in current SD"
572     elif "owner" in hash_ref and hash_cur["owner"] != hash_ref["owner"]:
573         txt = "\tOwner mismatch: %s (in ref) %s" \
574               "(in current)\n" % (hash_ref["owner"], hash_cur["owner"])
575
576     if "group" not in hash_cur:
577         txt = "%s\tNo group in current SD" % txt
578     elif "group" in hash_ref and hash_cur["group"] != hash_ref["group"]:
579         txt = "%s\tGroup mismatch: %s (in ref) %s" \
580               "(in current)\n" % (txt, hash_ref["group"], hash_cur["group"])
581
582     parts = ["dacl"]
583     if checkSacl:
584         parts.append("sacl")
585     for part in parts:
586         if part in hash_cur and part in hash_ref:
587
588             # both are present, check if they contain the same ACE
589             h_cur = set()
590             h_ref = set()
591             c_cur = chunck_acl(hash_cur[part])
592             c_ref = chunck_acl(hash_ref[part])
593
594             for elem in c_cur["aces"]:
595                 h_cur.add(elem)
596
597             for elem in c_ref["aces"]:
598                 h_ref.add(elem)
599
600             for k in set(h_ref):
601                 if k in h_cur:
602                     h_cur.remove(k)
603                     h_ref.remove(k)
604
605             if len(h_cur) + len(h_ref) > 0:
606                 txt = "%s\tPart %s is different between reference" \
607                       " and current here is the detail:\n" % (txt, part)
608
609                 for item in h_cur:
610                     txt = "%s\t\t%s ACE is not present in the" \
611                           " reference\n" % (txt, item)
612
613                 for item in h_ref:
614                     txt = "%s\t\t%s ACE is not present in the" \
615                           " current\n" % (txt, item)
616
617         elif part in hash_cur and part not in hash_ref:
618             txt = "%s\tReference ACL hasn't a %s part\n" % (txt, part)
619         elif part not in hash_cur and part in hash_ref:
620             txt = "%s\tCurrent ACL hasn't a %s part\n" % (txt, part)
621
622     return txt