def __fix_dns_privilege_members(self): ldap = api.Backend.ldap2 cn = 'Update PBAC memberOf %s' % time.time() task_dn = DN(('cn', cn), ('cn', 'memberof task'), ('cn', 'tasks'), ('cn', 'config')) basedn = DN(api.env.container_privilege, api.env.basedn) entry = ldap.make_entry( task_dn, objectclass=['top', 'extensibleObject'], cn=[cn], basedn=[basedn], filter=['(objectclass=*)'], ttl=[10]) ldap.add_entry(entry) start_time = time.time() while True: try: task = ldap.get_entry(task_dn) except errors.NotFound: break if 'nstaskexitcode' in task: break time.sleep(1) if time.time() > (start_time + 60): raise errors.TaskTimeout(task='memberof', task_dn=task_dn)
def __fix_dns_privilege_members(self): ldap = self.api.Backend.ldap2 cn = 'Update PBAC memberOf %s' % time.time() task_dn = DN(('cn', cn), ('cn', 'memberof task'), ('cn', 'tasks'), ('cn', 'config')) basedn = DN(self.api.env.container_privilege, self.api.env.basedn) entry = ldap.make_entry(task_dn, objectclass=['top', 'extensibleObject'], cn=[cn], basedn=[basedn], filter=['(objectclass=*)'], ttl=[10]) ldap.add_entry(entry) start_time = time.time() while True: try: task = ldap.get_entry(task_dn) except errors.NotFound: break if 'nstaskexitcode' in task: break time.sleep(1) if time.time() > (start_time + 60): raise errors.TaskTimeout(task='memberof', task_dn=task_dn)
def trusted_domain(): """Fixture providing mocked AD trust entries The fixture yields after creating a mock of AD trust entries in the directory server. After the test, the entries are deleted from the directory. """ trusted_dom = TRUSTED_DOMAIN_MOCK # Write the changes with mocked_trust_containers(), MockLDAP() as ldap: ldap.add_entry(trusted_dom['dn'], trusted_dom['ldif']) yield trusted_dom ldap.del_entry(trusted_dom['dn'])
def ensure_dnsserver_container_exists(ldap, api_instance, logger=logger): """ Create cn=servers,cn=dns,$SUFFIX container. If logger is not None, emit a message that the container already exists when DuplicateEntry is raised """ entry = ldap.make_entry( DN(api_instance.env.container_dnsservers, api_instance.env.basedn), { u'objectclass': [u'top', u'nsContainer'], u'cn': [u'servers'] }) try: ldap.add_entry(entry) except errors.DuplicateEntry: logger.debug('cn=servers,cn=dns container already exists')
def trusted_domain_with_suffix(): """Fixture providing mocked AD trust entries The fixture yields after creating a mock of AD trust entries in the directory server. After the test, the entries are deleted from the directory. """ trusted_dom = copy.deepcopy(TRUSTED_DOMAIN_MOCK) trusted_dom['ldif']['ipaNTAdditionalSuffixes'] = (encode_mockldap_value( trusted_dom['name'])) # Write the changes with mocked_trust_containers(), MockLDAP() as ldap: ldap.add_entry(trusted_dom['dn'], trusted_dom['ldif']) yield trusted_dom ldap.del_entry(trusted_dom['dn'])
def ensure_dnsserver_container_exists(ldap, api_instance, logger=logger): """ Create cn=servers,cn=dns,$SUFFIX container. If logger is not None, emit a message that the container already exists when DuplicateEntry is raised """ entry = ldap.make_entry( DN(api_instance.env.container_dnsservers, api_instance.env.basedn), { u'objectclass': [u'top', u'nsContainer'], u'cn': [u'servers'] } ) try: ldap.add_entry(entry) except errors.DuplicateEntry: logger.debug('cn=servers,cn=dns container already exists')
def trusted_domain_with_suffix(): """Fixture providing mocked AD trust entries The fixture yields after creating a mock of AD trust entries in the directory server. After the test, the entries are deleted from the directory. """ trusted_dom = copy.deepcopy(TRUSTED_DOMAIN_MOCK) trusted_dom['ldif']['ipaNTAdditionalSuffixes'] = ( encode_mockldap_value(trusted_dom['name']) ) # Write the changes with mocked_trust_containers(), MockLDAP() as ldap: ldap.add_entry(trusted_dom['dn'], trusted_dom['ldif']) yield trusted_dom ldap.del_entry(trusted_dom['dn'])
def __setup_replica_keys(self): keylabel = replica_keylabel_template % DNSName(self.fqdn).\ make_absolute().canonicalize().ToASCII() ldap = api.Backend.ldap2 dn_base = DN(('cn', 'keys'), ('cn', 'sec'), ('cn', 'dns'), api.env.basedn) with open(paths.DNSSEC_SOFTHSM_PIN, "r") as f: pin = f.read() os.environ["SOFTHSM2_CONF"] = paths.DNSSEC_SOFTHSM2_CONF p11 = _ipap11helper.P11_Helper(SOFTHSM_DNSSEC_TOKEN_LABEL, pin, paths.LIBSOFTHSM2_SO) try: # generate replica keypair logger.debug("Creating replica's key pair") key_id = None while True: # check if key with this ID exist in softHSM key_id = _ipap11helper.gen_key_id() replica_pubkey_dn = DN(('ipk11UniqueId', 'autogenerate'), dn_base) pub_keys = p11.find_keys(_ipap11helper.KEY_CLASS_PUBLIC_KEY, label=keylabel, id=key_id) if pub_keys: # key with id exists continue priv_keys = p11.find_keys(_ipap11helper.KEY_CLASS_PRIVATE_KEY, label=keylabel, id=key_id) if not priv_keys: break # we found unique id public_key_handle, _privkey_handle = p11.generate_replica_key_pair( keylabel, key_id, pub_cka_verify=False, pub_cka_verify_recover=False, pub_cka_wrap=True, priv_cka_unwrap=True, priv_cka_sensitive=True, priv_cka_extractable=False) # export public key public_key_blob = p11.export_public_key(public_key_handle) # save key to LDAP replica_pubkey_objectclass = [ 'ipk11Object', 'ipk11PublicKey', 'ipaPublicKeyObject', 'top' ] kw = { 'objectclass': replica_pubkey_objectclass, 'ipk11UniqueId': [u'autogenerate'], 'ipk11Label': [keylabel], 'ipaPublicKey': [public_key_blob], 'ipk11Id': [key_id], 'ipk11Wrap': [True], 'ipk11Verify': [False], 'ipk11VerifyRecover': [False], } logger.debug("Storing replica public key to LDAP, %s", replica_pubkey_dn) entry = ldap.make_entry(replica_pubkey_dn, **kw) ldap.add_entry(entry) logger.debug("Replica public key stored") logger.debug("Setting CKA_WRAP=False for old replica keys") # first create new keys, we don't want disable keys before, we # have new keys in softhsm and LDAP # get replica pub keys with CKA_WRAP=True replica_pub_keys = p11.find_keys( _ipap11helper.KEY_CLASS_PUBLIC_KEY, label=keylabel, cka_wrap=True) # old keys in softHSM for handle in replica_pub_keys: # don't disable wrapping for new key # compare IDs not handle if key_id != p11.get_attribute(handle, _ipap11helper.CKA_ID): p11.set_attribute(handle, _ipap11helper.CKA_WRAP, False) # get old keys from LDAP search_kw = { 'objectclass': u"ipaPublicKeyObject", 'ipk11Label': keylabel, 'ipk11Wrap': True, } filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL) entries, _truncated = ldap.find_entries(filter=filter, base_dn=dn_base) for entry in entries: # don't disable wrapping for new key if entry.single_value['ipk11Id'] != key_id: entry['ipk11Wrap'] = [False] ldap.update_entry(entry) finally: p11.finalize() # change tokens mod/owner logger.debug("Changing ownership of token files") for (root, dirs, files) in os.walk(paths.DNSSEC_TOKENS_DIR): for directory in dirs: dir_path = os.path.join(root, directory) os.chmod(dir_path, 0o770 | stat.S_ISGID) # chown to ods:named os.chown(dir_path, self.ods_uid, self.named_gid) for filename in files: file_path = os.path.join(root, filename) os.chmod(file_path, 0o770 | stat.S_ISGID) # chown to ods:named os.chown(file_path, self.ods_uid, self.named_gid)
def __setup_replica_keys(self): keylabel = replica_keylabel_template % DNSName(self.fqdn).\ make_absolute().canonicalize().ToASCII() ldap = self.admin_conn dn_base = DN(('cn', 'keys'), ('cn', 'sec'), ('cn', 'dns'), api.env.basedn) with open(paths.DNSSEC_SOFTHSM_PIN, "r") as f: pin = f.read() os.environ["SOFTHSM2_CONF"] = paths.DNSSEC_SOFTHSM2_CONF p11 = _ipap11helper.P11_Helper(softhsm_slot, pin, paths.LIBSOFTHSM2_SO) try: # generate replica keypair self.logger.debug("Creating replica's key pair") key_id = None while True: # check if key with this ID exist in softHSM # id is 16 Bytes long key_id = "".join(chr(random.randint(0, 255)) for _ in range(0, 16)) replica_pubkey_dn = DN(('ipk11UniqueId', 'autogenerate'), dn_base) pub_keys = p11.find_keys(_ipap11helper.KEY_CLASS_PUBLIC_KEY, label=keylabel, id=key_id) if pub_keys: # key with id exists continue priv_keys = p11.find_keys(_ipap11helper.KEY_CLASS_PRIVATE_KEY, label=keylabel, id=key_id) if not priv_keys: break # we found unique id public_key_handle, private_key_handle = p11.generate_replica_key_pair( keylabel, key_id, pub_cka_verify=False, pub_cka_verify_recover=False, pub_cka_wrap=True, priv_cka_unwrap=True, priv_cka_sensitive=True, priv_cka_extractable=False) # export public key public_key_blob = p11.export_public_key(public_key_handle) # save key to LDAP replica_pubkey_objectclass = [ 'ipk11Object', 'ipk11PublicKey', 'ipaPublicKeyObject', 'top' ] kw = { 'objectclass': replica_pubkey_objectclass, 'ipk11UniqueId': [u'autogenerate'], 'ipk11Label': [keylabel], 'ipaPublicKey': [public_key_blob], 'ipk11Id': [key_id], 'ipk11Wrap': [True], 'ipk11Verify': [False], 'ipk11VerifyRecover': [False], } self.logger.debug("Storing replica public key to LDAP, %s", replica_pubkey_dn) entry = ldap.make_entry(replica_pubkey_dn, **kw) ldap.add_entry(entry) self.logger.debug("Replica public key stored") self.logger.debug("Setting CKA_WRAP=False for old replica keys") # first create new keys, we don't want disable keys before, we # have new keys in softhsm and LDAP # get replica pub keys with CKA_WRAP=True replica_pub_keys = p11.find_keys(_ipap11helper.KEY_CLASS_PUBLIC_KEY, label=keylabel, cka_wrap=True) # old keys in softHSM for handle in replica_pub_keys: # don't disable wrapping for new key # compare IDs not handle if key_id != p11.get_attribute(handle, _ipap11helper.CKA_ID): p11.set_attribute(handle, _ipap11helper.CKA_WRAP, False) # get old keys from LDAP search_kw = { 'objectclass': u"ipaPublicKeyObject", 'ipk11Label': keylabel, 'ipk11Wrap': True, } filter = ldap.make_filter(search_kw, rules=ldap.MATCH_ALL) entries, truncated = ldap.find_entries(filter=filter, base_dn=dn_base) for entry in entries: # don't disable wrapping for new key if entry.single_value['ipk11Id'] != key_id: entry['ipk11Wrap'] = [False] ldap.update_entry(entry) finally: p11.finalize() # change tokens mod/owner self.logger.debug("Changing ownership of token files") for (root, dirs, files) in os.walk(paths.DNSSEC_TOKENS_DIR): for directory in dirs: dir_path = os.path.join(root, directory) os.chmod(dir_path, 0o770 | stat.S_ISGID) # chown to ods:named os.chown(dir_path, self.ods_uid, self.named_gid) for filename in files: file_path = os.path.join(root, filename) os.chmod(file_path, 0o770 | stat.S_ISGID) # chown to ods:named os.chown(file_path, self.ods_uid, self.named_gid)