1/**
2 *  @file
3 *  Python bindings to search SELinux Policy rules.
4 *
5 *  @author Dan Walsh  <dwalsh@redhat.com>
6 *
7 *  Copyright (C) 2012 Red Hat, INC
8 *
9 *  This program is free software; you can redistribute it and/or modify
10 *  it under the terms of the GNU General Public License as published by
11 *  the Free Software Foundation; either version 2 of the License, or
12 *  (at your option) any later version.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23
24#include "Python.h"
25
26#ifdef UNUSED
27#elif defined(__GNUC__)
28# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
29#elif defined(__LCLINT__)
30# define UNUSED(x) /*@unused@*/ x
31#else
32# define UNUSED(x) x
33#endif
34
35#include "policy.h"
36apol_policy_t *policy = NULL;
37
38/* other */
39#include <errno.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <assert.h>
44
45#define COPYRIGHT_INFO "Copyright (C) 2003-2007 Tresys Technology, LLC"
46
47PyObject *wrap_policy(PyObject *UNUSED(self), PyObject *args){
48    const char *policy_file;
49    apol_vector_t *mod_paths = NULL;
50    apol_policy_path_type_e path_type = APOL_POLICY_PATH_TYPE_MONOLITHIC;
51    apol_policy_path_t *pol_path = NULL;
52
53    if (!PyArg_ParseTuple(args, "z", &policy_file))
54	    return NULL;
55
56    if (policy)
57	    apol_policy_destroy(&policy);
58
59    int policy_load_options = 0;
60
61    pol_path = apol_policy_path_create(path_type, policy_file, mod_paths);
62    if (!pol_path) {
63	    apol_vector_destroy(&mod_paths);
64	    PyErr_SetString(PyExc_RuntimeError,strerror(ENOMEM));
65	    return NULL;
66    }
67    apol_vector_destroy(&mod_paths);
68
69    policy = apol_policy_create_from_policy_path(pol_path, policy_load_options, NULL, NULL);
70    apol_policy_path_destroy(&pol_path);
71    if (!policy) {
72	    PyErr_SetString(PyExc_RuntimeError,strerror(errno));
73	    return NULL;
74    }
75
76    return Py_None;
77}
78
79static PyMethodDef methods[] = {
80	{"policy", (PyCFunction) wrap_policy, METH_VARARGS,
81		 "Initialize SELinux policy for use with search and info"},
82	{"info", (PyCFunction) wrap_info, METH_VARARGS,
83		 "Return SELinux policy info about types, attributes, roles, users"},
84	{"search", (PyCFunction) wrap_search, METH_VARARGS,
85	"Search SELinux Policy for allow, neverallow, auditallow, dontaudit and transition records"},
86	{NULL, NULL, 0, NULL}	/* sentinel */
87};
88
89void init_policy(void) {
90PyObject *m;
91m = Py_InitModule("_policy", methods);
92init_info(m);
93}
94