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"
30 #include <sys/socket.h>
33 #define ENSC_WRAPPERS_PREFIX "vcontext: "
34 #define ENSC_WRAPPERS_UNISTD 1
35 #define ENSC_WRAPPERS_VSERVER 1
36 #define ENSC_WRAPPERS_FCNTL 1
37 #define ENSC_WRAPPERS_SOCKET 1
38 #define ENSC_WRAPPERS_IOSOCK 1
41 #define CMD_HELP 0x1000
42 #define CMD_VERSION 0x1001
43 #define CMD_XID 0x4000
44 #define CMD_CREATE 0x4001
45 #define CMD_MIGRATE 0x4003
46 #define CMD_FAKEINIT 0x4002
47 #define CMD_DISCONNECT 0x4004
48 #define CMD_UID 0x4005
49 #define CMD_CHROOT 0x4006
50 #define CMD_SILENT 0x4007
51 #define CMD_SYNCSOCK 0x4008
52 #define CMD_SYNCMSG 0x4009
56 { "help", no_argument, 0, CMD_HELP },
57 { "version", no_argument, 0, CMD_VERSION },
58 { "ctx", required_argument, 0, CMD_XID },
59 { "xid", required_argument, 0, CMD_XID },
60 { "create", no_argument, 0, CMD_CREATE },
61 { "migrate", no_argument, 0, CMD_MIGRATE },
62 { "fakeinit", no_argument, 0, CMD_FAKEINIT },
63 { "disconnect", no_argument, 0, CMD_DISCONNECT },
64 { "silent", no_argument, 0, CMD_SILENT },
65 { "uid", no_argument, 0, CMD_UID },
66 { "chroot", no_argument, 0, CMD_CHROOT },
67 { "syncsock", required_argument, 0, CMD_SYNCSOCK },
68 { "syncmsg", required_argument, 0, CMD_SYNCMSG },
81 char const * sync_sock;
82 char const * sync_msg;
85 int wrapper_exit_code = 255;
88 showHelp(int fd, char const *cmd, int res)
90 WRITE_MSG(fd, "Usage:\n ");
93 " --create [--xid <xid>] <opts>* [--] <program> <args>*\n ");
96 " --migrate --xid <xid> <opts>* [--] <program> <args>*\n"
99 " --chroot ... chroot into current directory\n"
100 " --uid <uid> ... change uid\n"
101 " --fakeinit ... set current process as general process reaper\n"
102 " for ctx (possible for --migrate only)\n"
103 " --disconnect ... start program in background\n"
104 " --silent ... be silent\n"
105 " --syncsock <file-name>\n"
106 " ... before executing the program, send a message\n"
107 " to the socket and wait until it closes.\n"
108 " <file-name> must be a SOCK_STREAM unix socket\n"
109 " --syncmsg <message>\n"
110 " ... use <message> as synchronization message; by\n"
111 " default, 'ok' will be used\n"
113 "'vcontext --create' exits with code 254 iff the context exists already.\n"
115 "Please report bugs to " PACKAGE_BUGREPORT "\n");
124 "vcontext " VERSION " -- manages the creation of security contexts\n"
125 "This program is part of " PACKAGE_STRING "\n\n"
126 "Copyright (C) 2004 Enrico Scholz\n"
127 VERSION_COPYRIGHT_DISCLAIMER);
131 static inline ALWAYSINLINE int
132 initSync(int p[2], bool do_disconnect)
134 if (!do_disconnect) return 0;
137 fcntl(p[1], F_SETFD, FD_CLOEXEC);
141 static inline ALWAYSINLINE void
142 doSyncStage1(int p[2], bool do_disconnect)
146 if (!do_disconnect) return;
148 fd = Eopen("/dev/null", O_RDONLY, 0);
152 if (fd!=0) Eclose(fd);
153 Ewrite(p[1], ".", 1);
156 static inline ALWAYSINLINE void
157 doSyncStage2(int p[2], bool do_disconnect)
159 if (!do_disconnect) return;
161 Ewrite(p[1], "X", 1);
165 waitOnSync(pid_t pid, int p[2])
171 l = Eread(p[0], &c, 1);
172 if (l!=1) exitLikeProcess(pid);
173 l = Eread(p[0], &c, 1);
174 if (l!=0) exitLikeProcess(pid);
177 static inline ALWAYSINLINE void
178 tellContext(xid_t ctx, bool do_it)
180 char buf[sizeof(xid_t)*3+2];
185 l = utilvserver_fmt_long(buf,ctx);
187 WRITE_MSG(2, "New security context is ");
193 connectExternalSync(char const *filename)
196 struct sockaddr_un addr;
198 if (filename==0) return -1;
200 ENSC_INIT_UNIX_SOCK(addr, filename);
202 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
203 Econnect(fd, &addr, sizeof(addr));
209 doExternalSync(int fd, char const *msg)
215 if (msg) EsendAll(fd, msg, strlen(msg));
216 Eshutdown(fd, SHUT_WR);
218 if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
219 WRITE_MSG(2, "vcontext: unexpected external synchronization event\n");
226 static inline ALWAYSINLINE int
227 doit(struct Arguments const *args, char *argv[])
230 pid_t pid = initSync(p, args->do_disconnect);
234 int ext_sync_fd = connectExternalSync(args->sync_sock);
236 if (args->do_create) {
237 xid = vc_create_context(args->xid);
239 if (errno==EEXIST) return 254;
241 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
242 return wrapper_exit_code;
245 tellContext(xid, args->verbosity>=1);
253 if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
255 if (getuid()!=args->uid) {
256 WRITE_MSG(2, "vcontext: Something went wrong while changing the UID\n");
261 if (args->is_fakeinit) {
262 struct vc_ctx_flags flags;
263 Evc_get_flags(xid, &flags);
264 #warning !!! IMPLEMENT ME !!!
267 WRITE_MSG(2, "vcontext: context has already a fakeinit-process\n");
272 if (args->do_migrate)
273 Evc_migrate_context(xid);
275 doExternalSync(ext_sync_fd, args->sync_msg);
276 doSyncStage1(p, args->do_disconnect);
277 execvp (argv[optind],argv+optind);
278 doSyncStage2(p, args->do_disconnect);
280 PERROR_Q("chcontext: execvp", argv[optind]);
288 int main (int argc, char *argv[])
290 struct Arguments args = {
293 .do_disconnect = false,
294 .is_fakeinit = false,
297 .xid = VC_DYNAMIC_XID,
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_CREATE : args.do_create = true; break;
309 case CMD_MIGRATE : args.do_migrate = true; break;
310 case CMD_DISCONNECT : args.do_disconnect = true; break;
311 case CMD_FAKEINIT : args.is_fakeinit = true; break;
312 case CMD_CHROOT : args.do_chroot = true; break;
313 case CMD_SILENT : --args.verbosity; break;
314 case CMD_XID : args.xid = atol(optarg); break;
315 case CMD_UID : args.uid = atol(optarg); break;
316 case CMD_SYNCSOCK : args.sync_sock = optarg; break;
317 case CMD_SYNCMSG : args.sync_msg = optarg; break;
320 WRITE_MSG(2, "Try '");
321 WRITE_STR(2, argv[0]);
322 WRITE_MSG(2, " --help\" for more information.\n");
328 if (!args.do_create && !args.do_migrate)
329 WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
330 else if (args.do_create && args.do_migrate)
331 WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
332 else if (!args.do_migrate && args.is_fakeinit)
333 WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
334 else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
335 WRITE_MSG(2, "vcontext: Can not migrate to an unknown context\n");
336 else if (optind>=argc)
337 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
339 return doit(&args, argv);