示例#1
0
    def execute(self, *args, **kw):
        dn = self.obj.get_dn(*args, **kw)
        entry = self.obj.backend.get_entry(
            dn, ['objectclass', 'primarymail', 'cansendexternally'])

        if 'mailsenderentity' in entry['objectclass']:
            try:
                del entry['primarymail']
            except KeyError:
                pass

            try:
                del entry['cansendexternally']
            except KeyError:
                pass

            while 'mailsenderentity' in entry['objectclass']:
                entry['objectclass'].remove('mailsenderentity')
        else:
            raise errors.AlreadyInactive()

        self.obj.backend.update_entry(entry)

        return dict(
            result=True,
            value=pkey_to_value(args[0], kw),
        )
示例#2
0
    def set_entry_active(self, dn, active):
        """Mark entry active/inactive."""

        assert isinstance(dn, DN)
        assert isinstance(active, bool)

        # get the entry in question
        entry_attrs = self.get_entry(dn, ['nsaccountlock'])

        # check nsAccountLock attribute
        account_lock_attr = entry_attrs.get('nsaccountlock', ['false'])
        account_lock_attr = account_lock_attr[0].lower()
        if active:
            if account_lock_attr == 'false':
                raise errors.AlreadyActive()
        else:
            if account_lock_attr == 'true':
                raise errors.AlreadyInactive()

        # LDAP expects string instead of Bool but it also requires it to be TRUE or FALSE,
        # not True or False as Python stringification does. Thus, we uppercase it.
        account_lock_attr = str(not active).upper()

        entry_attrs['nsaccountlock'] = account_lock_attr
        self.update_entry(entry_attrs)
示例#3
0
    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)
        entry_attrs = ldap.get_entry(dn, ['usercertificate'])

        check_required_principal(ldap, keys[-1])

        # See if we do any work at all here and if not raise an exception
        done_work = False

        if self.api.Command.ca_is_enabled()['result']:
            certs = self.api.Command.cert_find(service=keys)['result']

            if len(certs) > 0:
                revoke_certs(certs)
                # Remove the usercertificate altogether
                entry_attrs['usercertificate'] = None
                ldap.update_entry(entry_attrs)
                done_work = True

        self.obj.get_password_attributes(ldap, dn, entry_attrs)
        if entry_attrs['has_keytab']:
            ldap.remove_principal_key(dn)
            done_work = True

        if not done_work:
            raise errors.AlreadyInactive()

        return dict(
            result=True,
            value=pkey_to_value(keys[0], options),
        )
示例#4
0
class service_disable(LDAPQuery):
    __doc__ = _('Disable the Kerberos key and SSL certificate of a service.')

    has_output = output.standard_value
    msg_summary = _('Disabled service "%(value)s"')
    has_output_params = LDAPQuery.has_output_params + output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)
        entry_attrs = ldap.get_entry(dn, ['usercertificate'])

        (service, hostname, realm) = split_principal(keys[-1])
        check_required_principal(ldap, hostname, service)

        # See if we do any work at all here and if not raise an exception
        done_work = False

        if 'usercertificate' in entry_attrs:
            if self.api.Command.ca_is_enabled()['result']:
                cert = x509.normalize_certificate(entry_attrs.get('usercertificate')[0])
                try:
                    serial = unicode(x509.get_serial_number(cert, x509.DER))
                    try:
                        result = api.Command['cert_show'](unicode(serial))['result']
                        if 'revocation_reason' not in result:
                            try:
                                api.Command['cert_revoke'](unicode(serial), revocation_reason=4)
                            except errors.NotImplementedError:
                                # some CA's might not implement revoke
                                pass
                    except errors.NotImplementedError:
                        # some CA's might not implement revoke
                        pass
                except NSPRError, nsprerr:
                    if nsprerr.errno == -8183:
                        # If we can't decode the cert them proceed with
                        # disabling the service
                        self.log.info("Problem decoding certificate %s" % nsprerr.args[1])
                    else:
                        raise nsprerr

            # Remove the usercertificate altogether
            entry_attrs['usercertificate'] = None
            ldap.update_entry(entry_attrs)
            done_work = True

        self.obj.get_password_attributes(ldap, dn, entry_attrs)
        if entry_attrs['has_keytab']:
            ldap.remove_principal_key(dn)
            done_work = True

        if not done_work:
            raise errors.AlreadyInactive()

        return dict(
            result=True,
            value=pkey_to_value(keys[0], options),
        )
