Add smackfs check to Installer service.
[platform/core/security/security-manager.git] / src / server / client / client-cookie.cpp
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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        client-cookie.cpp
20  * @author      Pawel Polawski (p.polawski@partner.samsung.com)
21  * @version     1.0
22  * @brief       This file contain implementation of cookie functions for getting cookies
23  */
24
25
26 #include <cstdio>
27
28 #include <dpl/log/log.h>
29 #include <dpl/exception.h>
30
31 #include <message-buffer.h>
32 #include <client-common.h>
33 #include <protocols.h>
34
35 #include <security-server.h>
36
37 SECURITY_SERVER_API
38 int security_server_get_cookie_size(void)
39 {
40     return SecurityServer::COOKIE_SIZE;
41 }
42
43 SECURITY_SERVER_API
44 int security_server_request_cookie(char *cookie, size_t bufferSize)
45 {
46     using namespace SecurityServer;
47     MessageBuffer send, recv;
48     std::vector<char> receivedCookie;
49
50     LogDebug("security_server_request_cookie() called");
51
52     return try_catch([&] {
53         //checking parameters
54         if (bufferSize < COOKIE_SIZE) {
55             LogDebug("Buffer for cookie too small");
56             return SECURITY_SERVER_API_ERROR_BUFFER_TOO_SMALL;
57         }
58         if (cookie == NULL) {
59             LogDebug("Cookie pointer empty");
60             return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
61         }
62
63         //put data into buffer
64         Serialization::Serialize(send, (int)CookieCall::GET_COOKIE);
65
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);
70             return retval;
71         }
72
73         //receive response from server
74         Deserialization::Deserialize(recv, retval);
75         if (retval != SECURITY_SERVER_API_SUCCESS)
76             return retval;
77
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;
82         }
83
84         memcpy(cookie, &receivedCookie[0], receivedCookie.size());
85         return retval;
86     });
87 }
88
89 SECURITY_SERVER_API
90 int security_server_get_cookie_pid(const char *cookie)
91 {
92     using namespace SecurityServer;
93     MessageBuffer send, recv;
94     int pid;
95     int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
96
97     LogDebug("security_server_get_cookie_pid() called");
98
99     if (cookie == NULL)
100         return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
101
102     //preprae cookie to send
103     std::vector<char> key(cookie, cookie + COOKIE_SIZE);
104
105     return try_catch([&] {
106         //put data into buffer
107         Serialization::Serialize(send, (int)CookieCall::CHECK_PID);
108         Serialization::Serialize(send, key);
109
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);
114             return retval;
115         }
116
117         //receive response from server
118         Deserialization::Deserialize(recv, retval);
119         if (retval != SECURITY_SERVER_API_SUCCESS)
120             return retval;
121
122         Deserialization::Deserialize(recv, pid);
123         return pid;
124     });
125 }
126
127 SECURITY_SERVER_API
128 char * security_server_get_smacklabel_cookie(const char *cookie)
129 {
130     using namespace SecurityServer;
131     MessageBuffer send, recv;
132     int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
133     std::string label;
134
135     LogDebug("security_server_get_smacklabel_cookie() called");
136
137     if (cookie == NULL)
138         return NULL;
139
140     //preprae cookie to send
141     std::vector<char> key(cookie, cookie + COOKIE_SIZE);
142
143     try {
144         //put data into buffer
145         Serialization::Serialize(send, (int)CookieCall::CHECK_SMACKLABEL);
146         Serialization::Serialize(send, key);
147
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);
152             return NULL;
153         }
154
155         //receive response from server
156         Deserialization::Deserialize(recv, retval);
157         if (retval != SECURITY_SERVER_API_SUCCESS)
158             return NULL;
159
160         Deserialization::Deserialize(recv, label);
161
162         return strdup(label.c_str());
163
164     } catch (MessageBuffer::Exception::Base &e) {
165         LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
166     } catch (std::exception &e) {
167         LogDebug("STD exception " << e.what());
168     } catch (...) {
169         LogDebug("Unknown exception occured");
170     }
171
172     return NULL;
173 }
174
175 SECURITY_SERVER_API
176 int security_server_check_privilege(const char *cookie, gid_t privilege)
177 {
178     using namespace SecurityServer;
179     MessageBuffer send, recv;
180     int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
181
182     LogDebug("security_server_check_privilege() called");
183
184     if (cookie == NULL)
185         return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
186
187     //preprae cookie to send
188     std::vector<char> key(cookie, cookie + COOKIE_SIZE);
189
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);
195
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);
200             return retval;
201         }
202
203         //receive response from server
204         Deserialization::Deserialize(recv, retval);
205         return retval;
206     });
207 }
208
209 SECURITY_SERVER_API
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)
214 {
215 #if 0
216     using namespace SecurityServer;
217     MessageBuffer send, recv;
218     int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
219
220     LogDebug("security_server_check_privilege_by_cookie() called");
221
222     if ((cookie == NULL) || (object == NULL) || (access_rights == NULL))
223         return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
224
225     //preprae cookie to send
226     std::vector<char> key(cookie, cookie + COOKIE_SIZE);
227
228     std::string obj(object);
229     std::string access(access_rights);
230
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);
237
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);
242             return retval;
243         }
244
245         //receive response from server
246         Deserialization::Deserialize(recv, retval);
247         return retval;
248     });
249 #endif
250         return SECURITY_SERVER_API_SUCCESS;
251 }
252
253 SECURITY_SERVER_API
254 int security_server_get_uid_by_cookie(const char *cookie, uid_t *uid)
255 {
256     using namespace SecurityServer;
257     MessageBuffer send, recv;
258     int retval = SECURITY_SERVER_API_ERROR_UNKNOWN;
259
260     LogDebug("security_server_get_uid_by_cookie() called");
261
262     if ((cookie == NULL) || (uid == NULL))
263         return SECURITY_SERVER_API_ERROR_INPUT_PARAM;
264
265     //preprae cookie to send
266     std::vector<char> key(cookie, cookie + COOKIE_SIZE);
267
268     return try_catch([&] {
269         //put data into buffer
270         Serialization::Serialize(send, (int)CookieCall::CHECK_UID);
271         Serialization::Serialize(send, key);
272
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);
277             return retval;
278         }
279
280         //receive response from server
281         Deserialization::Deserialize(recv, retval);
282         if (retval == SECURITY_SERVER_API_SUCCESS) {
283             int tmp;
284             Deserialization::Deserialize(recv, tmp);
285             *uid = static_cast<uid_t>(tmp);
286         }
287
288         return retval;
289     });
290 }
291