4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Bumjin Im <bj.im@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License
26 #include <sys/socket.h>
27 #include <sys/types.h>
33 #include "security-server-common.h"
34 #include "security-server-cookie.h"
35 #include "security-server-comm.h"
36 #include "security-server-util.h"
37 #include "security-server.h"
40 /* Get all cookie info response *
43 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
44 * |---------------------------------------------------------------|
45 * | version=0x01 |MessageID=0x52 | Message Length |
46 * |---------------------------------------------------------------|
47 * | return code | tot # of cooks (32bit) |
48 * |---------------------------------------------------------------|
49 * | cont'd... | 1st cmdline_len (32bit) |
50 * |---------------------------------------------------------------|
51 * | cont'd... | 1st permission_len (32bit) |
52 * ----------------------------------------------------------------|
57 * |---------------------------------------------------------------|
59 * |---------------------------------------------------------------|
60 * | 1st cmdline (string) |
61 * |---------------------------------------------------------------|
63 * |---------------------------------------------------------------|
65 * |---------------------------------------------------------------|
67 * |---------------------------------------------------------------|
68 * | 2nd cmdline_len (32bit) |
69 * |---------------------------------------------------------------|
70 * | 2nd permission_len (32bit) |
71 * |---------------------------------------------------------------|
75 * |---------------------------------------------------------------|
76 * | 2nd PID (32 bit) |
77 * |---------------------------------------------------------------|
78 * | 2nd cmdline (string) |
79 * |---------------------------------------------------------------|
81 * |---------------------------------------------------------------|
83 * |---------------------------------------------------------------|
85 * |---------------------------------------------------------------|
91 unsigned char * get_all_cookie_info(cookie_list *list, int *size)
93 cookie_list *current = list;
94 int ptr, total_num, total_size, tempnum, i;
95 unsigned char *buf = NULL, *tempptr = NULL;
98 total_size = sizeof(hdr) + sizeof(int);
100 buf = malloc(total_size); /* header size */
101 ptr = sizeof(hdr) + sizeof(int);
102 total_num = 0; /* Total # of cookies initial value */
104 while(current != NULL)
106 current = garbage_collection(current);
111 total_size += sizeof(int) + sizeof(int) + SECURITY_SERVER_COOKIE_LEN + sizeof(int) + current->path_len + (current->permission_len * sizeof(int));
112 tempptr = realloc(buf, total_size);
115 SEC_SVR_DBG("%s", "Out of memory");
120 tempnum = current->path_len;
121 memcpy(buf+ptr, &tempnum, sizeof(int));
123 tempnum = current->permission_len;
124 memcpy(buf+ptr, &tempnum, sizeof(int));
126 memcpy(buf+ptr, current->cookie, SECURITY_SERVER_COOKIE_LEN);
127 ptr += SECURITY_SERVER_COOKIE_LEN;
128 tempnum = current->pid;
129 memcpy(buf+ptr, &tempnum, sizeof(int));
131 memcpy(buf+ptr, current->path, current->path_len);
132 ptr += current->path_len;
134 for(i=0;i<current->permission_len;i++)
136 tempnum = current->permissions[i];
137 memcpy(buf+ptr, &tempnum, sizeof(int));
140 current = current->next;
143 if(total_size > 65530)
145 SEC_SVR_DBG("Packet too big. message length overflow: %d", total_size);
150 hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
151 hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_RESPONSE;
152 hdr.basic_hdr.msg_len =(unsigned short)( total_size - sizeof(hdr));
153 hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
154 memcpy(buf, &hdr, sizeof(hdr));
156 memcpy(buf + sizeof(hdr), &tempnum, sizeof(int));
161 int send_all_cookie_info(const unsigned char *buf, int size, int sockfd)
165 ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
166 if(ret == SECURITY_SERVER_ERROR_POLL)
168 SEC_SVR_DBG("%s", "poll() error");
169 return SECURITY_SERVER_ERROR_SEND_FAILED;
171 if(ret == SECURITY_SERVER_ERROR_TIMEOUT)
173 SEC_SVR_DBG("%s", "poll() timeout");
174 return SECURITY_SERVER_ERROR_SEND_FAILED;
178 ret = write(sockfd, buf, size);
181 return SECURITY_SERVER_ERROR_SEND_FAILED;
182 return SECURITY_SERVER_SUCCESS;
185 /* Get one cookie info response *
188 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
189 * |---------------------------------------------------------------|
190 * | version=0x01 |MessageID=0x54 | Message Length |
191 * |---------------------------------------------------------------|
192 * | return code | cmdline_len (32bit)t) |
193 * |---------------------------------------------------------------|
194 * | cont'd... | permission_len (32bit) |
195 * ----------------------------------------------------------------|
197 * |---------------- |
200 * |---------------------------------------------------------------|
202 * |---------------------------------------------------------------|
203 * | cmdline (string) |
204 * |---------------------------------------------------------------|
206 * |---------------------------------------------------------------|
208 * |---------------------------------------------------------------|
210 * |---------------------------------------------------------------|
212 int send_one_cookie_info(const cookie_list *list, int sockfd)
214 unsigned char *buf = NULL;
216 int total_size, ptr = 0, tempnum, ret, i;
218 total_size = sizeof(hdr) + sizeof(int) + sizeof(int) + SECURITY_SERVER_COOKIE_LEN + sizeof(int) + list->path_len + (list->permission_len * sizeof(int));
219 buf = malloc(total_size);
222 SEC_SVR_DBG("%s", "Out of memory");
223 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
226 hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
227 hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE;
228 hdr.basic_hdr.msg_len =sizeof(int) + sizeof(int) + SECURITY_SERVER_COOKIE_LEN + sizeof(int) + list->path_len + (list->permission_len * sizeof(int));
229 hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
230 memcpy(buf, &hdr, sizeof(hdr));
233 tempnum = list->path_len;
234 memcpy(buf+ptr, &tempnum, sizeof(int));
236 tempnum = list->permission_len;
237 memcpy(buf+ptr, &tempnum, sizeof(int));
239 memcpy(buf+ptr, list->cookie, SECURITY_SERVER_COOKIE_LEN);
240 ptr += SECURITY_SERVER_COOKIE_LEN;
242 memcpy(buf+ptr, &tempnum, sizeof(int));
244 memcpy(buf+ptr, list->path, list->path_len);
245 ptr += list->path_len;
247 for(i=0;i<list->permission_len;i++)
249 tempnum = list->permissions[i];
250 memcpy(buf+ptr, &tempnum, sizeof(int));
254 ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
255 if(ret == SECURITY_SERVER_ERROR_POLL)
257 SEC_SVR_DBG("%s", "poll() error");
259 return SECURITY_SERVER_ERROR_SEND_FAILED;
261 if(ret == SECURITY_SERVER_ERROR_TIMEOUT)
263 SEC_SVR_DBG("%s", "poll() timeout");
265 return SECURITY_SERVER_ERROR_SEND_FAILED;
269 ret = write(sockfd, buf, total_size);
272 return SECURITY_SERVER_ERROR_SEND_FAILED;
273 return SECURITY_SERVER_SUCCESS;
276 int util_process_all_cookie(int sockfd, cookie_list* list)
278 unsigned char *buf = NULL;
280 buf = get_all_cookie_info(list, &ret);
283 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
286 ret = send_all_cookie_info(buf, ret, sockfd);
292 int util_process_cookie_from_pid(int sockfd, cookie_list* list)
295 cookie_list *result = NULL;
297 ret = read(sockfd, &pid, sizeof(int));
298 if(ret < sizeof(int))
300 SEC_SVR_DBG("Received cookie size is too small: %d", ret);
301 return SECURITY_SERVER_ERROR_RECV_FAILED;
305 SEC_SVR_DBG("%s", "ERROR: Default cookie is not allowed to be retrieved");
306 ret = send_generic_response(sockfd, SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE,
307 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
308 if(ret != SECURITY_SERVER_SUCCESS)
310 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", ret);
313 result = search_cookie_from_pid(list, pid);
316 ret = send_generic_response(sockfd, SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE,
317 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
318 if(ret != SECURITY_SERVER_SUCCESS)
320 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", ret);
325 ret = send_one_cookie_info(result, sockfd);
326 if(ret != SECURITY_SERVER_SUCCESS)
328 SEC_SVR_DBG("ERROR: Cannot send cookie info response: %d", ret);
335 int util_process_cookie_from_cookie(int sockfd, cookie_list* list)
337 unsigned char cookie[SECURITY_SERVER_COOKIE_LEN];
339 int privileges[] = { 0 }; //only one privilege to check - root
340 cookie_list *result = NULL;
342 ret = read(sockfd, cookie, SECURITY_SERVER_COOKIE_LEN);
343 if(ret < SECURITY_SERVER_COOKIE_LEN)
345 SEC_SVR_DBG("Received cookie size is too small: %d", ret);
346 return SECURITY_SERVER_ERROR_RECV_FAILED;
348 result = search_cookie(list, cookie, privileges, 1);
351 ret = send_generic_response(sockfd, SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE,
352 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
353 if(ret != SECURITY_SERVER_SUCCESS)
355 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", ret);
360 ret = send_one_cookie_info(result, sockfd);
361 if(ret != SECURITY_SERVER_SUCCESS)
363 SEC_SVR_DBG("ERROR: Cannot send cookie info response: %d", ret);