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 "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "lib/messaging/irpc.h"
29 #include "scripting/ejs/ejsrpc.h"
30 #include "dlinklist.h"
31 #include "lib/events/events.h"
34 state of a irpc 'connection'
36 struct ejs_irpc_connection {
37 const char *server_name;
39 struct messaging_context *msg_ctx;
43 messaging clients need server IDs as well ...
45 #define EJS_ID_BASE 0x30000000
48 setup a context for talking to a irpc server
50 status = irpc.connect("smb_server");
52 static int ejs_irpc_connect(MprVarHandle eid, int argc, char **argv)
56 struct event_context *ev;
57 struct ejs_irpc_connection *p;
58 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
60 /* validate arguments */
62 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
66 p = talloc(this, struct ejs_irpc_connection);
71 p->server_name = argv[0];
73 ev = event_context_find(p);
75 /* create a messaging context, looping as we have no way to
76 allocate temporary server ids automatically */
77 for (i=0;i<10000;i++) {
78 p->msg_ctx = messaging_init(p, EJS_ID_BASE + i, ev);
79 if (p->msg_ctx) break;
81 if (p->msg_ctx == NULL) {
82 ejsSetErrorMsg(eid, "irpc_connect unable to create a messaging context");
87 p->dest_ids = irpc_servers_byname(p->msg_ctx, p->server_name);
88 if (p->dest_ids == NULL || p->dest_ids[0] == 0) {
90 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
92 mprSetPtrChild(this, "irpc", p);
93 status = NT_STATUS_OK;
96 mpr_Return(eid, mprNTSTATUS(status));
102 connect to an rpc server
104 status = rpc.connect("ncacn_ip_tcp:localhost");
105 status = rpc.connect("ncacn_ip_tcp:localhost", "pipe_name");
107 static int ejs_rpc_connect(MprVarHandle eid, int argc, char **argv)
109 const char *binding, *pipe_name;
110 const struct dcerpc_interface_table *iface;
112 struct dcerpc_pipe *p;
113 struct cli_credentials *creds;
114 struct event_context *ev;
115 struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0);
116 struct MprVar *credentials;
118 /* validate arguments */
120 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
125 if (strchr(binding, ':') == NULL) {
126 /* its an irpc connect */
127 return ejs_irpc_connect(eid, argc, argv);
133 pipe_name = mprToString(mprGetProperty(this, "pipe_name", NULL));
136 iface = idl_iface_by_name(pipe_name);
138 status = NT_STATUS_OBJECT_NAME_INVALID;
142 credentials = mprGetProperty(this, "credentials", NULL);
144 creds = mprGetPtr(credentials, "creds");
146 creds = cmdline_credentials;
149 creds = cli_credentials_init(mprMemCtx());
150 cli_credentials_guess(creds);
151 cli_credentials_set_anonymous(creds);
154 ev = event_context_find(mprMemCtx());
156 status = dcerpc_pipe_connect(this, &p, binding,
157 iface->uuid, iface->if_version,
159 if (!NT_STATUS_IS_OK(status)) goto done;
161 /* callers don't allocate ref vars in the ejs interface */
162 p->conn->flags |= DCERPC_NDR_REF_ALLOC;
164 /* by making the pipe a child of the connection variable, it will
165 auto close when it goes out of scope in the script */
166 mprSetPtrChild(this, "pipe", p);
169 mpr_Return(eid, mprNTSTATUS(status));
175 make an irpc call - called via the same interface as rpc
177 static int ejs_irpc_call(int eid, struct MprVar *io,
178 const struct dcerpc_interface_table *iface, int callnum,
179 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
184 const struct dcerpc_interface_call *call;
185 struct ejs_irpc_connection *p;
186 struct irpc_request **reqs;
188 struct MprVar *results;
190 p = mprGetThisPtr(eid, "irpc");
192 ejs = talloc(mprMemCtx(), struct ejs_rpc);
194 status = NT_STATUS_NO_MEMORY;
198 call = &iface->calls[callnum];
201 ejs->callname = call->name;
203 /* allocate the C structure */
204 ptr = talloc_zero_size(ejs, call->struct_size);
206 status = NT_STATUS_NO_MEMORY;
210 /* convert the mpr object into a C structure */
211 status = ejs_pull(ejs, io, ptr);
212 if (!NT_STATUS_IS_OK(status)) {
216 for (count=0;p->dest_ids[count];count++) /* noop */ ;
218 /* we need to make a call per server */
219 reqs = talloc_array(ejs, struct irpc_request *, count);
221 status = NT_STATUS_NO_MEMORY;
225 /* make the actual calls */
226 for (i=0;i<count;i++) {
227 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
228 iface, callnum, ptr, ptr);
229 if (reqs[i] == NULL) {
230 status = NT_STATUS_NO_MEMORY;
233 talloc_steal(reqs, reqs[i]);
236 mprSetVar(io, "results", mprObject("results"));
237 results = mprGetProperty(io, "results", NULL);
239 /* and receive the results, placing them in io.results[i] */
240 for (i=0;i<count;i++) {
241 struct MprVar *output;
243 status = irpc_call_recv(reqs[i]);
244 if (!NT_STATUS_IS_OK(status)) {
247 status = ejs_push(ejs, io, ptr);
248 if (!NT_STATUS_IS_OK(status)) {
252 /* add to the results array */
253 output = mprGetProperty(io, "output", NULL);
256 mprItoa(i, idx, sizeof(idx));
257 mprSetProperty(results, idx, output);
258 mprDeleteProperty(io, "output");
261 mprSetVar(results, "length", mprCreateIntegerVar(i));
265 mpr_Return(eid, mprNTSTATUS(status));
266 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
274 backend code for making an rpc call - this is called from the pidl generated ejs
277 int ejs_rpc_call(int eid, int argc, struct MprVar **argv,
278 const struct dcerpc_interface_table *iface, int callnum,
279 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
282 struct dcerpc_pipe *p;
285 struct rpc_request *req;
287 const struct dcerpc_interface_call *call;
289 if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
290 ejsSetErrorMsg(eid, "rpc_call invalid arguments");
296 if (mprGetThisPtr(eid, "irpc")) {
297 /* its an irpc call */
298 return ejs_irpc_call(eid, io, iface, callnum, ejs_pull, ejs_push);
301 /* get the pipe info */
302 p = mprGetThisPtr(eid, "pipe");
304 ejsSetErrorMsg(eid, "rpc_call invalid pipe");
308 ejs = talloc(mprMemCtx(), struct ejs_rpc);
310 status = NT_STATUS_NO_MEMORY;
314 call = &iface->calls[callnum];
317 ejs->callname = call->name;
319 /* allocate the C structure */
320 ptr = talloc_zero_size(ejs, call->struct_size);
322 status = NT_STATUS_NO_MEMORY;
326 /* convert the mpr object into a C structure */
327 status = ejs_pull(ejs, io, ptr);
328 if (!NT_STATUS_IS_OK(status)) {
332 /* make the actual call */
333 req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
335 /* if requested, print the structure */
336 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
337 ndr_print_function_debug(call->ndr_print, call->name, NDR_IN, ptr);
341 status = NT_STATUS_NO_MEMORY;
345 status = dcerpc_ndr_request_recv(req);
346 if (!NT_STATUS_IS_OK(status)) {
350 /* print the 'out' structure, if needed */
351 if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
352 ndr_print_function_debug(call->ndr_print, call->name, NDR_OUT, ptr);
355 status = ejs_push(ejs, io, ptr);
359 mpr_Return(eid, mprNTSTATUS(status));
360 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
367 hook called by generated RPC interfaces at the end of their init routines
368 used to add generic operations on the pipe
370 int ejs_rpc_init(struct MprVar *obj, const char *name)
372 mprSetStringCFunction(obj, "connect", ejs_rpc_connect);
373 if (mprGetProperty(obj, "pipe_name", NULL) == NULL) {
374 mprSetVar(obj, "pipe_name", mprString(name));