merge with 0.30.213
[util-vserver.git] / src / nattribute.c
1 // $Id: nattribute.c 2403 2006-11-24 23:06:08Z dhozac $    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // Copyright (C) 2006 Daniel Hokka Zakrisson
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; version 2 of the License.
9 //  
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //  
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "util.h"
25 #include <lib/vserver.h>
26
27 #include <getopt.h>
28 #include <stdint.h>
29 #include <errno.h>
30
31 #define ENSC_WRAPPERS_PREFIX    "nattribute: "
32 #define ENSC_WRAPPERS_VSERVER   1
33 #define ENSC_WRAPPERS_UNISTD    1
34 #include <wrappers.h>
35
36 #define CMD_HELP                0x1000
37 #define CMD_VERSION             0x1001
38 #define CMD_NID                 0x2000
39 #define CMD_SET                 0x2001
40 #define CMD_CAP                 0x2002
41 #define CMD_FLAG                0x2003
42 #define CMD_SECURE              0x2004
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   { "nid",        required_argument, 0, CMD_NID },
51   { "set",        no_argument,       0, CMD_SET },
52   { "ncap",       required_argument, 0, CMD_CAP },
53   { "flag",       required_argument, 0, CMD_FLAG },
54   { "secure",     no_argument,       0, CMD_SECURE },
55   {0,0,0,0}
56 };
57
58 struct Arguments {
59     nid_t               nid;
60     struct vc_net_flags flags;
61     struct vc_net_caps  caps;
62 };
63
64 static void
65 showHelp(int fd, char const *cmd, int res)
66 {
67   WRITE_MSG(fd, "Usage:\n    ");
68   WRITE_STR(fd, cmd);
69   WRITE_MSG(fd,
70             " --set [--nid <nid>] [--ncap [~!]<ncap>] [--flag [~!]<flag>] [--secure] -- [<program> <args>*]\n"
71             "\n"
72             " --ncap <cap>   ...  network capability to be added\n"
73             " --flag <flag>  ...  network flag to be added\n"
74             "\n"
75             "Please report bugs to " PACKAGE_BUGREPORT "\n");
76
77   exit(res);
78 }
79
80 static void
81 showVersion()
82 {
83   WRITE_MSG(1,
84             "nattribute " VERSION " -- sets attributes of network contexts\n"
85             "This program is part of " PACKAGE_STRING "\n\n"
86             "Copyright (C) 2004 Enrico Scholz\n"
87             "Copyright (C) 2006 Daniel Hokka Zakrisson\n"
88             VERSION_COPYRIGHT_DISCLAIMER);
89   exit(0);
90 }
91
92 static void
93 parseFlags(char const *str, struct vc_net_flags *flags)
94 {
95   struct vc_err_listparser      err;
96   int                           rc;
97
98   rc = vc_list2nflag(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 parseNCaps(char const *str, struct vc_net_caps *caps)
110 {
111   struct vc_err_listparser      err;
112   int                           rc;
113
114   rc = vc_list2ncap(str,0, &err, caps);
115   
116   if (rc==-1) {
117     WRITE_MSG(2, "Unknown ncap '");
118     Vwrite(2, err.ptr, err.len);
119     WRITE_MSG(2, "'\n");
120     exit(wrapper_exit_code);
121   }
122 }
123
124 static void
125 parseSecure(struct vc_net_flags * flags,
126             struct vc_net_caps  * caps)
127 {
128     // TODO: generalize this
129   caps->ncaps = 0ull;
130   caps->cmask = 0ull;
131
132   flags->flagword = VC_NXF_HIDE_NETIF;
133   flags->mask     = VC_NXF_HIDE_NETIF;
134 }
135
136 int main(int argc, char *argv[])
137 {
138   struct Arguments              args = {
139     .nid   = VC_NOCTX,
140     .flags = { .flagword = 0, .mask = 0 },
141     .caps  = { .ncaps = 0, .cmask = 0 },
142   };
143   
144   while (1) {
145     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
146     if (c==-1) break;
147
148     switch (c) {
149       case CMD_HELP     :  showHelp(1, argv[0], 0);
150       case CMD_VERSION  :  showVersion();
151       case CMD_SET      :  break; // default op currently
152       case CMD_NID      :  args.nid = Evc_nidopt2nid(optarg,true); break;
153       case CMD_FLAG     :  parseFlags(optarg, &args.flags);        break;
154       case CMD_CAP      :  parseNCaps(optarg, &args.caps);         break;
155       case CMD_SECURE   :  parseSecure(&args.flags, &args.caps);   break;
156       default           :
157         WRITE_MSG(2, "Try '");
158         WRITE_STR(2, argv[0]);
159         WRITE_MSG(2, " --help' for more information.\n");
160         return 255;
161         break;
162     }
163   }
164
165   if (args.nid==VC_NOCTX) args.nid = Evc_get_task_nid(0);
166
167   if (args.caps.cmask &&
168       vc_set_ncaps(args.nid, &args.caps)==-1)
169     perror(ENSC_WRAPPERS_PREFIX "vc_set_ncaps()");
170   else if (args.flags.mask &&
171            vc_set_nflags(args.nid, &args.flags)==-1)
172     perror(ENSC_WRAPPERS_PREFIX "vc_set_nflags()");
173   else if (optind<argc)
174     EexecvpD(argv[optind], argv+optind);
175   else
176     return EXIT_SUCCESS;
177
178   return EXIT_FAILURE;
179 }