Rewrite and fix CynaraAdmin::SetPolicies
[platform/core/security/security-manager.git] / src / common / master-req.cpp
1 /*
2  *  Copyright (c) 2015 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 /*
20  * @file        master-req.cpp
21  * @author      Lukasz Kostyra <l.kostyra@samsung.com>
22  * @brief       Definitions of master request calls
23  */
24
25 #include "master-req.h"
26
27 #include <dpl/serialization.h>
28
29 #include "message-buffer.h"
30 #include "connection.h"
31
32 namespace SecurityManager {
33 namespace MasterReq {
34
35 int CynaraPolicyUpdate(const std::string &appId, const std::string &uidstr,
36                        const std::vector<std::string> &privileges)
37 {
38     int ret;
39     MessageBuffer sendBuf, retBuf;
40
41     Serialization::Serialize(sendBuf,
42         static_cast<int>(MasterSecurityModuleCall::CYNARA_UPDATE_POLICY),
43         appId, uidstr, privileges);
44
45     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
46     if (ret == SECURITY_MANAGER_API_SUCCESS)
47         Deserialization::Deserialize(retBuf, ret);
48
49     return ret;
50 }
51
52 int CynaraUserInit(const uid_t uidAdded, int userType)
53 {
54     int ret;
55     MessageBuffer sendBuf, retBuf;
56
57     Serialization::Serialize(sendBuf,
58         static_cast<int>(MasterSecurityModuleCall::CYNARA_USER_INIT),
59         uidAdded, userType);
60
61     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
62     if (ret == SECURITY_MANAGER_API_SUCCESS)
63         Deserialization::Deserialize(retBuf, ret);
64
65     return ret;
66 }
67
68 int CynaraUserRemove(const uid_t uidDeleted)
69 {
70     int ret;
71     MessageBuffer sendBuf, retBuf;
72
73     Serialization::Serialize(sendBuf,
74         static_cast<int>(MasterSecurityModuleCall::CYNARA_USER_REMOVE),
75         uidDeleted);
76
77     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
78     if (ret == SECURITY_MANAGER_API_SUCCESS)
79         Deserialization::Deserialize(retBuf, ret);
80
81     return ret;
82 }
83
84 int SmackInstallRules(const std::string &appId, const std::string &pkgId,
85                       const std::vector<std::string> &pkgContents)
86 {
87     int ret;
88     MessageBuffer sendBuf, retBuf;
89     Serialization::Serialize(sendBuf,
90         static_cast<int>(MasterSecurityModuleCall::SMACK_INSTALL_RULES),
91         appId, pkgId, pkgContents);
92
93     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
94     if (ret == SECURITY_MANAGER_API_SUCCESS)
95         Deserialization::Deserialize(retBuf, ret);
96
97     return ret;
98 }
99
100 int SmackUninstallRules(const std::string &appId, const std::string &pkgId,
101                         const std::vector<std::string> &pkgContents, const bool removePkg)
102 {
103     int ret;
104     MessageBuffer sendBuf, retBuf;
105
106     Serialization::Serialize(sendBuf,
107         static_cast<int>(MasterSecurityModuleCall::SMACK_UNINSTALL_RULES),
108         appId, pkgId, pkgContents, removePkg);
109
110     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
111     if (ret == SECURITY_MANAGER_API_SUCCESS)
112         Deserialization::Deserialize(retBuf, ret);
113
114     return ret;
115 }
116
117 // Following three requests are just forwarded security-manager API calls
118 // these do not access Privilege DB, so all can be forwarded to Master
119 int PolicyUpdate(const std::vector<policy_entry> &policyEntries, uid_t uid, pid_t pid,
120                               const std::string &smackLabel)
121 {
122     int ret;
123     MessageBuffer sendBuf, retBuf;
124
125     Serialization::Serialize(sendBuf,
126         static_cast<int>(MasterSecurityModuleCall::POLICY_UPDATE),
127         policyEntries, uid, pid, smackLabel);
128
129     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
130     if (ret == SECURITY_MANAGER_API_SUCCESS)
131         Deserialization::Deserialize(retBuf, ret);
132
133     return ret;
134 }
135
136 int GetConfiguredPolicy(bool forAdmin, const policy_entry &filter, uid_t uid, pid_t pid,
137                         const std::string &smackLabel, std::vector<policy_entry> &policyEntries)
138 {
139     int ret;
140     MessageBuffer sendBuf, retBuf;
141
142     Serialization::Serialize(sendBuf,
143         static_cast<int>(MasterSecurityModuleCall::GET_CONFIGURED_POLICY),
144         forAdmin, filter, uid, pid, smackLabel);
145
146     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
147     if (ret == SECURITY_MANAGER_API_SUCCESS) {
148         Deserialization::Deserialize(retBuf, ret);
149         if (ret == SECURITY_MANAGER_API_SUCCESS)
150             Deserialization::Deserialize(retBuf, policyEntries);
151     }
152
153     return ret;
154 }
155
156 int GetPolicy(const policy_entry &filter, uid_t uid, pid_t pid, const std::string &smackLabel,
157               std::vector<policy_entry> &policyEntries)
158 {
159     int ret;
160     MessageBuffer sendBuf, retBuf;
161
162     Serialization::Serialize(sendBuf,
163         static_cast<int>(MasterSecurityModuleCall::GET_POLICY),
164         filter, uid, pid, smackLabel);
165
166     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
167     if (ret == SECURITY_MANAGER_API_SUCCESS) {
168         Deserialization::Deserialize(retBuf, ret);
169         if (ret == SECURITY_MANAGER_API_SUCCESS)
170             Deserialization::Deserialize(retBuf, policyEntries);
171     }
172
173     return ret;
174 }
175
176 int PolicyGetDesc(std::vector<std::string> &descriptions)
177 {
178     int ret;
179     MessageBuffer sendBuf, retBuf;
180
181     Serialization::Serialize(sendBuf,
182         static_cast<int>(MasterSecurityModuleCall::POLICY_GET_DESC));
183
184     ret = sendToServer(MASTER_SERVICE_SOCKET, sendBuf.Pop(), retBuf);
185     if (ret == SECURITY_MANAGER_API_SUCCESS) {
186         Deserialization::Deserialize(retBuf, ret);
187         if (ret == SECURITY_MANAGER_API_SUCCESS)
188             Deserialization::Deserialize(retBuf, descriptions);
189     }
190
191     return ret;
192 }
193
194 } // namespace MasterReq
195 } // namespace SecurityManager