Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created November 14, 2014 16:41
Show Gist options
  • Save lnicola/952289b620674dbb2f84 to your computer and use it in GitHub Desktop.
Save lnicola/952289b620674dbb2f84 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
#include <sys/acl.h>
#include <sys/types.h>
#include <acl/libacl.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
using namespace std;
bool get_user_uid(const char *name, uid_t &uid)
{
size_t bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize < 0) {
bufsize = 16384;
}
char *buf = static_cast<char *>(malloc(bufsize));
if (!buf) {
perror("malloc");
return false;
}
struct passwd pwd, *result;
int r = getpwnam_r(name, &pwd, buf, bufsize, &result);
free(buf);
if (!result) {
if (!r) {
fprintf(stderr, "not found\n");
return false;
} else {
errno = r;
perror("getpwnam_r");
return false;
}
}
uid = pwd.pw_uid;
return true;
}
bool get_gid_by_name(const char *name, gid_t &gid)
{
size_t bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
if (bufsize < 0) {
bufsize = 16384;
}
char *buf = static_cast<char *>(malloc(bufsize));
if (!buf) {
perror("malloc");
return false;
}
struct group grp, *result;
int r = getgrnam_r(name, &grp, buf, bufsize, &result);
free(buf);
if (!result) {
if (!r) {
fprintf(stderr, "not found\n");
return false;
} else {
errno = r;
perror("getgrnam_r");
return false;
}
}
gid = grp.gr_gid;
return true;
}
int main()
{
char buf[512];
fprintf(stderr, "%s\n", strerror_r(EINVAL, buf, 512));
return 0;
acl_t acl = acl_get_file("foo", ACL_TYPE_ACCESS);
if (!acl) {
perror("acl_get_file");
return -1;
}
acl_entry_t acl_entry;
if (acl_create_entry(&acl, &acl_entry) < 0) {
perror("acl_create_entry");
return -1;
}
acl_permset_t acl_permset;
if (acl_get_permset(acl_entry, &acl_permset) < 0) {
perror("acl_get_permset");
return -1;
}
if (acl_add_perm(acl_permset, ACL_READ) < 0) {
perror("acl_add_perm");
return -1;
}
if (acl_set_permset(acl_entry, acl_permset) < 0) {
perror("acl_set_permset");
return -1;
}
if (acl_set_tag_type(acl_entry, ACL_GROUP) < 0) {
perror("acl_set_tag_type");
return -1;
}
gid_t gid;
if (!get_gid_by_name("g1", gid)) {
fprintf(stderr, "cannot get uid\n");
return -1;
}
if (acl_set_qualifier(acl_entry, &gid) < 0) {
perror("acl_set_qualifier");
return -1;
}
if (acl_create_entry(&acl, &acl_entry) < 0) {
perror("acl_create_entry");
return -1;
}
if (acl_get_permset(acl_entry, &acl_permset) < 0) {
perror("acl_get_permset");
return -1;
}
if (acl_add_perm(acl_permset, ACL_READ | ACL_WRITE) < 0) {
perror("acl_add_perm");
return -1;
}
if (acl_set_permset(acl_entry, acl_permset) < 0) {
perror("acl_set_permset");
return -1;
}
if (acl_set_tag_type(acl_entry, ACL_MASK) < 0) {
perror("acl_set_tag_type");
return -1;
}
char *p = acl_to_text(acl, NULL);
if (!p) {
perror("acl_to_text");
return -1;
}
fprintf(stderr, "%s\n", p);
int last;
int r = acl_check(acl, &last);
if (r < 0) {
perror("acl_check");
return -1;
}
if (r > 0) {
fprintf(stderr, "%d %s\n", last, acl_error(r));
return -1;
}
if (acl_valid(acl) < 0) {
perror("acl_valid");
return -1;
}
if (acl_set_file("foo", ACL_TYPE_ACCESS, acl) < 0) {
perror("acl_set_file");
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment