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