xref: /aosp_15_r20/external/selinux/libsemanage/src/semanageswig_python.i (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /* Author: Spencer Shimko <[email protected]>
2  *
3  * Copyright (C) 2004-2005 Tresys Technology, LLC
4  * Copyright (C) 2006 Red Hat, Inc
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 St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /** standard typemaps **/
22 
23 %header %{
24 	#include <stdlib.h>
25 	#include <semanage/semanage.h>
26 	#include <sys/mman.h>
27 
28 	#define STATUS_SUCCESS 0
29 	#define STATUS_ERR -1
30 %}
31 
32 %include "stdint.i"
33 
34 %wrapper %{
35 
36 
37 	/* There are two ways to call this function:
38 	 * One is with a valid swig_type and destructor.
39 	 * Two is with a NULL swig_type and NULL destructor.
40 	 *
41 	 * In the first mode, the function converts
42 	 * an array of *cloned* objects [of the given pointer swig type]
43 	 * into a PyList, and destroys the array in the process
44 	 * (the objects pointers are preserved).
45 	 *
46 	 * In the second mode, the function converts
47 	 * an array of *constant* strings into a PyList, and destroys
48 	 * the array in the process
49 	 * (the strings are copied, originals not freed). */
50 
semanage_array2plist(semanage_handle_t * handle,void ** arr,unsigned int asize,swig_type_info * swig_type,void (* destructor)(void *),PyObject ** result)51 	static int semanage_array2plist(
52 		semanage_handle_t* handle,
53 		void** arr,
54 		unsigned int asize,
55 		swig_type_info* swig_type,
56 		void (*destructor) (void*),
57 		PyObject** result) {
58 
59 		PyObject* plist = PyList_New(0);
60 		unsigned int i;
61 
62 		if (!plist)
63 			goto err;
64 
65 		for (i = 0; i < asize; i++)  {
66 
67 			PyObject* obj = NULL;
68 
69 			/* NULL indicates string conversion,
70 			 * otherwise create an opaque pointer */
71 			if (!swig_type)
72 				obj = SWIG_FromCharPtr(arr[i]);
73 			else
74 				obj = SWIG_NewPointerObj(arr[i], swig_type, 0);
75 
76 			if (!obj)
77 				goto err;
78 
79 			if (PyList_Append(plist, obj) < 0)
80 				goto err;
81 		}
82 
83 		free(arr);
84 
85 		*result = plist;
86 		return STATUS_SUCCESS;
87 
88 		err:
89 		for (i = 0; i < asize; i++)
90 			if (destructor)
91 				destructor(arr[i]);
92 		free(arr);
93 		return STATUS_ERR;
94 	}
95 %}
96 /* a few helpful typemaps are available in this library */
97 %include <typemaps.i>
98 /* wrap all int*'s so they can be used for results
99    if it becomes necessary to send in data this should be changed to INOUT */
100 %apply int *OUTPUT { int * };
101 %apply int *OUTPUT { size_t * };
102 %apply int *OUTPUT { unsigned int * };
103 %apply int *OUTPUT { uint16_t * };
104 
105 %include <cstring.i>
106 /* This is needed to properly mmap binary data in SWIG */
107 %cstring_output_allocate_size(void **mapped_data, size_t *data_len, munmap(*$1, *$2));
108 
109 %typemap(in, numinputs=0) char **(char *temp=NULL) {
110 	$1 = &temp;
111 }
112 
113 %typemap(argout) char** {
114 	$result = SWIG_AppendOutput($result, SWIG_FromCharPtr(*$1));
115 	free(*$1);
116 }
117 
118 %typemap(in, numinputs=0) char ***(char **temp=NULL) {
119 	$1 = &temp;
120 }
121 
122 %typemap(argout) (
123  	semanage_handle_t* handle,
124 	const semanage_user_t* user,
125 	const char*** roles_arr,
126 	unsigned int* num_roles) {
127 
128 	if ($result) {
129 		int value;
130 		SWIG_AsVal_int($result, &value);
131 		if (value >= 0) {
132 			PyObject* plist = NULL;
133 			if (semanage_array2plist($1, (void**) *$3, *$4,
134                         	NULL, NULL, &plist) < 0)
135 				$result = SWIG_From_int(STATUS_ERR);
136 			else
137 				$result = SWIG_AppendOutput($result, plist);
138 		}
139 	}
140 }
141 
142 /** module typemaps**/
143 
144 /* the wrapper will setup this parameter for passing... the resulting python functions
145    will not take the semanage_module_info_t ** parameter */
146 %typemap(in, numinputs=0) semanage_module_info_t **(semanage_module_info_t *temp=NULL) {
147 	$1 = &temp;
148 }
149 
150 %typemap(argout) semanage_module_info_t ** {
151 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
152 }
153 
154 /** module key typemaps **/
155 
156 /* the wrapper will setup this parameter for passing... the resulting python functions
157    will not take the semanage_module_key_t ** parameter */
158 %typemap(in, numinputs=0) semanage_module_key_t **(semanage_module_key_t *temp=NULL) {
159 	$1 = &temp;
160 }
161 
162 %typemap(argout) semanage_module_key_t ** {
163 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
164 }
165 
166 /** context typemaps **/
167 
168 /* the wrapper will setup this parameter for passing... the resulting python functions
169    will not take the semanage_context_t ** parameter */
170 %typemap(in, numinputs=0) semanage_context_t **(semanage_context_t *temp=NULL) {
171 	$1 = &temp;
172 }
173 
174 %typemap(argout) semanage_context_t** {
175 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
176 }
177 
178 /** boolean typemaps **/
179 
180 /* the wrapper will setup this parameter for passing... the resulting python functions
181    will not take the semanage_bool_t *** parameter */
182 %typemap(in, numinputs=0) semanage_bool_t ***(semanage_bool_t **temp=NULL) {
183 	$1 = &temp;
184 }
185 
186 %typemap(argout) (
187  	semanage_handle_t* handle,
188 	semanage_bool_t*** records,
189 	unsigned int* count) {
190 
191 	if ($result) {
192 		int value;
193 		SWIG_AsVal_int($result, &value);
194 		if (value >= 0) {
195 			PyObject* plist = NULL;
196 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_bool,
197 				(void (*) (void*)) &semanage_bool_free, &plist) < 0)
198 				$result = SWIG_From_int(STATUS_ERR);
199 			else
200 				$result = SWIG_AppendOutput($result, plist);
201 		}
202 	}
203 }
204 
205 %typemap(in, numinputs=0) semanage_bool_t **(semanage_bool_t *temp=NULL) {
206 	$1 = &temp;
207 }
208 
209 %typemap(argout) semanage_bool_t ** {
210 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
211 }
212 
213 %typemap(argout) semanage_bool_key_t ** {
214 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
215 }
216 
217 %typemap(in, numinputs=0) semanage_bool_key_t **(semanage_bool_key_t *temp=NULL) {
218 	$1 = &temp;
219 }
220 
221 /** fcontext typemaps **/
222 
223 /* the wrapper will setup this parameter for passing... the resulting python functions
224    will not take the semanage_fcontext_t *** parameter */
225 %typemap(in, numinputs=0) semanage_fcontext_t ***(semanage_fcontext_t **temp=NULL) {
226         $1 = &temp;
227 }
228 
229 %typemap(argout) (
230  	semanage_handle_t* handle,
231 	semanage_fcontext_t*** records,
232 	unsigned int* count) {
233 
234 	if ($result) {
235 		int value;
236 		SWIG_AsVal_int($result, &value);
237 		if (value >= 0) {
238 			PyObject* plist = NULL;
239 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_fcontext,
240 				(void (*) (void*)) &semanage_fcontext_free, &plist) < 0)
241 				$result = SWIG_From_int(STATUS_ERR);
242 			else
243 				$result = SWIG_AppendOutput($result, plist);
244 		}
245 	}
246 }
247 
248 %typemap(in, numinputs=0) semanage_fcontext_t **(semanage_fcontext_t *temp=NULL) {
249         $1 = &temp;
250 }
251 
252 %typemap(argout) semanage_fcontext_t ** {
253 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
254 }
255 
256 %typemap(argout) semanage_fcontext_key_t ** {
257         $result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
258 }
259 
260 %typemap(in, numinputs=0) semanage_fcontext_key_t **(semanage_fcontext_key_t *temp=NULL) {
261         $1 = &temp;
262 }
263 
264 /** interface typemaps **/
265 
266 /* the wrapper will setup this parameter for passing... the resulting python functions
267    will not take the semanage_iface_t *** parameter */
268 %typemap(in, numinputs=0) semanage_iface_t ***(semanage_iface_t **temp=NULL) {
269 	$1 = &temp;
270 }
271 
272 
273 %typemap(argout) (
274  	semanage_handle_t* handle,
275 	semanage_iface_t*** records,
276 	unsigned int* count) {
277 
278 	if ($result) {
279 		int value;
280 		SWIG_AsVal_int($result, &value);
281 		if (value >= 0) {
282 			PyObject* plist = NULL;
283 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_iface,
284 				(void (*) (void*)) &semanage_iface_free, &plist) < 0)
285 				$result = SWIG_From_int(STATUS_ERR);
286 			else
287 				$result = SWIG_AppendOutput($result, plist);
288 		}
289 	}
290 }
291 
292 %typemap(in, numinputs=0) semanage_iface_t **(semanage_iface_t *temp=NULL) {
293 	$1 = &temp;
294 }
295 
296 %typemap(argout) semanage_iface_t ** {
297 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
298 }
299 
300 %typemap(argout) semanage_iface_key_t ** {
301 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
302 }
303 
304 %typemap(in, numinputs=0) semanage_iface_key_t **(semanage_iface_key_t *temp=NULL) {
305 	$1 = &temp;
306 }
307 
308 /** seuser typemaps **/
309 
310 /* the wrapper will setup this parameter for passing... the resulting python functions
311    will not take the semanage_seuser_t *** parameter */
312 %typemap(in, numinputs=0) semanage_seuser_t ***(semanage_seuser_t **temp=NULL) {
313 	$1 = &temp;
314 }
315 
316 
317 %typemap(argout) (
318  	semanage_handle_t* handle,
319 	semanage_seuser_t*** records,
320 	unsigned int* count) {
321 
322 	if ($result) {
323 		int value;
324 		SWIG_AsVal_int($result, &value);
325 		if (value >= 0) {
326 			PyObject* plist = NULL;
327 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_seuser,
328 				(void (*) (void*)) &semanage_seuser_free, &plist) < 0)
329 				$result = SWIG_From_int(STATUS_ERR);
330 			else
331 				$result = SWIG_AppendOutput($result, plist);
332 		}
333 	}
334 }
335 
336 %typemap(in, numinputs=0) semanage_seuser_t **(semanage_seuser_t *temp=NULL) {
337 	$1 = &temp;
338 }
339 
340 %typemap(argout) semanage_seuser_t ** {
341 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
342 }
343 
344 %typemap(argout) semanage_seuser_key_t ** {
345 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
346 }
347 
348 %typemap(in, numinputs=0) semanage_seuser_key_t **(semanage_seuser_key_t *temp=NULL) {
349 	$1 = &temp;
350 }
351 
352 /** user typemaps **/
353 
354 /* the wrapper will setup this parameter for passing... the resulting python functions
355    will not take the semanage_user_t *** parameter */
356 %typemap(in, numinputs=0) semanage_user_t ***(semanage_user_t **temp=NULL) {
357 	$1 = &temp;
358 }
359 
360 %typemap(argout) (
361  	semanage_handle_t* handle,
362 	semanage_user_t*** records,
363 	unsigned int* count) {
364 
365 	if ($result) {
366 		int value;
367 		SWIG_AsVal_int($result, &value);
368 		if (value >= 0) {
369 			PyObject* plist = NULL;
370 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_user,
371 				(void (*) (void*)) &semanage_user_free, &plist) < 0)
372 				$result = SWIG_From_int(STATUS_ERR);
373 			else
374 				$result = SWIG_AppendOutput($result, plist);
375 		}
376 	}
377 }
378 
379 %typemap(in, numinputs=0) semanage_user_t **(semanage_user_t *temp=NULL) {
380 	$1 = &temp;
381 }
382 
383 %typemap(argout) semanage_user_t ** {
384 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
385 }
386 
387 %typemap(argout) semanage_user_key_t ** {
388 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
389 }
390 
391 %typemap(in, numinputs=0) semanage_user_key_t **(semanage_user_key_t *temp=NULL) {
392 	$1 = &temp;
393 }
394 
395 /** port typemaps **/
396 
397 /* the wrapper will setup this parameter for passing... the resulting python functions
398    will not take the semanage_port_t *** parameter */
399 %typemap(in, numinputs=0) semanage_port_t ***(semanage_port_t **temp=NULL) {
400 	$1 = &temp;
401 }
402 
403 %typemap(argout) (
404  	semanage_handle_t* handle,
405 	semanage_port_t*** records,
406 	unsigned int* count) {
407 
408 	if ($result) {
409 		int value;
410 		SWIG_AsVal_int($result, &value);
411 		if (value >= 0) {
412 			PyObject* plist = NULL;
413 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_port,
414 				(void (*) (void*)) &semanage_port_free, &plist) < 0)
415 				$result = SWIG_From_int(STATUS_ERR);
416 			else
417 				$result = SWIG_AppendOutput($result, plist);
418 		}
419 	}
420 }
421 
422 %typemap(in, numinputs=0) semanage_port_t **(semanage_port_t *temp=NULL) {
423 	$1 = &temp;
424 }
425 
426 %typemap(argout) semanage_port_t ** {
427 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
428 }
429 
430 %typemap(argout) semanage_port_key_t ** {
431 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
432 }
433 
434 %typemap(in, numinputs=0) semanage_port_key_t **(semanage_port_key_t *temp=NULL) {
435 	$1 = &temp;
436 }
437 
438 /** ibpkey typemaps **/
439 
440 /* the wrapper will setup this parameter for passing... the resulting python functions
441    will not take the semanage_ibpkey_t *** parameter */
442 %typemap(in, numinputs=0) semanage_ibpkey_t ***(semanage_ibpkey_t **temp=NULL) {
443 	$1 = &temp;
444 }
445 
446 %typemap(argout) (
447 	semanage_handle_t* handle,
448 	semanage_ibpkey_t*** records,
449 	unsigned int* count) {
450 
451 	if ($result) {
452 		int value;
453 		SWIG_AsVal_int($result, &value);
454 		if (value >= 0) {
455 			PyObject* plist = NULL;
456 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_ibpkey,
457 				(void (*) (void*)) &semanage_ibpkey_free, &plist) < 0)
458 				$result = SWIG_From_int(STATUS_ERR);
459 			else
460 				$result = SWIG_AppendOutput($result, plist);
461 		}
462 	}
463 }
464 
465 %typemap(in, numinputs=0) semanage_ibpkey_t **(semanage_ibpkey_t *temp=NULL) {
466 	$1 = &temp;
467 }
468 
469 %typemap(argout) semanage_ibpkey_t ** {
470 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
471 }
472 
473 %typemap(argout) semanage_ibpkey_key_t ** {
474 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
475 }
476 
477 %typemap(in, numinputs=0) semanage_ibpkey_key_t **(semanage_ibpkey_key_t *temp=NULL) {
478 	$1 = &temp;
479 }
480 
481 /** ibendport typemaps **/
482 
483 /* the wrapper will setup this parameter for passing... the resulting python functions
484    will not take the semanage_ibendport_t *** parameter */
485 %typemap(in, numinputs=0) semanage_ibendport_t ***(semanage_ibendport_t **temp=NULL) {
486 	$1 = &temp;
487 }
488 
489 %typemap(argout) (
490 	semanage_handle_t* handle,
491 	semanage_ibendport_t*** records,
492 	unsigned int* count) {
493 
494 	if ($result) {
495 		int value;
496 		SWIG_AsVal_int($result, &value);
497 		if (value >= 0) {
498 			PyObject* plist = NULL;
499 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_ibendport,
500 				(void (*) (void*)) &semanage_ibendport_free, &plist) < 0)
501 				$result = SWIG_From_int(STATUS_ERR);
502 			else
503 				$result = SWIG_AppendOutput($result, plist);
504 		}
505 	}
506 }
507 
508 %typemap(in, numinputs=0) semanage_ibendport_t **(semanage_ibendport_t *temp=NULL) {
509 	$1 = &temp;
510 }
511 
512 %typemap(argout) semanage_ibendport_t ** {
513 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
514 }
515 
516 %typemap(argout) semanage_ibendport_key_t ** {
517 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
518 }
519 
520 %typemap(in, numinputs=0) semanage_ibendport_key_t **(semanage_ibendport_key_t *temp=NULL) {
521 	$1 = &temp;
522 }
523 
524 /** node typemaps **/
525 
526 /* the wrapper will setup this parameter for passing... the resulting python functions
527    will not take the semanage_node_t *** parameter */
528 %typemap(in, numinputs=0) semanage_node_t ***(semanage_node_t **temp=NULL) {
529 	$1 = &temp;
530 }
531 
532 %typemap(argout) (
533  	semanage_handle_t* handle,
534 	semanage_node_t*** records,
535 	unsigned int* count) {
536 
537 	if ($result) {
538 		int value;
539 		SWIG_AsVal_int($result, &value);
540 		if (value >= 0) {
541 			PyObject* plist = NULL;
542 			if (semanage_array2plist($1, (void**) *$2, *$3, SWIGTYPE_p_semanage_node,
543 				(void (*) (void*)) &semanage_node_free, &plist) < 0)
544 				$result = SWIG_From_int(STATUS_ERR);
545 			else
546 				$result = SWIG_AppendOutput($result, plist);
547 		}
548 	}
549 }
550 
551 %typemap(in, numinputs=0) semanage_node_t **(semanage_node_t *temp=NULL) {
552 	$1 = &temp;
553 }
554 
555 %typemap(argout) semanage_node_t ** {
556 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
557 }
558 
559 
560 %typemap(argout) semanage_node_key_t ** {
561 	$result = SWIG_AppendOutput($result, SWIG_NewPointerObj(*$1, $*1_descriptor, 0));
562 }
563 
564 %typemap(in, numinputs=0) semanage_node_key_t **(semanage_node_key_t *temp=NULL) {
565 	$1 = &temp;
566 }
567 
568 %include "semanageswig_python_exception.i"
569 %include "semanageswig.i"
570