1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
Copyright 2013 Yuriy Nazarov.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <sys/acl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/acl.h>
#include <acl/libacl.h>
#ifdef __cplusplus
}
#endif
#define USER_KEY "user"
#define USER_KEY_LENGTH 4
#define GROUP_KEY "group"
#define GROUP_KEY_LENGTH 5
#define OTHER_KEY "other"
#define OTHER_KEY_LENGTH 5
#define MASK_KEY "mask"
#define MASK_KEY_LENGTH 4
#define USER_OBJ_KEY "uperm"
#define USER_OBJ_KEY_LENGTH 5
#define GROUP_OBJ_KEY "gperm"
#define GROUP_OBJ_KEY_LENGTH 5
#define CONSTANT_YES 0
#define CONSTANT_NO 1
HV* derefHV(SV *hashref){
HV *ret_hash;
if (! SvROK(hashref)) //check that input value is really reference
return NULL;
ret_hash = (HV *)SvRV(hashref);
if (SvTYPE((SV *)ret_hash) != SVt_PVHV) //check that it's really hash
return NULL;
return ret_hash;
}
void add_perm_to_hash(HV *hash, int r, int w, int x, char *key, U32 key_len){
HV* perm_hash = newHV();
hv_store(perm_hash, "r", 1, newSViv( r!=0 ), 0);
hv_store(perm_hash, "w", 1, newSViv( w!=0 ), 0);
hv_store(perm_hash, "x", 1, newSViv( x!=0 ), 0);
hv_store(hash, key, key_len, newRV_noinc((SV*) perm_hash), 0);
}
void add_to_hash(HV *hash, acl_entry_t *ent, char *key, U32 key_len){
acl_permset_t permset;
acl_get_permset(*ent, &permset);
add_perm_to_hash(hash, acl_get_perm(permset, ACL_READ), acl_get_perm(permset, ACL_WRITE), acl_get_perm(permset, ACL_EXECUTE), key, key_len);
}
void set_perm(acl_entry_t ent, mode_t perm)
{
acl_permset_t set;
acl_get_permset(ent, &set);
if (perm & ACL_READ)
acl_add_perm(set, ACL_READ);
else
acl_delete_perm(set, ACL_READ);
if (perm & ACL_WRITE)
acl_add_perm(set, ACL_WRITE);
else
acl_delete_perm(set, ACL_WRITE);
if (perm & ACL_EXECUTE)
acl_add_perm(set, ACL_EXECUTE);
else
acl_delete_perm(set, ACL_EXECUTE);
}
int get_perm_from_hash(HV *hash, const char *key, int key_len){
HV *perm;
SV **perm_ref;
SV **atom_ref;
int perm_val = 0;
if(perm_ref = hv_fetch(hash, key, key_len, 0)){
perm = derefHV(*perm_ref);
if(NULL==perm){
return 0;
}
if(atom_ref = hv_fetch(perm, "r", 1, 0)){
if (! SvIOK(*atom_ref))
return 0;
perm_val |= SvIV(*atom_ref)?ACL_READ:0;
}
if(atom_ref = hv_fetch(perm, "w", 1, 0)){
if (! SvIOK(*atom_ref))
return 0;
perm_val |= SvIV(*atom_ref)?ACL_WRITE:0;
}
if(atom_ref = hv_fetch(perm, "x", 1, 0)){
if (! SvIOK(*atom_ref))
return 0;
perm_val |= SvIV(*atom_ref)?ACL_EXECUTE:0;
}
}
return perm_val;
}
int getfacl_internal(char *filename, HV **out_acl, HV **out_default_acl){ //returns count
struct stat st;
int i;
HV **acl_hashes[2] = {out_acl, out_default_acl};
acl_type_t acl_types[2] = {ACL_TYPE_ACCESS, ACL_TYPE_DEFAULT};
*out_acl = NULL;
*out_default_acl = NULL;
if (stat(filename, &st) != 0) {
return 0;
}
for(i=0; i<2; i++){
HV* acl_hash;
HV* ret_acl_uperm;
HV* ret_acl_gperm;
acl_entry_t ent;
acl_t acl;
int ret;
acl = acl_get_file(filename, acl_types[i]);
if (acl == NULL) {
continue;
}
ret = acl_get_entry(acl, ACL_FIRST_ENTRY, &ent);
if (ret != 1){
continue;
}
acl_hash = newHV();
ret_acl_uperm = newHV();
ret_acl_gperm = newHV();
while (ret > 0) {
acl_tag_t e_type;
acl_get_tag_type(ent, &e_type);
char id_str[30]; //Enough to print uint64_t
U32 id_str_len;
id_t *id_p;
switch(e_type) {
case ACL_USER_OBJ: add_to_hash(acl_hash, &ent, USER_OBJ_KEY, USER_OBJ_KEY_LENGTH); break;
case ACL_GROUP_OBJ: add_to_hash(acl_hash, &ent, GROUP_OBJ_KEY, GROUP_OBJ_KEY_LENGTH); break;
case ACL_MASK: add_to_hash(acl_hash, &ent, MASK_KEY, MASK_KEY_LENGTH); break;
case ACL_OTHER: add_to_hash(acl_hash, &ent, OTHER_KEY, OTHER_KEY_LENGTH); break;
case ACL_USER:
id_p = acl_get_qualifier(ent);
id_str_len = sprintf(id_str, "%d", *id_p);
add_to_hash(ret_acl_uperm, &ent, id_str, id_str_len);
break;
case ACL_GROUP:
id_p = acl_get_qualifier(ent);
id_str_len = sprintf(id_str, "%d", *id_p);
add_to_hash(ret_acl_gperm, &ent, id_str, id_str_len);
break;
}
ret = acl_get_entry(acl, ACL_NEXT_ENTRY, &ent);
}
hv_store(acl_hash, USER_KEY, USER_KEY_LENGTH, newRV_noinc((SV*) ret_acl_uperm), 0);
hv_store(acl_hash, GROUP_KEY, GROUP_KEY_LENGTH, newRV_noinc((SV*) ret_acl_gperm), 0);
*(acl_hashes[i]) = acl_hash;
}
if(NULL==*out_acl && NULL==*out_default_acl){
*out_acl = newHV();
add_perm_to_hash(*out_acl, st.st_mode && S_IRUSR, st.st_mode && S_IWUSR, st.st_mode && S_IXUSR, USER_OBJ_KEY, USER_OBJ_KEY_LENGTH);
add_perm_to_hash(*out_acl, st.st_mode && S_IRGRP, st.st_mode && S_IWGRP, st.st_mode && S_IXGRP, GROUP_OBJ_KEY, GROUP_OBJ_KEY_LENGTH);
add_perm_to_hash(*out_acl, st.st_mode && S_IROTH, st.st_mode && S_IWOTH, st.st_mode && S_IXOTH, OTHER_KEY, OTHER_KEY_LENGTH);
}
return (NULL==*out_acl)?0:( (NULL==*out_default_acl)?1:2 );
}
int setfacl_internal(char *filename, HV *in_acl_hash, HV *in_default_acl_hash){
HV *acl_hashes[3] = {in_acl_hash, in_default_acl_hash, NULL};
acl_type_t acl_types[3] = {ACL_TYPE_ACCESS, ACL_TYPE_DEFAULT, 0};
int i = 0;
int rc = CONSTANT_YES;
while(NULL != acl_hashes[i]){
acl_t acl = NULL;
acl_entry_t ent;
HE *hash_entry;
SV **hash_ref;
HV *user_hash = NULL;
HV *group_hash = NULL;
HV *current_acl = acl_hashes[i];
if(hash_ref = hv_fetch(current_acl, USER_KEY, USER_KEY_LENGTH, 0)){
user_hash = derefHV(*hash_ref);
}else{
//missing USER_KEY
}
if(hash_ref = hv_fetch(current_acl, GROUP_KEY, GROUP_KEY_LENGTH, 0)){
group_hash = derefHV(*hash_ref);
}else{
//missing GROUP_KEY
}
acl = acl_init(0);
if (!acl) {
rc = CONSTANT_NO;
}
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_USER_OBJ);
set_perm(ent, get_perm_from_hash(current_acl, USER_OBJ_KEY, USER_OBJ_KEY_LENGTH));
} else {
rc = CONSTANT_NO;
}
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_GROUP_OBJ);
set_perm(ent, get_perm_from_hash(current_acl, GROUP_OBJ_KEY, GROUP_OBJ_KEY_LENGTH));
} else {
rc = CONSTANT_NO;
}
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_MASK);
set_perm(ent, get_perm_from_hash(current_acl, MASK_KEY, MASK_KEY_LENGTH));
} else {
rc = CONSTANT_NO;
}
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_OTHER);
set_perm(ent, get_perm_from_hash(current_acl, OTHER_KEY, OTHER_KEY_LENGTH));
} else {
rc = CONSTANT_NO;
}
if(NULL != user_hash){
hv_iterinit(user_hash);
while(hash_entry = hv_iternext(user_hash)){
id_t id_p;
I32 key_len;
char *key = hv_iterkey(hash_entry, &key_len);
id_p = atoi(key);
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_USER);
acl_set_qualifier(ent, &id_p);
set_perm(ent, get_perm_from_hash(user_hash, key, key_len));
} else {
rc = CONSTANT_NO;
}
}
}
if(NULL != group_hash){
hv_iterinit(group_hash);
while(hash_entry = hv_iternext(group_hash)){
id_t id_p;
I32 key_len;
char *key = hv_iterkey(hash_entry, &key_len);
id_p = atoi(key);
if (acl_create_entry(&acl, &ent) == 0){
acl_set_tag_type(ent, ACL_GROUP);
acl_set_qualifier(ent, &id_p);
set_perm(ent, get_perm_from_hash(group_hash, key, key_len));
} else {
rc = CONSTANT_NO;
}
}
}
if (acl_set_file(filename, acl_types[i], acl) == -1) {
rc = CONSTANT_NO;
}
acl_free(acl);
i++;
}
return rc;
}
/*
* Exported code
*/
#define PACKAGE_NAME "Linux::ACL"
MODULE = Linux::ACL PACKAGE = Linux::ACL
void
getfacl(filename)
SV * filename;
PPCODE:
HV *acl, *default_acl;
STRLEN filename_string_length;
char *filename_string = SvPV(filename, filename_string_length);
int count = getfacl_internal(filename_string, &acl, &default_acl);
if(count>=1)
XPUSHs( sv_2mortal( newRV_noinc((SV*) acl) ) );
if(count>=2)
XPUSHs( sv_2mortal( newRV_noinc((SV*) default_acl) ) );
XSRETURN(count);
void
setfacl(filename, acl_hashref, ...)
SV *filename;
SV *acl_hashref;
PPCODE:
STRLEN filename_string_length;
char* filename_string = SvPV(filename, filename_string_length);
HV *acl_hash = derefHV(acl_hashref);
HV *default_acl_hash = NULL;
if( items > 2 )
default_acl_hash = derefHV(ST(2));
if(NULL == acl_hash){
XSRETURN_NO;
}
if( CONSTANT_YES == setfacl_internal(filename_string, acl_hash, default_acl_hash) ){
XSRETURN_YES;
}else{
XSRETURN_NO;
}
|