009d62137d91425c8b84c26f017ea9fc2f9e2b34
[cilux] / src / platform / linux / cilux.c
1
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <pwd.h>
5 #include <grp.h>
6
7 #include <platform.h>
8 #include <container.h>
9 #include "version.h"
10
11 /* -------------------------------------------------------------------------- */
12
13 static void re_exec_as_other();
14
15 /* -------------------------------------------------------------------------- */
16
17 int main(int argc, char* argv[])
18 {
19         c_init(0, cilux_version, cilux_ciux, 0);
20         re_exec_as_other();
21         c_run(0);
22
23         return 0;
24 }
25
26 void re_exec_as_other()
27 {
28         char* other="other";
29         uid_t uid=0;
30         gid_t gid=0;
31         if(getuid()==0){
32                 struct passwd* pw=getpwnam(other);
33                 if(!pw){ printf("fail: getpwnam(\"%s\");\n", other); exit(1); }
34                 uid=pw->pw_uid;
35                 gid=pw->pw_gid;
36         }
37
38         pid_t pid=fork();
39         switch(pid){
40                 case 0:
41                         break;
42                 case -1:
43                         printf("Failed to fork\n");
44                         exit(1);
45                 default:
46                         printf("pid %d\n", pid);
47                         exit(0);
48         }
49         setsid();
50
51         if(getuid()==0){
52                 if(setgroups(0,0)== -1){
53                         printf("fail: setgroups(0,0)\n");
54                         exit(1);
55                 }
56                 if(setgid(gid)== -1){
57                         printf("fail: setgid(%d)\n", gid);
58                         exit(1);
59                 }
60                 if(initgroups(other, gid)== -1){
61                         printf("fail: initgroups(\"%s\",%d)\n", other, gid);
62                         exit(1);
63                 }
64                 if(setuid(uid)== -1){
65                         printf("fail: setuid(%d)\n", uid);
66                         exit(1);
67                 }
68         }
69 }
70
71 /* -------------------------------------------------------------------------- */
72
73