1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

python: samba.dcerpc: Port RPC related stuff to Python 3

Port RPC related stuff like samba.dcerpc.misc and samba.dcerpc
Python modules and pyrpc_util to Python 3 compatible form.

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Pair-programmed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Lumir Balhar 2017-02-15 09:19:33 +01:00 committed by Andrew Bartlett
parent 2de5fa45e0
commit 3b1c0ba98a
5 changed files with 94 additions and 44 deletions

View File

@ -18,8 +18,34 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <Python.h> #include <Python.h>
#include "python/py3compat.h"
#include "librpc/gen_ndr/misc.h" #include "librpc/gen_ndr/misc.h"
#if PY_MAJOR_VERSION >= 3
static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
{
int ret;
struct GUID *self = pytalloc_get_ptr(py_self), *other;
other = pytalloc_get_ptr(py_other);
if (other == NULL) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
ret = GUID_compare(self, other);
switch (op) {
case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
case Py_LT: if (ret < 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
case Py_GT: if (ret > 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
}
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
#else
static int py_GUID_cmp(PyObject *py_self, PyObject *py_other) static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
{ {
int ret; int ret;
@ -37,12 +63,13 @@ static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
return 0; return 0;
} }
} }
#endif
static PyObject *py_GUID_str(PyObject *py_self) static PyObject *py_GUID_str(PyObject *py_self)
{ {
struct GUID *self = pytalloc_get_ptr(py_self); struct GUID *self = pytalloc_get_ptr(py_self);
char *str = GUID_string(NULL, self); char *str = GUID_string(NULL, self);
PyObject *ret = PyString_FromString(str); PyObject *ret = PyStr_FromString(str);
talloc_free(str); talloc_free(str);
return ret; return ret;
} }
@ -51,7 +78,7 @@ static PyObject *py_GUID_repr(PyObject *py_self)
{ {
struct GUID *self = pytalloc_get_ptr(py_self); struct GUID *self = pytalloc_get_ptr(py_self);
char *str = GUID_string(NULL, self); char *str = GUID_string(NULL, self);
PyObject *ret = PyString_FromFormat("GUID('%s')", str); PyObject *ret = PyStr_FromFormat("GUID('%s')", str);
talloc_free(str); talloc_free(str);
return ret; return ret;
} }
@ -68,13 +95,14 @@ static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
if (str != NULL) { if (str != NULL) {
DATA_BLOB guid_val; DATA_BLOB guid_val;
Py_ssize_t _size;
if (!PyString_Check(str)) { if (!PyStr_Check(str)) {
PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()"); PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()");
return -1; return -1;
} }
guid_val.data = (uint8_t *)PyString_AsString(str); guid_val.data = (uint8_t *)PyStr_AsUTF8AndSize(str, &_size);
guid_val.length = PyString_Size(str); guid_val.length = _size;
status = GUID_from_data_blob(&guid_val, guid); status = GUID_from_data_blob(&guid_val, guid);
if (!NT_STATUS_IS_OK(status)) { if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status); PyErr_SetNTSTATUS(status);
@ -90,7 +118,11 @@ static void py_GUID_patch(PyTypeObject *type)
type->tp_init = py_GUID_init; type->tp_init = py_GUID_init;
type->tp_str = py_GUID_str; type->tp_str = py_GUID_str;
type->tp_repr = py_GUID_repr; type->tp_repr = py_GUID_repr;
#if PY_MAJOR_VERSION >= 3
type->tp_richcompare = py_GUID_richcmp;
#else
type->tp_compare = py_GUID_cmp; type->tp_compare = py_GUID_cmp;
#endif
} }
#define PY_GUID_PATCH py_GUID_patch #define PY_GUID_PATCH py_GUID_patch
@ -120,7 +152,7 @@ static PyObject *py_policy_handle_repr(PyObject *py_self)
{ {
struct policy_handle *self = pytalloc_get_ptr(py_self); struct policy_handle *self = pytalloc_get_ptr(py_self);
char *uuid_str = GUID_string(NULL, &self->uuid); char *uuid_str = GUID_string(NULL, &self->uuid);
PyObject *ret = PyString_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str); PyObject *ret = PyStr_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
talloc_free(uuid_str); talloc_free(uuid_str);
return ret; return ret;
} }
@ -129,7 +161,7 @@ static PyObject *py_policy_handle_str(PyObject *py_self)
{ {
struct policy_handle *self = pytalloc_get_ptr(py_self); struct policy_handle *self = pytalloc_get_ptr(py_self);
char *uuid_str = GUID_string(NULL, &self->uuid); char *uuid_str = GUID_string(NULL, &self->uuid);
PyObject *ret = PyString_FromFormat("%d, %s", self->handle_type, uuid_str); PyObject *ret = PyStr_FromFormat("%d, %s", self->handle_type, uuid_str);
talloc_free(uuid_str); talloc_free(uuid_str);
return ret; return ret;
} }

