2009-05-12 11:46:14 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
2009-08-27 08:52:29 -05:00
|
|
|
# Pavel Zuna <pzuna@redhat.com>
|
2009-05-12 11:46:14 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# 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; version 2 only
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
"""
|
2009-06-16 09:51:44 -05:00
|
|
|
Netgroups
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
from ipalib import api, errors
|
2009-08-27 08:52:29 -05:00
|
|
|
from ipalib.plugins.baseldap import *
|
2010-02-08 06:03:28 -06:00
|
|
|
from ipalib import _, ngettext
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup(LDAPObject):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Netgroup object.
|
|
|
|
"""
|
2009-08-27 08:52:29 -05:00
|
|
|
container_dn = api.env.container_netgroup
|
|
|
|
object_name = 'netgroup'
|
|
|
|
object_name_plural = 'netgroups'
|
|
|
|
object_class = ['ipaobject', 'ipaassociation', 'ipanisnetgroup']
|
2009-10-21 09:12:11 -05:00
|
|
|
default_attributes = ['cn', 'description', 'member', 'memberof']
|
2009-08-27 08:52:29 -05:00
|
|
|
uuid_attribute = 'ipauniqueid'
|
|
|
|
attribute_names = {
|
|
|
|
'cn': 'name',
|
|
|
|
'member user': 'member users',
|
|
|
|
'member group': 'member groups',
|
|
|
|
'member host': 'member hosts',
|
|
|
|
'member hostgroup': 'member hostgroups',
|
|
|
|
'member netgroup': 'member netgroups',
|
|
|
|
'memberof netgroup': 'member of netgroups',
|
|
|
|
'externalhost': 'external hosts',
|
|
|
|
}
|
|
|
|
attribute_members = {
|
|
|
|
'member': ['user', 'group', 'host', 'hostgroup', 'netgroup'],
|
|
|
|
'memberof': ['netgroup'],
|
|
|
|
'externalhost': [],
|
|
|
|
}
|
|
|
|
|
2010-02-08 06:03:28 -06:00
|
|
|
label = _('Net Groups')
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
takes_params = (
|
|
|
|
Str('cn',
|
|
|
|
cli_name='name',
|
2009-12-11 16:37:23 -06:00
|
|
|
label='Netgroup name',
|
2009-08-27 08:52:29 -05:00
|
|
|
doc='netgroup name',
|
|
|
|
primary_key=True,
|
|
|
|
normalizer=lambda value: value.lower(),
|
|
|
|
),
|
|
|
|
Str('description',
|
|
|
|
cli_name='desc',
|
2009-12-11 16:37:23 -06:00
|
|
|
label='Description',
|
2009-08-27 08:52:29 -05:00
|
|
|
doc='netgroup description',
|
|
|
|
),
|
2009-05-12 11:46:14 -05:00
|
|
|
Str('nisdomainname?',
|
|
|
|
cli_name='nisdomain',
|
2009-12-11 16:37:23 -06:00
|
|
|
label='NIS domain name',
|
2009-05-12 11:46:14 -05:00
|
|
|
doc='NIS domain name',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
def get_dn(self, *keys, **kwargs):
|
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = self.backend.find_entry_by_attr(
|
|
|
|
self.primary_key.name, keys[-1], self.object_class, [''],
|
|
|
|
self.container_dn
|
|
|
|
)
|
|
|
|
except errors.NotFound:
|
|
|
|
dn = super(netgroup, self).get_dn(*keys, **kwargs)
|
|
|
|
return dn
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
def get_primary_key_from_dn(self, dn):
|
|
|
|
pkey = self.primary_key.name
|
|
|
|
(dn, entry_attrs) = self.backend.get_entry(dn, [pkey])
|
2009-10-06 08:50:43 -05:00
|
|
|
try:
|
|
|
|
return entry_attrs[pkey][0]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
return ''
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
api.register(netgroup)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_add(LDAPCreate):
|
|
|
|
"""
|
|
|
|
Create new netgroup.
|
|
|
|
"""
|
2009-12-10 09:39:24 -06:00
|
|
|
def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
|
2009-08-27 08:52:29 -05:00
|
|
|
if not dn.startswith('cn='):
|
|
|
|
msg = 'netgroup with name "%s" already exists' % keys[-1]
|
|
|
|
raise errors.DuplicateEntry(message=msg)
|
2009-05-12 11:46:14 -05:00
|
|
|
entry_attrs.setdefault('nisdomainname', self.api.env.domain)
|
2009-08-27 08:52:29 -05:00
|
|
|
dn = ldap.make_dn(
|
|
|
|
entry_attrs, self.obj.uuid_attribute, self.obj.container_dn
|
|
|
|
)
|
|
|
|
return dn
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(netgroup_add)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_del(LDAPDelete):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Delete netgroup.
|
|
|
|
"""
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(netgroup_del)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_mod(LDAPUpdate):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
2009-08-27 08:52:29 -05:00
|
|
|
Modify netgroup.
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_mod)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_find(LDAPSearch):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Search the groups.
|
|
|
|
"""
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_find)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_show(LDAPRetrieve):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Display netgroup.
|
|
|
|
"""
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_show)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_add_member(LDAPAddMember):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Add members to netgroup.
|
|
|
|
"""
|
2009-08-27 08:52:29 -05:00
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
if 'member' in failed and 'host' in failed['member']:
|
|
|
|
(dn, entry_attrs_) = ldap.get_entry(dn, ['externalhost'])
|
|
|
|
members = entry_attrs.get('member', [])
|
|
|
|
external_hosts = entry_attrs_.get('externalhost', [])
|
|
|
|
failed_hosts = []
|
|
|
|
completed_external = 0
|
|
|
|
for host in failed['member']['host']:
|
|
|
|
host = host.lower()
|
|
|
|
host_dn = self.api.Object['host'].get_dn(host)
|
|
|
|
if host not in external_hosts and host_dn not in members:
|
|
|
|
external_hosts.append(host)
|
|
|
|
completed_external += 1
|
|
|
|
else:
|
|
|
|
failed_hosts.append(host)
|
|
|
|
if completed_external:
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, {'externalhost': external_hosts})
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
failed['member']['host'] = failed_hosts
|
|
|
|
entry_attrs['externalhost'] = external_hosts
|
|
|
|
return (completed + completed_external, dn)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_add_member)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-08-27 08:52:29 -05:00
|
|
|
class netgroup_remove_member(LDAPRemoveMember):
|
|
|
|
"""
|
|
|
|
Remove members from netgroup.
|
|
|
|
"""
|
|
|
|
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
|
|
|
|
if 'member' in failed and 'host' in failed['member']:
|
|
|
|
(dn, entry_attrs) = ldap.get_entry(dn, ['externalhost'])
|
|
|
|
external_hosts = entry_attrs.get('externalhost', [])
|
|
|
|
failed_hosts = []
|
|
|
|
completed_external = 0
|
|
|
|
for host in failed['member']['host']:
|
|
|
|
host = host.lower()
|
|
|
|
if host in external_hosts:
|
|
|
|
external_hosts.remove(host)
|
|
|
|
completed_external += 1
|
|
|
|
else:
|
|
|
|
failed_hosts.append(host)
|
|
|
|
if completed_external:
|
|
|
|
try:
|
|
|
|
ldap.update_entry(dn, {'externalhost': external_hosts})
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
failed['member']['host'] = failed_hosts
|
|
|
|
entry_attrs['externalhost'] = external_hosts
|
|
|
|
return (completed + completed_external, dn)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
api.register(netgroup_remove_member)
|