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