10 staticforward PyTypeObject ACL_Type;
11 static PyObject* ACL_applyto(PyObject* obj, PyObject* args);
12 static PyObject* ACL_valid(PyObject* obj, PyObject* args);
15 static PyObject* ACL_get_state(PyObject *obj, PyObject* args);
16 static PyObject* ACL_set_state(PyObject *obj, PyObject* args);
18 staticforward PyTypeObject Entry_Type;
19 staticforward PyTypeObject Permset_Type;
20 static PyObject* Permset_new(PyTypeObject* type, PyObject* args,
24 static acl_perm_t holder_ACL_EXECUTE = ACL_EXECUTE;
25 static acl_perm_t holder_ACL_READ = ACL_READ;
26 static acl_perm_t holder_ACL_WRITE = ACL_WRITE;
40 PyObject *parent_acl; /* The parent acl, so it won't run out on us */
46 PyObject *parent_entry; /* The parent entry, so it won't run out on us */
47 acl_permset_t permset;
52 /* Creation of a new ACL instance */
53 static PyObject* ACL_new(PyTypeObject* type, PyObject* args,
57 newacl = type->tp_alloc(type, 0);
60 ((ACL_Object*)newacl)->acl = NULL;
62 ((ACL_Object*)newacl)->entry_id = ACL_FIRST_ENTRY;
69 /* Initialization of a new ACL instance */
70 static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
71 ACL_Object* self = (ACL_Object*) obj;
73 static char *kwlist[] = { "file", "fd", "text", "acl", "filedef",
75 char *format = "|sisO!sH";
78 static char *kwlist[] = { "file", "fd", "text", "acl", "filedef", NULL };
79 char *format = "|sisO!s";
85 ACL_Object* thesrc = NULL;
87 if(!PyTuple_Check(args) || PyTuple_Size(args) != 0 ||
88 (keywds != NULL && PyDict_Check(keywds) && PyDict_Size(keywds) > 1)) {
89 PyErr_SetString(PyExc_ValueError, "a max of one keyword argument"
93 if(!PyArg_ParseTupleAndKeywords(args, keywds, format, kwlist,
94 &file, &fd, &text, &ACL_Type,
102 /* Free the old acl_t without checking for error, we don't
104 if(self->acl != NULL)
108 self->acl = acl_get_file(file, ACL_TYPE_ACCESS);
109 else if(text != NULL)
110 self->acl = acl_from_text(text);
112 self->acl = acl_get_fd(fd);
113 else if(thesrc != NULL)
114 self->acl = acl_dup(thesrc->acl);
115 else if(filedef != NULL)
116 self->acl = acl_get_file(filedef, ACL_TYPE_DEFAULT);
118 else if(PyMapping_HasKeyString(keywds, kwlist[5]))
119 self->acl = acl_from_mode(mode);
122 self->acl = acl_init(0);
124 if(self->acl == NULL) {
125 PyErr_SetFromErrno(PyExc_IOError);
132 /* Standard type functions */
133 static void ACL_dealloc(PyObject* obj) {
134 ACL_Object *self = (ACL_Object*) obj;
135 PyObject *err_type, *err_value, *err_traceback;
136 int have_error = PyErr_Occurred() ? 1 : 0;
139 PyErr_Fetch(&err_type, &err_value, &err_traceback);
140 if(self->acl != NULL && acl_free(self->acl) != 0)
141 PyErr_WriteUnraisable(obj);
143 PyErr_Restore(err_type, err_value, err_traceback);
147 /* Converts the acl to a text format */
148 static PyObject* ACL_str(PyObject *obj) {
150 ACL_Object *self = (ACL_Object*) obj;
153 text = acl_to_text(self->acl, NULL);
155 return PyErr_SetFromErrno(PyExc_IOError);
157 ret = PyString_FromString(text);
158 if(acl_free(text) != 0) {
160 return PyErr_SetFromErrno(PyExc_IOError);
166 static char __to_any_text_doc__[] =
167 "Convert the ACL to a custom text format.\n"
169 "This method encapsulates the acl_to_any_text function. It allows a \n"
170 "customized text format to be generated for the ACL. See\n"
171 "acl_to_any_text(3) for more details.\n"
174 " - prefix: if given, this string will be prepended to all lines\n"
175 " - separator: a single character (defaults to '\\n'); this will be\n"
176 " user to separate the entries in the ACL\n"
177 " - options: a bitwise combination of:\n"
178 " TEXT_ABBREVIATE: use 'u' instead of 'user', 'g' instead of \n"
180 " TEXT_NUMERIC_IDS: User and group IDs are included as decimal\n"
181 " numbers instead of names\n"
182 " TEXT_SOME_EFFECTIVE: Include comments denoting the effective\n"
183 " permissions when some are masked\n"
184 " TEXT_ALL_EFFECTIVE: Include comments after all ACL entries\n"
185 " affected by an ACL_MASK entry\n"
186 " TEXT_SMART_INDENT: Used in combination with the _EFFECTIVE\n"
187 " options, this will ensure that comments \n"
188 " are alligned to the fourth tab position\n"
189 " (assuming one tab equal eight spaces\n"
192 /* Converts the acl to a custom text format */
193 static PyObject* ACL_to_any_text(PyObject *obj, PyObject *args,
196 ACL_Object *self = (ACL_Object*) obj;
198 char *arg_prefix = NULL;
199 char arg_separator = '\n';
201 static char *kwlist[] = {"prefix", "separator", "options", NULL};
203 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sci", kwlist, &arg_prefix,
204 &arg_separator, &arg_options))
207 text = acl_to_any_text(self->acl, arg_prefix, arg_separator, arg_options);
209 return PyErr_SetFromErrno(PyExc_IOError);
211 ret = PyString_FromString(text);
212 if(acl_free(text) != 0) {
214 return PyErr_SetFromErrno(PyExc_IOError);
219 static char __check_doc__[] =
220 "Check the ACL validity.\n"
222 "This is a non-portable, Linux specific extension that allow more\n"
223 "information to be retrieved in case an ACL is not valid than the\n"
224 "validate() method.\n"
226 "This method will return either False (the ACL is valid), or a tuple\n"
227 "with two elements. The first element is one of the following\n"
229 " - ACL_MULTI_ERROR: The ACL contains multiple entries that have a\n"
230 " tag type that may occur at most once\n"
231 " - ACL_DUPLICATE_ERROR: The ACL contains multiple ACL_USER or \n"
232 " ACL_GROUP entries with the same ID\n"
233 " - ACL_MISS_ERROR: A required entry is missing\n"
234 " - ACL_ENTRY_ERROR: The ACL contains an invalid entry tag type\n"
236 "The second element of the tuple is the index of the entry that is\n"
237 "invalid (in the same order as by iterating over the ACL entry)\n"
240 /* The acl_check method */
241 static PyObject* ACL_check(PyObject* obj, PyObject* args) {
242 ACL_Object *self = (ACL_Object*) obj;
246 if((result = acl_check(self->acl, &eindex)) == -1)
247 return PyErr_SetFromErrno(PyExc_IOError);
252 return PyTuple_Pack(2, PyInt_FromLong(result), PyInt_FromLong(eindex));
258 static char __applyto_doc__[] =
259 "Apply the ACL to a file or filehandle.\n"
262 " - either a filename or a file-like object or an integer; this\n"
263 " represents the filesystem object on which to act\n"
264 " - optional flag representing the type of ACL to set, either\n"
265 " ACL_TYPE_ACCESS (default) or ACL_TYPE_DEFAULT\n"
268 /* Applyes the ACL to a file */
269 static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
270 ACL_Object *self = (ACL_Object*) obj;
272 acl_type_t type = ACL_TYPE_ACCESS;
276 if (!PyArg_ParseTuple(args, "O|i", &myarg, &type))
279 if(PyString_Check(myarg)) {
280 char *filename = PyString_AS_STRING(myarg);
281 nret = acl_set_file(filename, type, self->acl);
282 } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
283 nret = acl_set_fd(fd, self->acl);
285 PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int,"
286 " or file-like object");
290 return PyErr_SetFromErrno(PyExc_IOError);
293 /* Return the result */
298 static char __valid_doc__[] =
299 "Test the ACL for validity.\n"
301 "This method tests the ACL to see if it is a valid ACL\n"
302 "in terms of the filesystem. More precisely, it checks that:\n"
304 "The ACL contains exactly one entry with each of the\n"
305 "ACL_USER_OBJ, ACL_GROUP_OBJ, and ACL_OTHER tag types. Entries\n"
306 "with ACL_USER and ACL_GROUP tag types may appear zero or more\n"
307 "times in an ACL. An ACL that contains entries of ACL_USER or\n"
308 "ACL_GROUP tag types must contain exactly one entry of the \n"
309 "ACL_MASK tag type. If an ACL contains no entries of\n"
310 "ACL_USER or ACL_GROUP tag types, the ACL_MASK entry is optional.\n"
312 "All user ID qualifiers must be unique among all entries of\n"
313 "the ACL_USER tag type, and all group IDs must be unique among all\n"
314 "entries of ACL_GROUP tag type.\n"
316 "The method will return 1 for a valid ACL and 0 for an invalid one.\n"
317 "This has been chosen because the specification for acl_valid in\n"
318 "the POSIX.1e standard documents only one possible value for errno\n"
319 "in case of an invalid ACL, so we can't differentiate between\n"
320 "classes of errors. Other suggestions are welcome.\n"
323 /* Checks the ACL for validity */
324 static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
325 ACL_Object *self = (ACL_Object*) obj;
327 if(acl_valid(self->acl) == -1) {
338 static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
339 ACL_Object *self = (ACL_Object*) obj;
344 size = acl_size(self->acl);
346 return PyErr_SetFromErrno(PyExc_IOError);
348 if((ret = PyString_FromStringAndSize(NULL, size)) == NULL)
350 buf = PyString_AsString(ret);
352 if((nsize = acl_copy_ext(buf, self->acl, size)) == -1) {
354 return PyErr_SetFromErrno(PyExc_IOError);
360 static PyObject* ACL_set_state(PyObject *obj, PyObject* args) {
361 ACL_Object *self = (ACL_Object*) obj;
366 /* Parse the argument */
367 if (!PyArg_ParseTuple(args, "s#", &buf, &bufsize))
370 /* Try to import the external representation */
371 if((ptr = acl_copy_int(buf)) == NULL)
372 return PyErr_SetFromErrno(PyExc_IOError);
374 /* Free the old acl. Should we ignore errors here? */
375 if(self->acl != NULL) {
376 if(acl_free(self->acl) == -1)
377 return PyErr_SetFromErrno(PyExc_IOError);
382 /* Return the result */
387 /* tp_iter for the ACL type; since it can be iterated only
388 * destructively, the type is its iterator
390 static PyObject* ACL_iter(PyObject *obj) {
391 ACL_Object *self = (ACL_Object*)obj;
392 self->entry_id = ACL_FIRST_ENTRY;
397 /* the tp_iternext function for the ACL type */
398 static PyObject* ACL_iternext(PyObject *obj) {
399 ACL_Object *self = (ACL_Object*)obj;
400 acl_entry_t the_entry_t;
401 Entry_Object *the_entry_obj;
404 nerr = acl_get_entry(self->acl, self->entry_id, &the_entry_t);
405 self->entry_id = ACL_NEXT_ENTRY;
407 return PyErr_SetFromErrno(PyExc_IOError);
409 /* Docs says this is not needed */
410 /*PyErr_SetObject(PyExc_StopIteration, Py_None);*/
414 the_entry_obj = (Entry_Object*) PyType_GenericNew(&Entry_Type, NULL, NULL);
415 if(the_entry_obj == NULL)
418 the_entry_obj->entry = the_entry_t;
420 the_entry_obj->parent_acl = obj;
421 Py_INCREF(obj); /* For the reference we have in entry->parent */
423 return (PyObject*)the_entry_obj;
426 static char __ACL_delete_entry_doc__[] =
427 "Deletes an entry from the ACL.\n"
429 "Note: Only with level 2\n"
431 " - the Entry object which should be deleted; note that after\n"
432 " this function is called, that object is unusable any longer\n"
433 " and should be deleted\n"
436 /* Deletes an entry from the ACL */
437 static PyObject* ACL_delete_entry(PyObject *obj, PyObject *args) {
438 ACL_Object *self = (ACL_Object*)obj;
441 if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &e))
444 if(acl_delete_entry(self->acl, e->entry) == -1)
445 return PyErr_SetFromErrno(PyExc_IOError);
447 /* Return the result */
452 static char __ACL_calc_mask_doc__[] =
453 "Compute the file group class mask.\n"
455 "The calc_mask() method calculates and sets the permissions \n"
456 "associated with the ACL_MASK Entry of the ACL.\n"
457 "The value of the new permissions is the union of the permissions \n"
458 "granted by all entries of tag type ACL_GROUP, ACL_GROUP_OBJ, or \n"
459 "ACL_USER. If the ACL already contains an ACL_MASK entry, its \n"
460 "permissions are overwritten; if it does not contain an ACL_MASK \n"
461 "Entry, one is added.\n"
463 "The order of existing entries in the ACL is undefined after this \n"
467 /* Updates the mask entry in the ACL */
468 static PyObject* ACL_calc_mask(PyObject *obj, PyObject *args) {
469 ACL_Object *self = (ACL_Object*)obj;
471 if(acl_calc_mask(&self->acl) == -1)
472 return PyErr_SetFromErrno(PyExc_IOError);
474 /* Return the result */
479 static char __ACL_append_doc__[] =
480 "Append a new Entry to the ACL and return it.\n"
482 "This is a convenience function to create a new Entry \n"
483 "and append it to the ACL.\n"
484 "If a parameter of type Entry instance is given, the \n"
485 "entry will be a copy of that one (as if copied with \n"
486 "Entry.copy()), otherwise, the new entry will be empty.\n"
489 /* Convenience method to create a new Entry */
490 static PyObject* ACL_append(PyObject *obj, PyObject *args) {
491 ACL_Object* self = (ACL_Object*) obj;
492 Entry_Object* newentry;
493 Entry_Object* oldentry = NULL;
496 newentry = (Entry_Object*)PyType_GenericNew(&Entry_Type, NULL, NULL);
497 if(newentry == NULL) {
501 if (!PyArg_ParseTuple(args, "|O!", &Entry_Type, &oldentry))
504 nret = acl_create_entry(&self->acl, &newentry->entry);
507 return PyErr_SetFromErrno(PyExc_IOError);
510 if(oldentry != NULL) {
511 nret = acl_copy_entry(newentry->entry, oldentry->entry);
514 return PyErr_SetFromErrno(PyExc_IOError);
518 newentry->parent_acl = obj;
521 return (PyObject*)newentry;
524 /***** Entry type *****/
526 /* Creation of a new Entry instance */
527 static PyObject* Entry_new(PyTypeObject* type, PyObject* args,
531 newentry = PyType_GenericNew(type, args, keywds);
533 if(newentry != NULL) {
534 ((Entry_Object*)newentry)->entry = NULL;
535 ((Entry_Object*)newentry)->parent_acl = NULL;
541 /* Initialization of a new Entry instance */
542 static int Entry_init(PyObject* obj, PyObject* args, PyObject *keywds) {
543 Entry_Object* self = (Entry_Object*) obj;
544 ACL_Object* parent = NULL;
546 if (!PyArg_ParseTuple(args, "O!", &ACL_Type, &parent))
549 if(acl_create_entry(&parent->acl, &self->entry) == -1) {
550 PyErr_SetFromErrno(PyExc_IOError);
554 self->parent_acl = (PyObject*)parent;
560 /* Free the Entry instance */
561 static void Entry_dealloc(PyObject* obj) {
562 Entry_Object *self = (Entry_Object*) obj;
563 PyObject *err_type, *err_value, *err_traceback;
564 int have_error = PyErr_Occurred() ? 1 : 0;
567 PyErr_Fetch(&err_type, &err_value, &err_traceback);
568 if(self->parent_acl != NULL) {
569 Py_DECREF(self->parent_acl);
570 self->parent_acl = NULL;
573 PyErr_Restore(err_type, err_value, err_traceback);
577 /* Converts the entry to a text format */
578 static PyObject* Entry_str(PyObject *obj) {
583 PyObject *format, *list;
584 Entry_Object *self = (Entry_Object*) obj;
586 if(acl_get_tag_type(self->entry, &tag) == -1) {
587 PyErr_SetFromErrno(PyExc_IOError);
590 if(tag == ACL_USER || tag == ACL_GROUP) {
591 if((p = acl_get_qualifier(self->entry)) == NULL) {
592 PyErr_SetFromErrno(PyExc_IOError);
595 qualifier = *(uid_t*)p;
601 format = PyString_FromString("ACL entry for %s");
604 list = PyTuple_New(1);
605 if(tag == ACL_UNDEFINED_TAG) {
606 PyTuple_SetItem(list, 0, PyString_FromString("undefined type"));
607 } else if(tag == ACL_USER_OBJ) {
608 PyTuple_SetItem(list, 0, PyString_FromString("the owner"));
609 } else if(tag == ACL_GROUP_OBJ) {
610 PyTuple_SetItem(list, 0, PyString_FromString("the group"));
611 } else if(tag == ACL_OTHER) {
612 PyTuple_SetItem(list, 0, PyString_FromString("the others"));
613 } else if(tag == ACL_USER) {
614 PyTuple_SetItem(list, 0, PyString_FromFormat("user with uid %d",
616 } else if(tag == ACL_GROUP) {
617 PyTuple_SetItem(list, 0, PyString_FromFormat("group with gid %d",
619 } else if(tag == ACL_MASK) {
620 PyTuple_SetItem(list, 0, PyString_FromString("the mask"));
622 PyTuple_SetItem(list, 0, PyString_FromString("UNKNOWN_TAG_TYPE!"));
624 ret = PyString_Format(format, list);
630 /* Sets the tag type of the entry */
631 static int Entry_set_tag_type(PyObject* obj, PyObject* value, void* arg) {
632 Entry_Object *self = (Entry_Object*) obj;
635 PyErr_SetString(PyExc_TypeError,
636 "tag type deletion is not supported");
640 if(!PyInt_Check(value)) {
641 PyErr_SetString(PyExc_TypeError,
642 "tag type must be integer");
645 if(acl_set_tag_type(self->entry, (acl_tag_t)PyInt_AsLong(value)) == -1) {
646 PyErr_SetFromErrno(PyExc_IOError);
653 /* Returns the tag type of the entry */
654 static PyObject* Entry_get_tag_type(PyObject *obj, void* arg) {
655 Entry_Object *self = (Entry_Object*) obj;
658 if (self->entry == NULL) {
659 PyErr_SetString(PyExc_AttributeError, "entry attribute");
662 if(acl_get_tag_type(self->entry, &value) == -1) {
663 PyErr_SetFromErrno(PyExc_IOError);
667 return PyInt_FromLong(value);
670 /* Sets the qualifier (either uid_t or gid_t) for the entry,
671 * usable only if the tag type if ACL_USER or ACL_GROUP
673 static int Entry_set_qualifier(PyObject* obj, PyObject* value, void* arg) {
674 Entry_Object *self = (Entry_Object*) obj;
678 PyErr_SetString(PyExc_TypeError,
679 "qualifier deletion is not supported");
683 if(!PyInt_Check(value)) {
684 PyErr_SetString(PyExc_TypeError,
685 "tag type must be integer");
688 uidgid = PyInt_AsLong(value);
689 if(acl_set_qualifier(self->entry, (void*)&uidgid) == -1) {
690 PyErr_SetFromErrno(PyExc_IOError);
697 /* Returns the qualifier of the entry */
698 static PyObject* Entry_get_qualifier(PyObject *obj, void* arg) {
699 Entry_Object *self = (Entry_Object*) obj;
703 if (self->entry == NULL) {
704 PyErr_SetString(PyExc_AttributeError, "entry attribute");
707 if((p = acl_get_qualifier(self->entry)) == NULL) {
708 PyErr_SetFromErrno(PyExc_IOError);
714 return PyInt_FromLong(value);
717 /* Returns the parent ACL of the entry */
718 static PyObject* Entry_get_parent(PyObject *obj, void* arg) {
719 Entry_Object *self = (Entry_Object*) obj;
721 Py_INCREF(self->parent_acl);
722 return self->parent_acl;
725 /* Returns the a new Permset representing the permset of the entry
726 * FIXME: Should return a new reference to the same object, which
727 * should be created at init time!
729 static PyObject* Entry_get_permset(PyObject *obj, void* arg) {
730 Entry_Object *self = (Entry_Object*)obj;
734 p = Permset_new(&Permset_Type, NULL, NULL);
737 ps = (Permset_Object*)p;
738 if(acl_get_permset(self->entry, &ps->permset) == -1) {
739 PyErr_SetFromErrno(PyExc_IOError);
742 ps->parent_entry = obj;
748 /* Sets the permset of the entry to the passed Permset */
749 static int Entry_set_permset(PyObject* obj, PyObject* value, void* arg) {
750 Entry_Object *self = (Entry_Object*)obj;
753 if(!PyObject_IsInstance(value, (PyObject*)&Permset_Type)) {
754 PyErr_SetString(PyExc_TypeError, "argument 1 must be posix1e.Permset");
757 p = (Permset_Object*)value;
758 if(acl_set_permset(self->entry, p->permset) == -1) {
759 PyErr_SetFromErrno(PyExc_IOError);
765 static char __Entry_copy_doc__[] =
766 "Copy an ACL entry.\n"
768 "This method sets all the parameters to those of another\n"
769 "entry, even one of another's ACL\n"
771 " - src, instance of type Entry\n"
774 /* Sets all the entry parameters to another's entry */
775 static PyObject* Entry_copy(PyObject *obj, PyObject *args) {
776 Entry_Object *self = (Entry_Object*)obj;
779 if(!PyArg_ParseTuple(args, "O!", &Entry_Type, &other))
782 if(acl_copy_entry(self->entry, other->entry) == -1)
783 return PyErr_SetFromErrno(PyExc_IOError);
789 /**** Permset type *****/
791 /* Creation of a new Permset instance */
792 static PyObject* Permset_new(PyTypeObject* type, PyObject* args,
794 PyObject* newpermset;
796 newpermset = PyType_GenericNew(type, args, keywds);
798 if(newpermset != NULL) {
799 ((Permset_Object*)newpermset)->permset = NULL;
800 ((Permset_Object*)newpermset)->parent_entry = NULL;
806 /* Initialization of a new Permset instance */
807 static int Permset_init(PyObject* obj, PyObject* args, PyObject *keywds) {
808 Permset_Object* self = (Permset_Object*) obj;
809 Entry_Object* parent = NULL;
811 if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &parent))
814 if(acl_get_permset(parent->entry, &self->permset) == -1) {
815 PyErr_SetFromErrno(PyExc_IOError);
819 self->parent_entry = (PyObject*)parent;
825 /* Free the Permset instance */
826 static void Permset_dealloc(PyObject* obj) {
827 Permset_Object *self = (Permset_Object*) obj;
828 PyObject *err_type, *err_value, *err_traceback;
829 int have_error = PyErr_Occurred() ? 1 : 0;
832 PyErr_Fetch(&err_type, &err_value, &err_traceback);
833 if(self->parent_entry != NULL) {
834 Py_DECREF(self->parent_entry);
835 self->parent_entry = NULL;
838 PyErr_Restore(err_type, err_value, err_traceback);
842 /* Permset string representation */
843 static PyObject* Permset_str(PyObject *obj) {
844 Permset_Object *self = (Permset_Object*) obj;
847 pstr[0] = get_perm(self->permset, ACL_READ) ? 'r' : '-';
848 pstr[1] = get_perm(self->permset, ACL_WRITE) ? 'w' : '-';
849 pstr[2] = get_perm(self->permset, ACL_EXECUTE) ? 'x' : '-';
850 return PyString_FromStringAndSize(pstr, 3);
853 static char __Permset_clear_doc__[] =
854 "Clear all permissions from the permission set.\n"
857 /* Clears all permissions from the permset */
858 static PyObject* Permset_clear(PyObject* obj, PyObject* args) {
859 Permset_Object *self = (Permset_Object*) obj;
861 if(acl_clear_perms(self->permset) == -1)
862 return PyErr_SetFromErrno(PyExc_IOError);
864 /* Return the result */
869 static PyObject* Permset_get_right(PyObject *obj, void* arg) {
870 Permset_Object *self = (Permset_Object*) obj;
872 if(get_perm(self->permset, *(acl_perm_t*)arg)) {
881 static int Permset_set_right(PyObject* obj, PyObject* value, void* arg) {
882 Permset_Object *self = (Permset_Object*) obj;
886 if(!PyInt_Check(value)) {
887 PyErr_SetString(PyExc_ValueError, "a maximum of one argument must"
891 on = PyInt_AsLong(value);
893 nerr = acl_add_perm(self->permset, *(acl_perm_t*)arg);
895 nerr = acl_delete_perm(self->permset, *(acl_perm_t*)arg);
897 PyErr_SetFromErrno(PyExc_IOError);
903 static char __Permset_add_doc__[] =
904 "Add a permission to the permission set.\n"
906 "The add() function adds the permission contained in \n"
907 "the argument perm to the permission set. An attempt \n"
908 "to add a permission that is already contained in the \n"
909 "permission set is not considered an error.\n"
911 " - perm a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...\n"
914 "Can raise: IOError\n"
917 static PyObject* Permset_add(PyObject* obj, PyObject* args) {
918 Permset_Object *self = (Permset_Object*) obj;
921 if (!PyArg_ParseTuple(args, "i", &right))
924 if(acl_add_perm(self->permset, (acl_perm_t) right) == -1)
925 return PyErr_SetFromErrno(PyExc_IOError);
927 /* Return the result */
932 static char __Permset_delete_doc__[] =
933 "Delete a permission from the permission set.\n"
935 "The delete() function deletes the permission contained in \n"
936 "the argument perm from the permission set. An attempt \n"
937 "to delete a permission that is not contained in the \n"
938 "permission set is not considered an error.\n"
940 " - perm a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...\n"
943 "Can raise: IOError\n"
946 static PyObject* Permset_delete(PyObject* obj, PyObject* args) {
947 Permset_Object *self = (Permset_Object*) obj;
950 if (!PyArg_ParseTuple(args, "i", &right))
953 if(acl_delete_perm(self->permset, (acl_perm_t) right) == -1)
954 return PyErr_SetFromErrno(PyExc_IOError);
956 /* Return the result */
961 static char __Permset_test_doc__[] =
962 "Test if a permission exists in the permission set.\n"
964 "The test() function tests if the permission contained in \n"
965 "the argument perm exits the permission set.\n"
967 " - perm a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...\n"
970 "Can raise: IOError\n"
973 static PyObject* Permset_test(PyObject* obj, PyObject* args) {
974 Permset_Object *self = (Permset_Object*) obj;
978 if (!PyArg_ParseTuple(args, "i", &right))
981 ret = get_perm(self->permset, (acl_perm_t) right);
983 return PyErr_SetFromErrno(PyExc_IOError);
996 static char __ACL_Type_doc__[] =
997 "Type which represents a POSIX ACL\n"
1000 " Only one keword parameter should be provided:\n"
1001 " - file=\"...\", meaning create ACL representing\n"
1002 " the access ACL of that file\n"
1003 " - filedef=\"...\", meaning create ACL representing\n"
1004 " the default ACL of that directory\n"
1005 " - fd=<int>, meaning create ACL representing\n"
1006 " the access ACL of that file descriptor\n"
1007 " - text=\"...\", meaning create ACL from a \n"
1008 " textual description\n"
1009 " - acl=<ACL instance>, meaning create a copy\n"
1010 " of an existing ACL instance\n"
1011 " - mode=<int>, meaning create an ACL from a numeric mode\n"
1012 " (e.g. mode=0644) (this is valid only when the C library\n"
1013 " provides the acl_from_mode call)\n"
1014 "If no parameters are passed, create an empty ACL; this\n"
1015 "makes sense only when your OS supports ACL modification\n"
1016 " (i.e. it implements full POSIX.1e support)\n"
1019 /* ACL type methods */
1020 static PyMethodDef ACL_methods[] = {
1021 {"applyto", ACL_applyto, METH_VARARGS, __applyto_doc__},
1022 {"valid", ACL_valid, METH_NOARGS, __valid_doc__},
1024 {"to_any_text", (PyCFunction)ACL_to_any_text, METH_VARARGS | METH_KEYWORDS,
1025 __to_any_text_doc__},
1026 {"check", ACL_check, METH_NOARGS, __check_doc__},
1029 {"__getstate__", ACL_get_state, METH_NOARGS,
1030 "Dumps the ACL to an external format."},
1031 {"__setstate__", ACL_set_state, METH_VARARGS,
1032 "Loads the ACL from an external format."},
1033 {"delete_entry", ACL_delete_entry, METH_VARARGS, __ACL_delete_entry_doc__},
1034 {"calc_mask", ACL_calc_mask, METH_NOARGS, __ACL_calc_mask_doc__},
1035 {"append", ACL_append, METH_VARARGS, __ACL_append_doc__},
1037 {NULL, NULL, 0, NULL}
1041 /* The definition of the ACL Type */
1042 static PyTypeObject ACL_Type = {
1043 PyObject_HEAD_INIT(NULL)
1048 ACL_dealloc, /* tp_dealloc */
1054 0, /* tp_as_number */
1055 0, /* tp_as_sequence */
1056 0, /* tp_as_mapping */
1059 ACL_str, /* tp_str */
1060 0, /* tp_getattro */
1061 0, /* tp_setattro */
1062 0, /* tp_as_buffer */
1063 Py_TPFLAGS_DEFAULT, /* tp_flags */
1064 __ACL_Type_doc__, /* tp_doc */
1065 0, /* tp_traverse */
1067 0, /* tp_richcompare */
1068 0, /* tp_weaklistoffset */
1074 0, /* tp_iternext */
1076 ACL_methods, /* tp_methods */
1081 0, /* tp_descr_get */
1082 0, /* tp_descr_set */
1083 0, /* tp_dictoffset */
1084 ACL_init, /* tp_init */
1086 ACL_new, /* tp_new */
1091 /* Entry type methods */
1092 static PyMethodDef Entry_methods[] = {
1093 {"copy", Entry_copy, METH_VARARGS, __Entry_copy_doc__},
1094 {NULL, NULL, 0, NULL}
1097 static char __Entry_tagtype_doc__[] =
1098 "The tag type of the current entry\n"
1101 " - ACL_UNDEFINED_TAG\n"
1104 " - ACL_GROUP_OBJ\n"
1110 static char __Entry_qualifier_doc__[] =
1111 "The qualifier of the current entry\n"
1113 "If the tag type is ACL_USER, this should be a user id.\n"
1114 "If the tag type if ACL_GROUP, this should be a group id.\n"
1115 "Else, it doesn't matter.\n"
1118 static char __Entry_parent_doc__[] =
1119 "The parent ACL of this entry\n"
1122 static char __Entry_permset_doc__[] =
1123 "The permission set of this ACL entry\n"
1127 static PyGetSetDef Entry_getsets[] = {
1128 {"tag_type", Entry_get_tag_type, Entry_set_tag_type,
1129 __Entry_tagtype_doc__},
1130 {"qualifier", Entry_get_qualifier, Entry_set_qualifier,
1131 __Entry_qualifier_doc__},
1132 {"parent", Entry_get_parent, NULL, __Entry_parent_doc__},
1133 {"permset", Entry_get_permset, Entry_set_permset, __Entry_permset_doc__},
1137 static char __Entry_Type_doc__[] =
1138 "Type which represents an entry in an ACL.\n"
1140 "The type exists only if the OS has full support for POSIX.1e\n"
1141 "Can be created either by:\n"
1142 " e = posix1e.Entry(myACL) # this creates a new entry in the ACL\n"
1144 " for entry in myACL:\n"
1147 "Note that the Entry keeps a reference to its ACL, so even if \n"
1148 "you delete the ACL, it won't be cleaned up and will continue to \n"
1149 "exist until its Entry(ies) will be deleted.\n"
1151 /* The definition of the Entry Type */
1152 static PyTypeObject Entry_Type = {
1153 PyObject_HEAD_INIT(NULL)
1156 sizeof(Entry_Object),
1158 Entry_dealloc, /* tp_dealloc */
1164 0, /* tp_as_number */
1165 0, /* tp_as_sequence */
1166 0, /* tp_as_mapping */
1169 Entry_str, /* tp_str */
1170 0, /* tp_getattro */
1171 0, /* tp_setattro */
1172 0, /* tp_as_buffer */
1173 Py_TPFLAGS_DEFAULT, /* tp_flags */
1174 __Entry_Type_doc__, /* tp_doc */
1175 0, /* tp_traverse */
1177 0, /* tp_richcompare */
1178 0, /* tp_weaklistoffset */
1180 0, /* tp_iternext */
1181 Entry_methods, /* tp_methods */
1183 Entry_getsets, /* tp_getset */
1186 0, /* tp_descr_get */
1187 0, /* tp_descr_set */
1188 0, /* tp_dictoffset */
1189 Entry_init, /* tp_init */
1191 Entry_new, /* tp_new */
1194 /* Permset type methods */
1195 static PyMethodDef Permset_methods[] = {
1196 {"clear", Permset_clear, METH_NOARGS, __Permset_clear_doc__, },
1197 {"add", Permset_add, METH_VARARGS, __Permset_add_doc__, },
1198 {"delete", Permset_delete, METH_VARARGS, __Permset_delete_doc__, },
1199 {"test", Permset_test, METH_VARARGS, __Permset_test_doc__, },
1200 {NULL, NULL, 0, NULL}
1203 static char __Permset_execute_doc__[] =
1204 "Execute permsission\n"
1206 "This is a convenience method of access; the \n"
1207 "same effect can be achieved using the functions\n"
1208 "add(), test(), delete(), and those can take any \n"
1209 "permission defined by your platform.\n"
1212 static char __Permset_read_doc__[] =
1213 "Read permsission\n"
1215 "This is a convenience method of access; the \n"
1216 "same effect can be achieved using the functions\n"
1217 "add(), test(), delete(), and those can take any \n"
1218 "permission defined by your platform.\n"
1221 static char __Permset_write_doc__[] =
1222 "Write permsission\n"
1224 "This is a convenience method of access; the \n"
1225 "same effect can be achieved using the functions\n"
1226 "add(), test(), delete(), and those can take any \n"
1227 "permission defined by your platform.\n"
1230 /* Permset getset */
1231 static PyGetSetDef Permset_getsets[] = {
1232 {"execute", Permset_get_right, Permset_set_right,
1233 __Permset_execute_doc__, &holder_ACL_EXECUTE},
1234 {"read", Permset_get_right, Permset_set_right,
1235 __Permset_read_doc__, &holder_ACL_READ},
1236 {"write", Permset_get_right, Permset_set_right,
1237 __Permset_write_doc__, &holder_ACL_WRITE},
1241 static char __Permset_Type_doc__[] =
1242 "Type which represents the permission set in an ACL entry\n"
1244 "The type exists only if the OS has full support for POSIX.1e\n"
1245 "Can be created either by:\n"
1246 " perms = myEntry.permset\n"
1248 " perms = posix1e.Permset(myEntry)\n"
1250 "Note that the Permset keeps a reference to its Entry, so even if \n"
1251 "you delete the entry, it won't be cleaned up and will continue to \n"
1252 "exist until its Permset will be deleted.\n"
1255 /* The definition of the Permset Type */
1256 static PyTypeObject Permset_Type = {
1257 PyObject_HEAD_INIT(NULL)
1260 sizeof(Permset_Object),
1262 Permset_dealloc, /* tp_dealloc */
1268 0, /* tp_as_number */
1269 0, /* tp_as_sequence */
1270 0, /* tp_as_mapping */
1273 Permset_str, /* tp_str */
1274 0, /* tp_getattro */
1275 0, /* tp_setattro */
1276 0, /* tp_as_buffer */
1277 Py_TPFLAGS_DEFAULT, /* tp_flags */
1278 __Permset_Type_doc__,/* tp_doc */
1279 0, /* tp_traverse */
1281 0, /* tp_richcompare */
1282 0, /* tp_weaklistoffset */
1284 0, /* tp_iternext */
1285 Permset_methods, /* tp_methods */
1287 Permset_getsets, /* tp_getset */
1290 0, /* tp_descr_get */
1291 0, /* tp_descr_set */
1292 0, /* tp_dictoffset */
1293 Permset_init, /* tp_init */
1295 Permset_new, /* tp_new */
1300 /* Module methods */
1302 static char __deletedef_doc__[] =
1303 "Delete the default ACL from a directory.\n"
1305 "This function deletes the default ACL associated with \n"
1306 "a directory (the ACL which will be ANDed with the mode\n"
1307 "parameter to the open, creat functions).\n"
1309 " - a string representing the directory whose default ACL\n"
1310 " should be deleted\n"
1313 /* Deletes the default ACL from a directory */
1314 static PyObject* aclmodule_delete_default(PyObject* obj, PyObject* args) {
1317 /* Parse the arguments */
1318 if (!PyArg_ParseTuple(args, "s", &filename))
1321 if(acl_delete_def_file(filename) == -1) {
1322 return PyErr_SetFromErrno(PyExc_IOError);
1325 /* Return the result */
1330 /* The module methods */
1331 static PyMethodDef aclmodule_methods[] = {
1332 {"delete_default", aclmodule_delete_default, METH_VARARGS,
1334 {NULL, NULL, 0, NULL}
1337 static char __posix1e_doc__[] =
1338 "POSIX.1e ACLs manipulation\n"
1340 "This module provides support for manipulating POSIX.1e ACLS\n"
1342 "Depending on the operating system support for POSIX.1e, \n"
1343 "the ACL type will have more or less capabilities:\n"
1344 " - level 1, only basic support, you can create\n"
1345 " ACLs from files and text descriptions;\n"
1346 " once created, the type is immutable\n"
1347 " - level 2, complete support, you can alter\n"
1348 " the ACL once it is created\n"
1350 "Also, in level 2, more types are available, corresponding\n"
1351 "to acl_entry_t (Entry type), acl_permset_t (Permset type).\n"
1354 ">>> import posix1e\n"
1355 ">>> acl1 = posix1e.ACL(file=\"file.txt\") \n"
1361 ">>> b = posix1e.ACL(text=\"u::rx,g::-,o::-\")\n"
1367 ">>> b.applyto(\"file.txt\")\n"
1368 ">>> print posix1e.ACL(file=\"file.txt\")\n"
1376 void initposix1e(void) {
1379 ACL_Type.ob_type = &PyType_Type;
1380 if(PyType_Ready(&ACL_Type) < 0)
1384 Entry_Type.ob_type = &PyType_Type;
1385 if(PyType_Ready(&Entry_Type) < 0)
1388 Permset_Type.ob_type = &PyType_Type;
1389 if(PyType_Ready(&Permset_Type) < 0)
1393 m = Py_InitModule3("posix1e", aclmodule_methods, __posix1e_doc__);
1395 d = PyModule_GetDict(m);
1399 Py_INCREF(&ACL_Type);
1400 if (PyDict_SetItemString(d, "ACL",
1401 (PyObject *) &ACL_Type) < 0)
1404 /* 23.3.6 acl_type_t values */
1405 PyModule_AddIntConstant(m, "ACL_TYPE_ACCESS", ACL_TYPE_ACCESS);
1406 PyModule_AddIntConstant(m, "ACL_TYPE_DEFAULT", ACL_TYPE_DEFAULT);
1410 Py_INCREF(&Entry_Type);
1411 if (PyDict_SetItemString(d, "Entry",
1412 (PyObject *) &Entry_Type) < 0)
1415 Py_INCREF(&Permset_Type);
1416 if (PyDict_SetItemString(d, "Permset",
1417 (PyObject *) &Permset_Type) < 0)
1420 /* 23.2.2 acl_perm_t values */
1421 PyModule_AddIntConstant(m, "ACL_READ", ACL_READ);
1422 PyModule_AddIntConstant(m, "ACL_WRITE", ACL_WRITE);
1423 PyModule_AddIntConstant(m, "ACL_EXECUTE", ACL_EXECUTE);
1425 /* 23.2.5 acl_tag_t values */
1426 PyModule_AddIntConstant(m, "ACL_UNDEFINED_TAG", ACL_UNDEFINED_TAG);
1427 PyModule_AddIntConstant(m, "ACL_USER_OBJ", ACL_USER_OBJ);
1428 PyModule_AddIntConstant(m, "ACL_USER", ACL_USER);
1429 PyModule_AddIntConstant(m, "ACL_GROUP_OBJ", ACL_GROUP_OBJ);
1430 PyModule_AddIntConstant(m, "ACL_GROUP", ACL_GROUP);
1431 PyModule_AddIntConstant(m, "ACL_MASK", ACL_MASK);
1432 PyModule_AddIntConstant(m, "ACL_OTHER", ACL_OTHER);
1437 /* Linux libacl specific acl_to_any_text constants */
1438 PyModule_AddIntConstant(m, "TEXT_ABBREVIATE", TEXT_ABBREVIATE);
1439 PyModule_AddIntConstant(m, "TEXT_NUMERIC_IDS", TEXT_NUMERIC_IDS);
1440 PyModule_AddIntConstant(m, "TEXT_SOME_EFFECTIVE", TEXT_SOME_EFFECTIVE);
1441 PyModule_AddIntConstant(m, "TEXT_ALL_EFFECTIVE", TEXT_ALL_EFFECTIVE);
1442 PyModule_AddIntConstant(m, "TEXT_SMART_INDENT", TEXT_SMART_INDENT);
1444 /* Linux libacl specific acl_check constants */
1445 PyModule_AddIntConstant(m, "ACL_MULTI_ERROR", ACL_MULTI_ERROR);
1446 PyModule_AddIntConstant(m, "ACL_DUPLICATE_ERROR", ACL_DUPLICATE_ERROR);
1447 PyModule_AddIntConstant(m, "ACL_MISS_ERROR", ACL_MISS_ERROR);
1448 PyModule_AddIntConstant(m, "ACL_ENTRY_ERROR", ACL_ENTRY_ERROR);