2010-12-01 10:23:52 -06:00
|
|
|
# Authors:
|
2013-11-13 09:31:58 -06:00
|
|
|
# Petr Viktorin <pviktori@redhat.com>
|
2010-12-01 10:23:52 -06:00
|
|
|
#
|
2013-11-13 09:31:58 -06:00
|
|
|
# Copyright (C) 2013 Red Hat
|
2010-12-01 10:23:52 -06:00
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2010-12-01 10:23:52 -06:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-24 21:48:30 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
import re
|
2013-12-05 11:10:02 -06:00
|
|
|
import traceback
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
from ipalib.plugins import baseldap
|
|
|
|
from ipalib import errors
|
|
|
|
from ipalib.parameters import Str, StrEnum, DNParam, Flag
|
2011-08-24 21:48:30 -05:00
|
|
|
from ipalib import api, _, ngettext
|
2013-09-12 03:43:44 -05:00
|
|
|
from ipalib.plugable import Registry
|
2013-11-13 09:31:58 -06:00
|
|
|
from ipalib.capabilities import client_has_capability
|
|
|
|
from ipalib.aci import ACI
|
|
|
|
from ipapython.dn import DN
|
2011-08-24 21:48:30 -05:00
|
|
|
from ipalib.request import context
|
|
|
|
|
|
|
|
__doc__ = _("""
|
2010-12-01 10:23:52 -06:00
|
|
|
Permissions
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2011-02-22 15:24:41 -06:00
|
|
|
A permission enables fine-grained delegation of rights. A permission is
|
2013-11-13 09:31:58 -06:00
|
|
|
a human-readable wrapper around a 389-ds Access Control Rule,
|
|
|
|
or instruction (ACI).
|
2011-02-22 15:24:41 -06:00
|
|
|
A permission grants the right to perform a specific task such as adding a
|
|
|
|
user, modifying a group, etc.
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2011-02-22 15:24:41 -06:00
|
|
|
A permission may not contain other permissions.
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
|
|
|
* A permission grants access to read, write, add, delete, read, search,
|
|
|
|
or compare.
|
2010-12-01 10:23:52 -06:00
|
|
|
* A privilege combines similar permissions (for example all the permissions
|
|
|
|
needed to add a user).
|
|
|
|
* A role grants a set of privileges to users, groups, hosts or hostgroups.
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
A permission is made up of a number of different parts:
|
|
|
|
|
|
|
|
1. The name of the permission.
|
2011-01-31 10:01:56 -06:00
|
|
|
2. The target of the permission.
|
2011-03-04 10:08:54 -06:00
|
|
|
3. The rights granted by the permission.
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2011-03-04 10:08:54 -06:00
|
|
|
Rights define what operations are allowed, and may be one or more
|
|
|
|
of the following:
|
2010-12-01 10:23:52 -06:00
|
|
|
1. write - write one or more attributes
|
|
|
|
2. read - read one or more attributes
|
2013-11-13 09:31:58 -06:00
|
|
|
3. search - search on one or more attributes
|
|
|
|
4. compare - compare one or more attributes
|
|
|
|
5. add - add a new entry to the tree
|
|
|
|
6. delete - delete an existing entry
|
|
|
|
7. all - all permissions are granted
|
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
Note the distinction between attributes and entries. The permissions are
|
|
|
|
independent, so being able to add a user does not mean that the user will
|
2011-03-04 10:08:54 -06:00
|
|
|
be editable.
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
There are a number of allowed targets:
|
2013-11-13 09:31:58 -06:00
|
|
|
1. subtree: a DN; the permission applies to the subtree under this DN
|
|
|
|
2. target filter: an LDAP filter
|
|
|
|
3. target: DN with possible wildcards, specifies entries permission applies to
|
|
|
|
""" + """
|
|
|
|
Additionally, there are the following convenience options.
|
|
|
|
Setting one of these options will set the corresponding attribute(s).
|
|
|
|
1. type: a type of object (user, group, etc); sets subtree and target filter.
|
|
|
|
2. memberof: apply to members of a group; sets target filter
|
|
|
|
3. targetgroup: grant access to modify a specific group (such as granting
|
|
|
|
the rights to manage group membership); sets target.
|
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
EXAMPLES:
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
Add a permission that grants the creation of users:
|
2011-01-31 10:01:56 -06:00
|
|
|
ipa permission-add --type=user --permissions=add "Add Users"
|
2013-11-13 09:31:58 -06:00
|
|
|
""" + """
|
2010-12-01 10:23:52 -06:00
|
|
|
Add a permission that grants the ability to manage group membership:
|
2011-01-31 10:01:56 -06:00
|
|
|
ipa permission-add --attrs=member --permissions=write --type=group "Manage Group Members"
|
2011-08-24 21:48:30 -05:00
|
|
|
""")
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
register = Registry()
|
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
VALID_OBJECT_TYPES = (u'user', u'group', u'host', u'service', u'hostgroup',
|
|
|
|
u'netgroup', u'dnsrecord',)
|
|
|
|
|
|
|
|
_DEPRECATED_OPTION_ALIASES = {
|
|
|
|
'permissions': 'ipapermright',
|
|
|
|
'attrs': 'ipapermallowedattr',
|
|
|
|
'filter': 'ipapermtargetfilter',
|
|
|
|
'subtree': 'ipapermlocation',
|
|
|
|
}
|
|
|
|
|
|
|
|
KNOWN_FLAGS = {'SYSTEM', 'V2'}
|
|
|
|
|
2011-02-01 10:57:18 -06:00
|
|
|
output_params = (
|
2012-02-02 14:28:15 -06:00
|
|
|
Str('aci',
|
|
|
|
label=_('ACI'),
|
|
|
|
),
|
2011-02-01 10:57:18 -06:00
|
|
|
)
|
|
|
|
|
2012-04-17 11:42:35 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def strip_ldap_prefix(uri):
|
|
|
|
prefix = 'ldap:///'
|
|
|
|
if not uri.startswith(prefix):
|
|
|
|
raise ValueError('%r does not start with %r' % (uri, prefix))
|
|
|
|
return uri[len(prefix):]
|
|
|
|
|
|
|
|
|
|
|
|
class DNOrURL(DNParam):
|
|
|
|
"""DN parameter that allows, and strips, a "ldap:///" prefix on input
|
|
|
|
|
|
|
|
Used for ``subtree`` to maintain backward compatibility.
|
2012-04-17 11:42:35 -05:00
|
|
|
"""
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
def _convert_scalar(self, value, index=None):
|
|
|
|
if isinstance(value, basestring) and value.startswith('ldap:///'):
|
|
|
|
value = strip_ldap_prefix(value)
|
|
|
|
return super(DNOrURL, self)._convert_scalar(value, index=index)
|
2012-04-17 11:42:35 -05:00
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
|
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission(baseldap.LDAPObject):
|
2010-12-01 10:23:52 -06:00
|
|
|
"""
|
|
|
|
Permission object.
|
|
|
|
"""
|
|
|
|
container_dn = api.env.container_permission
|
2011-07-12 11:01:25 -05:00
|
|
|
object_name = _('permission')
|
|
|
|
object_name_plural = _('permissions')
|
2013-11-13 09:31:58 -06:00
|
|
|
object_class = ['groupofnames', 'ipapermission', 'ipapermissionv2']
|
2011-01-31 10:01:56 -06:00
|
|
|
default_attributes = ['cn', 'member', 'memberof',
|
2013-11-13 09:31:58 -06:00
|
|
|
'memberindirect', 'ipapermissiontype', 'objectclass',
|
|
|
|
'ipapermdefaultattr', 'ipapermallowedattr', 'ipapermexcludedattr',
|
|
|
|
'ipapermbindruletype', 'ipapermlocation', 'ipapermright',
|
|
|
|
'ipapermtargetfilter', 'ipapermtarget'
|
2010-12-01 10:23:52 -06:00
|
|
|
]
|
|
|
|
attribute_members = {
|
|
|
|
'member': ['privilege'],
|
2012-11-15 20:38:26 -06:00
|
|
|
'memberindirect': ['role'],
|
2010-12-01 10:23:52 -06:00
|
|
|
}
|
2012-02-29 12:31:20 -06:00
|
|
|
rdn_is_primary_key = True
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
label = _('Permissions')
|
2011-07-13 21:10:47 -05:00
|
|
|
label_singular = _('Permission')
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
takes_params = (
|
|
|
|
Str('cn',
|
|
|
|
cli_name='name',
|
|
|
|
label=_('Permission name'),
|
|
|
|
primary_key=True,
|
2013-11-13 09:31:58 -06:00
|
|
|
pattern='^[-_ a-zA-Z0-9.]+$',
|
|
|
|
pattern_errmsg="May only contain letters, numbers, -, _, ., and space",
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
2013-11-13 09:31:58 -06:00
|
|
|
StrEnum(
|
|
|
|
'ipapermright*',
|
2010-12-01 10:23:52 -06:00
|
|
|
cli_name='permissions',
|
|
|
|
label=_('Permissions'),
|
2013-11-13 09:31:58 -06:00
|
|
|
doc=_('Rights to grant '
|
|
|
|
'(read, search, compare, write, add, delete, all)'),
|
|
|
|
values=(u'read', u'search', u'compare',
|
|
|
|
u'write', u'add', u'delete', u'all'),
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
2013-11-13 09:31:58 -06:00
|
|
|
Str('ipapermallowedattr*',
|
2010-12-01 10:23:52 -06:00
|
|
|
cli_name='attrs',
|
|
|
|
label=_('Attributes'),
|
2013-02-14 11:01:16 -06:00
|
|
|
doc=_('Attributes to which the permission applies'),
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
2013-11-13 09:31:58 -06:00
|
|
|
StrEnum(
|
|
|
|
'ipapermbindruletype',
|
|
|
|
cli_name='bindtype',
|
|
|
|
label=_('Bind rule type'),
|
|
|
|
doc=_('Bind rule type'),
|
|
|
|
autofill=True,
|
|
|
|
values=(u'permission',),
|
|
|
|
default=u'permission',
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
2013-11-13 09:31:58 -06:00
|
|
|
DNOrURL(
|
|
|
|
'ipapermlocation?',
|
2010-12-01 10:23:52 -06:00
|
|
|
cli_name='subtree',
|
|
|
|
label=_('Subtree'),
|
|
|
|
doc=_('Subtree to apply permissions to'),
|
2013-11-13 09:31:58 -06:00
|
|
|
flags={'ask_create'},
|
|
|
|
),
|
|
|
|
Str(
|
|
|
|
'ipapermtargetfilter?',
|
|
|
|
cli_name='filter',
|
|
|
|
label=_('ACI target filter'),
|
|
|
|
doc=_('ACI target filter'),
|
|
|
|
),
|
|
|
|
|
|
|
|
DNParam(
|
|
|
|
'ipapermtarget?',
|
|
|
|
cli_name='target',
|
|
|
|
label=_('ACI target DN'),
|
|
|
|
flags={'no_option'}
|
|
|
|
),
|
|
|
|
|
|
|
|
Str('memberof?',
|
|
|
|
label=_('Member of group'), # FIXME: Does this label make sense?
|
|
|
|
doc=_('Target members of a group (sets targetfilter)'),
|
|
|
|
flags={'ask_create', 'virtual_attribute'},
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
|
|
|
Str('targetgroup?',
|
|
|
|
label=_('Target group'),
|
2013-11-13 09:31:58 -06:00
|
|
|
doc=_('User group to apply permissions to (sets target)'),
|
|
|
|
flags={'ask_create', 'virtual_attribute'},
|
|
|
|
),
|
|
|
|
StrEnum(
|
|
|
|
'type?',
|
|
|
|
label=_('Type'),
|
|
|
|
doc=_('Type of IPA object (sets subtree and filter)'),
|
|
|
|
values=VALID_OBJECT_TYPES,
|
|
|
|
flags={'ask_create', 'virtual_attribute'},
|
2010-12-01 10:23:52 -06:00
|
|
|
),
|
2013-11-13 09:31:58 -06:00
|
|
|
) + tuple(
|
|
|
|
Str(old_name + '*',
|
|
|
|
doc=_('Deprecated; use %s' % new_name),
|
|
|
|
flags={'no_option', 'virtual_attribute'})
|
|
|
|
for old_name, new_name in _DEPRECATED_OPTION_ALIASES.items()
|
2010-12-01 10:23:52 -06:00
|
|
|
)
|
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def reject_system(self, entry):
|
|
|
|
"""Raise if permission entry has unknown flags, or is a SYSTEM perm"""
|
|
|
|
flags = entry.get('ipapermissiontype', [])
|
|
|
|
for flag in flags:
|
|
|
|
if flag not in KNOWN_FLAGS:
|
|
|
|
raise errors.ACIError(
|
|
|
|
info=_('Permission with unknown flag %s may not be '
|
|
|
|
'modified or removed') % flag)
|
|
|
|
if list(flags) == [u'SYSTEM']:
|
|
|
|
raise errors.ACIError(
|
|
|
|
info=_('A SYSTEM permission may not be modified or removed'))
|
|
|
|
|
|
|
|
def postprocess_result(self, entry, options):
|
|
|
|
"""Update a permission entry for output (in place)
|
|
|
|
|
|
|
|
:param entry: The entry to update
|
|
|
|
:param options:
|
|
|
|
Command options. Contains keys such as ``raw``, ``all``,
|
|
|
|
``pkey_only``, ``version``.
|
|
|
|
"""
|
|
|
|
if not options.get('raw') and not options.get('pkey_only'):
|
|
|
|
ipapermtargetfilter = entry.single_value.get('ipapermtargetfilter',
|
|
|
|
'')
|
|
|
|
ipapermtarget = entry.single_value.get('ipapermtarget')
|
|
|
|
ipapermlocation = entry.single_value.get('ipapermlocation')
|
|
|
|
|
|
|
|
# memberof
|
|
|
|
match = re.match('^\(memberof=(.*)\)$', ipapermtargetfilter, re.I)
|
|
|
|
if match:
|
|
|
|
dn = DN(match.group(1))
|
|
|
|
if dn[1:] == DN(self.api.Object.group.container_dn,
|
|
|
|
self.api.env.basedn)[:] and dn[0].attr == 'cn':
|
|
|
|
entry.single_value['memberof'] = dn[0].value
|
|
|
|
|
|
|
|
# targetgroup
|
|
|
|
if ipapermtarget:
|
|
|
|
dn = DN(ipapermtarget)
|
|
|
|
if (dn[1:] == DN(self.api.Object.group.container_dn,
|
|
|
|
self.api.env.basedn)[:] and
|
|
|
|
dn[0].attr == 'cn' and dn[0].value != '*'):
|
|
|
|
entry.single_value['targetgroup'] = dn[0].value
|
|
|
|
|
|
|
|
# type
|
|
|
|
if ipapermtarget and ipapermlocation:
|
|
|
|
for objname in VALID_OBJECT_TYPES:
|
|
|
|
obj = self.api.Object[objname]
|
|
|
|
wantdn = DN(obj.container_dn, self.api.env.basedn)
|
|
|
|
if DN(ipapermlocation) == wantdn:
|
|
|
|
targetdn = DN(
|
|
|
|
(obj.rdn_attribute or obj.primary_key.name, '*'),
|
|
|
|
obj.container_dn,
|
|
|
|
self.api.env.basedn)
|
|
|
|
if ipapermtarget == targetdn:
|
|
|
|
entry.single_value['type'] = objname
|
|
|
|
break
|
|
|
|
|
|
|
|
# old output names
|
|
|
|
if not client_has_capability(options['version'], 'permissions2'):
|
|
|
|
for old_name, new_name in _DEPRECATED_OPTION_ALIASES.items():
|
|
|
|
if new_name in entry:
|
|
|
|
entry[old_name] = entry[new_name]
|
|
|
|
del entry[new_name]
|
|
|
|
|
|
|
|
rights = entry.get('attributelevelrights')
|
|
|
|
if rights:
|
|
|
|
rights['memberof'] = rights['ipapermtargetfilter']
|
|
|
|
rights['targetgroup'] = rights['ipapermtarget']
|
|
|
|
|
|
|
|
type_rights = set(rights['ipapermtarget'])
|
|
|
|
type_rights.intersection_update(rights['ipapermlocation'])
|
|
|
|
rights['type'] = ''.join(sorted(type_rights,
|
|
|
|
key=rights['ipapermtarget'].index))
|
|
|
|
|
|
|
|
if not client_has_capability(options['version'], 'permissions2'):
|
|
|
|
for old_name, new_name in _DEPRECATED_OPTION_ALIASES.items():
|
|
|
|
if new_name in entry:
|
|
|
|
rights[old_name] = rights[new_name]
|
|
|
|
del rights[new_name]
|
|
|
|
|
|
|
|
if options.get('raw'):
|
|
|
|
# Retreive the ACI from LDAP to ensure we get the real thing
|
2013-12-13 04:10:28 -06:00
|
|
|
try:
|
|
|
|
acientry, acistring = self._get_aci_entry_and_string(entry)
|
|
|
|
except errors.NotFound:
|
|
|
|
if list(entry.get('ipapermissiontype')) == ['SYSTEM']:
|
|
|
|
# SYSTEM permissions don't have normal ACIs
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
entry.single_value['aci'] = acistring
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
if not client_has_capability(options['version'], 'permissions2'):
|
|
|
|
# Legacy clients expect some attributes as a single value
|
|
|
|
for attr in 'type', 'targetgroup', 'memberof', 'aci':
|
|
|
|
if attr in entry:
|
|
|
|
entry[attr] = entry.single_value[attr]
|
|
|
|
if 'subtree' in entry:
|
|
|
|
# Legacy clients expect subtree as a URL
|
|
|
|
dn = entry.single_value['subtree']
|
|
|
|
entry['subtree'] = u'ldap:///%s' % dn
|
|
|
|
if 'filter' in entry:
|
|
|
|
# Legacy clients expect filter without parentheses
|
|
|
|
new_filter = []
|
|
|
|
for flt in entry['filter']:
|
|
|
|
assert flt[0] == '(' and flt[-1] == ')'
|
|
|
|
new_filter.append(flt[1:-1])
|
|
|
|
entry['filter'] = new_filter
|
|
|
|
|
|
|
|
def make_aci(self, entry):
|
|
|
|
"""Make an ACI string from the given permission entry"""
|
|
|
|
|
|
|
|
aci = ACI()
|
|
|
|
name = entry.single_value['cn']
|
|
|
|
aci.name = 'permission:%s' % name
|
|
|
|
ipapermtarget = entry.single_value.get('ipapermtarget')
|
|
|
|
if ipapermtarget:
|
|
|
|
aci.set_target('ldap:///%s' % ipapermtarget)
|
|
|
|
ipapermtargetfilter = entry.single_value.get('ipapermtargetfilter')
|
|
|
|
if ipapermtargetfilter:
|
|
|
|
aci.set_target_filter(ipapermtargetfilter)
|
|
|
|
|
|
|
|
ipapermbindruletype = entry.single_value.get('ipapermbindruletype',
|
|
|
|
'permission')
|
|
|
|
if ipapermbindruletype == 'permission':
|
|
|
|
dn = DN(('cn', name), self.container_dn, self.api.env.basedn)
|
|
|
|
aci.set_bindrule('groupdn = "ldap:///%s"' % dn)
|
|
|
|
elif ipapermbindruletype == 'all':
|
|
|
|
aci.set_bindrule('userdn = "ldap:///all"')
|
|
|
|
elif ipapermbindruletype == 'anonymous':
|
|
|
|
aci.set_bindrule('userdn = "ldap:///anyone"')
|
|
|
|
else:
|
|
|
|
raise ValueError(ipapermbindruletype)
|
|
|
|
aci.permissions = entry['ipapermright']
|
|
|
|
aci.set_target_attr(entry.get('ipapermallowedattr', []))
|
|
|
|
|
|
|
|
return aci.export_to_string()
|
|
|
|
|
|
|
|
def add_aci(self, permission_entry):
|
|
|
|
"""Add the ACI coresponding to the given permission entry"""
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
acistring = self.make_aci(permission_entry)
|
|
|
|
location = permission_entry.single_value.get('ipapermlocation',
|
|
|
|
self.api.env.basedn)
|
|
|
|
|
|
|
|
self.log.debug('Adding ACI %r to %s' % (acistring, location))
|
2013-12-05 11:10:02 -06:00
|
|
|
try:
|
|
|
|
entry = ldap.get_entry(location, ['aci'])
|
|
|
|
except errors.NotFound:
|
|
|
|
raise errors.NotFound(reason=_('Entry %s not found') % location)
|
2013-11-13 09:31:58 -06:00
|
|
|
entry.setdefault('aci', []).append(acistring)
|
|
|
|
ldap.update_entry(entry)
|
|
|
|
|
|
|
|
def remove_aci(self, permission_entry):
|
2013-12-05 11:10:02 -06:00
|
|
|
"""Remove the ACI corresponding to the given permission entry
|
|
|
|
|
|
|
|
:return: tuple:
|
|
|
|
- entry
|
|
|
|
- removed ACI string, or None if none existed previously
|
|
|
|
"""
|
|
|
|
return self._replace_aci(permission_entry)
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
def update_aci(self, permission_entry, old_name=None):
|
2013-12-05 11:10:02 -06:00
|
|
|
"""Update the ACI corresponding to the given permission entry
|
|
|
|
|
|
|
|
:return: tuple:
|
|
|
|
- entry
|
|
|
|
- removed ACI string, or None if none existed previously
|
|
|
|
"""
|
2013-11-13 09:31:58 -06:00
|
|
|
new_acistring = self.make_aci(permission_entry)
|
2013-12-05 11:10:02 -06:00
|
|
|
return self._replace_aci(permission_entry, old_name, new_acistring)
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
def _replace_aci(self, permission_entry, old_name=None, new_acistring=None):
|
|
|
|
"""Replace ACI corresponding to permission_entry
|
|
|
|
|
|
|
|
:param old_name: the old name of the permission, if different from new
|
|
|
|
:param new_acistring: new ACI string; if None the ACI is just deleted
|
2013-12-05 11:10:02 -06:00
|
|
|
:return: tuple:
|
|
|
|
- entry
|
|
|
|
- removed ACI string, or None if none existed previously
|
2013-11-13 09:31:58 -06:00
|
|
|
"""
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
acientry, acistring = self._get_aci_entry_and_string(
|
|
|
|
permission_entry, old_name, notfound_ok=True)
|
|
|
|
|
|
|
|
# (pylint thinks `acientry` is just a dict, but it's an LDAPEntry)
|
|
|
|
acidn = acientry.dn # pylint: disable=E1103
|
|
|
|
|
|
|
|
if acistring is not None:
|
|
|
|
self.log.debug('Removing ACI %r from %s' % (acistring, acidn))
|
|
|
|
acientry['aci'].remove(acistring)
|
|
|
|
if new_acistring:
|
|
|
|
self.log.debug('Adding ACI %r to %s' % (new_acistring, acidn))
|
|
|
|
acientry['aci'].append(new_acistring)
|
2011-02-01 10:57:18 -06:00
|
|
|
try:
|
2013-11-13 09:31:58 -06:00
|
|
|
ldap.update_entry(acientry)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
self.log.info('No changes to ACI')
|
2013-12-05 11:10:02 -06:00
|
|
|
return acientry, acistring
|
2013-11-13 09:31:58 -06:00
|
|
|
|
|
|
|
def _get_aci_entry_and_string(self, permission_entry, name=None,
|
|
|
|
notfound_ok=False):
|
|
|
|
"""Get the entry and ACI corresponding to the permission entry
|
|
|
|
|
|
|
|
:param name: The name of the permission, or None for the cn
|
|
|
|
:param notfound_ok:
|
|
|
|
If true, (acientry, None) will be returned on missing ACI, rather
|
|
|
|
than raising exception
|
|
|
|
"""
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
if name is None:
|
|
|
|
name = permission_entry.single_value['cn']
|
|
|
|
location = permission_entry.single_value.get('ipapermlocation',
|
|
|
|
self.api.env.basedn)
|
|
|
|
wanted_aciname = 'permission:%s' % name
|
|
|
|
|
|
|
|
try:
|
|
|
|
acientry = ldap.get_entry(location, ['aci'])
|
2011-02-01 10:57:18 -06:00
|
|
|
except errors.NotFound:
|
2013-12-05 11:10:02 -06:00
|
|
|
acientry = ldap.make_entry(location)
|
2013-11-13 09:31:58 -06:00
|
|
|
acis = acientry.get('aci', ())
|
|
|
|
for acistring in acis:
|
|
|
|
aci = ACI(acistring)
|
|
|
|
if aci.name == wanted_aciname:
|
|
|
|
return acientry, acistring
|
|
|
|
else:
|
|
|
|
if notfound_ok:
|
|
|
|
return acientry, None
|
|
|
|
raise errors.NotFound(
|
|
|
|
reason=_('The ACI for permission %(name)s was not found '
|
|
|
|
'in %(dn)s ') % {'name': name, 'dn': location})
|
|
|
|
|
|
|
|
def upgrade_permission(self, entry, target_entry=None,
|
|
|
|
output_only=False):
|
|
|
|
"""Upgrade the given permission entry to V2, in-place
|
|
|
|
|
|
|
|
The entry is only upgraded if it is a plain old-style permission,
|
|
|
|
that is, it has no flags set.
|
|
|
|
|
|
|
|
:param target_entry:
|
|
|
|
If given, ``target_entry`` is filled from information taken
|
|
|
|
from the ACI corresponding to ``entry``.
|
|
|
|
If None, ``entry`` itself is filled
|
|
|
|
:param output_only:
|
|
|
|
If true, the flags are not updated to V2.
|
|
|
|
Used for the -find and -show commands.
|
|
|
|
"""
|
|
|
|
if entry.get('ipapermissiontype'):
|
|
|
|
# Only convert old-style, non-SYSTEM permissions -- i.e. no flags
|
|
|
|
return
|
|
|
|
base, acistring = self._get_aci_entry_and_string(entry)
|
|
|
|
|
|
|
|
if not target_entry:
|
|
|
|
target_entry = entry
|
|
|
|
|
|
|
|
# The DN of old permissions is always basedn
|
|
|
|
# (pylint thinks `base` is just a dict, but it's an LDAPEntry)
|
|
|
|
assert base.dn == self.api.env.basedn, base # pylint: disable=E1103
|
|
|
|
|
|
|
|
aci = ACI(acistring)
|
|
|
|
|
|
|
|
if 'targetattr' in aci.target:
|
|
|
|
target_entry['ipapermallowedattr'] = (
|
|
|
|
aci.target['targetattr']['expression'])
|
|
|
|
if 'target' in aci.target:
|
|
|
|
target_entry.single_value['ipapermtarget'] = DN(strip_ldap_prefix(
|
|
|
|
aci.target['target']['expression']))
|
|
|
|
if 'targetfilter' in aci.target:
|
|
|
|
target_entry.single_value['ipapermtargetfilter'] = unicode(
|
|
|
|
aci.target['targetfilter']['expression'])
|
|
|
|
if aci.bindrule['expression'] == 'ldap:///all':
|
|
|
|
target_entry.single_value['ipapermbindruletype'] = u'all'
|
|
|
|
elif aci.bindrule['expression'] == 'ldap:///anyone':
|
|
|
|
target_entry.single_value['ipapermbindruletype'] = u'anonymous'
|
|
|
|
else:
|
|
|
|
target_entry.single_value['ipapermbindruletype'] = u'permission'
|
|
|
|
target_entry['ipapermright'] = aci.permissions
|
|
|
|
if 'targetattr' in aci.target:
|
|
|
|
target_entry['ipapermallowedattr'] = [
|
|
|
|
unicode(a) for a in aci.target['targetattr']['expression']]
|
2011-02-01 10:57:18 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
if not output_only:
|
|
|
|
target_entry['ipapermissiontype'] = ['SYSTEM', 'V2']
|
2012-06-29 06:24:14 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
if 'ipapermissionv2' not in entry['objectclass']:
|
|
|
|
target_entry['objectclass'] = list(entry['objectclass']) + [
|
|
|
|
u'ipapermissionv2']
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
target_entry['ipapermlocation'] = [self.api.env.basedn]
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
# Make sure we're not losing *any info* by the upgrade
|
|
|
|
new_acistring = self.make_aci(target_entry)
|
|
|
|
if not ACI(new_acistring).isequal(aci):
|
|
|
|
raise ValueError('Cannot convert ACI, %r != %r' % (new_acistring,
|
|
|
|
acistring))
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def preprocess_options(self, options):
|
|
|
|
"""Preprocess options (in-place)"""
|
|
|
|
|
|
|
|
if options.get('subtree'):
|
|
|
|
if isinstance(options['subtree'], (list, tuple)):
|
|
|
|
[options['subtree']] = options['subtree']
|
2010-12-17 14:35:24 -06:00
|
|
|
try:
|
2013-11-13 09:31:58 -06:00
|
|
|
options['subtree'] = strip_ldap_prefix(options['subtree'])
|
|
|
|
except ValueError:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='subtree',
|
|
|
|
error='does not start with "ldap:///"')
|
|
|
|
|
|
|
|
# Handle old options
|
|
|
|
for old_name, new_name in _DEPRECATED_OPTION_ALIASES.items():
|
|
|
|
if old_name in options:
|
|
|
|
if client_has_capability(options['version'], 'permissions2'):
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name=old_name,
|
|
|
|
error=_('option was renamed; use %s') % new_name)
|
|
|
|
if new_name in options:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name=old_name,
|
|
|
|
error=(_('Cannot use %(old_name)s with %(new_name)s') %
|
|
|
|
{'old_name': old_name, 'new_name': new_name}))
|
|
|
|
options[new_name] = options[old_name]
|
|
|
|
del options[old_name]
|
|
|
|
|
|
|
|
# memberof
|
|
|
|
if 'memberof' in options:
|
|
|
|
memberof = options.pop('memberof')
|
|
|
|
if memberof:
|
|
|
|
if 'ipapermtargetfilter' in options:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermtargetfilter',
|
|
|
|
error=_('filter and memberof are mutually exclusive'))
|
|
|
|
try:
|
|
|
|
groupdn = self.api.Object.group.get_dn_if_exists(memberof)
|
|
|
|
except errors.NotFound:
|
|
|
|
raise errors.NotFound(
|
|
|
|
reason=_('%s: group not found') % memberof)
|
|
|
|
options['ipapermtargetfilter'] = u'(memberOf=%s)' % groupdn
|
|
|
|
else:
|
|
|
|
if 'ipapermtargetfilter' not in options:
|
|
|
|
options['ipapermtargetfilter'] = None
|
|
|
|
|
|
|
|
# targetgroup
|
|
|
|
if 'targetgroup' in options:
|
|
|
|
targetgroup = options.pop('targetgroup')
|
|
|
|
if targetgroup:
|
|
|
|
if 'ipapermtarget' in options:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermtarget',
|
|
|
|
error=_('target and targetgroup are mutually exclusive'))
|
|
|
|
try:
|
|
|
|
groupdn = self.api.Object.group.get_dn_if_exists(targetgroup)
|
|
|
|
except errors.NotFound:
|
|
|
|
raise errors.NotFound(
|
|
|
|
reason=_('%s: group not found') % targetgroup)
|
|
|
|
options['ipapermtarget'] = groupdn
|
|
|
|
else:
|
|
|
|
if 'ipapermtarget' not in options:
|
|
|
|
options['ipapermtarget'] = None
|
|
|
|
|
|
|
|
# type
|
|
|
|
if 'type' in options:
|
|
|
|
objtype = options.pop('type')
|
|
|
|
if objtype:
|
|
|
|
if 'ipapermlocation' in options:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermlocation',
|
|
|
|
error=_('subtree and type are mutually exclusive'))
|
|
|
|
if 'ipapermtarget' in options:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermtarget',
|
|
|
|
error=_('target and type are mutually exclusive'))
|
|
|
|
obj = self.api.Object[objtype.lower()]
|
|
|
|
container_dn = DN(obj.container_dn, self.api.env.basedn)
|
|
|
|
options['ipapermtarget'] = DN(
|
|
|
|
(obj.rdn_attribute or obj.primary_key.name, '*'),
|
|
|
|
container_dn)
|
|
|
|
options['ipapermlocation'] = container_dn
|
|
|
|
else:
|
|
|
|
if 'ipapermtarget' not in options:
|
|
|
|
options['ipapermtarget'] = None
|
|
|
|
if 'ipapermlocation' not in options:
|
|
|
|
options['ipapermlocation'] = None
|
|
|
|
|
|
|
|
def validate_permission(self, entry):
|
|
|
|
ldap = self.Backend.ldap2
|
|
|
|
|
|
|
|
# Rough filter validation by a search
|
|
|
|
if 'ipapermtargetfilter' in entry:
|
2010-12-17 14:35:24 -06:00
|
|
|
try:
|
2013-11-13 09:31:58 -06:00
|
|
|
ldap.find_entries(
|
|
|
|
filter=entry.single_value['ipapermtargetfilter'],
|
|
|
|
base_dn=self.env.basedn,
|
|
|
|
scope=ldap.SCOPE_BASE,
|
|
|
|
size_limit=1)
|
|
|
|
except errors.NotFound:
|
2010-12-17 14:35:24 -06:00
|
|
|
pass
|
2013-11-13 09:31:58 -06:00
|
|
|
except errors.BadSearchFilter:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermtargetfilter',
|
|
|
|
error=_('Bad search filter'))
|
|
|
|
|
2013-12-05 11:18:32 -06:00
|
|
|
# Ensure location exists
|
|
|
|
if entry.get('ipapermlocation'):
|
|
|
|
location = DN(entry.single_value['ipapermlocation'])
|
|
|
|
try:
|
|
|
|
ldap.get_entry(location, attrs_list=[])
|
|
|
|
except errors.NotFound:
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='ipapermlocation',
|
|
|
|
error=_('Entry %s does not exist') % location)
|
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
# Ensure there's something in the ACI's filter
|
|
|
|
needed_attrs = (
|
|
|
|
'ipapermtarget', 'ipapermtargetfilter', 'ipapermallowedattr')
|
|
|
|
if not any(entry.single_value.get(a) for a in needed_attrs):
|
|
|
|
raise errors.ValidationError(
|
|
|
|
name='target',
|
|
|
|
error=_('there must be at least one target entry specifier '
|
|
|
|
'(e.g. target, targetfilter, attrs)'))
|
|
|
|
|
|
|
|
# Ensure there's a right
|
|
|
|
if not entry.get('ipapermright'):
|
|
|
|
raise errors.RequirementError(name='ipapermright')
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_add_noaci(baseldap.LDAPCreate):
|
|
|
|
__doc__ = _('Add a system permission without an ACI (internal command)')
|
2012-06-27 06:10:10 -05:00
|
|
|
|
|
|
|
msg_summary = _('Added permission "%(value)s"')
|
|
|
|
NO_CLI = True
|
2013-11-13 09:31:58 -06:00
|
|
|
has_output_params = baseldap.LDAPCreate.has_output_params + output_params
|
2012-06-27 06:10:10 -05:00
|
|
|
|
|
|
|
takes_options = (
|
2013-11-13 09:31:58 -06:00
|
|
|
Str('ipapermissiontype+',
|
|
|
|
label=_('Permission flags'),
|
2012-06-27 06:10:10 -05:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_options(self):
|
2013-11-13 09:31:58 -06:00
|
|
|
perm_options = set(o.name for o in self.obj.takes_params)
|
2012-06-27 06:10:10 -05:00
|
|
|
for option in super(permission_add_noaci, self).get_options():
|
2013-11-13 09:31:58 -06:00
|
|
|
# From new options, only cn & ipapermissiontype are supported
|
|
|
|
if option.name in ['ipapermissiontype']:
|
|
|
|
yield option.clone()
|
|
|
|
# Other options such as raw, version are supported
|
|
|
|
elif option.name not in perm_options:
|
|
|
|
yield option.clone()
|
|
|
|
|
|
|
|
def pre_callback(self, ldap, dn, entry, attrs_list, *keys, **options):
|
|
|
|
entry['ipapermissiontype'] = list(options['ipapermissiontype'])
|
|
|
|
entry['objectclass'] = [oc for oc in entry['objectclass']
|
|
|
|
if oc.lower() != 'ipapermissionv2']
|
2012-06-27 06:10:10 -05:00
|
|
|
return dn
|
|
|
|
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_add(baseldap.LDAPCreate):
|
|
|
|
__doc__ = _('Add a new permission.')
|
|
|
|
|
|
|
|
msg_summary = _('Added permission "%(value)s"')
|
|
|
|
has_output_params = baseldap.LDAPCreate.has_output_params + output_params
|
|
|
|
|
|
|
|
# Need to override args_options_2_params so that processed options apply to
|
|
|
|
# the whole command, not just the callbacks
|
|
|
|
def args_options_2_params(self, *args, **options):
|
|
|
|
if self.env.in_server:
|
|
|
|
self.obj.preprocess_options(options)
|
|
|
|
|
|
|
|
return super(permission_add, self).args_options_2_params(
|
|
|
|
*args, **options)
|
|
|
|
|
|
|
|
def pre_callback(self, ldap, dn, entry, attrs_list, *keys, **options):
|
|
|
|
entry['ipapermissiontype'] = ['SYSTEM', 'V2']
|
|
|
|
entry['cn'] = list(keys)
|
|
|
|
if not entry.get('ipapermlocation'):
|
|
|
|
entry.setdefault('ipapermlocation', [api.env.basedn])
|
|
|
|
|
|
|
|
self.obj.validate_permission(entry)
|
|
|
|
return dn
|
|
|
|
|
|
|
|
def post_callback(self, ldap, dn, entry, *keys, **options):
|
|
|
|
self.obj.add_aci(entry)
|
|
|
|
self.obj.postprocess_result(entry, options)
|
|
|
|
return dn
|
|
|
|
|
|
|
|
|
|
|
|
@register()
|
|
|
|
class permission_del(baseldap.LDAPDelete):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Delete a permission.')
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
msg_summary = _('Deleted permission "%(value)s"')
|
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
takes_options = baseldap.LDAPDelete.takes_options + (
|
2012-06-27 06:10:10 -05:00
|
|
|
Flag('force',
|
|
|
|
label=_('Force'),
|
2013-11-13 09:31:58 -06:00
|
|
|
flags={'no_option', 'no_output'},
|
2012-06-27 06:10:10 -05:00
|
|
|
doc=_('force delete of SYSTEM permissions'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2010-12-01 10:23:52 -06:00
|
|
|
def pre_callback(self, ldap, dn, *keys, **options):
|
2011-01-21 02:20:01 -06:00
|
|
|
try:
|
2013-11-13 09:31:58 -06:00
|
|
|
entry = ldap.get_entry(dn, attrs_list=self.obj.default_attributes)
|
2011-01-21 02:20:01 -06:00
|
|
|
except errors.NotFound:
|
2013-11-13 09:31:58 -06:00
|
|
|
self.obj.handle_not_found(*keys)
|
|
|
|
|
|
|
|
if not options.get('force'):
|
|
|
|
self.obj.reject_system(entry)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.obj.remove_aci(entry)
|
|
|
|
except errors.NotFound:
|
|
|
|
errors.NotFound('ACI of permission %s was not found' % keys[0])
|
|
|
|
|
2010-12-01 10:23:52 -06:00
|
|
|
return dn
|
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_mod(baseldap.LDAPUpdate):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Modify a permission.')
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
msg_summary = _('Modified permission "%(value)s"')
|
2013-11-13 09:31:58 -06:00
|
|
|
has_output_params = baseldap.LDAPUpdate.has_output_params + output_params
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def args_options_2_params(self, *args, **options):
|
|
|
|
if self.env.in_server:
|
|
|
|
self.obj.preprocess_options(options)
|
|
|
|
|
|
|
|
return super(permission_mod, self).args_options_2_params(
|
|
|
|
*args, **options)
|
|
|
|
|
|
|
|
def pre_callback(self, ldap, dn, entry, attrs_list, *keys, **options):
|
|
|
|
if 'rename' in options and not options['rename']:
|
|
|
|
raise errors.ValidationError(name='rename',
|
|
|
|
error='New name can not be empty')
|
2011-02-01 10:57:18 -06:00
|
|
|
|
2010-12-10 21:52:44 -06:00
|
|
|
try:
|
2013-11-13 09:31:58 -06:00
|
|
|
attrs_list = self.obj.default_attributes
|
|
|
|
old_entry = ldap.get_entry(dn, attrs_list=attrs_list)
|
2010-12-10 21:52:44 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
2011-01-27 05:17:10 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
self.obj.reject_system(old_entry)
|
|
|
|
self.obj.upgrade_permission(old_entry)
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
# Since `entry` only contains the attributes we are currently changing,
|
|
|
|
# it cannot be used directly to generate an ACI.
|
|
|
|
# First we need to copy the original data into it.
|
|
|
|
for key, value in old_entry.iteritems():
|
|
|
|
if key not in options and key != 'cn':
|
|
|
|
entry.setdefault(key, value)
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
if not entry.get('ipapermlocation'):
|
|
|
|
entry['ipapermlocation'] = [self.api.env.basedn]
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
self.obj.validate_permission(entry)
|
2011-01-27 05:17:10 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
old_location = old_entry.single_value.get('ipapermlocation',
|
|
|
|
self.api.env.basedn)
|
|
|
|
if old_location == options.get('ipapermlocation', old_location):
|
|
|
|
context.permision_moving_aci = False
|
|
|
|
else:
|
|
|
|
context.permision_moving_aci = True
|
|
|
|
try:
|
2013-12-05 11:10:02 -06:00
|
|
|
context.old_aci_info = self.obj.remove_aci(old_entry)
|
2013-11-13 09:31:58 -06:00
|
|
|
except errors.NotFound, e:
|
|
|
|
self.log.error('permission ACI not found: %s' % e)
|
2011-01-21 02:20:01 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
# To pass data to postcallback, we currently need to use the context
|
|
|
|
context.old_entry = old_entry
|
2011-01-27 05:17:10 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
return dn
|
2011-01-21 02:20:01 -06:00
|
|
|
|
2013-12-05 11:10:02 -06:00
|
|
|
def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
|
|
|
|
if call_func.func_name == 'update_entry':
|
|
|
|
self._revert_aci()
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
def _revert_aci(self):
|
|
|
|
old_aci_info = getattr(context, 'old_aci_info', None)
|
|
|
|
if old_aci_info:
|
|
|
|
# Try to roll back the old ACI
|
|
|
|
entry, old_aci_string = old_aci_info
|
|
|
|
if old_aci_string:
|
|
|
|
self.log.warn('Reverting ACI on %s to %s' % (entry.dn,
|
|
|
|
old_aci_string))
|
|
|
|
entry['aci'].append(old_aci_string)
|
|
|
|
self.Backend.ldap2.update_entry(entry)
|
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def post_callback(self, ldap, dn, entry, *keys, **options):
|
|
|
|
old_entry = context.old_entry
|
2012-08-22 09:39:01 -05:00
|
|
|
|
2013-12-05 11:10:02 -06:00
|
|
|
try:
|
|
|
|
if context.permision_moving_aci:
|
|
|
|
self.obj.add_aci(entry)
|
|
|
|
else:
|
|
|
|
self.obj.update_aci(entry, old_entry.single_value['cn'])
|
|
|
|
except Exception:
|
|
|
|
self.log.error('Error updating ACI: %s' % traceback.format_exc())
|
|
|
|
self.log.warn('Reverting entry')
|
|
|
|
ldap.update_entry(old_entry)
|
|
|
|
self._revert_aci()
|
|
|
|
raise
|
2013-11-13 09:31:58 -06:00
|
|
|
self.obj.postprocess_result(entry, options)
|
|
|
|
entry['dn'] = entry.dn
|
2010-12-01 10:23:52 -06:00
|
|
|
return dn
|
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_find(baseldap.LDAPSearch):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Search for permissions.')
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
msg_summary = ngettext(
|
2013-11-13 09:31:58 -06:00
|
|
|
'%(count)d permission matched', '%(count)d permissions matched', 0)
|
|
|
|
has_output_params = baseldap.LDAPSearch.has_output_params + output_params
|
2010-12-01 10:23:52 -06:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def args_options_2_params(self, *args, **options):
|
|
|
|
if self.env.in_server:
|
|
|
|
self.obj.preprocess_options(options)
|
2012-04-17 11:42:35 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
return super(permission_find, self).args_options_2_params(
|
|
|
|
*args, **options)
|
|
|
|
|
|
|
|
def post_callback(self, ldap, entries, truncated, *args, **options):
|
|
|
|
attribute_options = [o for o in options
|
|
|
|
if (o in self.options and
|
|
|
|
self.options[o].attribute)]
|
2012-04-17 11:42:35 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
if not options.get('pkey_only'):
|
2012-05-31 05:39:24 -05:00
|
|
|
for entry in entries:
|
2013-11-13 09:31:58 -06:00
|
|
|
# Old-style permissions might have matched (e.g. by name)
|
|
|
|
self.obj.upgrade_permission(entry, output_only=True)
|
2012-05-23 10:00:24 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
if not truncated:
|
|
|
|
if 'sizelimit' in options:
|
|
|
|
max_entries = options['sizelimit']
|
|
|
|
else:
|
|
|
|
config = ldap.get_ipa_config()[1]
|
|
|
|
max_entries = int(config.single_value['ipasearchrecordslimit'])
|
|
|
|
|
|
|
|
filters = ['(objectclass=ipaPermission)',
|
|
|
|
'(!(ipaPermissionType=V2))']
|
|
|
|
if args:
|
|
|
|
filters.append(ldap.make_filter_from_attr('cn', args[0],
|
|
|
|
exact=False))
|
|
|
|
attrs_list = list(self.obj.default_attributes)
|
|
|
|
attrs_list += list(self.obj.attribute_members)
|
|
|
|
if options.get('all'):
|
|
|
|
attrs_list.append('*')
|
|
|
|
try:
|
|
|
|
legacy_entries = ldap.get_entries(
|
|
|
|
base_dn=DN(self.obj.container_dn, self.api.env.basedn),
|
|
|
|
filter=ldap.combine_filters(filters, rules=ldap.MATCH_ALL),
|
|
|
|
attrs_list=attrs_list)
|
|
|
|
except errors.NotFound:
|
|
|
|
legacy_entries = ()
|
|
|
|
self.log.debug('potential legacy entries: %s', len(legacy_entries))
|
|
|
|
nonlegacy_names = {e.single_value['cn'] for e in entries}
|
|
|
|
for entry in legacy_entries:
|
|
|
|
if entry.single_value['cn'] in nonlegacy_names:
|
|
|
|
continue
|
|
|
|
if len(entries) > max_entries:
|
|
|
|
# We've over the limit, pop the last entry and set
|
|
|
|
# truncated flag
|
|
|
|
# (this is easier to do than checking before adding
|
|
|
|
# the entry to results)
|
|
|
|
entries.pop()
|
|
|
|
truncated = True
|
|
|
|
break
|
|
|
|
self.obj.upgrade_permission(entry, output_only=True)
|
|
|
|
cn = entry.single_value['cn']
|
|
|
|
if any(a.lower() in cn.lower() for a in args if a):
|
|
|
|
entries.append(entry)
|
|
|
|
else:
|
|
|
|
# If all given options match, include the entry
|
|
|
|
# Do a case-insensitive match, on any value if multi-valued
|
|
|
|
for opt in attribute_options:
|
|
|
|
optval = options[opt]
|
|
|
|
if not isinstance(optval, (tuple, list)):
|
|
|
|
optval = [optval]
|
|
|
|
value = entry.get(opt)
|
|
|
|
if not value:
|
|
|
|
break
|
|
|
|
if not all(any(str(ov).lower() in str(v).lower()
|
|
|
|
for v in value) for ov in optval):
|
|
|
|
break
|
2012-05-31 05:39:24 -05:00
|
|
|
else:
|
2013-11-13 09:31:58 -06:00
|
|
|
entries.append(entry)
|
|
|
|
|
|
|
|
for entry in entries:
|
|
|
|
if options.get('pkey_only'):
|
|
|
|
for opt_name in entry.keys():
|
|
|
|
if opt_name != self.obj.primary_key.name:
|
|
|
|
del entry[opt_name]
|
|
|
|
else:
|
|
|
|
self.obj.postprocess_result(entry, options)
|
|
|
|
|
2012-05-23 10:00:24 -05:00
|
|
|
return truncated
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_show(baseldap.LDAPRetrieve):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Display information about a permission.')
|
2013-11-13 09:31:58 -06:00
|
|
|
has_output_params = baseldap.LDAPRetrieve.has_output_params + output_params
|
2011-08-24 21:48:30 -05:00
|
|
|
|
2013-11-13 09:31:58 -06:00
|
|
|
def post_callback(self, ldap, dn, entry, *keys, **options):
|
|
|
|
self.obj.upgrade_permission(entry, output_only=True)
|
|
|
|
self.obj.postprocess_result(entry, options)
|
2010-12-01 10:23:52 -06:00
|
|
|
return dn
|
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_add_member(baseldap.LDAPAddMember):
|
|
|
|
"""Add members to a permission."""
|
2011-01-20 14:07:43 -06:00
|
|
|
NO_CLI = True
|
2010-12-01 10:23:52 -06:00
|
|
|
|
|
|
|
|
2013-09-12 03:43:44 -05:00
|
|
|
@register()
|
2013-11-13 09:31:58 -06:00
|
|
|
class permission_remove_member(baseldap.LDAPRemoveMember):
|
|
|
|
"""Remove members from a permission."""
|
2011-01-20 14:07:43 -06:00
|
|
|
NO_CLI = True
|