]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/cap-list.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / cap-list.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2822da4f
LP
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
11c3a366 21#include <errno.h>
2822da4f
LP
22#include <string.h>
23
dd1f5bd0
YW
24#include "alloc-util.h"
25#include "capability-util.h"
2822da4f 26#include "cap-list.h"
dd1f5bd0 27#include "extract-word.h"
93cc7779 28#include "macro.h"
2822da4f 29#include "missing.h"
6bedfcbb
LP
30#include "parse-util.h"
31#include "util.h"
2822da4f 32
c9f7b4d3 33static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
2822da4f 34
2822da4f 35#include "cap-from-name.h"
cf0fbc49 36#include "cap-to-name.h"
2822da4f
LP
37
38const 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
49int 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}
097df453
FB
67
68int capability_list_length(void) {
69 return (int) ELEMENTSOF(capability_names);
70}
dd1f5bd0
YW
71
72int 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
6088cefb 90 if (!GREEDY_REALLOC(str, allocated, n + add + 2))
dd1f5bd0
YW
91 return -ENOMEM;
92
93 strcpy(mempcpy(str + n, p, add), " ");
94 n += add + 1;
95 }
96
6088cefb
ZJS
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 */
dd1f5bd0
YW
101
102 *s = str;
103 str = NULL;
104
105 return 0;
106}
107
108int 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}