a75f8cf209a7c3d99120dfe9fa0f438c6b56baad
[util-vserver.git] / util-vserver / lib / bcaps-v13.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <ensc@delenn.intern.sigma-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "vserver.h"
24 #include "internal.h"
25 #include <lib_internal/util-dimof.h>
26 #include <linux/capability.h>
27
28 #include <string.h>
29 #include <assert.h>
30
31 #ifndef CAP_QUOTACTL
32 #  define CAP_QUOTACTL  VC_CAP_QUOTACTL
33 #endif
34
35 #define DECL(VAL) { #VAL, sizeof(#VAL)-1, 1 << (CAP_ ## VAL) }
36
37 static struct Mapping_uint64 const VALUES[] = {
38   DECL(CHOWN),
39   DECL(DAC_OVERRIDE),
40   DECL(DAC_READ_SEARCH),
41   DECL(FOWNER),
42   DECL(FSETID),
43   DECL(KILL),
44   DECL(SETGID),
45   DECL(SETUID),
46   DECL(SETPCAP),
47   DECL(LINUX_IMMUTABLE),
48   DECL(NET_BIND_SERVICE),
49   DECL(NET_BROADCAST),
50   DECL(NET_ADMIN),
51   DECL(NET_RAW),
52   DECL(IPC_LOCK),
53   DECL(IPC_OWNER),
54   DECL(SYS_MODULE),
55   DECL(SYS_RAWIO),
56   DECL(SYS_CHROOT),
57   DECL(SYS_PTRACE),
58   DECL(SYS_PACCT),
59   DECL(SYS_ADMIN),
60   DECL(SYS_BOOT),
61   DECL(SYS_NICE),
62   DECL(SYS_RESOURCE),
63   DECL(SYS_TIME),
64   DECL(SYS_TTY_CONFIG),
65   DECL(MKNOD),
66   DECL(LEASE),
67   DECL(QUOTACTL),
68 };
69
70 inline static char const *
71 removePrefix(char const *str, size_t *len)
72 {
73   if ((len==0 || *len==0 || *len>=4) &&
74       strncasecmp("cap_", str, 4)==0) {
75     if (len && *len>=4) *len -= 4;
76     return str+4;
77   }
78   else
79     return str;
80 }
81
82 uint_least64_t
83 vc_text2bcap(char const *str, size_t len)
84 {
85   char const *  tmp = removePrefix(str, &len);
86   ssize_t       idx = utilvserver_value2text_uint64(tmp, len,
87                                                     VALUES, DIM_OF(VALUES));
88   if (idx==-1) return 0;
89   else         return VALUES[idx].val;
90 }
91
92 char const *
93 vc_lobcap2text(uint_least64_t *val)
94 {
95   ssize_t       idx = utilvserver_text2value_uint64(val,
96                                                     VALUES, DIM_OF(VALUES));
97
98   if (idx==-1) return 0;
99   else         return VALUES[idx].id;
100 }