OSDN Git Service

Fix building issues with pie-x86
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / db.c
1 /*
2 ** Copyright 2013, Koushik Dutta (@koush)
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <limits.h>
20 #include <sqlite3.h>
21 #include <time.h>
22
23 #include "su.h"
24
25 struct callback_data_t {
26     struct su_context *ctx;
27     policy_t policy;
28 };
29
30 static int database_callback(void *v, int argc, char **argv, char **azColName){
31     struct callback_data_t *data = (struct callback_data_t *)v;
32     int command_match = 0;
33     policy_t policy = DENY;
34     int i;
35     time_t until = 0;
36     for(i = 0; i < argc; i++) {
37         if (strcmp(azColName[i], "policy") == 0) {
38             if (argv[i] == NULL) {
39                 policy = DENY;
40             }
41             if (strcmp(argv[i], "allow") == 0) {
42                 policy = ALLOW;
43             }
44             else if (strcmp(argv[i], "interactive") == 0) {
45                 policy = INTERACTIVE;
46             }
47             else {
48                 policy = DENY;
49             }
50         }
51         else if (strcmp(azColName[i], "command") == 0) {
52             // null or empty command means to match all commands (whitelist all from uid)
53             command_match = argv[i] == NULL || strlen(argv[i]) == 0 || strcmp(argv[i], get_command(&(data->ctx->to))) == 0;
54         }
55         else if (strcmp(azColName[i], "until") == 0) {
56             if (argv[i] != NULL) {
57                 until = atoi(argv[i]);
58             }
59         }
60     }
61
62     // check for command match
63     if (command_match) {
64         // also make sure this policy has not expired
65         if (until == 0 || until > time(NULL)) {
66             if (policy == DENY) {
67                 data->policy = DENY;
68                 return -1;
69             }
70
71             data->policy = ALLOW;
72             // even though we allow, continue, so we can see if there's another policy
73             // that denies...
74         }
75     }
76     
77     return 0;
78 }
79
80 policy_t database_check(struct su_context *ctx) {
81     sqlite3 *db = NULL;
82     
83     char query[512];
84     snprintf(query, sizeof(query), "select policy, until, command from uid_policy where uid=%d", ctx->from.uid);
85     int ret = sqlite3_open_v2(ctx->user.database_path, &db, SQLITE_OPEN_READONLY, NULL);
86     if (ret) {
87         LOGE("sqlite3 open %s failure: %d", ctx->user.database_path, ret);
88         sqlite3_close(db);
89         return INTERACTIVE;
90     }
91     
92     char *err = NULL;
93     struct callback_data_t data;
94     data.ctx = ctx;
95     data.policy = INTERACTIVE;
96     ret = sqlite3_exec(db, query, database_callback, &data, &err);
97     sqlite3_close(db);
98     if (err != NULL) {
99         LOGE("sqlite3_exec: %s", err);
100         return DENY;
101     }
102
103     return data.policy;
104 }