2018-06-12 22:34:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "magisk.h"
|
|
|
|
#include "db.h"
|
2018-11-16 09:20:30 +01:00
|
|
|
#include "daemon.h"
|
2018-06-12 22:34:05 +02:00
|
|
|
|
2018-11-01 18:23:12 +01:00
|
|
|
#define DB_VERSION 7
|
2018-10-28 19:49:04 +01:00
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
static sqlite3 *mDB = nullptr;
|
|
|
|
|
2018-11-05 00:24:08 +01:00
|
|
|
db_strings::db_strings() {
|
|
|
|
memset(data, 0, sizeof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *db_strings::operator[](const char *key) {
|
|
|
|
return data[getKeyIdx(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *db_strings::operator[](const char *key) const {
|
|
|
|
return data[getKeyIdx(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
char *db_strings::operator[](const int idx) {
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *db_strings::operator[](const int idx) const {
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_strings::getKeyIdx(const char *key) const {
|
|
|
|
int idx = DB_STRING_NUM;
|
|
|
|
for (int i = 0; i < DB_STRING_NUM; ++i) {
|
|
|
|
if (strcmp(key, DB_STRING_KEYS[i]) == 0)
|
|
|
|
idx = i;
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
db_settings::db_settings() : data {
|
|
|
|
ROOT_ACCESS_APPS_AND_ADB,
|
|
|
|
MULTIUSER_MODE_OWNER_ONLY,
|
2018-11-16 07:15:34 +01:00
|
|
|
NAMESPACE_MODE_REQUESTER,
|
|
|
|
1
|
2018-11-05 00:24:08 +01:00
|
|
|
} {}
|
|
|
|
|
|
|
|
int &db_settings::operator[](const int idx) {
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
const int &db_settings::operator[](const int idx) const {
|
|
|
|
return data[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int &db_settings::operator[](const char *key) {
|
|
|
|
return data[getKeyIdx(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const int &db_settings::operator[](const char *key) const {
|
|
|
|
return data[getKeyIdx(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
int db_settings::getKeyIdx(const char *key) const {
|
|
|
|
int idx = DB_SETTINGS_NUM;
|
|
|
|
for (int i = 0; i < DB_SETTINGS_NUM; ++i) {
|
|
|
|
if (strcmp(key, DB_SETTING_KEYS[i]) == 0)
|
|
|
|
idx = i;
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:38:06 +01:00
|
|
|
static int ver_cb(void *ver, int, char **data, char **) {
|
|
|
|
*((int *) ver) = atoi(data[0]);
|
2018-06-12 22:34:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
#define err_ret(e) if (e) return e;
|
2018-10-28 19:49:04 +01:00
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
static char *open_and_init_db(sqlite3 *&db) {
|
|
|
|
int ret = sqlite3_open_v2(MAGISKDB, &db,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nullptr);
|
|
|
|
if (ret)
|
2018-12-05 18:48:01 +01:00
|
|
|
return strdup(sqlite3_errmsg(db));
|
2018-10-27 23:54:48 +02:00
|
|
|
int ver, upgrade = 0;
|
2018-10-28 19:49:04 +01:00
|
|
|
char *err;
|
|
|
|
sqlite3_exec(db, "PRAGMA user_version", ver_cb, &ver, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-28 19:49:04 +01:00
|
|
|
if (ver > DB_VERSION) {
|
|
|
|
// Don't support downgrading database
|
2018-12-05 18:48:01 +01:00
|
|
|
sqlite3_close(db);
|
2018-11-04 09:38:06 +01:00
|
|
|
return nullptr;
|
2018-10-28 19:49:04 +01:00
|
|
|
}
|
2018-10-27 23:54:48 +02:00
|
|
|
if (ver < 3) {
|
|
|
|
// Policies
|
|
|
|
sqlite3_exec(db,
|
|
|
|
"CREATE TABLE IF NOT EXISTS policies "
|
|
|
|
"(uid INT, package_name TEXT, policy INT, until INT, "
|
|
|
|
"logging INT, notification INT, PRIMARY KEY(uid))",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-27 23:54:48 +02:00
|
|
|
// Logs
|
|
|
|
sqlite3_exec(db,
|
|
|
|
"CREATE TABLE IF NOT EXISTS logs "
|
|
|
|
"(from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, "
|
|
|
|
"to_uid INT, action INT, time INT, command TEXT)",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-27 23:54:48 +02:00
|
|
|
// Settings
|
|
|
|
sqlite3_exec(db,
|
|
|
|
"CREATE TABLE IF NOT EXISTS settings "
|
|
|
|
"(key TEXT, value INT, PRIMARY KEY(key))",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-27 23:54:48 +02:00
|
|
|
ver = 3;
|
|
|
|
upgrade = 1;
|
|
|
|
}
|
|
|
|
if (ver == 3) {
|
|
|
|
// Strings
|
|
|
|
sqlite3_exec(db,
|
|
|
|
"CREATE TABLE IF NOT EXISTS strings "
|
|
|
|
"(key TEXT, value TEXT, PRIMARY KEY(key))",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-27 23:54:48 +02:00
|
|
|
ver = 4;
|
|
|
|
upgrade = 1;
|
|
|
|
}
|
|
|
|
if (ver == 4) {
|
2018-11-04 09:38:06 +01:00
|
|
|
sqlite3_exec(db, "UPDATE policies SET uid=uid%100000", nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-27 23:54:48 +02:00
|
|
|
/* Skip version 5 */
|
|
|
|
ver = 6;
|
|
|
|
upgrade = 1;
|
|
|
|
}
|
2018-11-01 18:23:12 +01:00
|
|
|
if (ver == 5 || ver == 6) {
|
|
|
|
// Hide list
|
|
|
|
sqlite3_exec(db,
|
|
|
|
"CREATE TABLE IF NOT EXISTS hidelist "
|
|
|
|
"(process TEXT, PRIMARY KEY(process))",
|
2018-11-04 09:38:06 +01:00
|
|
|
nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-11-01 18:23:12 +01:00
|
|
|
ver = 7;
|
|
|
|
upgrade =1 ;
|
|
|
|
}
|
2018-10-27 23:54:48 +02:00
|
|
|
|
|
|
|
if (upgrade) {
|
|
|
|
// Set version
|
|
|
|
char query[32];
|
|
|
|
sprintf(query, "PRAGMA user_version=%d", ver);
|
2018-11-04 09:38:06 +01:00
|
|
|
sqlite3_exec(db, query, nullptr, nullptr, &err);
|
2018-11-16 09:20:30 +01:00
|
|
|
err_ret(err);
|
2018-10-28 19:49:04 +01:00
|
|
|
}
|
2018-11-16 09:20:30 +01:00
|
|
|
return nullptr;
|
2018-10-28 19:49:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
char *db_exec(const char *sql, int (*cb)(void *, int, char**, char**), void *v) {
|
|
|
|
char *err;
|
|
|
|
if (mDB == nullptr) {
|
|
|
|
err = open_and_init_db(mDB);
|
|
|
|
db_err_cmd(err,
|
|
|
|
// Open fails, remove and reconstruct
|
|
|
|
unlink(MAGISKDB);
|
|
|
|
err = open_and_init_db(mDB);
|
|
|
|
err_ret(err);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (mDB) {
|
|
|
|
sqlite3_exec(mDB, sql, cb, v, &err);
|
|
|
|
return err;
|
2018-10-27 23:54:48 +02:00
|
|
|
}
|
2018-11-16 09:20:30 +01:00
|
|
|
return nullptr;
|
2018-10-27 23:54:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:34:05 +02:00
|
|
|
static int settings_cb(void *v, int col_num, char **data, char **col_name) {
|
2018-11-05 00:24:08 +01:00
|
|
|
auto &cfg = *(db_settings *) v;
|
|
|
|
int value = -1;
|
|
|
|
const char *key = "";
|
2018-06-12 22:34:05 +02:00
|
|
|
for (int i = 0; i < col_num; ++i) {
|
|
|
|
if (strcmp(col_name[i], "key") == 0) {
|
2018-11-05 00:24:08 +01:00
|
|
|
key = data[i];
|
2018-06-12 22:34:05 +02:00
|
|
|
} else if (strcmp(col_name[i], "value") == 0) {
|
|
|
|
value = atoi(data[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 00:24:08 +01:00
|
|
|
if (key[0] && value >= 0) {
|
|
|
|
cfg[key] = value;
|
|
|
|
LOGD("magiskdb: query %s=[%d]\n", key, value);
|
2018-06-12 22:34:05 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
int get_db_settings(db_settings *dbs, int key) {
|
2018-10-27 23:54:48 +02:00
|
|
|
char *err;
|
2018-11-20 07:50:31 +01:00
|
|
|
if (key >= 0) {
|
2018-10-27 23:54:48 +02:00
|
|
|
char query[128];
|
2018-10-27 23:56:20 +02:00
|
|
|
sprintf(query, "SELECT key, value FROM settings WHERE key='%s'", DB_SETTING_KEYS[key]);
|
2018-11-16 09:20:30 +01:00
|
|
|
err = db_exec(query, settings_cb, dbs);
|
2018-10-27 23:54:48 +02:00
|
|
|
} else {
|
2018-11-16 09:20:30 +01:00
|
|
|
err = db_exec("SELECT key, value FROM settings", settings_cb, dbs);
|
2018-10-27 23:54:48 +02:00
|
|
|
}
|
2018-11-16 09:20:30 +01:00
|
|
|
db_err_cmd(err, return 1);
|
2018-10-27 23:54:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-12 22:34:05 +02:00
|
|
|
static int strings_cb(void *v, int col_num, char **data, char **col_name) {
|
2018-11-05 00:24:08 +01:00
|
|
|
auto &str = *(db_strings *) v;
|
|
|
|
const char *key = "", *value = "";
|
2018-06-12 22:34:05 +02:00
|
|
|
for (int i = 0; i < col_num; ++i) {
|
|
|
|
if (strcmp(col_name[i], "key") == 0) {
|
2018-11-05 00:24:08 +01:00
|
|
|
key = data[i];
|
2018-06-12 22:34:05 +02:00
|
|
|
} else if (strcmp(col_name[i], "value") == 0) {
|
|
|
|
value = data[i];
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 00:24:08 +01:00
|
|
|
if (key[0] && value[0]) {
|
|
|
|
strcpy(str[key], value);
|
|
|
|
LOGD("magiskdb: query %s=[%s]\n", key, value);
|
2018-06-12 22:34:05 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
int get_db_strings(db_strings *str, int key) {
|
2018-06-12 22:34:05 +02:00
|
|
|
char *err;
|
2018-11-20 07:50:31 +01:00
|
|
|
if (key >= 0) {
|
2018-06-12 22:34:05 +02:00
|
|
|
char query[128];
|
2018-10-27 23:56:20 +02:00
|
|
|
sprintf(query, "SELECT key, value FROM strings WHERE key='%s'", DB_STRING_KEYS[key]);
|
2018-11-16 09:20:30 +01:00
|
|
|
err = db_exec(query, strings_cb, str);
|
2018-06-12 22:34:05 +02:00
|
|
|
} else {
|
2018-11-16 09:20:30 +01:00
|
|
|
err = db_exec("SELECT key, value FROM strings", strings_cb, str);
|
2018-06-12 22:34:05 +02:00
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
LOGE("sqlite3_exec: %s\n", err);
|
2018-10-28 03:06:24 +01:00
|
|
|
sqlite3_free(err);
|
2018-06-12 22:34:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-27 23:54:48 +02:00
|
|
|
static int policy_cb(void *v, int col_num, char **data, char **col_name) {
|
2018-11-04 09:38:06 +01:00
|
|
|
auto su = (su_access *) v;
|
2018-10-27 23:54:48 +02:00
|
|
|
for (int i = 0; i < col_num; i++) {
|
|
|
|
if (strcmp(col_name[i], "policy") == 0)
|
|
|
|
su->policy = (policy_t) atoi(data[i]);
|
|
|
|
else if (strcmp(col_name[i], "logging") == 0)
|
|
|
|
su->log = atoi(data[i]);
|
|
|
|
else if (strcmp(col_name[i], "notification") == 0)
|
|
|
|
su->notify = atoi(data[i]);
|
|
|
|
}
|
|
|
|
LOGD("magiskdb: query policy=[%d] log=[%d] notify=[%d]\n", su->policy, su->log, su->notify);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
int get_uid_policy(int uid, struct su_access *su) {
|
2018-06-12 22:34:05 +02:00
|
|
|
char query[256], *err;
|
|
|
|
sprintf(query, "SELECT policy, logging, notification FROM policies "
|
2018-11-04 09:38:06 +01:00
|
|
|
"WHERE uid=%d AND (until=0 OR until>%li)", uid, time(nullptr));
|
2018-11-16 09:20:30 +01:00
|
|
|
err = db_exec(query, policy_cb, su);
|
|
|
|
db_err_cmd(err, return 1);
|
2018-06-12 22:34:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-27 23:54:48 +02:00
|
|
|
int validate_manager(char *alt_pkg, int userid, struct stat *st) {
|
2018-11-04 09:38:06 +01:00
|
|
|
if (st == nullptr) {
|
2018-06-12 22:34:05 +02:00
|
|
|
struct stat stat;
|
|
|
|
st = &stat;
|
|
|
|
}
|
|
|
|
// Prefer DE storage
|
|
|
|
const char *base = access("/data/user_de", F_OK) == 0 ? "/data/user_de" : "/data/user";
|
|
|
|
char app_path[128];
|
2018-10-27 23:54:48 +02:00
|
|
|
sprintf(app_path, "%s/%d/%s", base, userid, alt_pkg[0] ? alt_pkg : "xxx");
|
2018-06-12 22:34:05 +02:00
|
|
|
if (stat(app_path, st)) {
|
|
|
|
// Check the official package name
|
2018-11-04 09:38:06 +01:00
|
|
|
sprintf(app_path, "%s/%d/" JAVA_PACKAGE_NAME, base, userid);
|
2018-06-12 22:34:05 +02:00
|
|
|
if (stat(app_path, st)) {
|
|
|
|
LOGE("su: cannot find manager");
|
|
|
|
memset(st, 0, sizeof(*st));
|
2018-10-27 23:54:48 +02:00
|
|
|
alt_pkg[0] = '\0';
|
2018-06-12 22:34:05 +02:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// Switch to official package if exists
|
2018-10-27 23:54:48 +02:00
|
|
|
strcpy(alt_pkg, JAVA_PACKAGE_NAME);
|
2018-06-12 22:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-27 23:54:48 +02:00
|
|
|
|
|
|
|
static int print_cb(void *v, int col_num, char **data, char **col_name) {
|
2018-11-16 09:20:30 +01:00
|
|
|
FILE *out = (FILE *) v;
|
2018-10-27 23:54:48 +02:00
|
|
|
for (int i = 0; i < col_num; ++i) {
|
2018-11-16 09:20:30 +01:00
|
|
|
if (i) fprintf(out, "|");
|
|
|
|
fprintf(out, "%s=%s", col_name[i], data[i]);
|
2018-10-27 23:54:48 +02:00
|
|
|
}
|
2018-11-16 09:20:30 +01:00
|
|
|
fprintf(out, "\n");
|
2018-10-27 23:54:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:20:30 +01:00
|
|
|
void exec_sql(int client) {
|
|
|
|
char *sql = read_string(client);
|
|
|
|
FILE *out = fdopen(recv_fd(client), "a");
|
|
|
|
char *err = db_exec(sql, print_cb, out);
|
|
|
|
free(sql);
|
|
|
|
fclose(out);
|
|
|
|
db_err_cmd(err,
|
|
|
|
write_int(client, 1);
|
|
|
|
return;
|
|
|
|
);
|
|
|
|
write_int(client, 0);
|
2018-10-27 23:54:48 +02:00
|
|
|
}
|