PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/freeipa-3.0.0.pre1/ipalib/aci.py

#
Python | 332 lines | 284 code | 11 blank | 37 comment | 9 complexity | bdba47f848eae93e713638e8e533063b MD5 | raw file
Possible License(s): GPL-3.0
  1. # Authors:
  2. # Rob Crittenden <rcritten@redhat.com>
  3. #
  4. # Copyright (C) 2008 Red Hat
  5. # see file 'COPYING' for use and warranty information
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import shlex
  20. import re
  21. import ldap
  22. # The Python re module doesn't do nested parenthesis
  23. # Break the ACI into 3 pieces: target, name, permissions/bind_rules
  24. ACIPat = re.compile(r'\(version\s+3.0\s*;\s*acl\s+\"([^\"]*)\"\s*;\s*([^;]*);\s*\)', re.UNICODE)
  25. # Break the permissions/bind_rules out
  26. PermPat = re.compile(r'(\w+)\s*\((.*)\)\s+(.*)', re.UNICODE)
  27. # Break the bind rule out
  28. BindPat = re.compile(r'([a-zA-Z0-9;\.]+)\s*(\!?=)\s*(.*)', re.UNICODE)
  29. ACTIONS = ["allow", "deny"]
  30. PERMISSIONS = ["read", "write", "add", "delete", "search", "compare",
  31. "selfwrite", "proxy", "all"]
  32. class ACI:
  33. """
  34. Holds the basic data for an ACI entry, as stored in the cn=accounts
  35. entry in LDAP. Has methods to parse an ACI string and export to an
  36. ACI String.
  37. """
  38. def __init__(self,acistr=None):
  39. self.name = None
  40. self.source_group = None
  41. self.dest_group = None
  42. self.orig_acistr = acistr
  43. self.target = {}
  44. self.action = "allow"
  45. self.permissions = ["write"]
  46. self.bindrule = {}
  47. if acistr is not None:
  48. self._parse_acistr(acistr)
  49. def __getitem__(self,key):
  50. """Fake getting attributes by key for sorting"""
  51. if key == 0:
  52. return self.name
  53. if key == 1:
  54. return self.source_group
  55. if key == 2:
  56. return self.dest_group
  57. raise TypeError("Unknown key value %s" % key)
  58. def __repr__(self):
  59. """An alias for export_to_string()"""
  60. return self.export_to_string()
  61. def export_to_string(self):
  62. """Output a Directory Server-compatible ACI string"""
  63. self.validate()
  64. aci = ""
  65. for t in self.target:
  66. op = self.target[t]['operator']
  67. if type(self.target[t]['expression']) in (tuple, list):
  68. target = ""
  69. for l in self.target[t]['expression']:
  70. target = target + l + " || "
  71. target = target[:-4]
  72. aci = aci + "(%s %s \"%s\")" % (t, op, target)
  73. else:
  74. aci = aci + "(%s %s \"%s\")" % (t, op, self.target[t]['expression'])
  75. aci = aci + "(version 3.0;acl \"%s\";%s (%s) %s %s \"%s\"" % (self.name, self.action, ",".join(self.permissions), self.bindrule['keyword'], self.bindrule['operator'], self.bindrule['expression']) + ";)"
  76. return aci
  77. def _remove_quotes(self, s):
  78. # Remove leading and trailing quotes
  79. if s.startswith('"'):
  80. s = s[1:]
  81. if s.endswith('"'):
  82. s = s[:-1]
  83. return s
  84. def _parse_target(self, aci):
  85. lexer = shlex.shlex(aci.encode('utf-8'))
  86. lexer.wordchars = lexer.wordchars + "."
  87. l = []
  88. var = False
  89. op = "="
  90. for token in lexer:
  91. # We should have the form (a = b)(a = b)...
  92. if token == "(":
  93. var = lexer.next().strip()
  94. operator = lexer.next()
  95. if operator != "=" and operator != "!=":
  96. # Peek at the next char before giving up
  97. operator = operator + lexer.next()
  98. if operator != "=" and operator != "!=":
  99. raise SyntaxError("No operator in target, got '%s'" % operator)
  100. op = operator
  101. val = lexer.next().strip()
  102. val = self._remove_quotes(val)
  103. end = lexer.next()
  104. if end != ")":
  105. raise SyntaxError('No end parenthesis in target, got %s' % end)
  106. if var == 'targetattr':
  107. # Make a string of the form attr || attr || ... into a list
  108. t = re.split('[^a-zA-Z0-9;\*]+', val)
  109. self.target[var] = {}
  110. self.target[var]['operator'] = op
  111. self.target[var]['expression'] = t
  112. else:
  113. self.target[var] = {}
  114. self.target[var]['operator'] = op
  115. self.target[var]['expression'] = val
  116. def _parse_acistr(self, acistr):
  117. vstart = acistr.find('version 3.0')
  118. if vstart < 0:
  119. raise SyntaxError, "malformed ACI, unable to find version %s" % acistr
  120. acimatch = ACIPat.match(acistr[vstart-1:])
  121. if not acimatch or len(acimatch.groups()) < 2:
  122. raise SyntaxError, "malformed ACI, match for version and bind rule failed %s" % acistr
  123. self._parse_target(acistr[:vstart-1])
  124. self.name = acimatch.group(1)
  125. bindperms = PermPat.match(acimatch.group(2))
  126. if not bindperms or len(bindperms.groups()) < 3:
  127. raise SyntaxError, "malformed ACI, permissions match failed %s" % acistr
  128. self.action = bindperms.group(1)
  129. self.permissions = bindperms.group(2).replace(' ','').split(',')
  130. self.set_bindrule(bindperms.group(3))
  131. def validate(self):
  132. """Do some basic verification that this will produce a
  133. valid LDAP ACI.
  134. returns True if valid
  135. """
  136. if not type(self.permissions) in (tuple, list):
  137. raise SyntaxError, "permissions must be a list"
  138. for p in self.permissions:
  139. if not p.lower() in PERMISSIONS:
  140. raise SyntaxError, "invalid permission: '%s'" % p
  141. if not self.name:
  142. raise SyntaxError, "name must be set"
  143. if not isinstance(self.name, basestring):
  144. raise SyntaxError, "name must be a string"
  145. if not isinstance(self.target, dict) or len(self.target) == 0:
  146. raise SyntaxError, "target must be a non-empty dictionary"
  147. if not isinstance(self.bindrule, dict):
  148. raise SyntaxError, "bindrule must be a dictionary"
  149. if not self.bindrule.get('operator') or not self.bindrule.get('keyword') or not self.bindrule.get('expression'):
  150. raise SyntaxError, "bindrule is missing a component"
  151. return True
  152. def set_target_filter(self, filter, operator="="):
  153. self.target['targetfilter'] = {}
  154. if not filter.startswith("("):
  155. filter = "(" + filter + ")"
  156. self.target['targetfilter']['expression'] = filter
  157. self.target['targetfilter']['operator'] = operator
  158. def set_target_attr(self, attr, operator="="):
  159. if not attr:
  160. if 'targetattr' in self.target:
  161. del self.target['targetattr']
  162. return
  163. if not type(attr) in (tuple, list):
  164. attr = [attr]
  165. self.target['targetattr'] = {}
  166. self.target['targetattr']['expression'] = attr
  167. self.target['targetattr']['operator'] = operator
  168. def set_target(self, target, operator="="):
  169. assert target.startswith("ldap:///")
  170. self.target['target'] = {}
  171. self.target['target']['expression'] = target
  172. self.target['target']['operator'] = operator
  173. def set_bindrule(self, bindrule):
  174. match = BindPat.match(bindrule)
  175. if not match or len(match.groups()) < 3:
  176. raise SyntaxError, "malformed bind rule"
  177. self.set_bindrule_keyword(match.group(1))
  178. self.set_bindrule_operator(match.group(2))
  179. self.set_bindrule_expression(match.group(3).replace('"',''))
  180. def set_bindrule_keyword(self, keyword):
  181. self.bindrule['keyword'] = keyword
  182. def set_bindrule_operator(self, operator):
  183. self.bindrule['operator'] = operator
  184. def set_bindrule_expression(self, expression):
  185. self.bindrule['expression'] = expression
  186. def isequal(self, b):
  187. """
  188. Compare the current ACI to another one to see if they are
  189. the same.
  190. returns True if equal, False if not.
  191. """
  192. assert isinstance(b, ACI)
  193. try:
  194. if self.name.lower() != b.name.lower():
  195. return False
  196. if set(self.permissions) != set(b.permissions):
  197. return False
  198. if self.bindrule.get('keyword') != b.bindrule.get('keyword'):
  199. return False
  200. if self.bindrule.get('operator') != b.bindrule.get('operator'):
  201. return False
  202. if self.bindrule.get('expression') != b.bindrule.get('expression'):
  203. return False
  204. if self.target.get('targetfilter',{}).get('expression') != b.target.get('targetfilter',{}).get('expression'):
  205. return False
  206. if self.target.get('targetfilter',{}).get('operator') != b.target.get('targetfilter',{}).get('operator'):
  207. return False
  208. if set(self.target.get('targetattr', {}).get('expression', ())) != set(b.target.get('targetattr',{}).get('expression', ())):
  209. return False
  210. if self.target.get('targetattr',{}).get('operator') != b.target.get('targetattr',{}).get('operator'):
  211. return False
  212. if self.target.get('target',{}).get('expression') != b.target.get('target',{}).get('expression'):
  213. return False
  214. if self.target.get('target',{}).get('operator') != b.target.get('target',{}).get('operator'):
  215. return False
  216. except Exception:
  217. # If anything throws up then they are not equal
  218. return False
  219. # We got this far so lets declare them the same
  220. return True
  221. def extract_group_cns(aci_list, client):
  222. """
  223. Extracts all the cn's from a list of aci's and returns them as a hash
  224. from group_dn to group_cn.
  225. It first tries to cheat by looking at the first rdn for the
  226. group dn. If that's not cn for some reason, it looks up the group.
  227. """
  228. group_dn_to_cn = {}
  229. for aci in aci_list:
  230. for dn in (aci.source_group, aci.dest_group):
  231. if not group_dn_to_cn.has_key(dn):
  232. rdn_list = ldap.explode_dn(dn, 0)
  233. first_rdn = rdn_list[0]
  234. (type,value) = first_rdn.split('=')
  235. if type == "cn":
  236. group_dn_to_cn[dn] = value
  237. else:
  238. try:
  239. group = client.get_entry_by_dn(dn, ['cn'])
  240. group_dn_to_cn[dn] = group.getValue('cn')
  241. except Exception:
  242. group_dn_to_cn[dn] = 'unknown'
  243. return group_dn_to_cn
  244. if __name__ == '__main__':
  245. # a = ACI('(targetattr="title")(targetfilter="(memberOf=cn=bar,cn=groups,cn=accounts ,dc=example,dc=com)")(version 3.0;acl "foobar";allow (write) groupdn="ldap:///cn=foo,cn=groups,cn=accounts,dc=example,dc=com";)')
  246. # print a
  247. # a = ACI('(target="ldap:///uid=bjensen,dc=example,dc=com")(targetattr=*) (version 3.0;acl "aci1";allow (write) userdn="ldap:///self";)')
  248. # print a
  249. # a = ACI(' (targetattr = "givenName || sn || cn || displayName || title || initials || loginShell || gecos || homePhone || mobile || pager || facsimileTelephoneNumber || telephoneNumber || street || roomNumber || l || st || postalCode || manager || secretary || description || carLicense || labeledURI || inetUserHTTPURL || seeAlso || employeeType || businessCategory || ou")(version 3.0;acl "Self service";allow (write) userdn = "ldap:///self";)')
  250. # print a
  251. a = ACI('(target="ldap:///uid=*,cn=users,cn=accounts,dc=example,dc=com")(version 3.0;acl "add_user";allow (add) groupdn="ldap:///cn=add_user,cn=taskgroups,dc=example,dc=com";)')
  252. print a
  253. print "---"
  254. a = ACI('(targetattr=member)(target="ldap:///cn=ipausers,cn=groups,cn=accounts,dc=example,dc=com")(version 3.0;acl "add_user_to_default_group";allow (write) groupdn="ldap:///cn=add_user_to_default_group,cn=taskgroups,dc=example,dc=com";)')
  255. print a
  256. print "---"
  257. a = ACI('(targetattr!=member)(target="ldap:///cn=ipausers,cn=groups,cn=accounts,dc=example,dc=com")(version 3.0;acl "add_user_to_default_group";allow (write) groupdn="ldap:///cn=add_user_to_default_group,cn=taskgroups,dc=example,dc=com";)')
  258. print a
  259. print "---"
  260. a = ACI('(targetattr = "userPassword || krbPrincipalKey || sambaLMPassword || sambaNTPassword || passwordHistory")(version 3.0; acl "change_password"; allow (write) groupdn = "ldap:///cn=change_password,cn=taskgroups,dc=example,dc=com";)')
  261. print a
  262. print "---"
  263. a = ACI()
  264. a.name ="foo"
  265. a.set_target_attr(['title','givenname'], "!=")
  266. # a.set_bindrule("groupdn = \"ldap:///cn=foo,cn=groups,cn=accounts,dc=example,dc=com\"")
  267. a.set_bindrule_keyword("groupdn")
  268. a.set_bindrule_operator("=")
  269. a.set_bindrule_expression ("\"ldap:///cn=foo,cn=groups,cn=accounts,dc=example,dc=com\"")
  270. a.permissions = ['read','write','add']
  271. print a
  272. b = ACI()
  273. b.name ="foo"
  274. b.set_target_attr(['givenname','title'], "!=")
  275. b.set_bindrule_keyword("groupdn")
  276. b.set_bindrule_operator("=")
  277. b.set_bindrule_expression ("\"ldap:///cn=foo,cn=groups,cn=accounts,dc=example,dc=com\"")
  278. b.permissions = ['add','read','write']
  279. print b
  280. print a.isequal(b)
  281. a = ACI('(targetattr != "userPassword || krbPrincipalKey || sambaLMPassword || sambaNTPassword || passwordHistory || krbMKey")(version 3.0; acl "Enable Anonymous access"; allow (read, search, compare) userdn = "ldap:///anyone";)')
  282. print a
  283. a = ACI('(targetfilter = "(|(objectClass=person)(objectClass=krbPrincipalAux)(objectClass=posixAccount)(objectClass=groupOfNames)(objectClass=posixGroup))")(targetattr != "aci || userPassword || krbPrincipalKey || sambaLMPassword || sambaNTPassword || passwordHistory")(version 3.0; acl "Account Admins can manage Users and Groups"; allow (add, delete, read, write) groupdn = "ldap:///cn=admins,cn=groups,cn=accounts,dc=greyoak,dc=com";)')
  284. print a