0838fe81f400cbf9f7c3eac0e688f7b45063a0ca
[framework/security/libprivilege-control.git] / include / privilege-control.h
1 /*
2  * libprivilege control
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Kidong Kim <kd0228.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdbool.h>
23 #include <sys/types.h>
24 #include <sys/smack.h>
25
26 #ifndef _PRIVILEGE_CONTROL_H_
27 #define _PRIVILEGE_CONTROL_H_
28
29 /* Macros for converting preprocessor token to string */
30 #define STRINGIFY(x) #x
31 #define TOSTRING(x) STRINGIFY(x)
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif // __cplusplus
36
37 #ifndef API
38 #define API __attribute__((visibility("default")))
39 #endif // API
40
41 /* error codes */
42 #define PC_OPERATION_SUCCESS            ((int)0)
43 #define PC_ERR_FILE_OPERATION           -1
44 #define PC_ERR_MEM_OPERATION            -2
45 #define PC_ERR_NOT_PERMITTED            -3
46 #define PC_ERR_INVALID_PARAM            -4
47 #define PC_ERR_INVALID_OPERATION        -5
48 #define PC_ERR_DB_OPERATION                     -6
49
50 typedef enum {
51        APP_TYPE_WGT,
52        APP_TYPE_OSP,
53        APP_TYPE_OTHER,
54 } app_type_t;
55
56 /* APIs - used by applications */
57 int control_privilege(void) __attribute__((deprecated));
58
59 int set_privilege(const char* pkg_name) __attribute__((deprecated));
60
61 /**
62  * Function get process smack label base on pid.
63  * @param in:  pid of process
64  * @param out: label of process
65  * @return PC_OPERATION_SUCCESS on success PC_ERR_* on error.
66  */
67 int get_smack_label_from_process(pid_t pid, char smack_label[SMACK_LABEL_LEN + 1]);
68
69 /**
70  * Check if process with pid have access to object.
71  * This function check if subject have access to object via smack_have_access() function.
72  * If YES then returned access granted. In NO then function check if process with pid have
73  * CAP_MAC_OVERRIDE capability. If YES then return access granted.
74  * If NO then return access denied.
75  *
76  * @param pid of process
77  * @param label of object to access
78  * @param access_type
79  * @return 0 (no access) or 1 (access) or -1 (error)
80  */
81 int smack_pid_have_access(pid_t pid,
82                                                         const char* object,
83                                                         const char *access_type);
84
85 /**
86  * Set DAC and SMACK privileges for application.
87  * This function is meant to be call by the application launcher just before
88  * it launches an application. It will setup DAC and SMACK privileges based
89  * on app type and accesses.
90  * It must be called with root privileges, which will be dropped in the function.
91  *
92  * @param name package name
93  * @param type application type (currently distinguished types: "wgt" and other)
94  * @param path file system path to the binary
95  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
96  */
97 int set_app_privilege(const char* name, const char* type, const char* path);
98
99 /**
100  * For a UNIX socket endpoint determine the other side's app_id.
101  *
102  * @param sockfd socket file descriptor
103  * @return id of the connecting widget on success, NULL on failure.
104  * Caller is responsible for freeing the return widget id.
105  */
106 char* app_id_from_socket(int sockfd);
107
108 /**
109  * Inform about installation of a new app.
110  * It is intended to be called during app installation.
111  * It will create an empty SMACK rules file used by other functions operating
112  * on permissions if it doesn't already exist. It is needed for tracking
113  * lifetime of an app. It must be called by privileged user, before using any
114  * other app_* function. It may be called more than once during installation.
115  *
116  *
117  * @param app_id application identifier
118  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
119  */
120 int app_install(const char* app_id);
121
122 /**
123  * Inform about deinstallation of an app.
124  * It will remove the SMACK rules file, enabling future installation of app
125  * with the same identifier. It is needed for tracking lifetime of an app.
126  * You should call app_revoke_permissions() before this function.
127  * It must be called by privileged user.
128  *
129  *
130  * @param app_id application identifier
131  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
132  */
133 int app_uninstall(const char* app_id);
134
135 /**
136  * Inform about installation of new Anti Virus application.
137  * It is intended to be called during Anti Virus installation.
138  * It will give this application SMACK rules to RWX access to all other apps
139  * installed in system.
140  * It must be called by privileged user.
141  *
142  * @param app_id application identifier
143  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error.
144  */
145 int app_register_av(const char* app_av_id);
146
147 /**
148  * Grant SMACK permissions based on permissions list.
149  * It is intended to be called during app installation.
150  * It will construct SMACK rules based on permissions list, grant them
151  * and store it in a file, so they will be automatically granted on
152  * system boot.
153  * It must be called by privileged user.
154  * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
155  *
156  *
157  * @param app_id application identifier
158  * @param perm_list array of permission names, last element must be NULL
159  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
160  */
161 int app_add_permissions(const char* app_id, const char** perm_list)  __attribute__((deprecated));
162
163 /**
164  * Grant temporary SMACK permissions based on permissions list.
165  * It will construct SMACK rules based on permissions list, grant them,
166  * but not store it anywhere, so they won't be granted again on system boot.
167  * It must be called by privileged user.
168  * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
169  *
170  *
171  * @param app_id application identifier
172  * @param perm_list array of permission names, last element must be NULL
173  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
174  */
175 int app_add_volatile_permissions(const char* app_id, const char** perm_list)  __attribute__((deprecated));
176
177 /**
178  * Grant SMACK permissions based on permissions list.
179  * It is intended to be called during app installation.
180  * It will construct SMACK rules based on permissions list, grant them
181  * and store it in a file, so they will be automatically granted on
182  * system boot, when persistent mode is enabled.
183  * It must be called by privileged user.
184  *
185  *
186  * @param app_id application identifier
187  * @param app_type application type
188  * @param perm_list array of permission names, last element must be NULL
189  * @param persistent boolean for choosing between persistent and temporary rules
190  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
191  */
192 int app_enable_permissions(const char* app_id, app_type_t app_type, const char** perm_list, bool persistent);
193
194 /**
195  * Remove previously granted SMACK permissions based on permissions list.
196  * It will remove given permissions from an app, leaving other granted
197  * permissions untouched. Results will be persistent.
198  * It must be called by privileged user.
199  *
200  *
201  * @param app_id application identifier
202  * @param app_type application type
203  * @param perm_list array of permission names, last element must be NULL
204  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
205  */
206 int app_disable_permissions(const char* app_id, app_type_t app_type, const char** perm_list);
207
208 /**
209  * Revoke SMACK permissions from an application.
210  * This function should be called during app deinstallation.
211  * It will revoke all SMACK rules previously granted by app_add_permissions().
212  * It will also remove a rules file from disk.
213  * It must be called by privileged user.
214  *
215  * @param app_id application identifier
216  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
217  */
218 int app_revoke_permissions(const char* app_id);
219
220 /**
221  * Reset SMACK permissions for an application by revoking all previously
222  * granted rules and enabling them again from a rules file from disk.
223  * It must be called by privileged user.
224  *
225  * @param app_id application identifier
226  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
227  */
228 int app_reset_permissions(const char* app_id);
229
230 /**
231  * Recursively set SMACK access labels for an application directory
232  * and execute labels for executable files.
233  * This function should be called once during app installation.
234  * Results will be persistent on the file system.
235  * It must be called by privileged user.
236  *
237  * @param app_label label name
238  * @param path directory path
239  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
240  */
241 int app_label_dir(const char* app_label, const char* path);
242
243 /**
244  * Recursively set SMACK access and transmute labels for an application
245  * directory and adds SMACK rule for application.
246  * This function should be called once during app installation.
247  * Results will be persistent on the file system.
248  * It must be called by privileged user.
249  * Labels app_label and shared_label should not be equal.
250  *
251  * @param app_label label name, used as subject for SMACK rule
252  * @param shared_label, used as object for SMACK rule
253  * @param path directory path
254  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
255  */
256 int app_label_shared_dir(const char* app_label, const char* shared_label,
257                                                  const char* path);
258
259
260
261 /**
262  * Add SMACK rx rules for application identifiers to shared_label.
263  * This function should be called during app installation.
264  * It must be called by privileged user.
265  *
266  * @param shared_label label of the shared resource
267  * @param app_list list of application SMACK identifiers
268  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
269  */
270 int add_shared_dir_readers(const char* shared_label, const char** app_list);
271
272 /**
273  * Make two applications "friends", by giving them both full permissions on
274  * each other.
275  * Results will be persistent on the file system. Must be called after
276  * app_add_permissions() has been called for each application.
277  * It must be called by privileged user.
278  *
279  * @param app_id1 first application identifier
280  * @param app_id2 second application identifier
281  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
282  */
283 int app_add_friend(const char* app_id1, const char* app_id2);
284
285 /**
286  * Modify SMACK rules to give access from (subject)customer_label to (object)
287  * provider_label.
288  * Note: This function will do nothing if subject has already rwxat access to
289  * object. You can revoke this modyfication by calling app_rovoke_access.
290  *
291  * @param subject - label of client application
292  * @param object  - label of provider application
293  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
294  */
295 int app_give_access(const char* subject, const char* object, const char* permission) __attribute__ ((deprecated));
296
297 /**
298  * Revoke access granted by app_give_access. This function will not remove
299  * accesses that were granted before app_give_access call.
300  *
301  * @param subject - label of client application
302  * @param object  - label of provider application
303  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
304  */
305 int app_revoke_access(const char* subject, const char* object) __attribute__ ((deprecated));
306
307 /**
308  * Adds new api feature by installing new *.smack file.
309  * It must be called by privileged user.
310  *
311  * @param app_type application type
312  * @param api_feature_name name of newly added feature
313  * @param smack_rule_set set of rules required by the feature - NULL terminated
314  * list of NULL terminated rules.
315  * @param list_of_db_gids list of gids required to access databases controlled
316  * by the feature
317  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
318  */
319 int add_api_feature(app_type_t app_type,
320                                         const char* api_feature_name,
321                                         const char** set_smack_rule_set,
322                                         const gid_t* list_of_db_gids,
323                                         size_t list_size);
324
325 #ifdef __cplusplus
326 }
327 #endif // __cplusplus
328
329 #endif // _PRIVILEGE_CONTROL_H_