|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #include <stdlib.h>
|
18 |
| -#include <stdio.h> |
| 18 | +#include <sys/stat.h> |
19 | 19 | #include <limits.h>
|
20 | 20 | #include <cutils/log.h>
|
21 | 21 |
|
| 22 | +#include <sqlite3.h> |
| 23 | + |
22 | 24 | #include "su.h"
|
23 | 25 |
|
24 |
| -int database_check(const struct su_context *ctx) |
| 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) |
25 | 43 | {
|
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'; |
| 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"); |
38 | 64 | }
|
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); |
| 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; |
42 | 78 | }
|
43 |
| - fclose(fp); |
44 |
| - } else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) { |
45 |
| - LOGD("Using default"); |
46 |
| - allow = fgetc(fp); |
47 |
| - fclose(fp); |
48 | 79 | }
|
49 |
| - free(filename); |
50 | 80 |
|
51 |
| - if (allow == '1') { |
52 |
| - return DB_ALLOW; |
53 |
| - } else if (allow == '0') { |
54 |
| - return DB_DENY; |
55 |
| - } else { |
| 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 |
56 | 98 | return DB_INTERACTIVE;
|
57 | 99 | }
|
| 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; |
58 | 107 | }
|
0 commit comments