*/
#include <Python.h>
+#include "python/py3compat.h"
#include "includes.h"
#include "librpc/rpc/pyrpc_util.h"
#include "librpc/rpc/dcerpc.h"
return NT_STATUS_INVALID_PIPE_STATE;
}
+ /*
+ * Note: this allows nested event loops to happen,
+ * but as there's no top level event loop it's not that critical.
+ */
+ dcerpc_binding_handle_set_sync_ev(*binding_handle, event_ctx);
+
return NT_STATUS_OK;
}
const char *binding_string;
PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
NTSTATUS status;
+ unsigned int timeout = (unsigned int)-1;
const char *kwnames[] = {
- "binding", "lp_ctx", "credentials", "basis_connection", NULL
+ "binding", "lp_ctx", "credentials", "timeout", "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)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOIO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &timeout, &py_basis)) {
return NULL;
}
}
ret = PyObject_New(dcerpc_InterfaceObject, type);
+ if (ret == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
ret->pipe = NULL;
ret->binding_handle = NULL;
ret->mem_ctx = talloc_new(NULL);
if (event_ctx == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
if (lp_ctx == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
} else if (py_basis != Py_None) {
py_base = PyImport_ImportModule("samba.dcerpc.base");
if (py_base == NULL) {
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
if (ClientConnection_Type == NULL) {
PyErr_SetNone(PyExc_TypeError);
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
+ Py_DECREF(py_base);
return NULL;
}
if (!PyObject_TypeCheck(py_basis, ClientConnection_Type)) {
PyErr_SetString(PyExc_TypeError, "basis_connection must be a DCE/RPC connection");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
+ Py_DECREF(py_base);
+ Py_DECREF(ClientConnection_Type);
return NULL;
}
if (base_pipe == NULL) {
PyErr_NoMemory();
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
+ Py_DECREF(py_base);
+ Py_DECREF(ClientConnection_Type);
return NULL;
}
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
+ Py_DECREF(py_base);
+ Py_DECREF(ClientConnection_Type);
return NULL;
}
ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
+ Py_XDECREF(ClientConnection_Type);
+ Py_XDECREF(py_base);
} else {
struct tevent_context *event_ctx;
struct loadparm_context *lp_ctx;
if (event_ctx == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
if (lp_ctx == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
if (credentials == NULL) {
PyErr_SetString(PyExc_TypeError, "Expected credentials");
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
status = dcerpc_pipe_connect(ret->mem_ctx, &ret->pipe, binding_string,
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
TALLOC_FREE(ret->mem_ctx);
+ Py_DECREF(ret);
return NULL;
}
ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
ret->binding_handle = ret->pipe->binding_handle;
}
+
+ /* reset timeout for the handle */
+ if ((timeout != ((unsigned int)-1)) && (ret->binding_handle != NULL)) {
+ dcerpc_binding_handle_set_timeout(ret->binding_handle, timeout);
+ }
+
return (PyObject *)ret;
}
PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
(PyObject *)ret);
+ Py_CLEAR(ret);
}
return true;
return NULL;
}
- obj = (struct ndr_syntax_id *)pytalloc_get_ptr(ret);
+ obj = pytalloc_get_type(ret, struct ndr_syntax_id);
*obj = *syntax;
return ret;
r_ctx is the context that is a parent of r. It will be referenced by
the resulting python object
+
+ This MUST only be used by objects that are based on pytalloc_Object
+ otherwise the pytalloc_reference_ex() will fail.
*/
PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
TALLOC_CTX *r_ctx, void *r)
{
PyTypeObject *py_type;
PyObject *module;
+ PyObject *result = NULL;
if (r == NULL) {
Py_RETURN_NONE;
py_type = (PyTypeObject *)PyObject_GetAttrString(module, type_name);
if (py_type == NULL) {
+ Py_DECREF(module);
return NULL;
}
- return pytalloc_reference_ex(py_type, r_ctx, r);
+ result = pytalloc_reference_ex(py_type, r_ctx, r);
+ Py_CLEAR(module);
+ Py_CLEAR(py_type);
+ return result;
}
PyObject *PyString_FromStringOrNULL(const char *str)
if (str == NULL) {
Py_RETURN_NONE;
}
- return PyString_FromString(str);
+ return PyStr_FromString(str);
+}
+
+const char *pyrpc_PyStr_AsString(TALLOC_CTX *mem_ctx, PyObject *value)
+{
+ const char *test_str = NULL;
+ const char *talloc_str = NULL;
+ PyObject *unicode = NULL;
+
+ if (PyUnicode_Check(value)) {
+ unicode = PyUnicode_AsEncodedString(value, "utf-8", "ignore");
+ if (unicode == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ test_str = PyBytes_AS_STRING(unicode);
+ } else if (PyBytes_Check(value)) {
+ test_str = PyBytes_AS_STRING(value);
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "Expected string or unicode object, got %s",
+ Py_TYPE(value)->tp_name);
+ return NULL;
+ }
+
+ talloc_str = talloc_strdup(mem_ctx, test_str);
+ if (unicode != NULL) {
+ Py_DECREF(unicode);
+ }
+ if (talloc_str == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ return talloc_str;
+}
+
+PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
+ const void *in, const char *typename)
+{
+ PyObject *mem_ctx_obj = NULL;
+ PyObject *in_obj = NULL;
+ PyObject *ret = NULL;
+
+ mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
+ if (mem_ctx_obj == NULL) {
+ return NULL;
+ }
+
+ in_obj = pytalloc_GenericObject_reference_ex(mem_ctx, discard_const(in));
+ if (in_obj == NULL) {
+ Py_XDECREF(mem_ctx_obj);
+ return NULL;
+ }
+
+ ret = PyObject_CallMethod((PyObject *)type,
+ discard_const_p(char, "__import__"),
+ discard_const_p(char, "OiO"),
+ mem_ctx_obj, level, in_obj);
+ Py_XDECREF(mem_ctx_obj);
+ Py_XDECREF(in_obj);
+ if (ret == NULL) {
+ return NULL;
+ }
+
+ return ret;
+}
+
+void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
+ PyObject *in, const char *typename)
+{
+ PyObject *mem_ctx_obj = NULL;
+ PyObject *ret_obj = NULL;
+ void *ret = NULL;
+
+ mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
+ if (mem_ctx_obj == NULL) {
+ return NULL;
+ }
+
+ ret_obj = PyObject_CallMethod((PyObject *)type,
+ discard_const_p(char, "__export__"),
+ discard_const_p(char, "OiO"),
+ mem_ctx_obj, level, in);
+ Py_XDECREF(mem_ctx_obj);
+ if (ret_obj == NULL) {
+ return NULL;
+ }
+
+ ret = _pytalloc_get_type(ret_obj, typename);
+ Py_XDECREF(ret_obj);
+ return ret;
+}
+
+PyObject *py_dcerpc_ndr_pointer_deref(PyTypeObject *type, PyObject *obj)
+{
+ if (!PyObject_TypeCheck(obj, type)) {
+ PyErr_Format(PyExc_TypeError,
+ "Expected type '%s' but got type '%s'",
+ (type)->tp_name, Py_TYPE(obj)->tp_name);
+ return NULL;
+ }
+
+ return PyObject_GetAttrString(obj, discard_const_p(char, "value"));
+}
+
+PyObject *py_dcerpc_ndr_pointer_wrap(PyTypeObject *type, PyObject *obj)
+{
+ PyObject *args = NULL;
+ PyObject *ret_obj = NULL;
+
+ args = PyTuple_New(1);
+ if (args == NULL) {
+ return NULL;
+ }
+ Py_XINCREF(obj);
+ PyTuple_SetItem(args, 0, obj);
+
+ ret_obj = PyObject_Call((PyObject *)type, args, NULL);
+ Py_XDECREF(args);
+ return ret_obj;
}