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
25 #include <sys/socket.h>
26 #include <sys/types.h>
32 #include "security-server.h"
33 #include "security-server-common.h"
34 #include "security-server-util.h"
35 #include "security-server-comm.h"
37 #define TOTAL_PATH_MAX 256
39 #define mszBase64Table "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
40 #define BASE64_PAD '='
44 void printusage(char *cmdline)
46 printf("%s\n", "Usage: ");
47 printf("%s [Options]\n", cmdline);
48 printf("%s\n", "[Options]");
49 printf("%s\n", "-a:\tList all active cookies ");
50 printf("%s\n", "-f [filename]:\tList a specific cookie information from file");
51 printf("%s\n", "\tThe file must contain binary form of cookie");
52 printf("%s\n", "-p [pid]:\tList a specific cookie information for a process by PID");
53 printf("%s\n", "-s [base64 encoded cookie]:\tList a specific cookie information for a process by given base64 encoded cookie value");
54 printf("%s\n", "Example:");
55 printf("%s -a\n", cmdline);
56 printf("%s -f /tmp/mycookie.bin\n", cmdline);
57 printf("%s -p 2115\n", cmdline);
58 printf("%s -s asC34fddaxd6NDVDA43GFD345TfCADF==\n", cmdline);
61 void printstr(const unsigned char *data, int size)
66 printf("%c", data[i]);
71 void printperm(const unsigned char *data, int num)
74 for(i=0, ptr=0;i<num;i++)
76 memcpy(&tempnum, data+ptr, sizeof(int));
77 printf("%d, ", tempnum);
79 if(i % 6 == 0 && i != 0)
85 /* Send all cookie information request packet to security server *
88 * 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
89 * |---------------------------------------------------------------|
90 * | version=0x01 |MessageID=0x51 | Message Length = 0 |
91 * |---------------------------------------------------------------|
93 int send_all_cookie_info_request(int sockfd)
100 hdr.version = SECURITY_SERVER_MSG_VERSION;
101 hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_REQUEST;
105 retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
106 if(retval == SECURITY_SERVER_ERROR_POLL)
108 printf("Error: %s\n", "poll() error");
109 return SECURITY_SERVER_ERROR_SEND_FAILED;
111 if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
113 printf("Error: %s\n", "poll() timeout");
114 return SECURITY_SERVER_ERROR_SEND_FAILED;
118 retval = write(sockfd, &hdr, sizeof(hdr));
119 if(retval < sizeof(hdr))
122 printf("Error on write(): %d\n", retval);
123 return SECURITY_SERVER_ERROR_SEND_FAILED;
125 return SECURITY_SERVER_SUCCESS;
128 int recv_all_cookie_info(int sockfd)
130 int retval, total_cookie, ptr = 0, i, cmdline_len, perm_len, recved_pid;
132 unsigned char *buf = NULL;
135 retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
136 if(retval == SECURITY_SERVER_ERROR_POLL)
138 printf("Error: %s\n", "poll() error");
139 return SECURITY_SERVER_ERROR_RECV_FAILED;
141 if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
143 printf("Error: %s\n", "poll() timeout");
144 return SECURITY_SERVER_ERROR_RECV_FAILED;
147 /* Receive response */
148 retval = read(sockfd, &hdr, sizeof(response_header));
149 if(retval < sizeof(hdr) )
151 /* Error on socket */
152 printf("Error: Receive failed %d\n", retval);
153 return SECURITY_SERVER_ERROR_RECV_FAILED;
156 if(hdr.return_code != SECURITY_SERVER_RETURN_CODE_SUCCESS)
158 printf("Error: response error: %d\n", hdr.return_code);
159 return return_code_to_error_code(hdr.return_code);
162 if(hdr.basic_hdr.msg_id != SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_RESPONSE)
164 printf("Error: response error: different msg type %d\n", hdr.basic_hdr.msg_id );
165 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
168 buf = malloc(hdr.basic_hdr.msg_len);
171 printf("Error: Out of memory\n");
172 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
175 retval = read(sockfd, buf, hdr.basic_hdr.msg_len);
176 if(retval < hdr.basic_hdr.msg_len)
178 printf("Error: receiving too small amount. %d, %d\n", retval, hdr.basic_hdr.msg_len);
179 printhex(buf, retval);
182 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
185 memcpy(&total_cookie, buf, sizeof(int));
186 if(total_cookie == 0)
188 printf("There is no cookie available\n");
191 return SECURITY_SERVER_SUCCESS;
194 printf("--------------------------------\n");
195 for(i=0;i<total_cookie;i++)
197 printf("%dth cookie:\n", i+1);
198 memcpy(&cmdline_len, buf+ptr, sizeof(int));
200 memcpy(&perm_len, buf+ptr, sizeof(int));
203 printf("%s\n", "Cookie:");
204 printhex(buf + ptr, SECURITY_SERVER_COOKIE_LEN);
205 ptr += SECURITY_SERVER_COOKIE_LEN;
206 memcpy(&recved_pid, buf+ptr, sizeof(int));
210 printf("PID: %d (default cookie - for all root processes)\n", recved_pid);
211 printf("%s\n", "cmdline: N/A");
212 printf("%s\n", "Permissions (gids): N/A");
216 printf("PID: %d\n", recved_pid);
218 printf("%s\n", "cmdline:");
219 printstr(buf + ptr, cmdline_len);
222 printf("%s\n", "Permissions (gids):");
223 printperm(buf + ptr, perm_len);
224 ptr += (perm_len * sizeof(int));
226 printf("--------------------------------\n");
230 return SECURITY_SERVER_SUCCESS;
233 /* Send cookie information request from cookie packet to security server *
236 * 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
237 * |---------------------------------------------------------------|
238 * | version=0x01 |MessageID=0x55 | Message Length = 20 |
239 * |---------------------------------------------------------------|
243 * |---------------------------------------------------------------|
245 int send_cookie_info_request_from_cookie(int sockfd, const unsigned char *cookie)
250 int size = sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN;
251 unsigned char buf[size];
253 /* Assemble header */
254 hdr.version = SECURITY_SERVER_MSG_VERSION;
255 hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_COOKIE_REQUEST;
256 hdr.msg_len = SECURITY_SERVER_COOKIE_LEN;
258 memcpy(buf, &hdr, sizeof(hdr));
259 memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
262 retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
263 if(retval == SECURITY_SERVER_ERROR_POLL)
265 printf("Error: %s\n", "poll() error");
266 return SECURITY_SERVER_ERROR_SEND_FAILED;
268 if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
270 printf("Error: %s\n", "poll() timeout");
271 return SECURITY_SERVER_ERROR_SEND_FAILED;
275 retval = write(sockfd, buf, size);
279 printf("Error on write(): %d\n", retval);
280 return SECURITY_SERVER_ERROR_SEND_FAILED;
282 return SECURITY_SERVER_SUCCESS;
285 /* Send cookie information request from pid packet to security server *
288 * 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
289 * |---------------------------------------------------------------|
290 * | version=0x01 |MessageID=0x53 | Message Length = 20 |
291 * |---------------------------------------------------------------|
293 * |---------------------------------------------------------------|
295 int send_cookie_info_request_from_pid(int sockfd, int pid)
299 int size = sizeof(hdr) + sizeof(int);
300 unsigned char buf[size];
302 /* Assemble header */
303 hdr.version = SECURITY_SERVER_MSG_VERSION;
304 hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_PID_REQUEST;
305 hdr.msg_len = SECURITY_SERVER_COOKIE_LEN;
306 memcpy(buf, &hdr, sizeof(hdr));
307 memcpy(buf+sizeof(hdr), &pid, sizeof(int));
310 retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
311 if(retval == SECURITY_SERVER_ERROR_POLL)
313 printf("Error: %s\n", "poll() error");
314 return SECURITY_SERVER_ERROR_SEND_FAILED;
316 if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
318 printf("Error: %s\n", "poll() timeout");
319 return SECURITY_SERVER_ERROR_SEND_FAILED;
323 retval = write(sockfd, buf, size);
327 printf("Error on write(): %d\n", retval);
328 return SECURITY_SERVER_ERROR_SEND_FAILED;
330 return SECURITY_SERVER_SUCCESS;
333 int recv_cookie_info_response(sockfd)
335 unsigned char *buf = NULL;
336 int retval, cmdline_len, perm_len, recved_pid, ptr = 0;
340 retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
341 if(retval == SECURITY_SERVER_ERROR_POLL)
343 printf("Error: %s\n", "poll() error");
344 return SECURITY_SERVER_ERROR_RECV_FAILED;
346 if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
348 printf("Error: %s\n", "poll() timeout");
349 return SECURITY_SERVER_ERROR_RECV_FAILED;
352 /* Receive response */
353 retval = read(sockfd, &hdr, sizeof(response_header));
354 if(retval < sizeof(hdr) )
356 /* Error on socket */
357 printf("Error: Receive failed %d\n", retval);
358 return SECURITY_SERVER_ERROR_RECV_FAILED;
361 if(hdr.return_code != SECURITY_SERVER_RETURN_CODE_SUCCESS)
363 printf("Error: response error: %d\n", hdr.return_code);
364 return return_code_to_error_code(hdr.return_code);
367 if(hdr.basic_hdr.msg_id != SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE)
369 printf("Error: response error: different msg type %d\n" ,hdr.basic_hdr.msg_id);
370 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
373 buf = malloc(hdr.basic_hdr.msg_len);
376 printf("Error: Out of memory\n");
377 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
380 retval = read(sockfd, buf, hdr.basic_hdr.msg_len);
381 if(retval < hdr.basic_hdr.msg_len)
383 printf("Error: receiving too small amount. %d, %d\n", retval, hdr.basic_hdr.msg_len);
384 printhex(buf, retval);
387 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
390 memcpy(&cmdline_len, buf+ptr, sizeof(int));
392 memcpy(&perm_len, buf+ptr, sizeof(int));
395 printf("%s\n", "Cookie:");
396 printhex(buf + ptr, SECURITY_SERVER_COOKIE_LEN);
397 ptr += SECURITY_SERVER_COOKIE_LEN;
398 memcpy(&recved_pid, buf+ptr, sizeof(int));
402 printf("PID: %d (default cookie - for all root processes)\n", recved_pid);
403 printf("%s\n", "cmdline: N/A");
404 printf("%s\n", "Permissions (gids): N/A");
408 printf("PID: %d\n", recved_pid);
410 printf("%s\n", "cmdline:");
411 printstr(buf + ptr, cmdline_len);
414 printf("%s\n", "Permissions (gids):");
415 printperm(buf + ptr, perm_len);
420 return SECURITY_SERVER_SUCCESS;
423 void util_send_all_cookie_info_request(void)
425 int sockfd = -1, retval;
427 retval = connect_to_server(&sockfd);
428 if(retval != SECURITY_SERVER_SUCCESS)
430 /* Error on socket */
431 printf("Error: %s\n", "connection failed");
435 /* make request packet */
436 retval = send_all_cookie_info_request(sockfd);
437 if(retval != SECURITY_SERVER_SUCCESS)
439 /* Error on socket */
440 SEC_SVR_DBG("Error: send request failed: %d", retval);
443 retval = recv_all_cookie_info(sockfd);
446 printf("Error: Error receiving cookie list: %d\n", retval);
458 void util_read_cookie_from_bin_file(unsigned char *cookie, const char *path)
460 char total_path[TOTAL_PATH_MAX] = {0, };
464 if(path[0] == '/' || (path[0] == '.' && path[1] == '/'))
466 /* Using absolute path */
467 strncpy(total_path, path, TOTAL_PATH_MAX);
471 if (getcwd(total_path, TOTAL_PATH_MAX) == NULL)
473 printf("Cannot open cookie file\n");
476 snprintf(total_path, TOTAL_PATH_MAX, "%s/%s", total_path, path);
479 fp = fopen(total_path, "rb");
482 printf("Cannot open cookie file\n");
486 ret = fread(cookie, 1, SECURITY_SERVER_COOKIE_LEN, fp);
487 if(ret < SECURITY_SERVER_COOKIE_LEN)
489 printf("Cannot read cookie file: %d\n", ret);
498 void util_send_cookie_info_request_from_cookie(unsigned char *cookie)
500 int sockfd = -1, retval;
502 retval = connect_to_server(&sockfd);
503 if(retval != SECURITY_SERVER_SUCCESS)
505 /* Error on socket */
506 printf("Error: %s\n", "connection failed");
510 /* make request packet */
511 retval = send_cookie_info_request_from_cookie(sockfd, cookie);
512 if(retval != SECURITY_SERVER_SUCCESS)
514 /* Error on socket */
515 SEC_SVR_DBG("Error: send request failed: %d", retval);
518 retval = recv_cookie_info_response(sockfd);
519 if(retval == SECURITY_SERVER_ERROR_NO_SUCH_COOKIE)
521 printf("There is no such cookie available\n");
526 printf("Error: Error receiving cookie info: %d\n", retval);
538 unsigned char* util_base64_decode(unsigned char* input, long inputLength, long* outputLength)
540 unsigned char* pCurIn = input;
541 unsigned char* pCurOut;
542 long iOutCharNum = 0;
543 long lInputLength = inputLength;
545 unsigned char* inCode;
546 unsigned char* output;
548 if((input == NULL) || (inputLength <= 0))
551 /* calculate length of output data */
552 for(; lInputLength > 0; lInputLength--)
554 if ((*pCurIn) == BASE64_PAD)
556 (*outputLength) += ((iOutCharNum + 1) >> 1);
557 if ((iOutCharNum == 2) &&
558 ((lInputLength == 1) ||
559 (*(pCurIn + 1) != BASE64_PAD)))
566 inCode = (unsigned char*)strchr(mszBase64Table, *(pCurIn++));
570 if (iOutCharNum == 4)
572 (*outputLength) += 3;
576 (*outputLength) += ((iOutCharNum + 1)/2);
578 /* allocate memory for output data*/
579 output = malloc( *outputLength + 1 );
584 memset( output, 0, (*outputLength + 1) );
586 iOutCharNum = buf[0] = buf[1] = buf[2] = buf[3] = 0;
591 for(; inputLength>0; inputLength--)
593 if ((*pCurIn) == BASE64_PAD)
595 /*end-padding processing*/
596 if (iOutCharNum == 0)
600 (*(pCurOut++)) = ((buf[0] & 0x3F) << 2) + ((buf[1] & 0x30) >> 4);
601 if ((iOutCharNum == 3)||((iOutCharNum == 2) && ((lInputLength == 0) ||
602 ((*(pCurIn + 1)) != BASE64_PAD))))
604 (*(pCurOut++)) = ((buf[1] & 0x0F) << 4) + ((buf[2] & 0x3C) >> 2);
608 inCode = (unsigned char*)strchr(mszBase64Table, *(pCurIn++));
613 buf[iOutCharNum++] = (char)((unsigned long)inCode - (unsigned long)mszBase64Table);
614 if (iOutCharNum == 4)
616 *(pCurOut++) = ((buf[0] & 0x3F) << 2) + ((buf[1] & 0x30) >> 4);
617 *(pCurOut++) = ((buf[1] & 0x0F) << 4) + ((buf[2] & 0x3C) >> 2);
618 *(pCurOut++) = ((buf[2] & 0x03) << 6) + (buf[3] & 0x3F);
619 iOutCharNum = buf[0] = buf[1] = buf[2] = buf[3] = 0;
622 if (iOutCharNum == 0)
626 (*(pCurOut++)) = ((buf[0] & 0x3F) << 2) + ((buf[1] & 0x30) >> 4);
627 if (iOutCharNum == 3)
629 (*(pCurOut++)) = ((buf[1] & 0x0F) << 4) + ((buf[2] & 0x3C) >> 2);
634 void util_read_cookie_from_base64_string(unsigned char *cookie, const char *encoded_cookie)
636 unsigned char *decoded_cookie = NULL;
637 int encoded_len, decoded_len;
638 encoded_len = strlen(encoded_cookie);
640 decoded_cookie = util_base64_decode((unsigned char *)encoded_cookie, encoded_len, (long *)&decoded_len);
641 if(decoded_len != SECURITY_SERVER_COOKIE_LEN)
643 printf("Base64 decode failed: %d\n", decoded_len);
647 if(decoded_cookie == NULL)
649 printf("%s", "BASE64 decode failed:\n");
653 memcpy(cookie, decoded_cookie, SECURITY_SERVER_COOKIE_LEN);
654 if(decoded_cookie != NULL)
655 free(decoded_cookie);
660 void util_send_cookie_info_request_from_pid(const char *str_pid)
662 int retval, sockfd, pid;
666 printf("Wrong PID\n");
671 pid = strtoul(str_pid, 0, 10);
674 SEC_SVR_DBG("cannot change string to integer [%s]", str_pid);
678 retval = connect_to_server(&sockfd);
679 if(retval != SECURITY_SERVER_SUCCESS)
681 /* Error on socket */
682 printf("Error: %s\n", "connection failed");
686 /* make request packet */
687 retval = send_cookie_info_request_from_pid(sockfd, pid);
688 if(retval != SECURITY_SERVER_SUCCESS)
690 /* Error on socket */
691 SEC_SVR_DBG("Error: send request failed: %d", retval);
694 retval = recv_cookie_info_response(sockfd);
695 if(retval == SECURITY_SERVER_ERROR_NO_SUCH_COOKIE)
697 printf("There is no such cookie available\n");
702 printf("Error: Error receiving cookie info: %d\n", retval);
714 int main(int argc, char *argv[])
717 unsigned char cookie[20];
721 printf("You must be root to test. Current UID: %d\nExiting...\n", ret);
724 if(argc < 2 || argc > 4)
726 printf("Wrong usage: %d\n", argc);
730 if(strcmp(argv[1], "-a") == 0)
734 printf("Wrong usage: %d\n", argc);
739 util_send_all_cookie_info_request();
745 printf("Wrong usage: %d\n", argc);
750 if(strcmp(argv[1], "-f") == 0)
752 util_read_cookie_from_bin_file(cookie, argv[2]);
753 util_send_cookie_info_request_from_cookie(cookie);
757 if(strcmp(argv[1], "-p") == 0)
759 util_send_cookie_info_request_from_pid(argv[2]);
763 if(strcmp(argv[1], "-s") == 0)
765 util_read_cookie_from_base64_string(cookie, argv[2]);
766 util_send_cookie_info_request_from_cookie(cookie);
770 printf("%s", "Wrong usage\n");