1 ###################################################
2 # server boilerplate generator
3 # Copyright tridge@samba.org 2003
4 # Copyright metze@samba.org 2004
5 # released under the GNU GPL
7 package Parse::Pidl::Samba4::NDR::Server;
10 use Parse::Pidl::Util;
12 use vars qw($VERSION);
23 #####################################################
24 # generate the switch statement for function dispatch
25 sub gen_dispatch_switch($)
27 my $interface = shift;
29 foreach my $fn (@{$interface->{FUNCTIONS}}) {
30 next if not defined($fn->{OPNUM});
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";
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";
40 pidl "\t\tdcesrv_$fn->{NAME}(dce_call, mem_ctx, r2);\n";
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";
45 pidl "\t\tbreak;\n\t}\n";
49 #####################################################
50 # generate the switch statement for function reply
51 sub gen_reply_switch($)
53 my $interface = shift;
55 foreach my $fn (@{$interface->{FUNCTIONS}}) {
56 next if not defined($fn->{OPNUM});
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";
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";
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";
69 pidl "\t\tbreak;\n\t}\n";
73 #####################################################################
74 # produce boilerplate code for a interface
75 sub Boilerplate_Iface($)
77 my($interface) = shift;
78 my $name = $interface->{NAME};
80 my $uuid = lc($interface->{UUID});
81 my $if_version = $interface->{VERSION};
84 static NTSTATUS $name\__op_bind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
86 #ifdef DCESRV_INTERFACE_$uname\_BIND
87 return DCESRV_INTERFACE_$uname\_BIND(context,iface);
93 static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
95 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
96 DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
102 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
104 enum ndr_err_code ndr_err;
105 uint16_t opnum = dce_call->pkt.u.request.opnum;
107 dce_call->fault_code = 0;
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;
114 *r = talloc_named(mem_ctx,
115 ndr_table_$name.calls[opnum].struct_size,
117 ndr_table_$name.calls[opnum].name);
118 NT_STATUS_HAVE_NO_MEMORY(*r);
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 dce_call->fault_code = DCERPC_FAULT_NDR;
124 return NT_STATUS_NET_WRITE_FAULT;
130 static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
132 uint16_t opnum = dce_call->pkt.u.request.opnum;
136 gen_dispatch_switch($interface);
140 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
144 if (dce_call->fault_code != 0) {
145 return NT_STATUS_NET_WRITE_FAULT;
151 static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
153 uint16_t opnum = dce_call->pkt.u.request.opnum;
157 gen_reply_switch($interface);
161 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
165 if (dce_call->fault_code != 0) {
166 return NT_STATUS_NET_WRITE_FAULT;
172 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
174 enum ndr_err_code ndr_err;
175 uint16_t opnum = dce_call->pkt.u.request.opnum;
177 ndr_err = ndr_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
178 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
179 dce_call->fault_code = DCERPC_FAULT_NDR;
180 return NT_STATUS_NET_WRITE_FAULT;
186 static const struct dcesrv_interface dcesrv\_$name\_interface = {
188 .syntax_id = {".print_uuid($uuid).",$if_version},
189 .bind = $name\__op_bind,
190 .unbind = $name\__op_unbind,
191 .ndr_pull = $name\__op_ndr_pull,
192 .dispatch = $name\__op_dispatch,
193 .reply = $name\__op_reply,
194 .ndr_push = $name\__op_ndr_push,
195 #ifdef DCESRV_INTERFACE_$uname\_FLAGS
196 .flags = DCESRV_INTERFACE_$uname\_FLAGS
205 #####################################################################
206 # produce boilerplate code for an endpoint server
207 sub Boilerplate_Ep_Server($)
209 my($interface) = shift;
210 my $name = $interface->{NAME};
211 my $uname = uc $name;
214 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
217 #ifdef DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT
218 const char *ncacn_np_secondary_endpoint =
219 DCESRV_INTERFACE_$uname\_NCACN_NP_SECONDARY_ENDPOINT;
221 const char *ncacn_np_secondary_endpoint = NULL;
224 for (i=0;i<ndr_table_$name.endpoints->count;i++) {
226 const char *name = ndr_table_$name.endpoints->names[i];
228 ret = dcesrv_interface_register(dce_ctx,
230 ncacn_np_secondary_endpoint,
231 &dcesrv_$name\_interface,
233 if (!NT_STATUS_IS_OK(ret)) {
234 DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
242 static bool $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
244 if (dcesrv_$name\_interface.syntax_id.if_version == if_version &&
245 GUID_equal(\&dcesrv\_$name\_interface.syntax_id.uuid, uuid)) {
246 memcpy(iface,&dcesrv\_$name\_interface, sizeof(*iface));
253 static bool $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
255 if (strcmp(dcesrv_$name\_interface.name, name)==0) {
256 memcpy(iface, &dcesrv_$name\_interface, sizeof(*iface));
263 NTSTATUS dcerpc_server_$name\_init(TALLOC_CTX *ctx)
266 static const struct dcesrv_endpoint_server ep_server = {
267 /* fill in our name */
270 /* fill in all the operations */
271 #ifdef DCESRV_INTERFACE_$uname\_INIT_SERVER
272 .init_server = DCESRV_INTERFACE_$uname\_INIT_SERVER,
274 .init_server = $name\__op_init_server,
276 .interface_by_uuid = $name\__op_interface_by_uuid,
277 .interface_by_name = $name\__op_interface_by_name
279 /* register ourselves with the DCERPC subsystem. */
280 ret = dcerpc_register_ep_server(&ep_server);
282 if (!NT_STATUS_IS_OK(ret)) {
283 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
293 #####################################################################
294 # dcerpc server boilerplate from a parsed IDL structure
295 sub ParseInterface($)
297 my($interface) = shift;
300 $res .= "NTSTATUS dcerpc_server_$interface->{NAME}\_init(TALLOC_CTX *);\n";
303 if (!defined $interface->{PROPERTIES}->{uuid}) {
307 if (!defined $interface->{PROPERTIES}->{version}) {
308 $interface->{PROPERTIES}->{version} = "0.0";
311 foreach my $fn (@{$interface->{FUNCTIONS}}) {
312 if (defined($fn->{OPNUM})) { $count++; }
319 $res .= "/* $interface->{NAME} - dcerpc server boilerplate generated by pidl */\n\n";
320 Boilerplate_Iface($interface);
321 Boilerplate_Ep_Server($interface);
328 my($ndr,$header) = @_;
331 $res .= "/* server functions auto-generated by pidl */\n";
332 $res .= "#include \"$header\"\n";
333 $res .= "#include <util/debug.h>\n";
336 foreach my $x (@{$ndr}) {
337 ParseInterface($x) if ($x->{TYPE} eq "INTERFACE" and not defined($x->{PROPERTIES}{object}));