cafa08482904a6a8746e1bdba65450d18118b27c
[platform/core/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
25 #ifndef _PRIVILEGE_CONTROL_H_
26 #define _PRIVILEGE_CONTROL_H_
27
28 /* Macros for converting preprocessor token to string */
29 #define STRINGIFY(x) #x
30 #define TOSTRING(x) STRINGIFY(x)
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif // __cplusplus
35
36 #ifndef API
37 #define API __attribute__((visibility("default")))
38 #endif // API
39
40 #define DEPRECATED __attribute__((deprecated))
41 #define UNUSED __attribute__((unused))
42
43 /* error codes */
44 #define PC_OPERATION_SUCCESS            ((int)0)
45 #define PC_ERR_FILE_OPERATION           -1
46 #define PC_ERR_MEM_OPERATION            -2
47 #define PC_ERR_NOT_PERMITTED            -3
48 #define PC_ERR_INVALID_PARAM            -4
49 #define PC_ERR_INVALID_OPERATION        -5
50 #define PC_ERR_DB_OPERATION             -6
51
52 /// Label is taken by another application
53 #define PC_ERR_DB_LABEL_TAKEN           -7
54
55 /// Query fails during preparing a SQL statement
56 #define PC_ERR_DB_QUERY_PREP            -8
57
58 /// Query fails during binding to a SQL statement
59 #define PC_ERR_DB_QUERY_BIND            -9
60
61 /// Query fails during stepping a SQL statement
62 #define PC_ERR_DB_QUERY_STEP            -10
63
64 // Unable to establish a connection with the database
65 #define PC_ERR_DB_CONNECTION            -11
66
67 // There is no application with such app_id
68 #define PC_ERR_DB_NO_SUCH_APP           -12
69
70 // There already exists a permission with this name and type
71 #define PC_ERR_DB_PERM_FORBIDDEN        -13
72
73
74 typedef enum {
75         PERM_APP_TYPE_WRT,
76         PERM_APP_TYPE_OSP,
77         PERM_APP_TYPE_OTHER,
78         PERM_APP_TYPE_WRT_PARTNER,
79         PERM_APP_TYPE_WRT_PLATFORM,
80         PERM_APP_TYPE_OSP_PARTNER,
81         PERM_APP_TYPE_OSP_PLATFORM,
82         PERM_APP_TYPE_EFL,
83 } app_type_t;
84
85 typedef enum {
86         PERM_APP_PATH_PRIVATE,
87         PERM_APP_PATH_GROUP,
88         PERM_APP_PATH_PUBLIC,
89         PERM_APP_PATH_SETTINGS,
90         PERM_APP_PATH_NPRUNTIME,
91         PERM_APP_PATH_ANY_LABEL,
92 } app_path_type_t;
93
94
95 // TODO: after all projects change their code delete these defines
96 // Historical in app_type_t
97 #define PERM_APP_TYPE_WGT PERM_APP_TYPE_WRT
98 #define PERM_APP_TYPE_WGT_PARTNER PERM_APP_TYPE_WRT_PARTNER
99 #define PERM_APP_TYPE_WGT_PLATFORM PERM_APP_TYPE_WRT_PLATFORM
100
101
102 #define APP_TYPE_WGT PERM_APP_TYPE_WRT
103 #define APP_TYPE_OSP PERM_APP_TYPE_OSP
104 #define APP_TYPE_OTHER PERM_APP_TYPE_OTHER
105 #define APP_TYPE_WGT_PARTNER PERM_APP_TYPE_WRT_PARTNER
106 #define APP_TYPE_WGT_PLATFORM PERM_APP_TYPE_WRT_PLATFORM
107 #define APP_TYPE_OSP_PARTNER PERM_APP_TYPE_OSP_PARTNER
108 #define APP_TYPE_OSP_PLATFORM PERM_APP_TYPE_OSP_PLATFORM
109 #define APP_TYPE_EFL PERM_APP_TYPE_EFL
110
111 // Historical names in app_path_type_t
112 #define APP_PATH_PRIVATE PERM_APP_PATH_PRIVATE
113 #define APP_PATH_GROUP PERM_APP_PATH_GROUP
114 #define APP_PATH_PUBLIC PERM_APP_PATH_PUBLIC
115 #define APP_PATH_SETTINGS PERM_APP_PATH_SETTINGS
116 #define APP_PATH_ANY_LABEL PERM_APP_PATH_ANY_LABEL
117 #define APP_PATH_GROUP_RW APP_PATH_GROUP
118 #define APP_PATH_PUBLIC_RO APP_PATH_PUBLIC
119 #define APP_PATH_SETTINGS_RW APP_PATH_SETTINGS
120
121
122
123 /* APIs - used by applications */
124 int control_privilege(void) DEPRECATED;
125
126 int set_privilege(const char* pkg_name) DEPRECATED;
127
128 /**
129  * Gets smack label of a process, based on its pid.
130  *
131  * @param  pid          pid of process
132  * @param  smack_label  label of process
133  * @return              PC_OPERATION_SUCCESS on success PC_ERR_* on error.
134  */
135 int get_smack_label_from_process(pid_t pid, char *smack_label);
136
137 /**
138  * Checks if process with pid has access to object.
139  * This function checks if subject has access to object via smack_have_access() function.
140  * If YES then returns access granted. In NO then function checks if process with pid has
141  * CAP_MAC_OVERRIDE capability. If YES then returns access granted.
142  * If NO then returns access denied.
143  *
144  * @param  pid          pid of process
145  * @param  object       label of object to access
146  * @param  access_type  smack access type.
147  * @return              0 (no access) or 1 (access) or -1 (error)
148  */
149 int smack_pid_have_access(pid_t pid,
150                                                         const char* object,
151                                                         const char *access_type);
152
153 /**
154  * Set DAC and SMACK privileges for application.
155  * This function is meant to be called by the application launcher just before
156  * it launches an application. It will setup DAC and SMACK privileges based
157  * on app type and accesses.
158  * It must be called with root privileges, which will be dropped in the function.
159  *
160  * @param  name  package name
161  * @param  type  application type (currently distinguished types:
162  *               "wgt", "wgt_partner", "wgt_platform" and other)
163  * @param  path  file system path to the binary
164  * @return       PC_OPERATION_SUCCESS on success, PC_ERR_* on error
165  */
166 int perm_app_set_privilege(const char* name, const char* type, const char* path);
167 int set_app_privilege(const char* name, const char* type, const char* path) DEPRECATED;
168
169 /**
170  * For a UNIX socket endpoint determine the other side's pkg_id. Caller is
171  * responsible for freeing the return widget id.
172  *
173  * @param  sockfd  socket file descriptor
174  * @return         id of the connecting widget on success, NULL on failure.
175  */
176 char* perm_app_id_from_socket(int sockfd);
177 char* app_id_from_socket(int sockfd) DEPRECATED;
178
179 /**
180  * Adds an application to the database if it doesn't already exist. It is needed
181  * for tracking lifetime of an application. It must be called by privileged
182  * user, before using any other perm_app_* function regarding that application.
183  * It must be called within database transaction started with perm_begin() and
184  * finished with perm_end(). It may be called more than once during installation.
185  *
186  * @param  pkg_id  application identifier
187  * @return         PC_OPERATION_SUCCESS on success, PC_ERR_* on error
188  */
189 int perm_app_install(const char* pkg_id);
190 int app_install(const char* pkg_id) DEPRECATED;
191
192 /**
193  * Removes an application from the database with it's permissions, rules and
194  * directories, enabling future installation of the application with the same
195  * pkg_id. It is needed for tracking lifetime of an application. It must be
196  * called by privileged user and within database transaction started with
197  * perm_begin() and finished with perm_end().
198  *
199  * @param  pkg_id  application identifier
200  * @return         PC_OPERATION_SUCCESS on success, PC_ERR_* on error
201  */
202 int perm_app_uninstall(const char* pkg_id);
203 int app_uninstall(const char* pkg_id) DEPRECATED;
204
205 /**
206  * Inform about installation of new Anti Virus application.
207  * It is intended to be called during Anti Virus installation.
208  * It will give this application SMACK rules to RWX access to all other apps
209  * installed in system.
210  * It must be called by privileged user.
211  *
212  * @param app_id application identifier
213  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error.
214  */
215 int app_register_av(const char* app_av_id) DEPRECATED;
216
217 /**
218  * Grant SMACK permissions based on permissions list.
219  * It is intended to be called during app installation.
220  * It will construct SMACK rules based on permissions list, grant them
221  * and store it in a file, so they will be automatically granted on
222  * system boot.
223  * It must be called by privileged user.
224  * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
225  *
226  *
227  * @param app_id application identifier
228  * @param perm_list array of permission names, last element must be NULL
229  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
230  */
231 int app_add_permissions(const char* app_id, const char** perm_list) DEPRECATED;
232
233 /**
234  * Grant temporary SMACK permissions based on permissions list.
235  * It will construct SMACK rules based on permissions list, grant them,
236  * but not store it anywhere, so they won't be granted again on system boot.
237  * It must be called by privileged user.
238  * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
239  *
240  *
241  * @param app_id application identifier
242  * @param perm_list array of permission names, last element must be NULL
243  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
244  */
245 int app_add_volatile_permissions(const char* app_id, const char** perm_list) DEPRECATED;
246
247 /**
248  * Grants SMACK permissions to an application, based on permissions list. It is
249  * intended to be called during that application installation. The permissions
250  * will be persistent. It must be called by privileged user and within database
251  * transaction started with perm_begin() and finished with perm_end().
252  *
253  * @param  pkg_id     application identifier
254  * @param  app_type   application type
255  * @param  perm_list  array of permission names, last element must be NULL
256  * @return            PC_OPERATION_SUCCESS on success, PC_ERR_* on error
257  */
258 int perm_app_setup_permissions(const char* pkg_id, app_type_t app_type,
259                                const char** perm_list);
260
261 /**
262  * Grants SMACK permissions to an application, based on permissions list. It was
263  * intended to be called during that application installation. Permissions
264  * granted as volatile will not be present after system boot. It must be called
265  * by privileged user and within database transaction started with perm_begin()
266  * and finished with perm_end().
267  * In new code please call perm_app_setup_permissions during your application
268  * installation instead of this function.
269  *
270  * @param  pkg_id      application identifier
271  * @param  app_type    application type
272  * @param  perm_list   array of permission names, last element must be NULL
273  * @param  persistent  boolean for choosing between persistent and temporary rules
274  * @return             PC_OPERATION_SUCCESS on success, PC_ERR_* on error
275  */
276 int perm_app_enable_permissions(const char* pkg_id, app_type_t app_type, const char** perm_list, bool persistent);
277 int app_enable_permissions(const char* pkg_id, app_type_t app_type, const char** perm_list, bool persistent) DEPRECATED;
278
279 /**
280  * Removes previously granted SMACK permissions based on permissions list.
281  * It will remove given permissions from an application, leaving other granted
282  * permissions untouched. Results will be persistent. It must be called by
283  * privileged user and within database transaction started with perm_begin()
284  * and finished with perm_end().
285  *
286  * @param  pkg_id     application identifier
287  * @param  app_type   application type
288  * @param  perm_list  array of permission names, last element must be NULL
289  * @return            PC_OPERATION_SUCCESS on success, PC_ERR_* on error
290  */
291 int perm_app_disable_permissions(const char* pkg_id, app_type_t app_type, const char** perm_list);
292 int app_disable_permissions(const char* pkg_id, app_type_t app_type, const char** perm_list) DEPRECATED;
293
294 /**
295  * Removes all application's permissions, rules and directories registered in
296  * the database. It must be called by privileged user and within database
297  * transaction started with perm_begin() and finished with perm_end().
298  *
299  * @param  pkg_id  application identifier
300  * @return         PC_OPERATION_SUCCESS on success, PC_ERR_* on error
301  */
302 int perm_app_revoke_permissions(const char* pkg_id);
303 int app_revoke_permissions(const char* pkg_id) DEPRECATED;
304
305 /**
306  * Removes all application's permissions which are not persistent. It must be
307  * called by privileged user and within database transaction started with
308  * perm_begin() and finished with perm_end().
309  *
310  * @param  pkg_id  application identifier
311  * @return         PC_OPERATION_SUCCESS on success, PC_ERR_* on error
312  */
313 int perm_app_reset_permissions(const char* pkg_id);
314 int app_reset_permissions(const char* pkg_id) DEPRECATED;
315
316 /**
317  * Checks if an application has the privilege that is specified by the name.
318  * It must be called by privileged user.
319  *
320  * @param  pkg_id           application identifier
321  * @param  app_type         application type
322  * @param  permission_name  permission name
323  * @param  is_enabled       buffer for return value
324  * @return                  PC_OPERATION_SUCCESS on success, PC_ERR_* on error
325  */
326 int perm_app_has_permission(const char *pkg_id,
327                             app_type_t app_type,
328                             const char *permission_name,
329                             bool *is_enabled);
330
331 /**
332  * Get permissions for the specified app.
333  *
334  * In case of success caller is responsible for freeing memory allocated by it.
335  * Each cell in *ppp_perm_list except for the last (NULL) should be freed, followed by freeing
336  * *ppp_perm_list itself.
337  *
338  * In case of error an error code is returned and, provided that ppp_perm_list is not NULL,
339  * *ppp_perm_list is set to NULL.
340  *
341  * @param  pkg_id        application identifier
342  * @param  app_type      application type
343  * @param  ppp_perm_list buffer for return value
344  * @return               PC_OPERATION_SUCCESS on success, PC_ERR_* on error
345  */
346 int perm_app_get_permissions(const char *pkg_id, app_type_t app_type, char ***ppp_perm_list);
347
348 /**
349  * Recursively set SMACK access labels for an application directory
350  * and execute labels for executable files.
351  * This function should be called once during app installation.
352  * Results will be persistent on the file system.
353  * It must be called by privileged user.
354  * THIS FUNCTION IS NOW DEPRECATED. perm_app_setup_path() SHOULD BE USED INSTEAD.
355  *
356  * @param app_label label name
357  * @param path directory path
358  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
359  */
360 int app_label_dir(const char* app_label, const char* path) DEPRECATED;
361
362 /**
363  * Recursively set SMACK access and transmute labels for an application
364  * directory and add SMACK rule for application.
365  * This function should be called once during app installation.
366  * Results will be persistent on the file system.
367  * It must be called by privileged user.
368  * Labels app_label and shared_label should not be equal.
369  * THIS FUNCTION IS NOW DEPRECATED. app_setup_path() SHOULD BE USED INSTEAD.
370  *
371  * @param app_label label name, used as subject for SMACK rule
372  * @param shared_label, used as object for SMACK rule
373  * @param path directory path
374  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
375  */
376 int app_label_shared_dir(const char* app_label, const char* shared_label,
377                                                  const char* path) DEPRECATED;
378
379 /**
380  * Add SMACK rx rules for application identifiers to shared_label.
381  * This function should be called during app installation.
382  * It must be called by privileged user.
383  * THIS FUNCTION IS NOW DEPRECATED. NO REPLACEMENT IS NEEDED.
384  *
385  * @param shared_label label of the shared resource
386  * @param app_list list of application SMACK identifiers
387  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
388  */
389 int add_shared_dir_readers(const char* shared_label, const char** app_list) DEPRECATED;
390
391 /**
392  * Sets SMACK labels for an application directory (recursively) or for an executable/symlink
393  * file. The exact behavior depends on app_path_type argument:
394  *      - APP_PATH_PRIVATE: label with app's label, set access label on everything
395  *    and execute label on executable files and symlinks to executable files
396  *
397  *      - APP_PATH_GROUP: label with given shared_label, set access label on
398  *        everything and enable transmute on directories. Also give pkg_id full access
399  *        to the shared label.
400  *
401  *      - APP_PATH_PUBLIC: label with autogenerated label, set access label on
402  *        everything and enable transmute on directories. Give full access to the label to
403  *        pkg_id and RX access to all other applications.
404  *
405  *      - APP_PATH_SETTINGS: label with autogenerated label, set access label on
406  *        everything and enable transmute on directories. Give full access to the label to
407  *        pkg_id and RWX access to all appsetting applications.
408  *
409  *      - PERM_APP_PATH_NPRUNTIME: label executable file or symlink to an exec given in path param
410  *        with label "<pkg_id>.npruntime". Set execute label on it.
411  *        Give pkg_id RW access to new created label and give new label RXAT access to pkg_id.
412  *
413  *      - APP_PATH_ANY_LABEL: label with given shared_label. Set access label on
414  *        everything and execute label on executable files and symlinks to
415  *        executable files.
416  *
417  * This function should be called during application installation. Results will
418  * be persistent on the file system. It must be called by privileged user and
419  * within database transaction started with perm_begin() and finished with
420  * perm_end().
421  *
422  * @param  pkg_id         application identifier
423  * @param  path           file or directory path
424  * @param  app_path_type  application path type
425  * @param  shared_label   optional argument for APP_PATH_GROUP_RW and
426  *                        APP_PATH_ANY_LABEL path type; type is const char*
427  * @return                PC_OPERATION_SUCCESS on success, PC_ERR_* on error
428  */
429 int perm_app_setup_path(const char* pkg_id, const char* path, app_path_type_t app_path_type, ...);
430 int app_setup_path(const char* pkg_id, const char* path, app_path_type_t app_path_type, ...) DEPRECATED;
431
432 /**
433  * Get paths of the specified type for the given application.
434  *
435  * Provided type must be one of PERM_APP_PATH_GROUP, PERM_APP_PATH_PUBLIC, PERM_APP_PATH_SETTINGS,
436  * PERM_APP_PATH_NPRUNTIME, as other types are not stored in the database.
437  *
438  * In case of success caller is responsible for freeing memory allocated by it.
439  * Each cell in *ppp_paths except for the last (NULL) should be freed, followed by freeing
440  * *ppp_paths itself.
441  *
442  * In case of error an error code is returned and, provided that ppp_paths is not NULL,
443  * *ppp_paths is set to NULL.
444  *
445  * @param  pkg_id        application identifier
446  * @param  app_path_type type of path
447  * @param  ppp_paths     buffer for return value
448  * @return               PC_OPERATION_SUCCESS on success, PC_ERR_* on error
449  */
450 int perm_app_get_paths(const char* pkg_id, app_path_type_t app_path_type, char*** ppp_paths);
451
452 /**
453  * Remove path and all rules associated with it from the database.
454  *
455  * This does not remove data from the filesystem.
456  *
457  * @param  pkg_id application identifier
458  * @param  path   path to remove
459  * @return        PC_OPERATION_SUCCESS on success, PC_ERR_* on error
460  */
461 int perm_app_remove_path(const char* pkg_id, const char *path);
462
463 /**
464  * Make two applications "friends", by giving them both full permissions on
465  * each other.
466  * Results will be persistent on the file system. Must be called after
467  * app_add_permissions() has been called for each application.
468  * It must be called by privileged user.
469  *
470  * @param pkg_id1 first application identifier
471  * @param pkg_id2 second application identifier
472  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
473  */
474 int perm_app_add_friend(const char* pkg_id1, const char* pkg_id2);
475 int app_add_friend(const char* pkg_id1, const char* pkg_id2) DEPRECATED;
476
477 /**
478  * Adds new feature to the database. It must be called by privileged user and
479  * within database transaction started with perm_begin() and finished with
480  * perm_end().
481  *
482  * @param  app_type          application type
483  * @param  api_feature_name  name of newly added feature
484  * @param  smack_rule_set    set of rules required by the feature - NULL terminated
485  *                           list of NULL terminated rules.
486  * @param  list_of_db_gids   list of gids required to access databases controlled
487  *                           by the feature
488  * @return                   PC_OPERATION_SUCCESS on success, PC_ERR_* on error
489  */
490 int perm_add_api_feature(app_type_t app_type,
491                                         const char* api_feature_name,
492                                         const char** set_smack_rule_set,
493                                         const gid_t* list_of_db_gids,
494                                         size_t list_size);
495 int add_api_feature(app_type_t app_type,
496                     const char* api_feature_name,
497                     const char** set_smack_rule_set,
498                     const gid_t* list_of_db_gids,
499                     size_t list_size) DEPRECATED;
500
501
502 /**
503  * Starts exclusive database transaction. Run before functions modifying
504  * database.
505  *
506  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
507  */
508 int perm_begin(void);
509
510 /**
511  * Ends exclusive database transaction. Run after functions modifying database.
512  * If an error occurred during the transaction then all modifications will be
513  * rolled back.
514  *
515  * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
516  */
517 int perm_end(void);
518
519 /**
520  * Adds additional rules to the database. The rules can use wild-cards and labels.
521  * It must be called within database transaction started with perm_begin() and
522  * finished with perm_end().
523  *
524  * @param  set_smack_rule_set  an array of rules, NULL terminated
525  * @return                     PC_OPERATION_SUCCESS on success, PC_ERR_* on error
526  */
527 int perm_add_additional_rules(const char** set_smack_rule_set);
528
529 /**
530  * Get message connected to error code.
531  *
532  * @param errnum error code
533  * @return string describing the error code
534  */
535 const char* perm_strerror(int errnum);
536
537 #ifdef __cplusplus
538 }
539 #endif // __cplusplus
540
541 #endif // _PRIVILEGE_CONTROL_H_