18 #ifdef COMPARE_TO_LIBACL
19 #include "acl/libacl.h"
20 #else // COMPARE_TO_LIBACL
23 #define ACL_READ (0x04)
24 #define ACL_WRITE (0x02)
25 #define ACL_EXECUTE (0x01)
28 #define ACL_UNDEFINED_TAG (0x00)
29 #define ACL_USER_OBJ (0x01)
30 #define ACL_USER (0x02)
31 #define ACL_GROUP_OBJ (0x04)
32 #define ACL_GROUP (0x08)
33 #define ACL_MASK (0x10)
34 #define ACL_OTHER (0x20)
37 #define ACL_UNDEFINED_ID ((id_t) - 1)
39 #endif // COMPARE_TO_LIBACL
41 #define ACL_EA_VERSION 0x0002
51 if (e_tag != other.
e_tag) {
52 return e_tag < other.
e_tag;
54 return e_id < other.
e_id;
65 std::size_t entry_pos = 0;
66 while (entry_pos != string::npos) {
69 size_t const sep_pos = acl_string.find_first_of(
",\n", entry_pos);
70 if (sep_pos == string::npos) {
71 if (acl_string.length() > entry_pos) {
72 entry_length = acl_string.length() - entry_pos;
73 next_pos = string::npos;
79 assert(sep_pos >= entry_pos);
80 entry_length = sep_pos - entry_pos;
81 next_pos = sep_pos + 1;
83 if (entry_length == 0) {
89 string entry(acl_string, entry_pos, entry_length);
93 size_t comment_pos = entry.find(
'#');
94 if (comment_pos != string::npos) {
95 entry = string(entry, 0, comment_pos);
101 if (entry.length() == 0) {
105 string_entries.push_back(entry);
118 for (
const char &c : str) {
120 case 'r': *perms |=
ACL_READ;
break;
124 default:
return EINVAL;
135 size_t sep_pos = str.find(
':');
136 if (sep_pos == string::npos) {
139 string const type(str, 0, sep_pos);
140 size_t next_field_pos = sep_pos + 1;
141 sep_pos = str.find(
':', next_field_pos);
142 if (sep_pos == string::npos) {
145 string const qualifier(str, next_field_pos, sep_pos - next_field_pos);
146 next_field_pos = sep_pos + 1;
147 string permissions(str, next_field_pos);
149 if (!type.compare(
"user") || !type.compare(
"u")) {
151 }
else if (!type.compare(
"group") || !type.compare(
"g")) {
153 }
else if (!type.compare(
"other") || !type.compare(
"o")) {
155 }
else if (!type.compare(
"mask") || !type.compare(
"m")) {
162 if (qualifier.empty()) {
165 char* at_null_terminator_if_number;
166 long number = strtol(qualifier.c_str(), &at_null_terminator_if_number, 10);
167 if (*at_null_terminator_if_number !=
'\0') {
170 [[maybe_unused]] gid_t main_gid;
172 ok =
GetUidOf(qualifier, &uid, &main_gid);
185 entry.
e_id = htole32(number);
189 u_int16_t host_byteorder_perms;
195 entry.
e_perm = htole16(host_byteorder_perms);
214 bool types_met[
ACL_OTHER + 1] = {
false, };
216 for (
auto entry_it = entries.begin(); entry_it != entries.end(); ++entry_it) {
219 bool &type_met = types_met[e.
e_tag];
257 vector<string> string_entries;
264 vector<acl_ea_entry> entries;
265 for (
auto string_it = string_entries.begin(); string_it != string_entries.end(); ++string_it) {
272 o_equiv_mode =
false;
274 entries.push_back(entry);
278 sort(entries.begin(), entries.end());
293 size_t acl_entry_count = entries.size();
295 char *buf =
static_cast<char*
>(malloc(buf_size));
302 for (
auto entry_it = entries.begin(); entry_it != entries.end(); ++entry_it) {
303 *ext_entry = *entry_it;
312 #ifdef COMPARE_TO_LIBACL
313 int acl_from_text_to_xattr_value_libacl(
const string textual_acl,
char *&o_binary_acl,
size_t &o_size,
bool &o_equiv_mode)
315 acl_t acl = acl_from_text(textual_acl.c_str());
319 if (acl_valid(acl) != 0) {
325 int equiv = acl_equiv_mode(acl, NULL);
328 o_equiv_mode = equiv == 0;
330 o_binary_acl = (
char *)acl_to_xattr(acl, &o_size);
339 int acl_from_text_to_xattr_value_both_impl(
const string textual_acl,
char *&o_binary_acl,
size_t &o_size,
bool &o_equiv_mode)
348 l.ret = acl_from_text_to_xattr_value_libacl(textual_acl, l.binary_acl, l.binary_size, l.equiv_mode);
351 assert(b.binary_size == l.binary_size);
352 assert(0 == memcmp(b.binary_acl, l.binary_acl, b.binary_size));
353 assert(b.equiv_mode == l.equiv_mode);
356 o_binary_acl = b.binary_acl;
357 o_size = b.binary_size;
358 o_equiv_mode = b.equiv_mode;
361 #endif // COMPARE_TO_LIBACL
assert((mem||(size==0))&&"Out Of Memory")
static bool acl_valid_builtin(const vector< acl_ea_entry > &entries)
static int acl_parms_from_text(const string &str, u_int16_t *perms)
int acl_from_text_to_xattr_value(const string &textual_acl, char *&o_binary_acl, size_t &o_size, bool &o_equiv_mode)
static int acl_from_text_to_string_entries(const string &acl_string, vector< string > &string_entries)
bool GetGidOf(const std::string &groupname, gid_t *gid)
bool GetUidOf(const std::string &username, uid_t *uid, gid_t *main_gid)
bool operator<(const acl_ea_entry &other) const
static int acl_entry_from_text(const string &str, acl_ea_entry &entry)