Debian Codegraph @

libselinux/src/selinuxswig_python.i

raw
1 /* Author: James Athey
2 */
3
4 /* Never build rpm_execcon interface */
5 #ifndef DISABLE_RPM
6 #define DISABLE_RPM
7 #endif
8
9 %module selinux
10 %{
11 #include "selinux/selinux.h"
12 %}
13
14 %pythoncode %{
15
16 import shutil
17 import os
18
19 DISABLED = -1
20 PERMISSIVE = 0
21 ENFORCING = 1
22
23 def restorecon(path, recursive=False, verbose=False, force=False):
24 """ Restore SELinux context on a given path
25
26 Arguments:
27 path -- The pathname for the file or directory to be relabeled.
28
29 Keyword arguments:
30 recursive -- Change files and directories file labels recursively (default False)
31 verbose -- Show changes in file labels (default False)
32 force -- Force reset of context to match file_context for customizable files,
33 and the default file context, changing the user, role, range portion as well
34 as the type (default False)
35 """
36
37 restorecon_flags = SELINUX_RESTORECON_IGNORE_DIGEST | SELINUX_RESTORECON_REALPATH
38 if recursive:
39 restorecon_flags |= SELINUX_RESTORECON_RECURSE
40 if verbose:
41 restorecon_flags |= SELINUX_RESTORECON_VERBOSE
42 if force:
43 restorecon_flags |= SELINUX_RESTORECON_SET_SPECFILE_CTX
44 selinux_restorecon(os.path.expanduser(path), restorecon_flags)
45
46 def chcon(path, context, recursive=False):
47 """ Set the SELinux context on a given path """
48 lsetfilecon(path, context)
49 if recursive:
50 for root, dirs, files in os.walk(path):
51 for name in files + dirs:
52 lsetfilecon(os.path.join(root, name), context)
53
54 def copytree(src, dest):
55 """ An SELinux-friendly shutil.copytree method """
56 shutil.copytree(src, dest)
57 restorecon(dest, recursive=True)
58
59 def install(src, dest):
60 """ An SELinux-friendly shutil.move method """
61 shutil.move(src, dest)
62 restorecon(dest, recursive=True)
63 %}
64
65 /* security_get_boolean_names() typemap */
66 %typemap(argout) (char ***names, int *len) {
67 PyObject* list = PyList_New(*$2);
68 int i;
69 for (i = 0; i < *$2; i++) {
70 PyList_SetItem(list, i, PyString_FromString((*$1)[i]));
71 }
72 $result = SWIG_Python_AppendOutput($result, list);
73 }
74
75 /* return a sid along with the result */
76 %typemap(argout) (security_id_t * sid) {
77 if (*$1) {
78 %append_output(SWIG_NewPointerObj(*$1, $descriptor(security_id_t), 0));
79 } else {
80 Py_INCREF(Py_None);
81 %append_output(Py_None);
82 }
83 }
84
85 %typemap(in,numinputs=0) security_id_t *(security_id_t temp) {
86 $1 = &temp;
87 }
88
89 %typemap(in, numinputs=0) void *(char *temp=NULL) {
90 $1 = temp;
91 }
92
93 /* Makes security_compute_user() return a Python list of contexts */
94 %typemap(argout) (char ***con) {
95 PyObject* plist;
96 int i, len = 0;
97
98 if (*$1) {
99 while((*$1)[len])
100 len++;
101 plist = PyList_New(len);
102 for (i = 0; i < len; i++) {
103 PyList_SetItem(plist, i, PyString_FromString((*$1)[i]));
104 }
105 } else {
106 plist = PyList_New(0);
107 }
108
109 $result = SWIG_Python_AppendOutput($result, plist);
110 }
111
112 /* Makes functions in get_context_list.h return a Python list of contexts */
113 %typemap(argout) (char ***list) {
114 PyObject* plist;
115 int i;
116
117 if (*$1) {
118 plist = PyList_New(result);
119 for (i = 0; i < result; i++) {
120 PyList_SetItem(plist, i, PyString_FromString((*$1)[i]));
121 }
122 } else {
123 plist = PyList_New(0);
124 }
125 /* Only return the Python list, don't need to return the length anymore */
126 $result = plist;
127 }
128
129 %typemap(in,noblock=1,numinputs=0) char ** (char * temp = 0) {
130 $1 = &temp;
131 }
132 %typemap(freearg,match="in") char ** "";
133 %typemap(argout,noblock=1) char ** {
134 if (*$1) {
135 %append_output(SWIG_FromCharPtr(*$1));
136 freecon(*$1);
137 }
138 else {
139 Py_INCREF(Py_None);
140 %append_output(Py_None);
141 }
142 }
143
144 %typemap(in,noblock=1,numinputs=0) char ** (char * temp = 0) {
145 $1 = &temp;
146 }
147 %typemap(freearg,match="in") char ** "";
148 %typemap(argout,noblock=1) char ** {
149 if (*$1) {
150 %append_output(SWIG_FromCharPtr(*$1));
151 free(*$1);
152 }
153 else {
154 Py_INCREF(Py_None);
155 %append_output(Py_None);
156 }
157 }
158
159 %include "selinuxswig_python_exception.i"
160 %include "selinuxswig.i"