2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-12-10 03:16:14 +01:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <errno.h>
|
2014-12-10 03:16:14 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-08-07 16:25:11 +02:00
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "capability-util.h"
|
2014-12-10 03:16:14 +01:00
|
|
|
#include "cap-list.h"
|
2017-08-07 16:25:11 +02:00
|
|
|
#include "extract-word.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "macro.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2020-07-09 23:15:47 +02:00
|
|
|
#include "stdio-util.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "util.h"
|
2014-12-10 03:16:14 +01:00
|
|
|
|
2017-01-10 08:39:05 +01:00
|
|
|
static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
|
2014-12-10 03:16:14 +01:00
|
|
|
|
|
|
|
#include "cap-from-name.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "cap-to-name.h"
|
2014-12-10 03:16:14 +01:00
|
|
|
|
|
|
|
const char *capability_to_name(int id) {
|
|
|
|
if (id < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2018-12-02 05:58:18 +01:00
|
|
|
if ((size_t) id >= ELEMENTSOF(capability_names))
|
2014-12-10 03:16:14 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return capability_names[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
int capability_from_name(const char *name) {
|
|
|
|
const struct capability_name *sc;
|
|
|
|
int r, i;
|
|
|
|
|
|
|
|
assert(name);
|
|
|
|
|
|
|
|
/* Try to parse numeric capability */
|
|
|
|
r = safe_atoi(name, &i);
|
2017-11-28 14:06:34 +01:00
|
|
|
if (r >= 0) {
|
2020-07-09 23:15:47 +02:00
|
|
|
if (i >= 0 && i < 64)
|
2017-11-28 14:06:34 +01:00
|
|
|
return i;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-12-10 03:16:14 +01:00
|
|
|
|
|
|
|
/* Try to parse string capability */
|
|
|
|
sc = lookup_capability(name, strlen(name));
|
|
|
|
if (!sc)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return sc->id;
|
|
|
|
}
|
2014-12-23 22:51:40 +01:00
|
|
|
|
2020-11-25 21:22:24 +01:00
|
|
|
/* This is the number of capability names we are *compiled* with.
|
|
|
|
* For the max capability number of the currently-running kernel,
|
|
|
|
* use cap_last_cap(). */
|
2014-12-23 22:51:40 +01:00
|
|
|
int capability_list_length(void) {
|
|
|
|
return (int) ELEMENTSOF(capability_names);
|
|
|
|
}
|
2017-08-07 16:25:11 +02:00
|
|
|
|
|
|
|
int capability_set_to_string_alloc(uint64_t set, char **s) {
|
|
|
|
_cleanup_free_ char *str = NULL;
|
|
|
|
size_t allocated = 0, n = 0;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
2020-07-10 16:53:51 +02:00
|
|
|
for (unsigned i = 0; i <= cap_last_cap(); i++)
|
2017-08-07 16:25:11 +02:00
|
|
|
if (set & (UINT64_C(1) << i)) {
|
|
|
|
const char *p;
|
2020-07-09 23:15:47 +02:00
|
|
|
char buf[2 + 16 + 1];
|
2017-08-07 16:25:11 +02:00
|
|
|
size_t add;
|
|
|
|
|
|
|
|
p = capability_to_name(i);
|
2020-07-09 23:15:47 +02:00
|
|
|
if (!p) {
|
2020-07-10 16:53:51 +02:00
|
|
|
xsprintf(buf, "0x%x", i);
|
2020-07-09 23:15:47 +02:00
|
|
|
p = buf;
|
|
|
|
}
|
2017-08-07 16:25:11 +02:00
|
|
|
|
|
|
|
add = strlen(p);
|
|
|
|
|
2017-09-25 11:09:57 +02:00
|
|
|
if (!GREEDY_REALLOC(str, allocated, n + add + 2))
|
2017-08-07 16:25:11 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
strcpy(mempcpy(str + n, p, add), " ");
|
|
|
|
n += add + 1;
|
|
|
|
}
|
|
|
|
|
2017-09-25 11:09:57 +02:00
|
|
|
if (!GREEDY_REALLOC(str, allocated, n + 1))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
|
2017-08-07 16:25:11 +02:00
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*s = TAKE_PTR(str);
|
2017-08-07 16:25:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int capability_set_from_string(const char *s, uint64_t *set) {
|
|
|
|
uint64_t val = 0;
|
|
|
|
|
|
|
|
assert(set);
|
|
|
|
|
2020-07-09 23:23:07 +02:00
|
|
|
for (const char *p = s;;) {
|
2017-08-07 16:25:11 +02:00
|
|
|
_cleanup_free_ char *word = NULL;
|
|
|
|
int r;
|
|
|
|
|
2019-06-28 11:15:05 +02:00
|
|
|
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
|
2017-08-07 16:25:11 +02:00
|
|
|
if (r == -ENOMEM)
|
|
|
|
return r;
|
|
|
|
if (r <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r = capability_from_name(word);
|
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;
|
|
|
|
}
|
|
|
|
|
|
|
|
*set = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|