use $(LIBENSCVECTOR) instead of libensc_vector.a
[util-vserver.git] / util-vserver / src / vcontext.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24 #include "lib/internal.h"
25 #include "lib_internal/jail.h"
26
27 #include <vserver.h>
28 #include <getopt.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <assert.h>
34 #include <signal.h>
35
36 #define ENSC_WRAPPERS_PREFIX    "vcontext: "
37 #define ENSC_WRAPPERS_UNISTD    1
38 #define ENSC_WRAPPERS_VSERVER   1
39 #define ENSC_WRAPPERS_FCNTL     1
40 #define ENSC_WRAPPERS_SOCKET    1
41 #define ENSC_WRAPPERS_IOSOCK    1
42 #include <wrappers.h>
43
44 #define CMD_HELP                0x1000
45 #define CMD_VERSION             0x1001
46 #define CMD_XID                 0x4000
47 #define CMD_CREATE              0x4001
48 #define CMD_MIGRATE             0x4003
49 #define CMD_INITPID             0x4002
50 #define CMD_DISCONNECT          0x4004
51 #define CMD_UID                 0x4005
52 #define CMD_CHROOT              0x4006
53 #define CMD_SILENT              0x4007
54 #define CMD_SYNCSOCK            0x4008
55 #define CMD_SYNCMSG             0x4009
56 #define CMD_MIGRATESELF         0x400a
57 #define CMD_ENDSETUP            0x400b
58 #define CMD_SILENTEXIST         0x400c
59 #define CMD_NAMESPACE           0x400d
60
61
62 struct option const
63 CMDLINE_OPTIONS[] = {
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   { "initpid",      no_argument,        0, CMD_INITPID },
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   { "namespace",    no_argument,        0, CMD_NAMESPACE },
79   { "syncsock",     required_argument,  0, CMD_SYNCSOCK },
80   { "syncmsg",      required_argument,  0, CMD_SYNCMSG },
81 #if 1  
82   { "fakeinit",     no_argument,        0, CMD_INITPID },       // compatibility
83 #endif  
84   { 0,0,0,0 },
85 };
86
87 struct Arguments {
88     bool                do_create;
89     bool                do_migrate;
90     bool                do_migrateself;
91     bool                do_disconnect;
92     bool                do_endsetup;
93     bool                is_initpid;
94     bool                is_silentexist;
95     bool                set_namespace;
96     int                 verbosity;
97     bool                do_chroot;
98     uid_t               uid;
99     xid_t               xid;
100     char const *        sync_sock;
101     char const *        sync_msg;
102 };
103
104 int             wrapper_exit_code = 255;
105
106 static void
107 showHelp(int fd, char const *cmd, int res)
108 {
109   WRITE_MSG(fd, "Usage:\n    ");
110   WRITE_STR(fd, cmd);
111   WRITE_MSG(fd,
112             " --create [--xid <xid>] <opts>* [--] <program> <args>*\n    ");
113   WRITE_STR(fd, cmd);
114   WRITE_MSG(fd,
115             " [(--migrate --xid <xid>)|--migrate-self]  <opts>* [--] <program> <args>*\n"
116             "\n"
117             "<opts> can be:\n"
118             "    --chroot        ...  chroot into current directory\n"
119             "    --namespace     ...  execute namespace management operations\n"
120             "    --uid <uid>     ...  change uid\n"
121             "    --initpid       ...  set current process as general process reaper\n"
122             "                         for ctx (possible for --migrate only)\n"
123             "    --endsetup      ...  clear the setup flag; usefully for migrate only\n"
124             "    --disconnect    ...  start program in background\n"
125             "    --silent        ...  be silent\n"
126             "    --silentexist   ...  be silent when context exists already; usefully\n"
127             "                         for '--create' only\n"
128             "    --syncsock <file-name>\n"
129             "                    ...  before executing the program, send a message\n"
130             "                         to the socket and wait until it closes.\n"
131             "                         <file-name> must be a SOCK_STREAM unix socket\n"
132             "    --syncmsg <message>\n"
133             "                    ...  use <message> as synchronization message; by\n"
134             "                         default, 'ok' will be used\n"
135             "\n"
136             "'vcontext --create' exits with code 254 iff the context exists already.\n"
137             "\n"
138             "Please report bugs to " PACKAGE_BUGREPORT "\n");
139
140   exit(res);
141 }
142
143 static void
144 showVersion()
145 {
146   WRITE_MSG(1,
147             "vcontext " VERSION " -- manages the creation of security contexts\n"
148             "This program is part of " PACKAGE_STRING "\n\n"
149             "Copyright (C) 2004 Enrico Scholz\n"
150             VERSION_COPYRIGHT_DISCLAIMER);
151   exit(0);
152 }
153
154 #include "context-sync.hc"
155
156 static inline ALWAYSINLINE void
157 tellContext(xid_t ctx, bool do_it)
158 {
159   char          buf[sizeof(xid_t)*3+2];
160   size_t        l;
161
162   if (!do_it) return;
163
164   l = utilvserver_fmt_long(buf,ctx);
165
166   WRITE_MSG(1, "New security context is ");
167   Vwrite   (1, buf, l);
168   WRITE_MSG(1, "\n");
169 }
170
171 static int
172 connectExternalSync(char const *filename)
173 {
174   int                   fd;
175   struct sockaddr_un    addr;
176   
177   if (filename==0) return -1;
178
179   ENSC_INIT_UNIX_SOCK(addr, filename);
180
181   fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
182   Econnect(fd, &addr, sizeof(addr));
183
184   return fd;
185 }
186
187 static void
188 setFlags(struct Arguments const *args, xid_t xid)
189 {
190   struct vc_ctx_flags   flags = { 0,0 };
191
192   if (args->is_initpid)
193     flags.mask |=  VC_VXF_STATE_INIT;
194
195   if (args->do_endsetup)
196     flags.mask |=  VC_VXF_STATE_SETUP;
197
198   if (flags.mask!=0) {
199     DPRINTF("set_flags: mask=%08llx, flag=%08llx\n", flags.mask, flags.flagword);
200     Evc_set_cflags(xid, &flags);
201   }
202 }
203
204 static void
205 doExternalSync(int fd, char const *msg)
206 {
207   char          c;
208   
209   if (fd==-1) return;
210
211   if (msg) EsendAll(fd, msg, strlen(msg));
212   Eshutdown(fd, SHUT_WR);
213
214   if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
215     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unexpected external synchronization event\n");
216     exit(255);
217   }
218
219   Eclose(fd);
220 }
221
222 static inline ALWAYSINLINE int
223 doit(struct Arguments const *args, char *argv[])
224 {
225   int                   p[2][2];
226   pid_t                 pid = initSync(p, args->do_disconnect);
227   
228   if (pid==0) {
229     xid_t                       xid;
230     int                         ext_sync_fd = connectExternalSync(args->sync_sock);
231
232     doSyncStage0(p, args->do_disconnect);
233     
234     if (args->do_create) {
235       xid = vc_ctx_create(args->xid);
236       if (xid==VC_NOCTX) {
237         switch (errno) {
238           case EEXIST   :
239             if (!args->is_silentexist)
240               perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
241             return 254;
242           default       :
243             perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
244             return wrapper_exit_code;
245         }
246       }
247       tellContext(xid, args->verbosity>=1);
248     }
249     else
250       xid = args->xid;
251
252     if (args->do_chroot) {
253       Echroot(".");
254       if (args->set_namespace) {
255         if (args->do_migrateself)  Evc_set_namespace();
256         else if (args->do_migrate) Evc_enter_namespace(xid);
257       }
258     }
259
260     setFlags(args, xid);
261
262     if (args->do_migrate && !args->do_migrateself)
263       Evc_ctx_migrate(xid);
264
265     if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
266       Esetuid(args->uid);
267       if (getuid()!=args->uid) {
268         WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Something went wrong while changing the UID\n");
269         exit(255);
270       }
271     }
272     
273     doExternalSync(ext_sync_fd, args->sync_msg);
274     doSyncStage1(p, args->do_disconnect);
275     DPRINTF("doit: pid=%u, ppid=%u\n", getpid(), getppid());
276     execvp (argv[optind],argv+optind);
277     doSyncStage2(p, args->do_disconnect);
278
279     PERROR_Q(ENSC_WRAPPERS_PREFIX "execvp", argv[optind]);
280     exit(255);
281   }
282
283   assert(args->do_disconnect);
284     
285   waitOnSync(pid, p, args->xid!=VC_DYNAMIC_XID && args->do_migrate);
286   return EXIT_SUCCESS;
287 }
288
289 int main (int argc, char *argv[])
290 {
291   struct Arguments              args = {
292     .do_create      = false,
293     .do_migrate     = false,
294     .do_migrateself = false,
295     .do_disconnect  = false,
296     .do_endsetup    = false,
297     .is_initpid     = false,
298     .is_silentexist = false,
299     .set_namespace  = false,
300     .verbosity      = 1,
301     .uid            = -1,
302     .xid            = VC_DYNAMIC_XID,
303     .sync_msg       = "ok",
304   };
305   
306   while (1) {
307     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
308     if (c==-1) break;
309     
310     switch (c) {
311       case CMD_HELP             :  showHelp(1, argv[0], 0);
312       case CMD_VERSION          :  showVersion();
313       case CMD_CREATE           :  args.do_create      = true;   break;
314       case CMD_MIGRATE          :  args.do_migrate     = true;   break;
315       case CMD_DISCONNECT       :  args.do_disconnect  = true;   break;
316       case CMD_ENDSETUP         :  args.do_endsetup    = true;   break;
317       case CMD_INITPID          :  args.is_initpid     = true;   break;
318       case CMD_CHROOT           :  args.do_chroot      = true;   break;
319       case CMD_NAMESPACE        :  args.set_namespace  = true;   break;
320       case CMD_SILENTEXIST      :  args.is_silentexist = true;   break;
321       case CMD_SYNCSOCK         :  args.sync_sock      = optarg; break;
322       case CMD_SYNCMSG          :  args.sync_msg       = optarg; break;
323       case CMD_UID              :  args.uid = atol(optarg);      break;
324       case CMD_XID              :  args.xid = Evc_xidopt2xid(optarg,true); break;
325       case CMD_SILENT           :  --args.verbosity; break;
326       case CMD_MIGRATESELF      :
327         args.do_migrate     = true;
328         args.do_migrateself = true;
329         break;
330
331       default           :
332         WRITE_MSG(2, "Try '");
333         WRITE_STR(2, argv[0]);
334         WRITE_MSG(2, " --help\" for more information.\n");
335         return 255;
336         break;
337     }
338   }
339
340   signal(SIGCHLD, SIG_DFL);
341   
342   if (args.do_migrateself)
343     args.xid = Evc_get_task_xid(0);
344   
345   if (!args.do_create && !args.do_migrate)
346     WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
347   else if (args.do_create  &&  args.do_migrate)
348     WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
349   else if (!args.do_migrate && args.is_initpid)
350     WRITE_MSG(2, "'--initpid' is possible in combination with '--migrate' only\n");
351   else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
352     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "Can not migrate to an unknown context\n");
353   else if (optind>=argc)
354     WRITE_MSG(2, "No command given; use '--help' for more information.\n");
355   else
356     return doit(&args, argv);
357
358   return 255;
359 }