3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on reducecap.cc by Jacques Gelinas
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)
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.
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.
31 #include "linuxcaps.h"
35 # define CAP_QUOTACTL 29
38 extern int capget (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
39 extern int capset (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
43 fprintf (stderr,"reducecap version %s\n",VERSION);
44 fprintf (stderr,"reducecap [ options ] command argument\n");
48 static void reducecap_print(struct __user_cap_data_struct *user)
50 static const char *tb[]={
53 "CAP_DAC_READ_SEARCH",
60 "CAP_LINUX_IMMUTABLE",
61 "CAP_NET_BIND_SERVICE",
84 printf ("%22s %9s %9s %9s\n","Capability","Effective","Permitted"
86 for (i=0; tb[i] != NULL; i++){
88 printf ("%22s %9s %9s %9s\n"
90 ,(user->effective & bit) ? "X " : " "
91 ,(user->permitted & bit) ? "X " : " "
92 ,(user->inheritable & bit) ? "X " : " ");
96 static void reducecap_show()
98 struct __user_cap_header_struct header;
99 struct __user_cap_data_struct user;
100 header.version = _LINUX_CAPABILITY_VERSION;
101 header.pid = getpid();
102 if (capget(&header,&user)==-1){
105 reducecap_print (&user);
111 int main (int argc, char *argv[])
114 unsigned long remove = 0;
117 unsigned long secure = (1<<CAP_LINUX_IMMUTABLE)
118 |(1<<CAP_NET_BROADCAST)
129 |(1<<CAP_SYS_RESOURCE)
134 for (i=1; i<argc; i++){
135 const char *arg = argv[i];
136 const char *opt = argv[i+1];
137 if (strcmp(arg,"--secure")==0){
139 }else if (strcmp(arg,"--show")==0){
141 }else if (strcmp(arg,"--flag")==0){
142 if (strcmp(opt,"lock")==0){
144 }else if (strcmp(opt,"sched")==0){
146 }else if (strcmp(opt,"nproc")==0){
148 }else if (strcmp(opt,"private")==0){
150 }else if (strcmp(opt,"hideinfo")==0){
153 fprintf (stderr,"Unknown flag %s\n",opt);
156 }else if (arg[0] == '-' && arg[1] == '-'){
161 // The following capabilities are normally available
162 // to vservers administrator, but are place for
165 {"DAC_OVERRIDE",CAP_DAC_OVERRIDE},
166 {"DAC_READ_SEARCH",CAP_DAC_READ_SEARCH},
167 {"FOWNER",CAP_FOWNER},
168 {"FSETID",CAP_FSETID},
170 {"SETGID",CAP_SETGID},
171 {"SETUID",CAP_SETUID},
172 {"SETPCAP",CAP_SETPCAP},
173 {"SYS_TTY_CONFIG",CAP_SYS_TTY_CONFIG},
175 {"SYS_CHROOT",CAP_SYS_CHROOT},
177 // Those capabilities are not normally available
178 // to vservers because they are not needed and
179 // may represent a security risk
180 {"LINUX_IMMUTABLE",CAP_LINUX_IMMUTABLE},
181 {"NET_BIND_SERVICE",CAP_NET_BIND_SERVICE},
182 {"NET_BROADCAST",CAP_NET_BROADCAST},
183 {"NET_ADMIN", CAP_NET_ADMIN},
184 {"NET_RAW", CAP_NET_RAW},
185 {"IPC_LOCK", CAP_IPC_LOCK},
186 {"IPC_OWNER", CAP_IPC_OWNER},
187 {"SYS_MODULE",CAP_SYS_MODULE},
188 {"SYS_RAWIO", CAP_SYS_RAWIO},
189 {"SYS_PACCT", CAP_SYS_PACCT},
190 {"SYS_ADMIN", CAP_SYS_ADMIN},
191 {"SYS_BOOT", CAP_SYS_BOOT},
192 {"SYS_NICE", CAP_SYS_NICE},
193 {"SYS_RESOURCE",CAP_SYS_RESOURCE},
194 {"SYS_TIME", CAP_SYS_TIME},
195 {"MKNOD", CAP_MKNOD},
196 {"QUOTACTL", CAP_QUOTACTL},
205 if (strncmp(arg, "CAP_", 4)==0) arg += 4;
206 for (j=0; tbcap[j].option != NULL; j++){
207 if (strcasecmp(tbcap[j].option,arg)==0){
208 remove |= (1<<tbcap[j].bit);
212 if (tbcap[j].option == NULL){
226 struct __user_cap_header_struct header;
227 struct __user_cap_data_struct user;
228 header.version = _LINUX_CAPABILITY_VERSION;
230 if (capget(&header,&user)==-1){
234 reducecap_print (&user);
236 if (vc_new_s_context(-2,remove,flags)==-1){
237 perror ("new_s_context -2");
239 fprintf (stderr,"Executing\n");
240 execvp (argv[i],argv+i);
241 fprintf (stderr,"Can't execute command %s\n",argv[i]);