3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on chcontext.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 chcontext is a wrapper to user the new_s_context system call. It
22 does little more than mapping command line option to the system call
40 fprintf (stderr,"chcontext version %s\n",VERSION);
42 ,"chcontext [ options ] command arguments ...\n"
44 "chcontext allocate a new security context and executes\n"
45 "a command in that context.\n"
46 "By default, a new/unused context is allocated\n"
50 "\tAdd a capability from the command. This option may be\n"
51 "\trepeated several time.\n"
52 "\tSee /usr/include/linux/capability.h\n"
53 "\tIn general, this option is used with the --secure option\n"
54 "\t--secure removes most critical capabilities and --cap\n"
55 "\tadds specific ones.\n"
59 "\tRemove a capability from the command. This option may be\n"
60 "\trepeated several time.\n"
61 "\tSee /usr/include/linux/capability.h\n"
64 "\tSelect the context. On root in context 0 is allowed to\n"
65 "\tselect a specific context.\n"
66 "\tContext number 1 is special. It can see all processes\n"
67 "\tin any contexts, but can't kill them though.\n"
68 "\tOption --ctx may be repeated several times to specify up to 16 contexts.\n"
71 "\tStart the command in background and make the process\n"
72 "\ta child of process 1.\n"
74 "--domainname new_domainname\n"
75 "\tSet the domainname (NIS) in the new security context.\n"
76 "\tUse \"none\" to unset the domain name.\n"
79 "\tSet one flag in the new or current security context. The following\n"
80 "\tflags are supported. The option may be used several time.\n"
82 "\tfakeinit: The new process will believe it is process number 1.\n"
83 " Useful to run a real /sbin/init in a vserver.\n"
84 "\tlock: The new process is trapped and can't use chcontext anymore.\n"
85 "\tsched: The new process and its children will share a common \n"
86 " execution priority.\n"
87 "\tnproc: Limit the number of process in the vserver according to\n"
88 " ulimit setting. Normally, ulimit is a per user thing.\n"
89 " With this flag, it becomes a per vserver thing.\n"
90 "\tprivate: No one can join this security context once created.\n"
91 "\tulimit: Apply the current ulimit to the whole context\n"
93 "--hostname new_hostname\n"
94 "\tSet the hostname in the new security context\n"
95 "\tThis is need because if you create a less privileged\n"
96 "\tsecurity context, it may be unable to change its hostname\n"
99 "\tRemove all the capabilities to make a virtual server trustable\n"
102 "\tDo not print the allocated context number.\n"
104 "Information about context is found in /proc/self/status\n");
108 int main (int argc, char *argv[])
117 unsigned remove_cap = 0;
118 unsigned add_cap = 0;
119 unsigned long secure = ( ( 1<<VC_CAP_LINUX_IMMUTABLE)
120 |(1<<VC_CAP_NET_BROADCAST)
121 |(1<<VC_CAP_NET_ADMIN)
123 |(1<<VC_CAP_IPC_LOCK)
124 |(1<<VC_CAP_IPC_OWNER)
125 |(1<<VC_CAP_SYS_MODULE)
126 |(1<<VC_CAP_SYS_RAWIO)
127 |(1<<VC_CAP_SYS_PACCT)
128 |(1<<VC_CAP_SYS_ADMIN)
129 |(1<<VC_CAP_SYS_BOOT)
130 |(1<<VC_CAP_SYS_NICE)
131 |(1<<VC_CAP_SYS_RESOURCE)
132 |(1<<VC_CAP_SYS_TIME)
134 |(1<<VC_CAP_QUOTACTL));
135 const char *hostname=NULL, *domainname=NULL;
137 for (i=1; i<argc; i++){
138 const char *arg = argv[i];
139 const char *opt = argv[i+1];
140 if (strcmp(arg,"--ctx")==0){
142 fprintf (stderr,"Too many context, max 16, ignored.\n");
144 ctxs[nbctx++] = atoi(opt);
147 }else if (strcmp(arg,"--disconnect")==0){
149 }else if (strcmp(arg,"--silent")==0){
151 }else if (strcmp(arg,"--flag")==0){
152 if (strcmp(opt,"lock")==0){
154 }else if (strcmp(opt,"sched")==0){
156 }else if (strcmp(opt,"nproc")==0){
158 }else if (strcmp(opt,"private")==0){
160 }else if (strcmp(opt,"fakeinit")==0){
162 }else if (strcmp(opt,"hideinfo")==0){
164 }else if (strcmp(opt,"ulimit")==0){
167 fprintf (stderr,"Unknown flag %s\n",opt);
170 }else if (strcmp(arg,"--cap")==0){
171 unsigned *cap = &add_cap;
179 bit = vc_text2cap(opt);
181 if (bit!=-1) *cap |= (1<<bit);
183 fprintf (stderr,"Unknown capability %s\n",opt);
186 }else if (strcmp(arg,"--secure")==0){
187 remove_cap |= secure;
188 }else if (strcmp(arg,"--hostname")==0){
191 }else if (strcmp(arg,"--domainname")==0){
192 if (opt != NULL && strcmp(opt,"none")==0) opt = "";
201 }else if (argv[i][0] == '-'){
205 We must fork early because fakeinit set the current
206 process as the special init process
208 if (disconnect == 0 || fork()==0){
210 int xflags = flags & 16;
212 if (nbctx == 0) ctxs[nbctx++] = -1;
213 newctx = vc_new_s_context(ctxs[0],0,flags&~16);
215 if (hostname != NULL){
216 if (sethostname (hostname,strlen(hostname))==-1){
217 fprintf (stderr,"Can't set the host name (%s)\n"
220 printf ("Host name is now %s\n",hostname);
223 if (domainname != NULL){
224 setdomainname (domainname,strlen(domainname));
226 printf ("Domain name is now %s\n",domainname);
229 remove_cap &= (~add_cap);
230 if (remove_cap!=0 || xflags!=0)
231 vc_new_s_context (-2,remove_cap,xflags);
233 printf ("New security context is %d\n"
234 ,ctxs[0] == -1 ? newctx : ctxs[0]);
236 execvp (argv[i],argv+i);
237 fprintf (stderr,"Can't exec %s (%s)\n",argv[i]
240 perror ("Can't set the new security context\n");
242 if (disconnect != 0) _exit(0);