97a6375de4b44328da9355aa5989d2388f69f411
[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, Operation 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
84     ~CynaraAdminPolicy();
85 };
86
87 class CynaraAdmin
88 {
89 public:
90
91     typedef std::map<Bucket, const std::string > BucketsMap;
92     static BucketsMap Buckets;
93
94     virtual ~CynaraAdmin();
95
96     static CynaraAdmin &getInstance();
97
98     /**
99      * Update Cynara policies.
100      * Caller must have permission to access Cynara administrative socket.
101      *
102      * @param policies vector of CynaraAdminPolicy objects to send to Cynara
103      */
104     void SetPolicies(const std::vector<CynaraAdminPolicy> &policies);
105
106     /**
107      * Update Cynara policies for the package and the user, using two vectors
108      * of privileges: privileges set before (and already enabled in Cynara)
109      * and new privileges, to be set in Cynara.
110      * Difference will be calculated, removing old unneeded privileges and
111      * adding new, previously not enabled privileges.
112      * Caller must have permission to access Cynara administrative socket.
113      *
114      * @param label application Smack label
115      * @param user user identifier
116      * @param oldPrivileges previously enabled privileges for the package.
117      *        Must be sorted and without duplicates.
118      * @param newPrivileges currently enabled privileges for the package.
119      *        Must be sorted and without duplicates.
120      *
121      * TODO: drop oldPrivileges argument and get them directly from Cynara.
122      * Appropriate Cynara interface is needed first.
123      */
124     void UpdateAppPolicy(const std::string &label, const std::string &user,
125         const std::vector<std::string> &oldPrivileges,
126         const std::vector<std::string> &newPrivileges);
127
128     /**
129      * Depending on user type, create link between MAIN bucket and appropriate
130      * USER_TYPE_* bucket for newly added user uid to apply permissions for that
131      * user type.
132      * @throws CynaraException::InvalidParam.
133      *
134      * @param uid new user uid
135      * @param userType type as enumerated in security-manager.h
136      */
137     void UserInit(uid_t uid, security_manager_user_type userType);
138
139     /**
140      * Removes all entries for a user from cynara database
141      *
142      * @param uid removed user uid
143      */
144     void UserRemove(uid_t uid);
145
146     /**
147      * List Cynara policies that match selected criteria in given bucket.
148      *
149      * @param bucketName name of the bucket to search policies in
150      * @param appId string with id of app to match in search
151      * @param user user string to match in search
152      * @param privilege privilege string to match in search
153      * @param policies empty vector for results of policies filtering.
154      *
155      */
156     void ListPolicies(const std::string &bucketName,
157         const std::string &appId,
158         const std::string &user,
159         const std::string &privilege,
160         std::vector<CynaraAdminPolicy> &policies);
161
162 private:
163     CynaraAdmin();
164
165     /**
166      * Empty bucket using filter - matching rules will be removed
167      *
168      * @param bucketName name of the bucket to be emptied
169      * @param recursive flag to remove privileges recursively
170      * @param client client name
171      * @param user user name
172      * @param privilege privilege name
173      */
174     void EmptyBucket(const std::string &bucketName, bool recursive,
175         const std::string &client, const std::string &user, const std::string &privilege);
176
177     struct cynara_admin *m_CynaraAdmin;
178 };
179
180 class Cynara
181 {
182 public:
183     virtual ~Cynara();
184
185     static Cynara &getInstance();
186
187     /**
188      * Ask Cynara for permission.
189      *
190      * @param label application Smack label
191      * @param privilege privilege identifier
192      * @param user user identifier (uid)
193      * @param session session identifier
194      * @return true if access is permitted, false if denied
195      */
196     bool check(const std::string &label, const std::string &privilege,
197         const std::string &user, const std::string &session);
198
199 private:
200     Cynara();
201     struct cynara *m_Cynara;
202 };
203
204
205 } // namespace SecurityManager
206
207 #endif // _SECURITY_MANAGER_CYNARA_