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