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