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"
28 #include "dlinklist.h"
31 connect to an rpc server
33 var conn = new Object();
34 status = rpc_connect(conn, "ncacn_ip_tcp:localhost", "rpcecho");
36 static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv)
38 const char *binding, *pipe_name;
39 const struct dcerpc_interface_table *iface;
41 struct dcerpc_pipe *p;
43 struct cli_credentials *creds = cmdline_credentials;
44 struct event_context *ev;
46 /* validate arguments */
48 argv[0]->type != MPR_TYPE_OBJECT ||
49 argv[1]->type != MPR_TYPE_STRING ||
50 argv[2]->type != MPR_TYPE_STRING) {
51 ejsSetErrorMsg(eid, "rpc_connect invalid arguments");
56 binding = mprToString(argv[1]);
57 pipe_name = mprToString(argv[2]);
59 iface = idl_iface_by_name(pipe_name);
61 status = NT_STATUS_OBJECT_NAME_INVALID;
66 creds = cli_credentials_init(mprMemCtx());
67 cli_credentials_guess(creds);
68 cli_credentials_set_username(creds, "", CRED_GUESSED);
69 cli_credentials_set_password(creds, "", CRED_GUESSED);
72 ev = talloc_find_parent_bytype(mprMemCtx(), struct event_context);
74 status = dcerpc_pipe_connect(mprMemCtx(), &p, binding,
75 iface->uuid, iface->if_version,
77 if (!NT_STATUS_IS_OK(status)) goto done;
79 /* callers don't allocate ref vars in the ejs interface */
80 p->conn->flags |= DCERPC_NDR_REF_ALLOC;
82 mprSetPtr(conn, "pipe", p);
83 mprSetPtr(conn, "iface", iface);
86 ejsSetReturnValue(eid, mprNTSTATUS(status));
94 status = rpc_call(conn, "echo_AddOne", io);
96 int ejs_rpc_call(int eid, int argc, struct MprVar **argv,
98 ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
100 struct MprVar *conn, *io;
101 const struct dcerpc_interface_table *iface;
102 struct dcerpc_pipe *p;
103 const struct dcerpc_interface_call *call;
106 struct rpc_request *req;
110 argv[0]->type != MPR_TYPE_OBJECT ||
111 argv[1]->type != MPR_TYPE_OBJECT) {
112 ejsSetErrorMsg(eid, "rpc_call invalid arguments");
119 /* get the pipe info */
120 p = mprGetPtr(conn, "pipe");
121 iface = mprGetPtr(conn, "iface");
122 if (p == NULL || iface == NULL) {
123 ejsSetErrorMsg(eid, "rpc_call invalid pipe");
127 /* find the call by name */
128 call = dcerpc_iface_find_call(iface, callname);
130 status = NT_STATUS_OBJECT_NAME_INVALID;
133 callnum = call - iface->calls;
135 /* allocate the C structure */
136 ptr = talloc_zero_size(mprMemCtx(), call->struct_size);
138 status = NT_STATUS_NO_MEMORY;
142 /* convert the mpr object into a C structure */
143 status = ejs_pull_rpc(eid, callname, io, ptr, ejs_pull);
144 if (!NT_STATUS_IS_OK(status)) {
148 /* if requested, print the structure */
149 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
150 ndr_print_function_debug(call->ndr_print, call->name, NDR_IN, ptr);
153 /* make the actual call */
154 req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
156 status = NT_STATUS_NO_MEMORY;
160 status = dcerpc_ndr_request_recv(req);
162 /* print the 'out' structure, if needed */
163 if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
164 ndr_print_function_debug(call->ndr_print, call->name, NDR_OUT, ptr);
167 status = ejs_push_rpc(eid, callname, io, ptr, ejs_push);
171 ejsSetReturnValue(eid, mprNTSTATUS(status));
172 if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
179 /* a list of registered ejs rpc modules */
180 static struct ejs_register {
181 struct ejs_register *next, *prev;
184 ejs_constants_t constants;
188 register a generated ejs module
190 NTSTATUS smbcalls_register_ejs(const char *name,
192 ejs_constants_t constants)
194 struct ejs_register *r;
195 void *ctx = ejs_registered;
197 ctx = talloc_autofree_context();
199 r = talloc(ctx, struct ejs_register);
200 NT_STATUS_HAVE_NO_MEMORY(r);
203 r->constants = constants;
204 DLIST_ADD(ejs_registered, r);
209 setup C functions that be called from ejs
211 void smb_setup_ejs_rpc(void)
213 struct ejs_register *r;
215 ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
216 for (r=ejs_registered;r;r=r->next) {
222 setup constants for rpc calls
224 void smb_setup_ejs_rpc_constants(int eid)
226 struct ejs_register *r;
229 for (r=ejs_registered;r;r=r->next) {
233 v = mprCreatePtrVar(NULL, "NULL");
234 mprSetProperty(ejsGetGlobalObject(eid), "NULL", &v);