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.
30 #include "linuxcaps.h"
33 extern int capget (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
34 extern int capset (struct __user_cap_header_struct *, struct __user_cap_data_struct *);
38 fprintf (stderr,"reducecap version %s\n",VERSION);
39 fprintf (stderr,"reducecap [ options ] command argument\n");
43 static void reducecap_print(struct __user_cap_data_struct *user)
45 static const char *tb[]={
48 "CAP_DAC_READ_SEARCH",
55 "CAP_LINUX_IMMUTABLE",
56 "CAP_NET_BIND_SERVICE",
79 printf ("%22s %9s %9s %9s\n","Capability","Effective","Permitted"
81 for (i=0; tb[i] != NULL; i++){
83 printf ("%22s %9s %9s %9s\n"
85 ,(user->effective & bit) ? "X " : " "
86 ,(user->permitted & bit) ? "X " : " "
87 ,(user->inheritable & bit) ? "X " : " ");
91 static void reducecap_show()
93 struct __user_cap_header_struct header;
94 struct __user_cap_data_struct user;
95 header.version = _LINUX_CAPABILITY_VERSION;
96 header.pid = getpid();
97 if (capget(&header,&user)==-1){
100 reducecap_print (&user);
106 int main (int argc, char *argv[])
109 unsigned long remove = 0;
112 unsigned long secure = (1<<CAP_LINUX_IMMUTABLE)
113 |(1<<CAP_NET_BROADCAST)
124 |(1<<CAP_SYS_RESOURCE)
128 for (i=1; i<argc; i++){
129 const char *arg = argv[i];
130 const char *opt = argv[i+1];
131 if (strcmp(arg,"--secure")==0){
133 }else if (strcmp(arg,"--show")==0){
135 }else if (strcmp(arg,"--flag")==0){
136 if (strcmp(opt,"lock")==0){
138 }else if (strcmp(opt,"sched")==0){
140 }else if (strcmp(opt,"nproc")==0){
142 }else if (strcmp(opt,"private")==0){
144 }else if (strcmp(opt,"hideinfo")==0){
147 fprintf (stderr,"Unknown flag %s\n",opt);
150 }else if (arg[0] == '-' && arg[1] == '-'){
155 // The following capabilities are normally available
156 // to vservers administrator, but are place for
158 {"CAP_CHOWN",CAP_CHOWN},
159 {"CAP_DAC_OVERRIDE",CAP_DAC_OVERRIDE},
160 {"CAP_DAC_READ_SEARCH",CAP_DAC_READ_SEARCH},
161 {"CAP_FOWNER",CAP_FOWNER},
162 {"CAP_FSETID",CAP_FSETID},
163 {"CAP_KILL",CAP_KILL},
164 {"CAP_SETGID",CAP_SETGID},
165 {"CAP_SETUID",CAP_SETUID},
166 {"CAP_SETPCAP",CAP_SETPCAP},
167 {"CAP_SYS_TTY_CONFIG",CAP_SYS_TTY_CONFIG},
168 {"CAP_LEASE",CAP_LEASE},
169 {"CAP_SYS_CHROOT",CAP_SYS_CHROOT},
171 // Those capabilities are not normally available
172 // to vservers because they are not needed and
173 // may represent a security risk
174 {"--LINUX_IMMUTABLE",CAP_LINUX_IMMUTABLE},
175 {"--NET_BIND_SERVICE",CAP_NET_BIND_SERVICE},
176 {"--NET_BROADCAST",CAP_NET_BROADCAST},
177 {"--NET_ADMIN", CAP_NET_ADMIN},
178 {"--NET_RAW", CAP_NET_RAW},
179 {"--IPC_LOCK", CAP_IPC_LOCK},
180 {"--IPC_OWNER", CAP_IPC_OWNER},
181 {"--SYS_MODULE",CAP_SYS_MODULE},
182 {"--SYS_RAWIO", CAP_SYS_RAWIO},
183 {"--SYS_PACCT", CAP_SYS_PACCT},
184 {"--SYS_ADMIN", CAP_SYS_ADMIN},
185 {"--SYS_BOOT", CAP_SYS_BOOT},
186 {"--SYS_NICE", CAP_SYS_NICE},
187 {"--SYS_RESOURCE",CAP_SYS_RESOURCE},
188 {"--SYS_TIME", CAP_SYS_TIME},
189 {"--MKNOD", CAP_MKNOD},
193 for (j=0; tbcap[j].option != NULL; j++){
194 if (strcasecmp(tbcap[j].option,arg)==0){
195 remove |= (1<<tbcap[j].bit);
199 if (tbcap[j].option != NULL){
212 }else if (argv[i][0] == '-'){
215 struct __user_cap_header_struct header;
216 struct __user_cap_data_struct user;
217 header.version = _LINUX_CAPABILITY_VERSION;
219 if (capget(&header,&user)==-1){
223 reducecap_print (&user);
225 if (call_new_s_context(0,NULL,remove,flags)==-1){
226 perror ("new_s_context -2");
228 fprintf (stderr,"Executing\n");
229 execvp (argv[i],argv+i);
230 fprintf (stderr,"Can't execute command %s\n",argv[i]);