LCOV - code coverage report
Current view: top level - source4/libnet - py_net_dckeytab.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 24 35 68.6 %
Date: 2021-09-23 10:06:22 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             : 
       5             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
       6             :    Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2009
       7             :    Copyright (C) Alexander Bokovoy <ab@samba.org> 2012
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include <Python.h>
      24             : #include "includes.h"
      25             : #include "python/py3compat.h"
      26             : #include "python/modules.h"
      27             : #include "py_net.h"
      28             : #include "libnet_export_keytab.h"
      29             : 
      30             : void initdckeytab(void);
      31             : 
      32          13 : static PyObject *py_net_export_keytab(py_net_Object *self, PyObject *args, PyObject *kwargs)
      33             : {
      34             :         struct libnet_export_keytab r;
      35             :         TALLOC_CTX *mem_ctx;
      36          13 :         const char *kwnames[] = { "keytab", "principal", NULL };
      37             :         NTSTATUS status;
      38          13 :         r.in.principal = NULL;
      39             : 
      40          13 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z:export_keytab", discard_const_p(char *, kwnames),
      41             :                                          &r.in.keytab_name,
      42             :                                          &r.in.principal)) {
      43           0 :                 return NULL;
      44             :         }
      45             : 
      46          13 :         mem_ctx = talloc_new(self->mem_ctx);
      47          13 :         if (mem_ctx == NULL) {
      48           0 :                 PyErr_NoMemory();
      49           0 :                 return NULL;
      50             :         }
      51             : 
      52          13 :         status = libnet_export_keytab(self->libnet_ctx, mem_ctx, &r);
      53          13 :         if (NT_STATUS_IS_ERR(status)) {
      54           0 :                 PyErr_SetString(PyExc_RuntimeError,
      55           0 :                                 r.out.error_string?r.out.error_string:nt_errstr(status));
      56           0 :                 talloc_free(mem_ctx);
      57           0 :                 return NULL;
      58             :         }
      59             : 
      60          13 :         talloc_free(mem_ctx);
      61             : 
      62          13 :         Py_RETURN_NONE;
      63             : }
      64             : 
      65             : static const char py_net_export_keytab_doc[] = "export_keytab(keytab, name)\n\n"
      66             : "Export the DC keytab to a keytab file.";
      67             : 
      68             : static PyMethodDef export_keytab_method_table[] = {
      69             :         {"export_keytab", PY_DISCARD_FUNC_SIG(PyCFunction,
      70             :                                               py_net_export_keytab),
      71             :                 METH_VARARGS|METH_KEYWORDS, py_net_export_keytab_doc},
      72             :         { NULL, NULL, 0, NULL }
      73             : };
      74             : 
      75             : /*
      76             :  * A fake Python module to inject export_keytab() method into existing samba.net.Net class.
      77             :  * Python enforces that every loaded module actually creates Python module record in
      78             :  * the global module table even if we don't really need that record. Thus, we initialize
      79             :  * dckeytab module but never use it.
      80             :  * */
      81             : void initdckeytab(void);
      82             : static struct PyModuleDef moduledef = {
      83             :     PyModuleDef_HEAD_INIT,
      84             :     .m_name = "dckeytab",
      85             :     .m_doc = "dckeytab",
      86             :     .m_size = -1,
      87             :     .m_methods = NULL
      88             : };
      89             : 
      90         657 : MODULE_INIT_FUNC(dckeytab)
      91             : {
      92         657 :         PyObject *m = NULL;
      93             :         PyObject *Net;
      94             :         PyObject *descr;
      95             :         int ret;
      96             : 
      97         657 :         m = PyModule_Create(&moduledef);
      98         657 :         if (m == NULL)
      99           0 :                 return m;
     100             : 
     101         657 :         m = PyImport_ImportModule("samba.net");
     102         657 :         if (m == NULL)
     103           0 :                 return m;
     104             : 
     105         657 :         Net = (PyObject *)PyObject_GetAttrString(m, "Net");
     106         657 :         if (Net == NULL)
     107           0 :                 return m;
     108             : 
     109         657 :         descr = PyDescr_NewMethod((PyTypeObject*)Net, &export_keytab_method_table[0]);
     110         657 :         if (descr == NULL)
     111           0 :                 return m;
     112             : 
     113         657 :         ret = PyDict_SetItemString(((PyTypeObject*)Net)->tp_dict,
     114             :                                      export_keytab_method_table[0].ml_name,
     115             :                                      descr);
     116         657 :         if (ret != -1) {
     117         657 :                 Py_DECREF(descr);
     118             :         }
     119             : 
     120         624 :         return m;
     121             : }

Generated by: LCOV version 1.13