OSDN Git Service

intial commit
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / db.c
1 /*
2 ** Copyright 2010, Adam Shanks (@ChainsDD)
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
21 #include "su.h"
22
23 int database_check(struct su_context *ctx)
24 {
25     FILE *fp;
26     char filename[PATH_MAX];
27     char allow[7];
28     int last = 0;
29     int from_uid = ctx->from.uid;
30     
31     if (ctx->user.owner_mode) {
32         from_uid = from_uid % 100000;
33     }
34
35     snprintf(filename, sizeof(filename),
36                 "%s/%u-%u", ctx->user.store_path, from_uid, ctx->to.uid);
37     if ((fp = fopen(filename, "r"))) {
38         LOGD("Found file %s", filename);
39         
40         while (fgets(allow, sizeof(allow), fp)) {
41             last = strlen(allow) - 1;
42             if (last >= 0)
43                     allow[last] = 0;
44                 
45             char cmd[ARG_MAX];
46             fgets(cmd, sizeof(cmd), fp);
47             /* skip trailing '\n' */
48             last = strlen(cmd) - 1;
49             if (last >= 0)
50                 cmd[last] = 0;
51
52             LOGD("Comparing '%s' to '%s'", cmd, get_command(&ctx->to));
53             if (strcmp(cmd, get_command(&ctx->to)) == 0)
54                 break;
55             else if (strcmp(cmd, "any") == 0) {
56                 ctx->to.all = 1;
57                 break;
58             }
59             else
60                 strcpy(allow, "prompt");
61         }
62         fclose(fp);
63     } else if ((fp = fopen(ctx->user.store_default, "r"))) {
64         LOGD("Using default file %s", ctx->user.store_default);
65         fgets(allow, sizeof(allow), fp);
66         last = strlen(allow) - 1;
67         if (last >=0)
68             allow[last] = 0;
69         
70         fclose(fp);
71     }
72
73     if (strcmp(allow, "allow") == 0) {
74         return ALLOW;
75     } else if (strcmp(allow, "deny") == 0) {
76         return DENY;
77     } else {
78         if (ctx->user.userid != 0 && ctx->user.owner_mode) {
79             return DENY;
80         } else {
81             return INTERACTIVE;
82         }
83     }
84 }