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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "librpc/gen_ndr/echo.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "lib/messaging/irpc.h"
28 #include "scripting/ejs/ejsrpc.h"
29 #include "lib/util/dlinklist.h"
30 #include "lib/events/events.h"
31 #include "librpc/ndr/ndr_table.h"
32 #include "auth/credentials/credentials.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "cluster/cluster.h"
37 state of a irpc 'connection'
39 struct ejs_irpc_connection {
40 const char *server_name;
41 struct server_id *dest_ids;
42 struct messaging_context *msg_ctx;
46 messaging clients need server IDs as well ...
48 #define EJS_ID_BASE 0x30000000
51 setup a context for talking to a irpc server
53 status = irpc.connect("smb_server");
55 static int ejs_irpc_connect(MprVarHandle eid, int argc, char **argv)
59 struct event_context *ev;
60 struct ejs_irpc_connection *p;
61 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
63 /* validate arguments */
65 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
69 p = talloc(this, struct ejs_irpc_connection);
74 p->server_name = argv[0];
76 ev = event_context_find(p);
78 /* create a messaging context, looping as we have no way to
79 allocate temporary server ids automatically */
80 for (i=0;i<10000;i++) {
81 p->msg_ctx = messaging_init(p,
82 lp_messaging_path(p, global_loadparm),
83 cluster_id(EJS_ID_BASE + i), ev);
84 if (p->msg_ctx) break;
86 if (p->msg_ctx == NULL) {
87 ejsSetErrorMsg(eid, "irpc_connect unable to create a messaging context");
92 p->dest_ids = irpc_servers_byname(p->msg_ctx, p, p->server_name);
93 if (p->dest_ids == NULL || p->dest_ids[0].id == 0) {
95 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
97 mprSetPtrChild(this, "irpc", p);
98 status = NT_STATUS_OK;
101 mpr_Return(eid, mprNTSTATUS(status));
107 connect to an rpc server
109 status = rpc.connect("ncacn_ip_tcp:localhost");
110 status = rpc.connect("ncacn_ip_tcp:localhost", "pipe_name");
112 static int ejs_rpc_connect(MprVarHandle eid, int argc, char **argv)
114 const char *binding, *pipe_name;
115 const struct ndr_interface_table *iface;
117 struct dcerpc_pipe *p;
118 struct cli_credentials *creds;
119 struct event_context *ev;
120 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
121 struct MprVar *credentials;
123 /* validate arguments */
125 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
130 if (strchr(binding, ':') == NULL) {
131 /* its an irpc connect */
132 return ejs_irpc_connect(eid, argc, argv);
138 pipe_name = mprToString(mprGetProperty(this, "pipe_name", NULL));
141 iface = ndr_table_by_name(pipe_name);
143 status = NT_STATUS_OBJECT_NAME_INVALID;
147 credentials = mprGetProperty(this, "credentials", NULL);
149 creds = (struct cli_credentials *)
150 mprGetPtr(credentials, "creds");
152 creds = cmdline_credentials;
155 creds = cli_credentials_init(mprMemCtx());
156 cli_credentials_set_anonymous(creds);
159 ev = event_context_find(mprMemCtx());
161 status = dcerpc_pipe_connect(this, &p, binding, iface, creds, ev);
162 if (!NT_STATUS_IS_OK(status)) goto done;
164 /* callers don't allocate ref vars in the ejs interface */
165 p->conn->flags |= DCERPC_NDR_REF_ALLOC;
167 /* by making the pipe a child of the connection variable, it will
168 auto close when it goes out of scope in the script */
169 mprSetPtrChild(this, "pipe", p);
172 mpr_Return(eid, mprNTSTATUS(status));
178 make an irpc call - called via the same interface as rpc
180 static int ejs_irpc_call(int eid, struct MprVar *io,
181 const struct ndr_interface_table *iface, int callnum,
182 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
187 const struct ndr_interface_call *call;
188 struct ejs_irpc_connection *p;
189 struct irpc_request **reqs;
191 struct MprVar *results;
193 p = (struct ejs_irpc_connection *)mprGetThisPtr(eid, "irpc");
195 ejs = talloc(mprMemCtx(), struct ejs_rpc);
197 status = NT_STATUS_NO_MEMORY;
201 call = &iface->calls[callnum];
204 ejs->callname = call->name;
206 /* allocate the C structure */
207 ptr = talloc_zero_size(ejs, call->struct_size);
209 status = NT_STATUS_NO_MEMORY;
213 /* convert the mpr object into a C structure */
214 status = ejs_pull(ejs, io, ptr);
215 if (!NT_STATUS_IS_OK(status)) {
219 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
221 /* we need to make a call per server */
222 reqs = talloc_array(ejs, struct irpc_request *, count);
224 status = NT_STATUS_NO_MEMORY;
228 /* make the actual calls */
229 for (i=0;i<count;i++) {
230 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
231 iface, callnum, ptr, ptr);
232 if (reqs[i] == NULL) {
233 status = NT_STATUS_NO_MEMORY;
236 talloc_steal(reqs, reqs[i]);
239 mprSetVar(io, "results", mprObject("results"));
240 results = mprGetProperty(io, "results", NULL);
242 /* and receive the results, placing them in io.results[i] */
243 for (i=0;i<count;i++) {
244 struct MprVar *output;
246 status = irpc_call_recv(reqs[i]);
247 if (!NT_STATUS_IS_OK(status)) {
250 status = ejs_push(ejs, io, ptr);
251 if (!NT_STATUS_IS_OK(status)) {
255 /* add to the results array */
256 output = mprGetProperty(io, "output", NULL);
259 mprItoa(i, idx, sizeof(idx));
260 mprSetProperty(results, idx, output);
261 mprDeleteProperty(io, "output");
264 mprSetVar(results, "length", mprCreateIntegerVar(i));
268 mpr_Return(eid, mprNTSTATUS(status));
269 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
277 backend code for making an rpc call - this is called from the pidl generated ejs
280 int ejs_rpc_call(int eid, int argc, struct MprVar **argv,
281 const struct ndr_interface_table *iface, int callnum,
282 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
285 struct dcerpc_pipe *p;
288 struct rpc_request *req;
290 const struct ndr_interface_call *call;
292 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
293 ejsSetErrorMsg(eid, "rpc_call invalid arguments");
299 if (mprGetThisPtr(eid, "irpc")) {
300 /* its an irpc call */
301 return ejs_irpc_call(eid, io, iface, callnum, ejs_pull, ejs_push);
304 /* get the pipe info */
305 p = mprGetThisPtr(eid, "pipe");
307 ejsSetErrorMsg(eid, "rpc_call invalid pipe");
311 ejs = talloc(mprMemCtx(), struct ejs_rpc);
313 status = NT_STATUS_NO_MEMORY;
317 call = &iface->calls[callnum];
320 ejs->callname = call->name;
322 /* allocate the C structure */
323 ptr = talloc_zero_size(ejs, call->struct_size);
325 status = NT_STATUS_NO_MEMORY;
329 /* convert the mpr object into a C structure */
330 status = ejs_pull(ejs, io, ptr);
331 if (!NT_STATUS_IS_OK(status)) {
335 /* make the actual call */
336 req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
338 /* if requested, print the structure */
339 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
340 ndr_print_function_debug(call->ndr_print, call->name, NDR_IN, ptr);
344 status = NT_STATUS_NO_MEMORY;
348 status = dcerpc_ndr_request_recv(req);
349 if (!NT_STATUS_IS_OK(status)) {
353 /* print the 'out' structure, if needed */
354 if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
355 ndr_print_function_debug(call->ndr_print, call->name, NDR_OUT, ptr);
358 status = ejs_push(ejs, io, ptr);
362 mpr_Return(eid, mprNTSTATUS(status));
363 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
370 hook called by generated RPC interfaces at the end of their init routines
371 used to add generic operations on the pipe
373 int ejs_rpc_init(struct MprVar *obj, const char *name)
377 mprSetStringCFunction(obj, "connect", ejs_rpc_connect);
378 if (mprGetProperty(obj, "pipe_name", NULL) == NULL) {
379 mprSetVar(obj, "pipe_name", mprString(name));