2010-09-27 15:51:28 -05:00
|
|
|
# Authors:
|
|
|
|
# Jr Aquino <jr.aquino@citrixonline.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 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.
|
2010-09-27 15:51:28 -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
|
|
|
|
|
|
|
import platform
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from ipalib import api, errors, util
|
|
|
|
from ipalib import Str
|
|
|
|
from ipalib.plugins.baseldap import *
|
|
|
|
from ipalib import _, ngettext
|
|
|
|
|
|
|
|
__doc__ = _("""
|
2010-09-27 15:51:28 -05:00
|
|
|
Sudo Commands
|
|
|
|
|
|
|
|
Commands used as building blocks for sudo
|
|
|
|
|
|
|
|
EXAMPLES:
|
|
|
|
|
2011-05-04 03:26:18 -05:00
|
|
|
Create a new command
|
2011-01-06 09:26:06 -06:00
|
|
|
ipa sudocmd-add --desc='For reading log files' /usr/bin/less
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
Remove a command
|
|
|
|
ipa sudocmd-del /usr/bin/less
|
|
|
|
|
2011-08-24 21:48:30 -05:00
|
|
|
""")
|
2010-09-27 15:51:28 -05:00
|
|
|
|
2011-08-24 16:27:32 -05:00
|
|
|
topic = ('sudo', _('commands for controlling sudo configuration'))
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
class sudocmd(LDAPObject):
|
|
|
|
"""
|
|
|
|
Sudo Command object.
|
|
|
|
"""
|
|
|
|
container_dn = api.env.container_sudocmd
|
2011-07-12 11:01:25 -05:00
|
|
|
object_name = _('sudo command')
|
|
|
|
object_name_plural = _('sudo commands')
|
2010-09-27 15:51:28 -05:00
|
|
|
object_class = ['ipaobject', 'ipasudocmd']
|
|
|
|
# object_class_config = 'ipahostobjectclasses'
|
|
|
|
search_attributes = [
|
2010-11-02 11:00:40 -05:00
|
|
|
'sudocmd', 'description',
|
2010-09-27 15:51:28 -05:00
|
|
|
]
|
|
|
|
default_attributes = [
|
2011-02-23 16:10:08 -06:00
|
|
|
'sudocmd', 'description', 'memberof',
|
2010-09-27 15:51:28 -05:00
|
|
|
]
|
2011-02-23 16:10:08 -06:00
|
|
|
attribute_members = {
|
|
|
|
'memberof': ['sudocmdgroup'],
|
|
|
|
}
|
2010-09-27 15:51:28 -05:00
|
|
|
uuid_attribute = 'ipauniqueid'
|
2012-03-08 06:55:00 -06:00
|
|
|
rdn_attribute = 'ipauniqueid'
|
2011-02-28 10:44:27 -06:00
|
|
|
label = _('Sudo Commands')
|
2011-07-13 21:10:47 -05:00
|
|
|
label_singular = _('Sudo Command')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
takes_params = (
|
2010-11-02 11:00:40 -05:00
|
|
|
Str('sudocmd',
|
2010-09-27 15:51:28 -05:00
|
|
|
cli_name='command',
|
|
|
|
label=_('Sudo Command'),
|
|
|
|
primary_key=True,
|
|
|
|
),
|
|
|
|
Str('description?',
|
|
|
|
cli_name='desc',
|
|
|
|
label=_('Description'),
|
|
|
|
doc=_('A description of this command'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_dn(self, *keys, **options):
|
|
|
|
if keys[-1].endswith('.'):
|
|
|
|
keys[-1] = keys[-1][:-1]
|
|
|
|
dn = super(sudocmd, self).get_dn(*keys, **options)
|
|
|
|
try:
|
|
|
|
self.backend.get_entry(dn, [''])
|
|
|
|
except errors.NotFound:
|
|
|
|
try:
|
|
|
|
(dn, entry_attrs) = self.backend.find_entry_by_attr(
|
2010-11-02 11:00:40 -05:00
|
|
|
'sudocmd', keys[-1], self.object_class, [''],
|
2013-02-04 02:47:00 -06:00
|
|
|
DN(self.container_dn, api.env.basedn)
|
2010-09-27 15:51:28 -05:00
|
|
|
)
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
return dn
|
|
|
|
|
|
|
|
api.register(sudocmd)
|
|
|
|
|
|
|
|
class sudocmd_add(LDAPCreate):
|
2011-08-30 10:10:27 -05:00
|
|
|
__doc__ = _('Create new Sudo Command.')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
2011-08-30 10:10:27 -05:00
|
|
|
msg_summary = _('Added Sudo Command "%(value)s"')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
api.register(sudocmd_add)
|
|
|
|
|
|
|
|
class sudocmd_del(LDAPDelete):
|
2011-08-30 10:10:27 -05:00
|
|
|
__doc__ = _('Delete Sudo Command.')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
2011-08-30 10:10:27 -05:00
|
|
|
msg_summary = _('Deleted Sudo Command "%(value)s"')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
2012-03-13 07:37:24 -05:00
|
|
|
def pre_callback(self, ldap, dn, *keys, **options):
|
|
|
|
filters = [
|
|
|
|
ldap.make_filter_from_attr(attr, dn)
|
|
|
|
for attr in ('memberallowcmd', 'memberdenycmd')]
|
|
|
|
filter = ldap.combine_filters(filters, ldap.MATCH_ANY)
|
|
|
|
filter = ldap.combine_filters(
|
|
|
|
(filter, ldap.make_filter_from_attr('objectClass', 'ipasudorule')),
|
|
|
|
ldap.MATCH_ALL)
|
|
|
|
dependent_sudorules = []
|
|
|
|
try:
|
|
|
|
entries, truncated = ldap.find_entries(
|
|
|
|
filter, ['cn'],
|
|
|
|
base_dn=DN(api.env.container_sudorule, api.env.basedn))
|
|
|
|
except errors.NotFound:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for entry_dn, entry_attrs in entries:
|
|
|
|
[cn] = entry_attrs['cn']
|
|
|
|
dependent_sudorules.append(cn)
|
|
|
|
|
|
|
|
if dependent_sudorules:
|
|
|
|
raise errors.DependentEntry(
|
|
|
|
key=keys[0], label='sudorule',
|
|
|
|
dependent=', '.join(dependent_sudorules))
|
|
|
|
return dn
|
|
|
|
|
2010-09-27 15:51:28 -05:00
|
|
|
api.register(sudocmd_del)
|
|
|
|
|
|
|
|
class sudocmd_mod(LDAPUpdate):
|
2011-08-30 10:10:27 -05:00
|
|
|
__doc__ = _('Modify Sudo Command.')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
2011-08-30 10:10:27 -05:00
|
|
|
msg_summary = _('Modified Sudo Command "%(value)s"')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
api.register(sudocmd_mod)
|
|
|
|
|
|
|
|
class sudocmd_find(LDAPSearch):
|
2011-08-30 10:10:27 -05:00
|
|
|
__doc__ = _('Search for Sudo Commands.')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
msg_summary = ngettext(
|
2011-08-30 10:10:27 -05:00
|
|
|
'%(count)d Sudo Command matched', '%(count)d Sudo Commands matched', 0
|
2010-09-27 15:51:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
api.register(sudocmd_find)
|
|
|
|
|
|
|
|
class sudocmd_show(LDAPRetrieve):
|
2011-08-30 10:10:27 -05:00
|
|
|
__doc__ = _('Display Sudo Command.')
|
2010-09-27 15:51:28 -05:00
|
|
|
|
|
|
|
api.register(sudocmd_show)
|