6f17e3056c92f49173ec714cfe6caf69d35b86a2
[platform/core/security/privilege-info.git] / src / privilege_info.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <libintl.h>
4 #include <dlog.h>
5 #include <privilege_db_manager.h>
6 #include "privilege_information.h"
7 #ifdef LOG_TAG
8 #undef LOG_TAG
9 #define LOG_TAG "PRIVILEGE_INFO"
10 #endif
11
12 #define TryReturn(condition, expr, returnValue, ...)  \
13         if (!(condition)) { \
14                 expr; \
15                 LOGE(__VA_ARGS__); \
16                 return returnValue; \
17         }
18
19 #ifndef PI_API
20 #define PI_API __attribute__((visibility("default")))
21 #endif
22
23 typedef enum {
24         PRVINFO_ERROR_NO_MATCHING_PRIVILEGE             = TIZEN_ERROR_PRIVILEGE_INFORMATION | 0x01
25 } privilege_info_internal_error_e;
26
27 int privilege_info_get_string_id(const char* package_type_string, int display, const char* api_version, const char *privilege, char **string_id)
28 {
29         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
30         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
31
32         char* temp = NULL;
33         int ret;
34         privilege_db_manager_package_type_e package_type;
35
36         if (package_type_string != NULL)
37                 goto get_string_id_with_package_type;
38
39         /* Check NATIVE */
40         if (display)
41                 ret = privilege_db_manager_get_privilege_display(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
42         else
43                 ret = privilege_db_manager_get_privilege_description(PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
44
45         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE)
46                 goto err_none;
47         else if (ret != PRIVILEGE_DB_NO_EXIST_RESULT)
48                 goto err_internal_error;
49
50         /* Check WEB */
51
52 get_string_id_with_package_type:
53         if (package_type_string == NULL || strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_WEB") == 0)
54                 package_type = PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_WRT;
55         else if (strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0)
56                 package_type = PRIVILEGE_DB_MANAGER_PACKAGE_TYPE_CORE;
57         else
58                 return PRVINFO_ERROR_INVALID_PARAMETER;
59
60         if (display)
61                 ret = privilege_db_manager_get_privilege_display(package_type, privilege, api_version, &temp);
62         else
63                 ret = privilege_db_manager_get_privilege_description(package_type, privilege, api_version, &temp);
64
65
66         if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE)
67                 goto err_none;
68         else if (ret == PRIVILEGE_DB_NO_EXIST_RESULT)
69                 goto err_no_matching_privilege;
70         else
71                 goto err_internal_error;
72
73 err_none:
74         *string_id = strdup(temp);
75         TryReturn(*string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
76         free(temp);
77         return PRVINFO_ERROR_NONE;
78
79 err_no_matching_privilege:
80         *string_id = NULL;
81         free(temp);
82         return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
83
84 err_internal_error:
85         free(temp);
86         return PRVINFO_ERROR_INTERNAL_ERROR;
87 }
88
89 int privilege_info_get_string_by_string_id(const char *string_id, char **string)
90 {
91         char *temp = NULL;
92         TryReturn(string_id != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] string_id is NULL");
93
94         temp = dgettext("privilege", string_id);
95
96         *string = strdup(temp);
97         TryReturn(*string != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of string failed.");
98
99         return PRVINFO_ERROR_NONE;
100 }
101
102 PI_API
103 int privilege_info_get_display_name(const char* api_version, const char* privilege, char** display_name)
104 {
105         int ret = 0;
106         char* string_id = NULL;
107
108         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
109         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
110
111         ret = privilege_info_get_string_id(NULL, 1, api_version, privilege, &string_id);
112
113         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
114                 char* tempPrivilege = NULL;
115                 char* token = NULL;
116                 char* temp = NULL;
117                 char* save = NULL;
118
119                 tempPrivilege = strdup(privilege);
120                 TryReturn(tempPrivilege != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] tempPrivilege's strdup is failed.");
121
122                 token = strtok_r(tempPrivilege, "/", &save);
123                 while (token) {
124                         temp = token;
125                         token = strtok_r(NULL, "/", &save);
126                 }
127                 *display_name = strdup(temp);
128                 TryReturn(*display_name != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
129                 free(tempPrivilege);
130         } else if (ret == PRVINFO_ERROR_NONE) {
131                 ret = privilege_info_get_string_by_string_id(string_id, display_name);
132                 free(string_id);
133                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
134         } else {
135                 return PRVINFO_ERROR_INTERNAL_ERROR;
136         }
137         return PRVINFO_ERROR_NONE;
138 }
139
140
141 PI_API
142 int privilege_info_get_description(const char* api_version, const char* privilege, char** description)
143 {
144         int ret = 0;
145         char* string_id = NULL;
146
147         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
148         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
149
150         ret = privilege_info_get_string_id(NULL, 0, api_version, privilege, &string_id);
151
152         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
153                 ret = privilege_info_get_string_by_string_id("IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED", description);
154                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
155         } else if (ret == PRVINFO_ERROR_NONE) {
156                 ret = privilege_info_get_string_by_string_id(string_id, description);
157                 free(string_id);
158                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
159         } else {
160                 return PRVINFO_ERROR_INTERNAL_ERROR;
161         }
162         return PRVINFO_ERROR_NONE;
163 }
164
165 PI_API
166 int privilege_info_get_display_name_by_pkgtype(const const char* package_type, const char* api_version, const char* privilege, char** display_name)
167 {
168         int ret = 0;
169         char* string_id = NULL;
170
171         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
172         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
173         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
174
175         ret = privilege_info_get_string_id(package_type, 1, api_version, privilege, &string_id);
176         TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
177
178         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
179                 char* tempPrivilege = NULL;
180                 char* token = NULL;
181                 char* temp = NULL;
182                 char* save = NULL;
183                 tempPrivilege = strdup(privilege);
184                 TryReturn(tempPrivilege != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] tempPrivilege's strdup is failed.");
185                 token = strtok_r(tempPrivilege, "/", &save);
186                 while (token) {
187                         temp = token;
188                         token = strtok_r(NULL, "/", &save);
189                 }
190                 *display_name = strdup(temp);
191                 TryReturn(*display_name != NULL, free(tempPrivilege), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
192                 free(tempPrivilege);
193         } else if (ret == PRVINFO_ERROR_NONE) {
194                 ret = privilege_info_get_string_by_string_id(string_id, display_name);
195                 free(string_id);
196                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
197         } else {
198                 return PRVINFO_ERROR_INTERNAL_ERROR;
199         }
200         return PRVINFO_ERROR_NONE;
201 }
202
203 PI_API
204 int privilege_info_get_description_by_pkgtype(const char* package_type, const char* api_version, const char* privilege, char** description)
205 {
206         int ret = 0;
207         char* string_id = NULL;
208
209         TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
210         TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
211         TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
212
213         ret = privilege_info_get_string_id(package_type, 0, api_version, privilege, &string_id);
214         TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
215
216         if (ret == PRVINFO_ERROR_NO_MATCHING_PRIVILEGE) {
217                 ret = privilege_info_get_string_by_string_id("IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED", description);
218                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
219         } else if (ret == PRVINFO_ERROR_NONE) {
220                 ret = privilege_info_get_string_by_string_id(string_id, description);
221                 free(string_id);
222                 TryReturn(ret == PRVINFO_ERROR_NONE, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
223         } else {
224                 return PRVINFO_ERROR_INTERNAL_ERROR;
225         }
226         return PRVINFO_ERROR_NONE;
227 }
228