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