libreplace: Fix the build on Solaris
[sfrench/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_paritions_crossref_subdomain_descriptor(domain_sid, name_map={}):
365     sddl = "O:SubdomainAdminsG:SubdomainAdminsD:AI" \
366     "(A;;RPWPCRCCLCLORCWOWDSW;;;SubdomainAdmins)"
367     "(A;;RPLCLORC;;;AU)"
368     "(A;;RPWPCRCCDCLCLORCWOWDSDDTSW;;;SY)"
369     return sddl2binary(sddl, domain_sid, name_map)
370
371 def get_wellknown_sds(samdb):
372
373     # Then subcontainers
374     subcontainers = [
375         (ldb.Dn(samdb, "%s" % str(samdb.domain_dn())), get_domain_descriptor),
376         (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(samdb.domain_dn())), get_domain_delete_protected2_descriptor),
377         (ldb.Dn(samdb, "CN=System,%s" % str(samdb.domain_dn())), get_domain_delete_protected1_descriptor),
378         (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(samdb.domain_dn())), get_domain_infrastructure_descriptor),
379         (ldb.Dn(samdb, "CN=Builtin,%s" % str(samdb.domain_dn())), get_domain_builtin_descriptor),
380         (ldb.Dn(samdb, "CN=Computers,%s" % str(samdb.domain_dn())), get_domain_computers_descriptor),
381         (ldb.Dn(samdb, "CN=Users,%s" % str(samdb.domain_dn())), get_domain_users_descriptor),
382         (ldb.Dn(samdb, "OU=Domain Controllers,%s" % str(samdb.domain_dn())), get_domain_controllers_descriptor),
383         (ldb.Dn(samdb, "CN=MicrosoftDNS,CN=System,%s" % str(samdb.domain_dn())), get_dns_domain_microsoft_dns_descriptor),
384
385         (ldb.Dn(samdb, "%s" % str(samdb.get_config_basedn())), get_config_descriptor),
386         (ldb.Dn(samdb, "CN=NTDS Quotas,%s" % str(samdb.get_config_basedn())), get_config_ntds_quotas_descriptor),
387         (ldb.Dn(samdb, "CN=LostAndFoundConfig,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
388         (ldb.Dn(samdb, "CN=Services,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1_descriptor),
389         (ldb.Dn(samdb, "CN=Physical Locations,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
390         (ldb.Dn(samdb, "CN=WellKnown Security Principals,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
391         (ldb.Dn(samdb, "CN=ForestUpdates,%s" % str(samdb.get_config_basedn())), get_config_delete_protected1wd_descriptor),
392         (ldb.Dn(samdb, "CN=DisplaySpecifiers,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
393         (ldb.Dn(samdb, "CN=Extended-Rights,%s" % str(samdb.get_config_basedn())), get_config_delete_protected2_descriptor),
394         (ldb.Dn(samdb, "CN=Partitions,%s" % str(samdb.get_config_basedn())), get_config_partitions_descriptor),
395         (ldb.Dn(samdb, "CN=Sites,%s" % str(samdb.get_config_basedn())), get_config_sites_descriptor),
396
397         (ldb.Dn(samdb, "%s" % str(samdb.get_schema_basedn())), get_schema_descriptor),
398     ]
399
400     current = samdb.search(expression="(objectClass=*)",
401                            base="", scope=ldb.SCOPE_BASE,
402                            attrs=["namingContexts"])
403
404     for nc in current[0]["namingContexts"]:
405
406         dnsforestdn = ldb.Dn(samdb, "DC=ForestDnsZones,%s" % (str(samdb.get_root_basedn())))
407         if ldb.Dn(samdb, nc) == dnsforestdn:
408             c = (ldb.Dn(samdb, "%s" % str(dnsforestdn)), get_dns_partition_descriptor)
409             subcontainers.append(c)
410             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsforestdn)),
411                  get_domain_delete_protected1_descriptor)
412             subcontainers.append(c)
413             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsforestdn)),
414                  get_domain_delete_protected2_descriptor)
415             subcontainers.append(c)
416             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsforestdn)),
417                  get_dns_forest_microsoft_dns_descriptor)
418             subcontainers.append(c)
419             continue
420
421         dnsdomaindn = ldb.Dn(samdb, "DC=DomainDnsZones,%s" % (str(samdb.domain_dn())))
422         if ldb.Dn(samdb, nc) == dnsdomaindn:
423             c = (ldb.Dn(samdb, "%s" % str(dnsdomaindn)), get_dns_partition_descriptor)
424             subcontainers.append(c)
425             c = (ldb.Dn(samdb, "CN=Infrastructure,%s" % str(dnsdomaindn)),
426                  get_domain_delete_protected1_descriptor)
427             subcontainers.append(c)
428             c = (ldb.Dn(samdb, "CN=LostAndFound,%s" % str(dnsdomaindn)),
429                  get_domain_delete_protected2_descriptor)
430             subcontainers.append(c)
431             c = (ldb.Dn(samdb, "CN=MicrosoftDNS,%s" % str(dnsdomaindn)),
432                  get_dns_domain_microsoft_dns_descriptor)
433             subcontainers.append(c)
434
435     return subcontainers
436
437 def chunck_acl(acl):
438     """Return separate ACE of an ACL
439
440     :param acl: A string representing the ACL
441     :return: A hash with different parts
442     """
443
444     p = re.compile(r'(\w+)?(\(.*?\))')
445     tab = p.findall(acl)
446
447     hash = {}
448     hash["aces"] = []
449     for e in tab:
450         if len(e[0]) > 0:
451             hash["flags"] = e[0]
452         hash["aces"].append(e[1])
453
454     return hash
455
456
457 def chunck_sddl(sddl):
458     """ Return separate parts of the SDDL (owner, group, ...)
459
460     :param sddl: An string containing the SDDL to chunk
461     :return: A hash with the different chunk
462     """
463
464     p = re.compile(r'([OGDS]:)(.*?)(?=(?:[GDS]:|$))')
465     tab = p.findall(sddl)
466
467     hash = {}
468     for e in tab:
469         if e[0] == "O:":
470             hash["owner"] = e[1]
471         if e[0] == "G:":
472             hash["group"] = e[1]
473         if e[0] == "D:":
474             hash["dacl"] = e[1]
475         if e[0] == "S:":
476             hash["sacl"] = e[1]
477
478     return hash
479
480
481 def get_clean_sd(sd):
482     """Get the SD without any inherited ACEs
483
484     :param sd: SD to strip
485     :return: An SD with inherited ACEs stripped
486     """
487
488     sd_clean = security.descriptor()
489     sd_clean.owner_sid = sd.owner_sid
490     sd_clean.group_sid = sd.group_sid
491     sd_clean.type = sd.type
492     sd_clean.revision = sd.revision
493
494     aces = []
495     if sd.sacl is not None:
496         aces = sd.sacl.aces
497     for i in range(0, len(aces)):
498         ace = aces[i]
499
500         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
501             sd_clean.sacl_add(ace)
502             continue
503
504     aces = []
505     if sd.dacl is not None:
506         aces = sd.dacl.aces
507     for i in range(0, len(aces)):
508         ace = aces[i]
509
510         if not ace.flags & security.SEC_ACE_FLAG_INHERITED_ACE:
511             sd_clean.dacl_add(ace)
512             continue
513     return sd_clean
514
515
516 def get_diff_sds(refsd, cursd, domainsid, checkSacl = True):
517     """Get the difference between 2 sd
518
519     This function split the textual representation of ACL into smaller
520     chunck in order to not to report a simple permutation as a difference
521
522     :param refsddl: First sddl to compare
523     :param cursddl: Second sddl to compare
524     :param checkSacl: If false we skip the sacl checks
525     :return: A string that explain difference between sddls
526     """
527
528     cursddl = get_clean_sd(cursd).as_sddl(domainsid)
529     refsddl = get_clean_sd(refsd).as_sddl(domainsid)
530
531     txt = ""
532     hash_cur = chunck_sddl(cursddl)
533     hash_ref = chunck_sddl(refsddl)
534
535     if not hash_cur.has_key("owner"):
536         txt = "\tNo owner in current SD"
537     elif hash_ref.has_key("owner") and hash_cur["owner"] != hash_ref["owner"]:
538         txt = "\tOwner mismatch: %s (in ref) %s" \
539               "(in current)\n" % (hash_ref["owner"], hash_cur["owner"])
540
541     if not hash_cur.has_key("group"):
542         txt = "%s\tNo group in current SD" % txt
543     elif hash_ref.has_key("group") and hash_cur["group"] != hash_ref["group"]:
544         txt = "%s\tGroup mismatch: %s (in ref) %s" \
545               "(in current)\n" % (txt, hash_ref["group"], hash_cur["group"])
546
547     parts = [ "dacl" ]
548     if checkSacl:
549         parts.append("sacl")
550     for part in parts:
551         if hash_cur.has_key(part) and hash_ref.has_key(part):
552
553             # both are present, check if they contain the same ACE
554             h_cur = set()
555             h_ref = set()
556             c_cur = chunck_acl(hash_cur[part])
557             c_ref = chunck_acl(hash_ref[part])
558
559             for elem in c_cur["aces"]:
560                 h_cur.add(elem)
561
562             for elem in c_ref["aces"]:
563                 h_ref.add(elem)
564
565             for k in set(h_ref):
566                 if k in h_cur:
567                     h_cur.remove(k)
568                     h_ref.remove(k)
569
570             if len(h_cur) + len(h_ref) > 0:
571                 txt = "%s\tPart %s is different between reference" \
572                       " and current here is the detail:\n" % (txt, part)
573
574                 for item in h_cur:
575                     txt = "%s\t\t%s ACE is not present in the" \
576                           " reference\n" % (txt, item)
577
578                 for item in h_ref:
579                     txt = "%s\t\t%s ACE is not present in the" \
580                           " current\n" % (txt, item)
581
582         elif hash_cur.has_key(part) and not hash_ref.has_key(part):
583             txt = "%s\tReference ACL hasn't a %s part\n" % (txt, part)
584         elif not hash_cur.has_key(part) and hash_ref.has_key(part):
585             txt = "%s\tCurrent ACL hasn't a %s part\n" % (txt, part)
586
587     return txt