root/source4/param/pyparam.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PyAPI_DATA
  2. py_lp_ctx_get_helper
  3. py_lp_ctx_load
  4. py_lp_ctx_load_default
  5. py_lp_ctx_get
  6. py_lp_ctx_is_myname
  7. py_lp_ctx_is_mydomain
  8. py_lp_ctx_set
  9. py_lp_ctx_private_path
  10. py_lp_ctx_default_service
  11. py_lp_ctx_config_file
  12. py_lp_ctx_new
  13. py_lp_ctx_len
  14. py_lp_ctx_getitem
  15. lp_from_py_object
  16. py_default_loadparm_context
  17. py_default_path
  18. initparam

   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 <stdint.h>
  21 #include <stdbool.h>
  22 
  23 #include "includes.h"
  24 #include "param/param.h"
  25 #include "param/loadparm.h"
  26 #include <Python.h>
  27 #include "pytalloc.h"
  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 #ifndef Py_RETURN_NONE
  36 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  37 #endif
  38 
  39 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
  40 
  41 PyAPI_DATA(PyTypeObject) PyLoadparmContext;
     /* [<][>][^][v][top][bottom][index][help] */
  42 PyAPI_DATA(PyTypeObject) PyLoadparmService;
  43 
  44 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)
  45 {
  46         return py_talloc_import(&PyLoadparmService, service);
  47 }
  48 
  49 static PyObject *py_lp_ctx_get_helper(struct loadparm_context *lp_ctx, const char *service_name, const char *param_name)
     /* [<][>][^][v][top][bottom][index][help] */
  50 {
  51     struct parm_struct *parm = NULL;
  52     void *parm_ptr = NULL;
  53     int i;
  54 
  55     if (service_name != NULL) {
  56         struct loadparm_service *service;
  57         /* its a share parameter */
  58         service = lp_service(lp_ctx, service_name);
  59         if (service == NULL) {
  60             return NULL;
  61         }
  62         if (strchr(param_name, ':')) {
  63             /* its a parametric option on a share */
  64             const char *type = talloc_strndup(lp_ctx, 
  65                               param_name, 
  66                               strcspn(param_name, ":"));
  67             const char *option = strchr(param_name, ':') + 1;
  68             const char *value;
  69             if (type == NULL || option == NULL) {
  70                 return NULL;
  71             }
  72             value = lp_get_parametric(lp_ctx, service, type, option);
  73             if (value == NULL) {
  74                 return NULL;
  75             }
  76             return PyString_FromString(value);
  77         }
  78 
  79         parm = lp_parm_struct(param_name);
  80         if (parm == NULL || parm->pclass == P_GLOBAL) {
  81             return NULL;
  82         }
  83         parm_ptr = lp_parm_ptr(lp_ctx, service, parm);
  84     } else if (strchr(param_name, ':')) {
  85         /* its a global parametric option */
  86         const char *type = talloc_strndup(lp_ctx, 
  87                           param_name, strcspn(param_name, ":"));
  88         const char *option = strchr(param_name, ':') + 1;
  89         const char *value;
  90         if (type == NULL || option == NULL) {
  91             return NULL;
  92         }
  93         value = lp_get_parametric(lp_ctx, NULL, type, option);
  94         if (value == NULL)
  95             return NULL;
  96         return PyString_FromString(value);
  97     } else {
  98         /* its a global parameter */
  99         parm = lp_parm_struct(param_name);
 100         if (parm == NULL) {
 101             return NULL;
 102         }
 103         parm_ptr = lp_parm_ptr(lp_ctx, NULL, parm);
 104     }
 105 
 106     if (parm == NULL || parm_ptr == NULL) {
 107         return NULL;
 108     }
 109 
 110     /* construct and return the right type of python object */
 111     switch (parm->type) {
 112     case P_STRING:
 113     case P_USTRING:
 114         return PyString_FromString(*(char **)parm_ptr);
 115     case P_BOOL:
 116         return PyBool_FromLong(*(bool *)parm_ptr);
 117     case P_INTEGER:
 118     case P_OCTAL:
 119     case P_BYTES:
 120         return PyLong_FromLong(*(int *)parm_ptr);
 121     case P_ENUM:
 122         for (i=0; parm->enum_list[i].name; i++) {
 123             if (*(int *)parm_ptr == parm->enum_list[i].value) {
 124                 return PyString_FromString(parm->enum_list[i].name);
 125             }
 126         }
 127         return NULL;
 128     case P_LIST: 
 129         {
 130             int j;
 131             const char **strlist = *(const char ***)parm_ptr;
 132             PyObject *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)
     /* [<][>][^][v][top][bottom][index][help] */
 146 {
 147         char *filename;
 148         bool ret;
 149         if (!PyArg_ParseTuple(args, "s", &filename))
 150                 return NULL;
 151 
 152         ret = lp_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)
     /* [<][>][^][v][top][bottom][index][help] */
 162 {
 163         bool ret;
 164         ret = lp_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)
     /* [<][>][^][v][top][bottom][index][help] */
 174 {
 175         char *param_name;
 176         char *section_name = NULL;
 177         PyObject *ret;
 178         if (!PyArg_ParseTuple(args, "s|s", &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)
     /* [<][>][^][v][top][bottom][index][help] */
 188 {
 189         char *name;
 190         if (!PyArg_ParseTuple(args, "s", &name))
 191                 return NULL;
 192 
 193         return PyBool_FromLong(lp_is_myname(PyLoadparmContext_AsLoadparmContext(self), name));
 194 }
 195 
 196 static PyObject *py_lp_ctx_is_mydomain(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 197 {
 198         char *name;
 199         if (!PyArg_ParseTuple(args, "s", &name))
 200                 return NULL;
 201 
 202         return PyBool_FromLong(lp_is_mydomain(PyLoadparmContext_AsLoadparmContext(self), name));
 203 }
 204 
 205 static PyObject *py_lp_ctx_set(py_talloc_Object *self, PyObject *args)
     /* [<][>][^][v][top][bottom][index][help] */
 206 {
 207         char *name, *value;
 208         bool ret;
 209         if (!PyArg_ParseTuple(args, "ss", &name, &value))
 210                 return NULL;
 211 
 212         ret = lp_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)
     /* [<][>][^][v][top][bottom][index][help] */
 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 PyMethodDef py_lp_ctx_methods[] = {
 236         { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
 237                 "S.load(filename) -> None\n"
 238                 "Load specified file." },
 239         { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,
 240                 "S.load_default() -> None\n"
 241                 "Load default smb.conf file." },
 242         { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,
 243                 "S.is_myname(name) -> bool\n"
 244                 "Check whether the specified name matches one of our netbios names." },
 245         { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,
 246                 "S.is_mydomain(name) -> bool\n"
 247                 "Check whether the specified name matches our domain name." },
 248         { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,
 249                 "S.get(name, service_name) -> value\n"
 250                 "Find specified parameter." },
 251         { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,
 252                 "S.set(name, value) -> bool\n"
 253                 "Change a parameter." },
 254         { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
 255                 "S.private_path(name) -> path\n" },
 256         { NULL }
 257 };
 258 
 259 static PyObject *py_lp_ctx_default_service(py_talloc_Object *self, void *closure)
     /* [<][>][^][v][top][bottom][index][help] */
 260 {
 261         return PyLoadparmService_FromService(lp_default_service(PyLoadparmContext_AsLoadparmContext(self)));
 262 }
 263 
 264 static PyObject *py_lp_ctx_config_file(py_talloc_Object *self, void *closure)
     /* [<][>][^][v][top][bottom][index][help] */
 265 {
 266         const char *configfile = lp_configfile(PyLoadparmContext_AsLoadparmContext(self));
 267         if (configfile == NULL)
 268                 Py_RETURN_NONE;
 269         else
 270                 return PyString_FromString(configfile);
 271 }
 272 
 273 static PyGetSetDef py_lp_ctx_getset[] = {
 274         { discard_const_p(char, "default_service"), (getter)py_lp_ctx_default_service, NULL, NULL },
 275         { discard_const_p(char, "configfile"), (getter)py_lp_ctx_config_file, NULL,
 276           discard_const_p(char, "Name of last config file that was loaded.") },
 277         { NULL }
 278 };
 279 
 280 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     /* [<][>][^][v][top][bottom][index][help] */
 281 {
 282         return py_talloc_import(type, loadparm_init(NULL));
 283 }
 284 
 285 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self)
     /* [<][>][^][v][top][bottom][index][help] */
 286 {
 287         return lp_numservices(PyLoadparmContext_AsLoadparmContext(self));
 288 }
 289 
 290 static PyObject *py_lp_ctx_getitem(py_talloc_Object *self, PyObject *name)
     /* [<][>][^][v][top][bottom][index][help] */
 291 {
 292         struct loadparm_service *service;
 293         if (!PyString_Check(name)) {
 294                 PyErr_SetString(PyExc_TypeError, "Only string subscripts are supported");
 295                 return NULL;
 296         }
 297         service = lp_service(PyLoadparmContext_AsLoadparmContext(self), PyString_AsString(name));
 298         if (service == NULL) {
 299                 PyErr_SetString(PyExc_KeyError, "No such section");
 300                 return NULL;
 301         }
 302         return PyLoadparmService_FromService(service);
 303 }
 304 
 305 static PyMappingMethods py_lp_ctx_mapping = {
 306         .mp_length = (lenfunc)py_lp_ctx_len,
 307         .mp_subscript = (binaryfunc)py_lp_ctx_getitem,
 308 };
 309 
 310 PyTypeObject PyLoadparmContext = {
 311         .tp_name = "LoadParm",
 312         .tp_basicsize = sizeof(py_talloc_Object),
 313         .tp_dealloc = py_talloc_dealloc,
 314         .tp_getset = py_lp_ctx_getset,
 315         .tp_methods = py_lp_ctx_methods,
 316         .tp_new = py_lp_ctx_new,
 317         .tp_as_mapping = &py_lp_ctx_mapping,
 318         .tp_flags = Py_TPFLAGS_DEFAULT,
 319 };
 320 
 321 PyTypeObject PyLoadparmService = {
 322         .tp_name = "LoadparmService",
 323         .tp_dealloc = py_talloc_dealloc,
 324         .tp_basicsize = sizeof(py_talloc_Object),
 325         .tp_flags = Py_TPFLAGS_DEFAULT,
 326 };
 327 
 328 _PUBLIC_ struct loadparm_context *lp_from_py_object(PyObject *py_obj)
     /* [<][>][^][v][top][bottom][index][help] */
 329 {
 330     struct loadparm_context *lp_ctx;
 331     if (PyString_Check(py_obj)) {
 332         lp_ctx = loadparm_init(NULL);
 333         if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
 334             talloc_free(lp_ctx);
 335             PyErr_Format(PyExc_RuntimeError, 
 336                          "Unable to load %s", PyString_AsString(py_obj));
 337             return NULL;
 338         }
 339         return lp_ctx;
 340     }
 341 
 342     if (py_obj == Py_None) {
 343         lp_ctx = loadparm_init(NULL);
 344         /* We're not checking that loading the file succeeded *on purpose */
 345         lp_load_default(lp_ctx);
 346         return lp_ctx;
 347     }
 348 
 349     return PyLoadparmContext_AsLoadparmContext(py_obj);
 350 }
 351 
 352 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx)
     /* [<][>][^][v][top][bottom][index][help] */
 353 {
 354     struct loadparm_context *ret;
 355     ret = loadparm_init(mem_ctx);
 356     if (!lp_load_default(ret))
 357         return NULL;
 358     return ret;
 359 }
 360 
 361 static PyObject *py_default_path(PyObject *self)
     /* [<][>][^][v][top][bottom][index][help] */
 362 {
 363     return PyString_FromString(lp_default_path());
 364 }
 365 
 366 static PyMethodDef pyparam_methods[] = {
 367     { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 
 368         "Returns the default smb.conf path." },
 369     { NULL }
 370 };
 371 
 372 void initparam(void)
     /* [<][>][^][v][top][bottom][index][help] */
 373 {
 374         PyObject *m;
 375 
 376         if (PyType_Ready(&PyLoadparmContext) < 0)
 377                 return;
 378 
 379         m = Py_InitModule3("param", pyparam_methods, "Parsing and writing Samba configuration files.");
 380         if (m == NULL)
 381                 return;
 382 
 383         Py_INCREF(&PyLoadparmContext);
 384         PyModule_AddObject(m, "LoadParm", (PyObject *)&PyLoadparmContext);
 385 }

/* [<][>][^][v][top][bottom][index][help] */