Magisk/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java

266 lines
9.4 KiB
Java
Raw Normal View History

2017-02-12 23:26:30 +08:00
package com.topjohnwu.magisk.database;
2017-06-01 03:18:41 +08:00
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
2017-07-24 00:34:34 +08:00
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
2017-07-24 00:34:34 +08:00
import android.text.TextUtils;
2017-06-01 00:19:52 +08:00
import com.topjohnwu.magisk.MagiskManager;
2017-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.Policy;
import com.topjohnwu.magisk.container.SuLogEntry;
2017-06-01 00:19:52 +08:00
import com.topjohnwu.magisk.utils.Utils;
2017-02-12 23:26:30 +08:00
2017-06-01 00:19:52 +08:00
import java.io.File;
2017-07-24 00:34:34 +08:00
import java.text.DateFormat;
import java.util.ArrayList;
2017-06-01 00:19:52 +08:00
import java.util.Collections;
2017-07-24 00:34:34 +08:00
import java.util.Date;
import java.util.List;
public class SuDatabaseHelper extends SQLiteOpenHelper {
2017-06-01 03:18:41 +08:00
public static final String ROOT_ACCESS = "root_access";
2017-08-29 01:34:42 +08:00
public static final int ROOT_ACCESS_DISABLED = 0;
public static final int ROOT_ACCESS_APPS_ONLY = 1;
public static final int ROOT_ACCESS_ADB_ONLY = 2;
public static final int ROOT_ACCESS_APPS_AND_ADB = 3;
2017-06-01 03:18:41 +08:00
public static final String MULTIUSER_MODE = "multiuser_mode";
2017-08-29 01:34:42 +08:00
public static final int MULTIUSER_MODE_OWNER_ONLY = 0;
public static final int MULTIUSER_MODE_OWNER_MANAGED = 1;
public static final int MULTIUSER_MODE_USER = 2;
2017-06-08 22:27:24 +08:00
public static final String MNT_NS = "mnt_ns";
2017-08-29 01:34:42 +08:00
public static final int NAMESPACE_MODE_GLOBAL = 0;
public static final int NAMESPACE_MODE_REQUESTER = 1;
public static final int NAMESPACE_MODE_ISOLATE = 2;
2017-06-01 03:18:41 +08:00
2017-09-15 18:03:25 +08:00
public static final String DB_NAME = "su.db";
2017-07-24 01:20:03 +08:00
private static final int DATABASE_VER = 3;
2017-06-01 00:19:52 +08:00
private static final String POLICY_TABLE = "policies";
private static final String LOG_TABLE = "logs";
private static final String SETTINGS_TABLE = "settings";
2017-09-15 18:03:25 +08:00
private MagiskManager mm;
2017-06-01 00:19:52 +08:00
private PackageManager pm;
2017-07-21 00:46:13 +08:00
private SQLiteDatabase mDb;
public SuDatabaseHelper(Context context) {
2017-09-15 18:03:25 +08:00
super(context, DB_NAME, null, DATABASE_VER);
mm = Utils.getMagiskManager(context);
2017-06-01 00:19:52 +08:00
pm = context.getPackageManager();
2017-07-21 00:46:13 +08:00
mDb = getWritableDatabase();
2017-06-20 18:25:18 +08:00
cleanup();
}
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 0) {
2017-06-01 00:19:52 +08:00
createTables(db);
oldVersion = 2;
}
if (oldVersion == 1) {
// We're dropping column app_name, rename and re-construct table
db.execSQL("ALTER TABLE " + POLICY_TABLE + " RENAME TO " + POLICY_TABLE + "_old");
// Create the new tables
createTables(db);
// Migrate old data to new tables
db.execSQL(
2017-06-01 00:19:52 +08:00
"INSERT INTO " + POLICY_TABLE + " SELECT " +
"uid, package_name, policy, until, logging, notification " +
"FROM " + POLICY_TABLE + "_old");
db.execSQL("DROP TABLE " + POLICY_TABLE + "_old");
2017-09-15 18:03:25 +08:00
File oldDB = mm.getDatabasePath("sulog.db");
2017-06-01 00:19:52 +08:00
if (oldDB.exists()) {
migrateLegacyLogList(oldDB, db);
2017-09-15 18:03:25 +08:00
mm.deleteDatabase("sulog.db");
2017-06-01 00:19:52 +08:00
}
++oldVersion;
}
2017-07-24 01:20:03 +08:00
if (oldVersion == 2) {
db.execSQL("UPDATE " + LOG_TABLE + " SET time=time*1000");
++oldVersion;
}
}
2017-06-01 00:19:52 +08:00
private void createTables(SQLiteDatabase db) {
// Policies
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + POLICY_TABLE + " " +
"(uid INT, package_name TEXT, policy INT, " +
"until INT, logging INT, notification INT, " +
"PRIMARY KEY(uid))");
// Logs
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + LOG_TABLE + " " +
"(from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " +
"to_uid INT, action INT, time INT, command TEXT)");
// Settings
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + SETTINGS_TABLE + " " +
"(key TEXT, value INT, PRIMARY KEY(key))");
}
2017-06-20 18:25:18 +08:00
private void cleanup() {
// Clear outdated policies
2017-07-21 00:46:13 +08:00
mDb.delete(POLICY_TABLE, "until > 0 AND until < ?",
new String[] { String.valueOf(System.currentTimeMillis() / 1000) });
2017-06-20 18:25:18 +08:00
// Clear outdated logs
2017-07-21 00:46:13 +08:00
mDb.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf(
2017-09-15 18:03:25 +08:00
System.currentTimeMillis() - mm.suLogTimeout * 86400000) });
2017-06-20 18:25:18 +08:00
}
2017-06-01 00:19:52 +08:00
public void deletePolicy(Policy policy) {
deletePolicy(policy.packageName);
}
public void deletePolicy(String pkg) {
2017-07-21 00:46:13 +08:00
mDb.delete(POLICY_TABLE, "package_name=?", new String[] { pkg });
2017-06-01 00:19:52 +08:00
}
public void deletePolicy(int uid) {
2017-07-21 00:46:13 +08:00
mDb.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)});
2017-06-20 18:25:18 +08:00
}
public Policy getPolicy(int uid) {
Policy policy = null;
2017-07-21 00:46:13 +08:00
try (Cursor c = mDb.query(POLICY_TABLE, null, "uid=?", new String[] { String.valueOf(uid) }, null, null, null)) {
if (c.moveToNext()) {
2017-06-01 00:19:52 +08:00
policy = new Policy(c, pm);
}
2017-06-01 00:19:52 +08:00
} catch (PackageManager.NameNotFoundException e) {
deletePolicy(uid);
return null;
}
return policy;
}
2017-05-31 16:31:33 +08:00
public Policy getPolicy(String pkg) {
Policy policy = null;
2017-07-21 00:46:13 +08:00
try (Cursor c = mDb.query(POLICY_TABLE, null, "package_name=?", new String[] { pkg }, null, null, null)) {
2017-05-31 16:31:33 +08:00
if (c.moveToNext()) {
2017-06-01 00:19:52 +08:00
policy = new Policy(c, pm);
2017-05-31 16:31:33 +08:00
}
2017-06-01 00:19:52 +08:00
} catch (PackageManager.NameNotFoundException e) {
deletePolicy(pkg);
return null;
2017-05-31 16:31:33 +08:00
}
return policy;
}
public void addPolicy(Policy policy) {
2017-07-21 00:46:13 +08:00
mDb.replace(POLICY_TABLE, null, policy.getContentValues());
}
2017-05-31 16:31:33 +08:00
public void updatePolicy(Policy policy) {
2017-07-21 00:46:13 +08:00
mDb.update(POLICY_TABLE, policy.getContentValues(), "package_name=?",
2017-05-31 16:31:33 +08:00
new String[] { policy.packageName });
}
public List<Policy> getPolicyList(PackageManager pm) {
2017-07-21 00:46:13 +08:00
try (Cursor c = mDb.query(POLICY_TABLE, null, null, null, null, null, null)) {
List<Policy> ret = new ArrayList<>(c.getCount());
while (c.moveToNext()) {
2017-06-20 17:56:47 +08:00
try {
2017-07-21 00:46:13 +08:00
Policy policy = new Policy(c, pm);
2017-06-20 17:56:47 +08:00
// The application changed UID for some reason, check user config
if (policy.info.uid != policy.uid) {
2017-09-15 18:03:25 +08:00
if (mm.suReauth) {
2017-06-20 17:56:47 +08:00
// Reauth required, remove from DB
2017-07-21 00:46:13 +08:00
deletePolicy(policy);
2017-06-20 17:56:47 +08:00
continue;
} else {
2017-06-20 18:25:18 +08:00
// No reauth, update to use the new UID
2017-06-20 17:56:47 +08:00
policy.uid = policy.info.uid;
2017-07-21 00:46:13 +08:00
updatePolicy(policy);
2017-06-20 17:56:47 +08:00
}
}
ret.add(policy);
} catch (PackageManager.NameNotFoundException e) {
// The app no longer exist, remove from DB
2017-07-21 00:46:13 +08:00
deletePolicy(c.getInt(c.getColumnIndex("uid")));
2017-06-20 17:56:47 +08:00
}
}
2017-07-21 00:46:13 +08:00
Collections.sort(ret);
return ret;
2017-06-01 00:19:52 +08:00
}
}
2017-07-24 00:34:34 +08:00
public List<List<Integer>> getLogStructure() {
try (Cursor c = mDb.query(LOG_TABLE, new String[] { "time" }, null, null, null, null, "time DESC")) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> list = null;
String dateString = null, newString;
2017-06-01 00:19:52 +08:00
while (c.moveToNext()) {
2017-07-24 01:20:03 +08:00
Date date = new Date(c.getLong(c.getColumnIndex("time")));
2017-07-24 00:34:34 +08:00
newString = DateFormat.getDateInstance(DateFormat.MEDIUM, MagiskManager.locale).format(date);
if (!TextUtils.equals(dateString, newString)) {
dateString = newString;
list = new ArrayList<>();
ret.add(list);
}
list.add(c.getPosition());
2017-06-01 00:19:52 +08:00
}
2017-07-21 00:46:13 +08:00
return ret;
}
}
2017-06-01 00:19:52 +08:00
2017-07-24 00:34:34 +08:00
public Cursor getLogCursor() {
return getLogCursor(mDb);
2017-06-01 00:19:52 +08:00
}
2017-07-24 00:34:34 +08:00
public Cursor getLogCursor(SQLiteDatabase db) {
return db.query(LOG_TABLE, null, null, null, null, null, "time DESC");
2017-06-01 00:19:52 +08:00
}
2017-07-24 00:34:34 +08:00
private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) {
try (SQLiteDatabase oldDb = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
Cursor c = getLogCursor(oldDb)) {
while (c.moveToNext()) {
ContentValues values = new ContentValues();
DatabaseUtils.cursorRowToContentValues(c, values);
newDB.insert(LOG_TABLE, null, values);
}
}
2017-06-01 00:19:52 +08:00
}
public void addLog(SuLogEntry log) {
2017-07-21 00:46:13 +08:00
mDb.insert(LOG_TABLE, null, log.getContentValues());
2017-06-01 00:19:52 +08:00
}
public void clearLogs() {
2017-07-21 00:46:13 +08:00
mDb.delete(LOG_TABLE, null, null);
2017-06-01 00:19:52 +08:00
}
2017-06-01 03:18:41 +08:00
public void setSettings(String key, int value) {
ContentValues data = new ContentValues();
data.put("key", key);
data.put("value", value);
2017-07-21 00:46:13 +08:00
mDb.replace(SETTINGS_TABLE, null, data);
2017-06-01 03:18:41 +08:00
}
public int getSettings(String key, int defaultValue) {
int value = defaultValue;
2017-07-21 00:46:13 +08:00
try (Cursor c = mDb.query(SETTINGS_TABLE, null, "key=?",new String[] { key }, null, null, null)) {
if (c.moveToNext()) {
2017-06-01 03:18:41 +08:00
value = c.getInt(c.getColumnIndex("value"));
}
}
return value;
}
}