2 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Bumjin Im <bj.im@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
19 * @file client-cookie.cpp
20 * @author Pawel Polawski (p.polawski@partner.samsung.com)
22 * @brief This file contain implementation of cookie functions for getting cookies
28 #include <dpl/log/log.h>
29 #include <dpl/exception.h>
31 #include <message-buffer.h>
32 #include <client-common.h>
33 #include <protocols.h>
35 #include <security-server.h>
38 int security_server_get_cookie_size(void)
40 return SecurityServer::COOKIE_SIZE;
44 int security_server_request_cookie(char *cookie, size_t bufferSize)
46 using namespace SecurityServer;
47 MessageBuffer send, recv;
48 std::vector<char> receivedCookie;
50 LogDebug("security_server_request_cookie() called");
52 return try_catch([&] {
54 if (bufferSize < COOKIE_SIZE) {
55 LogDebug("Buffer for cookie too small");
56 return SECURITY_SERVER_API_ERROR_BUFFER_TOO_SMALL;
59 LogDebug("Cookie pointer empty");
60 return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
63 //put data into buffer
64 Serialization::Serialize(send, (int)CookieCall::GET_COOKIE);
66 //send buffer to server
67 int retval = sendToServer(SERVICE_SOCKET_COOKIE_GET, send.Pop(), recv);
68 if (retval != SECURITY_SERVER_API_SUCCESS) {
69 LogDebug("Error in sendToServer. Error code: " << retval);
73 //receive response from server
74 Deserialization::Deserialize(recv, retval);
75 if (retval != SECURITY_SERVER_API_SUCCESS)
78 Deserialization::Deserialize(recv, receivedCookie);
79 if (receivedCookie.size() != COOKIE_SIZE) {
80 LogDebug("No match in cookie size");
81 return SECURITY_SERVER_API_ERROR_BAD_RESPONSE;
84 memcpy(cookie, &receivedCookie[0], receivedCookie.size());
90 int security_server_get_cookie_pid(const char *cookie)
92 using namespace SecurityServer;
93 MessageBuffer send, recv;
95 int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
97 LogDebug("security_server_get_cookie_pid() called");
100 return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
102 //preprae cookie to send
103 std::vector<char> key(cookie, cookie + COOKIE_SIZE);
105 return try_catch([&] {
106 //put data into buffer
107 Serialization::Serialize(send, (int)CookieCall::CHECK_PID);
108 Serialization::Serialize(send, key);
110 //send buffer to server
111 retval = sendToServer(SERVICE_SOCKET_COOKIE_CHECK, send.Pop(), recv);
112 if (retval != SECURITY_SERVER_API_SUCCESS) {
113 LogDebug("Error in sendToServer. Error code: " << retval);
117 //receive response from server
118 Deserialization::Deserialize(recv, retval);
119 if (retval != SECURITY_SERVER_API_SUCCESS)
122 Deserialization::Deserialize(recv, pid);
128 char * security_server_get_smacklabel_cookie(const char *cookie)
130 using namespace SecurityServer;
131 MessageBuffer send, recv;
132 int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
135 LogDebug("security_server_get_smacklabel_cookie() called");
140 //preprae cookie to send
141 std::vector<char> key(cookie, cookie + COOKIE_SIZE);
144 //put data into buffer
145 Serialization::Serialize(send, (int)CookieCall::CHECK_SMACKLABEL);
146 Serialization::Serialize(send, key);
148 //send buffer to server
149 retval = sendToServer(SERVICE_SOCKET_COOKIE_CHECK, send.Pop(), recv);
150 if (retval != SECURITY_SERVER_API_SUCCESS) {
151 LogDebug("Error in sendToServer. Error code: " << retval);
155 //receive response from server
156 Deserialization::Deserialize(recv, retval);
157 if (retval != SECURITY_SERVER_API_SUCCESS)
160 Deserialization::Deserialize(recv, label);
162 return strdup(label.c_str());
164 } catch (MessageBuffer::Exception::Base &e) {
165 LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
166 } catch (std::exception &e) {
167 LogDebug("STD exception " << e.what());
169 LogDebug("Unknown exception occured");
176 int security_server_check_privilege(const char *cookie, gid_t privilege)
178 using namespace SecurityServer;
179 MessageBuffer send, recv;
180 int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
182 LogDebug("security_server_check_privilege() called");
185 return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
187 //preprae cookie to send
188 std::vector<char> key(cookie, cookie + COOKIE_SIZE);
190 return try_catch([&] {
191 //put data into buffer
192 Serialization::Serialize(send, (int)CookieCall::CHECK_PRIVILEGE_GID);
193 Serialization::Serialize(send, key);
194 Serialization::Serialize(send, (int)privilege);
196 //send buffer to server
197 retval = sendToServer(SERVICE_SOCKET_COOKIE_CHECK, send.Pop(), recv);
198 if (retval != SECURITY_SERVER_API_SUCCESS) {
199 LogDebug("Error in sendToServer. Error code: " << retval);
203 //receive response from server
204 Deserialization::Deserialize(recv, retval);
210 int security_server_check_privilege_by_cookie(
211 const char *cookie SECURITY_SERVER_UNUSED,
212 const char *object SECURITY_SERVER_UNUSED,
213 const char *access_rights SECURITY_SERVER_UNUSED)
216 using namespace SecurityServer;
217 MessageBuffer send, recv;
218 int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
220 LogDebug("security_server_check_privilege_by_cookie() called");
222 if ((cookie == NULL) || (object == NULL) || (access_rights == NULL))
223 return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
225 //preprae cookie to send
226 std::vector<char> key(cookie, cookie + COOKIE_SIZE);
228 std::string obj(object);
229 std::string access(access_rights);
231 return try_catch([&] {
232 //put data into buffer
233 Serialization::Serialize(send, (int)CookieCall::CHECK_PRIVILEGE);
234 Serialization::Serialize(send, key);
235 Serialization::Serialize(send, obj);
236 Serialization::Serialize(send, access);
238 //send buffer to server
239 retval = sendToServer(SERVICE_SOCKET_COOKIE_CHECK, send.Pop(), recv);
240 if (retval != SECURITY_SERVER_API_SUCCESS) {
241 LogDebug("Error in sendToServer. Error code: " << retval);
245 //receive response from server
246 Deserialization::Deserialize(recv, retval);
250 return SECURITY_SERVER_API_SUCCESS;
254 int security_server_get_uid_by_cookie(const char *cookie, uid_t *uid)
256 using namespace SecurityServer;
257 MessageBuffer send, recv;
258 int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
260 LogDebug("security_server_get_uid_by_cookie() called");
262 if ((cookie == NULL) || (uid == NULL))
263 return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
265 //preprae cookie to send
266 std::vector<char> key(cookie, cookie + COOKIE_SIZE);
268 return try_catch([&] {
269 //put data into buffer
270 Serialization::Serialize(send, (int)CookieCall::CHECK_UID);
271 Serialization::Serialize(send, key);
273 //send buffer to server
274 retval = sendToServer(SERVICE_SOCKET_COOKIE_CHECK, send.Pop(), recv);
275 if (retval != SECURITY_SERVER_API_SUCCESS) {
276 LogDebug("Error in sendToServer. Error code: " << retval);
280 //receive response from server
281 Deserialization::Deserialize(recv, retval);
282 if (retval == SECURITY_SERVER_API_SUCCESS) {
284 Deserialization::Deserialize(recv, tmp);
285 *uid = static_cast<uid_t>(tmp);