2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Rafal Krypa <r.krypa@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * @author Rafal Krypa <r.krypa@samsung.com>
21 * @brief Wrapper class for Cynara interface
24 #ifndef _SECURITY_MANAGER_CYNARA_
25 #define _SECURITY_MANAGER_CYNARA_
27 #include <cynara-client.h>
28 #include <cynara-admin.h>
29 #include <dpl/exception.h>
34 #include "security-manager.h"
36 namespace SecurityManager {
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)
61 struct CynaraAdminPolicy : cynara_admin_policy
63 enum class Operation {
64 Deny = CYNARA_ADMIN_DENY,
65 Allow = CYNARA_ADMIN_ALLOW,
66 Delete = CYNARA_ADMIN_DELETE,
67 Bucket = CYNARA_ADMIN_BUCKET,
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));
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));
78 /* Don't provide copy constructor, it would cause pointer trouble. */
79 CynaraAdminPolicy(const CynaraAdminPolicy &that) = delete;
81 /* Move constructor is the way to go. */
82 CynaraAdminPolicy(CynaraAdminPolicy &&that);
91 typedef std::map<Bucket, const std::string > BucketsMap;
92 static BucketsMap Buckets;
94 typedef std::map<int, std::string> TypeToDescriptionMap;
95 typedef std::map<std::string, int> DescriptionToTypeMap;
97 virtual ~CynaraAdmin();
99 static CynaraAdmin &getInstance();
102 * Update Cynara policies.
103 * Caller must have permission to access Cynara administrative socket.
105 * @param policies vector of CynaraAdminPolicy objects to send to Cynara
107 void SetPolicies(const std::vector<CynaraAdminPolicy> &policies);
110 * Update Cynara policies for the package and the user, using two vectors
111 * of privileges: privileges set before (and already enabled in Cynara)
112 * and new privileges, to be set in Cynara.
113 * Difference will be calculated, removing old unneeded privileges and
114 * adding new, previously not enabled privileges.
115 * Caller must have permission to access Cynara administrative socket.
117 * @param label application Smack label
118 * @param user user identifier
119 * @param oldPrivileges previously enabled privileges for the package.
120 * Must be sorted and without duplicates.
121 * @param newPrivileges currently enabled privileges for the package.
122 * Must be sorted and without duplicates.
124 * TODO: drop oldPrivileges argument and get them directly from Cynara.
125 * Appropriate Cynara interface is needed first.
127 void UpdateAppPolicy(const std::string &label, const std::string &user,
128 const std::vector<std::string> &oldPrivileges,
129 const std::vector<std::string> &newPrivileges);
132 * Depending on user type, create link between MAIN bucket and appropriate
133 * USER_TYPE_* bucket for newly added user uid to apply permissions for that
135 * @throws CynaraException::InvalidParam.
137 * @param uid new user uid
138 * @param userType type as enumerated in security-manager.h
140 void UserInit(uid_t uid, security_manager_user_type userType);
143 * Removes all entries for a user from cynara database
145 * @param uid removed user uid
147 void UserRemove(uid_t uid);
150 * List Cynara policies that match selected criteria in given bucket.
152 * @param bucketName name of the bucket to search policies in
153 * @param appId string with id of app to match in search
154 * @param user user string to match in search
155 * @param privilege privilege string to match in search
156 * @param policies empty vector for results of policies filtering.
159 void ListPolicies(const std::string &bucketName,
160 const std::string &appId,
161 const std::string &user,
162 const std::string &privilege,
163 std::vector<CynaraAdminPolicy> &policies);
166 * Wrapper for Cynara API function cynara_admin_list_policies_descriptions.
167 * It collects all policies descriptions, extracts names
168 * of policies and returns as std strings. Caller is responsible for clearing
169 * vector passed as argument.
171 * @param policiesDescriptions empty vector for policies descriptions.
173 void ListPoliciesDescriptions(std::vector<std::string> &policiesDescriptions);
176 * Function translates internal Cynara policy type integer to string
177 * description. Descriptions are retrieved from Cynara using
178 * ListPoliciesDescriptions() function. Caller can force refetching of
179 * descriptions list from Cynara on each call.
181 * @throws std::out_of_range
183 * @param policyType Cynara policy result type.
184 * @param forceRefresh switch to force refetching of descriptions from Cynara.
186 std::string convertToPolicyDescription(const int policyType, bool forceRefresh = false);
189 * Function translates Cynara policy result string
190 * description to internal Cynara policy type integer.
191 * Descriptions are retrieved from Cynara using
192 * ListPoliciesDescriptions() function. Caller can force refetching of
193 * descriptions list from Cynara on each call.
195 * @throws std::out_of_range
197 * @param policy Cynara policy result string description.
198 * @param forceRefresh switch to force refetching of descriptions from Cynara.
200 int convertToPolicyType(const std::string &policy, bool forceRefresh = false);
206 * Empty bucket using filter - matching rules will be removed
208 * @param bucketName name of the bucket to be emptied
209 * @param recursive flag to remove privileges recursively
210 * @param client client name
211 * @param user user name
212 * @param privilege privilege name
214 void EmptyBucket(const std::string &bucketName, bool recursive,
215 const std::string &client, const std::string &user, const std::string &privilege);
218 * Get Cynara policies result descriptions and cache them in std::map
220 * @param forceRefresh true if you want to reinitialize mappings
222 void FetchCynaraPolicyDescriptions(bool forceRefresh = false);
224 struct cynara_admin *m_CynaraAdmin;
226 static TypeToDescriptionMap TypeToDescription;
227 static DescriptionToTypeMap DescriptionToType;
228 bool m_policyDescriptionsInitialized;
236 static Cynara &getInstance();
239 * Ask Cynara for permission.
241 * @param label application Smack label
242 * @param privilege privilege identifier
243 * @param user user identifier (uid)
244 * @param session session identifier
245 * @return true if access is permitted, false if denied
247 bool check(const std::string &label, const std::string &privilege,
248 const std::string &user, const std::string &session);
252 struct cynara *m_Cynara;
256 } // namespace SecurityManager
258 #endif // _SECURITY_MANAGER_CYNARA_