1CAP_FROM_TEXT(3) Linux Programmer's Manual CAP_FROM_TEXT(3)
2
3
4
6 cap_from_text, cap_to_text, cap_to_name, cap_from_name - capability
7 state textual representation translation
8
10 #include <sys/capability.h>
11
12 cap_t cap_from_text(const char *buf_p);
13
14 char *cap_to_text(cap_t caps, ssize_t *length_p);
15
16 int cap_from_name(const char *name, cap_value_t *cap_p);
17
18 char *cap_to_name(cap_value_t cap);
19
20 Link with -lcap.
21
23 These functions translate a capability state between an internal repre‐
24 sentation and a textual one. The internal representation is managed by
25 the capability functions in working storage. The textual representation
26 is a structured, human-readable string suitable for display.
27
28 cap_from_text() allocates and initializes a capability state in working
29 storage. It then sets the contents of this newly created capability
30 state to the state represented by a human-readable, nul-terminated
31 character string pointed to by buf_p. It returns a pointer to the
32 newly created capability state. When the capability state in working
33 storage is no longer required, the caller should free any releasable
34 memory by calling cap_free() with cap_t as an argument. The function
35 returns an error if it cannot parse the contents of the string pointed
36 to by buf_p or does not recognize any capability_name or flag character
37 as valid. The function also returns an error if any flag is both set
38 and cleared within a single clause.
39
40 cap_to_text() converts the capability state in working storage identi‐
41 fied by cap_p into a nul-terminated human-readable string. This func‐
42 tion allocates any memory necessary to contain the string, and returns
43 a pointer to the string. If the pointer len_p is not NULL, the func‐
44 tion shall also return the full length of the string (not including the
45 nul terminator) in the location pointed to by len_p. The capability
46 state in working storage, identified by cap_p, is completely repre‐
47 sented in the character string. When the capability state in working
48 storage is no longer required, the caller should free any releasable
49 memory by calling cap_free() with the returned string pointer as an
50 argument.
51
52 cap_from_name() converts a text representation of a capability, such as
53 "cap_chown", to its numerical representation (CAP_CHOWN=0), writing the
54 decoded value into *cap_p. If cap_p is NULL no result is written, but
55 the return code of the function indicates whether or not the specified
56 capability can be represented by the library.
57
58 cap_to_name() converts a capability index value, cap, to a libcap-allo‐
59 cated textual string. This string should be deallocated with
60 cap_free().
61
63 A textual representation of capability sets consists of one or more
64 whitespace-separated clauses. Each clause specifies some operations on
65 a capability set; the set starts out with all capabilities lowered, and
66 the meaning of the string is the state of the capability set after all
67 the clauses have been applied in order.
68
69 Each clause consists of a list of comma-separated capability names (or
70 the word `all'), followed by an action-list. An action-list consists
71 of a sequence of operator flag pairs. Legal operators are: `=', '+',
72 and `-'. Legal flags are: `e', `i', and `p'. These flags are case-
73 sensitive and specify the Effective, Inheritable and Permitted sets
74 respectively.
75
76 In the capability name lists, all names are case-insensitive. The spe‐
77 cial name `all' specifies all capabilities; it is equivalent to a list
78 naming every capability individually.
79
80 Unnamed capabilities can also be specified by number. This feature
81 ensures that libcap can support capabilities that were not allocated at
82 the time libcap was compiled. However, generally upgrading libcap will
83 add names for recently allocated capabilities.
84
85 The `=' operator indicates that the listed capabilities are first reset
86 in all three capability sets. The subsequent flags (which are optional
87 when associated with this operator) indicate that the listed capabili‐
88 ties for the corresponding set are to be raised. For example: "all=p"
89 means lower every capability in the Effective and Inheritable sets but
90 raise all of the Permitted capabilities; or, "cap_fowner=ep" means
91 raise the Effective and Permitted override-file-ownership capability,
92 while lowering this Inheritable capability.
93
94 In the case that the leading operator is `=', and no list of capabili‐
95 ties is provided, the action-list is assumed to refer to `all' capabil‐
96 ities. For example, the following three clauses are equivalent to each
97 other (and indicate a completely empty capability set): "all="; "=";
98 "cap_chown,<every-other-capability>=".
99
100 The operators, `+' and `-' both require an explicit preceding capabil‐
101 ity list and one or more explicit trailing flags. The `+' operator
102 will raise all of the listed capabilities in the flagged capability
103 sets. The `-' operator will lower all of the listed capabilities in
104 the flagged capability sets. For example: "all+p" will raise all of
105 the Permitted capabilities; "cap_fowner+p-i" will raise the override-
106 file-ownership capability in the Permitted capability set and lower
107 this Inheritable capability; "cap_fowner+pe-i" and "cap_fowner=+pe" are
108 equivalent.
109
111 cap_from_text(), cap_to_text() and cap_to_text() return a non-NULL
112 value on success, and NULL on failure. cap_from_text() returns 0 for
113 success, and -1 on failure (unknown capability).
114
115 On failure, errno is set to EINVAL, or ENOMEM.
116
118 cap_from_text() and cap_to_text() are specified by the withdrawn
119 POSIX.1e draft specification. cap_from_name() and cap_to_name() are
120 Linux extensions.
121
123 The example program below demonstrates the use of cap_from_text() and
124 cap_to_text(). The following shell session shows a some example runs:
125
126 $ ./a.out "cap_chown=p cap_chown+e"
127 caps_to_text() returned "= cap_chown+ep"
128 $ ./a.out "all=pe cap_chown-e cap_kill-pe"
129 caps_to_text() returned "=ep cap_chown-e cap_kill-ep"
130
131 The source code of the program is as follows:
132
133 #include <stdlib.h>
134 #include <stdio.h>
135 #include <sys/capability.h>
136
137 #define handle_error(msg) \
138 do { perror(msg); exit(EXIT_FAILURE); } while (0)
139
140 int
141 main(int argc, char *argv[])
142 {
143 cap_t caps;
144 char *txt_caps;
145
146 if (argc != 2) {
147 fprintf(stderr, "%s <textual-cap-set>\n", argv[0]);
148 exit(EXIT_FAILURE);
149 }
150
151 caps = cap_from_text(argv[1]);
152 if (caps == NULL)
153 handle_error("cap_from_text");
154
155 txt_caps = cap_to_text(caps, NULL);
156 if (txt_caps == NULL)
157 handle_error("cap_to_text");
158
159 printf("caps_to_text() returned \"%s\"\n", txt_caps);
160
161 if (cap_free(txt_caps) != 0 || cap_free(caps) != 0)
162 handle_error("cap_free");
163
164 exit(EXIT_SUCCESS);
165 }
166
168 libcap(3), cap_clear(3), cap_compare(3), cap_copy_ext(3),
169 cap_get_file(3), cap_get_proc(3), cap_init(3), capabilities(7)
170
171
172
173 2008-05-10 CAP_FROM_TEXT(3)