merge with master
[framework/security/security-server.git] / src / server / security-server-main.c
1 /*
2  * security-server
3  *
4  *  Copyright (c) 2000 - 2011 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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <pthread.h>
31 #include <limits.h>
32 #include <fcntl.h>
33 #include <sys/smack.h>
34
35 #include "security-server-cookie.h"
36 #include "security-server-common.h"
37 #include "security-server-password.h"
38 #include "security-server-comm.h"
39
40 /* Set cookie as a global variable */
41 cookie_list *c_list;
42 pthread_mutex_t cookie_mutex;
43 int thread_status[SECURITY_SERVER_NUM_THREADS];
44 struct security_server_thread_param {
45         int client_sockfd;
46         int server_sockfd;
47         int thread_status;
48 };
49
50 /************************************************************************************************/
51 /* Just for test. This code must be removed on release */
52 #include "security-server-util.h"
53 /************************************************************************************************/
54
55 #if 0
56 void printhex(unsigned char *data, int size)
57 {
58         int i;
59         for(i=0;i<size;i++)
60         {
61                 if(data[i] < 0xF)
62                         printf("0");
63
64                 printf("%X ", data[i]);
65                 if(((i+1) % 16) == 0 && i != 0)
66                         printf("\n");
67         }
68         printf("\n");
69 }
70
71 void print_cookie(cookie_list *list)
72 {
73         int i;
74         printf("%s", "cookie:\n");
75         printhex(list->cookie, SECURITY_SERVER_COOKIE_LEN);
76         printf("path_len: %d\n", list->path_len);
77         printf("permission_len: %d\n", list->permission_len);
78         printf("PID: %d\n", list->pid);
79         printf("path: %s\n", list->path);
80         printf("%s", "permissions: ");
81         for(i=0;i<list->permission_len;i++)
82         {
83                 printf("%d ", list->permissions[i]);
84         }
85         printf("%s", "\n");
86         printf("prev: %p\n", list->prev);
87         printf("next: %p\n", list->next);
88 }
89 #endif
90
91 /* Object name is actually name of a Group ID *
92  * This function opens /etc/group file and search group ID and
93  * returns the string */
94 int search_object_name(int gid, char *obj, int obj_size)
95 {
96         FILE *fp = NULL;
97         char *linebuf = NULL, *token = NULL, *token2, *tempstr = NULL;
98         int ret = 0, tmp_gid, bufsize;
99         fp = fopen("/etc/group", "r");
100         if(fp == NULL)
101         {
102                 /* cannot open /etc/group */
103                 SEC_SVR_DBG("%s", "Cannot open /etc/group");
104                 return SECURITY_SERVER_ERROR_FILE_OPERATION;
105         }
106
107         linebuf = malloc(128);
108         bufsize = 128;
109         if(linebuf == NULL)
110         {
111                 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
112                 SEC_SVR_DBG("%s", "cannot malloc()");
113                 goto error;
114         }
115
116         bzero(linebuf, bufsize);
117         ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
118         while(fgets(linebuf, bufsize, fp) != NULL)
119         {
120                 while(linebuf[bufsize -2] != 0)
121                 {
122                         linebuf[bufsize -1] = (char) fgetc(fp);
123                         tempstr = realloc(linebuf, bufsize + 128);
124                         if(tempstr == NULL)
125                         {
126                                 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
127                                 goto error;
128                         }
129                         linebuf = tempstr;
130                         bzero(linebuf + bufsize, 128);
131                         fgets(linebuf + bufsize, 128, fp);
132                         bufsize += 128;
133                 }
134
135                 token = strtok(linebuf, ":");   /* group name */
136                 if(token == NULL)
137                 {
138                         SEC_SVR_DBG("/etc/group is not valid. cannot find gid: [%s]", linebuf);
139                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
140                         goto error;
141                 }
142                 token2 = strtok(NULL, ":");     /* group password */
143                 if(token2== NULL)
144                 {
145                         SEC_SVR_DBG("/etc/group is not valid. cannot find gid: [%s]", linebuf);
146                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
147                         goto error;
148                 }
149                 token2 = strtok(NULL, ":");     /* gid */
150                 if(token2 == NULL)
151                 {
152                         SEC_SVR_DBG("/etc/group is not valid. cannot find gid: [%s]", linebuf);
153                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
154                         goto error;
155                 }
156
157                 errno = 0;
158                 tmp_gid = strtoul(token2, 0, 10);
159                 if (errno != 0)
160                 {
161                         SEC_SVR_DBG("cannot change string to integer [%s]", token2);
162                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
163                         goto error;
164                 }
165
166                 if(tmp_gid == gid)
167                 {
168                         /* We found it */
169                         if(strlen(token) > obj_size)
170                         {
171                                 ret = SECURITY_SERVER_ERROR_BUFFER_TOO_SMALL;
172                                 SEC_SVR_DBG("buffer is too small. %d --> %d", obj_size, strlen(token));
173                                 goto error;
174                         }
175                         strncpy(obj, token, strlen(token));
176                         obj[strlen(token)] = 0;
177                         ret = SECURITY_SERVER_SUCCESS;
178                         break;
179                 }
180                 bzero(linebuf, bufsize);
181         }
182
183 error:
184         if(linebuf != NULL)
185                 free(linebuf);
186         if(fp != NULL)
187                 fclose(fp);
188         return ret;
189 }
190
191 /* Search GID from group name *
192  * This function opens /etc/group and search group name by given gid */
193 int search_gid(const char *obj)
194 {
195         FILE *fp = NULL;
196         char *linebuf = NULL, *token = NULL, *token2, *tempstr = NULL;
197         int ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT, tmp_gid, bufsize;
198
199         SEC_SVR_DBG("Searching for object %s", obj);
200
201         fp = fopen("/etc/group", "r");
202         if(fp == NULL)
203         {
204                 /* cannot open /etc/group */
205                 SEC_SVR_DBG("%s", "cannot open /etc/group");
206                 return SECURITY_SERVER_ERROR_FILE_OPERATION;
207         }
208
209         linebuf = malloc(128);
210         bufsize = 128;
211         if(linebuf == NULL)
212         {
213                 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
214                 SEC_SVR_DBG("%s", "Out Of Memory");
215                 goto error;
216         }
217
218         bzero(linebuf, bufsize);
219         while(fgets(linebuf, bufsize, fp) != NULL)
220         {
221                 while(linebuf[bufsize -2] != 0 )
222                 {
223                         linebuf[bufsize -1] = (char) fgetc(fp);
224                         tempstr = realloc(linebuf, bufsize + 128);
225                         if(tempstr == NULL)
226                         {
227                                 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
228                                 goto error;
229                         }
230                         linebuf = tempstr;
231                         bzero(linebuf + bufsize, 128);
232                         fgets(linebuf + bufsize, 128, fp);
233                         bufsize += 128;
234                 }
235
236                 token = strtok(linebuf, ":");   /* group name */
237                 token2 = strtok(NULL, ":");     /* group password */
238                 token2 = strtok(NULL, ":");     /* gid */
239                 if(token2 == NULL)
240                 {
241                         SEC_SVR_DBG("/etc/group is not valid. cannot find gid: [%s]", linebuf);
242                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
243                         goto error;
244                 }
245                 errno = 0;
246                 tmp_gid = strtoul(token2, 0, 10);
247                 if ( errno != 0 )
248                 {
249                         SEC_SVR_DBG("cannot change string to integer [%s]", token2);
250                         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
251                         goto error;
252                 }
253
254                 if(strcmp(obj, token) == 0)
255                 {
256                         /* We found it */
257                         ret = tmp_gid;
258                         SEC_SVR_DBG("GID of %s is found: %d", obj, ret);
259                         break;
260                 }
261                 bzero(linebuf, bufsize);
262         }
263
264 error:
265         if(linebuf != NULL)
266                 free(linebuf);
267         if(fp != NULL)
268                 fclose(fp);
269         return ret;
270 }
271
272 /* Signal handler for processes */
273 static void security_server_sig_child(int signo, siginfo_t *info, void *data)
274 {
275         int status;
276         pid_t child_pid;
277         pid_t child_pgid;
278
279         child_pgid = getpgid(info->si_pid);
280         SEC_SVR_DBG("Signal handler: dead_pid=%d, pgid=%d",info->si_pid,child_pgid);
281
282         while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
283                 if(child_pid == child_pgid)
284                         killpg(child_pgid,SIGKILL);
285         }
286
287         return;
288 }
289
290 /* Execute a debugging tool by fork() and execve() */
291 int execute_debug_tool(int argc, char *const *argv, int server_sockfd, int client_sockfd)
292 {
293         int ret, i;
294         SEC_SVR_DBG("%s", "Executing tool");
295
296         ret = fork();
297         if(ret == 0)
298         {
299                 close(client_sockfd);
300                 close(server_sockfd);
301                 setsid();
302
303                 for(i=0;i<_NSIG;i++)
304                         signal(i, SIG_DFL);
305
306                 ret = execv(argv[0], argv);
307                 if(ret == -1)
308                 {
309                         SEC_SVR_DBG("Error:Failed to execute [%d]", errno);
310                         exit(-1);
311                 }
312         }
313         if(ret < 0)
314         {
315                 SEC_SVR_DBG("Error: Failed to fork [%d]", errno);
316                 return SECURITY_SERVER_ERROR_SERVER_ERROR;
317         }
318         return SECURITY_SERVER_SUCCESS;
319 }
320
321 int process_cookie_request(int sockfd)
322 {
323         int retval, client_pid, client_uid;
324         cookie_list *created_cookie = NULL;
325
326         /* Authenticate client */
327         retval = authenticate_client_application(sockfd, &client_pid, &client_uid);
328         if(retval != SECURITY_SERVER_SUCCESS)
329         {
330                 SEC_SVR_DBG("%s", "Client Authentication Failed");
331                 retval = send_generic_response(sockfd,
332                                 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
333                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
334                 if(retval != SECURITY_SERVER_SUCCESS)
335                 {
336                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
337                 }
338                 goto error;
339         }
340         /* If client application is root process, just respond default cookie */
341     /*
342         if( client_uid == 0)
343         {
344                 SEC_SVR_DBG("%s", "Requested application is a root process");
345                 created_cookie = c_list;
346                 if(c_list == NULL)
347                 {
348                         SEC_SVR_DBG("%s", "Cannot read default cookie");
349                         goto error;
350                 }
351         }
352         else
353         {
354     */
355         //TODO: Remove above code if there will be no crashes without it
356         //All process should be treaded the same
357                 /* Create a new cookie. or find existing one */
358                 pthread_mutex_lock(&cookie_mutex);
359                 created_cookie = create_cookie_item(client_pid, sockfd, c_list);
360                 pthread_mutex_unlock(&cookie_mutex);
361                 if(created_cookie == NULL)
362                 {
363                         SEC_SVR_DBG("%s","Cannot create a cookie");
364                         goto error;
365                 }
366
367     //let others know if this cookie belongs to root process
368     if(client_uid == 0)
369         created_cookie->is_roots_process = 1;
370     else
371         created_cookie->is_roots_process = 0;
372
373         //}
374         /* send cookie as response */
375         retval = send_cookie(sockfd, created_cookie->cookie);
376         if(retval != SECURITY_SERVER_SUCCESS)
377         {
378                 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
379         }
380         SEC_SVR_DBG("Server: Cookie created for client PID %d LABEL >%s<",
381                     created_cookie->pid,
382                     (created_cookie->smack_label)?(created_cookie->smack_label):"NULL");
383
384         SEC_SVR_DBG("%s", "Server: Cookie has been sent to client");
385
386 error:
387         return retval;
388 }
389
390 int process_check_privilege_request(int sockfd)
391 {
392         /* Authenticate client */
393         int retval, client_pid, requested_privilege;
394     int privileges[1];
395         unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
396         cookie_list *search_result = NULL;
397
398         retval = authenticate_client_middleware(sockfd, &client_pid);
399         if(retval != SECURITY_SERVER_SUCCESS)
400         {
401                 SEC_SVR_DBG("%s", "Client Authentication Failed");
402                 retval = send_generic_response(sockfd,
403                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
404                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
405                 if(retval != SECURITY_SERVER_SUCCESS)
406                 {
407                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
408                 }
409                 goto error;;
410         }
411
412         retval = recv_check_privilege_request(sockfd,
413                                 requested_cookie, &requested_privilege);
414         if(retval == SECURITY_SERVER_ERROR_RECV_FAILED)
415         {
416                 SEC_SVR_DBG("%s", "Receiving request failed");
417                 retval = send_generic_response(sockfd,
418                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
419                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
420                 if(retval != SECURITY_SERVER_SUCCESS)
421                 {
422                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
423                 }
424                 goto error;;
425         }
426
427         if(requested_privilege < 1)
428         {
429                 SEC_SVR_DBG("Requiring bad privilege [%d]", requested_privilege);
430                 retval = send_generic_response(sockfd,
431                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
432                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
433                 if(retval != SECURITY_SERVER_SUCCESS)
434                 {
435                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
436                 }
437                 goto error;
438         }
439
440         /* Search cookie list */
441         pthread_mutex_lock(&cookie_mutex);
442     privileges[0] = requested_privilege;
443         search_result = search_cookie(c_list, requested_cookie, privileges, 1);
444         pthread_mutex_unlock(&cookie_mutex);
445         if(search_result != NULL)
446         {
447                 /* We found */
448                 SEC_SVR_DBG("We found the cookie with %d privilege and pid:%d", requested_privilege, client_pid);
449                 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
450                 retval = send_generic_response(sockfd,
451                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
452                                 SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
453                 if(retval != SECURITY_SERVER_SUCCESS)
454                 {
455                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
456                 }
457         }
458         else
459         {
460                 /* It's not exist */
461                 SEC_SVR_DBG("Could not find the cookie with %d privilege", requested_privilege);
462                 retval = send_generic_response(sockfd,
463                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
464                                 SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
465                 if(retval != SECURITY_SERVER_SUCCESS)
466                 {
467                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
468                 }
469         }
470 error:
471         return retval;
472 }
473
474 int process_check_privilege_new_request(int sockfd)
475 {
476         /* Authenticate client */
477         int retval, client_pid, requested_privilege;
478         unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
479         cookie_list *search_result = NULL;
480         char object_label[MAX_OBJECT_LABEL_LEN+1];
481         char access_rights[MAX_MODE_STR_LEN+1];
482
483         retval = authenticate_client_middleware(sockfd, &client_pid);
484         if(retval != SECURITY_SERVER_SUCCESS)
485         {
486                 SEC_SVR_DBG("%s", "Client Authentication Failed");
487                 retval = send_generic_response(sockfd, 
488                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
489                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
490                 if(retval != SECURITY_SERVER_SUCCESS)
491                 {
492                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
493                 }
494                 goto error;;
495         }
496
497         retval = recv_check_privilege_new_request(
498                      sockfd, requested_cookie, object_label, access_rights);
499         if(retval == SECURITY_SERVER_ERROR_RECV_FAILED)
500         {
501                 SEC_SVR_DBG("%s", "Receiving request failed");
502                 retval = send_generic_response(sockfd, 
503                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
504                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
505                 if(retval != SECURITY_SERVER_SUCCESS)
506                 {
507                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
508                 }
509                 goto error;;
510         }
511
512         /* Search cookie list */
513         pthread_mutex_lock(&cookie_mutex);
514         search_result = search_cookie_new(c_list, requested_cookie, object_label, access_rights);
515         pthread_mutex_unlock(&cookie_mutex);
516
517         if(search_result != NULL)
518     {
519                 /* We found */
520                 SEC_SVR_DBG("We found the cookie with %s rights and pid:%d", access_rights, client_pid);
521                 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
522                 retval = send_generic_response(sockfd, 
523                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE, 
524                                 SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
525                 if(retval != SECURITY_SERVER_SUCCESS)
526                 {
527                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
528                 }
529         }
530         else
531         {
532                 /* It's not exist */
533                 SEC_SVR_DBG("Could not find the cookie with %s rights", access_rights);
534                 retval = send_generic_response(sockfd, 
535                                 SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE, 
536                                 SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
537                 if(retval != SECURITY_SERVER_SUCCESS)
538                 {
539                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
540                 }
541         }
542 error:
543         return retval;
544
545
546 }
547
548 int process_object_name_request(int sockfd)
549 {
550         int retval, client_pid, requested_privilege;
551         char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
552
553         /* Authenticate client */
554         retval = authenticate_client_middleware(sockfd, &client_pid);
555         if(retval != SECURITY_SERVER_SUCCESS)
556         {
557                 SEC_SVR_DBG("%s", "Client Authentication Failed");
558                 retval = send_generic_response(sockfd,
559                                 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
560                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
561                 if(retval != SECURITY_SERVER_SUCCESS)
562                 {
563                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
564                 }
565                 goto error;
566         }
567
568         /* Receive GID */
569         retval = read(sockfd, &requested_privilege, sizeof(requested_privilege));
570         if (retval < sizeof(requested_privilege))
571         {
572                 SEC_SVR_DBG("%s", "Receiving request failed");
573                 retval = send_generic_response(sockfd,
574                                 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
575                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
576                 if(retval != SECURITY_SERVER_SUCCESS)
577                 {
578                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
579                 }
580                 goto error;
581         }
582
583         /* Search from /etc/group */
584         retval = search_object_name(requested_privilege,
585                         object_name,
586                         SECURITY_SERVER_MAX_OBJ_NAME);
587         if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
588         {
589                 /* It's not exist */
590                 SEC_SVR_DBG("There is no such object for gid [%d]", requested_privilege);
591                 retval = send_generic_response(sockfd,
592                                 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
593                                 SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
594                 if(retval != SECURITY_SERVER_SUCCESS)
595                 {
596                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
597                 }
598                 goto error;
599         }
600         if(retval != SECURITY_SERVER_SUCCESS)
601         {
602                 /* Error occurred */
603                 SEC_SVR_DBG("Error on searching object name [%d]", retval);
604                 retval = send_generic_response(sockfd,
605                                 SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
606                                 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
607                 if(retval != SECURITY_SERVER_SUCCESS)
608                 {
609                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
610                 }
611                 goto error;
612         }
613
614         /* We found */
615         SEC_SVR_DBG("We found object: %s", object_name);
616         retval = send_object_name(sockfd, object_name);
617         if(retval != SECURITY_SERVER_SUCCESS)
618         {
619                 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
620         }
621
622 error:
623         return retval;
624 }
625
626 int process_gid_request(int sockfd, int msg_len)
627 {
628         int retval, client_pid;
629         char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
630         /* Authenticate client as middleware daemon */
631         retval = authenticate_client_middleware(sockfd, &client_pid);
632         if(retval != SECURITY_SERVER_SUCCESS)
633         {
634                 SEC_SVR_DBG("%s", "Client authentication failed");
635                 retval = send_generic_response(sockfd,
636                                 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
637                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
638                 if(retval != SECURITY_SERVER_SUCCESS)
639                 {
640                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
641                 }
642                 goto error;
643         }
644
645         if(msg_len >= SECURITY_SERVER_MAX_OBJ_NAME)
646         {
647                 /* Too big ojbect name */
648                 SEC_SVR_DBG("%s", "Object name is too big");
649                 retval = send_generic_response(sockfd,
650                                 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
651                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
652                 if(retval != SECURITY_SERVER_SUCCESS)
653                 {
654                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
655                 }
656                 goto error;
657         }
658
659         /* Receive group name */
660         retval = read(sockfd, object_name, msg_len);
661         if (retval < msg_len )
662         {
663                 SEC_SVR_DBG("%s", "Failed to read object name");
664                 retval = send_generic_response(sockfd,
665                                 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
666                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
667                 if(retval != SECURITY_SERVER_SUCCESS)
668                 {
669                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
670                 }
671                 goto error;
672         }
673         object_name[msg_len] = 0;
674
675         /* Search /etc/group for the given group name */
676         retval = search_gid(object_name);
677         if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
678         {
679                 /* Not exist */
680                 SEC_SVR_DBG("The object [%s] is not exist", object_name);
681                 retval = send_generic_response(sockfd,
682                                 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
683                                 SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
684                 if(retval != SECURITY_SERVER_SUCCESS)
685                 {
686                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
687                 }
688                 goto error;
689         }
690
691         if(retval < 0)
692         {
693                 /* Error occurred */
694                 SEC_SVR_DBG("Cannot send the response. %d", retval);
695                 retval = send_generic_response(sockfd,
696                                 SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
697                                 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
698                 if(retval != SECURITY_SERVER_SUCCESS)
699                 {
700                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
701                 }
702
703                 goto error;
704         }
705         /* We found */
706         retval = send_gid(sockfd, retval);
707         if(retval != SECURITY_SERVER_SUCCESS)
708         {
709                 SEC_SVR_DBG("ERROR: Cannot gid response: %d", retval);
710         }
711 error:
712         return retval;
713 }
714
715 int process_pid_request(int sockfd)
716 {
717         int retval, client_pid;
718         unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
719     int * privileges;
720         cookie_list *search_result = NULL;
721
722         /* Authenticate client */
723         retval = authenticate_client_middleware(sockfd, &client_pid);
724         if(retval != SECURITY_SERVER_SUCCESS)
725         {
726                 SEC_SVR_DBG("%s", "Client Authentication Failed");
727                 retval = send_generic_response(sockfd,
728                                 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
729                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
730                 if(retval != SECURITY_SERVER_SUCCESS)
731                 {
732                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
733                 }
734                 goto error;
735         }
736
737         retval = recv_pid_request(sockfd, requested_cookie);
738         if(retval == SECURITY_SERVER_ERROR_RECV_FAILED)
739         {
740                 SEC_SVR_DBG("%s", "Receiving request failed");
741                 retval = send_generic_response(sockfd,
742                                 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
743                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
744                 if(retval != SECURITY_SERVER_SUCCESS)
745                 {
746                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
747                 }
748                 goto error;
749         }
750
751         /* Search cookie list */
752         pthread_mutex_lock(&cookie_mutex);
753
754     retval = get_client_gid_list(sockfd, &privileges);
755     if(retval < 0)
756     {
757         SEC_SVR_DBG("ERROR: Cannot get GID list");
758         goto error;
759     }
760
761         search_result = search_cookie(c_list, requested_cookie, privileges, retval);
762     free(privileges);
763
764         pthread_mutex_unlock(&cookie_mutex);
765         if(search_result != NULL)
766         {
767                 /* We found */
768                 SEC_SVR_DBG("We found the cookie and pid:%d", search_result->pid);
769                 SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
770                 retval = send_pid(sockfd, search_result->pid);
771
772                 if(retval != SECURITY_SERVER_SUCCESS)
773                 {
774                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
775                 }
776         }
777         else
778         {
779                 /* It's not exist */
780                 SEC_SVR_DBG("%s", "Could not find the cookie");
781                 retval = send_generic_response(sockfd,
782                                 SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
783                                 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
784                 if(retval != SECURITY_SERVER_SUCCESS)
785                 {
786                         SEC_SVR_DBG("ERROR: Cannot send pid response: %d", retval);
787                 }
788         }
789 error:
790         return retval;
791 }
792
793 int process_smack_request(int sockfd)
794 {
795     int retval, client_pid;
796     int privileges[1];
797     unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
798     cookie_list *search_result = NULL;
799     //handler for SMACK label
800     char * label = NULL;
801     //buffer for storing file path
802     const int BUFFSIZE = 30;
803     char path[BUFFSIZE];
804     int fd;
805
806         /* Authenticate client */
807         retval = authenticate_client_middleware(sockfd, &client_pid);
808         if(retval != SECURITY_SERVER_SUCCESS)
809         {
810                 SEC_SVR_DBG("%s", "Client Authentication Failed");
811                 retval = send_generic_response(sockfd,
812                                 SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
813                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
814                 if(retval != SECURITY_SERVER_SUCCESS)
815                 {
816                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
817                 }
818                 goto error;
819         }
820
821     retval = recv_smack_request(sockfd, requested_cookie);
822     if(retval == SECURITY_SERVER_ERROR_RECV_FAILED)
823     {
824         SEC_SVR_DBG("%s", "Receiving request failed");
825         retval = send_generic_response(sockfd,
826             SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
827             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
828         if(retval != SECURITY_SERVER_SUCCESS)
829         {
830             SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
831         }
832         goto error;
833     }
834
835     /* Search cookie list */
836     pthread_mutex_lock(&cookie_mutex);
837
838     retval = get_client_gid_list(sockfd, &privileges);
839     if(retval < 0)
840     {
841         SEC_SVR_DBG("ERROR: Cannot get GID list");
842         goto error;
843     }
844
845     search_result = search_cookie(c_list, requested_cookie, privileges, retval);
846     free(privileges);
847
848     pthread_mutex_unlock(&cookie_mutex);
849     if(search_result != NULL)
850     {
851         /* We found */
852         SEC_SVR_DBG("We found the cookie and pid:%d", search_result->pid);
853         SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
854
855         //clearing buffer
856         memset(path, 0x00, BUFFSIZE);
857
858         //preparing file path
859         snprintf(path, BUFFSIZE, "/proc/%d/attr/current", search_result->pid);
860         SEC_SVR_DBG("Path to file: %s\n", path);
861
862         //allocation place for label
863         label = calloc(SMACK_LABEL_LEN, 1);
864         if(NULL == label)
865         {
866             SEC_SVR_DBG("Client ERROR: Memory allocation error");
867             goto error;
868         }
869
870         //clearing buffer for label
871         memset(label, 0x00, SMACK_LABEL_LEN);
872
873         //opening file /proc/<pid>/attr/curent with SMACK label
874         fd = open(path, O_RDONLY);
875         if(fd < 0)
876         {
877             SEC_SVR_DBG("Client ERROR: Unable to open file in /proc");
878             goto error;
879         }
880
881         //reading label from file, it is NOT NULL TERMINATED
882         retval = read(fd, label, SMACK_LABEL_LEN);
883         close(fd);
884         if(retval < 0)
885         {
886             SEC_SVR_DBG("Client ERROR: Unable to read from file");
887             goto error;
888         }
889
890         SEC_SVR_DBG("Readed label is: %s\n", label);
891
892                 retval = send_smack(sockfd, label);
893
894                 if(retval != SECURITY_SERVER_SUCCESS)
895                 {
896                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
897                 }
898         }
899         else
900         {
901                 /* It's not exist */
902                 SEC_SVR_DBG("%s", "Could not find the cookie");
903                 retval = send_generic_response(sockfd,
904                                 SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
905                                 SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
906                 if(retval != SECURITY_SERVER_SUCCESS)
907                 {
908                         SEC_SVR_DBG("ERROR: Cannot send SMACK label response: %d", retval);
909                 }
910         }
911 error:
912     if(NULL != label)
913         free(label);
914
915         return retval;
916 }
917
918 int process_tool_request(int client_sockfd, int server_sockfd)
919 {
920         int retval, argcnum;
921         char **recved_argv = NULL;
922
923         /* Authenticate client */
924         retval = authenticate_developer_shell(client_sockfd);
925         if(retval != SECURITY_SERVER_SUCCESS)
926         {
927                 SEC_SVR_DBG("%s", "Client Authentication Failed");
928                 retval = send_generic_response(client_sockfd,
929                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
930                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
931                 if(retval != SECURITY_SERVER_SUCCESS)
932                 {
933                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
934                 }
935                 goto error;
936         }
937
938         /* Receive Total number of argv */
939         argcnum = 0;
940         retval = read(client_sockfd, &argcnum, sizeof(int));
941         if((retval < sizeof(int)) || argcnum > (UINT_MAX/sizeof(char *))-2 || argcnum < 0)
942         {
943                 SEC_SVR_DBG("Error: argc recieve failed: %d", retval);
944                 retval = send_generic_response(client_sockfd,
945                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
946                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
947                 if(retval != SECURITY_SERVER_SUCCESS)
948                 {
949                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
950                 }
951                 goto error;
952         }
953         argcnum += 2;
954         recved_argv = (char **)malloc(sizeof(char *) * argcnum);
955         if(recved_argv == NULL)
956         {
957                 SEC_SVR_DBG("Error: malloc() failed: %d", retval);
958                 retval = send_generic_response(client_sockfd,
959                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
960                                 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
961                 if(retval != SECURITY_SERVER_SUCCESS)
962                 {
963                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
964                 }
965                 goto error;
966         }
967         memset(recved_argv, 0, sizeof(char *) * argcnum);
968
969         retval = recv_launch_tool_request(client_sockfd, argcnum -1, recved_argv);
970         if(retval == SECURITY_SERVER_ERROR_RECV_FAILED || retval == SECURITY_SERVER_ERROR_OUT_OF_MEMORY)
971         {
972                 SEC_SVR_DBG("%s", "Receiving request failed");
973                 recved_argv = NULL;
974                 retval = send_generic_response(client_sockfd,
975                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
976                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
977                 if(retval != SECURITY_SERVER_SUCCESS)
978                 {
979                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
980                 }
981                 goto error;
982         }
983         if(argcnum < 2)
984         {
985                 SEC_SVR_DBG("Error: Too small number of argv [%d]", argcnum);
986                 retval = send_generic_response(client_sockfd,
987                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
988                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
989                 if(retval != SECURITY_SERVER_SUCCESS)
990                 {
991                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
992                 }
993                 goto error;
994         }
995         /* Execute the command */
996         retval = execute_debug_tool(argcnum, recved_argv, server_sockfd, client_sockfd);
997         if(retval != SECURITY_SERVER_SUCCESS)
998         {
999                 SEC_SVR_DBG("Error: Cannot execute debug tool [%d]", retval);
1000                 retval = send_generic_response(client_sockfd,
1001                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1002                                 SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1003                 if(retval != SECURITY_SERVER_SUCCESS)
1004                 {
1005                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1006                 }
1007         }
1008         else
1009         {
1010                 SEC_SVR_DBG("%s", "Tool has been executed");
1011                 retval = send_generic_response(client_sockfd,
1012                                 SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1013                                 SECURITY_SERVER_RETURN_CODE_SUCCESS);
1014                 if(retval != SECURITY_SERVER_SUCCESS)
1015                 {
1016                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1017                 }
1018         }
1019 error:
1020         if(recved_argv != NULL)
1021         {
1022                 /* Free */
1023                 free_argv(recved_argv, argcnum);
1024                 recved_argv = NULL;
1025                 argcnum =0;;
1026         }
1027         return retval;
1028 }
1029
1030 void *security_server_thread(void *param)
1031 {
1032         int client_sockfd = -1, client_uid, client_pid;
1033         int server_sockfd, retval, argcnum;
1034         basic_header basic_hdr;
1035         struct security_server_thread_param *my_param;
1036
1037         my_param = (struct security_server_thread_param *) param;
1038         client_sockfd = my_param->client_sockfd;
1039         server_sockfd = my_param->server_sockfd;
1040
1041         /* Receive request header */
1042         retval = recv_hdr(client_sockfd, &basic_hdr);
1043         if(retval == SECURITY_SERVER_ERROR_TIMEOUT || retval == SECURITY_SERVER_ERROR_RECV_FAILED
1044                 || retval == SECURITY_SERVER_ERROR_SOCKET)
1045         {
1046                 SEC_SVR_DBG("Receiving header error [%d]",retval);
1047                 close(client_sockfd);
1048                 client_sockfd = -1;
1049                 goto error;;
1050         }
1051
1052         if(retval != SECURITY_SERVER_SUCCESS)
1053         {
1054                 /* Response */
1055                 SEC_SVR_DBG("Receiving header error [%d]",retval);
1056                 retval = send_generic_response(client_sockfd,
1057                                 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1058                                 SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1059                 if(retval != SECURITY_SERVER_SUCCESS)
1060                 {
1061                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1062                         goto error;
1063                 }
1064                 safe_server_sock_close(client_sockfd);
1065                 client_sockfd = -1;
1066                 goto error;
1067         }
1068
1069         /* Act different for request message ID */
1070         switch(basic_hdr.msg_id)
1071         {
1072                 case SECURITY_SERVER_MSG_TYPE_COOKIE_REQUEST:
1073                         SEC_SVR_DBG("%s", "Cookie request received");
1074                         process_cookie_request(client_sockfd);
1075                         break;
1076
1077                 case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_REQUEST:
1078                         SEC_SVR_DBG("%s", "Privilege check received");
1079                         process_check_privilege_request(client_sockfd);
1080                         break;
1081
1082                 case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_REQUEST:
1083                         SEC_SVR_DBG("%s", "Privilege check (new mode) received");
1084                         process_check_privilege_new_request(client_sockfd);
1085                         break;
1086
1087                 case SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_REQUEST:
1088                         SEC_SVR_DBG("%s", "Get object name request received");
1089                         process_object_name_request(client_sockfd);
1090                         break;
1091
1092                 case SECURITY_SERVER_MSG_TYPE_GID_REQUEST:
1093                         SEC_SVR_DBG("%s", "Get GID received");
1094                         process_gid_request(client_sockfd, (int)basic_hdr.msg_len);
1095                         break;
1096
1097                 case SECURITY_SERVER_MSG_TYPE_PID_REQUEST:
1098                         SEC_SVR_DBG("%s", "pid request received");
1099                         process_pid_request(client_sockfd);
1100                         break;
1101
1102                 case SECURITY_SERVER_MSG_TYPE_SMACK_REQUEST:
1103                         SEC_SVR_DBG("%s", "SMACK label request received");
1104                         process_smack_request(client_sockfd);
1105                         break;
1106
1107                 case SECURITY_SERVER_MSG_TYPE_TOOL_REQUEST:
1108                         SEC_SVR_DBG("%s", "launch tool request received");
1109                         process_tool_request(client_sockfd, server_sockfd);
1110                         break;
1111
1112                 case SECURITY_SERVER_MSG_TYPE_VALID_PWD_REQUEST:
1113                         SEC_SVR_DBG("%s", "Server: validate password request received");
1114                         process_valid_pwd_request(client_sockfd);
1115                         break;
1116
1117                 case SECURITY_SERVER_MSG_TYPE_SET_PWD_REQUEST:
1118                         SEC_SVR_DBG("%s", "Server: set password request received");
1119                         process_set_pwd_request(client_sockfd);
1120                         break;
1121
1122                 case SECURITY_SERVER_MSG_TYPE_RESET_PWD_REQUEST:
1123                         SEC_SVR_DBG("%s", "Server: reset password request received");
1124                         process_reset_pwd_request(client_sockfd);
1125                         break;
1126
1127                 case SECURITY_SERVER_MSG_TYPE_CHK_PWD_REQUEST:
1128                         SEC_SVR_DBG("%s", "Server: check password request received");
1129                         process_chk_pwd_request(client_sockfd);
1130                         break;
1131
1132                 case SECURITY_SERVER_MSG_TYPE_SET_PWD_HISTORY_REQUEST:
1133                         SEC_SVR_DBG("%s", "Server: set password histroy request received");
1134                         process_set_pwd_history_request(client_sockfd);
1135                         break;
1136
1137                 case SECURITY_SERVER_MSG_TYPE_SET_PWD_MAX_CHALLENGE_REQUEST:
1138                     SEC_SVR_DBG("%s", "Server: set password max challenge request received");
1139                     process_set_pwd_max_challenge_request(client_sockfd);
1140                     break;
1141
1142         case SECURITY_SERVER_MSG_TYPE_SET_PWD_VALIDITY_REQUEST:
1143             SEC_SVR_DBG("%s", "Server: set password validity request received");
1144             process_set_pwd_validity_request(client_sockfd);
1145             break;
1146
1147 /************************************************************************************************/
1148 /* Just for test. This code must be removed on release */
1149                 case SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_REQUEST:
1150                         SEC_SVR_DBG("%s", "all cookie info request received -- NEED TO BE DELETED ON RELEASE");
1151                         retval = authenticate_client_application(client_sockfd, &client_pid, &client_uid);
1152                         if(retval != SECURITY_SERVER_SUCCESS)
1153                         {
1154                                 SEC_SVR_DBG("%s", "Client Authentication Failed");
1155                                 retval = send_generic_response(client_sockfd,
1156                                                 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1157                                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1158                                 if(retval != SECURITY_SERVER_SUCCESS)
1159                                 {
1160                                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1161                                 }
1162                                 break;
1163                         }
1164                         retval = util_process_all_cookie(client_sockfd, c_list);
1165                         if(retval != SECURITY_SERVER_SUCCESS)
1166                         {
1167                                 SEC_SVR_DBG("ERROR: Cannot send all cookie info: %d", retval);
1168                         }
1169                         break;
1170
1171                 case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_PID_REQUEST:
1172                         SEC_SVR_DBG("%s", "cookie info from pid request received -- NEED TO BE DELETED ON RELEASE");
1173                         if(retval != SECURITY_SERVER_SUCCESS)
1174                         {
1175                                 SEC_SVR_DBG("%s", "Client Authentication Failed");
1176                                 retval = send_generic_response(client_sockfd,
1177                                                 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1178                                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1179                                 if(retval != SECURITY_SERVER_SUCCESS)
1180                                 {
1181                                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1182                                 }
1183                                 break;
1184                         }
1185                         util_process_cookie_from_pid(client_sockfd, c_list);
1186                         break;
1187
1188                 case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_COOKIE_REQUEST:
1189                         SEC_SVR_DBG("%s", "cookie info from cookie request received -- NEED TO BE DELETED ON RELEASE");
1190                         if(retval != SECURITY_SERVER_SUCCESS)
1191                         {
1192                                 SEC_SVR_DBG("%s", "Client Authentication Failed");
1193                                 retval = send_generic_response(client_sockfd,
1194                                                 SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1195                                                 SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1196                                 if(retval != SECURITY_SERVER_SUCCESS)
1197                                 {
1198                                         SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1199                                 }
1200                                 break;
1201                         }
1202                         util_process_cookie_from_cookie(client_sockfd, c_list);
1203                         break;
1204 /************************************************************************************************/
1205
1206
1207                 default:
1208                         SEC_SVR_DBG("Unknown msg ID :%d", basic_hdr.msg_id);
1209                         /* Unknown message ID */
1210                         retval = send_generic_response(client_sockfd,
1211                         SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1212                         SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1213                         if(retval != SECURITY_SERVER_SUCCESS)
1214                         {
1215                                 SEC_SVR_DBG("ERROR: Cannot send generic response: %d", retval);
1216                         }
1217                         break;
1218         }
1219
1220         if(client_sockfd > 0)
1221         {
1222                 safe_server_sock_close(client_sockfd);
1223                 client_sockfd = -1;
1224         }
1225
1226 error:
1227         if(client_sockfd > 0)
1228                 close(client_sockfd);
1229         thread_status[my_param->thread_status] = 0;
1230         pthread_detach(pthread_self());
1231         pthread_exit(NULL);
1232 }
1233
1234 void *security_server_main_thread(void *data)
1235 {
1236         int server_sockfd = 0, retval, client_sockfd = -1, args[2], rc;
1237         struct sigaction act, dummy;
1238         pthread_t threads[SECURITY_SERVER_NUM_THREADS];
1239         struct security_server_thread_param param[SECURITY_SERVER_NUM_THREADS];
1240
1241         SEC_SVR_DBG("%s", "Starting Security Server main thread");
1242
1243         /* security server must be executed by root */
1244         if(getuid() != 0)
1245         {
1246                 fprintf(stderr, "%s\n", "You are not root. exiting...");
1247                 goto error;
1248         }
1249
1250         for(retval = 0 ; retval < SECURITY_SERVER_NUM_THREADS; retval++)
1251                 thread_status[retval] = 0;
1252         initiate_try();
1253
1254         /* Create and bind a Unix domain socket */
1255         retval = create_new_socket(&server_sockfd);
1256         if(retval != SECURITY_SERVER_SUCCESS)
1257         {
1258                 SEC_SVR_DBG("%s", "cannot create socket. exiting...");
1259                 goto error;
1260         }
1261
1262         if(listen(server_sockfd, 5) < 0)
1263         {
1264                 SEC_SVR_DBG("%s", "listen() failed. exiting...");
1265                 goto error;
1266         }
1267
1268         /* Create a default cookie --> Cookie for root process */
1269         c_list = create_default_cookie();
1270         if(c_list == NULL)
1271         {
1272                 SEC_SVR_DBG("%s", "cannot make a default cookie. exiting...");
1273                 goto error;
1274         }
1275
1276         /* Init signal handler */
1277         act.sa_handler = NULL;
1278         act.sa_sigaction = security_server_sig_child;
1279         sigemptyset(&act.sa_mask);
1280         act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
1281
1282         if (sigaction(SIGCHLD, &act, &dummy) < 0)
1283         {
1284                 SEC_SVR_DBG("%s", "cannot change session");
1285         }
1286
1287         pthread_mutex_init(&cookie_mutex, NULL);
1288
1289         while(1)
1290         {
1291                 /* Accept a new client */
1292                 if(client_sockfd < 0)
1293                         client_sockfd = accept_client(server_sockfd);
1294
1295                 if(client_sockfd == SECURITY_SERVER_ERROR_TIMEOUT)
1296                         continue;
1297                 if(client_sockfd < 0)
1298                         goto error;
1299                 SEC_SVR_DBG("Server: new connection has been accepted: %d", client_sockfd);
1300                 retval = 0;
1301                 while(1)
1302                 {
1303                         if(thread_status[retval] == 0)
1304                         {
1305                                 thread_status[retval] = 1;
1306                                 param[retval].client_sockfd = client_sockfd;
1307                                 param[retval].server_sockfd = server_sockfd;
1308                                 param[retval].thread_status= retval;
1309                                 SEC_SVR_DBG("Server: Creating a new thread: %d", retval);
1310                                 rc =pthread_create(&threads[retval], NULL, security_server_thread, (void *)&param[retval]);
1311                                 if (rc)
1312                                 {
1313                                         SEC_SVR_DBG("Error: Server: Cannot create thread:%d", rc);
1314                                         goto error;
1315                                 }
1316                                 break;
1317                         }
1318                         retval++;
1319                         if(retval >= SECURITY_SERVER_NUM_THREADS)
1320                                 retval = 0;
1321                 }
1322                 client_sockfd = -1;
1323         }
1324 error:
1325         if(server_sockfd > 0)
1326                 close(server_sockfd);
1327
1328         pthread_detach(pthread_self());
1329         pthread_exit(NULL);
1330 }
1331
1332 int main(int argc, char* argv[])
1333 {
1334         int res;
1335         pthread_t main_thread;
1336
1337         res = pthread_create(&main_thread, NULL, security_server_main_thread, NULL);
1338         if (res == 0)
1339         {
1340                 while (1)
1341                         sleep(60);
1342         }
1343         else
1344         {
1345                 SEC_SVR_DBG("Error: Server: Cannot create main security server thread: %d", res);
1346         }
1347         pthread_exit(NULL);
1348         return 0;
1349 }
1350