236e6e17031ade0602e29b2dd0c7c94a8c0a0e49
[samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / ServerCompat.pm
1 ###################################################
2 # server boilerplate generator
3 # Copyright tridge@samba.org 2003
4 # Copyright metze@samba.org 2004
5 # Copyright scabrero@samba.org 2019
6 # released under the GNU GPL
7
8 package Parse::Pidl::Samba4::NDR::ServerCompat;
9
10 use Exporter;
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(Parse);
13
14 use Parse::Pidl::Util qw(print_uuid);
15 use Parse::Pidl::Typelist qw(mapTypeName);
16
17 use vars qw($VERSION);
18 $VERSION = '1.0';
19
20 use strict;
21
22 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
23 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
24 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
25 sub pidlnoindent($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$txt\n" : "\n"; }
26 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; }
27 sub pidl_both($$) { my ($self, $txt) = @_; $self->{hdr} .= "$txt\n"; $self->{res_hdr} .= "$txt\n"; }
28
29 sub new($)
30 {
31         my ($class) = shift;
32         my $self = { res => "", res_hdr => "", tabs => "" };
33         bless($self, $class);
34 }
35
36 #####################################################
37 # generate the switch statement for function dispatch
38 sub gen_dispatch_switch($)
39 {
40         my ($self, $interface) = @_;
41
42         foreach my $fn (@{$interface->{FUNCTIONS}}) {
43                 next if not defined($fn->{OPNUM});
44
45                 my $fname = $fn->{NAME};
46                 my $ufname = uc($fname);
47
48                 $self->pidl("case $fn->{OPNUM}: { /* $fn->{NAME} */");
49                 $self->indent();
50                 $self->pidl("struct $fname *r2 = (struct $fname *)r;");
51                 $self->pidl("if (DEBUGLEVEL >= 10) {");
52                 $self->indent();
53                 $self->pidl("NDR_PRINT_FUNCTION_DEBUG($fname, NDR_IN, r2);");
54                 $self->deindent();
55                 $self->pidl("}");
56
57                 $self->pidl_hdr("struct $fname;");
58
59                 if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
60                         $self->pidl_hdr(mapTypeName($fn->{RETURN_TYPE}) . " _$fname(struct pipes_struct *p, struct $fname *r);");
61                         $self->pidl("r2->out.result = _$fname(p, r2);");
62                 } else {
63                         $self->pidl_hdr("void _$fname(struct pipes_struct *p, struct $fname *r);");
64                         $self->pidl("_$fname(p, r2);");
65                 }
66
67                 $self->pidl("break;");
68                 $self->deindent();
69                 $self->pidl("}");
70         }
71 }
72
73 #####################################################
74 # generate the switch statement for function reply
75 sub gen_reply_switch($)
76 {
77         my ($self, $interface) = @_;
78
79         foreach my $fn (@{$interface->{FUNCTIONS}}) {
80                 next if not defined($fn->{OPNUM});
81
82                 $self->pidl("case $fn->{OPNUM}: { /* $fn->{NAME} */");
83                 $self->indent();
84                 $self->pidl("struct $fn->{NAME} *r2 = (struct $fn->{NAME} *)r;");
85                 $self->pidl("if (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {");
86                 $self->indent();
87                 $self->pidl("DEBUG(5,(\"function $fn->{NAME} replied async\\n\"));");
88                 $self->deindent();
89                 $self->pidl("}");
90                 $self->pidl("if (DEBUGLEVEL >= 10 && dce_call->fault_code == 0) {");
91                 $self->indent();
92                 $self->pidl("NDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);");
93                 $self->deindent();
94                 $self->pidl("}");
95                 $self->pidl("if (dce_call->fault_code != 0) {");
96                 $self->indent();
97                 $self->pidl("DBG_WARNING(\"dcerpc_fault %s in $fn->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code));");
98                 $self->deindent();
99                 $self->pidl("}");
100                 $self->pidl("break;");
101                 $self->deindent();
102                 $self->pidl("}");
103         }
104 }
105
106 #####################################################################
107 # produce boilerplate code for a interface
108 sub boilerplate_iface($)
109 {
110         my ($self, $interface) = @_;
111
112         my $name = $interface->{NAME};
113         my $uname = uc $name;
114         my $uuid = lc($interface->{UUID});
115         my $if_version = $interface->{VERSION};
116
117         $self->pidl("static NTSTATUS $name\__op_bind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)");
118         $self->pidl("{");
119         $self->indent();
120         $self->pidl("/* TODO Retrieve pipes struct */");
121         $self->pidl("/* TODO Init pipe handles */");
122         $self->pidl("/* TODO Init pipe context */");
123         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_BIND");
124         $self->pidl("return DCESRV_INTERFACE_$uname\_BIND(context,iface);");
125         $self->pidlnoindent("#else");
126         $self->pidl("return NT_STATUS_OK;");
127         $self->deindent();
128         $self->pidl("#endif");
129         $self->pidl("}");
130         $self->pidl("");
131
132         $self->pidl("static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)");
133         $self->pidl("{");
134         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_UNBIND");
135         $self->indent();
136         $self->pidl("DCESRV_INTERFACE_$uname\_UNBIND(context, iface);");
137         $self->pidlnoindent("#else");
138         $self->pidl("return;");
139         $self->pidlnoindent("#endif");
140         $self->deindent();
141         $self->pidl("}");
142         $self->pidl("");
143
144         $self->pidl("static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)");
145         $self->pidl("{");
146         $self->indent();
147         $self->pidl("enum ndr_err_code ndr_err;");
148         $self->pidl("uint16_t opnum = dce_call->pkt.u.request.opnum;");
149         $self->pidl("");
150         $self->pidl("dce_call->fault_code = 0;");
151         $self->pidl("");
152         $self->pidl("if (opnum >= ndr_table_$name.num_calls) {");
153         $self->indent();
154         $self->pidl("dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;");
155         $self->pidl("return NT_STATUS_NET_WRITE_FAULT;");
156         $self->deindent();
157         $self->pidl("}");
158         $self->pidl("");
159         $self->pidl("*r = talloc_named(mem_ctx, ndr_table_$name.calls[opnum].struct_size, \"struct %s\", ndr_table_$name.calls[opnum].name);");
160         $self->pidl("NT_STATUS_HAVE_NO_MEMORY(*r);");
161         $self->pidl("");
162         $self->pidl("/* unravel the NDR for the packet */");
163         $self->pidl("ndr_err = ndr_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);");
164         $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {");
165         $self->indent();
166         $self->pidl("dce_call->fault_code = DCERPC_FAULT_NDR;");
167         $self->pidl("return NT_STATUS_NET_WRITE_FAULT;");
168         $self->deindent();
169         $self->pidl("}");
170         $self->pidl("");
171         $self->pidl("return NT_STATUS_OK;");
172         $self->deindent();
173         $self->pidl("}");
174         $self->pidl("");
175
176         $self->pidl("static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)");
177         $self->pidl("{");
178         $self->indent();
179         $self->pidl("uint16_t opnum = dce_call->pkt.u.request.opnum;");
180         $self->pidl("struct pipes_struct *p = NULL;");
181         $self->pidl("NTSTATUS status = NT_STATUS_OK;");
182         $self->pidl("");
183         $self->pidl("/* TODO Retrieve pipes struct */");
184         $self->pidl("/* TODO Update pipes struct opnum */");
185         $self->pidl("/* TODO Update pipes struct session info */");
186         $self->pidl("/* TODO Reset pipes struct fault state */");
187         $self->pidl("");
188         $self->pidl("switch (opnum) {");
189         $self->gen_dispatch_switch($interface);
190         $self->pidl("default:");
191         $self->indent();
192         $self->pidl("dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;");
193         $self->pidl("break;");
194         $self->deindent();
195         $self->pidl("}");
196         $self->pidl("");
197         $self->pidl("/* TODO Check pipes struct fault state */");
198         $self->pidl("if (dce_call->fault_code != 0) {");
199         $self->indent();
200         $self->pidl("status = NT_STATUS_NET_WRITE_FAULT;");
201         $self->deindent();
202         $self->pidl("}");
203         $self->pidl("");
204
205         $self->pidl("return status;");
206         $self->deindent();
207         $self->pidl("}");
208         $self->pidl("");
209
210         $self->pidl("static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)");
211         $self->pidl("{");
212         $self->indent();
213         $self->pidl("uint16_t opnum = dce_call->pkt.u.request.opnum;");
214         $self->pidl("");
215         $self->pidl("switch (opnum) {");
216         $self->gen_reply_switch($interface);
217         $self->pidl("default:");
218         $self->indent();
219         $self->pidl("dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;");
220         $self->pidl("break;");
221         $self->deindent();
222         $self->pidl("}");
223         $self->pidl("");
224
225         $self->pidl("if (dce_call->fault_code != 0) {");
226         $self->indent();
227         $self->pidl("return NT_STATUS_NET_WRITE_FAULT;");
228         $self->deindent();
229         $self->pidl("}");
230         $self->pidl("");
231         $self->pidl("return NT_STATUS_OK;");
232         $self->deindent();
233         $self->pidl("}");
234         $self->pidl("");
235
236         $self->pidl("static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)");
237         $self->pidl("{");
238         $self->indent();
239         $self->pidl("enum ndr_err_code ndr_err;");
240         $self->pidl("uint16_t opnum = dce_call->pkt.u.request.opnum;");
241         $self->pidl("");
242         $self->pidl("ndr_err = ndr_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);");
243         $self->pidl("if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {");
244         $self->indent();
245         $self->pidl("dce_call->fault_code = DCERPC_FAULT_NDR;");
246         $self->pidl("return NT_STATUS_NET_WRITE_FAULT;");
247         $self->deindent();
248         $self->pidl("}");
249         $self->pidl("");
250         $self->pidl("return NT_STATUS_OK;");
251         $self->deindent();
252         $self->pidl("}");
253         $self->pidl("");
254
255         $self->pidl("static const struct dcesrv_interface dcesrv\_$name\_interface = {");
256         $self->indent();
257         $self->pidl(".name      = \"$name\",");
258         $self->pidl(".syntax_id = {".print_uuid($uuid).",$if_version},");
259         $self->pidl(".bind      = $name\__op_bind,");
260         $self->pidl(".unbind    = $name\__op_unbind,");
261         $self->pidl(".ndr_pull  = $name\__op_ndr_pull,");
262         $self->pidl(".dispatch  = $name\__op_dispatch,");
263         $self->pidl(".reply     = $name\__op_reply,");
264         $self->pidl(".ndr_push  = $name\__op_ndr_push,");
265         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_FLAGS");
266         $self->pidl(".flags     = DCESRV_INTERFACE_$uname\_FLAGS");
267         $self->pidlnoindent("#else");
268         $self->pidl(".flags     = 0");
269         $self->pidlnoindent("#endif");
270         $self->deindent();
271         $self->pidl("};");
272         $self->pidl("");
273 }
274
275 #####################################################################
276 # produce boilerplate code for an endpoint server
277 sub boilerplate_ep_server($)
278 {
279         my ($self, $interface) = @_;
280         my $name = $interface->{NAME};
281         my $uname = uc $name;
282
283         $self->pidl("static NTSTATUS $name\__check_register_in_endpoint(const char *name, struct dcerpc_binding *binding) {");
284         $self->indent();
285         $self->pidl("enum dcerpc_transport_t transport = dcerpc_binding_get_transport(binding);");
286         $self->pidl("");
287         $self->pidl("/* If service is embedded, register only for ncacn_np");
288         $self->pidl(" * see 8466b3c85e4b835e57e41776853093f4a0edc8b8");
289         $self->pidl(" */");
290         $self->pidl("if (rpc_service_mode(name) == RPC_SERVICE_MODE_EMBEDDED && transport != NCACN_NP) {");
291         $self->indent();
292         $self->pidl("DBG_INFO(\"Interface \'$name\' not registered in endpoint \'%s\' as service is embedded\\n\", name);");
293         $self->pidl("return NT_STATUS_NOT_SUPPORTED;");
294         $self->deindent();
295         $self->pidl("}");
296         $self->pidl("");
297         $self->pidl("return NT_STATUS_OK;");
298         $self->deindent();
299         $self->pidl("}");
300         $self->pidl("");
301
302         $self->pidl("static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)");
303         $self->pidl("{");
304         $self->indent();
305         $self->pidl("int i;");
306         $self->pidl("NTSTATUS ret;");
307         $self->pidl("struct dcerpc_binding *binding;");
308         $self->pidl("");
309         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT");
310         $self->pidl("const char *ncacn_np_secondary_endpoint = DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT;");
311         $self->pidlnoindent("#else");
312         $self->pidl("const char *ncacn_np_secondary_endpoint = NULL;");
313         $self->pidlnoindent("#endif");
314         $self->pidl("");
315         $self->pidl("for (i=0;i<ndr_table_$name.endpoints->count;i++) {");
316         $self->indent();
317         $self->pidl("const char *name = ndr_table_$name.endpoints->names[i];");
318         $self->pidl("");
319         $self->pidl("/* TODO Register the interface for local dispatching */");
320         $self->pidl("");
321         $self->pidl("ret = dcerpc_parse_binding(dce_ctx, name, &binding);");
322         $self->pidl("if (NT_STATUS_IS_ERR(ret)) {");
323         $self->indent();
324         $self->pidl("DBG_ERR(\"Failed to parse binding string \'%s\'\\n\", name);");
325         $self->pidl("return ret;");
326         $self->deindent();
327         $self->pidl("}");
328         $self->pidl("");
329         $self->pidl("ret = $name\__check_register_in_endpoint(\"$name\", binding);");
330         $self->pidl("if (NT_STATUS_IS_ERR(ret)) {");
331         $self->indent();
332         $self->pidl("talloc_free(binding);");
333         $self->pidl("continue;");
334         $self->deindent();
335         $self->pidl("}");
336         $self->pidl("talloc_free(binding);");
337         $self->pidl("");
338         $self->pidl("ret = dcesrv_interface_register(dce_ctx, name, ncacn_np_secondary_endpoint, &dcesrv_$name\_interface, NULL);");
339         $self->pidl("if (!NT_STATUS_IS_OK(ret)) {");
340         $self->indent();
341         $self->pidl("DBG_ERR(\"Failed to register endpoint \'%s\'\\n\",name);");
342         $self->pidl("return ret;");
343         $self->deindent();
344         $self->pidl("}");
345         $self->deindent();
346         $self->pidl("}");
347         $self->pidl("");
348         $self->pidl("return NT_STATUS_OK;");
349         $self->deindent();
350         $self->pidl("}");
351         $self->pidl("");
352
353         $self->pidl("static NTSTATUS $name\__op_shutdown_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)");
354         $self->pidl("{");
355         $self->indent();
356         $self->pidl("return NT_STATUS_OK;");
357         $self->deindent();
358         $self->pidl("}");
359         $self->pidl("");
360
361         $self->pidl("static bool $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)");
362         $self->pidl("{");
363         $self->indent();
364         $self->pidl("if (dcesrv_$name\_interface.syntax_id.if_version == if_version && GUID_equal(\&dcesrv\_$name\_interface.syntax_id.uuid, uuid)) {");
365         $self->indent();
366         $self->pidl("memcpy(iface,&dcesrv\_$name\_interface, sizeof(*iface));");
367         $self->pidl("return true;");
368         $self->deindent();
369         $self->pidl("}");
370         $self->pidl("");
371         $self->pidl("return false;");
372         $self->deindent();
373         $self->pidl("}");
374         $self->pidl("");
375
376         $self->pidl("static bool $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)");
377         $self->pidl("{");
378         $self->indent();
379         $self->pidl("if (strcmp(dcesrv_$name\_interface.name, name)==0) {");
380         $self->indent();
381         $self->pidl("memcpy(iface, &dcesrv_$name\_interface, sizeof(*iface));");
382         $self->pidl("return true;");
383         $self->deindent();
384         $self->pidl("}");
385         $self->pidl("");
386         $self->pidl("return false;");
387         $self->deindent();
388         $self->pidl("}");
389         $self->pidl("");
390
391         $self->pidl("static const struct dcesrv_endpoint_server $name\_ep_server = {");
392         $self->indent();
393         $self->pidl("/* fill in our name */");
394         $self->pidl(".name = \"$name\",");
395         $self->pidl("");
396         $self->pidl("/* Initialization flag */");
397         $self->pidl(".initialized = false,");
398         $self->pidl("");
399         $self->pidl("/* fill in all the operations */");
400         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_INIT_SERVER");
401         $self->pidl(".init_server = DCESRV_INTERFACE_$uname\_INIT_SERVER,");
402         $self->pidlnoindent("#else");
403         $self->pidl(".init_server = $name\__op_init_server,");
404         $self->pidlnoindent("#endif");
405         $self->pidlnoindent("#ifdef DCESRV_INTERFACE_$uname\_SHUTDOWN_SERVER");
406         $self->pidl(".shutdown_server = DCESRV_INTERFACE_$uname\_SHUTDOWN_SERVER,");
407         $self->pidlnoindent("#else");
408         $self->pidl(".shutdown_server = $name\__op_shutdown_server,");
409         $self->pidlnoindent("#endif");
410         $self->pidl(".interface_by_uuid = $name\__op_interface_by_uuid,");
411         $self->pidl(".interface_by_name = $name\__op_interface_by_name");
412         $self->deindent();
413         $self->pidl("};");
414         $self->pidl("");
415
416         $self->pidl("const struct dcesrv_endpoint_server *$name\_get_ep_server(void)");
417         $self->pidl("{");
418         $self->indent();
419         $self->pidl("return &$name\_ep_server;");
420         $self->deindent();
421         $self->pidl("}");
422 }
423
424 #####################################################################
425 # dcerpc server boilerplate from a parsed IDL structure
426 sub parse_interface($)
427 {
428         my ($self, $interface) = @_;
429         my $count = 0;
430         my $uif = uc($interface->{NAME});
431
432
433         $self->pidl_hdr("#ifndef __NDR_${uif}_SCOMPAT_H__");
434         $self->pidl_hdr("#define __NDR_${uif}_SCOMPAT_H__");
435         $self->pidl_hdr("");
436         $self->pidl_hdr("struct pipes_struct;");
437         $self->pidl_hdr("struct dcesrv_endpoint_server;");
438         $self->pidl_hdr("struct dcesrv_call_state;");
439         $self->pidl_hdr("");
440         $self->pidl_hdr("const struct dcesrv_endpoint_server *$interface->{NAME}\_get_ep_server(void);");
441         $self->pidl_hdr("");
442
443         if (!defined $interface->{PROPERTIES}->{uuid}) {
444                 $self->pidl_hdr("#endif /* __NDR_${uif}_SCOMPAT_H__ */");
445                 return;
446         }
447
448         if (!defined $interface->{PROPERTIES}->{version}) {
449                 $interface->{PROPERTIES}->{version} = "0.0";
450         }
451
452         foreach my $fn (@{$interface->{FUNCTIONS}}) {
453                 if (defined($fn->{OPNUM})) { $count++; }
454         }
455
456         if ($count == 0) {
457                 $self->pidl_hdr("#endif /* __NDR_${uif}_SCOMPAT_H__ */");
458                 return;
459         }
460
461         $self->pidl("/* $interface->{NAME} - dcerpc server boilerplate generated by pidl */");
462         $self->boilerplate_iface($interface);
463         $self->boilerplate_ep_server($interface);
464
465         $self->pidl_hdr("#endif /* __NDR_${uif}_SCOMPAT_H__ */");
466 }
467
468 sub Parse($$)
469 {
470         my ($self, $ndr, $h_scompat, $header) = @_;
471
472         $self->pidl("/* s3 compat server functions auto-generated by pidl */");
473         $self->pidl("#include \"$header\"");
474         $self->pidl("#include \"$h_scompat\"");
475
476         $self->pidl("#include <librpc/rpc/dcesrv_core.h>");
477         $self->pidl("#include <rpc_server/rpc_config.h>");
478         $self->pidl("#include <util/debug.h>");
479         $self->pidl("");
480
481         foreach my $x (@{$ndr}) {
482                 $self->parse_interface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object}));
483         }
484
485         return ($self->{res}, $self->{res_hdr});
486 }
487
488 1;