2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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>
21 #include <privilege_checker.h>
23 #include <types_internal.h>
25 #include "config_loader.h"
26 #include "privilege.h"
28 typedef std::map<std::string, std::string> string_map_t;
30 static string_map_t *privilege_map = NULL;
32 bool ctx::privilege_manager::init()
34 IF_FAIL_RETURN(privilege_map == NULL, true);
36 privilege_map = new(std::nothrow) string_map_t;
38 if (!ctx::access_config_loader::load()) {
47 void ctx::privilege_manager::release()
53 void ctx::privilege_manager::set(const char* subject, const char* priv)
55 (*privilege_map)[subject] = priv;
58 bool ctx::privilege_manager::is_allowed(const char* pkg_id, const char* subject)
60 IF_FAIL_RETURN_TAG(pkg_id && subject, true, _E, "Invalid parameter");
61 IF_FAIL_RETURN_TAG(pkg_id[0]!='\0' && subject[0]!='\0', true, _E, "Invalid parameter");
63 string_map_t::iterator it = privilege_map->find(subject);
64 if (it == privilege_map->end()) {
65 // Non-Privileged Subject
69 _D("PkgId: %s, Priv: %s", pkg_id, (it->second).c_str());
70 std::string priv = "http://tizen.org/privilege/";
71 priv += (it->second).c_str();
72 int ret = privilege_checker_check_package_privilege(pkg_id, priv.c_str());
73 _D("Privilege Check Result: %#x", ret);
74 return (ret == PRIV_CHECKER_ERR_NONE);
77 std::string ctx::privilege_manager::get_pkg_id(const char* app_id)
80 IF_FAIL_RETURN_TAG(app_id, pkg_id, _E, "Null AppId");
83 pkgmgrinfo_appinfo_h app_info;
85 ret = pkgmgrinfo_appinfo_get_appinfo(app_id, &app_info);
86 IF_FAIL_RETURN_TAG(ret == PMINFO_R_OK, pkg_id, _E, "Failed to get app_info");
88 char *pkg_name = NULL;
89 ret = pkgmgrinfo_appinfo_get_pkgname(app_info, &pkg_name);
90 if (ret != PMINFO_R_OK || pkg_name == NULL) {
91 pkgmgrinfo_appinfo_destroy_appinfo(app_info);
92 _E("Failed to get package name");
97 pkgmgrinfo_appinfo_destroy_appinfo(app_info);