r8073: a successful rpc call from ejs!
authorAndrew Tridgell <tridge@samba.org>
Sat, 2 Jul 2005 11:12:33 +0000 (11:12 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:19:05 +0000 (13:19 -0500)
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)

source4/librpc/rpc/dcerpc_util.c
source4/scripting/ejs/config.mk
source4/scripting/ejs/ejs_echo.c [new file with mode: 0644]
source4/scripting/ejs/ejsrpc.c [new file with mode: 0644]
source4/scripting/ejs/ejsrpc.h [new file with mode: 0644]
source4/scripting/ejs/mprutil.c
source4/scripting/ejs/smbcalls_rpc.c

index 49b7c32a8abe8fddc2ff18ff63d84f1d48dd99fd..bc771c73f8dde4a1466b94da6b40664ff3100e6f 100644 (file)
@@ -85,6 +85,20 @@ const struct dcerpc_interface_table *idl_iface_by_uuid(const char *uuid)
        return NULL;
 }
 
+/*
+  find a dcerpc call on an interface by name
+*/
+const struct dcerpc_interface_call *dcerpc_iface_find_call(const struct dcerpc_interface_table *iface,
+                                                          const char *name)
+{
+       int i;
+       for (i=0;i<iface->num_calls;i++) {
+               if (strcmp(iface->calls[i].name, name) == 0) {
+                       return &iface->calls[i];
+               }
+       }
+       return NULL;
+}
 
 /* 
    push a ncacn_packet into a blob, potentially with auth info
index d5003d07e7563a322c0a718c49dd4dbf930768cb..0ce7f2df831fbdcffe2c5a566c884ba1c85ed617 100644 (file)
@@ -1,3 +1,14 @@
+#######################
+# 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]
@@ -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 (file)
index 0000000..d24ddfa
--- /dev/null
@@ -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 (file)
index 0000000..bc5fdd6
--- /dev/null
@@ -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 (file)
index 0000000..b2d491d
--- /dev/null
@@ -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);
+
index cd5ec3b5cd50e4cbf9172a5c14192175286fc0c5..1c640a5d5e3fe51367781eea220e6396b64f5872 100644 (file)
@@ -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);
 }
 
index 22aa2b59f888855d3683a2e1eba9665a265d1788..ec47da0e9c2b8750aa94aa84c9c279b2c2ab7a63 100644 (file)
@@ -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);
 }
+
+