dns: Add support for MX queries
[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_domain_controllers_descriptor(domain_sid, name_map={}):
272     sddl = "D:" \
273     "(A;;RPLCLORC;;;AU)" \
274     "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
275     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
276     "(A;;RPLCLORC;;;ED)" \
277     "S:" \
278     "(AU;SA;CCDCWOWDSDDT;;;WD)" \
279     "(AU;CISA;WP;;;WD)"
280     return sddl2binary(sddl, domain_sid, name_map)
281
282 def get_domain_delete_protected1_descriptor(domain_sid, name_map={}):
283     sddl = "D:AI" \
284     "(A;;RPLCLORC;;;AU)" \
285     "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
286     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
287     return sddl2binary(sddl, domain_sid, name_map)
288
289 def get_domain_delete_protected2_descriptor(domain_sid, name_map={}):
290     sddl = "D:AI" \
291     "(A;;RPLCLORC;;;AU)" \
292     "(A;;RPWPCRCCDCLCLORCWOWDSW;;;DA)" \
293     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
294     return sddl2binary(sddl, domain_sid, name_map)
295
296 def get_dns_partition_descriptor(domain_sid, name_map={}):
297     sddl = "O:SYG:BAD:AI" \
298     "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
299     "(OA;CIIO;RP;4c164200-20c0-11d0-a768-00aa006e0529;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
300     "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
301     "(OA;CIIO;RP;5f202010-79a5-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
302     "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
303     "(OA;CIIO;RP;bc0ac240-79a9-11d0-9020-00c04fc2d4cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
304     "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
305     "(OA;CIIO;RP;59ba2f42-79a2-11d0-9020-00c04fc2d3cf;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
306     "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
307     "(OA;CIIO;RP;037088f8-0ae1-11d2-b422-00a0c968f939;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
308     "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ER)" \
309     "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a86-0de6-11d0-a285-00aa003049e2;ED)" \
310     "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967a9c-0de6-11d0-a285-00aa003049e2;ED)" \
311     "(OA;CIIO;RP;b7c69e6d-2cc7-11d2-854e-00a0c983f608;bf967aba-0de6-11d0-a285-00aa003049e2;ED)" \
312     "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;BA)" \
313     "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
314     "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
315     "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
316     "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
317     "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;BA)" \
318     "(OA;;CR;e2a36dc9-ae17-47c3-b58b-be34c55ba633;;IF)" \
319     "(OA;;RP;c7407360-20bf-11d0-a768-00aa006e0529;;RU)" \
320     "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;RU)" \
321     "(OA;CIIO;RPLCLORC;;4828cc14-1437-45bc-9b07-ad6f015e5f28;RU)" \
322     "(OA;CIIO;RPLCLORC;;bf967a9c-0de6-11d0-a285-00aa003049e2;RU)" \
323     "(OA;CIIO;RPLCLORC;;bf967aba-0de6-11d0-a285-00aa003049e2;RU)" \
324     "(OA;;CR;05c74c5e-4deb-43b4-bd9f-86664c2a7fd5;;AU)" \
325     "(OA;;CR;89e95b76-444d-4c62-991a-0facbeda640c;;ED)" \
326     "(OA;;CR;ccc2dc7d-a6ad-4a7a-8846-c04e3cc53501;;AU)" \
327     "(OA;;CR;280f369c-67c7-438e-ae98-1d46f3c6f541;;AU)" \
328     "(OA;;CR;1131f6aa-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
329     "(OA;;CR;1131f6ab-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
330     "(OA;;CR;1131f6ac-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
331     "(OA;;CR;1131f6ad-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
332     "(OA;;CR;1131f6ae-9c07-11d1-f79f-00c04fc2dcd2;;ED)" \
333     "(OA;;RP;b8119fd0-04f6-4762-ab7a-4986c76b3f9a;;AU)" \
334     "(OA;CIIO;RPWPCR;91e647de-d96f-4b70-9557-d63ff4f3ccd8;;PS)" \
335     "(A;;RPWPCRCCLCLORCWOWDSW;;;DA)" \
336     "(A;CI;RPWPCRCCDCLCLORCWOWDSDDTSW;;;EA)" \
337     "(A;;RPRC;;;RU)" \
338     "(A;CI;LC;;;RU)" \
339     "(A;CI;RPWPCRCCLCLORCWOWDSDSW;;;BA)" \
340     "(A;;RP;;;WD)" \
341     "(A;;RPLCLORC;;;ED)" \
342     "(A;;RPLCLORC;;;AU)" \
343     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
344     "S:AI" \
345     "(OU;CISA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
346     "(OU;CISA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)" \
347     "(AU;SA;CR;;;DU)(AU;SA;CR;;;BA)(AU;SA;WPWOWD;;;WD)"
348     return sddl2binary(sddl, domain_sid, name_map)
349
350 def get_dns_forest_microsoft_dns_descriptor(domain_sid, name_map={}):
351     sddl = "O:SYG:SYD:AI" \
352     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
353     "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
354     return sddl2binary(sddl, domain_sid, name_map)
355
356 def get_dns_domain_microsoft_dns_descriptor(domain_sid, name_map={}):
357     sddl = "O:SYG:SYD:AI" \
358     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;DA)" \
359     "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;DnsAdmins)" \
360     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)" \
361     "(A;CI;RPWPCRCCDCLCRCWOWDSDDTSW;;;ED)"
362     return sddl2binary(sddl, domain_sid, name_map)
363
364 def get_wellknown_sds(samdb):
365
366     # Then subcontainers
367     subcontainers = [
368         (ldb.Dn(samdb, "%s" % str(samdb.domain_dn())), get_domain_descriptor),
369         (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(samdb.domain_dn())), get_domain_delete_protected2_descriptor),
370         (ldb.Dn(samdb, "CN=System,%s" % str(samdb.domain_dn())), get_domain_delete_protected1_descriptor),
371         (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(samdb.domain_dn())), get_domain_infrastructure_descriptor),
372         (ldb.Dn(samdb, "CN=Builtin,%s" % str(samdb.domain_dn())), get_domain_builtin_descriptor),
373         (ldb.Dn(samdb, "CN=Computers,%s" % str(samdb.domain_dn())), get_domain_computers_descriptor),
374         (ldb.Dn(samdb, "CN=Users,%s" % str(samdb.domain_dn())), get_domain_users_descriptor),
375         (ldb.Dn(samdb, "OU=Domain Controllers,%s" % str(samdb.domain_dn())), get_domain_controllers_descriptor),
376         (ldb.Dn(samdb, "CN=MicrosoftDNS,CN=System,%s" % str(samdb.domain_dn())), get_dns_domain_microsoft_dns_descriptor),
377
378         (ldb.Dn(samdb, "%s" % str(samdb.get_config_basedn())), get_config_descriptor),
379         (ldb.Dn(samdb, "CN=NTDS Quotas,%s" % str(samdb.get_config_basedn())), get_config_ntds_quotas_descriptor),
380         (ldb.Dn(samdb, "CN=LostAndFoundConfig,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
381         (ldb.Dn(samdb, "CN=Services,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1_descriptor),
382         (ldb.Dn(samdb, "CN=Physical Locations,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
383         (ldb.Dn(samdb, "CN=WellKnown Security Principals,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
384         (ldb.Dn(samdb, "CN=ForestUpdates,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
385         (ldb.Dn(samdb, "CN=DisplaySpecifiers,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
386         (ldb.Dn(samdb, "CN=Extended-Rights,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
387         (ldb.Dn(samdb, "CN=Partitions,%s" % str(samdb.get_config_basedn())), get_config_partitions_descriptor),
388         (ldb.Dn(samdb, "CN=Sites,%s" % str(samdb.get_config_basedn())), get_config_sites_descriptor),
389
390         (ldb.Dn(samdb, "%s" % str(samdb.get_schema_basedn())), get_schema_descriptor),
391     ]
392
393     current = samdb.search(expression="(objectClass=*)",
394                            base="", scope=ldb.SCOPE_BASE,
395                            attrs=["namingContexts"])
396
397     for nc in current[0]["namingContexts"]:
398
399         dnsforestdn = ldb.Dn(samdb, "DC=ForestDnsZones,%s" % (str(samdb.get_root_basedn())))
400         if ldb.Dn(samdb, nc) == dnsforestdn:
401             c = (ldb.Dn(samdb, "%s" % str(dnsforestdn)), get_dns_partition_descriptor)
402             subcontainers.append(c)
403             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsforestdn)),
404                  get_domain_delete_protected1_descriptor)
405             subcontainers.append(c)
406             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsforestdn)),
407                  get_domain_delete_protected2_descriptor)
408             subcontainers.append(c)
409             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsforestdn)),
410                  get_dns_forest_microsoft_dns_descriptor)
411             subcontainers.append(c)
412             continue
413
414         dnsdomaindn = ldb.Dn(samdb, "DC=DomainDnsZones,%s" % (str(samdb.domain_dn())))
415         if ldb.Dn(samdb, nc) == dnsdomaindn:
416             c = (ldb.Dn(samdb, "%s" % str(dnsdomaindn)), get_dns_partition_descriptor)
417             subcontainers.append(c)
418             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsdomaindn)),
419                  get_domain_delete_protected1_descriptor)
420             subcontainers.append(c)
421             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsdomaindn)),
422                  get_domain_delete_protected2_descriptor)
423             subcontainers.append(c)
424             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsdomaindn)),
425                  get_dns_domain_microsoft_dns_descriptor)
426             subcontainers.append(c)
427
428     return subcontainers
429
430
431 def chunck_acl(acl):
432     """Return separate ACE of an ACL
433
434     :param acl: A string representing the ACL
435     :return: A hash with different parts
436     """
437
438     p = re.compile(r'(\w+)?(\(.*?\))')
439     tab = p.findall(acl)
440
441     hash = {}
442     hash["aces"] = []
443     for e in tab:
444         if len(e[0]) > 0:
445             hash["flags"] = e[0]
446         hash["aces"].append(e[1])
447
448     return hash
449
450
451 def chunck_sddl(sddl):
452     """ Return separate parts of the SDDL (owner, group, ...)
453
454     :param sddl: An string containing the SDDL to chunk
455     :return: A hash with the different chunk
456     """
457
458     p = re.compile(r'([OGDS]:)(.*?)(?=(?:[GDS]:|$))')
459     tab = p.findall(sddl)
460
461     hash = {}
462     for e in tab:
463         if e[0] == "O:":
464             hash["owner"] = e[1]
465         if e[0] == "G:":
466             hash["group"] = e[1]
467         if e[0] == "D:":
468             hash["dacl"] = e[1]
469         if e[0] == "S:":
470             hash["sacl"] = e[1]
471
472     return hash
473
474
475 def get_clean_sd(sd):
476     """Get the SD without any inherited ACEs
477
478     :param sd: SD to strip
479     :return: An SD with inherited ACEs stripped
480     """
481
482     sd_clean = security.descriptor()
483     sd_clean.owner_sid = sd.owner_sid
484     sd_clean.group_sid = sd.group_sid
485     sd_clean.type = sd.type
486     sd_clean.revision = sd.revision
487
488     aces = []
489     if sd.sacl is not None:
490         aces = sd.sacl.aces
491     for i in range(0, len(aces)):
492         ace = aces[i]
493
494         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
495             sd_clean.sacl_add(ace)
496             continue
497
498     aces = []
499     if sd.dacl is not None:
500         aces = sd.dacl.aces
501     for i in range(0, len(aces)):
502         ace = aces[i]
503
504         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
505             sd_clean.dacl_add(ace)
506             continue
507     return sd_clean
508
509
510 def get_diff_sds(refsd, cursd, domainsid, checkSacl = True):
511     """Get the difference between 2 sd
512
513     This function split the textual representation of ACL into smaller
514     chunck in order to not to report a simple permutation as a difference
515
516     :param refsddl: First sddl to compare
517     :param cursddl: Second sddl to compare
518     :param checkSacl: If false we skip the sacl checks
519     :return: A string that explain difference between sddls
520     """
521
522     cursddl = get_clean_sd(cursd).as_sddl(domainsid)
523     refsddl = get_clean_sd(refsd).as_sddl(domainsid)
524
525     txt = ""
526     hash_cur = chunck_sddl(cursddl)
527     hash_ref = chunck_sddl(refsddl)
528
529     if not hash_cur.has_key("owner"):
530         txt = "\tNo owner in current SD"
531     elif hash_ref.has_key("owner") and hash_cur["owner"] != hash_ref["owner"]:
532         txt = "\tOwner mismatch: %s (in ref) %s" \
533               "(in current)\n" % (hash_ref["owner"], hash_cur["owner"])
534
535     if not hash_cur.has_key("group"):
536         txt = "%s\tNo group in current SD" % txt
537     elif hash_ref.has_key("group") and hash_cur["group"] != hash_ref["group"]:
538         txt = "%s\tGroup mismatch: %s (in ref) %s" \
539               "(in current)\n" % (txt, hash_ref["group"], hash_cur["group"])
540
541     parts = [ "dacl" ]
542     if checkSacl:
543         parts.append("sacl")
544     for part in parts:
545         if hash_cur.has_key(part) and hash_ref.has_key(part):
546
547             # both are present, check if they contain the same ACE
548             h_cur = set()
549             h_ref = set()
550             c_cur = chunck_acl(hash_cur[part])
551             c_ref = chunck_acl(hash_ref[part])
552
553             for elem in c_cur["aces"]:
554                 h_cur.add(elem)
555
556             for elem in c_ref["aces"]:
557                 h_ref.add(elem)
558
559             for k in set(h_ref):
560                 if k in h_cur:
561                     h_cur.remove(k)
562                     h_ref.remove(k)
563
564             if len(h_cur) + len(h_ref) > 0:
565                 txt = "%s\tPart %s is different between reference" \
566                       " and current here is the detail:\n" % (txt, part)
567
568                 for item in h_cur:
569                     txt = "%s\t\t%s ACE is not present in the" \
570                           " reference\n" % (txt, item)
571
572                 for item in h_ref:
573                     txt = "%s\t\t%s ACE is not present in the" \
574                           " current\n" % (txt, item)
575
576         elif hash_cur.has_key(part) and not hash_ref.has_key(part):
577             txt = "%s\tReference ACL hasn't a %s part\n" % (txt, part)
578         elif not hash_cur.has_key(part) and hash_ref.has_key(part):
579             txt = "%s\tCurrent ACL hasn't a %s part\n" % (txt, part)
580
581     return txt