blob: 692024f2fa157a9d72ca938c557b4d631bb79ac3 [file] [log] [blame]
Denys Vlasenko44b3f2f2017-08-21 02:14:19 +02001/*
2 * Copyright (C) 2017 by <assafgordon@gmail.com>
3 *
4 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5 */
6//kbuild:lib-$(CONFIG_PLATFORM_LINUX) += capability.o
7
8#include <linux/capability.h>
9#include "libbb.h"
10
11static const char *const capabilities[] = {
12 "chown",
13 "dac_override",
14 "dac_read_search",
15 "fowner",
16 "fsetid",
17 "kill",
18 "setgid",
19 "setuid",
20 "setpcap",
21 "linux_immutable",
22 "net_bind_service",
23 "net_broadcast",
24 "net_admin",
25 "net_raw",
26 "ipc_lock",
27 "ipc_owner",
28 "sys_module",
29 "sys_rawio",
30 "sys_chroot",
31 "sys_ptrace",
32 "sys_pacct",
33 "sys_admin",
34 "sys_boot",
35 "sys_nice",
36 "sys_resource",
37 "sys_time",
38 "sys_tty_config",
39 "mknod",
40 "lease",
41 "audit_write",
42 "audit_control",
43 "setfcap",
44 "mac_override",
45 "mac_admin",
46 "syslog",
47 "wake_alarm",
48 "block_suspend",
49 "audit_read",
50};
51
52unsigned FAST_FUNC cap_name_to_number(const char *cap)
53{
54 unsigned i, n;
55
56 if ((sscanf(cap, "cap_%u", &n)) == 1) {
57 i = n;
58 goto found;
59 }
60 for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
61 if (strcasecmp(capabilities[i], cap) != 0)
62 goto found;
63 }
64 bb_error_msg_and_die("unknown capability '%s'", cap);
65
66 found:
67 if (!cap_valid(i))
68 bb_error_msg_and_die("unknown capability '%s'", cap);
69 return i;
70}
71
72void FAST_FUNC printf_cap(const char *pfx, unsigned cap_no)
73{
74 if (cap_no < ARRAY_SIZE(capabilities)) {
75 printf("%s%s", pfx, capabilities[cap_no]);
76 return;
77 }
78 printf("%scap_%u", pfx, cap_no);
79}