minor optimizations
[util-vserver.git] / util-vserver / src / reducecap.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on reducecap.cc by Jacques Gelinas
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; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "util.h"
25 #include "vserver.h"
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <sys/param.h>
34
35 #include "linuxcaps.h"
36
37 #define ENSC_WRAPPERS_PREFIX    "reducecap: "
38 #define ENSC_WRAPPERS_VSERVER   1
39 #define ENSC_WRAPPERS_UNISTD    1
40 #include <wrappers.h>
41
42
43 #define CMD_HELP        0x1000
44 #define CMD_VERSION     0x1001
45
46 #define CMD_SHOW        0x2000
47 #define CMD_PID         0x2001
48
49 #define CMD_CAP         0x4000
50 #define CMD_FLAG        0x4004
51 #define CMD_SECURE      0x4006
52
53 #ifdef VC_ENABLE_API_LEGACY
54 #  define CMD_OBSOLETE_CHOWN                    0x8000
55 #  define CMD_OBSOLETE_DAC_OVERRIDE             0x8001
56 #  define CMD_OBSOLETE_DAC_READ_SEARCH          0x8002
57 #  define CMD_OBSOLETE_FOWNER                   0x8003
58 #  define CMD_OBSOLETE_FSETID                   0x8004
59 #  define CMD_OBSOLETE_KILL                     0x8005
60 #  define CMD_OBSOLETE_SETGID                   0x8006
61 #  define CMD_OBSOLETE_SETUID                   0x8007
62 #  define CMD_OBSOLETE_SETPCAP                  0x8008
63 #  define CMD_OBSOLETE_SYS_TTY_CONFIG           0x8009
64 #  define CMD_OBSOLETE_LEASE                    0x800a
65 #  define CMD_OBSOLETE_SYS_CHROOT               0x800b
66 #  define CMD_OBSOLETE_X_LINUX_IMMUTABLE        0x800c
67 #  define CMD_OBSOLETE_X_NET_BIND_SERVICE       0x800d
68 #  define CMD_OBSOLETE_X_NET_BROADCAST          0x800e
69 #  define CMD_OBSOLETE_X_NET_ADMIN              0x800f
70 #  define CMD_OBSOLETE_X_NET_RAW                0x8010
71 #  define CMD_OBSOLETE_X_IPC_LOCK               0x8011
72 #  define CMD_OBSOLETE_X_IPC_OWNER              0x8012
73 #  define CMD_OBSOLETE_X_SYS_MODULE             0x8013
74 #  define CMD_OBSOLETE_X_SYS_RAWIO              0x8014
75 #  define CMD_OBSOLETE_X_SYS_PACCT              0x8015
76 #  define CMD_OBSOLETE_X_SYS_ADMIN              0x8016
77 #  define CMD_OBSOLETE_X_SYS_BOOT               0x8017
78 #  define CMD_OBSOLETE_X_SYS_NICE               0x8018
79 #  define CMD_OBSOLETE_X_SYS_RESOURCE           0x8019
80 #  define CMD_OBSOLETE_X_SYS_TIME               0x801a
81 #  define CMD_OBSOLETE_X_MKNOD                  0x801b
82 #  define CMD_OBSOLETE_X_QUOTACTL               0x801c
83
84 static char const * const       OBSOLETE_MAPPING[] = {
85   // 0                  1                  2                  3
86   "CHOWN",           "DAC_OVERRIDE",     "DAC_READ_SEARCH", "FOWNER",
87   "FSETID",          "KILL",             "SETGID",          "SETUID",
88   "SETPCAP",         "SYS_TTY_CONFIG",   "LEASE",           "SYS_CHROOT",
89   "LINUX_IMMUTABLE", "NET_BIND_SERVICE", "NET_BROADCAST",   "NET_ADMIN",
90   "NET_RAW",         "IPC_LOCK",         "IPC_OWNER",       "SYS_MODULE",
91   "SYS_RAWIO",       "SYS_PACCT",        "SYS_ADMIN",       "SYS_BOOT",
92   "SYS_NICE",        "SYS_RESOURCE",     "SYS_TIME",        "MKNOD",
93   "QUOTACTL" };
94 #endif
95
96 struct option const
97 CMDLINE_OPTIONS[] = {
98   { "help",     no_argument,  0, CMD_HELP },
99   { "version",  no_argument,  0, CMD_VERSION },
100   { "cap",        required_argument,  0, CMD_CAP },
101   { "flag",       required_argument,  0, CMD_FLAG },
102   { "secure",     no_argument,        0, CMD_SECURE },
103   { "show",       no_argument,        0, CMD_SHOW },
104   { "pid",        required_argument,  0, CMD_PID },
105 #ifdef VC_ENABLE_API_LEGACY
106   { "CAP_CHOWN",              no_argument,  0, CMD_OBSOLETE_CHOWN },
107   { "CAP_DAC_OVERRIDE",       no_argument,  0, CMD_OBSOLETE_DAC_OVERRIDE },
108   { "CAP_DAC_READ_SEARCH",    no_argument,  0, CMD_OBSOLETE_DAC_READ_SEARCH },
109   { "CAP_FOWNER",             no_argument,  0, CMD_OBSOLETE_FOWNER },
110   { "CAP_FSETID",             no_argument,  0, CMD_OBSOLETE_FSETID },
111   { "CAP_KILL",               no_argument,  0, CMD_OBSOLETE_KILL },
112   { "CAP_SETGID",             no_argument,  0, CMD_OBSOLETE_SETGID },
113   { "CAP_SETUID",             no_argument,  0, CMD_OBSOLETE_SETUID },
114   { "CAP_SETPCAP",            no_argument,  0, CMD_OBSOLETE_SETPCAP },
115   { "CAP_SYS_TTY_CONFIG",     no_argument,  0, CMD_OBSOLETE_SYS_TTY_CONFIG },
116   { "CAP_LEASE",              no_argument,  0, CMD_OBSOLETE_LEASE },
117   { "CAP_SYS_CHROOT",         no_argument,  0, CMD_OBSOLETE_SYS_CHROOT },
118   { "--CAP_LINUX_IMMUTABLE",  no_argument,  0, CMD_OBSOLETE_X_LINUX_IMMUTABLE },
119   { "--CAP_NET_BIND_SERVICE", no_argument,  0, CMD_OBSOLETE_X_NET_BIND_SERVICE },
120   { "--CAP_NET_BROADCAST",    no_argument,  0, CMD_OBSOLETE_X_NET_BROADCAST },
121   { "--CAP_NET_ADMIN",        no_argument,  0, CMD_OBSOLETE_X_NET_ADMIN },
122   { "--CAP_NET_RAW",          no_argument,  0, CMD_OBSOLETE_X_NET_RAW },
123   { "--CAP_IPC_LOCK",         no_argument,  0, CMD_OBSOLETE_X_IPC_LOCK },
124   { "--CAP_IPC_OWNER",        no_argument,  0, CMD_OBSOLETE_X_IPC_OWNER },
125   { "--CAP_SYS_MODULE",       no_argument,  0, CMD_OBSOLETE_X_SYS_MODULE },
126   { "--CAP_SYS_RAWIO",        no_argument,  0, CMD_OBSOLETE_X_SYS_RAWIO },
127   { "--CAP_SYS_PACCT",        no_argument,  0, CMD_OBSOLETE_X_SYS_PACCT },
128   { "--CAP_SYS_ADMIN",        no_argument,  0, CMD_OBSOLETE_X_SYS_ADMIN },
129   { "--CAP_SYS_BOOT",         no_argument,  0, CMD_OBSOLETE_X_SYS_BOOT },
130   { "--CAP_SYS_NICE",         no_argument,  0, CMD_OBSOLETE_X_SYS_NICE },
131   { "--CAP_SYS_RESOURCE",     no_argument,  0, CMD_OBSOLETE_X_SYS_RESOURCE },
132   { "--CAP_SYS_TIME",         no_argument,  0, CMD_OBSOLETE_X_SYS_TIME },
133   { "--CAP_MKNOD",            no_argument,  0, CMD_OBSOLETE_X_MKNOD },
134   { "--CAP_QUOTACTL",         no_argument,  0, CMD_OBSOLETE_X_QUOTACTL }, 
135 #endif
136   { 0,0,0,0 }
137 };
138
139 int wrapper_exit_code   = 255;
140
141 extern int capget (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
142 extern int capset (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
143
144 static void
145 showHelp(int fd, char const *cmd, int res)
146 {
147   WRITE_MSG(fd, "Usage:\n  ");
148   WRITE_STR(fd, cmd);
149   WRITE_MSG(fd,
150             " [--show] [--secure] [--flag <flag>] [--cap <capability>] [--] <cmd> <args>*\n  ");
151   WRITE_STR(fd, cmd);
152   WRITE_MSG(fd,
153             " --show [--pid <pid>]\n\n"
154             "Please report bugs to " PACKAGE_BUGREPORT "\n");
155
156   exit(res);
157 }
158
159 static void
160 showVersion()
161 {
162   WRITE_MSG(1,
163             "reducecap " VERSION " -- starts programs with reduced capabilities\n"
164             "This program is part of " PACKAGE_STRING "\n\n"
165             "Copyright (C) 2003,2004 Enrico Scholz\n"
166             VERSION_COPYRIGHT_DISCLAIMER);
167   exit(0);
168 }
169
170 static void
171 printReducecap(struct __user_cap_data_struct *user)
172 {
173   int i;
174   WRITE_MSG(1, "            Capability Effective  Permitted  Inheritable\n");
175
176   for (i=0;; ++i) {
177     size_t const        len  = 23 + 10*2 + 4+2;
178     char const *        text = vc_cap2text(i);
179     int                 bit  = 1<<i;
180     size_t              l;
181     char                buf[len];
182     if (text==0) break;
183
184     memset(buf, ' ', sizeof buf);
185     buf[len-1] = '\n';
186     l = MIN(strlen(text), 22);
187     memcpy(buf, text, l);
188     buf[23 + 10*0 + 4] = (user->effective   & bit) ? 'X' : ' ';
189     buf[23 + 10*1 + 4] = (user->permitted   & bit) ? 'X' : ' ';
190     buf[23 + 10*2 + 4] = (user->inheritable & bit) ? 'X' : ' ';
191     write(1, buf, len);
192   }
193 }
194
195 static void
196 show(pid_t pid)
197 {
198   struct __user_cap_header_struct header;
199   struct __user_cap_data_struct user;
200   header.version = _LINUX_CAPABILITY_VERSION;
201   header.pid     = pid;
202   if (capget(&header,&user)==-1){
203     perror ("reducecap: capget()");
204     exit(wrapper_exit_code);
205   }
206   
207   printReducecap(&user);
208 }
209
210 static uint32_t
211 getCap(char const *cap)
212 {
213   int           bit = vc_text2cap(cap);
214   if (bit!=0) {
215     WRITE_MSG(2, "Unknown capability '");
216     WRITE_STR(2, optarg);
217     WRITE_MSG(2, "'; try '--help' for more information\n");
218     exit(wrapper_exit_code);
219   }
220
221   return (1<<bit);
222 }
223
224 int main (int argc, char *argv[])
225 {
226   uint32_t              remove  = 0;
227   bool                  do_show = false;
228   uint32_t              flags   = 0;
229   pid_t                 pid     = 0;
230 #ifdef VC_ENABLE_API_LEGACY
231   bool                  show_obsolete_warning = true;
232 #endif    
233
234   while (1) {
235     int         c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
236     if (c==-1) break;
237
238 #ifdef VC_ENABLE_API_LEGACY
239     if (c>=CMD_OBSOLETE_CHOWN && c<=CMD_OBSOLETE_X_QUOTACTL) {
240       if (show_obsolete_warning) {
241         WRITE_MSG(2, "reducecap: warning, obsolete CLI used\n");
242         show_obsolete_warning = false;
243       }
244
245       remove = getCap(OBSOLETE_MAPPING[c-CMD_OBSOLETE_CHOWN]);
246       continue;
247     }
248 #endif    
249     switch (c) {
250       case CMD_HELP             :  showHelp(1, argv[0], 0);
251       case CMD_VERSION          :  showVersion();
252       case CMD_SECURE           :  remove  = vc_get_insecurebcaps(); break;
253       case CMD_SHOW             :  do_show = true;  break; 
254       case CMD_PID              :  pid     = atoi(optarg);   break;
255       case CMD_CAP              :  remove  = getCap(optarg); break;
256       case CMD_FLAG             : {
257         struct vc_err_listparser        err;
258         
259         flags = vc_list2cflag_compat(optarg, 0, &err);
260         if (err.ptr!=0) {
261           WRITE_MSG(2, "Unknown flag '");
262           write(2, err.ptr, err.len);
263           WRITE_MSG(2, "'\n");
264           exit(wrapper_exit_code);
265         }
266         break;
267       }
268     }
269   }
270
271   if (!do_show && optind==argc) {
272     WRITE_MSG(2, "No command given; use '--help' for more information\n");
273     exit(wrapper_exit_code);
274   }
275
276   if (!do_show && pid!=0) {
277     WRITE_MSG(2, "A pid can be specified in '--show' mode only; use '--help' for more information\n");
278     exit(wrapper_exit_code);
279   }  
280
281   if (do_show && optind==argc)
282     show(pid);
283   else {
284     Evc_new_s_context(VC_SAMECTX, remove, flags);
285     if (do_show) show(pid);
286
287     WRITE_MSG(2, "Executing\n");
288     Eexecvp(argv[optind], argv+optind);
289   }
290
291   return EXIT_SUCCESS;
292 }