upload tizen1.0 source
[framework/security/security-server.git] / src / util / security-server-util.c
1 /*
2  *  security-server
3  *
4  *  Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  *  Contact: Bumjin Im <bj.im@samsung.com>
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  *
20  */
21
22 #include <poll.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <sys/un.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include "security-server.h"
33 #include "security-server-common.h"
34 #include "security-server-util.h"
35 #include "security-server-comm.h"
36
37 #define TOTAL_PATH_MAX 256
38
39 #define mszBase64Table  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
40 #define BASE64_PAD      '='
41
42
43
44  void printusage(char *cmdline)
45 {
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);
59 }
60
61 void printstr(const unsigned char *data, int size)
62 {
63         int i;
64         for(i=0;i<size;i++)
65         {
66                 printf("%c", data[i]);
67         }
68         printf("\n");
69 }
70
71 void printperm(const unsigned char *data, int num)
72 {
73         int i, ptr, tempnum;
74         for(i=0, ptr=0;i<num;i++)
75         {
76                 memcpy(&tempnum, data+ptr, sizeof(int));
77                 printf("%d, ", tempnum);
78                 ptr+= sizeof(int);
79                 if(i % 6 == 0 && i != 0)
80                         printf("\n");
81         }
82         printf("\n");
83 }
84
85 /* Send all cookie information request packet to security server *
86  * 
87  * Message format
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  * |---------------------------------------------------------------|
92  */
93 int send_all_cookie_info_request(int sockfd)
94 {
95
96         basic_header hdr;
97         int retval;
98
99         /* Assemble header */
100         hdr.version = SECURITY_SERVER_MSG_VERSION;
101         hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_REQUEST;
102         hdr.msg_len = 0;
103
104         /* Check poll */
105         retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
106         if(retval == SECURITY_SERVER_ERROR_POLL)
107         {
108                 printf("Error: %s\n", "poll() error");
109                 return SECURITY_SERVER_ERROR_SEND_FAILED;
110         }
111         if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
112         {
113                 printf("Error: %s\n", "poll() timeout");
114                 return SECURITY_SERVER_ERROR_SEND_FAILED;
115         }
116
117         /* Send to server */
118         retval = write(sockfd, &hdr, sizeof(hdr));
119         if(retval < sizeof(hdr))
120         {
121                 /* Write error */
122                 printf("Error on write(): %d\n", retval);
123                 return SECURITY_SERVER_ERROR_SEND_FAILED;
124         }
125         return SECURITY_SERVER_SUCCESS; 
126 }
127
128 int recv_all_cookie_info(int sockfd)
129 {
130         int retval, total_cookie, ptr = 0, i, cmdline_len, perm_len, recved_pid;
131         response_header hdr;
132         unsigned char *buf = NULL;
133
134         /* Check poll */
135         retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
136         if(retval == SECURITY_SERVER_ERROR_POLL)
137         {
138                 printf("Error: %s\n", "poll() error");
139                 return SECURITY_SERVER_ERROR_RECV_FAILED;
140         }
141         if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
142         {
143                 printf("Error: %s\n", "poll() timeout");
144                 return SECURITY_SERVER_ERROR_RECV_FAILED;
145         }
146
147         /* Receive response */
148         retval = read(sockfd, &hdr, sizeof(response_header));
149         if(retval < sizeof(hdr) )
150         {
151                 /* Error on socket */
152                 printf("Error: Receive failed %d\n", retval);
153                 return  SECURITY_SERVER_ERROR_RECV_FAILED;
154         }
155
156         if(hdr.return_code != SECURITY_SERVER_RETURN_CODE_SUCCESS)
157         {
158                 printf("Error: response error: %d\n", hdr.return_code);
159                 return return_code_to_error_code(hdr.return_code);
160         }
161
162         if(hdr.basic_hdr.msg_id != SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_RESPONSE)
163         {
164                 printf("Error: response error: different msg type %d\n", hdr.basic_hdr.msg_id );
165                 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
166         }
167
168         buf = malloc(hdr.basic_hdr.msg_len);
169         if(buf == NULL)
170         {
171                 printf("Error: Out of memory\n");
172                 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
173         }
174
175         retval = read(sockfd, buf, hdr.basic_hdr.msg_len);
176         if(retval < hdr.basic_hdr.msg_len)
177         {
178                 printf("Error: receiving too small amount. %d, %d\n", retval,  hdr.basic_hdr.msg_len);
179                 printhex(buf, retval);
180                 if(buf != NULL)
181                         free(buf);
182                 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
183         }
184
185         memcpy(&total_cookie, buf, sizeof(int));
186         if(total_cookie == 0)
187         {
188                 printf("There is no cookie available\n");
189                 if(buf != NULL)
190                         free(buf);
191                 return SECURITY_SERVER_SUCCESS;
192         }
193         ptr = sizeof(int);
194         printf("--------------------------------\n");
195         for(i=0;i<total_cookie;i++)
196         {
197                 printf("%dth cookie:\n", i+1);
198                 memcpy(&cmdline_len, buf+ptr, sizeof(int));
199                 ptr += sizeof(int);
200                 memcpy(&perm_len, buf+ptr, sizeof(int));
201                 ptr+= sizeof(int);
202
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));
207                 ptr+= sizeof(int);
208                 if(recved_pid == 0)
209                 {
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");
213                 }
214                 else
215                 {
216                         printf("PID: %d\n", recved_pid);
217
218                         printf("%s\n", "cmdline:");
219                         printstr(buf + ptr, cmdline_len);
220                         ptr += cmdline_len;
221
222                         printf("%s\n", "Permissions (gids):");
223                         printperm(buf + ptr, perm_len);
224                         ptr += (perm_len * sizeof(int));
225                 }
226                 printf("--------------------------------\n");
227         }
228         if(buf != NULL)
229                 free(buf);
230         return SECURITY_SERVER_SUCCESS;
231 }
232
233 /* Send cookie information request from cookie packet to security server *
234  * 
235  * Message format
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  * |---------------------------------------------------------------|
240  * |                                                               |
241  * |                         cookie                                |
242  * |                                                               |
243  * |---------------------------------------------------------------|
244  */
245 int send_cookie_info_request_from_cookie(int sockfd, const unsigned char *cookie)
246 {
247
248         basic_header hdr;
249         int retval;
250         int size = sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN;
251         unsigned char buf[size];
252
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;
257
258         memcpy(buf, &hdr, sizeof(hdr));
259         memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
260
261         /* Check poll */
262         retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
263         if(retval == SECURITY_SERVER_ERROR_POLL)
264         {
265                 printf("Error: %s\n", "poll() error");
266                 return SECURITY_SERVER_ERROR_SEND_FAILED;
267         }
268         if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
269         {
270                 printf("Error: %s\n", "poll() timeout");
271                 return SECURITY_SERVER_ERROR_SEND_FAILED;
272         }
273
274         /* Send to server */
275         retval = write(sockfd, buf, size);
276         if(retval < size)
277         {
278                 /* Write error */
279                 printf("Error on write(): %d\n", retval);
280                 return SECURITY_SERVER_ERROR_SEND_FAILED;
281         }
282         return SECURITY_SERVER_SUCCESS; 
283 }
284
285 /* Send cookie information request from pid packet to security server *
286  * 
287  * Message format
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  * |---------------------------------------------------------------|
292  * |                            pid                                |
293  * |---------------------------------------------------------------|
294  */
295 int send_cookie_info_request_from_pid(int sockfd, int pid)
296 {
297         basic_header hdr;
298         int retval;
299         int size = sizeof(hdr) + sizeof(int);
300         unsigned char buf[size];
301
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));
308
309         /* Check poll */
310         retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
311         if(retval == SECURITY_SERVER_ERROR_POLL)
312         {
313                 printf("Error: %s\n", "poll() error");
314                 return SECURITY_SERVER_ERROR_SEND_FAILED;
315         }
316         if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
317         {
318                 printf("Error: %s\n", "poll() timeout");
319                 return SECURITY_SERVER_ERROR_SEND_FAILED;
320         }
321
322         /* Send to server */
323         retval = write(sockfd, buf, size);
324         if(retval < size)
325         {
326                 /* Write error */
327                 printf("Error on write(): %d\n", retval);
328                 return SECURITY_SERVER_ERROR_SEND_FAILED;
329         }
330         return SECURITY_SERVER_SUCCESS; 
331 }
332
333 int recv_cookie_info_response(sockfd)
334 {
335         unsigned char *buf = NULL;
336         int retval, cmdline_len, perm_len, recved_pid, ptr = 0;
337         response_header hdr;
338
339         /* Check poll */
340         retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
341         if(retval == SECURITY_SERVER_ERROR_POLL)
342         {
343                 printf("Error: %s\n", "poll() error");
344                 return SECURITY_SERVER_ERROR_RECV_FAILED;
345         }
346         if(retval == SECURITY_SERVER_ERROR_TIMEOUT)
347         {
348                 printf("Error: %s\n", "poll() timeout");
349                 return SECURITY_SERVER_ERROR_RECV_FAILED;
350         }
351
352         /* Receive response */
353         retval = read(sockfd, &hdr, sizeof(response_header));
354         if(retval < sizeof(hdr) )
355         {
356                 /* Error on socket */
357                 printf("Error: Receive failed %d\n", retval);
358                 return  SECURITY_SERVER_ERROR_RECV_FAILED;
359         }
360
361         if(hdr.return_code != SECURITY_SERVER_RETURN_CODE_SUCCESS)
362         {
363                 printf("Error: response error: %d\n", hdr.return_code);
364                 return return_code_to_error_code(hdr.return_code);
365         }
366
367         if(hdr.basic_hdr.msg_id != SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_RESPONSE)
368         {
369                 printf("Error: response error: different msg type %d\n" ,hdr.basic_hdr.msg_id);
370                 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
371         }
372
373         buf = malloc(hdr.basic_hdr.msg_len);
374         if(buf == NULL)
375         {
376                 printf("Error: Out of memory\n");
377                 return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
378         }
379
380         retval = read(sockfd, buf, hdr.basic_hdr.msg_len);
381         if(retval < hdr.basic_hdr.msg_len)
382         {
383                 printf("Error: receiving too small amount. %d, %d\n", retval,  hdr.basic_hdr.msg_len);
384                 printhex(buf, retval);
385                 if(buf != NULL)
386                         free(buf);
387                 return SECURITY_SERVER_ERROR_BAD_RESPONSE;
388         }
389
390         memcpy(&cmdline_len, buf+ptr, sizeof(int));
391         ptr += sizeof(int);
392         memcpy(&perm_len, buf+ptr, sizeof(int));
393         ptr+= sizeof(int);
394
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));
399         ptr+= sizeof(int);
400         if(recved_pid == 0)
401         {
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");
405         }
406         else
407         {
408                 printf("PID: %d\n", recved_pid);
409
410                 printf("%s\n", "cmdline:");
411                 printstr(buf + ptr, cmdline_len);
412                 ptr += cmdline_len;
413
414                 printf("%s\n", "Permissions (gids):");
415                 printperm(buf + ptr, perm_len);
416         }
417
418         free(buf);
419
420         return SECURITY_SERVER_SUCCESS;
421 }
422
423 void util_send_all_cookie_info_request(void)
424 {
425         int sockfd = -1, retval;
426
427         retval = connect_to_server(&sockfd);
428         if(retval != SECURITY_SERVER_SUCCESS)
429         {
430                 /* Error on socket */
431                 printf("Error: %s\n", "connection failed");
432                 goto error;
433         }
434
435         /* make request packet */
436         retval = send_all_cookie_info_request(sockfd);
437         if(retval != SECURITY_SERVER_SUCCESS)
438         {
439                 /* Error on socket */
440                 SEC_SVR_DBG("Error: send request failed: %d", retval);
441                 goto error;
442         }
443         retval = recv_all_cookie_info(sockfd);
444         if(retval <0)
445         {
446                 printf("Error: Error receiving cookie list: %d\n", retval);
447                 goto error;
448         }
449
450 error:
451         if(sockfd > 0)
452         {
453                 close(sockfd);
454         }
455         return;
456 }
457
458 void util_read_cookie_from_bin_file(unsigned char *cookie, const char *path)
459 {
460         char total_path[TOTAL_PATH_MAX] = {0, };
461         FILE *fp = NULL;
462         int ret;
463
464         if(path[0] == '/' || (path[0] == '.' && path[1] == '/'))
465         {
466                 /* Using absolute path */
467                 strncpy(total_path, path, TOTAL_PATH_MAX);
468         }
469         else
470         {
471                 if (getcwd(total_path, TOTAL_PATH_MAX) == NULL)
472                 {
473                         printf("Cannot open cookie file\n");
474                         exit(1);
475                 }
476                 snprintf(total_path, TOTAL_PATH_MAX, "%s/%s", total_path, path);
477         }
478
479         fp = fopen(total_path, "rb");
480         if(fp == NULL)
481         {
482                 printf("Cannot open cookie file\n");
483                 exit(1);
484         }
485
486         ret = fread(cookie, 1, SECURITY_SERVER_COOKIE_LEN, fp);
487         if(ret < SECURITY_SERVER_COOKIE_LEN)
488         {
489                 printf("Cannot read cookie file: %d\n", ret);
490                 fclose(fp);
491                 exit(1);
492         }
493
494         fclose(fp);
495         return;
496 }
497
498 void util_send_cookie_info_request_from_cookie(unsigned char *cookie)
499 {
500         int sockfd = -1, retval;
501
502         retval = connect_to_server(&sockfd);
503         if(retval != SECURITY_SERVER_SUCCESS)
504         {
505                 /* Error on socket */
506                 printf("Error: %s\n", "connection failed");
507                 goto error;
508         }
509
510         /* make request packet */
511         retval = send_cookie_info_request_from_cookie(sockfd, cookie);
512         if(retval != SECURITY_SERVER_SUCCESS)
513         {
514                 /* Error on socket */
515                 SEC_SVR_DBG("Error: send request failed: %d", retval);
516                 goto error;
517         }
518         retval = recv_cookie_info_response(sockfd);
519         if(retval == SECURITY_SERVER_ERROR_NO_SUCH_COOKIE)
520         {
521                 printf("There is no such cookie available\n");
522                 goto error;
523         }
524         if(retval <0)
525         {
526                 printf("Error: Error receiving cookie info: %d\n", retval);
527                 goto error;
528         }
529
530 error:
531         if(sockfd > 0)
532         {
533                 close(sockfd);
534         }
535         return;
536 }
537
538 unsigned char* util_base64_decode(unsigned char* input, long inputLength, long* outputLength)
539 {
540         unsigned char* pCurIn = input;
541         unsigned char* pCurOut;
542         long iOutCharNum = 0;
543         long lInputLength = inputLength;
544         char buf[4];
545         unsigned char* inCode;
546         unsigned char* output;
547         (*outputLength) = 0;
548         if((input == NULL) || (inputLength <= 0))
549         return NULL;
550
551          /* calculate length of output data */
552         for(; lInputLength > 0; lInputLength--)
553         {
554                 if ((*pCurIn) == BASE64_PAD)
555                 {
556                         (*outputLength) += ((iOutCharNum + 1) >> 1);
557                         if ((iOutCharNum == 2) &&
558                         ((lInputLength == 1) ||
559                         (*(pCurIn + 1) != BASE64_PAD)))
560                         {
561                                 (*outputLength)++;
562                         }
563                         iOutCharNum = 0;
564                         break;
565                 }
566                 inCode = (unsigned char*)strchr(mszBase64Table, *(pCurIn++));
567                 if (!inCode)
568                         continue;
569                 iOutCharNum++;
570                 if (iOutCharNum == 4)
571                 {
572                         (*outputLength) += 3;
573                         iOutCharNum=0;
574                 }
575         }
576         (*outputLength) += ((iOutCharNum + 1)/2);
577
578         /* allocate memory for output data*/
579         output = malloc( *outputLength + 1 );
580         if(NULL == output)
581         {
582                 return NULL;
583         }
584         memset( output, 0, (*outputLength + 1) );
585         pCurOut = output;
586         iOutCharNum = buf[0] = buf[1] = buf[2] = buf[3] = 0;
587
588         /* decode data*/
589         pCurIn = input;
590
591         for(; inputLength>0; inputLength--)
592         {
593                 if ((*pCurIn) == BASE64_PAD)
594                 {
595                         /*end-padding processing*/
596                         if (iOutCharNum == 0)
597                         {
598                                 return output;
599                         }
600                         (*(pCurOut++)) = ((buf[0] & 0x3F) << 2) + ((buf[1] & 0x30) >> 4);
601                         if ((iOutCharNum == 3)||((iOutCharNum == 2) && ((lInputLength == 0) ||
602                         ((*(pCurIn + 1)) != BASE64_PAD))))
603                         {
604                                 (*(pCurOut++)) = ((buf[1] & 0x0F) << 4) + ((buf[2] & 0x3C) >> 2);
605                         }
606                         return output;
607                 }
608                 inCode = (unsigned char*)strchr(mszBase64Table, *(pCurIn++));
609                 if (!inCode)
610                 {
611                         continue;
612                 }
613                 buf[iOutCharNum++] = (char)((unsigned long)inCode - (unsigned long)mszBase64Table);
614                 if (iOutCharNum == 4)
615                 {
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;
620                 }
621         }
622         if (iOutCharNum == 0)
623         {
624                 return output;
625         }
626         (*(pCurOut++)) = ((buf[0] & 0x3F) << 2) + ((buf[1] & 0x30) >> 4);
627         if (iOutCharNum == 3)
628         {
629                 (*(pCurOut++)) = ((buf[1] & 0x0F) << 4) + ((buf[2] & 0x3C) >> 2);
630         }
631         return output;
632 }
633
634 void util_read_cookie_from_base64_string(unsigned char *cookie, const char *encoded_cookie)
635 {
636         unsigned char *decoded_cookie = NULL;
637         int encoded_len, decoded_len;
638         encoded_len = strlen(encoded_cookie);
639
640         decoded_cookie = util_base64_decode((unsigned char *)encoded_cookie, encoded_len, (long *)&decoded_len);
641         if(decoded_len != SECURITY_SERVER_COOKIE_LEN)
642         {
643                 printf("Base64 decode failed: %d\n", decoded_len);
644                 exit(1);
645         }
646
647         if(decoded_cookie == NULL)
648         {
649                 printf("%s", "BASE64 decode failed:\n");
650                 exit(1);
651         }
652
653         memcpy(cookie, decoded_cookie, SECURITY_SERVER_COOKIE_LEN);
654         if(decoded_cookie != NULL)
655                 free(decoded_cookie);
656
657         return;
658 }
659
660 void util_send_cookie_info_request_from_pid(const char *str_pid)
661 {
662         int retval, sockfd, pid;
663
664         if(str_pid == NULL)
665         {
666                 printf("Wrong PID\n");
667                 return;
668         }
669
670         errno = 0;
671         pid = strtoul(str_pid, 0, 10);
672         if (errno != 0)
673         {
674                 SEC_SVR_DBG("cannot change string to integer [%s]", str_pid);
675                 return;
676         }
677
678         retval = connect_to_server(&sockfd);
679         if(retval != SECURITY_SERVER_SUCCESS)
680         {
681                 /* Error on socket */
682                 printf("Error: %s\n", "connection failed");
683                 goto error;
684         }
685
686         /* make request packet */
687         retval = send_cookie_info_request_from_pid(sockfd, pid);
688         if(retval != SECURITY_SERVER_SUCCESS)
689         {
690                 /* Error on socket */
691                 SEC_SVR_DBG("Error: send request failed: %d", retval);
692                 goto error;
693         }
694         retval = recv_cookie_info_response(sockfd);
695         if(retval == SECURITY_SERVER_ERROR_NO_SUCH_COOKIE)
696         {
697                 printf("There is no such cookie available\n");
698                 goto error;
699         }
700         if(retval <0)
701         {
702                 printf("Error: Error receiving cookie info: %d\n", retval);
703                 goto error;
704         }
705
706 error:
707         if(sockfd > 0)
708         {
709                 close(sockfd);
710         }
711         return;
712 }
713
714 int main(int argc, char *argv[])
715 {
716         int ret;
717         unsigned char cookie[20];
718         ret = getuid();
719         if(ret != 0)
720         {
721                 printf("You must be root to test. Current UID: %d\nExiting...\n", ret);
722                 exit(1);
723         }
724         if(argc < 2 || argc > 4)
725         {
726                 printf("Wrong usage: %d\n", argc);
727                 printusage(argv[0]);
728                 exit(1);
729         }
730         if(strcmp(argv[1], "-a") == 0)
731         {
732                 if(argc != 2)
733                 {
734                         printf("Wrong usage: %d\n", argc);
735                         printusage(argv[0]);
736                         exit(1);
737                 }
738                         
739                 util_send_all_cookie_info_request();
740                 exit(0);
741         }
742
743         if(argc < 3)
744         {
745                 printf("Wrong usage: %d\n", argc);
746                 printusage(argv[0]);
747                 exit(1);
748         }
749
750         if(strcmp(argv[1], "-f") == 0)
751         {
752                 util_read_cookie_from_bin_file(cookie, argv[2]);
753                 util_send_cookie_info_request_from_cookie(cookie);
754                 exit(0);
755         }
756
757         if(strcmp(argv[1], "-p") == 0)
758         {
759                 util_send_cookie_info_request_from_pid(argv[2]);
760                 exit(0);
761         }
762
763         if(strcmp(argv[1], "-s") == 0)
764         {
765                 util_read_cookie_from_base64_string(cookie, argv[2]);
766                 util_send_cookie_info_request_from_cookie(cookie);
767                 exit(0);
768         }
769
770         printf("%s", "Wrong usage\n");
771         printusage(argv[0]);
772         exit(1);
773 }