Changeset 988 for vendor/current/source4/param/pyparam.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source4/param/pyparam.c
r740 r988 22 22 #include "param/param.h" 23 23 #include "param/loadparm.h" 24 #include "lib/talloc/pytalloc.h"24 #include <pytalloc.h> 25 25 #include "dynconfig/dynconfig.h" 26 26 27 /* There's no Py_ssize_t in 2.4, apparently */ 28 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5 29 typedef int Py_ssize_t; 30 typedef inquiry lenfunc; 31 #endif 32 33 #define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context) 34 #define PyLoadparmService_AsLoadparmService(obj) py_talloc_get_type(obj, struct loadparm_service) 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) 35 31 36 32 extern PyTypeObject PyLoadparmContext; 37 33 extern PyTypeObject PyLoadparmService; 38 34 39 PyObject *PyLoadparmService_FromService(struct loadparm_service *service)40 { 41 return py _talloc_reference(&PyLoadparmService, service);35 static PyObject *PyLoadparmService_FromService(struct loadparm_service *service) 36 { 37 return pytalloc_reference(&PyLoadparmService, service); 42 38 } 43 39 … … 72 68 } 73 69 74 parm = lpcfg_parm_struct( param_name);75 if (parm == NULL || parm->p class == P_GLOBAL) {70 parm = lpcfg_parm_struct(lp_ctx, param_name); 71 if (parm == NULL || parm->p_class == P_GLOBAL) { 76 72 return NULL; 77 73 } … … 92 88 } else { 93 89 /* its a global parameter */ 94 parm = lpcfg_parm_struct( param_name);90 parm = lpcfg_parm_struct(lp_ctx, param_name); 95 91 if (parm == NULL) { 96 92 return NULL; … … 105 101 /* construct and return the right type of python object */ 106 102 switch (parm->type) { 103 case P_CHAR: 104 return PyString_FromFormat("%c", *(char *)parm_ptr); 107 105 case P_STRING: 108 106 case P_USTRING: … … 110 108 case P_BOOL: 111 109 return PyBool_FromLong(*(bool *)parm_ptr); 110 case P_BOOLREV: 111 return PyBool_FromLong(!(*(bool *)parm_ptr)); 112 112 case P_INTEGER: 113 113 case P_OCTAL: … … 138 138 return pylist; 139 139 } 140 141 break;142 140 } 143 141 return NULL; … … 145 143 } 146 144 147 static PyObject *py_lp_ctx_load( py_talloc_Object *self, PyObject *args)145 static PyObject *py_lp_ctx_load(PyObject *self, PyObject *args) 148 146 { 149 147 char *filename; … … 161 159 } 162 160 163 static PyObject *py_lp_ctx_load_default( py_talloc_Object *self)161 static PyObject *py_lp_ctx_load_default(PyObject *self, PyObject *unused) 164 162 { 165 163 bool ret; … … 173 171 } 174 172 175 static PyObject *py_lp_ctx_get( py_talloc_Object *self, PyObject *args)173 static PyObject *py_lp_ctx_get(PyObject *self, PyObject *args) 176 174 { 177 175 char *param_name; … … 187 185 } 188 186 189 static PyObject *py_lp_ctx_is_myname( py_talloc_Object *self, PyObject *args)187 static PyObject *py_lp_ctx_is_myname(PyObject *self, PyObject *args) 190 188 { 191 189 char *name; … … 196 194 } 197 195 198 static PyObject *py_lp_ctx_is_mydomain( py_talloc_Object *self, PyObject *args)196 static PyObject *py_lp_ctx_is_mydomain(PyObject *self, PyObject *args) 199 197 { 200 198 char *name; … … 205 203 } 206 204 207 static PyObject *py_lp_ctx_set( py_talloc_Object *self, PyObject *args)205 static PyObject *py_lp_ctx_set(PyObject *self, PyObject *args) 208 206 { 209 207 char *name, *value; … … 221 219 } 222 220 223 static PyObject *py_lp_ctx_private_path( py_talloc_Object *self, PyObject *args)221 static PyObject *py_lp_ctx_private_path(PyObject *self, PyObject *args) 224 222 { 225 223 char *name, *path; … … 228 226 return NULL; 229 227 230 path = private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name);228 path = lpcfg_private_path(NULL, PyLoadparmContext_AsLoadparmContext(self), name); 231 229 ret = PyString_FromString(path); 232 230 talloc_free(path); … … 235 233 } 236 234 237 static PyObject *py_lp_ctx_services( py_talloc_Object *self)235 static PyObject *py_lp_ctx_services(PyObject *self, PyObject *unused) 238 236 { 239 237 struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self); … … 250 248 } 251 249 250 static PyObject *py_lp_ctx_server_role(PyObject *self, PyObject *unused) 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 252 262 static PyObject *py_lp_dump(PyObject *self, PyObject *args) 253 263 { … … 262 272 f = PyFile_AsFile(py_stream); 263 273 if (f == NULL) { 264 PyErr_SetString(PyExc_TypeError, "Not a file stream");265 274 return NULL; 266 275 } … … 271 280 } 272 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, ¶m_name, §ion_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, PyObject *unused) 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 273 331 274 332 static PyMethodDef py_lp_ctx_methods[] = { 275 { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS,333 { "load", py_lp_ctx_load, METH_VARARGS, 276 334 "S.load(filename) -> None\n" 277 335 "Load specified file." }, 278 { "load_default", (PyCFunction)py_lp_ctx_load_default, METH_NOARGS,336 { "load_default", py_lp_ctx_load_default, METH_NOARGS, 279 337 "S.load_default() -> None\n" 280 338 "Load default smb.conf file." }, 281 { "is_myname", (PyCFunction)py_lp_ctx_is_myname, METH_VARARGS,339 { "is_myname", py_lp_ctx_is_myname, METH_VARARGS, 282 340 "S.is_myname(name) -> bool\n" 283 341 "Check whether the specified name matches one of our netbios names." }, 284 { "is_mydomain", (PyCFunction)py_lp_ctx_is_mydomain, METH_VARARGS,342 { "is_mydomain", py_lp_ctx_is_mydomain, METH_VARARGS, 285 343 "S.is_mydomain(name) -> bool\n" 286 344 "Check whether the specified name matches our domain name." }, 287 { "get", (PyCFunction)py_lp_ctx_get, METH_VARARGS,345 { "get", py_lp_ctx_get, METH_VARARGS, 288 346 "S.get(name, service_name) -> value\n" 289 347 "Find specified parameter." }, 290 { "set", (PyCFunction)py_lp_ctx_set, METH_VARARGS,348 { "set", py_lp_ctx_set, METH_VARARGS, 291 349 "S.set(name, value) -> bool\n" 292 350 "Change a parameter." }, 293 { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,351 { "private_path", py_lp_ctx_private_path, METH_VARARGS, 294 352 "S.private_path(name) -> path\n" }, 295 { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,353 { "services", py_lp_ctx_services, METH_NOARGS, 296 354 "S.services() -> list" }, 297 { "dump", (PyCFunction)py_lp_dump, METH_VARARGS, 355 { "server_role", py_lp_ctx_server_role, METH_NOARGS, 356 "S.server_role() -> value\n" 357 "Get the server role." }, 358 { "dump", py_lp_dump, METH_VARARGS, 298 359 "S.dump(stream, show_defaults=False)" }, 360 { "dump_a_parameter", py_lp_dump_a_parameter, METH_VARARGS, 361 "S.dump_a_parameter(stream, name, service_name)" }, 362 { "samdb_url", py_samdb_url, METH_NOARGS, 363 "S.samdb_url() -> string\n" 364 "Returns the current URL for sam.ldb." }, 299 365 { NULL } 300 366 }; 301 367 302 static PyObject *py_lp_ctx_default_service( py_talloc_Object *self, void *closure)368 static PyObject *py_lp_ctx_default_service(PyObject *self, void *closure) 303 369 { 304 370 return PyLoadparmService_FromService(lpcfg_default_service(PyLoadparmContext_AsLoadparmContext(self))); 305 371 } 306 372 307 static PyObject *py_lp_ctx_config_file( py_talloc_Object *self, void *closure)373 static PyObject *py_lp_ctx_config_file(PyObject *self, void *closure) 308 374 { 309 375 const char *configfile = lpcfg_configfile(PyLoadparmContext_AsLoadparmContext(self)); … … 323 389 static PyObject *py_lp_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 324 390 { 325 py_talloc_Object *ret = (py_talloc_Object *)type->tp_alloc(type, 0); 326 if (ret == NULL) { 327 PyErr_NoMemory(); 328 return NULL; 329 } 330 ret->talloc_ctx = talloc_new(NULL); 331 if (ret->talloc_ctx == NULL) { 332 PyErr_NoMemory(); 333 return NULL; 334 } 335 ret->ptr = loadparm_init_global(false); 336 return (PyObject *)ret; 337 } 338 339 static Py_ssize_t py_lp_ctx_len(py_talloc_Object *self) 391 return pytalloc_reference(type, loadparm_init_global(false)); 392 } 393 394 static Py_ssize_t py_lp_ctx_len(PyObject *self) 340 395 { 341 396 return lpcfg_numservices(PyLoadparmContext_AsLoadparmContext(self)); 342 397 } 343 398 344 static PyObject *py_lp_ctx_getitem( py_talloc_Object *self, PyObject *name)399 static PyObject *py_lp_ctx_getitem(PyObject *self, PyObject *name) 345 400 { 346 401 struct loadparm_service *service; … … 363 418 364 419 PyTypeObject PyLoadparmContext = { 365 .tp_name = "LoadParm", 366 .tp_basicsize = sizeof(py_talloc_Object), 420 .tp_name = "param.LoadParm", 367 421 .tp_getset = py_lp_ctx_getset, 368 422 .tp_methods = py_lp_ctx_methods, … … 387 441 f = PyFile_AsFile(py_stream); 388 442 if (f == NULL) { 389 PyErr_SetString(PyExc_TypeError, "Not a file stream");390 443 return NULL; 391 444 } … … 410 463 411 464 PyTypeObject PyLoadparmService = { 412 .tp_name = "LoadparmService", 413 .tp_basicsize = sizeof(py_talloc_Object), 465 .tp_name = "param.LoadparmService", 414 466 .tp_methods = py_lp_service_methods, 415 467 .tp_flags = Py_TPFLAGS_DEFAULT, … … 418 470 static PyObject *py_default_path(PyObject *self) 419 471 { 420 472 return PyString_FromString(lp_default_path()); 421 473 } 422 474 423 475 static PyObject *py_setup_dir(PyObject *self) 424 476 { 425 return PyString_FromString(dyn_SETUPDIR); 477 return PyString_FromString(dyn_SETUPDIR); 478 } 479 480 static PyObject *py_modules_dir(PyObject *self) 481 { 482 return PyString_FromString(dyn_MODULESDIR); 483 } 484 485 static PyObject *py_bin_dir(PyObject *self) 486 { 487 return PyString_FromString(dyn_BINDIR); 488 } 489 490 static PyObject *py_sbin_dir(PyObject *self) 491 { 492 return PyString_FromString(dyn_SBINDIR); 426 493 } 427 494 428 495 static PyMethodDef pyparam_methods[] = { 429 { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 430 "Returns the default smb.conf path." }, 431 { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS, 432 "Returns the compiled in location of provision tempates." }, 433 { NULL } 496 { "default_path", (PyCFunction)py_default_path, METH_NOARGS, 497 "Returns the default smb.conf path." }, 498 { "setup_dir", (PyCFunction)py_setup_dir, METH_NOARGS, 499 "Returns the compiled in location of provision tempates." }, 500 { "modules_dir", (PyCFunction)py_modules_dir, METH_NOARGS, 501 "Returns the compiled in location of modules." }, 502 { "bin_dir", (PyCFunction)py_bin_dir, METH_NOARGS, 503 "Returns the compiled in BINDIR." }, 504 { "sbin_dir", (PyCFunction)py_sbin_dir, METH_NOARGS, 505 "Returns the compiled in SBINDIR." }, 506 { NULL } 434 507 }; 435 508 … … 437 510 { 438 511 PyObject *m; 439 PyTypeObject *talloc_type = PyTalloc_GetObjectType(); 440 if ( talloc_type == NULL)512 513 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmContext) < 0) 441 514 return; 442 515 443 PyLoadparmContext.tp_base = talloc_type; 444 PyLoadparmService.tp_base = talloc_type; 445 446 if (PyType_Ready(&PyLoadparmContext) < 0) 447 return; 448 449 if (PyType_Ready(&PyLoadparmService) < 0) 516 if (pytalloc_BaseObject_PyType_Ready(&PyLoadparmService) < 0) 450 517 return; 451 518
Note:
See TracChangeset
for help on using the changeset viewer.