Example #1
0
 def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
     assert isinstance(dn, DN)
     if options.get('ip_address') and dns_container_exists(ldap):
         parts = keys[-1].split('.')
         host = parts[0]
         domain = unicode('.'.join(parts[1:]))
         check_reverse = not options.get('no_reverse', False)
         add_records_for_host_validation('ip_address',
                 DNSName(host),
                 DNSName(domain).make_absolute(),
                 options['ip_address'],
                 check_forward=True,
                 check_reverse=check_reverse)
     if not options.get('force', False) and not 'ip_address' in options:
         util.validate_host_dns(self.log, keys[-1])
     if 'locality' in entry_attrs:
         entry_attrs['l'] = entry_attrs['locality']
     entry_attrs['cn'] = keys[-1]
     entry_attrs['serverhostname'] = keys[-1].split('.', 1)[0]
     if not entry_attrs.get('userpassword', False) and not options.get('random', False):
         entry_attrs['krbprincipalname'] = 'host/%s@%s' % (
             keys[-1], self.api.env.realm
         )
         if 'krbprincipalaux' not in entry_attrs['objectclass']:
             entry_attrs['objectclass'].append('krbprincipalaux')
         if 'krbprincipal' not in entry_attrs['objectclass']:
             entry_attrs['objectclass'].append('krbprincipal')
     else:
         if 'krbprincipalaux' in entry_attrs['objectclass']:
             entry_attrs['objectclass'].remove('krbprincipalaux')
         if 'krbprincipal' in entry_attrs['objectclass']:
             entry_attrs['objectclass'].remove('krbprincipal')
     if options.get('random'):
         entry_attrs['userpassword'] = ipa_generate_password(characters=host_pwd_chars)
         # save the password so it can be displayed in post_callback
         setattr(context, 'randompassword', entry_attrs['userpassword'])
     certs = options.get('usercertificate', [])
     certs_der = map(x509.normalize_certificate, certs)
     for cert in certs_der:
         x509.verify_cert_subject(ldap, keys[-1], cert)
     entry_attrs['usercertificate'] = certs_der
     entry_attrs['managedby'] = dn
     entry_attrs['objectclass'].append('ieee802device')
     entry_attrs['objectclass'].append('ipasshhost')
     update_krbticketflags(ldap, entry_attrs, attrs_list, options, False)
     if 'krbticketflags' in entry_attrs:
         entry_attrs['objectclass'].append('krbticketpolicyaux')
     return dn
Example #2
0
 def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
     assert isinstance(dn, DN)
     if options.get('ip_address') and dns_container_exists(ldap):
         parts = keys[-1].split('.')
         host = parts[0]
         domain = unicode('.'.join(parts[1:]))
         check_reverse = not options.get('no_reverse', False)
         add_records_for_host_validation('ip_address',
                 DNSName(host),
                 DNSName(domain).make_absolute(),
                 options['ip_address'],
                 check_forward=True,
                 check_reverse=check_reverse)
     if not options.get('force', False) and not 'ip_address' in options:
         util.verify_host_resolvable(keys[-1], self.log)
     if 'locality' in entry_attrs:
         entry_attrs['l'] = entry_attrs['locality']
     entry_attrs['cn'] = keys[-1]
     entry_attrs['serverhostname'] = keys[-1].split('.', 1)[0]
     if not entry_attrs.get('userpassword', False) and not options.get('random', False):
         entry_attrs['krbprincipalname'] = 'host/%s@%s' % (
             keys[-1], self.api.env.realm
         )
         if 'krbprincipalaux' not in entry_attrs['objectclass']:
             entry_attrs['objectclass'].append('krbprincipalaux')
         if 'krbprincipal' not in entry_attrs['objectclass']:
             entry_attrs['objectclass'].append('krbprincipal')
     else:
         if 'krbprincipalaux' in entry_attrs['objectclass']:
             entry_attrs['objectclass'].remove('krbprincipalaux')
         if 'krbprincipal' in entry_attrs['objectclass']:
             entry_attrs['objectclass'].remove('krbprincipal')
     if options.get('random'):
         entry_attrs['userpassword'] = ipa_generate_password(characters=host_pwd_chars)
         # save the password so it can be displayed in post_callback
         setattr(context, 'randompassword', entry_attrs['userpassword'])
     certs = options.get('usercertificate', [])
     certs_der = [x509.normalize_certificate(c) for c in certs]
     for cert in certs_der:
         x509.verify_cert_subject(ldap, keys[-1], cert)
     entry_attrs['usercertificate'] = certs_der
     entry_attrs['managedby'] = dn
     entry_attrs['objectclass'].append('ieee802device')
     entry_attrs['objectclass'].append('ipasshhost')
     update_krbticketflags(ldap, entry_attrs, attrs_list, options, False)
     if 'krbticketflags' in entry_attrs:
         entry_attrs['objectclass'].append('krbticketpolicyaux')
     return dn
