Tizen 2.1 base
[platform/core/security/libprivilege-control.git] / src / slp-su.c
1 /*
2  * libprivilege control
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Kidong Kim <kd0228.kim@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 <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 #include "privilege-control.h"
29
30 void print_usage(void)
31 {
32         printf("%s", "Usage: slp-su [PKG_NAME]\n\n");
33         printf("%s", "Execute new shell which be belonged to user related with PKG_NAME\n\n");
34 }
35
36 int main(int argc, char* argv[])
37 {
38         pid_t pid = -1;
39         char* buf = NULL;
40
41         if(argc != 2)
42         {
43                 fprintf(stderr, "%s", "[ERR] Check your argument.\n\n");
44                 print_usage();
45                 return 0;
46         }
47
48         pid = fork();
49         switch(pid)
50         {
51                 case 0:         // child
52                         {
53                                 if(set_app_privilege(argv[1], NULL, NULL) == 0) // success
54                                 {
55                                         fprintf(stderr, "%s", "[LOG] Success to execute set_privilege()\n");
56                                 }
57                                 else
58                                 {
59                                         fprintf(stderr, "%s", "[ERR] Fail to execute set_privilege()\n");
60                                         exit(1);
61                                 }
62
63                                 buf = getenv("HOME");
64                                 if(buf == NULL) // fail
65                                 {
66                                         fprintf(stderr, "%s", "[ERR] Fail to execute getenv()\n");
67                                         exit(0);
68                                 }
69                                 else
70                                 {
71                                         fprintf(stderr, "%s: [%s]%s", "[LOG] HOME", buf, "\n");
72                                 }
73                                 
74                                 if(chdir(buf) == 0)     // success
75                                 {
76                                         fprintf(stderr, "%s", "[LOG] Success to change working directory\n");
77                                 }
78                                 else
79                                 {
80                                         fprintf(stderr, "%s", "[ERR] Fail to execute chdir()\n");
81                                         exit(0);
82                                 }
83                                 
84                                 execl("/bin/sh", "/bin/sh", NULL);
85                                 break;
86                         }
87                 case -1:        // error
88                         {
89                                 fprintf(stderr, "%s", "[ERR] Fail to execute fork()\n");
90                                 exit(1);
91                                 break;
92                         }
93                 default:        // parent
94                         {
95                                 wait((int*)0);
96                                 fprintf(stderr, "%s", "[LOG] Parent end\n");
97                                 exit(0);
98                         }
99         }
100
101         return 1;
102 }