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
164 {"CAP_CHOWN",CAP_CHOWN},
165 {"CAP_DAC_OVERRIDE",CAP_DAC_OVERRIDE},
166 {"CAP_DAC_READ_SEARCH",CAP_DAC_READ_SEARCH},
167 {"CAP_FOWNER",CAP_FOWNER},
168 {"CAP_FSETID",CAP_FSETID},
169 {"CAP_KILL",CAP_KILL},
170 {"CAP_SETGID",CAP_SETGID},
171 {"CAP_SETUID",CAP_SETUID},
172 {"CAP_SETPCAP",CAP_SETPCAP},
173 {"CAP_SYS_TTY_CONFIG",CAP_SYS_TTY_CONFIG},
174 {"CAP_LEASE",CAP_LEASE},
175 {"CAP_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},
200 for (j=0; tbcap[j].option != NULL; j++){
201 if (strcasecmp(tbcap[j].option,arg)==0){
202 remove |= (1<<tbcap[j].bit);
206 if (tbcap[j].option != NULL){
219 }else if (argv[i][0] == '-'){
222 struct __user_cap_header_struct header;
223 struct __user_cap_data_struct user;
224 header.version = _LINUX_CAPABILITY_VERSION;
226 if (capget(&header,&user)==-1){
230 reducecap_print (&user);
232 if (vc_new_s_context(-2,remove,flags)==-1){
233 perror ("new_s_context -2");
235 fprintf (stderr,"Executing\n");
236 execvp (argv[i],argv+i);
237 fprintf (stderr,"Can't execute command %s\n",argv[i]);