Skip to content

Commit d74df17

Browse files
committed
Revert "Revert "Use flat files for stored apps""
This reverts commit 588c414.
1 parent 588c414 commit d74df17

File tree

3 files changed

+31
-83
lines changed

3 files changed

+31
-83
lines changed

Android.mk

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ include $(CLEAR_VARS)
44
LOCAL_MODULE := su
55
LOCAL_SRC_FILES := su.c db.c activity.c utils.c
66

7-
LOCAL_C_INCLUDES += external/sqlite/dist
8-
97
LOCAL_STATIC_LIBRARIES := \
108
liblog \
11-
libsqlite \
129
libc \
1310

1411
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)

db.c

Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,44 @@
1515
*/
1616

1717
#include <stdlib.h>
18-
#include <sys/stat.h>
18+
#include <stdio.h>
1919
#include <limits.h>
2020
#include <cutils/log.h>
2121

22-
#include <sqlite3.h>
23-
2422
#include "su.h"
2523

26-
static sqlite3 *db_init(void)
27-
{
28-
sqlite3 *db;
29-
int rc;
30-
31-
rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
32-
if ( rc ) {
33-
LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
34-
return NULL;
35-
}
36-
37-
// Create an automatic busy handler in case the db is locked
38-
sqlite3_busy_timeout(db, 1000);
39-
return db;
40-
}
41-
42-
static int db_check(sqlite3 *db, const struct su_context *ctx)
24+
int database_check(const struct su_context *ctx)
4325
{
44-
char sql[4096];
45-
char *zErrmsg;
46-
char **result;
47-
int nrow,ncol;
48-
int allow = DB_INTERACTIVE;
49-
50-
sqlite3_snprintf(
51-
sizeof(sql), sql,
52-
"SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
53-
ctx->from.uid, ctx->to.uid, get_command(&ctx->to)
54-
);
55-
56-
if (strlen(sql) >= sizeof(sql)-1)
57-
return DB_DENY;
58-
59-
int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
60-
if (error != SQLITE_OK) {
61-
LOGE("Database check failed with error message %s", zErrmsg);
62-
if (error == SQLITE_BUSY) {
63-
LOGE("Specifically, the database is busy");
26+
FILE *fp;
27+
char allow = '-';
28+
char *filename = malloc(snprintf(NULL, 0, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid) + 1);
29+
sprintf(filename, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid);
30+
if ((fp = fopen(filename, "r"))) {
31+
LOGD("Found file");
32+
char cmd[PATH_MAX];
33+
fgets(cmd, sizeof(cmd), fp);
34+
int last = strlen(cmd) - 1;
35+
LOGD("this is the last character %u of the string", cmd[5]);
36+
if (cmd[last] == '\n') {
37+
cmd[last] = '\0';
6438
}
65-
return DB_DENY;
66-
}
67-
68-
if (nrow == 0 || ncol != 3)
69-
goto out;
70-
71-
if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
72-
if (strcmp(result[5], "1") == 0) {
73-
allow = DB_ALLOW;
74-
} else if (strcmp(result[5], "-1") == 0){
75-
allow = DB_INTERACTIVE;
76-
} else {
77-
allow = DB_DENY;
39+
LOGD("Comparing %c %s, %u to %s", cmd[last - 2], cmd, last, get_command(&ctx->to));
40+
if (strcmp(cmd, get_command(&ctx->to)) == 0) {
41+
allow = fgetc(fp);
7842
}
43+
fclose(fp);
44+
} else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) {
45+
LOGD("Using default");
46+
allow = fgetc(fp);
47+
fclose(fp);
7948
}
49+
free(filename);
8050

81-
out:
82-
sqlite3_free_table(result);
83-
84-
return allow;
85-
}
86-
87-
int database_check(const struct su_context *ctx)
88-
{
89-
sqlite3 *db;
90-
int dballow;
91-
92-
LOGE("sudb - Opening database");
93-
db = db_init();
94-
if (!db) {
95-
LOGE("sudb - Could not open database, prompt user");
96-
// if the database could not be opened, we can assume we need to
97-
// prompt the user
51+
if (allow == '1') {
52+
return DB_ALLOW;
53+
} else if (allow == '0') {
54+
return DB_DENY;
55+
} else {
9856
return DB_INTERACTIVE;
9957
}
100-
101-
LOGE("sudb - Database opened");
102-
dballow = db_check(db, ctx);
103-
// Close the database, we're done with it. If it stays open, it will cause problems
104-
sqlite3_close(db);
105-
LOGE("sudb - Database closed");
106-
return dballow;
10758
}

su.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
#define REQUESTOR "com.noshufou.android.su"
2727
#define REQUESTOR_DATA_PATH "/data/data/" REQUESTOR
28-
#define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
28+
#define REQUESTOR_CACHE_PATH REQUESTOR_DATA_PATH "/cache"
2929

30-
#define REQUESTOR_DATABASES_PATH REQUESTOR_DATA_PATH "/databases"
31-
#define REQUESTOR_DATABASE_PATH REQUESTOR_DATABASES_PATH "/permissions.sqlite"
30+
#define REQUESTOR_STORED_PATH REQUESTOR_DATA_PATH "/files/stored"
31+
#define REQUESTOR_STORED_DEFAULT REQUESTOR_STORED_PATH "/default"
3232

3333
/* intent actions */
3434
#define ACTION_REQUEST REQUESTOR ".REQUEST"

0 commit comments

Comments
 (0)