3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "lib/internal.h"
25 #include "lib_internal/jail.h"
27 #include "linuxcaps.h"
33 #include <sys/socket.h>
37 #define ENSC_WRAPPERS_PREFIX "vcontext: "
38 #define ENSC_WRAPPERS_UNISTD 1
39 #define ENSC_WRAPPERS_VSERVER 1
40 #define ENSC_WRAPPERS_FCNTL 1
41 #define ENSC_WRAPPERS_SOCKET 1
42 #define ENSC_WRAPPERS_IOSOCK 1
45 #define CMD_HELP 0x1000
46 #define CMD_VERSION 0x1001
47 #define CMD_XID 0x4000
48 #define CMD_CREATE 0x4001
49 #define CMD_MIGRATE 0x4003
50 #define CMD_FAKEINIT 0x4002
51 #define CMD_DISCONNECT 0x4004
52 #define CMD_UID 0x4005
53 #define CMD_CHROOT 0x4006
54 #define CMD_SILENT 0x4007
55 #define CMD_SYNCSOCK 0x4008
56 #define CMD_SYNCMSG 0x4009
57 #define CMD_MIGRATESELF 0x400a
58 #define CMD_ENDSETUP 0x400b
59 #define CMD_SILENTEXIST 0x400c
64 { "help", no_argument, 0, CMD_HELP },
65 { "version", no_argument, 0, CMD_VERSION },
66 { "ctx", required_argument, 0, CMD_XID },
67 { "xid", required_argument, 0, CMD_XID },
68 { "create", no_argument, 0, CMD_CREATE },
69 { "migrate", no_argument, 0, CMD_MIGRATE },
70 { "migrate-self", no_argument, 0, CMD_MIGRATESELF },
71 { "fakeinit", no_argument, 0, CMD_FAKEINIT },
72 { "endsetup", no_argument, 0, CMD_ENDSETUP },
73 { "disconnect", no_argument, 0, CMD_DISCONNECT },
74 { "silent", no_argument, 0, CMD_SILENT },
75 { "silentexist", no_argument, 0, CMD_SILENTEXIST },
76 { "uid", required_argument, 0, CMD_UID },
77 { "chroot", no_argument, 0, CMD_CHROOT },
78 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
79 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
95 char const * sync_sock;
96 char const * sync_msg;
99 int wrapper_exit_code = 255;
102 showHelp(int fd, char const *cmd, int res)
104 WRITE_MSG(fd, "Usage:\n ");
107 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
110 " [(--migrate --xid <xid>)|--migrate-self] <opts>* [--] <program> <args>*\n"
113 " --chroot ... chroot into current directory\n"
114 " --uid <uid> ... change uid\n"
115 " --fakeinit ... set current process as general process reaper\n"
116 " for ctx (possible for --migrate only)\n"
117 " --endsetup ... clear the setup flag; usefully for migrate only\n"
118 " --disconnect ... start program in background\n"
119 " --silent ... be silent\n"
120 " --silentexist ... be silent when context exists already; usefully\n"
121 " for '--create' only\n"
122 " --syncsock <file-name>\n"
123 " ... before executing the program, send a message\n"
124 " to the socket and wait until it closes.\n"
125 " <file-name> must be a SOCK_STREAM unix socket\n"
126 " --syncmsg <message>\n"
127 " ... use <message> as synchronization message; by\n"
128 " default, 'ok' will be used\n"
130 "'vcontext --create' exits with code 254 iff the context exists already.\n"
132 "Please report bugs to " PACKAGE_BUGREPORT "\n");
141 "vcontext " VERSION " -- manages the creation of security contexts\n"
142 "This program is part of " PACKAGE_STRING "\n\n"
143 "Copyright (C) 2004 Enrico Scholz\n"
144 VERSION_COPYRIGHT_DISCLAIMER);
148 #include "context-sync.hc"
150 static inline ALWAYSINLINE void
151 tellContext(xid_t ctx, bool do_it)
153 char buf[sizeof(xid_t)*3+2];
158 l = utilvserver_fmt_long(buf,ctx);
160 WRITE_MSG(1, "New security context is ");
166 connectExternalSync(char const *filename)
169 struct sockaddr_un addr;
171 if (filename==0) return -1;
173 ENSC_INIT_UNIX_SOCK(addr, filename);
175 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
176 Econnect(fd, &addr, sizeof(addr));
182 setFlags(struct Arguments const *args, xid_t xid)
184 struct vc_ctx_flags flags = { 0,0 };
186 if (args->is_fakeinit)
187 flags.mask |= VC_VXF_STATE_INIT;
189 if (args->do_endsetup)
190 flags.mask |= VC_VXF_STATE_SETUP;
193 DPRINTF("set_flags: mask=%08llx, flag=%08llx\n", flags.mask, flags.flagword);
194 Evc_set_cflags(xid, &flags);
199 doExternalSync(int fd, char const *msg)
205 if (msg) EsendAll(fd, msg, strlen(msg));
206 Eshutdown(fd, SHUT_WR);
208 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
209 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unexpected external synchronization event\n");
216 static inline ALWAYSINLINE int
217 doit(struct Arguments const *args, char *argv[])
220 pid_t pid = initSync(p, args->do_disconnect);
224 int ext_sync_fd = connectExternalSync(args->sync_sock);
226 doSyncStage0(p, args->do_disconnect);
228 if (args->do_create) {
229 xid = vc_ctx_create(args->xid);
233 if (!args->is_silentexist)
234 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
237 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
238 return wrapper_exit_code;
241 tellContext(xid, args->verbosity>=1);
251 if (args->do_migrate && !args->do_migrateself)
252 Evc_ctx_migrate(xid);
254 if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
256 if (getuid()!=args->uid) {
257 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
262 doExternalSync(ext_sync_fd, args->sync_msg);
263 doSyncStage1(p, args->do_disconnect);
264 DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
265 execvp (argv[optind],argv+optind);
266 doSyncStage2(p, args->do_disconnect);
268 PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
272 assert(args->do_disconnect);
274 waitOnSync(pid, p, args->xid!=VC_DYNAMIC_XID && args->do_migrate);
278 int main (int argc, char *argv[])
280 struct Arguments args = {
283 .do_migrateself = false,
284 .do_disconnect = false,
285 .do_endsetup = false,
286 .is_fakeinit = false,
287 .is_silentexist = false,
290 .xid = VC_DYNAMIC_XID,
295 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
299 case CMD_HELP : showHelp(1, argv[0], 0);
300 case CMD_VERSION : showVersion();
301 case CMD_CREATE : args.do_create = true; break;
302 case CMD_MIGRATE : args.do_migrate = true; break;
303 case CMD_DISCONNECT : args.do_disconnect = true; break;
304 case CMD_ENDSETUP : args.do_endsetup = true; break;
305 case CMD_FAKEINIT : args.is_fakeinit = true; break;
306 case CMD_CHROOT : args.do_chroot = true; break;
307 case CMD_SILENTEXIST : args.is_silentexist = true; break;
308 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
309 case CMD_SYNCMSG : args.sync_msg = optarg; break;
310 case CMD_UID : args.uid = atol(optarg); break;
311 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
312 case CMD_SILENT : --args.verbosity; break;
313 case CMD_MIGRATESELF :
314 args.do_migrate = true;
315 args.do_migrateself = true;
319 WRITE_MSG(2, "Try '");
320 WRITE_STR(2, argv[0]);
321 WRITE_MSG(2, " --help\" for more information.\n");
327 if (args.do_migrateself)
328 args.xid = Evc_get_task_xid(0);
330 if (!args.do_create && !args.do_migrate)
331 WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
332 else if (args.do_create && args.do_migrate)
333 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
334 else if (!args.do_migrate && args.is_fakeinit)
335 WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
336 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
337 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
338 else if (optind>=argc)
339 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
341 return doit(&args, argv);