2 Unix SMB/CIFS implementation.
4 Python interface to ldb, Samba-specific functions
6 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "lib/ldb/pyldb.h"
26 #include "param/pyparam.h"
27 #include "auth/credentials/pycredentials.h"
29 static PyObject *pyldb_module;
30 staticforward PyTypeObject PySambaLdb;
32 static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
35 struct loadparm_context *lp_ctx;
36 struct ldb_context *ldb;
38 if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
41 lp_ctx = lp_from_py_object(py_lp_ctx);
43 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
47 ldb = PyLdb_AsLdbContext(self);
49 ldb_set_opaque(ldb, "loadparm", lp_ctx);
54 static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
57 struct cli_credentials *creds;
58 struct ldb_context *ldb;
60 if (!PyArg_ParseTuple(args, "O", &py_creds))
63 creds = cli_credentials_from_py_object(py_creds);
65 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
69 ldb = PyLdb_AsLdbContext(self);
71 ldb_set_opaque(ldb, "credentials", creds);
76 /* XXX: This function really should be in libldb's pyldb.c */
77 static PyObject *py_ldb_set_opaque_integer(PyObject *self, PyObject *args)
80 int *old_val, *new_val;
81 char *py_opaque_name, *opaque_name_talloc;
82 struct ldb_context *ldb;
85 if (!PyArg_ParseTuple(args, "si", &py_opaque_name, &value))
88 ldb = PyLdb_AsLdbContext(self);
90 /* see if we have a cached copy */
91 old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
92 /* XXX: We shouldn't just blindly assume that the value that is
93 * already present has the size of an int and is not shared
94 * with other code that may rely on it not changing.
102 tmp_ctx = talloc_new(ldb);
103 if (tmp_ctx == NULL) {
108 new_val = talloc(tmp_ctx, int);
109 if (new_val == NULL) {
110 talloc_free(tmp_ctx);
115 opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
116 if (opaque_name_talloc == NULL) {
117 talloc_free(tmp_ctx);
124 /* cache the domain_sid in the ldb */
125 if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
126 talloc_free(tmp_ctx);
127 PyErr_SetString(PyExc_RuntimeError,
128 "Failed to set opaque integer into the ldb");
132 talloc_steal(ldb, new_val);
133 talloc_steal(ldb, opaque_name_talloc);
134 talloc_free(tmp_ctx);
139 static PyMethodDef py_samba_ldb_methods[] = {
140 { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS,
141 "ldb_set_loadparm(ldb, session_info)\n"
142 "Set loadparm context to use when connecting." },
143 { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
144 "ldb_set_credentials(ldb, credentials)\n"
145 "Set credentials to use when connecting." },
146 { "set_opaque_integer", (PyCFunction)py_ldb_set_opaque_integer,
147 METH_VARARGS, NULL },
151 static PyTypeObject PySambaLdb = {
152 .tp_name = "samba.Ldb",
153 .tp_doc = "Connection to a LDB database.",
154 .tp_methods = py_samba_ldb_methods,
155 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
163 pyldb_module = PyImport_ImportModule("ldb");
164 if (pyldb_module == NULL)
167 PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
168 if (PySambaLdb.tp_base == NULL)
171 if (PyType_Ready(&PySambaLdb) < 0)
174 m = Py_InitModule3("_ldb", NULL, "Samba-specific LDB python bindings");
178 Py_INCREF(&PySambaLdb);
179 PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);