/* Unix SMB/CIFS implementation. Python interface to ldb, Samba-specific functions Copyright (C) 2007-2010 Jelmer Vernooij This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see . */ #include #include "includes.h" #include #include "lib/ldb/pyldb.h" #include "param/pyparam.h" #include "auth/credentials/pycredentials.h" static PyObject *pyldb_module; staticforward PyTypeObject PySambaLdb; static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args) { PyObject *py_lp_ctx; struct loadparm_context *lp_ctx; struct ldb_context *ldb; if (!PyArg_ParseTuple(args, "O", &py_lp_ctx)) return NULL; lp_ctx = lp_from_py_object(py_lp_ctx); if (lp_ctx == NULL) { PyErr_SetString(PyExc_TypeError, "Expected loadparm object"); return NULL; } ldb = PyLdb_AsLdbContext(self); ldb_set_opaque(ldb, "loadparm", lp_ctx); Py_RETURN_NONE; } static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args) { PyObject *py_creds; struct cli_credentials *creds; struct ldb_context *ldb; if (!PyArg_ParseTuple(args, "O", &py_creds)) return NULL; creds = cli_credentials_from_py_object(py_creds); if (creds == NULL) { PyErr_SetString(PyExc_TypeError, "Expected credentials object"); return NULL; } ldb = PyLdb_AsLdbContext(self); ldb_set_opaque(ldb, "credentials", creds); Py_RETURN_NONE; } /* XXX: This function really should be in libldb's pyldb.c */ static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args) { int value; int *old_val, *new_val; char *py_opaque_name, *opaque_name_talloc; struct ldb_context *ldb; TALLOC_CTX *tmp_ctx; if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value)) return NULL; ldb = PyLdb_AsLdbContext(self); /* see if we have a cached copy */ old_val = (int *)ldb_get_opaque(ldb, py_opaque_name); /* XXX: We shouldn't just blindly assume that the value that is * already present has the size of an int and is not shared * with other code that may rely on it not changing. * JRV 20100403 */ if (old_val) { *old_val = value; Py_RETURN_NONE; } tmp_ctx = talloc_new(ldb); if (tmp_ctx == NULL) { PyErr_NoMemory(); return NULL; } new_val = talloc(tmp_ctx, int); if (new_val == NULL) { talloc_free(tmp_ctx); PyErr_NoMemory(); return NULL; } opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name); if (opaque_name_talloc == NULL) { talloc_free(tmp_ctx); PyErr_NoMemory(); return NULL; } *new_val = value; /* cache the domain_sid in the ldb */ if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) { talloc_free(tmp_ctx); PyErr_SetString(PyExc_RuntimeError, "Failed to set opaque integer into the ldb"); return NULL; } talloc_steal(ldb, new_val); talloc_steal(ldb, opaque_name_talloc); talloc_free(tmp_ctx); Py_RETURN_NONE; } static PyMethodDef py_samba_ldb_methods[] = { { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, "ldb_set_loadparm(ldb, session_info)\n" "Set loadparm context to use when connecting." }, { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, "ldb_set_credentials(ldb, credentials)\n" "Set credentials to use when connecting." }, { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer, METH_VARARGS, NULL }, { NULL }, }; static PyTypeObject PySambaLdb = { .tp_name = "samba.Ldb", .tp_doc = "Connection to a LDB database.", .tp_methods = py_samba_ldb_methods, .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, }; void init_ldb(void) { PyObject *m; pyldb_module = PyImport_ImportModule("ldb"); if (pyldb_module == NULL) return; PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb"); if (PySambaLdb.tp_base == NULL) return; if (PyType_Ready(&PySambaLdb) < 0) return; m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings"); if (m == NULL) return; Py_INCREF(&PySambaLdb); PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb); }