Move label generation to global commons
[platform/core/test/security-tests.git] / src / common / app_install_helper.cpp
1 /*
2  * Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
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 <fcntl.h>
18 #include <map>
19 #include <string>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/smack.h>
23 #include <unistd.h>
24 #include <utility>
25
26 #include <security-manager-types.h>
27
28 #include <dpl/test/test_runner.h>
29 #include <tzplatform.h>
30 #include <label_generator.h>
31
32 #include "app_install_helper.h"
33
34 AppInstallHelper::AppInstallHelper(AppInstallHelper &&other)
35     : m_appName(std::move(other.m_appName)), m_pkgName(std::move(other.m_pkgName)),
36       m_isLocal(other.m_isLocal), m_uidGid(other.m_uidGid),
37       m_version(std::move(other.m_version)), m_installType(other.m_installType),
38       m_isHybrid(other.m_isHybrid), m_installDir(std::move(other.m_installDir)),
39       m_dirTypeMap(std::move(other.m_dirTypeMap)),
40       m_fileTypeMap(std::move(other.m_fileTypeMap)),
41       m_privileges(std::move(other.m_privileges)),
42       m_appDefinedPrivileges(std::move(other.m_appDefinedPrivileges)),
43       m_author(std::move(other.m_author)),
44       m_creatorPid(other.m_creatorPid)
45 {
46     other.m_creatorPid = -1;
47 }
48
49 std::string AppInstallHelper::getInstallDir() const {
50     return m_installDir + getPkgId();
51 }
52
53 std::string AppInstallHelper::getTrustedDir(int i) const {
54     return getInstallDir() + "/trustedDir" + std::to_string(i);
55 }
56
57 std::string AppInstallHelper::getPrivateDir(int i) const {
58     return getInstallDir() + "/app_dir" + std::to_string(i) +"/";
59 }
60
61 std::string AppInstallHelper::getPrivateRODir(int i) const {
62     return getInstallDir() + "/app_dir_ro" + std::to_string(i) +"/";
63 }
64
65 std::string AppInstallHelper::getPublicDir() const {
66     return getInstallDir() + "/app_public_ro/";
67 }
68
69 std::string AppInstallHelper::getPrivatePath(int i) const {
70     return getPrivateDir() + "shareme" + std::to_string(i);
71 }
72
73 std::string AppInstallHelper::getSharedRODir(int i) const {
74     return getInstallDir() + "/app_dir_rw_others_ro" + std::to_string(i) +"/";
75 }
76
77 std::string AppInstallHelper::getAppId() const {
78     return m_appName + "_app_id";
79 }
80
81 std::string AppInstallHelper::getPkgId() const {
82     return m_pkgName + "_pkg_id";
83 }
84
85 void AppInstallHelper::setVersion(const std::string &version) {
86     m_version = version;
87 }
88
89 std::string AppInstallHelper::getVersion() const {
90     return m_version;
91 }
92
93 int AppInstallHelper::getUID() const {
94     return m_uidGid;
95 }
96
97 int AppInstallHelper::getGID() const {
98     return m_uidGid;
99 }
100
101 void AppInstallHelper::createInstallDir() {
102     create(mkdir, getInstallDir());
103     m_isInstallDirCreated = true;
104 }
105
106 void AppInstallHelper::createTrustedDir(int i) {
107     if (create(mkdir, getTrustedDir(i)))
108         m_dirTypeMap[SECURITY_MANAGER_PATH_TRUSTED_RW].emplace_back(getTrustedDir(i));
109 }
110
111 void AppInstallHelper::createPrivateDir(int i) {
112     if (create(mkdir, getPrivateDir(i)))
113         m_dirTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getPrivateDir(i));
114 }
115
116 void AppInstallHelper::createPublicDir() {
117     if (mkdir(getPublicDir().c_str(), 0777) == 0) {
118         m_dirTypeMap[SECURITY_MANAGER_PATH_PUBLIC_RO].emplace_back(getPublicDir());
119     }
120 }
121
122 void AppInstallHelper::createPrivateFile(int i) {
123     // This is intentional, let all private file be in one directory
124     createPrivateDir();
125     if (create(creat, getPrivatePath(i)))
126         m_fileTypeMap[SECURITY_MANAGER_PATH_RW].emplace_back(getPrivatePath(i));
127 }
128
129 void AppInstallHelper::createSharedRODir(int i) {
130     if (create(mkdir, getSharedRODir(i)))
131         m_dirTypeMap[SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO].emplace_back(getSharedRODir(i));
132 }
133
134 void AppInstallHelper::createPrivateRODir(int i) {
135     if (create(mkdir, getPrivateRODir(i)))
136         m_dirTypeMap[SECURITY_MANAGER_PATH_RO].emplace_back(getPrivateRODir(i));
137 }
138
139 void AppInstallHelper::setHybrid() {
140     m_isHybrid = true;
141 }
142
143 bool AppInstallHelper::getIsHybrid() const {
144     return m_isHybrid;
145 }
146
147 void AppInstallHelper::addPrivileges(const std::vector<std::string> &privileges) {
148     for (auto &p : privileges) {
149         addPrivilege(Privilege(p));
150     }
151 }
152
153 std::vector<std::string> AppInstallHelper::getPrivilegesNames() const {
154     std::vector<std::string> privileges;
155     for (auto &p : m_privileges) {
156         privileges.push_back(p.getName());
157     }
158     return privileges;
159 }
160
161 void AppInstallHelper::addPrivilege(Privilege privilege) {
162     m_privileges.push_back(std::move(privilege));
163 }
164
165 void AppInstallHelper::addPrivileges(const PrivilegeVector &privileges) {
166     std::copy(privileges.begin(), privileges.end(), std::back_inserter(m_privileges));
167 }
168
169 const PrivilegeVector& AppInstallHelper::getPrivileges() const {
170     return m_privileges;
171 }
172
173 void AppInstallHelper::addAppDefinedPrivilege(Privilege privilege) {
174     m_appDefinedPrivileges.push_back(std::move(privilege));
175 }
176
177 const PrivilegeVector& AppInstallHelper::getAppDefinedPrivileges() const {
178     return m_appDefinedPrivileges;
179 }
180
181 void AppInstallHelper::revokeRules() const {
182     RUNNER_ASSERT_MSG(
183         0 == smack_revoke_subject(generateAppLabel().c_str()),
184         "Revoking smack subject failed");
185 }
186
187 std::string AppInstallHelper::generateAppLabel() const {
188     return generateProcessLabel(getAppId(), getPkgId(), getIsHybrid());
189 }
190
191 std::string AppInstallHelper::generatePkgLabel() const {
192     return generatePathRWLabel(getPkgId());
193 }
194
195 const AppInstallHelper::TypePathsMap& AppInstallHelper::getDirsMap() const {
196     return m_dirTypeMap;
197 }
198
199 const AppInstallHelper::TypePathsMap& AppInstallHelper::getFilesMap() const {
200     return m_fileTypeMap;
201 }
202
203 void AppInstallHelper::removePaths() {
204     for (const auto &oneTypePaths : m_dirTypeMap)
205             for (const auto& path : oneTypePaths.second)
206                 rmdir(path.c_str());
207
208     m_dirTypeMap.clear();
209
210     for (const auto &oneTypePaths : m_fileTypeMap)
211             for (const auto& path : oneTypePaths.second)
212                 unlink(path.c_str());
213
214     m_fileTypeMap.clear();
215
216     rmdir(getInstallDir().c_str());
217     m_isInstallDirCreated = false;
218 }
219
220 void AppInstallHelper::setInstallPath() {
221     if (m_isLocal)
222         m_installDir = TzPlatformConfig::appDirPath(getUID());
223     else
224         m_installDir = TzPlatformConfig::globalAppDir() + "/";
225 }
226
227 bool AppInstallHelper::create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path) {
228     if (!m_isInstallDirCreated && path != getInstallDir())
229         createInstallDir();
230     if (creatFun(path.c_str(), 0751) == 0) {
231        // Local paths need user change
232        if (!m_isLocal || chown(path.c_str(), m_uidGid, m_uidGid) == 0)
233                 return true;
234        }
235     return false;
236 }
237
238 void AppInstallHelper::setAuthor(const std::string &author) {
239     m_author = author;
240 }
241 std::string AppInstallHelper::getAuthor() const {
242     return m_author;
243 }
244
245 void AppInstallHelper::setInstallType(app_install_type type) {
246     m_installType = type;
247 }
248 app_install_type AppInstallHelper::getInstallType() const {
249     return m_installType;
250 }
251