e06bf6002ba1ab49e4663743fc393b0eff601a20
[util-vserver.git] / src / vattribute.c
1 // $Id: vattribute.c,v 1.14 2005/03/24 12:44:17 ensc Exp $    --*- 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/vserver.h>
25
26 #include <getopt.h>
27 #include <stdint.h>
28 #include <errno.h>
29
30 #define ENSC_WRAPPERS_PREFIX    "vattribute: "
31 #define ENSC_WRAPPERS_VSERVER   1
32 #define ENSC_WRAPPERS_UNISTD    1
33 #include <wrappers.h>
34
35 #define CMD_HELP                0x1000
36 #define CMD_VERSION             0x1001
37 #define CMD_XID                 0x2000
38 #define CMD_SET                 0x2001
39 #define CMD_CCAP                0x2002
40 #define CMD_FLAG                0x2003
41 #define CMD_SECURE              0x2004
42 #define CMD_BCAP                0x2005
43
44 int                     wrapper_exit_code = 1;
45
46 struct option const
47 CMDLINE_OPTIONS[] = {
48   { "help",       no_argument,       0, CMD_HELP },
49   { "version",    no_argument,       0, CMD_VERSION },
50   { "xid",        required_argument, 0, CMD_XID },
51   { "set",        no_argument,       0, CMD_SET },
52   { "ccap",       required_argument, 0, CMD_CCAP },
53   { "bcap",       required_argument, 0, CMD_BCAP },
54   { "flag",       required_argument, 0, CMD_FLAG },
55   { "secure",     no_argument,       0, CMD_SECURE },
56   {0,0,0,0}
57 };
58
59 struct Arguments {
60     xid_t               xid;
61     struct vc_ctx_flags flags;
62     struct vc_ctx_caps  caps;
63 };
64
65 static void
66 showHelp(int fd, char const *cmd, int res)
67 {
68   WRITE_MSG(fd, "Usage:\n    ");
69   WRITE_STR(fd, cmd);
70   WRITE_MSG(fd,
71             " --set [--xid <xid>] [--bcap [~!]<cap>] [--ccap [~!]<cap>] [--flag [~!]<flag>] [--secure] -- [<program> <args>*]\n"
72             "\n"
73             " --bcap <cap>   ...  system  capability to be added\n"
74             " --cap  <cap>   ...  context capability to be added\n"
75             "\n"
76             "Please report bugs to " PACKAGE_BUGREPORT "\n");
77
78   exit(res);
79 }
80
81 static void
82 showVersion()
83 {
84   WRITE_MSG(1,
85             "vattribute " VERSION " -- sets attributes of vservers\n"
86             "This program is part of " PACKAGE_STRING "\n\n"
87             "Copyright (C) 2004 Enrico Scholz\n"
88             VERSION_COPYRIGHT_DISCLAIMER);
89   exit(0);
90 }
91
92 static void
93 parseFlags(char const *str, struct vc_ctx_flags *flags)
94 {
95   struct vc_err_listparser      err;
96   int                           rc;
97
98   rc = vc_list2cflag(str,0, &err, flags);
99   
100   if (rc==-1) {
101     WRITE_MSG(2, "Unknown flag '");
102     Vwrite(2, err.ptr, err.len);
103     WRITE_MSG(2, "'\n");
104     exit(wrapper_exit_code);
105   }
106 }
107
108 static void
109 parseBCaps(char const *str, struct vc_ctx_caps *caps)
110 {
111   struct vc_err_listparser      err;
112   int                           rc;
113
114   rc = vc_list2bcap(str,0, &err, caps);
115   
116   if (rc==-1) {
117     WRITE_MSG(2, "Unknown bcap '");
118     Vwrite(2, err.ptr, err.len);
119     WRITE_MSG(2, "'\n");
120     exit(wrapper_exit_code);
121   }
122 }
123
124 static void
125 parseCCaps(char const *str, struct vc_ctx_caps *caps)
126 {
127   struct vc_err_listparser      err;
128   int                           rc;
129
130   rc = vc_list2ccap(str,0, &err, caps);
131   
132   if (rc==-1) {
133     WRITE_MSG(2, "Unknown ccap '");
134     Vwrite(2, err.ptr, err.len);
135     WRITE_MSG(2, "'\n");
136     exit(wrapper_exit_code);
137   }
138 }
139
140 static void
141 parseSecure(struct vc_ctx_flags UNUSED * flags,
142             struct vc_ctx_caps  UNUSED * caps)
143 {
144   caps->ccaps = ~vc_get_insecureccaps();
145   caps->cmask = ~0ull;
146   caps->bcaps = ~vc_get_insecurebcaps();
147   caps->bmask = ~0ull;
148
149     // TODO: generalize this
150   flags->flagword = VC_VXF_HIDE_NETIF;
151   flags->mask     = VC_VXF_HIDE_NETIF;
152 }
153
154 int main(int argc, char *argv[])
155 {
156   struct Arguments              args = {
157     .xid   = VC_NOCTX,
158     .flags = { .flagword = 0, .mask = 0 },
159     .caps  = { .bcaps = 0, .bmask = 0,.ccaps = 0, .cmask = 0 },
160   };
161   
162   while (1) {
163     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
164     if (c==-1) break;
165
166     switch (c) {
167       case CMD_HELP     :  showHelp(1, argv[0], 0);
168       case CMD_VERSION  :  showVersion();
169       case CMD_SET      :  break; // default op currently
170       case CMD_XID      :  args.xid = Evc_xidopt2xid(optarg,true); break;
171       case CMD_FLAG     :  parseFlags(optarg, &args.flags);        break;
172       case CMD_CCAP     :  parseCCaps(optarg, &args.caps);         break;
173       case CMD_BCAP     :  parseBCaps(optarg, &args.caps);         break;
174       case CMD_SECURE   :  parseSecure(&args.flags, &args.caps);   break;
175       default           :
176         WRITE_MSG(2, "Try '");
177         WRITE_STR(2, argv[0]);
178         WRITE_MSG(2, " --help\" for more information.\n");
179         return 255;
180         break;
181     }
182   }
183
184   if (args.xid==VC_NOCTX) args.xid = Evc_get_task_xid(0);
185
186   if ((args.caps.cmask || args.caps.bmask) &&
187       vc_set_ccaps(args.xid, &args.caps)==-1)
188     perror(ENSC_WRAPPERS_PREFIX "vc_set_ccaps()");
189   else if (args.flags.mask &&
190            vc_set_cflags(args.xid, &args.flags)==-1)
191     perror(ENSC_WRAPPERS_PREFIX "vc_set_flags()");
192   else if (optind<argc)
193     EexecvpD(argv[optind], argv+optind);
194   else
195     return EXIT_SUCCESS;
196
197   return EXIT_FAILURE;
198 }