Skip to content

Commit

Permalink
Revert "Revert "Use flat files for stored apps""
Browse files Browse the repository at this point in the history
This reverts commit 588c414.
  • Loading branch information
ChainsDD committed Jul 8, 2012
1 parent 588c414 commit d74df17
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 83 deletions.
3 changes: 0 additions & 3 deletions Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ include $(CLEAR_VARS)
LOCAL_MODULE := su
LOCAL_SRC_FILES := su.c db.c activity.c utils.c

LOCAL_C_INCLUDES += external/sqlite/dist

LOCAL_STATIC_LIBRARIES := \
liblog \
libsqlite \
libc \

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
Expand Down
105 changes: 28 additions & 77 deletions db.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,93 +15,44 @@
*/

#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
#include <limits.h>
#include <cutils/log.h>

#include <sqlite3.h>

#include "su.h"

static sqlite3 *db_init(void)
{
sqlite3 *db;
int rc;

rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
if ( rc ) {
LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
return NULL;
}

// Create an automatic busy handler in case the db is locked
sqlite3_busy_timeout(db, 1000);
return db;
}

static int db_check(sqlite3 *db, const struct su_context *ctx)
int database_check(const struct su_context *ctx)
{
char sql[4096];
char *zErrmsg;
char **result;
int nrow,ncol;
int allow = DB_INTERACTIVE;

sqlite3_snprintf(
sizeof(sql), sql,
"SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
ctx->from.uid, ctx->to.uid, get_command(&ctx->to)
);

if (strlen(sql) >= sizeof(sql)-1)
return DB_DENY;

int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
if (error != SQLITE_OK) {
LOGE("Database check failed with error message %s", zErrmsg);
if (error == SQLITE_BUSY) {
LOGE("Specifically, the database is busy");
FILE *fp;
char allow = '-';
char *filename = malloc(snprintf(NULL, 0, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid) + 1);
sprintf(filename, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid);
if ((fp = fopen(filename, "r"))) {
LOGD("Found file");
char cmd[PATH_MAX];

This comment has been minimized.

Copy link
@Tungstwenty

Tungstwenty Aug 20, 2012

IMO this should be a loop for the multiple commands that a single app can launch as root, if it opts for the "su -c cmdXXX" syntax instead of "su" and then accessing stdin/out.
Currently, any app that launches more than 1 su command using that approach will keep asking for permissions over and over.
On the frontend Superuser app the DB already supports multiple commands, but writing to this cache file is also overwriting it every time with a single line.
One example is the BetterBatteryStats app, altough I think chamonix already changed it in the latest version to use the stdin approach.
Cheers!

This comment has been minimized.

Copy link
@ChainsDD

ChainsDD via email Aug 20, 2012

Author Owner
fgets(cmd, sizeof(cmd), fp);
int last = strlen(cmd) - 1;
LOGD("this is the last character %u of the string", cmd[5]);
if (cmd[last] == '\n') {
cmd[last] = '\0';
}
return DB_DENY;
}

if (nrow == 0 || ncol != 3)
goto out;

if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
if (strcmp(result[5], "1") == 0) {
allow = DB_ALLOW;
} else if (strcmp(result[5], "-1") == 0){
allow = DB_INTERACTIVE;
} else {
allow = DB_DENY;
LOGD("Comparing %c %s, %u to %s", cmd[last - 2], cmd, last, get_command(&ctx->to));
if (strcmp(cmd, get_command(&ctx->to)) == 0) {
allow = fgetc(fp);
}
fclose(fp);
} else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) {
LOGD("Using default");
allow = fgetc(fp);
fclose(fp);
}
free(filename);

out:
sqlite3_free_table(result);

return allow;
}

int database_check(const struct su_context *ctx)
{
sqlite3 *db;
int dballow;

LOGE("sudb - Opening database");
db = db_init();
if (!db) {
LOGE("sudb - Could not open database, prompt user");
// if the database could not be opened, we can assume we need to
// prompt the user
if (allow == '1') {
return DB_ALLOW;
} else if (allow == '0') {
return DB_DENY;
} else {
return DB_INTERACTIVE;
}

LOGE("sudb - Database opened");
dballow = db_check(db, ctx);
// Close the database, we're done with it. If it stays open, it will cause problems
sqlite3_close(db);
LOGE("sudb - Database closed");
return dballow;
}
6 changes: 3 additions & 3 deletions su.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

#define REQUESTOR "com.noshufou.android.su"
#define REQUESTOR_DATA_PATH "/data/data/" REQUESTOR
#define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
#define REQUESTOR_CACHE_PATH REQUESTOR_DATA_PATH "/cache"

#define REQUESTOR_DATABASES_PATH REQUESTOR_DATA_PATH "/databases"
#define REQUESTOR_DATABASE_PATH REQUESTOR_DATABASES_PATH "/permissions.sqlite"
#define REQUESTOR_STORED_PATH REQUESTOR_DATA_PATH "/files/stored"
#define REQUESTOR_STORED_DEFAULT REQUESTOR_STORED_PATH "/default"

/* intent actions */
#define ACTION_REQUEST REQUESTOR ".REQUEST"
Expand Down

0 comments on commit d74df17

Please sign in to comment.