|
| 1 | +#include <stdlib.h> |
| 2 | +#include <string.h> |
| 3 | +#include <sys/stat.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <errno.h> |
| 6 | + |
| 7 | +#include "../constants.h" |
| 8 | +#include "../utils.h" |
| 9 | + |
| 10 | +#include "apatch.h" |
| 11 | + |
| 12 | +enum RootImplState apatch_get_existence(void) { |
| 13 | + struct stat s; |
| 14 | + if (stat("/data/adb/apd", &s) != 0) { |
| 15 | + LOGE("APATCH | Failed to stat /data/adb/apd: %s\n", strerror(errno)); |
| 16 | + |
| 17 | + return Inexistent; |
| 18 | + } |
| 19 | + |
| 20 | + char apatch_version[32]; |
| 21 | + char *const argv[] = { "apd", "-V", NULL }; |
| 22 | + |
| 23 | + LOGI("APATCH | Checking for apd existence\n"); |
| 24 | + if (!exec_command(apatch_version, sizeof(apatch_version), "/data/adb/apd", argv)) { |
| 25 | + LOGE("APATCH | Failed to execute apd binary: %s\n", strerror(errno)); |
| 26 | + errno = 0; |
| 27 | + |
| 28 | + return Inexistent; |
| 29 | + } |
| 30 | + |
| 31 | + int version = atoi(apatch_version + strlen("apd ")); |
| 32 | + LOGI("APATCH | apd version: %d\n", version); |
| 33 | + if (version == 0) return Abnormal; |
| 34 | + |
| 35 | + if (version >= MIN_APATCH_VERSION && version <= 999999) return Supported; |
| 36 | + if (version >= 1 && version <= MIN_APATCH_VERSION - 1) return TooOld; |
| 37 | + |
| 38 | + return Inexistent; |
| 39 | +} |
| 40 | + |
| 41 | +struct package_config { |
| 42 | + uid_t uid; |
| 43 | + bool root_granted; |
| 44 | + bool umount_needed; |
| 45 | +}; |
| 46 | + |
| 47 | +struct packages_config { |
| 48 | + struct package_config *configs; |
| 49 | + size_t size; |
| 50 | +}; |
| 51 | + |
| 52 | +bool _apatch_get_package_config(struct packages_config *config) { |
| 53 | + FILE *fp = fopen("/data/adb/ap/package_config", "r"); |
| 54 | + if (fp == NULL) { |
| 55 | + LOGE("APATCH | Failed to open package_config: %s\n", strerror(errno)); |
| 56 | + |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + char line[256]; |
| 61 | + /* INFO: Skip the CSV header */ |
| 62 | + fgets(line, sizeof(line), fp); |
| 63 | + |
| 64 | + while (fgets(line, sizeof(line), fp) != NULL) { |
| 65 | + config->configs = realloc(config, (config->size + 1) * sizeof(struct package_config)); |
| 66 | + if (config->configs == NULL) { |
| 67 | + LOGE("APATCH | Failed to realloc package config: %s\n", strerror(errno)); |
| 68 | + |
| 69 | + fclose(fp); |
| 70 | + |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + strtok(line, ","); |
| 75 | + |
| 76 | + char *exclude_str = strtok(NULL, ","); |
| 77 | + if (exclude_str == NULL) continue; |
| 78 | + |
| 79 | + char *allow_str = strtok(NULL, ","); |
| 80 | + if (allow_str == NULL) continue; |
| 81 | + |
| 82 | + char *uid_str = strtok(NULL, ","); |
| 83 | + if (uid_str == NULL) continue; |
| 84 | + |
| 85 | + config->configs[config->size].uid = atoi(uid_str); |
| 86 | + config->configs[config->size].root_granted = strcmp(allow_str, "1") == 0; |
| 87 | + config->configs[config->size].umount_needed = strcmp(exclude_str, "1") == 0; |
| 88 | + |
| 89 | + config->size++; |
| 90 | + } |
| 91 | + |
| 92 | + fclose(fp); |
| 93 | + |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +bool apatch_uid_granted_root(uid_t uid) { |
| 98 | + struct packages_config *config = NULL; |
| 99 | + if (!_apatch_get_package_config(config)) { |
| 100 | + free(config); |
| 101 | + |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + for (size_t i = 0; i < config->size; i++) { |
| 106 | + if (config->configs[i].uid == uid) { |
| 107 | + free(config); |
| 108 | + |
| 109 | + return config->configs[i].root_granted; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + free(config); |
| 114 | + |
| 115 | + return false; |
| 116 | +} |
| 117 | + |
| 118 | +bool apatch_uid_should_umount(uid_t uid) { |
| 119 | + struct packages_config *config = NULL; |
| 120 | + if (!_apatch_get_package_config(config)) { |
| 121 | + free(config); |
| 122 | + |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + for (size_t i = 0; i < config->size; i++) { |
| 127 | + if (config->configs[i].uid == uid) { |
| 128 | + free(config); |
| 129 | + |
| 130 | + return config->configs[i].umount_needed; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + free(config); |
| 135 | + |
| 136 | + return false; |
| 137 | +} |
| 138 | + |
| 139 | +bool apatch_uid_is_manager(uid_t uid) { |
| 140 | + struct stat s; |
| 141 | + if (stat("/data/user_de/0/me.bmax.apatch", &s) == -1) return false; |
| 142 | + |
| 143 | + return s.st_uid == uid; |
| 144 | +} |
0 commit comments