3 // Copyright (C) 2003,2004 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
32 #include "wrappers-vserver.h"
43 #define CMD_HELP 0x1000
44 #define CMD_VERSION 0x1001
45 #define CMD_CAP 0x4000
46 #define CMD_CTX 0x4001
47 #define CMD_DISCONNECT 0x4002
48 #define CMD_DOMAINNAME 0x4003
49 #define CMD_FLAG 0x4004
50 #define CMD_HOSTNAME 0x4005
51 #define CMD_SECURE 0x4006
52 #define CMD_SILENT 0x4007
54 int wrapper_exit_code = 255;
58 { "help", no_argument, 0, CMD_HELP },
59 { "version", no_argument, 0, CMD_VERSION },
60 { "cap", required_argument, 0, CMD_CAP },
61 { "ctx", required_argument, 0, CMD_CTX },
62 { "disconnect", no_argument, 0, CMD_DISCONNECT },
63 { "domainname", required_argument, 0, CMD_DOMAINNAME },
64 { "flag", required_argument, 0, CMD_FLAG },
65 { "hostname", required_argument, 0, CMD_HOSTNAME },
66 { "secure", no_argument, 0, CMD_SECURE },
67 { "silent", no_argument, 0, CMD_SILENT },
79 char const * hostname;
80 char const * domainname;
83 static struct Arguments const * global_args = 0;
86 showHelp(int fd, char const *cmd, int res)
88 WRITE_MSG(fd, "Usage: ");
91 " [--cap [!]<cap_name>] [--secure] [--ctx <num>] [--disconnect]\n"
92 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
93 " [--silent] [--] command arguments ...\n"
95 "chcontext allocate a new security context and executes\n"
96 "a command in that context.\n"
97 "By default, a new/unused context is allocated\n"
100 " Add a capability from the command. This option may be\n"
101 " repeated several time.\n"
102 " See /usr/include/linux/capability.h\n"
103 " In general, this option is used with the --secure option\n"
104 " --secure removes most critical capabilities and --cap\n"
105 " adds specific ones.\n"
109 " Remove a capability from the command. This option may be\n"
110 " repeated several time.\n"
111 " See /usr/include/linux/capability.h\n"
114 " Select the context. On root in context 0 is allowed to\n"
115 " select a specific context.\n"
116 " Context number 1 is special. It can see all processes\n"
117 " in any contexts, but can't kill them though.\n"
118 " Option --ctx may be repeated several times to specify up to 16 contexts.\n"
121 " Start the command in background and make the process\n"
122 " a child of process 1.\n"
124 "--domainname new_domainname\n"
125 " Set the domainname (NIS) in the new security context.\n"
126 " Use \"none\" to unset the domain name.\n"
129 " Set one flag in the new or current security context. The following\n"
130 " flags are supported. The option may be used several time.\n"
132 " fakeinit: The new process will believe it is process number 1.\n"
133 " Useful to run a real /sbin/init in a vserver.\n"
134 " lock: The new process is trapped and can't use chcontext anymore.\n"
135 " sched: The new process and its children will share a common \n"
136 " execution priority.\n"
137 " nproc: Limit the number of process in the vserver according to\n"
138 " ulimit setting. Normally, ulimit is a per user thing.\n"
139 " With this flag, it becomes a per vserver thing.\n"
140 " private: No one can join this security context once created.\n"
141 " ulimit: Apply the current ulimit to the whole context\n"
143 "--hostname new_hostname\n"
144 " Set the hostname in the new security context\n"
145 " This is need because if you create a less privileged\n"
146 " security context, it may be unable to change its hostname\n"
149 " Remove all the capabilities to make a virtual server trustable\n"
152 " Do not print the allocated context number.\n"
154 "Please report bugs to " PACKAGE_BUGREPORT "\n");
163 "chcontext " VERSION " -- allocates/enters a security context\n"
164 "This program is part of " PACKAGE_STRING "\n\n"
165 "Copyright (C) 2003,2004 Enrico Scholz\n"
166 VERSION_COPYRIGHT_DISCLAIMER);
171 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
183 bit = vc_text2cap(str);
185 if (bit!=-1) *cap |= (1<<bit);
187 WRITE_MSG(2, "Unknown capability '");
195 setFlags(char const *str, uint32_t *flags)
198 char const *ptr = strchr(str, ',');
199 size_t cnt = ptr ? (size_t)(ptr-str) : strlen(str);
201 if (strncmp(str, "lock", cnt)==0) *flags |= S_CTX_INFO_LOCK;
202 else if (strncmp(str, "sched", cnt)==0) *flags |= S_CTX_INFO_SCHED;
203 else if (strncmp(str, "nproc", cnt)==0) *flags |= S_CTX_INFO_NPROC;
204 else if (strncmp(str, "private", cnt)==0) *flags |= S_CTX_INFO_PRIVATE;
205 else if (strncmp(str, "fakeinit", cnt)==0) *flags |= S_CTX_INFO_INIT;
206 else if (strncmp(str, "hideinfo", cnt)==0) *flags |= S_CTX_INFO_HIDEINFO;
207 else if (strncmp(str, "ulimit", cnt)==0) *flags |= S_CTX_INFO_ULIMIT;
209 WRITE_MSG(2, "Unknown flag '");
221 static inline ALWAYSINLINE void
222 setHostname(char const *name)
224 if (name == NULL) return;
226 if (vc_set_vhi_name(VC_SAMECTX, vcVHI_NODENAME, name, strlen(name))==-1) {
227 perror("setHostname()");
230 if (!global_args->do_silent) {
231 WRITE_MSG(1, "Host name is now ");
237 static inline ALWAYSINLINE void
238 setDomainname(char const *name)
240 if (name == NULL) return;
242 if (vc_set_vhi_name(VC_SAMECTX, vcVHI_DOMAINNAME, name, strlen(name))==-1) {
243 perror("setDomainname()");
246 if (!global_args->do_silent) {
247 WRITE_MSG(1, "Domain name is now ");
253 static inline ALWAYSINLINE void
254 tellContext(xid_t ctx)
256 char buf[sizeof(xid_t)*3+2];
259 if (global_args->do_silent) return;
261 l = utilvserver_fmt_long(buf,ctx);
263 WRITE_MSG(2, "New security context is ");
268 static inline ALWAYSINLINE int
271 if (!global_args->do_disconnect) return 0;
274 fcntl(p[1], F_SETFD, FD_CLOEXEC);
278 static inline ALWAYSINLINE void
279 doSyncStage1(int p[2])
283 if (!global_args->do_disconnect) return;
285 fd = Eopen("/dev/null", O_RDONLY, 0);
289 if (fd!=0) Eclose(fd);
290 Ewrite(p[1], ".", 1);
293 static inline ALWAYSINLINE void
294 doSyncStage2(int p[2])
296 if (!global_args->do_disconnect) return;
298 Ewrite(p[1], "X", 1);
302 waitOnSync(pid_t pid, int p[2])
307 assert(global_args->do_disconnect);
311 l = Eread(p[0], &c, 1);
312 if (l!=1) exitLikeProcess(pid);
313 l = Eread(p[0], &c, 1);
314 if (l!=0) exitLikeProcess(pid);
317 int main (int argc, char *argv[])
319 struct Arguments args = {
321 .do_disconnect = false,
337 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
341 case CMD_HELP : showHelp(1, argv[0], 0);
342 case CMD_VERSION : showVersion();
343 case CMD_DISCONNECT : args.do_disconnect = true; break;
344 case CMD_SILENT : args.do_silent = true; break;
345 case CMD_DOMAINNAME : args.domainname = optarg; break;
346 case CMD_HOSTNAME : args.hostname = optarg; break;
349 setCap(optarg, &args.add_caps, &args.remove_caps);
352 args.remove_caps |= vc_get_securecaps();
355 setFlags(optarg, &args.flags);
359 WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
360 if (args.nbctx>=DIM_OF(args.ctxs)) {
361 WRITE_MSG(2, "Too many contexts given\n");
364 args.ctxs[args.nbctx++] = atoi(optarg);
369 WRITE_MSG(2, "Try '");
370 WRITE_STR(2, argv[0]);
371 WRITE_MSG(2, " --help\" for more information.\n");
378 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
382 if (args.domainname && strcmp(args.domainname, "none")==0)
383 args.domainname = "";
386 args.ctxs[args.nbctx++] = VC_RANDCTX;
388 xflags = args.flags & 16;
390 newctx = Evc_new_s_context(args.ctxs[0],0,args.flags&~16);
391 args.remove_caps &= (~args.add_caps);
392 setHostname(args.hostname);
393 setDomainname(args.domainname);
398 if (args.remove_caps!=0 || xflags!=0)
399 Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
400 tellContext(args.ctxs[0]==VC_RANDCTX ? newctx : args.ctxs[0]);
403 execvp (argv[optind],argv+optind);
414 #ifdef ENSC_TESTSUITE
415 #define FLAG_TEST(STR,EXP) \
418 setFlags(STR, &flag); \
419 assert(flag==(EXP)); \
422 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
424 uint32_t add=0,del=0; \
425 setCap(STR, &add, &del); \
426 assert(add==(EXP_ADD)); \
427 assert(del==(EXP_DEL)); \
433 FLAG_TEST("lock", 1);
434 FLAG_TEST("lock,sched", 3);
436 CAP_TEST("CHOWN", 1, 0);
437 CAP_TEST("CAP_CHOWN", 1, 0);
438 CAP_TEST("!CHOWN", 0, 1);
439 CAP_TEST("!CAP_CHOWN", 0, 1);