forked from iustin/pylibacl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl.c
1887 lines (1654 loc) · 57.8 KB
/
acl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
posix1e - a python module exposing the posix acl functions
Copyright (C) 2002-2009, 2012, 2014, 2015 Iustin Pop <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <Python.h>
#include <sys/types.h>
#include <sys/acl.h>
#ifdef HAVE_LINUX
#include <acl/libacl.h>
#define get_perm acl_get_perm
#elif HAVE_FREEBSD
#define get_perm acl_get_perm_np
#endif
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#define PyInt_Check(op) PyLong_Check(op)
#define PyInt_FromString PyLong_FromString
#define PyInt_FromUnicode PyLong_FromUnicode
#define PyInt_FromLong PyLong_FromLong
#define PyInt_FromSize_t PyLong_FromSize_t
#define PyInt_FromSsize_t PyLong_FromSsize_t
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AsSsize_t PyLong_AsSsize_t
#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
#define PyInt_AS_LONG PyLong_AS_LONG
#define MyString_ConcatAndDel PyUnicode_AppendAndDel
#define MyString_FromFormat PyUnicode_FromFormat
#define MyString_FromString PyUnicode_FromString
#define MyString_FromStringAndSize PyUnicode_FromStringAndSize
#else
#define PyBytes_Check PyString_Check
#define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
#define PyBytes_FromString PyString_FromString
#define PyBytes_FromFormat PyString_FromFormat
#define PyBytes_ConcatAndDel PyString_ConcatAndDel
#define MyString_ConcatAndDel PyBytes_ConcatAndDel
#define MyString_FromFormat PyBytes_FromFormat
#define MyString_FromString PyBytes_FromString
#define MyString_FromStringAndSize PyBytes_FromStringAndSize
/* Python 2.6 already defines Py_TYPE */
#ifndef Py_TYPE
#define Py_TYPE(o) (((PyObject*)(o))->ob_type)
#endif
#endif
/* Used for cpychecker: */
/* The checker automatically defines this preprocessor name when creating
the custom attribute: */
#if defined(WITH_CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF_ATTRIBUTE)
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename) \
__attribute__((cpychecker_type_object_for_typedef(typename)))
#else
/* This handles the case where we're compiling with a "vanilla"
compiler that doesn't supply this attribute: */
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename)
#endif
/* The checker automatically defines this preprocessor name when creating
the custom attribute: */
#if defined(WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE)
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
__attribute__((cpychecker_negative_result_sets_exception))
#else
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#endif
static PyTypeObject ACL_Type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("ACL_Object");
static PyObject* ACL_applyto(PyObject* obj, PyObject* args);
static PyObject* ACL_valid(PyObject* obj, PyObject* args);
#ifdef HAVE_ACL_COPY_EXT
static PyObject* ACL_get_state(PyObject *obj, PyObject* args);
static PyObject* ACL_set_state(PyObject *obj, PyObject* args);
#endif
#ifdef HAVE_LEVEL2
static PyTypeObject Entry_Type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("Entry_Object");
static PyTypeObject Permset_Type
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF("Permset_Object");
static PyObject* Permset_new(PyTypeObject* type, PyObject* args,
PyObject *keywds);
#endif
static acl_perm_t holder_ACL_EXECUTE = ACL_EXECUTE;
static acl_perm_t holder_ACL_READ = ACL_READ;
static acl_perm_t holder_ACL_WRITE = ACL_WRITE;
typedef struct {
PyObject_HEAD
acl_t acl;
#ifdef HAVE_LEVEL2
int entry_id;
#endif
} ACL_Object;
#ifdef HAVE_LEVEL2
typedef struct {
PyObject_HEAD
PyObject *parent_acl; /* The parent acl, so it won't run out on us */
acl_entry_t entry;
} Entry_Object;
typedef struct {
PyObject_HEAD
PyObject *parent_entry; /* The parent entry, so it won't run out on us */
acl_permset_t permset;
} Permset_Object;
#endif
/* Creation of a new ACL instance */
static PyObject* ACL_new(PyTypeObject* type, PyObject* args,
PyObject *keywds) {
PyObject* newacl;
newacl = type->tp_alloc(type, 0);
if(newacl != NULL) {
((ACL_Object*)newacl)->acl = NULL;
#ifdef HAVEL_LEVEL2
((ACL_Object*)newacl)->entry_id = ACL_FIRST_ENTRY;
#endif
}
return newacl;
}
/* Initialization of a new ACL instance */
static int ACL_init(PyObject* obj, PyObject* args, PyObject *keywds) {
ACL_Object* self = (ACL_Object*) obj;
#ifdef HAVE_LINUX
static char *kwlist[] = { "file", "fd", "text", "acl", "filedef",
"mode", NULL };
char *format = "|etisO!sH";
mode_t mode = 0;
#else
static char *kwlist[] = { "file", "fd", "text", "acl", "filedef", NULL };
char *format = "|etisO!s";
#endif
char *file = NULL;
char *filedef = NULL;
char *text = NULL;
int fd = -1;
ACL_Object* thesrc = NULL;
if(!PyTuple_Check(args) || PyTuple_Size(args) != 0 ||
(keywds != NULL && PyDict_Check(keywds) && PyDict_Size(keywds) > 1)) {
PyErr_SetString(PyExc_ValueError, "a max of one keyword argument"
" must be passed");
return -1;
}
if(!PyArg_ParseTupleAndKeywords(args, keywds, format, kwlist,
NULL, &file, &fd, &text, &ACL_Type,
&thesrc, &filedef
#ifdef HAVE_LINUX
, &mode
#endif
))
return -1;
/* Free the old acl_t without checking for error, we don't
* care right now */
if(self->acl != NULL)
acl_free(self->acl);
if(file != NULL)
self->acl = acl_get_file(file, ACL_TYPE_ACCESS);
else if(text != NULL)
self->acl = acl_from_text(text);
else if(fd != -1)
self->acl = acl_get_fd(fd);
else if(thesrc != NULL)
self->acl = acl_dup(thesrc->acl);
else if(filedef != NULL)
self->acl = acl_get_file(filedef, ACL_TYPE_DEFAULT);
#ifdef HAVE_LINUX
else if(PyMapping_HasKeyString(keywds, kwlist[5]))
self->acl = acl_from_mode(mode);
#endif
else
self->acl = acl_init(0);
if(self->acl == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
return 0;
}
/* Standard type functions */
static void ACL_dealloc(PyObject* obj) {
ACL_Object *self = (ACL_Object*) obj;
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;
if (have_error)
PyErr_Fetch(&err_type, &err_value, &err_traceback);
if(self->acl != NULL && acl_free(self->acl) != 0)
PyErr_WriteUnraisable(obj);
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
PyObject_DEL(self);
}
/* Converts the acl to a text format */
static PyObject* ACL_str(PyObject *obj) {
char *text;
ACL_Object *self = (ACL_Object*) obj;
PyObject *ret;
text = acl_to_text(self->acl, NULL);
if(text == NULL) {
return PyErr_SetFromErrno(PyExc_IOError);
}
ret = MyString_FromString(text);
if(acl_free(text) != 0) {
Py_XDECREF(ret);
return PyErr_SetFromErrno(PyExc_IOError);
}
return ret;
}
#ifdef HAVE_LINUX
static char __to_any_text_doc__[] =
"to_any_text([prefix='', separator='n', options=0])\n"
"Convert the ACL to a custom text format.\n"
"\n"
"This method encapsulates the ``acl_to_any_text()`` function.\n"
"It allows a customized text format to be generated for the ACL. See\n"
":manpage:`acl_to_any_text(3)` for more details.\n"
"\n"
":param string prefix: if given, this string will be pre-pended to\n"
" all lines\n"
":param string separator: a single character (defaults to '\\n'); this will"
" be used to separate the entries in the ACL\n"
":param options: a bitwise combination of:\n\n"
" - :py:data:`TEXT_ABBREVIATE`: use 'u' instead of 'user', 'g' \n"
" instead of 'group', etc.\n"
" - :py:data:`TEXT_NUMERIC_IDS`: User and group IDs are included as\n"
" decimal numbers instead of names\n"
" - :py:data:`TEXT_SOME_EFFECTIVE`: Include comments denoting the\n"
" effective permissions when some are masked\n"
" - :py:data:`TEXT_ALL_EFFECTIVE`: Include comments after all ACL\n"
" entries affected by an ACL_MASK entry\n"
" - :py:data:`TEXT_SMART_INDENT`: Used in combination with the\n"
" _EFFECTIVE options, this will ensure that comments are aligned\n"
" to the fourth tab position (assuming one tab equals eight spaces)\n"
":rtype: string\n"
;
/* Converts the acl to a custom text format */
static PyObject* ACL_to_any_text(PyObject *obj, PyObject *args,
PyObject *kwds) {
char *text;
ACL_Object *self = (ACL_Object*) obj;
PyObject *ret;
const char *arg_prefix = NULL;
char arg_separator = '\n';
int arg_options = 0;
static char *kwlist[] = {"prefix", "separator", "options", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sci", kwlist, &arg_prefix,
&arg_separator, &arg_options))
return NULL;
text = acl_to_any_text(self->acl, arg_prefix, arg_separator, arg_options);
if(text == NULL) {
return PyErr_SetFromErrno(PyExc_IOError);
}
ret = PyBytes_FromString(text);
if(acl_free(text) != 0) {
Py_XDECREF(ret);
return PyErr_SetFromErrno(PyExc_IOError);
}
return ret;
}
static char __check_doc__[] =
"Check the ACL validity.\n"
"\n"
"This is a non-portable, Linux specific extension that allow more\n"
"information to be retrieved in case an ACL is not valid than via the\n"
":py:func:`valid` method.\n"
"\n"
"This method will return either False (the ACL is valid), or a tuple\n"
"with two elements. The first element is one of the following\n"
"constants:\n\n"
" - :py:data:`ACL_MULTI_ERROR`: The ACL contains multiple entries that\n"
" have a tag type that may occur at most once\n"
" - :py:data:`ACL_DUPLICATE_ERROR`: The ACL contains multiple \n"
" :py:data:`ACL_USER` or :py:data:`ACL_GROUP` entries with the\n"
" same ID\n"
" - :py:data:`ACL_MISS_ERROR`: A required entry is missing\n"
" - :py:data:`ACL_ENTRY_ERROR`: The ACL contains an invalid entry\n"
" tag type\n"
"\n"
"The second element of the tuple is the index of the entry that is\n"
"invalid (in the same order as by iterating over the ACL entry)\n"
;
/* The acl_check method */
static PyObject* ACL_check(PyObject* obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
int result;
int eindex;
if((result = acl_check(self->acl, &eindex)) == -1)
return PyErr_SetFromErrno(PyExc_IOError);
if(result == 0) {
Py_RETURN_FALSE;
}
return Py_BuildValue("(ii)", result, eindex);
}
/* Implementation of the rich compare for ACLs */
static PyObject* ACL_richcompare(PyObject* o1, PyObject* o2, int op) {
ACL_Object *acl1, *acl2;
int n;
PyObject *ret;
if(!PyObject_IsInstance(o2, (PyObject*)&ACL_Type)) {
if(op == Py_EQ)
Py_RETURN_FALSE;
if(op == Py_NE)
Py_RETURN_TRUE;
PyErr_SetString(PyExc_TypeError, "can only compare to an ACL");
return NULL;
}
acl1 = (ACL_Object*)o1;
acl2 = (ACL_Object*)o2;
if((n=acl_cmp(acl1->acl, acl2->acl))==-1)
return PyErr_SetFromErrno(PyExc_IOError);
switch(op) {
case Py_EQ:
ret = n == 0 ? Py_True : Py_False;
break;
case Py_NE:
ret = n == 1 ? Py_True : Py_False;
break;
default:
ret = Py_NotImplemented;
}
Py_INCREF(ret);
return ret;
}
static char __equiv_mode_doc__[] =
"Return the octal mode the ACL is equivalent to.\n"
"\n"
"This is a non-portable, Linux specific extension that checks\n"
"if the ACL is a basic ACL and returns the corresponding mode.\n"
"\n"
":rtype: integer\n"
":raise IOError: An IOerror exception will be raised if the ACL is\n"
" an extended ACL.\n"
;
/* The acl_equiv_mode method */
static PyObject* ACL_equiv_mode(PyObject* obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
mode_t mode;
if(acl_equiv_mode(self->acl, &mode) == -1)
return PyErr_SetFromErrno(PyExc_IOError);
return PyInt_FromLong(mode);
}
#endif
/* Implementation of the compare for ACLs */
static int ACL_nocmp(PyObject* o1, PyObject* o2) {
PyErr_SetString(PyExc_TypeError, "cannot compare ACLs using cmp()");
return -1;
}
/* Custom methods */
static char __applyto_doc__[] =
"applyto(item[, flag=ACL_TYPE_ACCESS])\n"
"Apply the ACL to a file or filehandle.\n"
"\n"
":param item: either a filename or a file-like object or an integer;\n"
" this represents the filesystem object on which to act\n"
":param flag: optional flag representing the type of ACL to set, either\n"
" :py:data:`ACL_TYPE_ACCESS` (default) or :py:data:`ACL_TYPE_DEFAULT`\n"
;
/* Applies the ACL to a file */
static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
PyObject *myarg;
acl_type_t type = ACL_TYPE_ACCESS;
int nret;
int fd;
if (!PyArg_ParseTuple(args, "O|I", &myarg, &type))
return NULL;
if(PyBytes_Check(myarg)) {
char *filename = PyBytes_AS_STRING(myarg);
nret = acl_set_file(filename, type, self->acl);
} else if (PyUnicode_Check(myarg)) {
PyObject *o =
PyUnicode_AsEncodedString(myarg,
Py_FileSystemDefaultEncoding, "strict");
if (o == NULL)
return NULL;
const char *filename = PyBytes_AS_STRING(o);
nret = acl_set_file(filename, type, self->acl);
Py_DECREF(o);
} else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
nret = acl_set_fd(fd, self->acl);
} else {
PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int,"
" or file-like object");
return 0;
}
if(nret == -1) {
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
static char __valid_doc__[] =
"Test the ACL for validity.\n"
"\n"
"This method tests the ACL to see if it is a valid ACL\n"
"in terms of the file-system. More precisely, it checks that:\n"
"\n"
"The ACL contains exactly one entry with each of the\n"
":py:data:`ACL_USER_OBJ`, :py:data:`ACL_GROUP_OBJ`, and \n"
":py:data:`ACL_OTHER` tag types. Entries\n"
"with :py:data:`ACL_USER` and :py:data:`ACL_GROUP` tag types may\n"
"appear zero or more\n"
"times in an ACL. An ACL that contains entries of :py:data:`ACL_USER` or\n"
":py:data:`ACL_GROUP` tag types must contain exactly one entry of the \n"
":py:data:`ACL_MASK` tag type. If an ACL contains no entries of\n"
":py:data:`ACL_USER` or :py:data:`ACL_GROUP` tag types, the\n"
":py:data:`ACL_MASK` entry is optional.\n"
"\n"
"All user ID qualifiers must be unique among all entries of\n"
"the :py:data:`ACL_USER` tag type, and all group IDs must be unique\n"
"among all entries of :py:data:`ACL_GROUP` tag type.\n"
"\n"
"The method will return 1 for a valid ACL and 0 for an invalid one.\n"
"This has been chosen because the specification for\n"
":manpage:`acl_valid(3)`\n"
"in the POSIX.1e standard documents only one possible value for errno\n"
"in case of an invalid ACL, so we can't differentiate between\n"
"classes of errors. Other suggestions are welcome.\n"
"\n"
":return: 0 or 1\n"
":rtype: integer\n"
;
/* Checks the ACL for validity */
static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
if(acl_valid(self->acl) == -1) {
Py_RETURN_FALSE;
} else {
Py_RETURN_TRUE;
}
}
#ifdef HAVE_ACL_COPY_EXT
static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
PyObject *ret;
ssize_t size, nsize;
char *buf;
size = acl_size(self->acl);
if(size == -1)
return PyErr_SetFromErrno(PyExc_IOError);
if((ret = PyBytes_FromStringAndSize(NULL, size)) == NULL)
return NULL;
buf = PyBytes_AsString(ret);
if((nsize = acl_copy_ext(buf, self->acl, size)) == -1) {
Py_DECREF(ret);
return PyErr_SetFromErrno(PyExc_IOError);
}
return ret;
}
static PyObject* ACL_set_state(PyObject *obj, PyObject* args) {
ACL_Object *self = (ACL_Object*) obj;
const void *buf;
int bufsize;
acl_t ptr;
/* Parse the argument */
if (!PyArg_ParseTuple(args, "s#", &buf, &bufsize))
return NULL;
/* Try to import the external representation */
if((ptr = acl_copy_int(buf)) == NULL)
return PyErr_SetFromErrno(PyExc_IOError);
/* Free the old acl. Should we ignore errors here? */
if(self->acl != NULL) {
if(acl_free(self->acl) == -1)
return PyErr_SetFromErrno(PyExc_IOError);
}
self->acl = ptr;
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
#endif
#ifdef HAVE_LEVEL2
/* tp_iter for the ACL type; since it can be iterated only
* destructively, the type is its iterator
*/
static PyObject* ACL_iter(PyObject *obj) {
ACL_Object *self = (ACL_Object*)obj;
self->entry_id = ACL_FIRST_ENTRY;
Py_INCREF(obj);
return obj;
}
/* the tp_iternext function for the ACL type */
static PyObject* ACL_iternext(PyObject *obj) {
ACL_Object *self = (ACL_Object*)obj;
acl_entry_t the_entry_t;
Entry_Object *the_entry_obj;
int nerr;
nerr = acl_get_entry(self->acl, self->entry_id, &the_entry_t);
self->entry_id = ACL_NEXT_ENTRY;
if(nerr == -1)
return PyErr_SetFromErrno(PyExc_IOError);
else if(nerr == 0) {
/* Docs says this is not needed */
/*PyErr_SetObject(PyExc_StopIteration, Py_None);*/
return NULL;
}
the_entry_obj = (Entry_Object*) PyType_GenericNew(&Entry_Type, NULL, NULL);
if(the_entry_obj == NULL)
return NULL;
the_entry_obj->entry = the_entry_t;
the_entry_obj->parent_acl = obj;
Py_INCREF(obj); /* For the reference we have in entry->parent */
return (PyObject*)the_entry_obj;
}
static char __ACL_delete_entry_doc__[] =
"delete_entry(entry)\n"
"Deletes an entry from the ACL.\n"
"\n"
".. note:: Only available with level 2.\n"
"\n"
":param entry: the Entry object which should be deleted; note that after\n"
" this function is called, that object is unusable any longer\n"
" and should be deleted\n"
;
/* Deletes an entry from the ACL */
static PyObject* ACL_delete_entry(PyObject *obj, PyObject *args) {
ACL_Object *self = (ACL_Object*)obj;
Entry_Object *e;
if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &e))
return NULL;
if(acl_delete_entry(self->acl, e->entry) == -1)
return PyErr_SetFromErrno(PyExc_IOError);
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
static char __ACL_calc_mask_doc__[] =
"Compute the file group class mask.\n"
"\n"
"The calc_mask() method calculates and sets the permissions \n"
"associated with the :py:data:`ACL_MASK` Entry of the ACL.\n"
"The value of the new permissions is the union of the permissions \n"
"granted by all entries of tag type :py:data:`ACL_GROUP`, \n"
":py:data:`ACL_GROUP_OBJ`, or \n"
":py:data:`ACL_USER`. If the ACL already contains an :py:data:`ACL_MASK`\n"
"entry, its \n"
"permissions are overwritten; if it does not contain an \n"
":py:data:`ACL_MASK` Entry, one is added.\n"
"\n"
"The order of existing entries in the ACL is undefined after this \n"
"function.\n"
;
/* Updates the mask entry in the ACL */
static PyObject* ACL_calc_mask(PyObject *obj, PyObject *args) {
ACL_Object *self = (ACL_Object*)obj;
if(acl_calc_mask(&self->acl) == -1)
return PyErr_SetFromErrno(PyExc_IOError);
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
static char __ACL_append_doc__[] =
"append([entry])\n"
"Append a new Entry to the ACL and return it.\n"
"\n"
"This is a convenience function to create a new Entry \n"
"and append it to the ACL.\n"
"If a parameter of type Entry instance is given, the \n"
"entry will be a copy of that one (as if copied with \n"
":py:func:`Entry.copy`), otherwise, the new entry will be empty.\n"
"\n"
":rtype: :py:class:`Entry`\n"
":returns: the newly created entry\n"
;
/* Convenience method to create a new Entry */
static PyObject* ACL_append(PyObject *obj, PyObject *args) {
ACL_Object* self = (ACL_Object*) obj;
Entry_Object* newentry;
Entry_Object* oldentry = NULL;
int nret;
newentry = (Entry_Object*)PyType_GenericNew(&Entry_Type, NULL, NULL);
if(newentry == NULL) {
return NULL;
}
if (!PyArg_ParseTuple(args, "|O!", &Entry_Type, &oldentry)) {
Py_DECREF(newentry);
return NULL;
}
nret = acl_create_entry(&self->acl, &newentry->entry);
if(nret == -1) {
Py_DECREF(newentry);
return PyErr_SetFromErrno(PyExc_IOError);
}
if(oldentry != NULL) {
nret = acl_copy_entry(newentry->entry, oldentry->entry);
if(nret == -1) {
Py_DECREF(newentry);
return PyErr_SetFromErrno(PyExc_IOError);
}
}
newentry->parent_acl = obj;
Py_INCREF(obj);
return (PyObject*)newentry;
}
/***** Entry type *****/
typedef struct {
acl_tag_t tag;
union {
uid_t uid;
gid_t gid;
};
} tag_qual;
/* Pre-declaring the function is more friendly to cpychecker, sigh. */
static int get_tag_qualifier(acl_entry_t entry, tag_qual *tq)
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
/* Helper function to get the tag and qualifier of an Entry at the
same time. This is "needed" because the acl_get_qualifier function
returns a pointer to different types, based on the tag value, and
thus it's not straightforward to get the right type.
It sets a Python exception if an error occurs, and returns -1 in
this case. If successful, the tag is set to the tag type, the
qualifier (if any) to either the uid or the gid entry in the
tag_qual structure, and the return value is 0.
*/
static int get_tag_qualifier(acl_entry_t entry, tag_qual *tq) {
void *p;
if(acl_get_tag_type(entry, &tq->tag) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
if (tq->tag == ACL_USER || tq->tag == ACL_GROUP) {
if((p = acl_get_qualifier(entry)) == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
if (tq->tag == ACL_USER) {
tq->uid = *(uid_t*)p;
} else {
tq->gid = *(gid_t*)p;
}
acl_free(p);
}
return 0;
}
/* Creation of a new Entry instance */
static PyObject* Entry_new(PyTypeObject* type, PyObject* args,
PyObject *keywds) {
PyObject* newentry;
newentry = PyType_GenericNew(type, args, keywds);
if(newentry != NULL) {
((Entry_Object*)newentry)->entry = NULL;
((Entry_Object*)newentry)->parent_acl = NULL;
}
return newentry;
}
/* Initialization of a new Entry instance */
static int Entry_init(PyObject* obj, PyObject* args, PyObject *keywds) {
Entry_Object* self = (Entry_Object*) obj;
ACL_Object* parent = NULL;
if (!PyArg_ParseTuple(args, "O!", &ACL_Type, &parent))
return -1;
if(acl_create_entry(&parent->acl, &self->entry) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
self->parent_acl = (PyObject*)parent;
Py_INCREF(parent);
return 0;
}
/* Free the Entry instance */
static void Entry_dealloc(PyObject* obj) {
Entry_Object *self = (Entry_Object*) obj;
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;
if (have_error)
PyErr_Fetch(&err_type, &err_value, &err_traceback);
if(self->parent_acl != NULL) {
Py_DECREF(self->parent_acl);
self->parent_acl = NULL;
}
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
PyObject_DEL(self);
}
/* Converts the entry to a text format */
static PyObject* Entry_str(PyObject *obj) {
PyObject *format, *kind;
Entry_Object *self = (Entry_Object*) obj;
tag_qual tq;
if(get_tag_qualifier(self->entry, &tq) < 0) {
return NULL;
}
format = MyString_FromString("ACL entry for ");
if(format == NULL)
return NULL;
switch(tq.tag) {
case ACL_UNDEFINED_TAG:
kind = MyString_FromString("undefined type");
break;
case ACL_USER_OBJ:
kind = MyString_FromString("the owner");
break;
case ACL_GROUP_OBJ:
kind = MyString_FromString("the group");
break;
case ACL_OTHER:
kind = MyString_FromString("the others");
break;
case ACL_USER:
/* FIXME: here and in the group case, we're formatting with
unsigned, because there's no way to automatically determine
the signed-ness of the types; on Linux(glibc) they're
unsigned, so we'll go along with that */
kind = MyString_FromFormat("user with uid %u", tq.uid);
break;
case ACL_GROUP:
kind = MyString_FromFormat("group with gid %u", tq.gid);
break;
case ACL_MASK:
kind = MyString_FromString("the mask");
break;
default:
kind = MyString_FromString("UNKNOWN_TAG_TYPE!");
break;
}
if (kind == NULL) {
Py_DECREF(format);
return NULL;
}
MyString_ConcatAndDel(&format, kind);
return format;
}
/* Sets the tag type of the entry */
static int Entry_set_tag_type(PyObject* obj, PyObject* value, void* arg) {
Entry_Object *self = (Entry_Object*) obj;
if(value == NULL) {
PyErr_SetString(PyExc_TypeError,
"tag type deletion is not supported");
return -1;
}
if(!PyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"tag type must be integer");
return -1;
}
if(acl_set_tag_type(self->entry, (acl_tag_t)PyInt_AsLong(value)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
return 0;
}
/* Returns the tag type of the entry */
static PyObject* Entry_get_tag_type(PyObject *obj, void* arg) {
Entry_Object *self = (Entry_Object*) obj;
acl_tag_t value;
if (self->entry == NULL) {
PyErr_SetString(PyExc_AttributeError, "entry attribute");
return NULL;
}
if(acl_get_tag_type(self->entry, &value) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return PyInt_FromLong(value);
}
/* Sets the qualifier (either uid_t or gid_t) for the entry,
* usable only if the tag type if ACL_USER or ACL_GROUP
*/
static int Entry_set_qualifier(PyObject* obj, PyObject* value, void* arg) {
Entry_Object *self = (Entry_Object*) obj;
long uidgid;
uid_t uid;
gid_t gid;
void *p;
acl_tag_t tag;
if(value == NULL) {
PyErr_SetString(PyExc_TypeError,
"qualifier deletion is not supported");
return -1;
}
if(!PyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"qualifier must be integer");
return -1;
}
if((uidgid = PyInt_AsLong(value)) == -1) {
if(PyErr_Occurred() != NULL) {
return -1;
}
}
/* Due to how acl_set_qualifier takes its argument, we have to do
this ugly dance with two variables and a pointer that will
point to one of them. */
if(acl_get_tag_type(self->entry, &tag) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
uid = uidgid;
gid = uidgid;
switch(tag) {
case ACL_USER:
if((long)uid != uidgid) {
PyErr_SetString(PyExc_OverflowError, "cannot assign given qualifier");
return -1;
} else {
p = &uid;
}
break;
case ACL_GROUP:
if((long)gid != uidgid) {
PyErr_SetString(PyExc_OverflowError, "cannot assign given qualifier");
return -1;
} else {
p = &gid;
}
break;
default:
PyErr_SetString(PyExc_TypeError,
"can only set qualifiers on ACL_USER or ACL_GROUP entries");
return -1;
}
if(acl_set_qualifier(self->entry, p) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
return 0;
}
/* Returns the qualifier of the entry */
static PyObject* Entry_get_qualifier(PyObject *obj, void* arg) {
Entry_Object *self = (Entry_Object*) obj;
long value;
tag_qual tq;
if (self->entry == NULL) {
PyErr_SetString(PyExc_AttributeError, "entry attribute");
return NULL;
}
if(get_tag_qualifier(self->entry, &tq) < 0) {
return NULL;
}
if (tq.tag == ACL_USER) {
value = tq.uid;
} else if (tq.tag == ACL_GROUP) {
value = tq.gid;
} else {
PyErr_SetString(PyExc_TypeError,
"given entry doesn't have an user or"
" group tag");
return NULL;
}
return PyInt_FromLong(value);
}
/* Returns the parent ACL of the entry */
static PyObject* Entry_get_parent(PyObject *obj, void* arg) {
Entry_Object *self = (Entry_Object*) obj;
Py_INCREF(self->parent_acl);
return self->parent_acl;
}
/* Returns the a new Permset representing the permset of the entry
* FIXME: Should return a new reference to the same object, which
* should be created at init time!
*/
static PyObject* Entry_get_permset(PyObject *obj, void* arg) {
Entry_Object *self = (Entry_Object*)obj;
PyObject *p;
Permset_Object *ps;
p = Permset_new(&Permset_Type, NULL, NULL);
if(p == NULL)
return NULL;
ps = (Permset_Object*)p;
if(acl_get_permset(self->entry, &ps->permset) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
Py_DECREF(p);