LCOV - code coverage report
Current view: top level - lib/ldb - pyldb_util.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 47 54 87.0 %
Date: 2021-09-23 10:06:22 Functions: 4 4 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 <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    20822193 : static PyTypeObject * PyLdb_GetPyType(const char *typename)
      36             : {
      37    20822193 :         PyTypeObject *type = NULL;
      38     2357736 :         bool ok;
      39             : 
      40    20822193 :         if (ldb_module == NULL) {
      41        2878 :                 ldb_module = PyImport_ImportModule("ldb");
      42        2878 :                 if (ldb_module == NULL) {
      43             :                         return NULL;
      44             :                 }
      45             :         }
      46             : 
      47    20822193 :         type = (PyTypeObject *)PyObject_GetAttrString(ldb_module, typename);
      48             : 
      49             : 
      50    20822193 :         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    20822193 :         ok = PyType_Check(type);
      58    20822193 :         if (! ok) {
      59           0 :                 PyErr_Format(PyExc_TypeError,
      60             :                              "Expected type ldb.%s, not %s",
      61             :                              typename, Py_TYPE(type)->tp_name);
      62           0 :                 Py_DECREF(type);
      63           0 :                 return NULL;
      64             :         }
      65             : 
      66             :         return type;
      67             : }
      68             : 
      69        1742 : bool pyldb_check_type(PyObject *obj, const char *typename)
      70             : {
      71        1742 :         bool ok = false;
      72        1742 :         PyTypeObject *type = PyLdb_GetPyType(typename);
      73        1742 :         if (type != NULL) {
      74        1742 :                 ok = PyObject_TypeCheck(obj, type);
      75        1742 :                 Py_DECREF(type);
      76             :         }
      77        1742 :         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     3732386 : bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
      89             :                    struct ldb_context *ldb_ctx, struct ldb_dn **dn)
      90             : {
      91      527093 :         struct ldb_dn *odn;
      92      527093 :         PyTypeObject *PyLdb_Dn_Type;
      93             : 
      94     3732386 :         if (ldb_ctx != NULL && (PyUnicode_Check(object))) {
      95     1342226 :                 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyUnicode_AsUTF8(object));
      96     1342226 :                 *dn = odn;
      97     1342226 :                 return true;
      98             :         }
      99             : 
     100     2390160 :         if (ldb_ctx != NULL && PyBytes_Check(object)) {
     101        2404 :                 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyBytes_AsString(object));
     102        2404 :                 *dn = odn;
     103        2404 :                 return true;
     104             :         }
     105             : 
     106     2387756 :         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
     107     2387756 :         if (PyLdb_Dn_Type == NULL) {
     108             :                 return false;
     109             :         }
     110             : 
     111     2387756 :         if (PyObject_TypeCheck(object, PyLdb_Dn_Type)) {
     112     2387754 :                 *dn = pyldb_Dn_AS_DN(object);
     113     2387754 :                 return true;
     114             :         }
     115             : 
     116           2 :         PyErr_SetString(PyExc_TypeError, "Expected DN");
     117           2 :         return false;
     118             : }
     119             : 
     120    18432704 : PyObject *pyldb_Dn_FromDn(struct ldb_dn *dn)
     121             : {
     122     1989318 :         PyLdbDnObject *py_ret;
     123     1989318 :         PyTypeObject *PyLdb_Dn_Type;
     124             : 
     125    18432704 :         if (dn == NULL) {
     126           9 :                 Py_RETURN_NONE;
     127             :         }
     128             : 
     129    18432695 :         PyLdb_Dn_Type = PyLdb_GetPyType("Dn");
     130    18432695 :         if (PyLdb_Dn_Type == NULL) {
     131             :                 return NULL;
     132             :         }
     133             : 
     134    18432695 :         py_ret = (PyLdbDnObject *)PyLdb_Dn_Type->tp_alloc(PyLdb_Dn_Type, 0);
     135    18432695 :         if (py_ret == NULL) {
     136           0 :                 PyErr_NoMemory();
     137           0 :                 return NULL;
     138             :         }
     139    18432695 :         py_ret->mem_ctx = talloc_new(NULL);
     140    18432695 :         py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
     141    18432695 :         return (PyObject *)py_ret;
     142             : }

Generated by: LCOV version 1.13