summaryrefslogtreecommitdiff
path: root/source4/scripting
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-07-02 11:12:33 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:19:05 -0500
commit9331714787f663a1b46447598a7edf0ebff7d355 (patch)
treed203ba0c341e121f28717c45ce868a02ea1b4ac8 /source4/scripting
parent75883a2237f52863df2eff1fe9d9ee8408b40feb (diff)
downloadsamba-9331714787f663a1b46447598a7edf0ebff7d355.tar.gz
samba-9331714787f663a1b46447598a7edf0ebff7d355.tar.bz2
samba-9331714787f663a1b46447598a7edf0ebff7d355.zip
r8073: a successful rpc call from ejs!
the ejs_echo.c code is the stuff that needs to be auto-generated by pidl. It only does echo_AddOne so far. We also need a table for registering these calls. The code is hard-wired for echo_AddOne for now. (This used to be commit b1ea58ddc482c373783d16331dd07378010ba39a)
Diffstat (limited to 'source4/scripting')
-rw-r--r--source4/scripting/ejs/config.mk13
-rw-r--r--source4/scripting/ejs/ejs_echo.c45
-rw-r--r--source4/scripting/ejs/ejsrpc.c118
-rw-r--r--source4/scripting/ejs/ejsrpc.h48
-rw-r--r--source4/scripting/ejs/mprutil.c4
-rw-r--r--source4/scripting/ejs/smbcalls_rpc.c77
6 files changed, 292 insertions, 13 deletions
diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk
index d5003d07e7..0ce7f2df83 100644
--- a/source4/scripting/ejs/config.mk
+++ b/source4/scripting/ejs/config.mk
@@ -1,4 +1,15 @@
#######################
+# Start LIBRARY EJSRPC
+[SUBSYSTEM::EJSRPC]
+OBJ_FILES = \
+ scripting/ejs/ejsrpc.o \
+ scripting/ejs/ejs_echo.o
+REQUIRED_SUBSYSTEMS = RPC_NDR_ECHO
+NOPROTO = YES
+# End SUBSYSTEM EJSRPC
+#######################
+
+#######################
# Start LIBRARY SMBCALLS
[SUBSYSTEM::SMBCALLS]
OBJ_FILES = \
@@ -9,7 +20,7 @@ OBJ_FILES = \
scripting/ejs/smbcalls_cli.o \
scripting/ejs/smbcalls_rpc.o \
scripting/ejs/mprutil.o
-REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC RPC_NDR_ECHO
+REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC RPC_NDR_ECHO EJSRPC
# End SUBSYSTEM SMBCALLS
#######################
diff --git a/source4/scripting/ejs/ejs_echo.c b/source4/scripting/ejs/ejs_echo.c
new file mode 100644
index 0000000000..d24ddfa110
--- /dev/null
+++ b/source4/scripting/ejs/ejs_echo.c
@@ -0,0 +1,45 @@
+/*
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "lib/ejs/ejs.h"
+#include "scripting/ejs/ejsrpc.h"
+#include "librpc/gen_ndr/ndr_echo.h"
+
+/*
+ this should be auto-generated by pidl, but isn't yet
+*/
+NTSTATUS ejs_pull_echo_AddOne(struct ejs_rpc *ejs,
+ struct MprVar *v, struct echo_AddOne *r)
+{
+ NDR_CHECK(ejs_pull_uint32(ejs, v, "in.in_data", &r->in.in_data));
+ return NT_STATUS_OK;
+}
+
+
+NTSTATUS ejs_push_echo_AddOne(struct ejs_rpc *ejs,
+ struct MprVar *v,
+ const struct echo_AddOne *r)
+{
+ NDR_CHECK(ejs_push_uint32(ejs, v, "out.out_data", *r->out.out_data));
+ return NT_STATUS_OK;
+}
diff --git a/source4/scripting/ejs/ejsrpc.c b/source4/scripting/ejs/ejsrpc.c
new file mode 100644
index 0000000000..bc5fdd647f
--- /dev/null
+++ b/source4/scripting/ejs/ejsrpc.c
@@ -0,0 +1,118 @@
+/*
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "lib/ejs/ejs.h"
+#include "scripting/ejs/ejsrpc.h"
+
+NTSTATUS ejs_pull_rpc(struct MprVar *v, void *ptr, ejs_pull_function_t ejs_pull)
+{
+ struct ejs_rpc *ejs = talloc(ptr, struct ejs_rpc);
+ return ejs_pull(ejs, v, ptr);
+}
+
+
+NTSTATUS ejs_push_rpc(struct MprVar *v, const void *ptr, ejs_push_function_t ejs_push)
+{
+ struct ejs_rpc *ejs = talloc(ptr, struct ejs_rpc);
+ return ejs_push(ejs, v, ptr);
+}
+
+
+/*
+ find a mpr component, allowing for sub objects, using the '.' convention
+*/
+static struct MprVar *mprGetVar(struct MprVar *v, const char *name)
+{
+ const char *p = strchr(name, '.');
+ char *objname;
+ struct MprVar *v2;
+ if (p == NULL) {
+ return mprGetProperty(v, name, NULL);
+ }
+ objname = talloc_strndup(mprMemCtx(), name, p-name);
+ if (objname == NULL) {
+ return NULL;
+ }
+ v2 = mprGetProperty(v, objname, NULL);
+ if (v2 == NULL) {
+ talloc_free(objname);
+ return NULL;
+ }
+ v2 = mprGetVar(v2, p+1);
+ talloc_free(objname);
+ return v2;
+}
+
+
+/*
+ set a mpr component, allowing for sub objects, using the '.' convention
+*/
+static NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val)
+{
+ const char *p = strchr(name, '.');
+ char *objname;
+ struct MprVar *v2;
+ NTSTATUS status;
+ if (p == NULL) {
+ v2 = mprSetProperty(v, name, &val);
+ if (v2 == NULL) {
+ return NT_STATUS_INVALID_PARAMETER_MIX;
+ }
+ return NT_STATUS_OK;
+ }
+ objname = talloc_strndup(mprMemCtx(), name, p-name);
+ if (objname == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+ v2 = mprGetProperty(v, objname, NULL);
+ if (v2 == NULL) {
+ struct MprVar val2 = mprCreateObjVar(objname, MPR_DEFAULT_HASH_SIZE);
+ v2 = mprCreateProperty(v, objname, &val2);
+ }
+ status = mprSetVar(v2, p+1, val);
+ talloc_free(objname);
+ return status;
+}
+
+
+/*
+ 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)
+{
+ struct MprVar *var;
+ var = mprGetVar(v, name);
+ if (var == NULL) {
+ return NT_STATUS_INVALID_PARAMETER_MIX;
+ }
+ *r = mprVarToInteger(var);
+ return NT_STATUS_OK;
+
+}
+
+NTSTATUS ejs_push_uint32(struct ejs_rpc *ejs,
+ struct MprVar *v, const char *name, uint32_t r)
+{
+ return mprSetVar(v, name, mprCreateIntegerVar(r));
+}
diff --git a/source4/scripting/ejs/ejsrpc.h b/source4/scripting/ejs/ejsrpc.h
new file mode 100644
index 0000000000..b2d491d94d
--- /dev/null
+++ b/source4/scripting/ejs/ejsrpc.h
@@ -0,0 +1,48 @@
+/*
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+struct ejs_rpc {
+
+};
+
+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_pull_rpc(struct MprVar *v, void *ptr, ejs_pull_function_t ejs_pull);
+NTSTATUS ejs_push_rpc(struct MprVar *v, const void *ptr, ejs_push_function_t ejs_push);
+NTSTATUS ejs_pull_uint32(struct ejs_rpc *ejs,
+ struct MprVar *v, const char *name, uint32_t *r);
+NTSTATUS ejs_push_uint32(struct ejs_rpc *ejs,
+ struct MprVar *v, const char *name, uint32_t r);
+
+
+/* echo calls */
+
+#include "librpc/gen_ndr/ndr_echo.h"
+
+NTSTATUS ejs_pull_echo_AddOne(struct ejs_rpc *ejs,
+ struct MprVar *v, struct echo_AddOne *r);
+NTSTATUS ejs_push_echo_AddOne(struct ejs_rpc *ejs,
+ struct MprVar *v,
+ const struct echo_AddOne *r);
+
diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c
index cd5ec3b5cd..1c640a5d5e 100644
--- a/source4/scripting/ejs/mprutil.c
+++ b/source4/scripting/ejs/mprutil.c
@@ -218,9 +218,9 @@ struct MprVar mprWERROR(WERROR status)
/*
set a pointer in a existing MprVar
*/
-void mprSetPtr(struct MprVar *v, const char *propname, void *p)
+void mprSetPtr(struct MprVar *v, const char *propname, const void *p)
{
- struct MprVar val = mprCreatePtrVar(p, talloc_get_name(p));
+ struct MprVar val = mprCreatePtrVar(discard_const(p), NULL);
mprCreateProperty(v, propname, &val);
}
diff --git a/source4/scripting/ejs/smbcalls_rpc.c b/source4/scripting/ejs/smbcalls_rpc.c
index 22aa2b59f8..ec47da0e9c 100644
--- a/source4/scripting/ejs/smbcalls_rpc.c
+++ b/source4/scripting/ejs/smbcalls_rpc.c
@@ -24,6 +24,7 @@
#include "lib/ejs/ejs.h"
#include "librpc/gen_ndr/ndr_echo.h"
#include "lib/cmdline/popt_common.h"
+#include "scripting/ejs/ejsrpc.h"
/*
connect to an rpc server
@@ -61,9 +62,13 @@ static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv)
status = dcerpc_pipe_connect(mprMemCtx(), &p, binding,
iface->uuid, iface->if_version,
cmdline_credentials, NULL);
- if (NT_STATUS_IS_OK(status)) {
- mprSetPtr(conn, "pipe", p);
- }
+ 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;
+
+ mprSetPtr(conn, "pipe", p);
+ mprSetPtr(conn, "iface", iface);
done:
ejsSetReturnValue(eid, mprNTSTATUS(status));
@@ -78,10 +83,15 @@ done:
*/
static int ejs_rpc_call(MprVarHandle eid, int argc, struct MprVar **argv)
{
- struct dcerpc_pipe *p;
struct MprVar *conn, *io;
- const char *call;
+ const struct dcerpc_interface_table *iface;
+ struct dcerpc_pipe *p;
+ const char *callname;
+ const struct dcerpc_interface_call *call;
NTSTATUS status;
+ void *ptr;
+ struct rpc_request *req;
+ int callnum;
if (argc != 3 ||
argv[0]->type != MPR_TYPE_OBJECT ||
@@ -91,17 +101,62 @@ static int ejs_rpc_call(MprVarHandle eid, int argc, struct MprVar **argv)
return -1;
}
- conn = argv[0];
- call = mprToString(argv[1]);
- io = argv[2];
+ conn = argv[0];
+ callname = mprToString(argv[1]);
+ io = argv[2];
+ /* get the pipe info */
p = mprGetPtr(conn, "pipe");
- if (p == NULL) {
+ iface = mprGetPtr(conn, "iface");
+ if (p == NULL || iface == NULL) {
ejsSetErrorMsg(eid, "rpc_call invalid pipe");
return -1;
}
- status = NT_STATUS_NOT_IMPLEMENTED;
+ /* find the call by name */
+ call = dcerpc_iface_find_call(iface, callname);
+ if (call == NULL) {
+ status = NT_STATUS_OBJECT_NAME_INVALID;
+ goto done;
+ }
+ callnum = call - iface->calls;
+
+ /* allocate the C structure */
+ ptr = talloc_zero_size(mprMemCtx(), call->struct_size);
+ if (ptr == NULL) {
+ status = NT_STATUS_NO_MEMORY;
+ goto done;
+ }
+
+ /* convert the mpr object into a C structure */
+ status = ejs_pull_rpc(io, ptr, (ejs_pull_function_t)ejs_pull_echo_AddOne);
+ if (!NT_STATUS_IS_OK(status)) {
+ goto done;
+ }
+
+ /* 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);
+ }
+
+ /* make the actual call */
+ req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
+ if (req == NULL) {
+ status = NT_STATUS_NO_MEMORY;
+ talloc_free(ptr);
+ goto done;
+ }
+ status = dcerpc_ndr_request_recv(req);
+
+ /* 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_rpc(io, ptr, (ejs_push_function_t)ejs_push_echo_AddOne);
+
+ talloc_free(ptr);
+done:
ejsSetReturnValue(eid, mprNTSTATUS(status));
return 0;
}
@@ -114,3 +169,5 @@ void smb_setup_ejs_rpc(void)
ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
ejsDefineCFunction(-1, "rpc_call", ejs_rpc_call, NULL, MPR_VAR_SCRIPT_HANDLE);
}
+
+