2 Unix SMB/CIFS implementation.
4 provide interfaces to rpc calls from ejs scripts
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/ejs/ejs.h"
25 #include "librpc/gen_ndr/ndr_echo.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "scripting/ejs/ejsrpc.h"
30 connect to an rpc server
32 var conn = new Object();
33 status = rpc_connect(conn, "ncacn_ip_tcp:localhost", "rpcecho");
35 static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv)
37 const char *binding, *pipe_name;
38 const struct dcerpc_interface_table *iface;
40 struct dcerpc_pipe *p;
43 /* validate arguments */
45 argv[0]->type != MPR_TYPE_OBJECT ||
46 argv[1]->type != MPR_TYPE_STRING ||
47 argv[2]->type != MPR_TYPE_STRING) {
48 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
53 binding = mprToString(argv[1]);
54 pipe_name = mprToString(argv[2]);
56 iface = idl_iface_by_name(pipe_name);
58 status = NT_STATUS_OBJECT_NAME_INVALID;
62 status = dcerpc_pipe_connect(mprMemCtx(), &p, binding,
63 iface->uuid, iface->if_version,
64 cmdline_credentials, NULL);
65 if (!NT_STATUS_IS_OK(status)) goto done;
67 /* callers don't allocate ref vars in the ejs interface */
68 p->conn->flags |= DCERPC_NDR_REF_ALLOC;
70 mprSetPtr(conn, "pipe", p);
71 mprSetPtr(conn, "iface", iface);
74 ejsSetReturnValue(eid, mprNTSTATUS(status));
82 status = rpc_call(conn, "echo_AddOne", io);
84 int ejs_rpc_call(int eid, int argc, struct MprVar **argv,
86 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
88 struct MprVar *conn, *io;
89 const struct dcerpc_interface_table *iface;
90 struct dcerpc_pipe *p;
91 const struct dcerpc_interface_call *call;
94 struct rpc_request *req;
98 argv[0]->type != MPR_TYPE_OBJECT ||
99 argv[1]->type != MPR_TYPE_OBJECT) {
100 ejsSetErrorMsg(eid, "rpc_call invalid arguments");
107 /* get the pipe info */
108 p = mprGetPtr(conn, "pipe");
109 iface = mprGetPtr(conn, "iface");
110 if (p == NULL || iface == NULL) {
111 ejsSetErrorMsg(eid, "rpc_call invalid pipe");
115 /* find the call by name */
116 call = dcerpc_iface_find_call(iface, callname);
118 status = NT_STATUS_OBJECT_NAME_INVALID;
121 callnum = call - iface->calls;
123 /* allocate the C structure */
124 ptr = talloc_zero_size(mprMemCtx(), call->struct_size);
126 status = NT_STATUS_NO_MEMORY;
130 /* convert the mpr object into a C structure */
131 status = ejs_pull_rpc(eid, callname, io, ptr, ejs_pull);
132 if (!NT_STATUS_IS_OK(status)) {
136 /* if requested, print the structure */
137 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
138 ndr_print_function_debug(call->ndr_print, call->name, NDR_IN, ptr);
141 /* make the actual call */
142 req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
144 status = NT_STATUS_NO_MEMORY;
148 status = dcerpc_ndr_request_recv(req);
150 /* print the 'out' structure, if needed */
151 if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
152 ndr_print_function_debug(call->ndr_print, call->name, NDR_OUT, ptr);
155 status = ejs_push_rpc(eid, callname, io, ptr, ejs_push);
159 ejsSetReturnValue(eid, mprNTSTATUS(status));
160 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
168 setup C functions that be called from ejs
170 void smb_setup_ejs_rpc(void)
172 void setup_ejs_rpcecho(void);
173 ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE);