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