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