Fix in StepWgtPatchStorageDirectory
[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/extension_config_parser.h"
26
27 namespace {
28 const char kPluginsDirectory[] = "/res/wgt/plugin/";
29 const char kArchArmv7l[] = "armv7l";
30 const char kArchI586[] = "i586";
31 const char kArchDefault[] = "default";
32 }
33
34 namespace wgt {
35 namespace security {
36
37 common_installer::Step::Status StepCheckExtensionPrivileges::precheck() {
38   if (!context_->manifest_data.get()) {
39     LOG(ERROR) << "Manifest data is not set";
40     return Status::ERROR;
41   }
42   return Status::OK;
43 }
44 common_installer::Step::Status StepCheckExtensionPrivileges::process() {
45   std::string app_ext_config_pattern(GetExtensionPath());
46
47   manifest_x* m = context_->manifest_data.get();
48   std::set<std::string> current_privileges;
49   for (privilege_x* priv : GListRange<privilege_x*>(m->privileges)) {
50     if (strcmp(priv->type, common_installer::kWebPrivilegeType) == 0)
51       current_privileges.insert(priv->value);
52   }
53
54   std::set<std::string> xmlFiles;
55   {
56     glob_t glob_result;
57     glob(app_ext_config_pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
58     for (unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
59       xmlFiles.insert(glob_result.gl_pathv[i]);
60     }
61   }
62   GList* privileges = nullptr;
63   BOOST_SCOPE_EXIT_ALL(&) {
64     g_list_free_full(privileges, &common_installer::FreePrivilegeX);
65   };
66   for (auto it = xmlFiles.begin(); it != xmlFiles.end(); ++it) {
67     LOG(DEBUG) << "start to parse extension xml : " << *it;
68     ExtensionConfigParser extensionParser(*it);
69     std::vector<std::string> list = extensionParser.GetExtensionPrivilegeList();
70     for (const std::string& priv : list) {
71       if (current_privileges.find(priv) == current_privileges.end()) {
72         privilege_x* privilege =
73             reinterpret_cast<privilege_x*>(calloc(1, sizeof(privilege_x)));
74         privilege->type = strdup(common_installer::kWebPrivilegeType);
75         privilege->value = strdup(priv.c_str());
76         privileges = g_list_append(privileges, privilege);
77       }
78     }
79   }
80
81   if (privileges) {
82     if (!CheckPrivilegeLevel(privileges)) {
83       LOG(DEBUG) << "Fail to validation of privilege";
84       return Status::ERROR;
85     }
86     m->privileges = g_list_concat(m->privileges, privileges);
87     privileges = nullptr;
88   }
89   return Status::OK;
90 }
91
92 std::string StepCheckExtensionPrivileges::GetExtensionPath() {
93   std::string app_ext_config_pattern(context_->pkg_path.get().string());
94   app_ext_config_pattern.append(kPluginsDirectory);
95   struct utsname u;
96   if (0 == uname(&u)) {
97     std::string machine = u.machine;
98     LOG(DEBUG) << "Machine archicture for user defined plugins: " << machine;
99     if (!machine.empty()) {
100       if (machine == kArchArmv7l) {
101         app_ext_config_pattern.append(kArchArmv7l);
102       } else if (machine == kArchI586) {
103         app_ext_config_pattern.append(kArchI586);
104       } else {
105         app_ext_config_pattern.append(kArchDefault);
106       }
107     } else {
108       LOG(ERROR) << "cannot get machine info";
109       app_ext_config_pattern.append(kArchDefault);
110     }
111     app_ext_config_pattern.append("/");
112   }
113   app_ext_config_pattern.append("*");
114   app_ext_config_pattern.append(".xml");
115   return app_ext_config_pattern;
116 }
117
118 bool StepCheckExtensionPrivileges::CheckPrivilegeLevel(
119     GList* privileges) {
120   if (g_list_length(privileges) == 0)
121     return true;
122
123   std::string error_message;
124   if (!common_installer::ValidatePrivilegeLevel(
125          context_->privilege_level.get(),
126          context_->uid.get(),
127          context_->manifest_data.get()->api_version,
128          privileges,
129          &error_message)) {
130     if (!error_message.empty()) {
131       LOG(ERROR) << "error_message: " << error_message;
132     }
133     return false;
134   }
135   return true;
136 }
137
138 }  // namespace security
139 }  // namespace wgt