示例#5
0
    def execute(self, cn):
        ldap = self.obj.backend

        dn = self.obj.get_dn(cn)
        entry_attrs = {'ipaenabledflag': 'FALSE'}

        try:
            ldap.update_entry(dn, entry_attrs)
        except errors.EmptyModlist:
            raise errors.AlreadyInactive()
        except errors.NotFound:
            self.obj.handle_not_found(cn)

        return dict(
            result=True,
            value=cn,
        )
示例#6
0
    def execute(self, cn, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(cn)
        try:
            entry_attrs = ldap.get_entry(dn, ['ipaenabledflag'])
        except errors.NotFound:
            raise self.obj.handle_not_found(cn)

        entry_attrs['ipaenabledflag'] = ['FALSE']

        try:
            ldap.update_entry(entry_attrs)
        except errors.EmptyModlist:
            raise errors.AlreadyInactive()

        return dict(
            result=True,
            value=pkey_to_value(cn, options),
        )
    def execute(self, *args, **kw):
        dn = self.obj.get_dn(*args, **kw)
        entry = self.obj.backend.get_entry(dn, ['objectclass', 'alias'])

        if 'mailenabledgroup' in [o.lower() for o in entry['objectclass']]:
            try:
                del entry['alias']
            except KeyError:
                pass

            entry['objectclass'].remove('mailenabledgroup')
        else:
            raise errors.AlreadyInactive()

        self.obj.backend.update_entry(entry)

        return dict(
            result=True,
            value=pkey_to_value(args[0], kw),
        )
class test_selinuxusermap(Declarative):
    cleanup_commands = [
        ('selinuxusermap_del', [rule1], {}),
        ('group_del', [group1], {}),
        ('user_del', [user1], {}),
        ('host_del', [host1], {}),
        ('hbacrule_del', [hbacrule1], {}),
        ('hbacrule_del', [hbacrule2], {}),
    ]

    tests = [

        dict(
            desc='Try to retrieve non-existent %r' % rule1,
            command=('selinuxusermap_show', [rule1], {}),
            expected=errors.NotFound(
                reason=u'%s: SELinux User Map rule not found' % rule1),
        ),


        dict(
            desc='Try to update non-existent %r' % rule1,
            command=('selinuxusermap_mod', [rule1], dict(description=u'Foo')),
            expected=errors.NotFound(
                reason=u'%s: SELinux User Map rule not found' % rule1),
        ),


        dict(
            desc='Try to delete non-existent %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=errors.NotFound(
                reason=u'%s: SELinux User Map rule not found' % rule1),
        ),


        dict(
            desc='Create rule %r' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1)
            ),
            expected=dict(
                value=rule1,
                summary=u'Added SELinux User Map "%s"' % rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    objectclass=objectclasses.selinuxusermap,
                    ipauniqueid=[fuzzy_uuid],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            ),
        ),


        dict(
            desc='Try to create duplicate %r' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1)
            ),
            expected=errors.DuplicateEntry(message=u'SELinux User Map rule ' +
                u'with name "%s" already exists' % rule1),
        ),


        dict(
            desc='Retrieve rule %r' % rule1,
            command=('selinuxusermap_show', [rule1], {}),
            expected=dict(
                value=rule1,
                summary=None,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            ),
        ),


        dict(
            desc='Update rule %r' % rule1,
            command=(
                'selinuxusermap_mod', [rule1],
                    dict(ipaselinuxuser=selinuxuser2)
            ),
            expected=dict(
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                ),
                summary=u'Modified SELinux User Map "%s"' % rule1,
                value=rule1,
            ),
        ),


        dict(
            desc='Retrieve %r to verify update' % rule1,
            command=('selinuxusermap_show', [rule1], {}),
            expected=dict(
                value=rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
                summary=None,
            ),
        ),


        dict(
            desc='Search for rule %r' % rule1,
            command=('selinuxusermap_find', [], dict(cn=rule1)),
            expected=dict(
                count=1,
                truncated=False,
                result=[
                    dict(
                        cn=[rule1],
                        ipaselinuxuser=[selinuxuser2],
                        ipaenabledflag=[u'TRUE'],
                        dn=fuzzy_selinuxusermapdn,
                    ),
                ],
                summary=u'1 SELinux User Map matched',
            ),
        ),


        ###############
        # Create additional entries needed for testing
        dict(
            desc='Create %r' % user1,
            command=(
                'user_add', [], dict(givenname=u'Test', sn=u'User1')
            ),
            expected=dict(
                value=user1,
                summary=u'Added user "%s"' % user1,
                result=get_user_result(user1, u'Test', u'User1', 'add'),
            ),
        ),

        dict(
            desc='Create group %r' % group1,
            command=(
                'group_add', [group1], dict(description=u'Test desc 1')
            ),
            expected=dict(
                value=group1,
                summary=u'Added group "%s"' % group1,
                result=dict(
                    cn=[group1],
                    description=[u'Test desc 1'],
                    gidnumber=[fuzzy_digits],
                    objectclass=objectclasses.group + [u'posixgroup'],
                    ipauniqueid=[fuzzy_uuid],
                    dn=DN(('cn', group1), ('cn', 'groups'), ('cn', 'accounts'),
                          api.env.basedn),
                ),
            ),
        ),


        dict(
            desc='Add member %r to %r' % (user1, group1),
            command=(
                'group_add_member', [group1], dict(user=user1)
            ),
            expected=dict(
                completed=1,
                failed=dict(
                    member=dict(
                        group=tuple(),
                        user=tuple(),
                    ),
                ),
                result={
                        'dn': DN(('cn', group1), ('cn', 'groups'),
                            ('cn', 'accounts'), api.env.basedn),
                        'member_user': (user1,),
                        'gidnumber': [fuzzy_digits],
                        'cn': [group1],
                        'description': [u'Test desc 1'],
                },
            ),
        ),


        dict(
            desc='Create host %r' % host1,
            command=('host_add', [host1],
                dict(
                    description=u'Test host 1',
                    l=u'Undisclosed location 1',
                    force=True,
                ),
            ),
            expected=dict(
                value=host1,
                summary=u'Added host "%s"' % host1,
                result=dict(
                    dn=hostdn1,
                    fqdn=[host1],
                    description=[u'Test host 1'],
                    l=[u'Undisclosed location 1'],
                    krbprincipalname=[u'host/%s@%s' % (host1, api.env.realm)],
                    krbcanonicalname=[u'host/%s@%s' % (host1, api.env.realm)],
                    objectclass=objectclasses.host,
                    ipauniqueid=[fuzzy_uuid],
                    managedby_host=[host1],
                    has_keytab=False,
                    has_password=False,
                ),
            ),
        ),


        dict(
            desc='Create HBAC rule %r' % hbacrule1,
            command=(
                'hbacrule_add', [hbacrule1], {}
            ),
            expected=dict(
                value=hbacrule1,
                summary=u'Added HBAC rule "%s"' % hbacrule1,
                result=dict(
                    cn=[hbacrule1],
                    objectclass=objectclasses.hbacrule,
                    ipauniqueid=[fuzzy_uuid],
                    accessruletype=[u'allow'],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_hbacruledn,
                ),
            ),
        ),


        dict(
            desc='Create HBAC rule %r' % hbacrule2,
            command=(
                'hbacrule_add', [hbacrule2], {}
            ),
            expected=dict(
                value=hbacrule2,
                summary=u'Added HBAC rule "%s"' % hbacrule2,
                result=dict(
                    cn=[hbacrule2],
                    objectclass=objectclasses.hbacrule,
                    ipauniqueid=[fuzzy_uuid],
                    accessruletype=[u'allow'],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_hbacruledn,
                ),
            ),
        ),


        ###############
        # Fill out rule with members and/or pointers to HBAC rules
        dict(
            desc='Add user to %r' % rule1,
            command=('selinuxusermap_add_user', [rule1], dict(user=user1)),
            expected=dict(
                failed=dict(memberuser=dict(group=[], user=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    memberuser_user=[user1],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Add non-existent user to %r' % rule1,
            command=('selinuxusermap_add_user', [rule1],
                dict(user=u'notfound')),
            expected=dict(
                failed=dict(
                    memberuser=dict(group=[],
                                    user=[(u'notfound', u'no such entry')])
                        ),
                completed=0,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    memberuser_user=[user1],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Remove user from %r' % rule1,
            command=('selinuxusermap_remove_user', [rule1], dict(user=user1)),
            expected=dict(
                failed=dict(memberuser=dict(group=[], user=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Remove non-existent user to %r' % rule1,
            command=('selinuxusermap_remove_user', [rule1],
                dict(user=u'notfound')),
            expected=dict(
                failed=dict(
                    memberuser=dict(group=[],
                        user=[(u'notfound', u'This entry is not a member')]
                            )
                        ),
                completed=0,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Add group to %r' % rule1,
            command=('selinuxusermap_add_user', [rule1], dict(group=group1)),
            expected=dict(
                failed=dict(memberuser=dict(group=[], user=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    memberuser_group=[group1],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Add host to %r' % rule1,
            command=('selinuxusermap_add_host', [rule1], dict(host=host1)),
            expected=dict(
                failed=dict(memberhost=dict(hostgroup=[], host=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    memberhost_host=[host1],
                    memberuser_group=[group1],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        ###############
        # Test enabling and disabling
        dict(
            desc='Disable %r' % rule1,
            command=('selinuxusermap_disable', [rule1], {}),
            expected=dict(
                result=True,
                value=rule1,
                summary=u'Disabled SELinux User Map "%s"' % rule1,
            )
        ),


        dict(
            desc='Disable %r again' % rule1,
            command=('selinuxusermap_disable', [rule1], {}),
            expected=errors.AlreadyInactive(),
        ),


        dict(
            desc='Enable %r' % rule1,
            command=('selinuxusermap_enable', [rule1], {}),
            expected=dict(
                result=True,
                value=rule1,
                summary=u'Enabled SELinux User Map "%s"' % rule1,
            )
        ),


        dict(
            desc='Re-enable %r again' % rule1,
            command=('selinuxusermap_enable', [rule1], {}),
            expected=errors.AlreadyActive(),
        ),


        # Point to an HBAC Rule
        dict(
            desc='Add an HBAC rule to %r that has other members' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(seealso=hbacrule1)
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),


        dict(
            desc='Remove host from %r' % rule1,
            command=('selinuxusermap_remove_host', [rule1], dict(host=host1)),
            expected=dict(
                failed=dict(memberhost=dict(hostgroup=[], host=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    memberuser_group=[group1],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Remove group from %r' % rule1,
            command=('selinuxusermap_remove_user', [rule1],
                dict(group=group1)),
            expected=dict(
                failed=dict(memberuser=dict(group=[], user=[])),
                completed=1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            )
        ),


        dict(
            desc='Add non-existent HBAC rule to %r' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(seealso=u'notfound')
            ),
            expected=errors.NotFound(
                reason=u'HBAC rule notfound not found'),
        ),


        dict(
            desc='Add an HBAC rule to %r' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(seealso=hbacrule1)
            ),
            expected=dict(
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser2],
                    ipaenabledflag=[u'TRUE'],
                    seealso=hbacrule1,
                ),
                summary=u'Modified SELinux User Map "%s"' % rule1,
                value=rule1,
            ),
        ),


        dict(
            desc='Add user to %r that has HBAC' % rule1,
            command=('selinuxusermap_add_user', [rule1], dict(user=user1)),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),


        dict(
            desc='Add host to %r that has HBAC' % rule1,
            command=('selinuxusermap_add_host', [rule1], dict(host=host1)),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),


        dict(
            desc='Try to delete HBAC rule pointed to by %r' % rule1,
            command=('hbacrule_del', [hbacrule1], {}),
            expected=errors.DependentEntry(key=hbacrule1,
                label=u'SELinux User Map', dependent=rule1)
        ),


        # This tests selinuxusermap-find --hbacrule=<foo> returns an
        # exact match
        dict(
            desc='Try to delete similarly named HBAC rule %r' % hbacrule2,
            command=('hbacrule_del', [hbacrule2], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[hbacrule2],
                summary=u'Deleted HBAC rule "%s"' % hbacrule2,
            )
        ),


        # Test clean up
        dict(
            desc='Delete %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[rule1],
                summary=u'Deleted SELinux User Map "%s"' % rule1,
            )
        ),


        dict(
            desc='Try to delete non-existent %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=errors.NotFound(
                reason=u'%s: SELinux User Map rule not found' % rule1),
        ),


        # Some negative tests
        dict(
            desc='Create rule with unknown user %r' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=u'notfound:s0:c0')
            ),
            expected=errors.NotFound(reason=u'SELinux user notfound:s0:c0 ' +
                u'not found in ordering list (in config)'),
        ),


        dict(
            desc='Create rule with invalid user bad+user',
            command=(
                'selinuxusermap_add', [rule1], dict(ipaselinuxuser=u'bad+user')
            ),
            expected=errors.ValidationError(
                name='selinuxuser',
                error=u'Invalid SELinux user name, only a-Z, _ '
                      'and . are allowed'
            ),
        ),


        dict(
            desc='Create rule with invalid MCS xguest_u:s999',
            command=(
                'selinuxusermap_add', [rule1],
                     dict(ipaselinuxuser=u'xguest_u:s999')
            ),
            expected=errors.ValidationError(name='selinuxuser',
                error=u'Invalid MLS value, must match s[0-15](-s[0-15])'),
        ),


        dict(
            desc='Create rule with invalid MLS xguest_u:s0:p88',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=u'xguest_u:s0:p88')
            ),
            expected=errors.ValidationError(name='selinuxuser',
                error=u'Invalid MCS value, must match c[0-1023].c[0-1023] ' +
                    u'and/or c[0-1023]-c[0-c0123]'),
        ),


        dict(
            desc='Create rule with invalid MLS xguest_u:s0:c0.c1028',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=u'xguest_u:s0-s0:c0.c1028')
            ),
            expected=errors.ValidationError(name='selinuxuser',
                error=u'Invalid MCS value, must match c[0-1023].c[0-1023] ' +
                    u'and/or c[0-1023]-c[0-c0123]'),
        ),


        dict(
            desc='Create rule with invalid user via setattr',
            command=(
                'selinuxusermap_mod', [rule1],
                    dict(setattr=u'ipaselinuxuser=deny')
            ),
            expected=errors.ValidationError(name='ipaselinuxuser',
                error=u'Invalid MLS value, must match s[0-15](-s[0-15])'),
        ),

        dict(
            desc='Create rule with both --hbacrule and --usercat set',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1,
                         seealso=hbacrule1,
                         usercategory=u'all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Create rule with both --hbacrule and --hostcat set',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1,
                         seealso=hbacrule1,
                         hostcategory=u'all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Create rule with both --hbacrule '
                 'and --usercat set via setattr',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1,
                         seealso=hbacrule1,
                         setattr=u'usercategory=all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Create rule with both --hbacrule '
                 'and --hostcat set via setattr',
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1,
                         seealso=hbacrule1,
                         setattr=u'hostcategory=all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Create rule %r with --hbacrule' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                dict(ipaselinuxuser=selinuxuser1, seealso=hbacrule1)
            ),
            expected=dict(
                value=rule1,
                summary=u'Added SELinux User Map "%s"' % rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    objectclass=objectclasses.selinuxusermap,
                    ipauniqueid=[fuzzy_uuid],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                    seealso=hbacrule1
                ),
            ),
        ),

        dict(
            desc='Add an --usercat to %r that has HBAC set' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(usercategory=u'all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Add an --hostcat to %r that has HBAC set' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(hostcategory=u'all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Add an usercat via setattr to %r that has HBAC set' % rule1,
            command=(
                'selinuxusermap_mod', [rule1],
                dict(setattr=u'usercategory=all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Add an hostcat via setattr to %r that has HBAC set' % rule1,
            command=(
                'selinuxusermap_mod', [rule1],
                dict(setattr=u'hostcategory=all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Delete %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[rule1],
                summary=u'Deleted SELinux User Map "%s"' % rule1,
            )
        ),

        dict(
            desc='Create rule %r with usercat and hostcat set' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                    dict(ipaselinuxuser=selinuxuser1,
                         usercategory=u'all',
                         hostcategory=u'all')
            ),
            expected=dict(
                value=rule1,
                summary=u'Added SELinux User Map "%s"' % rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    objectclass=objectclasses.selinuxusermap,
                    ipauniqueid=[fuzzy_uuid],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                    usercategory=[u'all'],
                    hostcategory=[u'all']
                ),
            ),
        ),

        dict(
            desc='Add HBAC rule to %r that has usercat and hostcat' % rule1,
            command=(
                'selinuxusermap_mod', [rule1], dict(seealso=hbacrule1)
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Delete %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[rule1],
                summary=u'Deleted SELinux User Map "%s"' % rule1,
            )
        ),

        dict(
            desc='Create rule %r' % rule1,
            command=(
                'selinuxusermap_add', [rule1],
                dict(ipaselinuxuser=selinuxuser1)
            ),
            expected=dict(
                value=rule1,
                summary=u'Added SELinux User Map "%s"' % rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    objectclass=objectclasses.selinuxusermap,
                    ipauniqueid=[fuzzy_uuid],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                ),
            ),
        ),

        dict(
            desc='Add HBAC rule, hostcat and usercat to %r' % rule1,
            command=(
                'selinuxusermap_mod', [rule1],
                    dict(seealso=hbacrule1,
                         usercategory=u'all',
                         hostcategory=u'all')
            ),
            expected=errors.MutuallyExclusiveError(
                reason=u'HBAC rule and local members cannot both be set'),
        ),

        dict(
            desc='Delete %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[rule1],
                summary=u'Deleted SELinux User Map "%s"' % rule1,
            )
        ),

       dict(
            desc='Create rule %r with '
                 '--setattr=seealso=<allow_all rule DN>' % rule1,
            command=(
                'selinuxusermap_add',
                [rule1],
                dict(ipaselinuxuser=selinuxuser1,
                     setattr=u'seealso=%s' % allow_all_rule_dn)
            ),
            expected=dict(
                value=rule1,
                summary=u'Added SELinux User Map "%s"' % rule1,
                result=dict(
                    cn=[rule1],
                    ipaselinuxuser=[selinuxuser1],
                    objectclass=objectclasses.selinuxusermap,
                    ipauniqueid=[fuzzy_uuid],
                    ipaenabledflag=[u'TRUE'],
                    dn=fuzzy_selinuxusermapdn,
                    seealso=u'allow_all',
                ),
            ),
        ),

        dict(
            desc='Delete %r' % rule1,
            command=('selinuxusermap_del', [rule1], {}),
            expected=dict(
                result=dict(failed=[]),
                value=[rule1],
                summary=u'Deleted SELinux User Map "%s"' % rule1,
            )
        ),
    ]
示例#9
0
class host_disable(LDAPQuery):
    __doc__ = _('Disable the Kerberos key, SSL certificate and all services of a host.')

    has_output = output.standard_value
    msg_summary = _('Disabled host "%(value)s"')

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        # If we aren't given a fqdn, find it
        if _hostname_validator(None, keys[-1]) is not None:
            hostentry = api.Command['host_show'](keys[-1])['result']
            fqdn = hostentry['fqdn'][0]
        else:
            fqdn = keys[-1]

        host_is_master(ldap, fqdn)

        # See if we actually do anthing here, and if not raise an exception
        done_work = False

        dn = self.obj.get_dn(*keys, **options)
        try:
            (dn, entry_attrs) = ldap.get_entry(dn, ['usercertificate'])
        except errors.NotFound:
            self.obj.handle_not_found(*keys)

        truncated = True
        while truncated:
            try:
                ret = api.Command['service_find'](fqdn)
                truncated = ret['truncated']
                services = ret['result']
            except errors.NotFound:
                break
            else:
                for entry_attrs in services:
                    principal = entry_attrs['krbprincipalname'][0]
                    (service, hostname, realm) = split_principal(principal)
                    if hostname.lower() == fqdn:
                        try:
                            api.Command['service_disable'](principal)
                            done_work = True
                        except errors.AlreadyInactive:
                            pass
        if 'usercertificate' in entry_attrs:
            cert = x509.normalize_certificate(entry_attrs.get('usercertificate')[0])
            try:
                serial = unicode(x509.get_serial_number(cert, x509.DER))
                try:
                    result = api.Command['cert_show'](unicode(serial))['result']
                    if 'revocation_reason' not in result:
                        try:
                            api.Command['cert_revoke'](unicode(serial), revocation_reason=4)
                        except errors.NotImplementedError:
                            # some CA's might not implement revoke
                            pass
                except errors.NotImplementedError:
                    # some CA's might not implement revoke
                    pass
            except NSPRError, nsprerr:
                if nsprerr.errno == -8183:
                    # If we can't decode the cert them proceed with
                    # disabling the host.
                    self.log.info("Problem decoding certificate %s" % nsprerr.args[1])
                else:
                    raise nsprerr

            # Remove the usercertificate altogether
            ldap.update_entry(dn, {'usercertificate': None})
            done_work = True

        self.obj.get_password_attributes(ldap, dn, entry_attrs)
        if entry_attrs['has_keytab']:
            ldap.remove_principal_key(dn)
            done_work = True

        if not done_work:
            raise errors.AlreadyInactive()

        return dict(
            result=True,
            value=keys[0],
        )
示例#10
0
    def execute(self, *keys, **options):
        ldap = self.obj.backend

        # If we aren't given a fqdn, find it
        if _hostname_validator(None, keys[-1]) is not None:
            hostentry = api.Command['host_show'](keys[-1])['result']
            fqdn = hostentry['fqdn'][0]
        else:
            fqdn = keys[-1]

        host_is_master(ldap, fqdn)

        # See if we actually do anthing here, and if not raise an exception
        done_work = False

        truncated = True
        while truncated:
            try:
                ret = api.Command['service_find'](fqdn)
                truncated = ret['truncated']
                services = ret['result']
            except errors.NotFound:
                break
            else:
                for entry_attrs in services:
                    principal = entry_attrs['krbprincipalname'][0]
                    (service, hostname, realm) = split_principal(principal)
                    if hostname.lower() == fqdn:
                        try:
                            api.Command['service_disable'](principal)
                            done_work = True
                        except errors.AlreadyInactive:
                            pass

        dn = self.obj.get_dn(*keys, **options)
        try:
            entry_attrs = ldap.get_entry(dn, ['usercertificate'])
        except errors.NotFound:
            self.obj.handle_not_found(*keys)
        if self.api.Command.ca_is_enabled()['result']:
            certs = entry_attrs.get('usercertificate', [])

            if certs:
                revoke_certs(certs, self.log)
                # Remove the usercertificate altogether
                entry_attrs['usercertificate'] = None
                ldap.update_entry(entry_attrs)
                done_work = True

        self.obj.get_password_attributes(ldap, dn, entry_attrs)
        if entry_attrs['has_keytab']:
            ldap.remove_principal_key(dn)
            done_work = True

        if not done_work:
            raise errors.AlreadyInactive()

        return dict(
            result=True,
            value=pkey_to_value(keys[0], options),
        )