View File

@ -15,4 +15,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from base import * from samba.dcerpc.base import *

View File

@ -18,6 +18,7 @@
*/ */
#include <Python.h> #include <Python.h>
#include "python/py3compat.h"
#include "includes.h" #include "includes.h"
#include <structmember.h> #include <structmember.h>
#include "librpc/rpc/pyrpc.h" #include "librpc/rpc/pyrpc.h"
@ -37,7 +38,7 @@ static PyTypeObject *ndr_syntax_id_Type;
static bool PyString_AsGUID(PyObject *object, struct GUID *uuid) static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
{ {
NTSTATUS status; NTSTATUS status;
status = GUID_from_string(PyString_AsString(object), uuid); status = GUID_from_string(PyStr_AsString(object), uuid);
if (NT_STATUS_IS_ERR(status)) { if (NT_STATUS_IS_ERR(status)) {
PyErr_SetNTSTATUS(status); PyErr_SetNTSTATUS(status);
return false; return false;
@ -49,7 +50,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy
{ {
ZERO_STRUCTP(syntax_id); ZERO_STRUCTP(syntax_id);
if (PyString_Check(object)) { if (PyStr_Check(object)) {
return PyString_AsGUID(object, &syntax_id->uuid); return PyString_AsGUID(object, &syntax_id->uuid);
} else if (PyTuple_Check(object)) { } else if (PyTuple_Check(object)) {
if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
@ -57,7 +58,7 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy
return false; return false;
} }
if (!PyString_Check(PyTuple_GetItem(object, 0))) { if (!PyStr_Check(PyTuple_GetItem(object, 0))) {
PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
return false; return false;
} }
@ -87,7 +88,7 @@ static PyObject *py_iface_server_name(PyObject *obj, void *closure)
if (server_name == NULL) if (server_name == NULL)
Py_RETURN_NONE; Py_RETURN_NONE;
return PyString_FromString(server_name); return PyStr_FromString(server_name);
} }
static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id) static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
@ -128,7 +129,7 @@ static PyObject *py_iface_session_key(PyObject *obj, void *closure)
NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key); NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key);
PyErr_NTSTATUS_IS_ERR_RAISE(status); PyErr_NTSTATUS_IS_ERR_RAISE(status);
return PyString_FromStringAndSize((const char *)session_key.data, session_key.length); return PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length);
} }
static PyObject *py_iface_user_session_key(PyObject *obj, void *closure) static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
@ -166,7 +167,7 @@ static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
return NULL; return NULL;
} }
session_key_obj = PyString_FromStringAndSize((const char *)session_key.data, session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data,
session_key.length); session_key.length);
talloc_free(mem_ctx); talloc_free(mem_ctx);
return session_key_obj; return session_key_obj;
@ -260,7 +261,7 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar
return NULL; return NULL;
} }
ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length); ret = PyBytes_FromStringAndSize((char *)data_out.data, data_out.length);
talloc_free(mem_ctx); talloc_free(mem_ctx);
return ret; return ret;
@ -337,7 +338,7 @@ static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObje
} }
static PyTypeObject dcerpc_InterfaceType = { static PyTypeObject dcerpc_InterfaceType = {
PyObject_HEAD_INIT(NULL) 0, PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "dcerpc.ClientConnection", .tp_name = "dcerpc.ClientConnection",
.tp_basicsize = sizeof(dcerpc_InterfaceObject), .tp_basicsize = sizeof(dcerpc_InterfaceObject),
.tp_dealloc = dcerpc_interface_dealloc, .tp_dealloc = dcerpc_interface_dealloc,
@ -359,7 +360,7 @@ static PyObject *py_transfer_syntax_ndr_new(PyTypeObject *type, PyObject *args,
} }
static PyTypeObject py_transfer_syntax_ndr_SyntaxType = { static PyTypeObject py_transfer_syntax_ndr_SyntaxType = {
PyObject_HEAD_INIT(NULL) 0, PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "base.transfer_syntax_ndr", .tp_name = "base.transfer_syntax_ndr",
.tp_doc = "transfer_syntax_ndr()\n", .tp_doc = "transfer_syntax_ndr()\n",
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@ -372,7 +373,7 @@ static PyObject *py_transfer_syntax_ndr64_new(PyTypeObject *type, PyObject *args
} }
static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = { static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = {
PyObject_HEAD_INIT(NULL) 0, PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "base.transfer_syntax_ndr64", .tp_name = "base.transfer_syntax_ndr64",
.tp_doc = "transfer_syntax_ndr64()\n", .tp_doc = "transfer_syntax_ndr64()\n",
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@ -410,25 +411,32 @@ static PyObject *py_bind_time_features_syntax_new(PyTypeObject *type, PyObject *
} }
static PyTypeObject py_bind_time_features_syntax_SyntaxType = { static PyTypeObject py_bind_time_features_syntax_SyntaxType = {
PyObject_HEAD_INIT(NULL) 0, PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "base.bind_time_features_syntax", .tp_name = "base.bind_time_features_syntax",
.tp_doc = "bind_time_features_syntax(features)\n", .tp_doc = "bind_time_features_syntax(features)\n",
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = py_bind_time_features_syntax_new, .tp_new = py_bind_time_features_syntax_new,
}; };
void initbase(void) static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
.m_name = "base",
.m_doc = "DCE/RPC protocol implementation",
.m_size = -1,
};
MODULE_INIT_FUNC(base)
{ {
PyObject *m; PyObject *m;
PyObject *dep_samba_dcerpc_misc; PyObject *dep_samba_dcerpc_misc;
dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc"); dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
if (dep_samba_dcerpc_misc == NULL) if (dep_samba_dcerpc_misc == NULL)
return; return NULL;
ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id"); ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
if (ndr_syntax_id_Type == NULL) if (ndr_syntax_id_Type == NULL)
return; return NULL;
py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type; py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size(); py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
@ -438,18 +446,18 @@ void initbase(void)
py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size(); py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
if (PyType_Ready(&dcerpc_InterfaceType) < 0) if (PyType_Ready(&dcerpc_InterfaceType) < 0)
return; return NULL;
if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0) if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0)
return; return NULL;
if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0) if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0)
return; return NULL;
if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0) if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0)
return; return NULL;
m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation"); m = PyModule_Create(&moduledef);
if (m == NULL) if (m == NULL)
return; return NULL;
Py_INCREF((PyObject *)&dcerpc_InterfaceType); Py_INCREF((PyObject *)&dcerpc_InterfaceType);
PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType); PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
@ -460,4 +468,5 @@ void initbase(void)
PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType); PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType); Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType); PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
return m;
} }

