2009-05-12 11:45:30 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
2009-08-27 08:52:05 -05:00
|
|
|
# Pavel Zuna <pzuna@redhat.com>
|
2009-05-12 11:45:30 -05:00
|
|
|
#
|
|
|
|
# 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-05-12 11:45:30 -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.plugins.baseldap import *
|
2011-08-30 17:38:22 -05:00
|
|
|
from ipalib import api, Int, _, ngettext, errors
|
2012-02-24 13:39:56 -06:00
|
|
|
from ipalib.plugins.netgroup import NETGROUP_PATTERN, NETGROUP_PATTERN_ERRMSG
|
2011-08-30 17:38:22 -05:00
|
|
|
from ipalib.dn import DN
|
2011-08-24 21:48:30 -05:00
|
|
|
|
|
|
|
__doc__ = _("""
|
2009-05-12 11:45:30 -05:00
|
|
|
Groups of hosts.
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Manage groups of hosts. This is useful for applying access control to a
|
|
|
|
number of hosts by using Host-based Access Control.
|
2010-06-02 13:08:50 -05:00
|
|
|
|
|
|
|
EXAMPLES:
|
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Add a new host group:
|
|
|
|
ipa hostgroup-add --desc="Baltimore hosts" baltimore
|
2010-06-02 13:08:50 -05:00
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Add another new host group:
|
|
|
|
ipa hostgroup-add --desc="Maryland hosts" maryland
|
|
|
|
|
|
|
|
Add members to the hostgroup:
|
2010-06-02 13:08:50 -05:00
|
|
|
ipa hostgroup-add-member --hosts=box1,box2,box3 baltimore
|
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Add a hostgroup as a member of another hostgroup:
|
|
|
|
ipa hostgroup-add-member --hostgroups=baltimore maryland
|
|
|
|
|
|
|
|
Remove a host from the hostgroup:
|
2010-06-02 13:08:50 -05:00
|
|
|
ipa hostgroup-remove-member --hosts=box2 baltimore
|
|
|
|
|
|
|
|
Display a host group:
|
|
|
|
ipa hostgroup-show baltimore
|
|
|
|
|
2010-08-24 22:40:32 -05:00
|
|
|
Delete a hostgroup:
|
2010-06-02 13:08:50 -05:00
|
|
|
ipa hostgroup-del baltimore
|
2011-08-24 21:48:30 -05:00
|
|
|
""")
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup(LDAPObject):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Hostgroup object.
|
|
|
|
"""
|
2009-08-27 08:52:05 -05:00
|
|
|
container_dn = api.env.container_hostgroup
|
2011-07-12 11:01:25 -05:00
|
|
|
object_name = _('host group')
|
|
|
|
object_name_plural = _('host groups')
|
2009-08-27 08:52:05 -05:00
|
|
|
object_class = ['ipaobject', 'ipahostgroup']
|
2010-10-04 16:45:40 -05:00
|
|
|
default_attributes = ['cn', 'description', 'member', 'memberof',
|
2011-02-19 22:09:03 -06:00
|
|
|
'memberindirect', 'memberofindirect',
|
2010-10-04 16:45:40 -05:00
|
|
|
]
|
2009-08-27 08:52:05 -05:00
|
|
|
uuid_attribute = 'ipauniqueid'
|
|
|
|
attribute_members = {
|
|
|
|
'member': ['host', 'hostgroup'],
|
2011-08-30 13:08:34 -05:00
|
|
|
'memberof': ['hostgroup', 'netgroup', 'hbacrule', 'sudorule'],
|
2010-10-04 16:45:40 -05:00
|
|
|
'memberindirect': ['host', 'hostgroup'],
|
2011-06-24 14:18:35 -05:00
|
|
|
'memberofindirect': ['hostgroup', 'hbacrule', 'sudorule'],
|
2009-08-27 08:52:05 -05:00
|
|
|
}
|
|
|
|
|
2010-02-08 06:03:28 -06:00
|
|
|
label = _('Host Groups')
|
2011-07-13 21:10:47 -05:00
|
|
|
label_singular = _('Host Group')
|
2010-02-08 06:03:28 -06:00
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
takes_params = (
|
|
|
|
Str('cn',
|
2012-02-24 13:39:56 -06:00
|
|
|
pattern=NETGROUP_PATTERN,
|
|
|
|
pattern_errmsg=NETGROUP_PATTERN_ERRMSG,
|
2010-12-02 15:29:26 -06:00
|
|
|
cli_name='hostgroup_name',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Host-group'),
|
|
|
|
doc=_('Name of host-group'),
|
2009-08-27 08:52:05 -05:00
|
|
|
primary_key=True,
|
|
|
|
normalizer=lambda value: value.lower(),
|
|
|
|
),
|
|
|
|
Str('description',
|
|
|
|
cli_name='desc',
|
2010-02-19 10:08:16 -06:00
|
|
|
label=_('Description'),
|
|
|
|
doc=_('A description of this host-group'),
|
2009-08-27 08:52:05 -05:00
|
|
|
),
|
|
|
|
)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def suppress_netgroup_memberof(self, dn, entry_attrs):
|
|
|
|
"""
|
|
|
|
We don't want to show managed netgroups so remove them from the
|
|
|
|
memberOf list.
|
|
|
|
"""
|
|
|
|
if 'memberof' in entry_attrs:
|
|
|
|
hgdn = DN(dn)
|
|
|
|
for member in entry_attrs['memberof']:
|
|
|
|
ngdn = DN(member)
|
|
|
|
if ngdn['cn'] == hgdn['cn']:
|
|
|
|
try:
|
|
|
|
netgroup = api.Command['netgroup_show'](ngdn['cn'], all=True)['result']
|
|
|
|
if self.has_objectclass(netgroup['objectclass'], 'mepmanagedentry'):
|
|
|
|
entry_attrs['memberof'].remove(member)
|
|
|
|
return
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_add(LDAPCreate):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add a new hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-12-15 14:36:14 -06:00
|
|
|
msg_summary = _('Added hostgroup "%(value)s"')
|
|
|
|
|
2011-10-05 09:18:16 -05:00
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
|
|
|
try:
|
2011-10-17 07:26:13 -05:00
|
|
|
# check duplicity with hostgroups first to provide proper error
|
|
|
|
netgroup = api.Command['hostgroup_show'](keys[-1])
|
|
|
|
self.obj.handle_duplicate_entry(*keys)
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
# when enabled, a managed netgroup is created for every hostgroup
|
|
|
|
# make sure that the netgroup can be created
|
2011-10-05 09:18:16 -05:00
|
|
|
netgroup = api.Command['netgroup_show'](keys[-1])
|
|
|
|
raise errors.DuplicateEntry(message=unicode(_(\
|
2011-10-17 07:26:13 -05:00
|
|
|
u'netgroup with name "%s" already exists. ' \
|
|
|
|
u'Hostgroups and netgroups share a common namespace'\
|
|
|
|
) % keys[-1]))
|
2011-10-05 09:18:16 -05:00
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return dn
|
|
|
|
|
2011-07-01 14:32:31 -05:00
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
2011-08-30 17:38:22 -05:00
|
|
|
# Always wait for the associated netgroup to be created so we can
|
|
|
|
# be sure to ignore it in memberOf
|
|
|
|
newentry = wait_for_value(ldap, dn, 'objectclass', 'mepOriginEntry')
|
|
|
|
entry_from_entry(entry_attrs, newentry)
|
|
|
|
self.obj.suppress_netgroup_memberof(dn, entry_attrs)
|
2011-07-01 14:32:31 -05:00
|
|
|
|
|
|
|
return dn
|
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(hostgroup_add)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_del(LDAPDelete):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Delete a hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-12-15 14:36:14 -06:00
|
|
|
msg_summary = _('Deleted hostgroup "%(value)s"')
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(hostgroup_del)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_mod(LDAPUpdate):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Modify a hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-12-15 14:36:14 -06:00
|
|
|
msg_summary = _('Modified hostgroup "%(value)s"')
|
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
|
|
|
self.obj.suppress_netgroup_memberof(dn, entry_attrs)
|
|
|
|
return dn
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_mod)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_find(LDAPSearch):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Search for hostgroups.')
|
|
|
|
|
2011-01-04 14:15:54 -06:00
|
|
|
member_attributes = ['member', 'memberof']
|
2009-12-15 14:36:14 -06:00
|
|
|
msg_summary = ngettext(
|
2011-02-23 15:47:49 -06:00
|
|
|
'%(count)d hostgroup matched', '%(count)d hostgroups matched', 0
|
2009-12-15 14:36:14 -06:00
|
|
|
)
|
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def post_callback(self, ldap, entries, truncated, *args, **options):
|
2011-10-26 04:12:38 -05:00
|
|
|
if options.get('pkey_only', False):
|
|
|
|
return
|
2011-08-30 17:38:22 -05:00
|
|
|
for entry in entries:
|
|
|
|
(dn, entry_attrs) = entry
|
|
|
|
self.obj.suppress_netgroup_memberof(dn, entry_attrs)
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_find)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_show(LDAPRetrieve):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Display information about a hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
|
|
|
|
self.obj.suppress_netgroup_memberof( dn, entry_attrs)
|
|
|
|
return dn
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_show)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_add_member(LDAPAddMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Add members to a hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
self.obj.suppress_netgroup_memberof(dn, entry_attrs)
|
|
|
|
return (completed, dn)
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_add_member)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:05 -05:00
|
|
|
class hostgroup_remove_member(LDAPRemoveMember):
|
2011-08-24 21:48:30 -05:00
|
|
|
__doc__ = _('Remove members from a hostgroup.')
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2011-08-30 17:38:22 -05:00
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
self.obj.suppress_netgroup_memberof(dn, entry_attrs)
|
|
|
|
return (completed, dn)
|
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
api.register(hostgroup_remove_member)
|