2 * Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
22 #include <sys/smack.h>
26 #include <security-manager-types.h>
28 #include <dpl/test/test_runner.h>
29 #include <tzplatform.h>
30 #include <label_generator.h>
31 #include <tests_common.h>
32 #include <tzplatform.h>
34 #include "app_install_helper.h"
37 std::string genSkelPath() {
38 static std::string skelPkgDir;
39 if (!skelPkgDir.empty())
41 std::string app = TzPlatformConfig::getEnv(TZ_USER_APP);
42 std::string home = TzPlatformConfig::getEnv(TZ_USER_HOME);
43 std::string skelDir = "/etc/skel";
45 skelPkgDir.assign(app);
46 skelPkgDir.replace(0, home.length(), skelDir);
51 const AppInstallHelper::TypePathMap typeToPath = {
52 {SECURITY_MANAGER_PATH_RW, "app_rw"},
53 {SECURITY_MANAGER_PATH_RO, "app_ro"},
54 {SECURITY_MANAGER_PATH_PUBLIC_RO, "public_ro"},
55 {SECURITY_MANAGER_PATH_TRUSTED_RW, "trusted_rw"},
56 {SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO, "shared_ro"}
60 AppInstallHelper::AppInstallHelper(AppInstallHelper &&other)
61 : m_appName(std::move(other.m_appName)), m_pkgName(std::move(other.m_pkgName)),
62 m_isLocal(other.m_isLocal), m_uidGid(other.m_uidGid),
63 m_version(std::move(other.m_version)), m_installType(other.m_installType),
64 m_isHybrid(other.m_isHybrid),
65 m_rootPaths(std::move(other.m_rootPaths)),
66 m_dirTypeMap(std::move(other.m_dirTypeMap)),
67 m_fileTypeMap(std::move(other.m_fileTypeMap)),
68 m_privileges(std::move(other.m_privileges)),
69 m_appDefinedPrivileges(std::move(other.m_appDefinedPrivileges)),
70 m_author(std::move(other.m_author)),
71 m_creatorPid(other.m_creatorPid)
73 other.m_creatorPid = -1;
76 void AppInstallHelper::setInstallPath(RootType type) const {
77 RootInfo &info = m_rootPaths[type];
78 if (!info.path.empty())
84 info.path = TzPlatformConfig::appDirPath(getUID()) + getPkgId();
86 info.path = TzPlatformConfig::globalAppDir() + "/" + getPkgId();
88 case RootType::EXTENDED:
90 info.path = TzPlatformConfig::extendedSdUserDir(getUID()) + "/" + getPkgId();
92 info.path = TzPlatformConfig::extendedSdDir() + "/" + getPkgId();
95 info.path = genSkelPath() + "/" + getPkgId();
100 std::string AppInstallHelper::getInstallDir(RootType type) const {
101 setInstallPath(type);
102 return m_rootPaths[type].path;
105 std::string AppInstallHelper::getPath(app_install_path_type smType, PathType pType, int i, RootType rType) const {
109 path = getInstallDir(rType) + "/" + typeToPath.at(smType) + "_dir" + std::to_string(i);
112 // put files in the directory of the same type
113 path = getInstallDir(rType) + "/" + typeToPath.at(smType) + "_dir0/" + typeToPath.at(smType) + std::to_string(i);
119 std::string AppInstallHelper::getTrustedDir(int i, RootType type) const {
120 return getPath(SECURITY_MANAGER_PATH_TRUSTED_RW, PathType::DIR, i , type);
123 std::string AppInstallHelper::getPrivateDir(int i, RootType type) const {
124 return getPath(SECURITY_MANAGER_PATH_RW, PathType::DIR, i, type);
127 std::string AppInstallHelper::getPrivateRODir(int i, RootType type) const {
128 return getPath(SECURITY_MANAGER_PATH_RO, PathType::DIR, i, type);
131 std::string AppInstallHelper::getPublicDir(RootType type) const {
132 return getPath(SECURITY_MANAGER_PATH_PUBLIC_RO, PathType::DIR, 0, type);
135 std::string AppInstallHelper::getPrivatePath(int i, RootType type) const {
136 return getPath(SECURITY_MANAGER_PATH_RW, PathType::FILE, i, type);
139 std::string AppInstallHelper::getSharedRODir(int i, RootType type) const {
140 return getPath(SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO, PathType::DIR, i, type);
143 std::string AppInstallHelper::getAppId() const {
144 return m_appName + "_app_id";
147 std::string AppInstallHelper::getPkgId() const {
148 return m_pkgName + "_pkg_id";
151 void AppInstallHelper::setVersion(const std::string &version) {
155 std::string AppInstallHelper::getVersion() const {
159 int AppInstallHelper::getUID() const {
163 int AppInstallHelper::getGID() const {
167 bool AppInstallHelper::createFile(app_install_path_type smType, const std::string &path) {
168 if (creat(path.c_str(), 0751) == 0) {
169 // Local paths need user change
170 m_fileTypeMap[smType].push_back(std::move(path));
171 if (!m_isLocal || chown(path.c_str(), m_uidGid, m_uidGid) == 0)
177 bool AppInstallHelper::createDir(app_install_path_type smType, const std::string &path, bool isBasePath) {
178 mktreeSafe(path, 0777);
179 // Dont pass base pkg dirs to SM, because transmute will be forced on RO subdirs
181 m_dirTypeMap[smType].push_back(std::move(path));
182 if (!m_isLocal || chown(path.c_str(), m_uidGid, m_uidGid) == 0)
188 void AppInstallHelper::createInstallDir(RootType type) {
189 setInstallPath(type);
190 RootInfo &info = m_rootPaths[type];
193 createDir(SECURITY_MANAGER_PATH_PUBLIC_RO, info.path, true);
194 info.isCreated = true;
197 void AppInstallHelper::createPath(app_install_path_type smType, PathType pType, int i, RootType rType) {
198 createInstallDir(rType);
199 std::string path = getPath(smType, pType, i, rType);
202 createDir(smType, path);
205 createPath(smType, PathType::DIR, i, rType);
206 createFile(smType, path);
211 void AppInstallHelper::createTrustedDir(int i, RootType type) {
212 createPath(SECURITY_MANAGER_PATH_TRUSTED_RW, PathType::DIR, i, type);
215 void AppInstallHelper::createPrivateDir(int i, RootType type) {
216 createPath(SECURITY_MANAGER_PATH_RW, PathType::DIR, i, type);
219 void AppInstallHelper::createPublicDir(RootType type) {
220 createPath(SECURITY_MANAGER_PATH_PUBLIC_RO, PathType::DIR, 0, type);
223 void AppInstallHelper::createPrivateFile(int i, RootType type) {
224 createPath(SECURITY_MANAGER_PATH_RW, PathType::FILE, i, type);
227 void AppInstallHelper::createSharedRODir(int i, RootType type) {
228 createPath(SECURITY_MANAGER_PATH_OWNER_RW_OTHER_RO, PathType::DIR, i, type);
231 void AppInstallHelper::createPrivateRODir(int i, RootType type) {
232 createPath(SECURITY_MANAGER_PATH_RO, PathType::DIR, i, type);
235 void AppInstallHelper::setHybrid() {
239 bool AppInstallHelper::getIsHybrid() const {
243 void AppInstallHelper::addPrivileges(const std::vector<std::string> &privileges) {
244 for (auto &p : privileges) {
245 addPrivilege(Privilege(p));
249 std::vector<std::string> AppInstallHelper::getPrivilegesNames() const {
250 std::vector<std::string> privileges;
251 for (auto &p : m_privileges) {
252 privileges.push_back(p.getName());
257 void AppInstallHelper::addPrivilege(Privilege privilege) {
258 m_privileges.push_back(std::move(privilege));
261 void AppInstallHelper::addPrivileges(const PrivilegeVector &privileges) {
262 std::copy(privileges.begin(), privileges.end(), std::back_inserter(m_privileges));
265 const PrivilegeVector& AppInstallHelper::getPrivileges() const {
269 void AppInstallHelper::addAppDefinedPrivilege(Privilege privilege) {
270 m_appDefinedPrivileges.push_back(std::move(privilege));
273 const PrivilegeVector& AppInstallHelper::getAppDefinedPrivileges() const {
274 return m_appDefinedPrivileges;
277 void AppInstallHelper::revokeRules() const {
279 0 == smack_revoke_subject(generateAppLabel().c_str()),
280 "Revoking smack subject failed");
283 std::string AppInstallHelper::generateAppLabel() const {
284 return generateProcessLabel(getAppId(), getPkgId(), getIsHybrid());
287 std::string AppInstallHelper::generatePkgLabel() const {
288 return generatePathRWLabel(getPkgId());
291 const AppInstallHelper::TypePathsMap& AppInstallHelper::getDirsMap() const {
295 const AppInstallHelper::TypePathsMap& AppInstallHelper::getFilesMap() const {
296 return m_fileTypeMap;
299 void AppInstallHelper::removePaths() {
300 for (const auto &oneTypePaths : m_dirTypeMap)
301 for (const auto& path : oneTypePaths.second)
304 m_dirTypeMap.clear();
306 for (const auto &oneTypePaths : m_fileTypeMap)
307 for (const auto& path : oneTypePaths.second)
308 unlink(path.c_str());
310 m_fileTypeMap.clear();
312 for (auto& rootInfo : m_rootPaths) {
313 if (rootInfo.second.isCreated)
314 rmdir(rootInfo.second.path.c_str());
315 rootInfo.second.isCreated = false;
319 void AppInstallHelper::setAuthor(const std::string &author) {
322 std::string AppInstallHelper::getAuthor() const {
326 void AppInstallHelper::setInstallType(app_install_type type) {
327 m_installType = type;
329 app_install_type AppInstallHelper::getInstallType() const {
330 return m_installType;