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