2009-09-30 09:24:25 -05:00
|
|
|
# Authors:
|
|
|
|
# Pavel Zuna <pzuna@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# 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.
|
2009-09-30 09:24:25 -05: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
|
|
|
|
|
|
|
from ipalib import api, errors
|
|
|
|
from ipalib import AccessTime, Password, Str, StrEnum
|
|
|
|
from ipalib.plugins.baseldap import *
|
|
|
|
from ipalib import _, ngettext
|
|
|
|
|
|
|
|
__doc__ = _("""
|
2010-08-24 22:40:32 -05:00
|
|
|
Host-based access control
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Control who can access what services on what hosts and from where. You
|
|
|
|
can use HBAC to control which users or groups on a source host can
|
2010-12-03 12:57:19 -06:00
|
|
|
access a service, or group of services, on a target host.
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
You can also specify a category of users, target hosts, and source
|
|
|
|
hosts. This is currently limited to "all", but might be expanded in the
|
|
|
|
future.
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Target hosts and source hosts in HBAC rules must be hosts managed by IPA.
|
|
|
|
|
|
|
|
The available services and groups of services are controlled by the
|
|
|
|
hbacsvc and hbacsvcgroup plug-ins respectively.
|
2010-06-02 13:08:50 -05:00
|
|
|
|
|
|
|
EXAMPLES:
|
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Create a rule, "test1", that grants all users access to the host "server" from
|
2010-06-02 13:08:50 -05:00
|
|
|
anywhere:
|
2011-07-19 19:46:20 -05:00
|
|
|
ipa hbacrule-add --usercat=all --srchostcat=all test1
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-add-host --hosts=server.example.com test1
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Display the properties of a named HBAC rule:
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-show test1
|
2010-06-02 13:08:50 -05:00
|
|
|
|
|
|
|
Create a rule for a specific service. This lets the user john access
|
|
|
|
the sshd service on any machine from any machine:
|
2011-07-19 19:46:20 -05:00
|
|
|
ipa hbacrule-add --hostcat=all --srchostcat=all john_sshd
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-add-user --users=john john_sshd
|
|
|
|
ipa hbacrule-add-service --hbacsvcs=sshd john_sshd
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-09-20 12:09:02 -05:00
|
|
|
Create a rule for a new service group. This lets the user john access
|
2011-03-04 10:08:54 -06:00
|
|
|
the FTP service on any machine from any machine:
|
2010-09-20 12:09:02 -05:00
|
|
|
ipa hbacsvcgroup-add ftpers
|
|
|
|
ipa hbacsvc-add sftp
|
|
|
|
ipa hbacsvcgroup-add-member --hbacsvcs=ftp,sftp ftpers
|
2011-07-19 19:46:20 -05:00
|
|
|
ipa hbacrule-add --hostcat=all --srchostcat=all john_ftp
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-add-user --users=john john_ftp
|
|
|
|
ipa hbacrule-add-service --hbacsvcgroups=ftpers john_ftp
|
2010-09-20 12:09:02 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Disable a named HBAC rule:
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-disable test1
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Remove a named HBAC rule:
|
2010-12-21 06:20:18 -06:00
|
|
|
ipa hbacrule-del allow_server
|
2011-08-24 21:48:30 -05:00
|
|
|
""")
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2010-12-03 12:57:19 -06:00
|
|
|
|
|
|
|
# AccessTime support is being removed for now.
|
|
|
|
#
|
|
|
|
# You can also control the times that the rule is active.
|
|
|
|
#
|
|
|
|
# The access time(s) of a host are cumulative and are not guaranteed to be
|
|
|
|
# applied in the order displayed.
|
|
|
|
#
|
|
|
|
# Specify that the rule "test1" be active every day between 0800 and 1400:
|
2010-12-21 06:20:18 -06:00
|
|
|
# ipa hbacrule-add-accesstime --time='periodic daily 0800-1400' test1
|
2010-12-03 12:57:19 -06:00
|
|
|
#
|
|
|
|
# Specify that the rule "test1" be active once, from 10:32 until 10:33 on
|
|
|
|
# December 16, 2010:
|
2010-12-21 06:20:18 -06:00
|
|
|
# ipa hbacrule-add-accesstime --time='absolute 201012161032 ~ 201012161033' test1
|
2010-12-03 12:57:19 -06:00
|
|
|
|
|
|
|
|
2011-08-24 16:27:32 -05:00
|
|
|
topic = ('hbac', _('Host-based access control commands'))
|
2010-12-08 09:22:51 -06:00
|
|
|
|
2011-07-06 16:45:53 -05:00
|
|
|
def validate_type(ugettext, type):
|
|
|
|
if type.lower() == 'deny':
|
|
|
|
raise errors.ValidationError(name='type', error=_('The deny type has been deprecated.'))
|
|
|
|
|
2010-10-11 09:07:15 -05:00
|
|
|
def is_all(options, attribute):
|
|
|
|
"""
|
|
|
|
See if options[attribute] is lower-case 'all' in a safe way.
|
|
|
|
"""
|
|
|
|
if attribute in options and \
|
|
|
|
options[attribute] is not None and \
|
|
|
|
options[attribute].lower() == 'all':
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule(LDAPObject):
|
2009-09-30 09:24:25 -05:00
|
|
|
"""
|
|
|
|
HBAC object.
|
|
|
|
"""
|
|
|
|
container_dn = api.env.container_hbac
|
2011-07-12 11:01:25 -05:00
|
|
|
object_name = _('HBAC rule')
|
|
|
|
object_name_plural = _('HBAC rules')
|
2009-09-30 09:24:25 -05:00
|
|
|
object_class = ['ipaassociation', 'ipahbacrule']
|
|
|
|
default_attributes = [
|
2011-07-19 19:46:20 -05:00
|
|
|
'cn', 'ipaenabledflag',
|
2010-12-03 12:57:19 -06:00
|
|
|
'description', 'usercategory', 'hostcategory',
|
2010-05-14 08:37:54 -05:00
|
|
|
'sourcehostcategory', 'servicecategory', 'ipaenabledflag',
|
|
|
|
'memberuser', 'sourcehost', 'memberhost', 'memberservice',
|
2011-08-17 10:49:49 -05:00
|
|
|
'memberhostgroup', 'externalhost',
|
2009-09-30 09:24:25 -05:00
|
|
|
]
|
|
|
|
uuid_attribute = 'ipauniqueid'
|
2010-10-27 12:04:06 -05:00
|
|
|
rdn_attribute = 'ipauniqueid'
|
2009-09-30 09:24:25 -05:00
|
|
|
attribute_members = {
|
|
|
|
'memberuser': ['user', 'group'],
|
|
|
|
'memberhost': ['host', 'hostgroup'],
|
|
|
|
'sourcehost': ['host', 'hostgroup'],
|
2010-05-14 08:37:54 -05:00
|
|
|
'memberservice': ['hbacsvc', 'hbacsvcgroup'],
|
2009-09-30 09:24:25 -05:00
|
|
|
}
|
|
|
|
|
2011-06-24 11:39:48 -05:00
|
|
|
label = _('HBAC Rules')
|
2011-07-13 21:10:47 -05:00
|
|
|
label_singular = _('HBAC Rule')
|
2010-02-08 06:03:28 -06:00
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
takes_params = (
|
|
|
|
Str('cn',
|
|
|
|
cli_name='name',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Rule name'),
|
2009-09-30 09:24:25 -05:00
|
|
|
primary_key=True,
|
|
|
|
),
|
2011-07-06 16:45:53 -05:00
|
|
|
StrEnum('accessruletype', validate_type,
|
2009-09-30 09:24:25 -05:00
|
|
|
cli_name='type',
|
2011-07-06 16:45:53 -05:00
|
|
|
doc=_('Rule type (allow)'),
|
2010-05-04 14:28:32 -05:00
|
|
|
label=_('Rule type'),
|
2009-09-30 09:24:25 -05:00
|
|
|
values=(u'allow', u'deny'),
|
2011-07-06 16:45:53 -05:00
|
|
|
default=u'allow',
|
|
|
|
autofill=True,
|
2011-07-19 19:46:20 -05:00
|
|
|
exclude='webui',
|
|
|
|
flags=['no_option', 'no_output'],
|
2009-09-30 09:24:25 -05:00
|
|
|
),
|
2010-05-14 08:37:54 -05:00
|
|
|
# FIXME: {user,host,sourcehost,service}categories should expand in the future
|
2009-11-18 09:39:12 -06:00
|
|
|
StrEnum('usercategory?',
|
|
|
|
cli_name='usercat',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('User category'),
|
|
|
|
doc=_('User category the rule applies to'),
|
2009-11-18 09:39:12 -06:00
|
|
|
values=(u'all', ),
|
|
|
|
),
|
|
|
|
StrEnum('hostcategory?',
|
|
|
|
cli_name='hostcat',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Host category'),
|
|
|
|
doc=_('Host category the rule applies to'),
|
2009-11-18 09:39:12 -06:00
|
|
|
values=(u'all', ),
|
|
|
|
),
|
|
|
|
StrEnum('sourcehostcategory?',
|
|
|
|
cli_name='srchostcat',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Source host category'),
|
|
|
|
doc=_('Source host category the rule applies to'),
|
2009-11-18 09:39:12 -06:00
|
|
|
values=(u'all', ),
|
|
|
|
),
|
2010-05-14 08:37:54 -05:00
|
|
|
StrEnum('servicecategory?',
|
|
|
|
cli_name='servicecat',
|
|
|
|
label=_('Service category'),
|
|
|
|
doc=_('Service category the rule applies to'),
|
|
|
|
values=(u'all', ),
|
|
|
|
),
|
2010-12-03 12:57:19 -06:00
|
|
|
# AccessTime('accesstime?',
|
|
|
|
# cli_name='time',
|
|
|
|
# label=_('Access time'),
|
|
|
|
# ),
|
2009-09-30 09:24:25 -05:00
|
|
|
Str('description?',
|
|
|
|
cli_name='desc',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Description'),
|
2009-09-30 09:24:25 -05:00
|
|
|
),
|
2010-05-04 14:28:32 -05:00
|
|
|
Flag('ipaenabledflag?',
|
|
|
|
label=_('Enabled'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2010-05-14 08:37:54 -05:00
|
|
|
Str('memberuser_user?',
|
|
|
|
label=_('Users'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2010-11-24 08:19:31 -06:00
|
|
|
Str('memberuser_group?',
|
2011-09-09 18:58:52 -05:00
|
|
|
label=_('User Groups'),
|
2010-11-24 08:19:31 -06:00
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2010-05-14 08:37:54 -05:00
|
|
|
Str('memberhost_host?',
|
|
|
|
label=_('Hosts'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
|
|
|
Str('memberhost_hostgroup?',
|
|
|
|
label=_('Host Groups'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
|
|
|
Str('sourcehost_host?',
|
2011-09-09 18:58:52 -05:00
|
|
|
label=_('Source Hosts'),
|
2010-05-14 08:37:54 -05:00
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2011-02-18 00:02:51 -06:00
|
|
|
Str('sourcehost_hostgroup?',
|
2011-09-09 18:58:52 -05:00
|
|
|
label=_('Source Host Groups'),
|
2011-02-18 00:02:51 -06:00
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2010-11-24 08:19:31 -06:00
|
|
|
Str('memberservice_hbacsvc?',
|
2010-05-14 08:37:54 -05:00
|
|
|
label=_('Services'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2010-11-24 08:19:31 -06:00
|
|
|
Str('memberservice_hbacsvcgroup?',
|
2010-05-14 08:37:54 -05:00
|
|
|
label=_('Service Groups'),
|
|
|
|
flags=['no_create', 'no_update', 'no_search'],
|
|
|
|
),
|
2009-09-30 09:24:25 -05:00
|
|
|
)
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add(LDAPCreate):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Create a new HBAC rule.')
|
2011-02-04 07:03:30 -06:00
|
|
|
|
|
|
|
msg_summary = _('Added HBAC rule "%(value)s"')
|
|
|
|
|
2009-12-10 09:39:24 -06:00
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
2009-12-09 10:09:53 -06:00
|
|
|
# HBAC rules are enabled by default
|
2009-11-02 05:59:40 -06:00
|
|
|
entry_attrs['ipaenabledflag'] = 'TRUE'
|
2010-10-27 12:04:06 -05:00
|
|
|
return dn
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_add)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_del(LDAPDelete):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Delete an HBAC rule.')
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2011-02-04 07:03:30 -06:00
|
|
|
msg_summary = _('Deleted HBAC rule "%(value)s"')
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_del)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_mod(LDAPUpdate):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Modify an HBAC rule.')
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2011-02-04 07:03:30 -06:00
|
|
|
msg_summary = _('Modified HBAC rule "%(value)s"')
|
|
|
|
|
2010-09-15 08:20:14 -05:00
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
2010-12-10 12:15:53 -06:00
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, attrs_list)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
|
|
|
|
2010-10-11 09:07:15 -05:00
|
|
|
if is_all(options, 'usercategory') and 'memberuser' in entry_attrs:
|
2010-09-15 08:20:14 -05:00
|
|
|
raise errors.MutuallyExclusiveError(reason="user category cannot be set to 'all' while there are allowed users")
|
2010-10-11 09:07:15 -05:00
|
|
|
if is_all(options, 'hostcategory') and 'memberhost' in entry_attrs:
|
2010-09-15 08:20:14 -05:00
|
|
|
raise errors.MutuallyExclusiveError(reason="host category cannot be set to 'all' while there are allowed hosts")
|
2010-10-11 09:07:15 -05:00
|
|
|
if is_all(options, 'sourcehostcategory') and 'sourcehost' in entry_attrs:
|
2010-09-15 08:20:14 -05:00
|
|
|
raise errors.MutuallyExclusiveError(reason="sourcehost category cannot be set to 'all' while there are allowed source hosts")
|
2010-10-11 09:07:15 -05:00
|
|
|
if is_all(options, 'servicecategory') and 'memberservice' in entry_attrs:
|
2010-09-15 08:20:14 -05:00
|
|
|
raise errors.MutuallyExclusiveError(reason="service category cannot be set to 'all' while there are allowed services")
|
|
|
|
return dn
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_mod)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_find(LDAPSearch):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Search for HBAC rules.')
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2011-02-04 07:03:30 -06:00
|
|
|
msg_summary = ngettext(
|
|
|
|
'%(count)d HBAC rule matched', '%(count)d HBAC rules matched', 0
|
|
|
|
)
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_find)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_show(LDAPRetrieve):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Display the properties of an HBAC rule.')
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_show)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_enable(LDAPQuery):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Enable an HBAC rule.')
|
2011-02-04 07:03:30 -06:00
|
|
|
|
|
|
|
msg_summary = _('Enabled HBAC rule "%(value)s"')
|
|
|
|
has_output = output.standard_value
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
def execute(self, cn):
|
|
|
|
ldap = self.obj.backend
|
|
|
|
|
|
|
|
dn = self.obj.get_dn(cn)
|
2009-11-02 05:59:40 -06:00
|
|
|
entry_attrs = {'ipaenabledflag': 'TRUE'}
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, entry_attrs)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
2010-11-29 08:37:47 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(cn)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2011-02-04 07:03:30 -06:00
|
|
|
return dict(
|
|
|
|
result=True,
|
|
|
|
value=cn,
|
|
|
|
)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_enable)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_disable(LDAPQuery):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Disable an HBAC rule.')
|
2011-02-04 07:03:30 -06:00
|
|
|
|
|
|
|
msg_summary = _('Disabled HBAC rule "%(value)s"')
|
|
|
|
has_output = output.standard_value
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
def execute(self, cn):
|
|
|
|
ldap = self.obj.backend
|
|
|
|
|
|
|
|
dn = self.obj.get_dn(cn)
|
2009-11-02 05:59:40 -06:00
|
|
|
entry_attrs = {'ipaenabledflag': 'FALSE'}
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, entry_attrs)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
2010-11-29 08:37:47 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(cn)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2011-02-04 07:03:30 -06:00
|
|
|
return dict(
|
|
|
|
result=True,
|
|
|
|
value=cn,
|
|
|
|
)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_disable)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add_accesstime(LDAPQuery):
|
2009-11-18 09:39:12 -06:00
|
|
|
"""
|
2010-08-24 22:40:32 -05:00
|
|
|
Add an access time to an HBAC rule.
|
2009-11-18 09:39:12 -06:00
|
|
|
"""
|
2010-02-19 10:08:16 -06:00
|
|
|
|
2009-11-18 09:39:12 -06:00
|
|
|
takes_options = (
|
2009-11-18 10:33:55 -06:00
|
|
|
AccessTime('accesstime',
|
2009-11-18 09:39:12 -06:00
|
|
|
cli_name='time',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Access time'),
|
2009-11-18 09:39:12 -06:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, cn, **options):
|
|
|
|
ldap = self.obj.backend
|
|
|
|
|
|
|
|
dn = self.obj.get_dn(cn)
|
|
|
|
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, ['accesstime'])
|
|
|
|
entry_attrs.setdefault('accesstime', []).append(
|
|
|
|
options['accesstime']
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, entry_attrs)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
2010-11-29 08:37:47 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(cn)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=True)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, cn, **options):
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_dashed(
|
|
|
|
'Added access time "%s" to HBAC rule "%s"' % (
|
|
|
|
options['accesstime'], cn
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
#api.register(hbacrule_add_accesstime)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_remove_accesstime(LDAPQuery):
|
2009-11-18 09:39:12 -06:00
|
|
|
"""
|
|
|
|
Remove access time to HBAC rule.
|
|
|
|
"""
|
|
|
|
takes_options = (
|
2009-11-18 10:33:55 -06:00
|
|
|
AccessTime('accesstime?',
|
2009-11-18 09:39:12 -06:00
|
|
|
cli_name='time',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Access time'),
|
2009-11-18 09:39:12 -06:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, cn, **options):
|
|
|
|
ldap = self.obj.backend
|
|
|
|
|
|
|
|
dn = self.obj.get_dn(cn)
|
|
|
|
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, ['accesstime'])
|
|
|
|
try:
|
|
|
|
entry_attrs.setdefault('accesstime', []).remove(
|
|
|
|
options['accesstime']
|
|
|
|
)
|
|
|
|
ldap.update_entry(dn, entry_attrs)
|
|
|
|
except (ValueError, errors.EmptyModlist):
|
|
|
|
pass
|
2010-11-29 08:37:47 -06:00
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(cn)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=True)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
|
|
|
def output_for_cli(self, textui, result, cn, **options):
|
|
|
|
textui.print_name(self.name)
|
|
|
|
textui.print_dashed(
|
|
|
|
'Removed access time "%s" from HBAC rule "%s"' % (
|
|
|
|
options['accesstime'], cn
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
#api.register(hbacrule_remove_accesstime)
|
2009-11-18 09:39:12 -06:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add_user(LDAPAddMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add users and groups to an HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['memberuser']
|
|
|
|
member_count_out = ('%i object added.', '%i objects added.')
|
|
|
|
|
2010-09-15 08:20:14 -05:00
|
|
|
def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
|
2011-09-26 02:20:03 -05:00
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, self.obj.default_attributes)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
2010-09-15 08:20:14 -05:00
|
|
|
if 'usercategory' in entry_attrs and \
|
|
|
|
entry_attrs['usercategory'][0].lower() == 'all':
|
|
|
|
raise errors.MutuallyExclusiveError(reason="users cannot be added when user category='all'")
|
|
|
|
return dn
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_add_user)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_remove_user(LDAPRemoveMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Remove users and groups from an HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['memberuser']
|
|
|
|
member_count_out = ('%i object removed.', '%i objects removed.')
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_remove_user)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add_host(LDAPAddMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add target hosts and hostgroups to an HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['memberhost']
|
|
|
|
member_count_out = ('%i object added.', '%i objects added.')
|
|
|
|
|
2010-09-15 08:20:14 -05:00
|
|
|
def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
|
2011-09-26 02:20:03 -05:00
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, self.obj.default_attributes)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
2010-09-15 08:20:14 -05:00
|
|
|
if 'hostcategory' in entry_attrs and \
|
|
|
|
entry_attrs['hostcategory'][0].lower() == 'all':
|
|
|
|
raise errors.MutuallyExclusiveError(reason="hosts cannot be added when host category='all'")
|
|
|
|
return dn
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_add_host)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_remove_host(LDAPRemoveMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Remove target hosts and hostgroups from an HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['memberhost']
|
|
|
|
member_count_out = ('%i object removed.', '%i objects removed.')
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_remove_host)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add_sourcehost(LDAPAddMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add source hosts and hostgroups from a HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['sourcehost']
|
|
|
|
member_count_out = ('%i object added.', '%i objects added.')
|
|
|
|
|
2010-09-15 08:20:14 -05:00
|
|
|
def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
|
2011-09-26 02:20:03 -05:00
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, self.obj.default_attributes)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
2010-09-15 08:20:14 -05:00
|
|
|
if 'sourcehostcategory' in entry_attrs and \
|
|
|
|
entry_attrs['sourcehostcategory'][0].lower() == 'all':
|
|
|
|
raise errors.MutuallyExclusiveError(reason="source hosts cannot be added when sourcehost category='all'")
|
|
|
|
return dn
|
|
|
|
|
2011-08-17 10:49:49 -05:00
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
completed_external = 0
|
|
|
|
# Sift through the host failures. We assume that these are all
|
|
|
|
# hosts that aren't stored in IPA, aka external hosts.
|
|
|
|
if 'sourcehost' in failed and 'host' in failed['sourcehost']:
|
|
|
|
(dn, entry_attrs_) = ldap.get_entry(dn, ['externalhost'])
|
|
|
|
members = entry_attrs.get('sourcehost', [])
|
|
|
|
external_hosts = entry_attrs_.get('externalhost', [])
|
|
|
|
failed_hosts = []
|
|
|
|
for host in failed['sourcehost']['host']:
|
|
|
|
hostname = host[0].lower()
|
|
|
|
host_dn = self.api.Object['host'].get_dn(hostname)
|
|
|
|
if hostname in external_hosts:
|
|
|
|
failed_hosts.append((hostname, unicode(errors.AlreadyGroupMember())))
|
|
|
|
elif hostname not in external_hosts and host_dn not in members:
|
|
|
|
external_hosts.append(hostname)
|
|
|
|
completed_external += 1
|
|
|
|
else:
|
|
|
|
failed_hosts.append((hostname, unicode(errors.NotFound())))
|
|
|
|
if completed_external:
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, {'externalhost': external_hosts})
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
entry_attrs['externalhost'] = external_hosts
|
|
|
|
failed['sourcehost']['host'] = failed_hosts
|
|
|
|
return (completed + completed_external, dn)
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_add_sourcehost)
|
2009-09-30 09:24:25 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_remove_sourcehost(LDAPRemoveMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Remove source hosts and hostgroups from an HBAC rule.')
|
|
|
|
|
2009-09-30 09:24:25 -05:00
|
|
|
member_attributes = ['sourcehost']
|
|
|
|
member_count_out = ('%i object removed.', '%i objects removed.')
|
|
|
|
|
2011-08-17 10:49:49 -05:00
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
# Run through the host failures and gracefully remove any defined as
|
|
|
|
# as an externalhost.
|
|
|
|
if 'sourcehost' in failed and 'host' in failed['sourcehost']:
|
|
|
|
(dn, entry_attrs_) = ldap.get_entry(dn, ['externalhost'])
|
|
|
|
external_hosts = entry_attrs_.get('externalhost', [])
|
|
|
|
failed_hosts = []
|
|
|
|
completed_external = 0
|
|
|
|
for host in failed['sourcehost']['host']:
|
|
|
|
hostname = host[0].lower()
|
|
|
|
if hostname in external_hosts:
|
|
|
|
external_hosts.remove(hostname)
|
|
|
|
completed_external += 1
|
|
|
|
else:
|
|
|
|
failed_hosts.append(hostname)
|
|
|
|
if completed_external:
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, {'externalhost': external_hosts})
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
failed['sourcehost']['host'] = failed_hosts
|
|
|
|
entry_attrs['externalhost'] = external_hosts
|
|
|
|
return (completed + completed_external, dn)
|
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_remove_sourcehost)
|
2010-05-14 08:37:54 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_add_service(LDAPAddMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add services to an HBAC rule.')
|
|
|
|
|
2010-05-14 08:37:54 -05:00
|
|
|
member_attributes = ['memberservice']
|
|
|
|
member_count_out = ('%i object added.', '%i objects added.')
|
|
|
|
|
2010-09-15 08:20:14 -05:00
|
|
|
def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
|
2011-09-26 02:20:03 -05:00
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, self.obj.default_attributes)
|
|
|
|
except errors.NotFound:
|
|
|
|
self.obj.handle_not_found(*keys)
|
2010-09-15 08:20:14 -05:00
|
|
|
if 'servicecategory' in entry_attrs and \
|
|
|
|
entry_attrs['servicecategory'][0].lower() == 'all':
|
|
|
|
raise errors.MutuallyExclusiveError(reason="services cannot be added when service category='all'")
|
|
|
|
return dn
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_add_service)
|
2010-05-14 08:37:54 -05:00
|
|
|
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
class hbacrule_remove_service(LDAPRemoveMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Remove service and service groups from an HBAC rule.')
|
|
|
|
|
2010-05-14 08:37:54 -05:00
|
|
|
member_attributes = ['memberservice']
|
|
|
|
member_count_out = ('%i object removed.', '%i objects removed.')
|
|
|
|
|
2010-12-21 06:20:18 -06:00
|
|
|
api.register(hbacrule_remove_service)
|