From 4ff9dcaa17d705351abe3a74d6911e3000ef4cce Mon Sep 17 00:00:00 2001 From: weishu Date: Sat, 21 Oct 2023 22:05:19 +0800 Subject: [PATCH] assets: allow to use default uid, gid, groups, capabilities and context --- .../main/java/me/weishu/kernelsu/Natives.kt | 11 +++++-- .../ui/viewmodel/TemplateViewModel.kt | 29 +++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/manager/app/src/main/java/me/weishu/kernelsu/Natives.kt b/manager/app/src/main/java/me/weishu/kernelsu/Natives.kt index e672bf18f7b8..1ca552c63dfc 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/Natives.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/Natives.kt @@ -18,6 +18,11 @@ object Natives { // 11071: Fix the issue of failing to set a custom SELinux type. const val MINIMAL_SUPPORTED_KERNEL = 11071 + const val KERNEL_SU_DOMAIN = "u:r:su:s0" + + const val ROOT_UID = 0 + const val ROOT_GID = 0 + init { System.loadLibrary("kernelsu") } @@ -84,11 +89,11 @@ object Natives { // these are used for root profile val rootUseDefault: Boolean = true, val rootTemplate: String? = null, - val uid: Int = 0, - val gid: Int = 0, + val uid: Int = ROOT_UID, + val gid: Int = ROOT_GID, val groups: List = mutableListOf(), val capabilities: List = mutableListOf(), - val context: String = "u:r:su:s0", + val context: String = KERNEL_SU_DOMAIN, val namespace: Int = Namespace.INHERITED.ordinal, val nonRootUseDefault: Boolean = true, diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt index 20b28d125caa..55895ec855de 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/TemplateViewModel.kt @@ -47,11 +47,11 @@ class TemplateViewModel : ViewModel() { val local: Boolean = true, val namespace: Int = Natives.Profile.Namespace.INHERITED.ordinal, - val uid: Int = 0, - val gid: Int = 0, + val uid: Int = Natives.ROOT_UID, + val gid: Int = Natives.ROOT_GID, val groups: List = mutableListOf(), val capabilities: List = mutableListOf(), - val context: String = "u:r:su:s0", + val context: String = Natives.KERNEL_SU_DOMAIN, val rules: List = mutableListOf(), ) : Parcelable @@ -140,13 +140,13 @@ private fun JSONArray.mapCatching( } private inline fun > getEnumOrdinals( - jsonArray: JSONArray, enumClass: Class + jsonArray: JSONArray?, enumClass: Class ): List { - return jsonArray.mapCatching({ name -> + return jsonArray?.mapCatching({ name -> enumValueOf(name.uppercase()) }, { Log.e(TAG, "ignore invalid enum ${enumClass.simpleName}: $it", it) - }) + }).orEmpty() } fun getTemplateInfoById(id: String): TemplateViewModel.TemplateInfo? { @@ -171,8 +171,13 @@ private fun getLocaleString(json: JSONObject, key: String): String { private fun fromJSON(templateJson: JSONObject): TemplateViewModel.TemplateInfo? { return runCatching { - val groupsJsonArray = templateJson.getJSONArray("groups") - val capabilitiesJsonArray = templateJson.getJSONArray("capabilities") + val groupsJsonArray = templateJson.optJSONArray("groups") + val capabilitiesJsonArray = templateJson.optJSONArray("capabilities") + val context = templateJson.optString("context").takeIf { it.isNotEmpty() } + ?: Natives.KERNEL_SU_DOMAIN; + val namespace = templateJson.optString("namespace").takeIf { it.isNotEmpty() } + ?: Natives.Profile.Namespace.INHERITED.name + val rulesJsonArray = templateJson.optJSONArray("rules") val templateInfo = TemplateViewModel.TemplateInfo( id = templateJson.getString("id"), @@ -181,15 +186,15 @@ private fun fromJSON(templateJson: JSONObject): TemplateViewModel.TemplateInfo? author = templateJson.optString("author"), local = templateJson.optBoolean("local"), namespace = Natives.Profile.Namespace.valueOf( - templateJson.getString("namespace").uppercase() + namespace.uppercase() ).ordinal, - uid = templateJson.getInt("uid"), - gid = templateJson.getInt("gid"), + uid = templateJson.optInt("uid", Natives.ROOT_UID), + gid = templateJson.optInt("gid", Natives.ROOT_GID), groups = getEnumOrdinals(groupsJsonArray, Groups::class.java).map { it.gid }, capabilities = getEnumOrdinals( capabilitiesJsonArray, Capabilities::class.java ).map { it.cap }, - context = templateJson.getString("context"), + context = context, rules = rulesJsonArray?.mapCatching({ it }, { Log.e(TAG, "ignore invalid rule: $it", it) }).orEmpty()