summaryrefslogtreecommitdiff
path: root/source4/librpc/rpc/pyrpc.c
diff options
context:
space:
mode:
authorbubulle <bubulle@alioth.debian.org>2011-11-12 13:00:54 +0000
committerbubulle <bubulle@alioth.debian.org>2011-11-12 13:00:54 +0000
commit6fba685eec3a1165ec0b82d72d3ae71e946a1404 (patch)
treef3c0543c8f9df4a22ed62e3bd99d9d7bc1054c14 /source4/librpc/rpc/pyrpc.c
parent77a7925c0509068d5cd2affd94a3996d0a86035a (diff)
downloadsamba-6fba685eec3a1165ec0b82d72d3ae71e946a1404.tar.gz
Merge upstream 3.6.1 source
git-svn-id: svn://svn.debian.org/svn/pkg-samba/trunk/samba@3972 fc4039ab-9d04-0410-8cac-899223bdd6b0
Diffstat (limited to 'source4/librpc/rpc/pyrpc.c')
-rw-r--r--source4/librpc/rpc/pyrpc.c241
1 files changed, 57 insertions, 184 deletions
diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c
index e50a077625..2d6e1f6c61 100644
--- a/source4/librpc/rpc/pyrpc.c
+++ b/source4/librpc/rpc/pyrpc.c
@@ -2,107 +2,34 @@
Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
-
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "includes.h"
#include <Python.h>
+#include "includes.h"
#include <structmember.h>
#include "librpc/rpc/pyrpc.h"
-#include "librpc/rpc/dcerpc.h"
#include "lib/events/events.h"
#include "param/pyparam.h"
+#include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/pyrpc_util.h"
#include "auth/credentials/pycredentials.h"
-#ifndef Py_RETURN_NONE
-#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
-#endif
-
-static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface,
- const struct PyNdrRpcMethodDef *md,
- PyObject *args, PyObject *kwargs)
-{
- TALLOC_CTX *mem_ctx;
- NTSTATUS status;
- void *r;
- PyObject *result = Py_None;
-
- if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
- PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
- return NULL;
- }
-
- mem_ctx = talloc_new(NULL);
- if (mem_ctx == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
- if (r == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- if (!md->pack_in_data(args, kwargs, r)) {
- talloc_free(mem_ctx);
- return NULL;
- }
-
- status = md->call(iface->pipe, mem_ctx, r);
- if (NT_STATUS_IS_ERR(status)) {
- PyErr_SetDCERPCStatus(iface->pipe, status);
- talloc_free(mem_ctx);
- return NULL;
- }
-
- result = md->unpack_out_data(r);
+void initbase(void);
- talloc_free(mem_ctx);
- return result;
-}
-
-static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
-{
- dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
- const struct PyNdrRpcMethodDef *md = (const struct PyNdrRpcMethodDef *)wrapped;
-
- return py_dcerpc_run_function(iface, md, args, kwargs);
-}
-
-
-bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
-{
- int i;
- for (i = 0; mds[i].name; i++) {
- PyObject *ret;
- struct wrapperbase *wb = (struct wrapperbase *)calloc(sizeof(struct wrapperbase), 1);
-
- wb->name = discard_const_p(char, mds[i].name);
- wb->flags = PyWrapperFlag_KEYWORDS;
- wb->wrapper = (wrapperfunc)py_dcerpc_call_wrapper;
- wb->doc = discard_const_p(char, mds[i].doc);
-
- ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
-
- PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
- (PyObject *)ret);
- }
-
- return true;
-}
+staticforward PyTypeObject dcerpc_InterfaceType;
static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
{
@@ -146,13 +73,13 @@ static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *sy
PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
return false;
-}
+}
static PyObject *py_iface_server_name(PyObject *obj, void *closure)
{
const char *server_name;
dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
-
+
server_name = dcerpc_server_name(iface->pipe);
if (server_name == NULL)
Py_RETURN_NONE;
@@ -207,18 +134,6 @@ static PyMemberDef dcerpc_interface_members[] = {
{ NULL }
};
-void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
-{
- if (p != NULL && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
- const char *errstr = dcerpc_errstr(NULL, p->last_fault_code);
- PyErr_SetObject(PyExc_RuntimeError,
- Py_BuildValue("(i,s)", p->last_fault_code,
- errstr));
- } else {
- PyErr_SetNTSTATUS(status);
- }
-}
-
static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
{
dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
@@ -231,10 +146,12 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar
PyObject *object = NULL;
struct GUID object_guid;
TALLOC_CTX *mem_ctx = talloc_new(NULL);
+ uint32_t out_flags = 0;
const char *kwnames[] = { "opnum", "data", "object", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request",
discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
+ talloc_free(mem_ctx);
return NULL;
}
@@ -244,13 +161,21 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar
ZERO_STRUCT(data_out);
if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
+ talloc_free(mem_ctx);
return NULL;
}
- status = dcerpc_request(iface->pipe, object?&object_guid:NULL,
- opnum, mem_ctx, &data_in, &data_out);
-
- if (NT_STATUS_IS_ERR(status)) {
+ status = dcerpc_binding_handle_raw_call(iface->binding_handle,
+ object?&object_guid:NULL,
+ opnum,
+ 0, /* in_flags */
+ data_in.data,
+ data_in.length,
+ mem_ctx,
+ &data_out.data,
+ &data_out.length,
+ &out_flags);
+ if (!NT_STATUS_IS_OK(status)) {
PyErr_SetDCERPCStatus(iface->pipe, status);
talloc_free(mem_ctx);
return NULL;
@@ -290,7 +215,7 @@ static PyObject *py_iface_alter_context(PyObject *self, PyObject *args, PyObject
status = dcerpc_alter_context(iface->pipe, iface->pipe, &abstract_syntax,
&transfer_syntax);
- if (NT_STATUS_IS_ERR(status)) {
+ if (!NT_STATUS_IS_OK(status)) {
PyErr_SetDCERPCStatus(iface->pipe, status);
return NULL;
}
@@ -298,93 +223,27 @@ static PyObject *py_iface_alter_context(PyObject *self, PyObject *args, PyObject
Py_RETURN_NONE;
}
-PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs, const struct ndr_interface_table *table)
-{
- dcerpc_InterfaceObject *ret;
- const char *binding_string;
- struct cli_credentials *credentials;
- struct loadparm_context *lp_ctx = NULL;
- PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
- TALLOC_CTX *mem_ctx = NULL;
- struct tevent_context *event_ctx;
- NTSTATUS status;
-
- const char *kwnames[] = {
- "binding", "lp_ctx", "credentials", "basis_connection", NULL
- };
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {
- return NULL;
- }
-
- lp_ctx = lp_from_py_object(py_lp_ctx);
- if (lp_ctx == NULL) {
- PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
- return NULL;
- }
-
- status = dcerpc_init(lp_ctx);
- if (!NT_STATUS_IS_OK(status)) {
- PyErr_SetNTSTATUS(status);
- return NULL;
- }
- credentials = cli_credentials_from_py_object(py_credentials);
- if (credentials == NULL) {
- PyErr_SetString(PyExc_TypeError, "Expected credentials");
- return NULL;
- }
- ret = PyObject_New(dcerpc_InterfaceObject, type);
-
- event_ctx = event_context_init(mem_ctx);
-
- if (py_basis != Py_None) {
- struct dcerpc_pipe *base_pipe;
-
- if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {
- PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection");
- talloc_free(mem_ctx);
- return NULL;
- }
-
- base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;
-
- status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
- } else {
- status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string,
- table, credentials, event_ctx, lp_ctx);
- }
- if (NT_STATUS_IS_ERR(status)) {
- PyErr_SetNTSTATUS(status);
- talloc_free(mem_ctx);
- return NULL;
- }
-
- ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
- return (PyObject *)ret;
-}
-
static PyMethodDef dcerpc_interface_methods[] = {
{ "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
{ "alter_context", (PyCFunction)py_iface_alter_context, METH_VARARGS|METH_KEYWORDS, "S.alter_context(syntax)\nChange to a different interface" },
{ NULL, NULL, 0, NULL },
};
-
static void dcerpc_interface_dealloc(PyObject* self)
{
dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
- talloc_free(interface->pipe);
- PyObject_Del(self);
+ talloc_free(interface->mem_ctx);
+ self->ob_type->tp_free(self);
}
-static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
+static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
dcerpc_InterfaceObject *ret;
const char *binding_string;
struct cli_credentials *credentials;
struct loadparm_context *lp_ctx = NULL;
PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
- TALLOC_CTX *mem_ctx = NULL;
+ TALLOC_CTX *mem_ctx;
struct tevent_context *event_ctx;
NTSTATUS status;
@@ -398,37 +257,49 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje
return NULL;
}
- lp_ctx = lp_from_py_object(py_lp_ctx);
+ mem_ctx = talloc_new(NULL);
+ if (mem_ctx == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
if (lp_ctx == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
+ talloc_free(mem_ctx);
return NULL;
}
credentials = cli_credentials_from_py_object(py_credentials);
if (credentials == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected credentials");
+ talloc_free(mem_ctx);
return NULL;
}
- ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
+ ret = PyObject_New(dcerpc_InterfaceObject, type);
+ ret->mem_ctx = mem_ctx;
- event_ctx = s4_event_context_init(mem_ctx);
+ event_ctx = s4_event_context_init(ret->mem_ctx);
- /* Create a dummy interface table struct. TODO: In the future, we should rather just allow
- * connecting without requiring an interface table.
+ /* Create a dummy interface table struct. TODO: In the future, we should
+ * rather just allow connecting without requiring an interface table.
*/
- table = talloc_zero(mem_ctx, struct ndr_interface_table);
+ table = talloc_zero(ret->mem_ctx, struct ndr_interface_table);
if (table == NULL) {
PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
+ talloc_free(mem_ctx);
return NULL;
}
if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
+ talloc_free(mem_ctx);
return NULL;
}
ret->pipe = NULL;
+ ret->binding_handle = NULL;
if (py_basis != Py_None) {
struct dcerpc_pipe *base_pipe;
@@ -439,26 +310,28 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje
return NULL;
}
- base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;
+ base_pipe = talloc_reference(ret->mem_ctx,
+ ((dcerpc_InterfaceObject *)py_basis)->pipe);
+
+ status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
- status = dcerpc_secondary_context(base_pipe, &ret->pipe,
- table);
- ret->pipe = talloc_steal(NULL, ret->pipe);
+ ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
} else {
- status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string,
+ status = dcerpc_pipe_connect(ret->mem_ctx, &ret->pipe, binding_string,
table, credentials, event_ctx, lp_ctx);
}
- if (NT_STATUS_IS_ERR(status)) {
+ if (!NT_STATUS_IS_OK(status)) {
PyErr_SetDCERPCStatus(ret->pipe, status);
- talloc_free(mem_ctx);
+ talloc_free(ret->mem_ctx);
return NULL;
}
ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
+ ret->binding_handle = ret->pipe->binding_handle;
return (PyObject *)ret;
}
-PyTypeObject dcerpc_InterfaceType = {
+static PyTypeObject dcerpc_InterfaceType = {
PyObject_HEAD_INIT(NULL) 0,
.tp_name = "dcerpc.ClientConnection",
.tp_basicsize = sizeof(dcerpc_InterfaceObject),