source4/provision: fix talloc_steal on unallocated memory
[samba.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 "python/py3compat.h"
22 #include "includes.h"
23 #include "param/param.h"
24 #include "param/loadparm.h"
25 #include <pytalloc.h>
26 #include "dynconfig/dynconfig.h"
27
28 #define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
29 #define PyLoadparmService_AsLoadparmService(obj) pytalloc_get_type(obj, struct loadparm_service)
30
31 extern PyTypeObject PyLoadparmContext;
32 extern PyTypeObject PyLoadparmService;
33
34 static PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
35 {
36         return pytalloc_reference(&PyLoadparmService, service);
37 }
38
39 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
40 {
41         struct parm_struct *parm = NULL;
42         void *parm_ptr = NULL;
43         int i;
44
45         if (service_name != NULL && strwicmp(service_name, GLOBAL_NAME) && 
46                 strwicmp(service_name, GLOBAL_NAME2)) {
47                 struct loadparm_service *service;
48                 /* its a share parameter */
49                 service = lpcfg_service(lp_ctx, service_name);
50                 if (service == NULL) {
51                         return NULL;
52                 }
53                 if (strchr(param_name, ':')) {
54                         /* its a parametric option on a share */
55                         const char *type = talloc_strndup(lp_ctx, param_name,
56                                                                                           strcspn(param_name, ":"));
57                         const char *option = strchr(param_name, ':') + 1;
58                         const char *value;
59                         if (type == NULL || option == NULL) {
60                         return NULL;
61                         }
62                         value = lpcfg_get_parametric(lp_ctx, service, type, option);
63                         if (value == NULL) {
64                         return NULL;
65                         }
66                         return PyStr_FromString(value);
67                 }
68
69                 parm = lpcfg_parm_struct(lp_ctx, param_name);
70                 if (parm == NULL || parm->p_class == P_GLOBAL) {
71                         return NULL;
72                 }
73                 parm_ptr = lpcfg_parm_ptr(lp_ctx, service, parm);
74     } else if (strchr(param_name, ':')) {
75                 /* its a global parametric option */
76                 const char *type = talloc_strndup(lp_ctx,
77                                   param_name, strcspn(param_name, ":"));
78                 const char *option = strchr(param_name, ':') + 1;
79                 const char *value;
80                 if (type == NULL || option == NULL) {
81                         return NULL;
82                 }
83                 value = lpcfg_get_parametric(lp_ctx, NULL, type, option);
84                 if (value == NULL)
85                         return NULL;
86                 return PyStr_FromString(value);
87         } else {
88                 /* its a global parameter */
89                 parm = lpcfg_parm_struct(lp_ctx, param_name);
90                 if (parm == NULL) {
91                         return NULL;
92                 }
93                 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, parm);
94         }
95
96         if (parm == NULL || parm_ptr == NULL) {
97                 return NULL;
98     }
99
100     /* construct and return the right type of python object */
101     switch (parm->type) {
102     case P_CHAR:
103         return PyStr_FromFormat("%c", *(char *)parm_ptr);
104     case P_STRING:
105     case P_USTRING:
106         return PyStr_FromString(*(char **)parm_ptr);
107     case P_BOOL:
108         return PyBool_FromLong(*(bool *)parm_ptr);
109     case P_BOOLREV:
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 PyStr_FromString(parm->enum_list[i].name);
119             }
120         }
121         return NULL;
122     case P_CMDLIST:
123     case P_LIST: 
124         {
125             int j;
126             const char **strlist = *(const char ***)parm_ptr;
127             PyObject *pylist;
128                 
129             if(strlist == NULL) {
130                     return PyList_New(0);
131             }
132                 
133             pylist = PyList_New(str_list_length(strlist));
134             for (j = 0; strlist[j]; j++) 
135                 PyList_SetItem(pylist, j, 
136                                PyStr_FromString(strlist[j]));
137             return pylist;
138         }
139     }
140     return NULL;
141
142 }
143
144 static PyObject *py_lp_ctx_load(PyObject *self, PyObject *args)
145 {
146         char *filename;
147         bool ret;
148         if (!PyArg_ParseTuple(args, "s", &filename))
149                 return NULL;
150
151         ret = lpcfg_load(PyLoadparmContext_AsLoadparmContext(self), filename);
152
153         if (!ret) {
154                 PyErr_Format(PyExc_RuntimeError, "Unable to load file %s", filename);
155                 return NULL;
156         }
157         Py_RETURN_NONE;
158 }
159
160 static PyObject *py_lp_ctx_load_default(PyObject *self, PyObject *unused)
161 {
162         bool ret;
163         ret = lpcfg_load_default(PyLoadparmContext_AsLoadparmContext(self));
164
165         if (!ret) {
166                 PyErr_SetString(PyExc_RuntimeError, "Unable to load default file");
167                 return NULL;
168         }
169         Py_RETURN_NONE;
170 }
171
172 static PyObject *py_lp_ctx_get(PyObject *self, PyObject *args)
173 {
174         char *param_name;
175         char *section_name = NULL;
176         PyObject *ret;
177         if (!PyArg_ParseTuple(args, "s|z", &param_name, &section_name))
178                 return NULL;
179
180         ret = py_lp_ctx_get_helper(PyLoadparmContext_AsLoadparmContext(self), section_name, param_name);
181         if (ret == NULL)
182                 Py_RETURN_NONE;
183         return ret;
184 }
185
186 static PyObject *py_lp_ctx_is_myname(PyObject *self, PyObject *args)
187 {
188         char *name;
189         if (!PyArg_ParseTuple(args, "s", &name))
190                 return NULL;
191
192         return PyBool_FromLong(lpcfg_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
193 }
194
195 static PyObject *py_lp_ctx_is_mydomain(PyObject *self, PyObject *args)
196 {
197         char *name;
198         if (!PyArg_ParseTuple(args, "s", &name))
199                 return NULL;
200
201         return PyBool_FromLong(lpcfg_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
202 }
203
204 static PyObject *py_lp_ctx_set(PyObject *self, PyObject *args)
205 {
206         char *name, *value;
207         bool ret;
208         if (!PyArg_ParseTuple(args, "ss", &name, &value))
209                 return NULL;
210
211         ret = lpcfg_set_cmdline(PyLoadparmContext_AsLoadparmContext(self), name, value);
212         if (!ret) {
213                 PyErr_SetString(PyExc_RuntimeError, "Unable to set parameter");
214                 return NULL;
215         }
216
217         Py_RETURN_NONE;
218 }
219
220 static PyObject *py_lp_ctx_private_path(PyObject *self, PyObject *args)
221 {
222         char *name, *path;
223         PyObject *ret;
224         if (!PyArg_ParseTuple(args, "s", &name))
225                 return NULL;
226
227         path = lpcfg_private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);
228         ret = PyStr_FromString(path);
229         talloc_free(path);
230
231         return ret;
232 }
233
234 static PyObject *py_lp_ctx_services(PyObject *self, PyObject *unused)
235 {
236         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
237         PyObject *ret;
238         int i;
239         ret = PyList_New(lpcfg_numservices(lp_ctx));
240         for (i = 0; i < lpcfg_numservices(lp_ctx); i++) {
241                 struct loadparm_service *service = lpcfg_servicebynum(lp_ctx, i);
242                 if (service != NULL) {
243                         PyList_SetItem(ret, i, PyStr_FromString(lpcfg_servicename(service)));
244                 }
245         }
246         return ret;
247 }
248
249 static PyObject *py_lp_ctx_server_role(PyObject *self, PyObject *unused)
250 {
251         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
252         uint32_t role;
253         const char *role_str;
254
255         role = lpcfg_server_role(lp_ctx);
256         role_str = server_role_str(role);
257
258         return PyStr_FromString(role_str);
259 }
260
261 static PyObject *py_lp_dump(PyObject *self, PyObject *args)
262 {
263         bool show_defaults = false;
264         const char *file_name = "";
265         const char *mode = "w";
266         FILE *f;
267         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
268
269         if (!PyArg_ParseTuple(args, "|bss", &show_defaults, &file_name, &mode))
270                 return NULL;
271
272         if (file_name[0] == '\0') {
273                 f = stdout;
274         } else {
275                 f = fopen(file_name, mode);
276         }
277
278         if (f == NULL) {
279                 PyErr_SetFromErrno(PyExc_IOError);
280                 return NULL;
281         }
282
283         lpcfg_dump(lp_ctx, f, show_defaults, lpcfg_numservices(lp_ctx));
284
285         if (f != stdout) {
286                 fclose(f);
287         }
288
289         Py_RETURN_NONE;
290 }
291
292 static PyObject *py_lp_dump_a_parameter(PyObject *self, PyObject *args)
293 {
294         char *param_name;
295         const char *section_name = NULL;
296         const char *file_name = "";
297         const char *mode = "w";
298         FILE *f;
299         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
300         struct loadparm_service *service;
301         bool ret;
302
303         if (!PyArg_ParseTuple(args, "s|zss", &param_name, &section_name, &file_name, &mode))
304                 return NULL;
305
306         if (file_name[0] == '\0') {
307                 f = stdout;
308         } else {
309                 f = fopen(file_name, mode);
310         }
311
312         if (f == NULL) {
313                 return NULL;
314         }
315
316         if (section_name != NULL && strwicmp(section_name, GLOBAL_NAME) &&
317                 strwicmp(section_name, GLOBAL_NAME2)) {
318                 /* it's a share parameter */
319                 service = lpcfg_service(lp_ctx, section_name);
320                 if (service == NULL) {
321                         PyErr_Format(PyExc_RuntimeError, "Unknown section %s", section_name);
322                         return NULL;
323                 }
324         } else {
325                 /* it's global */
326                 service = NULL;
327                 section_name = "global";
328         }
329
330         ret = lpcfg_dump_a_parameter(lp_ctx, service, param_name, f);
331
332         if (!ret) {
333                 PyErr_Format(PyExc_RuntimeError, "Parameter %s unknown for section %s", param_name, section_name);
334                 return NULL;
335         }
336
337         if (f != stdout) {
338                 fclose(f);
339         }
340
341         Py_RETURN_NONE;
342
343 }
344
345 static PyObject *py_lp_log_level(PyObject *self, PyObject *unused)
346 {
347         int ret = DEBUGLEVEL_CLASS[DBGC_CLASS];
348         return PyInt_FromLong(ret);
349 }
350
351
352 static PyObject *py_samdb_url(PyObject *self, PyObject *unused)
353 {
354         struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
355         return PyStr_FromFormat("tdb://%s/sam.ldb", lpcfg_private_dir(lp_ctx));
356 }
357
358
359 static PyMethodDef py_lp_ctx_methods[] = {
360         { "load", py_lp_ctx_load, METH_VARARGS,
361                 "S.load(filename) -> None\n"
362                 "Load specified file." },
363         { "load_default", py_lp_ctx_load_default, METH_NOARGS,
364                 "S.load_default() -> None\n"
365                 "Load default smb.conf file." },
366         { "is_myname", py_lp_ctx_is_myname, METH_VARARGS,
367                 "S.is_myname(name) -> bool\n"
368                 "Check whether the specified name matches one of our netbios names." },
369         { "is_mydomain", py_lp_ctx_is_mydomain, METH_VARARGS,
370                 "S.is_mydomain(name) -> bool\n"
371                 "Check whether the specified name matches our domain name." },
372         { "get", py_lp_ctx_get, METH_VARARGS,
373                 "S.get(name, service_name) -> value\n"
374                 "Find specified parameter." },
375         { "set", py_lp_ctx_set, METH_VARARGS,
376                 "S.set(name, value) -> bool\n"
377                 "Change a parameter." },
378         { "private_path", py_lp_ctx_private_path, METH_VARARGS,
379                 "S.private_path(name) -> path\n" },
380         { "services", py_lp_ctx_services, METH_NOARGS,
381                 "S.services() -> list" },
382         { "server_role", py_lp_ctx_server_role, METH_NOARGS,
383                 "S.server_role() -> value\n"
384                 "Get the server role." },
385         { "dump", py_lp_dump, METH_VARARGS,
386                 "S.dump(show_defaults=False, file_name='', mode='w')" },
387         { "dump_a_parameter", py_lp_dump_a_parameter, METH_VARARGS,
388                 "S.dump_a_parameter(name, service_name, file_name='', mode='w')" },
389         { "log_level", py_lp_log_level, METH_NOARGS,
390                 "S.log_level() -> int\n Get the active log level" },
391         { "samdb_url", py_samdb_url, METH_NOARGS,
392                 "S.samdb_url() -> string\n"
393                 "Returns the current URL for sam.ldb." },
394         { NULL }
395 };
396
397 static PyObject *py_lp_ctx_default_service(PyObject *self, void *closure)
398 {
399         return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self)));
400 }
401
402 static PyObject *py_lp_ctx_config_file(PyObject *self, void *closure)
403 {
404         const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self));
405         if (configfile == NULL)
406                 Py_RETURN_NONE;
407         else
408                 return PyStr_FromString(configfile);
409 }
410
411 static PyGetSetDef py_lp_ctx_getset[] = {
412         { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
413         { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
414           discard_const_p(char, "Name of last config file that was loaded.") },
415         { NULL }
416 };
417
418 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
419 {
420         return pytalloc_reference(type, loadparm_init_global(false));
421 }
422
423 static Py_ssize_t py_lp_ctx_len(PyObject *self)
424 {
425         return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self));
426 }
427
428 static PyObject *py_lp_ctx_getitem(PyObject *self, PyObject *name)
429 {
430         struct loadparm_service *service;
431         if (!PyStr_Check(name)) {
432                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
433                 return NULL;
434         }
435         service = lpcfg_service(PyLoadparmContext_AsLoadparmContext(self), PyStr_AsString(name));
436         if (service == NULL) {
437                 PyErr_SetString(PyExc_KeyError, "No such section");
438                 return NULL;
439         }
440         return PyLoadparmService_FromService(service);
441 }
442
443 static PyMappingMethods py_lp_ctx_mapping = {
444         .mp_length = (lenfunc)py_lp_ctx_len,
445         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
446 };
447
448 PyTypeObject PyLoadparmContext = {
449         .tp_name = "param.LoadParm",
450         .tp_getset = py_lp_ctx_getset,
451         .tp_methods = py_lp_ctx_methods,
452         .tp_new = py_lp_ctx_new,
453         .tp_as_mapping = &py_lp_ctx_mapping,
454         .tp_flags = Py_TPFLAGS_DEFAULT,
455 };
456
457 static PyObject *py_lp_service_dump(PyObject *self, PyObject *args)
458 {
459         bool show_defaults = false;
460         FILE *f;
461         const char *file_name = "";
462         const char *mode = "w";
463         struct loadparm_service *service = PyLoadparmService_AsLoadparmService(self);
464         struct loadparm_service *default_service;
465         PyObject *py_default_service;
466
467         if (!PyArg_ParseTuple(args, "O|bss", &py_default_service, &show_defaults, &file_name, &mode))
468                 return NULL;
469
470         if (file_name[0] == '\0') {
471                 f = stdout;
472         } else {
473                 f = fopen(file_name, mode);
474         }
475
476         if (f == NULL) {
477                 return NULL;
478         }
479
480         if (!PyObject_TypeCheck(py_default_service, &PyLoadparmService)) {
481                 PyErr_SetNone(PyExc_TypeError);
482                 return NULL;
483         }
484
485         default_service = PyLoadparmService_AsLoadparmService(py_default_service);
486
487         lpcfg_dump_one(f, show_defaults, service, default_service);
488
489         if (f != stdout) {
490                 fclose(f);
491         }
492
493         Py_RETURN_NONE;
494 }
495
496 static PyMethodDef py_lp_service_methods[] = {
497         { "dump", (PyCFunction)py_lp_service_dump, METH_VARARGS, 
498                 "S.dump(default_service, show_defaults=False, file_name='', mode='w')" },
499         { NULL }
500 };
501
502 PyTypeObject PyLoadparmService = {
503         .tp_name = "param.LoadparmService",
504         .tp_methods = py_lp_service_methods,
505         .tp_flags = Py_TPFLAGS_DEFAULT,
506 };
507
508 static PyObject *py_default_path(PyObject *self)
509 {
510         return PyStr_FromString(lp_default_path());
511 }
512
513 static PyObject *py_setup_dir(PyObject *self)
514 {
515         return PyStr_FromString(dyn_SETUPDIR);
516 }
517
518 static PyObject *py_modules_dir(PyObject *self)
519 {
520         return PyStr_FromString(dyn_MODULESDIR);
521 }
522
523 static PyObject *py_bin_dir(PyObject *self)
524 {
525         return PyStr_FromString(dyn_BINDIR);
526 }
527
528 static PyObject *py_sbin_dir(PyObject *self)
529 {
530         return PyStr_FromString(dyn_SBINDIR);
531 }
532
533 static PyMethodDef pyparam_methods[] = {
534         { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
535                 "Returns the default smb.conf path." },
536         { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS,
537                 "Returns the compiled in location of provision tempates." },
538         { "modules_dir", (PyCFunction)py_modules_dir, METH_NOARGS,
539                 "Returns the compiled in location of modules." },
540         { "bin_dir", (PyCFunction)py_bin_dir, METH_NOARGS,
541                 "Returns the compiled in BINDIR." },
542         { "sbin_dir", (PyCFunction)py_sbin_dir, METH_NOARGS,
543                 "Returns the compiled in SBINDIR." },
544         { NULL }
545 };
546
547 static struct PyModuleDef moduledef = {
548         PyModuleDef_HEAD_INIT,
549         .m_name = "param",
550         .m_doc = "Parsing and writing Samba configuration files.",
551         .m_size = -1,
552         .m_methods = pyparam_methods,
553 };
554
555 MODULE_INIT_FUNC(param)
556 {
557         PyObject *m;
558         PyTypeObject *talloc_type = pytalloc_GetObjectType();
559         if (talloc_type == NULL)
560                 return NULL;
561
562         if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmContext) < 0)
563                 return NULL;
564
565         if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmService) < 0)
566                 return NULL;
567
568         m = PyModule_Create(&moduledef);
569         if (m == NULL)
570                 return NULL;
571
572         Py_INCREF(&PyLoadparmContext);
573         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
574         return m;
575 }