Magisk/db.c

65 lines
1.5 KiB
C
Raw Normal View History

/*
** 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"
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;
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]);
}
} else if (strcmp(azColName[i], "until") == 0) {
2017-04-14 19:21:31 +00:00
if (argv[i] != NULL) {
until = atol(argv[i]);
}
}
}
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;
}
void database_check(struct su_context *ctx) {
2017-04-14 19:21:31 +00:00
sqlite3 *db = NULL;
// 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];
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) {
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
}
char *err = NULL;
ret = sqlite3_exec(db, query, database_callback, ctx, &err);
2017-04-14 19:21:31 +00:00
sqlite3_close(db);
if (err != NULL) {
LOGE("sqlite3_exec: %s\n", err);
ctx->info->policy = DENY;
2017-04-14 19:21:31 +00:00
}
}