r8200: - added stub functions for union pull/push
[jra/samba/.git] / source / scripting / ejs / smbcalls_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide interfaces to rpc calls from ejs scripts
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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
29 /*
30   connect to an rpc server
31      example: 
32         var conn = new Object();
33         status = rpc_connect(conn, "ncacn_ip_tcp:localhost", "rpcecho");
34 */
35 static int ejs_rpc_connect(MprVarHandle eid, int argc, struct MprVar **argv)
36 {
37         const char *binding, *pipe_name;
38         const struct dcerpc_interface_table *iface;
39         NTSTATUS status;
40         struct dcerpc_pipe *p;
41         struct MprVar *conn;
42
43         /* validate arguments */
44         if (argc != 3 ||
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");
49                 return -1;
50         }
51
52         conn       = argv[0];
53         binding    = mprToString(argv[1]);
54         pipe_name  = mprToString(argv[2]);
55
56         iface = idl_iface_by_name(pipe_name);
57         if (iface == NULL) {
58                 status = NT_STATUS_OBJECT_NAME_INVALID;
59                 goto done;
60         }
61
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;
66
67         /* callers don't allocate ref vars in the ejs interface */
68         p->conn->flags |= DCERPC_NDR_REF_ALLOC;
69
70         mprSetPtr(conn, "pipe", p);
71         mprSetPtr(conn, "iface", iface);
72
73 done:
74         ejsSetReturnValue(eid, mprNTSTATUS(status));
75         return 0;
76 }
77
78
79 /*
80   make an rpc call
81      example:
82             status = rpc_call(conn, "echo_AddOne", io);
83 */
84  int ejs_rpc_call(int eid, int argc, struct MprVar **argv,
85                   const char *callname,
86                   ejs_pull_function_t ejs_pull, ejs_push_function_t ejs_push)
87 {
88         struct MprVar *conn, *io;
89         const struct dcerpc_interface_table *iface;
90         struct dcerpc_pipe *p;
91         const struct dcerpc_interface_call *call;
92         NTSTATUS status;
93         void *ptr;
94         struct rpc_request *req;
95         int callnum;
96
97         if (argc != 2 ||
98             argv[0]->type != MPR_TYPE_OBJECT ||
99             argv[1]->type != MPR_TYPE_OBJECT) {
100                 ejsSetErrorMsg(eid, "rpc_call invalid arguments");
101                 return -1;
102         }
103             
104         conn     = argv[0];
105         io       = argv[1];
106
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");
112                 return -1;
113         }
114
115         /* find the call by name */
116         call = dcerpc_iface_find_call(iface, callname);
117         if (call == NULL) {
118                 status = NT_STATUS_OBJECT_NAME_INVALID;
119                 goto done;
120         }
121         callnum = call - iface->calls;
122
123         /* allocate the C structure */
124         ptr = talloc_zero_size(mprMemCtx(), call->struct_size);
125         if (ptr == NULL) {
126                 status = NT_STATUS_NO_MEMORY;
127                 goto done;
128         }
129
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)) {
133                 goto done;
134         }
135
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);
139         }
140
141         /* make the actual call */
142         req = dcerpc_ndr_request_send(p, NULL, iface, callnum, ptr, ptr);
143         if (req == NULL) {
144                 status = NT_STATUS_NO_MEMORY;
145                 talloc_free(ptr);
146                 goto done;
147         }
148         status = dcerpc_ndr_request_recv(req);
149
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);
153         }
154
155         status = ejs_push_rpc(eid, callname, io, ptr, ejs_push);
156
157         talloc_free(ptr);
158 done:
159         ejsSetReturnValue(eid, mprNTSTATUS(status));
160         if (NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_ERROR)) {
161                 return -1;
162         }
163         return 0;
164 }
165
166
167 /*
168   setup C functions that be called from ejs
169 */
170 void smb_setup_ejs_rpc(void)
171 {
172         void setup_ejs_rpcecho(void);
173         ejsDefineCFunction(-1, "rpc_connect", ejs_rpc_connect, NULL, MPR_VAR_SCRIPT_HANDLE);
174         setup_ejs_rpcecho();
175 }