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