View File

@ -21,6 +21,7 @@
*/ */
#include <Python.h> #include <Python.h>
#include "python/py3compat.h"
#include "includes.h" #include "includes.h"
#include "librpc/rpc/pyrpc_util.h" #include "librpc/rpc/pyrpc_util.h"
#include "librpc/rpc/dcerpc.h" #include "librpc/rpc/dcerpc.h"
@ -388,7 +389,7 @@ PyObject *PyString_FromStringOrNULL(const char *str)
if (str == NULL) { if (str == NULL) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
return PyString_FromString(str); return PyStr_FromString(str);
} }
PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level, PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,

View File

@ -174,18 +174,23 @@ bld.SAMBA_LIBRARY('dcerpc',
vnum='0.0.1' vnum='0.0.1'
) )
bld.SAMBA_SUBSYSTEM('pyrpc_util', for env in bld.gen_python_environments():
source='rpc/pyrpc_util.c', pyrpc_util = bld.pyembed_libname('pyrpc_util')
public_deps='pytalloc-util pyparam_util dcerpc MESSAGING', pytalloc_util = bld.pyembed_libname('pytalloc-util')
pyext=True, pyparam_util = bld.pyembed_libname('pyparam_util')
)
bld.SAMBA_SUBSYSTEM(pyrpc_util,
source='rpc/pyrpc_util.c',
public_deps='%s %s dcerpc MESSAGING' % (pytalloc_util, pyparam_util),
pyext=True,
)
bld.SAMBA_PYTHON('python_dcerpc',
source='rpc/pyrpc.c',
public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG %s gensec' % (pyrpc_util),
realname='samba/dcerpc/base.so'
)
bld.SAMBA_PYTHON('python_dcerpc',
source='rpc/pyrpc.c',
public_deps='LIBCLI_SMB samba-util samba-hostconfig dcerpc-samr RPC_NDR_LSA DYNCONFIG pyrpc_util gensec',
realname='samba/dcerpc/base.so'
)
bld.SAMBA_PYTHON('python_srvsvc', bld.SAMBA_PYTHON('python_srvsvc',
source='../../librpc/gen_ndr/py_srvsvc.c', source='../../librpc/gen_ndr/py_srvsvc.c',
@ -224,11 +229,14 @@ bld.SAMBA_PYTHON('python_winreg',
) )
bld.SAMBA_PYTHON('python_dcerpc_misc', for env in bld.gen_python_environments():
source='../../librpc/gen_ndr/py_misc.c', pytalloc_util = bld.pyembed_libname('pytalloc-util')
deps='pytalloc-util pyrpc_util ndr-krb5pac', pyrpc_util = bld.pyembed_libname('pyrpc_util')
realname='samba/dcerpc/misc.so' bld.SAMBA_PYTHON('python_dcerpc_misc',
) source='../../librpc/gen_ndr/py_misc.c',
deps='%s %s ndr-krb5pac' % (pytalloc_util, pyrpc_util),
realname='samba/dcerpc/misc.so'
)
bld.SAMBA_PYTHON('python_initshutdown', bld.SAMBA_PYTHON('python_initshutdown',