Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / src / security-manager-tests / common / app_install_helper.h
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 #pragma once
17
18 #include <fcntl.h>
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <sys/types.h>
23 #include <vector>
24 #include <unistd.h>
25
26 #include <security-manager-types.h>
27
28 struct AppInstallHelper {
29
30     using TypePathsMap = std::map<app_install_path_type, std::vector<std::string>>;
31     AppInstallHelper(const std::string &appNamePrefix,
32                      const std::string &pkgNamePrefix,
33                      bool isLocal,
34                      uid_t uid,
35                      std::string version = std::string())
36       : m_appName(appNamePrefix), m_pkgName(pkgNamePrefix), m_isLocal(isLocal), m_uidGid(uid), m_version(version),
37         m_installType(SM_APP_INSTALL_NONE), m_isHybrid(false), m_isInstallDirCreated(false), m_creatorPid(getpid())
38     {
39         setInstallPath();
40     }
41
42     AppInstallHelper(const std::string &appNamePrefix,
43                      const std::string &pkgNamePrefix,
44                      uid_t uid)
45       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, uid)
46     {}
47
48     AppInstallHelper(const std::string &namePrefix)
49       : AppInstallHelper(namePrefix, namePrefix, false, geteuid())
50     {}
51
52     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix)
53       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, geteuid())
54     {}
55
56     AppInstallHelper(const std::string &namePrefix, uid_t uid)
57       : AppInstallHelper(namePrefix, namePrefix, true, uid)
58     {}
59
60     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix, uid_t uid,
61                      const std::string &version)
62       : AppInstallHelper(appNamePrefix, pkgNamePrefix, true, uid, version)
63     {}
64
65     AppInstallHelper(const std::string &namePrefix, uid_t uid, const std::string &version)
66       : AppInstallHelper(namePrefix, namePrefix, true, uid, version)
67     {}
68
69     AppInstallHelper(const AppInstallHelper &other) = default;
70     AppInstallHelper& operator=(const AppInstallHelper &other) = delete;
71     AppInstallHelper(AppInstallHelper &&other);
72
73     // App info getters and setters
74     std::string getAppId() const;
75     std::string getPkgId() const;
76     int getUID() const;
77     int getGID() const;
78     void setVersion(const std::string &version);
79     std::string getVersion() const;
80     void setAuthor(const std::string &author);
81     std::string getAuthor() const;
82     void setInstallType(app_install_type type);
83     app_install_type getInstallType() const;
84     void setHybrid();
85     bool getIsHybrid() const;
86
87     // Path types creation and removal on file system
88     void createInstallDir();
89     void createTrustedDir(int i = 0);
90     void createPrivateDir(int i = 0);
91     void createPublicDir();
92     void createPrivateFile(int i = 0);
93     void createSharedRODir(int i = 0);
94     void createPrivateRODir(int i = 0);
95     void removePaths();
96
97     // Path getters
98     std::string getInstallDir() const;
99     std::string getTrustedDir(int i = 0) const;
100     std::string getPrivateDir(int i = 0) const;
101     std::string getPrivateRODir(int i = 0) const;
102     std::string getPublicDir() const;
103     std::string getPrivatePath(int i = 0) const;
104     std::string getSharedRODir(int i = 0) const;
105     const TypePathsMap& getDirsMap() const;
106     const TypePathsMap& getFilesMap() const;
107
108     // Privileges
109     void addPrivilege(const std::string &privilege);
110     void addPrivileges(const std::vector<std::string> &privileges);
111     std::vector<std::string> getPrivileges() const;
112
113     // Smack
114     std::string generateAppLabel() const;
115     std::string generatePkgLabel() const;
116     void revokeRules() const;
117     virtual ~AppInstallHelper() {
118         if (m_creatorPid == getpid())
119             removePaths();
120     }
121
122 protected:
123     void setInstallPath();
124     bool create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path);
125     std::string m_appName;
126     std::string m_pkgName;
127     bool m_isLocal;
128     int m_uidGid;
129     std::string m_version;
130     app_install_type m_installType;
131     bool m_isHybrid;
132     std::string m_installDir;
133     bool m_isInstallDirCreated;
134     TypePathsMap m_dirTypeMap;
135     TypePathsMap m_fileTypeMap;
136     std::vector<std::string> m_privileges;
137     std::string m_author;
138
139     pid_t m_creatorPid;
140 };