2009-05-12 11:46:14 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# 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-06-16 07:38:27 -05:00
|
|
|
from ipalib.plugins.basegroup import *
|
2009-05-12 11:46:14 -05:00
|
|
|
from ipalib import uuid
|
|
|
|
|
|
|
|
_container_dn = 'cn=ng,cn=alt'
|
|
|
|
_default_attributes = [
|
2009-05-22 05:12:27 -05:00
|
|
|
'cn', 'description', 'member', 'memberUser', 'memberhost','externalhost'
|
2009-05-12 11:46:14 -05:00
|
|
|
]
|
2009-05-22 05:12:27 -05:00
|
|
|
_default_class = 'ipanisnetgroup'
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class netgroup(basegroup):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Netgroup object.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class netgroup_add(basegroup_add):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Create new netgroup.
|
|
|
|
"""
|
|
|
|
takes_options = (
|
|
|
|
Str('nisdomainname?',
|
|
|
|
cli_name='nisdomain',
|
|
|
|
doc='NIS domain name',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
|
|
|
"""
|
|
|
|
Execute the netgroup-add operation.
|
|
|
|
|
|
|
|
The dn should not be passed as a keyword argument as it is constructed
|
|
|
|
by this method.
|
|
|
|
|
|
|
|
Returns the entry as it will be created in LDAP.
|
|
|
|
|
|
|
|
:param cn: The name of the netgroup
|
|
|
|
:param kw: Keyword arguments for the other LDAP attributes.
|
|
|
|
"""
|
|
|
|
assert 'cn' not in kw
|
|
|
|
assert 'dn' not in kw
|
|
|
|
ldap = self.api.Backend.ldap2
|
|
|
|
|
|
|
|
entry_attrs = self.args_options_2_entry(cn, **kw)
|
2009-08-10 15:24:10 -05:00
|
|
|
entry_attrs['objectclass'] = ['top', 'ipaobject', 'ipaassociation', _default_class]
|
2009-05-12 11:46:14 -05:00
|
|
|
entry_attrs.setdefault('nisdomainname', self.api.env.domain)
|
2009-08-10 15:24:10 -05:00
|
|
|
entry_attrs['ipauniqueid'] = str(uuid.uuid1())
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-05-22 05:12:27 -05:00
|
|
|
dn = ldap.make_dn(entry_attrs, 'ipauniqueid', _container_dn)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
ldap.add_entry(dn, entry_attrs)
|
|
|
|
|
|
|
|
return ldap.get_entry(dn, _default_attributes)
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(netgroup_add)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class netgroup_del(basegroup_del):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Delete netgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
filter_class = _default_class
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 09:51:44 -05:00
|
|
|
return super(netgroup_del, self).execute(cn, **kw)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(netgroup_del)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class netgroup_mod(basegroup_mod):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Edit an existing netgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
filter_class = _default_class
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 07:38:27 -05:00
|
|
|
return super(netgroup_mod, self).execute(cn, **kw)
|
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-06-16 07:38:27 -05:00
|
|
|
class netgroup_find(basegroup_find):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Search the groups.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
filter_class = _default_class
|
|
|
|
|
2009-06-24 08:18:34 -05:00
|
|
|
def execute(self, term, **kw):
|
|
|
|
return super(netgroup_find, self).execute(term, **kw)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_find)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class netgroup_show(basegroup_show):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Display netgroup.
|
|
|
|
"""
|
|
|
|
filter_class = _default_class
|
|
|
|
default_attributes = _default_attributes
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 07:38:27 -05:00
|
|
|
return super(netgroup_show, self).execute(cn, **kw)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(netgroup_show)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class netgroup_add_member(basegroup_add_member):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Add members to netgroup.
|
|
|
|
"""
|
|
|
|
default_attributes = _default_attributes
|
|
|
|
container = _container_dn
|
|
|
|
filter_class = _default_class
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
takes_options = basegroup_add_member.takes_options + (
|
2009-05-12 11:46:14 -05:00
|
|
|
List('hosts?',
|
|
|
|
cli_name='hosts',
|
|
|
|
doc='comma-separated list of hosts to add'
|
|
|
|
),
|
|
|
|
List('hostgroups?',
|
|
|
|
cli_name='hostgroups',
|
|
|
|
doc='comma-separated list of host groups to add'
|
|
|
|
),
|
|
|
|
List('netgroups?',
|
|
|
|
cli_name='netgroups',
|
|
|
|
doc='comma-separated list of netgroups to add'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _add_external(self, ldap, completed, members, group_dn):
|
2009-07-02 08:17:50 -05:00
|
|
|
add_failed = {}
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.get_entry(group_dn, ['externalhost'])
|
2009-05-22 05:12:27 -05:00
|
|
|
external_hosts = entry_attrs.get('externalhost', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
e = errors.AlreadyGroupMember()
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
for m in members.keys():
|
2009-05-12 11:46:14 -05:00
|
|
|
m = m.lower()
|
|
|
|
if m not in external_hosts:
|
|
|
|
external_hosts.append(m)
|
|
|
|
completed += 1
|
|
|
|
else:
|
2009-07-02 08:17:50 -05:00
|
|
|
add_failed[m] = 'ERROR: %s' % e.message
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
try:
|
2009-06-24 08:18:34 -05:00
|
|
|
ldap.update_entry(group_dn, {'externalhost': external_hosts})
|
2009-05-12 11:46:14 -05:00
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return (completed, add_failed)
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
|
|
|
"""
|
|
|
|
Execute the group-add-member operation.
|
|
|
|
|
|
|
|
Returns a tuple containing the number of members added
|
|
|
|
and the updated entry.
|
|
|
|
|
|
|
|
:param cn: The group name to add new members to.
|
|
|
|
:param kw: groups is a comma-separated list of groups to add
|
|
|
|
:param kw: users is a comma-separated list of users to add
|
|
|
|
:param kw: hostgroups is a comma-separated list of hostgroups to add
|
|
|
|
"""
|
|
|
|
assert self.container
|
|
|
|
ldap = self.api.Backend.ldap2
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.find_entry_by_attr(
|
|
|
|
'cn', cn, self.filter_class, [''], self.container
|
|
|
|
)
|
2009-05-12 11:46:14 -05:00
|
|
|
to_add = []
|
2009-07-02 08:17:50 -05:00
|
|
|
add_failed = {}
|
2009-05-12 11:46:14 -05:00
|
|
|
completed = 0
|
|
|
|
|
|
|
|
members = kw.get('groups', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'cn', 'ipausergroup',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_group
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('users', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'uid', 'posixaccount',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_user
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
hosts_failed = {}
|
2009-05-12 11:46:14 -05:00
|
|
|
members = kw.get('hosts', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
(to_add, hosts_failed) = find_members(
|
|
|
|
ldap, hosts_failed, members, 'cn', 'ipahost',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_host
|
|
|
|
)
|
|
|
|
|
|
|
|
# If a host is not found we'll consider it an externalHost. It will
|
|
|
|
# be up to the user to handle typos
|
2009-07-02 08:17:50 -05:00
|
|
|
if hosts_failed:
|
|
|
|
(completed, hosts_failed) = self._add_external(
|
|
|
|
ldap, completed, hosts_failed, dn
|
|
|
|
)
|
|
|
|
add_failed.update(hosts_failed)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hostgroups', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'cn', 'ipahostgroup',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_hostgroup
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('netgroups', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
|
|
|
ldap, add_failed, members, 'cn', _default_class,
|
|
|
|
_container_dn
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
return (
|
|
|
|
completed, add_failed, ldap.get_entry(dn, _default_attributes)
|
|
|
|
)
|
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-07-09 12:02:44 -05:00
|
|
|
class netgroup_remove_member(basegroup_remove_member):
|
2009-05-12 11:46:14 -05:00
|
|
|
"""
|
|
|
|
Remove a member from a netgroup.
|
|
|
|
"""
|
|
|
|
default_attributes = _default_attributes
|
|
|
|
container = _container_dn
|
|
|
|
filter_class = _default_class
|
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
takes_options = basegroup_remove_member.takes_options + (
|
2009-05-12 11:46:14 -05:00
|
|
|
List('hosts?',
|
|
|
|
cli_name='hosts',
|
|
|
|
doc='comma-separated list of hosts to remove'
|
|
|
|
),
|
|
|
|
List('hostgroups?',
|
|
|
|
cli_name='hostgroups',
|
|
|
|
doc='comma-separated list of host groups to remove'
|
|
|
|
),
|
|
|
|
List('netgroups?',
|
|
|
|
cli_name='netgroups',
|
|
|
|
doc='comma-separated list of netgroups to remove'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _del_external(self, ldap, completed, members, group_dn):
|
2009-07-02 08:17:50 -05:00
|
|
|
rem_failed = {}
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.get_entry(group_dn, ['externalhost'])
|
2009-05-22 05:12:27 -05:00
|
|
|
external_hosts = entry_attrs.get('externalhost', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
e = errors.NotGroupMember()
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
for m in members.keys():
|
2009-05-12 11:46:14 -05:00
|
|
|
m = m.lower()
|
|
|
|
if m in external_hosts:
|
|
|
|
external_hosts.remove(m)
|
|
|
|
completed += 1
|
|
|
|
else:
|
2009-07-02 08:17:50 -05:00
|
|
|
rem_failed[m] = 'ERROR: %s' % e.message
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
try:
|
2009-06-24 08:18:34 -05:00
|
|
|
ldap.update_entry(group_dn, {'externalhost': external_hosts})
|
2009-05-12 11:46:14 -05:00
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return (completed, rem_failed)
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
|
|
|
"""
|
|
|
|
Execute the group-remove-member operation.
|
|
|
|
|
|
|
|
Returns a tuple containing the number of members removed
|
|
|
|
and the updated entry.
|
|
|
|
|
|
|
|
:param cn: The group name to remove new members to.
|
|
|
|
:param kw: groups is a comma-separated list of groups to remove
|
|
|
|
:param kw: users is a comma-separated list of users to remove
|
|
|
|
:param kw: hostgroups is a comma-separated list of hostgroups to remove
|
|
|
|
"""
|
|
|
|
assert self.container
|
|
|
|
ldap = self.api.Backend.ldap2
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.find_entry_by_attr(
|
|
|
|
'cn', cn, self.filter_class, [''], self.container
|
|
|
|
)
|
2009-05-12 11:46:14 -05:00
|
|
|
to_rem = []
|
2009-07-02 08:17:50 -05:00
|
|
|
rem_failed = {}
|
2009-05-12 11:46:14 -05:00
|
|
|
completed = 0
|
|
|
|
|
|
|
|
members = kw.get('groups', [])
|
|
|
|
(to_rem, rem_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, rem_failed, members, 'cn', 'ipausergroup',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_group
|
|
|
|
)
|
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_rem, rem_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('users', [])
|
|
|
|
(to_rem, rem_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, rem_failed, members, 'uid', 'posixaccount',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_user
|
|
|
|
)
|
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_rem, rem_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
hosts_failed = {}
|
2009-05-12 11:46:14 -05:00
|
|
|
members = kw.get('hosts', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
(to_rem, hosts_failed) = find_members(
|
|
|
|
ldap, hosts_failed, members, 'cn', 'ipahost',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_host
|
|
|
|
)
|
|
|
|
|
|
|
|
# If a host is not found we'll consider it an externalHost. It will
|
|
|
|
# be up to the user to handle typos
|
2009-07-02 08:17:50 -05:00
|
|
|
if hosts_failed:
|
|
|
|
(completed, hosts_failed) = self._del_external(
|
|
|
|
ldap, completed, hosts_failed, dn
|
|
|
|
)
|
|
|
|
rem_failed.update(hosts_failed)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_rem, rem_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hostgroups', [])
|
|
|
|
(to_rem, rem_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, rem_failed, members, 'cn', 'ipahostgroup',
|
2009-05-12 11:46:14 -05:00
|
|
|
self.api.env.container_hostgroup
|
|
|
|
)
|
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_rem, rem_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('netgroups', [])
|
|
|
|
(to_rem, rem_failed) = find_members(
|
|
|
|
ldap, rem_failed, members, 'cn', _default_class,
|
|
|
|
_container_dn
|
|
|
|
)
|
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_rem, rem_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
return (
|
|
|
|
completed, rem_failed, ldap.get_entry(dn, _default_attributes)
|
|
|
|
)
|
2009-05-12 11:46:14 -05:00
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
api.register(netgroup_remove_member)
|
2009-05-12 11:46:14 -05:00
|
|
|
|