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