def backup_offline(src_service_path, dest_tarfile_path, samdb_conn, smb_conf_path): """ Backup files and ntacls to a tarfile for a service """ service = src_service_path.rstrip('/').rsplit('/', 1)[-1] tempdir = tempfile.mkdtemp() session_info = system_session_unix() dom_sid_str = samdb_conn.get_domain_sid() dom_sid = security.dom_sid(dom_sid_str) ntacls_helper = NtaclsHelper(service, smb_conf_path, dom_sid) for dirpath, dirnames, filenames in os.walk(src_service_path): # each dir only cares about its direct children rel_dirpath = os.path.relpath(dirpath, start=src_service_path) dst_dirpath = os.path.join(tempdir, rel_dirpath) # create sub dirs and NTACL file for dirname in dirnames: src = os.path.join(dirpath, dirname) dst = os.path.join(dst_dirpath, dirname) # mkdir with metadata smbd.mkdir(dst, session_info, service) ntacl_sddl_str = ntacls_helper.getntacl(src, session_info, as_sddl=True) _create_ntacl_file(dst, ntacl_sddl_str) # create files and NTACL file, then copy data for filename in filenames: src = os.path.join(dirpath, filename) dst = os.path.join(dst_dirpath, filename) # create an empty file with metadata smbd.create_file(dst, session_info, service) ntacl_sddl_str = ntacls_helper.getntacl(src, session_info, as_sddl=True) _create_ntacl_file(dst, ntacl_sddl_str) # now put data in with open(src, 'rb') as src_file: data = src_file.read() with open(dst, 'wb') as dst_file: dst_file.write(data) # add all files in tempdir to tarfile without a top folder with tarfile.open(name=dest_tarfile_path, mode='w:gz') as tar: for name in os.listdir(tempdir): path = os.path.join(tempdir, name) tar.add(path, arcname=name) shutil.rmtree(tempdir)
def test_smbd_create_file(self): """ A smoke test for smbd.create_file and smbd.unlink API """ filepath = os.path.join(self.service_root, 'a-file') smbd.create_file(filepath, self.service) self.assertTrue(os.path.isfile(filepath)) # As well as checking that unlink works, this removes the # fake xattrs from the dev/inode based DB smbd.unlink(filepath, self.service) self.assertFalse(os.path.isfile(filepath))
def backup_restore(src_tarfile_path, dst_service_path, samdb_conn, smb_conf_path): """ Restore files and ntacls from a tarfile to a service """ service = dst_service_path.rstrip('/').rsplit('/', 1)[-1] tempdir = tempfile.mkdtemp() # src files dom_sid_str = samdb_conn.get_domain_sid() dom_sid = security.dom_sid(dom_sid_str) ntacls_helper = NtaclsHelper(service, smb_conf_path, dom_sid) with tarfile.open(src_tarfile_path) as f: f.extractall(path=tempdir) # e.g.: /tmp/tmpRNystY/{dir1,dir1.NTACL,...file1,file1.NTACL} for dirpath, dirnames, filenames in os.walk(tempdir): rel_dirpath = os.path.relpath(dirpath, start=tempdir) dst_dirpath = os.path.normpath( os.path.join(dst_service_path, rel_dirpath)) for dirname in dirnames: if not dirname.endswith('.NTACL'): src = os.path.join(dirpath, dirname) dst = os.path.join(dst_dirpath, dirname) if not os.path.isdir(dst): # dst must be absolute path for smbd API smbd.mkdir(dst, service) ntacl_sddl_str = _read_ntacl_file(src) ntacls_helper.setntacl(dst, ntacl_sddl_str) for filename in filenames: if not filename.endswith('.NTACL'): src = os.path.join(dirpath, filename) dst = os.path.join(dst_dirpath, filename) if not os.path.isfile(dst): # dst must be absolute path for smbd API smbd.create_file(dst, service) ntacl_sddl_str = _read_ntacl_file(src) ntacls_helper.setntacl(dst, ntacl_sddl_str) # now put data in with open(src, 'rb') as src_file: data = src_file.read() with open(dst, 'wb') as dst_file: dst_file.write(data) shutil.rmtree(tempdir)
def backup_offline(src_service_path, dest_tarfile_path, samdb_conn, smb_conf_path): """ Backup files and ntacls to a tarfile for a service """ service = src_service_path.rstrip('/').rsplit('/', 1)[-1] tempdir = tempfile.mkdtemp() dom_sid_str = samdb_conn.get_domain_sid() dom_sid = security.dom_sid(dom_sid_str) ntacls_helper = NtaclsHelper(service, smb_conf_path, dom_sid) for dirpath, dirnames, filenames in os.walk(src_service_path): # each dir only cares about its direct children rel_dirpath = os.path.relpath(dirpath, start=src_service_path) dst_dirpath = os.path.join(tempdir, rel_dirpath) # create sub dirs and NTACL file for dirname in dirnames: src = os.path.join(dirpath, dirname) dst = os.path.join(dst_dirpath, dirname) # mkdir with metadata smbd.mkdir(dst, service) ntacl_sddl_str = ntacls_helper.getntacl(src, as_sddl=True) _create_ntacl_file(dst, ntacl_sddl_str) # create files and NTACL file, then copy data for filename in filenames: src = os.path.join(dirpath, filename) dst = os.path.join(dst_dirpath, filename) # create an empty file with metadata smbd.create_file(dst, service) ntacl_sddl_str = ntacls_helper.getntacl(src, as_sddl=True) _create_ntacl_file(dst, ntacl_sddl_str) # now put data in with open(src, 'rb') as src_file: data = src_file.read() with open(dst, 'wb') as dst_file: dst_file.write(data) # add all files in tempdir to tarfile without a top folder with tarfile.open(name=dest_tarfile_path, mode='w:gz') as tar: for name in os.listdir(tempdir): path = os.path.join(tempdir, name) tar.add(path, arcname=name) shutil.rmtree(tempdir)
def test_smbd_create_file(self): """ A smoke test for smbd.create_file and smbd.unlink API """ filepath = os.path.join(self.service_root, 'a-file') smbd.create_file(filepath, system_session_unix(), self.service) self.assertTrue(os.path.isfile(filepath)) mode = os.stat(filepath).st_mode # This works in conjunction with the TEST_UMASK in smbd_base # to ensure that permissions are not related to the umask # but instead the smb.conf settings self.assertEqual(mode & 0o777, 0o644) # As well as checking that unlink works, this removes the # fake xattrs from the dev/inode based DB smbd.unlink(filepath, system_session_unix(), self.service) self.assertFalse(os.path.isfile(filepath))