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 #define ENSC_WRAPPERS_UNISTD 1
31 #define ENSC_WRAPPERS_VSERVER 1
32 #define ENSC_WRAPPERS_FCNTL 1
35 #define CMD_HELP 0x1000
36 #define CMD_VERSION 0x1001
37 #define CMD_XID 0x4000
38 #define CMD_CREATE 0x4001
39 #define CMD_MIGRATE 0x4003
40 #define CMD_FAKEINIT 0x4002
41 #define CMD_DISCONNECT 0x4004
42 #define CMD_UID 0x4005
43 #define CMD_CHROOT 0x4006
44 #define CMD_SILENT 0x4007
48 { "help", no_argument, 0, CMD_HELP },
49 { "version", no_argument, 0, CMD_VERSION },
50 { "ctx", required_argument, 0, CMD_XID },
51 { "xid", required_argument, 0, CMD_XID },
52 { "create", no_argument, 0, CMD_CREATE },
53 { "migrate", no_argument, 0, CMD_MIGRATE },
54 { "fakeinit", no_argument, 0, CMD_FAKEINIT },
55 { "disconnect", no_argument, 0, CMD_DISCONNECT },
56 { "silent", no_argument, 0, CMD_SILENT },
57 { "uid", no_argument, 0, CMD_UID },
58 { "chroot", no_argument, 0, CMD_CHROOT },
73 int wrapper_exit_code = 255;
76 showHelp(int fd, char const *cmd, int res)
78 WRITE_MSG(fd, "Usage: ");
81 " [--xid <xid>] <opts>* [--] <program> <args>*\n"
82 " --create [--migrate] [--xid <xid>] <opts>* [--] <program> <args>*\n"
83 " --migrate --xid <xid> <opts>* [--] <program> <args>*\n"
86 " --chroot ... chroot into current directory\n"
87 " --uid <uid> ... change uid\n"
88 " --fakeinit ... set current process as general process reaper\n"
89 " for ctx (possible for --migrate only)\n"
90 " --silent ... be silent\n"
92 "Please report bugs to " PACKAGE_BUGREPORT "\n");
101 "vcontext " VERSION " -- manages the creation of security contexts\n"
102 "This program is part of " PACKAGE_STRING "\n\n"
103 "Copyright (C) 2003,2004 Enrico Scholz\n"
104 VERSION_COPYRIGHT_DISCLAIMER);
108 static inline ALWAYSINLINE int
109 initSync(int p[2], bool do_disconnect)
111 if (!do_disconnect) return 0;
114 fcntl(p[1], F_SETFD, FD_CLOEXEC);
118 static inline ALWAYSINLINE void
119 doSyncStage1(int p[2], bool do_disconnect)
123 if (!do_disconnect) return;
125 fd = Eopen("/dev/null", O_RDONLY, 0);
129 if (fd!=0) Eclose(fd);
130 Ewrite(p[1], ".", 1);
133 static inline ALWAYSINLINE void
134 doSyncStage2(int p[2], bool do_disconnect)
136 if (!do_disconnect) return;
138 Ewrite(p[1], "X", 1);
142 waitOnSync(pid_t pid, int p[2])
148 l = Eread(p[0], &c, 1);
149 if (l!=1) exitLikeProcess(pid);
150 l = Eread(p[0], &c, 1);
151 if (l!=0) exitLikeProcess(pid);
154 static inline ALWAYSINLINE void
155 tellContext(xid_t ctx, bool do_it)
157 char buf[sizeof(xid_t)*3+2];
162 l = utilvserver_fmt_long(buf,ctx);
164 WRITE_MSG(2, "New security context is ");
169 int main (int argc, char *argv[])
173 struct Arguments args = {
176 .do_disconnect = false,
177 .is_fakeinit = false,
180 .xid = VC_DYNAMIC_XID,
184 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
188 case CMD_HELP : showHelp(1, argv[0], 0);
189 case CMD_VERSION : showVersion();
190 case CMD_CREATE : args.do_create = true; break;
191 case CMD_MIGRATE : args.do_migrate = true; break;
192 case CMD_DISCONNECT : args.do_disconnect = true; break;
193 case CMD_FAKEINIT : args.is_fakeinit = true; break;
194 case CMD_CHROOT : args.do_chroot = true; break;
195 case CMD_SILENT : --args.verbosity; break;
196 case CMD_XID : args.xid = atol(optarg); break;
197 case CMD_UID : args.uid = atol(optarg); break;
200 WRITE_MSG(2, "Try '");
201 WRITE_STR(2, argv[0]);
202 WRITE_MSG(2, " --help\" for more information.\n");
209 WRITE_MSG(2, "No command given; use '--help' for more information.\n");
213 if (!args.do_create && !args.do_migrate)
214 args.do_create = args.do_migrate = true;
216 if (!args.do_migrate && args.is_fakeinit) {
217 WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
221 if (!args.do_create && args.xid==VC_DYNAMIC_XID) {
222 WRITE_MSG(2, "vcontext: Can not migrate to an unknown context\n");
226 pid = initSync(p, args.do_disconnect);
229 if (args.do_create) {
230 xid = Evc_create_context(args.xid);
231 tellContext(xid, args.verbosity>=1);
239 if (args.uid!=(uid_t)(-1) && getuid()!=args.uid) {
241 if (getuid()!=args.uid) {
242 WRITE_MSG(2, "vcontext: Something went wrong while changing the UID\n");
247 if (args.is_fakeinit) {
248 struct vc_ctx_flags flags;
249 Evc_get_flags(xid, &flags);
250 #warning !!! IMPLEMENT ME !!!
253 WRITE_MSG(2, "vcontext: context has already a fakeinit-process\n");
259 Evc_migrate_context(xid);
261 doSyncStage1(p, args.do_disconnect);
262 execvp (argv[optind],argv+optind);
263 doSyncStage2(p, args.do_disconnect);
265 PERROR_Q("chcontext: execvp", argv[optind]);