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