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