1 /* lookup_table.c --
2 * Copyright 2009, 2013 Red Hat Inc.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; see the file COPYING.LIB. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1335, USA.
19 *
20 * Authors:
21 * Steve Grubb <[email protected]>
22 */
23
24 #include "config.h"
25 #include <stddef.h>
26 #include <linux/capability.h>
27 #include <strings.h>
28 #include <stdio.h>
29 #include <stdlib.h> // free
30
31
32 #define hidden __attribute__ ((visibility ("hidden")))
33 extern unsigned int last_cap hidden;
34
35 #undef cap_valid
36 #define cap_valid(x) ((x) <= last_cap)
37
38
39 struct transtab {
40 int value;
41 int offset;
42 };
43
44 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
45 #define MSGSTRFIELD1(line) str##line
46
47
48 /* To create the following tables in a DSO-friendly way we split them in
49 two separate variables: a long string which is created by concatenating
50 all strings referenced in the table and the table itself, which uses
51 offsets instead of string pointers. To do this without increasing
52 the maintenance burden we use a lot of preprocessor magic. All the
53 maintainer has to do is to add a new entry to the included file and
54 recompile. */
55
56 static const union captab_msgstr_t {
57 struct {
58 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof (s)];
59 #include "captab.h"
60 #undef _S
61 };
62 char str[0];
63 } captab_msgstr = { {
64 #define _S(n, s) s,
65 #include "captab.h"
66 #undef _S
67 } };
68 static const struct transtab captab[] = {
69 #define _S(n, s) { n, offsetof(union captab_msgstr_t, \
70 MSGSTRFIELD(__LINE__)) },
71 #include "captab.h"
72 #undef _S
73 };
74 #define CAP_NG_CAPABILITY_NAMES (sizeof(captab)/sizeof(captab[0]))
75
76
77
78
capng_lookup_name(const struct transtab * table,const char * tabstr,size_t length,const char * name)79 static int capng_lookup_name(const struct transtab *table,
80 const char *tabstr, size_t length, const char *name)
81 {
82 size_t i;
83
84 for (i = 0; i < length; i++) {
85 if (!strcasecmp(tabstr + table[i].offset, name))
86 return table[i].value;
87 }
88 return -1;
89 }
90
capng_lookup_number(const struct transtab * table,const char * tabstr,size_t length,int number)91 static const char *capng_lookup_number(const struct transtab *table,
92 const char *tabstr, size_t length,
93 int number)
94 {
95 size_t i;
96
97 for (i = 0; i < length; i++) {
98 if (table[i].value == number)
99 return tabstr + table[i].offset;
100 }
101 return NULL;
102 }
103
capng_name_to_capability(const char * name)104 int capng_name_to_capability(const char *name)
105 {
106 return capng_lookup_name(captab, captab_msgstr.str,
107 CAP_NG_CAPABILITY_NAMES, name);
108 }
109
110 static char *ptr2 = NULL;
capng_capability_to_name(unsigned int capability)111 const char *capng_capability_to_name(unsigned int capability)
112 {
113 const char *ptr;
114
115 if (!cap_valid(capability))
116 return NULL;
117
118 ptr = capng_lookup_number(captab, captab_msgstr.str,
119 CAP_NG_CAPABILITY_NAMES, capability);
120 if (ptr == NULL) { // This leaks memory, but should almost never be used
121 free(ptr2);
122 if (asprintf(&ptr2, "cap_%u", capability) < 0)
123 ptr = NULL;
124 else
125 ptr = ptr2;
126 }
127 return ptr;
128 }
129
130