e3ed156e4b39f32a4007d8bd6db590599369475a
[util-vserver.git] / lib / capabilities.c
1 // $Id: capabilities.c,v 1.5 2005/07/15 16:32:05 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-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
24 #include "vserver.h"
25 #include <string.h>
26
27 #if 1
28 #  define DECLARECAP(X,Y) { #X, VC_##X }
29 #else
30 #  define DECLARECAP(X,Y) { #X, Y }
31 #endif
32
33 static struct {
34     char const * const          id;
35     unsigned char               bit;
36 } const CAP2BIT[] = {
37   DECLARECAP(CAP_CHOWN,             0),
38   DECLARECAP(CAP_DAC_OVERRIDE,      1),
39   DECLARECAP(CAP_DAC_READ_SEARCH,   2),
40   DECLARECAP(CAP_FOWNER,            3),
41   DECLARECAP(CAP_FSETID,            4),
42   DECLARECAP(CAP_KILL,              5),
43   DECLARECAP(CAP_SETGID,            6),
44   DECLARECAP(CAP_SETUID,            7),
45   DECLARECAP(CAP_SETPCAP,           8),
46   DECLARECAP(CAP_LINUX_IMMUTABLE,   9),
47   DECLARECAP(CAP_NET_BIND_SERVICE, 10),
48   DECLARECAP(CAP_NET_BROADCAST,    11),
49   DECLARECAP(CAP_NET_ADMIN,        12),
50   DECLARECAP(CAP_NET_RAW,          13),
51   DECLARECAP(CAP_IPC_LOCK,         14),
52   DECLARECAP(CAP_IPC_OWNER,        15),
53   DECLARECAP(CAP_SYS_MODULE,       16),
54   DECLARECAP(CAP_SYS_RAWIO,        17),
55   DECLARECAP(CAP_SYS_CHROOT,       18),
56   DECLARECAP(CAP_SYS_PTRACE,       19),
57   DECLARECAP(CAP_SYS_PACCT,        20),
58   DECLARECAP(CAP_SYS_ADMIN,        21),
59   DECLARECAP(CAP_SYS_BOOT,         22),
60   DECLARECAP(CAP_SYS_NICE,         23),
61   DECLARECAP(CAP_SYS_RESOURCE,     24),
62   DECLARECAP(CAP_SYS_TIME,         25),
63   DECLARECAP(CAP_SYS_TTY_CONFIG,   26),
64   DECLARECAP(CAP_MKNOD,            27),
65   DECLARECAP(CAP_LEASE,            28),
66   { "CAP_QUOTACTL",                29 },
67 };
68   
69 int
70 vc_text2cap(char const *str)
71 {
72   size_t        i;
73   if (strncmp(str, "CAP_", 4)==0) str += 4;
74
75   for (i=0; i<sizeof(CAP2BIT)/sizeof(CAP2BIT[0]); ++i)
76     if (strcmp(CAP2BIT[i].id+4, str)==0) return CAP2BIT[i].bit;
77
78   return -1;
79 }
80
81 char const *
82 vc_cap2text(unsigned int bit)
83 {
84   if ((size_t)bit>=sizeof(CAP2BIT)/sizeof(CAP2BIT[0])) return 0;
85   return CAP2BIT[bit].id;
86 }