]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cap-list.c
in-addr-util: add new helper call in_addr_prefix_from_string_auto()
[thirdparty/systemd.git] / src / basic / cap-list.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <string.h>
22
23 #include "alloc-util.h"
24 #include "capability-util.h"
25 #include "cap-list.h"
26 #include "extract-word.h"
27 #include "macro.h"
28 #include "missing.h"
29 #include "parse-util.h"
30 #include "util.h"
31
32 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
33
34 #include "cap-from-name.h"
35 #include "cap-to-name.h"
36
37 const char *capability_to_name(int id) {
38
39 if (id < 0)
40 return NULL;
41
42 if (id >= (int) ELEMENTSOF(capability_names))
43 return NULL;
44
45 return capability_names[id];
46 }
47
48 int capability_from_name(const char *name) {
49 const struct capability_name *sc;
50 int r, i;
51
52 assert(name);
53
54 /* Try to parse numeric capability */
55 r = safe_atoi(name, &i);
56 if (r >= 0 && i >= 0)
57 return i;
58
59 /* Try to parse string capability */
60 sc = lookup_capability(name, strlen(name));
61 if (!sc)
62 return -EINVAL;
63
64 return sc->id;
65 }
66
67 int capability_list_length(void) {
68 return (int) ELEMENTSOF(capability_names);
69 }
70
71 int capability_set_to_string_alloc(uint64_t set, char **s) {
72 _cleanup_free_ char *str = NULL;
73 unsigned long i;
74 size_t allocated = 0, n = 0;
75
76 assert(s);
77
78 for (i = 0; i < cap_last_cap(); i++)
79 if (set & (UINT64_C(1) << i)) {
80 const char *p;
81 size_t add;
82
83 p = capability_to_name(i);
84 if (!p)
85 return -EINVAL;
86
87 add = strlen(p);
88
89 if (!GREEDY_REALLOC0(str, allocated, n + add + 2))
90 return -ENOMEM;
91
92 strcpy(mempcpy(str + n, p, add), " ");
93 n += add + 1;
94 }
95
96 if (n != 0)
97 str[n - 1] = '\0';
98
99 *s = str;
100 str = NULL;
101
102 return 0;
103 }
104
105 int capability_set_from_string(const char *s, uint64_t *set) {
106 uint64_t val = 0;
107 const char *p;
108
109 assert(set);
110
111 for (p = s;;) {
112 _cleanup_free_ char *word = NULL;
113 int r;
114
115 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
116 if (r == -ENOMEM)
117 return r;
118 if (r <= 0)
119 break;
120
121 r = capability_from_name(word);
122 if (r < 0)
123 continue;
124
125 val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;
126 }
127
128 *set = val;
129
130 return 0;
131 }