1 ###################################################
2 # server boilerplate generator
3 # Copyright tridge@samba.org 2003
4 # Copyright metze@samba.org 2004
5 # released under the GNU GPL
19 #####################################################
20 # generate the switch statement for function dispatch
21 sub gen_dispatch_switch($)
26 foreach my $d (@{$data}) {
27 next if ($d->{TYPE} ne "FUNCTION");
29 pidl "\tcase $count: {\n";
30 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
31 pidl "\t\tif (DEBUGLEVEL > 10) {\n";
32 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_IN, r2);\n";
34 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
35 pidl "\t\tr2->out.result = $d->{NAME}(dce_call, mem_ctx, r2);\n";
37 pidl "\t\t$d->{NAME}(dce_call, mem_ctx, r2);\n";
39 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
40 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} will reply async\\n\"));\n";
42 pidl "\t\tbreak;\n\t}\n";
47 #####################################################
48 # generate the switch statement for function reply
49 sub gen_reply_switch($)
54 foreach my $d (@{$data}) {
55 next if ($d->{TYPE} ne "FUNCTION");
57 pidl "\tcase $count: {\n";
58 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
59 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
60 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} replied async\\n\"));\n";
62 pidl "\t\tif (DEBUGLEVEL > 10 && dce_call->fault_code == 0) {\n";
63 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
65 pidl "\t\tif (dce_call->fault_code != 0) {\n";
66 pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
68 pidl "\t\tbreak;\n\t}\n";
73 #####################################################################
74 # produce boilerplate code for a interface
75 sub Boilerplate_Iface($)
77 my($interface) = shift;
78 my($data) = $interface->{DATA};
79 my $name = $interface->{NAME};
81 my $uuid = util::make_str($interface->{PROPERTIES}->{uuid});
82 my $if_version = $interface->{PROPERTIES}->{version};
85 static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
87 #ifdef DCESRV_INTERFACE_$uname\_BIND
88 return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface);
94 static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
96 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
97 DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
103 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
106 uint16_t opnum = dce_call->pkt.u.request.opnum;
108 dce_call->fault_code = 0;
110 if (opnum >= dcerpc_table_$name.num_calls) {
111 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
112 return NT_STATUS_NET_WRITE_FAULT;
115 *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size);
116 NT_STATUS_HAVE_NO_MEMORY(*r);
118 /* unravel the NDR for the packet */
119 status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
120 if (!NT_STATUS_IS_OK(status)) {
121 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
122 &dce_call->pkt.u.request.stub_and_verifier);
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($data);
140 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
144 if (dce_call->fault_code != 0) {
145 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
146 &dce_call->pkt.u.request.stub_and_verifier);
147 return NT_STATUS_NET_WRITE_FAULT;
153 static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
155 uint16_t opnum = dce_call->pkt.u.request.opnum;
159 gen_reply_switch($data);
163 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
167 if (dce_call->fault_code != 0) {
168 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
169 &dce_call->pkt.u.request.stub_and_verifier);
170 return NT_STATUS_NET_WRITE_FAULT;
176 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, void *r)
179 uint16_t opnum = dce_call->pkt.u.request.opnum;
181 status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
182 if (!NT_STATUS_IS_OK(status)) {
183 dce_call->fault_code = DCERPC_FAULT_NDR;
184 return NT_STATUS_NET_WRITE_FAULT;
190 static const struct dcesrv_interface $name\_interface = {
193 .if_version = $if_version,
194 .bind = $name\__op_bind,
195 .unbind = $name\__op_unbind,
196 .ndr_pull = $name\__op_ndr_pull,
197 .dispatch = $name\__op_dispatch,
198 .reply = $name\__op_reply,
199 .ndr_push = $name\__op_ndr_push
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)
218 for (i=0;i<dcerpc_table_$name.endpoints->count;i++) {
220 const char *name = dcerpc_table_$name.endpoints->names[i];
222 ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL);
223 if (!NT_STATUS_IS_OK(ret)) {
224 DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
232 static BOOL $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
234 if ($name\_interface.if_version == if_version &&
235 strcmp($name\_interface.uuid, uuid)==0) {
236 memcpy(iface,&$name\_interface, sizeof(*iface));
243 static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
245 if (strcmp($name\_interface.name, name)==0) {
246 memcpy(iface,&$name\_interface, sizeof(*iface));
253 NTSTATUS dcerpc_server_$name\_init(void)
256 struct dcesrv_endpoint_server ep_server;
258 /* fill in our name */
259 ep_server.name = \"$name\";
261 /* fill in all the operations */
262 ep_server.init_server = $name\__op_init_server;
264 ep_server.interface_by_uuid = $name\__op_interface_by_uuid;
265 ep_server.interface_by_name = $name\__op_interface_by_name;
267 /* register ourselves with the DCERPC subsystem. */
268 ret = dcerpc_register_ep_server(&ep_server);
270 if (!NT_STATUS_IS_OK(ret)) {
271 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
281 #####################################################################
282 # dcerpc server boilerplate from a parsed IDL structure
283 sub ParseInterface($)
285 my($interface) = shift;
286 my($data) = $interface->{DATA};
291 if (!defined $interface->{PROPERTIES}->{uuid}) {
295 if (!defined $interface->{PROPERTIES}->{version}) {
296 $interface->{PROPERTIES}->{version} = "0.0";
299 foreach my $d (@{$data}) {
300 if ($d->{TYPE} eq "FUNCTION") { $count++; }
307 $res = "/* dcerpc server boilerplate generated by pidl */\n\n";
308 Boilerplate_Iface($interface);
309 Boilerplate_Ep_Server($interface);