Magisk/native/jni/include/db.hpp

158 lines
2.7 KiB
C++
Raw Normal View History

2019-03-06 14:16:12 +01:00
#pragma once
2018-06-13 23:09:54 +02:00
#include <sys/stat.h>
2019-03-06 14:16:12 +01:00
#include <map>
#include <string>
#include <string_view>
#include <functional>
2019-03-06 14:16:12 +01:00
template <class T, size_t num>
class db_data_base {
public:
T& operator [](std::string_view key) {
return data[getKeyIdx(key)];
}
const T& operator [](std::string_view key) const {
return data[getKeyIdx(key)];
}
T& operator [](int key) {
return data[key];
}
const T& operator [](int key) const {
return data[key];
}
protected:
T data[num + 1];
virtual int getKeyIdx(std::string_view key) const = 0;
};
/***************
* DB Settings *
***************/
2018-10-04 07:49:52 +02:00
#define DB_SETTING_KEYS \
2018-11-01 18:23:12 +01:00
((const char *[]) { \
"root_access", \
"multiuser_mode", \
2018-11-16 07:15:34 +01:00
"mnt_ns", \
"magiskhide", \
})
2018-11-16 07:15:34 +01:00
#define DB_SETTINGS_NUM 4
2018-11-05 00:24:08 +01:00
// Settings keys
enum {
ROOT_ACCESS = 0,
SU_MULTIUSER_MODE,
2018-11-16 07:15:34 +01:00
SU_MNT_NS,
HIDE_CONFIG
};
// Values for root_access
enum {
ROOT_ACCESS_DISABLED = 0,
ROOT_ACCESS_APPS_ONLY,
ROOT_ACCESS_ADB_ONLY,
ROOT_ACCESS_APPS_AND_ADB
};
// Values for multiuser_mode
enum {
MULTIUSER_MODE_OWNER_ONLY = 0,
MULTIUSER_MODE_OWNER_MANAGED,
MULTIUSER_MODE_USER
};
// Values for mnt_ns
enum {
NAMESPACE_MODE_GLOBAL = 0,
NAMESPACE_MODE_REQUESTER,
NAMESPACE_MODE_ISOLATE
};
2019-03-06 14:16:12 +01:00
class db_settings : public db_data_base<int, DB_SETTINGS_NUM> {
2018-11-05 00:24:08 +01:00
public:
db_settings();
2019-03-06 14:16:12 +01:00
protected:
int getKeyIdx(std::string_view key) const override;
};
/**************
* DB Strings *
**************/
2018-10-04 07:49:52 +02:00
#define DB_STRING_KEYS \
2018-11-01 18:23:12 +01:00
((const char *[]) { \
2018-10-04 07:49:52 +02:00
"requester", \
})
2018-11-04 09:38:06 +01:00
#define DB_STRING_NUM 1
2018-11-05 00:24:08 +01:00
// Strings keys
enum {
2018-06-13 20:47:43 +02:00
SU_MANAGER = 0
};
2019-03-06 14:16:12 +01:00
class db_strings : public db_data_base<std::string, DB_STRING_NUM> {
protected:
int getKeyIdx(std::string_view key) const override;
};
/*************
* SU Access *
*************/
typedef enum {
QUERY = 0,
DENY = 1,
ALLOW = 2,
} policy_t;
struct su_access {
policy_t policy;
int log;
int notify;
};
2018-11-04 09:38:06 +01:00
#define DEFAULT_SU_ACCESS (su_access) { \
.policy = QUERY, \
.log = 1, \
.notify = 1 \
}
2018-11-04 09:38:06 +01:00
#define SILENT_SU_ACCESS (su_access) { \
.policy = ALLOW, \
.log = 0, \
.notify = 0 \
}
2018-11-04 09:38:06 +01:00
#define NO_SU_ACCESS (su_access) { \
.policy = DENY, \
.log = 0, \
.notify = 0 \
}
/********************
* Public Functions *
********************/
2019-03-06 14:16:12 +01:00
typedef std::map<std::string_view, std::string_view> db_row;
typedef std::function<bool(db_row&)> db_row_cb;
int get_db_settings(db_settings &cfg, int key = -1);
int get_db_strings(db_strings &str, int key = -1);
int get_uid_policy(su_access &su, int uid);
bool check_manager(std::string *pkg = nullptr);
bool validate_manager(std::string &pkg, int userid, struct stat *st);
void exec_sql(int client);
2019-03-06 14:16:12 +01:00
char *db_exec(const char *sql);
char *db_exec(const char *sql, const db_row_cb &fn);
2020-01-10 20:20:59 +01:00
bool db_err(char *e);
2020-01-10 20:20:59 +01:00
#define db_err_cmd(e, cmd) if (db_err(e)) { cmd; }