ctdb-daemon: Use ctdb_parse_node_address() in ctdbd
[samba4-gss.git] / lib / ldb / pyldb_util.c
blob44bc9941d2931f3a4bc5d785ba629311ed3271f4
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to ldb - utility functions.
6 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "lib/replace/system/python.h"
27 #include "ldb.h"
28 #include "pyldb.h"
30 static PyObject *ldb_module = NULL;
32 /**
33 * Find out PyTypeObject in ldb module for a given typename
35 static PyTypeObject * PyLdb_GetPyType(const char *typename)
37 PyTypeObject *type = NULL;
38 bool ok;
40 if (ldb_module == NULL) {
41 ldb_module = PyImport_ImportModule("ldb");
42 if (ldb_module == NULL) {
43 return NULL;
47 type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, typename);
50 if (type == NULL) {
51 PyErr_Format(PyExc_NameError,
52 "Unable to find type %s in ldb module",
53 typename);
54 return NULL;
57 ok = PyType_Check(type);
58 if (! ok) {
59 PyErr_Format(PyExc_TypeError,
60 "Expected type ldb.%s, not %s",
61 typename, Py_TYPE(type)->tp_name);
62 Py_DECREF(type);
63 return NULL;
66 return type;
69 bool pyldb_check_type(PyObject *obj, const char *typename)
71 bool ok = false;
72 PyTypeObject *type = PyLdb_GetPyType(typename);
73 if (type != NULL) {
74 ok = PyObject_TypeCheck(obj, type);
75 Py_DECREF(type);
77 return ok;
80 /**
81 * Obtain a ldb DN from a Python object.
83 * @param mem_ctx Memory context
84 * @param object Python object
85 * @param ldb_ctx LDB context
86 * @return Whether or not the conversion succeeded
88 bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
89 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
91 struct ldb_dn *odn;
92 PyTypeObject *PyLdb_Dn_Type;
93 bool is_dn;
95 if (ldb_ctx != NULL && (PyUnicode_Check(object))) {
96 const char *odn_str = NULL;
98 odn_str = PyUnicode_AsUTF8(object);
99 if (odn_str == NULL) {
100 return false;
103 odn = ldb_dn_new(mem_ctx, ldb_ctx, odn_str);
104 if (odn == NULL) {
105 PyErr_NoMemory();
106 return false;
109 *dn = odn;
110 return true;
113 if (ldb_ctx != NULL && PyBytes_Check(object)) {
114 const char *odn_str = NULL;
116 odn_str = PyBytes_AsString(object);
117 if (odn_str == NULL) {
118 return false;
121 odn = ldb_dn_new(mem_ctx, ldb_ctx, odn_str);
122 if (odn == NULL) {
123 PyErr_NoMemory();
124 return false;
127 *dn = odn;
128 return true;
131 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
132 if (PyLdb_Dn_Type == NULL) {
133 PyErr_SetString(PyExc_TypeError, "Expected DN");
134 return false;
137 is_dn = PyObject_TypeCheck(object, PyLdb_Dn_Type);
138 Py_DECREF(PyLdb_Dn_Type);
139 if (is_dn) {
140 *dn = pyldb_Dn_AS_DN(object);
141 return true;
144 PyErr_SetString(PyExc_TypeError, "Expected DN");
145 return false;
148 PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn, PyLdbObject *pyldb)
150 TALLOC_CTX *mem_ctx = NULL;
151 struct ldb_dn *dn_ref = NULL;
152 PyLdbDnObject *py_ret;
153 PyTypeObject *PyLdb_Dn_Type;
155 if (dn == NULL) {
156 Py_RETURN_NONE;
159 mem_ctx = talloc_new(NULL);
160 if (mem_ctx == NULL) {
161 return PyErr_NoMemory();
164 dn_ref = talloc_reference(mem_ctx, dn);
165 if (dn_ref == NULL) {
166 talloc_free(mem_ctx);
167 return PyErr_NoMemory();
170 PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
171 if (PyLdb_Dn_Type == NULL) {
172 talloc_free(mem_ctx);
173 return NULL;
176 py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
177 Py_DECREF(PyLdb_Dn_Type);
178 if (py_ret == NULL) {
179 talloc_free(mem_ctx);
180 PyErr_NoMemory();
181 return NULL;
183 py_ret->mem_ctx = mem_ctx;
184 py_ret->dn = dn;
185 py_ret->pyldb = pyldb;
187 Py_INCREF(py_ret->pyldb);
188 return (PyObject *)py_ret;
191 void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
193 PyObject *exc = NULL;
194 const char *ldb_error_string = NULL;
196 if (ret == LDB_ERR_PYTHON_EXCEPTION) {
197 return; /* Python exception should already be set, just keep that */
200 if (ldb_ctx != NULL) {
201 ldb_error_string = ldb_errstring(ldb_ctx);
203 /* either no LDB context, no string stored or string reset */
204 if (ldb_error_string == NULL) {
205 ldb_error_string = ldb_strerror(ret);
208 exc = Py_BuildValue("(i,s)", ret, ldb_error_string);
209 if (exc == NULL) {
211 * Py_BuildValue failed, and will have set its own exception.
212 * It isn't the one we wanted, but it will have to do.
213 * This is all very unexpected.
215 fprintf(stderr, "could not make LdbError %d!\n", ret);
216 return;
218 PyErr_SetObject(error, exc);
219 Py_DECREF(exc);