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