From: Yunjin Lee Date: Mon, 22 Jun 2020 08:39:57 +0000 (+0900) Subject: Revise logic to get privilege display name and description X-Git-Tag: submit/tizen/20200720.104233~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7c29d9bb9abd57f69f68bdad95ef3ccf93d384a8;p=platform%2Fcore%2Fsecurity%2Fprivilege-checker.git Revise logic to get privilege display name and description - Remove redundant code and branch that never passed through. - Return NULL if the given privilege have no DID to display - The same changes as privilege-info : https://review.tizen.org/gerrit/#/c/platform/core/security/privilege-info/+/231441/ Change-Id: I7093d1a38970ca2a11b0933025845baed2a1e04b Signed-off-by: Yunjin Lee --- diff --git a/capi/src/privilege_info.c b/capi/src/privilege_info.c index 8fdb5a7..3cef4a3 100755 --- a/capi/src/privilege_info.c +++ b/capi/src/privilege_info.c @@ -36,6 +36,13 @@ return returnValue; \ } +#define SafeFree(var) { \ + if (var != NULL) { \ + free(var); \ + var = NULL; \ + } \ +} + void privilege_info_list_free(GList* list) { g_list_free_full(list, free); @@ -311,240 +318,121 @@ int privilege_info_get_privilege_group_display_name(const char *privilege_group, return PRVMGR_ERR_NONE; } -int privilege_info_get_name_string_id(const char *privilege, char **name_string_id) +static int __get_privilege_display_name_string_id_by_package_type(privilege_manager_package_type_e package_type, const char* privilege, char** string_id) { - TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is NULL"); - - char *temp = NULL; - - /* Check Native */ - int ret = privilege_db_manager_get_privilege_display(PRVMGR_PACKAGE_TYPE_CORE, privilege, NULL, &temp); - + char* temp = NULL; + int ret = privilege_db_manager_get_privilege_display(package_type, privilege, NULL, &temp); if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) { - if (temp == NULL) { - *name_string_id = NULL; - } else if (strcmp(temp, "") == 0) { - *name_string_id = strdup(""); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; + if (strcmp(temp, "") != 0) { + *string_id = strdup(temp); + TryReturn(string_id != NULL, SafeFree(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] strdup() failed."); } else { - *name_string_id = (char *)calloc(strlen(temp) + 1, sizeof(char)); - TryReturn(*name_string_id != NULL, free(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation is failed."); - memcpy(*name_string_id, temp, strlen(temp)); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; - } - } else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; - } - - if (temp != NULL) { - free(temp); - temp = NULL; - } - /* Check WRT */ - ret = privilege_db_manager_get_privilege_display(PRVMGR_PACKAGE_TYPE_WRT, privilege, NULL, &temp); - - if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) { - if (temp == NULL) { - *name_string_id = NULL; - } else if (strcmp(temp, "") == 0) { - *name_string_id = strdup(""); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; - } else { - *name_string_id = (char *)calloc(strlen(temp) + 1, sizeof(char)); - TryReturn(*name_string_id != NULL, free(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation is failed."); - memcpy(*name_string_id, temp, strlen(temp)); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; + LOGE("%s privilege %s exist but have no display name", package_type == PRVMGR_PACKAGE_TYPE_WRT ? "wrt" : "core", privilege); } - } else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; + ret = PRVMGR_ERR_NONE; + } else if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { + LOGD("%s privilege %s not exist", package_type == PRVMGR_PACKAGE_TYPE_WRT ? "wrt" : "core", privilege); + ret = PRVMGR_ERR_NO_EXIST_PRIVILEGE; } else { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; + LOGE("privilege_db_manager_get_privilege_display() failed. ret = %d", ret); + ret = PRVMGR_ERR_INTERNAL_ERROR; } - - return PRVMGR_ERR_NONE; + SafeFree(temp); + return ret; } -int privilege_info_get_privilege_display_name(const char *privilege, char **name) +static int __get_privilege_display_name_string_id(const char *privilege, char **string_id) { - int ret = 0; - char *name_string_id = NULL; - TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is NULL"); - ret = privilege_info_get_name_string_id(privilege, &name_string_id); - if (name_string_id == NULL) { - /* - char tempPrivilege[256] = {0, }; - char* temp = NULL; - char* buffer = NULL; - memcpy(tempPrivilege, privilege, strlen(privilege)); - temp = strtok(tempPrivilege, "/"); - while(temp) - { - buffer = temp; - temp = strtok(NULL, "/"); - } - *name = (char*)calloc(strlen(buffer) + 1, sizeof(char)); - TryReturn(*name != NULL, PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation failed."); - - memcpy(*name, buffer, strlen(buffer)); - - *name = strdup(""); */ - } else if (strcmp(name_string_id, "") == 0) { - *name = strdup("display string is not defined yet"); - } else { - ret = privilege_info_get_privilege_string_by_string_id(name_string_id, name); - free(name_string_id); - name_string_id = NULL; - TryReturn(ret == PRVMGR_ERR_NONE, , PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation failed."); - } + int ret = __get_privilege_display_name_string_id_by_package_type(PRVMGR_PACKAGE_TYPE_CORE, privilege, string_id); - if (name_string_id != NULL) - free(name_string_id); + if (ret == PRVMGR_ERR_NO_EXIST_PRIVILEGE) + ret = __get_privilege_display_name_string_id_by_package_type(PRVMGR_PACKAGE_TYPE_WRT, privilege, string_id); - return PRVMGR_ERR_NONE; + if (ret == PRVMGR_ERR_NO_EXIST_PRIVILEGE) + LOGE("Privilege [%s] doesn't exist for any type of package", privilege); + + return ret; } -int privilege_info_get_description_string_id(const char *privilege, char **description_string_id) +int privilege_info_get_privilege_display_name(const char *privilege, char **name) { TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is NULL"); - char *temp = NULL; - - /* Check Native */ - int ret = privilege_db_manager_get_privilege_description(PRVMGR_PACKAGE_TYPE_CORE, privilege, NULL, &temp); + char *string_id = NULL; + int ret = 0; - if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) { - if (temp == NULL) { - *description_string_id = NULL; - } else if (strcmp(temp, "") == 0) { - *description_string_id = strdup(""); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; - } else { - *description_string_id = (char *)calloc(strlen(temp) + 1, sizeof(char)); - TryReturn(*description_string_id != NULL, free(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation is failed."); - memcpy(*description_string_id, temp, strlen(temp)); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; - } - } else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; + ret = __get_privilege_display_name_string_id(privilege, &string_id); + if (ret == PRVMGR_ERR_NONE) { + if (string_id != NULL) + ret = privilege_info_get_privilege_string_by_string_id(string_id, name); + } else if (ret == PRVMGR_ERR_NO_EXIST_PRIVILEGE) { + ret = PRVMGR_ERR_NONE; + } else { + LOGE("__get_privilege_display_name_string_id() failed. ret = %d", ret); } - if (temp != NULL) { - free(temp); - temp = NULL; - } - /* Check WRT */ - ret = privilege_db_manager_get_privilege_description(PRVMGR_PACKAGE_TYPE_WRT, privilege, NULL, &temp); + SafeFree(string_id); + + return ret; +} +static int __get_privilege_description_string_id_by_package_type(privilege_manager_package_type_e package_type, const char* privilege, char** string_id) +{ + char* temp = NULL; + int ret = privilege_db_manager_get_privilege_description(package_type, privilege, NULL, &temp); if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE) { - if (temp == NULL) { - *description_string_id = NULL; - } else if (strcmp(temp, "") == 0) { - *description_string_id = strdup(""); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; + if (strcmp(temp, "") != 0) { + *string_id = strdup(temp); + TryReturn(string_id != NULL, SafeFree(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] strdup() failed."); } else { - *description_string_id = (char *)calloc(strlen(temp) + 1, sizeof(char)); - TryReturn(*description_string_id != NULL, free(temp), PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation is failed."); - memcpy(*description_string_id, temp, strlen(temp)); - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_NONE; + LOGE("%s privilege %s exist but have no description", package_type == PRVMGR_PACKAGE_TYPE_WRT ? "wrt" : "core", privilege); } - } else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; + ret = PRVMGR_ERR_NONE; + } else if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { + LOGD("%s privilege %s not exist", package_type == PRVMGR_PACKAGE_TYPE_WRT ? "wrt" : "core", privilege); + ret = PRVMGR_ERR_NO_EXIST_PRIVILEGE; } else { - if (temp != NULL) { - free(temp); - temp = NULL; - } - return PRVMGR_ERR_INTERNAL_ERROR; + LOGE("privilege_db_manager_get_privilege_description() failed. ret = %d", ret); + ret = PRVMGR_ERR_INTERNAL_ERROR; } + SafeFree(temp); + return ret; +} - return PRVMGR_ERR_NONE; +static int __get_privilege_description_string_id(const char *privilege, char **string_id) +{ + TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is NULL"); + + int ret = __get_privilege_description_string_id_by_package_type(PRVMGR_PACKAGE_TYPE_CORE, privilege, string_id); + + if (ret == PRVMGR_ERR_NO_EXIST_PRIVILEGE) + ret = __get_privilege_description_string_id_by_package_type(PRVMGR_PACKAGE_TYPE_WRT, privilege, string_id); + + return ret; } int privilege_info_get_privilege_description(const char *privilege, char **description) { - int ret = 0; - char *description_string_id = NULL; - TryReturn(privilege != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privilege is NULL"); - ret = privilege_info_get_description_string_id(privilege, &description_string_id); - if (description_string_id == NULL) { - /* - char *temp = NULL; - temp = dgettext("privilege", "IDS_TPLATFORM_BODY_THIS_PRIVILEGE_IS_NOT_DEFINED"); - *description = (char*)calloc(strlen(temp) + 1, sizeof(char)); - TryReturn(*description != NULL, , PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation failed."); - - memcpy(*description, temp, strlen(temp)); + char *string_id = NULL; + int ret = 0; - *description = strdup("");*/ - } else if (strcmp(description_string_id, "") == 0) { - *description = strdup("description string is not defined yet"); + ret = __get_privilege_description_string_id(privilege, &string_id); + if (ret == PRVMGR_ERR_NONE) { + if (string_id != NULL) + ret = privilege_info_get_privilege_string_by_string_id(string_id, description); + } else if (ret == PRVMGR_ERR_NO_EXIST_PRIVILEGE) { + ret = PRVMGR_ERR_NONE; } else { - ret = privilege_info_get_privilege_string_by_string_id(description_string_id, description); - free(description_string_id); - description_string_id = NULL; - TryReturn(ret == PRVMGR_ERR_NONE, , PRVMGR_ERR_OUT_OF_MEMORY, "[PRVMGR_ERR_OUT_OF_MEMORY] Memory allocation failed."); + LOGE("__get_privilege_description_string_id() failed. ret = %d", ret); } - if (description_string_id != NULL) - free(description_string_id); + SafeFree(string_id); - return PRVMGR_ERR_NONE; + return ret; } int privilege_info_is_privacy(const char* privilege)