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"
44 #define ENSC_WRAPPERS_PREFIX "chcontext: "
45 #define ENSC_WRAPPERS_VSERVER 1
46 #define ENSC_WRAPPERS_UNISTD 1
47 #define ENSC_WRAPPERS_FCNTL 1
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
61 int wrapper_exit_code = 255;
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 },
87 char const * hostname;
88 char const * domainname;
91 static struct Arguments const * global_args = 0;
94 showHelp(int fd, char const *cmd, int res)
96 VSERVER_DECLARE_CMD(cmd);
98 WRITE_MSG(fd, "Usage: ");
101 " [--cap [!]<cap_name>] [--secure] [--xid <num>] [--disconnect]\n"
102 " [--domainname <name>] [--hostname <name>] [--flag <flags>+]\n"
103 " [--silent] [--] command arguments ...\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"
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"
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"
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"
131 " Start the command in background and make the process\n"
132 " a child of process 1.\n"
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"
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"
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"
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"
159 " Remove all the capabilities to make a virtual server trustable\n"
162 " Do not print the allocated context number.\n"
164 "Please report bugs to " PACKAGE_BUGREPORT "\n");
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);
181 setCap(char const *str, uint32_t *add_caps, uint32_t *remove_caps)
193 bit = vc_text2cap(str);
195 if (bit!=-1) *cap |= (1<<bit);
197 WRITE_MSG(2, "Unknown capability '");
200 exit(wrapper_exit_code);
205 setFlags(char const *str, uint32_t *flags)
207 struct vc_err_listparser err;
209 *flags = vc_list2flag_compat(str, 0, &err);
212 WRITE_MSG(2, "Unknown flag '");
213 write(2, err.ptr, err.len);
215 exit(wrapper_exit_code);
219 static inline ALWAYSINLINE void
220 setHostname(char const *name)
222 if (name == NULL) return;
224 if (sethostname(name, strlen(name))==-1) {
225 perror("chcontext: sethostname()");
228 if (!global_args->do_silent) {
229 WRITE_MSG(1, "Host name is now ");
235 static inline ALWAYSINLINE void
236 setDomainname(char const *name)
238 if (name == NULL) return;
240 if (setdomainname(name, strlen(name))==-1) {
241 perror("chcontext: setdomainname()");
244 if (!global_args->do_silent) {
245 WRITE_MSG(1, "Domain name is now ");
251 static inline ALWAYSINLINE void
252 tellContext(xid_t ctx)
254 char buf[sizeof(xid_t)*3+2];
257 if (global_args->do_silent) return;
259 l = utilvserver_fmt_long(buf,ctx);
261 WRITE_MSG(1, "New security context is ");
266 #include "context-sync.hc"
268 int main (int argc, char *argv[])
270 struct Arguments args = {
272 .do_disconnect = false,
288 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
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;
300 setCap(optarg, &args.add_caps, &args.remove_caps);
303 args.remove_caps |= vc_get_insecurecaps();
306 setFlags(optarg, &args.flags);
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");
315 args.ctxs[args.nbctx++] = Evc_xidopt2xid(optarg, true);
320 WRITE_MSG(2, "Try '");
321 WRITE_STR(2, argv[0]);
322 WRITE_MSG(2, " --help\" for more information.\n");
329 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
333 if (args.domainname && strcmp(args.domainname, "none")==0)
334 args.domainname = "";
337 args.ctxs[args.nbctx++] = VC_DYNAMIC_XID;
339 xflags = args.flags & S_CTX_INFO_INIT;
340 args.flags &= ~S_CTX_INFO_INIT;
342 pid = initSync(p, args.do_disconnect);
344 doSyncStage0(p, args.do_disconnect);
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);
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]);
355 doSyncStage1(p, args.do_disconnect);
356 execvp (argv[optind],argv+optind);
357 doSyncStage2(p, args.do_disconnect);
359 PERROR_Q("chcontext: execvp", argv[optind]);
363 waitOnSync(pid, p, args.ctxs[0]!=VC_DYNAMIC_XID);
367 #ifdef ENSC_TESTSUITE
368 #define FLAG_TEST(STR,EXP) \
371 setFlags(STR, &flag); \
372 assert(flag==(EXP)); \
375 #define CAP_TEST(STR,EXP_ADD,EXP_DEL) \
377 uint32_t add=0,del=0; \
378 setCap(STR, &add, &del); \
379 assert(add==(EXP_ADD)); \
380 assert(del==(EXP_DEL)); \
386 FLAG_TEST("lock", 1);
387 FLAG_TEST("lock,sched", 3);
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);