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