Avoid the use of PyAPI_DATA, which is for internal Python API's.
[nivanova/samba-autobuild/.git] / source4 / param / pyparam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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 "param/param.h"
23 #include "param/loadparm.h"
24 #include "lib/talloc/pytalloc.h"
25
26 /* There's no Py_ssize_t in 2.4, apparently */
27 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
28 typedef int Py_ssize_t;
29 typedef inquiry lenfunc;
30 #endif
31
32 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
33 #define PyLoadparmService_AsLoadparmService(obj) py_talloc_get_type(obj, struct loadparm_service)
34
35 extern PyTypeObject PyLoadparmContext;
36 extern PyTypeObject PyLoadparmService;
37
38 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
39 {
40         return py_talloc_reference(&PyLoadparmService, service);
41 }
42
43 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
44 {
45         struct parm_struct *parm = NULL;
46         void *parm_ptr = NULL;
47         int i;
48
49         if (service_name != NULL && strwicmp(service_name, GLOBAL_NAME) && 
50                 strwicmp(service_name, GLOBAL_NAME2)) {
51                 struct loadparm_service *service;
52                 /* its a share parameter */
53                 service = lpcfg_service(lp_ctx, service_name);
54                 if (service == NULL) {
55                         return NULL;
56                 }
57                 if (strchr(param_name, ':')) {
58                         /* its a parametric option on a share */
59                         const char *type = talloc_strndup(lp_ctx, param_name,
60                                                                                           strcspn(param_name, ":"));
61                         const char *option = strchr(param_name, ':') + 1;
62                         const char *value;
63                         if (type == NULL || option == NULL) {
64                         return NULL;
65                         }
66                         value = lpcfg_get_parametric(lp_ctx, service, type, option);
67                         if (value == NULL) {
68                         return NULL;
69                         }
70                         return PyString_FromString(value);
71                 }
72
73                 parm = lpcfg_parm_struct(param_name);
74                 if (parm == NULL || parm->pclass == P_GLOBAL) {
75                         return NULL;
76                 }
77                 parm_ptr = lpcfg_parm_ptr(lp_ctx, service, parm);
78     } else if (strchr(param_name, ':')) {
79                 /* its a global parametric option */
80                 const char *type = talloc_strndup(lp_ctx,
81                                   param_name, strcspn(param_name, ":"));
82                 const char *option = strchr(param_name, ':') + 1;
83                 const char *value;
84                 if (type == NULL || option == NULL) {
85                         return NULL;
86                 }
87                 value = lpcfg_get_parametric(lp_ctx, NULL, type, option);
88                 if (value == NULL)
89                         return NULL;
90                 return PyString_FromString(value);
91         } else {
92                 /* its a global parameter */
93                 parm = lpcfg_parm_struct(param_name);
94                 if (parm == NULL) {
95                         return NULL;
96                 }
97                 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
98         }
99
100         if (parm == NULL || parm_ptr == NULL) {
101                 return NULL;
102     }
103
104     /* construct and return the right type of python object */
105     switch (parm->type) {
106     case P_STRING:
107     case P_USTRING:
108         return PyString_FromString(*(char **)parm_ptr);
109     case P_BOOL:
110         return PyBool_FromLong(*(bool *)parm_ptr);
111     case P_INTEGER:
112     case P_OCTAL:
113     case P_BYTES:
114         return PyLong_FromLong(*(int *)parm_ptr);
115     case P_ENUM:
116         for (i=0; parm->enum_list[i].name; i++) {
117             if (*(int *)parm_ptr == parm->enum_list[i].value) {
118                 return PyString_FromString(parm->enum_list[i].name);
119             }
120         }
121         return NULL;
122     case P_LIST: 
123         {
124             int j;
125             const char **strlist = *(const char ***)parm_ptr;
126             PyObject *pylist;
127                 
128                 if(strlist == NULL) {
129                         return PyList_New(0);
130                 }
131                 
132                 pylist = PyList_New(str_list_length(strlist));
133             for (j = 0; strlist[j]; j++) 
134                 PyList_SetItem(pylist, j, 
135                                PyString_FromString(strlist[j]));
136             return pylist;
137         }
138
139         break;
140     }
141     return NULL;
142
143 }
144
145 static PyObject *py_lp_ctx_load(py_talloc_Object *self, PyObject *args)
146 {
147         char *filename;
148         bool ret;
149         if (!PyArg_ParseTuple(args, "s", &filename))
150                 return NULL;
151
152         ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
153
154         if (!ret) {
155                 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
156                 return NULL;
157         }
158         Py_RETURN_NONE;
159 }
160
161 static PyObject *py_lp_ctx_load_default(py_talloc_Object *self)
162 {
163         bool ret;
164         ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
165
166         if (!ret) {
167                 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
168                 return NULL;
169         }
170         Py_RETURN_NONE;
171 }
172
173 static PyObject *py_lp_ctx_get(py_talloc_Object *self, PyObject *args)
174 {
175         char *param_name;
176         char *section_name = NULL;
177         PyObject *ret;
178         if (!PyArg_ParseTuple(args, "s|z", &param_name, &section_name))
179                 return NULL;
180
181         ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
182         if (ret == NULL)
183                 Py_RETURN_NONE;
184         return ret;
185 }
186
187 static PyObject *py_lp_ctx_is_myname(py_talloc_Object *self, PyObject *args)
188 {
189         char *name;
190         if (!PyArg_ParseTuple(args, "s", &name))
191                 return NULL;
192
193         return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
194 }
195
196 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
197 {
198         char *name;
199         if (!PyArg_ParseTuple(args, "s", &name))
200                 return NULL;
201
202         return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
203 }
204
205 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
206 {
207         char *name, *value;
208         bool ret;
209         if (!PyArg_ParseTuple(args, "ss", &name, &value))
210                 return NULL;
211
212         ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
213         if (!ret) {
214                 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
215                 return NULL;
216         }
217
218         Py_RETURN_NONE;
219 }
220
221 static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
222 {
223         char *name, *path;
224         PyObject *ret;
225         if (!PyArg_ParseTuple(args, "s", &name))
226                 return NULL;
227
228         path = private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
229         ret = PyString_FromString(path);
230         talloc_free(path);
231
232         return ret;
233 }
234
235 static PyObject *py_lp_ctx_services(py_talloc_Object *self)
236 {
237         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
238         PyObject *ret;
239         int i;
240         ret = PyList_New(lpcfg_numservices(lp_ctx));
241         for (i = 0; i < lpcfg_numservices(lp_ctx); i++) {
242                 struct loadparm_service *service = lpcfg_servicebynum(lp_ctx, i);
243                 if (service != NULL) {
244                         PyList_SetItem(ret, i, PyString_FromString(lpcfg_servicename(service)));
245                 }
246         }
247         return ret;
248 }
249
250 static PyObject *py_lp_dump(PyObject *self, PyObject *args)
251 {
252         PyObject *py_stream;
253         bool show_defaults = false;
254         FILE *f;
255         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
256
257         if (!PyArg_ParseTuple(args, "O|b", &py_stream, &show_defaults))
258                 return NULL;
259
260         f = PyFile_AsFile(py_stream);
261         if (f == NULL) {
262                 PyErr_SetString(PyExc_TypeError, "Not a file stream");
263                 return NULL;
264         }
265
266         lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
267
268         Py_RETURN_NONE;
269 }
270
271
272 static PyMethodDef py_lp_ctx_methods[] = {
273         { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
274                 "S.load(filename) -> None\n"
275                 "Load specified file." },
276         { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
277                 "S.load_default() -> None\n"
278                 "Load default smb.conf file." },
279         { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
280                 "S.is_myname(name) -> bool\n"
281                 "Check whether the specified name matches one of our netbios names." },
282         { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
283                 "S.is_mydomain(name) -> bool\n"
284                 "Check whether the specified name matches our domain name." },
285         { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
286                 "S.get(name, service_name) -> value\n"
287                 "Find specified parameter." },
288         { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
289                 "S.set(name, value) -> bool\n"
290                 "Change a parameter." },
291         { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
292                 "S.private_path(name) -> path\n" },
293         { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
294                 "S.services() -> list" },
295         { "dump", (PyCFunction)py_lp_dump, METH_VARARGS, 
296                 "S.dump(stream, show_defaults=False)" },
297         { NULL }
298 };
299
300 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
301 {
302         return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
303 }
304
305 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
306 {
307         const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
308         if (configfile == NULL)
309                 Py_RETURN_NONE;
310         else
311                 return PyString_FromString(configfile);
312 }
313
314 static PyGetSetDef py_lp_ctx_getset[] = {
315         { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
316         { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
317           discard_const_p(char, "Name of last config file that was loaded.") },
318         { NULL }
319 };
320
321 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
322 {
323         py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0);
324         if (ret == NULL) {
325                 PyErr_NoMemory();
326                 return NULL;
327         }
328         ret->talloc_ctx = talloc_new(NULL);
329         if (ret->talloc_ctx == NULL) {
330                 PyErr_NoMemory();
331                 return NULL;
332         }
333         ret->ptr = loadparm_init(ret->talloc_ctx);
334         return (PyObject *)ret;
335 }
336
337 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
338 {
339         return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
340 }
341
342 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
343 {
344         struct loadparm_service *service;
345         if (!PyString_Check(name)) {
346                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
347                 return NULL;
348         }
349         service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
350         if (service == NULL) {
351                 PyErr_SetString(PyExc_KeyError, "No such section");
352                 return NULL;
353         }
354         return PyLoadparmService_FromService(service);
355 }
356
357 static PyMappingMethods py_lp_ctx_mapping = {
358         .mp_length = (lenfunc)py_lp_ctx_len,
359         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
360 };
361
362 PyTypeObject PyLoadparmContext = {
363         .tp_name = "LoadParm",
364         .tp_basicsize = sizeof(py_talloc_Object),
365         .tp_dealloc = py_talloc_dealloc,
366         .tp_getset = py_lp_ctx_getset,
367         .tp_methods = py_lp_ctx_methods,
368         .tp_new = py_lp_ctx_new,
369         .tp_as_mapping = &py_lp_ctx_mapping,
370         .tp_flags = Py_TPFLAGS_DEFAULT,
371 };
372
373 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
374 {
375         PyObject *py_stream;
376         bool show_defaults = false;
377         FILE *f;
378         struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
379         struct loadparm_service *default_service;
380         PyObject *py_default_service;
381
382         if (!PyArg_ParseTuple(args, "OO|b", &py_stream, &py_default_service,
383                                                   &show_defaults))
384                 return NULL;
385
386         f = PyFile_AsFile(py_stream);
387         if (f == NULL) {
388                 PyErr_SetString(PyExc_TypeError, "Not a file stream");
389                 return NULL;
390         }
391
392         if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
393                 PyErr_SetNone(PyExc_TypeError);
394                 return NULL;
395         }
396
397         default_service = PyLoadparmService_AsLoadparmService(py_default_service);
398
399         lpcfg_dump_one(f, show_defaults, service, default_service);
400
401         Py_RETURN_NONE;
402 }
403
404 static PyMethodDef py_lp_service_methods[] = {
405         { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS, 
406                 "S.dump(f, default_service, show_defaults=False)" },
407         { NULL }
408 };
409
410 PyTypeObject PyLoadparmService = {
411         .tp_name = "LoadparmService",
412         .tp_dealloc = py_talloc_dealloc,
413         .tp_basicsize = sizeof(py_talloc_Object),
414         .tp_methods = py_lp_service_methods,
415         .tp_flags = Py_TPFLAGS_DEFAULT,
416 };
417
418 static PyObject *py_default_path(PyObject *self)
419 {
420     return PyString_FromString(lp_default_path());
421 }
422
423 static PyMethodDef pyparam_methods[] = {
424     { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
425         "Returns the default smb.conf path." },
426     { NULL }
427 };
428
429 void initparam(void)
430 {
431         PyObject *m;
432
433         if (PyType_Ready(&PyLoadparmContext) < 0)
434                 return;
435
436         if (PyType_Ready(&PyLoadparmService) < 0)
437                 return;
438
439         m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
440         if (m == NULL)
441                 return;
442
443         Py_INCREF(&PyLoadparmContext);
444         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
445 }