Line data Source code
1 : /* 2 : Unix SMB/CIFS implementation. 3 : 4 : Python interface to ldb - utility functions. 5 : 6 : Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org> 7 : 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 11 : 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. 16 : 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. 21 : 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/>. 24 : */ 25 : 26 : #include "lib/replace/system/python.h" 27 : #include "ldb.h" 28 : #include "pyldb.h" 29 : 30 : static PyObject *ldb_module = NULL; 31 : 32 : /** 33 : * Find out PyTypeObject in ldb module for a given typename 34 : */ 35 26154211 : static PyTypeObject * PyLdb_GetPyType(const char *typename) 36 : { 37 26154211 : PyTypeObject *type = NULL; 38 22375643 : bool ok; 39 : 40 26154211 : if (ldb_module == NULL) { 41 3120 : ldb_module = PyImport_ImportModule("ldb"); 42 3120 : if (ldb_module == NULL) { 43 0 : return NULL; 44 : } 45 : } 46 : 47 26154211 : type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, typename); 48 : 49 : 50 26154211 : if (type == NULL) { 51 0 : PyErr_Format(PyExc_NameError, 52 : "Unable to find type %s in ldb module", 53 : typename); 54 0 : return NULL; 55 : } 56 : 57 26154211 : ok = PyType_Check(type); 58 26154211 : if (! ok) { 59 0 : PyErr_Format(PyExc_TypeError, 60 : "Expected type ldb.%s, not %s", 61 0 : typename, Py_TYPE(type)->tp_name); 62 0 : Py_DECREF(type); 63 0 : return NULL; 64 : } 65 : 66 3778568 : return type; 67 : } 68 : 69 1830 : bool pyldb_check_type(PyObject *obj, const char *typename) 70 : { 71 1830 : bool ok = false; 72 1830 : PyTypeObject *type = PyLdb_GetPyType(typename); 73 1830 : if (type != NULL) { 74 1830 : ok = PyObject_TypeCheck(obj, type); 75 1536 : Py_DECREF(type); 76 : } 77 1830 : return ok; 78 : } 79 : 80 : /** 81 : * Obtain a ldb DN from a Python object. 82 : * 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 87 : */ 88 4483599 : bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 89 : struct ldb_context *ldb_ctx, struct ldb_dn **dn) 90 : { 91 3762465 : struct ldb_dn *odn; 92 3762465 : PyTypeObject *PyLdb_Dn_Type; 93 3762465 : bool is_dn; 94 : 95 4483599 : if (ldb_ctx != NULL && (PyUnicode_Check(object))) { 96 1663271 : const char *odn_str = NULL; 97 : 98 1663271 : odn_str = PyUnicode_AsUTF8(object); 99 1663271 : if (odn_str == NULL) { 100 0 : return false; 101 : } 102 : 103 1663271 : odn = ldb_dn_new(mem_ctx, ldb_ctx, odn_str); 104 1663271 : if (odn == NULL) { 105 0 : PyErr_NoMemory(); 106 0 : return false; 107 : } 108 : 109 1663271 : *dn = odn; 110 1663271 : return true; 111 : } 112 : 113 2820328 : if (ldb_ctx != NULL && PyBytes_Check(object)) { 114 2463 : const char *odn_str = NULL; 115 : 116 2463 : odn_str = PyBytes_AsString(object); 117 2463 : if (odn_str == NULL) { 118 0 : return false; 119 : } 120 : 121 2463 : odn = ldb_dn_new(mem_ctx, ldb_ctx, odn_str); 122 2463 : if (odn == NULL) { 123 0 : PyErr_NoMemory(); 124 0 : return false; 125 : } 126 : 127 2463 : *dn = odn; 128 2463 : return true; 129 : } 130 : 131 2817865 : PyLdb_Dn_Type = PyLdb_GetPyType("Dn"); 132 2817865 : if (PyLdb_Dn_Type == NULL) { 133 0 : return false; 134 : } 135 : 136 2817865 : is_dn = PyObject_TypeCheck(object, PyLdb_Dn_Type); 137 2337506 : Py_DECREF(PyLdb_Dn_Type); 138 2817865 : if (is_dn) { 139 2817862 : *dn = pyldb_Dn_AS_DN(object); 140 2817862 : return true; 141 : } 142 : 143 3 : PyErr_SetString(PyExc_TypeError, "Expected DN"); 144 3 : return false; 145 : } 146 : 147 23334527 : PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn) 148 : { 149 23334527 : TALLOC_CTX *mem_ctx = NULL; 150 23334527 : struct ldb_dn *dn_ref = NULL; 151 20036612 : PyLdbDnObject *py_ret; 152 20036612 : PyTypeObject *PyLdb_Dn_Type; 153 : 154 23334527 : if (dn == NULL) { 155 11 : Py_RETURN_NONE; 156 : } 157 : 158 23334516 : mem_ctx = talloc_new(NULL); 159 23334516 : if (mem_ctx == NULL) { 160 0 : return PyErr_NoMemory(); 161 : } 162 : 163 23334516 : dn_ref = talloc_reference(mem_ctx, dn); 164 23334516 : if (dn_ref == NULL) { 165 0 : talloc_free(mem_ctx); 166 0 : return PyErr_NoMemory(); 167 : } 168 : 169 23334516 : PyLdb_Dn_Type = PyLdb_GetPyType("Dn"); 170 23334516 : if (PyLdb_Dn_Type == NULL) { 171 0 : talloc_free(mem_ctx); 172 0 : return NULL; 173 : } 174 : 175 23334516 : py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0); 176 20036601 : Py_DECREF(PyLdb_Dn_Type); 177 23334516 : if (py_ret == NULL) { 178 0 : talloc_free(mem_ctx); 179 0 : PyErr_NoMemory(); 180 0 : return NULL; 181 : } 182 23334516 : py_ret->mem_ctx = mem_ctx; 183 23334516 : py_ret->dn = dn; 184 23334516 : return (PyObject *)py_ret; 185 : }