Fix Cynara policy setting, use Smack label as app identifier
[platform/core/security/security-manager.git] / src / server / service / cynara.cpp
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.cpp
20  * @author      Rafal Krypa <r.krypa@samsung.com>
21  * @brief       Wrapper class for Cynara interface
22  */
23
24 #include <cstring>
25 #include <string>
26 #include <vector>
27 #include "cynara.h"
28
29 #include <dpl/log/log.h>
30
31 namespace SecurityManager {
32
33
34 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
35         const std::string &privilege, Operation operation,
36         const std::string &bucket)
37 {
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());
42
43     if (this->bucket == nullptr || this->client == nullptr ||
44         this->user == nullptr || this->privilege == nullptr) {
45         free(this->bucket);
46         free(this->client);
47         free(this->user);
48         free(this->privilege);
49         ThrowMsg(CynaraException::OutOfMemory,
50                 std::string("Error in CynaraAdminPolicy allocation."));
51     }
52
53     this->result = static_cast<int>(operation);
54     this->result_extra = nullptr;
55 }
56
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)
60 {
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;
67
68     if (this->bucket == nullptr || this->client == nullptr ||
69         this->user == nullptr || this->privilege == nullptr ||
70         this->result_extra == nullptr) {
71         free(this->bucket);
72         free(this->client);
73         free(this->user);
74         free(this->privilege);
75         free(this->result_extra);
76         ThrowMsg(CynaraException::OutOfMemory,
77                 std::string("Error in CynaraAdminPolicy allocation."));
78     }
79 }
80
81 CynaraAdminPolicy::CynaraAdminPolicy(CynaraAdminPolicy &&that)
82 {
83     bucket = that.bucket;
84     client = that.client;
85     user = that.user;
86     privilege = that.privilege;
87     result_extra = that.result_extra;
88     result = that.result;
89
90     that.bucket = nullptr;
91     that.client = nullptr;
92     that.user = nullptr;
93     that.privilege = nullptr;
94     that.result_extra = nullptr;
95 }
96
97 CynaraAdminPolicy::~CynaraAdminPolicy()
98 {
99     free(this->bucket);
100     free(this->client);
101     free(this->user);
102     free(this->privilege);
103     free(this->result_extra);
104 }
105
106 static void checkCynaraAdminError(int result, const std::string &msg)
107 {
108     switch (result) {
109         case CYNARA_ADMIN_API_SUCCESS:
110             return;
111         case CYNARA_ADMIN_API_OUT_OF_MEMORY:
112             ThrowMsg(CynaraException::OutOfMemory, msg);
113         case CYNARA_ADMIN_API_INVALID_PARAM:
114             ThrowMsg(CynaraException::InvalidParam, msg);
115         case CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE:
116             ThrowMsg(CynaraException::ServiceNotAvailable, msg);
117         default:
118             ThrowMsg(CynaraException::UnknownError, msg);
119     }
120 }
121
122 CynaraAdmin::CynaraAdmin()
123 {
124     checkCynaraAdminError(
125         cynara_admin_initialize(&m_CynaraAdmin),
126         "Cannot connect to Cynara administrative interface.");
127 }
128
129 CynaraAdmin::~CynaraAdmin()
130 {
131     cynara_admin_finish(m_CynaraAdmin);
132 }
133
134 void CynaraAdmin::SetPolicies(const std::vector<CynaraAdminPolicy> &policies)
135 {
136     std::vector<const struct cynara_admin_policy *> pp_policies(policies.size() + 1);
137
138     LogDebug("Sending " << policies.size() << " policies to Cynara");
139     for (std::size_t i = 0; i < policies.size(); ++i) {
140         pp_policies[i] = static_cast<const struct cynara_admin_policy *>(&policies[i]);
141         LogDebug("policies[" << i << "] = {" <<
142             ".bucket = " << pp_policies[i]->bucket << ", " <<
143             ".client = " << pp_policies[i]->client << ", " <<
144             ".user = " << pp_policies[i]->user << ", " <<
145             ".privilege = " << pp_policies[i]->privilege << ", " <<
146             ".result = " << pp_policies[i]->result << ", " <<
147             ".result_extra = " << pp_policies[i]->result_extra << "}");
148     }
149
150     pp_policies[policies.size()] = nullptr;
151
152     checkCynaraAdminError(
153         cynara_admin_set_policies(m_CynaraAdmin, pp_policies.data()),
154         "Error while updating Cynara policy.");
155 }
156
157 void CynaraAdmin::UpdatePackagePolicy(
158     const std::string &label,
159     const std::string &user,
160     const std::vector<std::string> &oldPrivileges,
161     const std::vector<std::string> &newPrivileges)
162 {
163     CynaraAdmin cynaraAdmin;
164     std::vector<CynaraAdminPolicy> policies;
165
166     // Perform sort-merge join on oldPrivileges and newPrivileges.
167     // Assume that they are already sorted and without duplicates.
168     auto oldIter = oldPrivileges.begin();
169     auto newIter = newPrivileges.begin();
170
171     while (oldIter != oldPrivileges.end() && newIter != newPrivileges.end()) {
172         int compare = oldIter->compare(*newIter);
173         if (compare == 0) {
174             LogDebug("(user = " << user << " label = " << label << ") " <<
175                 "keeping privilege " << *newIter);
176             ++oldIter;
177             ++newIter;
178             continue;
179         } else if (compare < 0) {
180             LogDebug("(user = " << user << " label = " << label << ") " <<
181                 "removing privilege " << *oldIter);
182             policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
183                     CynaraAdminPolicy::Operation::Delete));
184             ++oldIter;
185         } else {
186             LogDebug("(user = " << user << " label = " << label << ") " <<
187                 "adding privilege " << *newIter);
188             policies.push_back(CynaraAdminPolicy(label, user, *newIter,
189                     CynaraAdminPolicy::Operation::Allow));
190             ++newIter;
191         }
192     }
193
194     for (; oldIter != oldPrivileges.end(); ++oldIter) {
195         LogDebug("(user = " << user << " label = " << label << ") " <<
196             "removing privilege " << *oldIter);
197         policies.push_back(CynaraAdminPolicy(label, user, *oldIter,
198                     CynaraAdminPolicy::Operation::Delete));
199     }
200
201     for (; newIter != newPrivileges.end(); ++newIter) {
202         LogDebug("(user = " << user << " label = " << label << ") " <<
203             "adding privilege " << *newIter);
204         policies.push_back(CynaraAdminPolicy(label, user, *newIter,
205                     CynaraAdminPolicy::Operation::Allow));
206     }
207
208     cynaraAdmin.SetPolicies(policies);
209 }
210
211
212 } // namespace SecurityManager