2 ** Copyright 2013, Koushik Dutta (@koush)
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
25 struct callback_data_t {
26 struct su_context *ctx;
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;
36 for(i = 0; i < argc; i++) {
37 if (strcmp(azColName[i], "policy") == 0) {
38 if (argv[i] == NULL) {
41 if (strcmp(argv[i], "allow") == 0) {
44 else if (strcmp(argv[i], "interactive") == 0) {
51 else if (strcmp(azColName[i], "command") == 0) {
52 // null command means to match all commands (whitelist all from uid)
53 command_match = argv[i] == NULL || strcmp(argv[i], get_command(&(data->ctx->to))) == 0;
55 else if (strcmp(azColName[i], "until") == 0) {
56 if (argv[i] != NULL) {
57 until = atoi(argv[i]);
62 // check for command match
64 // also make sure this policy has not expired
65 if (until == 0 || until < time(NULL)) {
72 // even though we allow, continue, so we can see if there's another policy
80 policy_t database_check(struct su_context *ctx) {
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);
87 LOGE("sqlite3 open failure: %d", ret);
94 struct callback_data_t data;
96 data.policy = INTERACTIVE;
97 ret = sqlite3_exec(db, query, database_callback, &data, &err);
100 LOGE("sqlite3_exec: %s", err);