]> git.k1024.org Git - pylibacl.git/blob - acl.c
Unittest fixes for python 3
[pylibacl.git] / acl.c
1 /*
2     posix1e - a python module exposing the posix acl functions
3
4     Copyright (C) 2002-2008 Iustin Pop <iusty@k1024.org>
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19     02110-1301  USA
20
21 */
22
23 #include <Python.h>
24
25 #include <sys/types.h>
26 #include <sys/acl.h>
27
28 #ifdef HAVE_LINUX
29 #include <acl/libacl.h>
30 #define get_perm acl_get_perm
31 #elif HAVE_FREEBSD
32 #define get_perm acl_get_perm_np
33 #endif
34
35 #if PY_MAJOR_VERSION >= 3
36 #define IS_PY3K
37 #define PyInt_Check(op) PyLong_Check(op)
38 #define PyInt_FromString PyLong_FromString
39 #define PyInt_FromUnicode PyLong_FromUnicode
40 #define PyInt_FromLong PyLong_FromLong
41 #define PyInt_FromSize_t PyLong_FromSize_t
42 #define PyInt_FromSsize_t PyLong_FromSsize_t
43 #define PyInt_AsLong PyLong_AsLong
44 #define PyInt_AsSsize_t PyLong_AsSsize_t
45 #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
46 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
47 #define PyInt_AS_LONG PyLong_AS_LONG
48 #else
49 #define PyBytes_Check PyString_Check
50 #define PyBytes_AS_STRING PyString_AS_STRING
51 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
52 #define PyBytes_FromString PyString_FromString
53 #define PyBytes_FromFormat PyString_FromFormat
54 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
55
56 #define Py_TYPE(o)    (((PyObject*)(o))->ob_type)
57 #endif
58
59 static PyTypeObject ACL_Type;
60 static PyObject* ACL_applyto(PyObject* obj, PyObject* args);
61 static PyObject* ACL_valid(PyObject* obj, PyObject* args);
62
63 #ifdef HAVE_ACL_COPY_EXT
64 static PyObject* ACL_get_state(PyObject *obj, PyObject* args);
65 static PyObject* ACL_set_state(PyObject *obj, PyObject* args);
66 #endif
67
68 #ifdef HAVE_LEVEL2
69 static PyTypeObject Entry_Type;
70 static PyTypeObject Permset_Type;
71 static PyObject* Permset_new(PyTypeObject* type, PyObject* args,
72                              PyObject *keywds);
73 #endif
74
75 static acl_perm_t holder_ACL_EXECUTE = ACL_EXECUTE;
76 static acl_perm_t holder_ACL_READ = ACL_READ;
77 static acl_perm_t holder_ACL_WRITE = ACL_WRITE;
78
79 typedef struct {
80     PyObject_HEAD
81     acl_t acl;
82 #ifdef HAVE_LEVEL2
83     int entry_id;
84 #endif
85 } ACL_Object;
86
87 #ifdef HAVE_LEVEL2
88
89 typedef struct {
90     PyObject_HEAD
91     PyObject *parent_acl; /* The parent acl, so it won't run out on us */
92     acl_entry_t entry;
93 } Entry_Object;
94
95 typedef struct {
96     PyObject_HEAD
97     PyObject *parent_entry; /* The parent entry, so it won't run out on us */
98     acl_permset_t permset;
99 } Permset_Object;
100
101 #endif
102
103 /* Creation of a new ACL instance */
104 static PyObject* ACL_new(PyTypeObject* type, PyObject* args,
105                          PyObject *keywds) {
106     PyObject* newacl;
107
108     newacl = type->tp_alloc(type, 0);
109
110     if(newacl != NULL) {
111         ((ACL_Object*)newacl)->acl = NULL;
112 #ifdef HAVEL_LEVEL2
113         ((ACL_Object*)newacl)->entry_id = ACL_FIRST_ENTRY;
114 #endif
115     }
116
117     return newacl;
118 }
119
120 /* Initialization of a new ACL instance */
121 static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
122     ACL_Object* self = (ACL_Object*) obj;
123 #ifdef HAVE_LINUX
124     static char *kwlist[] = { "file", "fd", "text", "acl", "filedef",
125                               "mode", NULL };
126     char *format = "|sisO!sH";
127     mode_t mode = 0;
128 #else
129     static char *kwlist[] = { "file", "fd", "text", "acl", "filedef", NULL };
130     char *format = "|sisO!s";
131 #endif
132     char *file = NULL;
133     char *filedef = NULL;
134     char *text = NULL;
135     int fd = -1;
136     ACL_Object* thesrc = NULL;
137
138     if(!PyTuple_Check(args) || PyTuple_Size(args) != 0 ||
139        (keywds != NULL && PyDict_Check(keywds) && PyDict_Size(keywds) > 1)) {
140         PyErr_SetString(PyExc_ValueError, "a max of one keyword argument"
141                         " must be passed");
142         return -1;
143     }
144     if(!PyArg_ParseTupleAndKeywords(args, keywds, format, kwlist,
145                                     &file, &fd, &text, &ACL_Type,
146                                     &thesrc, &filedef
147 #ifdef HAVE_LINUX
148                                     , &mode
149 #endif
150                                     ))
151         return -1;
152
153     /* Free the old acl_t without checking for error, we don't
154      * care right now */
155     if(self->acl != NULL)
156         acl_free(self->acl);
157
158     if(file != NULL)
159         self->acl = acl_get_file(file, ACL_TYPE_ACCESS);
160     else if(text != NULL)
161         self->acl = acl_from_text(text);
162     else if(fd != -1)
163         self->acl = acl_get_fd(fd);
164     else if(thesrc != NULL)
165         self->acl = acl_dup(thesrc->acl);
166     else if(filedef != NULL)
167         self->acl = acl_get_file(filedef, ACL_TYPE_DEFAULT);
168 #ifdef HAVE_LINUX
169     else if(PyMapping_HasKeyString(keywds, kwlist[5]))
170         self->acl = acl_from_mode(mode);
171 #endif
172     else
173         self->acl = acl_init(0);
174
175     if(self->acl == NULL) {
176         PyErr_SetFromErrno(PyExc_IOError);
177         return -1;
178     }
179
180     return 0;
181 }
182
183 /* Standard type functions */
184 static void ACL_dealloc(PyObject* obj) {
185     ACL_Object *self = (ACL_Object*) obj;
186     PyObject *err_type, *err_value, *err_traceback;
187     int have_error = PyErr_Occurred() ? 1 : 0;
188
189     if (have_error)
190         PyErr_Fetch(&err_type, &err_value, &err_traceback);
191     if(self->acl != NULL && acl_free(self->acl) != 0)
192         PyErr_WriteUnraisable(obj);
193     if (have_error)
194         PyErr_Restore(err_type, err_value, err_traceback);
195     PyObject_DEL(self);
196 }
197
198 /* Converts the acl to a text format */
199 static PyObject* ACL_str(PyObject *obj) {
200     char *text;
201     ACL_Object *self = (ACL_Object*) obj;
202     PyObject *ret;
203
204     text = acl_to_text(self->acl, NULL);
205     if(text == NULL) {
206         return PyErr_SetFromErrno(PyExc_IOError);
207     }
208     ret = PyBytes_FromString(text);
209     if(acl_free(text) != 0) {
210         Py_DECREF(ret);
211         return PyErr_SetFromErrno(PyExc_IOError);
212     }
213     return ret;
214 }
215
216 #ifdef HAVE_LINUX
217 static char __to_any_text_doc__[] =
218   "Convert the ACL to a custom text format.\n"
219   "\n"
220   "This method encapsulates the acl_to_any_text function. It allows a \n"
221   "customized text format to be generated for the ACL. See\n"
222   "acl_to_any_text(3) for more details.\n"
223   "\n"
224   "Parameters:\n"
225   "  - prefix: if given, this string will be prepended to all lines\n"
226   "  - separator: a single character (defaults to '\\n'); this will be\n"
227   "               user to separate the entries in the ACL\n"
228   "  - options: a bitwise combination of:\n"
229   "    -  TEXT_ABBREVIATE: use 'u' instead of 'user', 'g' instead of \n"
230   "                       'group', etc.\n"
231   "    -  TEXT_NUMERIC_IDS: User and group IDs are included as decimal\n"
232   "                         numbers instead of names\n"
233   "    -  TEXT_SOME_EFFECTIVE: Include comments denoting the effective\n"
234   "                            permissions when some are masked\n"
235   "    -  TEXT_ALL_EFFECTIVE: Include comments after all ACL entries\n"
236   "                           affected by an ACL_MASK entry\n"
237   "    -  TEXT_SMART_INDENT: Used in combination with the _EFFECTIVE\n"
238   "                          options, this will ensure that comments \n"
239   "                          are alligned to the fourth tab position\n"
240   "                          (assuming one tab equals eight spaces)\n"
241   ;
242
243 /* Converts the acl to a custom text format */
244 static PyObject* ACL_to_any_text(PyObject *obj, PyObject *args,
245                                  PyObject *kwds) {
246     char *text;
247     ACL_Object *self = (ACL_Object*) obj;
248     PyObject *ret;
249     char *arg_prefix = NULL;
250     char arg_separator = '\n';
251     int arg_options = 0;
252     static char *kwlist[] = {"prefix", "separator", "options", NULL};
253
254     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sci", kwlist, &arg_prefix,
255                                      &arg_separator, &arg_options))
256       return NULL;
257
258     text = acl_to_any_text(self->acl, arg_prefix, arg_separator, arg_options);
259     if(text == NULL) {
260         return PyErr_SetFromErrno(PyExc_IOError);
261     }
262     ret = PyBytes_FromString(text);
263     if(acl_free(text) != 0) {
264         Py_DECREF(ret);
265         return PyErr_SetFromErrno(PyExc_IOError);
266     }
267     return ret;
268 }
269
270 static char __check_doc__[] =
271     "Check the ACL validity.\n"
272     "\n"
273     "This is a non-portable, Linux specific extension that allow more\n"
274     "information to be retrieved in case an ACL is not valid than the\n"
275     "validate() method.\n"
276     "\n"
277     "This method will return either False (the ACL is valid), or a tuple\n"
278     "with two elements. The first element is one of the following\n"
279     "constants:\n"
280     "  - ACL_MULTI_ERROR: The ACL contains multiple entries that have a\n"
281     "                     tag type that may occur at most once\n"
282     "  - ACL_DUPLICATE_ERROR: The ACL contains multiple ACL_USER or \n"
283     "                         ACL_GROUP entries  with the same ID\n"
284     "  - ACL_MISS_ERROR: A required entry is missing\n"
285     "  - ACL_ENTRY_ERROR: The ACL contains an invalid entry tag type\n"
286     "\n"
287     "The second element of the tuple is the index of the entry that is\n"
288     "invalid (in the same order as by iterating over the ACL entry)\n"
289     ;
290
291 /* The acl_check method */
292 static PyObject* ACL_check(PyObject* obj, PyObject* args) {
293     ACL_Object *self = (ACL_Object*) obj;
294     int result;
295     int eindex;
296
297     if((result = acl_check(self->acl, &eindex)) == -1)
298         return PyErr_SetFromErrno(PyExc_IOError);
299     if(result == 0) {
300         Py_INCREF(Py_False);
301         return Py_False;
302     }
303     return PyTuple_Pack(2, PyInt_FromLong(result), PyInt_FromLong(eindex));
304 }
305
306 /* Implementation of the rich compare for ACLs */
307 static PyObject* ACL_richcompare(PyObject* o1, PyObject* o2, int op) {
308     ACL_Object *acl1, *acl2;
309     int n;
310     PyObject *ret;
311
312     if(!PyObject_IsInstance(o2, (PyObject*)&ACL_Type)) {
313         if(op == Py_EQ)
314             Py_RETURN_FALSE;
315         if(op == Py_NE)
316             Py_RETURN_TRUE;
317         PyErr_SetString(PyExc_TypeError, "can only compare to an ACL");
318         return NULL;
319     }
320
321     acl1 = (ACL_Object*)o1;
322     acl2 = (ACL_Object*)o2;
323     if((n=acl_cmp(acl1->acl, acl2->acl))==-1)
324         return PyErr_SetFromErrno(PyExc_IOError);
325     switch(op) {
326     case Py_EQ:
327         ret = n == 0 ? Py_True : Py_False;
328         break;
329     case Py_NE:
330         ret = n == 1 ? Py_True : Py_False;
331         break;
332     default:
333         ret = Py_NotImplemented;
334     }
335     Py_INCREF(ret);
336     return ret;
337 }
338
339 static char __equiv_mode_doc__[] =
340     "Return the octal mode the ACL is equivalent to.\n"
341     "\n"
342     "This is a non-portable, Linux specific extension that checks\n"
343     "if the ACL is a basic ACL and returns the corresponding mode.\n"
344     "\n"
345     "An IOerror exception will be raised if the ACL is an extended ACL\n"
346     ;
347
348 /* The acl_equiv_mode method */
349 static PyObject* ACL_equiv_mode(PyObject* obj, PyObject* args) {
350     ACL_Object *self = (ACL_Object*) obj;
351     mode_t mode;
352
353     if(acl_equiv_mode(self->acl, &mode) == -1)
354         return PyErr_SetFromErrno(PyExc_IOError);
355     return PyInt_FromLong(mode);
356 }
357 #endif
358
359 /* Implementation of the compare for ACLs */
360 static int ACL_nocmp(PyObject* o1, PyObject* o2) {
361
362     PyErr_SetString(PyExc_TypeError, "cannot compare ACLs using cmp()");
363     return -1;
364 }
365
366 /* Custom methods */
367 static char __applyto_doc__[] =
368     "Apply the ACL to a file or filehandle.\n"
369     "\n"
370     "Parameters:\n"
371     "  - either a filename or a file-like object or an integer; this\n"
372     "    represents the filesystem object on which to act\n"
373     "  - optional flag representing the type of ACL to set, either\n"
374     "    ACL_TYPE_ACCESS (default) or ACL_TYPE_DEFAULT\n"
375     ;
376
377 /* Applyes the ACL to a file */
378 static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
379     ACL_Object *self = (ACL_Object*) obj;
380     PyObject *myarg;
381     acl_type_t type = ACL_TYPE_ACCESS;
382     int nret;
383     int fd;
384
385     if (!PyArg_ParseTuple(args, "O|i", &myarg, &type))
386         return NULL;
387
388     if(PyBytes_Check(myarg)) {
389         char *filename = PyBytes_AS_STRING(myarg);
390         nret = acl_set_file(filename, type, self->acl);
391     } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
392         nret = acl_set_fd(fd, self->acl);
393     } else {
394         PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int,"
395                         " or file-like object");
396         return 0;
397     }
398     if(nret == -1) {
399         return PyErr_SetFromErrno(PyExc_IOError);
400     }
401
402     /* Return the result */
403     Py_INCREF(Py_None);
404     return Py_None;
405 }
406
407 static char __valid_doc__[] =
408     "Test the ACL for validity.\n"
409     "\n"
410     "This method tests the ACL to see if it is a valid ACL\n"
411     "in terms of the filesystem. More precisely, it checks that:\n"
412     "\n"
413     "The ACL contains exactly one entry with each of the\n"
414     "ACL_USER_OBJ, ACL_GROUP_OBJ, and ACL_OTHER tag types. Entries\n"
415     "with ACL_USER and ACL_GROUP tag types may appear zero or more\n"
416     "times in an ACL. An ACL that contains entries of ACL_USER or\n"
417     "ACL_GROUP tag types must contain exactly one entry of the \n"
418     "ACL_MASK tag type. If an ACL contains no entries of\n"
419     "ACL_USER or ACL_GROUP tag types, the ACL_MASK entry is optional.\n"
420     "\n"
421     "All user ID qualifiers must be unique among all entries of\n"
422     "the ACL_USER tag type, and all group IDs must be unique among all\n"
423     "entries of ACL_GROUP tag type.\n"
424     "\n"
425     "The method will return 1 for a valid ACL and 0 for an invalid one.\n"
426     "This has been chosen because the specification for acl_valid in\n"
427     "the POSIX.1e standard documents only one possible value for errno\n"
428     "in case of an invalid ACL, so we can't differentiate between\n"
429     "classes of errors. Other suggestions are welcome.\n"
430     ;
431
432 /* Checks the ACL for validity */
433 static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
434     ACL_Object *self = (ACL_Object*) obj;
435
436     if(acl_valid(self->acl) == -1) {
437         Py_INCREF(Py_False);
438         return Py_False;
439     } else {
440         Py_INCREF(Py_True);
441         return Py_True;
442     }
443 }
444
445 #ifdef HAVE_ACL_COPY_EXT
446 static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
447     ACL_Object *self = (ACL_Object*) obj;
448     PyObject *ret;
449     ssize_t size, nsize;
450     char *buf;
451
452     size = acl_size(self->acl);
453     if(size == -1)
454         return PyErr_SetFromErrno(PyExc_IOError);
455
456     if((ret = PyBytes_FromStringAndSize(NULL, size)) == NULL)
457         return NULL;
458     buf = PyBytes_AsString(ret);
459
460     if((nsize = acl_copy_ext(buf, self->acl, size)) == -1) {
461         Py_DECREF(ret);
462         return PyErr_SetFromErrno(PyExc_IOError);
463     }
464
465     return ret;
466 }
467
468 static PyObject* ACL_set_state(PyObject *obj, PyObject* args) {
469     ACL_Object *self = (ACL_Object*) obj;
470     const void *buf;
471     int bufsize;
472     acl_t ptr;
473
474     /* Parse the argument */
475     if (!PyArg_ParseTuple(args, "s#", &buf, &bufsize))
476         return NULL;
477
478     /* Try to import the external representation */
479     if((ptr = acl_copy_int(buf)) == NULL)
480         return PyErr_SetFromErrno(PyExc_IOError);
481
482     /* Free the old acl. Should we ignore errors here? */
483     if(self->acl != NULL) {
484         if(acl_free(self->acl) == -1)
485             return PyErr_SetFromErrno(PyExc_IOError);
486     }
487
488     self->acl = ptr;
489
490     /* Return the result */
491     Py_INCREF(Py_None);
492     return Py_None;
493 }
494 #endif
495
496 #ifdef HAVE_LEVEL2
497
498 /* tp_iter for the ACL type; since it can be iterated only
499  * destructively, the type is its iterator
500  */
501 static PyObject* ACL_iter(PyObject *obj) {
502     ACL_Object *self = (ACL_Object*)obj;
503     self->entry_id = ACL_FIRST_ENTRY;
504     Py_INCREF(obj);
505     return obj;
506 }
507
508 /* the tp_iternext function for the ACL type */
509 static PyObject* ACL_iternext(PyObject *obj) {
510     ACL_Object *self = (ACL_Object*)obj;
511     acl_entry_t the_entry_t;
512     Entry_Object *the_entry_obj;
513     int nerr;
514
515     nerr = acl_get_entry(self->acl, self->entry_id, &the_entry_t);
516     self->entry_id = ACL_NEXT_ENTRY;
517     if(nerr == -1)
518         return PyErr_SetFromErrno(PyExc_IOError);
519     else if(nerr == 0) {
520         /* Docs says this is not needed */
521         /*PyErr_SetObject(PyExc_StopIteration, Py_None);*/
522         return NULL;
523     }
524
525     the_entry_obj = (Entry_Object*) PyType_GenericNew(&Entry_Type, NULL, NULL);
526     if(the_entry_obj == NULL)
527         return NULL;
528
529     the_entry_obj->entry = the_entry_t;
530
531     the_entry_obj->parent_acl = obj;
532     Py_INCREF(obj); /* For the reference we have in entry->parent */
533
534     return (PyObject*)the_entry_obj;
535 }
536
537 static char __ACL_delete_entry_doc__[] =
538     "Deletes an entry from the ACL.\n"
539     "\n"
540     "Note: Only with level 2\n"
541     "Parameters:\n"
542     "  - the Entry object which should be deleted; note that after\n"
543     "    this function is called, that object is unusable any longer\n"
544     "    and should be deleted\n"
545     ;
546
547 /* Deletes an entry from the ACL */
548 static PyObject* ACL_delete_entry(PyObject *obj, PyObject *args) {
549     ACL_Object *self = (ACL_Object*)obj;
550     Entry_Object *e;
551
552     if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &e))
553         return NULL;
554
555     if(acl_delete_entry(self->acl, e->entry) == -1)
556         return PyErr_SetFromErrno(PyExc_IOError);
557
558     /* Return the result */
559     Py_INCREF(Py_None);
560     return Py_None;
561 }
562
563 static char __ACL_calc_mask_doc__[] =
564     "Compute the file group class mask.\n"
565     "\n"
566     "The calc_mask() method calculates and sets the permissions \n"
567     "associated with the ACL_MASK Entry of the ACL.\n"
568     "The value of the new permissions is the union of the permissions \n"
569     "granted by all entries of tag type ACL_GROUP, ACL_GROUP_OBJ, or \n"
570     "ACL_USER.  If the ACL already contains an ACL_MASK entry, its \n"
571     "permissions are overwritten; if it does not contain an ACL_MASK \n"
572     "Entry, one is added.\n"
573     "\n"
574     "The order of existing entries in the ACL is undefined after this \n"
575     "function.\n"
576     ;
577
578 /* Updates the mask entry in the ACL */
579 static PyObject* ACL_calc_mask(PyObject *obj, PyObject *args) {
580     ACL_Object *self = (ACL_Object*)obj;
581
582     if(acl_calc_mask(&self->acl) == -1)
583         return PyErr_SetFromErrno(PyExc_IOError);
584
585     /* Return the result */
586     Py_INCREF(Py_None);
587     return Py_None;
588 }
589
590 static char __ACL_append_doc__[] =
591     "Append a new Entry to the ACL and return it.\n"
592     "\n"
593     "This is a convenience function to create a new Entry \n"
594     "and append it to the ACL.\n"
595     "If a parameter of type Entry instance is given, the \n"
596     "entry will be a copy of that one (as if copied with \n"
597     "Entry.copy()), otherwise, the new entry will be empty.\n"
598     ;
599
600 /* Convenience method to create a new Entry */
601 static PyObject* ACL_append(PyObject *obj, PyObject *args) {
602     ACL_Object* self = (ACL_Object*) obj;
603     Entry_Object* newentry;
604     Entry_Object* oldentry = NULL;
605     int nret;
606
607     newentry = (Entry_Object*)PyType_GenericNew(&Entry_Type, NULL, NULL);
608     if(newentry == NULL) {
609         return NULL;
610     }
611
612     if (!PyArg_ParseTuple(args, "|O!", &Entry_Type, &oldentry))
613         return NULL;
614
615     nret = acl_create_entry(&self->acl, &newentry->entry);
616     if(nret == -1) {
617         Py_DECREF(newentry);
618         return PyErr_SetFromErrno(PyExc_IOError);
619     }
620
621     if(oldentry != NULL) {
622         nret = acl_copy_entry(newentry->entry, oldentry->entry);
623         if(nret == -1) {
624             Py_DECREF(newentry);
625             return PyErr_SetFromErrno(PyExc_IOError);
626         }
627     }
628
629     newentry->parent_acl = obj;
630     Py_INCREF(obj);
631
632     return (PyObject*)newentry;
633 }
634
635 /***** Entry type *****/
636
637 /* Creation of a new Entry instance */
638 static PyObject* Entry_new(PyTypeObject* type, PyObject* args,
639                            PyObject *keywds) {
640     PyObject* newentry;
641
642     newentry = PyType_GenericNew(type, args, keywds);
643
644     if(newentry != NULL) {
645         ((Entry_Object*)newentry)->entry = NULL;
646         ((Entry_Object*)newentry)->parent_acl = NULL;
647     }
648
649     return newentry;
650 }
651
652 /* Initialization of a new Entry instance */
653 static int Entry_init(PyObject* obj, PyObject* args, PyObject *keywds) {
654     Entry_Object* self = (Entry_Object*) obj;
655     ACL_Object* parent = NULL;
656
657     if (!PyArg_ParseTuple(args, "O!", &ACL_Type, &parent))
658         return -1;
659
660     if(acl_create_entry(&parent->acl, &self->entry) == -1) {
661         PyErr_SetFromErrno(PyExc_IOError);
662         return -1;
663     }
664
665     self->parent_acl = (PyObject*)parent;
666     Py_INCREF(parent);
667
668     return 0;
669 }
670
671 /* Free the Entry instance */
672 static void Entry_dealloc(PyObject* obj) {
673     Entry_Object *self = (Entry_Object*) obj;
674     PyObject *err_type, *err_value, *err_traceback;
675     int have_error = PyErr_Occurred() ? 1 : 0;
676
677     if (have_error)
678         PyErr_Fetch(&err_type, &err_value, &err_traceback);
679     if(self->parent_acl != NULL) {
680         Py_DECREF(self->parent_acl);
681         self->parent_acl = NULL;
682     }
683     if (have_error)
684         PyErr_Restore(err_type, err_value, err_traceback);
685     PyObject_DEL(self);
686 }
687
688 /* Converts the entry to a text format */
689 static PyObject* Entry_str(PyObject *obj) {
690     acl_tag_t tag;
691     uid_t qualifier;
692     void *p;
693     PyObject *format, *kind;
694     Entry_Object *self = (Entry_Object*) obj;
695
696     if(acl_get_tag_type(self->entry, &tag) == -1) {
697         PyErr_SetFromErrno(PyExc_IOError);
698         return NULL;
699     }
700     if(tag == ACL_USER || tag == ACL_GROUP) {
701         if((p = acl_get_qualifier(self->entry)) == NULL) {
702             PyErr_SetFromErrno(PyExc_IOError);
703             return NULL;
704         }
705         qualifier = *(uid_t*)p;
706         acl_free(p);
707     } else {
708         qualifier = 0;
709     }
710
711     format = PyBytes_FromString("ACL entry for ");
712     if(format == NULL)
713         return NULL;
714     if(tag == ACL_UNDEFINED_TAG) {
715         kind = PyBytes_FromString("undefined type");
716     } else if(tag == ACL_USER_OBJ) {
717         kind = PyBytes_FromString("the owner");
718     } else if(tag == ACL_GROUP_OBJ) {
719         kind = PyBytes_FromString("the group");
720     } else if(tag == ACL_OTHER) {
721         kind = PyBytes_FromString("the others");
722     } else if(tag == ACL_USER) {
723         kind = PyBytes_FromFormat("user with uid %d", qualifier);
724     } else if(tag == ACL_GROUP) {
725         kind = PyBytes_FromFormat("group with gid %d", qualifier);
726     } else if(tag == ACL_MASK) {
727         kind = PyBytes_FromString("the mask");
728     } else {
729         kind = PyBytes_FromString("UNKNOWN_TAG_TYPE!");
730     }
731     if (kind == NULL)
732         return NULL;
733     PyBytes_ConcatAndDel(&format, kind);
734     Py_DECREF(format);
735     return format;
736 }
737
738 /* Sets the tag type of the entry */
739 static int Entry_set_tag_type(PyObject* obj, PyObject* value, void* arg) {
740     Entry_Object *self = (Entry_Object*) obj;
741
742     if(value == NULL) {
743         PyErr_SetString(PyExc_TypeError,
744                         "tag type deletion is not supported");
745         return -1;
746     }
747
748     if(!PyInt_Check(value)) {
749         PyErr_SetString(PyExc_TypeError,
750                         "tag type must be integer");
751         return -1;
752     }
753     if(acl_set_tag_type(self->entry, (acl_tag_t)PyInt_AsLong(value)) == -1) {
754         PyErr_SetFromErrno(PyExc_IOError);
755         return -1;
756     }
757
758     return 0;
759 }
760
761 /* Returns the tag type of the entry */
762 static PyObject* Entry_get_tag_type(PyObject *obj, void* arg) {
763     Entry_Object *self = (Entry_Object*) obj;
764     acl_tag_t value;
765
766     if (self->entry == NULL) {
767         PyErr_SetString(PyExc_AttributeError, "entry attribute");
768         return NULL;
769     }
770     if(acl_get_tag_type(self->entry, &value) == -1) {
771         PyErr_SetFromErrno(PyExc_IOError);
772         return NULL;
773     }
774
775     return PyInt_FromLong(value);
776 }
777
778 /* Sets the qualifier (either uid_t or gid_t) for the entry,
779  * usable only if the tag type if ACL_USER or ACL_GROUP
780  */
781 static int Entry_set_qualifier(PyObject* obj, PyObject* value, void* arg) {
782     Entry_Object *self = (Entry_Object*) obj;
783     int uidgid;
784
785     if(value == NULL) {
786         PyErr_SetString(PyExc_TypeError,
787                         "qualifier deletion is not supported");
788         return -1;
789     }
790
791     if(!PyInt_Check(value)) {
792         PyErr_SetString(PyExc_TypeError,
793                         "tag type must be integer");
794         return -1;
795     }
796     uidgid = PyInt_AsLong(value);
797     if(acl_set_qualifier(self->entry, (void*)&uidgid) == -1) {
798         PyErr_SetFromErrno(PyExc_IOError);
799         return -1;
800     }
801
802     return 0;
803 }
804
805 /* Returns the qualifier of the entry */
806 static PyObject* Entry_get_qualifier(PyObject *obj, void* arg) {
807     Entry_Object *self = (Entry_Object*) obj;
808     void *p;
809     int value;
810
811     if (self->entry == NULL) {
812         PyErr_SetString(PyExc_AttributeError, "entry attribute");
813         return NULL;
814     }
815     if((p = acl_get_qualifier(self->entry)) == NULL) {
816         PyErr_SetFromErrno(PyExc_IOError);
817         return NULL;
818     }
819     value = *(uid_t*)p;
820     acl_free(p);
821
822     return PyInt_FromLong(value);
823 }
824
825 /* Returns the parent ACL of the entry */
826 static PyObject* Entry_get_parent(PyObject *obj, void* arg) {
827     Entry_Object *self = (Entry_Object*) obj;
828
829     Py_INCREF(self->parent_acl);
830     return self->parent_acl;
831 }
832
833 /* Returns the a new Permset representing the permset of the entry
834  * FIXME: Should return a new reference to the same object, which
835  * should be created at init time!
836  */
837 static PyObject* Entry_get_permset(PyObject *obj, void* arg) {
838     Entry_Object *self = (Entry_Object*)obj;
839     PyObject *p;
840     Permset_Object *ps;
841
842     p = Permset_new(&Permset_Type, NULL, NULL);
843     if(p == NULL)
844         return NULL;
845     ps = (Permset_Object*)p;
846     if(acl_get_permset(self->entry, &ps->permset) == -1) {
847         PyErr_SetFromErrno(PyExc_IOError);
848         return NULL;
849     }
850     ps->parent_entry = obj;
851     Py_INCREF(obj);
852
853     return (PyObject*)p;
854 }
855
856 /* Sets the permset of the entry to the passed Permset */
857 static int Entry_set_permset(PyObject* obj, PyObject* value, void* arg) {
858     Entry_Object *self = (Entry_Object*)obj;
859     Permset_Object *p;
860
861     if(!PyObject_IsInstance(value, (PyObject*)&Permset_Type)) {
862         PyErr_SetString(PyExc_TypeError, "argument 1 must be posix1e.Permset");
863         return -1;
864     }
865     p = (Permset_Object*)value;
866     if(acl_set_permset(self->entry, p->permset) == -1) {
867         PyErr_SetFromErrno(PyExc_IOError);
868         return -1;
869     }
870     return 0;
871 }
872
873 static char __Entry_copy_doc__[] =
874     "Copy an ACL entry.\n"
875     "\n"
876     "This method sets all the parameters to those of another\n"
877     "entry, even one of another's ACL\n"
878     "Parameters:\n"
879     " - src, instance of type Entry\n"
880     ;
881
882 /* Sets all the entry parameters to another's entry */
883 static PyObject* Entry_copy(PyObject *obj, PyObject *args) {
884     Entry_Object *self = (Entry_Object*)obj;
885     Entry_Object *other;
886
887     if(!PyArg_ParseTuple(args, "O!", &Entry_Type, &other))
888         return NULL;
889
890     if(acl_copy_entry(self->entry, other->entry) == -1)
891         return PyErr_SetFromErrno(PyExc_IOError);
892
893     Py_INCREF(Py_None);
894     return Py_None;
895 }
896
897 /**** Permset type *****/
898
899 /* Creation of a new Permset instance */
900 static PyObject* Permset_new(PyTypeObject* type, PyObject* args,
901                              PyObject *keywds) {
902     PyObject* newpermset;
903
904     newpermset = PyType_GenericNew(type, args, keywds);
905
906     if(newpermset != NULL) {
907         ((Permset_Object*)newpermset)->permset = NULL;
908         ((Permset_Object*)newpermset)->parent_entry = NULL;
909     }
910
911     return newpermset;
912 }
913
914 /* Initialization of a new Permset instance */
915 static int Permset_init(PyObject* obj, PyObject* args, PyObject *keywds) {
916     Permset_Object* self = (Permset_Object*) obj;
917     Entry_Object* parent = NULL;
918
919     if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &parent))
920         return -1;
921
922     if(acl_get_permset(parent->entry, &self->permset) == -1) {
923         PyErr_SetFromErrno(PyExc_IOError);
924         return -1;
925     }
926
927     self->parent_entry = (PyObject*)parent;
928     Py_INCREF(parent);
929
930     return 0;
931 }
932
933 /* Free the Permset instance */
934 static void Permset_dealloc(PyObject* obj) {
935     Permset_Object *self = (Permset_Object*) obj;
936     PyObject *err_type, *err_value, *err_traceback;
937     int have_error = PyErr_Occurred() ? 1 : 0;
938
939     if (have_error)
940         PyErr_Fetch(&err_type, &err_value, &err_traceback);
941     if(self->parent_entry != NULL) {
942         Py_DECREF(self->parent_entry);
943         self->parent_entry = NULL;
944     }
945     if (have_error)
946         PyErr_Restore(err_type, err_value, err_traceback);
947     PyObject_DEL(self);
948 }
949
950 /* Permset string representation */
951 static PyObject* Permset_str(PyObject *obj) {
952     Permset_Object *self = (Permset_Object*) obj;
953     char pstr[3];
954
955     pstr[0] = get_perm(self->permset, ACL_READ) ? 'r' : '-';
956     pstr[1] = get_perm(self->permset, ACL_WRITE) ? 'w' : '-';
957     pstr[2] = get_perm(self->permset, ACL_EXECUTE) ? 'x' : '-';
958     return PyBytes_FromStringAndSize(pstr, 3);
959 }
960
961 static char __Permset_clear_doc__[] =
962     "Clear all permissions from the permission set.\n"
963     ;
964
965 /* Clears all permissions from the permset */
966 static PyObject* Permset_clear(PyObject* obj, PyObject* args) {
967     Permset_Object *self = (Permset_Object*) obj;
968
969     if(acl_clear_perms(self->permset) == -1)
970         return PyErr_SetFromErrno(PyExc_IOError);
971
972     /* Return the result */
973     Py_INCREF(Py_None);
974     return Py_None;
975 }
976
977 static PyObject* Permset_get_right(PyObject *obj, void* arg) {
978     Permset_Object *self = (Permset_Object*) obj;
979
980     if(get_perm(self->permset, *(acl_perm_t*)arg)) {
981         Py_INCREF(Py_True);
982         return Py_True;
983     } else {
984         Py_INCREF(Py_False);
985         return Py_False;
986     }
987 }
988
989 static int Permset_set_right(PyObject* obj, PyObject* value, void* arg) {
990     Permset_Object *self = (Permset_Object*) obj;
991     int on;
992     int nerr;
993
994     if(!PyInt_Check(value)) {
995         PyErr_SetString(PyExc_ValueError, "a maximum of one argument must"
996                         " be passed");
997         return -1;
998     }
999     on = PyInt_AsLong(value);
1000     if(on)
1001         nerr = acl_add_perm(self->permset, *(acl_perm_t*)arg);
1002     else
1003         nerr = acl_delete_perm(self->permset, *(acl_perm_t*)arg);
1004     if(nerr == -1) {
1005         PyErr_SetFromErrno(PyExc_IOError);
1006         return -1;
1007     }
1008     return 0;
1009 }
1010
1011 static char __Permset_add_doc__[] =
1012     "Add a permission to the permission set.\n"
1013     "\n"
1014     "The add() function adds the permission contained in \n"
1015     "the argument perm to the permission set.  An attempt \n"
1016     "to add a permission that is already contained in the \n"
1017     "permission set is not considered an error.\n"
1018     "\n"
1019     "Parameters:\n\n"
1020     "  - perm: a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...)\n"
1021     "\n"
1022     "Return value: None\n"
1023     "\n"
1024     "Can raise: IOError\n"
1025     ;
1026
1027 static PyObject* Permset_add(PyObject* obj, PyObject* args) {
1028     Permset_Object *self = (Permset_Object*) obj;
1029     int right;
1030
1031     if (!PyArg_ParseTuple(args, "i", &right))
1032         return NULL;
1033
1034     if(acl_add_perm(self->permset, (acl_perm_t) right) == -1)
1035         return PyErr_SetFromErrno(PyExc_IOError);
1036
1037     /* Return the result */
1038     Py_INCREF(Py_None);
1039     return Py_None;
1040 }
1041
1042 static char __Permset_delete_doc__[] =
1043     "Delete a permission from the permission set.\n"
1044     "\n"
1045     "The delete() function deletes the permission contained in \n"
1046     "the argument perm from the permission set.  An attempt \n"
1047     "to delete a permission that is not contained in the \n"
1048     "permission set is not considered an error.\n"
1049     "Parameters:\n\n"
1050     "  - perm a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...)\n"
1051     "Return value: None\n"
1052     "\n"
1053     "Can raise: IOError\n"
1054     ;
1055
1056 static PyObject* Permset_delete(PyObject* obj, PyObject* args) {
1057     Permset_Object *self = (Permset_Object*) obj;
1058     int right;
1059
1060     if (!PyArg_ParseTuple(args, "i", &right))
1061         return NULL;
1062
1063     if(acl_delete_perm(self->permset, (acl_perm_t) right) == -1)
1064         return PyErr_SetFromErrno(PyExc_IOError);
1065
1066     /* Return the result */
1067     Py_INCREF(Py_None);
1068     return Py_None;
1069 }
1070
1071 static char __Permset_test_doc__[] =
1072     "Test if a permission exists in the permission set.\n"
1073     "\n"
1074     "The test() function tests if the permission contained in \n"
1075     "the argument perm exits the permission set.\n"
1076     "Parameters:\n\n"
1077     "  - perm a permission (ACL_WRITE, ACL_READ, ACL_EXECUTE, ...)\n"
1078     "Return value: Boolean\n"
1079     "\n"
1080     "Can raise: IOError\n"
1081     ;
1082
1083 static PyObject* Permset_test(PyObject* obj, PyObject* args) {
1084     Permset_Object *self = (Permset_Object*) obj;
1085     int right;
1086     int ret;
1087
1088     if (!PyArg_ParseTuple(args, "i", &right))
1089         return NULL;
1090
1091     ret = get_perm(self->permset, (acl_perm_t) right);
1092     if(ret == -1)
1093         return PyErr_SetFromErrno(PyExc_IOError);
1094
1095     if(ret) {
1096         Py_INCREF(Py_True);
1097         return Py_True;
1098     } else {
1099         Py_INCREF(Py_False);
1100         return Py_False;
1101     }
1102 }
1103
1104 #endif
1105
1106 static char __ACL_Type_doc__[] =
1107     "Type which represents a POSIX ACL\n"
1108     "\n"
1109     "Parameters (only one keword parameter should be provided):\n"
1110     "  - file=\"...\", meaning create ACL representing\n"
1111     "    the access ACL of that file\n"
1112     "  - filedef=\"...\", meaning create ACL representing\n"
1113     "    the default ACL of that directory\n"
1114     "  - fd=<int>, meaning create ACL representing\n"
1115     "    the access ACL of that file descriptor\n"
1116     "  - text=\"...\", meaning create ACL from a \n"
1117     "    textual description\n"
1118     "  - acl=<ACL instance>, meaning create a copy\n"
1119     "    of an existing ACL instance\n"
1120     "  - mode=<int>, meaning create an ACL from a numeric mode\n"
1121     "    (e.g. mode=0644) (this is valid only when the C library\n"
1122     "    provides the acl_from_mode call)\n"
1123     "\n"
1124     "If no parameters are passed, create an empty ACL; this\n"
1125     "makes sense only when your OS supports ACL modification\n"
1126     "(i.e. it implements full POSIX.1e support)\n"
1127     ;
1128
1129 /* ACL type methods */
1130 static PyMethodDef ACL_methods[] = {
1131     {"applyto", ACL_applyto, METH_VARARGS, __applyto_doc__},
1132     {"valid", ACL_valid, METH_NOARGS, __valid_doc__},
1133 #ifdef HAVE_LINUX
1134     {"to_any_text", (PyCFunction)ACL_to_any_text, METH_VARARGS | METH_KEYWORDS,
1135      __to_any_text_doc__},
1136     {"check", ACL_check, METH_NOARGS, __check_doc__},
1137     {"equiv_mode", ACL_equiv_mode, METH_NOARGS, __equiv_mode_doc__},
1138 #endif
1139 #ifdef HAVE_ACL_COPYEXT
1140     {"__getstate__", ACL_get_state, METH_NOARGS,
1141      "Dumps the ACL to an external format."},
1142     {"__setstate__", ACL_set_state, METH_VARARGS,
1143      "Loads the ACL from an external format."},
1144 #endif
1145 #ifdef HAVE_LEVEL2
1146     {"delete_entry", ACL_delete_entry, METH_VARARGS, __ACL_delete_entry_doc__},
1147     {"calc_mask", ACL_calc_mask, METH_NOARGS, __ACL_calc_mask_doc__},
1148     {"append", ACL_append, METH_VARARGS, __ACL_append_doc__},
1149 #endif
1150     {NULL, NULL, 0, NULL}
1151 };
1152
1153
1154 /* The definition of the ACL Type */
1155 static PyTypeObject ACL_Type = {
1156 #ifdef IS_PY3K
1157     PyVarObject_HEAD_INIT(NULL, 0)
1158 #else
1159     PyObject_HEAD_INIT(NULL)
1160     0,
1161 #endif
1162     "posix1e.ACL",
1163     sizeof(ACL_Object),
1164     0,
1165     ACL_dealloc,        /* tp_dealloc */
1166     0,                  /* tp_print */
1167     0,                  /* tp_getattr */
1168     0,                  /* tp_setattr */
1169     ACL_nocmp,          /* tp_compare */
1170     0,                  /* tp_repr */
1171     0,                  /* tp_as_number */
1172     0,                  /* tp_as_sequence */
1173     0,                  /* tp_as_mapping */
1174     0,                  /* tp_hash */
1175     0,                  /* tp_call */
1176     ACL_str,            /* tp_str */
1177     0,                  /* tp_getattro */
1178     0,                  /* tp_setattro */
1179     0,                  /* tp_as_buffer */
1180     Py_TPFLAGS_DEFAULT, /* tp_flags */
1181     __ACL_Type_doc__,   /* tp_doc */
1182     0,                  /* tp_traverse */
1183     0,                  /* tp_clear */
1184 #ifdef HAVE_LINUX
1185     ACL_richcompare,    /* tp_richcompare */
1186 #else
1187     0,                  /* tp_richcompare */
1188 #endif
1189     0,                  /* tp_weaklistoffset */
1190 #ifdef HAVE_LEVEL2
1191     ACL_iter,
1192     ACL_iternext,
1193 #else
1194     0,                  /* tp_iter */
1195     0,                  /* tp_iternext */
1196 #endif
1197     ACL_methods,        /* tp_methods */
1198     0,                  /* tp_members */
1199     0,                  /* tp_getset */
1200     0,                  /* tp_base */
1201     0,                  /* tp_dict */
1202     0,                  /* tp_descr_get */
1203     0,                  /* tp_descr_set */
1204     0,                  /* tp_dictoffset */
1205     ACL_init,           /* tp_init */
1206     0,                  /* tp_alloc */
1207     ACL_new,            /* tp_new */
1208 };
1209
1210 #ifdef HAVE_LEVEL2
1211
1212 /* Entry type methods */
1213 static PyMethodDef Entry_methods[] = {
1214     {"copy", Entry_copy, METH_VARARGS, __Entry_copy_doc__},
1215     {NULL, NULL, 0, NULL}
1216 };
1217
1218 static char __Entry_tagtype_doc__[] =
1219     "The tag type of the current entry\n"
1220     "\n"
1221     "This is one of:\n"
1222     " - ACL_UNDEFINED_TAG\n"
1223     " - ACL_USER_OBJ\n"
1224     " - ACL_USER\n"
1225     " - ACL_GROUP_OBJ\n"
1226     " - ACL_GROUP\n"
1227     " - ACL_MASK\n"
1228     " - ACL_OTHER\n"
1229     ;
1230
1231 static char __Entry_qualifier_doc__[] =
1232     "The qualifier of the current entry\n"
1233     "\n"
1234     "If the tag type is ACL_USER, this should be a user id.\n"
1235     "If the tag type if ACL_GROUP, this should be a group id.\n"
1236     "Else, it doesn't matter.\n"
1237     ;
1238
1239 static char __Entry_parent_doc__[] =
1240     "The parent ACL of this entry\n"
1241     ;
1242
1243 static char __Entry_permset_doc__[] =
1244     "The permission set of this ACL entry\n"
1245     ;
1246
1247 /* Entry getset */
1248 static PyGetSetDef Entry_getsets[] = {
1249     {"tag_type", Entry_get_tag_type, Entry_set_tag_type,
1250      __Entry_tagtype_doc__},
1251     {"qualifier", Entry_get_qualifier, Entry_set_qualifier,
1252      __Entry_qualifier_doc__},
1253     {"parent", Entry_get_parent, NULL, __Entry_parent_doc__},
1254     {"permset", Entry_get_permset, Entry_set_permset, __Entry_permset_doc__},
1255     {NULL}
1256 };
1257
1258 static char __Entry_Type_doc__[] =
1259     "Type which represents an entry in an ACL.\n"
1260     "\n"
1261     "The type exists only if the OS has full support for POSIX.1e\n"
1262     "Can be created either by:\n"
1263     "\n"
1264     "  >>> e = posix1e.Entry(myACL) # this creates a new entry in the ACL\n"
1265     "  >>> e = myACL.append() # another way for doing the same thing\n"
1266     "\n"
1267     "or by:\n"
1268     "  >>> for entry in myACL:\n"
1269     "  ...     print entry\n"
1270     "\n"
1271     "Note that the Entry keeps a reference to its ACL, so even if \n"
1272     "you delete the ACL, it won't be cleaned up and will continue to \n"
1273     "exist until its Entry(ies) will be deleted.\n"
1274     ;
1275 /* The definition of the Entry Type */
1276 static PyTypeObject Entry_Type = {
1277 #ifdef IS_PY3K
1278     PyVarObject_HEAD_INIT(NULL, 0)
1279 #else
1280     PyObject_HEAD_INIT(NULL)
1281     0,
1282 #endif
1283     "posix1e.Entry",
1284     sizeof(Entry_Object),
1285     0,
1286     Entry_dealloc,      /* tp_dealloc */
1287     0,                  /* tp_print */
1288     0,                  /* tp_getattr */
1289     0,                  /* tp_setattr */
1290     0,                  /* tp_compare */
1291     0,                  /* tp_repr */
1292     0,                  /* tp_as_number */
1293     0,                  /* tp_as_sequence */
1294     0,                  /* tp_as_mapping */
1295     0,                  /* tp_hash */
1296     0,                  /* tp_call */
1297     Entry_str,          /* tp_str */
1298     0,                  /* tp_getattro */
1299     0,                  /* tp_setattro */
1300     0,                  /* tp_as_buffer */
1301     Py_TPFLAGS_DEFAULT, /* tp_flags */
1302     __Entry_Type_doc__, /* tp_doc */
1303     0,                  /* tp_traverse */
1304     0,                  /* tp_clear */
1305     0,                  /* tp_richcompare */
1306     0,                  /* tp_weaklistoffset */
1307     0,                  /* tp_iter */
1308     0,                  /* tp_iternext */
1309     Entry_methods,      /* tp_methods */
1310     0,                  /* tp_members */
1311     Entry_getsets,      /* tp_getset */
1312     0,                  /* tp_base */
1313     0,                  /* tp_dict */
1314     0,                  /* tp_descr_get */
1315     0,                  /* tp_descr_set */
1316     0,                  /* tp_dictoffset */
1317     Entry_init,         /* tp_init */
1318     0,                  /* tp_alloc */
1319     Entry_new,          /* tp_new */
1320 };
1321
1322 /* Permset type methods */
1323 static PyMethodDef Permset_methods[] = {
1324     {"clear", Permset_clear, METH_NOARGS, __Permset_clear_doc__, },
1325     {"add", Permset_add, METH_VARARGS, __Permset_add_doc__, },
1326     {"delete", Permset_delete, METH_VARARGS, __Permset_delete_doc__, },
1327     {"test", Permset_test, METH_VARARGS, __Permset_test_doc__, },
1328     {NULL, NULL, 0, NULL}
1329 };
1330
1331 static char __Permset_execute_doc__[] =
1332     "Execute permsission\n"
1333     "\n"
1334     "This is a convenience method of access; the \n"
1335     "same effect can be achieved using the functions\n"
1336     "add(), test(), delete(), and those can take any \n"
1337     "permission defined by your platform.\n"
1338     ;
1339
1340 static char __Permset_read_doc__[] =
1341     "Read permsission\n"
1342     "\n"
1343     "This is a convenience method of access; the \n"
1344     "same effect can be achieved using the functions\n"
1345     "add(), test(), delete(), and those can take any \n"
1346     "permission defined by your platform.\n"
1347     ;
1348
1349 static char __Permset_write_doc__[] =
1350     "Write permsission\n"
1351     "\n"
1352     "This is a convenience method of access; the \n"
1353     "same effect can be achieved using the functions\n"
1354     "add(), test(), delete(), and those can take any \n"
1355     "permission defined by your platform.\n"
1356     ;
1357
1358 /* Permset getset */
1359 static PyGetSetDef Permset_getsets[] = {
1360     {"execute", Permset_get_right, Permset_set_right,
1361      __Permset_execute_doc__, &holder_ACL_EXECUTE},
1362     {"read", Permset_get_right, Permset_set_right,
1363      __Permset_read_doc__, &holder_ACL_READ},
1364     {"write", Permset_get_right, Permset_set_right,
1365      __Permset_write_doc__, &holder_ACL_WRITE},
1366     {NULL}
1367 };
1368
1369 static char __Permset_Type_doc__[] =
1370     "Type which represents the permission set in an ACL entry\n"
1371     "\n"
1372     "The type exists only if the OS has full support for POSIX.1e\n"
1373     "Can be retrieved either by:\n\n"
1374     ">>> perms = myEntry.permset\n"
1375     "\n"
1376     "or by:\n\n"
1377     ">>> perms = posix1e.Permset(myEntry)\n"
1378     "\n"
1379     "Note that the Permset keeps a reference to its Entry, so even if \n"
1380     "you delete the entry, it won't be cleaned up and will continue to \n"
1381     "exist until its Permset will be deleted.\n"
1382     ;
1383
1384 /* The definition of the Permset Type */
1385 static PyTypeObject Permset_Type = {
1386 #ifdef IS_PY3K
1387     PyVarObject_HEAD_INIT(NULL, 0)
1388 #else
1389     PyObject_HEAD_INIT(NULL)
1390     0,
1391 #endif
1392     "posix1e.Permset",
1393     sizeof(Permset_Object),
1394     0,
1395     Permset_dealloc,    /* tp_dealloc */
1396     0,                  /* tp_print */
1397     0,                  /* tp_getattr */
1398     0,                  /* tp_setattr */
1399     0,                  /* tp_compare */
1400     0,                  /* tp_repr */
1401     0,                  /* tp_as_number */
1402     0,                  /* tp_as_sequence */
1403     0,                  /* tp_as_mapping */
1404     0,                  /* tp_hash */
1405     0,                  /* tp_call */
1406     Permset_str,        /* tp_str */
1407     0,                  /* tp_getattro */
1408     0,                  /* tp_setattro */
1409     0,                  /* tp_as_buffer */
1410     Py_TPFLAGS_DEFAULT, /* tp_flags */
1411     __Permset_Type_doc__,/* tp_doc */
1412     0,                  /* tp_traverse */
1413     0,                  /* tp_clear */
1414     0,                  /* tp_richcompare */
1415     0,                  /* tp_weaklistoffset */
1416     0,                  /* tp_iter */
1417     0,                  /* tp_iternext */
1418     Permset_methods,    /* tp_methods */
1419     0,                  /* tp_members */
1420     Permset_getsets,    /* tp_getset */
1421     0,                  /* tp_base */
1422     0,                  /* tp_dict */
1423     0,                  /* tp_descr_get */
1424     0,                  /* tp_descr_set */
1425     0,                  /* tp_dictoffset */
1426     Permset_init,       /* tp_init */
1427     0,                  /* tp_alloc */
1428     Permset_new,        /* tp_new */
1429 };
1430
1431 #endif
1432
1433 /* Module methods */
1434
1435 static char __deletedef_doc__[] =
1436     "Delete the default ACL from a directory.\n"
1437     "\n"
1438     "This function deletes the default ACL associated with \n"
1439     "a directory (the ACL which will be ANDed with the mode\n"
1440     "parameter to the open, creat functions).\n"
1441     "Parameters:\n"
1442     "  - a string representing the directory whose default ACL\n"
1443     "    should be deleted\n"
1444     ;
1445
1446 /* Deletes the default ACL from a directory */
1447 static PyObject* aclmodule_delete_default(PyObject* obj, PyObject* args) {
1448     char *filename;
1449
1450     /* Parse the arguments */
1451     if (!PyArg_ParseTuple(args, "s", &filename))
1452         return NULL;
1453
1454     if(acl_delete_def_file(filename) == -1) {
1455         return PyErr_SetFromErrno(PyExc_IOError);
1456     }
1457
1458     /* Return the result */
1459     Py_INCREF(Py_None);
1460     return Py_None;
1461 }
1462
1463 #ifdef HAVE_LINUX
1464 static char __has_extended_doc__[] =
1465     "Check if a file or filehandle has an extended ACL.\n"
1466     "\n"
1467     "Parameter:\n"
1468     "  - either a filename or a file-like object or an integer; this\n"
1469     "    represents the filesystem object on which to act\n"
1470     ;
1471
1472 /* Check for extended ACL a file or fd */
1473 static PyObject* aclmodule_has_extended(PyObject* obj, PyObject* args) {
1474     PyObject *myarg;
1475     int nret;
1476     int fd;
1477
1478     if (!PyArg_ParseTuple(args, "O", &myarg))
1479         return NULL;
1480
1481     if(PyBytes_Check(myarg)) {
1482         const char *filename = PyBytes_AS_STRING(myarg);
1483         nret = acl_extended_file(filename);
1484     } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
1485         nret = acl_extended_fd(fd);
1486     } else {
1487         PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int,"
1488                         " or file-like object");
1489         return 0;
1490     }
1491     if(nret == -1) {
1492         return PyErr_SetFromErrno(PyExc_IOError);
1493     }
1494
1495     /* Return the result */
1496     return PyBool_FromLong(nret);
1497 }
1498 #endif
1499
1500 /* The module methods */
1501 static PyMethodDef aclmodule_methods[] = {
1502     {"delete_default", aclmodule_delete_default, METH_VARARGS,
1503      __deletedef_doc__},
1504 #ifdef HAVE_LINUX
1505     {"has_extended", aclmodule_has_extended, METH_VARARGS,
1506      __has_extended_doc__},
1507 #endif
1508     {NULL, NULL, 0, NULL}
1509 };
1510
1511 static char __posix1e_doc__[] =
1512     "POSIX.1e ACLs manipulation\n"
1513     "\n"
1514     "This module provides support for manipulating POSIX.1e ACLS\n"
1515     "\n"
1516     "Depending on the operating system support for POSIX.1e, \n"
1517     "the ACL type will have more or less capabilities:\n"
1518     "  - level 1, only basic support, you can create\n"
1519     "    ACLs from files and text descriptions;\n"
1520     "    once created, the type is immutable\n"
1521     "  - level 2, complete support, you can alter\n"
1522     "    the ACL once it is created\n"
1523     "\n"
1524     "Also, in level 2, more types are available, corresponding\n"
1525     "to acl_entry_t (the Entry type), acl_permset_t (the Permset type).\n"
1526     "\n"
1527     "The existence of level 2 support and other extensions can be\n"
1528     "checked by the constants:\n"
1529     "  - HAS_ACL_ENTRY for level 2 and the Entry/Permset classes\n"
1530     "  - HAS_ACL_FROM_MODE for ACL(mode=...) usage\n"
1531     "  - HAS_ACL_CHECK for the ACL().check function\n"
1532     "  - HAS_EXTENDED_CHECK for the module-level has_extended function\n"
1533     "  - HAS_EQUIV_MODE for the ACL().equiv_mode method\n"
1534     "\n"
1535     "Example:\n"
1536     "\n"
1537     ">>> import posix1e\n"
1538     ">>> acl1 = posix1e.ACL(file=\"file.txt\") \n"
1539     ">>> print acl1\n"
1540     "user::rw-\n"
1541     "group::rw-\n"
1542     "other::r--\n"
1543     ">>>\n"
1544     ">>> b = posix1e.ACL(text=\"u::rx,g::-,o::-\")\n"
1545     ">>> print b\n"
1546     "user::r-x\n"
1547     "group::---\n"
1548     "other::---\n"
1549     ">>>\n"
1550     ">>> b.applyto(\"file.txt\")\n"
1551     ">>> print posix1e.ACL(file=\"file.txt\")\n"
1552     "user::r-x\n"
1553     "group::---\n"
1554     "other::---\n"
1555     ">>>\n"
1556     "\n"
1557     ;
1558
1559 #ifdef IS_PY3K
1560
1561 static struct PyModuleDef posix1emodule = {
1562     PyModuleDef_HEAD_INIT,
1563     "posix1e",
1564     __posix1e_doc__,
1565     0,
1566     aclmodule_methods,
1567 };
1568
1569 #define INITERROR return NULL
1570
1571 PyMODINIT_FUNC
1572 PyInit_posix1e(void)
1573
1574 #else
1575 #define INITERROR return
1576
1577 void initposix1e(void)
1578 #endif
1579 {
1580     PyObject *m, *d;
1581
1582     Py_TYPE(&ACL_Type) = &PyType_Type;
1583     if(PyType_Ready(&ACL_Type) < 0)
1584         INITERROR;
1585
1586 #ifdef HAVE_LEVEL2
1587     Py_TYPE(&Entry_Type) = &PyType_Type;
1588     if(PyType_Ready(&Entry_Type) < 0)
1589         INITERROR;
1590
1591     Py_TYPE(&Permset_Type) = &PyType_Type;
1592     if(PyType_Ready(&Permset_Type) < 0)
1593         INITERROR;
1594 #endif
1595
1596 #ifdef IS_PY3K
1597     m = PyModule_Create(&posix1emodule);
1598 #else
1599     m = Py_InitModule3("posix1e", aclmodule_methods, __posix1e_doc__);
1600 #endif
1601     if (m==NULL)
1602         INITERROR;
1603
1604     d = PyModule_GetDict(m);
1605     if (d == NULL)
1606         INITERROR;
1607
1608     Py_INCREF(&ACL_Type);
1609     if (PyDict_SetItemString(d, "ACL",
1610                              (PyObject *) &ACL_Type) < 0)
1611         INITERROR;
1612
1613     /* 23.3.6 acl_type_t values */
1614     PyModule_AddIntConstant(m, "ACL_TYPE_ACCESS", ACL_TYPE_ACCESS);
1615     PyModule_AddIntConstant(m, "ACL_TYPE_DEFAULT", ACL_TYPE_DEFAULT);
1616
1617
1618 #ifdef HAVE_LEVEL2
1619     Py_INCREF(&Entry_Type);
1620     if (PyDict_SetItemString(d, "Entry",
1621                              (PyObject *) &Entry_Type) < 0)
1622         INITERROR;
1623
1624     Py_INCREF(&Permset_Type);
1625     if (PyDict_SetItemString(d, "Permset",
1626                              (PyObject *) &Permset_Type) < 0)
1627         INITERROR;
1628
1629     /* 23.2.2 acl_perm_t values */
1630     PyModule_AddIntConstant(m, "ACL_READ", ACL_READ);
1631     PyModule_AddIntConstant(m, "ACL_WRITE", ACL_WRITE);
1632     PyModule_AddIntConstant(m, "ACL_EXECUTE", ACL_EXECUTE);
1633
1634     /* 23.2.5 acl_tag_t values */
1635     PyModule_AddIntConstant(m, "ACL_UNDEFINED_TAG", ACL_UNDEFINED_TAG);
1636     PyModule_AddIntConstant(m, "ACL_USER_OBJ", ACL_USER_OBJ);
1637     PyModule_AddIntConstant(m, "ACL_USER", ACL_USER);
1638     PyModule_AddIntConstant(m, "ACL_GROUP_OBJ", ACL_GROUP_OBJ);
1639     PyModule_AddIntConstant(m, "ACL_GROUP", ACL_GROUP);
1640     PyModule_AddIntConstant(m, "ACL_MASK", ACL_MASK);
1641     PyModule_AddIntConstant(m, "ACL_OTHER", ACL_OTHER);
1642
1643     /* Document extended functionality via easy-to-use constants */
1644     PyModule_AddIntConstant(m, "HAS_ACL_ENTRY", 1);
1645 #else
1646     PyModule_AddIntConstant(m, "HAS_ACL_ENTRY", 0);
1647 #endif
1648
1649 #ifdef HAVE_LINUX
1650     /* Linux libacl specific acl_to_any_text constants */
1651     PyModule_AddIntConstant(m, "TEXT_ABBREVIATE", TEXT_ABBREVIATE);
1652     PyModule_AddIntConstant(m, "TEXT_NUMERIC_IDS", TEXT_NUMERIC_IDS);
1653     PyModule_AddIntConstant(m, "TEXT_SOME_EFFECTIVE", TEXT_SOME_EFFECTIVE);
1654     PyModule_AddIntConstant(m, "TEXT_ALL_EFFECTIVE", TEXT_ALL_EFFECTIVE);
1655     PyModule_AddIntConstant(m, "TEXT_SMART_INDENT", TEXT_SMART_INDENT);
1656
1657     /* Linux libacl specific acl_check constants */
1658     PyModule_AddIntConstant(m, "ACL_MULTI_ERROR", ACL_MULTI_ERROR);
1659     PyModule_AddIntConstant(m, "ACL_DUPLICATE_ERROR", ACL_DUPLICATE_ERROR);
1660     PyModule_AddIntConstant(m, "ACL_MISS_ERROR", ACL_MISS_ERROR);
1661     PyModule_AddIntConstant(m, "ACL_ENTRY_ERROR", ACL_ENTRY_ERROR);
1662
1663     /* declare the Linux extensions */
1664     PyModule_AddIntConstant(m, "HAS_ACL_FROM_MODE", 1);
1665     PyModule_AddIntConstant(m, "HAS_ACL_CHECK", 1);
1666     PyModule_AddIntConstant(m, "HAS_EXTENDED_CHECK", 1);
1667     PyModule_AddIntConstant(m, "HAS_EQUIV_MODE", 1);
1668 #else
1669     PyModule_AddIntConstant(m, "HAS_ACL_FROM_MODE", 0);
1670     PyModule_AddIntConstant(m, "HAS_ACL_CHECK", 0);
1671     PyModule_AddIntConstant(m, "HAS_EXTENDED_CHECK", 0);
1672     PyModule_AddIntConstant(m, "HAS_EQUIV_MODE", 0);
1673 #endif
1674
1675 #ifdef IS_PY3K
1676     return m;
1677 #endif
1678 }