diff options
28 files changed, 1000 insertions, 2471 deletions
diff --git a/source4/lib/messaging/config.mk b/source4/lib/messaging/config.mk index eaf7e3581e..e92f78c8e3 100644 --- a/source4/lib/messaging/config.mk +++ b/source4/lib/messaging/config.mk @@ -1,6 +1,3 @@ - -################################################ -# Start SUBSYSTEM MESSAGING [SUBSYSTEM::MESSAGING] PUBLIC_DEPENDENCIES = \ LIBSAMBA-UTIL \ @@ -9,8 +6,13 @@ PUBLIC_DEPENDENCIES = \ UNIX_PRIVS \ UTIL_TDB \ CLUSTER \ - LIBNDR -# End SUBSYSTEM MESSAGING -################################################ + LIBNDR \ + samba-socket MESSAGING_OBJ_FILES = $(libmessagingsrcdir)/messaging.o + +[PYTHON::python_messaging] +LIBRARY_REALNAME = samba/messaging.$(SHLIBEXT) +PRIVATE_DEPENDENCIES = MESSAGING LIBEVENTS python_irpc + +python_messaging_OBJ_FILES = $(libmessagingsrcdir)/pymessaging.o diff --git a/source4/lib/messaging/irpc.h b/source4/lib/messaging/irpc.h index f44c0af3ec..65e98dce2c 100644 --- a/source4/lib/messaging/irpc.h +++ b/source4/lib/messaging/irpc.h @@ -125,6 +125,7 @@ NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name); struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx, TALLOC_CTX *mem_ctx, const char *name); void irpc_remove_name(struct messaging_context *msg_ctx, const char *name); NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status); +struct server_id messaging_get_server_id(struct messaging_context *msg_ctx); #endif diff --git a/source4/lib/messaging/messaging.c b/source4/lib/messaging/messaging.c index e7b654894f..4b90e8c4fd 100644 --- a/source4/lib/messaging/messaging.c +++ b/source4/lib/messaging/messaging.c @@ -1113,3 +1113,8 @@ void irpc_remove_name(struct messaging_context *msg_ctx, const char *name) tdb_unlock_bystring(t->tdb, name); talloc_free(t); } + +struct server_id messaging_get_server_id(struct messaging_context *msg_ctx) +{ + return msg_ctx->server_id; +} diff --git a/source4/lib/messaging/pymessaging.c b/source4/lib/messaging/pymessaging.c new file mode 100644 index 0000000000..1c22fb431a --- /dev/null +++ b/source4/lib/messaging/pymessaging.c @@ -0,0 +1,559 @@ +/* + Unix SMB/CIFS implementation. + Copyright © Jelmer Vernooij <jelmer@samba.org> 2008 + + Based on the equivalent for EJS: + Copyright © Andrew Tridgell <tridge@samba.org> 2005 + + 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 "libcli/util/pyerrors.h" +#include "librpc/rpc/pyrpc.h" +#include "lib/messaging/irpc.h" +#include "lib/messaging/messaging.h" +#include "lib/events/events.h" +#include "cluster/cluster.h" +#include "param/param.h" +#include "librpc/gen_ndr/py_irpc.h" + +PyAPI_DATA(PyTypeObject) messaging_Type; +PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType; + +static bool server_id_from_py(PyObject *object, struct server_id *server_id) +{ + if (!PyTuple_Check(object)) { + PyErr_SetString(PyExc_ValueError, "Expected tuple"); + return false; + } + + if (PyTuple_Size(object) == 3) { + return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node); + } else { + int id, id2; + if (!PyArg_ParseTuple(object, "ii", &id, &id2)) + return false; + *server_id = cluster_id(id, id2); + return true; + } +} + +typedef struct { + PyObject_HEAD + TALLOC_CTX *mem_ctx; + struct messaging_context *msg_ctx; +} messaging_Object; + +PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs) +{ + struct event_context *ev; + const char *kwnames[] = { "own_id", "messaging_path", NULL }; + PyObject *own_id = Py_None; + const char *messaging_path = NULL; + messaging_Object *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect", + discard_const_p(char *, kwnames), &own_id, &messaging_path)) { + return NULL; + } + + ret = PyObject_New(messaging_Object, &messaging_Type); + if (ret == NULL) + return NULL; + + ret->mem_ctx = talloc_new(NULL); + + ev = event_context_init(ret->mem_ctx); + + if (messaging_path == NULL) { + messaging_path = lp_messaging_path(ret, global_loadparm); + } else { + messaging_path = talloc_strdup(ret->mem_ctx, messaging_path); + } + + if (own_id != Py_None) { + struct server_id server_id; + + if (!server_id_from_py(own_id, &server_id)) + return NULL; + + ret->msg_ctx = messaging_init(ret->mem_ctx, + messaging_path, + server_id, + lp_iconv_convenience(global_loadparm), + ev); + } else { + ret->msg_ctx = messaging_client_init(ret->mem_ctx, + messaging_path, + lp_iconv_convenience(global_loadparm), + ev); + } + + if (ret->msg_ctx == NULL) { + PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context"); + talloc_free(ret->mem_ctx); + return NULL; + } + + return (PyObject *)ret; +} + +static void py_messaging_dealloc(PyObject *self) +{ + messaging_Object *iface = (messaging_Object *)self; + talloc_free(iface->msg_ctx); + PyObject_Del(self); +} + +static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs) +{ + messaging_Object *iface = (messaging_Object *)self; + uint32_t msg_type; + DATA_BLOB data; + PyObject *target; + NTSTATUS status; + struct server_id server; + const char *kwnames[] = { "target", "msg_type", "data", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send", + discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &data.length)) { + return NULL; + } + + if (!server_id_from_py(target, &server)) + return NULL; + + status = messaging_send(iface->msg_ctx, server, msg_type, &data); + if (NT_STATUS_IS_ERR(status)) { + PyErr_SetNTSTATUS(status); + return NULL; + } + + return Py_None; +} + +static void py_msg_callback_wrapper(struct messaging_context *msg, void *private, + uint32_t msg_type, + struct server_id server_id, DATA_BLOB *data) +{ + PyObject *callback = (PyObject *)private; + + PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type, + server_id.id, server_id.id2, server_id.node, + data->data, data->length); +} + +static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs) +{ + messaging_Object *iface = (messaging_Object *)self; + uint32_t msg_type = -1; + PyObject *callback; + NTSTATUS status; + const char *kwnames[] = { "callback", "msg_type", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send", + discard_const_p(char *, kwnames), &callback, &msg_type)) { + return NULL; + } + + Py_INCREF(callback); + + if (msg_type == -1) { + status = messaging_register_tmp(iface->msg_ctx, callback, + py_msg_callback_wrapper, &msg_type); + } else { + status = messaging_register(iface->msg_ctx, callback, + msg_type, py_msg_callback_wrapper); + } + if (NT_STATUS_IS_ERR(status)) { + PyErr_SetNTSTATUS(status); + return NULL; + } + + return PyLong_FromLong(msg_type); +} + +static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs) +{ + messaging_Object *iface = (messaging_Object *)self; + uint32_t msg_type = -1; + PyObject *callback; + const char *kwnames[] = { "callback", "msg_type", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send", + discard_const_p(char *, kwnames), &callback, &msg_type)) { + return NULL; + } + + messaging_deregister(iface->msg_ctx, msg_type, callback); + + Py_DECREF(callback); + + return Py_None; +} + +static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs) +{ + messaging_Object *iface = (messaging_Object *)self; + NTSTATUS status; + char *name; + const char *kwnames[] = { "name", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send", + discard_const_p(char *, kwnames), &name)) { + return NULL; + } + + status = irpc_add_name(iface->msg_ctx, name); + if (NT_STATUS_IS_ERR(status)) { + PyErr_SetNTSTATUS(status); + return NULL; + } + + return Py_None; +} + + +static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs) +{ + messaging_Object *iface = (messaging_Object *)self; + char *name; + const char *kwnames[] = { "name", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send", + discard_const_p(char *, kwnames), &name)) { + return NULL; + } + + irpc_remove_name(iface->msg_ctx, name); + + return Py_None; +} + +static PyMethodDef py_messaging_methods[] = { + { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS, + "S.send(target, msg_type, data) -> None\nSend a message" }, + { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS, + "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" }, + { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS, + "S.deregister(callback, msg_type) -> None\nDeregister a message handler" }, + { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" }, + { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" }, + { NULL, NULL, 0, NULL } +}; + +static PyObject *py_messaging_server_id(PyObject *obj, void *closure) +{ + messaging_Object *iface = (messaging_Object *)obj; + struct server_id server_id = messaging_get_server_id(iface->msg_ctx); + + return Py_BuildValue("(iii)", server_id.id, server_id.id2, + server_id.node); +} + +static PyGetSetDef py_messaging_getset[] = { + { discard_const_p(char, "server_id"), py_messaging_server_id, NULL, + discard_const_p(char, "local server id") }, + { NULL }, +}; + + +PyTypeObject messaging_Type = { + PyObject_HEAD_INIT(NULL) 0, + .tp_name = "irpc.Messaging", + .tp_basicsize = sizeof(messaging_Object), + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_new = py_messaging_connect, + .tp_dealloc = py_messaging_dealloc, + .tp_methods = py_messaging_methods, + .tp_getset = py_messaging_getset, + .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \ + "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \ + "If no path is specified, the default path from smb.conf will be used." +}; + + +/* + state of a irpc 'connection' +*/ +typedef struct { + PyObject_HEAD + const char *server_name; + struct server_id *dest_ids; + struct messaging_context *msg_ctx; + TALLOC_CTX *mem_ctx; +} irpc_ClientConnectionObject; + +/* + setup a context for talking to a irpc server + example: + status = irpc.connect("smb_server"); +*/ + +PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs) +{ + struct event_context *ev; + const char *kwnames[] = { "server", "own_id", "messaging_path", NULL }; + char *server; + const char *messaging_path = NULL; + PyObject *own_id = Py_None; + irpc_ClientConnectionObject *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect", + discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) { + return NULL; + } + + ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType); + if (ret == NULL) + return NULL; + + ret->mem_ctx = talloc_new(NULL); + + ret->server_name = server; + + ev = event_context_init(ret->mem_ctx); + + if (messaging_path == NULL) { + messaging_path = lp_messaging_path(ret, global_loadparm); + } else { + messaging_path = talloc_strdup(ret->mem_ctx, messaging_path); + } + + if (own_id != Py_None) { + struct server_id server_id; + + if (!server_id_from_py(own_id, &server_id)) + return NULL; + + ret->msg_ctx = messaging_init(ret->mem_ctx, + messaging_path, + server_id, + lp_iconv_convenience(global_loadparm), + ev); + } else { + ret->msg_ctx = messaging_client_init(ret->mem_ctx, + messaging_path, + lp_iconv_convenience(global_loadparm), + ev); + } + + if (ret->msg_ctx == NULL) { + PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context"); + talloc_free(ret->mem_ctx); + return NULL; + } + + ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name); + if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) { + talloc_free(ret->mem_ctx); + PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND); + return NULL; + } else { + return (PyObject *)ret; + } +} + +typedef struct { + PyObject_HEAD + struct irpc_request **reqs; + int count; + int current; + TALLOC_CTX *mem_ctx; + py_data_unpack_fn unpack_fn; +} irpc_ResultObject; + + +static PyObject *irpc_result_next(irpc_ResultObject *iterator) +{ + NTSTATUS status; + + if (iterator->current >= iterator->count) { + PyErr_SetString(PyExc_StopIteration, "No more results"); + return NULL; + } + + status = irpc_call_recv(iterator->reqs[iterator->current]); + iterator->current++; + if (!NT_STATUS_IS_OK(status)) { + PyErr_SetNTSTATUS(status); + return NULL; + } + + return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r); +} + +static PyObject *irpc_result_len(irpc_ResultObject *self) +{ + return PyLong_FromLong(self->count); +} + +static PyMethodDef irpc_result_methods[] = { + { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS, + "Number of elements returned"}, + { NULL } +}; + +static void irpc_result_dealloc(PyObject *self) +{ + talloc_free(((irpc_ResultObject *)self)->mem_ctx); + PyObject_Del(self); +} + +PyTypeObject irpc_ResultIteratorType = { + PyObject_HEAD_INIT(NULL) 0, + .tp_name = "irpc.ResultIterator", + .tp_basicsize = sizeof(irpc_ResultObject), + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_iternext = (iternextfunc)irpc_result_next, + .tp_iter = PyObject_SelfIter, + .tp_methods = irpc_result_methods, + .tp_dealloc = irpc_result_dealloc, +}; + +static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs) +{ + void *ptr; + struct irpc_request **reqs; + int i, count; + NTSTATUS status; + TALLOC_CTX *mem_ctx = talloc_new(NULL); + irpc_ResultObject *ret; + + /* allocate the C structure */ + ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size); + if (ptr == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + + /* convert the mpr object into a C structure */ + if (!method_def->pack_in_data(args, kwargs, ptr)) { + talloc_free(mem_ctx); + return NULL; + } + + for (count=0;p->dest_ids[count].id;count++) /* noop */ ; + + /* we need to make a call per server */ + reqs = talloc_array(mem_ctx, struct irpc_request *, count); + if (reqs == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + + /* make the actual calls */ + for (i=0;i<count;i++) { + reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i], + method_def->table, method_def->opnum, ptr, ptr); + if (reqs[i] == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + talloc_steal(reqs, reqs[i]); + } + + ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType); + ret->mem_ctx = mem_ctx; + ret->reqs = reqs; + ret->count = count; + ret->current = 0; + ret->unpack_fn = method_def->unpack_out_data; + + return (PyObject *)ret; +done: + talloc_free(mem_ctx); + PyErr_SetNTSTATUS(status); + return NULL; +} + +static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs) +{ + irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self; + struct PyNdrRpcMethodDef *md = wrapped; + + return py_irpc_call(iface, md, args, kwargs); +} + +static void py_irpc_dealloc(PyObject *self) +{ + irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self; + talloc_free(iface->mem_ctx); + PyObject_Del(self); +} + +PyTypeObject irpc_ClientConnectionType = { + PyObject_HEAD_INIT(NULL) 0, + .tp_name = "irpc.ClientConnection", + .tp_basicsize = sizeof(irpc_ClientConnectionObject), + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_new = py_irpc_connect, + .tp_dealloc = py_irpc_dealloc, + .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \ + "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \ + "If no path is specified, the default path from smb.conf will be used." +}; + +static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds) +{ + int i; + for (i = 0; mds[i].name; i++) { + PyObject *ret; + struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1); + + wb->name = discard_const_p(char, mds[i].name); + wb->flags = PyWrapperFlag_KEYWORDS; + wb->wrapper = (wrapperfunc)py_irpc_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; +} + +void initmessaging(void) +{ + extern void initirpc(void); + PyObject *mod; + + if (PyType_Ready(&irpc_ClientConnectionType) < 0) + return; + + if (PyType_Ready(&messaging_Type) < 0) + return; + + if (PyType_Ready(&irpc_ResultIteratorType) < 0) + return; + + if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods)) + return; + + mod = Py_InitModule3("messaging", NULL, "Internal RPC"); + if (mod == NULL) + return; + + initirpc(); + + Py_INCREF((PyObject *)&irpc_ClientConnectionType); + PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType); + + Py_INCREF((PyObject *)&messaging_Type); + PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type); +} diff --git a/source4/lib/messaging/tests/bindings.py b/source4/lib/messaging/tests/bindings.py new file mode 100644 index 0000000000..3d58843799 --- /dev/null +++ b/source4/lib/messaging/tests/bindings.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Unix SMB/CIFS implementation. +# Copyright © 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/>. +# + +from samba.irpc import Messaging +from unittest import TestCase + +class MessagingTests(TestCase): + def get_context(self, *args, **kwargs): + kwargs["messaging_path"] = "." + return Messaging(*args, **kwargs) + + def test_register(self): + x = self.get_context() + def callback(): + pass + msg_type = x.register(callback) + x.deregister(callback, msg_type) + + def test_assign_server_id(self): + x = self.get_context() + self.assertTrue(isinstance(x.server_id, tuple)) + self.assertEquals(3, len(x.server_id)) + + def test_ping_speed(self): + server_ctx = self.get_context((0, 1)) + def ping_callback(src, data): + server_ctx.send(src, data) + def exit_callback(): + print "received exit" + msg_ping = server_ctx.register(ping_callback) + msg_exit = server_ctx.register(exit_callback) + + def pong_callback(): + print "received pong" + client_ctx = self.get_context((0, 2)) + msg_pong = client_ctx.register(pong_callback) + + client_ctx.send((0,1), msg_ping, "testing") + client_ctx.send((0,1), msg_ping, "") + diff --git a/source4/librpc/config.mk b/source4/librpc/config.mk index 2943c7b516..ab25921ef8 100644 --- a/source4/librpc/config.mk +++ b/source4/librpc/config.mk @@ -434,6 +434,11 @@ PUBLIC_DEPENDENCIES = dcerpc NDR_UNIXINFO RPC_NDR_UNIXINFO_OBJ_FILES = $(gen_ndrsrcdir)/ndr_unixinfo_c.o +[SUBSYSTEM::RPC_NDR_IRPC] +PUBLIC_DEPENDENCIES = dcerpc NDR_IRPC + +RPC_NDR_IRPC_OBJ_FILES = $(gen_ndrsrcdir)/ndr_irpc_c.o + [LIBRARY::dcerpc_samr] PUBLIC_DEPENDENCIES = dcerpc NDR_SAMR @@ -658,6 +663,12 @@ PRIVATE_DEPENDENCIES = dcerpc_atsvc PYTALLOC param swig_credentials python_dcer python_atsvc_OBJ_FILES = $(gen_ndrsrcdir)/py_atsvc.o +[PYTHON::python_nbt] +LIBRARY_REALNAME = samba/nbt.$(SHLIBEXT) +PRIVATE_DEPENDENCIES = NDR_NBT PYTALLOC param swig_credentials python_dcerpc + +python_nbt_OBJ_FILES = $(gen_ndrsrcdir)/py_nbt.o + [PYTHON::python_samr] LIBRARY_REALNAME = samba/dcerpc/samr.$(SHLIBEXT) PRIVATE_DEPENDENCIES = dcerpc_samr PYTALLOC python_dcerpc_security python_lsa python_dcerpc_misc swig_credentials param python_dcerpc @@ -694,6 +705,12 @@ PRIVATE_DEPENDENCIES = RPC_NDR_UNIXINFO PYTALLOC param swig_credentials python_d python_unixinfo_OBJ_FILES = $(gen_ndrsrcdir)/py_unixinfo.o +[PYTHON::python_irpc] +LIBRARY_REALNAME = samba/irpc.$(SHLIBEXT) +PRIVATE_DEPENDENCIES = RPC_NDR_IRPC PYTALLOC param swig_credentials python_dcerpc_security python_dcerpc_misc python_dcerpc python_nbt + +python_irpc_OBJ_FILES = $(gen_ndrsrcdir)/py_irpc.o + [PYTHON::python_drsuapi] LIBRARY_REALNAME = samba/dcerpc/drsuapi.$(SHLIBEXT) PRIVATE_DEPENDENCIES = RPC_NDR_DRSUAPI PYTALLOC param swig_credentials python_dcerpc_misc python_dcerpc_security python_dcerpc diff --git a/source4/librpc/rpc/dcerpc.h b/source4/librpc/rpc/dcerpc.h index ca6311a4fc..487f9f2eda 100644 --- a/source4/librpc/rpc/dcerpc.h +++ b/source4/librpc/rpc/dcerpc.h @@ -370,5 +370,6 @@ NTSTATUS dcerpc_request(struct dcerpc_pipe *p, DATA_BLOB *stub_data_in, DATA_BLOB *stub_data_out); +typedef NTSTATUS (*dcerpc_call_fn) (struct dcerpc_pipe *, TALLOC_CTX *, void *); #endif /* __DCERPC_H__ */ diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 26242e8235..d8344be511 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -24,6 +24,74 @@ #include "librpc/rpc/dcerpc.h" #include "lib/events/events.h" +static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface, 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) + return NULL; + + r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size); + if (r == NULL) + 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); + + 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; + struct PyNdrRpcMethodDef *md = 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 = 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; +} + static bool PyString_AsGUID(PyObject *object, struct GUID *uuid) { NTSTATUS status; @@ -311,7 +379,6 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje talloc_free(mem_ctx); return NULL; } - ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC; return (PyObject *)ret; } diff --git a/source4/librpc/rpc/pyrpc.h b/source4/librpc/rpc/pyrpc.h index 209d7d3f1f..af9ca728d8 100644 --- a/source4/librpc/rpc/pyrpc.h +++ b/source4/librpc/rpc/pyrpc.h @@ -21,6 +21,7 @@ #define _PYRPC_H_ #include "libcli/util/pyerrors.h" +#include "librpc/rpc/dcerpc.h" #define PY_CHECK_TYPE(type, var, fail) \ if (!type ## _Check(var)) {\ @@ -28,8 +29,10 @@ fail; \ } +#define dom_sid0_Type dom_sid_Type #define dom_sid2_Type dom_sid_Type #define dom_sid28_Type dom_sid_Type +#define dom_sid0_Check dom_sid_Check #define dom_sid2_Check dom_sid_Check #define dom_sid28_Check dom_sid_Check @@ -52,4 +55,19 @@ PyAPI_DATA(PyTypeObject) dcerpc_InterfaceType; void PyErr_SetDCERPCStatus(struct dcerpc_pipe *pipe, NTSTATUS status); +typedef bool (*py_data_pack_fn) (PyObject *args, PyObject *kwargs, void *r); +typedef PyObject *(*py_data_unpack_fn) (void *r); + +struct PyNdrRpcMethodDef { + const char *name; + const char *doc; + dcerpc_call_fn call; + py_data_pack_fn pack_in_data; + py_data_unpack_fn unpack_out_data; + uint32_t opnum; + const struct ndr_interface_table *table; +}; + +bool PyInterface_AddNdrRpcMethods(PyTypeObject *object, const struct PyNdrRpcMethodDef *mds); + #endif /* _PYRPC_H_ */ diff --git a/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm b/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm index dbbdb6bca1..60d0dafc8a 100644 --- a/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm +++ b/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -21,7 +21,8 @@ $VERSION = '0.01'; sub new($) { my ($class) = @_; my $self = { res => "", res_hdr => "", tabs => "", constants => {}, - module_methods => [], module_objects => [], module_types => []}; + module_methods => [], module_objects => [], ready_types => [], + readycode => [] }; bless($self, $class); } @@ -107,7 +108,7 @@ sub FromUnionToPythonFunction($$$$) $self->indent; if ($e->{NAME}) { - $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret"); + $self->ConvertObjectToPython($mem_ctx, {}, $e, "$name->$e->{NAME}", "ret", "return NULL;"); } else { $self->pidl("ret = Py_None;"); } @@ -181,7 +182,7 @@ sub PythonStruct($$$$$$) $self->indent; $self->pidl("$cname *object = py_talloc_get_ptr(obj);"); $self->pidl("PyObject *py_$e->{NAME};"); - $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}"); + $self->ConvertObjectToPython("py_talloc_get_mem_ctx(obj)", $env, $e, $varname, "py_$e->{NAME}", "return NULL;"); $self->pidl("return py_$e->{NAME};"); $self->deindent; $self->pidl("}"); @@ -323,25 +324,9 @@ sub get_metadata_var($) return undef; } -sub PythonFunctionBody($$$) +sub find_metadata_args($) { - my ($self, $fn, $iface, $prettyname) = @_; - - $self->pidl("dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;"); - $self->pidl("NTSTATUS status;"); - $self->pidl("TALLOC_CTX *mem_ctx = talloc_new(NULL);"); - $self->pidl("struct $fn->{NAME} *r = talloc_zero(mem_ctx, struct $fn->{NAME});"); - $self->pidl("PyObject *result = Py_None;"); - - my $env = GenerateFunctionInEnv($fn, "r->"); - my $result_size = 0; - - my $args_format = ""; - my $args_string = ""; - my $args_names = ""; - - my $signature = "S.$prettyname("; - + my ($fn) = @_; my $metadata_args = { in => {}, out => {} }; # Determine arguments that are metadata for other arguments (size_is/length_is) @@ -354,71 +339,37 @@ sub PythonFunctionBody($$$) } } + return $metadata_args; +} + +sub PythonFunctionUnpackOut($$$) +{ + my ($self, $fn, $fnname) = @_; + + my $outfnname = "unpack_$fnname\_args_out"; + my $signature = ""; + + my $metadata_args = find_metadata_args($fn); + + my $env = GenerateFunctionOutEnv($fn, "r->"); + my $result_size = 0; + + $self->pidl("static PyObject *$outfnname(struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + $self->pidl("PyObject *result = Py_None;"); foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/out/,@{$e->{DIRECTION}})); next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); $self->pidl("PyObject *py_$e->{NAME};"); - if (grep(/out/,@{$e->{DIRECTION}})) { - $result_size++; - } - if (grep(/in/,@{$e->{DIRECTION}})) { - $args_format .= "O"; - $args_string .= ", &py_$e->{NAME}"; - $args_names .= "\"$e->{NAME}\", "; - $signature .= "$e->{NAME}, "; - } - } - if (substr($signature, -2) eq ", ") { - $signature = substr($signature, 0, -2); + $result_size++; } - $signature.= ") -> "; - - $self->pidl("const char *kwnames[] = {"); - $self->indent; - $self->pidl($args_names . "NULL"); - $self->deindent; - $self->pidl("};"); - - $self->pidl(""); - $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {"); - $self->indent; - $self->pidl("return NULL;"); - $self->deindent; - $self->pidl("}"); - $self->pidl(""); if ($fn->{RETURN_TYPE}) { $result_size++ unless ($fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "NTSTATUS"); } - my $fail = "talloc_free(mem_ctx); return NULL;"; - foreach my $e (@{$fn->{ELEMENTS}}) { - next unless (grep(/in/,@{$e->{DIRECTION}})); - if ($metadata_args->{in}->{$e->{NAME}}) { - my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; - $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); - my $val = "PyList_Size($py_var)"; - if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { - $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(mem_ctx, r->in.$e->{NAME});"); - $self->pidl("*r->in.$e->{NAME} = $val;"); - } else { - $self->pidl("r->in.$e->{NAME} = $val;"); - } - } else { - $self->ConvertObjectFromPython($env, "mem_ctx", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail); - } - } - $self->pidl("status = dcerpc_$fn->{NAME}(iface->pipe, mem_ctx, r);"); - $self->pidl("if (NT_STATUS_IS_ERR(status)) {"); - $self->indent; - $self->pidl("PyErr_SetDCERPCStatus(iface->pipe, status);"); - $self->pidl("talloc_free(mem_ctx);"); - $self->pidl("return NULL;"); - $self->deindent; - $self->pidl("}"); - $self->pidl(""); - - $env = GenerateFunctionOutEnv($fn, "r->"); my $i = 0; if ($result_size > 1) { @@ -432,7 +383,7 @@ sub PythonFunctionBody($$$) next if ($metadata_args->{out}->{$e->{NAME}}); my $py_name = "py_$e->{NAME}"; if (grep(/out/,@{$e->{DIRECTION}})) { - $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name); + $self->ConvertObjectToPython("r", $env, $e, "r->out.$e->{NAME}", $py_name, "return NULL;"); if ($result_size > 1) { $self->pidl("PyTuple_SetItem(result, $i, $py_name);"); $i++; @@ -445,9 +396,9 @@ sub PythonFunctionBody($$$) } if (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "NTSTATUS") { - $self->handle_ntstatus("r->out.result", "NULL", "mem_ctx"); + $self->handle_ntstatus("r->out.result", "NULL", undef); } elsif (defined($fn->{RETURN_TYPE}) and $fn->{RETURN_TYPE} eq "WERROR") { - $self->handle_werror("r->out.result", "NULL", "mem_ctx"); + $self->handle_werror("r->out.result", "NULL", undef); } elsif (defined($fn->{RETURN_TYPE})) { my $conv = $self->ConvertObjectToPythonData("r", $fn->{RETURN_TYPE}, "r->out.result"); if ($result_size > 1) { @@ -465,10 +416,80 @@ sub PythonFunctionBody($$$) $signature .= ")"; } - $self->pidl("talloc_free(mem_ctx);"); $self->pidl("return result;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + return ($outfnname, $signature); +} + +sub PythonFunctionPackIn($$$) +{ + my ($self, $fn, $fnname) = @_; + my $metadata_args = find_metadata_args($fn); + + my $infnname = "pack_$fnname\_args_in"; + + $self->pidl("static bool $infnname(PyObject *args, PyObject *kwargs, struct $fn->{NAME} *r)"); + $self->pidl("{"); + $self->indent; + my $args_format = ""; + my $args_string = ""; + my $args_names = ""; + my $signature = ""; + + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + next if (($metadata_args->{in}->{$e->{NAME}} and grep(/in/, @{$e->{DIRECTION}})) or + ($metadata_args->{out}->{$e->{NAME}}) and grep(/out/, @{$e->{DIRECTION}})); + $self->pidl("PyObject *py_$e->{NAME};"); + $args_format .= "O"; + $args_string .= ", &py_$e->{NAME}"; + $args_names .= "\"$e->{NAME}\", "; + $signature .= "$e->{NAME}, "; + } + if (substr($signature, -2) eq ", ") { + $signature = substr($signature, 0, -2); + } + $self->pidl("const char *kwnames[] = {"); + $self->indent; + $self->pidl($args_names . "NULL"); + $self->deindent; + $self->pidl("};"); + + $self->pidl(""); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"$args_format:$fn->{NAME}\", discard_const_p(char *, kwnames)$args_string)) {"); + $self->indent; + $self->pidl("return false;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + + my $env = GenerateFunctionInEnv($fn, "r->"); - return $signature; + my $fail = "return false;"; + foreach my $e (@{$fn->{ELEMENTS}}) { + next unless (grep(/in/,@{$e->{DIRECTION}})); + if ($metadata_args->{in}->{$e->{NAME}}) { + my $py_var = "py_".$metadata_args->{in}->{$e->{NAME}}; + $self->pidl("PY_CHECK_TYPE(PyList, $py_var, $fail);"); + my $val = "PyList_Size($py_var)"; + if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { + $self->pidl("r->in.$e->{NAME} = talloc_ptrtype(r, r->in.$e->{NAME});"); + $self->pidl("*r->in.$e->{NAME} = $val;"); + } else { + $self->pidl("r->in.$e->{NAME} = $val;"); + } + } else { + $self->ConvertObjectFromPython($env, "r", $e, "py_$e->{NAME}", "r->in.$e->{NAME}", $fail); + } + } + $self->pidl("return true;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + return ($infnname, $signature); } sub PythonFunction($$$) @@ -478,16 +499,17 @@ sub PythonFunction($$$) my $fnname = "py_$fn->{NAME}"; my $docstring = $self->DocString($fn, $fn->{NAME}); - $self->pidl("static PyObject *$fnname(PyObject *self, PyObject *args, PyObject *kwargs)"); - $self->pidl("{"); - $self->indent; + my ($insignature, $outsignature); + my ($infn, $outfn); + if (has_property($fn, "todo")) { - $self->pidl("PyErr_SetString(PyExc_NotImplementedError, \"No marshalling code available yet for $prettyname\");"); - $self->pidl("return NULL;"); unless ($docstring) { $docstring = "NULL"; } + $infn = "NULL"; + $outfn = "NULL"; } else { - my $signature = $self->PythonFunctionBody($fn, $iface, $prettyname); - + ($infn, $insignature) = $self->PythonFunctionPackIn($fn, $fnname); + ($outfn, $outsignature) = $self->PythonFunctionUnpackOut($fn, $fnname); + my $signature = "S.$prettyname($insignature) -> $outsignature"; if ($docstring) { $docstring = "\"$signature\\n\\n\"$docstring"; } else { @@ -495,11 +517,7 @@ sub PythonFunction($$$) } } - $self->deindent; - $self->pidl("}"); - $self->pidl(""); - - return ($fnname, $docstring); + return ($infn, $outfn, $docstring); } sub handle_werror($$$$) @@ -627,18 +645,19 @@ sub Interface($$$) $prettyname =~ s/^$interface->{NAME}_//; $prettyname =~ s/^$basename\_//; - my ($fnname, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname); + my ($infn, $outfn, $fndocstring) = $self->PythonFunction($d, $interface->{NAME}, $prettyname); - push (@fns, [$fnname, $prettyname, $fndocstring]); + push (@fns, [$infn, $outfn, "dcerpc_$d->{NAME}", $prettyname, $fndocstring, $d->{OPNUM}]); } - $self->pidl("static PyMethodDef interface_$interface->{NAME}\_methods[] = {"); + $self->pidl("const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[] = {"); + $self->pidl_hdr("extern const struct PyNdrRpcMethodDef py_ndr_$interface->{NAME}\_methods[];"); $self->indent; foreach my $d (@fns) { - my ($c_fn, $prettyname, $docstring) = @$d; - $self->pidl("{ \"$prettyname\", (PyCFunction)$c_fn, METH_VARARGS|METH_KEYWORDS, $docstring },"); + my ($infn, $outfn, $callfn, $prettyname, $docstring, $opnum) = @$d; + $self->pidl("{ \"$prettyname\", $docstring, (dcerpc_call_fn)$callfn, (py_data_pack_fn)$infn, (py_data_unpack_fn)$outfn, $opnum, &ndr_table_$interface->{NAME} },"); } - $self->pidl("{ NULL, NULL, 0, NULL }"); + $self->pidl("{ NULL }"); $self->deindent; $self->pidl("};"); $self->pidl(""); @@ -744,7 +763,6 @@ sub Interface($$$) $self->pidl(".tp_name = \"$basename.$interface->{NAME}\","); $self->pidl(".tp_basicsize = sizeof(dcerpc_InterfaceObject),"); $self->pidl(".tp_base = &dcerpc_InterfaceType,"); - $self->pidl(".tp_methods = interface_$interface->{NAME}_methods,"); $self->pidl(".tp_doc = $docstring,"); $self->pidl(".tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,"); $self->pidl(".tp_new = interface_$interface->{NAME}_new,"); @@ -754,6 +772,7 @@ sub Interface($$$) $self->pidl(""); $self->register_module_typeobject($interface->{NAME}, "&$interface->{NAME}_InterfaceType"); + $self->register_module_readycode(["if (!PyInterface_AddNdrRpcMethods(&$interface->{NAME}_InterfaceType, py_ndr_$interface->{NAME}\_methods))", "\treturn;", ""]); } $self->pidl_hdr("\n"); @@ -773,7 +792,20 @@ sub register_module_typeobject($$$) $self->register_module_object($name, "(PyObject *)$py_name"); - push (@{$self->{module_types}}, [$name, $py_name]) + $self->check_ready_type($py_name); +} + +sub check_ready_type($$) +{ + my ($self, $py_name) = @_; + push (@{$self->{ready_types}}, $py_name) unless (grep(/^$py_name$/,@{$self->{ready_types}})); +} + +sub register_module_readycode($$) +{ + my ($self, $code) = @_; + + push (@{$self->{readycode}}, @$code); } sub register_module_object($$$) @@ -1000,14 +1032,24 @@ sub ConvertObjectToPythonData($$$$$) die("unknown type ".mapTypeName($ctype) . ": $cvar"); } -sub ConvertObjectToPythonLevel($$$$$) +sub fail_on_null($$$) { - my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var) = @_; + my ($self, $var, $fail) = @_; + $self->pidl("if ($var == NULL) {"); + $self->indent; + $self->pidl($fail); + $self->deindent; + $self->pidl("}"); +} + +sub ConvertObjectToPythonLevel($$$$$$) +{ + my ($self, $mem_ctx, $env, $e, $l, $var_name, $py_var, $fail) = @_; my $nl = GetNextLevel($e, $l); if ($l->{TYPE} eq "POINTER") { if ($nl->{TYPE} eq "DATA" and Parse::Pidl::Typelist::scalar_is_reference($nl->{DATA_TYPE})) { - $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var); + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, $var_name, $py_var, $fail); return; } if ($l->{POINTER_TYPE} ne "ref") { @@ -1018,7 +1060,7 @@ sub ConvertObjectToPythonLevel($$$$$) $self->pidl("} else {"); $self->indent; } - $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var); + $self->ConvertObjectToPythonLevel($var_name, $env, $e, $nl, get_value_of($var_name), $py_var, $fail); if ($l->{POINTER_TYPE} ne "ref") { $self->deindent; $self->pidl("}"); @@ -1041,6 +1083,7 @@ sub ConvertObjectToPythonLevel($$$$$) $length = ParseExpr($length, $env, $e); $self->pidl("$py_var = PyList_New($length);"); + $self->fail_on_null($py_var, $fail); $self->pidl("{"); $self->indent; my $counter = "$e->{NAME}_cntr_$l->{LEVEL_INDEX}"; @@ -1049,7 +1092,7 @@ sub ConvertObjectToPythonLevel($$$$$) $self->indent; my $member_var = "py_$e->{NAME}_$l->{LEVEL_INDEX}"; $self->pidl("PyObject *$member_var;"); - $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var); + $self->ConvertObjectToPythonLevel($var_name, $env, $e, GetNextLevel($e, $l), $var_name."[$counter]", $member_var, $fail); $self->pidl("PyList_SetItem($py_var, $counter, $member_var);"); $self->deindent; $self->pidl("}"); @@ -1060,6 +1103,8 @@ sub ConvertObjectToPythonLevel($$$$$) $var_name = get_pointer_to($var_name); my $switch = ParseExpr($l->{SWITCH_IS}, $env, $e); $self->pidl("$py_var = py_import_" . GetNextLevel($e, $l)->{DATA_TYPE} . "($mem_ctx, $switch, $var_name);"); + $self->fail_on_null($py_var, $fail); + } elsif ($l->{TYPE} eq "DATA") { if (not Parse::Pidl::Typelist::is_scalar($l->{DATA_TYPE})) { $var_name = get_pointer_to($var_name); @@ -1067,7 +1112,7 @@ sub ConvertObjectToPythonLevel($$$$$) my $conv = $self->ConvertObjectToPythonData($mem_ctx, $l->{DATA_TYPE}, $var_name); $self->pidl("$py_var = $conv;"); } elsif ($l->{TYPE} eq "SUBCONTEXT") { - $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var); + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $e, GetNextLevel($e, $l), $var_name, $py_var, $fail); } else { die("Unknown level type $l->{TYPE} $var_name"); } @@ -1075,9 +1120,9 @@ sub ConvertObjectToPythonLevel($$$$$) sub ConvertObjectToPython($$$$$$) { - my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var) = @_; + my ($self, $mem_ctx, $env, $ctype, $cvar, $py_var, $fail) = @_; - $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var); + $self->ConvertObjectToPythonLevel($mem_ctx, $env, $ctype, $ctype->{LEVELS}[0], $cvar, $py_var, $fail); } sub Parse($$$$$) @@ -1127,12 +1172,13 @@ sub Parse($$$$$) $self->pidl("PyObject *m;"); $self->pidl(""); - foreach (@{$self->{module_types}}) { - my ($object_name, $c_name) = @$_; - $self->pidl("if (PyType_Ready($c_name) < 0)"); + foreach (@{$self->{ready_types}}) { + $self->pidl("if (PyType_Ready($_) < 0)"); $self->pidl("\treturn;"); } + $self->pidl($_) foreach (@{$self->{readycode}}); + $self->pidl(""); $self->pidl("m = Py_InitModule3(\"$basename\", $basename\_methods, \"$basename DCE/RPC\");"); diff --git a/source4/scripting/bin/samba3dump b/source4/scripting/bin/samba3dump index d89667233f..c11f8dbd0d 100755 --- a/source4/scripting/bin/samba3dump +++ b/source4/scripting/bin/samba3dump @@ -14,7 +14,7 @@ sys.path.insert(0, "bin/python") import samba import samba.samba3 -parser = optparse.OptionParser("provision <libdir> [<smb.conf>]") +parser = optparse.OptionParser("samba3dump <libdir> [<smb.conf>]") parser.add_option("--format", type="choice", metavar="FORMAT", choices=["full", "summary"]) @@ -96,7 +96,7 @@ def print_samba3_secrets(secrets): def print_samba3_regdb(regdb): print_header("Registry") - from registry import str_regtype + from samba.registry import str_regtype for k in regdb.keys(): print "[%s]" % k diff --git a/source4/scripting/bin/smbstatus b/source4/scripting/bin/smbstatus index 4dfc3365a1..bbd0e84826 100755 --- a/source4/scripting/bin/smbstatus +++ b/source4/scripting/bin/smbstatus @@ -1,96 +1,83 @@ -#!/bin/sh -exec smbscript "$0" ${1+"$@"} -/* - provide information on connected users and open files - Copyright Andrew Tridgell 2005 - Released under the GNU GPL version 3 or later -*/ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# provide information on connected users and open files +# Copyright ǒ Jelmer Vernooij 2008 +# +# Based on the original in EJS: +# Copyright Andrew Tridgell 2005 +# Released under the GNU GPL version 3 or later +# -libinclude("base.js"); -libinclude("management.js"); +import os, sys -var options = new Object(); +sys.path.insert(0, "bin/python") -options = GetOptions(ARGV, - "POPT_AUTOHELP", - "POPT_COMMON_SAMBA", - "POPT_COMMON_VERSION", - "nbt"); -if (options == undefined) { - println("Failed to parse options: " + options.ERROR); - return -1; -} +import optparse +import samba.getopt as options +from samba import irpc, messaging -/* - show open sessions -*/ -function show_sessions() -{ - var sessions = smbsrv_sessions(); - var i; - var sys = sys_init(); - if (sessions == undefined) { - println("No sessions open"); - return; - } - printf("User Client Connected at\n"); - printf("-------------------------------------------------------------------------------\n"); - for (i=0;i<sessions.length;i++) { - var info = sessions[i]; - var fulluser = sprintf("%s/%s", info.account_name, info.domain_name); - printf("%-30s %16s %s\n", - fulluser, info.client_ip, sys.httptime(info.connect_time)); - } - printf("\n"); -} +def show_sessions(conn): + """show open sessions""" -/* - show open tree connects -*/ -function show_tcons() -{ - var tcons = smbsrv_tcons(); - var sys = sys_init(); - if (tcons == undefined) { - println("No tree connects"); - return; - } - printf("Share Client Connected at\n"); - printf("-------------------------------------------------------------------------------\n"); - for (i=0;i<tcons.length;i++) { - var info = tcons[i]; - printf("%-30s %16s %s\n", - info.share_name, info.client_ip, sys.httptime(info.connect_time)); - } -} + sessions = conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS).next() + print "User Client Connected at" + print "-------------------------------------------------------------------------------" + for session in sessions: + fulluser = "%s/%s" % (session.account_name, session.domain_name) + print "%-30s %16s %s" % (fulluser, session.client_ip, sys.httptime(session.connect_time)) + print "" -/* - show nbtd information -*/ -function show_nbt() -{ - var stats = nbtd_statistics(); - if (stats == undefined) { - println("nbt server not running"); - return; - } - var r; - println("NBT server statistics:"); - for (r in stats) { - print("\t" + r + ":\t" + stats[r] + "\n"); - } - println(""); -} +def show_tcons(open_connection): + """show open tree connects""" + conn = open_connection("smb_server") + tcons = conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS).next() + print "Share Client Connected at" + print "-------------------------------------------------------------------------------" + for tcon in tcons: + print "%-30s %16s %s" % (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time)) -var lp = loadparm_init(); -printf("%s\n\n", lp.get("server string")); +def show_nbt(open_connection): + """show nbtd information""" + conn = open_connection("nbt_server") + stats = conn.nbtd_information(irpc.NBTD_INFO_STATISTICS).next() + print "NBT server statistics:" + fields = [("total_received", "Total received"), + ("total_sent", "Total sent"), + ("query_count", "Query count"), + ("register_count", "Register count"), + ("release_count", "Release count")] + for (field, description) in fields: + print "\t%s:\t%s" % (description, getattr(stats, field)) + print -if (options['nbt'] != undefined) { - show_nbt(); -} else { - show_sessions(); - show_tcons(); -} +parser = optparse.OptionParser("%s [options]" % sys.argv[0]) +sambaopts = options.SambaOptions(parser) +parser.add_option_group(sambaopts) +parser.add_option("--messaging-path", type="string", metavar="PATH", + help="messaging path") +parser.add_option("--nbt", help="show NetBIOS status", action="store_true") -return 0; +opts, args = parser.parse_args() + +lp = sambaopts.get_loadparm() + +print "%s" % lp.get("server string") + +messaging_path = (opts.messaging_path or os.path.join(lp.get("private dir"), "smbd.tmp", "messaging")) + +def open_connection(name): + return messaging.ClientConnection(name, messaging_path=messaging_path) + +if opts.nbt: + show_nbt(open_connection) +else: + try: + conn = open_connection("smb_server") + except RuntimeError, (num, msg): + if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND': + print "No active connections" + else: + show_sessions(conn) + show_tcons(conn) diff --git a/source4/scripting/bin/winreg b/source4/scripting/bin/winreg deleted file mode 100755 index 883c6d7ee3..0000000000 --- a/source4/scripting/bin/winreg +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/sh -exec smbscript "$0" ${1+"$@"} -/* - tool to manipulate a remote registry - Copyright Andrew Tridgell 2005 - Released under the GNU GPL version 3 or later -*/ - -var options = GetOptions(ARGV, - "POPT_AUTOHELP", - "POPT_COMMON_SAMBA", - "POPT_COMMON_CREDENTIALS", - "createkey=s"); -if (options == undefined) { - println("Failed to parse options"); - return -1; -} - -libinclude("base.js"); -libinclude("winreg.js"); - -if (options.ARGV.length < 1) { - println("Usage: winreg.js <BINDING> [path]"); - return -1; -} -var binding = options.ARGV[0]; -reg = winregObj(); - -print("Connecting to " + binding + "\n"); -status = reg.connect(binding); -if (status.is_ok != true) { - print("Failed to connect to " + binding + " - " + status.errstr + "\n"); - return -1; -} - -function list_values(path) { - var list = reg.enum_values(path); - var i; - if (list == undefined) { - return; - } - for (i=0;i<list.length;i++) { - var v = list[i]; - printf("\ttype=%-30s size=%4d '%s'\n", reg.typestring(v.type), v.size, v.name); - if (v.type == reg.REG_SZ || v.type == reg.REG_EXPAND_SZ) { - printf("\t\t'%s'\n", v.value); - } - if (v.type == reg.REG_MULTI_SZ) { - var j; - for (j in v.value) { - printf("\t\t'%s'\n", v.value[j]); - } - } - if (v.type == reg.REG_DWORD || v.type == reg.REG_DWORD_BIG_ENDIAN) { - printf("\t\t0x%08x (%d)\n", v.value, v.value); - } - if (v.type == reg.REG_QWORD) { - printf("\t\t0x%llx (%lld)\n", v.value, v.value); - } - } -} - -function list_path(path) { - var count = 0; - var list = reg.enum_path(path); - if (list == undefined) { - println("Unable to list " + path); - return 0; - } - var i; - list_values(path); - count = count + list.length; - for (i=0;i<list.length;i++) { - var npath; - if (path) { - npath = path + "\\" + list[i]; - } else { - npath = list[i]; - } - println(npath); - count = count + list_path(npath); - } - return count; -} - -var root; - -if (options.ARGV.length > 1) { - root = options.ARGV[1]; -} else { - root = ''; -} - -if (options.createkey) { - var ok = reg.create_key("HKLM\\SOFTWARE", options.createkey); - if (!ok) { - println("Failed to create key"); - } -} else { - printf("Listing registry tree '%s'\n", root); - var count = list_path(root); - if (count == 0) { - println("No entries found"); - return 1; - } -} -return 0; diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk index c1a1ca1f0f..c5199669d5 100644 --- a/source4/scripting/ejs/config.mk +++ b/source4/scripting/ejs/config.mk @@ -1,7 +1,3 @@ -[SUBSYSTEM::EJSRPC] - -EJSRPC_OBJ_FILES = $(ejsscriptsrcdir)/ejsrpc.o - [MODULE::smbcalls_config] OUTPUT_TYPE = MERGED_OBJ SUBSYSTEM = smbcalls @@ -17,14 +13,6 @@ PRIVATE_DEPENDENCIES = LIBLDB SAMDB LIBNDR smbcalls_ldb_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_ldb.o -[MODULE::smbcalls_reg] -SUBSYSTEM = smbcalls -OUTPUT_TYPE = MERGED_OBJ -INIT_FUNCTION = smb_setup_ejs_reg -PRIVATE_DEPENDENCIES = registry SAMDB LIBNDR - -smbcalls_reg_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_reg.o - [MODULE::smbcalls_nbt] SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ @@ -39,14 +27,6 @@ INIT_FUNCTION = smb_setup_ejs_random smbcalls_rand_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_rand.o -[MODULE::smbcalls_nss] -SUBSYSTEM = smbcalls -OUTPUT_TYPE = MERGED_OBJ -INIT_FUNCTION = smb_setup_ejs_nss -PRIVATE_DEPENDENCIES = NSS_WRAPPER - -smbcalls_nss_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_nss.o - [MODULE::smbcalls_data] SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ @@ -83,16 +63,13 @@ mkinclude ejsnet/config.mk [SUBSYSTEM::smbcalls] PRIVATE_DEPENDENCIES = \ EJS LIBSAMBA-UTIL \ - EJSRPC MESSAGING \ + MESSAGING \ LIBSAMBA-NET LIBCLI_SMB LIBPOPT \ CREDENTIALS POPT_CREDENTIALS POPT_SAMBA \ - dcerpc \ NDR_TABLE smbcalls_OBJ_FILES = $(addprefix $(ejsscriptsrcdir)/, \ smbcalls.o \ - smbcalls_cli.o \ - smbcalls_rpc.o \ smbcalls_options.o \ smbcalls_creds.o \ smbcalls_param.o \ diff --git a/source4/scripting/ejs/ejsrpc.c b/source4/scripting/ejs/ejsrpc.c deleted file mode 100644 index c7c02dbd64..0000000000 --- a/source4/scripting/ejs/ejsrpc.c +++ /dev/null @@ -1,424 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide interfaces to rpc calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - - 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 "lib/appweb/ejs/ejs.h" -#include "scripting/ejs/smbcalls.h" -#include "librpc/gen_ndr/lsa.h" -#include "librpc/gen_ndr/winreg.h" -#include "scripting/ejs/ejsrpc.h" -#include "libcli/security/security.h" - -/* - set the switch var to be used by the next union switch -*/ -void ejs_set_switch(struct ejs_rpc *ejs, uint32_t switch_var) -{ - ejs->switch_var = switch_var; -} - -/* - panic in the ejs wrapper code - */ -NTSTATUS ejs_panic(struct ejs_rpc *ejs, const char *why) -{ - ejsSetErrorMsg(ejs->eid, "rpc_call '%s' failed - %s", ejs->callname, why); - return NT_STATUS_INTERNAL_ERROR; -} - -/* - start the ejs pull process for a structure -*/ -NTSTATUS ejs_pull_struct_start(struct ejs_rpc *ejs, struct MprVar **v, const char *name) -{ - return mprGetVar(v, name); -} - - -/* - start the ejs push process for a structure -*/ -NTSTATUS ejs_push_struct_start(struct ejs_rpc *ejs, struct MprVar **v, const char *name) -{ - EJS_CHECK(mprSetVar(*v, name, mprObject(name))); - return mprGetVar(v, name); -} - -/* - pull a uint8 from a mpr variable to a C element -*/ -NTSTATUS ejs_pull_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint8_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; - -} - -NTSTATUS ejs_push_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint8_t *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - -/* - pull a uint16 from a mpr variable to a C element -*/ -NTSTATUS ejs_pull_uint16(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint16_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; - -} - -NTSTATUS ejs_push_uint16(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint16_t *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - -/* - pull a uint32 from a mpr variable to a C element -*/ -NTSTATUS ejs_pull_uint32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint32_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_uint32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint32_t *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - -/* - pull a int32 from a mpr variable to a C element -*/ -NTSTATUS ejs_pull_int32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, int32_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_int32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const int32_t *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - -/* - pull a uint32 from a mpr variable to a C element -*/ -NTSTATUS ejs_pull_time_t(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, time_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_time_t(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const time_t *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - -NTSTATUS ejs_pull_hyper(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToNumber(v); - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_hyper(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r) -{ - return mprSetVar(v, name, mprCreateNumberVar(*r)); -} - -NTSTATUS ejs_pull_dlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, int64_t *r) -{ - return ejs_pull_hyper(ejs, v, name, (uint64_t *)r); -} - -NTSTATUS ejs_push_dlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const int64_t *r) -{ - return ejs_push_hyper(ejs, v, name, (const uint64_t *)r); -} - -NTSTATUS ejs_pull_udlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r) -{ - return ejs_pull_hyper(ejs, v, name, r); -} - -NTSTATUS ejs_push_udlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r) -{ - return ejs_push_hyper(ejs, v, name, r); -} - -NTSTATUS ejs_pull_NTTIME(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r) -{ - return ejs_pull_hyper(ejs, v, name, r); -} - -NTSTATUS ejs_push_NTTIME(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r) -{ - return ejs_push_hyper(ejs, v, name, r); -} - -NTSTATUS ejs_push_WERROR(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const WERROR *r) -{ - return ejs_push_string(ejs, v, name, win_errstr(*r)); -} - -NTSTATUS ejs_push_NTSTATUS(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const NTSTATUS *r) -{ - return ejs_push_string(ejs, v, name, nt_errstr(*r)); -} - - -/* - pull a enum from a mpr variable to a C element - a enum is just treating as an unsigned integer at this level -*/ -NTSTATUS ejs_pull_enum(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, unsigned *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToInteger(v); - return NT_STATUS_OK; - -} - -NTSTATUS ejs_push_enum(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const unsigned *r) -{ - return mprSetVar(v, name, mprCreateIntegerVar(*r)); -} - - -/* - pull a string -*/ -NTSTATUS ejs_pull_string(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char **s) -{ - EJS_CHECK(mprGetVar(&v, name)); - *s = mprToString(v); - return NT_STATUS_OK; -} - -/* - push a string -*/ -NTSTATUS ejs_push_string(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char *s) -{ - return mprSetVar(v, name, mprString(s)); -} - - -/* - pull a ipv4address (internally a string) -*/ -NTSTATUS ejs_pull_ipv4address(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char **s) -{ - return ejs_pull_string(ejs, v, name, s); -} - -/* - push a ipv4address (internally a string) -*/ -NTSTATUS ejs_push_ipv4address(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char *s) -{ - return ejs_push_string(ejs, v, name, s); -} - - -NTSTATUS ejs_pull_dom_sid(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct dom_sid *r) -{ - struct dom_sid *sid; - EJS_CHECK(mprGetVar(&v, name)); - sid = dom_sid_parse_talloc(ejs, mprToString(v)); - NT_STATUS_HAVE_NO_MEMORY(sid); - *r = *sid; - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_dom_sid(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct dom_sid *r) -{ - char *sidstr = dom_sid_string(ejs, r); - NT_STATUS_HAVE_NO_MEMORY(sidstr); - return mprSetVar(v, name, mprString(sidstr)); -} - -NTSTATUS ejs_pull_GUID(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct GUID *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - return GUID_from_string(mprToString(v), r); -} - -NTSTATUS ejs_push_GUID(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct GUID *r) -{ - char *guid = GUID_string(ejs, r); - NT_STATUS_HAVE_NO_MEMORY(guid); - return mprSetVar(v, name, mprString(guid)); -} - -NTSTATUS ejs_push_null(struct ejs_rpc *ejs, struct MprVar *v, const char *name) -{ - return mprSetVar(v, name, mprCreatePtrVar(NULL)); -} - -bool ejs_pull_null(struct ejs_rpc *ejs, struct MprVar *v, const char *name) -{ - NTSTATUS status = mprGetVar(&v, name); - if (!NT_STATUS_IS_OK(status)) { - return false; - } - if (v->type == MPR_TYPE_PTR && v->ptr == NULL) { - return true; - } - return false; -} - -/* - pull a lsa_String -*/ -NTSTATUS ejs_pull_lsa_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct lsa_String *r) -{ - return ejs_pull_string(ejs, v, name, &r->string); -} - -/* - push a lsa_String -*/ -NTSTATUS ejs_push_lsa_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct lsa_String *r) -{ - return ejs_push_string(ejs, v, name, r->string); -} - -/* - pull a winreg_String -*/ -NTSTATUS ejs_pull_winreg_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct winreg_String *r) -{ - return ejs_pull_string(ejs, v, name, &r->name); -} - -/* - push a winreg_String -*/ -NTSTATUS ejs_push_winreg_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct winreg_String *r) -{ - return ejs_push_string(ejs, v, name, r->name); -} - -NTSTATUS ejs_pull_DATA_BLOB(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, DATA_BLOB *r) -{ - return NT_STATUS_NOT_IMPLEMENTED; -} - -NTSTATUS ejs_push_DATA_BLOB(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, - const DATA_BLOB *r) -{ - return NT_STATUS_NOT_IMPLEMENTED; -} - -NTSTATUS ejs_pull_bool(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, bool *r) -{ - EJS_CHECK(mprGetVar(&v, name)); - *r = mprVarToBool(v); - return NT_STATUS_OK; -} - -NTSTATUS ejs_push_bool(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const bool *r) -{ - return mprSetVar(v, name, mprCreateBoolVar(*r)); -} - - -/* - pull a uint8 array from a mpr variable to a C element - treating as a data blob -*/ -NTSTATUS ejs_pull_array_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, - uint8_t *r, uint32_t length) -{ - NTSTATUS status; - DATA_BLOB *blob; - - status = mprGetVar(&v, name); - NT_STATUS_NOT_OK_RETURN(status); - - blob = mprToDataBlob(v); - if (blob == NULL) { - return NT_STATUS_OBJECT_NAME_INVALID; - } - if (blob->length != length) { - return NT_STATUS_INFO_LENGTH_MISMATCH; - } - memcpy(r, blob->data, length); - return NT_STATUS_OK; - -} - -NTSTATUS ejs_push_array_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, - const uint8_t *r, uint32_t length) -{ - DATA_BLOB blob; - blob.data = (uint8_t *)discard_const(r); - blob.length = length; - mprSetVar(v, name, mprDataBlob(blob)); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/ejsrpc.h b/source4/scripting/ejs/ejsrpc.h deleted file mode 100644 index cc22a26700..0000000000 --- a/source4/scripting/ejs/ejsrpc.h +++ /dev/null @@ -1,172 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - ejs <-> rpc interface definitions - - Copyright (C) Andrew Tridgell 2005 - - 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 "librpc/gen_ndr/security.h" -#include "librpc/rpc/dcerpc.h" - -struct ejs_rpc { - int eid; - const char *callname; - /* as ejs does only one pass, we can use a single var for switch - handling */ - uint32_t switch_var; -}; - -typedef NTSTATUS (*ejs_pull_t)(struct ejs_rpc *, struct MprVar *, const char *, void *); -typedef NTSTATUS (*ejs_push_t)(struct ejs_rpc *, struct MprVar *, const char *, const void *); -typedef NTSTATUS (*ejs_pull_function_t)(struct ejs_rpc *, struct MprVar *, void *); -typedef NTSTATUS (*ejs_push_function_t)(struct ejs_rpc *, struct MprVar *, const void *); - -NTSTATUS ejs_panic(struct ejs_rpc *ejs, const char *why); -void ejs_set_switch(struct ejs_rpc *ejs, uint32_t switch_var); - -NTSTATUS smbcalls_register_ejs(const char *name, MprCFunction fn); - - -int ejs_rpc_call(int eid, int argc, struct MprVar **argv, - const struct ndr_interface_table *iface, int callnum, - ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push); - -NTSTATUS ejs_pull_struct_start(struct ejs_rpc *ejs, struct MprVar **v, const char *name); -NTSTATUS ejs_push_struct_start(struct ejs_rpc *ejs, struct MprVar **v, const char *name); - -NTSTATUS ejs_pull_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint8_t *r); -NTSTATUS ejs_push_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint8_t *r); -NTSTATUS ejs_pull_uint16(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint16_t *r); -NTSTATUS ejs_push_uint16(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint16_t *r); -NTSTATUS ejs_pull_uint32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint32_t *r); -NTSTATUS ejs_push_int32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const int32_t *r); -NTSTATUS ejs_pull_int32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, int32_t *r); -NTSTATUS ejs_push_uint32(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint32_t *r); -NTSTATUS ejs_pull_hyper(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r); -NTSTATUS ejs_push_hyper(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r); -NTSTATUS ejs_pull_dlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, int64_t *r); -NTSTATUS ejs_push_dlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const int64_t *r); -NTSTATUS ejs_pull_udlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r); -NTSTATUS ejs_push_udlong(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r); -NTSTATUS ejs_pull_NTTIME(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, uint64_t *r); -NTSTATUS ejs_push_NTTIME(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const uint64_t *r); -NTSTATUS ejs_pull_time_t(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, time_t *r); -NTSTATUS ejs_push_time_t(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const time_t *r); -NTSTATUS ejs_pull_enum(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, unsigned *r); -NTSTATUS ejs_push_enum(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const unsigned *r); -NTSTATUS ejs_pull_string(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char **s); -NTSTATUS ejs_push_string(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char *s); -NTSTATUS ejs_pull_ipv4address(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char **s); -NTSTATUS ejs_push_ipv4address(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const char *s); -void ejs_set_constant_int(int eid, const char *name, int value); -void ejs_set_constant_string(int eid, const char *name, const char *value); - -NTSTATUS ejs_pull_dom_sid(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct dom_sid *r); -NTSTATUS ejs_push_dom_sid(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct dom_sid *r); -NTSTATUS ejs_push_null(struct ejs_rpc *ejs, struct MprVar *v, const char *name); -bool ejs_pull_null(struct ejs_rpc *ejs, struct MprVar *v, const char *name); -NTSTATUS ejs_push_WERROR(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const WERROR *r); -NTSTATUS ejs_push_NTSTATUS(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const NTSTATUS *r); -NTSTATUS ejs_pull_DATA_BLOB(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, DATA_BLOB *r); -NTSTATUS ejs_push_DATA_BLOB(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const DATA_BLOB *r); -NTSTATUS ejs_pull_bool(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, bool *r); -NTSTATUS ejs_push_bool(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const bool *r); - -NTSTATUS ejs_pull_array_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, - uint8_t *r, uint32_t length); -NTSTATUS ejs_push_array_uint8(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, - const uint8_t *r, uint32_t length); - -NTSTATUS ejs_pull_GUID(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct GUID *r); -NTSTATUS ejs_push_GUID(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct GUID *r); - -struct lsa_String; -NTSTATUS ejs_pull_lsa_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct lsa_String *r); -NTSTATUS ejs_push_lsa_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct lsa_String *r); - -struct winreg_String; -NTSTATUS ejs_pull_winreg_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, struct winreg_String *r); -NTSTATUS ejs_push_winreg_String(struct ejs_rpc *ejs, - struct MprVar *v, const char *name, const struct winreg_String *r); - -#define EJS_ALLOC(ejs, s) do { \ - (s) = talloc_ptrtype(ejs, (s)); \ - if (!(s)) return ejs_panic(ejs, "out of memory"); \ -} while (0) - -#define EJS_ALLOC_N(ejs, s, n) do { \ - (s) = talloc_array_ptrtype(ejs, (s), n); \ - if (!(s)) return ejs_panic(ejs, "out of memory"); \ -} while (0) - -#define EJS_CHECK(cmd) do { \ - NTSTATUS _status; \ - _status = cmd; \ - if (!NT_STATUS_IS_OK(_status)) { \ - return _status; \ - } \ -} while (0) - -/* some types are equivalent for ejs */ -#define ejs_pull_dom_sid2 ejs_pull_dom_sid -#define ejs_push_dom_sid2 ejs_push_dom_sid -#define ejs_pull_dom_sid28 ejs_pull_dom_sid -#define ejs_push_dom_sid28 ejs_push_dom_sid -#define ejs_pull_NTTIME_hyper ejs_pull_NTTIME -#define ejs_push_NTTIME_hyper ejs_push_NTTIME -#define ejs_pull_NTTIME_1sec ejs_pull_NTTIME -#define ejs_push_NTTIME_1sec ejs_push_NTTIME - diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 98d6be07bf..bdf70ca831 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -203,7 +203,6 @@ void smb_setup_ejs_functions(void (*exception_handler)(const char *)) ejs_exception_handler = exception_handler; - smb_setup_ejs_cli(); smb_setup_ejs_options(); smb_setup_ejs_credentials(); smb_setup_ejs_param(); diff --git a/source4/scripting/ejs/smbcalls_cli.c b/source4/scripting/ejs/smbcalls_cli.c deleted file mode 100644 index dbb36312da..0000000000 --- a/source4/scripting/ejs/smbcalls_cli.c +++ /dev/null @@ -1,722 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Tim Potter 2005 - - 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 "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "libcli/raw/libcliraw.h" -#include "libcli/composite/composite.h" -#include "libcli/smb_composite/smb_composite.h" -#include "libcli/libcli.h" -#include "libcli/resolve/resolve.h" -#include "auth/credentials/credentials.h" -#include "param/param.h" - -#if 0 - -#include "librpc/gen_ndr/ndr_nbt.h" - -/* Connect to a server */ - -static int ejs_cli_connect(MprVarHandle eid, int argc, char **argv) -{ - struct smbcli_socket *sock; - struct smbcli_transport *transport; - struct nbt_name calling, called; - NTSTATUS result; - - if (argc != 1) { - ejsSetErrorMsg(eid, "connect invalid arguments"); - return -1; - } - - /* Socket connect */ - - sock = smbcli_sock_init(NULL, NULL); - - if (!sock) { - ejsSetErrorMsg(eid, "socket initialisation failed"); - return -1; - } - - if (!smbcli_sock_connect_byname(sock, argv[0], 0)) { - ejsSetErrorMsg(eid, "socket connect failed"); - return -1; - } - - transport = smbcli_transport_init(sock, sock, false); - - if (!transport) { - ejsSetErrorMsg(eid, "transport init failed"); - return -1; - } - - /* Send a netbios session request */ - - make_nbt_name_client(&calling, lp_netbios_name()); - - nbt_choose_called_name(NULL, &called, argv[0], NBT_NAME_SERVER); - - if (!smbcli_transport_connect(transport, &calling, &called)) { - ejsSetErrorMsg(eid, "transport establishment failed"); - return -1; - } - - result = smb_raw_negotiate(transport, lp_maxprotocol()); - - if (!NT_STATUS_IS_OK(result)) { - mpr_Return(eid, mprNTSTATUS(result)); - return 0; - } - - /* Return a socket object */ - - mpr_Return(eid, mprCreatePtrVar(transport)); - - return 0; -} - -/* Perform a session setup: - - session_setup(conn, "DOMAIN\\USERNAME%PASSWORD"); - session_setup(conn, USERNAME, PASSWORD); - session_setup(conn, DOMAIN, USERNAME, PASSWORD); - session_setup(conn); // anonymous - - */ - -static int ejs_cli_ssetup(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_transport *transport; - struct smbcli_session *session; - struct smb_composite_sesssetup setup; - struct cli_credentials *creds; - NTSTATUS status; - int result = -1; - - /* Argument parsing */ - - if (argc < 1 || argc > 4) { - ejsSetErrorMsg(eid, "session_setup invalid arguments"); - return -1; - } - - if (!mprVarIsPtr(argv[0]->type)) { - ejsSetErrorMsg(eid, "first arg is not a connect handle"); - return -1; - } - - transport = argv[0]->ptr; - creds = cli_credentials_init(transport); - cli_credentials_set_conf(creds); - - if (argc == 4) { - - /* DOMAIN, USERNAME, PASSWORD form */ - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 1 must be a string"); - goto done; - } - - cli_credentials_set_domain(creds, argv[1]->string, - CRED_SPECIFIED); - - if (!mprVarIsString(argv[2]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - goto done; - } - - cli_credentials_set_username(creds, argv[2]->string, - CRED_SPECIFIED); - - if (!mprVarIsString(argv[3]->type)) { - ejsSetErrorMsg(eid, "arg 3 must be a string"); - goto done; - } - - cli_credentials_set_password(creds, argv[3]->string, - CRED_SPECIFIED); - - } else if (argc == 3) { - - /* USERNAME, PASSWORD form */ - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg1 must be a string"); - goto done; - } - - cli_credentials_set_username(creds, argv[1]->string, - CRED_SPECIFIED); - - if (!mprVarIsString(argv[2]->type)) { - - ejsSetErrorMsg(eid, "arg2 must be a string"); - goto done; - } - - cli_credentials_set_password(creds, argv[2]->string, - CRED_SPECIFIED); - - } else if (argc == 2) { - - /* DOMAIN/USERNAME%PASSWORD form */ - - cli_credentials_parse_string(creds, argv[1]->string, - CRED_SPECIFIED); - - } else { - - /* Anonymous connection */ - - cli_credentials_set_anonymous(creds); - } - - /* Do session setup */ - - session = smbcli_session_init(transport, transport, false); - - if (!session) { - ejsSetErrorMsg(eid, "session init failed"); - return -1; - } - - setup.in.sesskey = transport->negotiate.sesskey; - setup.in.capabilities = transport->negotiate.capabilities; - setup.in.credentials = creds; - setup.in.workgroup = lp_workgroup(); - - status = smb_composite_sesssetup(session, &setup); - - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "session_setup: %s", nt_errstr(status)); - return -1; - } - - session->vuid = setup.out.vuid; - - /* Return a session object */ - - mpr_Return(eid, mprCreatePtrVar(session)); - - result = 0; - - done: - talloc_free(creds); - return result; -} - -/* Perform a tree connect - - tree_connect(session, SHARE); - - */ - -static int ejs_cli_tree_connect(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_session *session; - struct smbcli_tree *tree; - union smb_tcon tcon; - TALLOC_CTX *mem_ctx; - NTSTATUS status; - const char *password = ""; - - /* Argument parsing */ - - if (argc != 2) { - ejsSetErrorMsg(eid, "tree_connect invalid arguments"); - return -1; - } - - if (!mprVarIsPtr(argv[0]->type)) { - ejsSetErrorMsg(eid, "first arg is not a session handle"); - return -1; - } - - session = argv[0]->ptr; - tree = smbcli_tree_init(session, session, false); - - if (!tree) { - ejsSetErrorMsg(eid, "tree init failed"); - return -1; - } - - mem_ctx = talloc_init("tcon"); - if (!mem_ctx) { - ejsSetErrorMsg(eid, "talloc_init failed"); - return -1; - } - - /* Do tree connect */ - - tcon.generic.level = RAW_TCON_TCONX; - tcon.tconx.in.flags = 0; - - if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) { - tcon.tconx.in.password = data_blob(NULL, 0); - } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) { - tcon.tconx.in.password = data_blob_talloc(mem_ctx, NULL, 24); - if (session->transport->negotiate.secblob.length < 8) { - ejsSetErrorMsg(eid, "invalid security blob"); - return -1; - } - SMBencrypt(password, session->transport->negotiate.secblob.data, tcon.tconx.in.password.data); - } else { - tcon.tconx.in.password = data_blob_talloc(mem_ctx, password, strlen(password)+1); - } - - tcon.tconx.in.path = argv[1]->string; - tcon.tconx.in.device = "?????"; - - status = smb_tree_connect(tree, mem_ctx, &tcon); - - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "tree_connect: %s", nt_errstr(status)); - return -1; - } - - tree->tid = tcon.tconx.out.tid; - - talloc_free(mem_ctx); - - mpr_Return(eid, mprCreatePtrVar(tree)); - - return 0; -} - -/* Perform a tree disconnect - - tree_disconnect(tree); - - */ -static int ejs_cli_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS status; - - /* Argument parsing */ - - if (argc != 1) { - ejsSetErrorMsg(eid, "tree_disconnect invalid arguments"); - return -1; - } - - if (!mprVarIsPtr(argv[0]->type)) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = argv[0]->ptr; - - status = smb_tree_disconnect(tree); - - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "tree_disconnect: %s", nt_errstr(status)); - return -1; - } - - talloc_free(tree); - - return 0; -} - -/* Perform a ulogoff - - session_logoff(session); - - */ -static int ejs_cli_session_logoff(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_session *session; - NTSTATUS status; - - /* Argument parsing */ - - if (argc != 1) { - ejsSetErrorMsg(eid, "session_logoff invalid arguments"); - return -1; - } - - if (!mprVarIsPtr(argv[0]->type)) { - ejsSetErrorMsg(eid, "first arg is not a session handle"); - return -1; - } - - session = argv[0]->ptr; - - status = smb_raw_ulogoff(session); - - if (!NT_STATUS_IS_OK(status)) { - ejsSetErrorMsg(eid, "session_logoff: %s", nt_errstr(status)); - return -1; - } - - talloc_free(session); - - return 0; -} - -/* Perform a connection close - - disconnect(conn); - - */ -static int ejs_cli_disconnect(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_sock *sock; - - /* Argument parsing */ - - if (argc != 1) { - ejsSetErrorMsg(eid, "disconnect invalid arguments"); - return -1; - } - - if (!mprVarIsPtr(argv[0]->type)) { - ejsSetErrorMsg(eid, "first arg is not a connect handle"); - return -1; - } - - sock = argv[0]->ptr; - - talloc_free(sock); - - return 0; -} - -#endif - -/* Perform a tree connect: - - tree_handle = tree_connect("\\\\frogurt\\homes", "user%pass"); - */ - -static int ejs_tree_connect(MprVarHandle eid, int argc, char **argv) -{ - struct cli_credentials *creds; - struct smb_composite_connect io; - struct smbcli_tree *tree; - char *hostname, *sharename; - NTSTATUS result; - TALLOC_CTX *mem_ctx; - - if (argc != 2) { - ejsSetErrorMsg(eid, "tree_connect(): invalid number of args"); - return -1; - } - - /* Set up host, share destination */ - - mem_ctx = talloc_new(mprMemCtx()); - smbcli_parse_unc(argv[0], mem_ctx, &hostname, &sharename); - - /* Set up credentials */ - - creds = cli_credentials_init(NULL); - cli_credentials_set_conf(creds, mprLpCtx()); - cli_credentials_parse_string(creds, argv[1], CRED_SPECIFIED); - - /* Do connect */ - - io.in.dest_host = hostname; - io.in.dest_ports = lp_smb_ports(mprLpCtx()); - io.in.called_name = strupper_talloc(mem_ctx, hostname); - io.in.service = sharename; - io.in.service_type = "?????"; - io.in.credentials = creds; - io.in.fallback_to_anonymous = false; - io.in.workgroup = lp_workgroup(mprLpCtx()); - lp_smbcli_options(mprLpCtx(), &io.in.options); - - result = smb_composite_connect(&io, mem_ctx, - lp_resolve_context(mprLpCtx()), - NULL); - tree = io.out.tree; - - talloc_free(mem_ctx); - - if (!NT_STATUS_IS_OK(result)) { - mpr_Return(eid, mprNTSTATUS(result)); - return 0; - } - - mpr_Return(eid, mprCreatePtrVar(tree)); - - return 0; -} - -#define IS_TREE_HANDLE(x) (mprVarIsPtr((x)->type) && \ - talloc_check_name((x)->ptr, "struct smbcli_tree")) - -/* Perform a tree disconnect: - - tree_disconnect(tree_handle); - */ - -static int ejs_tree_disconnect(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS result; - - if (argc != 1) { - ejsSetErrorMsg(eid, - "tree_disconnect(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = talloc_get_type(argv[0]->ptr, struct smbcli_tree); - - result = smb_tree_disconnect(tree); - - mpr_Return(eid, mprNTSTATUS(result)); - - return 0; -} - -/* Create a directory: - - result = mkdir(tree_handle, DIRNAME); - */ - -static int ejs_mkdir(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS result; - - if (argc != 2) { - ejsSetErrorMsg(eid, "mkdir(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = (struct smbcli_tree *)argv[0]->ptr; - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - return -1; - } - - result = smbcli_mkdir(tree, argv[1]->string); - - mpr_Return(eid, mprNTSTATUS(result)); - - return 0; -} - -/* Remove a directory: - - result = rmdir(tree_handle, DIRNAME); - */ - -static int ejs_rmdir(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS result; - - if (argc != 2) { - ejsSetErrorMsg(eid, "rmdir(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = (struct smbcli_tree *)argv[0]->ptr; - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - return -1; - } - - result = smbcli_rmdir(tree, argv[1]->string); - - mpr_Return(eid, mprNTSTATUS(result)); - - return 0; -} - -/* Rename a file or directory: - - result = rename(tree_handle, SRCFILE, DESTFILE); - */ - -static int ejs_rename(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS result; - - if (argc != 3) { - ejsSetErrorMsg(eid, "rename(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = (struct smbcli_tree *)argv[0]->ptr; - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - return -1; - } - - if (!mprVarIsString(argv[2]->type)) { - ejsSetErrorMsg(eid, "arg 3 must be a string"); - return -1; - } - - result = smbcli_rename(tree, argv[1]->string, argv[2]->string); - - mpr_Return(eid, mprNTSTATUS(result)); - - return 0; -} - -/* Unlink a file or directory: - - result = unlink(tree_handle, FILENAME); - */ - -static int ejs_unlink(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - NTSTATUS result; - - if (argc != 2) { - ejsSetErrorMsg(eid, "unlink(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = (struct smbcli_tree *)argv[0]->ptr; - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - return -1; - } - - result = smbcli_unlink(tree, argv[1]->string); - - mpr_Return(eid, mprNTSTATUS(result)); - - return 0; -} - -/* List directory contents - - result = list(tree_handle, ARG1, ...); - */ - -static void ejs_list_helper(struct clilist_file_info *info, const char *mask, - void *state) - -{ - MprVar *result = (MprVar *)state; - char idx[16]; - - mprItoa(result->properties->numDataItems, idx, sizeof(idx)); - mprSetVar(result, idx, mprString(info->name)); -} - -static int ejs_list(MprVarHandle eid, int argc, MprVar **argv) -{ - struct smbcli_tree *tree; - char *mask; - uint16_t attribute; - MprVar result; - - if (argc != 3) { - ejsSetErrorMsg(eid, "list(): invalid number of args"); - return -1; - } - - if (!IS_TREE_HANDLE(argv[0])) { - ejsSetErrorMsg(eid, "first arg is not a tree handle"); - return -1; - } - - tree = (struct smbcli_tree *)argv[0]->ptr; - - if (!mprVarIsString(argv[1]->type)) { - ejsSetErrorMsg(eid, "arg 2 must be a string"); - return -1; - } - - mask = argv[1]->string; - - if (!mprVarIsNumber(argv[2]->type)) { - ejsSetErrorMsg(eid, "arg 3 must be a number"); - return -1; - } - - attribute = mprVarToInteger(argv[2]); - - result = mprObject("list"); - - smbcli_list(tree, mask, attribute, ejs_list_helper, &result); - - mpr_Return(eid, result); - - return 0; -} - -/* - setup C functions that be called from ejs -*/ -void smb_setup_ejs_cli(void) -{ - ejsDefineStringCFunction(-1, "tree_connect", ejs_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "tree_disconnect", ejs_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); - - ejsDefineCFunction(-1, "mkdir", ejs_mkdir, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "rmdir", ejs_rmdir, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "rename", ejs_rename, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "unlink", ejs_unlink, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "list", ejs_list, NULL, MPR_VAR_SCRIPT_HANDLE); - - -#if 0 - ejsDefineStringCFunction(-1, "connect", ejs_cli_connect, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "session_setup", ejs_cli_ssetup, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "tree_connect", ejs_cli_tree_connect, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "tree_disconnect", ejs_cli_tree_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "session_logoff", ejs_cli_session_logoff, NULL, MPR_VAR_SCRIPT_HANDLE); - ejsDefineCFunction(-1, "disconnect", ejs_cli_disconnect, NULL, MPR_VAR_SCRIPT_HANDLE); -#endif -} diff --git a/source4/scripting/ejs/smbcalls_nss.c b/source4/scripting/ejs/smbcalls_nss.c deleted file mode 100644 index 58f86d3b6c..0000000000 --- a/source4/scripting/ejs/smbcalls_nss.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide access to getpwnam() and related calls - - Copyright (C) Andrew Tridgell 2005 - - 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 "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "system/passwd.h" - - -/* - return a struct passwd as an object -*/ -static struct MprVar mpr_passwd(struct passwd *pwd) -{ - struct MprVar ret; - if (pwd == NULL) { - return mprCreateUndefinedVar(); - } - ret = mprObject("passwd"); - - mprSetVar(&ret, "pw_name", mprString(pwd->pw_name)); - mprSetVar(&ret, "pw_passwd", mprString(pwd->pw_passwd)); - mprSetVar(&ret, "pw_uid", mprCreateIntegerVar(pwd->pw_uid)); - mprSetVar(&ret, "pw_gid", mprCreateIntegerVar(pwd->pw_gid)); - mprSetVar(&ret, "pw_gecos", mprString(pwd->pw_gecos)); - mprSetVar(&ret, "pw_dir", mprString(pwd->pw_dir)); - mprSetVar(&ret, "pw_shell", mprString(pwd->pw_shell)); - return ret; -} - -/* - return a struct passwd as an object -*/ -static struct MprVar mpr_group(struct group *grp) -{ - struct MprVar ret; - if (grp == NULL) { - return mprCreateUndefinedVar(); - } - ret = mprObject("group"); - - mprSetVar(&ret, "gr_name", mprString(grp->gr_name)); - mprSetVar(&ret, "gr_passwd", mprString(grp->gr_passwd)); - mprSetVar(&ret, "gr_gid", mprCreateIntegerVar(grp->gr_gid)); - mprSetVar(&ret, "gr_mem", mprList("gr_mem", (const char **)grp->gr_mem)); - return ret; -} - - -/* - usage: - var pw = nss.getpwnam("root"); - - returns an object containing struct passwd entries -*/ -static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv) -{ - /* validate arguments */ - if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "getpwnam invalid arguments"); - return -1; - } - - mpr_Return(eid, mpr_passwd(getpwnam(mprToString(argv[0])))); - return 0; -} - -/* - usage: - var pw = nss.getpwuid(0); - - returns an object containing struct passwd entries -*/ -static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv) -{ - /* validate arguments */ - if (argc != 1 || !mprVarIsNumber(argv[0]->type)) { - ejsSetErrorMsg(eid, "getpwuid invalid arguments"); - return -1; - } - mpr_Return(eid, mpr_passwd(getpwuid(mprToInt(argv[0])))); - return 0; -} - -/* - usage: - var pw = nss.getgrnam("users"); - - returns an object containing struct group entries -*/ -static int ejs_getgrnam(MprVarHandle eid, int argc, struct MprVar **argv) -{ - /* validate arguments */ - if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "getgrnam invalid arguments"); - return -1; - } - mpr_Return(eid, mpr_group(getgrnam(mprToString(argv[0])))); - return 0; -} - -/* - usage: - var pw = nss.getgrgid(0); - - returns an object containing struct group entries -*/ -static int ejs_getgrgid(MprVarHandle eid, int argc, struct MprVar **argv) -{ - /* validate arguments */ - if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) { - ejsSetErrorMsg(eid, "getgrgid invalid arguments"); - return -1; - } - mpr_Return(eid, mpr_group(getgrgid(mprToInt(argv[0])))); - return 0; -} - - -/* - initialise nss ejs subsystem -*/ -static int ejs_nss_init(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *nss = mprInitObject(eid, "nss", argc, argv); - - mprSetCFunction(nss, "getpwnam", ejs_getpwnam); - mprSetCFunction(nss, "getpwuid", ejs_getpwuid); - mprSetCFunction(nss, "getgrnam", ejs_getgrnam); - mprSetCFunction(nss, "getgrgid", ejs_getgrgid); - - return 0; -} - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_nss(void) -{ - ejsDefineCFunction(-1, "nss_init", ejs_nss_init, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbcalls_reg.c b/source4/scripting/ejs/smbcalls_reg.c deleted file mode 100644 index ed8653d3a7..0000000000 --- a/source4/scripting/ejs/smbcalls_reg.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide hooks into smbd C calls from ejs scripts - - Copyright (C) Jelmer Vernooij 2007 - - 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 "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "dsdb/samdb/samdb.h" -#include "librpc/ndr/libndr.h" -#include "lib/registry/registry.h" - -/* - get the connected db - */ -static struct registry_context *ejs_get_reg_context(int eid) -{ - struct registry_context *rctx = (struct registry_context *)mprGetThisPtr(eid, "registry"); - if (rctx == NULL) { - ejsSetErrorMsg(eid, "unable to find registry"); - } - return rctx; -} - -static int ejs_apply_patchfile(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct registry_context *rctx; - WERROR error; - - /* validate arguments */ - if (argc != 1) { - ejsSetErrorMsg(eid, "reg.apply_patchfile invalid number of arguments"); - return -1; - } - - rctx = ejs_get_reg_context(eid); - if (rctx == NULL) { - return -1; - } - - error = reg_diff_apply(rctx, mprToString(argv[0])); - - mpr_Return(eid, mprWERROR(error)); - - return 0; -} - -/* - initialise registry ejs subsystem -*/ -static int ejs_reg_open(MprVarHandle eid, int argc, struct MprVar **argv) -{ - struct MprVar *reg = mprInitObject(eid, "registry", argc, argv); - struct registry_context *rctx; - WERROR error; - - error = reg_open_samba(mprMemCtx(), &rctx, mprEventCtx(), mprLpCtx(), NULL, NULL); - SMB_ASSERT(W_ERROR_IS_OK(error)); - - mprSetPtrChild(reg, "registry", rctx); - mprSetCFunction(reg, "apply_patchfile", ejs_apply_patchfile); - - return 0; -} - - -/* - setup C functions that be called from ejs -*/ -NTSTATUS smb_setup_ejs_reg(void) -{ - ejsDefineCFunction(-1, "reg_open", ejs_reg_open, NULL, MPR_VAR_SCRIPT_HANDLE); - return NT_STATUS_OK; -} diff --git a/source4/scripting/ejs/smbcalls_rpc.c b/source4/scripting/ejs/smbcalls_rpc.c deleted file mode 100644 index 94774d708b..0000000000 --- a/source4/scripting/ejs/smbcalls_rpc.c +++ /dev/null @@ -1,385 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - provide interfaces to rpc calls from ejs scripts - - Copyright (C) Andrew Tridgell 2005 - - 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 "scripting/ejs/smbcalls.h" -#include "lib/appweb/ejs/ejs.h" -#include "librpc/gen_ndr/echo.h" -#include "lib/cmdline/popt_common.h" -#include "lib/messaging/irpc.h" -#include "scripting/ejs/ejsrpc.h" -#include "lib/util/dlinklist.h" -#include "lib/events/events.h" -#include "librpc/ndr/ndr_table.h" -#include "auth/credentials/credentials.h" -#include "librpc/rpc/dcerpc.h" -#include "cluster/cluster.h" - -/* - state of a irpc 'connection' -*/ -struct ejs_irpc_connection { - const char *server_name; - struct server_id *dest_ids; - struct messaging_context *msg_ctx; -}; - -/* - messaging clients need server IDs as well ... - */ -#define EJS_ID_BASE 0x30000000 - -/* - setup a context for talking to a irpc server - example: - status = irpc.connect("smb_server"); -*/ -static int ejs_irpc_connect(MprVarHandle eid, int argc, char **argv) -{ - NTSTATUS status; - int i; - struct event_context *ev; - struct ejs_irpc_connection *p; - struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); - - /* validate arguments */ - if (argc != 1) { - ejsSetErrorMsg(eid, "rpc_connect invalid arguments"); - return -1; - } - - p = talloc(this, struct ejs_irpc_connection); - if (p == NULL) { - return -1; - } - - p->server_name = argv[0]; - - ev = mprEventCtx(); - - /* create a messaging context, looping as we have no way to - allocate temporary server ids automatically */ - for (i=0;i<10000;i++) { - p->msg_ctx = messaging_init(p, - lp_messaging_path(p, mprLpCtx()), - cluster_id(EJS_ID_BASE, i), - lp_iconv_convenience(mprLpCtx()), - ev); - if (p->msg_ctx) break; - } - if (p->msg_ctx == NULL) { - ejsSetErrorMsg(eid, "irpc_connect unable to create a messaging context"); - talloc_free(p); - return -1; - } - - p->dest_ids = irpc_servers_byname(p->msg_ctx, p, p->server_name); - if (p->dest_ids == NULL || p->dest_ids[0].id == 0) { - talloc_free(p); - status = NT_STATUS_OBJECT_NAME_NOT_FOUND; - } else { - mprSetPtrChild(this, "irpc", p); - status = NT_STATUS_OK; - } - - mpr_Return(eid, mprNTSTATUS(status)); - return 0; -} - - -/* - connect to an rpc server - examples: - status = rpc.connect("ncacn_ip_tcp:localhost"); - status = rpc.connect("ncacn_ip_tcp:localhost", "pipe_name"); -*/ -static int ejs_rpc_connect(MprVarHandle eid, int argc, char **argv) -{ - const char *binding, *pipe_name; - const struct ndr_interface_table *iface; - NTSTATUS status; - struct dcerpc_pipe *p; - struct cli_credentials *creds; - struct event_context *ev; - struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); - struct MprVar *credentials; - - /* validate arguments */ - if (argc < 1) { - ejsSetErrorMsg(eid, "rpc_connect invalid arguments"); - return -1; - } - - binding = argv[0]; - if (strchr(binding, ':') == NULL) { - /* its an irpc connect */ - return ejs_irpc_connect(eid, argc, argv); - } - - if (argc > 1) { - pipe_name = argv[1]; - } else { - pipe_name = mprToString(mprGetProperty(this, "pipe_name", NULL)); - } - - iface = ndr_table_by_name(pipe_name); - if (iface == NULL) { - status = NT_STATUS_OBJECT_NAME_INVALID; - goto done; - } - - credentials = mprGetProperty(this, "credentials", NULL); - if (credentials) { - creds = (struct cli_credentials *) - mprGetPtr(credentials, "creds"); - } else { - creds = cmdline_credentials; - } - if (creds == NULL) { - creds = cli_credentials_init(mprMemCtx()); - cli_credentials_set_anonymous(creds); - } - - ev = mprEventCtx(); - - status = dcerpc_pipe_connect(this, &p, binding, iface, creds, ev, - mprLpCtx()); - if (!NT_STATUS_IS_OK(status)) goto done; - - /* callers don't allocate ref vars in the ejs interface */ - p->conn->flags |= DCERPC_NDR_REF_ALLOC; - - /* by making the pipe a child of the connection variable, it will - auto close when it goes out of scope in the script */ - mprSetPtrChild(this, "pipe", p); - -done: - mpr_Return(eid, mprNTSTATUS(status)); - return 0; -} - - -/* - make an irpc call - called via the same interface as rpc -*/ -static int ejs_irpc_call(int eid, struct MprVar *io, - const struct ndr_interface_table *iface, int callnum, - ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push) -{ - NTSTATUS status; - void *ptr; - struct ejs_rpc *ejs; - const struct ndr_interface_call *call; - struct ejs_irpc_connection *p; - struct irpc_request **reqs; - int i, count; - struct MprVar *results; - - p = (struct ejs_irpc_connection *)mprGetThisPtr(eid, "irpc"); - - ejs = talloc(mprMemCtx(), struct ejs_rpc); - if (ejs == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - call = &iface->calls[callnum]; - - ejs->eid = eid; - ejs->callname = call->name; - - /* allocate the C structure */ - ptr = talloc_zero_size(ejs, call->struct_size); - if (ptr == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - /* convert the mpr object into a C structure */ - status = ejs_pull(ejs, io, ptr); - if (!NT_STATUS_IS_OK(status)) { - goto done; - } - - for (count=0;p->dest_ids[count].id;count++) /* noop */ ; - - /* we need to make a call per server */ - reqs = talloc_array(ejs, struct irpc_request *, count); - if (reqs == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - /* make the actual calls */ - for (i=0;i<count;i++) { - reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i], - iface, callnum, ptr, ptr); - if (reqs[i] == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - talloc_steal(reqs, reqs[i]); - } - - mprSetVar(io, "results", mprObject("results")); - results = mprGetProperty(io, "results", NULL); - - /* and receive the results, placing them in io.results[i] */ - for (i=0;i<count;i++) { - struct MprVar *output; - - status = irpc_call_recv(reqs[i]); - if (!NT_STATUS_IS_OK(status)) { - goto done; - } - status = ejs_push(ejs, io, ptr); - if (!NT_STATUS_IS_OK(status)) { - goto done; - } - - /* add to the results array */ - output = mprGetProperty(io, "output", NULL); - if (output) { - char idx[16]; - mprItoa(i, idx, sizeof(idx)); - mprSetProperty(results, idx, output); - mprDeleteProperty(io, "output"); - } - } - mprSetVar(results, "length", mprCreateIntegerVar(i)); - -done: - talloc_free(ejs); - mpr_Return(eid, mprNTSTATUS(status)); - if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) { - return -1; - } - return 0; -} - - -/* - backend code for making an rpc call - this is called from the pidl generated ejs - code -*/ - int ejs_rpc_call(int eid, int argc, struct MprVar **argv, - const struct ndr_interface_table *iface, int callnum, - ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push) -{ - struct MprVar *io; - struct dcerpc_pipe *p; - NTSTATUS status; - void *ptr; - struct rpc_request *req; - struct ejs_rpc *ejs; - const struct ndr_interface_call *call; - - if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) { - ejsSetErrorMsg(eid, "rpc_call invalid arguments"); - return -1; - } - - io = argv[0]; - - if (mprGetThisPtr(eid, "irpc")) { - /* its an irpc call */ - return ejs_irpc_call(eid, io, iface, callnum, ejs_pull, ejs_push); - } - - /* get the pipe info */ - p = mprGetThisPtr(eid, "pipe"); - if (p == NULL) { - ejsSetErrorMsg(eid, "rpc_call invalid pipe"); - return -1; - } - - ejs = talloc(mprMemCtx(), struct ejs_rpc); - if (ejs == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - call = &iface->calls[callnum]; - - ejs->eid = eid; - ejs->callname = call->name; - - /* allocate the C structure */ - ptr = talloc_zero_size(ejs, call->struct_size); - if (ptr == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - /* convert the mpr object into a C structure */ - status = ejs_pull(ejs, io, ptr); - if (!NT_STATUS_IS_OK(status)) { - goto done; - } - - /* make the actual call */ - req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr); - - /* if requested, print the structure */ - if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) { - ndr_print_function_debug(call->ndr_print, call->name, NDR_IN, ptr); - } - - if (req == NULL) { - status = NT_STATUS_NO_MEMORY; - goto done; - } - - status = dcerpc_ndr_request_recv(req); - if (!NT_STATUS_IS_OK(status)) { - goto done; - } - - /* print the 'out' structure, if needed */ - if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) { - ndr_print_function_debug(call->ndr_print, call->name, NDR_OUT, ptr); - } - - status = ejs_push(ejs, io, ptr); - -done: - talloc_free(ejs); - mpr_Return(eid, mprNTSTATUS(status)); - if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) { - return -1; - } - return 0; -} - -/* - hook called by generated RPC interfaces at the end of their init routines - used to add generic operations on the pipe -*/ -int ejs_rpc_init(struct MprVar *obj, const char *name) -{ - ndr_table_init(); - - mprSetStringCFunction(obj, "connect", ejs_rpc_connect); - if (mprGetProperty(obj, "pipe_name", NULL) == NULL) { - mprSetVar(obj, "pipe_name", mprString(name)); - } - return 0; -} diff --git a/source4/scripting/libjs/auth.js b/source4/scripting/libjs/auth.js deleted file mode 100644 index 3fe81d0ea7..0000000000 --- a/source4/scripting/libjs/auth.js +++ /dev/null @@ -1,18 +0,0 @@ -/* - auth js library functions - Copyright Andrew Tridgell 2005 - released under the GNU GPL version 3 or later -*/ - - -/* - get a list of domains for SWAT authentication -*/ -function getDomainList() -{ - var ret = new Array(2); - var lp = loadparm_init(); - ret[0] = "System User"; - ret[1] = lp.get("workgroup"); - return ret; -} diff --git a/source4/scripting/libjs/management.js b/source4/scripting/libjs/management.js deleted file mode 100644 index 4a43275156..0000000000 --- a/source4/scripting/libjs/management.js +++ /dev/null @@ -1,157 +0,0 @@ -/* - backend code for Samba4 management - Copyright Andrew Tridgell 2005 - Released under the GNU GPL version 3 or later -*/ - - -/* - return a list of current sessions -*/ -function smbsrv_sessions() -{ - var irpc = irpc_init(); - status = irpc.connect("smb_server"); - if (status.is_ok != true) { - return undefined; - } - - var io = irpcObj(); - io.input.level = irpc.SMBSRV_INFO_SESSIONS; - status = irpc.smbsrv_information(io); - if (status.is_ok != true) { - return undefined; - } - - /* gather the results into a single array */ - var i, count=0, ret = new Array(0); - for (i=0;i<io.results.length;i++) { - var sessions = io.results[i].info.sessions.sessions; - var j; - for (j=0;j<sessions.length;j++) { - ret[count] = sessions[j]; - count++; - } - } - return ret; -} - -/* - return a list of current tree connects -*/ -function smbsrv_tcons() -{ - var irpc = irpc_init(); - status = irpc.connect("smb_server"); - if (status.is_ok != true) { - return undefined; - } - - var io = irpcObj(); - io.input.level = irpc.SMBSRV_INFO_TCONS; - status = irpc.smbsrv_information(io); - if (status.is_ok != true) { - return undefined; - } - - /* gather the results into a single array */ - var i, count=0, ret = new Object(); - for (i=0;i<io.results.length;i++) { - var tcons = io.results[i].info.tcons.tcons; - var j; - for (j=0;j<tcons.length;j++) { - ret[count] = tcons[j]; - count++; - } - } - ret.length = count; - return ret; -} - -/* - return nbtd statistics -*/ -function nbtd_statistics() -{ - var irpc = irpc_init(); - status = irpc.connect("nbt_server"); - if (status.is_ok != true) { - return undefined; - } - - var io = irpcObj(); - io.input.level = irpc.NBTD_INFO_STATISTICS; - status = irpc.nbtd_information(io); - if (status.is_ok != true) { - return undefined; - } - return io.results[0].info.stats; -} - -/* - see if a service is enabled -*/ -function service_enabled(name) -{ - var lp = loadparm_init(); - var services = lp.get("server services"); - var i; - for (i=0;i<services.length;i++) { - if (services[i] == name) { - return true; - } - } - return false; -} - -/* - show status of a server -*/ -function server_status(name) -{ - var i; - var io; - var irpc = irpc_init(); - - if (!service_enabled(name)) { - return "DISABLED"; - } - - status = irpc.connect(name + "_server"); - if (status.is_ok != true) { - return "DOWN"; - } - - var io = irpcObj(); - status = irpc.irpc_uptime(io); - if (status.is_ok != true) { - return "NOT RESPONDING"; - } - - return "RUNNING"; -} - -/* - show status of a stream server -*/ -function stream_server_status(name) -{ - var irpc = irpc_init(); - - if (!service_enabled(name)) { - return "DISABLED"; - } - status = irpc.connect(name + "_server"); - if (status.is_ok != true) { - return "0 connections"; - } - - var io = irpcObj(); - status = irpc.irpc_uptime(io); - if (status.is_ok != true) { - return "NOT RESPONDING"; - } - - var n = io.results.length; - return sprintf("%u connection%s", n, plural(n)); -} diff --git a/source4/scripting/bin/samr.py b/source4/scripting/python/examples/samr.py index e91b5bc312..1f2afbe688 100755 --- a/source4/scripting/bin/samr.py +++ b/source4/scripting/python/examples/samr.py @@ -100,7 +100,7 @@ if len(sys.argv) != 2: binding = sys.argv[1] -print "Connecting to " + binding +print "Connecting to %s" % binding try: samr = samr.samr(binding) except Exception, e: diff --git a/source4/scripting/bin/winreg.py b/source4/scripting/python/examples/winreg.py index 19d39e56ab..80b48ecfd7 100755 --- a/source4/scripting/bin/winreg.py +++ b/source4/scripting/python/examples/winreg.py @@ -11,7 +11,7 @@ import sys # Find right directory when running from source tree sys.path.insert(0, "bin/python") -import winreg +from samba.dcerpc import winreg import optparse import samba.getopt as options diff --git a/source4/selftest/samba4_tests.sh b/source4/selftest/samba4_tests.sh index 2076ed9c65..11d909a2bc 100755 --- a/source4/selftest/samba4_tests.sh +++ b/source4/selftest/samba4_tests.sh @@ -344,6 +344,7 @@ plantest "dcerpc.bare.python" dc $SUBUNITRUN samba.tests.dcerpc.bare plantest "samdb.python" dc $SUBUNITRUN samba.tests.samdb plantest "unixinfo.python" dc $SUBUNITRUN samba.tests.dcerpc.unix plantest "events.python" none PYTHONPATH="$PYTHONPATH:lib/events" $SUBUNITRUN tests +plantest "messaging.python" none PYTHONPATH="$PYTHONPATH:lib/messaging/tests" $SUBUNITRUN bindings plantest "samba3sam.python" none PYTHONPATH="$PYTHONPATH:dsdb/samdb/ldb_modules/tests" $SUBUNITRUN samba3sam plantest "rpcecho.python" dc $SUBUNITRUN samba.tests.dcerpc.rpcecho plantest "winreg.python" dc $SUBUNITRUN -U\$USERNAME%\$PASSWORD samba.tests.dcerpc.registry diff --git a/source4/smb_server/smb_server.c b/source4/smb_server/smb_server.c index 367557dbb7..6eccb836d6 100644 --- a/source4/smb_server/smb_server.c +++ b/source4/smb_server/smb_server.c @@ -150,12 +150,12 @@ static void smbsrv_accept(struct stream_connection *conn) smb_conn->connection = conn; conn->private = smb_conn; - irpc_add_name(conn->msg_ctx, "smb_server"); - smb_conn->statistics.connect_time = timeval_current(); smbsrv_management_init(smb_conn); + irpc_add_name(conn->msg_ctx, "smb_server"); + if (!NT_STATUS_IS_OK(share_get_context_by_name(smb_conn, lp_share_backend(smb_conn->lp_ctx), smb_conn->connection->event.ctx, smb_conn->lp_ctx, &(smb_conn->share_context)))) { diff --git a/source4/web_server/swat/__init__.py b/source4/web_server/swat/__init__.py new file mode 100644 index 0000000000..e0d85dbe2c --- /dev/null +++ b/source4/web_server/swat/__init__.py @@ -0,0 +1,27 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008 +# +# Implementation of SWAT that uses WSGI +# +# 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/>. +# + +def SWAT(environ, start_response): + status = '200 OK' + response_headers = [('Content-type','text/plain')] + start_response(status, response_headers) + return ['Hello world!\n'] + |