Unify privilege representation
[platform/core/test/security-tests.git] / src / security-manager-tests / common / policy_configuration.h
1 /*
2  * Copyright (c) 2016-2020 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 #ifndef _SECURITY_MANAGER_TEST_POLICY_CONFIGURATION_
17 #define _SECURITY_MANAGER_TEST_POLICY_CONFIGURATION_
18
19 #include <map>
20 #include <string>
21 #include <vector>
22
23 #include <sys/types.h>
24
25 namespace SecurityManagerTest {
26
27 gid_t nameToGid(const char *name);
28
29 class PolicyConfiguration {
30 public:
31     typedef std::vector<gid_t> GidVector;
32     typedef std::vector<std::string> GroupVector;
33     typedef std::vector<std::string> PrivVector;
34     typedef std::map<std::string, std::string> PrivGroupMap;
35
36     struct UserDescription {
37         PrivVector privVector;
38         GroupVector groupVector;
39         GidVector gidVector;
40     };
41
42     enum UserType { GUEST, NORMAL, ADMIN, SYSTEM };
43
44     std::string getConfigFilePath(UserType userType);
45     PrivVector getUserPriv(UserType userType);
46     GroupVector getUserGroup(UserType userType);
47     GidVector getUserGid(UserType userType);
48     GidVector getGid();
49     GroupVector getGroup();
50     UserDescription& getUserDescription(UserType userType);
51     gid_t groupToGid(const std::string &gname);
52     PrivGroupMap getPrivGroupMap();
53     PrivVector getSystemdManagedPrivs();
54
55     template <typename T>
56     GroupVector privToGroup(const T &privVector);
57     GidVector groupToGid(const GroupVector &groupVector);
58
59     static bool getIsAskuserEnabled();
60     static std::string getPkgRulesFilePath();
61     static std::string getAppRulesFilePath();
62
63 private:
64     UserDescription loadUserDescription(UserType userType);
65     PrivVector loadPrivFile(const std::string &path);
66     void loadPrivGroupMap(void);
67
68     PrivGroupMap m_privGroupMap;
69     std::map<std::string, gid_t> m_groupGidMap;
70     std::map<UserType, UserDescription> m_userDescriptionMap;
71 };
72
73 template <typename T>
74 PolicyConfiguration::GroupVector PolicyConfiguration::privToGroup(const T &privVector) {
75     GroupVector result;
76     if (m_privGroupMap.empty())
77         loadPrivGroupMap();
78     for (auto &e : privVector) {
79         auto it = m_privGroupMap.find(e);
80         if (it == m_privGroupMap.end())
81             continue;
82         result.push_back(it->second);
83     }
84     return result;
85 }
86
87
88 } // namespace SecurityManagerTest
89
90 #endif