2 * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <system_info.h>
25 #include <privilege_db_manager.h>
26 #include <privilege_info.h>
28 #include "privilege_information.h"
32 #define LOG_TAG "PRIVILEGE_INFO"
35 #define PRIVACY_FEATURE "http://tizen.org/feature/security.privacy_privilege"
36 #define CHECK_FEATURE_SUPPORTED(feature_name) \
38 bool is_supported = false; \
39 int ret = system_info_get_platform_bool(feature_name, &is_supported); \
40 if (ret != SYSTEM_INFO_ERROR_NONE) { \
41 LOGE("system_info_get_platform_bool failed. ret = %d", ret); \
42 return PRVINFO_ERROR_INTERNAL_ERROR; \
44 if (!is_supported) { \
45 LOGE("%s is disabled", feature_name); \
46 return PRVINFO_ERROR_NOT_SUPPORTED; \
50 #define TryReturn(condition, expr, returnValue, ...) \
58 #define PI_API __attribute__((visibility("default")))
61 int privilege_info_get_string_id(const char *package_type_string, int display, const char *api_version, const char *privilege, char **string_id)
65 privilege_manager_package_type_e package_type;
67 if (package_type_string != NULL)
68 goto get_string_id_with_package_type;
72 ret = privilege_db_manager_get_privilege_display(PRVMGR_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
74 ret = privilege_db_manager_get_privilege_description(PRVMGR_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
76 if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE && temp != NULL)
78 else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT)
83 get_string_id_with_package_type:
84 if (package_type_string == NULL || strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_WEB") == 0)
85 package_type = PRVMGR_PACKAGE_TYPE_WRT;
86 else if (strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0)
87 package_type = PRVMGR_PACKAGE_TYPE_CORE;
89 return PRVINFO_ERROR_INVALID_PARAMETER;
92 ret = privilege_db_manager_get_privilege_display(package_type, privilege, api_version, &temp);
94 ret = privilege_db_manager_get_privilege_description(package_type, privilege, api_version, &temp);
96 if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE && temp != NULL)
102 *string_id = strdup(temp);
103 TryReturn(*string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
105 return PRVINFO_ERROR_NONE;
111 if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT)
112 return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
114 return PRVINFO_ERROR_INTERNAL_ERROR;
117 int privilege_info_get_string_by_string_id(const char *string_id, char **string)
119 TryReturn(string_id != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] string_id is NULL");
121 temp = dgettext("privilege", string_id);
123 *string = strdup(temp);
124 TryReturn(*string != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of string failed.");
126 return PRVINFO_ERROR_NONE;
130 int privilege_info_get_display_name(const char *api_version, const char *privilege, char **display_name)
132 TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
133 TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
136 char* string_id = NULL;
138 ret = privilege_info_get_string_id(NULL, 1, api_version, privilege, &string_id);
140 if (ret == PRVINFO_ERROR_NONE)
141 ret = privilege_info_get_string_by_string_id(string_id, display_name);
142 if (string_id != NULL)
148 int privilege_info_get_description(const char *api_version, const char *privilege, char **description)
150 TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
151 TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
154 char* string_id = NULL;
156 ret = privilege_info_get_string_id(NULL, 0, api_version, privilege, &string_id);
158 if (ret == PRVINFO_ERROR_NONE)
159 ret = privilege_info_get_string_by_string_id(string_id, description);
160 if (string_id != NULL)
167 int privilege_info_get_display_name_by_pkgtype(const char *package_type, const char *api_version, const char *privilege, char **display_name)
169 TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
170 TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
171 TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
174 char* string_id = NULL;
176 ret = privilege_info_get_string_id(package_type, 1, api_version, privilege, &string_id);
177 TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
179 if (ret == PRVINFO_ERROR_NONE)
180 ret = privilege_info_get_string_by_string_id(string_id, display_name);
181 if (string_id != NULL)
188 int privilege_info_get_description_by_pkgtype(const char *package_type, const char *api_version, const char *privilege, char **description)
190 TryReturn(package_type != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] package_type is NULL");
191 TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
192 TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
195 char* string_id = NULL;
197 ret = privilege_info_get_string_id(package_type, 0, api_version, privilege, &string_id);
198 TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
200 if (ret == PRVINFO_ERROR_NONE)
201 ret = privilege_info_get_string_by_string_id(string_id, description);
202 if (string_id != NULL)
209 int privilege_info_get_privacy_display_name(const char *privilege, char **privacy_display_name)
211 CHECK_FEATURE_SUPPORTED(PRIVACY_FEATURE);
212 TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
213 TryReturn(privilege_db_manager_is('p', privilege) == 1, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege does not exist or is not a privacy related");
215 char* privacy_id = NULL;
216 char* privacy_display_string_id = NULL;
218 TryReturn(privilege_db_manager_get_privacy_by_privilege(privilege, &privacy_id) == PRIVILEGE_DB_MANAGER_ERR_NONE && privacy_id != NULL, , PRVINFO_ERROR_INTERNAL_ERROR, "[PRVINFO_ERROR_INTERNAL_ERROR] privilege_db_manager_get_privacy_by_privilege failed");
220 TryReturn(privilege_db_manager_get_privacy_display(privacy_id, &privacy_display_string_id) == PRIVILEGE_DB_MANAGER_ERR_NONE && privacy_display_string_id != NULL, free(privacy_id), PRVINFO_ERROR_INTERNAL_ERROR, "[PRVINFO_ERROR_INTERNAL_ERROR] privilege_db_manager_get_privacy_display failed");
222 TryReturn(privilege_info_get_string_by_string_id(privacy_display_string_id, privacy_display_name) == PRVINFO_ERROR_NONE && *privacy_display_name != NULL, free(privacy_id); free(privacy_display_string_id), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
225 free(privacy_display_string_id);
226 return PRVINFO_ERROR_NONE;
230 int privilege_info_get_privilege_info_list(const char* locale, GList* privilege_name_list, GList** privilege_info_list, privilege_consumer_return_code_e* return_result)
233 GList *temp_privilege_info_list = NULL;
234 char* privilege_display = NULL;
235 char* privilege_description = NULL;
236 int is_invaild_parameter_count = 0;
237 int privilege_name_list_size = 0;
238 privilege_consumer_return_code_e consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_SUCCESS;
239 int ret = PRVINFO_ERROR_NONE;
241 if (privilege_name_list == NULL) {
242 LOGE("[PRVINFO_ERROR_INVALID_PARAMETER] privilege_list is NULL");
243 return PRVINFO_ERROR_INVALID_PARAMETER;
245 char *orig_locale = NULL;
246 char *result = (char *)setlocale(LC_ALL, NULL);
248 LOGE("failed to get original locale. errno = %d", errno);
250 orig_locale = strdup(result);
251 TryReturn(orig_locale != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of orig_locale failed");
252 LOGD("orig_locale = %s", orig_locale);
254 result = (char *)setlocale(LC_ALL, locale);
256 LOGI("succeeded in setting locale = %s", result);
258 LOGE("failed to set locale. set locale to en_US.UTF8. errno = %d", errno);
259 setlocale(LC_ALL, "en_US.UTF8");
261 consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_UNKNOWN_LOCALE_CODE;
264 for (l = privilege_name_list; l != NULL; l = l->next) {
265 char* privilege_name = (char*)l->data;
267 LOGI("privilege_name = %s", privilege_name);
269 ret = privilege_info_get_privilege_display_name(privilege_name, &privilege_display);
270 if (ret == PRVMGR_ERR_NONE) {
271 if (privilege_display != NULL) {
272 LOGI("display : %s", privilege_display);
274 privilege_display = strdup("");
275 if (privilege_display == NULL) {
276 LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
277 ret = PRVINFO_ERROR_OUT_OF_MEMORY;
280 is_invaild_parameter_count++;
283 LOGE("failed to call privilege_info_get_privilege_display_name.");
284 ret = PRVINFO_ERROR_INTERNAL_ERROR;
288 LOGI("privilege_display = %s", privilege_display);
290 ret = privilege_info_get_privilege_description(privilege_name, &privilege_description);
291 if (ret == PRVMGR_ERR_NONE) {
292 if (privilege_description != NULL) {
293 LOGI("description : %s", privilege_description);
295 privilege_description = strdup("");
296 if (privilege_description == NULL) {
297 LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
298 ret = PRVINFO_ERROR_OUT_OF_MEMORY;
303 LOGE("failed to call privilege_info_get_privilege_description.");
304 ret = PRVINFO_ERROR_INTERNAL_ERROR;
308 LOGD("privilege_description = %s", privilege_description);
310 privilege_info_s* privilege_info = (privilege_info_s*)malloc(sizeof(privilege_info_s));
311 memset(privilege_info, 0, sizeof(privilege_info_s));
313 privilege_info->privilege_name = strdup(privilege_name);
314 if (privilege_info->privilege_name == NULL) {
315 LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
316 ret = PRVINFO_ERROR_OUT_OF_MEMORY;
317 free(privilege_info);
320 privilege_info->display_name = strdup(privilege_display);
321 if (privilege_info->display_name == NULL) {
322 LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
323 ret = PRVINFO_ERROR_OUT_OF_MEMORY;
324 free(privilege_info->privilege_name);
325 free(privilege_info);
328 privilege_info->description = strdup(privilege_description);
329 if (privilege_info->description == NULL) {
330 LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
331 ret = PRVINFO_ERROR_OUT_OF_MEMORY;
332 free(privilege_info->privilege_name);
333 free(privilege_info->display_name);
334 free(privilege_info);
337 LOGI("privilege_info->privilege_name = %s", privilege_info->privilege_name);
338 LOGI("privilege_info->display_name = %s", privilege_info->display_name);
339 LOGI("privilege_info->description = %s", privilege_info->description);
341 temp_privilege_info_list = g_list_append(temp_privilege_info_list, privilege_info);
343 privilege_name_list_size++;
345 if (privilege_display != NULL) {
346 free(privilege_display);
347 privilege_display = NULL;
350 if (privilege_description != NULL) {
351 free(privilege_description);
352 privilege_description = NULL;
356 if (is_invaild_parameter_count == privilege_name_list_size)
357 consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_INVALID_PARAMETER;
361 *privilege_info_list = temp_privilege_info_list;
362 *return_result = consumer_return_code;
364 if (privilege_display != NULL) {
365 free(privilege_display);
366 privilege_display = NULL;
369 if (privilege_description != NULL) {
370 free(privilege_description);
371 privilege_description = NULL;
373 if (orig_locale != NULL) {
374 setlocale(LC_ALL, orig_locale);
383 int privilege_info_free_privilege_info_list(GList* privilege_info_list)
385 if (privilege_info_list == NULL)
386 return PRVINFO_ERROR_INVALID_PARAMETER;
389 for (l = privilege_info_list; l != NULL; l = l->next) {
390 privilege_info_s* privilege_info = (privilege_info_s*)l->data;
391 free(privilege_info->privilege_name);
392 free(privilege_info->display_name);
393 free(privilege_info->description);
395 g_list_free(privilege_info_list);
396 return PRVINFO_ERROR_NONE;