2 * Copyright(c) 2017-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.
20 #include <pkgmgr-info.h>
22 #include "privilege_db_manager.h"
23 #include "privilege_private.h"
24 #include "privilege_package_info.h"
28 #define LOG_TAG "PRIVILEGE_PACKAGE_INFO"
31 #define TryReturn(condition, expr, returnValue, ...) \
38 int privilege_package_info_set_privacy_privilege(const uid_t uid, const char* pkgid, privilege_manager_package_type_e pkg_type, const char* api_version, GList* privilege_list)
41 return PRVMGR_ERR_NONE;
43 TryReturn(pkgid != NULL && api_version != NULL && privilege_list != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] pkgid, api_version, and privilege_list must not be NULL.");
44 int ret = privilege_db_manager_set_package_privacy_privilege_info(uid, pkgid, pkg_type, api_version, privilege_list);
45 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE) {
46 LOGE("privilege_db_manager_set_package_privacy_privilege_info failed. ret = %d", ret);
47 return PRVMGR_ERR_INTERNAL_ERROR;
49 return PRVMGR_ERR_NONE;
52 int privilege_package_info_unset_package_privilege_info(const uid_t uid, const char* pkgid)
55 return PRVMGR_ERR_NONE;
57 TryReturn(pkgid != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] pkgid must not be NULL.");
58 int ret = privilege_db_manager_unset_package_privilege_info(uid, pkgid);
59 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE) {
60 LOGE("privilege_db_manager_unset_package_privilege_info failed. ret = %d", ret);
61 return PRVMGR_ERR_INTERNAL_ERROR;
63 return PRVMGR_ERR_NONE;
66 static int __get_pkg_type(uid_t uid, const char *pkgid, privilege_manager_package_type_e *pkg_type)
70 pkgmgrinfo_pkginfo_h handle = NULL;
71 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
72 if (ret != PMINFO_R_OK) {
73 LOGE("failed to get pkginfo handle! pkgid <%s>", pkgid);
76 ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
77 if (ret != PMINFO_R_OK) {
78 LOGE("failed to get type, pkgid <%s>", pkgid);
79 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
82 if (strcmp(type, "wgt") == 0)
83 *pkg_type = PRVMGR_PACKAGE_TYPE_WRT;
85 *pkg_type = PRVMGR_PACKAGE_TYPE_CORE;
86 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
90 static bool __is_disabled_package(uid_t uid, const char *pkgid)
92 pkgmgrinfo_pkginfo_h handle = NULL;
93 int ret = pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(pkgid, uid, &handle);
96 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
98 if (ret == PMINFO_R_OK) {
105 int privilege_package_info_is_privacy_requestable(const uid_t uid, const char* pkgid, const char* privilege, bool* is_requestable)
107 TryReturn(pkgid != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] pkgid must not be NULL.");
109 privilege_manager_package_type_e pkg_type = PRVMGR_PACKAGE_TYPE_NONE;
110 int ret = __get_pkg_type(uid, pkgid, &pkg_type);
111 TryReturn(ret == 0 && pkg_type != PRVMGR_PACKAGE_TYPE_NONE, , PRVMGR_ERR_INTERNAL_ERROR, "[PRVMGR_ERR_INTERNAL_ERROR] failed to get pkg type of <%s>", pkgid);
112 ret = privilege_db_manager_is_privacy_requestable(uid, pkgid, privilege, pkg_type, is_requestable);
114 if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) { // CASE: given pkgid have no privacy privileges
115 *is_requestable = false;
116 } else if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE) {
117 LOGE("privilege_db_manager_is_privacy_requestable_package failed. ret = %d", ret);
118 return PRVMGR_ERR_INTERNAL_ERROR;
120 return PRVMGR_ERR_NONE;
123 int privilege_package_info_get_all_privacy_package_list(const uid_t uid, GList** privacy_list)
125 int ret = privilege_db_manager_get_all_privacy_package_list(uid, privacy_list);
126 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE && ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) {
127 LOGE("privilege_db_manager_get_all_privacy_package_list failed. ret = %d", ret);
128 return PRVMGR_ERR_INTERNAL_ERROR;
132 for(l = *privacy_list; l != NULL;) { // Remove disabled packages
134 if (__is_disabled_package(uid, (const char*)l->data)) {
135 LOGD("uid = %d, pkgid = %s is disabled package. Remove.", uid, (char*)l->data);
136 *privacy_list = g_list_remove(*privacy_list, l->data);
141 return PRVMGR_ERR_NONE;
144 int privilege_package_info_get_privacy_list_by_pkgid(const uid_t uid, const char* pkgid, GList** privacy_list)
146 TryReturn(pkgid != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] pkgid must not be null");
147 int ret = privilege_db_manager_get_privacy_list_by_pkgid(uid, pkgid, privacy_list);
148 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE && ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) {
149 LOGE("privilege_db_manager_get_privacy_list_by_pkgid failed. ret = %d", ret);
150 return PRVMGR_ERR_INTERNAL_ERROR;
152 return PRVMGR_ERR_NONE;
155 int privilege_package_info_get_package_list_by_privacy(const uid_t uid, const char* privacy, GList** package_list)
157 TryReturn(privacy != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] privacy must not be null");
158 int ret = privilege_db_manager_get_package_list_by_privacy(uid, privacy, package_list);
159 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE && ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) {
160 LOGE("privilege_db_manager_get_package_list_by_privacy failed. ret = %d", ret);
161 return PRVMGR_ERR_INTERNAL_ERROR;
165 for(l = *package_list; l != NULL;) { // Remove disabled packages
167 if (__is_disabled_package(uid, (const char*)l->data)) {
168 LOGD("uid = %d, pkgid = %s is disabled package. Remove.", uid, (char*)l->data);
169 *package_list = g_list_remove(*package_list, l->data);
173 return PRVMGR_ERR_NONE;
176 int privilege_package_info_get_privilege_list_by_pkgid_and_privacy(const uid_t uid, const char* pkgid, const char* privacy, GList** privilege_list)
178 TryReturn(pkgid != NULL && privacy != NULL, , PRVMGR_ERR_INVALID_PARAMETER, "[PRVMGR_ERR_INVALID_PARAMETER] pkgid and privacy must not be null");
179 int ret = privilege_db_manager_get_privilege_list_by_pkgid_and_privacy(uid, pkgid, privacy, privilege_list);
180 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE && ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT) {
181 LOGE("privilege_db_manager_get_privilege_list_by_pkgid_and_privacy failed. ret = %d", ret);
182 return PRVMGR_ERR_INTERNAL_ERROR;
184 return PRVMGR_ERR_NONE;