2018-07-18 10:12:47 +00:00
|
|
|
/*
|
|
|
|
** 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>
|
2017-05-29 10:54:33 +00:00
|
|
|
#include <unistd.h>
|
2018-07-18 10:12:47 +00:00
|
|
|
|
2017-04-14 19:21:31 +00:00
|
|
|
#include "magisk.h"
|
2018-07-18 10:12:47 +00:00
|
|
|
#include "su.h"
|
|
|
|
|
2017-05-29 10:54:33 +00:00
|
|
|
static int database_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-29 10:54:33 +00:00
|
|
|
for(int i = 0; i < argc; i++) {
|
2017-04-14 19:21:31 +00:00
|
|
|
if (strcmp(azColName[i], "policy") == 0) {
|
|
|
|
if (argv[i] != NULL) {
|
|
|
|
policy = atoi(argv[i]);
|
|
|
|
}
|
2017-05-29 10:54:33 +00:00
|
|
|
} else if (strcmp(azColName[i], "until") == 0) {
|
2017-04-14 19:21:31 +00:00
|
|
|
if (argv[i] != NULL) {
|
|
|
|
until = atol(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 10:12:47 +00:00
|
|
|
|
2017-05-29 10:54:33 +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-04-14 19:21:31 +00:00
|
|
|
return 0;
|
2018-07-18 10:12:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 10:54:33 +00:00
|
|
|
void database_check(struct su_context *ctx) {
|
2017-04-14 19:21:31 +00:00
|
|
|
sqlite3 *db = NULL;
|
2017-05-29 10:54:33 +00:00
|
|
|
|
|
|
|
// Check if file is readable
|
|
|
|
if (access(ctx->user.database_path, R_OK) == -1)
|
|
|
|
return;
|
2017-04-14 19:21:31 +00:00
|
|
|
|
|
|
|
char query[512];
|
2017-05-29 10:54:33 +00:00
|
|
|
snprintf(query, sizeof(query), "SELECT policy, until FROM policies WHERE uid=%d", ctx->info->uid % 100000);
|
2017-04-14 19:21:31 +00:00
|
|
|
int ret = sqlite3_open_v2(ctx->user.database_path, &db, SQLITE_OPEN_READONLY, NULL);
|
|
|
|
if (ret) {
|
2017-05-29 10:54:33 +00:00
|
|
|
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
|
2017-04-14 19:21:31 +00:00
|
|
|
sqlite3_close(db);
|
2017-05-29 10:54:33 +00:00
|
|
|
return;
|
2017-04-14 19:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *err = NULL;
|
2017-05-29 10:54:33 +00:00
|
|
|
ret = sqlite3_exec(db, query, database_callback, ctx, &err);
|
2017-04-14 19:21:31 +00:00
|
|
|
sqlite3_close(db);
|
|
|
|
if (err != NULL) {
|
2017-05-29 10:54:33 +00:00
|
|
|
LOGE("sqlite3_exec: %s\n", err);
|
|
|
|
ctx->info->policy = DENY;
|
2017-04-14 19:21:31 +00:00
|
|
|
}
|
2018-07-18 10:12:47 +00:00
|
|
|
}
|