Example #3
0
    def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
        assert isinstance(dn, DN)
        # Allow an existing OTP to be reset but don't allow a OTP to be
        # added to an enrolled host.
        if options.get('userpassword') or options.get('random'):
            entry = {}
            self.obj.get_password_attributes(ldap, dn, entry)
            if not entry['has_password'] and entry['has_keytab']:
                raise errors.ValidationError(
                    name='password',
                    error=_('Password cannot be set on enrolled host.'))

        # Once a principal name is set it cannot be changed
        if 'cn' in entry_attrs:
            raise errors.ACIError(info=_('cn is immutable'))
        if 'locality' in entry_attrs:
            entry_attrs['l'] = entry_attrs['locality']
        if 'krbprincipalname' in entry_attrs:
            entry_attrs_old = ldap.get_entry(
                dn, ['objectclass', 'krbprincipalname']
            )
            if 'krbprincipalname' in entry_attrs_old:
                msg = 'Principal name already set, it is unchangeable.'
                raise errors.ACIError(info=msg)
            obj_classes = entry_attrs_old['objectclass']
            if 'krbprincipalaux' not in obj_classes:
                obj_classes.append('krbprincipalaux')
                entry_attrs['objectclass'] = obj_classes

        # verify certificates
        certs = entry_attrs.get('usercertificate') or []
        certs_der = map(x509.normalize_certificate, certs)
        for cert in certs_der:
            x509.verify_cert_subject(ldap, keys[-1], cert)

        # revoke removed certificates
        if certs and self.api.Command.ca_is_enabled()['result']:
            try:
                entry_attrs_old = ldap.get_entry(dn, ['usercertificate'])
            except errors.NotFound:
                self.obj.handle_not_found(*keys)
            old_certs = entry_attrs_old.get('usercertificate', [])
            old_certs_der = map(x509.normalize_certificate, old_certs)
            removed_certs_der = set(old_certs_der) - set(certs_der)
            revoke_certs(removed_certs_der, self.log)

        if certs:
            entry_attrs['usercertificate'] = certs_der

        if options.get('random'):
            entry_attrs['userpassword'] = ipa_generate_password(characters=host_pwd_chars)
            setattr(context, 'randompassword', entry_attrs['userpassword'])

        if 'macaddress' in entry_attrs:
            if 'objectclass' in entry_attrs:
                obj_classes = entry_attrs['objectclass']
            else:
                _entry_attrs = ldap.get_entry(dn, ['objectclass'])
                obj_classes = _entry_attrs['objectclass']
            if 'ieee802device' not in obj_classes:
                obj_classes.append('ieee802device')
                entry_attrs['objectclass'] = obj_classes

        if options.get('updatedns', False) and dns_container_exists(ldap):
            parts = keys[-1].split('.')
            domain = unicode('.'.join(parts[1:]))
            try:
                result = api.Command['dnszone_show'](domain)['result']
                domain = result['idnsname'][0]
            except errors.NotFound:
                self.obj.handle_not_found(*keys)
            update_sshfp_record(domain, unicode(parts[0]), entry_attrs)

        if 'ipasshpubkey' in entry_attrs:
            if 'objectclass' in entry_attrs:
                obj_classes = entry_attrs['objectclass']
            else:
                _entry_attrs = ldap.get_entry(dn, ['objectclass'])
                obj_classes = entry_attrs['objectclass'] = _entry_attrs['objectclass']
            if 'ipasshhost' not in obj_classes:
                obj_classes.append('ipasshhost')

        update_krbticketflags(ldap, entry_attrs, attrs_list, options, True)

        if 'krbticketflags' in entry_attrs:
            if 'objectclass' not in entry_attrs:
                entry_attrs_old = ldap.get_entry(dn, ['objectclass'])
                entry_attrs['objectclass'] = entry_attrs_old['objectclass']
            if 'krbticketpolicyaux' not in entry_attrs['objectclass']:
                entry_attrs['objectclass'].append('krbticketpolicyaux')

        return dn
