04a120035dfdfe3dcea7fe92ebf399ab9c166301
[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                      bool isHybrid = false)
37       : m_appName(appNamePrefix), m_pkgName(pkgNamePrefix), m_isLocal(isLocal), m_uidGid(uid), m_version(version),
38         m_installType(SM_APP_INSTALL_NONE), m_isHybrid(isHybrid), m_creatorPid(getpid())
39     {
40         setInstallPath();
41     }
42
43     AppInstallHelper(const std::string &appNamePrefix,
44                      const std::string &pkgNamePrefix,
45                      uid_t uid)
46       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, uid)
47     {}
48
49     AppInstallHelper(const std::string &namePrefix)
50       : AppInstallHelper(namePrefix, namePrefix, false, geteuid())
51     {}
52
53     AppInstallHelper(const std::string &appNamePrefix, const std::string &pkgNamePrefix)
54       : AppInstallHelper(appNamePrefix, pkgNamePrefix, false, geteuid())
55     {}
56
57     AppInstallHelper(const std::string &namePrefix, uid_t uid)
58       : AppInstallHelper(namePrefix, namePrefix, true, uid)
59     {}
60
61     // App info getters and setters
62     std::string getAppId() const;
63     std::string getPkgId() const;
64     int getUID() const;
65     int getGID() const;
66     void setVersion(const std::string &version);
67     std::string getVersion() const;
68     void setAuthor(const std::string &author);
69     std::string getAuthor() const;
70     void setInstallType(app_install_type type);
71     app_install_type getInstallType();
72     bool getIsHybrid() const;
73
74     // Path types creation and removal on file system
75     void createInstallDir();
76     void createTrustedDir(int i = 0);
77     void createPrivateDir();
78     void createPublicDir();
79     void createSharedFile(int i = 0);
80     void createSharedRODir();
81     void createPrivateRODir();
82     void removePaths();
83
84     // Path getters
85     std::string getInstallDir() const;
86     std::string getTrustedDir(int i = 0) const;
87     std::string getPrivateDir() const;
88     std::string getPrivateRODir() const;
89     std::string getPublicDir() const;
90     std::string getSharedPath(int i = 0) const;
91     std::string getSharedRODir() const;
92     const TypePathsMap& getDirsMap() const;
93     const TypePathsMap& getFilesMap() const;
94
95     // Privileges
96     void addPrivilege(const std::string &privilege);
97     void addPrivileges(const std::vector<std::string> &privileges);
98     std::vector<std::string> getPrivileges() const;
99
100     // Smack
101     std::string generateAppLabel() const;
102     std::string generatePkgLabel() const;
103     void revokeRules() const;
104     virtual ~AppInstallHelper() {
105         if (m_creatorPid == getpid())
106             removePaths();
107     }
108
109 protected:
110     void setInstallPath();
111     bool create(std::function<int(const char*, mode_t)> &&creatFun, const std::string &path);
112     std::string m_appName;
113     std::string m_pkgName;
114     bool m_isLocal;
115     int m_uidGid;
116     std::string m_version;
117     app_install_type m_installType;
118     bool m_isHybrid;
119     std::string m_installDir;
120     TypePathsMap m_dirTypeMap;
121     TypePathsMap m_fileTypeMap;
122     std::vector<std::string> m_privileges;
123     std::string m_author;
124
125     pid_t m_creatorPid;
126 };