Debian Codegraph @

pidl/lib/Parse/Pidl/Samba4/NDR/Server.pm

raw
1 ###################################################
2 # server boilerplate generator
3 # Copyright [email protected] 2003
4 # Copyright [email protected] 2004
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba4::NDR::Server;
8
9 use strict;
10 use warnings;
11 use Parse::Pidl::Util;
12
13 use vars qw($VERSION);
14 $VERSION = '0.01';
15
16 my($res);
17
18 sub pidl($)
19 {
20 $res .= shift;
21 }
22
23
24 #####################################################
25 # generate the switch statement for function dispatch
26 sub gen_dispatch_switch($)
27 {
28 my $interface = shift;
29
30 foreach my $fn (@{$interface->{FUNCTIONS}}) {
31 next if not defined($fn->{OPNUM});
32
33 pidl "\tcase $fn->{OPNUM}: {\n";
34 pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n";
35 pidl "\t\tif (DEBUGLEVEL >= 10) {\n";
36 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r2);\n";
37 pidl "\t\t}\n";
38 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
39 pidl "\t\tr2->out.result = dcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n";
40 } else {
41 pidl "\t\tdcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n";
42 }
43 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
44 pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} will reply async\\n\"));\n";
45 pidl "\t\t}\n";
46 pidl "\t\tbreak;\n\t}\n";
47 }
48 }
49
50 #####################################################
51 # generate the switch statement for function reply
52 sub gen_reply_switch($)
53 {
54 my $interface = shift;
55
56 foreach my $fn (@{$interface->{FUNCTIONS}}) {
57 next if not defined($fn->{OPNUM});
58
59 pidl "\tcase $fn->{OPNUM}: {\n";
60 pidl "\t\tstruct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;\n";
61 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
62 pidl "\t\t\tDEBUG(5,(\"function $fn->{NAME} replied async\\n\"));\n";
63 pidl "\t\t}\n";
64 pidl "\t\tif (DEBUGLEVEL >= 10 && dce_call->fault_code == 0) {\n";
65 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
66 pidl "\t\t}\n";
67 pidl "\t\tif (dce_call->fault_code != 0) {\n";
68 pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $fn->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
69 pidl "\t\t}\n";
70 pidl "\t\tbreak;\n\t}\n";
71 }
72 }
73
74 #####################################################################
75 # produce boilerplate code for a interface
76 sub Boilerplate_Iface($)
77 {
78 my($interface) = shift;
79 my $name = $interface->{NAME};
80 my $uname = uc $name;
81 my $uuid = lc($interface->{UUID});
82 my $if_version = $interface->{VERSION};
83
84 pidl "
85 static NTSTATUS $name\__op_bind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
86 {
87 #ifdef DCESRV_INTERFACE_$uname\_BIND
88 return DCESRV_INTERFACE_$uname\_BIND(context,iface);
89 #else
90 return NT_STATUS_OK;
91 #endif
92 }
93
94 static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
95 {
96 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
97 DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
98 #else
99 return;
100 #endif
101 }
102
103 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
104 {
105 enum ndr_err_code ndr_err;
106 uint16_t opnum = dce_call->pkt.u.request.opnum;
107
108 dce_call->fault_code = 0;
109
110 if (opnum >= ndr_table_$name.num_calls) {
111 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
112 return NT_STATUS_NET_WRITE_FAULT;
113 }
114
115 *r = talloc_named(mem_ctx,
116 ndr_table_$name.calls[opnum].struct_size,
117 \"struct %s\",
118 ndr_table_$name.calls[opnum].name);
119 NT_STATUS_HAVE_NO_MEMORY(*r);
120
121 /* unravel the NDR for the packet */
122 ndr_err = ndr_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
123 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
124 dce_call->fault_code = DCERPC_FAULT_NDR;
125 return NT_STATUS_NET_WRITE_FAULT;
126 }
127
128 return NT_STATUS_OK;
129 }
130
131 static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
132 {
133 uint16_t opnum = dce_call->pkt.u.request.opnum;
134
135 switch (opnum) {
136 ";
137 gen_dispatch_switch($interface);
138
139 pidl "
140 default:
141 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
142 break;
143 }
144
145 if (dce_call->fault_code != 0) {
146 return NT_STATUS_NET_WRITE_FAULT;
147 }
148
149 return NT_STATUS_OK;
150 }
151
152 static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
153 {
154 uint16_t opnum = dce_call->pkt.u.request.opnum;
155
156 switch (opnum) {
157 ";
158 gen_reply_switch($interface);
159
160 pidl "
161 default:
162 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
163 break;
164 }
165
166 if (dce_call->fault_code != 0) {
167 return NT_STATUS_NET_WRITE_FAULT;
168 }
169
170 return NT_STATUS_OK;
171 }
172
173 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
174 {
175 enum ndr_err_code ndr_err;
176 uint16_t opnum = dce_call->pkt.u.request.opnum;
177
178 ndr_err = ndr_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
179 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180 dce_call->fault_code = DCERPC_FAULT_NDR;
181 return NT_STATUS_NET_WRITE_FAULT;
182 }
183
184 return NT_STATUS_OK;
185 }
186
187 static const struct dcesrv_interface dcesrv\_$name\_interface = {
188 .name = \"$name\",
189 .syntax_id = {".print_uuid($uuid).",$if_version},
190 .bind = $name\__op_bind,
191 .unbind = $name\__op_unbind,
192 .ndr_pull = $name\__op_ndr_pull,
193 .dispatch = $name\__op_dispatch,
194 .reply = $name\__op_reply,
195 .ndr_push = $name\__op_ndr_push,
196 .local = NULL,
197 #ifdef DCESRV_INTERFACE_$uname\_FLAGS
198 .flags = DCESRV_INTERFACE_$uname\_FLAGS
199 #else
200 .flags = 0
201 #endif
202 };
203
204 ";
205 }
206
207 #####################################################################
208 # produce boilerplate code for an endpoint server
209 sub Boilerplate_Ep_Server($)
210 {
211 my($interface) = shift;
212 my $name = $interface->{NAME};
213 my $uname = uc $name;
214
215 pidl "
216 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
217 {
218 int i;
219 #ifdef DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT
220 const char *ncacn_np_secondary_endpoint =
221 DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT;
222 #else
223 const char *ncacn_np_secondary_endpoint = NULL;
224 #endif
225
226 for (i=0;i<ndr_table_$name.endpoints->count;i++) {
227 NTSTATUS ret;
228 const char *name = ndr_table_$name.endpoints->names[i];
229
230 ret = dcesrv_interface_register(dce_ctx,
231 name,
232 ncacn_np_secondary_endpoint,
233 &dcesrv_$name\_interface,
234 NULL);
235 if (!NT_STATUS_IS_OK(ret)) {
236 DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
237 return ret;
238 }
239 }
240
241 return NT_STATUS_OK;
242 }
243
244 static NTSTATUS $name\__op_shutdown_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
245 {
246 return NT_STATUS_OK;
247 }
248
249 static bool $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
250 {
251 if (dcesrv_$name\_interface.syntax_id.if_version == if_version &&
252 GUID_equal(\&dcesrv\_$name\_interface.syntax_id.uuid, uuid)) {
253 memcpy(iface,&dcesrv\_$name\_interface, sizeof(*iface));
254 return true;
255 }
256
257 return false;
258 }
259
260 static bool $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
261 {
262 if (strcmp(dcesrv_$name\_interface.name, name)==0) {
263 memcpy(iface, &dcesrv_$name\_interface, sizeof(*iface));
264 return true;
265 }
266
267 return false;
268 }
269
270 NTSTATUS dcerpc_server_$name\_init(TALLOC_CTX *ctx)
271 {
272 NTSTATUS ret;
273 static const struct dcesrv_endpoint_server ep_server = {
274 /* fill in our name */
275 .name = \"$name\",
276
277 /* Initialization flag */
278 .initialized = false,
279
280 /* fill in all the operations */
281 #ifdef DCESRV_INTERFACE_$uname\_INIT_SERVER
282 .init_server = DCESRV_INTERFACE_$uname\_INIT_SERVER,
283 #else
284 .init_server = $name\__op_init_server,
285 #endif
286 #ifdef DCESRV_INTERFACE_$uname\_SHUTDOWN_SERVER
287 .shutdown_server = DCESRV_INTERFACE_$uname\_SHUTDOWN_SERVER,
288 #else
289 .shutdown_server = $name\__op_shutdown_server,
290 #endif
291 .interface_by_uuid = $name\__op_interface_by_uuid,
292 .interface_by_name = $name\__op_interface_by_name
293 };
294 /* register ourselves with the DCERPC subsystem. */
295 ret = dcerpc_register_ep_server(&ep_server);
296
297 if (!NT_STATUS_IS_OK(ret)) {
298 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
299 return ret;
300 }
301
302 return ret;
303 }
304
305 ";
306 }
307
308 #####################################################################
309 # dcerpc server boilerplate from a parsed IDL structure
310 sub ParseInterface($)
311 {
312 my($interface) = shift;
313 my $count = 0;
314
315 $res .= "NTSTATUS dcerpc_server_$interface->{NAME}\_init(TALLOC_CTX *);\n";
316 $res .= "\n";
317
318 if (!defined $interface->{PROPERTIES}->{uuid}) {
319 return $res;
320 }
321
322 if (!defined $interface->{PROPERTIES}->{version}) {
323 $interface->{PROPERTIES}->{version} = "0.0";
324 }
325
326 foreach my $fn (@{$interface->{FUNCTIONS}}) {
327 if (defined($fn->{OPNUM})) { $count++; }
328 }
329
330 if ($count == 0) {
331 return $res;
332 }
333
334 $res .= "/* $interface->{NAME} - dcerpc server boilerplate generated by pidl */\n\n";
335 Boilerplate_Iface($interface);
336 Boilerplate_Ep_Server($interface);
337
338 return $res;
339 }
340
341 sub Parse($$)
342 {
343 my($ndr,$header) = @_;
344
345 $res = "";
346 $res .= "/* server functions auto-generated by pidl */\n";
347 $res .= "#include \"$header\"\n";
348 $res .= "#include <util/debug.h>\n";
349 $res .= "\n";
350
351 foreach my $x (@{$ndr}) {
352 ParseInterface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object}));
353 }
354
355 return $res;
356 }
357
358 1;