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