OSDN Git Service

allow customization of the requestor prefix
[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 // DO NOT CHANGE LINE BELOW, java package name will always be the same
41 #define JAVA_PACKAGE_NAME "com.koushikdutta.superuser"
42
43 // If --rename-manifest-package is used in AAPT, this
44 // must be changed to correspond to the new APK package name
45 // See the two Android.mk files for more details.
46 #ifndef REQUESTOR
47 #define REQUESTOR JAVA_PACKAGE_NAME
48 #endif
49 #ifndef REQUESTOR_PREFIX
50 #define REQUESTOR_PREFIX JAVA_PACKAGE_NAME
51 #endif
52 #define REQUESTOR_DATA_PATH "/data/data/"
53 #define REQUESTOR_FILES_PATH REQUESTOR_DATA_PATH REQUESTOR "/files"
54 #define REQUESTOR_USER_PATH "/data/user/"
55 #define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
56
57 // there's no guarantee that the db or files are actually created named as such by
58 // SQLiteOpenHelper, etc. Though that is the behavior as of current.
59 // it is up to the Android application to symlink as appropriate.
60 #define REQUESTOR_DATABASE_PATH REQUESTOR "/databases/su.sqlite"
61 #define REQUESTOR_MULTIUSER_MODE REQUESTOR_FILES_PATH "/multiuser_mode"
62
63 /* intent actions */
64 #define ACTION_REQUEST "start -n " REQUESTOR "/" REQUESTOR_PREFIX ".RequestActivity"
65 #define ACTION_NOTIFY "start -n " REQUESTOR "/" REQUESTOR_PREFIX ".NotifyActivity"
66 #define ACTION_RESULT "broadcast -n " REQUESTOR "/" REQUESTOR_PREFIX ".SuReceiver"
67
68 #define DEFAULT_SHELL "/system/bin/sh"
69
70 #define xstr(a) str(a)
71 #define str(a) #a
72
73 #define VERSION_CODE 2
74 #define VERSION xstr(VERSION_CODE) " " REQUESTOR
75
76 #define PROTO_VERSION 1
77
78 struct su_initiator {
79     pid_t pid;
80     unsigned uid;
81     unsigned user;
82     char name[64];
83     char bin[PATH_MAX];
84     char args[4096];
85 };
86
87 struct su_request {
88     unsigned uid;
89     char name[64];
90     int login;
91     int keepenv;
92     char *shell;
93     char *command;
94     char **argv;
95     int argc;
96     int optind;
97 };
98
99 struct su_user_info {
100     // the user in android userspace (multiuser)
101     // that invoked this action.
102     unsigned android_user_id;
103     // how su behaves with multiuser. see enum below.
104     int multiuser_mode;
105     // path to superuser directory. this is populated according
106     // to the multiuser mode.
107     // this is used to check uid/gid for protecting socket.
108     // this is used instead of database, as it is more likely
109     // to exist. db will not exist if su has never launched.
110     char base_path[PATH_MAX];
111     // path to su database. this is populated according
112     // to the multiuser mode.
113     char database_path[PATH_MAX];
114 };
115
116 struct su_context {
117     struct su_initiator from;
118     struct su_request to;
119     struct su_user_info user;
120     mode_t umask;
121     char sock_path[PATH_MAX];
122 };
123
124 // multiuser su behavior
125 typedef enum {
126   // only owner can su
127   MULTIUSER_MODE_OWNER_ONLY = 0,
128   // owner gets a su prompt
129   MULTIUSER_MODE_OWNER_MANAGED = 1,
130   // user gets a su prompt
131   MULTIUSER_MODE_USER = 2,
132   MULTIUSER_MODE_NONE = 3,
133 } multiuser_mode_t;
134
135 #define MULTIUSER_VALUE_OWNER_ONLY    "owner"
136 #define MULTIUSER_VALUE_OWNER_MANAGED "managed"
137 #define MULTIUSER_VALUE_USER          "user"
138 #define MULTIUSER_VALUE_NONE          "none"
139
140 typedef enum {
141     INTERACTIVE = 0,
142     DENY = 1,
143     ALLOW = 2,
144 } policy_t;
145
146 extern policy_t database_check(struct su_context *ctx);
147 extern void set_identity(unsigned int uid);
148 extern int send_request(struct su_context *ctx);
149 extern int send_result(struct su_context *ctx, policy_t policy);
150 extern int silent_run(char* command);
151
152 static inline char *get_command(const struct su_request *to)
153 {
154         return (to->command) ? to->command : to->shell;
155 }
156
157 void exec_loge(const char* fmt, ...);
158 void exec_logw(const char* fmt, ...);
159 void exec_logd(const char* fmt, ...);
160
161 // fallback to using /system/bin/log.
162 // can't use liblog.so because this is a static binary.
163 #ifndef LOGE
164 #define LOGE exec_loge
165 #endif
166 #ifndef LOGD
167 #define LOGD exec_logd
168 #endif
169 #ifndef LOGW
170 #define LOGW exec_logw
171 #endif
172
173 #if 0
174 #undef LOGE
175 #define LOGE(fmt,args...) fprintf(stderr, fmt, ##args)
176 #undef LOGD
177 #define LOGD(fmt,args...) fprintf(stderr, fmt, ##args)
178 #undef LOGW
179 #define LOGW(fmt,args...) fprintf(stderr, fmt, ##args)
180 #endif
181
182 #include <errno.h>
183 #include <string.h>
184 #define PLOGE(fmt,args...) LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno))
185 #define PLOGEV(fmt,err,args...) LOGE(fmt " failed with %d: %s", ##args, err, strerror(err))
186
187 #endif