OSDN Git Service

48a0c210a815dd7709df13eef870c3fe9459fdc4
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / su.h
1 /*
2 ** Copyright 2010, Adam Shanks (@ChainsDD)
3 ** Copyright 2008, Zinx Verituse (@zinxv)
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #ifndef SU_h 
19 #define SU_h 1
20
21 #ifdef LOG_TAG
22 #undef LOG_TAG
23 #endif
24 #define LOG_TAG "su"
25
26 #ifndef AID_SHELL
27 #define AID_SHELL (get_shell_uid())
28 #endif
29
30 #ifndef AID_ROOT
31 #define AID_ROOT  0
32 #endif
33
34 // CyanogenMod-specific behavior
35 #define CM_ROOT_ACCESS_DISABLED      0
36 #define CM_ROOT_ACCESS_APPS_ONLY     1
37 #define CM_ROOT_ACCESS_ADB_ONLY      2
38 #define CM_ROOT_ACCESS_APPS_AND_ADB  3
39
40 #define REQUESTOR "com.koushikdutta.superuser"
41 #define REQUESTOR_DATA_PATH "/data/data/"
42 #define REQUESTOR_FILES_PATH REQUESTOR_DATA_PATH REQUESTOR "/files"
43 #define REQUESTOR_USER_PATH "/data/user/"
44 #define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
45
46 // there's no guarantee that the db or files are actually created named as such by
47 // SQLiteOpenHelper, etc. Though that is the behavior as of current.
48 // it is up to the Android application to symlink as appropriate.
49 #define REQUESTOR_DATABASE_PATH REQUESTOR "/databases/su.sqlite"
50 #define REQUESTOR_MULTIUSER_MODE REQUESTOR_FILES_PATH "/multiuser_mode"
51
52 /* intent actions */
53 #define ACTION_REQUEST "start -n " REQUESTOR "/.RequestActivity"
54 #define ACTION_NOTIFY "start -n " REQUESTOR "/.NotifyActivity"
55 #define ACTION_RESULT "broadcast -n " REQUESTOR "/.SuReceiver"
56
57 #define DEFAULT_SHELL "/system/bin/sh"
58
59 #define xstr(a) str(a)
60 #define str(a) #a
61
62 #define VERSION_CODE 1
63 #define VERSION xstr(VERSION_CODE) " " REQUESTOR
64
65 #define PROTO_VERSION 1
66
67 struct su_initiator {
68     pid_t pid;
69     unsigned uid;
70     unsigned user;
71     char name[64];
72     char bin[PATH_MAX];
73     char args[4096];
74 };
75
76 struct su_request {
77     unsigned uid;
78     char name[64];
79     int login;
80     int keepenv;
81     char *shell;
82     char *command;
83     char **argv;
84     int argc;
85     int optind;
86 };
87
88 struct su_user_info {
89     // the user in android userspace (multiuser)
90     // that invoked this action.
91     unsigned android_user_id;
92     // how su behaves with multiuser. see enum below.
93     int multiuser_mode;
94     // path to superuser directory. this is populated according
95     // to the multiuser mode.
96     // this is used to check uid/gid for protecting socket.
97     // this is used instead of database, as it is more likely
98     // to exist. db will not exist if su has never launched.
99     char base_path[PATH_MAX];
100     // path to su database. this is populated according
101     // to the multiuser mode.
102     char database_path[PATH_MAX];
103 };
104
105 struct su_context {
106     struct su_initiator from;
107     struct su_request to;
108     struct su_user_info user;
109     mode_t umask;
110     char sock_path[PATH_MAX];
111 };
112
113 // multiuser su behavior
114 typedef enum {
115   // only owner can su
116   MULTIUSER_MODE_OWNER_ONLY = 0,
117   // owner gets a su prompt
118   MULTIUSER_MODE_OWNER_MANAGED = 1,
119   // user gets a su prompt
120   MULTIUSER_MODE_USER = 2,
121   MULTIUSER_MODE_NONE = 3,
122 } multiuser_mode_t;
123
124 #define MULTIUSER_VALUE_OWNER_ONLY    "owner"
125 #define MULTIUSER_VALUE_OWNER_MANAGED "managed"
126 #define MULTIUSER_VALUE_USER          "user"
127 #define MULTIUSER_VALUE_NONE          "none"
128
129 typedef enum {
130     INTERACTIVE = 0,
131     DENY = 1,
132     ALLOW = 2,
133 } policy_t;
134
135 extern policy_t database_check(struct su_context *ctx);
136 extern void set_identity(unsigned int uid);
137 extern int send_request(struct su_context *ctx);
138 extern int send_result(struct su_context *ctx, policy_t policy);
139 extern void sigchld_handler(int sig);
140
141 static inline char *get_command(const struct su_request *to)
142 {
143         return (to->command) ? to->command : to->shell;
144 }
145
146 void exec_loge(const char* fmt, ...);
147 void exec_logw(const char* fmt, ...);
148 void exec_logd(const char* fmt, ...);
149
150 // fallback to using /system/bin/log.
151 // can't use liblog.so because this is a static binary.
152 #ifndef LOGE
153 #define LOGE exec_loge
154 #endif
155 #ifndef LOGD
156 #define LOGD exec_logd
157 #endif
158 #ifndef LOGW
159 #define LOGW exec_logw
160 #endif
161
162 #if 0
163 #undef LOGE
164 #define LOGE(fmt,args...) fprintf(stderr, fmt, ##args)
165 #undef LOGD
166 #define LOGD(fmt,args...) fprintf(stderr, fmt, ##args)
167 #undef LOGW
168 #define LOGW(fmt,args...) fprintf(stderr, fmt, ##args)
169 #endif
170
171 #include <errno.h>
172 #include <string.h>
173 #define PLOGE(fmt,args...) LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno))
174 #define PLOGEV(fmt,err,args...) LOGE(fmt " failed with %d: %s", ##args, err, strerror(err))
175
176 #endif