fe66a2245111b8ed1f741b1c305d8e86cfc89908
[util-vserver.git] / src / chcontext.c
1 // $Id: chcontext.c,v 1.24 2005/03/22 15:05:24 ensc Exp $
2
3 // Copyright (C) 2003,2004 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
29 #include "util.h"
30 #include "vserver.h"
31 #include "internal.h"
32 #include "lib_internal/jail.h"
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <getopt.h>
40 #include <assert.h>
41 #include <fcntl.h>
42 #include <libgen.h>
43 #include <signal.h>
44
45 #define ENSC_WRAPPERS_PREFIX    "chcontext: "
46 #define ENSC_WRAPPERS_VSERVER   1
47 #define ENSC_WRAPPERS_UNISTD    1
48 #define ENSC_WRAPPERS_FCNTL     1
49 #include <wrappers.h>
50
51 #define CMD_HELP        0x1000
52 #define CMD_VERSION     0x1001
53 #define CMD_CAP         0x4000
54 #define CMD_CTX         0x4001
55 #define CMD_DISCONNECT  0x4002
56 #define CMD_DOMAINNAME  0x4003
57 #define CMD_FLAG        0x4004
58 #define CMD_HOSTNAME    0x4005
59 #define CMD_SECURE      0x4006
60 #define CMD_SILENT      0x4007
61
62 int wrapper_exit_code   = 255;
63
64 struct option const
65 CMDLINE_OPTIONS[] = {
66   { "help",     no_argument,  0, CMD_HELP },
67   { "version",  no_argument,  0, CMD_VERSION },
68   { "cap",        required_argument,  0, CMD_CAP },
69   { "ctx",        required_argument,  0, CMD_CTX },
70   { "xid",        required_argument,  0, CMD_CTX },
71   { "disconnect", no_argument,        0, CMD_DISCONNECT },
72   { "domainname", required_argument,  0, CMD_DOMAINNAME },
73   { "flag",       required_argument,  0, CMD_FLAG },
74   { "hostname",   required_argument,  0, CMD_HOSTNAME },
75   { "secure",     no_argument,        0, CMD_SECURE },
76   { "silent",     no_argument,        0, CMD_SILENT },
77   { 0,0,0,0 }
78 };
79
80 struct Arguments {
81     size_t              nbctx;
82     xid_t               ctxs[16];
83     bool                do_disconnect;
84     bool                do_silent;
85     unsigned int        flags;
86     uint32_t            remove_caps;
87     uint32_t            add_caps;
88     char const *        hostname;
89     char const *        domainname;
90 };
91
92 static struct Arguments const *         global_args = 0;
93
94 static void
95 showHelp(int fd, char const *cmd, int res)
96 {
97   VSERVER_DECLARE_CMD(cmd);
98   
99   WRITE_MSG(fd, "Usage: ");
100   WRITE_STR(fd, cmd);
101   WRITE_MSG(fd,
102             " [--cap [!]<cap_name>] [--secure] [--xid <num>] [--disconnect]\n"
103             "       [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
104             "       [--silent] [--] command arguments ...\n"
105             "\n"
106             "chcontext allocate a new security context and executes\n"
107             "a command in that context.\n"
108             "By default, a new/unused context is allocated\n"
109             "\n"
110             "--cap CAP_NAME\n"
111             "    Add a capability from the command. This option may be\n"
112             "    repeated several time.\n"
113             "    See /usr/include/linux/capability.h\n"
114             "    In general, this option is used with the --secure option\n"
115             "    --secure removes most critical capabilities and --cap\n"
116             "    adds specific ones.\n"
117             "\n"
118
119             "--cap !CAP_NAME\n"
120             "    Remove a capability from the command. This option may be\n"
121             "    repeated several time.\n"
122             "    See /usr/include/linux/capability.h\n"
123             "\n"
124             "--xid num\n"
125             "    Select the context. On root in context 0 is allowed to\n"
126             "    select a specific context.\n"
127             "    Context number 1 is special. It can see all processes\n"
128             "    in any contexts, but can't kill them though.\n"
129             "    Option --xid may be repeated several times to specify up to 16 contexts.\n"
130
131             "--disconnect\n"
132             "    Start the command in background and make the process\n"
133             "    a child of process 1.\n"
134
135             "--domainname new_domainname\n"
136             "    Set the domainname (NIS) in the new security context.\n"
137             "    Use \"none\" to unset the domain name.\n"
138
139             "--flag\n"
140             "    Set one flag in the new or current security context. The following\n"
141             "    flags are supported. The option may be used several time.\n"
142             "\n"
143             "        fakeinit: The new process will believe it is process number 1.\n"
144             "                  Useful to run a real /sbin/init in a vserver.\n"
145             "        lock:     The new process is trapped and can't use chcontext anymore.\n"
146             "        sched:    The new process and its children will share a common \n"
147             "                  execution priority.\n"
148             "        nproc:    Limit the number of process in the vserver according to\n"
149             "                  ulimit setting. Normally, ulimit is a per user thing.\n"
150             "                  With this flag, it becomes a per vserver thing.\n"
151             "        private:  No one can join this security context once created.\n"
152             "        ulimit:   Apply the current ulimit to the whole context\n"
153
154             "--hostname new_hostname\n"
155             "    Set the hostname in the new security context\n"
156             "    This is need because if you create a less privileged\n"
157             "    security context, it may be unable to change its hostname\n"
158
159             "--secure\n"
160             "    Remove all the capabilities to make a virtual server trustable\n"
161
162             "--silent\n"
163             "    Do not print the allocated context number.\n"
164             "\n"
165             "Please report bugs to " PACKAGE_BUGREPORT "\n");
166
167   exit(res);
168 }
169
170 static void
171 showVersion()
172 {
173   WRITE_MSG(1,
174             "chcontext-compat " VERSION " -- allocates/enters a security context\n"
175             "This program is part of " PACKAGE_STRING "\n\n"
176             "Copyright (C) 2003,2004 Enrico Scholz\n"
177             VERSION_COPYRIGHT_DISCLAIMER);
178   exit(0);
179 }
180
181 static inline void
182 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
183 {
184   uint32_t      *cap;
185   int           bit;
186   
187   if (str[0] != '!')
188     cap = add_caps;
189   else {
190     cap = remove_caps;
191     str++;
192   }
193         
194   bit = vc_text2cap(str);
195         
196   if (bit!=-1) *cap |= (1<<bit);
197   else {
198     WRITE_MSG(2, "Unknown capability '");
199     WRITE_STR(2, str);
200     WRITE_MSG(2, "'\n");
201     exit(wrapper_exit_code);
202   }
203 }
204
205 static inline void
206 setFlags(char const *str, uint32_t *flags)
207 {
208   struct vc_err_listparser      err;
209   
210   *flags = vc_list2cflag_compat(str, 0, &err);
211
212   if (err.ptr!=0) {
213     WRITE_MSG(2, "Unknown flag '");
214     Vwrite(2, err.ptr, err.len);
215     WRITE_MSG(2, "'\n");
216     exit(wrapper_exit_code);
217   }
218 }
219
220 static inline ALWAYSINLINE void
221 setHostname(char const *name)
222 {
223   if (name == NULL) return;
224   
225   if (sethostname(name, strlen(name))==-1) {
226     perror("chcontext: sethostname()");
227     exit(255);
228   }
229   if (!global_args->do_silent) {
230     WRITE_MSG(1, "Host name is now ");
231     WRITE_STR(1, name);
232     WRITE_MSG(1, "\n");
233   }
234 }
235
236 static inline ALWAYSINLINE void
237 setDomainname(char const *name)
238 {
239   if (name == NULL) return;
240   
241   if (setdomainname(name, strlen(name))==-1) {
242     perror("chcontext: setdomainname()");
243     exit(255);
244   }
245   if (!global_args->do_silent) {
246     WRITE_MSG(1, "Domain name is now ");
247     WRITE_STR(1, name);
248     WRITE_MSG(1, "\n");
249   }
250 }
251
252 static inline ALWAYSINLINE void
253 tellContext(xid_t ctx)
254 {
255   char          buf[sizeof(xid_t)*3+2];
256   size_t        l;
257
258   if (global_args->do_silent) return;
259
260   l = utilvserver_fmt_long(buf,ctx);
261
262   WRITE_MSG(1, "New security context is ");
263   Vwrite(1, buf, l);
264   WRITE_MSG(1, "\n");
265 }
266
267 #include "context-sync.hc"
268
269 int main (int argc, char *argv[])
270 {
271   struct Arguments args = {
272     .nbctx         = 0,
273     .do_disconnect = false,
274     .do_silent     = false,
275     .flags         = 0,
276     .remove_caps   = 0,
277     .add_caps      = 0,
278     .hostname      = 0,
279     .domainname    = 0
280   };
281   xid_t         newctx;
282   int           xflags;
283   int           p[2][2];
284   pid_t         pid;
285   
286   global_args = &args;
287   signal(SIGCHLD, SIG_DFL);
288
289   while (1) {
290     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
291     if (c==-1) break;
292
293     switch (c) {
294       case CMD_HELP             :  showHelp(1, argv[0], 0);
295       case CMD_VERSION          :  showVersion();
296       case CMD_DISCONNECT       :  args.do_disconnect = true;   break;
297       case CMD_SILENT           :  args.do_silent     = true;   break;
298       case CMD_DOMAINNAME       :  args.domainname    = optarg; break;
299       case CMD_HOSTNAME         :  args.hostname      = optarg; break;
300         
301       case CMD_CAP              :
302         setCap(optarg, &args.add_caps, &args.remove_caps);
303         break;
304       case CMD_SECURE           :
305         args.remove_caps |= vc_get_insecurebcaps();
306         break;
307       case CMD_FLAG             :
308         setFlags(optarg, &args.flags);
309         break;
310       case CMD_CTX              :
311         if (args.nbctx>0)
312           WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
313         if (args.nbctx>=DIM_OF(args.ctxs)) {
314           WRITE_MSG(2, "Too many contexts given\n");
315           exit(255);
316         }
317         args.ctxs[args.nbctx++] = Evc_xidopt2xid(optarg, true);
318         break;
319
320           
321       default           :
322         WRITE_MSG(2, "Try '");
323         WRITE_STR(2, argv[0]);
324         WRITE_MSG(2, " --help\" for more information.\n");
325         return 255;
326         break;
327     }
328   }
329
330   if (optind>=argc) {
331     WRITE_MSG(2, "No command given; use '--help' for more information.\n");
332     exit(255);
333   }
334   
335   if (args.domainname && strcmp(args.domainname, "none")==0)
336     args.domainname = "";
337     
338   if (args.nbctx == 0)
339     args.ctxs[args.nbctx++] = VC_DYNAMIC_XID;
340     
341   xflags      = args.flags & S_CTX_INFO_INIT;
342   args.flags &= ~S_CTX_INFO_INIT;
343
344   pid = initSync(p, args.do_disconnect);
345   if (pid==0) {
346     doSyncStage0(p, args.do_disconnect);
347     
348     newctx            = Evc_new_s_context(args.ctxs[0],0,args.flags);
349     args.remove_caps &= (~args.add_caps);
350     setHostname(args.hostname);
351     setDomainname(args.domainname);
352
353     if (args.remove_caps!=0 || xflags!=0)
354       Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
355     tellContext(args.ctxs[0]==VC_DYNAMIC_XID ? newctx : args.ctxs[0]);
356
357     doSyncStage1(p, args.do_disconnect);
358     execvp (argv[optind],argv+optind);
359     doSyncStage2(p, args.do_disconnect);
360
361     PERROR_Q("chcontext: execvp", argv[optind]);
362     exit(255);
363   }
364
365   waitOnSync(pid, p, args.ctxs[0]!=VC_DYNAMIC_XID);
366   return EXIT_SUCCESS;
367 }
368
369 #ifdef ENSC_TESTSUITE
370 #define FLAG_TEST(STR,EXP) \
371   {                        \
372     uint32_t    flag=0;    \
373     setFlags(STR, &flag);  \
374     assert(flag==(EXP));   \
375   }
376
377 #define CAP_TEST(STR,EXP_ADD,EXP_DEL)           \
378   {                                             \
379     uint32_t    add=0,del=0;                    \
380     setCap(STR, &add, &del);                    \
381     assert(add==(EXP_ADD));                     \
382     assert(del==(EXP_DEL));                     \
383   }
384
385 void
386 test()
387 {
388   FLAG_TEST("lock",       1);
389   FLAG_TEST("lock,sched", 3);
390
391   CAP_TEST("CHOWN",      1, 0);
392   CAP_TEST("CAP_CHOWN",  1, 0);
393   CAP_TEST("!CHOWN",     0, 1);
394   CAP_TEST("!CAP_CHOWN", 0, 1);
395 }
396 #endif