6881c370e74a19ea7f246ed017b75e14c8f60969
[platform/core/security/security-manager.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 #include <grp.h>
40 #include <stdint.h>
41
42 #include <privilege-control.h>
43 #include <security-server-system-observer.h>
44 #include <security-server-rules-revoker.h>
45
46 #include "security-server-cookie.h"
47 #include "security-server-common.h"
48 #include "security-server-password.h"
49 #include "security-server-comm.h"
50 #include "smack-check.h"
51
52 //definitions of security-server API labels
53 #define API_PASSWD_SET   "security-server::api-password-set"
54 #define API_PASSWD_CHECK "security-server::api-password-check"
55 #define API_DATA_SHARE   "security-server::api-data-share"
56 #define API_MIDDLEWARE   "security-server::api-middleware"
57 #define API_FREE_ACCESS  "*"
58
59 //required rule type
60 #define API_RULE_REQUIRED "w"
61
62 /* Set cookie as a global variable */
63 cookie_list *c_list;
64 pthread_mutex_t cookie_mutex;
65 int thread_status[SECURITY_SERVER_NUM_THREADS];
66 struct security_server_thread_param {
67     int client_sockfd;
68     int server_sockfd;
69     int thread_status;
70 };
71
72 int process_app_get_access_request(int sockfd, size_t msg_len);
73 static int netlink_enabled = 1; /* prevent memory leaks when netlink is disabled */
74
75
76 /************************************************************************************************/
77 /* Just for test. This code must be removed on release */
78 #include "security-server-util.h"
79 /************************************************************************************************/
80
81 #if 0
82 void printhex(unsigned char *data, int size)
83 {
84     int i;
85     for (i = 0; i < size; i++)
86     {
87         if (data[i] < 0xF)
88             printf("0");
89
90         printf("%X ", data[i]);
91         if (((i + 1) % 16) == 0 && i != 0)
92             printf("\n");
93     }
94     printf("\n");
95 }
96
97 void print_cookie(cookie_list *list)
98 {
99     int i;
100     printf("%s", "cookie:\n");
101     printhex(list->cookie, SECURITY_SERVER_COOKIE_LEN);
102     printf("path_len: %d\n", list->path ? strlen(list->path) : 0);
103     printf("permission_len: %d\n", list->permission_len);
104     printf("PID: %d\n", list->pid);
105     printf("path: %s\n", list->path);
106     printf("%s", "permissions: ");
107     for (i = 0; i < list->permission_len; i++)
108     {
109         printf("%d ", list->permissions[i]);
110     }
111     printf("%s", "\n");
112     printf("prev: %p\n", list->prev);
113     printf("next: %p\n", list->next);
114 }
115 #endif
116
117 char *read_exe_path_from_proc(pid_t pid)
118 {
119     char link[32];
120     char *exe = NULL;
121     size_t size = 64;
122     ssize_t cnt = 0;
123
124     // get link to executable
125     snprintf(link, sizeof(link), "/proc/%d/exe", pid);
126
127     for (;;)
128     {
129         exe = malloc(size);
130         if (exe == NULL)
131         {
132             SEC_SVR_ERR("Out of memory");
133             return NULL;
134         }
135
136         // read link target
137         cnt = readlink(link, exe, size);
138
139         // error
140         if (cnt < 0 || (size_t) cnt > size)
141         {
142             SEC_SVR_ERR("Can't locate process binary for pid[%d]", pid);
143             free(exe);
144             return NULL;
145         }
146
147         // read less than requested
148         if ((size_t) cnt < size)
149             break;
150
151         // read exactly the number of bytes requested
152         free(exe);
153         if (size > (SIZE_MAX >> 1))
154         {
155             SEC_SVR_ERR("Exe path too long (more than %d characters)", size);
156             return NULL;
157         }
158         size <<= 1;
159     }
160     // readlink does not append null byte to buffer.
161     exe[cnt] = '\0';
162     return exe;
163 }
164
165 /*
166  * Function that checks if API caller have access to specified label.
167  * In positive case (caller has access to the API) returns 1.
168  * In case of no access returns 0, and -1 in case of error.
169  */
170 int authorize_SS_API_caller_socket(int sockfd, char *required_API_label, char *required_rule)
171 {
172     int retval;
173     char *label = NULL;
174     char *path = NULL;
175     //for getting socket options
176     struct ucred cr;
177     unsigned int len;
178
179     SEC_SVR_DBG("Checking client SMACK access to SS API");
180
181     if (!smack_check()) {
182         SEC_SVR_ERR("No SMACK on device found, API PROTECTION DISABLED!!!");
183         retval = 1;
184         goto end;
185     }
186
187     retval = smack_new_label_from_socket(sockfd, &label);
188     if (retval != 0) {
189         SEC_SVR_ERR("%s", "Error in getting label from socket");
190         retval = -1;
191         goto end;
192     }
193
194     len = sizeof(cr);
195     retval = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
196     if (retval < 0)
197         SEC_SVR_ERR("Error in getsockopt() and getting binary path");
198     else
199         path = read_exe_path_from_proc(cr.pid);
200
201     retval = smack_have_access(label, required_API_label, required_rule);
202
203     //some log in SMACK format
204     if (retval > 0)
205         SEC_SVR_DBG("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", cr.pid, label, required_API_label, required_rule, retval, path);
206     else
207         SEC_SVR_ERR("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", cr.pid, label, required_API_label, required_rule, retval, path);
208
209 end:
210     if (path != NULL)
211         free(path);
212     if (label != NULL)
213         free(label);
214
215     return retval;
216 }
217
218 /* Object name is actually name of a Group ID *
219  * This function opens /etc/group file and search group ID and
220  * returns the string */
221 int search_object_name(int gid, char *obj, int obj_size)
222 {
223     FILE *fp = NULL;
224     char *linebuf = NULL, *token = NULL, *token2, *tempstr = NULL;
225     int ret = 0, tmp_gid, bufsize;
226     fp = fopen("/etc/group", "r");
227     if (fp == NULL)
228     {
229         /* cannot open /etc/group */
230         SEC_SVR_ERR("%s", "Cannot open /etc/group");
231         return SECURITY_SERVER_ERROR_FILE_OPERATION;
232     }
233
234     linebuf = malloc(128);
235     bufsize = 128;
236     if (linebuf == NULL)
237     {
238         ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
239         SEC_SVR_ERR("%s", "cannot malloc()");
240         goto error;
241     }
242
243     bzero(linebuf, bufsize);
244     ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
245     while (fgets(linebuf, bufsize, fp) != NULL)
246     {
247         while (linebuf[bufsize - 2] != 0)
248         {
249             linebuf[bufsize - 1] = (char) fgetc(fp);
250             tempstr = realloc(linebuf, bufsize + 128);
251             if (tempstr == NULL)
252             {
253                 ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
254                 goto error;
255             }
256             linebuf = tempstr;
257             bzero(linebuf + bufsize, 128);
258             fgets(linebuf + bufsize, 128, fp);
259             bufsize += 128;
260         }
261
262         token = strtok(linebuf, ":");   /* group name */
263         if (token == NULL)
264         {
265             SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
266             ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
267             goto error;
268         }
269         token2 = strtok(NULL, ":"); /* group password */
270         if (token2 == NULL)
271         {
272             SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
273             ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
274             goto error;
275         }
276         token2 = strtok(NULL, ":"); /* gid */
277         if (token2 == NULL)
278         {
279             SEC_SVR_ERR("/etc/group is not valid. cannot find gid: [%s]", linebuf);
280             ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
281             goto error;
282         }
283
284         errno = 0;
285         tmp_gid = strtoul(token2, 0, 10);
286         if (errno != 0)
287         {
288             SEC_SVR_ERR("cannot change string to integer [%s]", token2);
289             ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
290             goto error;
291         }
292
293         if (tmp_gid == gid)
294         {
295             /* We found it */
296             if ((int)strlen(token) > obj_size)
297             {
298                 ret = SECURITY_SERVER_ERROR_BUFFER_TOO_SMALL;
299                 SEC_SVR_ERR("buffer is too small. %d --> %d", obj_size, strlen(token));
300                 goto error;
301             }
302             strncpy(obj, token, strlen(token));
303             obj[strlen(token)] = 0;
304             ret = SECURITY_SERVER_SUCCESS;
305             break;
306         }
307         bzero(linebuf, bufsize);
308     }
309
310 error:
311     if (linebuf != NULL)
312         free(linebuf);
313     if (fp != NULL)
314         fclose(fp);
315     return ret;
316 }
317
318 /*
319  * Searches for group ID by given group name
320  */
321
322 int search_gid(const char *obj)
323 {
324     int ret = 0;
325     struct group *grpbuf = NULL;
326     struct group grp;
327     char *buf = NULL;
328     char *bigger_buf = NULL;
329     long int max_buf_size = 0;
330
331     /*
332      * The maximum needed size for buf can be found using sysconf(3) with the argument _SC_GETGR_R_SIZE_MAX
333  * If _SC_GETGR_R_SIZE_MAX is not returned we set max_buf_size to 1024 bytes. Enough to store few groups.
334      */
335     max_buf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
336     if (max_buf_size == -1)
337         max_buf_size = 1024;
338
339     buf = malloc((size_t)max_buf_size);
340     if (buf == NULL)
341     {
342         ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
343         SEC_SVR_ERR("Out Of Memory");
344         goto error;
345     }
346
347     /*
348      * There can be some corner cases when for example user is assigned to a lot of groups.
349      * In that case if buffer is to small getgrnam_r will return ERANGE error.
350      * Solution could be calling getgrnam_r with bigger buffer until it's enough big.
351      */
352     while ((ret = getgrnam_r(obj, &grp, buf, (size_t)max_buf_size, &grpbuf)) == ERANGE) {
353         max_buf_size *= 2;
354
355         bigger_buf = realloc(buf, (size_t)max_buf_size);
356         if (bigger_buf == NULL) {
357             ret = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
358             SEC_SVR_ERR("Out Of Memory");
359             goto error;
360         }
361
362         buf = bigger_buf;
363     }
364
365     if (ret != 0)
366     {
367         ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
368         SEC_SVR_ERR("getgrnam_r failed with error %s\n", strerror(errno));
369         goto error;
370     } else if (grpbuf == NULL) {
371         ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
372         SEC_SVR_ERR("Cannot find gid for group %s\n", obj);
373         goto error;
374     }
375
376     ret = grpbuf->gr_gid;
377
378 error:
379     free(buf);
380     return ret;
381 }
382
383 /* Signal handler for processes */
384 static void security_server_sig_child(int signo, siginfo_t *info, void *data)
385 {
386     int status;
387     pid_t child_pid;
388     pid_t child_pgid;
389
390     (void)signo;
391     (void)data;
392
393     child_pgid = getpgid(info->si_pid);
394     SEC_SVR_DBG("Signal handler: dead_pid=%d, pgid=%d",info->si_pid,child_pgid);
395
396     while ((child_pid = waitpid(-1, &status, WNOHANG)) > 0) {
397         if (child_pid == child_pgid)
398             killpg(child_pgid,SIGKILL);
399     }
400
401     return;
402 }
403
404 /* Execute a debugging tool by fork() and execve() */
405 int execute_debug_tool(int argc, char*const *argv, int server_sockfd, int client_sockfd)
406 {
407     int ret, i;
408     SEC_SVR_DBG("%s", "Executing tool");
409
410     (void)argc;
411
412     ret = fork();
413     if (ret == 0)
414     {
415         close(client_sockfd);
416         close(server_sockfd);
417         setsid();
418
419         for (i = 0; i < _NSIG; i++)
420             signal(i, SIG_DFL);
421
422         ret = execv(argv[0], argv);
423         if (ret == -1)
424         {
425             SEC_SVR_ERR("Error:Failed to execute [%d]", errno);
426             exit(-1);
427         }
428     }
429     if (ret < 0)
430     {
431         SEC_SVR_ERR("Error: Failed to fork [%d]", errno);
432         return SECURITY_SERVER_ERROR_SERVER_ERROR;
433     }
434     return SECURITY_SERVER_SUCCESS;
435 }
436
437 /* Authenticate the application is middleware daemon
438  * The middleware must run as root and the cmd line must be pre listed */
439 int authenticate_developer_shell(int sockfd)
440 {
441     int retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
442     struct ucred cr;
443     unsigned int cl = sizeof(cr);
444     char *exe = NULL;
445
446     /* get PID of socket peer */
447     if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) != 0)
448     {
449         retval = SECURITY_SERVER_ERROR_SOCKET;
450         SEC_SVR_ERR("%s", "Error on getsockopt");
451         goto error;
452     }
453
454     /* All middlewares will run as root */
455     if (cr.uid != SECURITY_SERVER_DEVELOPER_UID)
456     {
457         retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
458         SEC_SVR_ERR("Non root process has called API: %d", cr.uid);
459         goto error;
460     }
461
462     /* Read executable path of the PID from proc fs */
463     exe = read_exe_path_from_proc(cr.pid);
464     if (exe == NULL)
465     {
466         /* It's weired. no file in proc file system, */
467         retval = SECURITY_SERVER_ERROR_FILE_OPERATION;
468         SEC_SVR_ERR("Error on opening /proc/%d/exe", cr.pid);
469         goto error;
470     }
471
472     /* Search exe of the peer that is really debug tool */
473     if (strcmp(exe, SECURITY_SERVER_DEBUG_TOOL_PATH) != 0)
474     {
475         SEC_SVR_ERR("Error: Wrong exe path [%s]", exe);
476         retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
477         goto error;
478     }
479     retval = SECURITY_SERVER_SUCCESS;
480     SEC_SVR_DBG("%s", "Client Authenticated");
481
482 error:
483     if (exe != NULL)
484         free(exe);
485
486     return retval;
487 }
488
489 int process_cookie_request(int sockfd)
490 {
491     int retval, client_pid, client_uid;
492     cookie_list *created_cookie = NULL;
493     unsigned char cookie[SECURITY_SERVER_COOKIE_LEN];
494     pid_t cookie_pid;
495     char *cookie_label = NULL;
496
497     /* Authenticate client */
498     retval = authenticate_client_application(sockfd, &client_pid, &client_uid);
499     if (retval != SECURITY_SERVER_SUCCESS)
500     {
501         SEC_SVR_ERR("%s", "Client Authentication Failed");
502         retval = send_generic_response(sockfd,
503             SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
504             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
505         if (retval != SECURITY_SERVER_SUCCESS)
506         {
507             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
508         }
509         goto error;
510     }
511     /* If client application is root process, just respond default cookie */
512     /*
513     if( client_uid == 0)
514     {
515         SEC_SVR_DBG("%s", "Requested application is a root process");
516         created_cookie = c_list;
517         if(c_list == NULL)
518         {
519             SEC_SVR_DBG("%s", "Cannot read default cookie");
520             goto error;
521         }
522     }
523     else
524     {
525     */
526     //TODO: Remove above code if there will be no crashes without it
527     //All process should be treaded the same
528
529     /* Create a new cookie. or find existing one */
530     pthread_mutex_lock(&cookie_mutex);
531     created_cookie = create_cookie_item(client_pid, sockfd, c_list);
532     if (created_cookie == NULL)
533     {
534         pthread_mutex_unlock(&cookie_mutex);
535         SEC_SVR_ERR("%s","Cannot create a cookie");
536         goto error;
537     }
538
539     //let others know if this cookie belongs to root process
540     if (client_uid == 0)
541         created_cookie->is_roots_process = 1;
542     else
543         created_cookie->is_roots_process = 0;
544     memcpy(cookie, created_cookie->cookie, SECURITY_SERVER_COOKIE_LEN);
545     cookie_pid = created_cookie->pid;
546     if (created_cookie->smack_label)
547         cookie_label = strdup(created_cookie->smack_label);
548     else
549         cookie_label = strdup("NULL");
550     pthread_mutex_unlock(&cookie_mutex);
551
552     //}
553     /* send cookie as response */
554     retval = send_cookie(sockfd, cookie);
555     if (retval != SECURITY_SERVER_SUCCESS)
556     {
557         SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
558     }
559     SEC_SVR_DBG("Server: Cookie created for client PID %d LABEL >%s<",
560         cookie_pid, cookie_label);
561
562     SEC_SVR_DBG("%s", "Server: Cookie has been sent to client");
563     free(cookie_label);
564
565 error:
566     return retval;
567 }
568
569 int process_check_privilege_request(int sockfd)
570 {
571     /* Authenticate client */
572     int retval, client_pid, requested_privilege;
573     int privileges[1];
574     unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
575     cookie_list *search_result = NULL;
576
577     retval = authenticate_client_middleware(sockfd, &client_pid);
578     if (retval != SECURITY_SERVER_SUCCESS)
579     {
580         SEC_SVR_ERR("%s", "Client Authentication Failed");
581         retval = send_generic_response(sockfd,
582             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
583             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
584         if (retval != SECURITY_SERVER_SUCCESS)
585         {
586             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
587         }
588         goto error;;
589     }
590
591     retval = recv_check_privilege_request(sockfd,
592         requested_cookie, &requested_privilege);
593     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
594     {
595         SEC_SVR_ERR("%s", "Receiving request failed");
596         retval = send_generic_response(sockfd,
597             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
598             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
599         if (retval != SECURITY_SERVER_SUCCESS)
600         {
601             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
602         }
603         goto error;;
604     }
605
606     if (requested_privilege < 1)
607     {
608         SEC_SVR_ERR("Requiring bad privilege [%d]", requested_privilege);
609         retval = send_generic_response(sockfd,
610             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
611             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
612         if (retval != SECURITY_SERVER_SUCCESS)
613         {
614             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
615         }
616         goto error;
617     }
618
619     /* Search cookie list */
620     pthread_mutex_lock(&cookie_mutex);
621     privileges[0] = requested_privilege;
622     search_result = search_cookie(c_list, requested_cookie, privileges, 1);
623     pthread_mutex_unlock(&cookie_mutex);
624     if (search_result != NULL)
625     {
626         /* We found */
627         SEC_SVR_DBG("We found the cookie with %d privilege and pid:%d", requested_privilege, client_pid);
628         SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
629         retval = send_generic_response(sockfd,
630             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
631             SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
632         if (retval != SECURITY_SERVER_SUCCESS)
633         {
634             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
635         }
636     }
637     else
638     {
639         /* It's not exist */
640         SEC_SVR_ERR("Could not find the cookie with %d privilege", requested_privilege);
641         retval = send_generic_response(sockfd,
642             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE,
643             SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
644         if (retval != SECURITY_SERVER_SUCCESS)
645         {
646             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
647         }
648     }
649 error:
650     return retval;
651 }
652
653 int process_check_privilege_new_request(int sockfd)
654 {
655     /* Authenticate client */
656     int retval, client_pid;
657     unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
658     cookie_list *search_result = NULL;
659     char object_label[MAX_OBJECT_LABEL_LEN + 1];
660     char access_rights[MAX_MODE_STR_LEN + 1];
661
662     retval = authenticate_client_middleware(sockfd, &client_pid);
663     if (retval != SECURITY_SERVER_SUCCESS)
664     {
665         SEC_SVR_ERR("%s", "Client Authentication Failed");
666         retval = send_generic_response(sockfd,
667             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
668             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
669         if (retval != SECURITY_SERVER_SUCCESS)
670         {
671             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
672         }
673         goto error;;
674     }
675
676     retval = recv_check_privilege_new_request(
677         sockfd, requested_cookie, object_label, access_rights);
678     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
679     {
680         SEC_SVR_ERR("%s", "Receiving request failed");
681         retval = send_generic_response(sockfd,
682             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
683             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
684         if (retval != SECURITY_SERVER_SUCCESS)
685         {
686             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
687         }
688         goto error;;
689     }
690
691     /* Search cookie list */
692     pthread_mutex_lock(&cookie_mutex);
693     search_result = search_cookie_new(c_list, requested_cookie, object_label, access_rights);
694     pthread_mutex_unlock(&cookie_mutex);
695
696     if (search_result != NULL)
697     {
698         /* We found */
699         SEC_SVR_DBG("We found the cookie with %s rights and pid:%d", access_rights, client_pid);
700         SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
701         retval = send_generic_response(sockfd,
702             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
703             SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED);
704         if (retval != SECURITY_SERVER_SUCCESS)
705         {
706             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
707         }
708     }
709     else
710     {
711         /* It's not exist */
712         SEC_SVR_ERR("Could not find the cookie with %s rights", access_rights);
713         retval = send_generic_response(sockfd,
714             SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_RESPONSE,
715             SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
716         if (retval != SECURITY_SERVER_SUCCESS)
717         {
718             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
719         }
720     }
721 error:
722     return retval;
723 }
724
725 int process_object_name_request(int sockfd)
726 {
727     int retval, client_pid, requested_privilege;
728     char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
729
730     /* Authenticate client */
731     retval = authenticate_client_middleware(sockfd, &client_pid);
732     if (retval != SECURITY_SERVER_SUCCESS)
733     {
734         SEC_SVR_ERR("%s", "Client Authentication Failed");
735         retval = send_generic_response(sockfd,
736             SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
737             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
738         if (retval != SECURITY_SERVER_SUCCESS)
739         {
740             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
741         }
742         goto error;
743     }
744
745     /* Receive GID */
746     retval = TEMP_FAILURE_RETRY(read(sockfd, &requested_privilege, sizeof(requested_privilege)));
747     if (retval < (int)sizeof(requested_privilege))
748     {
749         SEC_SVR_ERR("%s", "Receiving request failed");
750         retval = send_generic_response(sockfd,
751             SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
752             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
753         if (retval != SECURITY_SERVER_SUCCESS)
754         {
755             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
756         }
757         goto error;
758     }
759
760     /* Search from /etc/group */
761     retval = search_object_name(requested_privilege,
762         object_name,
763         SECURITY_SERVER_MAX_OBJ_NAME);
764     if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
765     {
766         /* It's not exist */
767         SEC_SVR_ERR("There is no such object for gid [%d]", requested_privilege);
768         retval = send_generic_response(sockfd,
769             SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
770             SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
771         if (retval != SECURITY_SERVER_SUCCESS)
772         {
773             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
774         }
775         goto error;
776     }
777     if (retval != SECURITY_SERVER_SUCCESS)
778     {
779         /* Error occurred */
780         SEC_SVR_ERR("Error on searching object name [%d]", retval);
781         retval = send_generic_response(sockfd,
782             SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_RESPONSE,
783             SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
784         if (retval != SECURITY_SERVER_SUCCESS)
785         {
786             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
787         }
788         goto error;
789     }
790
791     /* We found */
792     SECURE_LOGD("We found object: %s", object_name);
793     retval = send_object_name(sockfd, object_name);
794     if (retval != SECURITY_SERVER_SUCCESS)
795     {
796         SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
797     }
798
799 error:
800     return retval;
801 }
802
803 int process_gid_request(int sockfd, int msg_len)
804 {
805     int retval, client_pid;
806     char object_name[SECURITY_SERVER_MAX_OBJ_NAME];
807     /* Authenticate client as middleware daemon */
808     retval = authenticate_client_middleware(sockfd, &client_pid);
809     if (retval != SECURITY_SERVER_SUCCESS)
810     {
811         SEC_SVR_ERR("%s", "Client authentication failed");
812         retval = send_generic_response(sockfd,
813             SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
814             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
815         if (retval != SECURITY_SERVER_SUCCESS)
816         {
817             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
818         }
819         goto error;
820     }
821
822     if (msg_len >= SECURITY_SERVER_MAX_OBJ_NAME)
823     {
824         /* Too big ojbect name */
825         SEC_SVR_ERR("%s", "Object name is too big");
826         retval = send_generic_response(sockfd,
827             SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
828             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
829         if (retval != SECURITY_SERVER_SUCCESS)
830         {
831             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
832         }
833         goto error;
834     }
835
836     /* Receive group name */
837     retval = TEMP_FAILURE_RETRY(read(sockfd, object_name, msg_len));
838     if (retval < msg_len)
839     {
840         SECURE_LOGE("%s", "Failed to read object name");
841         retval = send_generic_response(sockfd,
842             SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
843             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
844         if (retval != SECURITY_SERVER_SUCCESS)
845         {
846             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
847         }
848         goto error;
849     }
850     object_name[msg_len] = 0;
851
852     /* Search /etc/group for the given group name */
853     retval = search_gid(object_name);
854     if (retval == SECURITY_SERVER_ERROR_NO_SUCH_OBJECT)
855     {
856         /* Not exist */
857         SECURE_LOGE("The object [%s] is not exist", object_name);
858         retval = send_generic_response(sockfd,
859             SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
860             SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT);
861         if (retval != SECURITY_SERVER_SUCCESS)
862         {
863             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
864         }
865         goto error;
866     }
867
868     if (retval < 0)
869     {
870         /* Error occurred */
871         SEC_SVR_ERR("Cannot send the response. %d", retval);
872         retval = send_generic_response(sockfd,
873             SECURITY_SERVER_MSG_TYPE_GID_RESPONSE,
874             SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
875         if (retval != SECURITY_SERVER_SUCCESS)
876         {
877             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
878         }
879
880         goto error;
881     }
882     /* We found */
883     retval = send_gid(sockfd, retval);
884     if (retval != SECURITY_SERVER_SUCCESS)
885     {
886         SEC_SVR_ERR("ERROR: Cannot gid response: %d", retval);
887     }
888 error:
889     return retval;
890 }
891
892 int process_pid_request(int sockfd)
893 {
894     int retval, client_pid;
895     unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
896     int *privileges = NULL;
897     cookie_list *search_result = NULL;
898     pid_t cookie_pid = 0;
899
900     /* Authenticate client */
901     retval = authenticate_client_middleware(sockfd, &client_pid);
902     if (retval != SECURITY_SERVER_SUCCESS)
903     {
904         SEC_SVR_ERR("%s", "Client Authentication Failed");
905         retval = send_generic_response(sockfd,
906             SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
907             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
908         if (retval != SECURITY_SERVER_SUCCESS)
909         {
910             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
911         }
912         goto error;
913     }
914
915     retval = recv_pid_request(sockfd, requested_cookie);
916     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
917     {
918         SEC_SVR_ERR("%s", "Receiving request failed");
919         retval = send_generic_response(sockfd,
920             SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
921             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
922         if (retval != SECURITY_SERVER_SUCCESS)
923         {
924             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
925         }
926         goto error;
927     }
928
929     retval = get_client_gid_list(sockfd, &privileges);
930     if (retval < 0)
931     {
932         SEC_SVR_ERR("ERROR: Cannot get GID list");
933         goto error;
934     }
935
936     /* Search cookie list */
937     pthread_mutex_lock(&cookie_mutex);
938     search_result = search_cookie(c_list, requested_cookie, privileges, retval);
939     if (search_result)
940         cookie_pid = search_result->pid;
941     pthread_mutex_unlock(&cookie_mutex);
942
943     free(privileges);
944
945     if (search_result != NULL)
946     {
947         /* We found */
948         SEC_SVR_DBG("We found the cookie and pid:%d", cookie_pid);
949         SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
950         retval = send_pid(sockfd, cookie_pid);
951
952         if (retval != SECURITY_SERVER_SUCCESS)
953         {
954             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
955         }
956     }
957     else
958     {
959         /* It's not exist */
960         SEC_SVR_ERR("%s", "Could not find the cookie");
961         retval = send_generic_response(sockfd,
962             SECURITY_SERVER_MSG_TYPE_PID_RESPONSE,
963             SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
964         if (retval != SECURITY_SERVER_SUCCESS)
965         {
966             SEC_SVR_ERR("ERROR: Cannot send pid response: %d", retval);
967         }
968     }
969 error:
970     return retval;
971 }
972
973 int process_smack_request(int sockfd)
974 {
975     int retval, client_pid;
976     int *privileges = NULL;
977     unsigned char requested_cookie[SECURITY_SERVER_COOKIE_LEN];
978     cookie_list *search_result = NULL;
979     //handler for SMACK label
980     char *label = NULL;
981
982     /* Authenticate client */
983     retval = authenticate_client_middleware(sockfd, &client_pid);
984     if (retval != SECURITY_SERVER_SUCCESS)
985     {
986         SEC_SVR_ERR("%s", "Client Authentication Failed");
987         retval = send_generic_response(sockfd,
988             SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
989             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
990         if (retval != SECURITY_SERVER_SUCCESS)
991         {
992             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
993         }
994         goto error;
995     }
996
997     retval = recv_smack_request(sockfd, requested_cookie);
998     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED)
999     {
1000         SEC_SVR_ERR("%s", "Receiving request failed");
1001         retval = send_generic_response(sockfd,
1002             SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
1003             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1004         if (retval != SECURITY_SERVER_SUCCESS)
1005         {
1006             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1007         }
1008         goto error;
1009     }
1010
1011     retval = get_client_gid_list(sockfd, &privileges);
1012     if (retval < 0)
1013     {
1014         SEC_SVR_ERR("ERROR: Cannot get GID list");
1015         goto error;
1016     }
1017
1018     /* Search cookie list */
1019     pthread_mutex_lock(&cookie_mutex);
1020     search_result = search_cookie(c_list, requested_cookie, privileges, retval);
1021     if (search_result) {
1022         if (search_result->smack_label)
1023             label = strdup(search_result->smack_label);
1024         else {
1025             SEC_SVR_DBG("%s", "No SMACK support on device - returning empty label");
1026             label = strdup("");
1027         }
1028     }
1029     pthread_mutex_unlock(&cookie_mutex);
1030
1031     free(privileges);
1032
1033     if (search_result != NULL)
1034     {
1035         /* We found */
1036         SEC_SVR_DBG("We found the cookie and pid:%d", search_result->pid);
1037         SEC_SVR_DBG("%s", "Cookie comparison succeeded. Access granted.");
1038         SEC_SVR_DBG("Read label is: %s\n", label);
1039
1040         retval = send_smack(sockfd, label);
1041
1042         if (retval != SECURITY_SERVER_SUCCESS)
1043         {
1044             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1045         }
1046     }
1047     else
1048     {
1049         /* It's not exist */
1050         SEC_SVR_ERR("%s", "Could not find the cookie");
1051         retval = send_generic_response(sockfd,
1052             SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE,
1053             SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE);
1054         if (retval != SECURITY_SERVER_SUCCESS)
1055         {
1056             SEC_SVR_ERR("ERROR: Cannot send SMACK label response: %d", retval);
1057         }
1058     }
1059     free(label);
1060 error:
1061     return retval;
1062 }
1063
1064 int process_pid_privilege_check(int sockfd, int datasize)
1065 {
1066     //In this function we parsing received PID privilege check request
1067     int retval;
1068     int client_pid;
1069     int pid;
1070     char *object = NULL;
1071     char *access_rights = NULL;
1072     unsigned char return_code;
1073     char *path = NULL;
1074     char subject[SMACK_LABEL_LEN + 1];
1075     subject[0] = '\0';
1076
1077     //authenticate client
1078     retval = authenticate_client_middleware(sockfd, &client_pid);
1079
1080     if (retval != SECURITY_SERVER_SUCCESS) {
1081         SEC_SVR_ERR("%s", "Client Authentication Failed");
1082         retval = send_generic_response(sockfd,
1083             SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1084             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1085
1086         if (retval != SECURITY_SERVER_SUCCESS)
1087             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1088
1089         goto error;
1090     }
1091
1092     //receive request
1093     retval = recv_pid_privilege_request(sockfd, datasize, &pid, &object, &access_rights);
1094
1095     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED) {
1096         SEC_SVR_ERR("%s", "Receiving request failed");
1097         retval = send_generic_response(sockfd,
1098             SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1099             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1100
1101         if (retval != SECURITY_SERVER_SUCCESS)
1102             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1103
1104         goto error;
1105     }
1106
1107     if (smack_check()) {
1108         retval = smack_pid_have_access(pid, object, access_rights);
1109         SEC_SVR_DBG("smack_pid_have_access returned %d", retval);
1110
1111         if (get_smack_label_from_process(pid, subject) != PC_OPERATION_SUCCESS) {
1112             // subject label is set to empty string
1113             SEC_SVR_ERR("get_smack_label_from_process failed. Subject label has not been read.");
1114         } else {
1115             SEC_SVR_DBG("Subject label of client PID %d is: %s", pid, subject);
1116         }
1117     } else {
1118         SEC_SVR_DBG("SMACK is not available. Subject label has not been read.");
1119         retval = 1;
1120     }
1121
1122     path = read_exe_path_from_proc(pid);
1123
1124     if (retval > 0)
1125         SECURE_LOGD("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", pid, subject, object, access_rights, retval, path);
1126     else
1127         SECURE_LOGE("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=%s, result=%d, caller_path=%s", pid, subject, object, access_rights, retval, path);
1128
1129     if (path != NULL)
1130         free(path);
1131
1132     if (retval == 1)   //there is permission
1133         return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1134     else                //there is no permission
1135         return_code = SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED;
1136
1137     //send response
1138     retval = send_generic_response(sockfd,
1139         SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_RESPONSE,
1140         return_code);
1141
1142     if (retval != SECURITY_SERVER_SUCCESS)
1143         SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1144
1145 error:
1146
1147     if (object != NULL)
1148         free(object);
1149     if (access_rights != NULL)
1150         free(access_rights);
1151
1152     return retval;
1153 }
1154
1155 int process_tool_request(int client_sockfd, int server_sockfd)
1156 {
1157     int retval, argcnum = 0;
1158     char **recved_argv = NULL;
1159
1160     /* Authenticate client */
1161     retval = authenticate_developer_shell(client_sockfd);
1162     if (retval != SECURITY_SERVER_SUCCESS)
1163     {
1164         SEC_SVR_ERR("%s", "Client Authentication Failed");
1165         retval = send_generic_response(client_sockfd,
1166             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1167             SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1168         if (retval != SECURITY_SERVER_SUCCESS)
1169         {
1170             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1171         }
1172         goto error;
1173     }
1174
1175     /* Receive Total number of argv */
1176     retval = TEMP_FAILURE_RETRY(read(client_sockfd, &argcnum, sizeof(int)));
1177     if ((retval < (int)sizeof(int)) || argcnum > (UINT_MAX / sizeof(char*)) - 2 || argcnum < 0)
1178     {
1179         SEC_SVR_ERR("Error: argc recieve failed: %d", retval);
1180         retval = send_generic_response(client_sockfd,
1181             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1182             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1183         if (retval != SECURITY_SERVER_SUCCESS)
1184         {
1185             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1186         }
1187         goto error;
1188     }
1189     argcnum += 2;
1190     recved_argv = (char**)malloc(sizeof(char*) * argcnum);
1191     if (recved_argv == NULL)
1192     {
1193         SEC_SVR_ERR("Error: malloc() failed: %d", retval);
1194         retval = send_generic_response(client_sockfd,
1195             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1196             SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1197         if (retval != SECURITY_SERVER_SUCCESS)
1198         {
1199             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1200         }
1201         goto error;
1202     }
1203     memset(recved_argv, 0, sizeof(char*) * argcnum);
1204
1205     retval = recv_launch_tool_request(client_sockfd, argcnum - 1, recved_argv);
1206     if (retval == SECURITY_SERVER_ERROR_RECV_FAILED || retval == SECURITY_SERVER_ERROR_OUT_OF_MEMORY)
1207     {
1208         SEC_SVR_ERR("%s", "Receiving request failed");
1209         retval = send_generic_response(client_sockfd,
1210             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1211             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1212         if (retval != SECURITY_SERVER_SUCCESS)
1213         {
1214             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1215         }
1216         goto error;
1217     }
1218     if (argcnum < 2)
1219     {
1220         SEC_SVR_ERR("Error: Too small number of argv [%d]", argcnum);
1221         retval = send_generic_response(client_sockfd,
1222             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1223             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1224         if (retval != SECURITY_SERVER_SUCCESS)
1225         {
1226             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1227         }
1228         goto error;
1229     }
1230     /* Execute the command */
1231     retval = execute_debug_tool(argcnum, recved_argv, server_sockfd, client_sockfd);
1232     if (retval != SECURITY_SERVER_SUCCESS)
1233     {
1234         SEC_SVR_ERR("Error: Cannot execute debug tool [%d]", retval);
1235         retval = send_generic_response(client_sockfd,
1236             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1237             SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1238         if (retval != SECURITY_SERVER_SUCCESS)
1239         {
1240             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1241         }
1242     }
1243     else
1244     {
1245         SEC_SVR_DBG("%s", "Tool has been executed");
1246         retval = send_generic_response(client_sockfd,
1247             SECURITY_SERVER_MSG_TYPE_TOOL_RESPONSE,
1248             SECURITY_SERVER_RETURN_CODE_SUCCESS);
1249         if (retval != SECURITY_SERVER_SUCCESS)
1250         {
1251             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1252         }
1253     }
1254 error:
1255     free_argv(recved_argv, argcnum);
1256     return retval;
1257 }
1258
1259
1260 /* Send exe path response to client
1261  *
1262  * Get exe path response packet format
1263  *  0                   1                   2                   3
1264  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1265  * |---------------------------------------------------------------|
1266  * | version=0x01  |MessageID=0x24 |Message Length = 4+path length |
1267  * |---------------------------------------------------------------|
1268  * |  return code  |  Path length  |             Path              |
1269  * |---------------------------------------------------------------|
1270
1271 */
1272 int send_exe_path_response(int sockfd, const char *path)
1273 {
1274     response_header hdr;
1275     unsigned char *msg = NULL;
1276     unsigned char *ptr = NULL;
1277     int ret;
1278     size_t path_len = 0;
1279     unsigned short msg_len = 0;
1280
1281     if (!path) {
1282         SEC_SVR_ERR("Path is NULL");
1283         return SECURITY_SERVER_ERROR_INPUT_PARAM;
1284     }
1285
1286     path_len = strlen(path);
1287     msg_len = sizeof(hdr) + sizeof(size_t) + path_len;
1288     msg = (unsigned char*)malloc(msg_len * sizeof(unsigned char));
1289     if (!msg) {
1290         SEC_SVR_ERR("malloc failed");
1291         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1292     }
1293
1294     /* Assemble header */
1295     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
1296     hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE;
1297     hdr.basic_hdr.msg_len = sizeof(size_t) + path_len;
1298     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1299
1300     /* Prepare packet */
1301     ptr = msg;
1302     memcpy(ptr, &hdr, sizeof(hdr));
1303     ptr += sizeof(hdr);
1304     memcpy(ptr, &path_len, sizeof(size_t));
1305     ptr += sizeof(size_t);
1306     memcpy(ptr, path, path_len);
1307
1308     /* Check poll */
1309     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1310     if (ret == SECURITY_SERVER_ERROR_POLL)
1311     {
1312         SEC_SVR_ERR("%s", "poll() error");
1313         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1314         goto out;
1315     }
1316     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
1317     {
1318         SEC_SVR_ERR("%s", "poll() timeout");
1319         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1320         goto out;
1321     }
1322
1323     /* Send it */
1324     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, msg_len));
1325     if (ret < msg_len)
1326     {
1327         SEC_SVR_ERR("Error on write(): %d", ret);
1328         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
1329         goto out;
1330     }
1331     ret = SECURITY_SERVER_SUCCESS;
1332
1333 out:
1334     free(msg);
1335     return ret;
1336 }
1337
1338
1339 int process_exe_path_request(int sockfd)
1340 {
1341     pid_t pid;
1342     int retval;
1343     char *exe = NULL;
1344
1345     // read pid
1346     retval = TEMP_FAILURE_RETRY(read(sockfd, &pid, sizeof(pid_t)));
1347     if (retval < (ssize_t) sizeof(pid_t))
1348     {
1349         SEC_SVR_ERR("Server Error: recieve failed: %d", retval);
1350         retval = send_generic_response(
1351             sockfd,
1352             SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE,
1353             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1354
1355         if (retval != SECURITY_SERVER_SUCCESS)
1356             SEC_SVR_ERR("Server ERROR: Cannot send generic response: %d", retval);
1357         goto error;
1358     }
1359
1360     SEC_SVR_DBG("Server: Get exe path request for pid %d", pid);
1361
1362     // get executable path
1363     exe = read_exe_path_from_proc(pid);
1364     if (!exe)
1365     {
1366         SEC_SVR_ERR("Server: Failed to read executable path for pid %d", pid);
1367         retval = send_generic_response(
1368             sockfd,
1369             SECURITY_SERVER_MSG_TYPE_EXE_PATH_RESPONSE,
1370             SECURITY_SERVER_RETURN_CODE_SERVER_ERROR);
1371
1372         if (retval != SECURITY_SERVER_SUCCESS)
1373             SEC_SVR_ERR("Server ERROR: Cannot send generic response: %d", retval);
1374         goto error;
1375     }
1376
1377     // send response
1378     retval = send_exe_path_response(sockfd, exe);
1379     if (retval != SECURITY_SERVER_SUCCESS)
1380         SEC_SVR_ERR("ERROR: Cannot send exe path response: %d", retval);
1381
1382 error:
1383     free(exe);
1384     return retval;
1385 }
1386
1387 int client_has_access(int sockfd, const char *object)
1388 {
1389     char *label = NULL;
1390     int ret = 0;
1391     int pid = -1;
1392     int uid = -1;
1393     int retval;
1394     struct ucred socopt;
1395     unsigned int socoptSize = sizeof(socopt);
1396
1397     if (smack_check())
1398     {
1399         retval = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &socopt, &socoptSize);
1400         if (retval != 0) {
1401             SEC_SVR_DBG("%s", "Error on getsockopt");
1402             return 0;
1403         }
1404         //now we have PID in sockopt.pid
1405
1406         if (smack_new_label_from_socket(sockfd, &label)) {
1407             SEC_SVR_ERR("%s", "Error on smack_new_label_from_socket");
1408             label = NULL;
1409         }
1410
1411         if (0 >= (ret = smack_pid_have_access(socopt.pid, object, "rw"))) {
1412             ret = 0;
1413         }
1414     }
1415
1416     if (SECURITY_SERVER_SUCCESS == authenticate_client_application(sockfd, &pid, &uid))
1417         SEC_SVR_DBG("SS_SMACK: caller_pid=%d, subject=%s, object=%s, access=rw, result=%d",
1418             pid, label, object, ret);
1419
1420     free(label);
1421     return ret;
1422 }
1423
1424 void *security_server_thread(void *param)
1425 {
1426     int client_sockfd = -1, client_uid, client_pid;
1427     int server_sockfd, retval;
1428     basic_header basic_hdr;
1429     struct security_server_thread_param *my_param;
1430
1431     my_param = (struct security_server_thread_param*) param;
1432     client_sockfd = my_param->client_sockfd;
1433     server_sockfd = my_param->server_sockfd;
1434
1435     /* Receive request header */
1436     retval = recv_hdr(client_sockfd, &basic_hdr);
1437     if (retval == SECURITY_SERVER_ERROR_TIMEOUT || retval == SECURITY_SERVER_ERROR_RECV_FAILED
1438         || retval == SECURITY_SERVER_ERROR_SOCKET)
1439     {
1440         SEC_SVR_ERR("Receiving header error [%d]",retval);
1441         close(client_sockfd);
1442         client_sockfd = -1;
1443         goto error;;
1444     }
1445
1446     if (retval != SECURITY_SERVER_SUCCESS)
1447     {
1448         /* Response */
1449         SEC_SVR_ERR("Receiving header error [%d]",retval);
1450         retval = send_generic_response(client_sockfd,
1451             SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1452             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1453         if (retval != SECURITY_SERVER_SUCCESS)
1454         {
1455             SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1456             goto error;
1457         }
1458         safe_server_sock_close(client_sockfd);
1459         client_sockfd = -1;
1460         goto error;
1461     }
1462
1463     //TODO: Below authorize_SS_API_caller_socket() is used for authorize API caller by SMACK,
1464     //      at the moment return value is not checked and each access is allowed.
1465     //      If we realy want to restrict access it must be changed in future.
1466
1467     /* Act different for request message ID */
1468     switch (basic_hdr.msg_id)
1469     {
1470         case SECURITY_SERVER_MSG_TYPE_COOKIE_REQUEST:
1471             SEC_SVR_DBG("%s", "Cookie request received");
1472             authorize_SS_API_caller_socket(client_sockfd, API_FREE_ACCESS, API_RULE_REQUIRED);
1473             process_cookie_request(client_sockfd);
1474             break;
1475
1476         case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_REQUEST:
1477             SEC_SVR_DBG("%s", "Privilege check received");
1478             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1479             process_check_privilege_request(client_sockfd);
1480             break;
1481
1482         case SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_REQUEST:
1483             SEC_SVR_DBG("%s", "Privilege check (new mode) received");
1484             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1485             process_check_privilege_new_request(client_sockfd);
1486             break;
1487
1488         case SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_REQUEST:
1489             SEC_SVR_DBG("%s", "Get object name request received");
1490             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1491             process_object_name_request(client_sockfd);
1492             break;
1493
1494         case SECURITY_SERVER_MSG_TYPE_GID_REQUEST:
1495             SEC_SVR_DBG("%s", "Get GID received");
1496             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1497             process_gid_request(client_sockfd, (int)basic_hdr.msg_len);
1498             break;
1499
1500         case SECURITY_SERVER_MSG_TYPE_PID_REQUEST:
1501             SEC_SVR_DBG("%s", "pid request received");
1502             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1503             process_pid_request(client_sockfd);
1504             break;
1505
1506         case SECURITY_SERVER_MSG_TYPE_SMACK_REQUEST:
1507             SEC_SVR_DBG("%s", "SMACK label request received");
1508             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1509             process_smack_request(client_sockfd);
1510             break;
1511
1512         case SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_REQUEST:
1513             SEC_SVR_DBG("%s", "PID privilege check request received");
1514             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1515             //pass data size to function
1516             process_pid_privilege_check(client_sockfd, basic_hdr.msg_len);
1517             break;
1518
1519         case SECURITY_SERVER_MSG_TYPE_TOOL_REQUEST:
1520             SEC_SVR_DBG("%s", "launch tool request received");
1521             authorize_SS_API_caller_socket(client_sockfd, API_MIDDLEWARE, API_RULE_REQUIRED);
1522             process_tool_request(client_sockfd, server_sockfd);
1523             break;
1524
1525         case SECURITY_SERVER_MSG_TYPE_VALID_PWD_REQUEST:
1526             SECURE_LOGD("%s", "Server: validate password request received");
1527             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_CHECK, API_RULE_REQUIRED);
1528             process_valid_pwd_request(client_sockfd);
1529             break;
1530
1531         case SECURITY_SERVER_MSG_TYPE_SET_PWD_REQUEST:
1532             SECURE_LOGD("%s", "Server: set password request received");
1533             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1534             process_set_pwd_request(client_sockfd);
1535             break;
1536
1537         case SECURITY_SERVER_MSG_TYPE_RESET_PWD_REQUEST:
1538             SECURE_LOGD("%s", "Server: reset password request received");
1539             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1540             process_reset_pwd_request(client_sockfd);
1541             break;
1542
1543         case SECURITY_SERVER_MSG_TYPE_CHK_PWD_REQUEST:
1544             SECURE_LOGD("%s", "Server: check password request received");
1545             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_CHECK, API_RULE_REQUIRED);
1546             process_chk_pwd_request(client_sockfd);
1547             break;
1548
1549         case SECURITY_SERVER_MSG_TYPE_SET_PWD_HISTORY_REQUEST:
1550             SECURE_LOGD("%s", "Server: set password histroy request received");
1551             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1552             process_set_pwd_history_request(client_sockfd);
1553             break;
1554
1555         case SECURITY_SERVER_MSG_TYPE_SET_PWD_MAX_CHALLENGE_REQUEST:
1556             SECURE_LOGD("%s", "Server: set password max challenge request received");
1557             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1558             process_set_pwd_max_challenge_request(client_sockfd);
1559             break;
1560
1561         case SECURITY_SERVER_MSG_TYPE_SET_PWD_VALIDITY_REQUEST:
1562             SECURE_LOGD("%s", "Server: set password validity request received");
1563             authorize_SS_API_caller_socket(client_sockfd, API_PASSWD_SET, API_RULE_REQUIRED);
1564             process_set_pwd_validity_request(client_sockfd);
1565             break;
1566
1567         case SECURITY_SERVER_MSG_TYPE_APP_GIVE_ACCESS_REQUEST:
1568             SEC_SVR_DBG("%s", "Server: app give access request received");
1569             authorize_SS_API_caller_socket(client_sockfd, API_DATA_SHARE, API_RULE_REQUIRED);
1570             if (client_has_access(client_sockfd, API_DATA_SHARE)) {
1571                 SEC_SVR_DBG("%s", "Server: app give access request received");
1572                 if (basic_hdr.msg_len >= 0 && (size_t)basic_hdr.msg_len >= sizeof(basic_hdr)) {
1573                     process_app_get_access_request(client_sockfd,
1574                         basic_hdr.msg_len - sizeof(basic_hdr));
1575                 } else {
1576                     SEC_SVR_ERR("ERROR: Invalid message length: %d", basic_hdr.msg_len);
1577                 }
1578             } else {
1579                 SEC_SVR_DBG("%s", "Server: app give access request received (API DENIED - request will not proceed)");
1580                 send_generic_response(client_sockfd,
1581                     SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1582                     SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED);
1583             }
1584             break;
1585         /************************************************************************************************/
1586         /* Just for test. This code must be removed on release */
1587         case SECURITY_SERVER_MSG_TYPE_GET_ALL_COOKIES_REQUEST:
1588             SEC_SVR_DBG("%s", "all cookie info request received -- NEED TO BE DELETED ON RELEASE");
1589             retval = authenticate_client_application(client_sockfd, &client_pid, &client_uid);
1590             if (retval != SECURITY_SERVER_SUCCESS)
1591             {
1592                 SEC_SVR_ERR("%s", "Client Authentication Failed");
1593                 retval = send_generic_response(client_sockfd,
1594                     SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1595                     SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1596                 if (retval != SECURITY_SERVER_SUCCESS)
1597                 {
1598                     SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1599                 }
1600                 break;
1601             }
1602             retval = util_process_all_cookie(client_sockfd, c_list);
1603             if (retval != SECURITY_SERVER_SUCCESS)
1604             {
1605                 SEC_SVR_ERR("ERROR: Cannot send all cookie info: %d", retval);
1606             }
1607             break;
1608
1609         case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_PID_REQUEST:
1610             SEC_SVR_DBG("%s", "cookie info from pid request received -- NEED TO BE DELETED ON RELEASE");
1611             if (retval != SECURITY_SERVER_SUCCESS)
1612             {
1613                 SEC_SVR_ERR("%s", "Client Authentication Failed");
1614                 retval = send_generic_response(client_sockfd,
1615                     SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1616                     SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1617                 if (retval != SECURITY_SERVER_SUCCESS)
1618                 {
1619                     SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1620                 }
1621                 break;
1622             }
1623             util_process_cookie_from_pid(client_sockfd, c_list);
1624             break;
1625
1626         case SECURITY_SERVER_MSG_TYPE_GET_COOKIEINFO_FROM_COOKIE_REQUEST:
1627             SEC_SVR_DBG("%s", "cookie info from cookie request received -- NEED TO BE DELETED ON RELEASE");
1628             if (retval != SECURITY_SERVER_SUCCESS)
1629             {
1630                 SEC_SVR_ERR("%s", "Client Authentication Failed");
1631                 retval = send_generic_response(client_sockfd,
1632                     SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1633                     SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED);
1634                 if (retval != SECURITY_SERVER_SUCCESS)
1635                 {
1636                     SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1637                 }
1638                 break;
1639             }
1640             util_process_cookie_from_cookie(client_sockfd, c_list);
1641             break;
1642         /************************************************************************************************/
1643
1644
1645         default:
1646             SEC_SVR_ERR("Unknown msg ID :%d", basic_hdr.msg_id);
1647             /* Unknown message ID */
1648             retval = send_generic_response(client_sockfd,
1649             SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE,
1650             SECURITY_SERVER_RETURN_CODE_BAD_REQUEST);
1651             if (retval != SECURITY_SERVER_SUCCESS)
1652             {
1653                 SEC_SVR_ERR("ERROR: Cannot send generic response: %d", retval);
1654             }
1655             break;
1656     }
1657
1658     if (client_sockfd > 0)
1659     {
1660         safe_server_sock_close(client_sockfd);
1661         client_sockfd = -1;
1662     }
1663
1664 error:
1665     if (client_sockfd > 0)
1666         close(client_sockfd);
1667     thread_status[my_param->thread_status] = 0;
1668     pthread_detach(pthread_self());
1669     pthread_exit(NULL);
1670 }
1671
1672 void *security_server_main_thread(void *data)
1673 {
1674     int server_sockfd = 0, retval, client_sockfd = -1, rc;
1675     struct sigaction act, dummy;
1676     pthread_t threads[SECURITY_SERVER_NUM_THREADS];
1677     struct security_server_thread_param param[SECURITY_SERVER_NUM_THREADS];
1678
1679     (void)data;
1680
1681     SECURE_LOGD("%s", "Starting Security Server main thread");
1682
1683     /* security server must be executed by root */
1684     if (getuid() != 0)
1685     {
1686         fprintf(stderr, "%s\n", "You are not root. exiting...");
1687         goto error;
1688     }
1689
1690     for (retval = 0; retval < SECURITY_SERVER_NUM_THREADS; retval++)
1691         thread_status[retval] = 0;
1692     initiate_try();
1693
1694     /* Create and bind a Unix domain socket */
1695     retval = create_new_socket(&server_sockfd);
1696     if (retval != SECURITY_SERVER_SUCCESS)
1697     {
1698         SEC_SVR_ERR("%s", "cannot create socket. exiting...");
1699         goto error;
1700     }
1701
1702     if (listen(server_sockfd, 5) < 0)
1703     {
1704         SEC_SVR_ERR("%s", "listen() failed. exiting...");
1705         goto error;
1706     }
1707
1708     /* Create a default cookie --> Cookie for root process */
1709     c_list = create_default_cookie();
1710     if (c_list == NULL)
1711     {
1712         SEC_SVR_ERR("%s", "cannot make a default cookie. exiting...");
1713         goto error;
1714     }
1715
1716     /* Init signal handler */
1717     act.sa_handler = NULL;
1718     act.sa_sigaction = security_server_sig_child;
1719     sigemptyset(&act.sa_mask);
1720     act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
1721
1722     if (sigaction(SIGCHLD, &act, &dummy) < 0)
1723     {
1724         SEC_SVR_ERR("%s", "cannot change session");
1725     }
1726
1727     pthread_mutex_init(&cookie_mutex, NULL);
1728
1729     while (1)
1730     {
1731         /* Accept a new client */
1732         if (client_sockfd < 0)
1733             client_sockfd = accept_client(server_sockfd);
1734
1735         if (client_sockfd == SECURITY_SERVER_ERROR_TIMEOUT)
1736             continue;
1737         if (client_sockfd < 0)
1738             goto error;
1739         SEC_SVR_DBG("Server: new connection has been accepted: %d", client_sockfd);
1740         retval = 0;
1741         while (1)
1742         {
1743             if (thread_status[retval] == 0)
1744             {
1745                 thread_status[retval] = 1;
1746                 param[retval].client_sockfd = client_sockfd;
1747                 param[retval].server_sockfd = server_sockfd;
1748                 param[retval].thread_status = retval;
1749                 SEC_SVR_DBG("Server: Creating a new thread: %d", retval);
1750                 rc = pthread_create(&threads[retval], NULL, security_server_thread, (void*)&param[retval]);
1751                 if (rc)
1752                 {
1753                     SEC_SVR_ERR("Error: Server: Cannot create thread:%d", rc);
1754                     goto error;
1755                 }
1756                 break;
1757             }
1758             retval++;
1759             if (retval >= SECURITY_SERVER_NUM_THREADS)
1760                 retval = 0;
1761         }
1762         client_sockfd = -1;
1763     }
1764 error:
1765     if (server_sockfd > 0)
1766         close(server_sockfd);
1767
1768     pthread_detach(pthread_self());
1769     pthread_exit(NULL);
1770 }
1771
1772 ssize_t read_wrapper(int sockfd, void *buffer, size_t len)
1773 {
1774     unsigned char *buff = (unsigned char*)buffer;
1775     ssize_t done = 0;
1776     while (done < (int)len) {
1777         struct pollfd fds = { sockfd, POLLIN, 0};
1778         if (0 >= poll(&fds, 1, 1000))
1779             break;
1780         ssize_t ret = read(sockfd, buff + done, len - done);
1781         if (0 < ret) {
1782             done += ret;
1783             continue;
1784         }
1785         if (0 == ret)
1786             break;
1787         if (-1 == ret && EAGAIN != errno && EINTR != errno)
1788             break;
1789     }
1790     return done;
1791 }
1792
1793 int process_app_get_access_request(int sockfd, size_t msg_len)
1794 {
1795     char *message_buffer = NULL;
1796     char *client_label = NULL;
1797     char *provider_label = NULL;
1798     struct smack_accesses *smack = NULL;
1799     int ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
1800     int send_message_id = SECURITY_SERVER_MSG_TYPE_GENERIC_RESPONSE;
1801     int send_error_id = SECURITY_SERVER_RETURN_CODE_SERVER_ERROR;
1802     int client_pid = 0;
1803     static const char*const revoke = "-----";
1804     const char *permissions = "rwxat";
1805
1806     message_buffer = malloc(msg_len + 1);
1807     if (!message_buffer)
1808         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1809     message_buffer[msg_len] = 0;
1810
1811     ssize_t retval = read_wrapper(sockfd, message_buffer, msg_len);
1812
1813     if (retval < (ssize_t)msg_len) {
1814         SEC_SVR_ERR("%s", "Error in read. Message too short");
1815         send_error_id = SECURITY_SERVER_RETURN_CODE_BAD_REQUEST;
1816         ret = SECURITY_SERVER_ERROR_BAD_REQUEST;
1817         goto error;
1818     }
1819
1820     // Currently we don't use client_pid
1821     memcpy(&client_pid, message_buffer, sizeof(int));
1822     client_label = message_buffer + sizeof(int);
1823
1824     if (smack_check()) {
1825         if (0 != smack_new_label_from_socket(sockfd, &provider_label)) {
1826             SEC_SVR_ERR("%s", "Error in smack_new_label_from_socket");
1827             goto error;
1828         }
1829
1830         if (!util_smack_label_is_valid(client_label)) {
1831             send_error_id = SECURITY_SERVER_RETURN_CODE_BAD_REQUEST;
1832             goto error;
1833         }
1834
1835         if (smack_accesses_new(&smack))
1836             goto error;
1837
1838         if (smack_accesses_add_modify(smack, client_label,
1839                 provider_label, permissions, revoke))
1840             goto error;
1841
1842         if (smack_accesses_apply(smack)) {
1843             send_message_id = SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED;
1844             goto error;
1845         }
1846     }
1847
1848     ret = SECURITY_SERVER_SUCCESS;
1849     send_message_id = SECURITY_SERVER_MSG_TYPE_APP_GIVE_ACCESS_RESPONSE;
1850     send_error_id = SECURITY_SERVER_RETURN_CODE_SUCCESS;
1851
1852
1853 error:
1854     retval = send_generic_response(sockfd, send_message_id, send_error_id);
1855     if (retval != SECURITY_SERVER_SUCCESS)
1856         SEC_SVR_ERR("Server ERROR: Cannot send response: %d", retval);
1857
1858     free(message_buffer);
1859     free(provider_label);
1860     smack_accesses_free(smack);
1861     return ret;
1862 }
1863
1864 void *system_observer_main_thread(void *data)
1865 {
1866     system_observer_main(data);
1867     SEC_SVR_ERR("%s", "System observer: exit. No garbage collector support.");
1868     netlink_enabled = 0;
1869     pthread_detach(pthread_self());
1870     pthread_exit(NULL);
1871 }
1872
1873 int main(int argc, char *argv[])
1874 {
1875     int res;
1876     pthread_t main_thread;
1877
1878     (void)argc;
1879     (void)argv;
1880
1881     // create observer thread only if smack is enabled
1882     if (smack_check()) {
1883         pthread_t system_observer;
1884         system_observer_config so_config;
1885         so_config.event_callback = rules_revoker_callback;
1886
1887         res = pthread_create(&system_observer, NULL, system_observer_main_thread, (void*)&so_config);
1888
1889         if (res != 0)
1890             return -1;
1891     }
1892     else {
1893         SEC_SVR_DBG("SMACK is not available. Observer thread disabled.");
1894     }
1895
1896     res = pthread_create(&main_thread, NULL, security_server_main_thread, NULL);
1897     if (res == 0)
1898     {
1899         while (1)
1900             sleep(60);
1901     }
1902     else
1903     {
1904         SECURE_LOGE("Error: Server: Cannot create main security server thread: %d", res);
1905     }
1906     pthread_exit(NULL);
1907     return 0;
1908 }
1909