Magisk/db.c

118 lines
3.1 KiB
C
Raw Normal View History

/*
2017-05-31 19:19:45 +00:00
** Copyright 2017, John Wu (@topjohnwu)
** Copyright 2013, Koushik Dutta (@koush)
**
*/
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <sqlite3.h>
#include <time.h>
2017-04-14 19:21:31 +00:00
#include <string.h>
#include <unistd.h>
2017-04-14 19:21:31 +00:00
#include "magisk.h"
#include "su.h"
2017-05-31 19:19:45 +00:00
static int policy_callback(void *v, int argc, char **argv, char **azColName) {
struct su_context *ctx = (struct su_context *) v;
policy_t policy = QUERY;
2017-04-14 19:21:31 +00:00
time_t until = 0;
2017-05-31 19:19:45 +00:00
for (int i = 0; i < argc; i++) {
if (strcmp(azColName[i], "policy") == 0)
policy = atoi(argv[i]);
else if (strcmp(azColName[i], "until") == 0)
until = atol(argv[i]);
2017-04-14 19:21:31 +00:00
}
if (policy == DENY)
ctx->info->policy = DENY;
else if (policy == ALLOW && (until == 0 || until > time(NULL)))
ctx->info->policy = ALLOW;
2017-01-23 14:51:00 +00:00
2017-05-31 19:19:45 +00:00
LOGD("su_db: query policy=[%d]\n", ctx->info->policy);
return 0;
}
static int settings_callback(void *v, int argc, char **argv, char **azColName) {
struct su_context *ctx = (struct su_context *) v;
int *target, value;
char *entry;
for (int i = 0; i < argc; ++i) {
if (strcmp(azColName[i], "key") == 0) {
if (strcmp(argv[i], ROOT_ACCESS_ENTRY) == 0)
target = &ctx->info->root_access;
else if (strcmp(argv[i], MULTIUSER_MODE_ENTRY) == 0)
target = &ctx->info->multiuser_mode;
2017-06-08 14:50:39 +00:00
else if (strcmp(argv[i], NAMESPACE_MODE_ENTRY) == 0)
target = &ctx->info->mnt_ns;
2017-05-31 19:19:45 +00:00
entry = argv[i];
} else if (strcmp(azColName[i], "value") == 0) {
value = atoi(argv[i]);
}
}
LOGD("su_db: query %s=[%d]\n", entry, value);
*target = value;
2017-04-14 19:21:31 +00:00
return 0;
}
void database_check(struct su_context *ctx) {
2017-04-14 19:21:31 +00:00
sqlite3 *db = NULL;
2017-08-06 16:31:27 +00:00
int ret;
char query[512], *err = NULL;
2017-05-31 19:19:45 +00:00
// Set default values
ctx->info->root_access = ROOT_ACCESS_APPS_AND_ADB;
ctx->info->multiuser_mode = MULTIUSER_MODE_OWNER_ONLY;
2017-06-08 14:50:39 +00:00
ctx->info->mnt_ns = NAMESPACE_MODE_REQUESTER;
2017-05-31 19:19:45 +00:00
ctx->info->policy = QUERY;
// Open database
2017-09-15 10:01:31 +00:00
ret = sqlite3_open_v2(ctx->path.base_db, &db, SQLITE_OPEN_READONLY, NULL);
2017-04-14 19:21:31 +00:00
if (ret) {
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
2017-04-14 19:21:31 +00:00
sqlite3_close(db);
return;
2017-04-14 19:21:31 +00:00
}
2017-08-06 16:31:27 +00:00
// Check multiuser mode settings
snprintf(query, sizeof(query), "SELECT key, value FROM settings WHERE key='%s'", MULTIUSER_MODE_ENTRY);
sqlite3_exec(db, query, settings_callback, ctx, &err);
err = NULL;
2017-09-15 10:01:31 +00:00
if (ctx->info->uid / 100000 != 0 && ctx->info->multiuser_mode == MULTIUSER_MODE_USER) {
2017-08-06 16:31:27 +00:00
sqlite3_close(db);
// Open database
2017-09-15 10:01:31 +00:00
ret = sqlite3_open_v2(ctx->path.multiuser_db, &db, SQLITE_OPEN_READONLY, NULL);
2017-08-06 16:31:27 +00:00
if (ret) {
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
sqlite3_close(db);
return;
}
}
2017-05-31 19:19:45 +00:00
// Query for policy
snprintf(query, sizeof(query), "SELECT policy, until FROM policies WHERE uid=%d", ctx->info->uid % 100000);
sqlite3_exec(db, query, policy_callback, ctx, &err);
2017-08-06 16:31:27 +00:00
if (err != NULL) {
LOGE("sqlite3_exec: %s\n", err);
2017-08-06 16:31:27 +00:00
return;
}
2017-05-31 19:19:45 +00:00
err = NULL;
// Query for settings
2017-08-06 16:31:27 +00:00
snprintf(query, sizeof(query), "SELECT key, value FROM settings WHERE key!='%s'", MULTIUSER_MODE_ENTRY);
2017-05-31 19:19:45 +00:00
sqlite3_exec(db, query, settings_callback, ctx, &err);
2017-08-06 16:31:27 +00:00
if (err != NULL) {
2017-05-31 19:19:45 +00:00
LOGE("sqlite3_exec: %s\n", err);
2017-08-06 16:31:27 +00:00
return;
}
2017-05-31 19:19:45 +00:00
sqlite3_close(db);
}