Skip to content

Commit

Permalink
assets: allow to use default uid, gid, groups, capabilities and context
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Oct 21, 2023
1 parent 34b64b8 commit 4ff9dca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
11 changes: 8 additions & 3 deletions manager/app/src/main/java/me/weishu/kernelsu/Natives.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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<Int> = mutableListOf(),
val capabilities: List<Int> = mutableListOf(),
val context: String = "u:r:su:s0",
val context: String = KERNEL_SU_DOMAIN,
val namespace: Int = Namespace.INHERITED.ordinal,

val nonRootUseDefault: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = mutableListOf(),
val capabilities: List<Int> = mutableListOf(),
val context: String = "u:r:su:s0",
val context: String = Natives.KERNEL_SU_DOMAIN,
val rules: List<String> = mutableListOf(),
) : Parcelable

Expand Down Expand Up @@ -140,13 +140,13 @@ private fun <T, R> JSONArray.mapCatching(
}

private inline fun <reified T : Enum<T>> getEnumOrdinals(
jsonArray: JSONArray, enumClass: Class<T>
jsonArray: JSONArray?, enumClass: Class<T>
): List<T> {
return jsonArray.mapCatching<String, T>({ name ->
return jsonArray?.mapCatching<String, T>({ name ->
enumValueOf(name.uppercase())
}, {
Log.e(TAG, "ignore invalid enum ${enumClass.simpleName}: $it", it)
})
}).orEmpty()
}

fun getTemplateInfoById(id: String): TemplateViewModel.TemplateInfo? {
Expand All @@ -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"),
Expand All @@ -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<String, String>({ it }, {
Log.e(TAG, "ignore invalid rule: $it", it)
}).orEmpty()
Expand Down

0 comments on commit 4ff9dca

Please sign in to comment.