Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / access_control / privilege.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <string>
18 #include <map>
19
20 #include <pkgmgr-info.h>
21 #include <privilege_checker.h>
22
23 #include <types_internal.h>
24
25 #include "config_loader.h"
26 #include "privilege.h"
27
28 typedef std::map<std::string, std::string> string_map_t;
29
30 static string_map_t *privilege_map = NULL;
31
32 bool ctx::privilege_manager::init()
33 {
34         IF_FAIL_RETURN(privilege_map == NULL, true);
35
36         privilege_map = new(std::nothrow) string_map_t;
37
38         if (!ctx::access_config_loader::load()) {
39                 _E("Loading failed");
40                 delete privilege_map;
41                 return false;
42         }
43
44         return true;
45 }
46
47 void ctx::privilege_manager::release()
48 {
49         delete privilege_map;
50         privilege_map = NULL;
51 }
52
53 void ctx::privilege_manager::set(const char* subject, const char* priv)
54 {
55         (*privilege_map)[subject] = priv;
56 }
57
58 bool ctx::privilege_manager::is_allowed(const char* pkg_id, const char* subject)
59 {
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");
62
63         string_map_t::iterator it = privilege_map->find(subject);
64         if (it == privilege_map->end()) {
65                 // Non-Privileged Subject
66                 return true;
67         }
68
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);
75 }
76
77 std::string ctx::privilege_manager::get_pkg_id(const char* app_id)
78 {
79         std::string pkg_id;
80         IF_FAIL_RETURN_TAG(app_id, pkg_id, _E, "Null AppId");
81
82         int ret;
83         pkgmgrinfo_appinfo_h app_info;
84
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");
87
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");
93                 return pkg_id;
94         }
95
96         pkg_id = pkg_name;
97         pkgmgrinfo_appinfo_destroy_appinfo(app_info);
98         return pkg_id;
99 }