Example #4
0
    def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
        assert isinstance(dn, DN)
        # Allow an existing OTP to be reset but don't allow a OTP to be
        # added to an enrolled host.
        if options.get('userpassword') or options.get('random'):
            entry = {}
            self.obj.get_password_attributes(ldap, dn, entry)
            if not entry['has_password'] and entry['has_keytab']:
                raise errors.ValidationError(
                    name='password',
                    error=_('Password cannot be set on enrolled host.'))

        # Once a principal name is set it cannot be changed
        if 'cn' in entry_attrs:
            raise errors.ACIError(info=_('cn is immutable'))
        if 'locality' in entry_attrs:
            entry_attrs['l'] = entry_attrs['locality']
        if 'krbprincipalname' in entry_attrs:
            entry_attrs_old = ldap.get_entry(
                dn, ['objectclass', 'krbprincipalname']
            )
            if 'krbprincipalname' in entry_attrs_old:
                msg = 'Principal name already set, it is unchangeable.'
                raise errors.ACIError(info=msg)
            obj_classes = entry_attrs_old['objectclass']
            if 'krbprincipalaux' not in obj_classes:
                obj_classes.append('krbprincipalaux')
                entry_attrs['objectclass'] = obj_classes

        # verify certificates
        certs = entry_attrs.get('usercertificate') or []
        certs_der = [x509.normalize_certificate(c) for c in certs]
        for cert in certs_der:
            x509.verify_cert_subject(ldap, keys[-1], cert)

        # revoke removed certificates
        if certs and self.api.Command.ca_is_enabled()['result']:
            try:
                entry_attrs_old = ldap.get_entry(dn, ['usercertificate'])
            except errors.NotFound:
                self.obj.handle_not_found(*keys)
            old_certs = entry_attrs_old.get('usercertificate', [])
            old_certs_der = [x509.normalize_certificate(c) for c in old_certs]
            removed_certs_der = set(old_certs_der) - set(certs_der)
            revoke_certs(removed_certs_der, self.log)

        if certs:
            entry_attrs['usercertificate'] = certs_der

        if options.get('random'):
            entry_attrs['userpassword'] = ipa_generate_password(characters=host_pwd_chars)
            setattr(context, 'randompassword', entry_attrs['userpassword'])

        if 'macaddress' in entry_attrs:
            if 'objectclass' in entry_attrs:
                obj_classes = entry_attrs['objectclass']
            else:
                _entry_attrs = ldap.get_entry(dn, ['objectclass'])
                obj_classes = _entry_attrs['objectclass']
            if 'ieee802device' not in obj_classes:
                obj_classes.append('ieee802device')
                entry_attrs['objectclass'] = obj_classes

        if options.get('updatedns', False) and dns_container_exists(ldap):
            parts = keys[-1].split('.')
            domain = unicode('.'.join(parts[1:]))
            try:
                result = api.Command['dnszone_show'](domain)['result']
                domain = result['idnsname'][0]
            except errors.NotFound:
                self.obj.handle_not_found(*keys)
            update_sshfp_record(domain, unicode(parts[0]), entry_attrs)

        if 'ipasshpubkey' in entry_attrs:
            if 'objectclass' in entry_attrs:
                obj_classes = entry_attrs['objectclass']
            else:
                _entry_attrs = ldap.get_entry(dn, ['objectclass'])
                obj_classes = entry_attrs['objectclass'] = _entry_attrs['objectclass']
            if 'ipasshhost' not in obj_classes:
                obj_classes.append('ipasshhost')

        update_krbticketflags(ldap, entry_attrs, attrs_list, options, True)

        if 'krbticketflags' in entry_attrs:
            if 'objectclass' not in entry_attrs:
                entry_attrs_old = ldap.get_entry(dn, ['objectclass'])
                entry_attrs['objectclass'] = entry_attrs_old['objectclass']
            if 'krbticketpolicyaux' not in entry_attrs['objectclass']:
                entry_attrs['objectclass'].append('krbticketpolicyaux')

        return dn