added support for new migrate feature
[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
26 #include <vserver.h>
27 #include <getopt.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
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
39 #include <wrappers.h>
40
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
53
54 struct option const
55 CMDLINE_OPTIONS[] = {
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 },
69   { 0,0,0,0 },
70 };
71
72 struct Arguments {
73     bool                do_create;
74     bool                do_migrate;
75     bool                do_disconnect;
76     bool                is_fakeinit;
77     int                 verbosity;
78     bool                do_chroot;
79     uid_t               uid;
80     xid_t               xid;
81     char const *        sync_sock;
82     char const *        sync_msg;
83 };
84
85 int             wrapper_exit_code = 255;
86
87 static void
88 showHelp(int fd, char const *cmd, int res)
89 {
90   WRITE_MSG(fd, "Usage:\n    ");
91   WRITE_STR(fd, cmd);
92   WRITE_MSG(fd,
93             " --create [--xid <xid>] <opts>* [--] <program> <args>*\n    ");
94   WRITE_STR(fd, cmd);
95   WRITE_MSG(fd,
96             " --migrate --xid <xid>  <opts>* [--] <program> <args>*\n"
97             "\n"
98             "<opts> can be:\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"
112             "\n"
113             "'vcontext --create' exits with code 254 iff the context exists already.\n"
114             "\n"
115             "Please report bugs to " PACKAGE_BUGREPORT "\n");
116
117   exit(res);
118 }
119
120 static void
121 showVersion()
122 {
123   WRITE_MSG(1,
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);
128   exit(0);
129 }
130
131 static inline ALWAYSINLINE int
132 initSync(int p[2], bool do_disconnect)
133 {
134   if (!do_disconnect) return 0;
135
136   Epipe(p);
137   fcntl(p[1], F_SETFD, FD_CLOEXEC);
138   return Efork();
139 }
140
141 static inline ALWAYSINLINE void
142 doSyncStage1(int p[2], bool do_disconnect)
143 {
144   int   fd;
145
146   if (!do_disconnect) return;
147   
148   fd = Eopen("/dev/null", O_RDONLY, 0);
149   Esetsid();
150   Edup2(fd, 0);
151   Eclose(p[0]);
152   if (fd!=0) Eclose(fd);
153   Ewrite(p[1], ".", 1);
154 }
155
156 static inline ALWAYSINLINE void
157 doSyncStage2(int p[2], bool do_disconnect)
158 {
159   if (!do_disconnect) return;
160
161   Ewrite(p[1], "X", 1);
162 }
163
164 static void
165 waitOnSync(pid_t pid, int p[2])
166 {
167   int           c;
168   size_t        l;
169
170   Eclose(p[1]);
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);
175 }
176
177 static inline ALWAYSINLINE void
178 tellContext(xid_t ctx, bool do_it)
179 {
180   char          buf[sizeof(xid_t)*3+2];
181   size_t        l;
182
183   if (!do_it) return;
184
185   l = utilvserver_fmt_long(buf,ctx);
186
187   WRITE_MSG(2, "New security context is ");
188   write(2, buf, l);
189   WRITE_MSG(2, "\n");
190 }
191
192 static int
193 connectExternalSync(char const *filename)
194 {
195   int                   fd;
196   struct sockaddr_un    addr;
197   
198   if (filename==0) return -1;
199
200   ENSC_INIT_UNIX_SOCK(addr, filename);
201
202   fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
203   Econnect(fd, &addr, sizeof(addr));
204
205   return fd;
206 }
207
208 static void
209 doExternalSync(int fd, char const *msg)
210 {
211   char          c;
212   
213   if (fd==-1) return;
214
215   if (msg) EsendAll(fd, msg, strlen(msg));
216   Eshutdown(fd, SHUT_WR);
217
218   if (TEMP_FAILURE_RETRY(recv(fd, &c, 1, MSG_NOSIGNAL))!=0) {
219     WRITE_MSG(2, "vcontext: unexpected external synchronization event\n");
220     exit(255);
221   }
222
223   Eclose(fd);
224 }
225
226 static inline ALWAYSINLINE int
227 doit(struct Arguments const *args, char *argv[])
228 {
229   int                   p[2];
230   pid_t                 pid = initSync(p, args->do_disconnect);
231   
232   if (pid==0) {
233     xid_t       xid;
234     int         ext_sync_fd = connectExternalSync(args->sync_sock);
235     
236     if (args->do_create) {
237       xid = vc_create_context(args->xid);
238       if (xid==VC_NOCTX) {
239         if (errno==EEXIST) return 254;
240         else {
241           perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
242           return wrapper_exit_code;
243         }
244       }
245       tellContext(xid, args->verbosity>=1);
246     }
247     else
248       xid = args->xid;
249
250     if (args->do_chroot)
251       Echroot(".");
252
253     if (args->uid!=(uid_t)(-1) && getuid()!=args->uid) {
254       Esetuid(args->uid);
255       if (getuid()!=args->uid) {
256         WRITE_MSG(2, "vcontext: Something went wrong while changing the UID\n");
257         exit(255);
258       }
259     }
260
261     if (args->is_fakeinit) {
262       struct vc_ctx_flags       test_flags;
263       struct vc_ctx_flags       flags = {
264         .flagword = S_CTX_INFO_INIT,
265         .mask     = S_CTX_INFO_INIT
266       };
267       
268       Evc_get_flags(xid, &test_flags);
269       if ((flags.mask & S_CTX_INFO_INIT)==0) {
270         WRITE_MSG(2, "vcontext: context has already a fakeinit-process\n");
271         exit(255);
272       }
273
274       Evc_set_flags(xid, &flags);
275     }
276
277     if (args->do_migrate)
278       Evc_migrate_context(xid);
279
280     doExternalSync(ext_sync_fd, args->sync_msg);
281     doSyncStage1(p, args->do_disconnect);
282     execvp (argv[optind],argv+optind);
283     doSyncStage2(p, args->do_disconnect);
284
285     PERROR_Q("chcontext: execvp", argv[optind]);
286     exit(255);
287   }
288
289   waitOnSync(pid, p);
290   return EXIT_SUCCESS;
291 }
292
293 int main (int argc, char *argv[])
294 {
295   struct Arguments              args = {
296     .do_create     = false,
297     .do_migrate    = false,
298     .do_disconnect = false,
299     .is_fakeinit   = 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_FAKEINIT         :  args.is_fakeinit   = true; break;
317       case CMD_CHROOT           :  args.do_chroot     = true; break;
318       case CMD_SILENT           :  --args.verbosity;          break;
319       case CMD_XID              :  args.xid           = atol(optarg); break;
320       case CMD_UID              :  args.uid           = atol(optarg); break;
321       case CMD_SYNCSOCK         :  args.sync_sock     = optarg; break;
322       case CMD_SYNCMSG          :  args.sync_msg      = optarg; break;
323
324       default           :
325         WRITE_MSG(2, "Try '");
326         WRITE_STR(2, argv[0]);
327         WRITE_MSG(2, " --help\" for more information.\n");
328         return 255;
329         break;
330     }
331   }
332
333   if (!args.do_create && !args.do_migrate)
334     WRITE_MSG(2, "Neither '--create' nor '--migrate specified; try '--help' for more information\n");
335   else if (args.do_create  &&  args.do_migrate)
336     WRITE_MSG(2, "Can not specify '--create' and '--migrate' at the same time; try '--help' for more information\n");
337   else if (!args.do_migrate && args.is_fakeinit)
338     WRITE_MSG(2, "'--fakeinit' is possible in combination with '--migrate' only\n");
339   else if (!args.do_create && args.xid==VC_DYNAMIC_XID)
340     WRITE_MSG(2, "vcontext: Can not migrate to an unknown context\n");
341   else if (optind>=argc)
342     WRITE_MSG(2, "No command given; use '--help' for more information.\n");
343   else
344     return doit(&args, argv);
345
346   return 255;
347 }