796022dfc3599a159f4c97bdf9727011cb6098bf
[platform/core/appfw/wgt-backend.git] / src / wgt / step / security / step_check_extension_privileges.cc
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.
4
5 #include "wgt/step/security/step_check_extension_privileges.h"
6
7 #include <boost/scope_exit.hpp>
8
9 #include <common/privileges.h>
10 #include <common/certificate_validation.h>
11 #include <common/utils/glist_range.h>
12 #include <manifest_parser/values.h>
13
14 #include <pkgmgrinfo_basic.h>
15 #include <glib.h>
16 #include <glob.h>
17 #include <sys/utsname.h>
18
19 #include <set>
20 #include <vector>
21 #include <cstdlib>
22 #include <string>
23 #include <memory>
24
25 #include "wgt/utils/extension_config_parser.h"
26
27 namespace {
28
29 const char kPluginsDirectory[] = "/res/wgt/plugin/";
30 const char kArchArmv7l[] = "armv7l";
31 const char kArchI586[] = "i586";
32 const char kArchDefault[] = "default";
33
34 }  // namespace
35
36 namespace wgt {
37 namespace security {
38
39 common_installer::Step::Status StepCheckExtensionPrivileges::precheck() {
40   if (!context_->manifest_data.get()) {
41     LOG(ERROR) << "Manifest data is not set";
42     return Status::ERROR;
43   }
44   return Status::OK;
45 }
46 common_installer::Step::Status StepCheckExtensionPrivileges::process() {
47   std::string app_ext_config_pattern(GetExtensionPath());
48
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);
54   }
55
56   std::set<std::string> xmlFiles;
57   {
58     glob_t glob_result;
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]);
62     }
63     globfree(&glob_result);
64   }
65   GList* privileges = nullptr;
66   BOOST_SCOPE_EXIT_ALL(privileges) {
67     g_list_free_full(privileges, &common_installer::FreePrivilegeX);
68   };
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)));
77         if (!privilege) {
78           LOG(ERROR) << "Out of memory";
79           return Status::ERROR;
80         }
81         privilege->type = strdup(common_installer::kWebPrivilegeType);
82         if (!privilege->type) {
83           LOG(ERROR) << "Out of memory";
84           common_installer::FreePrivilegeX(privilege);
85           return Status::ERROR;
86         }
87         privilege->value = strdup(priv.c_str());
88         if (!privilege->value) {
89           LOG(ERROR) << "Out of memory";
90           common_installer::FreePrivilegeX(privilege);
91           return Status::ERROR;
92         }
93         privileges = g_list_append(privileges, privilege);
94       }
95     }
96   }
97
98   if (privileges) {
99     if (!CheckPrivilegeLevel(privileges)) {
100       LOG(DEBUG) << "Fail to validation of privilege";
101       return Status::ERROR;
102     }
103     m->privileges = g_list_concat(m->privileges, privileges);
104     privileges = nullptr;
105   }
106   return Status::OK;
107 }
108
109 std::string StepCheckExtensionPrivileges::GetExtensionPath() {
110   std::string app_ext_config_pattern(context_->GetPkgPath().string());
111   app_ext_config_pattern.append(kPluginsDirectory);
112   struct utsname u;
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);
121       } else {
122         app_ext_config_pattern.append(kArchDefault);
123       }
124     } else {
125       LOG(ERROR) << "cannot get machine info";
126       app_ext_config_pattern.append(kArchDefault);
127     }
128     app_ext_config_pattern.append("/");
129   }
130   app_ext_config_pattern.append("*");
131   app_ext_config_pattern.append(".xml");
132   return app_ext_config_pattern;
133 }
134
135 bool StepCheckExtensionPrivileges::CheckPrivilegeLevel(
136     GList* privileges) {
137   if (g_list_length(privileges) == 0)
138     return true;
139
140   std::string error_message;
141   if (!common_installer::ValidatePrivilegeLevel(
142          context_->privilege_level.get(),
143          context_->uid.get(),
144          context_->manifest_data.get()->api_version,
145          privileges,
146          &error_message)) {
147     if (!error_message.empty()) {
148       LOG(ERROR) << "error_message: " << error_message;
149     }
150     return false;
151   }
152   return true;
153 }
154
155 }  // namespace security
156 }  // namespace wgt