a75e414395fe915789a50089892d7d3dfcb99b6e
[platform/core/security/security-manager.git] / src / common / include / cynara.h
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        cynara.h
20  * @author      Rafal Krypa <r.krypa@samsung.com>
21  * @brief       Wrapper class for Cynara interface
22  */
23
24 #ifndef _SECURITY_MANAGER_CYNARA_
25 #define _SECURITY_MANAGER_CYNARA_
26
27 #include <cynara-client.h>
28 #include <cynara-admin.h>
29 #include <dpl/exception.h>
30 #include <string>
31 #include <vector>
32 #include <map>
33
34 #include "security-manager.h"
35
36 namespace SecurityManager {
37
38 enum class Bucket
39 {
40     PRIVACY_MANAGER,
41     MAIN,
42     USER_TYPE_ADMIN,
43     USER_TYPE_NORMAL,
44     USER_TYPE_GUEST,
45     USER_TYPE_SYSTEM,
46     ADMIN,
47     MANIFESTS
48 };
49
50 class CynaraException
51 {
52 public:
53     DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
54     DECLARE_EXCEPTION_TYPE(Base, OutOfMemory)
55     DECLARE_EXCEPTION_TYPE(Base, InvalidParam)
56     DECLARE_EXCEPTION_TYPE(Base, ServiceNotAvailable)
57     DECLARE_EXCEPTION_TYPE(Base, UnknownError)
58     DECLARE_EXCEPTION_TYPE(Base, BucketNotFound)
59 };
60
61 struct CynaraAdminPolicy : cynara_admin_policy
62 {
63     enum class Operation {
64         Deny = CYNARA_ADMIN_DENY,
65         Allow = CYNARA_ADMIN_ALLOW,
66         Delete = CYNARA_ADMIN_DELETE,
67         Bucket = CYNARA_ADMIN_BUCKET,
68     };
69
70     CynaraAdminPolicy(const std::string &client, const std::string &user,
71         const std::string &privilege, int operation,
72         const std::string &bucket = std::string(CYNARA_ADMIN_DEFAULT_BUCKET));
73
74     CynaraAdminPolicy(const std::string &client, const std::string &user,
75         const std::string &privilege, const std::string &goToBucket,
76         const std::string &bucket = std::string(CYNARA_ADMIN_DEFAULT_BUCKET));
77
78     /* Don't provide copy constructor, it would cause pointer trouble. */
79     CynaraAdminPolicy(const CynaraAdminPolicy &that) = delete;
80
81     /* Move constructor is the way to go. */
82     CynaraAdminPolicy(CynaraAdminPolicy &&that);
83     CynaraAdminPolicy& operator=(CynaraAdminPolicy &&that);
84
85     ~CynaraAdminPolicy();
86 };
87
88 class CynaraAdmin
89 {
90 public:
91
92     typedef std::map<Bucket, const std::string > BucketsMap;
93     static BucketsMap Buckets;
94
95     typedef  std::map<int, std::string> TypeToDescriptionMap;
96     typedef  std::map<std::string, int> DescriptionToTypeMap;
97
98     virtual ~CynaraAdmin();
99
100     static CynaraAdmin &getInstance();
101
102     /**
103      * Update Cynara policies.
104      * Caller must have permission to access Cynara administrative socket.
105      *
106      * @param policies vector of CynaraAdminPolicy objects to send to Cynara
107      */
108     void SetPolicies(const std::vector<CynaraAdminPolicy> &policies);
109
110     /**
111      * Update Cynara policies for the package and the user, using two vectors
112      * of privileges: privileges set before (and already enabled in Cynara)
113      * and new privileges, to be set in Cynara.
114      * Difference will be calculated, removing old unneeded privileges and
115      * adding new, previously not enabled privileges.
116      * Caller must have permission to access Cynara administrative socket.
117      *
118      * @param label application Smack label
119      * @param user user identifier
120      * @param oldPrivileges previously enabled privileges for the package.
121      *        Must be sorted and without duplicates.
122      * @param newPrivileges currently enabled privileges for the package.
123      *        Must be sorted and without duplicates.
124      *
125      * TODO: drop oldPrivileges argument and get them directly from Cynara.
126      * Appropriate Cynara interface is needed first.
127      */
128     void UpdateAppPolicy(const std::string &label, const std::string &user,
129         const std::vector<std::string> &oldPrivileges,
130         const std::vector<std::string> &newPrivileges);
131
132     /**
133      * Depending on user type, create link between MAIN bucket and appropriate
134      * USER_TYPE_* bucket for newly added user uid to apply permissions for that
135      * user type.
136      * @throws CynaraException::InvalidParam.
137      *
138      * @param uid new user uid
139      * @param userType type as enumerated in security-manager.h
140      */
141     void UserInit(uid_t uid, security_manager_user_type userType);
142
143     /**
144      * List all users registered in Cynara
145      *
146      * @param[out] listOfUsers list of users
147      */
148     void ListUsers(std::vector<uid_t> &listOfUsers);
149
150     /**
151      * Removes all entries for a user from cynara database
152      *
153      * @param uid removed user uid
154      */
155     void UserRemove(uid_t uid);
156
157     /**
158      * List Cynara policies that match selected criteria in given bucket.
159      *
160      * @param bucketName name of the bucket to search policies in
161      * @param appId string with id of app to match in search
162      * @param user user string to match in search
163      * @param privilege privilege string to match in search
164      * @param policies empty vector for results of policies filtering.
165      *
166      */
167     void ListPolicies(const std::string &bucketName,
168         const std::string &appId,
169         const std::string &user,
170         const std::string &privilege,
171         std::vector<CynaraAdminPolicy> &policies);
172
173     /**
174      * Wrapper for Cynara API function cynara_admin_list_policies_descriptions.
175      * It collects all policies descriptions, extracts names
176      * of policies and returns as std strings. Caller is responsible for clearing
177      * vector passed as argument.
178      *
179      * @param policiesDescriptions empty vector for policies descriptions.
180      */
181     void ListPoliciesDescriptions(std::vector<std::string> &policiesDescriptions);
182
183     /**
184      * Function translates internal Cynara policy type integer to string
185      * description. Descriptions are retrieved from Cynara using
186      * ListPoliciesDescriptions() function. Caller can force refetching of
187      * descriptions list from Cynara on each call.
188      *
189      * @throws std::out_of_range
190      *
191      * @param policyType Cynara policy result type.
192      * @param forceRefresh switch to force refetching of descriptions from Cynara.
193      */
194     std::string convertToPolicyDescription(const int policyType, bool forceRefresh = false);
195
196     /**
197      * Function translates Cynara policy result string
198      * description to internal Cynara policy type integer.
199      * Descriptions are retrieved from Cynara using
200      * ListPoliciesDescriptions() function. Caller can force refetching of
201      * descriptions list from Cynara on each call.
202      *
203      * @throws std::out_of_range
204      *
205      * @param policy Cynara policy result string description.
206      * @param forceRefresh switch to force refetching of descriptions from Cynara.
207      */
208     int convertToPolicyType(const std::string &policy, bool forceRefresh = false);
209
210     /**
211      * Ask Cynara for permission starting the search at specified bucket.
212      * Essentialy a wrapper on cynara_admin_check.
213      *
214      * @param label application Smack label
215      * @param privilege privilege string to match in search
216      * @param user user string to match in search
217      * @param bucket name of the bucket to search policies in
218      * @param result integer to return policy result
219      * @param resultExtra string to return additional information about policy
220      *        result. If result is Bucket then resultExtra is the name of
221      *        bucket.
222      * @param recursive flag to indicate if check should be done recursively in
223      *        all buckets linked with bucket provided
224      */
225     void Check(const std::string &label,
226         const std::string &user,
227         const std::string &privilege,
228         const std::string &bucket,
229         int &result,
230         std::string &resultExtra,
231         const bool recursive);
232
233     /**
234      * Get current policy level for privilege-manager functionality
235      * Returns current policy value for given application, user and privilege
236      * identifiers.
237      *
238      * @param label application Smack label
239      * @param user user identifier (uid)
240      * @param privilege privilege identifier
241      * @return current policy value
242      */
243     int GetPrivilegeManagerCurrLevel(const std::string &label, const std::string &user,
244         const std::string &privilege);
245
246     /**
247      * Get maximum policy level for privilege-manager functionality
248      * Returns maximum possible policy value for given application, user and privilege
249      * identifiers. The maximum limit is imposed by other policy settings that are
250      * currently in place.
251      *
252      * @param label application Smack label
253      * @param user user identifier (uid)
254      * @param privilege privilege identifier
255      * @return maximum policy value for PRIVACY_MANAGER bucket
256      */
257     int GetPrivilegeManagerMaxLevel(const std::string &label, const std::string &user,
258         const std::string &privilege);
259
260 private:
261     CynaraAdmin();
262
263     /**
264      * Empty bucket using filter - matching rules will be removed
265      *
266      * @param bucketName name of the bucket to be emptied
267      * @param recursive flag to remove privileges recursively
268      * @param client client name
269      * @param user user name
270      * @param privilege privilege name
271      */
272     void EmptyBucket(const std::string &bucketName, bool recursive,
273         const std::string &client, const std::string &user, const std::string &privilege);
274
275     /**
276      * Get Cynara policies result descriptions and cache them in std::map
277      *
278      * @param forceRefresh true if you want to reinitialize mappings
279      */
280     void FetchCynaraPolicyDescriptions(bool forceRefresh = false);
281
282     struct cynara_admin *m_CynaraAdmin;
283
284     static TypeToDescriptionMap TypeToDescription;
285     static DescriptionToTypeMap DescriptionToType;
286     bool m_policyDescriptionsInitialized;
287 };
288
289 class Cynara
290 {
291 public:
292     virtual ~Cynara();
293
294     static Cynara &getInstance();
295
296     /**
297      * Ask Cynara for permission.
298      *
299      * @param label application Smack label
300      * @param privilege privilege identifier
301      * @param user user identifier (uid)
302      * @param session session identifier
303      * @return true if access is permitted, false if denied
304      */
305     bool check(const std::string &label, const std::string &privilege,
306         const std::string &user, const std::string &session);
307
308 private:
309     Cynara();
310     struct cynara *m_Cynara;
311 };
312
313
314 } // namespace SecurityManager
315
316 #endif // _SECURITY_MANAGER_CYNARA_