Support more types of application paths
[platform/core/test/security-tests.git] / src / common / app_install_helper.h
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 #pragma once
17
18 #include <fcntl.h>
19 #include <functional>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include <security-manager-types.h>
29 #include <app_def_privilege.h>
30
31 struct AppInstallHelper {
32
33     using TypePathsMap = std::map<app_install_path_type, std::vector<std::string>>;
34     using TypePathMap = std::map<app_install_path_type, std::string>;
35
36     AppInstallHelper(const std::string &appNamePrefix,
37                      const std::string &pkgNamePrefix,
38                      bool isLocal,
39                      uid_t uid,
40                      std::string version = std::string())
41       : m_appName(appNamePrefix), m_pkgName(pkgNamePrefix), m_isLocal(isLocal), m_uidGid(uid), m_version(version),
42         m_installType(SM_APP_INSTALL_NONE), m_isHybrid(false), m_creatorPid(getpid())
43     {}
44
45     AppInstallHelper(const std::string &appNamePrefix,
46                      const std::string &pkgNamePrefix,
47                      uid_t uid)
48       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, uid)
49     {}
50
51     AppInstallHelper(const std::string &namePrefix)
52       : AppInstallHelper(namePrefix, namePrefix, false, geteuid())
53     {}
54
55     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix)
56       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, geteuid())
57     {}
58
59     AppInstallHelper(const std::string &namePrefix, uid_t uid)
60       : AppInstallHelper(namePrefix, namePrefix, true, uid)
61     {}
62
63     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix, uid_t uid,
64                      const std::string &version)
65       : AppInstallHelper(appNamePrefix, pkgNamePrefix, true, uid, version)
66     {}
67
68     AppInstallHelper(const std::string &namePrefix, uid_t uid, const std::string &version)
69       : AppInstallHelper(namePrefix, namePrefix, true, uid, version)
70     {}
71
72     AppInstallHelper(const AppInstallHelper &other) = default;
73     AppInstallHelper& operator=(const AppInstallHelper &other) = delete;
74     AppInstallHelper(AppInstallHelper &&other);
75
76     // App info getters and setters
77     std::string getAppId() const;
78     std::string getPkgId() const;
79     int getUID() const;
80     int getGID() const;
81     void setVersion(const std::string &version);
82     std::string getVersion() const;
83     void setAuthor(const std::string &author);
84     std::string getAuthor() const;
85     void setInstallType(app_install_type type);
86     app_install_type getInstallType() const;
87     void setHybrid();
88     bool getIsHybrid() const;
89
90     enum RootType {
91         BASE,
92         SKEL,
93         EXTENDED
94     };
95
96     enum PathType {
97         FILE,
98         DIR
99     };
100
101     // Path types creation and removal on file system
102     void createPath(app_install_path_type smType, PathType pType, int i = 0, RootType rType = RootType::BASE);
103
104     // below methods are deprecated
105     void createTrustedDir(int i = 0, RootType type = RootType::BASE);
106     void createPrivateDir(int i = 0, RootType type = RootType::BASE);
107     void createPublicDir(RootType type = RootType::BASE);
108     void createPrivateFile(int i = 0, RootType type = RootType::BASE);
109     void createSharedRODir(int i = 0, RootType type = RootType::BASE);
110     void createPrivateRODir(int i = 0, RootType type = RootType::BASE);
111     void removePaths();
112
113     // Path getters
114     std::string getPath(app_install_path_type smType, PathType pType, int i = 0, RootType rType = RootType::BASE) const;
115
116     // below methods are deprecated
117     std::string getInstallDir(RootType type) const;
118     std::string getTrustedDir(int i = 0, RootType type = RootType::BASE) const;
119     std::string getPrivateDir(int i = 0, RootType type = RootType::BASE) const;
120     std::string getPrivateRODir(int i = 0, RootType type = RootType::BASE) const;
121     std::string getPublicDir(RootType type = RootType::BASE) const;
122     std::string getPrivatePath(int i = 0, RootType type = RootType::BASE) const;
123     std::string getSharedRODir(int i = 0, RootType type = RootType::BASE) const;
124     const TypePathsMap& getDirsMap() const;
125     const TypePathsMap& getFilesMap() const;
126
127     // Privileges
128     std::vector<std::string> getPrivilegesNames() const;             // deprecated
129     void addPrivileges(const std::vector<std::string> &privileges);  // deprecated
130     void addPrivilege(Privilege privilege);
131     void addPrivileges(const PrivilegeVector &privileges);
132     const PrivilegeVector& getPrivileges() const;
133
134     void addAppDefinedPrivilege(Privilege privilege);
135     const PrivilegeVector& getAppDefinedPrivileges() const;
136
137     // Smack
138     std::string generateAppLabel() const;
139     std::string generatePkgLabel() const;
140     void revokeRules() const;
141     virtual ~AppInstallHelper() {
142         if (m_creatorPid == getpid())
143             removePaths();
144     }
145
146 protected:
147     struct RootInfo {
148         RootInfo() : isCreated(false) {}
149         std::string path;
150         int isCreated;
151     };
152     using RootTypeInfoMap = std::map<RootType, RootInfo>;
153
154     void setInstallPath(RootType type) const;
155
156     bool createFile(app_install_path_type smType, const std::string &path);
157     bool createDir(app_install_path_type smType, const std::string &path, bool isBasePath = false);
158     void createInstallDir(RootType type);
159
160     std::string m_appName;
161     std::string m_pkgName;
162     bool m_isLocal;
163     int m_uidGid;
164     std::string m_version;
165     app_install_type m_installType;
166     bool m_isHybrid;
167
168     mutable RootTypeInfoMap m_rootPaths;
169     TypePathsMap m_dirTypeMap;
170     TypePathsMap m_fileTypeMap;
171
172     PrivilegeVector m_privileges;
173     PrivilegeVector m_appDefinedPrivileges;
174
175     std::string m_author;
176
177     pid_t m_creatorPid;
178 };