1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "wgt/step/security/step_check_extension_privileges.h"
7 #include <boost/scope_exit.hpp>
9 #include <common/privileges.h>
10 #include <common/certificate_validation.h>
11 #include <common/utils/glist_range.h>
12 #include <manifest_parser/values.h>
14 #include <pkgmgrinfo_basic.h>
17 #include <sys/utsname.h>
25 #include "wgt/utils/extension_config_parser.h"
29 const char kPluginsDirectory[] = "/res/wgt/plugin/";
30 const char kArchArmv7l[] = "armv7l";
31 const char kArchI586[] = "i586";
32 const char kArchDefault[] = "default";
39 common_installer::Step::Status StepCheckExtensionPrivileges::precheck() {
40 if (!context_->manifest_data.get()) {
41 LOG(ERROR) << "Manifest data is not set";
46 common_installer::Step::Status StepCheckExtensionPrivileges::process() {
47 std::string app_ext_config_pattern(GetExtensionPath());
49 manifest_x* m = context_->manifest_data.get();
50 std::set<std::string> current_privileges;
51 for (privilege_x* priv : GListRange<privilege_x*>(m->privileges)) {
52 if (strcmp(priv->type, common_installer::kWebPrivilegeType) == 0)
53 current_privileges.insert(priv->value);
56 std::set<std::string> xmlFiles;
59 glob(app_ext_config_pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
60 for (unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
61 xmlFiles.insert(glob_result.gl_pathv[i]);
63 globfree(&glob_result);
65 GList* privileges = nullptr;
66 BOOST_SCOPE_EXIT_ALL(privileges) {
67 g_list_free_full(privileges, &common_installer::FreePrivilegeX);
69 for (auto it = xmlFiles.begin(); it != xmlFiles.end(); ++it) {
70 LOG(DEBUG) << "start to parse extension xml : " << *it;
71 ExtensionConfigParser extensionParser(*it);
72 std::vector<std::string> list = extensionParser.GetExtensionPrivilegeList();
73 for (const std::string& priv : list) {
74 if (current_privileges.find(priv) == current_privileges.end()) {
75 privilege_x* privilege =
76 reinterpret_cast<privilege_x*>(calloc(1, sizeof(privilege_x)));
78 LOG(ERROR) << "Out of memory";
81 privilege->type = strdup(common_installer::kWebPrivilegeType);
82 if (!privilege->type) {
83 LOG(ERROR) << "Out of memory";
84 common_installer::FreePrivilegeX(privilege);
87 privilege->value = strdup(priv.c_str());
88 if (!privilege->value) {
89 LOG(ERROR) << "Out of memory";
90 common_installer::FreePrivilegeX(privilege);
93 privileges = g_list_append(privileges, privilege);
99 if (!CheckPrivilegeLevel(privileges)) {
100 LOG(DEBUG) << "Fail to validation of privilege";
101 return Status::ERROR;
103 m->privileges = g_list_concat(m->privileges, privileges);
104 privileges = nullptr;
109 std::string StepCheckExtensionPrivileges::GetExtensionPath() {
110 std::string app_ext_config_pattern(context_->GetPkgPath().string());
111 app_ext_config_pattern.append(kPluginsDirectory);
113 if (0 == uname(&u)) {
114 std::string machine = u.machine;
115 LOG(DEBUG) << "Machine archicture for user defined plugins: " << machine;
116 if (!machine.empty()) {
117 if (machine == kArchArmv7l) {
118 app_ext_config_pattern.append(kArchArmv7l);
119 } else if (machine == kArchI586) {
120 app_ext_config_pattern.append(kArchI586);
122 app_ext_config_pattern.append(kArchDefault);
125 LOG(ERROR) << "cannot get machine info";
126 app_ext_config_pattern.append(kArchDefault);
128 app_ext_config_pattern.append("/");
130 app_ext_config_pattern.append("*");
131 app_ext_config_pattern.append(".xml");
132 return app_ext_config_pattern;
135 bool StepCheckExtensionPrivileges::CheckPrivilegeLevel(
137 if (g_list_length(privileges) == 0)
140 std::string error_message;
141 if (!common_installer::ValidatePrivilegeLevel(
142 context_->privilege_level.get(),
144 context_->manifest_data.get()->api_version,
147 if (!error_message.empty()) {
148 LOG(ERROR) << "error_message: " << error_message;
155 } // namespace security