python3 port for policy module
[metze/samba/wip.git] / source4 / lib / policy / pypolicy.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Python bindings for libpolicy
4  *  Copyright (C) Jelmer Vernooij 2010
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program 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
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include "python/py3compat.h"
23 #include "policy.h"
24 #include "libcli/util/pyerrors.h"
25
26 void initpolicy(void);
27
28 static PyObject *py_get_gpo_flags(PyObject *self, PyObject *args)
29 {
30         int flags;
31         PyObject *py_ret;
32         const char **ret;
33         TALLOC_CTX *mem_ctx;
34         int i;
35         NTSTATUS status;
36
37         if (!PyArg_ParseTuple(args, "i", &flags))
38                 return NULL;
39
40         mem_ctx = talloc_new(NULL);
41         if (mem_ctx == NULL) {
42                 PyErr_NoMemory();
43                 return NULL;
44         }
45
46         status = gp_get_gpo_flags(mem_ctx, flags, &ret);
47         if (!NT_STATUS_IS_OK(status)) {
48                 PyErr_SetNTSTATUS(status);
49                 talloc_free(mem_ctx);
50                 return NULL;
51         }
52
53         py_ret = PyList_New(0);
54         for (i = 0; ret[i]; i++) {
55                 PyObject *item = PyStr_FromString(ret[i]);
56                 if (item == NULL) {
57                         talloc_free(mem_ctx);
58                         Py_DECREF(py_ret);
59                         PyErr_NoMemory();
60                         return NULL;
61                 }
62                 PyList_Append(py_ret, item);
63         }
64
65         talloc_free(mem_ctx);
66
67         return py_ret;
68 }
69
70 static PyObject *py_get_gplink_options(PyObject *self, PyObject *args)
71 {
72         int flags;
73         PyObject *py_ret;
74         const char **ret;
75         TALLOC_CTX *mem_ctx;
76         int i;
77         NTSTATUS status;
78
79         if (!PyArg_ParseTuple(args, "i", &flags))
80                 return NULL;
81
82         mem_ctx = talloc_new(NULL);
83         if (mem_ctx == NULL) {
84                 PyErr_NoMemory();
85                 return NULL;
86         }
87
88         status = gp_get_gplink_options(mem_ctx, flags, &ret);
89         if (!NT_STATUS_IS_OK(status)) {
90                 PyErr_SetNTSTATUS(status);
91                 talloc_free(mem_ctx);
92                 return NULL;
93         }
94
95         py_ret = PyList_New(0);
96         for (i = 0; ret[i]; i++) {
97                 PyObject *item = PyStr_FromString(ret[i]);
98                 if (item == NULL) {
99                         talloc_free(mem_ctx);
100                         Py_DECREF(py_ret);
101                         PyErr_NoMemory();
102                         return NULL;
103                 }
104                 PyList_Append(py_ret, item);
105         }
106
107         talloc_free(mem_ctx);
108
109         return py_ret;
110 }
111
112 static PyObject *py_ads_to_dir_access_mask(PyObject *self, PyObject *args)
113 {
114         uint32_t access_mask, dir_mask;
115
116         if (! PyArg_ParseTuple(args, "I", &access_mask))
117                 return NULL;
118
119         dir_mask = gp_ads_to_dir_access_mask(access_mask);
120
121         return Py_BuildValue("I", dir_mask);
122 }
123
124
125 static PyMethodDef py_policy_methods[] = {
126         { "get_gpo_flags", (PyCFunction)py_get_gpo_flags, METH_VARARGS,
127                 "get_gpo_flags(flags) -> list" },
128         { "get_gplink_options", (PyCFunction)py_get_gplink_options, METH_VARARGS,
129                 "get_gplink_options(options) -> list" },
130         { "ads_to_dir_access_mask", (PyCFunction)py_ads_to_dir_access_mask, METH_VARARGS,
131                 "ads_to_dir_access_mask(access_mask) -> dir_mask" },
132         { NULL }
133 };
134
135 static struct PyModuleDef moduledef = {
136     PyModuleDef_HEAD_INIT,
137     .m_name = "policy",
138     .m_doc = "(Group) Policy manipulation",
139     .m_size = -1,
140     .m_methods = py_policy_methods,
141 };
142
143 MODULE_INIT_FUNC(policy)
144 {
145         PyObject *m = NULL;
146
147         m = PyModule_Create(&moduledef);
148         if (!m)
149                 return m;
150
151         PyModule_AddObject(m, "GPO_FLAG_USER_DISABLE",
152                                            PyInt_FromLong(GPO_FLAG_USER_DISABLE));
153         PyModule_AddObject(m, "GPO_MACHINE_USER_DISABLE",
154                                            PyInt_FromLong(GPO_FLAG_MACHINE_DISABLE));
155         PyModule_AddObject(m, "GPLINK_OPT_DISABLE",
156                                            PyInt_FromLong(GPLINK_OPT_DISABLE ));
157         PyModule_AddObject(m, "GPLINK_OPT_ENFORCE ",
158                                            PyInt_FromLong(GPLINK_OPT_ENFORCE ));
159         return m;
160 }