LCOV - code coverage report
Current view: top level - source4/librpc/ndr - py_security.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 137 282 48.6 %
Date: 2024-02-14 10:14:15 Functions: 21 35 60.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             :    
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             :    
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             :    
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : #include <Python.h>
      21             : #include "py3compat.h"
      22             : #include "libcli/security/sddl.h"
      23             : #include "libcli/security/security.h"
      24             : 
      25       14172 : static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
      26             : {
      27             :         PyObject *dict;
      28             :         int i;
      29       14172 :         if (type->tp_dict == NULL)
      30           0 :                 type->tp_dict = PyDict_New();
      31       14172 :         dict = type->tp_dict;
      32       77946 :         for (i = 0; methods[i].ml_name; i++) {
      33             :                 PyObject *descr;
      34       63774 :                 if (methods[i].ml_flags & METH_CLASS) 
      35        3543 :                         descr = PyCFunction_New(&methods[i], (PyObject *)type);
      36             :                 else 
      37       60231 :                         descr = PyDescr_NewMethod(type, &methods[i]);
      38       63774 :                 PyDict_SetItemString(dict, methods[i].ml_name, 
      39             :                                      descr);
      40       63774 :                 Py_CLEAR(descr);
      41             :         }
      42       14172 : }
      43             : 
      44        1887 : static PyObject *py_dom_sid_split(PyObject *py_self, PyObject *args)
      45             : {
      46        1887 :         struct dom_sid *self = pytalloc_get_ptr(py_self);
      47             :         struct dom_sid *domain_sid;
      48             :         TALLOC_CTX *mem_ctx;
      49             :         uint32_t rid;
      50             :         NTSTATUS status;
      51             :         PyObject *py_domain_sid;
      52             : 
      53        1887 :         mem_ctx = talloc_new(NULL);
      54        1887 :         if (mem_ctx == NULL) {
      55           0 :                 PyErr_NoMemory();
      56           0 :                 return NULL;
      57             :         }
      58             : 
      59        1887 :         status = dom_sid_split_rid(mem_ctx, self, &domain_sid, &rid);
      60        1887 :         if (!NT_STATUS_IS_OK(status)) {
      61           0 :                 PyErr_SetString(PyExc_RuntimeError, "dom_sid_split_rid failed");
      62           0 :                 talloc_free(mem_ctx);
      63           0 :                 return NULL;
      64             :         }
      65             : 
      66        1887 :         py_domain_sid = pytalloc_steal(&dom_sid_Type, domain_sid);
      67        1887 :         talloc_free(mem_ctx);
      68        1887 :         return Py_BuildValue("(OI)", py_domain_sid, rid);
      69             : }
      70             : 
      71             : #if PY_MAJOR_VERSION >= 3
      72        4209 : static PyObject *py_dom_sid_richcmp(PyObject *py_self, PyObject *py_other, int op)
      73             : {
      74        4209 :         struct dom_sid *self = pytalloc_get_ptr(py_self), *other;
      75             :         int val;
      76             : 
      77        4209 :         other = pytalloc_get_ptr(py_other);
      78        4209 :         if (other == NULL) {
      79           0 :                 Py_INCREF(Py_NotImplemented);
      80           0 :                 return Py_NotImplemented;
      81             :         }
      82             : 
      83        4209 :         val =  dom_sid_compare(self, other);
      84             : 
      85        4209 :         switch (op) {
      86        4092 :                         case Py_EQ: if (val == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      87         117 :                         case Py_NE: if (val != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      88           0 :                         case Py_LT: if (val <  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      89           0 :                         case Py_GT: if (val >  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      90           0 :                         case Py_LE: if (val <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      91           0 :                         case Py_GE: if (val >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      92             :         }
      93           0 :         Py_INCREF(Py_NotImplemented);
      94           0 :         return Py_NotImplemented;
      95             : }
      96             : #else
      97             : static int py_dom_sid_cmp(PyObject *py_self, PyObject *py_other)
      98             : {
      99             :         struct dom_sid *self = pytalloc_get_ptr(py_self), *other;
     100             :         int val;
     101             : 
     102             :         other = pytalloc_get_ptr(py_other);
     103             :         if (other == NULL)
     104             :                 return -1;
     105             : 
     106             :         val =  dom_sid_compare(self, other);
     107             :         if (val > 0) {
     108             :                 return 1;
     109             :         } else if (val < 0) {
     110             :                 return -1;
     111             :         }
     112             :         return 0;
     113             : }
     114             : #endif
     115             : 
     116       26396 : static PyObject *py_dom_sid_str(PyObject *py_self)
     117             : {
     118       26396 :         struct dom_sid *self = pytalloc_get_ptr(py_self);
     119             :         struct dom_sid_buf buf;
     120       26396 :         PyObject *ret = PyUnicode_FromString(dom_sid_str_buf(self, &buf));
     121       26396 :         return ret;
     122             : }
     123             : 
     124           0 : static PyObject *py_dom_sid_repr(PyObject *py_self)
     125             : {
     126           0 :         struct dom_sid *self = pytalloc_get_ptr(py_self);
     127             :         struct dom_sid_buf buf;
     128           0 :         PyObject *ret = PyUnicode_FromFormat(
     129             :                 "dom_sid('%s')", dom_sid_str_buf(self, &buf));
     130           0 :         return ret;
     131             : }
     132             : 
     133       32374 : static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
     134             : {
     135       32374 :         char *str = NULL;
     136       32374 :         struct dom_sid *sid = pytalloc_get_ptr(self);
     137       32374 :         const char *kwnames[] = { "str", NULL };
     138             : 
     139       32374 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
     140           0 :                 return -1;
     141             : 
     142       32374 :         if (str != NULL && !dom_sid_parse(str, sid)) {
     143        6666 :                 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
     144        6666 :                 return -1;
     145             :         }
     146             : 
     147       25708 :         return 0;
     148             : }
     149             : 
     150             : static PyMethodDef py_dom_sid_extra_methods[] = {
     151             :         { "split", (PyCFunction)py_dom_sid_split, METH_NOARGS,
     152             :                 "S.split() -> (domain_sid, rid)\n"
     153             :                 "Split a domain sid" },
     154             :         {0}
     155             : };
     156             : 
     157             : 
     158        3543 : static void py_dom_sid_patch(PyTypeObject *type)
     159             : {
     160        3543 :         type->tp_init = py_dom_sid_init;
     161        3543 :         type->tp_str = py_dom_sid_str;
     162        3543 :         type->tp_repr = py_dom_sid_repr;
     163             : #if PY_MAJOR_VERSION >= 3
     164        3543 :         type->tp_richcompare = py_dom_sid_richcmp;
     165             : #else
     166             :         type->tp_compare = py_dom_sid_cmp;
     167             : #endif
     168        3543 :         PyType_AddMethods(type, py_dom_sid_extra_methods);
     169        3543 : }
     170             : 
     171             : #define PY_DOM_SID_PATCH py_dom_sid_patch
     172             : 
     173        3636 : static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
     174             : {
     175        3636 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     176             :         NTSTATUS status;
     177             :         struct security_ace *ace;
     178             :         PyObject *py_ace;
     179        3636 :         Py_ssize_t idx = -1;
     180             : 
     181        3636 :         if (!PyArg_ParseTuple(args, "O|n", &py_ace, &idx))
     182           0 :                 return NULL;
     183             : 
     184        3636 :         ace = pytalloc_get_ptr(py_ace);
     185        3636 :         status = security_descriptor_sacl_insert(desc, ace, idx);
     186        3636 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     187        3636 :         Py_RETURN_NONE;
     188             : }
     189             : 
     190      547337 : static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
     191             : {
     192      547337 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     193             :         NTSTATUS status;
     194             :         struct security_ace *ace;
     195             :         PyObject *py_ace;
     196      547337 :         Py_ssize_t idx = -1;
     197             : 
     198      547337 :         if (!PyArg_ParseTuple(args, "O|n", &py_ace, &idx))
     199           0 :                 return NULL;
     200             : 
     201      547337 :         ace = pytalloc_get_ptr(py_ace);
     202             : 
     203      547337 :         status = security_descriptor_dacl_insert(desc, ace, idx);
     204      547337 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     205      547337 :         Py_RETURN_NONE;
     206             : }
     207             : 
     208           0 : static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
     209             : {
     210           0 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     211             :         NTSTATUS status;
     212             :         struct dom_sid *sid;
     213             :         PyObject *py_sid;
     214             : 
     215           0 :         if (!PyArg_ParseTuple(args, "O", &py_sid))
     216           0 :                 return NULL;
     217             : 
     218           0 :         sid = pytalloc_get_ptr(py_sid);
     219           0 :         status = security_descriptor_dacl_del(desc, sid);
     220           0 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     221           0 :         Py_RETURN_NONE;
     222             : }
     223             : 
     224           0 : static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
     225             : {
     226           0 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     227             :         NTSTATUS status;
     228             :         struct dom_sid *sid;
     229             :         PyObject *py_sid;
     230             : 
     231           0 :         if (!PyArg_ParseTuple(args, "O", &py_sid))
     232           0 :                 return NULL;
     233             : 
     234           0 :         sid = pytalloc_get_ptr(py_sid);
     235           0 :         status = security_descriptor_sacl_del(desc, sid);
     236           0 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     237           0 :         Py_RETURN_NONE;
     238             : }
     239             : 
     240       78662 : static PyObject *py_descriptor_dacl_del_ace(PyObject *self, PyObject *args)
     241             : {
     242       78662 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     243             :         NTSTATUS status;
     244       78662 :         struct security_ace *ace = NULL;
     245       78662 :         PyObject *py_ace = Py_None;
     246             : 
     247       78662 :         if (!PyArg_ParseTuple(args, "O!", &security_ace_Type, &py_ace))
     248           0 :                 return NULL;
     249             : 
     250       78662 :         if (!PyObject_TypeCheck(py_ace, &security_ace_Type)) {
     251           0 :                 PyErr_SetString(PyExc_TypeError,
     252             :                                 "expected security.security_ace "
     253             :                                 "for first argument to .dacl_del_ace");
     254           0 :                 return NULL;
     255             :         }
     256             : 
     257       78662 :         ace = pytalloc_get_ptr(py_ace);
     258       78662 :         status = security_descriptor_dacl_del_ace(desc, ace);
     259       78662 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     260       78653 :         Py_RETURN_NONE;
     261             : }
     262             : 
     263           0 : static PyObject *py_descriptor_sacl_del_ace(PyObject *self, PyObject *args)
     264             : {
     265           0 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     266             :         NTSTATUS status;
     267           0 :         struct security_ace *ace = NULL;
     268           0 :         PyObject *py_ace = Py_None;
     269             : 
     270           0 :         if (!PyArg_ParseTuple(args, "O!", &security_ace_Type, &py_ace))
     271           0 :                 return NULL;
     272             : 
     273           0 :         if (!PyObject_TypeCheck(py_ace, &security_ace_Type)) {
     274           0 :                 PyErr_SetString(PyExc_TypeError,
     275             :                                 "expected security.security_ace "
     276             :                                 "for first argument to .sacl_del_ace");
     277           0 :                 return NULL;
     278             :         }
     279             : 
     280           0 :         ace = pytalloc_get_ptr(py_ace);
     281           0 :         status = security_descriptor_sacl_del_ace(desc, ace);
     282           0 :         PyErr_NTSTATUS_IS_ERR_RAISE(status);
     283           0 :         Py_RETURN_NONE;
     284             : }
     285             : 
     286      646084 : static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
     287             : {
     288      646084 :         return pytalloc_steal(self, security_descriptor_initialise(NULL));
     289             : }
     290             : 
     291       31465 : static PyObject *py_descriptor_from_sddl(PyObject *self, PyObject *args)
     292             : {
     293             :         struct security_descriptor *secdesc;
     294             :         char *sddl;
     295             :         PyObject *py_sid;
     296             :         struct dom_sid *sid;
     297             : 
     298       31465 :         if (!PyArg_ParseTuple(args, "sO!", &sddl, &dom_sid_Type, &py_sid))
     299           0 :                 return NULL;
     300             : 
     301       31465 :         if (!PyObject_TypeCheck(py_sid, &dom_sid_Type)) {
     302           0 :                 PyErr_SetString(PyExc_TypeError,
     303             :                                 "expected security.dom_sid "
     304             :                                 "for second argument to .from_sddl");
     305           0 :                 return NULL;
     306             :         }
     307             : 
     308       31465 :         sid = pytalloc_get_ptr(py_sid);
     309             : 
     310       31465 :         secdesc = sddl_decode(NULL, sddl, sid);
     311       31465 :         if (secdesc == NULL) {
     312           0 :                 PyErr_SetString(PyExc_TypeError, "Unable to parse SDDL");
     313           0 :                 return NULL;
     314             :         }
     315             : 
     316       31465 :         return pytalloc_steal((PyTypeObject *)self, secdesc);
     317             : }
     318             : 
     319       72070 : static PyObject *py_descriptor_as_sddl(PyObject *self, PyObject *args)
     320             : {
     321             :         struct dom_sid *sid;
     322       72070 :         PyObject *py_sid = Py_None;
     323       72070 :         struct security_descriptor *desc = pytalloc_get_ptr(self);
     324             :         char *text;
     325             :         PyObject *ret;
     326             : 
     327       72070 :         if (!PyArg_ParseTuple(args, "|O!", &dom_sid_Type, &py_sid))
     328           0 :                 return NULL;
     329             : 
     330       72070 :         if (py_sid != Py_None)
     331       71786 :                 sid = pytalloc_get_ptr(py_sid);
     332             :         else
     333         284 :                 sid = NULL;
     334             : 
     335       72070 :         text = sddl_encode(NULL, desc, sid);
     336             : 
     337       72070 :         ret = PyUnicode_FromString(text);
     338             : 
     339       72070 :         talloc_free(text);
     340             : 
     341       72070 :         return ret;
     342             : }
     343             : 
     344             : static PyMethodDef py_descriptor_extra_methods[] = {
     345             :         { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS,
     346             :                 "S.sacl_add(ace) -> None\n"
     347             :                 "Add a security ace to this security descriptor" },
     348             :         { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS,
     349             :                 NULL },
     350             :         { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
     351             :                 NULL },
     352             :         { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
     353             :                 NULL },
     354             :         { "dacl_del_ace", (PyCFunction)py_descriptor_dacl_del_ace, METH_VARARGS,
     355             :                 NULL },
     356             :         { "sacl_del_ace", (PyCFunction)py_descriptor_sacl_del_ace, METH_VARARGS,
     357             :                 NULL },
     358             :         { "from_sddl", (PyCFunction)py_descriptor_from_sddl, METH_VARARGS|METH_CLASS,
     359             :                 NULL },
     360             :         { "as_sddl", (PyCFunction)py_descriptor_as_sddl, METH_VARARGS,
     361             :                 NULL },
     362             :         {0}
     363             : };
     364             : 
     365           0 : static PyObject *py_descriptor_richcmp(
     366             :         PyObject *py_self, PyObject *py_other, int op)
     367             : {
     368           0 :         struct security_descriptor *self = pytalloc_get_ptr(py_self);
     369           0 :         struct security_descriptor *other = pytalloc_get_ptr(py_other);
     370             :         bool eq;
     371             : 
     372           0 :         if (other == NULL) {
     373           0 :                 Py_INCREF(Py_NotImplemented);
     374           0 :                 return Py_NotImplemented;
     375             :         }
     376             : 
     377           0 :         eq = security_descriptor_equal(self, other);
     378             : 
     379           0 :         switch(op) {
     380           0 :         case Py_EQ:
     381           0 :                 if (eq) {
     382           0 :                         Py_RETURN_TRUE;
     383             :                 } else {
     384           0 :                         Py_RETURN_FALSE;
     385             :                 }
     386             :                 break;
     387           0 :         case Py_NE:
     388           0 :                 if (eq) {
     389           0 :                         Py_RETURN_FALSE;
     390             :                 } else {
     391           0 :                         Py_RETURN_TRUE;
     392             :                 }
     393             :                 break;
     394           0 :         default:
     395           0 :                 break;
     396             :         }
     397             : 
     398           0 :         Py_RETURN_NOTIMPLEMENTED;
     399             : }
     400             : 
     401        3543 : static void py_descriptor_patch(PyTypeObject *type)
     402             : {
     403        3543 :         type->tp_new = py_descriptor_new;
     404        3543 :         type->tp_richcompare = py_descriptor_richcmp;
     405        3543 :         PyType_AddMethods(type, py_descriptor_extra_methods);
     406        3543 : }
     407             : 
     408             : #define PY_DESCRIPTOR_PATCH py_descriptor_patch
     409             : 
     410           0 : static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
     411             : {
     412             :         PyObject *py_sid;
     413             :         struct dom_sid *sid;
     414           0 :         struct security_token *token = pytalloc_get_ptr(self);
     415           0 :         if (!PyArg_ParseTuple(args, "O", &py_sid))
     416           0 :                 return NULL;
     417             : 
     418           0 :         sid = pytalloc_get_ptr(py_sid);
     419             : 
     420           0 :         return PyBool_FromLong(security_token_is_sid(token, sid));
     421             : }
     422             : 
     423           0 : static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
     424             : {
     425             :         PyObject *py_sid;
     426             :         struct dom_sid *sid;
     427           0 :         struct security_token *token = pytalloc_get_ptr(self);
     428           0 :         if (!PyArg_ParseTuple(args, "O", &py_sid))
     429           0 :                 return NULL;
     430             : 
     431           0 :         sid = pytalloc_get_ptr(py_sid);
     432             : 
     433           0 :         return PyBool_FromLong(security_token_has_sid(token, sid));
     434             : }
     435             : 
     436           2 : static PyObject *py_token_is_anonymous(PyObject *self,
     437             :         PyObject *Py_UNUSED(ignored))
     438             : {
     439           2 :         struct security_token *token = pytalloc_get_ptr(self);
     440             :         
     441           2 :         return PyBool_FromLong(security_token_is_anonymous(token));
     442             : }
     443             : 
     444           2 : static PyObject *py_token_is_system(PyObject *self,
     445             :         PyObject *Py_UNUSED(ignored))
     446             : {
     447           2 :         struct security_token *token = pytalloc_get_ptr(self);
     448             :         
     449           2 :         return PyBool_FromLong(security_token_is_system(token));
     450             : }
     451             : 
     452           1 : static PyObject *py_token_has_builtin_administrators(PyObject *self,
     453             :         PyObject *Py_UNUSED(ignored))
     454             : {
     455           1 :         struct security_token *token = pytalloc_get_ptr(self);
     456             :         
     457           1 :         return PyBool_FromLong(security_token_has_builtin_administrators(token));
     458             : }
     459             : 
     460           0 : static PyObject *py_token_has_nt_authenticated_users(PyObject *self,
     461             :         PyObject *Py_UNUSED(ignored))
     462             : {
     463           0 :         struct security_token *token = pytalloc_get_ptr(self);
     464             :         
     465           0 :         return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
     466             : }
     467             : 
     468           0 : static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
     469             : {
     470             :         int priv;
     471           0 :         struct security_token *token = pytalloc_get_ptr(self);
     472             : 
     473           0 :         if (!PyArg_ParseTuple(args, "i", &priv))
     474           0 :                 return NULL;
     475             : 
     476           0 :         return PyBool_FromLong(security_token_has_privilege(token, priv));
     477             : }
     478             : 
     479           0 : static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
     480             : {
     481             :         int priv;
     482           0 :         struct security_token *token = pytalloc_get_ptr(self);
     483             : 
     484           0 :         if (!PyArg_ParseTuple(args, "i", &priv))
     485           0 :                 return NULL;
     486             : 
     487           0 :         security_token_set_privilege(token, priv);
     488           0 :         Py_RETURN_NONE;
     489             : }
     490             : 
     491           0 : static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
     492             : {
     493           0 :         return pytalloc_steal(self, security_token_initialise(NULL));
     494             : }       
     495             : 
     496             : static PyMethodDef py_token_extra_methods[] = {
     497             :         { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS,
     498             :                 "S.is_sid(sid) -> bool\n"
     499             :                 "Check whether this token is of the specified SID." },
     500             :         { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS,
     501             :                 NULL },
     502             :         { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS,
     503             :                 "S.is_anonymous() -> bool\n"
     504             :                 "Check whether this is an anonymous token." },
     505             :         { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS,
     506             :                 NULL },
     507             :         { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
     508             :                 NULL },
     509             :         { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
     510             :                 NULL },
     511             :         { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
     512             :                 NULL },
     513             :         { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
     514             :                 NULL },
     515             :         {0}
     516             : };
     517             : 
     518             : #define PY_TOKEN_PATCH py_token_patch
     519        3543 : static void py_token_patch(PyTypeObject *type)
     520             : {
     521        3543 :         type->tp_new = py_token_new;
     522        3543 :         PyType_AddMethods(type, py_token_extra_methods);
     523        3543 : }
     524             : 
     525           0 : static PyObject *py_privilege_name(PyObject *self, PyObject *args)
     526             : {
     527             :         int priv;
     528           0 :         const char *name = NULL;
     529           0 :         if (!PyArg_ParseTuple(args, "i", &priv)) {
     530           0 :                 return NULL;
     531             :         }
     532           0 :         name = sec_privilege_name(priv);
     533           0 :         if (name == NULL) {
     534           0 :                 PyErr_Format(PyExc_ValueError,
     535             :                              "Invalid privilege LUID: %d", priv);
     536           0 :                 return NULL;
     537             :         }
     538             : 
     539           0 :         return PyUnicode_FromString(name);
     540             : }
     541             : 
     542           0 : static PyObject *py_privilege_id(PyObject *self, PyObject *args)
     543             : {
     544             :         char *name;
     545             : 
     546           0 :         if (!PyArg_ParseTuple(args, "s", &name))
     547           0 :                 return NULL;
     548             : 
     549           0 :         return PyLong_FromLong(sec_privilege_id(name));
     550             : }
     551             : 
     552          99 : static PyObject *py_random_sid(PyObject *self,
     553             :         PyObject *Py_UNUSED(ignored))
     554             : {
     555             :         struct dom_sid *sid;
     556             :         PyObject *ret;
     557          99 :         char *str = talloc_asprintf(
     558             :                 NULL,
     559             :                 "S-1-5-21-%"PRIu32"-%"PRIu32"-%"PRIu32,
     560             :                 generate_random(),
     561             :                 generate_random(),
     562             :                 generate_random());
     563             : 
     564          99 :         sid = dom_sid_parse_talloc(NULL, str);
     565          99 :         talloc_free(str);
     566          99 :         ret = pytalloc_steal(&dom_sid_Type, sid);
     567          99 :         return ret;
     568             : }
     569             : 
     570             : static PyMethodDef py_mod_security_extra_methods[] = {
     571             :         { "random_sid", (PyCFunction)py_random_sid, METH_NOARGS, NULL },
     572             :         { "privilege_id", (PyCFunction)py_privilege_id, METH_VARARGS, NULL },
     573             :         { "privilege_name", (PyCFunction)py_privilege_name, METH_VARARGS, NULL },
     574             :         {0}
     575             : };
     576             : 
     577        3543 : static void py_mod_security_patch(PyObject *m)
     578             : {
     579             :         int i;
     580       14172 :         for (i = 0; py_mod_security_extra_methods[i].ml_name; i++) {
     581       10629 :                 PyObject *descr = PyCFunction_New(&py_mod_security_extra_methods[i], NULL);
     582       10629 :                 PyModule_AddObject(m, py_mod_security_extra_methods[i].ml_name,
     583             :                                    descr);
     584             :         }
     585        3543 : }
     586             : 
     587             : #define PY_MOD_SECURITY_PATCH py_mod_security_patch
     588             : 
     589       64941 : static PyObject *py_security_ace_equal(PyObject *py_self, PyObject *py_other, int op)
     590             : {
     591       64941 :         struct security_ace *self = pytalloc_get_ptr(py_self);
     592       64941 :         struct security_ace *other = NULL;
     593             :         bool eq;
     594             : 
     595       64941 :         if (!PyObject_TypeCheck(py_other, &security_ace_Type)) {
     596           0 :                 eq = false;
     597             :         } else {
     598       64941 :                 other = pytalloc_get_ptr(py_other);
     599       64941 :                 eq = security_ace_equal(self, other);
     600             :         }
     601             : 
     602       64941 :         switch(op) {
     603       64941 :         case Py_EQ:
     604       64941 :                 if (eq) {
     605         244 :                         Py_RETURN_TRUE;
     606             :                 } else {
     607       64697 :                         Py_RETURN_FALSE;
     608             :                 }
     609             :                 break;
     610           0 :         case Py_NE:
     611           0 :                 if (eq) {
     612           0 :                         Py_RETURN_FALSE;
     613             :                 } else {
     614           0 :                         Py_RETURN_TRUE;
     615             :                 }
     616             :                 break;
     617           0 :         default:
     618           0 :                 break;
     619             :         }
     620             : 
     621           0 :         Py_RETURN_NOTIMPLEMENTED;
     622             : }
     623             : 
     624           0 : static PyObject *py_security_ace_as_sddl(PyObject *self, PyObject *args)
     625             : {
     626           0 :         struct security_ace *ace = pytalloc_get_ptr(self);
     627           0 :         PyObject *py_sid = Py_None;
     628           0 :         struct dom_sid *sid = NULL;
     629           0 :         char *text = NULL;
     630           0 :         PyObject *ret = Py_None;
     631             : 
     632           0 :         if (!PyArg_ParseTuple(args, "O!", &dom_sid_Type, &py_sid))
     633           0 :                 return NULL;
     634             : 
     635           0 :         if (!PyObject_TypeCheck(py_sid, &dom_sid_Type)) {
     636           0 :                 PyErr_SetString(PyExc_TypeError,
     637             :                                 "expected security.dom_sid "
     638             :                                 "for second argument to .sddl_encode_ace");
     639           0 :                 return NULL;
     640             :         }
     641             : 
     642           0 :         sid = pytalloc_get_ptr(py_sid);
     643             : 
     644           0 :         text = sddl_encode_ace(NULL, ace, sid);
     645           0 :         if (text == NULL) {
     646           0 :                 return NULL;
     647             :         }
     648           0 :         ret = PyUnicode_FromString(text);
     649           0 :         talloc_free(text);
     650             : 
     651           0 :         return ret;
     652             : }
     653             : 
     654             : static PyMethodDef py_security_ace_extra_methods[] = {
     655             :         { "as_sddl", (PyCFunction)py_security_ace_as_sddl, METH_VARARGS, NULL },
     656             :         {0}
     657             : };
     658             : 
     659             : #define PY_ACE_PATCH py_security_ace_patch
     660             : 
     661        3543 : static void py_security_ace_patch(PyTypeObject *type)
     662             : {
     663        3543 :         type->tp_richcompare = py_security_ace_equal;
     664        3543 :         PyType_AddMethods(type, py_security_ace_extra_methods);
     665        3543 : }

Generated by: LCOV version 1.14