Cynara: implement method for setting policies
[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 namespace SecurityManager {
30
31
32 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
33         const std::string &privilege, Operation operation,
34         const std::string &bucket)
35 {
36     this->client = strdup(client.c_str());
37     this->user = strdup(user.c_str());
38     this->privilege = strdup(privilege.c_str());
39     this->bucket = strdup(bucket.c_str());
40
41     if (this->bucket == nullptr || this->client == nullptr ||
42         this->user == nullptr || this->privilege == nullptr) {
43         free(this->bucket);
44         free(this->client);
45         free(this->user);
46         free(this->privilege);
47         throw std::bad_alloc();
48     }
49
50     this->result = static_cast<int>(operation);
51     this->result_extra = nullptr;
52 }
53
54 CynaraAdminPolicy::CynaraAdminPolicy(const std::string &client, const std::string &user,
55     const std::string &privilege, const std::string &goToBucket,
56     const std::string &bucket)
57 {
58     this->bucket = strdup(bucket.c_str());
59     this->client = strdup(client.c_str());
60     this->user = strdup(user.c_str());
61     this->privilege = strdup(privilege.c_str());
62     this->result_extra = strdup(goToBucket.c_str());
63     this->result = CYNARA_ADMIN_BUCKET;
64
65     if (this->bucket == nullptr || this->client == nullptr ||
66         this->user == nullptr || this->privilege == nullptr ||
67         this->result_extra == nullptr) {
68         free(this->bucket);
69         free(this->client);
70         free(this->user);
71         free(this->privilege);
72         free(this->result_extra);
73         throw std::bad_alloc();
74     }
75 }
76
77 CynaraAdminPolicy::~CynaraAdminPolicy()
78 {
79     free(this->bucket);
80     free(this->client);
81     free(this->user);
82     free(this->privilege);
83     free(this->result_extra);
84 }
85
86 static void checkCynaraAdminError(int result, const std::string &msg)
87 {
88     switch (result) {
89         case CYNARA_ADMIN_API_SUCCESS:
90             return;
91         case CYNARA_ADMIN_API_OUT_OF_MEMORY:
92             ThrowMsg(CynaraException::OutOfMemory, msg);
93         case CYNARA_ADMIN_API_INVALID_PARAM:
94             ThrowMsg(CynaraException::InvalidParam, msg);
95         case CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE:
96             ThrowMsg(CynaraException::ServiceNotAvailable, msg);
97         default:
98             ThrowMsg(CynaraException::UnknownError, msg);
99     }
100 }
101
102 CynaraAdmin::CynaraAdmin()
103 {
104     checkCynaraAdminError(
105         cynara_admin_initialize(&m_CynaraAdmin),
106         "Cannot connect to Cynara administrative interface.");
107 }
108
109 CynaraAdmin::~CynaraAdmin()
110 {
111     cynara_admin_finish(m_CynaraAdmin);
112 }
113
114 void CynaraAdmin::SetPolicies(const std::vector<CynaraAdminPolicy> &policies)
115 {
116     std::vector<const struct cynara_admin_policy *> pp_policies(policies.size() + 1);
117
118     for (std::size_t i = 0; i < policies.size(); ++i)
119         pp_policies[i] = static_cast<const struct cynara_admin_policy *>(&policies[i]);
120
121     pp_policies[policies.size()] = nullptr;
122
123     checkCynaraAdminError(
124         cynara_admin_set_policies(m_CynaraAdmin, pp_policies.data()),
125         "Error while updating Cynara policy.");
126 }
127
128 } // namespace SecurityManager