bbb21f6e18a4a4d6c44695b978ba05800cef891c
[samba.git] / source4 / build / pidl / 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 IdlServer;
8
9 use strict;
10
11 my($res);
12
13 sub pidl($)
14 {
15         $res .= shift;
16 }
17
18
19 #####################################################
20 # generate the switch statement for function dispatch
21 sub gen_dispatch_switch($)
22 {
23         my $data = shift;
24
25         my $count = 0;
26         foreach my $d (@{$data}) {
27                 next if ($d->{TYPE} ne "FUNCTION");
28
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";
33                 pidl "\t\t}\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";
36                 } else {
37                         pidl "\t\t$d->{NAME}(dce_call, mem_ctx, r2);\n";
38                 }
39                 pidl "\t\tif (DEBUGLEVEL > 10 && dce_call->fault_code == 0) {\n";
40                 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
41                 pidl "\t\t}\n";
42                 pidl "\t\tif (dce_call->fault_code != 0) {\n";
43                 pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
44                 pidl "\t\t}\n";
45                 pidl "\t\tbreak;\n\t}\n";
46                 $count++; 
47         }
48 }
49
50
51 #####################################################################
52 # produce boilerplate code for a interface
53 sub Boilerplate_Iface($)
54 {
55         my($interface) = shift;
56         my($data) = $interface->{DATA};
57         my $name = $interface->{NAME};
58         my $uname = uc $name;
59         my $uuid = util::make_str($interface->{PROPERTIES}->{uuid});
60         my $if_version = $interface->{PROPERTIES}->{version};
61
62         pidl "
63 static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
64 {
65 #ifdef DCESRV_INTERFACE_$uname\_BIND
66         return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface);
67 #else
68         return NT_STATUS_OK;
69 #endif
70 }
71
72 static void $name\__op_unbind(struct dcesrv_connection *dce_conn, const struct dcesrv_interface *iface)
73 {
74 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
75         DCESRV_INTERFACE_$uname\_UNBIND(dce_conn,iface);
76 #else
77         return;
78 #endif
79 }
80
81 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
82 {
83         NTSTATUS status;
84         uint16 opnum = dce_call->pkt.u.request.opnum;
85
86         dce_call->fault_code = 0;
87
88         if (opnum >= dcerpc_table_$name.num_calls) {
89                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
90                 return NT_STATUS_NET_WRITE_FAULT;
91         }
92
93         *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size);
94         if (!*r) {
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         /* unravel the NDR for the packet */
99         status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
100         if (!NT_STATUS_IS_OK(status)) {
101                 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
102                                   &dce_call->pkt.u.request.stub_and_verifier);
103                 dce_call->fault_code = DCERPC_FAULT_NDR;
104                 return NT_STATUS_NET_WRITE_FAULT;
105         }
106
107         return NT_STATUS_OK;
108 }
109
110 static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
111 {
112         uint16 opnum = dce_call->pkt.u.request.opnum;
113
114         dce_call->fault_code = 0;
115
116         switch (opnum) {
117 ";
118         gen_dispatch_switch($data);
119
120 pidl "
121         default:
122                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
123                 break;
124         }
125
126         if (dce_call->fault_code != 0) {
127                 return NT_STATUS_NET_WRITE_FAULT;
128         }
129
130         return NT_STATUS_OK;
131 }
132
133 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, void *r)
134 {
135         NTSTATUS status;
136         uint16 opnum = dce_call->pkt.u.request.opnum;
137
138         status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
139         if (!NT_STATUS_IS_OK(status)) {
140                 dce_call->fault_code = DCERPC_FAULT_NDR;
141                 return NT_STATUS_NET_WRITE_FAULT;
142         }
143
144         return NT_STATUS_OK;
145 }
146
147 static const struct dcesrv_interface $name\_interface = {
148         \"$name\",
149         $uuid,
150         $if_version,
151         $name\__op_bind,
152         $name\__op_unbind,
153         $name\__op_ndr_pull,
154         $name\__op_dispatch,
155         $name\__op_ndr_push
156 };
157
158 ";
159 }
160
161 #####################################################################
162 # produce boilerplate code for an endpoint server
163 sub Boilerplate_Ep_Server($)
164 {
165         my($interface) = shift;
166         my $name = $interface->{NAME};
167         my $uname = uc $name;
168
169         pidl "
170 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
171 {
172         int i;
173
174         for (i=0;i<dcerpc_table_$name.endpoints->count;i++) {
175                 NTSTATUS ret;
176                 const char *name = dcerpc_table_$name.endpoints->names[i];
177
178                 ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL);
179                 if (!NT_STATUS_IS_OK(ret)) {
180                         DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
181                         return ret;
182                 }
183         }
184
185         return NT_STATUS_OK;
186 }
187
188 static BOOL $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32 if_version)
189 {
190         if ($name\_interface.if_version == if_version &&
191                 strcmp($name\_interface.uuid, uuid)==0) {
192                 memcpy(iface,&$name\_interface, sizeof(*iface));
193                 return True;
194         }
195
196         return False;
197 }
198
199 static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
200 {
201         if (strcmp($name\_interface.name, name)==0) {
202                 memcpy(iface,&$name\_interface, sizeof(*iface));
203                 return True;
204         }
205
206         return False;   
207 }
208         
209 NTSTATUS dcerpc_server_$name\_init(void)
210 {
211         NTSTATUS ret;
212         struct dcesrv_endpoint_server ep_server;
213
214         /* fill in our name */
215         ep_server.name = \"$name\";
216
217         /* fill in all the operations */
218         ep_server.init_server = $name\__op_init_server;
219
220         ep_server.interface_by_uuid = $name\__op_interface_by_uuid;
221         ep_server.interface_by_name = $name\__op_interface_by_name;
222
223         /* register ourselves with the DCERPC subsystem. */
224         ret = dcerpc_register_ep_server(&ep_server);
225
226         if (!NT_STATUS_IS_OK(ret)) {
227                 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
228                 return ret;
229         }
230
231         return ret;
232 }
233
234 ";
235 }
236
237 #####################################################################
238 # parse a parsed IDL structure back into an IDL file
239 sub ParseInterface($)
240 {
241         my($interface) = shift;
242         my($data) = $interface->{DATA};
243         my $count = 0;
244
245         $res = "";
246
247         if (!defined $interface->{PROPERTIES}->{uuid}) {
248                 return $res;
249         }
250
251         if (!defined $interface->{PROPERTIES}->{version}) {
252                 $interface->{PROPERTIES}->{version} = "0.0";
253         }
254
255         foreach my $d (@{$data}) {
256                 if ($d->{TYPE} eq "FUNCTION") { $count++; }
257         }
258
259         if ($count == 0) {
260                 return $res;
261         }
262
263         $res = "/* dcerpc server boilerplate generated by pidl */\n\n";
264         Boilerplate_Iface($interface);
265         Boilerplate_Ep_Server($interface);
266
267         return $res;
268 }
269
270 1;
271