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
42 #define ENSC_WRAPPERS_VSERVER 1
43 #define ENSC_WRAPPERS_UNISTD 1
44 #define ENSC_WRAPPERS_FCNTL 1
47 #define CMD_HELP 0x1000
48 #define CMD_VERSION 0x1001
49 #define CMD_CAP 0x4000
50 #define CMD_CTX 0x4001
51 #define CMD_DISCONNECT 0x4002
52 #define CMD_DOMAINNAME 0x4003
53 #define CMD_FLAG 0x4004
54 #define CMD_HOSTNAME 0x4005
55 #define CMD_SECURE 0x4006
56 #define CMD_SILENT 0x4007
58 int wrapper_exit_code = 255;
62 { "help", no_argument, 0, CMD_HELP },
63 { "version", no_argument, 0, CMD_VERSION },
64 { "cap", required_argument, 0, CMD_CAP },
65 { "ctx", required_argument, 0, CMD_CTX },
66 { "disconnect", no_argument, 0, CMD_DISCONNECT },
67 { "domainname", required_argument, 0, CMD_DOMAINNAME },
68 { "flag", required_argument, 0, CMD_FLAG },
69 { "hostname", required_argument, 0, CMD_HOSTNAME },
70 { "secure", no_argument, 0, CMD_SECURE },
71 { "silent", no_argument, 0, CMD_SILENT },
83 char const * hostname;
84 char const * domainname;
87 static struct Arguments const * global_args = 0;
90 showHelp(int fd, char const *cmd, int res)
92 WRITE_MSG(fd, "Usage: ");
95 " [--cap [!]<cap_name>] [--secure] [--ctx <num>] [--disconnect]\n"
96 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
97 " [--silent] [--] command arguments ...\n"
99 "chcontext allocate a new security context and executes\n"
100 "a command in that context.\n"
101 "By default, a new/unused context is allocated\n"
104 " Add a capability from the command. This option may be\n"
105 " repeated several time.\n"
106 " See /usr/include/linux/capability.h\n"
107 " In general, this option is used with the --secure option\n"
108 " --secure removes most critical capabilities and --cap\n"
109 " adds specific ones.\n"
113 " Remove a capability from the command. This option may be\n"
114 " repeated several time.\n"
115 " See /usr/include/linux/capability.h\n"
118 " Select the context. On root in context 0 is allowed to\n"
119 " select a specific context.\n"
120 " Context number 1 is special. It can see all processes\n"
121 " in any contexts, but can't kill them though.\n"
122 " Option --ctx may be repeated several times to specify up to 16 contexts.\n"
125 " Start the command in background and make the process\n"
126 " a child of process 1.\n"
128 "--domainname new_domainname\n"
129 " Set the domainname (NIS) in the new security context.\n"
130 " Use \"none\" to unset the domain name.\n"
133 " Set one flag in the new or current security context. The following\n"
134 " flags are supported. The option may be used several time.\n"
136 " fakeinit: The new process will believe it is process number 1.\n"
137 " Useful to run a real /sbin/init in a vserver.\n"
138 " lock: The new process is trapped and can't use chcontext anymore.\n"
139 " sched: The new process and its children will share a common \n"
140 " execution priority.\n"
141 " nproc: Limit the number of process in the vserver according to\n"
142 " ulimit setting. Normally, ulimit is a per user thing.\n"
143 " With this flag, it becomes a per vserver thing.\n"
144 " private: No one can join this security context once created.\n"
145 " ulimit: Apply the current ulimit to the whole context\n"
147 "--hostname new_hostname\n"
148 " Set the hostname in the new security context\n"
149 " This is need because if you create a less privileged\n"
150 " security context, it may be unable to change its hostname\n"
153 " Remove all the capabilities to make a virtual server trustable\n"
156 " Do not print the allocated context number.\n"
158 "Please report bugs to " PACKAGE_BUGREPORT "\n");
167 "chcontext " VERSION " -- allocates/enters a security context\n"
168 "This program is part of " PACKAGE_STRING "\n\n"
169 "Copyright (C) 2003,2004 Enrico Scholz\n"
170 VERSION_COPYRIGHT_DISCLAIMER);
175 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
187 bit = vc_text2cap(str);
189 if (bit!=-1) *cap |= (1<<bit);
191 WRITE_MSG(2, "Unknown capability '");
194 exit(wrapper_exit_code);
199 setFlags(char const *str, uint32_t *flags)
204 *flags = vc_textlist2flag(str, 0, &err_ptr, &err_len);
207 WRITE_MSG(2, "Unknown flag '");
208 write(2, err_ptr, err_len);
210 exit(wrapper_exit_code);
214 static inline ALWAYSINLINE void
215 setHostname(char const *name)
217 if (name == NULL) return;
219 if (sethostname(name, strlen(name))==-1) {
220 perror("sethostname()");
223 if (!global_args->do_silent) {
224 WRITE_MSG(1, "Host name is now ");
230 static inline ALWAYSINLINE void
231 setDomainname(char const *name)
233 if (name == NULL) return;
235 if (setdomainname(name, strlen(name))==-1) {
236 perror("setdomainname()");
239 if (!global_args->do_silent) {
240 WRITE_MSG(1, "Domain name is now ");
246 static inline ALWAYSINLINE void
247 tellContext(xid_t ctx)
249 char buf[sizeof(xid_t)*3+2];
252 if (global_args->do_silent) return;
254 l = utilvserver_fmt_long(buf,ctx);
256 WRITE_MSG(2, "New security context is ");
261 static inline ALWAYSINLINE int
264 if (!global_args->do_disconnect) return 0;
267 fcntl(p[1], F_SETFD, FD_CLOEXEC);
271 static inline ALWAYSINLINE void
272 doSyncStage1(int p[2])
276 if (!global_args->do_disconnect) return;
278 fd = Eopen("/dev/null", O_RDONLY, 0);
282 if (fd!=0) Eclose(fd);
283 Ewrite(p[1], ".", 1);
286 static inline ALWAYSINLINE void
287 doSyncStage2(int p[2])
289 if (!global_args->do_disconnect) return;
291 Ewrite(p[1], "X", 1);
295 waitOnSync(pid_t pid, int p[2])
300 assert(global_args->do_disconnect);
304 l = Eread(p[0], &c, 1);
305 if (l!=1) exitLikeProcess(pid);
306 l = Eread(p[0], &c, 1);
307 if (l!=0) exitLikeProcess(pid);
310 int main (int argc, char *argv[])
312 struct Arguments args = {
314 .do_disconnect = false,
330 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
334 case CMD_HELP : showHelp(1, argv[0], 0);
335 case CMD_VERSION : showVersion();
336 case CMD_DISCONNECT : args.do_disconnect = true; break;
337 case CMD_SILENT : args.do_silent = true; break;
338 case CMD_DOMAINNAME : args.domainname = optarg; break;
339 case CMD_HOSTNAME : args.hostname = optarg; break;
342 setCap(optarg, &args.add_caps, &args.remove_caps);
345 args.remove_caps |= vc_get_securecaps();
348 setFlags(optarg, &args.flags);
352 WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
353 if (args.nbctx>=DIM_OF(args.ctxs)) {
354 WRITE_MSG(2, "Too many contexts given\n");
357 args.ctxs[args.nbctx++] = atoi(optarg);
362 WRITE_MSG(2, "Try '");
363 WRITE_STR(2, argv[0]);
364 WRITE_MSG(2, " --help\" for more information.\n");
371 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
375 if (args.domainname && strcmp(args.domainname, "none")==0)
376 args.domainname = "";
379 args.ctxs[args.nbctx++] = VC_RANDCTX;
381 xflags = args.flags & S_CTX_INFO_INIT;
382 args.flags &= ~S_CTX_INFO_INIT;
386 newctx = Evc_new_s_context(args.ctxs[0],0,args.flags);
387 args.remove_caps &= (~args.add_caps);
388 setHostname(args.hostname);
389 setDomainname(args.domainname);
391 if (args.remove_caps!=0 || xflags!=0)
392 Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
393 tellContext(args.ctxs[0]==VC_RANDCTX ? newctx : args.ctxs[0]);
396 execvp (argv[optind],argv+optind);
407 #ifdef ENSC_TESTSUITE
408 #define FLAG_TEST(STR,EXP) \
411 setFlags(STR, &flag); \
412 assert(flag==(EXP)); \
415 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
417 uint32_t add=0,del=0; \
418 setCap(STR, &add, &del); \
419 assert(add==(EXP_ADD)); \
420 assert(del==(EXP_DEL)); \
426 FLAG_TEST("lock", 1);
427 FLAG_TEST("lock,sched", 3);
429 CAP_TEST("CHOWN", 1, 0);
430 CAP_TEST("CAP_CHOWN", 1, 0);
431 CAP_TEST("!CHOWN", 0, 1);
432 CAP_TEST("!CAP_CHOWN", 0, 1);