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