📄 provision.py
字号:
def make_smbconf(smbconf, setup_path, hostname, domain, realm, serverrole, targetdir): if hostname is None: hostname = socket.gethostname().split(".")[0].lower() if serverrole is None: serverrole = "standalone" assert serverrole in ("domain controller", "member server", "standalone") if serverrole == "domain controller": smbconfsuffix = "dc" elif serverrole == "member server": smbconfsuffix = "member" elif serverrole == "standalone": smbconfsuffix = "standalone" assert domain is not None assert realm is not None default_lp = param.LoadParm() #Load non-existant file default_lp.load(smbconf) if targetdir is not None: privatedir_line = "private dir = " + os.path.abspath(os.path.join(targetdir, "private")) lockdir_line = "lock dir = " + os.path.abspath(targetdir) default_lp.set("lock dir", os.path.abspath(targetdir)) else: privatedir_line = "" lockdir_line = "" sysvol = os.path.join(default_lp.get("lock dir"), "sysvol") netlogon = os.path.join(sysvol, realm.lower(), "scripts") setup_file(setup_path("provision.smb.conf.%s" % smbconfsuffix), smbconf, { "HOSTNAME": hostname, "DOMAIN": domain, "REALM": realm, "SERVERROLE": serverrole, "NETLOGONPATH": netlogon, "SYSVOLPATH": sysvol, "PRIVATEDIR_LINE": privatedir_line, "LOCKDIR_LINE": lockdir_line })def setup_name_mappings(samdb, idmap, sid, domaindn, root_uid, nobody_uid, users_gid, wheel_gid): """setup reasonable name mappings for sam names to unix names. :param samdb: SamDB object. :param idmap: IDmap db object. :param sid: The domain sid. :param domaindn: The domain DN. :param root_uid: uid of the UNIX root user. :param nobody_uid: uid of the UNIX nobody user. :param users_gid: gid of the UNIX users group. :param wheel_gid: gid of the UNIX wheel group.""" # add some foreign sids if they are not present already samdb.add_foreign(domaindn, "S-1-5-7", "Anonymous") samdb.add_foreign(domaindn, "S-1-1-0", "World") samdb.add_foreign(domaindn, "S-1-5-2", "Network") samdb.add_foreign(domaindn, "S-1-5-18", "System") samdb.add_foreign(domaindn, "S-1-5-11", "Authenticated Users") idmap.setup_name_mapping("S-1-5-7", idmap.TYPE_UID, nobody_uid) idmap.setup_name_mapping("S-1-5-32-544", idmap.TYPE_GID, wheel_gid) idmap.setup_name_mapping(sid + "-500", idmap.TYPE_UID, root_uid) idmap.setup_name_mapping(sid + "-513", idmap.TYPE_GID, users_gid)def setup_samdb_partitions(samdb_path, setup_path, message, lp, session_info, credentials, names, serverrole, ldap_backend=None, ldap_backend_type=None, erase=False): """Setup the partitions for the SAM database. Alternatively, provision() may call this, and then populate the database. :note: This will wipe the Sam Database! :note: This function always removes the local SAM LDB file. The erase parameter controls whether to erase the existing data, which may not be stored locally but in LDAP. """ assert session_info is not None samdb = SamDB(samdb_path, session_info=session_info, credentials=credentials, lp=lp) # Wipes the database try: samdb.erase() except: os.unlink(samdb_path) samdb = SamDB(samdb_path, session_info=session_info, credentials=credentials, lp=lp) #Add modules to the list to activate them by default #beware often order is important # # Some Known ordering constraints: # - rootdse must be first, as it makes redirects from "" -> cn=rootdse # - objectclass must be before password_hash, because password_hash checks # that the objectclass is of type person (filled in by objectclass # module when expanding the objectclass list) # - partition must be last # - each partition has its own module list then modules_list = ["rootdse", "paged_results", "ranged_results", "anr", "server_sort", "extended_dn", "asq", "rdn_name", "objectclass", "samldb", "kludge_acl", "operational"] tdb_modules_list = [ "subtree_rename", "subtree_delete", "linked_attributes"] modules_list2 = ["show_deleted", "partition"] domaindn_ldb = "users.ldb" if ldap_backend is not None: domaindn_ldb = ldap_backend configdn_ldb = "configuration.ldb" if ldap_backend is not None: configdn_ldb = ldap_backend schemadn_ldb = "schema.ldb" if ldap_backend is not None: schema_ldb = ldap_backend schemadn_ldb = ldap_backend if ldap_backend_type == "fedora-ds": backend_modules = ["nsuniqueid", "paged_searches"] # We can handle linked attributes here, as we don't have directory-side subtree operations tdb_modules_list = ["linked_attributes"] elif ldap_backend_type == "openldap": backend_modules = ["normalise", "entryuuid", "paged_searches"] # OpenLDAP handles subtree renames, so we don't want to do any of these things tdb_modules_list = None elif serverrole == "domain controller": backend_modules = ["repl_meta_data"] else: backend_modules = ["objectguid"] if tdb_modules_list is None: tdb_modules_list_as_string = "" else: tdb_modules_list_as_string = ","+",".join(tdb_modules_list) samdb.transaction_start() try: setup_add_ldif(samdb, setup_path("provision_partitions.ldif"), { "SCHEMADN": names.schemadn, "SCHEMADN_LDB": schemadn_ldb, "SCHEMADN_MOD2": ",objectguid", "CONFIGDN": names.configdn, "CONFIGDN_LDB": configdn_ldb, "DOMAINDN": names.domaindn, "DOMAINDN_LDB": domaindn_ldb, "SCHEMADN_MOD": "schema_fsmo,instancetype", "CONFIGDN_MOD": "naming_fsmo,instancetype", "DOMAINDN_MOD": "pdc_fsmo,password_hash,instancetype", "MODULES_LIST": ",".join(modules_list), "TDB_MODULES_LIST": tdb_modules_list_as_string, "MODULES_LIST2": ",".join(modules_list2), "BACKEND_MOD": ",".join(backend_modules), }) except: samdb.transaction_cancel() raise samdb.transaction_commit() samdb = SamDB(samdb_path, session_info=session_info, credentials=credentials, lp=lp) samdb.transaction_start() try: message("Setting up sam.ldb attributes") samdb.load_ldif_file_add(setup_path("provision_init.ldif")) message("Setting up sam.ldb rootDSE") setup_samdb_rootdse(samdb, setup_path, names) if erase: message("Erasing data from partitions") samdb.erase_partitions() except: samdb.transaction_cancel() raise samdb.transaction_commit() return samdbdef secretsdb_become_dc(secretsdb, setup_path, domain, realm, dnsdomain, netbiosname, domainsid, keytab_path, samdb_url, dns_keytab_path, dnspass, machinepass): """Add DC-specific bits to a secrets database. :param secretsdb: Ldb Handle to the secrets database :param setup_path: Setup path function :param machinepass: Machine password """ setup_ldb(secretsdb, setup_path("secrets_dc.ldif"), { "MACHINEPASS_B64": b64encode(machinepass), "DOMAIN": domain, "REALM": realm, "DNSDOMAIN": dnsdomain, "DOMAINSID": str(domainsid), "SECRETS_KEYTAB": keytab_path, "NETBIOSNAME": netbiosname, "SAM_LDB": samdb_url, "DNS_KEYTAB": dns_keytab_path, "DNSPASS_B64": b64encode(dnspass), })def setup_secretsdb(path, setup_path, session_info, credentials, lp): """Setup the secrets database. :param path: Path to the secrets database. :param setup_path: Get the path to a setup file. :param session_info: Session info. :param credentials: Credentials :param lp: Loadparm context :return: LDB handle for the created secrets database """ if os.path.exists(path): os.unlink(path) secrets_ldb = Ldb(path, session_info=session_info, credentials=credentials, lp=lp) secrets_ldb.erase() secrets_ldb.load_ldif_file_add(setup_path("secrets_init.ldif")) secrets_ldb = Ldb(path, session_info=session_info, credentials=credentials, lp=lp) secrets_ldb.load_ldif_file_add(setup_path("secrets.ldif")) return secrets_ldbdef setup_templatesdb(path, setup_path, session_info, credentials, lp): """Setup the templates database. :param path: Path to the database. :param setup_path: Function for obtaining the path to setup files. :param session_info: Session info :param credentials: Credentials :param lp: Loadparm context """ templates_ldb = SamDB(path, session_info=session_info, credentials=credentials, lp=lp) templates_ldb.erase() templates_ldb.load_ldif_file_add(setup_path("provision_templates.ldif"))def setup_registry(path, setup_path, session_info, credentials, lp): """Setup the registry. :param path: Path to the registry database :param setup_path: Function that returns the path to a setup. :param session_info: Session information :param credentials: Credentials :param lp: Loadparm context """ reg = registry.Registry() hive = registry.open_ldb(path, session_info=session_info, credentials=credentials, lp_ctx=lp) reg.mount_hive(hive, "HKEY_LOCAL_MACHINE") provision_reg = setup_path("provision.reg") assert os.path.exists(provision_reg) reg.diff_apply(provision_reg)def setup_idmapdb(path, setup_path, session_info, credentials, lp): """Setup the idmap database. :param path: path to the idmap database :param setup_path: Function that returns a path to a setup file :param session_info: Session information :param credentials: Credentials :param lp: Loadparm context """ if os.path.exists(path): os.unlink(path) idmap_ldb = IDmapDB(path, session_info=session_info, credentials=credentials, lp=lp) idmap_ldb.erase() idmap_ldb.load_ldif_file_add(setup_path("idmap_init.ldif")) return idmap_ldbdef setup_samdb_rootdse(samdb, setup_path, names): """Setup the SamDB rootdse. :param samdb: Sam Database handle :param setup_path: Obtain setup path """ setup_add_ldif(samdb, setup_path("provision_rootdse_add.ldif"), { "SCHEMADN": names.schemadn, "NETBIOSNAME": names.netbiosname, "DNSDOMAIN": names.dnsdomain, "REALM": names.realm, "DNSNAME": "%s.%s" % (names.hostname, names.dnsdomain), "DOMAINDN": names.domaindn, "ROOTDN": names.rootdn, "CONFIGDN": names.configdn, "SERVERDN": names.serverdn, }) def setup_self_join(samdb, names, machinepass, dnspass, domainsid, invocationid, setup_path, policyguid): """Join a host to its own domain.""" assert isinstance(invocationid, str) setup_add_ldif(samdb, setup_path("provision_self_join.ldif"), { "CONFIGDN": names.configdn, "SCHEMADN": names.schemadn, "DOMAINDN": names.domaindn, "SERVERDN": names.serverdn, "INVOCATIONID": invocationid, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": names.sitename, "DNSNAME": "%s.%s" % (names.hostname, names.dnsdomain), "MACHINEPASS_B64": b64encode(machinepass), "DNSPASS_B64": b64encode(dnspass), "REALM": names.realm, "DOMAIN": names.domain, "DNSDOMAIN": names.dnsdomain}) setup_add_ldif(samdb, setup_path("provision_group_policy.ldif"), { "POLICYGUID": policyguid, "DNSDOMAIN": names.dnsdomain, "DOMAINSID": str(domainsid), "DOMAINDN": names.domaindn})
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -