1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# pylint: disable-msg=W0622
#
# Copyright 2008 German Aerospace Center (DLR)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
ACL object handling according to WebDAV ACP specification.
"""
from webdav.acp.Ace import ACE
from webdav import Constants
from webdav.Connection import WebdavError
from webdav.davlib import XML_DOC_HEADER
__version__ = "$LastChangedRevision$"
class ACL(object):
"""
This class provides access to Access Control List funcionality
as specified in the WebDAV ACP.
@ivar aces: ACEs in ACL
@type aces: C{list} of L{ACE} objects
@ivar withInherited: Flag indicating whether ACL contains inherited ACEs.
@type withInherited: C{bool}
"""
# restrict instance variables
__slots__ = ('aces', 'withInherited')
def __init__(self, domroot=None, aces=None):
"""
Constructor should be called with either no parameters (create blank ACE),
or one parameter (a DOM tree or ACE list).
@param domroot: A DOM tree (default: None).
@type domroot: L{webdav.WebdavResponse.Element} object
@param aces: ACE objects (default: None)
@type aces: C{list} of L{ACE} objects
@raise WebdavError: When non-valid parameters are passed a L{WebdavError} is raised.
"""
self.withInherited = None
self.aces = []
if domroot:
for child in domroot.children:
if child.name == Constants.TAG_ACE and child.ns == Constants.NS_DAV:
self.addAce(ACE(child))
else:
# This shouldn't happen, someone screwed up with the params ...
raise WebdavError('Non-ACE tag handed to ACL constructor: ' + child.ns + child.name)
elif isinstance(aces, list) or isinstance(aces, tuple):
self.addAces(aces)
elif domroot == None and aces == None:
# no param ==> blank object
pass
else:
# This shouldn't happen, someone screwed up with the params ...
raise WebdavError('non-valid parameters handed to ACL constructor')
def __cmp__(self, other):
if not isinstance(other, ACL):
return 1
if self.withInherited == other.withInherited:
equal = 1
for ace in self.aces:
inList = 0
for otherAce in other.aces:
if ace == otherAce:
inList = 1
if inList == 0:
equal = 0
return not equal
else:
return 1
def __repr__(self):
repr = '<class ACL: '
if self.withInherited:
repr += 'with inherited, '
first = 1
repr += 'aces: ['
for ace in self.aces:
if first:
repr += '%s' % ace
first = 0
else:
repr += ', %s' % ace
return '%s]>' % (repr)
def copy(self, other):
'''Copy an ACL object.
@param other: Another ACL to copy.
@type other: L{ACL} object
@raise WebdavError: When an object that is not an L{ACL} is passed
a L{WebdavError} is raised.
'''
if not isinstance(other, ACL):
raise WebdavError('Non-ACL object passed to copy method: %s' % other.__class__)
self.withInherited = other.withInherited
if other.aces:
self.addAces(other.aces)
def toXML(self):
"""
Returns ACL content as a string of valid XML as described in WebDAV ACP.
"""
aclTag = 'D:' + Constants.TAG_ACL
return XML_DOC_HEADER +\
'<' + aclTag + ' xmlns:D="DAV:">' + reduce(lambda xml, ace: xml + ace.toXML() + '\n', [''] + self.aces) +\
'</' + aclTag + '>'
def addAce(self, ace):
'''
Adds the passed ACE object to list if it's not in it, yet.
@param ace: An ACE.
@type ace: L{ACE} object
'''
newAce = ACE()
newAce.copy(ace)
# only add it if it's not in the list, yet ...
inList = 0
for element in self.aces:
if element == ace:
inList = 1
if not inList:
self.aces.append(newAce)
def addAces(self, aces):
'''Adds the list of passed ACE objects to list.
@param aces: ACEs
@type aces: sequence of L{ACE} objects
'''
for ace in aces:
self.addAce(ace)
def delAce(self, ace):
'''Deletes the passed ACE object from list.
@param ace: An ACE.
@type ace: L{ACE} object
@raise WebdavError: When the ACE to be deleted is not within the ACL
a L{WebdavError} is raised.
'''
# find where it is and delete it ...
count = 0
index = 0
for element in self.aces:
count += 1
if element == ace:
index = count
if index:
self.aces.pop(index - 1)
else:
raise WebdavError('ACE to be deleted not in list: %s.' % ace)
def delAces(self, aces):
'''Deletes the list of passed ACE objects from list.
@param aces: ACEs
@type aces: sequence of L{ACE} objects
'''
for ace in aces:
self.delAce(ace)
def delPrincipalsAces(self, principal):
"""
Deletes all ACEs in ACL by given principal.
@param principal: A principal.
@type principal: L{Principal} object
"""
# find where it is and delete it ...
index = 0
while index < len(self.aces):
if self.aces[index].principal.principalURL == principal.principalURL:
self.aces.pop(index)
else:
index += 1
def joinGrantDeny(self):
"""
Returns a "refined" ACL of the ACL for ease of use in the UI.
The purpose is to post the user an ACE that can contain both, granted
and denied, privileges. So possible pairs of grant and deny ACEs are joined
to return them in one ACE. This resulting ACE then of course IS NOT valid
for setting ACLs anymore. They will have to be reconverted to yield valid
ACLs for the ACL method.
@return: A (non-valid) ACL that contains both grant and deny clauses in an ACE.
@rtype: L{ACL} object
"""
joinedAces = {}
for ace in self.aces:
if not ace.principal.principalURL is None:
principalKey = ace.principal.principalURL
elif not ace.principal.property is None:
principalKey = ace.principal.property
else:
principalKey = None
if ace.inherited:
principalKey = ace.inherited + ":" + principalKey
if principalKey in joinedAces:
joinedAces[principalKey].addGrantDenies(ace.grantDenies)
else:
joinedAces[principalKey] = ACE()
joinedAces[principalKey].copy(ace)
newAcl = ACL()
newAcl.addAces(joinedAces.values())
return newAcl
def splitGrantDeny(self):
"""
Returns a "refined" ACL of the ACL for ease of use in the UI.
The purpose is to post the user an ACE that can contain both, granted
and denied, privileges. So possible joined grant and deny clauses in ACEs
splitted to return them in separate ACEs. This resulting ACE then is valid
for setting ACLs again. This method is to be seen in conjunction with the
method joinGrantDeny as it reverts its effect.
@return: A valid ACL that contains only ACEs with either grant or deny clauses.
@rtype: L{ACL} object
"""
acesGrant = {}
acesDeny = {}
for ace in self.aces:
for grantDeny in ace.grantDenies:
if grantDeny.isGrant():
if ace.principal.principalURL in acesGrant:
ace.addGrantDeny(grantDeny)
else:
acesGrant[ace.principal.principalURL] = ACE()
acesGrant[ace.principal.principalURL].copy(ace)
acesGrant[ace.principal.principalURL].grantDenies = []
acesGrant[ace.principal.principalURL].addGrantDeny(grantDeny)
else:
if ace.principal.principalURL in acesDeny:
ace.addGrantDeny(grantDeny)
else:
acesDeny[ace.principal.principalURL] = ACE()
acesDeny[ace.principal.principalURL].copy(ace)
acesDeny[ace.principal.principalURL].grantDenies = []
acesDeny[ace.principal.principalURL].addGrantDeny(grantDeny)
newAcl = ACL()
newAcl.addAces(acesGrant.values())
newAcl.addAces(acesDeny.values())
return newAcl
def isValid(self):
"""
Returns true (1) if all contained ACE objects are valid,
otherwise false (0) is returned.
@return: Validity of ACL.
@rtype: C{bool}
"""
valid = 1
if len(self.aces):
for ace in self.aces:
if not ace.isValid():
valid = 0
return valid
def stripAces(self, inherited=True, protected=True):
"""
Returns an ACL object with all ACEs stripped that are inherited
and/or protected.
@param inherited: Flag to indicate whether inherited ACEs should
be stripped (default: True).
@type inherited: C{bool}
@param protected: Flag to indicate whether protected ACEs should
be stripped (default: True).
@type protected: C{bool}
@return: An ACL without the stripped ACEs.
@rtype: L{ACL} object
"""
newAcl = ACL()
if len(self.aces):
for ace in self.aces:
keep = 1
if inherited and ace.inherited:
keep = 0
elif protected and ace.protected:
keep = 0
if keep:
newAcl.addAce(ace)
return newAcl
|