Magisk/native/jni/include/db.hpp

138 lines
2.7 KiB
C++
Raw Normal View History

2019-03-06 13:16:12 +00:00
#pragma once
2018-06-13 21:09:54 +00:00
#include <sys/stat.h>
2019-03-06 13:16:12 +00:00
#include <map>
#include <string>
#include <string_view>
#include <functional>
2021-08-27 08:06:03 +00:00
template <class T, size_t N>
class db_dict {
2019-03-06 13:16:12 +00:00
public:
T& operator [](std::string_view key) {
2021-08-27 08:06:03 +00:00
return data[get_idx(key)];
}
2019-03-06 13:16:12 +00:00
const T& operator [](std::string_view key) const {
2021-08-27 08:06:03 +00:00
return data[get_idx(key)];
}
2019-03-06 13:16:12 +00:00
T& operator [](int key) {
return data[key];
}
2019-03-06 13:16:12 +00:00
const T& operator [](int key) const {
return data[key];
}
2019-03-06 13:16:12 +00:00
protected:
2021-08-27 08:06:03 +00:00
T data[N + 1];
virtual int get_idx(std::string_view key) const = 0;
2019-03-06 13:16:12 +00:00
};
/***************
* DB Settings *
***************/
2021-08-27 08:06:03 +00:00
constexpr const char *DB_SETTING_KEYS[] = {
"root_access",
"multiuser_mode",
"mnt_ns",
2021-09-15 09:49:54 +00:00
"denylist",
"zygisk"
2021-08-27 08:06:03 +00:00
};
2021-08-27 08:06:03 +00:00
// Settings key indices
enum {
ROOT_ACCESS = 0,
SU_MULTIUSER_MODE,
SU_MNT_NS,
2021-09-15 09:49:54 +00:00
DENYLIST_CONFIG,
ZYGISK_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
};
2021-08-27 08:06:03 +00:00
class db_settings : public db_dict<int, std::size(DB_SETTING_KEYS)> {
2018-11-04 23:24:08 +00:00
public:
db_settings();
2019-03-06 13:16:12 +00:00
protected:
2021-08-27 08:06:03 +00:00
int get_idx(std::string_view key) const override;
};
/**************
* DB Strings *
**************/
2021-08-27 08:06:03 +00:00
constexpr const char *DB_STRING_KEYS[] = { "requester" };
2021-08-27 08:06:03 +00:00
// Strings keys indices
enum {
SU_MANAGER = 0
};
2021-08-27 08:06:03 +00:00
class db_strings : public db_dict<std::string, std::size(DB_STRING_KEYS)> {
2019-03-06 13:16:12 +00:00
protected:
2021-08-27 08:06:03 +00:00
int get_idx(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;
};
2021-08-27 08:06:03 +00:00
#define DEFAULT_SU_ACCESS { QUERY, 1, 1 }
#define SILENT_SU_ACCESS { ALLOW, 0, 0 }
#define NO_SU_ACCESS { DENY, 0, 0 }
/********************
* Public Functions *
********************/
2021-08-27 08:06:03 +00:00
using db_row = std::map<std::string_view, std::string_view>;
using db_row_cb = std::function<bool(db_row&)>;
2019-03-06 13:16:12 +00:00
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);
2021-09-17 09:07:32 +00:00
bool get_manager(int user_id, std::string *pkg, struct stat *st);
bool get_manager(std::string *pkg = nullptr);
2021-09-18 09:38:53 +00:00
int get_manager_app_id();
void exec_sql(int client);
2019-03-06 13:16:12 +00:00
char *db_exec(const char *sql);
char *db_exec(const char *sql, const db_row_cb &fn);
2020-01-10 19:20:59 +00:00
bool db_err(char *e);
2020-01-10 19:20:59 +00:00
#define db_err_cmd(e, cmd) if (db_err(e)) { cmd; }