LCOV - code coverage report
Current view: top level - lib/ldb - pyldb_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 79 106 74.5 %
Date: 2024-05-31 13:13:24 Functions: 5 5 100.0 %

          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    89501876 : static PyTypeObject * PyLdb_GetPyType(const char *typename)
      36             : {
      37    89501876 :         PyTypeObject *type = NULL;
      38    76465984 :         bool ok;
      39             : 
      40    89501876 :         if (ldb_module == NULL) {
      41        3692 :                 ldb_module = PyImport_ImportModule("ldb");
      42        3692 :                 if (ldb_module == NULL) {
      43           0 :                         return NULL;
      44             :                 }
      45             :         }
      46             : 
      47    89501876 :         type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, typename);
      48             : 
      49             : 
      50    89501876 :         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    89501876 :         ok = PyType_Check(type);
      58    89501876 :         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    13035892 :         return type;
      67             : }
      68             : 
      69    63253853 : bool pyldb_check_type(PyObject *obj, const char *typename)
      70             : {
      71    63253853 :         bool ok = false;
      72    63253853 :         PyTypeObject *type = PyLdb_GetPyType(typename);
      73    63253853 :         if (type != NULL) {
      74    63253853 :                 ok = PyObject_TypeCheck(obj, type);
      75    54020325 :                 Py_DECREF(type);
      76             :         }
      77    63253853 :         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     4516989 : bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
      89             :                    struct ldb_context *ldb_ctx, struct ldb_dn **dn)
      90             : {
      91     3789698 :         struct ldb_dn *odn;
      92     3789698 :         PyTypeObject *PyLdb_Dn_Type;
      93     3789698 :         bool is_dn;
      94             : 
      95     4516989 :         if (ldb_ctx != NULL && (PyUnicode_Check(object))) {
      96     1643553 :                 const char *odn_str = NULL;
      97             : 
      98     1643553 :                 odn_str = PyUnicode_AsUTF8(object);
      99     1643553 :                 if (odn_str == NULL) {
     100           0 :                         return false;
     101             :                 }
     102             : 
     103     1643553 :                 odn = ldb_dn_new(mem_ctx, ldb_ctx, odn_str);
     104     1643553 :                 if (odn == NULL) {
     105           0 :                         PyErr_NoMemory();
     106           0 :                         return false;
     107             :                 }
     108             : 
     109     1643553 :                 *dn = odn;
     110     1643553 :                 return true;
     111             :         }
     112             : 
     113     2873436 :         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     2870973 :         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
     132     2870973 :         if (PyLdb_Dn_Type == NULL) {
     133           0 :                 PyErr_SetString(PyExc_TypeError, "Expected DN");
     134           0 :                 return false;
     135             :         }
     136             : 
     137     2870973 :         is_dn = PyObject_TypeCheck(object, PyLdb_Dn_Type);
     138     2385792 :         Py_DECREF(PyLdb_Dn_Type);
     139     2870973 :         if (is_dn) {
     140     2870971 :                 *dn = pyldb_Dn_AS_DN(object);
     141     2870971 :                 return true;
     142             :         }
     143             : 
     144           2 :         PyErr_SetString(PyExc_TypeError, "Expected DN");
     145           2 :         return false;
     146             : }
     147             : 
     148    23377050 : PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn, PyLdbObject *pyldb)
     149             : {
     150    23377050 :         TALLOC_CTX *mem_ctx = NULL;
     151    23377050 :         struct ldb_dn *dn_ref = NULL;
     152    20059867 :         PyLdbDnObject *py_ret;
     153    20059867 :         PyTypeObject *PyLdb_Dn_Type;
     154             : 
     155    23377050 :         if (dn == NULL) {
     156           0 :                 Py_RETURN_NONE;
     157             :         }
     158             : 
     159    23377050 :         mem_ctx = talloc_new(NULL);
     160    23377050 :         if (mem_ctx == NULL) {
     161           0 :                 return PyErr_NoMemory();
     162             :         }
     163             : 
     164    23377050 :         dn_ref = talloc_reference(mem_ctx, dn);
     165    23377050 :         if (dn_ref == NULL) {
     166           0 :                 talloc_free(mem_ctx);
     167           0 :                 return PyErr_NoMemory();
     168             :         }
     169             : 
     170    23377050 :         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
     171    23377050 :         if (PyLdb_Dn_Type == NULL) {
     172           0 :                 talloc_free(mem_ctx);
     173           0 :                 return NULL;
     174             :         }
     175             : 
     176    23377050 :         py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
     177    20059867 :         Py_DECREF(PyLdb_Dn_Type);
     178    23377050 :         if (py_ret == NULL) {
     179           0 :                 talloc_free(mem_ctx);
     180           0 :                 PyErr_NoMemory();
     181           0 :                 return NULL;
     182             :         }
     183    23377050 :         py_ret->mem_ctx = mem_ctx;
     184    23377050 :         py_ret->dn = dn;
     185    23377050 :         py_ret->pyldb = pyldb;
     186             : 
     187    23377050 :         Py_INCREF(py_ret->pyldb);
     188    23377050 :         return (PyObject *)py_ret;
     189             : }
     190             : 
     191      165049 : void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
     192             : {
     193      165049 :         PyObject *exc = NULL;
     194      165049 :         const char *ldb_error_string = NULL;
     195             : 
     196      165049 :         if (ret == LDB_ERR_PYTHON_EXCEPTION) {
     197           0 :                 return; /* Python exception should already be set, just keep that */
     198             :         }
     199             : 
     200      165049 :         if (ldb_ctx != NULL) {
     201      165049 :                 ldb_error_string = ldb_errstring(ldb_ctx);
     202             :         }
     203             :         /* either no LDB context, no string stored or string reset */
     204      165049 :         if (ldb_error_string == NULL) {
     205           1 :                 ldb_error_string = ldb_strerror(ret);
     206             :         }
     207             : 
     208      165049 :         exc = Py_BuildValue("(i,s)", ret, ldb_error_string);
     209      165049 :         if (exc == NULL) {
     210             :                 /*
     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.
     214             :                  */
     215           0 :                 fprintf(stderr, "could not make LdbError %d!\n", ret);
     216           0 :                 return;
     217             :         }
     218      165049 :         PyErr_SetObject(error, exc);
     219      134953 :         Py_DECREF(exc);
     220             : }

Generated by: LCOV version 1.14