21a8a15404a231316b72a0f00dc5b5e1bfdeb903
[util-vserver.git] / util-vserver / src / chcontext.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on chcontext.cc by Jacques Gelinas
5 //  
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)
9 // any later version.
10 //  
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.
15 //  
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.
19
20 /*
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
23         arguments.
24 */
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28 #include "compat.h"
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <errno.h>
35
36 #include "linuxcaps.h"
37 #include "vserver.h"
38
39 static void usage()
40 {
41         fprintf (stderr,"chcontext version %s\n",VERSION);
42         fprintf (stderr
43                 ,"chcontext [ options ] command arguments ...\n"
44                  "\n"
45                  "chcontext allocate a new security context and executes\n"
46                  "a command in that context.\n"
47                  "By default, a new/unused context is allocated\n"
48                  "\n"
49
50                  "--cap CAP_NAME\n"
51                  "\tAdd a capability from the command. This option may be\n"
52                  "\trepeated several time.\n"
53                  "\tSee /usr/include/linux/capability.h\n"
54                  "\tIn general, this option is used with the --secure option\n"
55                  "\t--secure removes most critical capabilities and --cap\n"
56                  "\tadds specific ones.\n"
57                  "\n"
58
59                  "--cap !CAP_NAME\n"
60                  "\tRemove a capability from the command. This option may be\n"
61                  "\trepeated several time.\n"
62                  "\tSee /usr/include/linux/capability.h\n"
63                  "\n"
64                  "--ctx num\n"
65                  "\tSelect the context. On root in context 0 is allowed to\n"
66                  "\tselect a specific context.\n"
67                  "\tContext number 1 is special. It can see all processes\n"
68                  "\tin any contexts, but can't kill them though.\n"
69                  "\tOption --ctx may be repeated several times to specify up to 16 contexts.\n"
70
71                  "--disconnect\n"
72                  "\tStart the command in background and make the process\n"
73                  "\ta child of process 1.\n"
74
75                  "--domainname new_domainname\n"
76                  "\tSet the domainname (NIS) in the new security context.\n"
77                  "\tUse \"none\" to unset the domain name.\n"
78
79                  "--flag\n"
80                  "\tSet one flag in the new or current security context. The following\n"
81                  "\tflags are supported. The option may be used several time.\n"
82                  "\n"
83                  "\tfakeinit: The new process will believe it is process number 1.\n"
84                  "            Useful to run a real /sbin/init in a vserver.\n"
85                  "\tlock: The new process is trapped and can't use chcontext anymore.\n"
86                  "\tsched: The new process and its children will share a common \n"
87                  "         execution priority.\n"
88                  "\tnproc: Limit the number of process in the vserver according to\n"
89                  "         ulimit setting. Normally, ulimit is a per user thing.\n"
90                  "         With this flag, it becomes a per vserver thing.\n"
91                  "\tprivate: No one can join this security context once created.\n"
92                  "\tulimit: Apply the current ulimit to the whole context\n"
93
94                  "--hostname new_hostname\n"
95                  "\tSet the hostname in the new security context\n"
96                  "\tThis is need because if you create a less privileged\n"
97                  "\tsecurity context, it may be unable to change its hostname\n"
98
99                  "--secure\n"
100                  "\tRemove all the capabilities to make a virtual server trustable\n"
101
102                  "--silent\n"
103                  "\tDo not print the allocated context number.\n"
104                  "\n"
105                  "Information about context is found in /proc/self/status\n");
106 }
107
108
109 int main (int argc, char *argv[])
110 {
111         int ret = -1;
112         int i;
113         int nbctx = 0;
114         int ctxs[16];
115         int disconnect = 0;
116         int fakeinit = 0;
117         int silent = 0;
118         int flags = 0;
119         unsigned remove_cap = 0;
120         unsigned add_cap = 0;
121         unsigned long secure = (1<<CAP_LINUX_IMMUTABLE)
122                 |(1<<CAP_NET_BROADCAST)
123                 |(1<<CAP_NET_ADMIN)
124                 |(1<<CAP_NET_RAW)
125                 |(1<<CAP_IPC_LOCK)
126                 |(1<<CAP_IPC_OWNER)
127                 |(1<<CAP_SYS_MODULE)
128                 |(1<<CAP_SYS_RAWIO)
129                 |(1<<CAP_SYS_PACCT)
130                 |(1<<CAP_SYS_ADMIN)
131                 |(1<<CAP_SYS_BOOT)
132                 |(1<<CAP_SYS_NICE)
133                 |(1<<CAP_SYS_RESOURCE)
134                 |(1<<CAP_SYS_TIME)
135                 |(1<<CAP_MKNOD);
136         const char *hostname=NULL, *domainname=NULL;
137
138         for (i=1; i<argc; i++){
139                 const char *arg = argv[i];
140                 const char *opt = argv[i+1];
141                 if (strcmp(arg,"--ctx")==0){
142                         if (nbctx >= 16){
143                                 fprintf (stderr,"Too many context, max 16, ignored.\n");
144                         }else{
145                                 ctxs[nbctx++] = atoi(opt);
146                         }
147                         i++;
148                 }else if (strcmp(arg,"--disconnect")==0){
149                         disconnect = 1;
150                 }else if (strcmp(arg,"--silent")==0){
151                         silent = 1;
152                 }else if (strcmp(arg,"--flag")==0){
153                         if (strcmp(opt,"lock")==0){
154                                 flags |= 1;
155                         }else if (strcmp(opt,"sched")==0){
156                                 flags |= 2;
157                         }else if (strcmp(opt,"nproc")==0){
158                                 flags |= 4;
159                         }else if (strcmp(opt,"private")==0){
160                                 flags |= 8;
161                         }else if (strcmp(opt,"fakeinit")==0){
162                                 fakeinit = 1;
163                                 flags |= 16;
164                         }else if (strcmp(opt,"hideinfo")==0){
165                                 flags |= 32;
166                         }else if (strcmp(opt,"ulimit")==0){
167                                 flags |= 64;
168                         }else{
169                                 fprintf (stderr,"Unknown flag %s\n",opt);
170                         }
171                         i++;
172                 }else if (strcmp(arg,"--cap")==0){
173                         static struct {
174                                 const char *option;
175                                 int bit;
176                         }tbcap[]={
177                                 // The following capabilities are normally available
178                                 // to vservers administrator, but are place for
179                                 // completeness
180                                 {"CAP_CHOWN",CAP_CHOWN},
181                                 {"CAP_DAC_OVERRIDE",CAP_DAC_OVERRIDE},
182                                 {"CAP_DAC_READ_SEARCH",CAP_DAC_READ_SEARCH},
183                                 {"CAP_FOWNER",CAP_FOWNER},
184                                 {"CAP_FSETID",CAP_FSETID},
185                                 {"CAP_KILL",CAP_KILL},
186                                 {"CAP_SETGID",CAP_SETGID},
187                                 {"CAP_SETUID",CAP_SETUID},
188                                 {"CAP_SETPCAP",CAP_SETPCAP},
189                                 {"CAP_SYS_TTY_CONFIG",CAP_SYS_TTY_CONFIG},
190                                 {"CAP_LEASE",CAP_LEASE},
191                                 {"CAP_SYS_CHROOT",CAP_SYS_CHROOT},
192
193                                 // Those capabilities are not normally available
194                                 // to vservers because they are not needed and
195                                 // may represent a security risk
196                                 {"CAP_LINUX_IMMUTABLE",CAP_LINUX_IMMUTABLE},
197                                 {"CAP_NET_BIND_SERVICE",CAP_NET_BIND_SERVICE},
198                                 {"CAP_NET_BROADCAST",CAP_NET_BROADCAST},
199                                 {"CAP_NET_ADMIN",       CAP_NET_ADMIN},
200                                 {"CAP_NET_RAW", CAP_NET_RAW},
201                                 {"CAP_IPC_LOCK",        CAP_IPC_LOCK},
202                                 {"CAP_IPC_OWNER",       CAP_IPC_OWNER},
203                                 {"CAP_SYS_MODULE",CAP_SYS_MODULE},
204                                 {"CAP_SYS_RAWIO",       CAP_SYS_RAWIO},
205                                 {"CAP_SYS_PACCT",       CAP_SYS_PACCT},
206                                 {"CAP_SYS_ADMIN",       CAP_SYS_ADMIN},
207                                 {"CAP_SYS_BOOT",        CAP_SYS_BOOT},
208                                 {"CAP_SYS_NICE",        CAP_SYS_NICE},
209                                 {"CAP_SYS_RESOURCE",CAP_SYS_RESOURCE},
210                                 {"CAP_SYS_TIME",        CAP_SYS_TIME},
211                                 {"CAP_MKNOD",           CAP_MKNOD},
212                                 {NULL,0}
213                         };
214                         int j;
215                         unsigned *cap = &add_cap;
216                         if (opt[0] == '!'){
217                                 cap = &remove_cap;
218                                 opt++;
219                         }
220                         for (j=0; tbcap[j].option != NULL; j++){
221                                 if (strcasecmp(tbcap[j].option,opt)==0){
222                                         *cap |= (1<<tbcap[j].bit);
223                                         break;
224                                 }
225                         }
226                         if (tbcap[j].option == NULL){
227                                 fprintf (stderr,"Unknown capability %s\n",opt);
228                         }
229                         i++;
230                 }else if (strcmp(arg,"--secure")==0){
231                         remove_cap |= secure;
232                 }else if (strcmp(arg,"--hostname")==0){
233                         hostname = opt;
234                         i++;
235                 }else if (strcmp(arg,"--domainname")==0){
236                         if (opt != NULL && strcmp(opt,"none")==0) opt = "";
237                         domainname = opt;
238                         i++;
239                 }else{
240                         break;
241                 }
242         }
243         if (i == argc){
244                 usage();
245         }else if (argv[i][0] == '-'){
246                 usage();
247         }else{
248                 /*
249                         We must fork early because fakeinit set the current
250                         process as the special init process
251                 */
252                 if (disconnect == 0 || fork()==0){
253                         int newctx;
254                         if (nbctx == 0) ctxs[nbctx++] = -1;
255                         newctx = vc_new_s_context(ctxs[0],0,flags);
256                         if (newctx != -1){
257                                 if (hostname != NULL){
258                                         if (sethostname (hostname,strlen(hostname))==-1){
259                                                 fprintf (stderr,"Can't set the host name (%s)\n"
260                                                         ,strerror(errno));
261                                         }else if (!silent){
262                                                 printf ("Host name is now %s\n",hostname);
263                                         }
264                                 }
265                                 if (domainname != NULL){
266                                         setdomainname (domainname,strlen(domainname));
267                                         if (!silent){
268                                                 printf ("Domain name is now %s\n",domainname);
269                                         }
270                                 }
271                                 remove_cap &= (~add_cap);
272                                 if (remove_cap != 0) vc_new_s_context (-2,remove_cap,0);
273                                 if (!silent){
274                                         printf ("New security context is %d\n"
275                                                 ,ctxs[0] == -1 ? newctx : ctxs[0]);
276                                 }
277                                 execvp (argv[i],argv+i);
278                                 fprintf (stderr,"Can't exec %s (%s)\n",argv[i]
279                                         ,strerror(errno));
280                         }else{
281                                 perror ("Can't set the new security context\n");
282                         }
283                         if (disconnect != 0) _exit(0);
284                 }
285         }
286         return ret;
287 }
288