LCOV - code coverage report
Current view: top level - lib/ldb-samba - pyldb.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 78 94 83.0 %
Date: 2024-04-21 15:09:00 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Python interface to ldb, Samba-specific functions
       5             : 
       6             :    Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
       7             : 
       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.
      12             : 
      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.
      17             : 
      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/>.
      20             : */
      21             : 
      22             : #include "lib/replace/system/python.h"
      23             : #include "python/py3compat.h"
      24             : #include "includes.h"
      25             : #include <ldb.h>
      26             : #include <pyldb.h>
      27             : #include "param/pyparam.h"
      28             : #include "auth/credentials/pycredentials.h"
      29             : #include "ldb_wrap.h"
      30             : #include "lib/ldb-samba/ldif_handlers.h"
      31             : #include "auth/pyauth.h"
      32             : #include "source4/dsdb/common/util.h"
      33             : #include "lib/ldb/include/ldb_private.h"
      34             : 
      35             : 
      36             : static PyObject *pyldb_module;
      37             : static PyObject *py_ldb_error;
      38             : static PyTypeObject PySambaLdb;
      39             : 
      40       34894 : static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args)
      41             : {
      42         407 :         PyObject *py_lp_ctx;
      43         407 :         struct loadparm_context *lp_ctx;
      44         407 :         struct ldb_context *ldb;
      45             : 
      46       34894 :         if (!PyArg_ParseTuple(args, "O", &py_lp_ctx))
      47           0 :                 return NULL;
      48             : 
      49       34894 :         ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
      50             : 
      51       34894 :         lp_ctx = lpcfg_from_py_object(ldb, py_lp_ctx);
      52       34894 :         if (lp_ctx == NULL) {
      53           0 :                 PyErr_SetString(PyExc_TypeError, "Expected loadparm object");
      54           0 :                 return NULL;
      55             :         }
      56             : 
      57       34894 :         ldb_set_opaque(ldb, "loadparm", lp_ctx);
      58             : 
      59       34894 :         Py_RETURN_NONE;
      60             : }
      61             : 
      62       31832 : static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args)
      63             : {
      64         110 :         PyObject *py_creds;
      65         110 :         struct cli_credentials *creds;
      66         110 :         struct ldb_context *ldb;
      67             : 
      68       31832 :         if (!PyArg_ParseTuple(args, "O", &py_creds))
      69           0 :                 return NULL;
      70             : 
      71       31832 :         creds = cli_credentials_from_py_object(py_creds);
      72       31832 :         if (creds == NULL) {
      73           0 :                 PyErr_SetString(PyExc_TypeError, "Expected credentials object");
      74           0 :                 return NULL;
      75             :         }
      76             : 
      77       31832 :         ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
      78             : 
      79       31832 :         ldb_set_opaque(ldb, "credentials", creds);
      80             : 
      81       31832 :         Py_RETURN_NONE;
      82             : }
      83             : 
      84       35375 : static PyObject *py_ldb_set_utf8_casefold(PyObject *self,
      85             :                 PyObject *Py_UNUSED(ignored))
      86             : {
      87         450 :         struct ldb_context *ldb;
      88             : 
      89       35375 :         ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
      90             : 
      91       35375 :         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
      92             : 
      93       35375 :         Py_RETURN_NONE;
      94             : }
      95             : 
      96       24452 : static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args)
      97             : {
      98         463 :         PyObject *py_session_info;
      99         463 :         struct auth_session_info *info;
     100         463 :         struct ldb_context *ldb;
     101         463 :         PyObject *mod_samba_auth;
     102         463 :         PyObject *PyAuthSession_Type;
     103         463 :         bool ret;
     104             : 
     105       24452 :         mod_samba_auth = PyImport_ImportModule("samba.dcerpc.auth");
     106       24452 :         if (mod_samba_auth == NULL)
     107           0 :                 return NULL;
     108             : 
     109       24452 :         PyAuthSession_Type = PyObject_GetAttrString(mod_samba_auth, "session_info");
     110       24452 :         if (PyAuthSession_Type == NULL) {
     111           0 :                 Py_CLEAR(mod_samba_auth);
     112           0 :                 return NULL;
     113             :         }
     114             : 
     115       24452 :         ret = PyArg_ParseTuple(args, "O!", PyAuthSession_Type, &py_session_info);
     116             : 
     117       17892 :         Py_DECREF(PyAuthSession_Type);
     118       24452 :         Py_DECREF(mod_samba_auth);
     119             : 
     120       24452 :         if (!ret)
     121           0 :                 return NULL;
     122             : 
     123       24452 :         ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
     124             : 
     125       24452 :         info = PyAuthSession_AsSession(py_session_info);
     126             : 
     127       24452 :         ldb_set_opaque(ldb, DSDB_SESSION_INFO, info);
     128             : 
     129       24452 :         Py_RETURN_NONE;
     130             : }
     131             : 
     132           4 : static PyObject *py_ldb_samba_schema_attribute_add(PyLdbObject *self,
     133             :                                                    PyObject *args)
     134             : {
     135           4 :         char *attribute, *syntax;
     136           4 :         const struct ldb_schema_syntax *s;
     137           4 :         unsigned int flags;
     138           4 :         int ret;
     139           4 :         struct ldb_context *ldb_ctx;
     140             : 
     141           4 :         if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
     142           0 :                 return NULL;
     143             : 
     144           4 :         ldb_ctx = pyldb_Ldb_AsLdbContext((PyObject *)self);
     145             : 
     146           4 :         s = ldb_samba_syntax_by_name(ldb_ctx, syntax);
     147           4 :         ret = ldb_schema_attribute_add_with_syntax(ldb_ctx, attribute,
     148             :                                                    flags, s);
     149             : 
     150           4 :         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb_ctx);
     151             : 
     152           4 :         Py_RETURN_NONE;
     153             : }
     154             : 
     155       35375 : static PyObject *py_ldb_register_samba_handlers(PyObject *self,
     156             :                 PyObject *Py_UNUSED(ignored))
     157             : {
     158         450 :         struct ldb_context *ldb;
     159         450 :         int ret;
     160             : 
     161             :         /* XXX: Perhaps call this from PySambaLdb's init function ? */
     162             : 
     163       35375 :         ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
     164       35375 :         ret = ldb_register_samba_handlers(ldb);
     165             : 
     166       35375 :         PyErr_LDB_ERROR_IS_ERR_RAISE(py_ldb_error, ret, ldb);
     167             : 
     168       35375 :         Py_RETURN_NONE;
     169             : }
     170             : 
     171             : static PyMethodDef py_samba_ldb_methods[] = {
     172             :         { "set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, 
     173             :                 "set_loadparm(session_info)\n"
     174             :                 "Set loadparm context to use when connecting." },
     175             :         { "set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS,
     176             :                 "set_credentials(credentials)\n"
     177             :                 "Set credentials to use when connecting." },
     178             :         { "set_utf8_casefold", (PyCFunction)py_ldb_set_utf8_casefold, 
     179             :                 METH_NOARGS,
     180             :                 "set_utf8_casefold()\n"
     181             :                 "Set the right Samba casefolding function for UTF8 charset." },
     182             :         { "register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers,
     183             :                 METH_NOARGS,
     184             :                 "register_samba_handlers()\n"
     185             :                 "Register Samba-specific LDB modules and schemas." },
     186             :         { "set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS,
     187             :                 "set_session_info(session_info)\n"
     188             :                 "Set session info to use when connecting." },
     189             :         { "samba_schema_attribute_add",
     190             :                 (PyCFunction)py_ldb_samba_schema_attribute_add,
     191             :                 METH_VARARGS, NULL },
     192             :         {0},
     193             : };
     194             : 
     195             : static struct PyModuleDef moduledef = {
     196             :     PyModuleDef_HEAD_INIT,
     197             :     .m_name = "_ldb",
     198             :     .m_doc = "Samba-specific LDB python bindings",
     199             :     .m_size = -1,
     200             :     .m_methods = py_samba_ldb_methods,
     201             : };
     202             : 
     203             : static PyTypeObject PySambaLdb = {
     204             :         .tp_name = "samba._ldb.Ldb",
     205             :         .tp_doc = "Connection to a LDB database.",
     206             :         .tp_methods = py_samba_ldb_methods,
     207             :         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
     208             : };
     209             : 
     210             : 
     211       12846 : MODULE_INIT_FUNC(_ldb)
     212             : {
     213         563 :         PyObject *m;
     214             : 
     215       12846 :         pyldb_module = PyImport_ImportModule("ldb");
     216       12846 :         if (pyldb_module == NULL)
     217           0 :                 return NULL;
     218             : 
     219       12846 :         PySambaLdb.tp_base = (PyTypeObject *)PyObject_GetAttrString(pyldb_module, "Ldb");
     220       12846 :         if (PySambaLdb.tp_base == NULL) {
     221           0 :                 Py_CLEAR(pyldb_module);
     222           0 :                 return NULL;
     223             :         }
     224             : 
     225       12846 :         py_ldb_error = PyObject_GetAttrString(pyldb_module, "LdbError");
     226             : 
     227       12846 :         Py_CLEAR(pyldb_module);
     228             : 
     229       12846 :         if (PyType_Ready(&PySambaLdb) < 0)
     230           0 :                 return NULL;
     231             : 
     232       12846 :         m = PyModule_Create(&moduledef);
     233       12846 :         if (m == NULL)
     234           0 :                 return NULL;
     235             : 
     236       10715 :         Py_INCREF(&PySambaLdb);
     237       12846 :         PyModule_AddObject(m, "Ldb", (PyObject *)&PySambaLdb);
     238             : 
     239             : #define ADD_LDB_STRING(val)  PyModule_AddStringConstant(m, #val, LDB_## val)
     240       12846 :         ADD_LDB_STRING(SYNTAX_SAMBA_INT32);
     241             : 
     242       12846 :         return m;
     243             : }

Generated by: LCOV version 1.14