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