6 staticforward PyTypeObject ACL_Type;
7 static PyObject* ACL_applyto(PyObject* obj, PyObject* args);
8 static PyObject* ACL_valid(PyObject* obj, PyObject* args);
10 static PyObject* ACL_get_state(PyObject *obj, PyObject* args);
11 static PyObject* ACL_set_state(PyObject *obj, PyObject* args);
13 staticforward PyTypeObject Entry_Type;
14 staticforward PyTypeObject Permset_Type;
27 PyObject *parent; /* The parent acl, so it won't run out on us */
33 PyObject *parent; /* The parent entry, so it won't run out on us */
34 acl_permset_t permset;
39 /* Creation of a new ACL instance */
40 static PyObject* ACL_new(PyTypeObject* type, PyObject* args, PyObject *keywds) {
43 newacl = type->tp_alloc(type, 0);
46 ((ACL_Object*)newacl)->acl = NULL;
47 ((ACL_Object*)newacl)->entry_id = ACL_FIRST_ENTRY;
53 /* Initialization of a new ACL instance */
54 static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
55 ACL_Object* self = (ACL_Object*) obj;
56 static char *kwlist[] = { "file", "fd", "text", "acl", NULL };
60 ACL_Object* thesrc = NULL;
63 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sisO!", kwlist,
64 &file, &fd, &text, &ACL_Type, &thesrc))
76 PyErr_SetString(PyExc_ValueError, "a maximum of one argument must be passed");
80 /* Free the old acl_t without checking for error, we don't
86 self->acl = acl_get_file(file, ACL_TYPE_ACCESS);
88 self->acl = acl_from_text(text);
90 self->acl = acl_get_fd(fd);
91 else if(thesrc != NULL)
92 self->acl = acl_dup(thesrc->acl);
94 self->acl = acl_init(0);
96 if(self->acl == NULL) {
97 PyErr_SetFromErrno(PyExc_IOError);
104 /* Standard type functions */
105 static void ACL_dealloc(PyObject* obj) {
106 ACL_Object *self = (ACL_Object*) obj;
107 PyObject *err_type, *err_value, *err_traceback;
108 int have_error = PyErr_Occurred() ? 1 : 0;
111 PyErr_Fetch(&err_type, &err_value, &err_traceback);
112 if(acl_free(self->acl) != 0)
113 PyErr_WriteUnraisable(obj);
115 PyErr_Restore(err_type, err_value, err_traceback);
119 /* Converts the acl to a text format */
120 static PyObject* ACL_repr(PyObject *obj) {
122 ACL_Object *self = (ACL_Object*) obj;
125 text = acl_to_text(self->acl, NULL);
127 return PyErr_SetFromErrno(PyExc_IOError);
129 ret = PyString_FromString(text);
130 if(acl_free(text) != 0) {
132 return PyErr_SetFromErrno(PyExc_IOError);
138 static char __applyto_doc__[] = \
139 "Apply the ACL to a file or filehandle.\n" \
142 " - either a filename or a file-like object or an integer; this\n" \
143 " represents the filesystem object on which to act\n" \
146 /* Applyes the ACL to a file */
147 static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
148 ACL_Object *self = (ACL_Object*) obj;
150 int type_default = 0;
151 acl_type_t type = ACL_TYPE_ACCESS;
155 if (!PyArg_ParseTuple(args, "O|i", &myarg, &type_default))
158 type = ACL_TYPE_DEFAULT;
160 if(PyString_Check(myarg)) {
161 char *filename = PyString_AS_STRING(myarg);
162 nret = acl_set_file(filename, type, self->acl);
163 } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
164 nret = acl_set_fd(fd, self->acl);
166 PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int, or file-like object");
170 return PyErr_SetFromErrno(PyExc_IOError);
173 /* Return the result */
178 static char __valid_doc__[] = \
179 "Test the ACL for validity.\n" \
181 "This method tests the ACL to see if it is a valid ACL\n" \
182 "in terms of the filesystem. More precisely, it checks:\n" \
183 "A valid ACL contains exactly one entry with each of the ACL_USER_OBJ,\n" \
184 "ACL_GROUP_OBJ, and ACL_OTHER tag types. Entries with ACL_USER and\n" \
185 "ACL_GROUP tag types may appear zero or more times in an ACL. An ACL that\n" \
186 "contains entries of ACL_USER or ACL_GROUP tag types must contain exactly\n" \
187 "one entry of the ACL_MASK tag type. If an ACL contains no entries of\n" \
188 "ACL_USER or ACL_GROUP tag types, the ACL_MASK entry is optional.\n" \
190 "All user ID qualifiers must be unique among all entries of ACL_USER tag\n" \
191 "type, and all group IDs must be unique among all entries of ACL_GROUP tag\n" \
195 /* Checks the ACL for validity */
196 static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
197 ACL_Object *self = (ACL_Object*) obj;
199 if(acl_valid(self->acl) == -1) {
200 return PyErr_SetFromErrno(PyExc_IOError);
203 /* Return the result */
210 static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
211 ACL_Object *self = (ACL_Object*) obj;
216 size = acl_size(self->acl);
218 return PyErr_SetFromErrno(PyExc_IOError);
220 if((ret = PyString_FromStringAndSize(NULL, size)) == NULL)
222 buf = PyString_AsString(ret);
224 if((nsize = acl_copy_ext(buf, self->acl, size)) == -1) {
226 return PyErr_SetFromErrno(PyExc_IOError);
232 static PyObject* ACL_set_state(PyObject *obj, PyObject* args) {
233 ACL_Object *self = (ACL_Object*) obj;
238 /* Parse the argument */
239 if (!PyArg_ParseTuple(args, "s#", &buf, &bufsize))
242 /* Try to import the external representation */
243 if((ptr = acl_copy_int(buf)) == NULL)
244 return PyErr_SetFromErrno(PyExc_IOError);
246 /* Free the old acl. Should we ignore errors here? */
247 if(self->acl != NULL) {
248 if(acl_free(self->acl) == -1)
249 return PyErr_SetFromErrno(PyExc_IOError);
254 /* Return the result */
259 static PyObject* ACL_iter(PyObject *obj) {
260 ACL_Object *self = (ACL_Object*)obj;
261 self->entry_id = ACL_FIRST_ENTRY;
266 static PyObject* ACL_iternext(PyObject *obj) {
267 ACL_Object *self = (ACL_Object*)obj;
268 acl_entry_t the_entry_t;
269 Entry_Object *the_entry_obj;
272 if((nerr = acl_get_entry(self->acl, self->entry_id, &the_entry_t)) == -1)
273 return PyErr_SetFromErrno(PyExc_IOError);
274 self->entry_id = ACL_NEXT_ENTRY;
276 PyErr_SetObject(PyExc_StopIteration, Py_None);
280 the_entry_obj = (Entry_Object*) PyType_GenericNew(&Entry_Type, NULL, NULL);
281 if(the_entry_obj == NULL)
284 the_entry_obj->entry = the_entry_t;
286 the_entry_obj->parent = obj;
287 Py_INCREF(obj); /* For the reference we have in entry->parent */
289 return (PyObject*)the_entry_obj;
292 /* Creation of a new Entry instance */
293 static PyObject* Entry_new(PyTypeObject* type, PyObject* args, PyObject *keywds) {
296 newentry = PyType_GenericNew(type, args, keywds);
298 if(newentry != NULL) {
299 ((Entry_Object*)newentry)->entry = NULL;
300 ((Entry_Object*)newentry)->parent = NULL;
306 /* Initialization of a new Entry instance */
307 static int Entry_init(PyObject* obj, PyObject* args, PyObject *keywds) {
308 Entry_Object* self = (Entry_Object*) obj;
309 ACL_Object* parent = NULL;
311 if (!PyArg_ParseTuple(args, "O!", &ACL_Type, &parent))
314 if(acl_create_entry(&parent->acl, &self->entry) == -1) {
315 PyErr_SetFromErrno(PyExc_IOError);
319 self->parent = (PyObject*)parent;
325 /* Free the Entry instance */
326 static void Entry_dealloc(PyObject* obj) {
327 Entry_Object *self = (Entry_Object*) obj;
328 PyObject *err_type, *err_value, *err_traceback;
329 int have_error = PyErr_Occurred() ? 1 : 0;
332 PyErr_Fetch(&err_type, &err_value, &err_traceback);
333 if(self->parent != NULL) {
334 Py_DECREF(self->parent);
338 PyErr_Restore(err_type, err_value, err_traceback);
342 static int Entry_set_tag_type(PyObject* obj, PyObject* value, void* arg) {
343 Entry_Object *self = (Entry_Object*) obj;
346 PyErr_SetString(PyExc_TypeError,
347 "tag type deletion is not supported");
351 if(!PyInt_Check(value)) {
352 PyErr_SetString(PyExc_TypeError,
353 "tag type must be integer");
356 if(acl_set_tag_type(self->entry, (acl_tag_t)PyInt_AsLong(value)) == -1) {
357 PyErr_SetFromErrno(PyExc_IOError);
364 static PyObject* Entry_get_tag_type(PyObject *obj, void* arg) {
365 Entry_Object *self = (Entry_Object*) obj;
368 if (self->entry == NULL) {
369 PyErr_SetString(PyExc_AttributeError, "entry attribute");
372 if(acl_get_tag_type(self->entry, &value) == -1) {
373 PyErr_SetFromErrno(PyExc_IOError);
377 return PyInt_FromLong(value);
380 static int Entry_set_qualifier(PyObject* obj, PyObject* value, void* arg) {
381 Entry_Object *self = (Entry_Object*) obj;
385 PyErr_SetString(PyExc_TypeError,
386 "qualifier deletion is not supported");
390 if(!PyInt_Check(value)) {
391 PyErr_SetString(PyExc_TypeError,
392 "tag type must be integer");
395 uidgid = PyInt_AsLong(value);
396 if(acl_set_qualifier(self->entry, (void*)&uidgid) == -1) {
397 PyErr_SetFromErrno(PyExc_IOError);
404 static PyObject* Entry_get_qualifier(PyObject *obj, void* arg) {
405 Entry_Object *self = (Entry_Object*) obj;
409 if (self->entry == NULL) {
410 PyErr_SetString(PyExc_AttributeError, "entry attribute");
413 if((p = acl_get_qualifier(self->entry)) == NULL) {
414 PyErr_SetFromErrno(PyExc_IOError);
419 return PyInt_FromLong(value);
422 static PyObject* Entry_get_parent(PyObject *obj, void* arg) {
423 Entry_Object *self = (Entry_Object*) obj;
425 Py_INCREF(self->parent);
431 static char __acltype_doc__[] = \
432 "Type which represents a POSIX ACL\n" \
435 " Only one keword parameter should be provided:\n"
436 " - file=\"...\", meaning create ACL representing\n"
437 " the ACL of that file\n" \
438 " - fd=<int>, meaning create ACL representing\n" \
439 " the ACL of that file descriptor\n" \
440 " - text=\"...\", meaning create ACL from a \n" \
441 " textual description\n" \
442 " - acl=<ACL instance>, meaning create a copy\n" \
443 " of an existing ACL instance\n" \
446 /* ACL type methods */
447 static PyMethodDef ACL_methods[] = {
448 {"applyto", ACL_applyto, METH_VARARGS, __applyto_doc__},
449 {"valid", ACL_valid, METH_NOARGS, __valid_doc__},
451 {"__getstate__", ACL_get_state, METH_NOARGS, "Dumps the ACL to an external format."},
452 {"__setstate__", ACL_set_state, METH_VARARGS, "Loads the ACL from an external format."},
454 {NULL, NULL, 0, NULL}
458 /* The definition of the ACL Type */
459 static PyTypeObject ACL_Type = {
460 PyObject_HEAD_INIT(NULL)
465 ACL_dealloc, /* tp_dealloc */
470 ACL_repr, /* tp_repr */
471 0, /* tp_as_number */
472 0, /* tp_as_sequence */
473 0, /* tp_as_mapping */
479 0, /* tp_as_buffer */
480 Py_TPFLAGS_DEFAULT, /* tp_flags */
481 __acltype_doc__, /* tp_doc */
484 0, /* tp_richcompare */
485 0, /* tp_weaklistoffset */
493 ACL_methods, /* tp_methods */
498 0, /* tp_descr_get */
499 0, /* tp_descr_set */
500 0, /* tp_dictoffset */
501 ACL_init, /* tp_init */
503 ACL_new, /* tp_new */
508 /* Entry type methods */
509 static PyMethodDef Entry_methods[] = {
510 {NULL, NULL, 0, NULL}
513 static char __Entry_tagtype_doc__[] = \
514 "The tag type of the current entry\n" \
516 "This is one of:\n" \
517 " - ACL_UNDEFINED_TAG\n" \
518 " - ACL_USER_OBJ\n" \
520 " - ACL_GROUP_OBJ\n" \
526 static char __Entry_qualifier_doc__[] = \
527 "The qualifier of the current entry\n" \
529 "If the tag type is ACL_USER, this should be a user id.\n" \
530 "If the tag type if ACL_GROUP, this should be a group id.\n" \
531 "Else, it doesn't matter.\n" \
534 static char __Entry_parent_doc__[] = \
535 "The parent ACL of this entry\n" \
539 static PyGetSetDef Entry_getsets[] = {
540 {"tag_type", Entry_get_tag_type, Entry_set_tag_type, __Entry_tagtype_doc__},
541 {"qualifier", Entry_get_qualifier, Entry_set_qualifier, __Entry_qualifier_doc__},
542 {"parent", Entry_get_parent, NULL, __Entry_parent_doc__},
546 /* The definition of the ACL Entry Type */
547 static PyTypeObject Entry_Type = {
548 PyObject_HEAD_INIT(NULL)
551 sizeof(Entry_Object),
553 Entry_dealloc, /* tp_dealloc */
558 0, //Entry_repr, /* tp_repr */
559 0, /* tp_as_number */
560 0, /* tp_as_sequence */
561 0, /* tp_as_mapping */
567 0, /* tp_as_buffer */
568 Py_TPFLAGS_DEFAULT, /* tp_flags */
569 __acltype_doc__, /* tp_doc */
572 0, /* tp_richcompare */
573 0, /* tp_weaklistoffset */
576 Entry_methods, /* tp_methods */
578 Entry_getsets, /* tp_getset */
581 0, /* tp_descr_get */
582 0, /* tp_descr_set */
583 0, /* tp_dictoffset */
584 Entry_init, /* tp_init */
586 Entry_new, /* tp_new */
593 static char __deletedef_doc__[] = \
594 "Delete the default ACL from a directory.\n" \
596 "This function deletes the default ACL associated with \n" \
597 "a directory (the ACL which will be ANDed with the mode\n" \
598 "parameter to the open, creat functions).\n" \
600 " - a string representing the directory whose default ACL\n" \
601 " should be deleted\n" \
604 /* Deletes the default ACL from a directory */
605 static PyObject* aclmodule_delete_default(PyObject* obj, PyObject* args) {
608 /* Parse the arguments */
609 if (!PyArg_ParseTuple(args, "s", &filename))
612 if(acl_delete_def_file(filename) == -1) {
613 return PyErr_SetFromErrno(PyExc_IOError);
616 /* Return the result */
621 /* The module methods */
622 static PyMethodDef aclmodule_methods[] = {
623 {"delete_default", aclmodule_delete_default, METH_VARARGS, __deletedef_doc__},
624 {NULL, NULL, 0, NULL}
627 static char __posix1e_doc__[] = \
628 "POSIX.1e ACLs manipulation\n" \
630 "This module provides support for manipulating POSIX.1e ACLS\n" \
632 "Depending on the operating system support for POSIX.1e, \n" \
633 "the ACL type will have more or less capabilities:\n" \
634 " - level 1, only basic support, you can create\n" \
635 " ACLs from files and text descriptions;\n" \
636 " once created, the type is immutable\n" \
637 " - level 2, complete support, you can alter\n"\
638 " the ACL once it is created\n" \
640 "Also, in level 2, more types will be available, corresponding\n" \
641 "to acl_entry_t, acl_permset_t, etc.\n" \
644 ">>> import posix1e\n" \
645 ">>> acl1 = posix1e.ACL(file=\"file.txt\") \n" \
651 ">>> b = posix1e.ACL(text=\"u::rx,g::-,o::-\")\n" \
657 ">>> b.applyto(\"file.txt\")\n" \
658 ">>> print posix1e.ACL(file=\"file.txt\")\n" \
666 DL_EXPORT(void) initposix1e(void) {
669 ACL_Type.ob_type = &PyType_Type;
671 if(PyType_Ready(&ACL_Type) < 0)
675 Entry_Type.ob_type = &PyType_Type;
677 if(PyType_Ready(&Entry_Type) < 0)
681 m = Py_InitModule3("posix1e", aclmodule_methods, __posix1e_doc__);
683 d = PyModule_GetDict(m);
687 Py_INCREF(&ACL_Type);
688 if (PyDict_SetItemString(d, "ACL",
689 (PyObject *) &ACL_Type) < 0)
692 Py_INCREF(&Entry_Type);
693 if (PyDict_SetItemString(d, "Entry",
694 (PyObject *) &Entry_Type) < 0)
697 /* 23.2.2 acl_perm_t values */
698 PyModule_AddIntConstant(m, "ACL_READ", ACL_READ);
699 PyModule_AddIntConstant(m, "ACL_WRITE", ACL_WRITE);
700 PyModule_AddIntConstant(m, "ACL_EXECUTE", ACL_EXECUTE);
702 /* 23.2.5 acl_tag_t values */
703 PyModule_AddIntConstant(m, "ACL_UNDEFINED_TAG", ACL_UNDEFINED_TAG);
704 PyModule_AddIntConstant(m, "ACL_USER_OBJ", ACL_USER_OBJ);
705 PyModule_AddIntConstant(m, "ACL_USER", ACL_USER);
706 PyModule_AddIntConstant(m, "ACL_GROUP_OBJ", ACL_GROUP_OBJ);
707 PyModule_AddIntConstant(m, "ACL_GROUP", ACL_GROUP);
708 PyModule_AddIntConstant(m, "ACL_MASK", ACL_MASK);
709 PyModule_AddIntConstant(m, "ACL_OTHER", ACL_OTHER);
711 /* 23.3.6 acl_type_t values */
712 PyModule_AddIntConstant(m, "ACL_TYPE_ACCESS", ACL_TYPE_ACCESS);
713 PyModule_AddIntConstant(m, "ACL_TYPE_DEFAULT", ACL_TYPE_DEFAULT);