From 86aabfb2e227871a2b9e9f36d99c36abcda4c807 Mon Sep 17 00:00:00 2001 From: Rikka Date: Mon, 1 May 2023 05:03:24 +0800 Subject: [PATCH] refactor: set permission --- stuffs/gapps.py | 34 +++++++++++------ stuffs/general.py | 89 ++++++++++++++++++++++++++++++++++++++------ stuffs/houdini.py | 2 - stuffs/magisk.py | 37 ++++++++++++------ stuffs/microg.py | 33 +--------------- stuffs/ndk.py | 3 -- stuffs/nodataperm.py | 4 -- stuffs/smartdock.py | 10 +++-- stuffs/widevine.py | 11 +----- 9 files changed, 135 insertions(+), 88 deletions(-) diff --git a/stuffs/gapps.py b/stuffs/gapps.py index fd4f777..88ef4bf 100644 --- a/stuffs/gapps.py +++ b/stuffs/gapps.py @@ -22,7 +22,6 @@ class Gapps(General): } } android_version = ... - arch = host() dl_link = ... act_md5 = ... dl_file_name = "gapps.zip" @@ -66,7 +65,9 @@ class Gapps(General): "priv-app/GoogleContactsSyncAdapter", "priv-app/GooglePartnerSetup", "product/overlay/PlayStoreOverlay.apk", - "system_ext/permissions/privapp-permissions-google-system-ext.xml", + "product/overlay/GmsOverlay.apk.apk", + "product/overlay/GmsSettingsProviderOverlay.apk", + "system_ext/etc/permissions/privapp-permissions-google-system-ext.xml", "system_ext/priv-app/GoogleFeedback", "system_ext/priv-app/GoogleServicesFramework", "system_ext/priv-app/SetupWizard", @@ -98,7 +99,7 @@ class Gapps(General): self.android_version = android_version self.dl_link = self.dl_links[android_version][self.arch[0]][0] self.act_md5 = self.dl_links[android_version][self.arch[0]][1] - if android_version=="11": + if android_version == "11": self.id = "OpenGapps" else: self.id = "MindTheGapps" @@ -135,6 +136,11 @@ class Gapps(General): for app in os.listdir(app_src_dir): shutil.copytree(os.path.join(app_src_dir, app), os.path.join( self.copy_dir, self.partition, "priv-app", app), dirs_exist_ok=True) + for f in os.listdir(os.path.join(self.copy_dir, self.partition, "priv-app", app)): + dst_file_path = os.path.join(os.path.join( + self.copy_dir, self.partition, "priv-app", app), f) + if os.path.splitext(dst_file_path)[1].lower() == ".apk": + self.extract_app_lib(dst_file_path) else: print(" Processing extra package : " + os.path.join(self.extract_to, "Core", lz_file)) @@ -149,12 +155,18 @@ class Gapps(General): self.copy_dir, self.partition, ccdir), dirs_exist_ok=True) def copy_13(self): - for root, dirs, files in os.walk(os.path.join(self.extract_to, "system")): - for dir in dirs: - os.chmod(os.path.join(root, dir), 0o755) + src_dir = os.path.join(self.extract_to, "system") + dst_dir = os.path.join(self.copy_dir, self.partition) + for root, dirs, files in os.walk(src_dir): + dir_name = os.path.basename(root) + # ้ๅކๆ–‡ไปถ for file in files: - os.chmod(os.path.join(root, file), 0o644) - os.chown(os.path.join(root, file), 0, 0) - - shutil.copytree(os.path.join(self.extract_to, "system"), os.path.join( - self.copy_dir, self.partition), dirs_exist_ok=True) + src_file_path = os.path.join(root, file) + dst_file_path = os.path.join(dst_dir, os.path.relpath( + src_file_path, src_dir)) + if not os.path.exists(os.path.dirname(dst_file_path)): + os.makedirs(os.path.dirname(dst_file_path)) + # Logger.info(f"{src_file_path} -> {dst_file_path}") + shutil.copy2(src_file_path, dst_file_path) + if os.path.splitext(dst_file_path)[1].lower() == ".apk": + self.extract_app_lib(dst_file_path) diff --git a/stuffs/general.py b/stuffs/general.py index ca96696..73a41da 100644 --- a/stuffs/general.py +++ b/stuffs/general.py @@ -8,10 +8,17 @@ from tools.helper import download_file, get_download_dir, host from tools import container from tools.logger import Logger + class General: + files = [] + + @property + def arch(self): + return host() + @property def skip_extract(self): - return False + return False @property def download_loc(self): @@ -25,7 +32,8 @@ class General: return "/tmp/waydroid" def download(self): - Logger.info("Downloading {} now to {} .....".format(self.dl_file_name, self.download_loc)) + Logger.info("Downloading {} now to {} .....".format( + self.dl_file_name, self.download_loc)) loc_md5 = "" if os.path.isfile(self.download_loc): with open(self.download_loc, "rb") as f: @@ -37,7 +45,7 @@ class General: Logger.warning( "md5 mismatches, redownloading now ....") loc_md5 = download_file(self.dl_link, self.download_loc) - + def remove(self): for f in self.files: file = os.path.join(self.copy_dir, self.partition, f) @@ -59,25 +67,29 @@ class General: z.extractall(self.extract_to) def add_props(self): - arch = host()[0] bin_dir = os.path.join(self.copy_dir, "system", "bin") - resetprop_rc=os.path.join(self.copy_dir, "system/etc/init/resetprop.rc") + resetprop_rc = os.path.join( + self.copy_dir, "system/etc/init/resetprop.rc") if not os.path.isfile(os.path.join(bin_dir, "_resetprop")): if not os.path.exists(bin_dir): os.makedirs(bin_dir) - shutil.copy(os.path.join("./bin",arch,"_resetprop"), bin_dir) + shutil.copy(os.path.join( + "./bin", self.arch[0], "_resetprop"), bin_dir) os.chmod(os.path.join(bin_dir, "_resetprop"), 0o755) if not os.path.isfile(os.path.join(bin_dir, "resetprop.sh")): with open(os.path.join(bin_dir, "resetprop.sh"), "w") as f: f.write("#!/system/bin/sh\n") - f.write("temp_dir=$(mktemp -d);ln -s /system/bin/_resetprop \"${temp_dir}/resetprop\"\n") - f.write("while read line; do \"${temp_dir}/resetprop\" ${line%=*} ${line#*=}; done < /vendor/waydroid.prop\n") + f.write( + "temp_dir=$(mktemp -d);ln -s /system/bin/_resetprop \"${temp_dir}/resetprop\"\n") + f.write( + "while read line; do \"${temp_dir}/resetprop\" ${line%=*} ${line#*=}; done < /vendor/waydroid.prop\n") os.chmod(os.path.join(bin_dir, "resetprop.sh"), 0o755) if not os.path.isfile(resetprop_rc): if not os.path.exists(os.path.dirname(resetprop_rc)): os.makedirs(os.path.dirname(resetprop_rc)) with open(resetprop_rc, "w") as f: - f.write("on post-fs-data\n exec u:r:su:s0 root root -- /system/bin/sh /system/bin/resetprop.sh") + f.write( + "on post-fs-data\n exec u:r:su:s0 root root -- /system/bin/sh /system/bin/resetprop.sh") os.chmod(resetprop_rc, 0o644) cfg = configparser.ConfigParser() @@ -86,17 +98,68 @@ class General: for key in self.apply_props.keys(): if self.apply_props[key]: cfg.set('properties', key, self.apply_props[key]) - + with open("/var/lib/waydroid/waydroid.cfg", "w") as f: cfg.write(f) - + + def extract_app_lib(self, apk_file_path): + lib_dest_dir = os.path.dirname(apk_file_path) + with zipfile.ZipFile(apk_file_path, "r") as apk: + for file_info in apk.infolist(): + file_name = file_info.filename + file_path = os.path.join(lib_dest_dir, file_name) + if file_info.filename.startswith(f"lib/{self.arch[0]}/") and file_name.endswith(".so"): + os.makedirs(os.path.dirname( + file_path), exist_ok=True) + with apk.open(file_info.filename) as src_file, open(file_path, "wb") as dest_file: + # Logger.info(f"{src_file} -> {dest_file}") + shutil.copyfileobj(src_file, dest_file) + + def set_path_perm(self, path): + if "bin" in path.split("/"): + perms = [0, 2000, 0o755, 0o777] + else: + perms = [0, 0, 0o755, 0o644] + + mode = os.stat(path).st_mode + + if os.path.isdir(path): + mode |= perms[2] + else: + mode |= perms[3] + + os.chown(path, perms[0], perms[1]) + os.chmod(path, mode) + + def set_perm2(self, path, recursive=False): + if not os.path.exists(path): + return + + if recursive and os.path.isdir(path): + for root, dirs, files in os.walk(path): + for dir in dirs: + self.set_path_perm(os.path.join(root, dir)) + for file in files: + self.set_path_perm(os.path.join(root, file)) + else: + self.set_path_perm(path) + + def set_perm(self): + for f in self.files: + path = os.path.join(self.copy_dir, self.partition, f) + if "*" in path: + for wildcard_file in glob.glob(path): + self.set_perm2(wildcard_file, recursive=True) + else: + self.set_perm2(path, recursive=True) + def remove_props(self): cfg = configparser.ConfigParser() cfg.read("/var/lib/waydroid/waydroid.cfg") for key in self.apply_props.keys(): cfg.remove_option('properties', key) - + with open("/var/lib/waydroid/waydroid.cfg", "w") as f: cfg.write(f) @@ -117,6 +180,7 @@ class General: self.extra1() if hasattr(self, "apply_props"): self.add_props() + self.set_perm() Logger.info(f"{self.id} installation finished") def uninstall(self): @@ -124,4 +188,5 @@ class General: if hasattr(self, "apply_props"): self.remove_props() self.extra2() + self.set_perm() Logger.info("Uninstallation finished") diff --git a/stuffs/houdini.py b/stuffs/houdini.py index 7567e5a..04333d1 100644 --- a/stuffs/houdini.py +++ b/stuffs/houdini.py @@ -55,7 +55,6 @@ on property:ro.enable.native.bridge.exec=1 self.act_md5 = self.dl_links[android_version][1] def copy(self): - run(["chmod", "+x", self.extract_to, "-R"]) Logger.info("Copying libhoudini library files ...") name = re.findall("([a-zA-Z0-9]+)\.zip", self.dl_link)[0] shutil.copytree(os.path.join(self.extract_to, "vendor_intel_proprietary_houdini-" + name, @@ -66,4 +65,3 @@ on property:ro.enable.native.bridge.exec=1 os.makedirs(os.path.dirname(init_path), exist_ok=True) with open(init_path, "w") as initfile: initfile.write(self.init_rc_component) - os.chmod(init_path, 0o644) diff --git a/stuffs/magisk.py b/stuffs/magisk.py index ddbef10..3992754 100644 --- a/stuffs/magisk.py +++ b/stuffs/magisk.py @@ -14,8 +14,7 @@ class Magisk(General): dl_file_name = "magisk.apk" extract_to = "/tmp/magisk_unpack" magisk_dir = os.path.join(partition, "etc", "init", "magisk") - machine = host() - files = ["etc/init/magisk"] + files = ["etc/init/magisk", "etc/init/bootanim.rc"] oringinal_bootanim = """ service bootanim /system/bin/bootanimation class core animation @@ -27,10 +26,10 @@ service bootanim /system/bin/bootanimation task_profiles MaxPerformance """ - bootanim_component = """ + bootanim_component = f""" on post-fs-data start logd - exec u:r:su:s0 root root -- /system/etc/init/magisk/magisk{arch} --auto-selinux --setup-sbin /system/etc/init/magisk + exec u:r:su:s0 root root -- /system/etc/init/magisk/magisk{host()[1]} --auto-selinux --setup-sbin /system/etc/init/magisk exec u:r:su:s0 root root -- /system/etc/init/magisk/magiskpolicy --live --magisk "allow * magisk_file lnk_file *" mkdir /sbin/.magisk 700 mkdir /sbin/.magisk/mirror 700 @@ -54,7 +53,7 @@ on property:init.svc.zygote=restarting on property:init.svc.zygote=stopped exec u:r:su:s0 root root -- /sbin/magisk --auto-selinux --zygote-restart - """.format(arch=machine[1]) + """ def download(self): if os.path.isfile(self.download_loc): @@ -79,14 +78,13 @@ on property:init.svc.zygote=stopped Logger.info("Copying magisk libs now ...") - lib_dir = os.path.join(self.extract_to, "lib", self.machine[0]) + lib_dir = os.path.join(self.extract_to, "lib", self.arch[0]) for parent, dirnames, filenames in os.walk(lib_dir): for filename in filenames: o_path = os.path.join(lib_dir, filename) filename = re.search('lib(.*)\.so', filename) n_path = os.path.join(magisk_absolute_dir, filename.group(1)) shutil.copyfile(o_path, n_path) - run(["chmod", "+x", n_path]) shutil.copyfile(self.download_loc, os.path.join(magisk_absolute_dir,"magisk.apk") ) shutil.copytree(os.path.join(self.extract_to, "assets", "chromeos"), os.path.join(magisk_absolute_dir, "chromeos"), dirs_exist_ok=True) assets_files = [ @@ -97,7 +95,6 @@ on property:init.svc.zygote=stopped ] for f in assets_files: shutil.copyfile(os.path.join(self.extract_to, "assets", f), os.path.join(magisk_absolute_dir, f)) - self.setup() # Updating Magisk from Magisk manager will modify bootanim.rc, # So it is necessary to backup the original bootanim.rc. @@ -107,11 +104,29 @@ on property:init.svc.zygote=stopped f_gz.write(self.oringinal_bootanim.encode('utf-8')) with open(bootanim_path, "w") as initfile: initfile.write(self.oringinal_bootanim+self.bootanim_component) - os.chmod(bootanim_path, 0o644) + def set_path_perm(self, path): + if "magisk" in path.split("/"): + perms = [0, 2000, 0o755, 0o755] + else: + perms = [0, 0, 0o755, 0o644] + + mode = os.stat(path).st_mode + + if os.path.isdir(path): + mode |= perms[2] + else: + mode |= perms[3] + + os.chown(path, perms[0], perms[1]) + os.chmod(path, mode) - # Delete the contents of upperdir def extra1(self): + self.delete_upper() + self.setup() + + # Delete the contents of upperdir + def delete_upper(self): if container.use_overlayfs(): sys_overlay_rw = "/var/lib/waydroid/overlay_rw" files = [ @@ -130,7 +145,7 @@ on property:init.svc.zygote=stopped os.remove(file) def extra2(self): - self.extra1() + self.delete_upper() data_dir = get_data_dir() files = [ os.path.join(data_dir, "adb/magisk.db"), diff --git a/stuffs/microg.py b/stuffs/microg.py index 5818be2..77496d7 100644 --- a/stuffs/microg.py +++ b/stuffs/microg.py @@ -51,7 +51,6 @@ class MicroG(General): dl_file_name = ... sdk = ... extract_to = "/tmp/microg/extract" - arch = host() rc_content = ''' on property:sys.boot_completed=1 start microg_service @@ -117,25 +116,7 @@ service microg_service /system/bin/sh /system/bin/npem elif android_version == "13": self.sdk = 33 - def set_permissions(self, path): - if "bin" in path.split("/"): - perms = [0, 2000, 0o755, 0o777] - else: - perms = [0, 0, 0o755, 0o644] - - mode = os.stat(path).st_mode - - if os.path.isdir(path): - mode |= perms[2] - else: - mode |= perms[3] - - os.chown(path, perms[0], perms[1]) - - os.chmod(path, mode) - def copy(self): - Logger.info("Copying libs and apks...") dst_dir = os.path.join(self.copy_dir, self.partition) src_dir = os.path.join(self.extract_to, "system") @@ -163,7 +144,6 @@ service microg_service /system/bin/sh /system/bin/npem for file in files: src_file_path = os.path.join(root, file) - self.set_permissions(src_file_path) if not flag: dst_file_path = os.path.join(dst_dir, os.path.relpath( src_file_path, src_dir)) @@ -175,24 +155,13 @@ service microg_service /system/bin/sh /system/bin/npem # Logger.info(f"{src_file_path} -> {dst_file_path}") shutil.copy2(src_file_path, dst_file_path) if os.path.splitext(dst_file_path)[1].lower() == ".apk": - lib_dest_dir = os.path.dirname(dst_file_path) - with zipfile.ZipFile(dst_file_path, "r") as apk: - for file_info in apk.infolist(): - file_name = file_info.filename - file_path = os.path.join(lib_dest_dir, file_name) - if file_info.filename.startswith(f"lib/{self.arch[0]}/") and file_name.endswith(".so"): - os.makedirs(os.path.dirname( - file_path), exist_ok=True) - with apk.open(file_info.filename) as src_file, open(file_path, "wb") as dest_file: - # Logger.info(f"{src_file} -> {dest_file}") - shutil.copyfileobj(src_file, dest_file) + self.extract_app_lib(dst_file_path) rc_dir = os.path.join(dst_dir, "etc/init/microg.rc") if not os.path.exists(os.path.dirname(rc_dir)): os.makedirs(os.path.dirname(rc_dir)) with open(rc_dir, "w") as f: f.write(self.rc_content) - self.set_permissions(rc_dir) def extra2(self): system_dir = os.path.join(self.copy_dir, self.partition) diff --git a/stuffs/ndk.py b/stuffs/ndk.py index 0e7a5b0..4476750 100644 --- a/stuffs/ndk.py +++ b/stuffs/ndk.py @@ -48,9 +48,6 @@ class Ndk(General): def copy(self): - run(["chmod", "+x", self.extract_to, "-R"]) Logger.info("Copying libndk library files ...") shutil.copytree(os.path.join(self.extract_to, "vendor_google_proprietary_ndk_translation-prebuilt-181d9290a69309511185c4417ba3d890b3caaaa8", "prebuilts"), os.path.join(self.copy_dir, self.partition), dirs_exist_ok=True) - init_path = os.path.join(self.copy_dir, self.partition, "etc", "init", "ndk_translation.rc") - os.chmod(init_path, 0o644) \ No newline at end of file diff --git a/stuffs/nodataperm.py b/stuffs/nodataperm.py index 6cb8fb1..0b0b029 100644 --- a/stuffs/nodataperm.py +++ b/stuffs/nodataperm.py @@ -40,10 +40,6 @@ class Nodataperm(General): with gzip.open(gz_filename, 'wb') as f_gz: with open(services_jar, "rb") as f: f_gz.write(f.read()) - os.chmod(os.path.join(extract_path, "framework", "services.jar"), 0o644) - os.chmod(os.path.join(extract_path, "etc", "nodataperm.sh"), 0o755) - os.chmod(os.path.join(extract_path, "etc", - "init", "nodataperm.rc"), 0o755) Logger.info("Copying widevine library files ...") shutil.copytree(extract_path, os.path.join( self.copy_dir, self.partition), dirs_exist_ok=True) diff --git a/stuffs/smartdock.py b/stuffs/smartdock.py index e192d0e..bc79c51 100644 --- a/stuffs/smartdock.py +++ b/stuffs/smartdock.py @@ -7,15 +7,16 @@ from tools.helper import run class Smartdock(General): id = "smartdock" - dl_link = "https://github.com/ayasa520/smartdock/releases/download/v1.9.6/smartdock.zip" + dl_link = "https://github.com/ayasa520/smartdock/releases/download/v1.9.7/smartdock.zip" partition = "system" extract_to = "/tmp/smartdockunpack" dl_file_name = "smartdock.zip" - act_md5 = "ad0cc5e023ac6ee97e7b013b9b0defee" + act_md5 = "be965374522d95f28a486c95b1208f0" apply_props = { "qemu.hw.mainkeys" : "1" } files = [ "etc/permissions/permissions_cu.axel.smartdock.xml", - "priv-app/SmartDock" + "priv-app/SmartDock", + "etc/init/smartdock.rc" ] rc_content = ''' on property:sys.boot_completed=1 @@ -33,12 +34,13 @@ service set_home_activity /system/bin/sh -c "cmd package set-home-activity cu.ax if not os.path.exists(os.path.join(self.copy_dir, self.partition, "etc", "permissions")): os.makedirs(os.path.join(self.copy_dir, self.partition, "etc", "permissions")) shutil.copyfile(os.path.join(self.extract_to, "app-release.apk"), - os.path.join(self.copy_dir, self.partition, "priv-app", "SmartDock", "smartdock.apk")) + os.path.join(self.copy_dir, self.partition, "priv-app/SmartDock/smartdock.apk")) shutil.copyfile(os.path.join(self.extract_to, "permissions_cu.axel.smartdock.xml"), os.path.join(self.copy_dir, self.partition, "etc", "permissions", "permissions_cu.axel.smartdock.xml")) rc_dir = os.path.join(self.copy_dir, self.partition, "etc/init/smartdock.rc") if not os.path.exists(os.path.dirname(rc_dir)): os.makedirs(os.path.dirname(rc_dir)) + self.extract_app_lib(os.path.join(self.copy_dir, self.partition, "priv-app/SmartDock/smartdock.apk")) with open(rc_dir, "w") as f: f.write(self.rc_content) diff --git a/stuffs/widevine.py b/stuffs/widevine.py index ad37a85..e32569f 100644 --- a/stuffs/widevine.py +++ b/stuffs/widevine.py @@ -23,7 +23,6 @@ class Widevine(General): "fed6898b5cfd2a908cb134df97802554"] } } - machine = host() dl_file_name = "widevine.zip" extract_to = "/tmp/widevineunpack" files = [ @@ -38,17 +37,11 @@ class Widevine(General): def __init__(self, android_version) -> None: super().__init__() - self.dl_link = self.dl_links[self.machine[0]][android_version][0] - self.act_md5 = self.dl_links[self.machine[0]][android_version][1] + self.dl_link = self.dl_links[self.arch[0]][android_version][0] + self.act_md5 = self.dl_links[self.arch[0]][android_version][1] def copy(self): - run(["chmod", "+x", self.extract_to, "-R"]) name = re.findall("([a-zA-Z0-9]+)\.zip", self.dl_link)[0] Logger.info("Copying widevine library files ...") shutil.copytree(os.path.join(self.extract_to, "vendor_google_proprietary_widevine-prebuilt-"+name, "prebuilts"), os.path.join(self.copy_dir, self.partition), dirs_exist_ok=True) - - for file in os.listdir(os.path.join(self.copy_dir, self.partition, "etc", "init")): - if file.endswith('.rc'): - os.chmod(os.path.join(self.copy_dir, self.partition, - "etc", "init", file), 0o644)