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