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
29 #include <dpl/log/log.h>
31 namespace SecurityManager {
34 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
35 const std::string &privilege, Operation operation,
36 const std::string &bucket)
38 this->client = strdup(client.c_str());
39 this->user = strdup(user.c_str());
40 this->privilege = strdup(privilege.c_str());
41 this->bucket = strdup(bucket.c_str());
43 if (this->bucket == nullptr || this->client == nullptr ||
44 this->user == nullptr || this->privilege == nullptr) {
48 free(this->privilege);
49 ThrowMsg(CynaraException::OutOfMemory,
50 std::string("Error in CynaraAdminPolicy allocation."));
53 this->result = static_cast<int>(operation);
54 this->result_extra = nullptr;
57 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
58 const std::string &privilege, const std::string &goToBucket,
59 const std::string &bucket)
61 this->bucket = strdup(bucket.c_str());
62 this->client = strdup(client.c_str());
63 this->user = strdup(user.c_str());
64 this->privilege = strdup(privilege.c_str());
65 this->result_extra = strdup(goToBucket.c_str());
66 this->result = CYNARA_ADMIN_BUCKET;
68 if (this->bucket == nullptr || this->client == nullptr ||
69 this->user == nullptr || this->privilege == nullptr ||
70 this->result_extra == nullptr) {
74 free(this->privilege);
75 free(this->result_extra);
76 ThrowMsg(CynaraException::OutOfMemory,
77 std::string("Error in CynaraAdminPolicy allocation."));
81 CynaraAdminPolicy::CynaraAdminPolicy(CynaraAdminPolicy &&that)
86 privilege = that.privilege;
87 result_extra = that.result_extra;
90 that.bucket = nullptr;
91 that.client = nullptr;
93 that.privilege = nullptr;
94 that.result_extra = nullptr;
97 CynaraAdminPolicy::~CynaraAdminPolicy()
102 free(this->privilege);
103 free(this->result_extra);
106 static bool checkCynaraError(int result, const std::string &msg)
109 case CYNARA_API_SUCCESS:
110 case CYNARA_API_ACCESS_ALLOWED:
112 case CYNARA_API_ACCESS_DENIED:
114 case CYNARA_API_OUT_OF_MEMORY:
115 ThrowMsg(CynaraException::OutOfMemory, msg);
116 case CYNARA_API_INVALID_PARAM:
117 ThrowMsg(CynaraException::InvalidParam, msg);
118 case CYNARA_API_SERVICE_NOT_AVAILABLE:
119 ThrowMsg(CynaraException::ServiceNotAvailable, msg);
121 ThrowMsg(CynaraException::UnknownError, msg);
125 CynaraAdmin::CynaraAdmin()
128 cynara_admin_initialize(&m_CynaraAdmin),
129 "Cannot connect to Cynara administrative interface.");
132 CynaraAdmin::~CynaraAdmin()
134 cynara_admin_finish(m_CynaraAdmin);
137 void CynaraAdmin::SetPolicies(const std::vector<CynaraAdminPolicy> &policies)
139 std::vector<const struct cynara_admin_policy *> pp_policies(policies.size() + 1);
141 LogDebug("Sending " << policies.size() << " policies to Cynara");
142 for (std::size_t i = 0; i < policies.size(); ++i) {
143 pp_policies[i] = static_cast<const struct cynara_admin_policy *>(&policies[i]);
144 LogDebug("policies[" << i << "] = {" <<
145 ".bucket = " << pp_policies[i]->bucket << ", " <<
146 ".client = " << pp_policies[i]->client << ", " <<
147 ".user = " << pp_policies[i]->user << ", " <<
148 ".privilege = " << pp_policies[i]->privilege << ", " <<
149 ".result = " << pp_policies[i]->result << ", " <<
150 ".result_extra = " << pp_policies[i]->result_extra << "}");
153 pp_policies[policies.size()] = nullptr;
156 cynara_admin_set_policies(m_CynaraAdmin, pp_policies.data()),
157 "Error while updating Cynara policy.");
160 void CynaraAdmin::UpdatePackagePolicy(
161 const std::string &label,
162 const std::string &user,
163 const std::vector<std::string> &oldPrivileges,
164 const std::vector<std::string> &newPrivileges)
166 CynaraAdmin cynaraAdmin;
167 std::vector<CynaraAdminPolicy> policies;
169 // Perform sort-merge join on oldPrivileges and newPrivileges.
170 // Assume that they are already sorted and without duplicates.
171 auto oldIter = oldPrivileges.begin();
172 auto newIter = newPrivileges.begin();
174 while (oldIter != oldPrivileges.end() && newIter != newPrivileges.end()) {
175 int compare = oldIter->compare(*newIter);
177 LogDebug("(user = " << user << " label = " << label << ") " <<
178 "keeping privilege " << *newIter);
182 } else if (compare < 0) {
183 LogDebug("(user = " << user << " label = " << label << ") " <<
184 "removing privilege " << *oldIter);
185 policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
186 CynaraAdminPolicy::Operation::Delete));
189 LogDebug("(user = " << user << " label = " << label << ") " <<
190 "adding privilege " << *newIter);
191 policies.push_back(CynaraAdminPolicy(label, user, *newIter,
192 CynaraAdminPolicy::Operation::Allow));
197 for (; oldIter != oldPrivileges.end(); ++oldIter) {
198 LogDebug("(user = " << user << " label = " << label << ") " <<
199 "removing privilege " << *oldIter);
200 policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
201 CynaraAdminPolicy::Operation::Delete));
204 for (; newIter != newPrivileges.end(); ++newIter) {
205 LogDebug("(user = " << user << " label = " << label << ") " <<
206 "adding privilege " << *newIter);
207 policies.push_back(CynaraAdminPolicy(label, user, *newIter,
208 CynaraAdminPolicy::Operation::Allow));
211 cynaraAdmin.SetPolicies(policies);
217 cynara_initialize(&m_Cynara, nullptr),
218 "Cannot connect to Cynara policy interface.");
223 cynara_finish(m_Cynara);
226 bool Cynara::check(const std::string &label, const std::string &privilege,
227 const std::string &user, const std::string &session)
229 return checkCynaraError(
230 cynara_check(m_Cynara,
231 label.c_str(), session.c_str(), user.c_str(), privilege.c_str()),
232 "Cannot check permission with Cynara.");
235 } // namespace SecurityManager