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