Add create_file to vfs_op_names
[Samba/gebeck_regimport.git] / source4 / param / pyparam.c
blob69c2613a0994276319cc21ff38192682ff21ba73
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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.
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.
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/>.
20 #include <stdint.h>
21 #include <stdbool.h>
23 #include "includes.h"
24 #include "param/param.h"
25 #include "param/loadparm.h"
26 #include "pytalloc.h"
28 /* There's no Py_ssize_t in 2.4, apparently */
29 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
30 typedef int Py_ssize_t;
31 typedef inquiry lenfunc;
32 #endif
34 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_ptr(obj)
36 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
37 PyAPI_DATA(PyTypeObject) PyLoadparmService;
39 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
41 return py_talloc_import(&PyLoadparmService, service);
44 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
46 struct parm_struct *parm = NULL;
47 void *parm_ptr = NULL;
48 int i;
50 if (service_name != NULL) {
51 struct loadparm_service *service;
52 /* its a share parameter */
53 service = lp_service(lp_ctx, service_name);
54 if (service == NULL) {
55 return NULL;
57 if (strchr(param_name, ':')) {
58 /* its a parametric option on a share */
59 const char *type = talloc_strndup(lp_ctx,
60 param_name,
61 strcspn(param_name, ":"));
62 const char *option = strchr(param_name, ':') + 1;
63 const char *value;
64 if (type == NULL || option == NULL) {
65 return NULL;
67 value = lp_get_parametric(lp_ctx, service, type, option);
68 if (value == NULL) {
69 return NULL;
71 return PyString_FromString(value);
74 parm = lp_parm_struct(param_name);
75 if (parm == NULL || parm->class == P_GLOBAL) {
76 return NULL;
78 parm_ptr = lp_parm_ptr(lp_ctx, service, parm);
79 } else if (strchr(param_name, ':')) {
80 /* its a global parametric option */
81 const char *type = talloc_strndup(lp_ctx,
82 param_name, strcspn(param_name, ":"));
83 const char *option = strchr(param_name, ':') + 1;
84 const char *value;
85 if (type == NULL || option == NULL) {
86 return NULL;
88 value = lp_get_parametric(lp_ctx, NULL, type, option);
89 if (value == NULL)
90 return NULL;
91 return PyString_FromString(value);
92 } else {
93 /* its a global parameter */
94 parm = lp_parm_struct(param_name);
95 if (parm == NULL) {
96 return NULL;
98 parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
101 if (parm == NULL || parm_ptr == NULL) {
102 return NULL;
105 /* construct and return the right type of python object */
106 switch (parm->type) {
107 case P_STRING:
108 case P_USTRING:
109 return PyString_FromString(*(char **)parm_ptr);
110 case P_BOOL:
111 return PyBool_FromLong(*(bool *)parm_ptr);
112 case P_INTEGER:
113 case P_OCTAL:
114 case P_BYTES:
115 return PyLong_FromLong(*(int *)parm_ptr);
116 case P_ENUM:
117 for (i=0; parm->enum_list[i].name; i++) {
118 if (*(int *)parm_ptr == parm->enum_list[i].value) {
119 return PyString_FromString(parm->enum_list[i].name);
122 return NULL;
123 case P_LIST:
125 int j;
126 const char **strlist = *(const char ***)parm_ptr;
127 PyObject *pylist = PyList_New(str_list_length(strlist));
128 for (j = 0; strlist[j]; j++)
129 PyList_SetItem(pylist, j,
130 PyString_FromString(strlist[j]));
131 return pylist;
134 break;
136 return NULL;
140 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
142 char *filename;
143 bool ret;
144 if (!PyArg_ParseTuple(args, "s", &filename))
145 return NULL;
147 ret = lp_load((struct loadparm_context *)self->ptr, filename);
149 if (!ret) {
150 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
151 return NULL;
153 return Py_None;
156 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
158 bool ret;
159 ret = lp_load_default(self->ptr);
161 if (!ret) {
162 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
163 return NULL;
165 return Py_None;
168 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
170 char *param_name;
171 char *section_name = NULL;
172 PyObject *ret;
173 if (!PyArg_ParseTuple(args, "s|s", &param_name, &section_name))
174 return NULL;
176 ret = py_lp_ctx_get_helper(self->ptr, section_name, param_name);
177 if (ret == NULL)
178 return Py_None;
179 return ret;
182 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
184 char *name;
185 if (!PyArg_ParseTuple(args, "s", &name))
186 return NULL;
188 return PyBool_FromLong(lp_is_myname(self->ptr, name));
191 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
193 char *name;
194 if (!PyArg_ParseTuple(args, "s", &name))
195 return NULL;
197 return PyBool_FromLong(lp_is_mydomain(self->ptr, name));
200 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
202 char *name, *value;
203 bool ret;
204 if (!PyArg_ParseTuple(args, "ss", &name, &value))
205 return NULL;
207 ret = lp_set_cmdline(self->ptr, name, value);
208 if (!ret) {
209 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
210 return NULL;
213 return Py_None;
216 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
218 char *name, *path;
219 PyObject *ret;
220 if (!PyArg_ParseTuple(args, "s", &name))
221 return NULL;
223 path = private_path(NULL, self->ptr, name);
224 ret = PyString_FromString(path);
225 talloc_free(path);
227 return ret;
230 static PyMethodDef py_lp_ctx_methods[] = {
231 { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,
232 "S.load(filename) -> None\n"
233 "Load specified file." },
234 { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
235 "S.load_default() -> None\n"
236 "Load default smb.conf file." },
237 { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
238 "S.is_myname(name) -> bool\n"
239 "Check whether the specified name matches one of our netbios names." },
240 { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
241 "S.is_mydomain(name) -> bool\n"
242 "Check whether the specified name matches our domain name." },
243 { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
244 "S.get(name, service_name) -> value\n"
245 "Find specified parameter." },
246 { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
247 "S.set(name, value) -> bool\n"
248 "Change a parameter." },
249 { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
250 "S.private_path(name) -> path\n" },
251 { NULL }
254 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
256 return PyLoadparmService_FromService(lp_default_service((struct loadparm_context *)self->ptr));
259 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
261 return PyString_FromString(lp_configfile(self->ptr));
264 static PyGetSetDef py_lp_ctx_getset[] = {
265 { (char *)"default_service", (getter)py_lp_ctx_default_service, NULL, NULL },
266 { (char *)"configfile", (getter)py_lp_ctx_config_file, NULL,
267 (char *)"Name of last config file that was loaded." },
268 { NULL }
271 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
273 return py_talloc_import(type, loadparm_init(NULL));
276 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
278 return lp_numservices(self->ptr);
281 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
283 struct loadparm_service *service;
284 if (!PyString_Check(name)) {
285 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
286 return NULL;
288 service = lp_service(self->ptr, PyString_AsString(name));
289 if (service == NULL) {
290 PyErr_SetString(PyExc_KeyError, "No such section");
291 return NULL;
293 return PyLoadparmService_FromService(service);
296 static PyMappingMethods py_lp_ctx_mapping = {
297 .mp_length = (lenfunc)py_lp_ctx_len,
298 .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
301 PyTypeObject PyLoadparmContext = {
302 .tp_name = "LoadParm",
303 .tp_basicsize = sizeof(py_talloc_Object),
304 .tp_dealloc = py_talloc_dealloc,
305 .tp_getset = py_lp_ctx_getset,
306 .tp_methods = py_lp_ctx_methods,
307 .tp_new = py_lp_ctx_new,
308 .tp_as_mapping = &py_lp_ctx_mapping,
309 .tp_flags = Py_TPFLAGS_DEFAULT,
312 PyTypeObject PyLoadparmService = {
313 .tp_name = "LoadparmService",
314 .tp_dealloc = py_talloc_dealloc,
315 .tp_basicsize = sizeof(py_talloc_Object),
316 .tp_flags = Py_TPFLAGS_DEFAULT,
319 _PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
321 struct loadparm_context *lp_ctx;
322 if (PyString_Check(py_obj)) {
323 lp_ctx = loadparm_init(NULL);
324 if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
325 talloc_free(lp_ctx);
326 PyErr_Format(PyExc_RuntimeError,
327 "Unable to load %s", PyString_AsString(py_obj));
328 return NULL;
330 return lp_ctx;
333 if (py_obj == Py_None) {
334 lp_ctx = loadparm_init(NULL);
335 /* We're not checking that loading the file succeeded *on purpose */
336 lp_load_default(lp_ctx);
337 return lp_ctx;
340 return PyLoadparmContext_AsLoadparmContext(py_obj);
343 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
345 struct loadparm_context *ret;
346 ret = loadparm_init(mem_ctx);
347 if (!lp_load_default(ret))
348 return NULL;
349 return ret;
352 void initparam(void)
354 PyObject *m;
356 if (PyType_Ready(&PyLoadparmContext) < 0)
357 return;
359 m = Py_InitModule3("param", NULL, "Parsing and writing Samba configuration files.");
360 if (m == NULL)
361 return;
363 Py_INCREF(&PyLoadparmContext);
364 PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);