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 "lib_internal/jail.h"
45 #define ENSC_WRAPPERS_PREFIX "chcontext: "
46 #define ENSC_WRAPPERS_VSERVER 1
47 #define ENSC_WRAPPERS_UNISTD 1
48 #define ENSC_WRAPPERS_FCNTL 1
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
62 int wrapper_exit_code = 255;
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 },
88 char const * hostname;
89 char const * domainname;
92 static struct Arguments const * global_args = 0;
95 showHelp(int fd, char const *cmd, int res)
97 VSERVER_DECLARE_CMD(cmd);
99 #if !defined(VC_ENABLE_API_COMPAT) && !defined(VC_ENABLE_API_LEGACY)
100 WRITE_MSG(1, "ERROR: tools were built without legacy API support; chcontext will not work!\n\n");
103 WRITE_MSG(fd, "Usage: ");
106 " [--cap [!]<cap_name>] [--secure] [--xid <num>] [--disconnect]\n"
107 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
108 " [--silent] [--] command arguments ...\n"
110 "chcontext allocate a new security context and executes\n"
111 "a command in that context.\n"
112 "By default, a new/unused context is allocated\n"
115 " Add a capability from the command. This option may be\n"
116 " repeated several time.\n"
117 " See /usr/include/linux/capability.h\n"
118 " In general, this option is used with the --secure option\n"
119 " --secure removes most critical capabilities and --cap\n"
120 " adds specific ones.\n"
124 " Remove a capability from the command. This option may be\n"
125 " repeated several time.\n"
126 " See /usr/include/linux/capability.h\n"
129 " Select the context. On root in context 0 is allowed to\n"
130 " select a specific context.\n"
131 " Context number 1 is special. It can see all processes\n"
132 " in any contexts, but can't kill them though.\n"
133 " Option --xid may be repeated several times to specify up to 16 contexts.\n"
136 " Start the command in background and make the process\n"
137 " a child of process 1.\n"
139 "--domainname new_domainname\n"
140 " Set the domainname (NIS) in the new security context.\n"
141 " Use \"none\" to unset the domain name.\n"
144 " Set one flag in the new or current security context. The following\n"
145 " flags are supported. The option may be used several time.\n"
147 " fakeinit: The new process will believe it is process number 1.\n"
148 " Useful to run a real /sbin/init in a vserver.\n"
149 " lock: The new process is trapped and can't use chcontext anymore.\n"
150 " sched: The new process and its children will share a common \n"
151 " execution priority.\n"
152 " nproc: Limit the number of process in the vserver according to\n"
153 " ulimit setting. Normally, ulimit is a per user thing.\n"
154 " With this flag, it becomes a per vserver thing.\n"
155 " private: No one can join this security context once created.\n"
156 " ulimit: Apply the current ulimit to the whole context\n"
158 "--hostname new_hostname\n"
159 " Set the hostname in the new security context\n"
160 " This is need because if you create a less privileged\n"
161 " security context, it may be unable to change its hostname\n"
164 " Remove all the capabilities to make a virtual server trustable\n"
167 " Do not print the allocated context number.\n"
169 "Please report bugs to " PACKAGE_BUGREPORT "\n");
178 "chcontext-compat " VERSION " -- allocates/enters a security context\n"
179 "This program is part of " PACKAGE_STRING "\n\n"
180 "Copyright (C) 2003,2004 Enrico Scholz\n"
181 VERSION_COPYRIGHT_DISCLAIMER);
185 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_LEGACY)
188 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
200 bit = vc_text2cap(str);
202 if (bit!=-1) *cap |= (1<<bit);
204 WRITE_MSG(2, "Unknown capability '");
207 exit(wrapper_exit_code);
212 setFlags(char const *str, uint32_t *flags)
214 struct vc_err_listparser err;
216 *flags = vc_list2cflag_compat(str, 0, &err);
219 WRITE_MSG(2, "Unknown flag '");
220 Vwrite(2, err.ptr, err.len);
222 exit(wrapper_exit_code);
226 static inline ALWAYSINLINE void
227 setHostname(char const *name)
229 if (name == NULL) return;
231 if (sethostname(name, strlen(name))==-1) {
232 perror("chcontext: sethostname()");
235 if (!global_args->do_silent) {
236 WRITE_MSG(1, "Host name is now ");
242 static inline ALWAYSINLINE void
243 setDomainname(char const *name)
245 if (name == NULL) return;
247 if (setdomainname(name, strlen(name))==-1) {
248 perror("chcontext: setdomainname()");
251 if (!global_args->do_silent) {
252 WRITE_MSG(1, "Domain name is now ");
258 static inline ALWAYSINLINE void
259 tellContext(xid_t ctx)
261 char buf[sizeof(xid_t)*3+2];
264 if (global_args->do_silent) return;
266 l = utilvserver_fmt_long(buf,ctx);
268 WRITE_MSG(1, "New security context is ");
273 #include "context-sync.hc"
278 int main (int argc, char *argv[])
280 struct Arguments args = {
282 .do_disconnect = false,
291 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_LEGACY)
299 signal(SIGCHLD, SIG_DFL);
302 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
306 case CMD_HELP : showHelp(1, argv[0], 0);
307 case CMD_VERSION : showVersion();
308 case CMD_DISCONNECT : args.do_disconnect = true; break;
309 case CMD_SILENT : args.do_silent = true; break;
310 case CMD_DOMAINNAME : args.domainname = optarg; break;
311 case CMD_HOSTNAME : args.hostname = optarg; break;
313 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_LEGACY)
315 setCap(optarg, &args.add_caps, &args.remove_caps);
318 args.remove_caps |= vc_get_insecurebcaps();
321 setFlags(optarg, &args.flags);
325 WRITE_MSG(2, "WARNING: More than one ctx not supported by this version\n");
326 if (args.nbctx>=DIM_OF(args.ctxs)) {
327 WRITE_MSG(2, "Too many contexts given\n");
330 args.ctxs[args.nbctx++] = Evc_xidopt2xid(optarg, true);
336 case CMD_CTX : break;
340 WRITE_MSG(2, "Try '");
341 WRITE_STR(2, argv[0]);
342 WRITE_MSG(2, " --help\" for more information.\n");
348 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_LEGACY)
350 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
354 if (args.domainname && strcmp(args.domainname, "none")==0)
355 args.domainname = "";
358 args.ctxs[args.nbctx++] = VC_DYNAMIC_XID;
360 xflags = args.flags & S_CTX_INFO_INIT;
361 args.flags &= ~S_CTX_INFO_INIT;
363 pid = initSync(p, args.do_disconnect);
365 doSyncStage0(p, args.do_disconnect);
367 newctx = Evc_new_s_context(args.ctxs[0],0,args.flags);
368 args.remove_caps &= (~args.add_caps);
369 setHostname(args.hostname);
370 setDomainname(args.domainname);
372 if (args.remove_caps!=0 || xflags!=0)
373 Evc_new_s_context (VC_SAMECTX,args.remove_caps,xflags);
374 tellContext(args.ctxs[0]==VC_DYNAMIC_XID ? newctx : args.ctxs[0]);
376 doSyncStage1(p, args.do_disconnect);
377 execvp (argv[optind],argv+optind);
378 doSyncStage2(p, args.do_disconnect);
380 PERROR_Q("chcontext: execvp", argv[optind]);
384 waitOnSync(pid, p, args.ctxs[0]!=VC_DYNAMIC_XID);
387 WRITE_MSG(2, "chcontext: tools were built without legacy API support; can not continue\n");
392 #if defined(VC_ENABLE_API_COMPAT) || defined(VC_ENABLE_API_LEGACY)
394 #ifdef ENSC_TESTSUITE
395 #define FLAG_TEST(STR,EXP) \
398 setFlags(STR, &flag); \
399 assert(flag==(EXP)); \
402 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
404 uint32_t add=0,del=0; \
405 setCap(STR, &add, &del); \
406 assert(add==(EXP_ADD)); \
407 assert(del==(EXP_DEL)); \
413 FLAG_TEST("lock", 1);
414 FLAG_TEST("lock,sched", 3);
416 CAP_TEST("CHOWN", 1, 0);
417 CAP_TEST("CAP_CHOWN", 1, 0);
418 CAP_TEST("!CHOWN", 0, 1);
419 CAP_TEST("!CAP_CHOWN", 0, 1);