2 * Copyright (c) 2016-2018 Samsung Electronics Co.
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
17 * @file src/agent/notification-daemon/Privilege.cpp
18 * @author Zofia Grzelewska <z.abramowska@samsung.com>
19 * @brief Implementation of Privilege Info wrappers
25 #include <sys/types.h>
29 #include "PrivilegeInfo.h"
31 #include <exception/Exception.h>
34 #include <privilegemgr/privilege_info.h>
35 #include <privilegemgr/privilege_db_manager.h>
40 namespace PrivilegeInfo {
46 GListWrap() : m_head(nullptr) {
47 m_head = g_list_alloc();
49 ALOGE("Failed to allocate glib list");
54 GListWrap(GList *_list) : m_head(_list) {}
56 ~GListWrap() { g_list_free(m_head);}
58 void append(gpointer data) {
59 GList *newHead = g_list_prepend(m_head, data);
60 if (newHead == nullptr) {
61 ALOGE("Failed to prepend the glib list");
66 GList * get() { return m_head; }
72 Privacy getPrivacyName(const Privilege &privilege) {
73 char* privacyName = nullptr;
74 int ret = privilege_info_get_privacy_by_privilege(privilege.c_str(), &privacyName);
75 if (ret != PRVMGR_ERR_NONE || !privacyName) {
76 ALOGE("Unable to get privacy group for privilege: <" << privilege << ">, err: <" << ret << ">");
80 std::unique_ptr<char, decltype(free) *> privacyNamePtr(privacyName, free);
81 return std::string(privacyName);
84 } //namespace anonymous
86 std::vector<Privacy> getPrivilegesPrivacies(const std::vector<std::string> &corePrivileges) {
87 std::set<Privacy> privaciesSet;
88 for (auto &privilege : corePrivileges) {
89 ALOGD("Getting privacy for privilege : " << privilege);
90 if (!isPrivacy(privilege)) {
91 ALOGD("Privilege " << privilege << " is not privacy, skipping");
94 Privacy privacy = getPrivacyName(privilege);
95 if (privacy.empty()) {
96 // FIXME: should we abort whole request or ignore privilege which cannot be mapped to privacy?
97 ALOGE("Something went wrong with fetching privacy name for " << privilege << ", aborting");
100 ALOGD("Privilege belongs to privacy " << privacy);
102 privaciesSet.insert(privacy);
104 return std::vector<Privacy>(privaciesSet.begin(), privaciesSet.end());
107 std::vector<Privilege> getPrivilegeMapping(AppInfo &appInfo, const std::string &appId, const Privilege &privilege) {
108 ALOGD("Mapping privilege " << privilege);
109 uid_t uid = geteuid();
110 std::string version = appInfo.apiVersion(appId, uid);
111 std::string type = appInfo.type(appId, uid);
113 if (version.empty() || type.empty()) {
114 ALOGE("Failed to fetch application version and type");
117 ALOGD("App " << appId << " is of type " << type << " and version " << version);
119 privilege_manager_package_type_e pkgType;
120 if (type == "c++app" || type == "capp") {
121 pkgType = PRVMGR_PACKAGE_TYPE_CORE;
122 } else if (type == "webapp") {
123 pkgType = PRVMGR_PACKAGE_TYPE_WRT;
125 ALOGD("Application type not supported by mapping : " << type);
130 privList.append(const_cast<void*>(static_cast<const void *>(privilege.c_str())));
132 GList *privMapped = nullptr;
133 int ret = privilege_db_manager_get_mapped_privilege_list(version.c_str(), pkgType, privList.get(), &privMapped);
134 if (ret != PRIVILEGE_DB_MANAGER_ERR_NONE || !privMapped) {
135 ALOGE("Unable to get mapping of privilege " << privilege << "; err: <" << ret << ">");
139 GListWrap privMappedWrap(privMapped);
140 std::vector<std::string> privMappedVector;
141 for (GList *l = privMappedWrap.get(); l != NULL; l = l->next) {
142 std::string corePriv = static_cast<char*>(l->data);
143 ALOGD("Privilege mapps to " << corePriv);
144 privMappedVector.push_back(std::move(corePriv));
147 return privMappedVector;
150 bool isPrivacy(const Privilege &privilege) {
151 return privilege_info_is_privacy(privilege.c_str()) == 1;
154 std::string getPrivacyDisplayName(const Privacy &privacy) {
155 char *displayName = nullptr;
156 int ret = privilege_info_get_privacy_display(privacy.c_str(), &displayName);
157 if (ret != PRVMGR_ERR_NONE || !displayName) {
158 ALOGE("Unable to get privacy display name for <" << privacy << ">, err: <" << ret << ">");
161 std::unique_ptr<char, decltype(free)*> displaNamePtr(displayName, free);
162 return std::string(displayName);
165 std::vector<Privilege> getPrivacyPrivileges(const Privacy &privacy) {
166 GList *privilegeList = nullptr;
168 int ret = privilege_info_get_privilege_list_by_privacy(privacy.c_str(), &privilegeList);
169 if (ret != PRVMGR_ERR_NONE || !privilegeList) {
170 ALOGE("Unable to get privacy group list of privileges; err: <" << ret << ">");
174 GListWrap privList(privilegeList);
175 std::vector<Privilege> privVector;
176 for (GList *l = privList.get(); l != NULL; l = l->next) {
177 privVector.push_back(static_cast<char*>(l->data));
183 } /* namespace AskUser */