LCOV - code coverage report
Current view: top level - source4/lib/policy - pypolicy.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 41 73 56.2 %
Date: 2021-09-23 10:06:22 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  Python bindings for libpolicy
       4             :  *  Copyright (C) Jelmer Vernooij 2010
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (at your option) any later version.
      10             :  *
      11             :  *  This program is distributed in the hope that it will be useful,
      12             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  *  GNU General Public License for more details.
      15             :  *
      16             :  *  You should have received a copy of the GNU General Public License
      17             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <Python.h>
      21             : #include "includes.h"
      22             : #include "python/py3compat.h"
      23             : #include "policy.h"
      24             : #include "libcli/util/pyerrors.h"
      25             : 
      26             : void initpolicy(void);
      27             : 
      28          31 : static PyObject *py_get_gpo_flags(PyObject *self, PyObject *args)
      29             : {
      30             :         int flags;
      31             :         PyObject *py_ret;
      32             :         const char **ret;
      33             :         TALLOC_CTX *mem_ctx;
      34             :         int i;
      35             :         NTSTATUS status;
      36             : 
      37          31 :         if (!PyArg_ParseTuple(args, "i", &flags))
      38           0 :                 return NULL;
      39             : 
      40          31 :         mem_ctx = talloc_new(NULL);
      41          31 :         if (mem_ctx == NULL) {
      42           0 :                 PyErr_NoMemory();
      43           0 :                 return NULL;
      44             :         }
      45             : 
      46          31 :         status = gp_get_gpo_flags(mem_ctx, flags, &ret);
      47          31 :         if (!NT_STATUS_IS_OK(status)) {
      48           0 :                 PyErr_SetNTSTATUS(status);
      49           0 :                 talloc_free(mem_ctx);
      50           0 :                 return NULL;
      51             :         }
      52             : 
      53          31 :         py_ret = PyList_New(0);
      54          32 :         for (i = 0; ret[i]; i++) {
      55           1 :                 int res = 0;
      56           1 :                 PyObject *item = PyUnicode_FromString(ret[i]);
      57           1 :                 if (item == NULL) {
      58           0 :                         talloc_free(mem_ctx);
      59           0 :                         Py_DECREF(py_ret);
      60           0 :                         PyErr_NoMemory();
      61           0 :                         return NULL;
      62             :                 }
      63           1 :                 res = PyList_Append(py_ret, item);
      64           1 :                 Py_CLEAR(item);
      65           1 :                 if (res == -1) {
      66           0 :                         Py_DECREF(py_ret);
      67           0 :                         talloc_free(mem_ctx);
      68           0 :                         return NULL;
      69             :                 }
      70             :         }
      71             : 
      72          31 :         talloc_free(mem_ctx);
      73             : 
      74          31 :         return py_ret;
      75             : }
      76             : 
      77           1 : static PyObject *py_get_gplink_options(PyObject *self, PyObject *args)
      78             : {
      79             :         int flags;
      80             :         PyObject *py_ret;
      81             :         const char **ret;
      82             :         TALLOC_CTX *mem_ctx;
      83             :         int i;
      84             :         NTSTATUS status;
      85             : 
      86           1 :         if (!PyArg_ParseTuple(args, "i", &flags))
      87           0 :                 return NULL;
      88             : 
      89           1 :         mem_ctx = talloc_new(NULL);
      90           1 :         if (mem_ctx == NULL) {
      91           0 :                 PyErr_NoMemory();
      92           0 :                 return NULL;
      93             :         }
      94             : 
      95           1 :         status = gp_get_gplink_options(mem_ctx, flags, &ret);
      96           1 :         if (!NT_STATUS_IS_OK(status)) {
      97           0 :                 PyErr_SetNTSTATUS(status);
      98           0 :                 talloc_free(mem_ctx);
      99           0 :                 return NULL;
     100             :         }
     101             : 
     102           1 :         py_ret = PyList_New(0);
     103           2 :         for (i = 0; ret[i]; i++) {
     104           1 :                 int res = 0;
     105           1 :                 PyObject *item = PyUnicode_FromString(ret[i]);
     106           1 :                 if (item == NULL) {
     107           0 :                         talloc_free(mem_ctx);
     108           0 :                         Py_DECREF(py_ret);
     109           0 :                         PyErr_NoMemory();
     110           0 :                         return NULL;
     111             :                 }
     112           1 :                 res = PyList_Append(py_ret, item);
     113           1 :                 Py_CLEAR(item);
     114           1 :                 if (res == -1) {
     115           0 :                         Py_DECREF(py_ret);
     116           0 :                         talloc_free(mem_ctx);
     117           0 :                         return NULL;
     118             :                 }
     119             :         }
     120             : 
     121           1 :         talloc_free(mem_ctx);
     122             : 
     123           1 :         return py_ret;
     124             : }
     125             : 
     126           0 : static PyObject *py_ads_to_dir_access_mask(PyObject *self, PyObject *args)
     127             : {
     128             :         uint32_t access_mask, dir_mask;
     129             : 
     130           0 :         if (! PyArg_ParseTuple(args, "I", &access_mask))
     131           0 :                 return NULL;
     132             : 
     133           0 :         dir_mask = gp_ads_to_dir_access_mask(access_mask);
     134             : 
     135           0 :         return Py_BuildValue("I", dir_mask);
     136             : }
     137             : 
     138             : 
     139             : static PyMethodDef py_policy_methods[] = {
     140             :         { "get_gpo_flags", (PyCFunction)py_get_gpo_flags, METH_VARARGS,
     141             :                 "get_gpo_flags(flags) -> list" },
     142             :         { "get_gplink_options", (PyCFunction)py_get_gplink_options, METH_VARARGS,
     143             :                 "get_gplink_options(options) -> list" },
     144             :         { "ads_to_dir_access_mask", (PyCFunction)py_ads_to_dir_access_mask, METH_VARARGS,
     145             :                 "ads_to_dir_access_mask(access_mask) -> dir_mask" },
     146             :         {0}
     147             : };
     148             : 
     149             : static struct PyModuleDef moduledef = {
     150             :     PyModuleDef_HEAD_INIT,
     151             :     .m_name = "policy",
     152             :     .m_doc = "(Group) Policy manipulation",
     153             :     .m_size = -1,
     154             :     .m_methods = py_policy_methods,
     155             : };
     156             : 
     157         191 : MODULE_INIT_FUNC(policy)
     158             : {
     159         191 :         PyObject *m = NULL;
     160             : 
     161         191 :         m = PyModule_Create(&moduledef);
     162         191 :         if (!m)
     163           0 :                 return m;
     164             : 
     165         191 :         PyModule_AddObject(m, "GPO_FLAG_USER_DISABLE",
     166             :                                            PyLong_FromLong(GPO_FLAG_USER_DISABLE));
     167         191 :         PyModule_AddObject(m, "GPO_MACHINE_USER_DISABLE",
     168             :                                            PyLong_FromLong(GPO_FLAG_MACHINE_DISABLE));
     169         191 :         PyModule_AddObject(m, "GPLINK_OPT_DISABLE",
     170             :                                            PyLong_FromLong(GPLINK_OPT_DISABLE ));
     171         191 :         PyModule_AddObject(m, "GPLINK_OPT_ENFORCE ",
     172             :                                            PyLong_FromLong(GPLINK_OPT_ENFORCE ));
     173         191 :         return m;
     174             : }

Generated by: LCOV version 1.13